DSA FUNDAMENTALS › MATRIX BASICS
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.
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.
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.
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.
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.
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.
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.
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.
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.
// 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.