Free Hosting

Multidimensional Arrays


The elements of an array can be of any data type, including arrays! An array of arrays is called a multidimensional array.
1
int anArray[3][5]; // a 3-element array of 5-element arrays
In this case, since we have 2 subscripts, this is a two-dimensional array. In a two-dimensional array, it is convenient to think of the first subscript as being the row, and the 2nd subscript as being the column. Conceptually, the above two-dimensional array is laid out as follows:
[0][0]  [0][1]  [0][2]  [0][3]  [0][4]
[1][0]  [1][1]  [1][2]  [1][3]  [1][4]
[2][0]  [2][1]  [2][2]  [2][3]  [2][4]
To access the elements of a two-dimensional array, simply use two subscripts:
1
anArray[2][3] = 7;
To initialize a two-dimensional array, it is easiest to use nested braces, with each set of numbers representing a row:
1
2
3
4
5
6
int anArray[3][5] =
{
{ 1, 2, 3, 4, 5, }, // row 0
{ 6, 7, 8, 9, 10, }, // row 1
{ 11, 12, 13, 14, 15 } // row 2
};
When the C++ compiler processes this list, it actually ignores the inner braces altogether. However, we highly recommend you use them anyway for readability purposes.
Two-dimensional arrays with initializer lists can omit (only) the first size specification:
1
2
3
4
5
6
int anArray[][5] =
{
{ 1, 2, 3, 4, 5, },
{ 6, 7, 8, 9, 10, },
{ 11, 12, 13, 14, 15 }
};
The compiler can do the math to figure out what the array size is. However, the following is not allowed:
1
2
3
4
5
int anArray[][] =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 }
};
Because the inner parenthesis are ignored, the compiler can not tell whether you intend to declare a 1×8, 2×4, 4×2, or 8×1 array in this case.
Just like normal arrays, multidimensional arrays can still be initialized to 0 as follows:
1
int anArray[3][5] = { 0 };
Note that this only works if you explicitly declare the size of the array! Otherwise, you will get a two-dimensional array with 1 row.
Accessing all of the elements of a two-dimensional array requires two loops: one for the row, and one for the column. Since two-dimensional arrays are typically accessed row by row, generally the row index is used as the outer loop.
1
2
3
for (int nRow = 0; nRow < nNumRows; nRow++)
    for (int nCol = 0; nCol < nNumCols; nCol++)
        cout << anArray[nRow][nCol];
Multidimensional arrays may be larger than two dimensions. Here is a declaration of a three-dimensional array:
1
int anArray[5][4][3];
Three-dimensional arrays are hard to initialize in any kind of intuitive way using initializer lists, so it’s typically better to initialize the array to 0 and explicitly assign values using nested loops.
Let’s take a look at a practical example of a two-dimensional array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Declare a 10x10 array
const int nNumRows = 10;
const int nNumCols = 10;
int nProduct[nNumRows ][nNumCols ] = { 0 };
 
// Calculate a multiplication table
for (int nRow = 0; nRow < nNumRows; nRow++)
    for (int nCol = 0; nCol < nNumCols; nCol++)
        nProduct[nRow][nCol] = nRow * nCol;
 
// Print the table
for (int nRow = 1; nRow < nNumRows; nRow++)
{
    for (int nCol = 1; nCol < nNumCols; nCol++)
        cout << nProduct[nRow][nCol] << "\t";
 
    cout << endl;
}
This program calculates and prints a multiplication table for all values between 1 and 9 (inclusive). Note that when printing the table, the for loops start from 1 instead of 0. This is to omit printing the 0 column and 0 row, which would just be a bunch of 0s! Here is the output:
1    2    3    4    5    6    7    8    9
2    4    6    8    10   12   14   16   18
3    6    9    12   15   18   21   24   27
4    8    12   16   20   24   28   32   36
5    10   15   20   25   30   35   40   45
6    12   18   24   30   36   42   48   54
7    14   21   28   35   42   49   56   63
8    16   24   32   40   48   56   64   72
9    18   27   36   45   54   63   72   81

0 comments:

Blogger Template by Clairvo