multidimensional array in c++ code example
Example 1: three dimensional array c++
int a[2][3]= {
{1, 2, 3},
{4, 5, 6}
};
cout << a[1][1];
int a[2][3][2]= {
{ {1, 2},
{2, 3},
{4, 5}
},
{ {6, 7},
{8, 9},
{10, 11}
}
};
cout << a[0][1][1];
Example 2: c++ code to write 2d array
#include <iostream>
using namespace std;
int main(){
int n,m;
int a[n][m];
cin >> n >>m;
for ( int i=0; i<n; i++){
for (int j=0; j<m; j++){
cin >> a[i][j];
}
}
for ( int x=0; x<n; x++){
for (int y=0; y<m; y++){
cout << "a[" << x << "][" << y << "]: ";
cout << a[x][y] << endl;
}
}
return 0;
}
Example 3: get elements of 2d array c++
void printMatrix(array<array<int, COLS>, ROWS> matrix){
for (auto row : matrix){
for (auto element : row){
cout << element << ' ';
}
cout << endl;
}
Example 4: multidimensional array c++
int MyArray[2][3] = { {2, 3, 4}, {12, 13, 14} };
std::cout << MyArray[0][2] << endl;
std::cout << MyArray[1][1] << endl;