I can't understand why this piece of code which is supposed to perform matrix multiplication goes wrong.
Input: 2x2 matrices with elements 1,2,3,4 in both matrices
Expected output: 7 10 15 22
Output given by this code: 15 22 12 16
int a[10][10], b[10][10], c[10][10], i, j, k, r1, c1, r2, c2;
int (*pa)[10][10] = &a, (*pb)[10][10] = &b, (*pc)[10][10] = &c;
for ( i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
*pc[i][j] = 0;
for(k = 0; k < c1; k++) {
*pc[i][j] += *pa[i][k] * *pb[k][j];
}
}
}
I tried debugging using print statements like this and these are the results:
When given 2x2 matrices which have 1,2,3,4 as their elements, these are the errors produced:
at 00 of a is 3
at 00 of b is 1
Elements you're multiplying: 3 1
But expected output is this:
at 00 of a is 1
at 00 of b is 1
(Same seems to happen for rest of the elements) Rest of the code which isn't pasted here is bug-free. Checked it thoroughly using print statements.