There will be situations where a table of values will have to be stored. Consider a two-dimensional matrix in mathematics.
Each element of the matrix is identified by its row and column names. The notation Aij refers to the element in the ith row and jth column. Similarly, A12 refers to the element in the first row and second column of the matrix. One of the natural applications for two dimensional array arises in the case of matrices. In C, there is a similar notation that is used when referring to elements of a multidimensional array. The equivalent notation of a two dimensional matrix is A [i] [j]. The array elements are represented in the memory as given below
column0 column1
[0][0] [0][1]
Row0 2 5
[1][0] [1][1]
Row1 3 8
Since multidimensional arrays are stored in memory as vectors, the array Arr_var [2] [3] is stored in memory in the following sequence
Arr_var [0][0]
Arr_var [0][1]
Arr_var [0][2]
Arr_var [1][0]
Arr_var [1][1]
Arr_var [1][2]
The general form of a multidimensional array is
type array_name[s1 ][s2][s3]...... [sm];
For example, in the following declarations
int A[3] [3];
float table [5] [4] [3] [5];
A is a two-dimensional array declared to contain 9 elements (3 rows and 3 columns and therefore, 3 * 3=9 elements) and table is a four dimensional array declared to contain 300 elements (5 *4 *3*5= 300 elements) of floating point type.
Initializing multidimensional arrays
Multidimensional arrays are initialized in a similar fashion like single dimensional arrays. The initializers are listed by rows. Brace pairs are used to separate the list of initializers for one row from the next, and commas placed after each brace. Consider a two dimensional array marks [5] [4].
| 0 | 1 | 2 | 3 | |
| 0 | 45 | 56 | 77 | 89 |
| 1 | 87 | 86 | 56 | 61 |
| 2 | 86 | 78 | 89 | 90 |
| 3 | 91 | 89 | 80 | 81 |
| 4 | 87 | 88 | 67 | 78 |
Thus, to define and initialize the mentioned in two-dimensional array marks [5] [4] to the values as the table. the declaration should be done in the following manner
int marks [5] [4] = {
(45, 56,77,89),
(87,86, 56, 61),
(86,78, 89,90),
(91,89,80, 81),
(87,88,67,78)
}
Similarly, a three dimensional array can be initialized as
int score[4] [2] [3] = {
{ {1,2,3}, {3,4,4} },
{ {5,5,3}, {6,8,9} },
{ {9,8,6}, {1,4,5} },
{ {3,5,6}, {8,4,9} }
};
Note