Declaration and referring an array element | Initialising the elements of an array in the array declaration |
The following is an example of multi-dimensional array declarations:
C++ version | Equivalent Turbo Pascal version |
int a[5][3], b [2][3][4]; | var a: array[0..4, 0..2] of integer; b: array[0..1, 0..2, 0..3] of integer; |
Note that the above declaration cannot be written as "int a[5, 8], b[2, 3, 4]".
For the declaration "int a[5][3]", the two-dimensional arrays a can be visualised as follows:
a |
column |
|||
[0] |
[1] |
[2] |
||
row |
[0] |
a[0][0] |
a[0][1] |
a[0][2] |
[1] |
a[1][0] |
a[1][1] |
a[1][2] |
|
[2] | a[2][0] |
a[2][1] |
a[2][2] |
|
[3] |
a[3][0] |
a[3][1] |
a[3][2] |
|
[4] |
a[4][0] |
a[4][1] |
a[4][2] |
A two-dimensional array can be considered as an array of arrays. Each row can be considered as a one-dimensional array. When the above two-dimensional array is stored in the memory, the first element is a[0][0], then a[0][1], next a[0][2], a[1][0], ..., so on.
Note that each subscript must be enclosed by one pair of square brackets.
Although three-dimensional arrays, four-dimensional arrays, etc., cannot be visualised here, the way of referring to elements is similar to referring elements in a two-dimensional array.
The following example shows how multi-dimensional arrays can be initialised.
int n[2][3] = {{14, 23, 7}, {10, 5, 8}}; |
|
The elements of an array must be initialised separately if the initialisation is not done in the array declaration.
If the number of elements in the array is more than the number of the initialisers, the remaining elements are automatically initialised to zero. For example:
int n[2][3] = {{14, 23}, {10}}; |
|
If an array is not initialised in array declaration, the values stored in the array are unknown until initialisation of the elements are done explicitly.
There will be a compile-time error if the number of initialisers is more than the number of the elements in the array.
The size of a multi-dimensional array should not be omitted in the declaration (except for the first level index). If the size of the first level index is missing, the array should be initialised (using an initialiser list) during declaration. The size of the first level index will be determined by the initialiser list. For example:
C++ statement | Remark |
int a[][2] = {{3, 4}, {7}, {2, 3}}; | This is a valid declaration. The size of the first level index 3. |
int a[][4]; | This is invalid because there is not initialised before. |
int a[3][] = {{4, 74}, {3, 2}, {1, 3}} | This is invalid because the size of the second level indexing missing. |
int a[][] = {{4, 7}, {3, 2}, {1,3}} | Same as above. |
The following program makes use of a two-dimensional array:
C++ version | Pascal version |
#include <iostream.h> main() { int table[10][10], i, j; for (i = 0; i < 10; i++) for (j = 0; j < 10; j++) table[i][j] = i * j; cout << " |"; for (i = 0; i < 10; i++) cout << " " << i << "|"; cout << endl; for (i = 1; i <= 11; i++) cout << "--+"; cout << endl; for (i = 0; i < 10; i++) { cout << " " << i << "|"; for (j = 0; j < 10; j++) { if (table[i][j] < 10) cout << " "; cout << table[i][j] << " "; } cout << endl; } return 0; } |
program prog_26(input, output); var table: array[0..9, 0..9] of integer; i, j: integer; begin for i := 0 to 9 do for j := 0 to 9 do table[i, j] := i * j; write(' |'); for i := 0 to 9 do write(' ', i, '|'); writeln; for i := 1 to 11 do write('--+'); writeln; for i := 0 to 9 do begin write(' ', i, '|'); for j := 0 to 9 do begin if table[i, j] < 10 then write(' '); write(table[i, j], ' ') end; writeln end end. |
Sample output: | |
| 0| 1| 2| 3| 4| 5| 6| 7|
8| 9| --+--+--+--+--+--+--+--+--+--+--+ 0| 0 0 0 0 0 0 0 0 0 0 1| 0 1 2 3 4 5 6 7 8 9 2| 0 2 4 6 8 10 12 14 16 18 3| 0 3 6 9 12 15 18 21 24 27 4| 0 4 8 12 16 20 24 28 32 36 5| 0 5 10 15 20 25 30 35 40 45 6| 0 6 12 18 24 30 36 42 48 54 7| 0 7 14 21 28 35 42 49 56 63 8| 0 8 16 24 32 40 48 56 64 72 9| 0 9 18 27 36 45 54 63 72 81 |