In C++, you can create an array of an array known as multi-dimensional array. For example:
intx[3][4];
Here, x is a two dimensional array. It can hold a maximum of 12 elements.
You can think this array as table with 3 rows and each row has 4 columns as shown below.
Three dimensional array also works in a similar way. For example:
floatx[2][4][3];
This array x can hold a maximum of 24 elements. You can think this example as: Each of the 2 elements can hold 4 elements, which makes 8 elements and each of those 8 elements can hold 3 elements. Hence, total number of elements this array can hold is 24.
Multidimensional Array Initialization
You can initialize a multidimensional array in more than one way.
Initialization of two dimensional array
inttest[2][3] = {2,4,-5,9,0,9};
Better way to initialize this array with same array elements as above.
C++ Program to store temperature of two different cities for a week and display it.
#include<iostream>usingnamespace std;constint CITY =2;constint WEEK =7;intmain(){inttemperature[CITY][WEEK]; cout <<"Enter all temperature for a week of first city and then second city. \n"; // Inserting the values into the temperature arrayfor (int i =0; i < CITY; ++i) {for(int j =0; j < WEEK; ++j) { cout <<"City "<< i +1<<", Day "<< j +1<<" : "; cin >>temperature[i][j]; } } cout <<"\n\nDisplaying Values:\n"; // Accessing the values from the temperature arrayfor (int i =0; i < CITY; ++i) {for(int j =0; j < WEEK; ++j) { cout <<"City "<< i +1<<", Day "<< j +1<<" = "<<temperature[i][j] << endl; } }return0;}
Output
Enter all temperature for a week of first city and then second city. City 1, Day 1 : 32City 1, Day 2 : 33City 1, Day 3 : 32City 1, Day 4 : 34City 1, Day 5 : 35City 1, Day 6 : 36City 1, Day 7 : 38City 2, Day 1 : 23City 2, Day 2 : 24City 2, Day 3 : 26City 2, Day 4 : 22City 2, Day 5 : 29City 2, Day 6 : 27City 2, Day 7 : 23Displaying Values:City 1, Day 1=32City 1, Day 2=33City 1, Day 3=32City 1, Day 4=34City 1, Day 5=35City 1, Day 6=36City 1, Day 7=38City 2, Day 1=23City 2, Day 2=24City 2, Day 3=26City 2, Day 4=22City 2, Day 5=29City 2, Day 6=27City 2, Day 7=23
Example 3: Three Dimensional Array
C++ Program to Store value entered by user in three dimensional array and display it.
#include<iostream>usingnamespace std;intmain(){ // This array can store upto 12 elements (2x3x2)inttest[2][3][2]; cout <<"Enter 12 values: \n"; // Inserting the values into the test array // using 3 nested for loops.for(int i =0; i <2; ++i) {for (int j =0; j <3; ++j) {for(int k =0; k <2; ++k ) { cin >>test[i][j][k]; } } } cout<<"\nDisplaying Value stored:"<<endl; // Displaying the values with proper index.for(int i =0; i <2; ++i) {for (int j =0; j <3; ++j) {for(int k =0; k <2; ++k) { cout <<"test["<< i <<"]["<< j <<"]["<< k <<"] = "<<test[i][j][k] << endl; } } }return0;}
Output
Enter 12 values: 123456789101112Displaying Value stored:test[0][0][0] =1test[0][0][1] =2test[0][1][0] =3test[0][1][1] =4test[0][2][0] =5test[0][2][1] =6test[1][0][0] =7test[1][0][1] =8test[1][1][0] =9test[1][1][1] =10test[1][2][0] =11test[1][2][1] =12
As the number of dimension increases, the complexity also increases tremendously although the concept is quite similar.
In this article, you'll learn about multi-dimensional arrays in C++. More specifically, how to declare them, access them and use them efficiently in your program.