Repository navigation
Expand file tree
/
Copy pathmatrix.c
More file actions
72 lines (58 loc) · 1.77 KB
/
Copy pathmatrix.c
File metadata and controls
72 lines (58 loc) · 1.77 KB
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
#include <stdio.h>
void normal2x2(int A[10][10], int B[10][10], int C[10][10]) {
C[1][1] = A[1][1] * B[1][1] + A[1][2] * B[2][1];
C[1][2] = A[1][1] * B[1][2] + A[1][2] * B[2][2];
C[2][1] = A[2][1] * B[1][1] + A[2][2] * B[2][1];
C[2][2] = A[2][1] * B[1][2] + A[2][2] * B[2][2];
}
void strassen2x2(int A[10][10], int B[10][10], int C[10][10]) {
int p, q, r, s, t, u, v;
p = (A[1][1] + A[2][2]) * (B[1][1] + B[2][2]);
q = (A[2][1] + A[2][2]) * B[1][1];
r = A[1][1] * (B[1][2] - B[2][2]);
s = A[2][2] * (B[2][1] - B[1][1]);
t = (A[1][1] + A[1][2]) * B[2][2];
u = (A[2][1] - A[1][1]) * (B[1][1] + B[1][2]);
v = (A[1][2] - A[2][2]) * (B[2][1] + B[2][2]);
C[1][1] = p + s - t + v;
C[1][2] = r + t;
C[2][1] = q + s;
C[2][2] = p + r - q + u;
}
void normalMultiply(int n, int A[10][10], int B[10][10], int C[10][10]) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
C[i][j] = 0;
for (int k = 1; k <= n; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
int main() {
int n;
printf("Enter matrix size (n x n): ");
scanf("%d", &n);
int A[10][10], B[10][10], C[10][10];
printf("Enter matrix A:\n");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &A[i][j]);
printf("Enter matrix B:\n");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &B[i][j]);
if (n == 2) {
normal2x2(A, B, C);
} else {
normalMultiply(n, A, B, C);
}
printf("Result matrix C:\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}