how to write multidimensional array in c++ code example
Example 1: loop over multidimensional array c++
int arr[2][3] = {
{1,2,3},
{4,5,6}
};
for(int i=0 ; i<sizeof(arr) / sizeof(arr[0]) ; i++){
for(int j=0 ; j<sizeof(arr[i])/sizeof(arr[i][0]) ; j++){
cout<<"2D array have: "<< arr[i][j]<<endl;
}
}
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;
}