-
Notifications
You must be signed in to change notification settings - Fork 16
/
20.c
104 lines (99 loc) · 2.35 KB
/
20.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, A[100][100], B[100][100], c[100][100],
d[100][100], row1, col1, row2, col2, k, sum = 0,
result[100][100];
printf("number of row and col for A matrix:\n");
scanf("%d %d", &row1, &col1);
// a matrix
// input
printf("A matrix is:\n");
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
printf("A[%d][%d]= ", i, j);
scanf("%d", &A[i][j]);
}
printf("\n");
}
// b matrix
// input
printf("number of row and col for B matrix:\n");
scanf("%d %d", &row2, &col2);
printf("B matrix is:\n");
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
printf("B[%d][%d]= ", i, j);
scanf("%d", &B[i][j]);
}
printf("\n");
}
// A output
printf("A=");
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
printf("\t%d", A[i][j]);
}
printf("\n");
}
// b output
printf("\n");
printf("B=");
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
printf("\t%d", B[i][j]);
}
printf("\n");
}
// sum
printf("sum of matrix:\n");
if (row1 == row2 && col1 == col2)
{
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
c[i][j] = A[i][j] + B[i][j];
printf("\t%d", c[i][j]);
}
printf("\n");
}
}
else
printf("math error");
// multiply
printf("multiply result of matrix:\n");
if (col1 == row2)
{
for (i = 0; i < row1; i++)
{
for (j = 0; j < col2; j++)
{
result[i][j] = 0;
for (k = 0; k < col1; k++)
{
result[i][j] += A[i][k] * B[k][j];
}
}
}
for (i = 0; i < row1; i++)
{
for (j = 0; j < col2; j++)
{
printf("\t%d", result[i][j]);
}
printf("\n");
}
}
else
printf("math error");
}