Troyseph
0
Q:

get elements of 2d array c++


for (int i = 0; i < m; i++) 
{ 
   for (int j = 0; j < n; j++) 
   { 
      cout << arr[i][j] << " "; 
   } 
     
   // Newline for new row 
   cout << endl; 
}
0
#include <array>
2 #include <iostream>
3
4 using namespace std;
5
6 //remember const!
7 const int ROWS = 2;
8 const int COLS = 3;
9
10 void printMatrix(array<array<int, COLS>, ROWS> matrix){
11 //for each row
12 for (int row = 0; row < matrix.size(); ++row){
13 //for each element in the current row
14 for (int col = 0; col < matrix[row].size(); ++col){
15 cout << matrix[row][col] << ' ';
16 }
17 cout << endl;
18 }
19 }
0
void printMatrix(array<array<int, COLS>, ROWS> matrix){
for (auto row : matrix){
//auto infers that row is of type array<int, COLS>
for (auto element : row){
cout << element << ' ';
}
cout << endl;
}
0
#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;
}
0

New to Communities?

Join the community