Multi-Dimensional Array
Multidimensional array are defined in the same manner as one dimensional arrays, except that a seperate pair of square brackets are required for each subscript (index).
Two-dimensional arrays are as a grid. A two dimensional array will require two pairs of square brackets and a three dimensional array will require three pairs of brackets and so on.
The general format of the multidimensional array is:
Example
The first line of the example defines the value as a integer type array having 20 rows, 30 columns with total of 600 elements. The second one is a three dimensional floating point array whose maximum size is 1000 elements.
In two dimensional array the upper left corner of any grid would be [0][0]. The element to its right would be [0][1], so on. Here is a little illustration to help.
[0][0] | [0][1] | [0][2] |
[1][0] | [1][1] | [1][2] |
[2][0] | [2][1] | [2][2] |
Initialization
Similar to one dimensional array, multidimensional arrays can also be initialized.
where a is a two dimensional array of integer numbers whose maximum size is 4 and its assignment would be
a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 3;
a[1][1] = 4;
A two-dimensional array is a grid, and a three dimensional array is as a cube. The above declaration would give you 27 integers (3 x 3 x 3).
Just like other arrays, it can be initialize like this
4, 5, 6,
7, 8, 9,
10, 11, 12,
13, 14, 15,
16, 17, 18,
19, 20, 21,
22, 23, 24,
25, 26, 27, }
Accessing Elements Of Two-Dimensional Array
Elements of two-dimensional arrrays can be accessed as follows:
Enter the array elements
4 6 8
7 2 1
1 1 3
The elements of the array are
4 6 8
7 2 1
1 1 3