Multidimensional Arrays

An array that has more than one subscrpt is known as a multidimensional array. Multidimensional arrays can be visualizes as arrays  of arrays. For example, a two dimensional array can be imagined as a two-dimensional table of a uniform concrete data type as shown below: 
                              0              1                 2                 3                 4

   0

 

 

 

 

 

   1

 

 

 

 

 

   2 

 

 

 

 

 

A two-dimensional array is the simplest form of a multidimensional array. In a two-dimensional array, two subscripts are enclosed in square brackets. The rirst subscript designates the row and the second subscript designates the column. A two-dimensional array is used for table processing or matrix manipulation.
Array Declaration
The general form of a two-dimensional array declaration in C++ is:
      type array-name[rows][coloumns];
where
      type is base data type of the array
      array-name is name of the array
      rows is first index, denotes number of rows
      columns is second index, denotes number of columns
For example mat represents a two-dimensional array of 3 by 5 values of type int. This array can be declared as:        int mat[3][5];
 

                                   0                     1                     2                     3                     4

   0

 

 

 

 

 

   1

 

 

 

mat[1][3] 

 

   2 

 

 

 

 

 

Array indicies always begin with 0.
The two dimensional array
int mat[3][5]; is equvivalent to
int mat[15]; //(3*5=15)
The only difference being that the compiler reminds us of the depth of each imaginary dimension.
The program
    #include<iostream.h>
    main()
    {
        int i,j;
        int mat[3][5];
        for(i=0;i<3;i++)
            for(j=0;j<5;j++)
                    cin>>mat[i][j];
        for(i=0;i<3;i++)
            for(j=0;j<5;j++)
                    cin>>mat[i][j];
}
may give an output of this kind:
                                  0                       1                  2                    3                        4    

   0

     2        

        4

      6

     8 

    10 

   1

     1 

       2

      3

     4 

     5

   2 

    3

       6

      9 

   12 

   15 

 
 

      

           HOME            LEARN C++                PREVIOUS                NEXT