Thursday, November 24, 2011

Learn C# Programming Tutorial Lesson 5 - Arrays


Imagine that you want to enter 10 numbers and calculate their average. You would need to declare 10 variables to do this. That is a lot of variables to have to declare and you still have to read the values in one at a time for each one. An easier way of doing this is to use an array. An array is many variables grouped together under one name.
To declare an array you must first have the data type followed immediately by empty square brackets. After that you put the array's name. Here is how to declare an array of integers called arr.
int[] arr;
This is not an array yet but just a reference to an array. You must set the number of dimensions using the new keyword followed by the data type again with square brackets after it that contain the number of elements you want in the array. The following example creates an array with 3 elements.
int[] arr = new int[3];

The following table will help you imagine what an array looks like.
Element NumberValue
05
112
27
You must use square brackets after the name of the array to access its values. Inside the square brackets you put the number of the element you want to access. The first array element is 0 and not 1 as you would think and the last element is 1 less than the size of the array. Here is an example of how to set all 3 values of an array and how to print them.
arr[0] = 5;
arr[1] = 12;
arr[2] = 7;
Console.WriteLine(arr[0]);
Console.WriteLine(arr[1]);
Console.WriteLine(arr[2]);

Arrays with loops

It is much easier to work with an array when you use a loop. Here is an example of how to print all the values in an array of 10 elements using a for loop.
int[] arr = new int[10];
for (int x = 0; x < 10; x++)
   Console.WriteLine(arr[x]);

You can go through all the elements in an array without having to specify how many of them there are by using a foreach loop. In the following example the y is given the value of each element of the array for each iteration of the loop.
foreach (int y in arr)
   Console.WriteLine(y);

Multi-dimensional arrays

A multi-dimensional array is an array that has elements in more than one dimension. A 2-dimensional can be thought of as having both an x and a y dimension. You do get arrays with more than 2 dimensions but they are not used very often. Here is a table that will help you imagine what a 2-dimensional array looks like.

012
0123
1456
2789
When you declare a 2-dimensional array you must separate the dimensions with a comma. Here is an example of how you would declare a 2-dimensional array with 3 elements as the size of each dimension.
int[,] arr2 = new int[3,3];
You also use a comma when setting the value.
arr2[1,2] = 5;
2 loops are needed when working with a 2-dimensional array. The one loop must be put inside the other. Here is an example of how to print all the values of a 2-dimensional array.
for (int i = 0; i < 3; i++)
   for (int j = 0; j < 3; j++)
      Console.WriteLine(arr2[i,j]);

1 comments:

Unknown said...

Nice post very helpful

dbakings

Post a Comment

Setec Institute || Group : SH6 | | Name : Kim Chanthy. Powered by Blogger.

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hot Sonakshi Sinha, Car Price in India