PROGRAM :
/*To write a C/C++ program to find the solution of
the system of linear equations using Gauss Jordan
Method.*/
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,i,j,k;
cout<<"\nEnter the no. of equations\n";
cin>>n;
double 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; i++)
{
for (j = 0; j < n+1; j++)
if (j != i)
{
double d = a[j][i] / a[i][i];
for (k = 0; k < n+1; k++)
a[j][k] -= a[i][k] * d;
}
}
cout<<"\n\nThe matrix after gauss-jordan 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++){
cout<<(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-jordan elimination is as follows:
10.000000 0.000000 0.000000 0.000000 50.000000
0.000000 3.800000 0.000000 0.000000 15.200000
0.000000 0.000000 2.447368 0.000000 -17.131579
0.000000 0.000000 0.000000 9.924731 9.924731
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 Jordan
Method.*/
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,i,j,k;
cout<<"\nEnter the no. of equations\n";
cin>>n;
double 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; i++)
{
for (j = 0; j < n+1; j++)
if (j != i)
{
double d = a[j][i] / a[i][i];
for (k = 0; k < n+1; k++)
a[j][k] -= a[i][k] * d;
}
}
cout<<"\n\nThe matrix after gauss-jordan 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++){
cout<<(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-jordan elimination is as follows:
10.000000 0.000000 0.000000 0.000000 50.000000
0.000000 3.800000 0.000000 0.000000 15.200000
0.000000 0.000000 2.447368 0.000000 -17.131579
0.000000 0.000000 0.000000 9.924731 9.924731
The values of the variables are as follows:
5
4
-7
1