Seg Fault Core Dumped [closed]

-1

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;
}
    
asked by anonymous 24.05.2017 / 02:35

1 answer

4

I think you've gotten involved: there on line 15, you say:

if (lA != cB)
{
    printf("\nImpossivel fazer a operação!!!");
    return 0;
}

But to multiply arrays, the amount of columns in the first array has to hit the number of lines in the second array. This understanding is reinforced by line 63, where multiplication proceeds:

MULT[i][j] += A[i][k] * B[k][j];

Notice how the index k is traversing the columns of A and the lines of B.

So the changes that need to be made are at line 15, saying if (cA != lB) instead of if (lA != cB) and line 62 (the cause of segfault), where for(k=0; k<cB; k++) has to be replaced with for(k=0; k<lB; k++)

With these two substitutions the code starts to work for non-square matrices; for square matrices, it already works as it is (by coincidence).

    
24.05.2017 / 03:45