A multidimensional array is an array with more than one dimension. It is the homogeneous collection of items where each element is accessed using multiple indices.
Syntax:
- datatype: Type of data to be stored in the array.
- arrayName: Name of the array.
- size1, size2,…, sizeN: Size of each dimension.
Ex:
1 2 | Two dimensional array: int two_d[2][4]; Three dimensional array: int three_d[2][4][8]; |
Two Dimensional Array (or 2D Array)
A two-dimensional array in C++ is the simplest form of a multi-dimensional array. It can be visualized as an array of arrays. The image below depicts a two-dimensional array.
A two-dimensional array is also called a matrix. It can be of any type like integer, character, float, etc. depending on the initialization. In the next section, we are going to discuss how we can initialize 2D arrays.
Initialize 2D array using the Initializer list
First Method: The below array has 2 rows and 4 columns. The elements are filled in a way that the first 4 elements are filled in the first row and the next 4 elements are filled in the second row.
Ex:
1 | int arr[2][4] = {0, 1, 2, 3, 4, 5, 6, 7}; |
Second Method: The below way is the cleaner way to initialize a 2D array the nested list represents the elements in a row and the number of elements inside it is equal to the number of columns in a 2D array. The number of nested lists represents the number of columns.
1 | int x[2][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}}; |
Initialization of 2D array using Loops
We can also initialize 2D array using loops. To initialize 2D array we have to use two nested loops and nested loops are equal to the dimension. For example, to initialize a 3D array we have to use three nested loops. Let’s see an example.
Ex:
1 2 3 4 5 6 7 8 9 10 11 | //In the below example we have initializes the 2D array with 1. //The outer loop is used to track rows “i=0” means // the first row because of 0 indexing similarly “j=0” means the first column and //combining this x [0][0] represents the first cell of the 2D array. int x[2][4]; for(int i = 0; i < 2; i++){ for(int j = 0; j < 4; j++){ x[i][j] = 1; } } |
Accessing Elements
We can access the elements of a 2-dimensional array using row and column indices. It is similar to matrix element position but the only difference is that here indexing starts from 0.
Syntax:
- i: Index of row.
- j: Index of the column.
Ex:
1 | int x[1][2]; //Below is the index of elements of the second row and third column. |
Ex:
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 | #include <iostream> using namespace std; int main() { int count = 1; // Declaring 2D array int array1[3][4]; // Initialize 2D array using loop for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { array1[i][j] = count; count++; } } // Printing the element of 2D array for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { cout << array1[i][j] << " "; } cout << endl; } return 0; } |
Output:
1 2 3 | 1 2 3 4 5 6 7 8 9 10 11 12 |
Three-Dimensional Array (or 3D Array)
The 3D array is a data structure that stores elements in a three-dimensional cuboid-like structure. It can be visualized as a collection of multiple two-dimensional arrays stacked on top of each other.
Syntax:
- dataType: Type of data to be stored in each element.
- arrayName: Name of the array.
- d: Number of 2D arrays or Depth of array.
- r: Number of rows in each 2D array.
- c: Number of columns in each 2D array.
Ex:
1 | int array[3][5][2]; |
Initialization of 3D Array using Initializer List
Method 1: In this method, we have to write the total number of elements inside curly braces, and each item is placed at its position according to the dimension given.
Ex:
1 2 3 | int x[3][5][2] = {0, 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, 30}; |
Method 2 (Better): In this method, we have partitioned the elements using nested lists and it is easy to read.
Ex:
1 2 3 4 5 | int x[3][5][2] = { { {0, 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, 30} }, }; |
Initialization of 3D Array using Loops
This method is the same as initializing a 2D array using loops with one more nested loop for the third dimension.
Ex:
1 2 3 4 5 6 7 8 | int x[3][5][2]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { for (int k = 0; k < 2; k++) { x[i][j][k] = (some_value); } } } |
Accessing elements
Accessing elements in 3D arrays is as simple as accessing elements in 2D arrays.
Ex:
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 count = 0; // declaring 3d array int x[2][2][3]; // initializing the array for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 3; k++) { x[i][j][k] = count; count++; } } } // printing the array for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 3; k++) { printf("x[%d][%d][%d] = %d \n", i, j, k, x[i][j][k]); count++; } } } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 | x[0][0][0] = 0 x[0][0][1] = 1 x[0][0][2] = 2 x[0][1][0] = 3 x[0][1][1] = 4 x[0][1][2] = 5 x[1][0][0] = 6 x[1][0][1] = 7 x[1][0][2] = 8 x[1][1][0] = 9 x[1][1][1] = 10 x[1][1][2] = 11 |