0
matrix
hi, sum of two matrix won't display. can you help? https://code.sololearn.com/cKh8lbZfHaiq
1 Resposta
+ 5
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], 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[1][1]: 5
Elements of 1st are a[1][2]: 1
Elements of 1st are a[2][1]: 6
Elements of 1st are a[2][2]: 2
Elements of 2nd matrix:
Elemets of 2nd are b[1][1]: -1
Elemets of 2nd are b[1][2]: 9
Elemets of 2nd are b[2][1]: 2
Elemets of 2nd are b[2][2]: -3
sum of two matrix is:
4 10
8 -1