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;
}
c++ program output iterative method gauss seidel
Sample Output

Sample Output
Sample Output

More Resources:
More programs on Numerical Analysis:
http://www.bragitoff.com/2015/11/numerical-analysis-c-programs-for-various-techniques/
Lab Write-Up(with Algorithm and Flow-Chart):
http://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

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.

8 thoughts on “Gauss-Seidel(Iterative Method) For System of Linear Equations-C++ Program

    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: http://www.bragitoff.com/2015/10/gauss-seidel-lab-write-up-with-algorithm-and-flowchart/

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

      Good Day!

  1. 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.

  2. 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!
      Thanks for your feedback.

      The necessary condition for Gauss-Seidel method is that the matrix should be diagonally dominant.
      https://en.wikipedia.org/wiki/Diagonally_dominant_matrix
      So I employed the above(PP) procedure to make the matrix DD.
      However, as you have just pointed out, it’s not perfect as it assumes that just swapping the rows would make the matrix DD. However, like the example you just gave, I think the matrix cannot be made DD by simply swapping rows.
      So I don’t think the system is solvable by Gauss-Seidel.
      The program above would preform Partial Pivoting and you can check it’s output by displaying the matrix after the PP procedure, but the Gauss-Seidel process would never terminate as the system is not DD. So I guess there should be a stricter check on the DD condition.
      This is just my understanding of what’s going on here.
      Would love some more input from others. I am a physicist btw.

      Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *