#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
#define N 3
int main()
{
float a[N][N+1],x[N],aerr,maxerr,t,s,err;
int i,j,itr,maxitr;
//1st Initializing array x
for(i=0;i<N;i++)
{x[i]=0;}
cout<<"Enter element of augmented matrix row wise "<<endl;
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
cin>>a[i][j];
cout<<"Enter the allowed error,";
cout<<"maximum iterations"<<endl;
cin>>aerr>>maxitr;
cout<<fixed;
cout<<"Iteration"<<setw(6)<<"x[1]";
cout<<setw(11)<<"x[2]";
cout<<setw(11)<<"x[3]"<<endl;
for(itr=1;itr<=maxitr;itr++)
{
maxerr=0;
for(i=0;i<N;i++)
{
s=0;
for(j=0;j<N;j++)
if(j != i)
s += a[i][j]*x[j];
t=(a[i][N]-s)/a[i][i];
err=fabs(x[i]-t);
if(err>maxerr)
maxerr=err;
x[i]=t;
}
cout<<setw(5)<<itr;
for(i=0;i<N;i++)
cout<<setw(11)<<setprecision(4)<<x[i];
cout<<endl;
if(maxerr<aerr)
{
cout<<"Converges in"<<setw(3)<<itr;
cout<<"iterations"<<endl;
for(i=0;i<N;i++)
{cout<<"x["<<setw(3)<<i+1<<"]=";
cout<<setw(7)<<setprecision(4)<<x[i]<<endl;}
return 0;
}
cout<<"Solution does not converge,";
cout<<" iterations not sufficient"<<endl;
return 1;
}
}
Output
Enter element of augmented matrix row wise
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
Enter the allowed error,maximum iterations
Iteration x[1] x[2] x[3]
1 0.5000 0.9167 -0.1944
Converges in 1iterations
x[ 1]= 0.5000
x[ 2]= 0.9167
x[ 3]=-0.1944
