2D Array in C++
Read a matrix into a C++ 2D array with nested loops and print it row by row. Learn how matrix[i][j] maps to rows and columns and how memory is laid out.
1#include <iostream>2using namespace std;34int main() {5 int rows, cols;67 cout << "Enter number of rows: ";8 cin >> rows;9 cout << "Enter number of columns: ";10 cin >> cols;1112 int matrix[rows][cols];1314 cout << "Enter matrix elements:" << endl;15 for (int i = 0; i < rows; i++) {16 for (int j = 0; j < cols; j++) {17 cin >> matrix[i][j];18 }19 }2021 cout << "Matrix:" << endl;22 for (int i = 0; i < rows; i++) {23 for (int j = 0; j < cols; j++) {24 cout << matrix[i][j] << " ";25 }26 cout << endl;27 }2829 return 0;30}
Sample Output
Matrix:
Header line.
1 2 3
Row 0 printed across.
In this one
- How matrix[i][j] means row i and column j
- How nested loops read and print a grid
- What row-major memory order means
- Why vector<vector<int>> is the portable version
How it works
- 1
Read rows (3) and cols (3).
- 2
Declare matrix[rows][cols].
- 3
Outer loop over rows, inner loop over columns, reading each cell.
- 4
Print with the same loops, ending each row with endl.
Edge cases
Non-square sizes
Enter 2 rows and 4 columns.
Everything still works: two lines of four numbers. Only transpose and multiplication care about shape.
Fewer numbers typed
Type only 8 numbers for a 3×3 grid.
cin keeps waiting for the ninth. If the input ends instead, the last cell is left unset and prints garbage.
What's going on
A 2D array is a grid. matrix[i][j] means row i, column j, both counted from 0. This program reads a grid of numbers and prints it back in the same shape.
cin >> rows; cin >> cols;
Sample: 3 and 3.
int matrix[rows][cols];
Rows and columns both come from input, so this is a run-time sized array. GCC and Clang accept it as an extension; standard C++ does not. vector<vector<int>> matrix(rows, vector<int>(cols)); is the portable version, and the 2D Vector program shows it.
Reading uses two loops:
for (int i = 0; i < rows; i++) picks a row.
for (int j = 0; j < cols; j++) walks across that row.
cin >> matrix[i][j]; fills one cell.
The inner loop finishes a whole row before the outer loop moves down. That is why typing 1 2 3, then 4 5 6, then 7 8 9 fills the grid the way you would expect. cin does not care about the line breaks; it just reads the next number. You could type all nine on one line and get the same matrix.
Printing uses the same two loops. Each cell prints with a trailing space, and cout << endl; sits after the inner loop, so each row ends on its own line. Move the endl inside the inner loop and every number lands on its own line.
In memory, C++ stores the rows one after another: matrix[0][0], matrix[0][1], matrix[0][2], matrix[1][0], .... This is called row-major order. Looping with i outside and j inside walks memory in order, which is also the faster direction.
Index traps: valid rows are 0 to rows − 1 and valid columns are 0 to cols − 1. i <= rows reads one row past the end. That is undefined behavior; you may see garbage or a crash, and C++ will not warn you.
Swap the loops (j outside, i inside) when printing and you get the transpose: rows become columns. Transpose of a Matrix builds on exactly that.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
In int m[2][3], how many elements are there?
Pick an answer to check it.
Question 2
Which is the last valid element of int m[3][4]?
Pick an answer to check it.
Questions people ask
Do I have to type each row on its own line?
No. cin reads numbers separated by any whitespace.
How do I pass a 2D array to a function?
With a fixed column count, like void f(int m[][3], int rows). With vectors, pass const vector<vector<int>>&.
How do I find a row sum?
Loop j over one row i and add matrix[i][j].