-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab_03_gauss_elimination.cpp
56 lines (49 loc) · 1.21 KB
/
lab_03_gauss_elimination.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
#include <iostream>
using namespace std;
int main()
{
int n, i, j, k;
cout << "Enter number of unknowns: ";
cin >> n;
float a[n][n + 1], x[n], ratio;
cout << "Enter Coefficients of Augmented Matrix: " << endl;
for (i = 0; i < n; i++)
for (j = 0; j < n + 1; j++)
cin >> a[i][j];
/* Gaussian Elimination */
for (i = 0; i < n - 1; i++)
{
if (a[i][i] == 0.0)
{
cout << "Mathematical Error!";
return 0;
}
for (j = i + 1; j < n; j++)
{
ratio = a[j][i] / a[i][i];
for (k = 0; k <= n; k++)
{
a[j][k] = a[j][k] - ratio * a[i][k];
}
}
}
/* Back Substitution */
x[n - 1] = a[n - 1][n] / a[n - 1][n - 1];
for (i = n - 2; i >= 0; i--)
{
x[i] = a[i][n];
for (j = i + 1; j < n; j++)
{
x[i] -= a[i][j] * x[j];
}
x[i] = x[i] / a[i][i];
}
/* Display Solution */
cout << endl
<< "Solution: " << endl;
for (i = 0; i < n; i++)
{
cout << "x[" << i + 1 << "] = " << x[i] << endl;
}
return 0;
}