-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_Matrix_Sum.c
49 lines (41 loc) · 1.27 KB
/
03_Matrix_Sum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
int main() {
int row, col, i, j;
// Input the number of rows and columns for the matrices
printf("Enter the number of rows: ");
scanf("%d", &row);
printf("Enter the number of columns: ");
scanf("%d", &col);
int matrix1[row][col], matrix2[row][col], sum[row][col];
// Input the elements of the first matrix
printf("Enter elements of the first matrix:\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
}
}
// Input the elements of the second matrix
printf("Enter elements of the second matrix:\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix2[i][j]);
}
}
// Calculate the sum of the matrices
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Print the sum matrix
printf("Sum of the matrices:\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}