Skip to main content
Engineering LibreTexts

9.5: Multidimensional Arrays

  • Page ID
    53883
  • In C++, we can define multidimensional arrays, an array of arrays. Data in multidimensional arrays are stored in tabular form (in row major order).

    I general, the form of declaring multidimensional arrays:

    data_type  array_name[size1][size2]....[sizeN];
    
    data_type: This is the type of data to be stored in the array. 
               data_type MUST be a valid C++ data type
    array_name: Name of the array
    size1, size2,... ,sizeN: The size of each of the dimensions
    

    Examples:

    Two dimensional array:
    int two_d[10][20];
    
    Three dimensional array:
    int three_d[10][20][30];
    

    Size of multidimensional arrays

    Total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions.
    For example:
    The above array named two_d[10][20] can store total (10*20) = 200 elements.
    Similarly the array three_d[10][20][30] can store total (10*20*30) = 6000 elements.

    Two-Dimensional Array

    A two dimensional array is the simplest form of a multidimensional array. We can see a two dimensional array as an array of one dimensional array for easier understanding.

    • The basic form of declaring a two-dimensional array of size row, col.
      Syntax:
      data_type array_name[row][col];
      data_type: Type of data to be stored. Valid C/C++ data type.
      
    • We can declare a two dimensional integer array say ‘newArray’ of size 10,20 as:
      int newArray[10][20];
      
    • Elements in two-dimensional arrays are commonly referred with a syntax of newArray[row][col].
    • A two dimensional array can be seen as a table with rows and columns where the row number ranges from 0 to (row-1) and column number ranges from 0 to (col-1). A two dimensional array ‘newArray’ with 3 rows and 3 columns is shown below:
    Screen Shot 2021-06-18 at 3.23.10 PM.png
    Figure \(\PageIndex{1}\): Two dimensional array ("Two dimensional array" by Pat McClanahan, Wikimedia Commans is licensed under CC BY-SA 4.0)

    Initializing Two Dimensional Arrays

    There are two ways in which a Two-Dimensional array can be initialized.

    First Method:

    int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
    

    The above array has 3 rows and 4 columns. The elements in the braces from left to right are stored in the table also from left to right. The elements will be fill the array, the first 4 elements from the left in first row, the next 4 elements in second row and so on.

    This is a valid approach, but it can be difficult to read and to debug in the case of a large array.

    Better Method:

    int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
    

    This type of initialization make use of nested braces. Each set of inner braces represents one row. In the above example there are total three rows so there are three sets of inner braces.

    Accessing Elements of Two-Dimensional Arrays

    Elements in two dimensional arrays are accessed using the both of the indexes.

    int newArray[2][1];
    

    The above example represents the element present in third row and second column.

    Remember: Arrays always count from 0 therefore, row index 2 is actually the third row, and column index 1 is actually the second column.

    To output all the elements of two dimensional array we can use nested for loops. One loop to traverse the rows and another to traverse columns.

    // C++ Program to print the elements of a
    // Two-Dimensional array
    #include<iostream>
    using namespace std;
    
    int main()
    {
        // an array with 3 rows and 2 columns.
        int newArrray[3][2] = {{0,1}, {2,3}, {4,5}};
        // output each array element's value
        for (int row = 0; row < 3; row++)
        {
            for (int col = 0; col < 2; col++)
            {
                cout << "Element at newArrray[" << row
                    << "][" << col << "]: ";
                cout << newArrray[row][col]<<endl;
            }
        }
        
        return 0;
    }
    

    Output:

    Element at newArrray[0][0]: 0
    Element at newArrray[0][1]: 1
    Element at newArrray[1][0]: 2
    Element at newArrray[1][1]: 3
    Element at newArrray[2][0]: 4
    Element at newArrray[2][1]: 5
    

    Three-Dimensional Array

    Image showing a 3 row, 3 column array, that is 3 dimensional. The 3 by 3 array is present in computer memory 3 times. This requires 3 indexes to get to the proper value.
    Figure \(\PageIndex{1}\): Three-Dimensional Array ("Three-Dimensional Array" by Harsh AgarwalGeeks for Geeks is licensed under CC BY-SA 4.0)

    Initializing Three-Dimensional Array

    Initialization in three dimensional array is same as that of two dimensional arrays. The difference is as the number of dimension increases so the number of nested braces will also increase.

    As with the tewo dimensional arrays, it is allowable to simply initialize a series of values that equals the length of the array.

    Method 1:

    int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
                     11, 12, 13, 14, 15, 16, 17, 18, 19,
                     20, 21, 22, 23};
    

    Better Method:

    int x[2][3][4] = 
     { 
       { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
       { {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }
     };
    

    Accessing elements in Three-Dimensional Arrays

    Accessing elements in three dimensional arrays is also similar to accessing two dimensional array elements. The difference is we have to use three loops instead of two loops for one additional dimension in three dimensional rrays.

    // C++ program to print elements of Three-Dimensional
    // Array
    #include<iostream>
    using namespace std;
    
    int main()
    {
        // initializing the 3-dimensional array
        int newArray[2][3][2] =
        {
            { {0,1}, {2,3}, {4,5} },
            { {6,7}, {8,9}, {10,11} }
        };
        
        // output each element's value
        for (int level1 = 0; level1 < 2; ++level1)
        {
            for (int level2 = 0; level2 < 3; ++level2)
            {
                for (int level3 = 0; level3 < 2; ++level3)
                {
                    cout << "Element at newArray[" << level1 << "][" << level2
                        << "][" << level3 << "] = " << newArray[level1][level2][level3]
                        << endl;
                }
            }
        }
        return 0;
    }
    

    Output:

    Element at newArray[0][0][0] = 0
    Element at newArray[0][0][1] = 1
    Element at newArray[0][1][0] = 2
    Element at newArray[0][1][1] = 3
    Element at newArray[0][2][0] = 4
    Element at newArray[0][2][1] = 5
    Element at newArray[1][0][0] = 6
    Element at newArray[1][0][1] = 7
    Element at newArray[1][1][0] = 8
    Element at newArray[1][1][1] = 9
    Element at newArray[1][2][0] = 10
    Element at newArray[1][2][1] = 11
    

    In similar ways, we can create arrays with any number of dimension. However the complexity also increases as the number of dimension increases.

    Adapted from:
    "Multidimensional Arrays in C / C++" by Harsh AgarwalGeeks for Geeks is licensed under CC BY-SA 4.0

    • Was this article helpful?