The objective was to debug a C++ program performing matrix multiplication. An issue was identified where the program was not providing the correct matrix multiplication output.
- Ran the program to observe the problem firsthand and identified that the issue was in the resultant multiplication output, as it provided an incorrect answer.
- The Matrix was printed successfully with the right data, validating the
setDataanddisplaymethods. - The problem was narrowed down to the
multiplymethod.
- The message "Matrix dimensions are incompatible for multiplication." (line 34) wasn't printed, so the code did not enter this condition.
- A breakpoint was set at line 38 and
rowsandother.colswere printed to verify their values, which were correct.
g++ -g -O0 -o Matrix Matrix.cpp
gdb Matrix
break 38
print rows
print other.cols- The 3 nested loops at line 40 were investigated, focusing on the assignment operation within them.
- A breakpoint was set at the beginning of the loops and
colswas printed to verify its value.
break 40
print cols- PS: We don't need to check
rowsandother.colsagain as they were previously checked and found to be correct.
- Tracked the changes to
result.datato observe how its value was being altered. - Observed the first cell
result.data[0][0]which was initially 0. After the first iteration, it became 7 (1*7), which was expected. - The second iteration posed an issue where the value was 18 (2*9), but it should have been 25 (1*7 + 2*9).
print result.data
continue
print result.data
continue
print result.data- Identified that line 43 was performing an assignment operation instead of an addition to the previous value:
result.data[i][j] = data[i][k] * other.data[k][j];
- Corrected the line to be as follows:
result.data[i][j] += data[i][k] * other.data[k][j];
- Re-ran the application with various test cases to ensure the corrected code now provides the expected and accurate matrix multiplication output.
The matrix multiplication logic was corrected by ensuring that the products of the respective elements were accumulated instead of being overwritten. This was achieved by altering the assignment operation to accumulate the values.