-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05-matrix-add.c
67 lines (57 loc) · 1.35 KB
/
05-matrix-add.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
int main() {
// int a[10];
/*
1 2 1 3 1 6
3 4 5 6 15 24
*/
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c); // 3 X 4
// a[i][0] --> ith row in a and 0th element ithn the ith row
printf("\nEnter elements of Matrix A:\n");
for (i = 0; i < r; ++i){
for (j = 0; j < c; ++j) {
printf("Enter element A[%d][%d]: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("\nMatrix A\n");
for (i = 0; i < r; ++i){
for (j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("\nEnter elements of Matrix B:\n");
for (i = 0; i < r; ++i){
for (j = 0; j < c; ++j) {
printf("Enter element B[%d][%d]: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
printf("\nMatrix B\n");
for (i = 0; i < r; ++i){
for (j = 0; j < c; ++j) {
printf("%d ", b[i][j]);
}
printf("\n");
}
// adding two matrices
for (i = 0; i < r; ++i){
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] * b[i][j];
}
}
// printing the result
printf("\nSum of the matrices: \n");
for (i = 0; i < r; ++i){
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}