Wednesday, September 29, 2010

(OOPS)Sorting Algorithm Using Templates

EX NO.                               SORTING ALGORITHM USING TEMPLATES

AIM: To create a c++ program to implement sorting algorithm using templates.

ALGORITHM:
Step 1:Start the program.
Step 2: Define the functions for bubble sort,insertion sort,selection sort and merge sort.
Step 3:Call the respective functions to perform the sorting operations.
Step 4:Display the results.
Step 5:Stop the program.

PROGRAM: 


#include<iostream.h>
#include<conio.h>
template<class T>
class sort
{
 public:
 T a[20];
 int n;
 sort()
  {
  }
 void display(T*a,int n)
  {
   for(int i=0;i<n;i++)
   {
    cout<<a[i]<<"\t";
   }
   cout<<endl;
  }
 void bubble(T*a,int n)
  {
   T tmp;
   for(int i=0;i<n-i;i++)
   {
    for(intj=i+1;j<n;j++)
    {
     if(a[i]>a[j])
     {
      tmp=a[i];
      a[i]=a[j];
      a[j]=tmp;
     }
    }
   }
  }
 void insertion(T*a,int n)
  {
   for(int p=1,p<n;p++)
   {
    T tmp=a[p];
    for(int j=p;j>0&&tmp<a[j-1];j--)
    a[j]=a[j-1]
    a[j]=tmp;
   }
  }
 void selection(T*a,int n)
  {
   T tmp,min;
   for(int i=0,i<n-1;i++)
   {
    min=i;
    for(int j=i+1;j<w,j++)
    {
     if(a[min]>a[j])
     min=j;
    }
    tmp=a[min];
    a[min]=a[i];
    a[i]=tmp;
   }
  }
 void merge(T*a,int n)
 {
  T.tmp array[20];
  merge(a,tmparray,0,n-1);
 }
 void merg(T*a,T*tmparray,int left,int right)
 {
  if(left<right)
  {
   int centre=(left+right)/2
   merge(a,tmparray,left,center);
   merge(a,tmparray,centre+1,right);
   {
    merge1(a,tmparray,left,center+1,right);
   }
  }

 void merge1(T*a,T*tmparray,int left pos,int right pos,int right end)
 {
  int left end=right pos-1;
  int tmppos=leftpos;
  int numelement=right end-left pos+1;
  while(leftpos<=leftend&&rightpos<=right end)
  {
   if(a[leftpos]<=a[right pos])
   tmparray[tmppos++]=a[leftpos++];
   else
   tmparray[tmpos++]=a[rightpos++];
  }
  while(leftpos<=leftend)
  tmparray[tmppos++]=a[leftpos++];
  while(rightpos<=rightend)
  tmparray[tmppos++]=a[rightpos++];
  for(int i=0;i<numelement;i++;rightend--)
  a[rightend]=tmparray[rightend];
 }
 };
 void main()
 {
  sort<int>s;
  inth.a[20],choice,b[20],c[10],d[20],e[20],f[20],g[20],dec;
  clrscr();
  cout<<"Enter the no: of elements:"<<endl;
  cin>>n;
  for(int i=0;i<n;i++)
  {
   cin>>a[i];
   b[i]=a[i];
   c[i]=a[i];
   d[i]=a[i];
   e[i]=a[i];
  }
  do
  {
   clrscr();
   cout<<"\t\t\t***MENU***\n";
   cout<<"1.bubble\n2.insertion\n3.selection\n4.merge\n"<<endl;
   cout<<"\t\t\tenter your choice:";
   cin>>choice;
   switch(choice)
   {
    case 1:
       cout<<"\n element before sorting:\n"<<endl;
       s.display(a,n);
       s.bubble(a,n);
       cout<<"\n elements after sorting:\n"<<endl;
       s.display(a,n);
       getch();
       break;
    case 2:
       cout<<"elements before sorting:\n"<<endl;
       s.display(b,n);
       s.insertion(b,n);
       cout<<"\nelement after sorting:\n"<<endl;
       display(b,n);
       getch();
       break;
    case 3:
       cout<<"\n element before sorting:\n"<<endl;
       s.display(c,n);
       s.selection(c,n);
       cout<<"elements after sorting:\n"<<endl;
       s.display(c,n);
       getch();
       break;
    case 4:
       cout<<"elements before sorting:\n"<<endl;
       s.display(d,n);
       s.mergesort(d,n);
       cout<<"\n Element after sorting:\n"<<endl;
       s.displsy(d,n);
       getch();
       break;
   }
  }
 while(choice<4);
 getch();
}

RESULT :
       Thus the c++ program to perform various sorting operations using templates has been executed and the output is verified successfully.

(OOPS)Overloding new & delete operator(longitude,latitude)

EX NO.4                                    PROGRAM TO OVERLOAD NEW AND DELETE OPERATOR


AIM: To write a c++ program to perform type conversion from basic to class type and class to basic type.

ALGORITHM: 
Step 1: Start the program.
Step 2: A member function,parametrized constructor is created to initialize the value of latitude and longitude.
Step 3: Another member function is created to display latitude and longitude values.
Step 4: New operator overloading function is created to allocate the memory.
Step 5: Delete operator overloading function is created to deallocate the memory.
Step 6: A main function is created to implement new and delete operator overloading.
Step 7:End the program.


PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class position
{
float longitude,latitude;
public:
position()
{
};
position (float a, float b)
{
latitude=a;
longitude=b;
}
void *operator new(size_t size)
{
cout<<"\nin new";
return malloc(sizeof(size));
}
void display()
{
cout<<"\n\nlatitude:"<<latitude<<"\n\nlongitude:"<<longitude<<"\n\n";
}
void operator delete(void *p)
{
cout<<"\nin delete \n";
}
};
main()
{
clrscr();
position a(23.4,78.6);
position *b;
cout<<"A value:\n";
a.display();
cout<<"B value:\n";
b=new position;
b->display();
*b=a;
delete b;
getch();
}


RESULT:
      Thus a c++ program for performing type conversion is written,executed and verified successfully. 

(OOPS)Implementation of linked list using template

EX NO.            IMPLEMENTATION OF LINKED LIST USING TEMPLATES


 AIM:To write a c++ program to implement the linked list using templates.

 ALGORITHM:
Step 1: Start the program.
Step 2: Create and declare the template class.
Step 3:To create,insert,delete,search and display the elements of linked list corresponding functions are created and called respectively.
Step 4:Define the main function
Step 5:Stop the program.


PROGRAM:
 #include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
template<class T>
class list
{
T item;
public:
struct node
{
 T data;
 struct node*next;
 };
 struct node*start,*p,*new1[25];
 void create();
 void insert();
 void delete1();
 void search();
 void display();
 };
 int pos,num,n,i,j;
 template<class T>
 void list<T>::create()
 {
 cout<<"\nenter the size";
 cin>>n;
 for(i=0;i<n;i++)
 {
 new1[i]=(struct node*)malloc(sizeof(struct node));
 cout<<"enter the data";
 cin>>item;
 new1[i]->data=item;
 new1[i]->next=NULL;
 if(i==0)
 {
 p=new1[i];
 start=new1[i];
 }
else
{
p->next=new1[i];
p=new1[i];
}
}
}
template<class T>
void list<T>::insert()
{
p=start;
cout<<"enter the pos";
cin>>pos;
new1[pos]=(struct node*)malloc(sizeof(struct node));
cout<<"enter the data";
cin>>item;
new1[pos]->data=item;
new1[pos]->next=NULL;
for(i=1;i<pos;i++)
{
p=p->next;
}
new1[pos]->next=p->next;
p->next=new1[pos];
n=n+1;
}
template<class T>
void list<T>::delete1()
{
p=start;
cout<<"enter the position to be deleted";
cin>>pos;
for(i=0;i<pos-1;i++)
{
p=p->next;
}
p->next=p->next->next;
cout<<endl<<"node deleted";
n-=1;
}
template<class T>
void list<T>::search()
{
p=start;
pos=0;
cout<<"enterthe element to be search";
cin>>num;
for(i=0;i<=n;i++)
{
if(p->data==num)
{
pos=i;
cout<<"element found at"<<pos<<"pos";
break;
}
else
{
p=p->next;
}
}
if(pos==0)
cout<<"\nelement not found";
}
template<class T>
void list<T>::display()
{
p=start;
for(i=1;i<=n;i++)
{
cout<<p->data<<"->";
p=p->next;
}
cout<<"NULL";
}
void main()
{
int c;
clrscr();
list<int>lis;
for(;;)
{
cout<<"\nmenu...\n1.create\n2.insert\n3.delete\n4.search\n5.display\n6.exit";
cout<<"\nenter the choice";
cin>>c;
switch(c)
{
case 1:
lis.create();
break;
case 2:
lis.insert();
break;
case 3:
lis.delete1();
break;
case 4:
lis.search();
break;
case 5:
lis.display();
break;
case 6:
exit(0);
break;
default:
cout<<"\nenter the correct choice";
}
}
}

RESULT:
              Thus the program to implement linked list using templates is executed and the output is verified.

(OOPS) Implementation of run time polymorphism

#include<iostream.h>
#include<conio.h>
class shapes
{
public:
virtual void area()
{
cout<<"\n program for calculating shapes";
}
};
class circle:public shapes
{
public:
int a,r;
void area()
{
cout<<"\n enter the radius";
cin>>r;
a=3.14*r*r;
cout<<"\n the area of the circle is"<<a;
}
};
class rectangle:public shapes
{
public:
float l,b,a;
void area()
{
cout<<"\n rectangle";
cout<<"\n enter the length";
cin>>l;
cout<<"\n enter the breadth";
cin>>b;
a=l*b;
cout<<"\n the area of the rectangle"<<a;
}
};
class triangle:public shapes
{
public:
float b,h,a;
void area()
{
cout<<"\n triangle";
cout<<"\n enter the breadth";
cin>>b;
cout<<"\n enter the height";
cin>>h;
a=0.5*b*h;
cout<<"\n area of the triangle is"<<a;
}
};
class square:public shapes
{
public:
float s,a;
void area()
{
cout<<"\n square";
cout<<"\n enter the side of the square";
cin>>s;
a=s*s;
cout<<"\n the area of the square is"<<a;
}
};
void main()
{
int ch=0;
shapes *p;
circle a1;
rectangle b1;
triangle c1;
square d1;
do{
cout<<"\n ....Menu....";
cout<<"\n1.circle";
cout<<"\n2.Rectangle";
cout<<"\n3.Triangle";
cout<<"\n4.Square";
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
p=&a1;
p->area();
break;
case 2:
p=&b1;
p->area();
break;
case 3:
p=&c1;
p->area();
break;
case 4:
p=&d1;
p->area();
break;
}
}while(ch!=5);
getch();
}

(OOPS)PROGRAM USING STATIC MEMBERS

EX.NO:            PROGRAM USING STATIC MEMBERS
DATE:

AIM:
                To write a c++ program to display the students details using static members.

ALGORITHM:

            STEP1: Start the process.

            STEP2: Get the number of students.

            STEP3: Get the name, roll no, marks of students.

            STEP4:

            STEP5: The total and averages are calculated.

            STEP6: Step3 to step5 is repeated for the given number of students.

            STEP7: Display the details.

            STEP8: Stop the process.

(OOPS)PROGRAM USING CONSTRUCTOR, DESTRUCTOR AND ASSIGNMENT OPERATOR OVERLOADING

EX.NO:                 PROGRAM USING CONSTRUCTOR, DESTRUCTOR
DATE:                      AND ASSIGNMENT OPERATOR OVERLOADING

AIM:
                To create two strings and concatinate using default constructors,parametrized constructors,copy constructor,destructor and overloaded assignment operator.

ALGORITHM:

            STEP1: Start the process.

            STEP2: A default constructor is created for string length allocation.

STEP3: A parameterized constructor is created in which parameter name is copied to another variable.

STEP4: An assignment operator is overloaded to copy one object name or value
                          to another.

            STEP5: A copy constructor is created which refers to the address of another
                         Object value.

            STEP6: Concatenation is performed with two strings.

            STEP7: Many objects are created and made to access the different constructors and the overloaded    assignment operator
              

             PROGRAM:

             #include<iostream.h>
             #include<conio.h>
             #include<string.h>
            #include<stdlib.h>
            class string
            {
             char *name;
             int length;
            public:
            string()
            {
             length=0;
             name=new char[length +1]
             }
             string(char*s)
             {
             length=strlen(s);
             name= new char[length +1];
             strcpy(name,s);
             }
             string(string &s)
             {
             length=strlen(s.name);
             name=new char(length +1);
             strcpy(name,s.name);
             }
             void display(void)
             {
              cout<<"\n"<<name;
             }
              void operator=(string s);
             string operator+(string s);
             ~string()
             {
              free(name);
             }
             };
            void string::operator=(string s)
            {
             length=strlen(s.name);
             name=new char[length +1];
             strcpy(name,s.name);
             }
             string string::operator+(string s)
            {
             string tm(strcat(name,s.name));
             return tm;
             }
             void main()
             {
             clrscr();
             char*first="JOSEPH";
             string name1(first),name2("LOUIS"),name3(name 2);
             string name 4=name1,name 5=name1+name2;
             cout<<"\n in copy constructor";
             name3.display ();
             cout<<"\n\n assignment operator";
             name4.display();
             cout<<"\n Concatinated name";
             name5.display();
             getch();
             }


            RESULT:
                           Thus  the two strings are created and concatinated using default constructor,parametrized constructor,copy constructor,overloading of assignment operator, destructor and output is verified succesfully.











            

(OOPS) ADDITION OF COMPLEX NUMBERS

EX.NO:                       ADDITION OF COMPLEX NUMBERS
DATE:                                  USING FRIEND FUNCTION

AIM:

              To write a c++ program to sum two complex numbers using friend function.

ALGORITHM:

            STEP1: Start the process.

            STEP2: A friend function called sum is defined.

            STEP3: The values are passed as arguments.

            STEP4: Sum is calculated for the two complex numbers and stored in a object.

            STEP5: Then the sum value is displayed.

            STEP6: Stop the process.

Friday, September 24, 2010

DSA EX.NO.1a IMPLEMENTATION OF SINGLY LINKED LIST

EX.NO.1a         IMPLEMENTATION OF SINGLY LINKED LIST
                           

AIM:
         
          To write a c program to implement singly linked list.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare a  structure  named as  node with  two fields, one  is data
field  and  another  one  address field , address   field  must  be a  pointer pointing  to next  node.

Step 3: CREATION:
          a. Get the size of lists that has to be created.
          b. Allocate memory space for each node.
          c. Create two pointers to point first node link all other nodes using
              pointers.

Step 4: INSERTION:
          a. Allocate memory space for new node.
          b. Get the position where the new node wants to be  inserted.
          c. Change the links.

Step 5: DELETION:
          a. Get the position of the node that has to be deleted.
          b. Change the links.

Step 6: DISPLAY:
          a. Check weather the list is empty or not.
          b. If list is not empty print the data until the address field is null.

Step 7: Stop the process.





PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
struct node
{
int data;
struct node*next;
};
struct node*start,*p,*new1;
int item,i,pos,n,num,null;
void display();
void create()
{
printf("Enter the size of list:");
scanf("%d",&n);
{
new1=(struct node*)malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&item);
new1->data=item;
new1->next=NULL;
if(i==1)
{
start=new1;
p=new1;
}
else
{
p->next=new1;
p=new1;
}
}
display();
}
void insert()
{
p=start;
printf("enter the position");
scanf("%d",&pos);
new1=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data");
scanf("%d",&item);
new1->data=item;
new1->next=NULL;
for(i=1;i<pos;i++)
p=p->next;
new1->next=p->next;
p->next=new1;
n=n+1;
display();
}
void delet()
{
printf("enter the position");
scanf("%d",&pos);
p=start;
for(i=1;i<pos;i++)
p=p->next;
p->next=p->next->next;
printf("node deleted");
n=n-1;
display();
}
void search()
{
p=start;pos=0;
printf("Enter the demand to be found");
scanf("%d",&num);
for(i=1;i<n;i++)
{
if(p->data==num)
{
printf("Elements is found at %d position\n",i-1);
pos=i;
break;
}
else
p=p->next;
}
if(pos==0)
printf("\nElement not found");
}
void display()
{
p=start;
for(i=1;i<=n;i++)
{
printf("%d->",p->data);
p=p->next;
}
printf("NULL");
}
void main()
{
int c;
clrscr();
for(;;)
{
printf("........MENU.........");
printf("\n1.create");
printf("\n2.insert");
printf("\n3.delete");
printf("\n4.search");
printf("\n5.display");
printf("\n6.exit");
printf("\nEnter the choice");
scanf("%d",&c);
switch(c)
{
case 1:create();break;
case 2:insert();break;
case 3:delet();break;
case 4:search();break;
case 5:display();break;
case 6:exit(0);break;
default:printf("Enter correct choice");
}
}