Write a C program to add two matrices.
Test case:
Input : 3
3
22 33 44 11 23 21 66 87 45 66 44 23 78 12 45 98 65 37
3*3 - row*column value, rest input - matrix elements
Answers
Answer:
#include <stdio.h>
int main() {
int r , c , r1 , c1;
printf("Enter the number of rows and collumns for the first matrix \n");
scanf("%d" , &r);
scanf("%d" , &c);
int sum = 0 , sum1 = 0 , total = 0;
printf("Enter the number of rows and collumns of the second matrix \n");
scanf("%d" , &r1);
scanf("%d" , &c1);
int arr[r][c];
int arr1[r1][c1];
printf("Enter elements of the first matrix- \n");
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
scanf("%d" , &arr[i][j]);
}
}
printf("The first matrix \n");
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
sum = sum + arr[i][j];
printf("%d " , arr[i][j]);
}
printf("\n");
}
printf("Enter elements of the second matrix- \n");
for(int i = 0; i < r1; i++){
for(int j = 0; j < c1; j++){
scanf("%d" , &arr1[i][j]);
}
}
printf("The second matrix \n");
for(int i = 0; i < r1; i++){
for(int j = 0; j < c1; j++){
sum1 = sum1 + arr1[i][j];
printf("%d " , arr1[i][j]);
}
printf("\n");
}
total = sum + sum1;
printf("The sum of the 2 matrixes is %d" , total);
return 0;
}
Explanation: