Question

This is actually a continued problem. Firstly, the idea was to append a matrix from a 1D array. Now I want it to be purely 2D. Like the first time, I'm having some problems with a segmentation fault, and I'm not sure where the problem is because I don't know how to use 2D arrays correctly. The program crashes with the following code. I've been fiddling around with it, but can't get it to work properly.

#include <stdio.h>
#include <stdlib.h>
#define ROWS 10
#define COLUMNS 10

void fillMatrix (int *matrix[ROWS][COLUMNS], int rows, int columns)
{
    int i, j;
    for(i = 0; i < rows; i++)
    {
        for(j= 0; j < columns; j++)
        {
            matrix[i][j] = (rand() % 10) + 1;
        }
    }
}
void printMatrix (int *matrix[ROWS][COLUMNS], int rows, int columns)
{
    int i, j;
    for(i = 0; i < rows; i++)
    {
        for(j= 0; j < columns; j++)
        {
            printf("%2d ", matrix[i][j]);
        }
        printf("\n\n");
    }
}
void sumMatrix (int *matrix[ROWS][COLUMNS], int *matrixA[ROWS][COLUMNS], int *matrixB[ROWS][COLUMNS], int rows, int columns)
{
    int i, j;
    for(i = 0; i < rows; i++)
    {
        for(j= 0; j < columns; j++)
        {
            matrix[i][j] = *matrixA[i][j] + *matrixB[i][j];
        }
    }
}

int main()
{
    int matrix[ROWS][COLUMNS], r, s, i;
    srand((unsigned) time(NULL));
    do{
    printf("Rows and columns (min 1 1, max 10 10) ");
    scanf("%d %d", &r, &s);
    }while(r > ROWS || r < 1 || s > COLUMNS || s < 1);

    int** matrixA;
    matrixA=(int**)malloc(r*sizeof(int*));
    for (i=0; i<r; i++)
        matrixA[i]=(int*)malloc(s*sizeof(int));

    printf("\nMatrix A:\n\n");
    fillMatrix(matrixA, r, s);
    printMatrix(matrixA, r, s);

    int** matrixB;
    matrixB=(int**)malloc(r*sizeof(int*));
    for (i=0; i<r; i++)
        matrixB[i]=(int*)malloc(s*sizeof(int));

    printf("\nMatrix B:\n\n");
    fillMatrix(matrixB, r, s);
    printMatrix(matrixB, r, s);

    int** matrixC;
    matrixC=(int**)malloc(r*sizeof(int*));
    for (i=0; i<r; i++)
        matrixC[i]=(int*)malloc(s*sizeof(int));

    printf("\nSummed up (Matrix C):\n\n");
    sumMatrix(matrixC, matrixA, matrixB, r, s);
    printMatrix(matrixC, r, s);

    free(matrixA);
    free(matrixB);
    free(matrixC);
    return 0;
}
Was it helpful?

Solution

Add time.h to the top and declare the function parameters as a pointer to a pointer and it should work.

Right now you have a triple pointer (a 2d array of pointers). Change it to int ** matrix for all similar parameters.

Similarly, remove all prepended asterisks from matrix operations inside your function:

matrix[i][j] = *matrixA[i][j] + *matrixB[i][j];

should be

matrix[i][j] = matrixA[i][j] + matrixB[i][j];

I removed brackets so the answer isn't excessively long, you probably should keep them in your code.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 10
#define COLUMNS 10

void fillMatrix (int **matrix, int rows, int columns)
{
    int i, j;
    for(i = 0; i < rows; i++)
        for(j= 0; j < columns; j++)
            matrix[i][j] = (rand() % 10) + 1;
}

void printMatrix (int **matrix, int rows, int columns)
{
    int i, j;
    for(i = 0; i < rows; i++)
    {
        for(j= 0; j < columns; j++)
            printf("%2d ", matrix[i][j]);
        printf("\n\n");
    }
}

void sumMatrix (int **matrix, int **matrixA, int **matrixB, int rows, int columns)
{
    int i, j;
    for(i = 0; i < rows; i++)
        for(j = 0; j < columns; j++)
            matrix[i][j] = matrixA[i][j] + matrixB[i][j];
}

int main()
{
    //...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top