Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATRIX BASICS
Matrix addition and subtraction do not combine whole rows or row totals. At position [i][j], you use A[i][j] and B[i][j], then place the result at the same position. For the matrices A = [[3, -1, 4], [2, 0, 5]] and B = [[1, 6, -2], [7, 3, 5]], the values at [0][0] are 3 and 1, so their sum is 4 and their difference is 2.
Apply that rule independently to every position. For addition, 3 + 1 = 4, -1 + 6 = 5, and 4 + -2 = 2 in the first row. The second row becomes 2 + 7 = 9, 0 + 3 = 3, and 5 + 5 = 10. Therefore A + B = [[4, 5, 2], [9, 3, 10]].
Subtraction follows the same alignment, but it keeps the order A - B. The first row becomes 3 - 1 = 2, -1 - 6 = -7, and 4 - -2 = 6. The second row becomes 2 - 7 = -5, 0 - 3 = -3, and 5 - 5 = 0. Therefore A - B = [[2, -7, 6], [-5, -3, 0]].
Which values produce the addition result at [1][2] for A and B?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Both A and B have 2 rows and 3 columns, so the row index i must visit 0 and 1, while the column index j must visit 0, 1, and 2. The nested loops use the same i and j three times: once for A, once for B, and once for the output. That repeated pair is what prevents a value from one position being combined with a value from another position.
int add[2][3];
int subtract[2][3];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
add[i][j] = A[i][j] + B[i][j];
subtract[i][j] = A[i][j] - B[i][j];
}
}When i = 1 and j = 2, the loop reads A[1][2] and B[1][2], which are both 5. It writes 10 into add[1][2] and 0 into subtract[1][2]. The loop never adds a row total first, because no step changes the current pair of indices into a row-wide calculation.
Addition is unchanged when its operands swap: A[i][j] + B[i][j] equals B[i][j] + A[i][j]. At [0][1], both orders give -1 + 6 = 5. Subtraction is different because its order matters. A[0][1] - B[0][1] is -1 - 6 = -7, while B[0][1] - A[0][1] is 6 - -1 = 7.
Swapping the operands in subtraction negates the result at every matching position. At [1][0], A[1][0] - B[1][0] is 2 - 7 = -5, but B[1][0] - A[1][0] is 7 - 2 = 5. The indices still match in both calculations, but the first value and second value have exchanged roles.
What is B[1][0] - A[1][0]?
B[1][0] - A[1][0]Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A and B can be added or subtracted only when they have the same dimensions. Here both are 2 by 3, so every position in A has exactly one matching position in B, and both result matrices are also 2 by 3. The output shape is inherited from the shared input shape, not created by combining row counts or column counts.
If one matrix has a missing row or column, some input position has no matching value. The calculation cannot produce a complete result matrix, and accessing that missing position can read invalid memory. When dimensions come from input rather than fixed declarations, compare the row counts and column counts before the nested loops access any cells.