-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearSysResolution.cpp
More file actions
192 lines (153 loc) · 5 KB
/
linearSysResolution.cpp
File metadata and controls
192 lines (153 loc) · 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// linearSysResolution.cpp : Questo file contiene la funzione 'main', in cui inizia e termina l'esecuzione del programma.
//
//include libs
#include <iostream>
#include <math.h>
//N is the maximum number of unkowns possible NOTE: Tested with 3 unknowns
#define N 10
using namespace std;
class Matrix {
public:
float mat[N][N + 1];
int dim;
/* for dim equations there will be dim unknowns which will be stored in array 'res' */
float res[N];
Matrix() {
cout << "Matrice inizialied waiting function to run" << endl;
system("cls");
}
void LoadBidimensionalArray(int uniqueDimension)
{
dim = uniqueDimension;
int i, j;
for (i = 0; i < dim; i++)
{
for (j = 0; j < dim+1; j++)
{
cout << "Inserire numero da caricare nella matrice nella posizione [" << i + 1 << ";" << j + 1 << "]" << endl;
cin >> mat[i][j];
}
}
}
int LoadDim()
/* if no of equations are n then size of augmented matrix will be n*n+1. So here we are declaring 2d array 'mat' of size n+n+1 */
{
do
{
cout << "Inserisci dimensione: ";
cin >> dim;
if (dim >= N)
{
cout << "Error dimension too big error" << endl;
}
} while (dim > N);
return dim;
}
//display matrix
void DisplayBidimensional()
{
int i, j;
for (i = 0; i < dim; i++)
{
for (j = 0; j < dim+1; j++)
{
cout << "\nEcco l'elemento caricato nella posizione [" << i + 1 << ":" << j + 1 << "]"
<< ": " << mat[i][j] << endl;
}
}
cout << "*********End Matrice Print**********" << endl;
}
//Swap Raw operation
void sawpRow() {
int i, j, k;
for (i = 0; i < dim; i++)
{
for (j = i + 1; j < dim; j++)
{
if (abs(mat[i][i]) < abs(mat[j][i]))
{
for (k = 0; k < dim + 1; k++)
{
cout << "\nSwapping position[" << i << ";" << k << "] with position[" << j << ";" << k << "]\n";
/* swapping mat[i][k] and mat[j][k] */
mat[i][k] = mat[i][k] + mat[j][k];
mat[j][k] = mat[i][k] - mat[j][k];
mat[i][k] = mat[i][k] - mat[j][k];
}
}
}
}
};
void gaussianElimination() {
/* performing Gaussian elimination */
int i, j, k;
for (i = 0; i < dim - 1; i++)
{
for (j = i + 1; j < dim; j++)
{
float f = mat[j][i] / mat[i][i];
for (k = 0; k < dim + 1; k++)
{
mat[j][k] = mat[j][k] - f * mat[i][k];
}
}
}
}
void backSub() {
int i, j;
/* Backward substitution for discovering values of unknowns */
for (i = dim - 1; i >= 0; i--)
{
res[i] = mat[i][dim];
for (j = i + 1; j < dim; j++)
{
if (i != j)
{
res[i] = res[i] - mat[i][j] * res[j];
}
}
res[i] = res[i] / mat[i][i];
}
}
//Utility get dim
int getBidimesionDimension()
{
return dim;
}
//Show linear sys solutions
void showSystemSolutions() {
int i;
cout << "\nThe values of unknowns for the above equations=>\n";
for (i = 0; i < dim; i++)
{
cout << res[i] << "\n";
}
}
~Matrix() {
cout << "\nMatrix destroyed\n";
}
};
int main()
{
//int i, j, k, n;
int dim;
Matrix linearSystem;
dim = linearSystem.LoadDim();
linearSystem.LoadBidimensionalArray(dim);
//Debug
/*cout << "\n ===== Debug Output =====\n";
linearSystem.DisplayBidimensional();
cout << "\n ===== End Debug Output =====\n";*/
linearSystem.sawpRow();
linearSystem.gaussianElimination();
//Debug
/*cout << "\n ===== Debug Output =====\n";
linearSystem.DisplayBidimensional();
cout << "\n ===== End Debug Output =====\n";*/
linearSystem.backSub();
linearSystem.showSystemSolutions();
system("PAUSE");
return 0;
}
// Per eseguire il programma: CTRL+F5 oppure Debug > Avvia senza eseguire debug
// Per eseguire il debug del programma: F5 oppure Debug > Avvia debug