Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATRIX BASICS
The left diagonal starts at the top-left cell and moves one row down and one column right at every step. Because the row index and column index are equal at each position, the condition is i == j. In the 5 x 5 matrix, row 0 selects column 0, row 1 selects column 1, and so on. This selects exactly one position from each row.
Position: (0,0) (1,1) (2,2) (3,3) (4,4)
Value: 1 7 13 19 25You can therefore access the left diagonal with matrix[i][i]. For the running matrix, those accesses produce 1, 7, 13, 19, and 25. The equal indices are not a coincidence: increasing both indices together moves along the diagonal while still choosing one cell from every row.
Which coordinates and values are selected by i == j in the running matrix?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The right diagonal starts in the top-right cell, so row 0 must use the last column. With zero-based indexing and n = 5, the last valid column is n - 1, which is 4. As the row increases, the column decreases, giving the formula n - 1 - i.
Row i: 0 1 2 3 4
Column: 4 3 2 1 0
Position: (0,4) (1,3) (2,2) (3,1) (4,0)
Value: 5 9 13 17 21For i = 0, n - 1 - i is 5 - 1 - 0, or 4, so the first access is matrix[0][4] and gives 5. The expression n - i would give 5 instead. Column 5 is outside the valid column indices 0 through 4, so n - i is an off-by-one error, not the right diagonal formula.
A row index from 0 through n - 1 reaches every diagonal position because each diagonal contains one selected cell in every row. For each row, matrix[i][i] accesses the left diagonal and matrix[i][n - 1 - i] accesses the right diagonal. You can print the first access on one output line and the second access on the next output line.
for (int i = 0; i < n; i++) {
cout << matrix[i][i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++) {
cout << matrix[i][n - 1 - i] << " ";
}For the running matrix, the first loop prints 1 7 13 19 25, and the second prints 5 9 13 17 21. Each loop performs n direct accesses, so printing either n-element diagonal takes O(n) time. Testing every cell would inspect all n squared positions even though only n positions belong to the chosen diagonal.
Complete this access so the loop prints 5, 9, 13, 17, 21: matrix[i][____]
matrix[i][____]Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
In an odd-sized square matrix, the two diagonals meet at the center. Here the center is row 2, column 2. Both accesses choose that cell: matrix[2][2] from the left diagonal formula and matrix[2][5 - 1 - 2], which is also matrix[2][2], from the right diagonal formula.
Left diagonal: 1 7 13 19 25
Right diagonal: 5 9 13 17 21The repeated 13 is correct because it is a member of both diagonals. The two output lines represent two separate selections, so each line includes the center once. If you combine both lines into one sequence, 13 appears twice because the same matrix cell was selected by two different diagonal rules, not because one rule visited it twice.