Managing Arrays and Strings 419
13
This, however, doesn’t correspond as closely to the real-world object as the two-dimen-
sion. When the game begins, the king is located in the fourth position in the first row;
that position corresponds to
Board[0][3];
assuming that the first subscript corresponds to row and the second to column.
Initializing Multidimensional Arrays ............................................................
You can initialize multidimensional arrays. You assign the list of values to array elements
in order, with the last array subscript (the one farthest to the right) 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, but they do make it easier to understand how the
numbers are distributed.
When initializing elements of an array, each value must be separated by a comma, with-
out regard to the braces. The entire initialization set must be within braces, and it must
end with a semicolon.
Listing 13.5 creates a two-dimensional array. The first dimension is the set of numbers
from zero to four. The second dimension consists of the double of each value in the first
dimension.
LISTING13.5 Creating a Multidimensional Array
0: // Listing 13.5 - Creating a Multidimensional Array
1: #include <iostream>
2: using namespace std;
3:
4: int main()