DSA SheetLesson · no judge

DSA FUNDAMENTALSMATRIX BASICS

Print a Matrix Row-Wise and Column-Wise

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 26

Every matrix element needs both a row index and a column index

The matrix [[3, 8, 1], [6, 2, 9]] has 2 rows and 3 columns. Because indexing starts at 0, valid row indices are 0 and 1, while valid column indices are 0, 1 and 2. An element is identified by writing its row index first and its column index second.

CPPThe first index selects the row, and the second index selects the column.
int matrix[2][3] = {
    {3, 8, 1},
    {6, 2, 9}
};

matrix[0][1] = 8;
matrix[1][2] = 9;

For example, matrix[0][1] is 8 because 8 is in row 0, column 1. Likewise, matrix[1][2] is 9 because 9 is in row 1, column 2. The value is not determined by one index alone, since the same column can contain different values in different rows.

The outer loop determines which row stays together

To print row-wise, let the row loop be outside and the column loop be inside. The outer loop selects one row, and the inner loop visits all three columns in that row before the outer loop moves to the next row.

CPPThe row loop stays on one row while the column loop moves across it.
for (int row = 0; row < 2; row++) {
    for (int column = 0; column < 3; column++) {
        cout << matrix[row][column] << " ";
    }
    cout << endl;
}

The visits are matrix[0][0], matrix[0][1], matrix[0][2], then matrix[1][0], matrix[1][1], matrix[1][2]. That produces 3, 8, 1 on the first line and 6, 2, 9 on the second line. The value 1 is the last value from row 0, so it is printed immediately before 6, the first value from row 1.

CHECKPOINT 1Not answered

With the row loop outside and the column loop inside, which value is printed immediately before 6?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

Making columns outermost changes the visit order without changing the matrix

To print column-wise, reverse the nesting: the column loop becomes outermost and the row loop becomes innermost. The outer loop selects one column, and the inner loop visits both rows in that column before moving to the next column.

CPPThe column loop stays on one column while the row loop moves down it.
for (int column = 0; column < 3; column++) {
    for (int row = 0; row < 2; row++) {
        cout << matrix[row][column] << " ";
    }
    cout << endl;
}

The visits are matrix[0][0], matrix[1][0], then matrix[0][1], matrix[1][1], then matrix[0][2], matrix[1][2]. The printed lines are 3, 6, then 8, 2, then 1, 9. The matrix has not moved or changed. Only the order in which its original slots are visited has changed.

CHECKPOINT 2Not answered

Complete the two loop headers so the first line is 3, 6, the second is 8, 2, and the third is 1, 9. Type the missing bounds in outer, inner order.

for (int column = 0; column < ???; column++) {
    for (int row = 0; row < ???; row++) {
        cout << matrix[row][column] << " ";
    }
}

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

two traversal paths through the 2 x 3 matrix [[3, 8, 1], [6, 2, 9]]Two copies of the 2 x 3 matrix are shown. Both contain row 0 as 3, 8, 1 and row 1 as 6, 2, 9. The row-wise copy has arrows from 3 to 8 to 1, then from 6 to 2 to 9, producing two printed lines. The column-wise copy has arrows from 3 to 6, then 8 to 2, then 1 to 9, producing three printed lines.381629row line 1: 3 8 1row line 2: 6 2 9column line 1: 3 6column line 2: 8 2column line 3: 1 9Two traversal paths through [[3, 8, 1], [6, 2, 9]]01201Row-wise: across, then down · Column-wise: down, then right
The outer loop selects whether complete rows or complete columns are printed together.

Each loop bound must match the index that loop controls

A row index can only be 0 or 1 in this matrix, so a loop controlling row must use row < 2. A column index can be 0, 1 or 2, so a loop controlling column must use column < 3. These bounds stay attached to the names of the indices, even when you swap the outer and inner loops.

CPPChanging the nesting changes the order, but each index keeps its own bound.
// Row-wise
for (int row = 0; row < 2; row++) {
    for (int column = 0; column < 3; column++) {
        cout << matrix[row][column] << " ";
    }
}

// Column-wise
for (int column = 0; column < 3; column++) {
    for (int row = 0; row < 2; row++) {
        cout << matrix[row][column] << " ";
    }
}

If you give the row loop the column bound, it may try row values 0, 1 and 2. row = 2 is invalid because this matrix has only rows 0 and 1. If you give the column loop the row bound, it visits only columns 0 and 1, so every value in column 2, including 1 and 9, is skipped. Exchanging loop bounds can therefore cause an invalid access or omit values, not merely change the printed order.

Next part · Quiz