how to define, pass, and walk through the different dimensions of an array : multi dimension array « Array « C++ Tutorial






#include <iostream>
using namespace std;

void vdisplay_results(char carray[][3][4]);
  
  char cglobal_cube[5][4][5]= {
                {
                  {'1','1','1','1','1'},
                  {'2','2','2','2',' '},
                  {'3','3','3','3',' '},
                  {'4','4','4',' ',' '},
                },
                {
                  {'1','1','1','1','1'},
                  {'2','2','2','2',' '},
                  {'3','3','3','3',' '}
                },
                {
                  {'1','1','1','1','1'},
                  {'2','2','2','2',' '}
                },
                {
                  {'1','1','1','1','1'},
                  {'2','2','2','2',' '},
                  {'3','3','3','3',' '},
                  {'4','4','4',' ',' '},
                },
                {
                  {'1','1','1','1','1'},
                  {'2','2','2','2',' '},
                  {'3','3','3','3',' '},
                  {'4','4','4',' ',' '},
                }
};

int imatrix[4][3]={ {1},{2},{3},{4} };
  
int main( )
{
 int irow_index, icolumn_index;
 char clocal_cube[2][3][4];

 cout << "sizeof clocal_cube         = "<< sizeof(clocal_cube)           << "\n";
 cout << "sizeof clocal_cube[0]      = "<< sizeof(clocal_cube[0])        << "\n";
 cout << "sizeof clocal_cube[0][0]   = "<< sizeof(clocal_cube[0][0])     << "\n";
 cout << "sizeof clocal_cube[0][0][0]= "<< sizeof(clocal_cube[0][0][0])  << "\n";

 vdisplay_results(clocal_cube);

 cout << "cglobal_cube[0][1][2] is     = " << cglobal_cube[0][1][2] << "\n";
 cout << "cglobal_cube[1][0][2] is     = " << cglobal_cube[1][0][2] << "\n";

 for(irow_index=0; irow_index < 4; irow_index++) {
   for(icolumn_index=0; icolumn_index < 5; icolumn_index++)
     cout << cglobal_cube[0][irow_index][icolumn_index];
   cout << "\n";
 }


 for(irow_index=0; irow_index < 4; irow_index++) {
   for(icolumn_index=0; icolumn_index < 5; icolumn_index++)
     cout << cglobal_cube[4][irow_index][icolumn_index];
   cout << "\n";
 }

 cout << "\nprint all of imatrix\n";
 for(irow_index=0; irow_index < 4; irow_index++) {
   for(icolumn_index=0; icolumn_index < 3; icolumn_index++)
     cout << imatrix[irow_index][icolumn_index];
   cout << "\n";
 }

 return (0);
}
  
void vdisplay_results(char carray[][3][4])
{
    cout << "sizeof carray           =" << sizeof(carray) << "\n";
    cout << " sizeof  carray[0]      =" << sizeof(carray[0]) << "\n";
    cout << " sizeof  cglobal_cube   =" << sizeof(cglobal_cube) << "\n";
    cout << " sizeof cglobal_cube[0] =" << sizeof(cglobal_cube[0])
                                        << "\n";
}








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