Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATRIX BASICS
For the matrix [[4, 2, 7, 1], [9, 5, 3, 8], [6, 10, 11, 12], [13, 14, 15, 16]], classify each position by comparing its row index with its column index. A position is in the upper triangle when row <= column. It is in the lower triangle when row >= column. The two triangles share the positions where the indices are equal.
| POSITION RELATIONSHIP | POSITIONS IN THE MATRIX | VALUES |
|---|---|---|
| row < column, upper-only | (0,1), (0,2), (0,3), (1,2), (1,3), (2,3) | 2, 7, 1, 3, 8, 12 |
| row = column, shared | (0,0), (1,1), (2,2), (3,3) | 4, 5, 11, 16 |
| row > column, lower-only | (1,0), (2,0), (2,1), (3,0), (3,1), (3,2) | 9, 6, 10, 13, 14, 15 |
The upper triangle contains the shared positions and the positions above them, which are the positions where the column index is larger. The lower triangle contains the shared positions and the positions below them, which are the positions where the row index is larger. This classification is based on indices, so it still works when the values in the matrix have no visual pattern.
The value 10 is at position (2,1). How should this position be classified?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The positions (0,0), (1,1), (2,2), and (3,3) have equal row and column indices. Their values are 4, 5, 11, and 16. Because 0 <= 0 and 0 >= 0 are both true, the same is true for every equal-index position.
The <= and >= symbols are necessary here. If you used row < column for the upper triangle, the values 4, 5, 11, and 16 would disappear from the upper output. If you used row > column for the lower triangle, those same values would disappear from the lower output. The diagonal positions are not outside the triangles, they are the boundary shared by both.
position value row <= column row >= column
(0,0) 4 true true
(1,1) 5 true true
(2,2) 11 true true
(3,3) 16 true trueTo print either triangle, keep the same nested traversal over all rows and columns. For the upper output, print the stored value when row <= column and print 0 otherwise. For the lower output, print the stored value when row >= column and print 0 otherwise. The traversal does not skip positions or move the values; it masks positions that do not belong.
for (int row = 0; row < n; row++) {
for (int column = 0; column < n; column++) {
if (row <= column) cout << matrix[row][column] << " ";
else cout << 0 << " ";
}
cout << '\n';
}
for (int row = 0; row < n; row++) {
for (int column = 0; column < n; column++) {
if (row >= column) cout << matrix[row][column] << " ";
else cout << 0 << " ";
}
cout << '\n';
}Upper output:
4 2 7 1
0 5 3 8
0 0 11 12
0 0 0 16
Lower output:
4 0 0 0
9 5 0 0
6 10 11 0
13 14 15 16Replace the incorrect lower-triangle condition with the condition that includes the diagonal and the positions below it.
if (row <= column) 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.
This 4 x 4 matrix has 4 x 4 = 16 positions. Each output performs one condition check for every position, including positions that become 0. The upper output therefore makes 16 checks, and the lower output makes another 16 checks. The complete process remains O(n^2) time because the nested traversal visits n squared positions.
Printing directly needs only the row index, the column index, and a few temporary values, so it uses O(1) extra space. The zeroes are not stored in a second matrix. They are chosen at the moment an excluded position is printed, while the original matrix remains unchanged.