//Gauss Elimination
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n,i,j,k;
cout.precision(4); //set precision
cout.setf(ios::fixed);
cout<<"\nEnter the no. of equations\n";
cin>>n; //input the no. of equations
float a[n][n+1],x[n]; //declare an array to store the elements of augmented-matrix
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]; //input the elements of array
for (i=0;i<n;i++) //Pivotisation
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<<"\nThe matrix after Pivotisation is:\n";
for (i=0;i<n;i++) //print the new matrix
{
for (j=0;j<=n;j++)
cout<<a[i][j]<<setw(16);
cout<<"\n";
}
for (i=0;i<n-1;i++) //loop to perform the gauss elimination
for (k=i+1;k<n;k++)
{
double t=a[k][i]/a[i][i];
for (j=0;j<=n;j++)
a[k][j]=a[k][j]-t*a[i][j]; //make the elements below the pivot elements equal to zero or elimnate the variables
}
cout<<"\n\nThe matrix after gauss-elimination is as follows:\n";
for (i=0;i<n;i++) //print the new matrix
{
for (j=0;j<=n;j++)
cout<<a[i][j]<<setw(16);
cout<<"\n";
}
for (i=n-1;i>=0;i--) //back-substitution
{ //x is an array whose values correspond to the values of x,y,z..
x[i]=a[i][n]; //make the variable to be calculated equal to the rhs of the last equation
for (j=i+1;j<n;j++)
if (j!=i) //then subtract all the lhs values except the coefficient of the variable whose value is being calculated
x[i]=x[i]-a[i][j]*x[j];
x[i]=x[i]/a[i][i]; //now finally divide the rhs by the coefficient of the variable to be calculated
}
cout<<"\nThe values of the variables are as follows:\n";
for (i=0;i<n;i++)
cout<<x[i]<<endl; // Print the values of x, y,z,....
return 0;
}

I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.

Hi there!
Actually I also noticed that there are some errors while using this code in Visual Studio. It turns out that we cannot define arrays this way in Visual Studio.
You can run the program on other compilers like Dev C++ if you want.

Hello Sakib,
I’m afraid, the program is not designed to handle the cases of infinite or no solution. However, you may some times get one of the solutions for the case of infinite solutions.

You could add a little code by yourself to determine if the system has no solution by checking if the Echelon Form you get after the Gaussian Elimination part has a row with all zeroes except in the last column. If you find such a row then the system has no solution. Similarly if a row has all zeroes then you have infinite solutions. Hope it helps!

A row with all zeroes but its last column a[i][n] being non zero makes up an invalid equation, like~ 0*x+0*y+….= nonzero
Which is incorrect 😀
Thanks Manas Sharma ^_^ 🙂

Hi Sara,
Does the code not run at all, or you are getting wrong results?
Also, I noticed there to be some problems with the algorithm I used in the above code, so you might wanna check out this version: https://www.bragitoff.com/2018/02/gauss-elimination-c-program/

Although the above is a C program not a C++ program.

Дякую, те що треба!

циклів надто багато , без образ.

Good Job.

How can i solve these errors in line 12

http://www.3rbz.com/uploads/7bf202e10ccd1.png

Hi there!

Actually I also noticed that there are some errors while using this code in Visual Studio. It turns out that we cannot define arrays this way in Visual Studio.

You can run the program on other compilers like Dev C++ if you want.

What are the cases when its impossible to solve & has more than one solution ?

Explain plz 🙂

Hello Sakib,

I’m afraid, the program is not designed to handle the cases of infinite or no solution. However, you may some times get one of the solutions for the case of infinite solutions.

You could add a little code by yourself to determine if the system has no solution by checking if the Echelon Form you get after the Gaussian Elimination part has a row with all zeroes except in the last column. If you find such a row then the system has no solution. Similarly if a row has all zeroes then you have infinite solutions. Hope it helps!

A row with all zeroes but its last column a[i][n] being non zero makes up an invalid equation, like~ 0*x+0*y+….= nonzero

Which is incorrect 😀

Thanks Manas Sharma ^_^ 🙂

Nice job!!!!

hey, whenever i run this program, its keeps returning ‘nan’ for the values of x, what do i do?

also, can you please help me asap, need it before tomorrow!!

the code doesn’t work with me .. any help ?

I used code block and doesn’t work

I’ll appreciate your help ,thanks

Hi Sara,

Does the code not run at all, or you are getting wrong results?

Also, I noticed there to be some problems with the algorithm I used in the above code, so you might wanna check out this version:

https://www.bragitoff.com/2018/02/gauss-elimination-c-program/

Although the above is a C program not a C++ program.

Hi Sara, Hi Manas,

the header should be changed a llitle bit:

#include

#include

#include // Reason – call of overloaded ‘abs(float&)’ is ambiguous

using namespace std;

The usage of namespace should also be eliminated 😉 …

The compilation using g++-5 and g++-8 using

g++-5 gauss_elim.pp -o gauss_elimin -Wall -Wextra -std=c++11 -O2

works properly!

Best regards and thank you for sharing this code!

Pavel

#include

#include

#include

using namespace std;

small question >>> why did you use >>> float a[n][n+1] in the code not float a[n][n+1]

#include ()

#include ()

#include ()

using namespace std;

Sorry guys – just add cmath 😀 …

what code should i add to have:

1 0 0 0 value

0 1 0 0 value

0 0 1 0 value

0 0 0 1 value

Can u help me with sth?

Plz email me and help me with ny question about this program