samedi 10 janvier 2015

C Programming and Matrix multiplication output issue


Vote count:

0




I'm currently learning c programming, to better my understanding of matrices in c I've tried to make this program. I seem to be having problem with the output, as you can see the program has 3 functions. First one allows you to input the values for the array and then displays it. Second functions performs the multiplication and the last should display the output of the multiplied matrix. However the output is strange. here is my code.The output i just below the code.



#include <stdio.h>


void read_matrix(int m2[][3] )
{
int i, j;
printf("input values for matrix in order of rows first \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d",&m2[i][j]);
}
}

for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", m2[i][j]);
}
printf("\n");
}
}



void multiply_matrices(int m1[][3], int m2[][3] ,int m3[][3])
{
int i, j, k;

for (i = 0; i < 3; i++){
for (j = 0; j < 3; j++){
for (k = 0; k < 3; k++){
m3[i][j] +=m1[i][k]*m2[k][j];
}
}
}
}



void write_matrix(int m3[][3] )
{
int i, j;

for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", m3[i][j]);
}
printf("\n");
}
}


int main(void)

{
int matrix1[3][3], matrix2[3][3], matrix3[3][3];

read_matrix(matrix1);
read_matrix(matrix2);


multiply_matrices(matrix1, matrix2, matrix3);

write_matrix(matrix3);

return 0;

}


and this is the output!



input values for matrix in order of rows first
1
2
3
2
2
2
1
2
2
1 2 3
2 2 2
1 2 2
input values for matrix in order of rows first
2
1
1
1
2
1
2
1
2
2 1 1
1 2 1
2 1 2
-858993450 -858993452 -858993451 /*This is the multiplied matrix output!*/
-858993450 -858993452 -858993452
-858993452 -858993453 -858993453
Press any key to continue . . .


I fear it may be just silly mistake, if so I'm sorry, but I cant see where I am going wrong at this moment.


Any help would be greatly appreciated



asked 36 secs ago







C Programming and Matrix multiplication output issue

Aucun commentaire:

Enregistrer un commentaire