Multidimensional arrays are often known as an array of arrays. In multidimensional arrays, the array is divided into rows and columns, mainly while considering multidimensional arrays we will be discussing mainly about two-dimensional arrays and a bit about three-dimensional arrays.
Syntax: data_type array_name[size1][size2][size3]------[sizeN];
In 2-D array we can declare an array as :
int arr[3][3] = { 1, 2, 3,
4, 5, 6,
7, 8, 9
};
where the first index value shows the number of the rows and the second index value shows the number of the columns in the array. To access the various elements in a 2-D array we can use:
printf("%d", a[2][3]);
/* output will be 6, as a[2][3] means the third element of the second row of the array */
In 3-D we can declare the array in the following manner :
int arr[3][3][3] =
{ 1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12,
13, 14, 15,
16, 17, 18,
19, 20, 21,
22, 23, 24,
25, 26, 27
};
/* here we have divided array into the grid for sake of convenience as in above declaration we have created 3 different grids, each has rows and columns */
If we want to access the element in a 3-D array we can do it as follows :
printf("%d",a[2][2][2]);
/* its output will be 26, as a[2][2][2] means first value in [ ] corresponds to the grid no. i.e. 3 and the second value in [ ] means the third row in the corresponding grid and last [ ] means the third column */
Ex:- int arr[3][5][12];
float table[5][4][5][3];
arr is a 3D array declared to contain 180 (3*5*12) int type elements. Similarly, the table is a 4D array containing 300 elements of float type.
Recommended Questions
Useful Files
Users Joined
Multidimensional arrays are often known as an array of arrays. In multidimensional arrays, the array is divided into rows and columns, mainly while considering multidimensional arrays we will be discussing mainly about two-dimensional arrays and a bit about three-dimensional arrays.
Syntax: data_type array_name[size1][size2][size3]------[sizeN];
In 2-D array we can declare an array as :
int arr[3][3] = { 1, 2, 3,
4, 5, 6,
7, 8, 9
};
where the first index value shows the number of the rows and the second index value shows the number of the columns in the array. To access the various elements in a 2-D array we can use:
printf("%d", a[2][3]);
/* output will be 6, as a[2][3] means the third element of the second row of the array */
In 3-D we can declare the array in the following manner :
int arr[3][3][3] =
{ 1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12,
13, 14, 15,
16, 17, 18,
19, 20, 21,
22, 23, 24,
25, 26, 27
};
/* here we have divided array into the grid for sake of convenience as in above declaration we have created 3 different grids, each has rows and columns */
If we want to access the element in a 3-D array we can do it as follows :
printf("%d",a[2][2][2]);
/* its output will be 26, as a[2][2][2] means first value in [ ] corresponds to the grid no. i.e. 3 and the second value in [ ] means the third row in the corresponding grid and last [ ] means the third column */
Ex:- int arr[3][5][12];
float table[5][4][5][3];
arr is a 3D array declared to contain 180 (3*5*12) int type elements. Similarly, the table is a 4D array containing 300 elements of float type.