#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
#define N 4
int main()
{
float a[N][N+1],t;
int i,j,k;
cout<<"Enter the elements of the";
cout<<" augmented matrix rowwise"<<endl;
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
cin>>a[i][j];
cout<<fixed;
for(j=0;j<N;j++)
for(i=0;i<N;i++)
if(i!=j)
{
t=a[i][j]/a[j][j];
for(k=0;k<N+1;k++)
a[i][k] -= a[j][k]*t;
}
//printing the diagonal matrix
cout<<"The diagonal matrix is :-"<<endl;
for(i=0;i<N;i++)
{
for(j=0;j<N+1;j++)
cout<<setw(9)<<setprecision(4)<<a[i][j];
cout<<endl;
}
//Now printing the results
cout<<"The solution is:-"<<endl;
for(i=0;i<N;i++)
{cout<<"x["<<setw(3)<<i+1<<"]=";
cout<<setw(7)<<setprecision(4);
cout<<a[i][N]/a[i][i]<<endl;
}
return 0;
}
Output
Enter the elements of the augmented matrix rowwise
10 -6 3 5 7
-6 8 -2 -4 5
3 2 4 11 3
5 -8 -2 3 7
10 -6 3 5 7
-6 8 -2 -4 5
3 2 4 11 3
5 -8 -2 3 7
The diagonal matrix is :-
10.0000 0.0000 0.0000 0.0000 28.0721
0.0000 4.4000 0.0000 0.0000 8.9642
0.0000 0.0000 3.2727 0.0000 -12.5495
0.0000 0.0000 0.0000 11.1667 5.9306
The solution is:-
x[ 1]= 2.8072
x[ 2]= 2.0373
x[ 3]=-3.8346
x[ 4]= 0.5311
