 matrix | SoloLearn: Learn to code for FREE!

0

# matrix

hi, sum of two matrix won't display. can you help? https://code.sololearn.com/cKh8lbZfHaiq

1 Answer

New Answer

+5

#include <stdio.h> int main() { int r, c, a, b, sum, i, j; printf("Enter number of rows:"); scanf("%d", &r); printf("Enter number of columns:"); scanf("%d", &c); printf("Enter elements of 1st matrix:\n"); for (i = 0; i < r; i++) for (j = 0; j < c; j++) { printf("Elements of 1st are a[%d][%d]: ", i + 1, j + 1); scanf("%d", &a[i][j]); } printf("\n Elements of 2nd matrix:\n"); for (i = 0; i < r; i++) for (j = 0; j < c; j++) { printf("Elemets of 2nd are b[%d][%d]: ", i + 1, j + 1); scanf("%d", &b[i][j]); } for (i = 0; i < r; i++) for (j = 0; j < c; j++) { sum[i][j] = a[i][j] + b[i][j]; } printf("\n sum of two matrix is: \n\n"); for (i = 0; i < r; i++) { for (j = 0; j < c; j++) { printf("%-3d ", sum[i][j]); } printf("\n"); } } Sample Output: Enter number of rows:2 Enter number of columns:2 Enter elements of 1st matrix: Elements of 1st are a: 5 Elements of 1st are a: 1 Elements of 1st are a: 6 Elements of 1st are a: 2 Elements of 2nd matrix: Elemets of 2nd are b: -1 Elemets of 2nd are b: 9 Elemets of 2nd are b: 2 Elemets of 2nd are b: -3 sum of two matrix is: 4 10 8 -1