Aezur
18
Q:

initialize 2d array c++

int** arr = new int*[10]; // Number of Students
int i=0, j;
for (i; i<10; i++) 
	arr[i] = new int[5]; // Number of Courses
/*In line[1], you're creating an array which can store the addresses
  of 10 arrays. In line[4], you're allocating memories for the 
  array addresses you've stored in the array 'arr'. So it comes out 
  to be a 10 x 5 array. */
2
#include <array>
2 #include <iostream>
3
4 using namespace std;
5
6 //remember const!
7 const int ROWS = 2;
8 const int COLS = 3;


int main(){
 array<array<int, COLS>, ROWS> matrix = {
 1, 2, 3,
 4, 5, 6
 };
  
  
  return 0;
}
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

New to Communities?

Join the community