Sunday 13 October 2013
no image

A Generic bubble sort

   #include <iostream>
   using namespace std;
   template <class X>
   void bubble(X *data, int size)
   {
     register int a, b;
     X t;
     for(a=1; a < size; a++)
      for(b=size-1; b >= a; b--)
     if(data[b-1] > data[b])
     {
       t = data[b-1];
       data[b-1] = data[b];
       data[b] = t;
     }
   }
   int main()
   {
     int i[] = {3, 2, 5, 6, 1, 8, 9, 3, 6, 9};
     double d[] = {1.2, 5.5, 2.2, 3.3};
     int j;
     bubble(i, 10); // sort ints
     bubble(d, 4); // sort doubles
     for(j=0; j<10; j++)
     cout << i[j] << ' ';
     cout << endl;
     for(j=0; j<4; j++)
     cout << d[j] << ' ';
     cout << endl;
     return 0;
   } 
Tuesday 8 October 2013
no image

Table cells that span more than one row/column

<html> <body> <h4> Cell that spans two columns:</h4> <table border="1"> <tr> <th>Name</th> <th colspan="2">Telephone</th> </tr> <tr> <td>Bill Gates</td> <td>555 77 854</td> <td>555 77 855</td> </tr> </table> <h4> Cell that spans two rows:</h4> <table border="1"> <tr> <th>First Name:</th> <td>Bill Gates</td> </tr> <tr> <th rowspan="2">Telephone:</th> <td>555 77 854</td> </tr> <tr> <td>555 77 855</td> </tr> </table> </body> </html>
Monday 7 October 2013
no image


Recursive Binary Search

   public class BinarySearchRecursive
   {
     public static final int NOT_FOUND = -1;
     /**
     * Performs the standard binary search
     * using two comparisons per level.
     * This is a driver that calls the recursive method.
     * @return index where item is found or NOT_FOUND if not found.
     */
     public static int binarySearch( Comparable [ ] a, Comparable x )
     {
       return binarySearch( a, x, 0, a.length -1 );
     }
     /**
     * Hidden recursive routine.
     */
     private static int binarySearch( Comparable [ ] a, Comparable x,int low, int high )
     {
       if( low > high )
         return NOT_FOUND;
       int mid = ( low + high ) / 2;
       if( a[ mid ].compareTo( x ) < 0 )
         return binarySearch( a, x, mid + 1, high );
       else if( a[ mid ].compareTo( x ) > 0 )
         return binarySearch( a, x, low, mid - 1 );
       else
         return mid;
     }
     // Test program
     public static void main( String [ ] args )
     {
       int SIZE = 8;
       Comparable [ ] a = new Integer [ SIZE ];
       for( int i = 0; i < SIZE; i++ )
         a[ i ] = new Integer( i * 2 );
    
       for( int i = 0; i < SIZE * 2; i++ )
         System.out.println( "Found " + i + " at " +
           binarySearch( a, new Integer( i ) ) );
     }
   }      
no image


Tower Of Hanoi Alogithm Using Recursion.

   #include<iostream.h>
   #include<stdio.h>
   #include<conio.h>
   class tower
   {
     int *t1,*t2,*t3;
     int x,y,z;
     public:
     void disp_tower();
     void move_disk(int tx,int ty);
     void toh(int n,int a,int b,int c);
     tower(int no);
     ~tower();
   };
   tower :: tower(int no)
   {
     t1 = new int[no+1];
     t2 = new int[no+1];
     t3 = new int[no+1];
     x = no;
     y = z = 0;
     for(int i=0,j=no ; i     {
       t1[i] = j;
       t2[i] = t2[i] = 0;
     }
     t1[no] = t2[no] = t3[no] = 0;
   }
   tower :: ~tower()
   {
     delete []t1;
     delete []t2;
     delete []t3;
   }
   void tower :: disp_tower()
   {
     clrscr();
     cout<<"
     X :: ";
     for(int i=0;i     {
       cout<<" "<     }
     cout<<"
     Y :: ";
     for(i=0;i     {
       cout<<" "<     }
     cout<<"
     Z :: ";
     for(i=0;i     {
       cout<<" "<     }
     getch();
   }
   void tower :: toh(int n,int tx,int ty,int tz) //x to y using z
   {
     if(n>=1)
     {
       toh(n-1,tx,tz,ty);
       move_disk(tx,ty); //x to y
       disp_tower();
       toh(n-1,tz,ty,tx);
     }
   }
   void tower :: move_disk(int tx,int ty)
   {
     switch(tx)
     {
       case 1:
       {
         if(ty==2)
           t2[y++] = t1[--x];
         else
           t3[z++] = t1[--x];
       }break;
       case 2:
       {
         if(ty==1)
           t1[x++] = t2[--y];
         else
           t3[z++] = t2[--y];
       }break;
       case 3:
       {
         if(ty==1)
           t1[x++] = t3[--z];
         else
           t2[y++] = t3[--z];
       }break;
     }//end of switch
   }   

no image


Use Of Pointers

   #include <iostream>
   using namespace std;
   int main()
   {
     // declare an integer and a float variable
     int IntNum;
     float FloatNum;
     // declare integer and float pointers
     int *pIntNum;
     float *pFloatNum;
     // initialize the integer and float variables
     IntNum = 10;
     FloatNum = 12.34;
     // store addresses in pointers
     pIntNum = & IntNum;
     pFloatNum = & FloatNum;
     // print out the original values
     cout << "Before increment: " << endl;
     cout << "\t IntNum is: " << IntNum << endl;
     cout << "\t FloatNum is: " << FloatNum << endl;
     // note that we need to dereference a pointer in order
     // to extract the value it contains.
     cout << "\t pIntNum contains: " << *pIntNum << endl;
     cout << "\t pFloatNum contains: " << *pFloatNum << endl;
     // increment values of the integer and float variables
     (*pIntNum)++; // dereference and then increment
     (*pFloatNum)++;
     // print out the values after increment
     cout << "After increment: " << endl;
     cout < < "\t IntNum is: " << IntNum << endl;
     cout < < "\t FloatNum is: " << FloatNum << endl;
     cout < < "\t pIntNum contains: " << *pIntNum << endl;
     cout < < "\t pFloatNum contains: " << *pFloatNum << endl;
     return 0;
   }      
no image


Virtual Function

   #include <iostream>
   using namespace std;
   class BaseClass
   {
     public:
       int i;
       BaseClass(int x)
       {
         i = x;
       }
       virtual void myFunction()
       {
         cout << "Using BaseClass version of myFunction(): ";
         cout << i << '\n';
       }
   };
   class DerivedClass1 : public BaseClass
   {
     public:
       DerivedClass1(int x) : BaseClass(x) {}
       void myFunction()
       {
         cout << "Using DerivedClass1's version of myFunction(): ";
         cout << i*i << '\n';
       }
   };
   class DerivedClass2 : public BaseClass
   {
   public:
       DerivedClass2(int x) : BaseClass(x) {}
       void myFunction()
       {
         cout << "Using DerivedClass2's version of myFunction(): ";
         cout << i+i << '\n';
       }
   };
   int main()
   {
     BaseClass *p;
     BaseClass ob(10);
     DerivedClass1 derivedObject1(10);
     DerivedClass2 derivedObject2(10);
     p = &ob;
     p->myFunction(); // use BaseClass's myFunction()
     p = &derivedObject1;
     p->myFunction(); // use DerivedClass1's myFunction()
     p = &derivedObject2;
     p->myFunction(); // use DerivedClass2's myFunction()
     return 0;
   }    
no image


Word Frequency

   #include <stdio.h>
   #define SIZE 80
   #define LEN 80
   int strword(char[],char[][]);
   int strword_p(char*,char**);
   void main()
   {
     char* s;
     char** w;
     char ch;
     do
     {
       clrscr();
       gotoxy(10,1);
       printf("Enter a string :");
       gets(s);
       gotoxy(10,2);
       printf("\nNumber of words :%d",strword_p(s,w));
       gotoxy(10,24);
       printf(" Continue(y/n)?");
       ch=getch();
     } while (ch=='y' || ch=='Y');
   }
   int strword_p(char *s,char **w)
   {
     int is_space=0,
     i=0,
     word_counter=0,
     j=0,
     is_printed=0,
     frequency=0;
     while (*(s+i)!='\0')
     {
       if (*(s+i)==' ' ||
       *(s+i)==',' ||
       *(s+i)=='.' ||
       *(s+i)==':')
       {
         if (is_space==0)
         {
           *(*(w+word_counter)+j)='\0';
           word_counter++;
           is_space=1;
           j=0;
         }
       }
       else
       {
         *(*(w+word_counter)+j)=*(s+i);
         j++;
         is_space=0;
       }
       i++;
     }
     if (is_space==0)
     {
       *(*(w+word_counter)+j)='\0';
       word_counter++;
     }
     for(j=0;j     {
       frequency=0;
       is_printed=0;
       for(i=0;i       {
         if (strcmp(w[i],w[j])==0)
         {
           frequency++;
           if (j           is_printed=1;
         }
       }
       if (!is_printed)
         printf("\n Word %d : %s ,frequency->%d",j+1,w[j],frequency);
     }
     printf("\n");
     return word_counter;
   }