Examples for Arrays

The following C++ code will read 10 integers from the keyboard and will write them in reverse order:

Example (1):
#include<iostream.h>

int main()
{
      int i = 0;
      int a[10];

      for( i = 0; i < 10; i++){
           cout << "Enter another number \n ";
           cin >> a[i];
       }

       cout << "Your numbers in reverse order are: \n";
       for( i = 9; i >= 0; i--){
            cout << a[i] << "  ";
       }
       cout << "\n";
       return 0;
}

Example (2):
The following C++ code creates a 2 dimensional array of 4 rows and 4 columns and read 4 rows and 4 columns (16 values) from the keyboard and will save the values in the 2 deimensional array.

#include<iostream.h>

int main()
{
      int index_x, index_y;
      int a[4][4];

      for( index_x = 0; index_x < 4; index_x++){
          for( index_y = 0; index_y < 4; index_y++){
             cout << "Enter another number \n ";
             cin >> a[index_x][index_y];
         }
      }

      cout << "Your numbers were: \n";
      for( index_x = 0; index_x < 4; index_x++){
          for( index_y = 0; index_y < 4; index_y++){
             cout << "a[" << index_x << "][" << index_y << "]= " << a[index_x][index_y] << "  ";
         }
         cout << "\n";
      }

      cout << "Let's write the rows \n";
          for( index_x = 0; index_x < 4; index_x++){
               cout << << "row ( " << index_x << " ): " ;
               for( index_y = 0; index_y < 4; index_y++){
                    cout << a[index_x][index_y] << " ";
         }
         cout << "\n";
      }
      cout << "\n";

      cout << Let's write the columns \n";
           for( index_y = 0; index_y < 4; index_y++){
                 cout << "column ( " << index_y << " ): " ;
                 for( index_x = 0; index_x < 4; index_x++){
                     cout << a[index_x][index_y] << " ";
         }
         cout << "\n";
      }
 

       return 0;
}