Subscribe:

Labels

Featured Posts

Tuesday, March 23, 2010

simple Inheritance

#include<iostream.h>

#include<conio.h>

class a

{

public:

int a;

a(){a=10;}

void disp()

{

cout<<a<<endl;

}

};

class b:a

{

public:

int b;

b(){b=30;}

void disp()

{

cout<<a<<endl<<b;

}

};

void main()

{

b obj;

clrscr();

obj.disp();

getch();

}

Monday, February 22, 2010

binary tree traversal using class in c++

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
     int data;
     node *left;
     node *right;
};
node *tree=NULL;
node *insert(node *tree,int ele);
int count=1;
class trav
{
public:
void preorder(node *tree);
void inorder(node *tree);
void postorder(node *tree);
};
void main()
{
  int ch,ele;
  trav t;
  do
  {
             
                    cout<<"\n\t\a\a1----INSERT A NODE IN A BINARY TREE.\a\a";
                    cout<<"\n\t\a\a2----PRE-ORDER TRAVERSAL.\a\a";
                    cout<<"\n\t\a\a3----IN-ORDER TRAVERSAL.\a\a";
                    cout<<"\n\t\a\a4----POST-ORDER TRAVERSAL.\a\a";
                    cout<<"\n\t\a\a5----EXIT.\a\a";
                    cout<<"\n\t\a\aENTER CHOICE::\a\a";
                    cin>>ch;
                    switch(ch)
                    {
                         case 1:
                         cout<<"\n\t\a\aENTER THE ELEMENT::\a\a";
                         cin>>ele;
                         tree=insert(tree,ele);
                         break;

                         case 2:
                         cout<<"\n\t\a\a****PRE-ORDER TRAVERSAL OF A TREE****\a\a";
                         t.preorder(tree);
                         break;

                         case 3:
                         cout<<"\n\t\a\a****IN-ORDER TRAVERSAL OF A TREE****\a\a";
                         t.inorder(tree);
                         break;

                         case 4:
                         cout<<"\n\t\a\a****POST-ORDER TRAVERSAL OF A TREE****\a\a";
                         t.postorder(tree);
                         break;

                         case 5:
                         exit(0);
                    }
               }while(ch!=5);
          }

          node *insert(node *tree,int ele)
          {
               if(tree==NULL)
               {
                    tree=new node;
                    tree->left=tree->right=NULL;
                    tree->data=ele;
                    count++;
               }
               else
               if(count%2==0)
               tree->left=insert(tree->left,ele);
               else
               tree->right=insert(tree->right,ele);
               return(tree);
          }

          void trav::preorder(node *tree)
          {
               if(tree!=NULL)
               {
                    cout<<tree->data;
                    preorder(tree->left);
                    preorder(tree->right);
                    getch();
               }
          }

          void trav::inorder(node *tree)
          {
               if(tree!=NULL)
               {
                    inorder(tree->left);
                    cout<<tree->data;
                    inorder(tree->right);
                    getch();
               }
          }

          void trav::postorder(node *tree)
          {
               if(tree!=NULL)
               {
                    postorder(tree->left);
                    postorder(tree->right);
                    cout<<tree->data;
                    getch();
               }
          }

Sunday, February 21, 2010

binary tree traversal

#include<iostream.h>
          #include<conio.h>
          #include<stdio.h>
          #include<stdlib.h>
          struct node
          {
               int data;
               node *left;
               node *right;
          };

          node *tree=NULL;
          node *insert(node *tree,int ele);

          void preorder(node *tree);
          void inorder(node *tree);
          void postorder(node *tree);
          int count=1;

          void main()
          {
               clrscr();
               int ch,ele;
               do
               {
                    clrscr();
                    cout<<"\n\t\a\a1----INSERT A NODE IN A BINARY TREE.\a\a";
                    cout<<"\n\t\a\a2----PRE-ORDER TRAVERSAL.\a\a";
                    cout<<"\n\t\a\a3----IN-ORDER TRAVERSAL.\a\a";
                    cout<<"\n\t\a\a4----POST-ORDER TRAVERSAL.\a\a";
                    cout<<"\n\t\a\a5----EXIT.\a\a";
                    cout<<"\n\t\a\aENTER CHOICE::\a\a";
                    cin>>ch;
                    switch(ch)
                    {
                         case 1:
                         cout<<"\n\t\a\aENTER THE ELEMENT::\a\a";
                         cin>>ele;
                         tree=insert(tree,ele);
                         break;

                         case 2:
                         cout<<"\n\t\a\a****PRE-ORDER TRAVERSAL OF A TREE****\a\a";
                         preorder(tree);
                         break;

                         case 3:
                         cout<<"\n\t\a\a****IN-ORDER TRAVERSAL OF A TREE****\a\a";
                         inorder(tree);
                         break;

                         case 4:
                         cout<<"\n\t\a\a****POST-ORDER TRAVERSAL OF A TREE****\a\a";
                         postorder(tree);
                         break;

                         case 5:
                         exit(0);
                    }
               }while(ch!=5);
          }

          node *insert(node *tree,int ele)
          {
               if(tree==NULL)
               {
                    tree=new node;
                    tree->left=tree->right=NULL;
                    tree->data=ele;
                    count++;
               }
               else
               if(count%2==0)
               tree->left=insert(tree->left,ele);
               else
               tree->right=insert(tree->right,ele);
               return(tree);
          }

          void preorder(node *tree)
          {
               if(tree!=NULL)
               {
                    cout<<tree->data;
                    preorder(tree->left);
                    preorder(tree->right);
                    getch();
               }
          }

          void inorder(node *tree)
          {
               if(tree!=NULL)
               {
                    inorder(tree->left);
                    cout<<tree->data;
                    inorder(tree->right);
                    getch();
               }
          }

          void postorder(node *tree)
          {
               if(tree!=NULL)
               {
                    postorder(tree->left);
                    postorder(tree->right);
                    cout<<tree->data;
                    getch();
               }
          }

Monday, February 15, 2010

warshalls

#include<iostream.h>
class warshal
{
 int n,i,j,k,d[10][10];
public:
 void getgraph();
 void war();
 void disp();
};
void warshal::getgraph()
{
 cout<<"Enter the no of vertices";
 cin>>n;
 cout<<"Enter the adjacency matrix";
 for(i=1;i<=n;i++)
  for(j=1;j<=n;j++)
  {
   cout<<"["<<i<<";"<<j<<"]:";
   cin>>d[i][j];
  }
}
void warshal::war()
{
 for(k=1;k<=n;k++)
  for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
    if(i!=j)
     d[i][j]=(d[i][j]||(d[i][k]&&d[k][j]));
}
void warshal::disp()
{
 cout<<"The transitive closure";
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
  {
   cout<<"\t"<<d[i][j];
  }
  cout<<"\n";
 }
}
void main()
{
 warshal f;

 f.getgraph();
 f.war();
 f.disp();

}

Sunday, February 14, 2010

Binary search

#include<iostream.h>

class bsearch
{
public:
 int a[10];
 void input();
 void binarySearch(int lb, int ub, int key);
};
void bsearch::input()
{
 int i;
 cout<<"Input should be sorted numbers \n";
 for(i=0;i<10;i++)
 {
  cin>>a[i];
 }
}
void bsearch::binarySearch(int lb, int ub, int key)
{
 int mid;
    int comparisonCount = 1;   
    
    mid = (lb + ub)/2;
 
 while((a[mid] != key) && (lb <= ub))
    {
  comparisonCount++;
        if (a[mid] > key) 
        {
   ub = mid - 1;   
        }                                                     
        else                                               
        {
   lb = mid + 1;    
        }
        mid = (lb + ub)/2;
 }
    if(lb <= ub)
    {
  cout<< "The number was found in a subscript: "<<mid <<"\n";
  cout<< a[mid];
        cout<< "\n The binary search found the number after " << comparisonCount << " comparisons.\n";             
    }
 else
  cout<< "Sorry, the number is not in this array list. The binary search made "<<comparisonCount << "comparisons.";
    return; 
}
void main()
{
 int key;

 bsearch b;

 b.input();
 cout<< "Enter the key or element to search \n";
 cin>>key;

 b.binarySearch(0, 9, key); 
}

Wednesday, February 10, 2010

Disclaimer

WARNING: READ Disclaimer Below Before Downloading Anything

We DO NOT host or upload most of the files that are available on this site. We merely search on INTERNET and index popular files openly available for anyone. These content are uploaded to the respective websites by their User Communities from all over the World and NOT by us. Please Contact Us through E-Mail, if you feel that any Content on this site are objectionable or violating your copyrights. The objectionable content shall be promptly removed from our Website.

Tuesday, February 9, 2010

constructor and destructor

#include <iostream>
#include <iostream.h>

#include <conio.h>

class myclass {

int a,b;

public:

myclass();                   // constructor

~myclass();                  // destructor

void show();

};