C++ Multidimensional Array
A multidimensional array is an array with more than one dimension. It means that it can grow in different directions i.e. instead of changing the length only, it can also change in width, depth or more. It is the homogeneous collection of items where each element is accessed using multiple indices.
Create Multidimensional Array
data_type array_name[s1][s2]...[sn];
where s1, s2,…, sn is the size of each dimension.
Example:
// Two dimensional array
int two_d[2][4];
// Three dimensional array
int three_d[2][4][8];
Size of Multidimensional Array
For 1D array, the length of the array is simply its size too. But multidimensional arrays have extra dimensions. So, the size of each dimension is considered separately. The number of elements is the product of all its dimensions’ size. It is similar to calculating area in 2D and volume in 3D.
For example, consider the below array:
int arr1[2][4];
- The array int arr[2][4] can store total (2 * 4) = 8 elements (product of its dimensions).
- The size in bytes can be calculated by multiplying the number of elements by size of each element or we can just use sizeof operator.
- In this case, size in bytes = 4*8 = 32 bytes.
To verify the above calculation, we can use sizeof() method to find the size of an array.
//Driver Code Starts{
#include <iostream>
using namespace std;
int main()
{
//Driver Code Ends }
// creating 2d and 3d array
int arr[2][4];
// using sizeof() operator to get size of arr
cout << sizeof(arr) << " bytes";
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
Output
32 bytes
We can have any number of dimensions in an array as per requirement, but the complexity of handling them also increases exponentially. That is why, the most widely used multidimensional arrays are:
- Two-Dimensional Array
- Three-Dimensional Array
Two-Dimensional Array (2D Array)
A two-dimensional array in C++ is a collection of elements organized the form of rows and columns. It can be visualized as a table or a grid, where each element is accessed using two indices: one for the row and one for the column. Like a one-dimensional array, two-dimensional array indices also range from 0 to n-1 for both rows and columns.
Create 2D array
data_type array_name[row_size][column_size];
Initialize 2D Array
Like 1D arrays, 2D arrays can also be initialized using a list of values enclosed inside {} curly brackets, but as 2D arrays have two dimensions, the list is nested inside another list to initialize each dimension one by one. It means that each row values are nested inside one big list.
int arr[2][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}};
Nesting can also be omitted, and values will still be assigned sequentially.
int arr[2][4] = {0, 1, 2, 3, 4, 5, 6, 7};
The above 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. The values will be initialized sequentially.
It is to be noted that the number of values should not exceed the total number of elements an array can store. It can have less values (partial initialization) but cannot have more values.
If all the elements are to be initialized to 0, then this syntax can be used:
int arr[2][4] = {0};
This can be only done for 0, not for any other value.
Access Elements of 2D Array
Elements of a 2-dimensional array have to be accessed using row and column indices. It is similar to matrix element position, but the only difference is that here indexing starts from 0.
arr[i][j];
where, i is the index of row and j is the index of the column. The range of indexes should be:
- 0 ≤ i ≤ (row_size – 1)
- 0 ≤ j ≤ (col_size – 1)
Any values other than that leads to the segmentation fault.
Example:
//Driver Code Starts{
#include <iostream>
using namespace std;
int main() {
//Driver Code Ends }
int arr[2][4] = {0, 1, 2, 3, 4, 5, 6, 7};
// Accessing 3rd element is 1st row
cout << arr[0][2] << endl;
// Accessing first element in 2nd row
cout << arr[1][0];
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
Output
2 4
Update Elements of 2D Array
The value at any index can be updated by using = assignment operator.
array_name[i][j] = new_value
The new_value should be of same data type.
Example:
//Driver Code Starts{
#include <iostream>
using namespace std;
int main() {
//Driver Code Ends }
int arr[2][4] = {0, 1, 2, 3, 4, 5, 6, 7};
// Updating 3rd element is 1st row
arr[0][2] = 22;
cout << arr[0][2] << endl;
// Updating first element in 2nd row
arr[1][0] = 99;
cout << arr[1][0];
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
Output
22 99
Traverse 2D Array
Two loops nested inside each other are needed to traverse a 2D array, one for moving though each dimension. First loop is used to move though the rows of 2D array, while other is used to move though columns in each row to access all the elements of the row.
//Driver Code Starts{
#include <iostream>
using namespace std;
int main() {
//Driver Code Ends }
int arr[2][4] = {0, 1, 2, 3, 4, 5, 6, 7};
// Outer loop to move through rows
for (int i = 0; i < 2; i++) {
// Inner loop to move though elements in each row
for (int j = 0; j < 4; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
Output
0 1 2 3 4 5 6 7
Three-Dimensional Array (3D Array)
A three-dimensional array in C++ is a collection of elements organized in a 3D cuboid-like structure. It can be visualized as a series of two-dimensional arrays stacked on top of each other. Each element is accessed using three indices: one for the depth, one for the row, and one for the column.
Create 3D Array
To declare a 3D array in C++, we need to specify its third dimension along with 2D dimensions.
data_type array_name[depth_size][row_size][column_size];
where depth is the number of 2D arrays in it.
Initialize 3D Array
Like 2D arrays, 3D arrays can also be initialized using a list of values enclosed inside {} curly brackets. However, in a 3D array, the values are grouped into 2D arrays, and each 2D array is nested inside another set of curly brackets.
int arr[2][2][3] = {
{{0, 1, 2}, {3, 4, 5}},
{{6, 7, 8}, {9, 10, 11}}
};
Alternatively, nesting can be omitted, and the values will still be filled sequentially:
int arr[2][2][3] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
This array has 2 layers (depth), 2 rows per layer, and 3 columns per row. The values are filled sequentially across the layers.
It is important to note that the number of values should not exceed the total number of elements the array can store. If fewer values are provided, the rest of the array is initialized to 0 (partial initialization).
If all the elements are to be initialized to 0, then this syntax can be used:
int arr[2][2][3] = {0};
This can be only done for 0, not for any other value.
Access Elements of 3D Array
The elements of a 3D array are accessed using three indices: depth, row, and column. These indices must be within the following ranges:
- 0 ≤ depth ≤ (depth_size – 1)
- 0 ≤ row ≤ (row_size – 1)
- 0 ≤ column ≤ (column_size – 1)
Example:
//Driver Code Starts{
#include <iostream>
using namespace std;
int main() {
//Driver Code Ends }
int arr[2][2][3] = {
{{0, 1, 2}, {3, 4, 5}},
{{6, 7, 8}, {9, 10, 11}}
};
// Accessing element at depth 0, row 1, column 2
cout << arr[0][1][2] << endl;
// Accessing element at depth 1, row 0, column 1
cout << arr[1][0][1];
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
Output
5 7
Update Elements of 3D Array
To update a value at any index of a 3D array, use the assignment operator = while accessing the element:
array_name[depth_index][row_index][column_index] = new_value;
Example:
//Driver Code Starts{
#include <iostream>
using namespace std;
int main() {
int arr[2][2][3] = {
{{0, 1, 2}, {3, 4, 5}},
{{6, 7, 8}, {9, 10, 11}}
};
//Driver Code Ends }
// Updating element at depth 0, row 1, column 2
arr[0][1][2] = 22;
cout << arr[0][1][2] << endl;
// Updating element at depth 1, row 0, column 1
arr[1][0][1] = 99;
cout << arr[1][0][1];
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
Output
22 99
Traverse 3D Array
To traverse a 3D array, you need three nested loops: one for each dimension (depth, row, column).
Example:
//Driver Code Starts{
#include <iostream>
using namespace std;
int main() {
//Driver Code Ends }
int arr[2][2][3] = {
{{0, 1, 2}, {3, 4, 5}},
{{6, 7, 8}, {9, 10, 11}}
};
// Traverse through all elements of 3D array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
cout << arr[i][j][k] << " ";
}
cout << endl;
}
}
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
Output
0 1 2 3 4 5 6 7 8 9 10 11
Pass Multidimensional Arrays to Functions
In C++, you can pass multidimensional arrays to functions. Since multidimensional arrays have more than one dimension, the function signature needs to account for all dimensions.
Passing a 2D Array to a Function
To pass a 2D array to a function, you can specify the number of columns (or other dimensions) in the function signature. The number of rows can be deduced automatically.
//Driver Code Starts{
#include <iostream>
using namespace std;
//Driver Code Ends }
// Function to print a 2D array
void print2DArray(int arr[2][3]) {
//Driver Code Starts{
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
int main() {
// Initializing a 2D array
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
// Passing the 2D array to the function
print2DArray(arr);
return 0;
}
//Driver Code Ends }
Passing a 3D Array to a Function
To pass a 3D array to a function, you need to specify the size of the second and third dimensions.
//Driver Code Starts{
#include <iostream>
using namespace std;
//Driver Code Ends }
// Function to print a 3D array
void print3DArray(int arr[2][3][4]) {
//Driver Code Starts{
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
cout << arr[i][j][k] << " ";
}
cout << endl;
}
}
}
int main() {
// Initializing a 3D array
int arr[2][3][4] = {
{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}},
{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}
};
// Passing the 3D array to the function
print3DArray(arr);
return 0;
}
//Driver Code Ends }