Seg Fault Core Dumped

Asked

Viewed 45 times

-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.

1 answer

4


I think you got caught up: there on line 15, you say:

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

But to multiply matrices, the amount of columns of the first matrix has to match the amount of lines of the second. This understanding is reinforced by line 63, where multiplication proceeds:

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

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

Therefore, the changes that need to be made are in line 15, saying if (cA != lB) instead of if (lA != cB) and on line 62 (the cause of segfault), where for(k=0; k<cB; k++) has to be replaced by 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 is (by coincidence).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.