Displays all elements of a 3-by-4 two-dimensional array - C++ Data Type

C++ examples for Data Type:Array

Description

Displays all elements of a 3-by-4 two-dimensional array

Demo Code

#include <iostream>
#include <iomanip>
using namespace std;
int main()/*from   w ww  .ja  va  2s .c  o  m*/
{
   const int NUMROWS = 3;
   const int NUMCOLS = 4;
   int i, j;
   int val[NUMROWS][NUMCOLS] = {8,1,9,2,3,1,7,6,4,5,2,0};
   cout << "\nDisplay of val array by explicit element"
   << endl << setw(4) << val[0][0] << setw(4) << val[0][1]
   << setw(4) << val[0][2] << setw(4) << val[0][3]
   << endl << setw(4) << val[1][0] << setw(4) << val[1][1]
   << setw(4) << val[1][2] << setw(4) << val[1][3]
   << endl << setw(4) << val[2][0] << setw(4) << val[2][1]
   << setw(4) << val[2][2] << setw(4) << val[2][3];
   cout << "\n\nDisplay of val array using a nested for loop";

   for (i = 0; i < NUMROWS; i++)
   {
      cout << endl;    // print a new line for each row
      for (j = 0; j < NUMCOLS; j++) {
         cout << setw(4) << val[i][j];
      }
   }
   cout << endl;
   return 0;
}

Result


Related Tutorials