Hey y'all.
Right here goes. I'm writing a matrix calculator, i've got add down, i've got subtract down, now I'm on multiply and sweet mother of gargantua its giving me hassle. I mean GARRR
I've the section of the code that i've tried below. It gives the output matrix the right dimensions, just not the right values.
Code:
int multiply_matrix(matrix_struct *matrix_a, matrix_struct *matrix_b, matrix_struct *matrix_c)
{
int row,col,sweep, position;
float total=0, matrix_total=0;
if((matrix_a->no_of_rows)!=(matrix_b->no_of_columns))
{
printf("\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
printf("= Sorry, the dimensions of your matrix =\n");
printf("= do not allow multilpication =\n");
printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n");
return 0;
}
else
{
free(matrix_c->matrix);
matrix_c->no_of_rows=matrix_b->no_of_rows;
matrix_c->no_of_columns=matrix_a->no_of_columns;
//allocate memory for rows
matrix_c->matrix=(float**)(malloc((matrix_c->no_of_rows)*sizeof(float*)));
//allocate memory for each row of the matrix
for(row=0;row<(matrix_c->no_of_rows);row++)
matrix_c->matrix[row]=(float*)(malloc((matrix_c->no_of_columns)*sizeof(float)));
//performing the muliplication
for(row=0;row<(matrix_c->no_of_rows); row++)
for(col=0;col<(matrix_c->no_of_columns);col++)
{
total=0;
matrix_total=0;
for(sweep=0; sweep<(matrix_a->no_of_rows); sweep++)
for(position=0; position<(matrix_b->no_of_columns); position++)
{
matrix_total=(matrix_a->matrix[sweep][position])*(matrix_b->matrix[position][sweep]);
total=+matrix_total;
}
matrix_c->matrix[row][col] = total;
}
printf("Your answer is:\n\n");
for(row=0;row<(matrix_c->no_of_rows); row++)
{
printf("\n");
for(col=0;col<(matrix_c->no_of_columns);col++)
printf("%f ",matrix_c->matrix[row][col]);
}
}
return 1;
}
This is just the function, if you want the rest of the code its in the attatchement below.
I really cant get my head around it, its driving me nuts, so any help would be appreciated on levels you cant imagine.
Cheers,
Mitch