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];
}