Home » Numerical Techniques Using C++ » Gauss Elimination Method C C++ Program & Algorithm

# Gauss Elimination Method C C++ Program & Algorithm

## Gauss Elimination Method C++

Gauss Elimination Method is a direct method to solve the system of linear equations.

It is quite general and well adaptive in computer operations and Numerical Techniques.

Gauss Elimination Method gives us the exact value of variables.

The simultaneous linear equations occur quite often in computational processes in almost every field.

• In engineering and science.
• Economics and Statistics
• Analysis of Electronic Circuits
• Output and Cost in Chemical Plants
• Optimizing Techniques
• Graphical methods and illustrations
• Numerical Techniques

So the solutions of these problems can be found by the Direct and Iterative Methods.

## x + y + z = 6 x – y + z = 22x – y + 3z = 9

Sol:  In this method, the variables are eliminated and the system is reduced to the upper triangular matrix from which the unknowns are found by back substitution.

### Step 1: Write the given System of Equations in the form of AX = b, i.e. Matrix Form.

Where as,
A = Coefficient Matrix,
X = variables (Column Matrix),
b = constants (Column Matrix.

### Step 3: Transform Augmented Matrix into Upper Triangular Matrix.

In the upper Triangular matrix, all the elements below the Diagonal are zero

Note: Only Row operations are allowed.

Now as you can see that it is now reduced to upper triangular matrix.

### Step 4:  Find equations corresponding to upper triangular matrix.

Now by using back Substitution reconstruct the equations and find the corresponding values of the variables x, y and z.

1.x + 1.y + 1.z = 6 —> x + y + z = 6
0.x – 2y + 0.z = – 4 —> -2y = -4 –> y = 2
0.x – 0.y + 2z = 6 —> 2z = 6 –> z = 3

Now, Put the value of y and z into eq. 1.
X + 2 + 3 = 6 –> x = 1
Therefore, the roots of the three equations are x = 1 , y = 2, z= 3

## C++ Program

```.hljs{display:block;overflow-x:auto;padding:.5em;color:#abb2bf;background:#282c34}.hljs-keyword,.hljs-operator{color:#f92672}.hljs-pattern-match{color:#f92672}.hljs-pattern-match .hljs-constructor{color:#61aeee}.hljs-function{color:#61aeee}.hljs-function .hljs-params{color:#a6e22e}.hljs-function .hljs-params .hljs-typing{color:#fd971f}.hljs-module-access .hljs-module{color:#7e57c2}.hljs-constructor{color:#e2b93d}.hljs-constructor .hljs-string{color:#9ccc65}.hljs-comment,.hljs-quote{color:#b18eb1;font-style:italic}.hljs-doctag,.hljs-formula{color:#c678dd}.hljs-section,.hljs-name,.hljs-selector-tag,.hljs-deletion,.hljs-subst{color:#e06c75}.hljs-literal{color:#56b6c2}.hljs-string,.hljs-regexp,.hljs-addition,.hljs-attribute,.hljs-meta-string{color:#98c379}.hljs-built_in,.hljs-class .hljs-title{color:#e6c07b}.hljs-attr,.hljs-variable,.hljs-template-variable,.hljs-type,.hljs-selector-class,.hljs-selector-attr,.hljs-selector-pseudo,.hljs-number{color:#d19a66}.hljs-symbol,.hljs-bullet,.hljs-link,.hljs-meta,.hljs-selector-id,.hljs-title{color:#61aeee}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}.hljs-link{text-decoration:underline}.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines) {white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```//Gauss Elimination Method C++ code
//wikkihut.com

#include<iostream>
#include<iomanip>
#include<cmath>
#define N 3
using namespace std;

int main()
{
float Matrix[N][N+1],x[N];
// Matrix = Augumented Matrix [Ad]
float temp,s;

//variables for loops
int i,j,k;

//Scan values of Matrix.

cout<<"Enter Elements of "<<N<<" Rows & "<<N+1<<" Columns\n";
cout<<fixed;

for(i=0; i<N; i++)
{
cout<<"\tEnter Row  "<<i+1<<" & Press Enter\n";
for(j=0; j<N+1; j++)
cin>>Matrix[i][j];
}

//make above matrix upper triangular Matrix

for(j=0; j<N-1; j++)
{
for(i=j+1; i<N; i++)
{
temp=Matrix[i][j]/Matrix[j][j];

for(k=0; k<N+1; k++)
Matrix[i][k]-=Matrix[j][k]*temp;
}
}

//print the Upper Triangular matrix

cout<<"\n ---------------------------------\n";
cout<<"\n Upper Triangular Matrix is:\n";
for(i=0; i<N; i++)
{
for(j=0; j<N+1; j++)
cout<<setw(8)<<setprecision(4)<<Matrix[i][j];
cout<<endl;
}

//find values of x,y,z using back substitution

cout<<"\n ---------------------------------\n";

for(i=N-1; i>=0; i--)
{
s=0;
for(j=i+1; j<N; j++)
s += Matrix[i][j]*x[j];
x[i]=(Matrix[i][N]-s)/Matrix[i][i];
}

//print values of x,y,z

cout<<"\n The Solution is:\n";
for(i=0; i<N; i++)
cout<<"x["<<setw(3)<<i+1<<"]="<<setw(7)<<setprecision(4)<<x[i]<<endl;

return 0;
//wikkihut.com
}``````

Method 2nd:
There is another method which is quite similar to this.
Step 1. Eliminate x from 2nd and 3rd equations.
Step 2. Eliminate y from 3rd equation only after step 1.
Step 3. Evaluate the unknowns, x, y, z by back substitution.