C++ allows Multi-Dimensional Arrays. This could be of 2D or 3D (two-dimensional or three-dimensional) Array. These arrays are stored in the form of a table (with rows and columns) which is also known as a matrix.
In C++, multidimensional array is also known as rectangular arrays.
Syntax of multi-dimensional array:
1 | data-type array_name[size1][size2]...[sizeN]; |
The data-type must be a valid C++ data type, a unique name must be specified to each array and the size must be of an integer constant.
1 2 3 4 5 | //Two Dimensional array (2D) int arr[2][3]; //Three Dimensional array (3D) int arr[2][4][6]; |
Initialization of Multi Dimensional Array in C++
We can initialize a Multi-Dimensional Array (two-dimensional or three-dimensional) in the following ways in C++.
1. Initialization of two-dimensional array:
The following is the 2 by 3 matrix array that is array with 2 rows and 3 columns.
1 2 3 4 5 6 7 | //Method 1 int arr[2][3] = { {4, 5, 7}, {8, 3, 20} }; OR //Method 2 int arr[2][3] = {4, 5, 7, 8, 3, 20}; |
There are two ways shown above to initialize an 2D array. However the method 1 is not preferred.
2. Initialization of three-dimensional array:
1 2 3 4 5 6 7 8 9 10 | //Method1 int arr[2][3][4] = { {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}}, {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}} }; OR //method2 int arr[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 two Dimensional array in C++
Accessing 2d Array:
An array can be accessed by using a specific index number. It can be achieved by placing the particular index number within the two brackets [][]. Such as:
1 2 3 4 | arr[2][3]; //the 4th element of the third row is accessed arr[3][2]; //the 3rd element of the fourth row is accessed OR int val = arr[2][3]; // assigning the element to val |
Accessing 3d Array:
Accessing 3-dimensional array is also as same as a two-dimensional array, the only difference is that in 3d array we have to specify 3 subscripts with a required index number.
Example of C++ Multi Dimensional Array:
Example: Two Dimensional Array in C++
C++ Program to display all elements in an 2D array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; int main() { int arr[3][2] = { { 10, 55 }, { 5, 7 }, { 18, -8 } }; cout << "Displaying each element" << endl; //using nested loop to access 2D Array element for (int row = 0; row < 3; ++row) { for (int col = 0; col < 2; ++col) { cout << "arr[" << row << "][" << col << "] = " << arr[row][col] << endl; } } return 0; } |
Output:
1 2 3 4 5 6 7 | Displaying each element arr[0][0] = 10 arr[0][1] = 55 arr[1][0] = 5 arr[1][1] = 7 arr[2][0] = 18 arr[2][1] = -8 |
Example: Three Dimensional Array in C++
C++ Program to display all elements in an 3D array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> using namespace std; int main() { int arr[2][3][2] = { { { 4, 8 }, { 2, 4 }, { 1, 6 } }, { { 3, 6 }, { 5, 4 }, { 9, 3 } } }; cout << "Displaying each element" << endl; //using nested loop to access 3D Array element 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 | Displaying each element arr[0][0][0] = 4 arr[0][0][1] = 8 arr[0][1][0] = 2 arr[0][1][1] = 4 arr[0][2][0] = 1 arr[0][2][1] = 6 arr[1][0][0] = 3 arr[1][0][1] = 6 arr[1][1][0] = 5 arr[1][1][1] = 4 arr[1][2][0] = 9 arr[1][2][1] = 3 |
As we saw the examples of 2D and 3D arrays, in a similar we can create any number dimension as required. However, the most used multidimensional array is a two-dimensional array.