Multidimensional Arrays

An array can be thought of as a row of data. You can imagine a grid of data, with rows and columns. This is a two-dimensional array of data, with one dimension representing each row and the second dimension representing each column. A three-dimensional array would be a cube, with one dimension representing width, a second dimension representing height, and a third dimension representing depth. You can even have arrays of more than three dimensions, though they are harder to imagine as objects in space.

When you declare arrays, each dimension is represented as a subscript in the array. Therefore, a two-dimensional array has two subscripts, a three-dimensional array has three subscripts, and so on. Arrays can have any number of dimensions, although it is likely that most of the arrays you create will have one or two dimensions.

A good example of a two-dimensional array is a chessboard. One dimension represents the eight rows; the other dimension represents the eight columns. Figure 15.3 illustrates this idea.

Figure 15.3. A chessboard and a two-dimensional array.


Suppose that you have a class named SQUARE. The declaration of an array named Board that represents it would be

SQUARE Board[8][8];

You could also represent the same data with a one-dimensional, 64-square array. For example:

SQUARE Board[64]

This doesn't correspond as closely to the real-world object as the two-dimensional array, however. When the game begins, the king is located in the fourth position in the first row. Counting from zero, that position corresponds to

Board[0][3];

assuming that the first subscript corresponds to row, and the second to column. The layout of positions for the entire board is illustrated in Figure 15.3.

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 and each of the former ones holding 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 just make it easier for the programmer 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 15.3 creates a two-dimensional array. The first dimension is the set of numbers from 0 to 4. However, unlike the previous example, the values are set so the second dimension consists of the double of each value in the first dimension.

Listing 15.3. Creating a Multidimensional Array
 0:  // Listing 15.3 - Creating A Multidimensional Array
 1:  #include <iostream>
 2:
 3:  int main()
 4:  {
 5:      int SomeArray[5][2] = { {0,0}, {1,2}, {2,4}, {3,6}, {4,8}};
 6:      for (int i = 0; i<5; i++)
 7:          for (int j=0; j<2; j++)
 8:          {
 9:              std::cout << "SomeArray[" << i << "][" << j << "]: ";
10:              std::cout << SomeArray[i][j]<< std::endl;
11:          }
12:      return 0;
13:  }


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
						

Line 5 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 5×2 grid, as Figure 15.4 shows.


Figure 15.4. A 5×2 array.


The values are initialized in pairs, although they could be computed as well. Lines 6 and 7 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.34.223