7.8. Multidimensional arrays

You can use arrays with two dimensions (i.e., subscripts) to represent tables of values consisting of information arranged in rows and columns. To identify a particular table element, we must specify two subscripts—by convention, the first identifies the element’s row and the second identifies the element’s column. arrays that require two subscripts to identify a particular element are called two-dimensional arrays or 2-D arrays. arrays with two or more dimensions are known as multidimensional arrays and can have more than two dimensions. Figure 7.19 illustrates a two-dimensional array, a. The array contains three rows and four columns, so it’s said to be a 3-by-4 array. In general, an array with m rows and n columns is called an m-by-n array.

Image

Fig. 7.19. Two-dimensional array with three rows and four columns.

Every element in array a is identified in Fig. 7.19 by an element name of the form a[i][j], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a. Notice that the names of the elements in row 0 all have a first subscript of 0; the names of the elements in column 3 all have a second subscript of 3.


Image Common Programming Error 7.4

Referencing a two-dimensional array element a[x][y] incorrectly as a[x, y] is an error. Actually, a[x, y] is treated as a[y], because C++ evaluates the expression x, y (containing a comma operator) simply as y (the last of the comma-separated expressions).


Figure 7.20 demonstrates initializing two-dimensional arrays in declarations. Lines 13–14 each declare an arrays of arrays with two rows and three columns. Notice the nested array type declaration. In each array, the type of its elements is specified as

array< int, columns >

indicating that each array contains as its elements three-element arrays of int values—the constant columns has the value 3.


 1   // Fig. 7.20: fig07_20.cpp
 2   // Initializing multidimensional arrays.
 3   #include <iostream>
 4   #include <array>
 5   using namespace std;
 6
 7   const size_t rows = 2;
 8   const size_t columns = 3;
 9   void printArray( const array< array< int, columns >, rows> & );
10
11   int main()
12   {
13      array< array< int, columns >, rows > array1 = { 1, 2, 3, 4, 5, 6 };
14      array< array< int, columns >, rows > array2 = { 1, 2, 3, 4, 5 };   
15
16      cout << "Values in array1 by row are:" << endl;
17      printArray( array1 );
18
19      cout << " Values in array2 by row are:" << endl;
20      printArray( array2 );
21   } // end main
22
23   // output array with two rows and three columns                 
24   void printArray( const array< array< int, columns >, rows> & a )
25   {                                                               
26      // loop through array's rows                                 
27      for ( auto const &row : a )                                  
28      {                                                            
29         // loop through columns of current row                    
30         for ( auto const &element : row )                         
31            cout << element << ' ';                                
32                                                                   
33         cout << endl; // start new line of output                 
34      } // end outer for                                           
35   } // end function printArray                                    


Values in array1 by row are:
1 2 3
4 5 6

Values in array2 by row are:
1 2 3
4 5 0


Fig. 7.20. Initializing multidimensional arrays.

The declaration of array1 (line 13) provides six initializers. The compiler initializes the elements of row 0 followed by the elements of row 1. So, the first three values initialize row 0’s elements to 1, 2 and 3, and the last three initialize row 1’s elements to 4, 5 and 6. The declaration of array2 (line 14) provides only five initializers. The initializers are assigned to row 0, then row 1. Any elements that do not have an explicit initializer are initialized to zero, so array2[1][2] is 0.

The program calls function printArray to output each array’s elements. Notice that the function prototype (line 9) and definition (lines 24–35) specify that the function receives a two row and three column array. The parameter receives the array by reference and is declared const because the function does not modify the array’s elements.

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

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