09
NovMultidimensional Arrays in C++
Multidimensional Arrays in C++: An Overview
We already saw what are arrays in C++. Now you might have become familiar with the basic concepts ofarray
in C++. Let us now begin with the Multidimensional Arrays in C++ in our C++ tutorial. To certify your skills, you can also look into our C++ Certification Course.Multidimensional Arrays in C++
Arrays
can have any number of dimensions. In C++ programming, you can create an array of arrays.Syntax
To create a multidimensionalarray
in C++, you need to specify the number of dimensions and the size of each dimension.dataType arrayName[size1][size2]...[sizeN];
Example
int arr[5][4];
Here, an integer type 2D array, arr
is declared with 5
rows and 4
columns thus a total of 20
integer elements.
In the same manner, as above, a 3D array can be declared.
Example
int arr[2][4][3];
The array arr
can store 24
integer elements.
Read More - C++ Interview Interview Questions and Answers
How to initialize a multidimensional array in C++?
- 2D array
It is a matrix (a table of rows and columns).int arr[2][3] = {{1, 3, 0}, {-1, 5, 9}};
int arr[][3] = {{1, 3, 0}, {-1, 5, 9}};
int arr[2][3] = {1, 3, 0, -1, 5, 9};
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}}
};
How to access the elements of a multi-dimensional array in C++?
- 2D array
In this type of array, you must specify the index
number of both the row and column.Example
#include <iostream>
using namespace std;
int main() {
int arr[2][3] = {{5, 6, 7}, {3, 6, 8}};
cout << arr[0][2];
return 0;
}
The coutstatement in the above code in C++ Compiler accesses the value of the element in the first row 0
and third column 2
of the array, arr
.
Output
7
3D array
Example
#include <iostream>
using namespace std;
int main() {
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}}
};
cout << arr[0][2][3];
return 0;
}
The cout
statement in the above code accesses the value of the element in the first row 0
, the third column 2
, and the fourth dimension 3
of the array, arr
.
Output
2
How to change the elements of a multidimensional array in C++?
To change the value of a specific element, refer to the index
number in each dimension.
- 2D array
Example
#include <iostream>
using namespace std;
int main() {
int matrix[2][3] = {{5, 6, 7}, {3, 6, 8}};
matrix[0][2] = 9;
cout << matrix[0][2]; // Now outputs 9
return 0;
}
Output
9
How to traverse a multidimensional array in C++?
Traversing a multidimensionalarray
can be done in two ways- 2D array
- using for loop
Example
#include <iostream> using namespace std; int main() { int arr[2][3] = {{5, 6, 7}, {3, 6, 8}}; int i, j; for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { cout << arr[i][j] << endl; } } return 0; }
Output
5 6 7 3 6 8
- using for-each loop
Example
#include <iostream> using namespace std; int main() { int arr[2][3] = {{5, 6, 7}, {3, 6, 8}}; for (const int (&row)[3] : arr) { for (int element : row) { cout << element << endl; } } return 0; }
In the above C++ code, we use a
range-based for
loop to iterate through each row in the 2D array. Again, for each row, we use anotherrange-based for
loop to iterate through the elements within that row.const int (&row)[3]
species that row is a reference to an array of integers with a size of 3.Output
5 6 7 3 6 8
Input and Output Multidimensional Array Elements in C++
We can use the cinfunction to take inputs of an array
from the user.
- 2D array
Example
#include <iostream>
using namespace std;
int main() {
int marks[2][3];
// Prompt user to enter grades
for (int i = 0; i < 2; i++) {
cout << "Enter the marks for student " << i + 1 << ":\n";
for (int j = 0; j < 3; j++) {
cout << "Subject " << j + 1 << ": ";
cin >> marks[i][j];
}
}
cout << "The marks entered are:\n";
// Display entered grades
for (int i = 0; i < 2; i++) {
cout << "Student " << i + 1 << ": ";
for (int j = 0; j < 3; j++) {
cout << "Subject " << j + 1 << ": " << marks[i][j] << endl;
}
}
return 0;
}
The above program is to print the marks of two students in 3 subjects. The first for
loop indicates the student number. The 2nd for
loop is used to take the marks of the 3 subjects for the student in the first for
loop from the user. In the same way, the two for
loops work for printing the output
Output
Enter the marks for student 1:
Subject 1: 95
Subject 2: 88
Subject 3: 92
Enter the marks for student 2:
Subject 1: 78
Subject 2: 85
Subject 3: 90
The marks entered are:
Student 1: Subject 1: 95
Subject 2: 88
Subject 3: 92
Student 2: Subject 1: 78
Subject 2: 85
Subject 3: 90
- 3D array
Example in C++ Online Editor
#include <iostream>
using namespace std;
int main() {
int arr[2][3][2];
cout << "Enter the values as given by the user: \n";
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 2; ++k) {
cin >> arr[i][j][k];
}
}
}
// Printing values with the proper index.
cout << "\nDisplaying values:\n";
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
Enter the values as given by the user:
1
2
3
4
5
6
7
8
9
10
11
12
Displaying values:
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
Summary
This was all about multidimensionalarrays
in C++. With this, the topic of arrays in C++ gets completed. Now, the only thing is to implement whatever learned till now. You must know where and what kind of array
is required. If you're interested in more tips and guidance, you may also consider a C++ Certification, which can validate your skills and enhance your credibility in the field.