-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgauss elimination.cpp
60 lines (59 loc) · 2.37 KB
/
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
57
58
59
60
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int n,i,j,k;
cout.precision(4); //set precision
cout.setf(ios::fixed);
cout<<"\nEnter the no. of equations\n";
cin>>n; //input the no. of equations
float a[n][n+1],x[n]; //declare an array to store the elements of augmented-matrix
cout<<"\nEnter the elements of the augmented-matrix row-wise:\n";
for (i=0;i<n;i++)
for (j=0;j<=n;j++)
cin>>a[i][j]; //input the elements of array
for (i=0;i<n;i++) //Pivotisation
for (k=i+1;k<n;k++)
if (fabs(a[i][i])<fabs(a[k][i]))
for (j=0;j<=n;j++)
{
double temp=a[i][j];
a[i][j]=a[k][j];
a[k][j]=temp;
}
cout<<"\nThe matrix after Pivotisation is:\n";
for (i=0;i<n;i++) //print the new matrix
{
for (j=0;j<=n;j++)
cout<<a[i][j]<<setw(16);
cout<<"\n";
}
for (i=0;i<n-1;i++) //loop to perform the gauss elimination
for (k=i+1;k<n;k++)
{
double t=a[k][i]/a[i][i];
for (j=0;j<n;j++)
a[k][j]=a[k][j]-t*a[i][j]; //make the elements below the pivot elements equal to zero or elimnate the variables
}
cout<<"\n\nThe matrix after gauss-elimination is as follows:\n";
for (i=0;i<n;i++) //print the new matrix
{
for (j=0;j<=n;j++)
cout<<a[i][j]<<setw(16);
cout<<"\n";
}
for (i=n-1;i>=0;i--) //back-substitution
{ //x is an array whose values correspond to the values of x,y,z..
x[i]=a[i][n]; //make the variable to be calculated equal to the rhs of the last equation
for (j=i+1;j<n;j++)
if (j!=i) //then subtract all the lhs values except the coefficient of the variable whose value is being calculated
x[i]=x[i]-a[i][j]*x[j];
x[i]=x[i]/a[i][i]; //now finally divide the rhs by the coefficient of the variable to be calculated
}
cout<<"\nThe values of the variables are as follows:\n";
for (i=0;i<n;i++)
cout<<x[i]<<endl; // Print the values of x, y,z,....
return 0;
}