 # Multidimensional Arrays in C++

05 Dec 2023
Intermediate
99 Views Learn via Video Course & by Doing Hands-on Labs

## Multidimensional Arrays in C++: An Overview

We already saw what are arrays in C++. Now you might have become familiar with the basic concepts of `array` 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 multidimensional `array` in C++, you need to specify the number of dimensions and the size of each dimension.
``````dataType arrayName[size1][size2]...[sizeN];
``````

#### Example

``````int arr;
``````

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;
``````

The array `arr` can store `24` integer elements.

## How to initialize a multidimensional array in C++?

### 2D array

It is a matrix (a table of rows and columns).
``````int arr = {{1, 3, 0}, {-1, 5, 9}};

int arr[] = {{1, 3, 0}, {-1, 5, 9}};

int arr = {1, 3, 0, -1, 5, 9};
``````

### 3D array

The initialization is similar to a 2D array.
``````int arr = {
{{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 = {{5, 6, 7}, {3, 6, 8}};
cout << arr;
return 0;
}
``````

The coutstatement in the above code 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 = {
{{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;
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 = {{5, 6, 7}, {3, 6, 8}};
matrix = 9;
cout << matrix; // Now outputs 9
return 0;
}
``````

#### Output

``````9
``````

## How to traverse a multidimensional array in C++?

Traversing a multidimensional `array` can be done in two ways

### 2D array

• using for loop

#### Example

``````#include <iostream>
using namespace std;
int main() {
int arr = {{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 = {{5, 6, 7}, {3, 6, 8}};

for (const int (&row) : 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 another `range-based for` loop to iterate through the elements within that row. `const int (&row)` 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;

// 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";

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

``` ```#include <iostream>
using namespace std;
int main() {
int arr;

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 = 1
arr = 2
arr = 3
arr = 4
arr = 5
arr = 6
arr = 7
arr = 8
arr = 9
arr = 10
arr = 11
arr = 12
``````
##### Summary
This was all about multidimensional `arrays` 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.

Share Article
Similar Articles 