-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixDetCalc.cpp
More file actions
147 lines (133 loc) · 4.5 KB
/
MatrixDetCalc.cpp
File metadata and controls
147 lines (133 loc) · 4.5 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
long findDet(vector<vector<int>> m, int n);
int main() {
int size = 0;
cout << "Welcome to the Matrix Determinant Calculator.\n";
while (true) { //Get size of matrix, and store in stringstream
cout << "Please enter the size \"n\" of an n by n matrix: ";
string inSize;
getline(cin, inSize);
stringstream s(inSize);
if (s >> size && size >= 0)
break;
cout << "Invalid input. Please enter a positive integer.\n";
}
vector<vector<int>> matrix;
int temp;
vector<int> tempVec;
string inRow;
matrix.resize(size); //Create matrix of size n
for (int i = 0; i < size; i++) {
retry:
tempVec.clear();
cout << "Please enter row " << i + 1 << " of your matrix with each number separated by a space: ";
getline(cin, inRow);
stringstream t(inRow);
for (int j = 0; j < size; j++){
if (!(t >> temp)){ //Fail if input can't be stored in a stringstream
cout << "Invalid input. Please try again.\n";
t.clear();
t.str("");
goto retry;
}
else
tempVec.push_back(temp); //Store each number in tempVec
}
matrix[i] = tempVec; //tempVec should now be a row of numbers; store it in matrix
}
string yn;
string num;
do{
int max = 0;
for (int i = 0; i < size; i++) //Calculate the longest number in the matrix, so the matrix can be displayed without overlap
for (int j = 0; j < size; j++)
if (abs(matrix[i][j]) > max)
max = abs(matrix[i][j]);
cout << "The matrix you entered is:";
for (int i = 0; i < size; i++) {
cout << endl;
for (int j = 0; j < size; j++) //Displays matrix the user entered, and ensures that there's at least 3 spaces between each number
cout << setw((streamsize)(log(max)/log(10)+3)) << matrix[i][j];
}
cout << "\nIs this correct? y/n: "; //Confirmation that matrix is correct
getline(cin, yn);
while (yn != "n" && yn != "y" && yn != "N" && yn != "Y"){
cout << "Please enter \"y\" or \"n\": ";
getline(cin, yn);
}
if (yn == "n" || yn == "N"){ //Allows user to change specific matrix entries that may have been entered incorrectly
cout << "Enter the position you would like to change in <row> <column> format, e.g., enter \"1 2\" to specify the number in row 1, column 2: ";
take2:
getline(cin, num);
stringstream r(num);
int row, col, val;
if (!(r >> row && r >> col)){
cout << "Incorrect format. Please try again: ";
r.clear();
r.str("");
num = "";
goto take2;
}
if (row < 1 || col < 1 || row > size || col > size){
cout << "Specified location is outside of the matrix. Please try again: ";
r.clear();
r.str("");
num = "";
goto take2;
}
cout << "Now enter the new value: ";
dumbuserhere:
r.clear();
r.str("");
num = "";
getline(cin, num);
r << num;
if (!(r >> val)){
cout << "Invalid input. Please enter the new value: ";
goto dumbuserhere;
}
matrix[row-1][col-1] = val;
}
} while (yn == "n" || yn == "N");
long ans;
if (size == 1)
ans = matrix[0][0]; //Trivial answer for size 1 matrix
else
ans = findDet(matrix, size); //Calculate determinant using recursion
cout << "The determinant is " << ans << ".\n";
return 0;
}
long findDet(vector<vector<int>> m, int n){
int cur;
long total = 0;
//Initialize a 3D vector, which is used to store the multiple matrices created by the solving process.
vector<vector<vector<int>>> matrices(n, vector<vector<int>>(n-1 , vector<int>(n-1, m[1][1])));
if (n == 2) //Base case
return m[0][0] * m[1][1] - m[1][0] * m[0][1];
else {
for (int c = 0; c < n; c++) {
cur = m[0][c]; //cur points to each number in the first row sequentially, c is the horizontal index of that number
for (int i = 1; i < n; i++) { //i refers to each row
for (int j = 0, k = 0; j < n; j++, k++) {
//j and k are both for columns, but both are required since the submatrix has to skip the column associated with c
if (j == c) //Skips the column associated with c
k++;
if (k == n) //In case column skipped was the last column
break;
matrices[c][i-1][j] = m[i][k]; //Adds current number to current submatrix (specified by c)
}
}
if ((c - 1) % 2)
//If the index number is odd, add the resulting determinant of the current number multiplied by the corresponding submatrix to the total
total += cur*findDet(matrices[c], n - 1);
else //Otherwise, subtract it from the total
total -= cur*findDet(matrices[c], n - 1);
}
}
return total;
}