-1
I’m having this error in the code and I can’t find the problem, can anyone point out to me what I’m doing wrong? What the code does: I get row and column of matrix A, after matrix B, check if they can be multiplied. I receive the values of matrix A and after matrix B. At this moment the error occurs. After that the matrices should be multiplied and the resulting matrix printed.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i, j, k, lA, cA, lB, cB;
float **A=NULL, **B=NULL, **MULT=NULL;
printf("\nInsira a dimensão da matriz A:");
scanf("%d%d", &lA, &cA);
A = (float**) malloc (lA * sizeof(float*));
if(A == NULL)
exit(-1);
printf("\nInsira a dimensão da matriz B:");
scanf("%d%d", &lB, &cB);
if (lA != cB)
{
printf("\nImpossivel fazer a operação!!!");
return 0;
}else
B = (float **) malloc (lB * sizeof(float*));
if(B == NULL)
exit(-1);
printf("\nInsira a matriz A: ");
for(i=0; i<lA; i++)
{
A[i] = (float *) malloc(cA * sizeof(float));
if(A[i]==NULL)
exit(-1);
for(j=0; j<cA; j++)
scanf("%f", &A[i][j]);
}
printf("\n\nInsira a matriz B: ");
for(i=0; i<lB; i++)
{
B[i] = (float *) malloc(cB * sizeof(float));
if(B[i]==NULL)
exit(-1);
for(j=0; j<cB; j++)
scanf("%f", &B[i][j]);
}
MULT = (float**)malloc(lA*sizeof(float*));
if(MULT==NULL)
exit(-1);
for(i=0; i<lA; i++)
{
MULT[i]=(float*)malloc(cB*sizeof(float));
if(MULT[i]==NULL)
exit(-1);
}
printf("\nMatriz produto: ");
for(i=0; i <lA; i++)
{
printf("\n[");
for(j=0; j<cB; j++)
{
for(k=0; k<lA; k++)
MULT[i][j] += A[i][k] * B[k][j];
printf(" %.1f", MULT[i][j]);
}
printf("]");
}
for(i=0; i<lA; i++)
free(A[i]);
free(A);
for(i=0; i<lB; i++)
free(B[i]);
free(B);
for(i=0; i<lA; i++)
free(MULT[i]);
free(MULT);
return 0;
}
I drizzled in my machine two matrices of order 2 and it worked of good.
– Sullyvan Nunes