I am a beginner in C.
I was trying to write some code that would carry out matrix multiplication using transpose.
Is there any way I can improve the code in terms of execution time?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <time.h>
int main()
{
int a[3][3] = {{1,0, 1}, {2, 2, 4},{1, 2, 3}};
int b[3][3] ={ { 2, 3, 1}, { 6, 6, 2 }, { 9, 9, 0 } };
int result[3][3];
double tmp;
int i,j,k;
for (i=0; i<3; i++) //i = col
{
for (k=0; k<3; k++)
{
tmp = a[i][k];
for (j=0; j<3; j++) //j = row
{
result[i][j] += tmp * b[k][j];
printf("%d\t",result[i][j]);
}
}
}
}
If your matrix is
int, you really shouldn’t use adoubleas a temporary. Converting integer to floating-point and back again for no purpose is very wasteful, it can cost a lot.