Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Mathematics ProgramRSS Feeds

Program to read a Linear System of Equations,then evaluate it by using Gauss-Elimination Method and show the result

Posted By: Easy Tutor     Category: C++ Programming     Views: 2722

A C++ Program to read a Linear System of Equations,then evaluate it by using LU-Decomposition Method and show the result.

Code for Program to read a Linear System of Equations,then evaluate it by using Gauss-Elimination Method and show the result in C++ Programming

 # include <iostream.h>
 # include   <stdlib.h>
 # include    <stdio.h>
 # include    <conio.h>
 # include     <math.h>

 constint max_size=5;

 int n=0;

 char Row_interchanging=NULL;

 longdouble A[max_size][max_size]={0};
 longdouble L[max_size][max_size]={0};
 longdouble U[max_size][max_size]={0};
 longdouble X[max_size]={0};

 void show_screen( );
 void clear_screen( );
 void get_size_of_linear_equations( );
 void show_input(constint,constint,constint=0);
 void get_input_linear_equation( );
 void show_AB_matrices( );
 void make_first_row_largest( );
 void initialize_LU_matrices( );
 void apply_LU_decomposition_method( );
 void interchange_rows(constint,constint);
 void show_LU_matrices( );
 void generate_result( );
 void show_result( );

 int main( )
    {
       clrscr( );
       textmode(C4350);

       show_screen( );
       get_size_of_linear_equations( );
       get_input_linear_equation( );
       show_AB_matrices( );
       apply_LU_decomposition_method( );
       show_LU_matrices( );
       generate_result( );
       show_result( );

       getch( );
       return 0;
    }

 /*************************************************************************///--------------------------  show_screen( )  ---------------------------///*************************************************************************/void show_screen( )
    {
       cprintf("\n********************************************************************************");
       cprintf("*************************-                           -**************************");
       cprintf("*------------------------- ");

       textbackground(1);
       cprintf(" LU-Decomposition Method ");
       textbackground(8);

       cprintf(" --------------------------*");
       cprintf("*-***********************-                           -************************-*");
       cprintf("*-****************************************************************************-*");

       for(int count=0;count<42;count++)
      cprintf("*-*                                                                          *-*");

       gotoxy(1,46);
       cprintf("*-****************************************************************************-*");
       cprintf("*------------------------------------------------------------------------------*");
       cprintf("********************************************************************************");

       gotoxy(1,2);
    }

 /*************************************************************************///-------------------------  clear_screen( )  ---------------------------///*************************************************************************/void clear_screen( )
    {
       for(int count=0;count<37;count++)
      {
         gotoxy(5,8+count);
         cout<<"                                                                        ";
      }

       gotoxy(1,2);
    }

 /*************************************************************************///------------------  get_size_of_linear_equations( )  ------------------///*************************************************************************/void get_size_of_linear_equations( )
    {
       do
      {
         clear_screen( );

         gotoxy(44,11);
         cout<<"[ Maximum size = 4 ]";

         gotoxy(6,9);
         cout<<"Enter the size of the System of Linear Equations = n = ";

         cin>>n;

         if(n<=0 || n>max_size)
        {
           gotoxy(12,25);
           cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";

           gotoxy(12,26);
           cout<<"        to try again.";

           n=int(getche( ));

           if(n==27)
              exit(0);
        }
      }
       while(n<=0 || n>max_size);

       gotoxy(6,15);
       cout<<"Do you want to use Row Interchanging Technique : (Y/N) ? : ";

       cin>>Row_interchanging;


       gotoxy(1,2);
    }

 /*************************************************************************///-------------  show_input(const int,const int,const int)  -------------///*************************************************************************/void show_input(constint x,constint y,constint y_cord)
    {
       int counter=0;
       int number_of_inputs=((x*n)+y+x);

       for(int count_1=0;count_1<n;count_1++)
      {
         gotoxy(6,(17+(count_1*2)+y_cord));
         cout<<"                                                                      ";

         gotoxy(6,(17+(count_1*2)+y_cord));

         for(int count_2=0;count_2<=n;count_2++)
        {
           if(counter==(number_of_inputs+1))
              textbackground(12);

           else
              textbackground(8);

           if(count_2==n)
              gotoxy((wherex( )+5),(17+(count_1*2)+y_cord));

           gotoxy(wherex( ),(17+(count_1*2)+y_cord));
           cprintf("      ");

           if(count_2==n)
              gotoxy((wherex( )-5),(17+(count_1*2)+y_cord));

           gotoxy((wherex( )-6),(17+(count_1*2)+y_cord));

           if(counter<=number_of_inputs && counter!=-1)
              {
             if(count_2==n)
                {
                   cout<<"  =  ";
                   cout<<A[count_1][count_2];
                }

             elseif(count_2==0)
                cout<<A[count_1][count_2]<<" x"<<(count_2+1);

             else
                cout<<fabs(A[count_1][count_2])<<" x"<<(count_2+1);
              }

           elseif(count_2==n)
              cout<<"  =  b"<<(count_1+1);

           else
              cout<<"a"<<(count_1+1)<<(count_2+1)<<" x"<<(count_2+1);

           if(count_2<(n-1) && A[count_1][(count_2+1)]>=0)
              cout<<"  +  ";

           elseif(count_2<(n-1))
              cout<<"  -  ";

           counter++;
        }
      }
    }

 /*************************************************************************///--------------------  get_input_linear_equation( )  -------------------///*************************************************************************/void get_input_linear_equation( )
    {
       clear_screen( );

       gotoxy(5,9);
       cout<<"Size of the System of Linear Equations = n = "<<n;

       gotoxy(5,13);
       cout<<"System of Linear Equations :";

       gotoxy(5,14);
       cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

       show_input(0,-1);

       for(int count_1=0;count_1<n;count_1++)
      {
         for(int count_2=0;count_2<=n;count_2++)
        {
           gotoxy(5,35);
           cout<<"                                                                    ";

           gotoxy(5,35);

           if(count_2<n)
              cout<<"Enter the value of a"<<(count_1+1)<<(count_2+1)<<" =  ";

           else
              cout<<"Enter the value of b"<<(count_1+1)<<" =  ";

           cin>>A[count_1][count_2];

           show_input(count_1,count_2);
        }
      }
    }

 /*************************************************************************///------------------------  show_AB_matrices( )  ------------------------///*************************************************************************/void show_AB_matrices( )
    {
       clear_screen( );

       gotoxy(5,9);
       cout<<"Convertion of given System of Linear Equations into Matrix-A & Matrix-B";

       gotoxy(5,10);
       cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

       gotoxy(12,13);
       cout<<"ÚÄ";

       gotoxy((12+(n*14)),13);
       cout<<"Ä¿";

       gotoxy(12,14);
       cout<<"³";

       gotoxy((13+(n*14)),14);
       cout<<"³";

       if((n%2)==0)
      gotoxy(6,(14+((n/2)*2)));

       else
      gotoxy(6,(15+((n/2)*2)));

       cout<<"A  =";

       for(int count_1=0;count_1<n;count_1++)
      {
         gotoxy(12,(15+(count_1*2)));
         cout<<"³";

         gotoxy(12,(16+(count_1*2)));
         cout<<"³";

         for(int count_2=0;count_2<n;count_2++)
        {
           gotoxy(15+(count_2*14),(15+(count_1*2)));
           cout<<A[count_1][count_2];
        }

         gotoxy((13+(n*14)),(15+(count_1*2)));
         cout<<"³";

         gotoxy((13+(n*14)),((count_1*2)+16));
         cout<<"³";
      }

       gotoxy(12,(15+(count_1*2)));
       cout<<"ÀÄ";

       gotoxy((12+(n*14)),(15+(count_1*2)));
       cout<<"ÄÙ";

       gotoxy(12,(20+(count_1*2)));
       cout<<"ÚÄ";

       gotoxy(26,(20+(count_1*2)));
       cout<<"Ä¿";

       gotoxy(12,(21+(count_1*2)));
       cout<<"³";

       gotoxy(27,(21+(count_1*2)));
       cout<<"³";

       gotoxy(6,(18+(count_1*2)+(n*2)));

       if((n%2)==0)
      gotoxy(6,(21+(count_1*2)+((n/2)*2)));

       else
      gotoxy(6,(22+(count_1*2)+((n/2)*2)));

       cout<<"B  =";

       for(int count_3=0;count_3<n;count_3++)
      {
         gotoxy(12,((22+(count_1*2))+(count_3*2)));
         cout<<"³";

         gotoxy(12,((23+(count_1*2))+(count_3*2)));
         cout<<"³";

         gotoxy(15,((22+(count_1*2))+(count_3*2)));
         cout<<A[count_3][n];

         gotoxy(27,((22+(count_1*2))+(count_3*2)));
         cout<<"³";

         gotoxy(27,((23+(count_1*2))+(count_3*2)));
         cout<<"³";
      }

       gotoxy(12,((22+(count_1*2))+(count_3*2)));
       cout<<"ÀÄ";

       gotoxy(26,((22+(count_1*2))+(count_3*2)));
       cout<<"ÄÙ";

       gotoxy(25,43);
       cout<<"Press any key to continue...";

       getch( );
    }

 /*************************************************************************///----------------------  make_first_row_largest( )  --------------------///*************************************************************************/void make_first_row_largest( )
    {
       int max_row_number=0;

       for(int count_1=0;count_1<n;count_1++)
      {
         if(fabs(A[max_row_number][0])<fabs(A[count_1][0]))
        max_row_number=count_1;
      }

       if(max_row_number>0)
      {
         longdouble temp[(max_size+1)]={0};

         for(int count_2=0;count_2<=n;count_2++)
        temp[count_2]=A[0][count_2];

         for(int count_3=0;count_3<=n;count_3++)
        A[0][count_3]=A[max_row_number][count_3];

         for(int count_4=0;count_4<=n;count_4++)
        A[max_row_number][count_4]=temp[count_4];
      }
    }

 /*************************************************************************///----------------------  initialize_LU_matrices( )  --------------------///*************************************************************************/void initialize_LU_matrices( )
    {
       for(int count_1=0;count_1<n;count_1++)
      {
         L[count_1][count_1]=1;
         L[count_1][n]=A[count_1][n];
      }

       for(int count_2=1;count_2<n;count_2++)
      L[count_2][0]=(A[count_2][0]/A[0][0]);

       for(int count_3=0;count_3<n;count_3++)
      U[0][count_3]=A[0][count_3];
    }

 /*************************************************************************///------------------  apply_LU_decomposition_method( )  -----------------///*************************************************************************/void apply_LU_decomposition_method( )
    {
       if(Row_interchanging=='Y' || Row_interchanging=='y')
      make_first_row_largest( );

       initialize_LU_matrices( );

       for(int count_1=0;count_1<(n-1);count_1++)
      {
         longdouble options[max_size]={0};

         int option_number=0;

         for(int count_2=(count_1+1);count_2<n;count_2++)
        {
           for(int count_3=0;count_3<n;count_3++)
              options[option_number]+=(L[count_2][count_3]*U[count_3][(count_1+1)]);

           options[option_number]=(A[count_2][(count_1+1)]-options[option_number]);
           option_number++;
        }

         int max_row_number=0;

         for(int count_4=0;count_4<option_number;count_4++)
        {
           if(fabs(options[max_row_number])<=fabs(options[count_4]))
              {
             if(fabs(options[max_row_number])==fabs(options[count_4]) &&
                   options[max_row_number]!=options[count_4])
               {
                  if(options[max_row_number]<options[count_4])
                 max_row_number=count_4;
               }

             else
                max_row_number=count_4;
              }
        }

         if(max_row_number>0 && (Row_interchanging=='Y' || Row_interchanging=='y'))
        interchange_rows((count_1+1),(count_1+1+max_row_number));

         U[(count_1+1)][(count_1+1)]=options[max_row_number];

         longdouble temp=0;

         for(int count_5=(count_1+2);count_5<n;count_5++)
        {
           for(int count_6=0;count_6<n;count_6++)
              temp+=(L[count_5][count_6]*U[count_6][(count_1+1)]);

           temp=A[count_5][(count_1+1)]-temp;
           temp/=U[(count_1+1)][(count_1+1)];

           L[count_5][(count_1+1)]=temp;
           temp=0;

           for(int count_7=0;count_7<n;count_7++)
              temp+=(L[(count_1+1)][count_7]*U[count_7][count_5]);

           temp=A[(count_1+1)][count_5]-temp;
           U[(count_1+1)][count_5]=temp;
           temp=0;
        }
      }
    }

 /*************************************************************************///---------------  interchange_rows(const int,const int)  ---------------///*************************************************************************/void interchange_rows(constint row_1,constint row_2)
    {
       longdouble temp_A[max_size]={0};

       for(int count_1=0;count_1<=n;count_1++)
      temp_A[count_1]=A[row_1][count_1];

       for(int count_2=0;count_2<=n;count_2++)
      A[row_1][count_2]=A[row_2][count_2];

       for(int count_3=0;count_3<=n;count_3++)
      A[row_2][count_3]=temp_A[count_3];

       longdouble temp_L0=L[row_1][0];
       longdouble temp_Ln=L[row_1][n];

       L[row_1][0]=L[row_2][0];
       L[row_2][0]=temp_L0;

       L[row_1][n]=L[row_2][n];
       L[row_2][n]=temp_Ln;
    }

 /*************************************************************************///------------------------  generate_result( )  -------------------------///*************************************************************************/void generate_result( )
    {
       for(int count_1=0;count_1<n;count_1++)
      {
         U[count_1][n]=L[count_1][n];

         for(int count_2=0;count_2<count_1;count_2++)
        L[count_1][count_2]=(L[count_1][count_2]*U[count_2][n]);

         for(int count_3=0;count_3<count_1;count_3++)
        U[count_1][n]-=L[count_1][count_3];

         U[count_1][n]/=L[count_1][count_1];
      }

       for(int count_4=(n-1);count_4>=0;count_4--)
      {
         X[count_4]=U[count_4][n];

         for(int count_5=(n-1);count_5>count_4;count_5--)
        U[count_4][count_5]=(U[count_4][count_5]*X[count_5]);

         for(int count_6=(n-1);count_6>count_4;count_6--)
        X[count_4]-=U[count_4][count_6];

         X[count_4]/=U[count_4][count_4];
      }
    }

 /*************************************************************************///------------------------  show_LU_matrices( )  ------------------------///*************************************************************************/void show_LU_matrices( )
    {
       clear_screen( );

       gotoxy(6,9);
       cout<<"Decomposition of Matrix-A into Matrix-L and Matrix-U :";

       gotoxy(6,10);
       cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

       for(int count_1=1;count_1<=2;count_1++)
      {
         gotoxy(12,((count_1*15)-2));
         cout<<"ÚÄ";

         gotoxy((12+(n*14)),((count_1*15)-2));
         cout<<"Ä¿";

         gotoxy(12,((count_1*15)-1));
         cout<<"³";

         gotoxy((13+(n*14)),((count_1*15)-1));
         cout<<"³";

         if((n%2)==0)
        gotoxy(6,((count_1*15)+((n/2)*2)-1));

         else
        gotoxy(6,((count_1*15)+((n/2)*2)));

         if(count_1==1)
        cout<<"L  =";

         elseif(count_1==2)
        cout<<"U  =";

         for(int count_2=0;count_2<n;count_2++)
        {
           gotoxy(12,((count_1*15)+(count_2*2)));
           cout<<"³";

           gotoxy(12,((count_1*15)+(count_2*2)+1));
           cout<<"³";

           for(int count_3=0;count_3<n;count_3++)
              {
             gotoxy(15+(count_3*14),((count_1*15)+(count_2*2)));

             if(count_1==1)
                cout<<L[count_2][count_3];

             elseif(count_1==2)
                cout<<U[count_2][count_3];
              }

           gotoxy((13+(n*14)),((count_1*15)+(count_2*2)));
           cout<<"³";

           gotoxy((13+(n*14)),((count_1*15)+(count_2*2)+1));
           cout<<"³";
        }

         gotoxy(12,((count_1*15)+(count_2*2)));
         cout<<"ÀÄ";

         gotoxy((12+(n*14)),((count_1*15)+(count_2*2)));
         cout<<"ÄÙ";
      }

       gotoxy(25,43);
       cout<<"Press any key to continue...";

       getch( );
    }

 /*************************************************************************///-----------------------------  show_result( )  ------------------------///*************************************************************************/void show_result( )
    {
       clear_screen( );

       gotoxy(6,10);
       cout<<"Given System of Linear Equations :";

       gotoxy(6,11);
       cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

       show_input((n-1),n,-4);

       gotoxy(6,26);
       cout<<"Result of given System of Linear Equations :";

       gotoxy(6,27);
       cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

       for(int count=0;count<n;count++)
      {
         gotoxy(10,(29+count+count));
         cout<<"x"<<(count+1)<<" = "<<X[count];
      }

       gotoxy(6,41);
       cout<<"Note : The given system of Linear Equations is solved by using";

       gotoxy(6,43);
       cout<<"       Do-Little Method";

       gotoxy(30,43);

       if(Row_interchanging=='Y' || Row_interchanging=='y')
      cout<<"and Row-Interchanging Method.";

       else
      cout<<".";

       gotoxy(1,2);
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to read a Linear System of Equations,then evaluate it by using Gauss-Elimination Method and show the result is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!