//Gaus-seidel
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout.precision(4);
cout.setf(ios::fixed);
int n,i,j,k,flag=0,count=0;
cout<<"\nEnter the no. of equations\n";
cin>>n; //Input no. of equations
double a[n][n+1]; //declare a 2d array for storing the elements of the augmented matrix
double x[n]; //declare an array to store the values of variables
double eps,y;
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];
cout<<"\nEnter the initial values of the variables:\n";
for (i=0;i<n;i++)
cin>>x[i];
cout<<"\nEnter the accuracy upto which you want the solution:\n";
cin>>eps;
for (i=0;i<n;i++) //Pivotisation(partial) to make the equations diagonally dominant
for (k=i+1;k<n;k++)
if (abs(a[i][i])<abs(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<<"Iter"<<setw(10);
for(i=0;i<n;i++)
cout<<"x"<<i<<setw(18);
cout<<"\n----------------------------------------------------------------------";
do //Perform iterations to calculate x1,x2,...xn
{
cout<<"\n"<<count+1<<"."<<setw(16);
for (i=0;i<n;i++) //Loop that calculates x1,x2,...xn
{
y=x[i];
x[i]=a[i][n];
for (j=0;j<n;j++)
{
if (j!=i)
x[i]=x[i]-a[i][j]*x[j];
}
x[i]=x[i]/a[i][i];
if (abs(x[i]-y)<=eps) //Compare the ne value with the last value
flag++;
cout<<x[i]<<setw(18);
}
cout<<"\n";
count++;
}while(flag<n); //If the values of all the variables don't differ from their previious values with error more than eps then flag must be 3 and hence stop the loop
cout<<"\n The solution is as follows:\n";
for (i=0;i<n;i++)
cout<<"x"<<i<<" = "<<x[i]<<endl; //Print the contents of x[]
return 0;
}

Hey! I am a Physics Major at the University of Delhi.
I dream of being a Theoretical Physicist one day. I love playing Guitar in my free time. I can speak English, Hindi and a little French. I am also a Web Development Enthusiast and a hardcore Gamer.