- #1
James889
- 192
- 1
Hello,
I am trying to write a program that multiplies together two 3x3 matrices.
However my program does not really work as intended, and i get some really strange results.
Any ideas?
I am trying to write a program that multiplies together two 3x3 matrices.
However my program does not really work as intended, and i get some really strange results.
Any ideas?
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int result_matrix[3][3];
int matrixA[3][3] = { {2,2,2},
{2,2,2},
{2,2,2} };
int matrixB[3][3] = { {4,4,4},
{4,4,4},
{4,4,4} };
for(int rows=0; rows<3; rows++) {
for(int columns=0; columns<3; columns++){
for(int r=0,c=0; r<3,c<3; r++,c++){
printf("Element at result_matrix[%d][%d] is MatrixA[%d][%d] * MatrixB[%d][%d]\n",rows,columns,r,c,c,r);
result_matrix[rows][columns] += matrixA[r][c] * matrixB[c][r];
}
}
}
printf("%d\n",result_matrix[0][0]);
printf("%d\n",result_matrix[0][1]);
printf("%d\n",result_matrix[0][2]);
printf("%d\n",result_matrix[1][0]);
printf("%d\n",result_matrix[1][1]);
printf("%d\n",result_matrix[1][2]);
printf("%d\n",result_matrix[2][0]);
printf("%d\n",result_matrix[2][1]);
printf("%d\n",result_matrix[2][2]);
return 0;
}
Last edited: