Sunday, December 21, 2008

2D,3D Array

Two dimension array-
The two dimension array is similar to single dimension arrays in some aspects. The representation is different. More number can be stored in this array.
Representation- x 1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
In this array there are five rows and five columns. Hence it is declared as x[5][5]. The numbers are stored in order as x[0][0],x[0][1]…..x[0][4]. Then next column x[1][0]…..this continuous upto x[4][4]. This is because the index starts from zero.
Eg.
Void main()
{
Int x[2][2],y,z;
Printf(“Enter 4 numbers”);
For(y=0;y<2;y++)
{
For(z=0;z<2;z++)
{
Scanf(“%d”,&x[y][z]);
}
}
Getch();
}
In the above program first the first for loop is fired. It takes the value of y as 0. Then the control enters the second for loop. The second for loop is executed 2 times upto z<2. Then it exits. The control again goes to the first loop. It increments the value of y. then again the second for loop is called. This continues till the first for loop exits.
Multidimensional array –
In multidimensional array everything is same as that of the two dimensional array but there are many two and one dimensional array in it.
Eg.x[2][2][2][2]

No comments:

Watch tv!