Code:
#include <stdio.h>
/*
* a[arows][acols] x b[brows][bcols] = c[arows][bcols]
*/
void mult(const double *a, const double *b, double *c,
size_t arows, size_t acols, size_t brows, size_t bcols)
{
size_t i, j, k, x, y, z;
if ( acols != brows )
{
return;
}
for ( i = 0; i < arows; ++i )
{
for ( j = 0; j < bcols; ++j )
{
x = j * arows + i;
c [ x ] = 0;
for ( k = 0; k < acols; ++k )
{
/*
matrixC[i] += matrixA[k * m_rows + row] * matrixB[col * m_rows + k];
*/
y = k * arows + i;
z = j * brows + k;
c [ x ] += a [ y ] * b [ z ];
}
}
}
}
void show(const double *array, size_t rows, size_t cols)
{
size_t r, c;
for ( r = 0; r < rows; ++r )
{
for ( c = 0; c < cols; ++c )
{
size_t i = c * rows + r;
printf("%4g ", array[i]);
}
putchar('\n');
}
putchar('\n');
}
int main(void)
{
double A_4Cx2R[] = {1,5,2,6,3,7,4,8};
double B_3Cx4R[] = {1,4,7,10,2,5,8,11,3,6,9,12};
double C_3Cx2R[6];
show(A_4Cx2R, 2, 4);
show(B_3Cx4R, 4, 3);
mult(A_4Cx2R, B_3Cx4R, C_3Cx2R, 2, 4, 4, 3);
show(C_3Cx2R, 2, 3);
return 0;
}
/* my output (check http://www.mai.liu.se/~halun/matrix/matrix.html)
1 2 3 4
5 6 7 8
1 2 3
4 5 6
7 8 9
10 11 12
70 80 90
158 184 210
*/
I'd thrown debugging