0% found this document useful (0 votes)
158 views

C Program For Gauss-Elimination For Solving A System of Linear Equations PDF

This C++ program uses Gauss elimination to solve systems of linear equations. It takes in the number of equations as input and stores the augmented matrix in an array. It performs pivotization and Gaussian elimination on the matrix. It then uses back-substitution to calculate the values of the variables and prints the results.

Uploaded by

Irmala Ayu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views

C Program For Gauss-Elimination For Solving A System of Linear Equations PDF

This C++ program uses Gauss elimination to solve systems of linear equations. It takes in the number of equations as input and stores the augmented matrix in an array. It performs pivotization and Gaussian elimination on the matrix. It then uses back-substitution to calculate the values of the variables and prints the results.

Uploaded by

Irmala Ayu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C++ Program for Gauss-Elimination for solving a System of Linear Equations | 1

//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

Manas Sharma (c) Bragitoff.com


C++ Program for Gauss-Elimination for solving a System of Linear Equations | 2
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;
}

Sample 1

Manas Sharma (c) Bragitoff.com


C++ Program for Gauss-Elimination for solving a System of Linear Equations | 3
Sample 2

Tutorial Video:

Manas Sharma
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.

Share this:

Click to share on Facebook (Opens in new window)


Click to share on Twitter (Opens in new window)
Click to share on Google+ (Opens in new window)
Click to share on WhatsApp (Opens in new window)
Click to share on Pinterest (Opens in new window)
Click to share on Reddit (Opens in new window)
Click to share on LinkedIn (Opens in new window)
Click to share on Skype (Opens in new window)
Click to email this to a friend (Opens in new window)
Click to print (Opens in new window)
Click to share on Tumblr (Opens in new window)
Click to share on Pocket (Opens in new window)
Click to share on Telegram (Opens in new window)

Like this:

Like Loading...

Consider donating if you found the


information useful
Appreciate your blog: $3 ▼

Manas Sharma (c) Bragitoff.com

You might also like