C++ Multi-Dimensional Arrays | Example

 Multi-Dimensional Arrays

Multi-dimensional arrays include arrays of arrays.

Declare a multi-dimensional array by specifying the variable type, and the array name, followed by a set of square brackets indicating the number of elements in the primary array, and then another set of square brackets indicating the number of elements in the sub-arrays:

string arr[2][4];

An array literal, which is a comma-separated list enclosed in curly braces, can be used to insert values just like regular arrays do. Each component of an array literal in a multi-dimensional array is an additional array literal.

string arr[2][4] = {
  { "A", "B", "C", "D" },
  { "E", "L", "G", "Q" }
};

An array declaration gains a new dimension for each set of square brackets. An array with two dimensions is one like the one shown above.

Any number of dimensions is possible for arrays. The number of dimensions in an array affects how complex the code is. The three-dimensional array below is:

string arr[2][2][2] = {
  {
    { "A", "B" },
    { "C", "Q" }
  },
  {
    { "O", "F" },
    { "J", "P" }
  }
};

Access a Multi-Dimensional Array’s Elements

You must enter an index number in each dimension of a multi-dimensional array in order to access an element.

This command accesses the value of the element in the letters array’s first row (zero) and third column (two).

string arr[2][4] = {
  { "A", "B", "C", "D" },
  { "E", "L", "G", "Q" }
};

cout << arr[0][2];  // Outputs "C"

Keep in mind that Array indexes begin at 0: The first component is [0]. The next element is [1], etc.

Modify Array Elements in Multiple Dimensions

Refer to the element’s index number in each of the dimensions to change the value of the element:

string arr[2][4] = {
  { "A", "B", "C", "D" },
  { "E", "F", "G", "H" }
};
arr[0][0] = "P";

cout << arr[0][0];  // Now outputs "P" instead of "A"

Loop Through an Array in Multiple Dimensions

You need a loop for each of the array’s dimensions in order to iterate through a multi-dimensional array.
The example below displays each letter in the arr array:

string arr[2][4] = {
  { "A", "B", "C", "D" },
  { "E", "F", "G", "H" }
};

for (int i = 0; i < 2; i++) {
  for (int j = 0; j < 4; j++) {
    cout << arr[i][j] << "\n";
  }
}

A three-dimensional array is looped through in this example:

string arr[2][2][2] = {
//geektocode.com
  {
    { "A", "B" },
    { "C", "D" }
  },
  {
    { "E", "F" },
    { "G", "H" }
  }
};

for (int i = 0; i < 2; i++) {
  for (int j = 0; j < 2; j++) {
    for (int k = 0; k < 2; k++) {
      cout << arr[i][j][k] << "\n";
    }
  }
}

Why Do Multidimensional Arrays Exist?

Grid representation is excellent with multi-dimensional arrays. This instance demonstrates a useful application for them. The following example represents a multi-dimensional array:

// C++ Program to Store value entered by user in
// three dimensional array and display it.

//geektocode.com

#include <iostream>
using namespace std;

int main() {
    // This array can store upto 12 elements (2x3x2)
    int arr[2][3][2] = {
                            {
                                {1, 2},
                                {3, 4},
                                {5, 6}
                            }, 
                            {
                                {7, 8}, 
                                {9, 10}, 
                                {11, 12}
                            }
                        };

    // Displaying the values with proper index.
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 2; ++k) {
                cout << "arr[" << i << "][" << j << "][" << k << "] = " << arr[i][j][k] << endl;
            }
        }
    }

    return 0;
}
Output:
arr[0][0][0] = 1 
arr[0][0][1] = 2 
arr[0][1][0] = 3 
arr[0][1][1] = 4 
arr[0][2][0] = 5 
arr[0][2][1] = 6 
arr[1][0][0] = 7 
arr[1][0][1] = 8 
arr[1][1][0] = 9 
arr[1][1][1] = 10 
arr[1][2][0] = 11 
arr[1][2][1] = 12

As we can see, as the dimensions rise, the array’s complexity rises exponentially.

Leave a Reply

Your email address will not be published. Required fields are marked *