C++ Programming Tutorial

 
 
 
 

Image

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

 const int max_size=5;

 int n=0;
 int iterations=0;

 char Diagonally_dominant=NULL;

 long double input[max_size][max_size]={0};
 long double previous_output[max_size]={0};
 long double output[max_size]={0};

 void show_screen( );
 void clear_screen( );
 void get_size_of_linear_equations( );
 void show_input(const int,const int);
 void get_input_linear_equation( );
 void sort_system_of_linear_equations( );
 void apply_guass_seidel_iterative_method( );
 void show_result( );

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

       show_screen( );
       get_size_of_linear_equations( );
       get_input_linear_equation( );

       if(Diagonally_dominant==\'Y\' || Diagonally_dominant==\'y\')
      sort_system_of_linear_equations( );

       apply_guass_seidel_iterative_method( );
       show_result( );

       getch( );
       return 0;
    }


 //--------------------------  show_screen( )  ---------------------------//


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

       textbackground(1);
       cprintf(\" Guass-Seidel\'s Itrative 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( )
    {
       textbackground(8);

       for(int count=0;count<37;count++)
      {
         gotoxy(3,8+count);
         cout<<\"                                                                            \";
      }

       gotoxy(1,2);
    }


 //------------------  get_size_of_linear_equations( )  ------------------//


 void get_size_of_linear_equations( )
    {
       do
      {
         clear_screen( );

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

         gotoxy(4,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(4,15);
       cout<<\"Enter the number of iterations that you want to perform = \";

       cin>>iterations;

       gotoxy(4,18);
       cout<<\"Do you want to make the System Diagonally Dominant : (Y/N) ? : \";

       cin>>Diagonally_dominant;

       gotoxy(1,2);
    }


 //------------------  show_input(const int,const int)  ------------------//


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

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

         gotoxy(4,(17+(count_1*2)));

         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)));

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

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

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

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

             else if(count_2==0)
                cout<<input[count_1][count_2]<<\" x\"<<(count_2+1);

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

           else if(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) && input[count_1][(count_2+1)]>=0)
              cout<<\"  +  \";

           else if(count_2<(n-1))
              cout<<\"  -  \";

           counter++;
        }
      }
    }


 //--------------------  get_input_linear_equation( )  -------------------//


 void get_input_linear_equation( )
    {
       clear_screen( );

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

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

       gotoxy(4,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(4,35);
           cout<<\"                                                                    \";

           gotoxy(4,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>>input[count_1][count_2];

           show_input(count_1,count_2);
        }
      }
    }


 //-----------------  sort_system_of_linear_equations( )  ----------------//


 void sort_system_of_linear_equations( )
    {
       for(int count_1=0;count_1<(n-1);count_1++)
      {
         for(int count_2=count_1;count_2<(n-1);count_2++)
        {
           for(int count_3=count_1;count_3<(n-1);count_3++)
              {
             long double temp[max_size]={0};

             if(fabs(input[count_3][count_1])<
                       fabs(input[(count_3+1)][count_1]))
                {
                   for(int count_4=0;count_4<=n;count_4++)
                  temp[count_4]=input[count_3][count_4];

                   for(int count_5=0;count_5<=n;count_5++)
                  input[count_3][count_5]=
                         input[(count_3+1)][count_5];

                   for(int count_6=0;count_6<=n;count_6++)
                  input[(count_3+1)][count_6]=temp[count_6];
                }
              }
        }
      }
    }


 //---------------  apply_guass_seidel_iterative_method( )  --------------//


 void apply_guass_seidel_iterative_method( )
    {
       clear_screen( );

       gotoxy(4,10);
       cout<<\"Solution :\";

       gotoxy(4,11);
       cout<<\"ÍÍÍÍÍÍÍÍÍÍ\";

       if(n==1)
      {
         gotoxy(4,13);
         cout<<\"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\";

         gotoxy(4,14);
         cout<<\"³   n   ³      X1       ³\";

         gotoxy(4,15);
         cout<<\"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\";

         gotoxy(4,16);
         cout<<\"³       ³               ³\";
      }

       else if(n==2)
      {
         gotoxy(4,13);
         cout<<\"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\";

         gotoxy(4,14);
         cout<<\"³   n   ³      X1       ³      X2       ³\";

         gotoxy(4,15);
         cout<<\"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\";

         gotoxy(4,16);
         cout<<\"³       ³               ³               ³\";
      }

       else if(n==3)
      {
         gotoxy(4,13);
         cout<<\"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\";

         gotoxy(4,14);
         cout<<\"³   n   ³      X1       ³      X2       ³      X3       ³\";

         gotoxy(4,15);
         cout<<\"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\";

         gotoxy(4,16);
         cout<<\"³       ³               ³               ³               ³\";
      }

       else if(n==4)
      {
         gotoxy(4,13);
         cout<<\"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\";

         gotoxy(4,14);
         cout<<\"³   n   ³      X1       ³      X2       ³      X3       ³      X4       ³\";

         gotoxy(4,15);
         cout<<\"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\";

         gotoxy(4,16);
         cout<<\"³       ³               ³               ³               ³               ³\";
      }

       for(int count_1=0;count_1<n;count_1++)
      {
         long double temp[max_size]={0};

         for(int count_2=0;count_2<=n;count_2++)
        {
           if(count_2!=count_1)
              temp[count_2]=(input[count_1][count_2]/input[count_1][count_1]);
        }

         for(int count_3=0;count_3<=n;count_3++)
        input[count_1][count_3]=temp[count_3];
      }

       for(int count_4=0;count_4<n;count_4++)
      {
         for(int count_5=0;count_5<n;count_5++)
        input[count_4][count_5]*=-1;
      }

       int x_cord=4;
       int y_cord=17;

       for(int count_6=0;count_6<=iterations;count_6++)
      {
         if(n==1)
        {
           gotoxy(x_cord,y_cord);
           cout<<\"³       ³               ³\";

           gotoxy(x_cord,(y_cord+1));
           cout<<\"³       ³               ³\";
        }

         else if(n==2)
        {
           gotoxy(x_cord,y_cord);
           cout<<\"³       ³               ³               ³\";

           gotoxy(x_cord,(y_cord+1));
           cout<<\"³       ³               ³               ³\";
        }

         else if(n==3)
        {
           gotoxy(x_cord,y_cord);
           cout<<\"³       ³               ³               ³               ³\";

           gotoxy(x_cord,(y_cord+1));
           cout<<\"³       ³               ³               ³               ³\";
        }

         else if(n==4)
        {
           gotoxy(x_cord,y_cord);
           cout<<\"³       ³               ³               ³               ³               ³\";

           gotoxy(x_cord,(y_cord+1));
           cout<<\"³       ³               ³               ³               ³               ³\";
        }

         gotoxy((x_cord+3),y_cord);
         cout<<count_6;

         gotoxy((x_cord+10),y_cord);
         cout<<output[0];

         if(n>=2)
        {
           gotoxy((x_cord+26),y_cord);
           cout<<output[1];
        }

         if(n>=3)
        {
           gotoxy((x_cord+42),y_cord);
           cout<<output[2];
        }

         if(n>=4)
        {
           gotoxy((x_cord+58),y_cord);
           cout<<output[3];
        }

         for(int count_7=0;count_7<n;count_7++)
        {
           output[count_7]=0;

           for(int count_8=0;count_8<n;count_8++)
              {
             if(count_8!=count_7)
                output[count_7]+=(input[count_7][count_8]*previous_output[count_8]);
              }

           output[count_7]+=input[count_7][count_8];
           previous_output[count_7]=output[count_7];
        }

         y_cord+=2;

         if((count_6%12)==0 && count_6<iterations && count_6>0)
        {
           y_cord=17;

           gotoxy(30,44);
           cout<<\"Press any key to continue...\";
           getch( );

           for(int count_9=1;count_9<25;count_9++)
              {
             gotoxy(3,(16+count_9));
             cout<<\"                                                                            \";
              }
        }
      }

       if(n==1)
      {
         gotoxy(x_cord,y_cord);
         cout<<\"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\";
      }

       else if(n==2)
      {
         gotoxy(x_cord,y_cord);
         cout<<\"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\";
      }

       else if(n==3)
      {
         gotoxy(x_cord,y_cord);
         cout<<\"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\";
      }

       else if(n==4)
      {
         gotoxy(x_cord,y_cord);
         cout<<\"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\";
      }

       gotoxy(30,44);
       cout<<\"Press any key to continue...\";
       getch( );
    }


 //-----------------------------  show_result( )  ------------------------//


 void show_result( )
    {
       clear_screen( );

       gotoxy(4,9);
       cout<<\"Gauss-Seidel System of Linear Equations :\";

       gotoxy(4,10);
       cout<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\";

       for(int count_1=0;count_1<n;count_1++)
      {
         gotoxy(4,(13+(count_1*3)));
         cout<<\"x\"<<(count_1+1);

         gotoxy((wherex( )-1),(wherey( )-1));
         cout<<\"(k+1)\";

         gotoxy((wherex( )-3),(wherey( )+1));
         cout<<\"    =  \";

         for(int count_2=0;count_2<=n;count_2++)
        {
           gotoxy(wherex( ),(13+(count_1*3)));

           if(count_2!=count_1 && count_2<n)
              {
             if(count_2!=0)
                cout<<fabs(input[count_1][count_2])<<\" x\"<<(count_2+1);

             else
                cout<<input[count_1][count_2]<<\" x\"<<(count_2+1);

             gotoxy((wherex( )-1),(wherey( )-1));

             if(count_2<count_1)
                cout<<\"(k+1)\";

             else
                cout<<\"(k)\";

             gotoxy((wherex( )-1),(wherey( )+1));

             if(input[count_1][(count_2+1)]>=0)
                cout<<\" + \";

             else
                cout<<\" - \";
              }

           else if(count_2!=count_1)
              cout<<fabs(input[count_1][count_2]);
        }
      }

       if(Diagonally_dominant==\'Y\' || Diagonally_dominant==\'y\')
      {
         gotoxy(4,26);
         cout<<\"Note: The given System of Linear Equations is solved by making it\";

         gotoxy(10,28);
         cout<<\"Diagonally Dominant.\";
      }

       gotoxy(4,31);
       cout<<\"Result of Given System of Linear Equations :\";

       gotoxy(4,32);
       cout<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\";

       for(int count_3=0;count_3<n;count_3++)
      {
         gotoxy(8,(34+count_3+count_3));
         cout<<\"x\"<<(count_3+1)<<\" = \"<<output[count_3];
      }

       gotoxy(4,(36+count_3+count_3));
       cout<<\"* Number of Iterations for the above result = \"<<iterations<<\".\";

       gotoxy(1,2);
    }

    Related Post:
  1. Program to illustrate the implementation of Reflection Transformation about the line y=x and y=-x

  2. Program of declaration of default arguments during creation of function

  3. Program to illustrate the implementation of Stack as an Arithmetic Expression Evaluater

  4. Inventory Management System

  5. Program that provides an example of dynamic memory allocation

  6. Program that displays result of students by compact if-else notation

  7. Program to convert infix expression to postfix / suffix expression using paranthesize

  8. Program to illustrate the implementation of Double Ended linked list as a Stack

  9. Program that provides an example of inheritance using private data

  10. Program to convert an Infix Expression into a Postfix Expression using Linked List as a Stack

  11. Program to fill a Polygon using Scan Line Polygon Fill Algorithm

  12. Program to draw different kinds of Dashed Lines

  13. Program to illustrate the use of pointer to pointers

  14. Program that takes short date from a user and displays medium date

  15. Program to estimate the value of Fourth Derivative of the function at the given points from the given data using Central Difference Formula of order 4

  16. Program for Banking Application

  17. Program to print table of values of the function y = e raise to -x

  18. Program to estimate the value of Third Derivative of the function at the given points from the given data using Central Difference Formula of Order 4

  19. Program to implement the Binary search Algorithm

  20. Program that takes 10 integer from a user and perform addition using class