#include<iostream.h>
void main()
{
float a[20][20],ratio,x[20];
int i,j,k,n;
cout<<"\nEnter the order of the matrix=";
cin>>n;
cout<<"\nEnter the coefficients & RHS \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+1;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<endl;
}
for(k=1;k<=n-1;k++)
{
for(i=k+1;i<=n;i++)
{
ratio=a[i][k]/a[k][k];
for(j=1;j<=n+1;j++)
a[i][j]-=ratio*a[k][j];
}
}
x[n]=a[n][n+1]/a[n][n];
for(k=n-1;k>=1;k--)
{
x[k]=a[k][n+1];
for(j=k+1;j<=n;j++)
x[k]-=a[k][j]*x[j];
x[k]=x[k]/a[k][k];
}
cout<<"\nThe Gauss-Elemination matrix is=\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
cout<<"\t\t"<<a[i][j];
}
cout<<endl;
}
for(i=1;i<=n;i++)
cout<<endl<<"\tx["<<i<<"]="<<x[i];
}
void main()
{
float a[20][20],ratio,x[20];
int i,j,k,n;
cout<<"\nEnter the order of the matrix=";
cin>>n;
cout<<"\nEnter the coefficients & RHS \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+1;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<endl;
}
for(k=1;k<=n-1;k++)
{
for(i=k+1;i<=n;i++)
{
ratio=a[i][k]/a[k][k];
for(j=1;j<=n+1;j++)
a[i][j]-=ratio*a[k][j];
}
}
x[n]=a[n][n+1]/a[n][n];
for(k=n-1;k>=1;k--)
{
x[k]=a[k][n+1];
for(j=k+1;j<=n;j++)
x[k]-=a[k][j]*x[j];
x[k]=x[k]/a[k][k];
}
cout<<"\nThe Gauss-Elemination matrix is=\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
cout<<"\t\t"<<a[i][j];
}
cout<<endl;
}
for(i=1;i<=n;i++)
cout<<endl<<"\tx["<<i<<"]="<<x[i];
}
No comments:
Post a Comment