C++ Programming Tutorial

 
 
 
 

Image

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

 const int max_size=13;

 int n=0;

 long double x[max_size]={0};
 long double divided_difference_table[max_size][max_size]={0};

 void show_screen( );
 void clear_screen( );
 void get_input( );
 void construct_divided_difference_table( );
 void show_divided_difference_table( );

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

       show_screen( );
       get_input( );
       construct_divided_difference_table( );
       show_divided_difference_table( );

       return 0;
     }



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


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

       textbackground(1);
       cprintf(\" Divided Difference Table \");
       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(3,8+count);
         cout<<\"                                                                            \";
      }

       gotoxy(1,2);
    }


 //-----------------------------  get_input( )  --------------------------//


 void get_input( )
    {
       do
      {
         clear_screen( );

         gotoxy(6,9);
         cout<<\"Number of Distinct Data Points :\";

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

         gotoxy(27,13);
         cout<<\"[ min. n = 2  |  max. n = 12 ]\";

         gotoxy(6,12);
         cout<<\"Enter the max. number of distinct data points = n = \";

         cin>>n;

         if(n<2 || n>12)
        {
           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<2 || n>12);

       clear_screen( );

       gotoxy(6,9);
       cout<<\"Data Points & Values of Function :\";

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

       gotoxy(25,12);
       cout<<\"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\";

       gotoxy(25,13);
       cout<<\"³       x       ³     f(x)      ³\";

       gotoxy(25,14);
       cout<<\"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\";

       gotoxy(25,15);
       cout<<\"³               ³               ³\";

       for(int count_1=0;count_1<n;count_1++)
      {
         gotoxy(25,(wherey( )+1));
         cout<<\"³               ³               ³\";

         gotoxy(25,(wherey( )+1));
         cout<<\"³               ³               ³\";
      }

       gotoxy(25,(wherey( )+1));
       cout<<\"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\";

       gotoxy(25,15);

       for(int count_2=0;count_2<n;count_2++)
      {
         gotoxy(25,(wherey( )+1));

         gotoxy(27,wherey( ));
         cin>>x[count_2];

         gotoxy(43,(wherey( )-1));
         cin>>divided_difference_table[0][count_2];
      }

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

       getch( );
    }


 //---------------  construct_divided_difference_table( )  ---------------//


 void construct_divided_difference_table( )
    {
       for(int count_1=1;count_1<n;count_1++)
      {
         for(int count_2=count_1;count_2<n;count_2++)
        {
           long double fx1=divided_difference_table[(count_1-1)][(count_2-1)];
           long double fx2=divided_difference_table[(count_1-1)][count_2];
           long double x1=x[(count_2-count_1)];
           long double x2=x[count_2];

           divided_difference_table[count_1][count_2]=((fx2-fx1)/(x2-x1));
        }
      }
    }


 //------------------  show_divided_difference_table( )  -----------------//


 void show_divided_difference_table( )
    {
       clear_screen( );

       gotoxy(4,10);
       cout<<\"Divided Difference Table :\";

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

       gotoxy(4,13);
       cout<<\"ÚÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\";

       gotoxy(4,14);
       cout<<\"³   x    ³     f(x)      \";

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

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

       int x_cord=4;
       int y_cord=17;

       for(int count_1=0;count_1<n;count_1++)
      {
         gotoxy(x_cord,y_cord);
         cout<<\"³        ³              \";

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

         gotoxy((x_cord+2),y_cord);
         cout<<x[count_1];

         gotoxy((x_cord+11),y_cord);
         cout<<divided_difference_table[0][count_1];

         y_cord+=2;
      }

       gotoxy(x_cord,y_cord);
       cout<<\"ÀÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\";

       x_cord=28;

       int count_2=0;

       for(int count_3=1;count_3<n;count_3++)
      {
         gotoxy(x_cord,13);
         cout<<\"ÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\";

         gotoxy(x_cord,14);
         cout<<\"³\";

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

         gotoxy(x_cord,16);
         cout<<\"³\";

         gotoxy(x_cord,y_cord);
         cout<<\"ÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\";

         gotoxy((x_cord+6),14);
         cout<<count_3;

         if(count_3==1)
        cout<<\"st\";

         else if(count_3==2)
        cout<<\"nd\";

         else if(count_3==3)
        cout<<\"rd\";

         else
        cout<<\"th\";

         y_cord=17;

         for(int count_4=0;count_4<n;count_4++)
        {
           gotoxy(x_cord,y_cord);
           cout<<\"³\";

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

           if(count_4>=count_3)
              {
             gotoxy((x_cord+2),(y_cord-count_3));
             cout<<divided_difference_table[count_3][count_4];
              }

           y_cord+=2;
        }

         x_cord+=16;
         count_2++;

         if((count_2%3)==0 && count_3<(n-1))
        {
           gotoxy(x_cord,13);
           cout<<\"ÂÄÄ\";

           gotoxy(x_cord,14);
           cout<<\"³\";

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

           gotoxy(x_cord,16);
           cout<<\"³\";

           gotoxy(x_cord,y_cord);
           cout<<\"ÁÄÄ\";

           y_cord=17;

           for(int count_5=0;count_5<n;count_5++)
              {
             gotoxy(x_cord,y_cord);
             cout<<\"³\";

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

             y_cord+=2;
              }

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

           getch( );

           y_cord=13;
           x_cord=28;

           for(int count_6=0;count_6<=(n+2);count_6++)
              {
             gotoxy(x_cord,y_cord);
             cout<<\"                                                   \";

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

             y_cord+=2;
              }

           y_cord-=2;
           count_2=0;
           count_3--;
        }
      }

       gotoxy(x_cord,13);
       cout<<\"¿\";

       gotoxy(x_cord,14);
       cout<<\"³\";

       gotoxy(x_cord,16);
       cout<<\"³\";

       y_cord=17;

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

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

         y_cord+=2;
      }

       gotoxy(x_cord,15);
       cout<<\"\";

       gotoxy(x_cord,y_cord);
       cout<<\"Ù\";

       gotoxy(10,44);
       cout<<\"Press \'V\' to view D.D. Table again or any other key to exit.\";

       char Option=NULL;

       Option=getch( );

       if(Option==\'v\' || Option==\'V\')
       show_divided_difference_table( );

       gotoxy(1,2);
    }