0

In the output, I get every addition right except the first one. In the output, it shows this :

enter the number of rows and columns of matrices to be added : 2 2
For matrix [a], Enter The Element at (0,0): 2
For matrix [a], Enter The Element at (0,1): 2
For matrix [a], Enter The Element at (1,0): 2
For matrix [a], Enter The Element at (1,1): 2

the matrix [a] is : 
2 2 
2 2 

For matrix [b], Enter The Element at (0,0): 22
For matrix [b], Enter The Element at (0,1): 2
For matrix [b], Enter The Element at (1,0): 
2
For matrix [b], Enter The Element at (1,1): 2

the matrix [b] is : 
22 2 
2 2 

Resultant matrix [c] : 
0 4 
4 4 

I don't know what caused the "0" in the position (0,0) of resultant matrix. I tried changing the i++ to ++i just to make sure its not the prefix and postfix problem but it doesn't seem to be it. I cannot figure out why I get 0 only in (0, 0). Please do help as I'm new to C. This is my code :

int add(int a[][20], int b[][20], int c[][20], int row, int col);
int row, col;
int a[100][20]; 
int b[100][20]; 
int c[100][20];
int main()
{
    printf("enter the number of rows and columns of matrices to be added : ");
    scanf("%d %d", &row, &col);
    for(int i=0;i<row;i++){
        for(int j=0;j<col;j++){
            printf("For matrix [a], Enter The Element at (%d,%d): ",i,j);
            scanf("%d", &a[i][j]);
        }
    }
    printf("\nthe matrix [a] is : \n");
    for(int i=0;i<row;i++){
        for(int j=0;j<col;j++){
            printf("%d", a[i][j]);
            printf(" ");
        }
        printf("\n");
    }
    printf("\n");
    for(int i=0;i<row;i++){
        for(int j=0;j<col;j++){
            printf("For matrix [b], Enter The Element at (%d,%d): ",i,j);
            scanf("%d", &b[i][j]);
        }
    }
    printf("\nthe matrix [b] is : \n");
    for(int i=0;i<row;i++){
        for(int j=0;j<col;j++){
            printf("%d", b[i][j]);
            printf(" ");
        }
        printf("\n");
    }
    printf("\n");
    int i, j;
    c[i][j] = add(a, b, c, row, col);
    printf("Resultant matrix [c] : \n");
    for (i=0; i<row; i++){
        for (j=0; j<col; j++){
            printf("%d", c[i][j]);
            printf (" ");
        }
        printf("\n");
    }
    return 0;
}

int add(int a[][20], int b[][20], int c[][20], int row, int col){
    int i, j;
    for (i=0; i<row; i++){
        for (j=0; j<col; j++){
            c[i][j] = a[i][j] + b[i][j];}}
    return c[i][j];
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • `c[i][j] = add(a, b, c, row, col);` -> `add(a, b, c, row, col);`. And make `add` a void function, returning `c[i][j]` makes no sense whatsoever. – Jabberwocky Aug 08 '21 at 09:26
  • @Jabberwocky thank you, tried that and code worked ^^ can I ask why returning an array made no sense? also, if I HAD to do this question with "int" instead of "void" would it be possible to not get an error? – wasabee Aug 08 '21 at 09:32
  • Returning an array would make sense, if it was possible in C, but it's not. And `return c[i][j]` does not return an array, but it returns a single element of the array. BTW 'i' and 'j' are out of range anyway after the end of the loops – Jabberwocky Aug 08 '21 at 09:35
  • Also read this: https://stackoverflow.com/questions/11656532/returning-an-array-using-c – Jabberwocky Aug 08 '21 at 09:37

1 Answers1

1

As the mentioned comments you don't need to retrun anything for matrix sumation because you have already changed that matrix by reference. Remove int retrun value of add and try something like that:

#include "stdio.h"
void add(int a[][20], int b[][20], int c[][20], int row, int col);
int row, col;
int a[100][20];
int b[100][20];
int c[100][20];
int main()
{
    printf("enter the number of rows and columns of matrices to be added : ");
    scanf("%d %d", &row, &col);
    for(int i=0;i<row;i++){
        for(int j=0;j<col;j++){
            printf("For matrix [a], Enter The Element at (%d,%d): ",i,j);
            scanf("%d", &a[i][j]);
        }
    }
    printf("\nthe matrix [a] is : \n");
    for(int i=0;i<row;i++){
        for(int j=0;j<col;j++){
            printf("%d", a[i][j]);
            printf(" ");
        }
        printf("\n");
    }
    printf("\n");
    for(int i=0;i<row;i++){
        for(int j=0;j<col;j++){
            printf("For matrix [b], Enter The Element at (%d,%d): ",i,j);
            scanf("%d", &b[i][j]);
        }
    }
    printf("\nthe matrix [b] is : \n");
    for(int i=0;i<row;i++){
        for(int j=0;j<col;j++){
            printf("%d", b[i][j]);
            printf(" ");
        }
        printf("\n");
    }
    printf("\n");
    int i, j;
    add(a, b, c, row, col);
    printf("Resultant matrix [c] : \n");
    for (i=0; i<row; i++){
        for (j=0; j<col; j++){
            printf("%d", c[i][j]);
            printf (" ");
        }
        printf("\n");
    }
    return 0;
}

void add(int a[][20], int b[][20], int c[][20], int row, int col){
    int i, j;
    for (i=0; i<row; i++){
        for (j=0; j<col; j++){
            c[i][j] = a[i][j] + b[i][j];}}
}
G. Emadi
  • 230
  • 1
  • 8