To iterate over a Two Dimensional Array in C++, you can use nested loops.
In this example,
matrix
.#include <iostream>
using namespace std;
int main() {
int matrix[3][3] = {{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
0 1 2 1 2 3 2 3 4
In this example,
matrix
.#include <iostream>
using namespace std;
int main() {
char matrix[2][2] = {{'a', 'b'}, {'c', 'd'}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
a b c d
In this tutorial, we learned How to Iterate over a Two Dimensional Array in C++ language with well detailed examples.