-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumericalMethods_GaussJordanMethod.cpp
69 lines (59 loc) · 1.39 KB
/
NumericalMethods_GaussJordanMethod.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
/// GAUSS JORDAN METHOD
#include<bits/stdc++.h>
using namespace std;
typedef double dd;
int main()
{
freopen("_in.txt", "r", stdin);
int totalEq;
printf("ENTER NUMBER OF EQUATIONS = ");
cin >> totalEq;
int k;
dd tmp;
dd pivot;
dd matrixA[totalEq][totalEq+1];
dd root[totalEq];
dd matrixB[totalEq];
cout << "ENTER COEFFICIENTS OF EQUATIONS = ";
puts("");
for(int i=0; i<totalEq; i++)
{
for(int j=0; j<=totalEq; j++)
{
cin >> matrixA[i][j];
}
}
///input done
/////////////////////GAUSS-JORDAN
for(int i=0; i<totalEq; i++)
{
pivot = matrixA[i][i];
for(int j=0; j<=totalEq; j++)
{
matrixA[i][j] /= pivot;
}///i-th equation normalized
for(int k=(i+1)%totalEq; k!=i; k=(k+1)%totalEq)
{
tmp = matrixA[k][i];
for(int j=0; j<=totalEq; j++)
{
matrixA[k][j] -= tmp*matrixA[i][j];
}
}
for(int p=0; p<totalEq; p++)
{
for(int q=0; q<=totalEq; q++)
{
cout << fixed << setprecision(1) << matrixA[p][q] << " ";
}
cout << endl;
}
cout << endl;
}
cout << "Required roots: ";
for(int i=0; i<totalEq; i++)
{
cout << matrixA[i][totalEq] << " ";
}
return 0;
}