C++

Initializing Multidimensional Arrays

You can initialize multidimensional arrays. You assign the list of values to array elements in order, with the last array subscript changing while each of the former holds steady. Therefore, if you have an array

int theArray[5][3]

the first three elements go into theArray[0]; the next three into theArray[1]; and so forth.

You initialize this array by writing

int theArray[5][3] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }

For the sake of clarity, you could group the initializations with braces. For example,

int theArray[5][3] = { {1,2,3},

{4,5,6},

{7,8,9},

{10,11,12},

{13,14,15} };

The compiler ignores the inner braces, which make it easier to understand how the numbers are distributed.

Each value must be separated by a comma, without regard to the braces. The entire initialization set must be within braces, and it must end with a semicolon.

Listing 11.5 creates a two-dimensional array. The first dimension is the set of numbers from 0 to 5. The second dimension consists of the double of each value in the first dimension.

Listing 11.5. Creating a multidimensional array.

1: #include <iostream.h>

2: int main()

3: {

4: int SomeArray[5][2] = { {0,0}, {1,2}, {2,4}, {3,6}, {4,8}};

5: for (int i = 0; i<5; i++)

6: for (int j=0; j<2; j++)

7: {

8: cout << "SomeArray[" << i << "][" << j << "]: ";

9: cout << SomeArray[i][j]<< endl;

10: }

11:

12: return 0;

13: }

OUTPUT:

SomeArray[0][0]: 0

SomeArray[0][1]: 0

SomeArray[1][0]: 1

SomeArray[1][1]: 2

SomeArray[2][0]: 2

SomeArray[2][1]: 4

SomeArray[3][0]: 3

SomeArray[3][1]: 6

SomeArray[4][0]: 4

SomeArray[4][1]: 8

ANALYSIS: Line 4 declares SomeArray to be a two-dimensional array. The first dimension consists of five integers; the second dimension consists of two integers. This creates a 5x2 grid, as Figure 11.4 shows.

11.4.JPG (6089 bytes)
Figure 11.4.
A 5x2 array.

The values are initialized in pairs, although they could be computed as well. Lines 5 and 6 create a nested for loop. The outer for loop ticks through each member of the first dimension. For every member in that dimension, the inner for loop ticks through each member of the second dimension. This is consistent with the printout. SomeArray[0][0] is followed by SomeArray[0][1]. The first dimension is incremented only after the second dimension is incremented by 1. Then the second dimension starts over.

A Word About Memory

When you declare an array, you tell the compiler exactly how many objects you expect to store in it. The compiler sets aside memory for all the objects, even if you never use it. This isn’t a problem with arrays for which you have a good idea of how many objects you’ll need. For example, a chess board has 64 squares, and cats have between 1 and 10 kittens. When you have no idea of how many objects you’ll need, however, you must use more advanced data structures.

This book looks at arrays of pointers, arrays built on the free store, and various other collections. Other more advanced data structures that solve large data storage problems are beyond the scope of this book. Two of the great things about programming are that there are always more things to learn and that there are always more books from which to learn.

Back to Index