-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample.c
46 lines (40 loc) · 1.23 KB
/
Sample.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
#include <stdio.h>
void input(int r, int c, int matrix[r][c], const char* name) {
printf("Enter elements of the %s Matrix:\n", name);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
scanf("%d", &matrix[i][j]);
}
}
}
void calculate(int r, int c, int matrix1[r][c], int matrix2[r][c], int result[r][c]) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
void display(int r, int c, int matrix[r][c]) {
printf("Output Matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int r, c;
printf("Enter the number of rows and columns of the matrices:\n");
scanf("%d %d", &r, &c);
if (r > 0 && c > 0) {
int matrix1[r][c], matrix2[r][c], result[r][c];
input(r, c, matrix1, "1st");
input(r, c, matrix2, "2nd");
calculate(r, c, matrix1, matrix2, result);
display(r, c, result);
} else {
printf("Error: Rows and columns should be greater than 0\n");
}
return 0;
}