PROGRAM :
/*To write a C/C++ program to find the solution of
the system of linear equations using Gauss Elimination
Method.*/
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,i,j,k;
cout<<"\nEnter the no. of equations\n";
cin>>n;
float a[n][n+1],x[n];
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];
for (i=0;i<n-1;i++){
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];
}
}
cout<<"\n\nThe matrix after gauss-elimination is as follows:\n";
for (i=0;i<n;i++)
{
for (j=0;j<=n;j++)
printf("%4f\t",a[i][j]);
printf("\n");
}
for(i=1;i<n;i++)
{
for (j=0;j<i;j++)
{
a[i][j]=0;
}
}
for (i=n-1;i>=0;i--)
{
x[i]=a[i][n];
for (j=i+1;j<n;j++)
if (j!=i)
x[i]=x[i]-a[i][j]*x[j];
x[i]=x[i]/a[i][i];
}
cout<<"\nThe values of the variables are as follows:\n";
for (i=0;i<n;i++){
if (i==n-1)
cout<<floor(x[i])<<endl;
else
cout<<round(x[i])<<endl;
}
return 0;
}
OUTPUT :
Enter the no. of equations
4
Enter the elements of the augmented-matrix row-wise:
10 -7 3 5 6
-6 8 -1 -4 5
3 1 4 11 2
5 -9 -2 4 7
The matrix after gauss-elimination is as follows:
10.000000 -7.000000 3.000000 5.000000 6.000000
0.000000 3.800000 0.800000 -1.000000 8.600000
-0.000000 -0.000000 2.447368 10.315789 -6.815791
0.000000 -0.000000 -0.000000 9.924731 9.924732
The values of the variables are as follows:
5
4
-7
1
/*To write a C/C++ program to find the solution of
the system of linear equations using Gauss Elimination
Method.*/
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,i,j,k;
cout<<"\nEnter the no. of equations\n";
cin>>n;
float a[n][n+1],x[n];
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];
for (i=0;i<n-1;i++){
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];
}
}
cout<<"\n\nThe matrix after gauss-elimination is as follows:\n";
for (i=0;i<n;i++)
{
for (j=0;j<=n;j++)
printf("%4f\t",a[i][j]);
printf("\n");
}
for(i=1;i<n;i++)
{
for (j=0;j<i;j++)
{
a[i][j]=0;
}
}
for (i=n-1;i>=0;i--)
{
x[i]=a[i][n];
for (j=i+1;j<n;j++)
if (j!=i)
x[i]=x[i]-a[i][j]*x[j];
x[i]=x[i]/a[i][i];
}
cout<<"\nThe values of the variables are as follows:\n";
for (i=0;i<n;i++){
if (i==n-1)
cout<<floor(x[i])<<endl;
else
cout<<round(x[i])<<endl;
}
return 0;
}
OUTPUT :
Enter the no. of equations
4
Enter the elements of the augmented-matrix row-wise:
10 -7 3 5 6
-6 8 -1 -4 5
3 1 4 11 2
5 -9 -2 4 7
The matrix after gauss-elimination is as follows:
10.000000 -7.000000 3.000000 5.000000 6.000000
0.000000 3.800000 0.800000 -1.000000 8.600000
-0.000000 -0.000000 2.447368 10.315789 -6.815791
0.000000 -0.000000 -0.000000 9.924731 9.924732
The values of the variables are as follows:
5
4
-7
1