Initializing multidimensional arrays : multi dimension array « Array « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

void display( const int [][ 3 ] );

int main()
{
   int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
   int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };
   int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };

   cout << "Values in array1 by row are:" << endl;
   display( array1 );

   cout << "\nValues in array2 by row are:" << endl;
   display( array2 );

   cout << "\nValues in array3 by row are:" << endl;
   display( array3 );
   return 0; 
}

void display( const int a[][ 3 ] )
{
   for ( int i = 0; i < 2; i++ )
   {    
      for ( int j = 0; j < 3; j++ ){
         cout << a[ i ][ j ] << ' ';
      }
      cout << endl;
   }
}
Values in array1 by row are:
1 2 3
4 5 6

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

Values in array3 by row are:
1 2 0
4 0 0








4.4.multi dimension array
4.4.1.Declare a two-dimension array
4.4.2.Initialize a two-dimension array
4.4.3.Creating A Multidimensional Array
4.4.4.Initializing multidimensional arrays
4.4.5.Search a two-dimension array
4.4.6.Use nested for loop to display the two dimesional array
4.4.7.Using pointer notation with a multidimensional array
4.4.8.Fill a two dimensional integer array element by element
4.4.9.how to define, pass, and walk through the different dimensions of an array