I need to create a new matrix by multiplying matrix B with matrix C.
B is:
C is:
I'm doing:
1 * 9 + 2 * 6 + 3 * 3;
1 * 8 + 2 * 5 + 3 * 2; etc
It should print:
30 |
24 |
18 |
84 |
69 |
54 |
138 |
114 |
90 |
But for some reason the last row results in 144, 119, 94.
Here is the code:
Code:
#include <stdio.h>
int main(void) {
int i = 0, j = 0;
/* h) matriz produto base na B e C. {{{ */
int B[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 9, 9}};
int C[3][3] = {{9, 8, 7},
{6, 5, 4},
{3, 2, 1}};
/* Inicializa todos os elementos da matriz
* com 0 como valor default. */
int M_PRODUTO[3][3] = {{0}};
int counter = 0;
/* Itera sobre as linhas. */
for (i = 0; i < 3; i++) {
/* Itera sobre as colunas. */
for (j = 0; j < 3; j++) {
for (counter = 0; counter < 3; counter++) {
M_PRODUTO[i][j] += ((B[i][counter]) * (C[counter][j]));
}
printf("\t%d", M_PRODUTO[i][j]);
}
printf("\n");
}
printf("\n\n");
return 0;
}
I'm compiling it with:
Code:
gcc -std=c99 -Wall mult_matr.c -o ./Bin/mult_matr
If I compute it on the paper or using a calculator the result is fine. Only when running the C program I get that wrong last row. If the logic is wrong, why do do program work fine for the first two rows?
Any insights are welcome.