-
Notifications
You must be signed in to change notification settings - Fork 0
/
mprod.cpp
92 lines (74 loc) · 2.05 KB
/
mprod.cpp
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
/* Created by IVAN ARENA @ivanprfbs - 2021 */
#include <iostream>
using namespace std;
void buildMatrix(int* A, int n);
void printMatrix(int* A, int m, int n);
void transpose(int* B, int n, int* BT);
void storeProduct(int* A, int m, int n, int* B, int x, int y, int* AxB);
int multiply(int* A, int m, int n, int* B, int x, int y);
// takes two m*n matrixes of integers as input and find their product
int main() {
int m, n, x, y;
do {
cout << "Enter first matrix dimensions: ";
cin >> m >> n;
cout << "Enter second matrix dimensions: ";
cin >> x >> y;
} while (n != x); // n of columns of the first matrix must be equal to n of rows of the second matrix
int A[m][n];
cout << "Input first matrix:" << endl;
buildMatrix(*A, m*n); // first matrix
int B[x][y];
cout << "Input second matrix:" << endl;
buildMatrix(*B, x*y); // second matrix
int BT[y][x];
for (int i = 0; i < x; i++) { // transposes second matrix to simplify operations
for (int j = 0; j < y; j++) {
BT[j][i] = B[i][j];
}
}
int AxB[m][y]; // creates product matrix
storeProduct(*A, m, n, *BT, x, y, *AxB);
cout << endl;
printMatrix(*AxB, m, y);
return 0;
}
void buildMatrix(int* A, int n) {
if (n != 0) {
cin >> *A;
buildMatrix(A+1, n-1);
}
}
void printMatrix(int* A, int m, int n) {
for (int i = 0; i < m*n; i++) {
if (i % n == 0) {
cout << endl;
}
cout << *(A + i) << " ";
}
cout << endl;
}
void storeProduct(int* A, int m, int n, int* B, int x, int y, int* AxB) { // moves in rows
if (m != 0) {
for (int i = 0; i < y; i++) {
*(AxB + i) = multiply(A, m, n, B+(i*x), x, y);
}
storeProduct(A+n, m-1, n, B, x, y, AxB+y);
}
}
int multiply(int* A, int m, int n, int* B, int x, int y) { // row-column product
if (!n) {
return 0;
} else {
return *A * *B + multiply(A+1, m, n-1, B+1, x, y);
}
}
/* TEST:
2 3
3 2
1 -2 1
2 1 3
2 1
3 2
1 1
*/