It is one thing to read user matrices, another is to multiply them and another is to display them. The function that multiplies the matrices should do no more than the multiplication itself. The reason for this is that if you want to multiply matrices read from a file or produced as a result of some other process, this will not be possible if you are reading them from the user. The same thing if you want to show an array without multiplication.
The functions of reading and showing an array are these:
void ler_matriz(int mat[][MAX], int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("Digite mat[%d][%d]: ", i, j);
scanf("%d", &mat[i][j]);
}
}
}
void mostrar_matriz(int mat[][MAX], int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
Let’s observe this condition:
na == nb && nb == nc && na == nc
The na == nc
is unnecessary. The same can be said for ma == mc
.
Another problem with your code is this:
for(j=0; j<nb && j<na; i++)
You are using j
as a loop variable, but is increasing the i
.
You can also use a whole return to represent error. Let’s put that 0 is ok and 1 is matrix size error.
So if you wanted to just multiply the elements in the same positions, then:
int multiplicar_elementos(
int matA[][MAX], int na, int ma,
int matB[][MAX], int nb, int mb,
int matC[][MAX], int nc, int mc)
{
if (na != nb || nb != nc || ma != mb || mb != mc) return 1;
for (int i = 0; i < na; i++) {
for (int j = 0; k < ma; j++) {
matC[i][j] = matA[i][j] * matB[i][j];
}
}
return 0;
}
However, if your idea is to implement the matrix multiplication, the problem here is bigger. In this case, the matrices do not have the same sizes. The number of rows in the first matrix is equal to the number of columns (and not rows) in the second. That is, unless you are multiplying square matrices, they nay should have the same sizes.
The product of a matrix of dimensions A x B by a B x C gives in a matrix A x C.
How are you multiplying an array na
x ma
by a nb
x mb
to give a nc
x mc
, then ma
must be equal to nb
, na
must be equal to nc
and mb
must be equal to mc
.
With this, we can reduce the six numbers of matrix sizes to just three.
The matrix multiplication algorithm will need three loops. The reason for this is that in the resulting matrix, each cell contains the sum of the products of the elements of a row of the first matrix by the elements of a column of the second. Thus, to fill each cell of the third matrix, you will need an internal loop running through the row of the first and the column of the second at the same time. Outside of this internal loop, you’ll need another two to visit each cell of the third matrix.
Your neat code for multiplying matrices looks like this:
int multiplicar_matrizes(
int matA[][MAX], int na, int ma,
int matB[][MAX], int nb, int mb,
int matC[][MAX], int nc, int mc)
{
if (ma != nb || na != nc || mb != mc) return 1;
int a = na, b = ma, c = mb;
for (int i = 0; i < a; i++) {
for (int k = 0; k < c; k++) {
matC[i][k] = 0;
for (int j = 0; j < b; j++) {
matC[i][k] += matA[i][j] * matB[j][k];
}
}
}
return 0;
}