Nick Gz
Nick Gz

Reputation: 17

Multiplication of matrices by dynamic memory allocation and void functions

I was writing a code for assignment, the code compiles but it doesn't run successfully. It successfully functions till taking input matrices then it shows error

#include <stdio.h>
#include <stdlib.h>

int r1, r2, c1, c2, **mat1, **mat2, **mat3, i, j, t;
void check(int c1, int r2);
void input();
void inputDisplay();
void multiplication(int **mat1,int **mat2);
void displayOutput(int **mat3);


int main()
{
    printf("enter the dimensions of first matrix\n");
    scanf("%d%d", &r1, &c1);
    printf("enter the dimensions of second mateix\n");
    scanf("%d%d", &r2, &c2);

    check(c1, r2);

    mat1 = (int **)malloc(r1*sizeof(int *));
    for(i=0; i<c1; i++)
    {
        mat1[i] =  (int *)malloc(c1*sizeof(int));
    }
    mat2 = (int **)malloc(r2*sizeof(int *));
    for(i=0; i<c2; i++)
    {
        mat2[i] = (int *)malloc(c2*sizeof(int));
    }
    mat3 = (int **)malloc(r1*sizeof(int *));
    for(i=0; i<c2; i++)
    {
        mat3[i] = (int *)malloc(c2*sizeof(int));
    }

    input();
    inputDisplay();
    multiplication(mat1, mat2);
    displayOutput(mat3);

    return 0;
}

void check(int c1, int r2)
{
    if(c1 != r2)
    {
        printf("these two matrices are not compatible for multiplication");
        exit(EXIT_FAILURE);
    }
}

void input()
{
    printf("enter first matrix\n");
    for(i=0; i < r1; i++)
    {
        for(j=0; j<c1; j++)
        {
            printf("value of %d row and %d column:", i + 1, j +1);
            scanf("%d", &mat1[i][j]);
        }
    }
    printf("enter second matrix\n");
    for(i=0; i<r2; i++)
    {
        for(j=0; j<c2; j++)
        {
            printf("value of %d row and %d column:", i + 1, j +1);
            scanf("%d", &mat2[i][j]);
        }
    }
}

void inputDisplay()
{
    printf("first matrix is:");
    for(i=0; i<r1; i++)
    {
        printf("\n");
        for(j=0; j<c1; j++)
        {
            printf("%d\t", mat1[i][j]);
        }
    }
    printf("\nsecond matrix is:");
    for(i=0; i<r2; i++)
    {
        printf("\n");
        for(j=0; j<c2; j++)
        {
            printf("%d\t", mat2[i][j]);
        }
    }
}

void multiplication(int **mat1, int **mat2)
{
    for(i=0; i<r1; i++)
    {
        for(j=0; j<c2; j++)
        {
            mat3[i][j] = 0;
            for(t = 0; t < c1; t++);
            {
                mat3[i][j] += mat1[i][t]*mat2[t][j];
            }
        }
    }

}

void displayOutput(int **mat3)
{
    printf("resultant matrix is:");
    for(i=0; i<r1; i++)
    {
        printf("\n");
        for(j=0; j<c2; j++)
        {
          printf("%d\t", mat3[i][j]);
        }
    }
}

After taking input matrices it shows Process returned -1073741819 (0xC0000005) -or- 0 as resultant matrix (which is false) -or- some garbage value as resultant matrix

Sample input: rows and columns of both input matrices are 2. all the elements in both input matrices are 1.

Upvotes: 0

Views: 125

Answers (1)

Limina102
Limina102

Reputation: 1087

The problem is that a ; is added at the line for(t = 0; t < c1; t++);. The intended

        for(t = 0; t < c1; t++)
        {
            mat3[i][j] += mat1[i][t]*mat2[t][j];
        }

becomes

        for(t = 0; t < c1; t++);
        {
            mat3[i][j] += mat1[i][t]*mat2[t][j];
        }

which is equal to

t = c1;
mat3[i][j] += mat1[i][t]*mat2[t][j];

and finally mat3[i][j] += mat1[i][c1]*mat2[c1][j];, which causes the segment fault.

What's more, xing pointed out that there is also a problem in the allocation process.

mat1 = (int **)malloc(r1*sizeof(int *));
for(i=0; i<c1; i++)
{
    mat1[i] =  (int *)malloc(c1*sizeof(int));
}
mat2 = (int **)malloc(r2*sizeof(int *));
for(i=0; i<c2; i++)
{
    mat2[i] = (int *)malloc(c2*sizeof(int));
}
mat3 = (int **)malloc(r1*sizeof(int *));
for(i=0; i<c2; i++)
{
    mat3[i] = (int *)malloc(c2*sizeof(int));
}

which should be

mat1 = (int **)malloc(r1*sizeof(int *));
for(i=0; i<r1; i++)
{
    mat1[i] =  (int *)malloc(c1*sizeof(int));
}
mat2 = (int **)malloc(r2*sizeof(int *));
for(i=0; i<r2; i++)
{
    mat2[i] = (int *)malloc(c2*sizeof(int));
}
mat3 = (int **)malloc(r1*sizeof(int *));
for(i=0; i<r1; i++)
{
    mat3[i] = (int *)malloc(c2*sizeof(int));
}

With a debugger like gdb, it is revealable that there is a SIGSEGV at line mat3[i][j] += mat1[i][t]*mat2[t][j];.

Program received signal SIGSEGV, Segmentation fault.
0x0000555555555871 in multiplication (mat1=0x555555559ac0, mat2=0x555555559b20)
    at m.c:107
107                 mat3[i][j] += mat1[i][t]*mat2[t][j];

Then, print t outputs that current value of t is 2, which is out of range. With this information it is easier to locate the problem.

Upvotes: 3

Related Questions