Loading...
C++ Multidimensional Array

C++ Multidimensional Array

In this tutorial, we'll learn about multi-dimensional arrays (2D array and 3D array) in C++ with the help of examples. More specifically, how to declare them, access them, and use them efficiently in our program.


Multidimensional Array

  • Multidimensional array are also known as array of arrays.
  • The data in multi-dimensional array is stored in a tabular form (row * column) as shown in the diagram below.

General form of declaration N-dimensional arrays :

data_type  array_name[size1][size2]....[sizeN];

data_type: Type of data to be stored in the array. 
Here data_type is valid C/C++ data type
array_name: Name of the array
size1, size2,... ,sizeN: Sizes of the dimensions

Size of Multidimensional Array

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 array int x[5][3] can store total (5 * 3) = 15 elements.

The array int x[5][3][6] can store total (5 * 3 * 6) = 90 elements.

For example:

int x[3][4];

Here, x is a two-dimensional array. It can hold a maximum of 12 elements.

We can think of this array as a table with 3 rows and each row has 4 columns as shown below.

C++ two dimensional array
Elements in two-dimensional array in C++ Programming

Three-dimensional arrays also work in a similar way. For example:

float x[2][3][5];

This array x can hold a maximum of 30 elements.

We can find out the total number of elements in the array simply by multiplying its dimensions:

2 x 3 x 5 = 30

Two dimensional Array

  • Two dimensional array is the simplest form of a Multidimensional array.
  • It can be visualized as an array of arrays.
  • 2D array is also called a matrix, we are going to discuss how we can initialize 2D array.
  • It can be of any type like integer, character, float, etc. depending on the Initialization.

Initialization of two-dimensional array

Like a normal array, we can initialize a multidimensional array in more than one way.

First method :

int arr[2][3] = {2, 0, 3, 5, 1, 12};

The above method is not preferred. A better way to initialize this array with the same array elements is given below:

Better method :

int  arr[2][3] = { {2, 0, 3}, {5, 1, 12}};

So, as you can see, we initialize a 2D array arr, with 2 rows and 3 columns as an array of arrays.

Total elements in this array : 2 * 3 = 6 , and Each element of the array is yet again an array of integers.

         Col 1    Col 2    Col 3
Row 1       2       0       3
Row 2       5       1       12

Example 1: Two Dimensional Array

// C++ Program to display all elements of two dimensional array
#include <iostream>
using namespace std;

int main() {
    // an array with 3 rows and 2 columns.
    int arr[3][2] = {{2, 0}, {3, 4}, {1, 5}};
                                  
    // use of Outer for loop to access the rows of the array
    for (int i = 0; i < 3; i++) {
                                  
      // Inner for loop to access the columns of the array
      for (int j = 0; j < 2; j++) {
          cout << "Elements at arr[" << i << "][" << j << "] = " << arr[i][j] << endl;
      }
    }

    return 0;
}

Output

Element at arr[0][0] = 2
Element at arr[0][1] = 0
Element at arr[1][0] = 3
Element at arr[1][1] = 4
Element at arr[2][0] = 1
Element at arr[2][1] = 5

In the above example, we have initialized a two-dimensional int array named arr that has 3 "rows" and 2 "columns".

Here, we have used the nested for loop to display the array elements.

  • the outer loop from i == 0 to i == 2 access the rows of the array
  • the inner loop from j == 0 to j == 1 access the columns of the array

Finally, we print the array elements in each iteration.


Example 2: Taking Input from user for Two Dimensional Array

#include <iostream>
using namespace std;
int main() {
    int num[2][3];

    // Taking input from the user
    cout << "Enter 6 integers: " << endl;
                                  
    // Storing user input in the array
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            cin >> numbers[i][j];
        }
    }
                                  
    cout << "The 2D array : " << endl;
                                  
    //  Printing array elements
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << "numbers[" << i << "][" << j << "]: " << numbers[i][j] << endl;
        }
    }
                                  
    return 0;
}

Output

Enter 6 numbers: 
0
1
2
3
4
5
The numbers are:
numbers[0][0]: 0
numbers[0][1]: 1
numbers[0][2]: 2
numbers[1][0]: 3
numbers[1][1]: 4
numbers[1][2]: 5

Here, we have used a nested for loop to take the input of the 2d array. Once all the input has been taken, we have used another nested for loop to print the array members.

Finally, we print the array elements in each iteration.


Three dimensional Array

  • Three dimensional array is form of a Multidimensional array.
  • 3D array is a multi-dimensional array used to store 3-dimensional information.
  • 3D array is essentially an array of arrays of arrays : it's an array or collection od 2D array. and a 2D array is an array of 1D array.
  • It can be of any type like integer, character, float, etc. depending on the Initialization.

Initialization of three-dimensional array

Initialization in 3D array is same as that of 2D array.

First method :

int arr[2][3][4] = {2, 1, 0, 2, 3, -1, 5, 10, 13, 7, 20, 
    12, 3, -4, 6, -2, 8, -1, 5, -5, -2, 11, 14, 0};

The above method is not a good way of initializing a three-dimensional array.

Better method :

int arr[2][3][4] = { 
    { {2, 1, 0, 2}, {3, -1, 5, 10}, {13, 7, 20, 12} },
    { {3, -4, 6, -2}, {8, -1, 5, -5}, {-2, 11, 14, 0} }
};

Notice the dimensions of this three-dimensional array.

The first dimension has the value 2. So, the two elements comprising the first dimension are:

Element 1 = { {2, 1, 0, 2}, {3, -1, 5, 10}, {13, 7, 20, 12} }
Element 2 = { {3, -4, 6, -2}, {8, -1, 5, -5}, {-2, 11, 14, 0} }

The second dimension has the value 3. Notice that each of the elements of the first dimension has three elements each:

{2, 1, 0, 2}, {3, -1, 5, 10} and {13, 7, 20, 12} for Element 1.
{3, -4, 6, -2}, {8, -1, 5, -5} and {-2, 11, 14, 0} for Element 2.

Finally, there are four int numbers inside each of the elements of the second dimension:

{2, 1, 0, 2}
{3, -1, 5, 10}
{13, 7, 20, 12}
... .. ...
... .. ...

Example 3: Program to Store value entered by user in 3D array

// Program to Store value entered by user in 3D array and display it.
#include <iostream>
using namespace std;
int main() {
    // This array can store upto 12 elements (2x3x2)
    // Initializing the 3D array
    int arr[2][3][2] = {
        { {0, 1}, {2, 3}, {4, 5} }, 
        { {6, 7}, {8, 9}, {10, 11} }
    };
                                  
    // Displaying each element's values with proper index number.
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 2; ++k) {
                cout << "Element at arr[" << i << "][" << j << "]
                    [" << k << "] = " << arr[i][j][k] << endl;
            }
        }
    }
                                  
    return 0;
}

Output

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

The basic concept of printing elements of a 3d array is similar to that of a 2d array.

However, since we are manipulating 3 dimensions, we use a 3 nested for loop :

  • the outer loop from i == 0 to i == 1 accesses the first dimension of the array
  • the middle loop from j == 0 to j == 2 accesses the second dimension of the array
  • the inner loop from k == 0 to k == 1 accesses the third dimension of the array

Finally, we print the array elements in each iteration.

  • In similar ways, we can create arrays with any number of dimension. However, the complexity also increases as the number of dimensions increases.
  • Note : The most used multidimensional array is the 2D array.

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Multidimensional array in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Array & Function.

- Related Topics