I am writing a code to multiply matrices of any dimensions. I noticed it works fine for square matrices as well as when the outer dimensions are greater than inner. However, I can't get it to work for cases where outer dimensions are smaller then inner, such as multiplying a 2X3 matrix with a 3X2 matrix. For these cases, it straight away terminates without printing anything. In the working cases, the correct resultant matrix is being printed.
Below is my code after taking inputs and calculating the resultant matrix. arr1 is a m1Xn1 matrix and arr2 is a m2Xn2 matrix.
int *res = (int*)calloc(m1*n2, sizeof(int));
for(int i = 0; i < m1; i++){
for(int j = 0; j < n2; j++){
*(res + i*m1 + j) = 0;
for(int k = 0; k < n1; k++){
*(res + i*m1 + j) += *(arr1 + i*m1 + k) * *(arr2 + k*n2 + j);
}
//printf("%d,%d = %d\n", i, j, *(res + i*m1 + j));
}
}
Can someone please explain what's going wrong?