-3

How to rewrite this program by using various functions. Program to find the sum and difference of two dynamic matrices. It uses pointer to pointer instead of two dimensional matrix for dynamic size of the matrix

#include<stdio.h>
#include<stdlib.h>
int main()
{   int i, n, j, **a, **b, **c, **d;
    printf("Input the size of matrix [if n x n, input n]: ");
    scanf("%d", &n);
    printf("\nInput matrix A\n");
    a=(int**)calloc(n, sizeof(int*));
    for(i=0; i<n; i++)
    {   *(a+i)=(int*)calloc(n, sizeof(int));
        if(!*a)
        {   printf("\nInsufficient memory");
            exit(0);
        }
        for(j=0; j<n; j++)
        {   
            printf("Enter [%d][%d] element: ", i, j);
            scanf("%d", (*(a+i)+j));
            printf("%d", *(*(a+i)+j));
        }
    }
    printf("\nInput matrix B\n");
    b=(int**)calloc(n, sizeof(int*));
    for(i=0; i<n; i++)
    {   *(b+i)=(int*)calloc(n, sizeof(int));
        if(!*b)
        {   printf("\nInsufficient memory");
            exit(0);
        }
        for(j=0; j<n; j++)
        {   
            printf("Enter [%d][%d] element: ", i, j);
            scanf("%d", (*(b+i)+j));
            printf("%d", *(*(b+i)+j));
        }
    }
    c=(int**)calloc(n, sizeof(int*));
    d=(int**)calloc(n, sizeof(int*));
    for(i=0; i<n; i++)
    {   *(c+i)=(int*)calloc(n, sizeof(int));
        *(d+i)=(int*)calloc(n, sizeof(int));
        if(!(*c&&*d))
        {   printf("Insufficient memory");
            exit(0);
        }
        for(j=0; j<n; j++)
        {   *(*(c+i)+j)=(*(*(a+i)+j))+(*(*(b+i)+j));
            *(*(d+i)+j)=(*(*(a+i)+j))-(*(*(b+i)+j));
            printf("\nA[%d][%d]= %d   B[%d][%d]= %d \t Sum= %d \t Diff= %d", i, j, *(*(a+i)+j),     i, j, *(*(b+i)+j), *(*(c+i)+j), *(*(d+i)+j));
        }
    }
    free(c);
    free(d);
    free(a);
    free(b);
    return 0;
}

I have written this code but, it is not working correctly

#include<stdio.h>
#include<stdlib.h>
void input(int n, int **a);
void operation(int n, int **a, int **b);
int main()
{   int i, n, j, **a, **b, **c, **d;
    printf("Input the size of matrix [if n x n, input n]: ");
    scanf("%d", &n);
    printf("\nInput matrix A\n");
    input(n, a);
    printf("\nInput matrix B\n");
    input(n, b);
    operation(n, a, b);
    free(c);
    free(d);
    free(a);
    free(b);
    return 0;
}
void input(int n, int **a)
{   int i, j;
    a=(int**)calloc(n, sizeof(int*));
    for(i=0; i<n; i++)
    {   *(a+i)=(int*)calloc(n, sizeof(int));
        if(!*a)
        {   printf("\nInsufficient memory");
            exit(0);
        }
        for(j=0; j<n; j++)
        {   
            printf("Enter [%d][%d] element: ", i, j);
            scanf("%d", (*(a+i)+j));
            printf("%d", *(*(a+i)+j));
        }
    }
}
void operation(int n, int**a, int**b)
{   int i, j, **c, **d;
    a=(int**)calloc(n, sizeof(int*));
    b=(int**)calloc(n, sizeof(int*));
    c=(int**)calloc(n, sizeof(int*));
    d=(int**)calloc(n, sizeof(int*));
    for(i=0; i<n; i++)
    {   *(a+i)=(int*)calloc(n, sizeof(int));
        *(b+i)=(int*)calloc(n, sizeof(int));
        *(c+i)=(int*)calloc(n, sizeof(int));
        *(d+i)=(int*)calloc(n, sizeof(int));
        if(!(*a&&*b&&*c&&*d))
        {   printf("Insufficient memory");
            exit(0);
        }
        for(j=0; j<n; j++)
        {   *(*(c+i)+j)=(*(*(a+i)+j))+(*(*(b+i)+j));
            *(*(d+i)+j)=(*(*(a+i)+j))-(*(*(b+i)+j));
            printf("\nA[%d][%d]= %d   B[%d][%d]= %d \t Sum= %d \t Diff= %d", i, j, *(*(a+i)+j), i, j, *(*(b+i)+j), *(*(c+i)+j), *(*(d+i)+j));
        }
    }
    free(c);
    free(d);
}

2 Answers2

2

Problem:

int **a is local to input function. So when you return from the function, value of local a is lost, and value of a in main will remain garbage. You must pass address of variable holding the matrix to make assigment stick. In other words the parameter should be int ***a.

Solution:

But you don't really need that parameter at all. You could just return the array:

int** input(int n)
{   
    int i, j;
    int ** a = calloc(n, sizeof(int*));
    for(i=0; i<n; i++)
    {   
        a[i] = calloc(n, sizeof(int));
        if(!a[i])

        ...

     return a;
}

Other problems:

Community
  • 1
  • 1
user694733
  • 15,208
  • 2
  • 42
  • 68
1
#include <stdio.h>
#include <stdlib.h>

int **new_imat(int n);
void drop_imat(int n, int **mat);
void input(int n, int **mat);
void operation(int n, int **mat_a, int **mat_b);

int main(){
    int i, j, n;
    int **a, **b;
    printf("Input the size of matrix [if n x n, input n]: ");
    scanf("%d", &n);
    a = new_imat(n);
    b = new_imat(n);
    printf("\nInput matrix A\n");
    input(n, a);
    printf("\nInput matrix B\n");
    input(n, b);
    operation(n, a, b);
    drop_imat(n, a);
    drop_imat(n, b);
    return 0;
}

typedef enum opt {
    NONINIT, INIT
} OPT;

void *alloc_with_err(size_t size, size_t type_size, OPT opt){
    void *ret = opt ? calloc(size, type_size) : malloc(size*type_size);
    if(!ret){
        fprintf(stderr, "\nInsufficient memory");
        exit(EXIT_FAILURE);
    }
    return ret;
}

int **new_imat(int n){
    int r, **mat;
    mat = alloc_with_err(n, sizeof(int*), NONINIT);
    for(r = 0; r < n; ++r){
        mat[r] = alloc_with_err(n, sizeof(int), INIT);
    }
    return mat;
}
void drop_imat(int n, int **mat){
    int r;
    for(r = 0; r < n; ++r)
        free(mat[r]);
    free(mat);
}

void input(int n, int **mat){
   int r, c;
    for(r=0; r<n; r++){
        for(c=0; c<n; c++){
            printf("Enter [%d][%d] element: ", r, c);
            scanf("%d", (*(mat+r)+c));
        }
    }
}
void print(int n, int **mat){
   int r, c;
    for(r=0; r<n; r++){
        for(c=0; c<n; c++){
            printf("%d ", mat[r][c]);
        }
        printf("\n");
    }
}

void operation(int n, int **a, int **b){
    int r, c;
    int **sum=new_imat(n);
    int **dif=new_imat(n);
    for(r=0; r < n; r++){
        for(c=0; c < n; c++){
           sum[r][c] = a[r][c] + b[r][c];
           dif[r][c] = a[r][c] - b[r][c];
        }
    }
    printf("A:\n");print(n, a);
    printf("B:\n");print(n, b);
    printf("Sum:\n"); print(n, sum);
    printf("Diff:\n");print(n, dif);
    drop_imat(n, sum);
    drop_imat(n, dif);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70