Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATRIX BASICS
A row sum combines the elements that share one row index. A column sum combines the elements that share one column index. In the matrix [ [3, -1, 4, 2], [0, 5, -2, 1], [7, 1, 0, -3] ], the first row sum is 3 + (-1) + 4 + 2 = 8, while the first column sum is 3 + 0 + 7 = 10. The groups use different directions, but every element belongs to one row group and one column group.
Matrix:
[ 3 -1 4 2 ] -> row sum 8
[ 0 5 -2 1 ] -> row sum 4
[ 7 1 0 -3 ] -> row sum 5
column sums:
10 5 2 0For row sums, the outer loop selects one row and the inner loop visits every column in that row. The accumulator must be created or set to zero after selecting the row but before adding its elements. That makes one accumulator belong to exactly one row.
for (int row = 0; row < 3; row++) {
int sum = 0;
for (int column = 0; column < 4; column++) {
sum += matrix[row][column];
}
cout << sum << " ";
}For the first row, sum becomes 3, then 2, then 6, then 8. The accumulator is reset before row 1, so that row produces 0, then 5, then 3, then 4. After another reset, row 2 produces 7, then 8, then 8, then 5. The printed row sums are therefore 8, 4, and 5.
Where must sum be initialized in a row-sum loop so it prints 8, 4, and 5 instead of 8, 12, and 17?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
For column sums, the outer loop selects one column and the inner loop visits all rows in that column. The matrix access does not change: matrix[row][column] still means the element at the chosen row and chosen column. Only the roles of the loops change.
for (int column = 0; column < 4; column++) {
int sum = 0;
for (int row = 0; row < 3; row++) {
sum += matrix[row][column];
}
cout << sum << " ";
}Column 0 adds matrix[0][0], matrix[1][0], and matrix[2][0], giving 3 + 0 + 7 = 10. Column 1 gives -1 + 5 + 1 = 5. Column 2 gives 4 + (-2) + 0 = 2, and column 3 gives 2 + 1 + (-3) = 0. The output is 10, 5, 2, and 0.
Repair this column-sum loop so it prints 10, 5, 2, and 0. Type the two corrections: the inner loop bound and the matrix access.
for (int column = 0; column < 4; column++) {
int sum = 0;
for (int row = 0; row < 4; row++) {
sum += matrix[column][row];
}
cout << sum << " ";
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
This matrix has 3 rows and 4 columns, so row-sum code must print exactly 3 values and column-sum code must print exactly 4 values. A loop that prints 4 row sums is grouping the data incorrectly, even if some values appear plausible. The rectangular shape makes the loop roles visible: the row index ranges from 0 to 2, while the column index ranges from 0 to 3.
The row sums are 8, 4, and 5, and they combine to 17. The column sums are 10, 5, 2, and 0, and they also combine to 17. This equality is a useful correctness check because both calculations include every matrix element exactly once. It cannot replace the separate subtotals: printing only 17 would hide which row or column was calculated incorrectly.