Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATRIX BASICS
To total the matrix [[4, -2, 7], [1, 0, 3]], create total once, before row-wise traversal begins. The value 0 is the starting total, not a value that belongs to any particular row. The same variable must remain alive while the nested loops visit all 2 rows and all 3 columns, so every element can contribute to one whole-matrix result.
int total = 0;
for (int row = 0; row < 2; row++) {
for (int column = 0; column < 3; column++) {
total += matrix[row][column];
}
}
return total;int total = 0;
for (int row = 0; row < 2; row++) {
for (int column = 0; column < 3; column++) {
total += matrix[row][column];
}
}
return total;Where must total = 0 appear if it must sum all six elements of [[4, -2, 7], [1, 0, 3]]?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Each visit adds the current element to the same total. Starting at 0, visiting 4 changes the total to 4. Visiting -2 decreases it to 2, because 4 + (-2) is 2. Visiting 7 changes it to 9. The next row continues from 9: adding 1 gives 10, adding 0 leaves it at 10, and adding 3 produces 13.
| VISITED ELEMENT | TOTAL AFTER ADDITION |
|---|---|
| None | 0 |
| 4 | 4 |
| -2 | 2 |
| 7 | 9 |
| 1 | 10 |
| 0 | 10 |
| 3 | 13 |
Negative and zero values do not require a special case. The minus sign makes -2 reduce the running total, and adding 0 changes nothing. Both values are still visited by the same nested loops and processed by the same addition.
The outer loop runs once for each row, so putting total = 0 at its start creates a new row-level total on every pass. The first row produces 9, but the next outer-loop pass immediately erases that value and starts again at 0. Processing [1, 0, 3] then produces 4, leaving only the second row's sum instead of the whole-matrix total 13.
for (int row = 0; row < 2; row++) {
total = 0;
for (int column = 0; column < 3; column++) {
total += matrix[row][column];
}
}
return total;Move the misplaced initialization so one total is preserved across both rows. Type the corrected placement in words.
for (int row = 0; row < 2; row++) {
total = 0;
for (int column = 0; column < 3; column++) {
total += matrix[row][column];
}
}
return total;Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The value becomes the matrix total only after the inner loop has visited every column of every row. Printing or returning total inside either loop exposes an intermediate value, such as 4 after the first cell or 9 at the row boundary. Put the output or return after the nested loops, where all six additions have completed, and the result is 13.
The loops visit rows times columns cells, so the time complexity is O(rows * columns). The algorithm keeps only the single total in addition to the matrix, so its extra space is O(1). The accumulator does not grow when the matrix grows, even though the number of additions does.