#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
float a[20][20],x[20],e;
int i,j,k,n,count=0;
cout<<"Enter Order of equation: ";
cin>>n;
for(i=0;i<n;i++)
x[i]=0;
cout<<"Enter Coefficients of equation row-wise:\n";
cout<<"/*Rule to enter Equation*/\n";
cout<<"The maximum coefficient of x will be the first equation\n";
cout<<"The maximum coefficient of y be the Second equation and so on\n";
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
cin>>a[i][j];
}
cout<<setprecision(4);
do
{
count++;
for(i=0;i<n;i++)
{
float sum=0.0;
for(j=0;j<n;j++)
{
if(j!=i)
sum+=a[i][j]*x[j];
}
float temp=(a[i][n]-sum)/a[i][i];
e=fabs(x[i]-temp);
x[i]=temp;
}
cout<<"Iteration "<<count<<"\t";
for(i=0;i<n;i++)
cout<<x[i]<<"\t";
cout<<"\n";
}while(e>0.0001);
cout<<"\nSolution Converges in "<<count<<" iteration\n";
for(i=0;i<n;i++)
cout<<"X"<<i<<"=\t"<<x[i]<<endl;
return 0;
}
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
float a[20][20],x[20],e;
int i,j,k,n,count=0;
cout<<"Enter Order of equation: ";
cin>>n;
for(i=0;i<n;i++)
x[i]=0;
cout<<"Enter Coefficients of equation row-wise:\n";
cout<<"/*Rule to enter Equation*/\n";
cout<<"The maximum coefficient of x will be the first equation\n";
cout<<"The maximum coefficient of y be the Second equation and so on\n";
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
cin>>a[i][j];
}
cout<<setprecision(4);
do
{
count++;
for(i=0;i<n;i++)
{
float sum=0.0;
for(j=0;j<n;j++)
{
if(j!=i)
sum+=a[i][j]*x[j];
}
float temp=(a[i][n]-sum)/a[i][i];
e=fabs(x[i]-temp);
x[i]=temp;
}
cout<<"Iteration "<<count<<"\t";
for(i=0;i<n;i++)
cout<<x[i]<<"\t";
cout<<"\n";
}while(e>0.0001);
cout<<"\nSolution Converges in "<<count<<" iteration\n";
for(i=0;i<n;i++)
cout<<"X"<<i<<"=\t"<<x[i]<<endl;
return 0;
}
No comments:
Post a Comment