This post will discuss how to print two-dimensional arrays (i.e., matrix) in C++.

A simple solution is to iterate over each row and column of the matrix using a simple for-loop and print each element. The following C++ program demonstrates it:

Download  Run Code

Output:

1 2 3
4 5 6
7 8 9

 
Here’s an equivalent version using a range-based for-loop. This can be used starting with C++ 11.

Download  Run Code

Output:

1 2 3
4 5 6
7 8 9

 
We can also create a template that deduces the size of the array from its declared type.

Download  Run Code

Output:

1 2 3
4 5 6
7 8 9

That’s all about printing two-dimensional arrays in C++.