Monday, February 2, 2015

Augmented Matrix Solution By reducing it to Row Echelon Form in C++

Read Also:  


Augmented Matrix Solution By reducing it to Row Echelon Form And then Calculate the result by backward substitution  (Code In C++)....


#include<iostream.h>
#include<conio.h>
class row{
    int i,j,k,n,a;
    float A[20][20],c,x[10],sum;
    public:
    row(){

    sum=0.0;
    cout<<"\nEnter the order of matrix: ";
    cin>>n;
    a=n;
    cout<<"\nEnter the elements of augmented matrix row-wise:\n\n";
    for(i=1; i<=n; i++)
    {
for(j=1; j<=(n+1); j++)
{
  if(a<j){cout<<"\nEnter the Value of B: (1st row):- \n ";}
   cout<<"A["<<i<<"]["<<j<<"] : ";
   cin>>A[i][j];
}
    }
    }

    void operation() {
    for(j=1; j<=n; j++)
    {
for(i=1; i<=n; i++)
{
   if(i>j)
   {
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
   A[i][k]=A[i][k]-c*A[j][k];
}
   }
}
    }

    x[n]=A[n][n+1]/A[n][n];
     for(i=n-1; i>=1; i--)
    {
sum=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];
    }
    }

    void result(){
    cout<<"\nThe solution is: \n";
    for(i=1; i<=n; i++)
    {
cout<<"\nx"<<i<<" =  "<<x[i]<<"\t";
    }
    }

void display()
{
cout<<"\n The Matrix is :-\n ";
for(int i=1;i<=n;i++){
cout<<endl;
for(int j=1;j<=n+1;j++){
cout<<A[i][j]<<"     ";
}}
}

};

void main()
{
clrscr();
row obj;
obj.operation();
obj.display();
obj.result();
getch();

}


Share this

0 Comment to "Augmented Matrix Solution By reducing it to Row Echelon Form in C++"