# Gauss-Seidel(Iterative Method) For System of Linear Equations-C++ Program

So I wrote this piece of code for solving a system of linear equations using Gauss-Seidel’s Iterative method in the fifth semester of my undergraduate course for my Numerical Analysis Class. Hope you guys find it useful.

```//Gaus-seidel (Written by: Manas Sharma - University of Delhi)
#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 n 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;
}
```

More Resources:
More programs on Numerical Analysis:
https://www.bragitoff.com/2015/11/numerical-analysis-c-programs-for-various-techniques/
Lab Write-Up(with Algorithm and Flow-Chart):
https://www.bragitoff.com/2015/10/gauss-seidel-lab-write-up-with-algorithm-and-flowchart/
Video Explaining the above code:

http://equation-solver.org/
https://en.wikipedia.org/wiki/Equation  ## 8 thoughts on “Gauss-Seidel(Iterative Method) For System of Linear Equations-C++ Program”

1. It’s surprising to find on bragitoff.com a resource so precious about
equations.
We will note your page as a benchmark for Gauss-Seidel(Iterative
Method) For System of Linear Equations-C++ Program.
We also invite you to link and other web resources for equations like http://equation-solver.org/ or https://en.wikipedia.org/wiki/Equation.
Thank you ang good luck!

1. Hi Princeton University,
Thanks for your comment. Actually I wrote that program for my Numerical Methods class. I am a Physics Postgraduate at University of Delhi.

I have updated the above post now to include more resources.
You might also want to check out this video of explanation of the above code on my YouTube Channel: https://www.youtube.com/watch?v=2G13gmF-7ZE&t=9s
The Lab-Write Up of the code which includes the algorithm and flow-chart: https://www.bragitoff.com/2015/10/gauss-seidel-lab-write-up-with-algorithm-and-flowchart/

And more programs on Numerical Methods: https://www.bragitoff.com/2015/11/numerical-analysis-c-programs-for-various-techniques/

Good Day!

2. Hi Manas Sharma! Can you provide a version of this program in C language?

1. Hi Rubik!
C is not very different from C++ .
I suppose all you need to do is replace the ‘cout< <' and 'cin>>’ statememts with the C equivalent ‘printf’ and ‘scanf’. Try to do that it’s not very hard. I am too busy to do it on my own.

3. Hi Manas. I found your code very useful. I just had a small doubt in Pivotisation part.. Especially when the following system of equation is considered..
0x + 2y+ z = 1
2x – y +2z = -3
x – 2y + 0z = 2

How is Partial Pivoting gonna work when we go for the third equation?

Thank you Mr. Manas.. Its a great work to explain this code

1. Hi there!