D Programming – Arrays

One of the concepts I love is arrays. I really like putting values inside one container. Feels like cheating and magic at the same time. It can store values and even variables, it can also be one-dimensional or multi-dimensional, and it just solves many problems.

Below is the code for declaring and accessing arrays:

import std.stdio;

int main(){
	int numbers[4] = [5,1,3,10];
	int sum = 0;
	
	for(int i = 0; i<numbers.length; i++){
		sum+= numbers[i];
	}
	
	writeln("The sum is ", sum);
	
	return 0;
}

Here is the output:

 

d-arrays

 

I hope you like this post.

Don’t forget to leave a comment.

Thank you very much.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s