I'm having this error in the code and I can not find the problem, can someone point me to 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 get the values of matrix A and after matrix B. At that moment the error occurs. After that you should multiply the matrices and print the resulting matrix.
#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;
}