1. ## matrix multiplier...

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;

}

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

2. At a glance:

>>total = +matrix_total;
presumably should be
>>total += matrix_total;

3. Yep, thats one bug now squashed. Now it seems to work, but using the example of a 2x2 matrix multiply by a 2x2 matrix, all the elements being 2 ( [2 2,2 2]*[2 2,2 2] ) rather than get a 2x2 matrix with all answers 8, i get a 2x2 matrix with all answers 16

also, when i do a 3x3 * 3x3 with all elements 3, rather than get 3x3 all elements being 27, they are all 81..

ie the 2x2 matrix is off by a factor of 2 and the 3x3 matrix is off by a factor of 3...

also, it doesnt work when i try a 1x4 matrix or 4x1 matrix

im still trying but its not going well...

4. sounds like you're calculating it too many times. Write it down on paper first in simple form, the follow the logic of your code to see where it's not doing what you want.

5. wish it were that simple. i think it's the right number of iterations. though a loop in a loop in a loop in a loop heaven knows. Plus i'm getting errors when the 2d array is of dimension 1 by something.

I'm going into uni tomorrow, so i'll ask a lecturer, who hopefully has some answers seeign as he set the damned thing. I'll post anything i learn on here. saying that i'm still gonna be on it all night

Chers for looking though

Mitch

6. Code:
```        for(row=0;row<(matrix_c->no_of_rows); row++)
for(col=0;col<(matrix_c->no_of_columns);col++)
{
matrix_c->matrix[row][col] = 0;

for(sweep=0; sweep<(matrix_a->no_of_columns); sweep++)
matrix_c->matrix[row][col] = matrix_a->matrix[row][sweep] * matrix_b->matrix[sweep][col];

}```

7. Thanks, it neatens my code a lot

I am just gonna leave it and ask my lecturers in the morning. I'll post their answer up here anyways, just in case anyone wanted to know.

Many thanks everyone

Mitch

8. ## fin

Code:
```           //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;
matrix_c->matrix[row][col] = 0;

for(sweep=0; sweep<(matrix_a->no_of_columns); sweep++)
{
matrix_total = matrix_a->matrix[row][sweep] * matrix_b->matrix[sweep][col];
total+=matrix_total;
}

matrix_c->matrix[row][col]=total;

}```
was the way to do it. I had gotten the rows and columns from which matrices getting muddled from further above and that was what was screwing with it all. Thanks for your help everyone, truly appreciated

Mitch