2 dimensional array c++ code example

Example 1: get elements of 2d array c++

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;
}

Example 2: multidimensional array c++

int MyArray[2][3] = { {2, 3, 4}, {12, 13, 14} };

	//visualize
	//2, 3, 4
	//12, 13, 14

	std::cout << MyArray[0][2] << endl; // 4
	std::cout << MyArray[1][1] << endl; // 13

//First square bracket means how many rows you want in your array
//Second squire bracket is for how many qulam you want in your array

Tags:

Java Example