the exercise deals with multiplying two matrices in which you give the dimensions by scanf
and the vectors as well. The problem comes when you print the result, an array of zeros and a dimension very different from the one that should come out appears.
I've tried changing the loops for
in the part that prints the matrix product, as well as the multiplication part of the matrix. But it has not had an effect.
terMatrix is the product matrix.
I attach the code:
#include <stdio.h>
int main(){
int m ;
int n;
int p;
printf("Numero de filas primera matriz:\n");
scanf("%d",&m);
printf("Numero de columnas de primera matriz:\n");
scanf("%d",&n);
printf("Numero de columnas segunda matriz\n");
scanf("%d",&p);
int primMatriz[m][n];
int secMatriz[n][p];
printf("ponga primera matriz:\n");
for(int i = 0; i<m;i++){
for(int j = 0 ; j<n; j++){
scanf("%d", &primMatriz[i][j]);
}
}
printf("ponga segunda matriz:\n");
for(int i = 0; i<n;i++){
for(int j = 0 ; j<p; j++){
scanf("%d", &secMatriz[i][j]);
}
}
int terMatriz[n][n];
for(int b = 0; b< p; b++){
for(int i = 0; i<m; i++){
int res = 0;
for(int j = 0; j< n;j++){
res += primMatriz[i][j] * secMatriz[j][b];
}
terMatriz[i][b] = res;
}
}
printf("El resultado es: \n");
for(int i =0; i<n; i++){
for(int j = 0; j<n ; j++){
printf("%d ",terMatriz[i][j]);
}
printf("\n");
}
return 0;
}
It is a misconception.
The multiplication between a matrix of dimensions AxB and another BxC gives a matrix of dimensions AxC.
Therefore the code should be:
The rest of the code is fine.