-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix-test.cpp
104 lines (90 loc) · 2.94 KB
/
matrix-test.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
93
94
95
96
97
98
99
100
101
102
103
104
#include "matrix.cpp"
double** getmatrixinput(int &height, int &width) {
std::cout << "enter matrix height: ";
std::cin >> height;
std::cout << std::endl << "enter matrix width: ";
std::cin >> width;
std::cout << std::endl << "enter all values of the matrix row by row going from left to right starting from the top" << std::endl;
double** m = initializedouble2dpointerarray(width, height);
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
std::cout << std::endl << "enter value for index " << i << "," << j << ": ";
std::cin >> m[i][j];
}
}
return m;
}
void multiplicationtest() {
int m1height, m1width, m2height, m2width;
double** m1 = getmatrixinput(m1height, m1width);
double** m2 = getmatrixinput(m2height, m2width);
std::cout << "matrix 1:" << std::endl;
printmatrix(m1, m1height, m1width);
std::cout << "matrix 2:" << std::endl;
printmatrix(m2, m2height, m2width);
double** result = multiplymatrices(m1, m2, m1height, m1width, m2height, m2width);
std::cout << "result: " << std::endl;
printmatrix(result, m1height, m2width);
}
void determinanttest() {
int height, width;
double** matrix = getmatrixinput(height, width);
std::cout << "matrix: " << std::endl;
printmatrix(matrix, height, width);
int det = finddeterminant(matrix, height, width);
std::cout << "determinant = " << det << std::endl;
}
void minortest() {
int height, width, i, j;
double** matrix = getmatrixinput(height, width);
printmatrix(matrix, height, width);
std::cout << "please enter i: ";
std::cin >> i;
std::cout << std::endl << "please enter j: ";
std::cin >> j;
double** minor = findminor(matrix, height, width, i, j);
printmatrix(minor, height-1, width-1);
}
void inversetest() {
int height, width;
double** matrix = getmatrixinput(height, width);
std::cout << "input matrix: " << std::endl;
printmatrix(matrix, height, width);
double** inverse = inversematrix(matrix, height, width);
std::cout << "inverse matrix: " << std::endl;
printmatrix(inverse, height, width);
}
void multiplybyconstanttest() {
int height, width;
double constant;
double** matrix = getmatrixinput(height, width);
std::cout << "input matrix: " << std::endl;
printmatrix(matrix, height, width);
std::cout << "enter constant to multiply matrix: " << std::endl;
std::cin >> constant;
matrix = multiplybyconstant(matrix, height, width, constant);
std::cout << "output matrix: " << std::endl;
printmatrix(matrix, height, width);
}
int main() {
std::cout << "Enter: 1 to test matrix multiplication \n 2 to test determinants \n 3 to test minors \n 4 to test inverse \n 5 to test multiplication by constant"
int choice << std::cin;
switch choice
{
case 1:
multiplicationtest();
break;
case 2:
determinanttest();
break;
case 3:
minortest();
break;
case 4:
inversetest();
break;
case 5:
multiplybyconstanttest();
break;
}
}