Multidimensional Arrays in C++
A multi-dimensional array can be termed as an array of arrays that stores homogeneous data in tabular form. This article covers Multidimensional Arrays in C++ with working examples. It also covers 2-dimentional and 3-dimentional array.
Introduction
An array of arrays that contains homogenous data in tabular form is commonly referred to as a multidimensional array. Data are stored in multidimensional arrays in a row-column order. In this article, we will learn more about multidimensional arrays in C++. Along with this, we will discuss how to declare, access, and use multidimensional arrays effectively in our program with several examples.
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
Table of contents
Multidimensional Arrays in C++
The array of arrays is called a multidimensional array.
The process of declaring a multidimensional array is by specifying the variable type and the array name, followed by a set of square brackets indicating the number of elements in the primary array, and then another set of square brackets indicating the number of elements in the sub-arrays:
string numbers [1] [2];
An array literal, a comma-separated list enclosed in curly brackets, can be used to insert values much as regular arrays do. Each component of an array literal inside a multidimensional array is an additional array literal.
string numbers[1] [2] = { { "X", "W", "Y", "Z" }, { "A", "B", "C", "D" }};
An array declaration gains a new dimension with each set of square brackets. An array with two dimensions is one like the one shown above.
Any no of dimensions is possible for arrays. The complexity of the code increases with an array’s number of dimensions. The following three-dimensional array is:
string numbers [1][2][2] = { { { "X", "W" }, { "Y", "Z" } }, { { "A", "B" }, { "C", "D" } }};
Size of Multidimensional Array
Multiplying the dimensions’ sizes yields the total n of items that are stored in a multidimensional array.
For instance:
The array1 – int a[7][10] can store total (7*10) = 70 items.
Similarly, the array2 – int a[2][5][10] can store total (2*5*10) = 100 items.
I – Two-Dimensional Array
The most basic type of multidimensional array is a two-dimensional array. For ease of understanding, we can think of a two-dimensional array as an array of one-dimensional arrays.
The simplest way to declare a 2-D array of sizes a and b is as follows:
data_type arr_name[a][b];
The type of data that will be stored is indicated here by the variable data type.
A two-dimensional integer array, say, “a,” of size 7,10, can be declared as follows:
int x[7][10]; Two-dimensional array elements are often identified by the notation a[i] [j], where i denote the row number, and ‘j’ denotes the column number
Representation of 2-D Array
Row and column indices are used to access elements in two-dimensional arrays.
A[0] [0] | A[0] [1] | A[0] [2] |
A[1] [0] | A[1] [1] | A[1] [2] |
A[2] [0] | A[2] [1] | A[2] [2] |
Let us see the C++ program below to understand two-dimensional arrays in a better manner:
#include<iostream>using namespace std;int main(){ int a[3][2] = {{1,1}, {4,5}, {3,7}};
for (int x = 0; x < 3; x++) { for (int y = 0; y < 2; y++) { cout << "The element at a[" << x << "][" << y << "]: "; cout << a[x][y]<<endl; } } return 0;}
The output of the C++ code is:
The element at a[0][0]: 1
The element at a[0][1]: 1
The element at a[1][0]: 4
The element at a[1][1]: 5
The element at a[2][0]: 3
The element at a[2][1]: 7
In the above example, we have set the two- dimensions of an array as [3][2] and further specified the values of each element of the array. The for loop x and y will iterate each element one by one for representation as a final two-dimensional array. Here, the outer loop i.e., x accesses the row elements, whereas the inner loop, y, will access the column elements.
II – Three-Dimensional Array
A 3-D array is initialized in the same way as a two-dimensional array. The distinction is that as the no of dimensions rises, so will the number of nested braces.
int a[2][3][4] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230};
However, a three-dimensional array should not be initialized in this manner. An improved method for initializing this array is:
int a[2][3][4] = {{ {0, 10, 20, 30} {40, 50, 60, 70} {80, 90, 100, 110} } { {120, 130, 140, 150} {160, 170, 180, 190} {200, 210, 220, 230}; }
Accessing a 3-D Array
The process of gaining access to items in three-dimensional arrays is comparable to 2-dimensional arrays. The distinction is that in three-dimensional arrays, we must use three loops rather than two loops for each additional dimension.
Let us see a C++ program below to understand how arrays work in three-dimension in a better manner:
#include <iostream>using namespace std;int main(){ int a[2][3][2] = { { { 10, 11 }, { 12, 13 }, { 14, 15 } }, { { 16, 17 }, { 18, 19 }, { 20, 21 } } };
for (int x = 0; x < 2; ++x) { for (int y = 0; y < 3; ++y) { for (int z = 0; z < 2; ++z) { cout << "The element at a[" << x << "][" << y << "][" << z << "] = " << a[x][y][z] << endl; } } } return 0;}
The output of this code is:
The element at a[0][0][0] = 10
The element at a[0][0][1] = 11
The element at a[0][1][0] = 12
The element at a[0][1][1] = 13
The element at a[0][2][0] = 14
The element at a[0][2][1] = 15
The element at a[1][0][0] = 16
The element at a[1][0][1] = 17
The element at a[1][1][0] = 18
The element at a[1][1][1] = 19
The element at a[1][2][0] = 20
The element at a[1][2][1] = 21
In this above C++ code, the 3-D array is being initialized as int a[2][3][2]. For iterating through 3 dimensions, we will need 3 ‘for’ loops, and finally, the array is represented with all the elements. Here, the outer loop, i.e., loop ‘x’, will access the first dimension of the array, the loop ‘y’ will access the second dimension of the array elements, and the ‘z’ loop will access the third dimension of the array elements.
We can make arrays with any dimension numbers similarly as we have showcased in 2-D and 3-D. As the number of dimensions rises, so does the complexity. The Two-Dimensional Array is the most popular type of multidimensional array.
Grid representation is a great application of multidimensional arrays. This example below demonstrates a small game of car fight hitting using a multidimensional array:
#include <iostream>using namespace std;int main() { bool toy[4][4] = { { 0, 1, 1, 0 }, { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 } };
int hit = 0; int Turns = 0;
while (hit < 4) { int row, column;
cout << "Selecting the number of coordinates\n"; cout << "Choose a row between 0 - 3: "; cin >> row;
cout << "Choose a column between 0 - 3: "; cin >> column;
if (toy[row][column]) { toy[row][column] = 0;
hit++; cout << "Hit! " << (4-hit) << " left.\n\n"; } else { cout << "Miss\n\n"; } Turns++; }
cout << "Winner!\n"; cout << "You won in " << Turns << " turns"; return 0;}
The above example showcases the fight between cars. We are firstly putting ‘1’ for the cars to indicate that the car is present, and ‘0’ represents that there is no car. ‘Hits’ and ‘Turns’ will keep track of the player’s number of hits and the number of turns he has played in these variables. The ‘hits<4’ will allow the player to keep moving unless they have hit all four cars. The ‘if’ statement will check if the car is present in the coordinates or not. If the player hits the car, the counter again returns to 0. The ‘cout << “Hit! “<< (4-hits) << “left. “‘ helps the player in knowing that he has hit the car and now he is left with how many cars. Turns++ counts the no of turns the player has taken in the game. The final result is shown in the last.
Conclusion
In this article, we have talked about multidimensional arrays in C++. An array of arrays that contains homogenous data in tabular form is commonly referred to as a multidimensional array. Data are stored in multidimensional arrays in a row-column order. We have also discussed 2-D and 3-D arrays and the ways to declare and access them with the help of a few examples.
Contributed by Megha Chadha
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio