#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j,k,n;
float a[10][10],x[10];
cout<<"Enter Order of Equation: ";
cin>>n;
cout<<"Enter Coefficients of Equation Row-wise\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
cin>>a[i][j];
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(j>i)
{
float s=a[j][i]/a[i][i];
for(k=1;k<=n+1;k++)
a[j][k]=a[j][k]-s*a[i][k];
}
}
}
setprecision(10);
x[n]=a[n][n+1]/a[n][n];
for(i=n-1;i>=1;i--)
{
float sum=0.0;
for(j=i+1;j<=n;j++)
{
sum=sum+a[i][j]*x[j];
}
x[i]=(a[i][n+1]-sum)/a[i][i];
}
cout<<"\nThe Solutions are:\n\n";
for(i=1;i<=n;i++)
{
cout<<"X"<<i<<"="<<x[i]<<"\n";
}
return(0);
}
#include<iomanip>
using namespace std;
int main()
{
int i,j,k,n;
float a[10][10],x[10];
cout<<"Enter Order of Equation: ";
cin>>n;
cout<<"Enter Coefficients of Equation Row-wise\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
cin>>a[i][j];
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(j>i)
{
float s=a[j][i]/a[i][i];
for(k=1;k<=n+1;k++)
a[j][k]=a[j][k]-s*a[i][k];
}
}
}
setprecision(10);
x[n]=a[n][n+1]/a[n][n];
for(i=n-1;i>=1;i--)
{
float sum=0.0;
for(j=i+1;j<=n;j++)
{
sum=sum+a[i][j]*x[j];
}
x[i]=(a[i][n+1]-sum)/a[i][i];
}
cout<<"\nThe Solutions are:\n\n";
for(i=1;i<=n;i++)
{
cout<<"X"<<i<<"="<<x[i]<<"\n";
}
return(0);
}
No comments:
Post a Comment