Wednesday, 12 October 2011

IMPLEMENTATION OF BUBBLE SORT


IMPLEMENTATION OF BUBBLE SORT

AIM:
             To write a program to implement bubble sort using templates
 ALGORITHM:

Step 1:Start the process.
Step 2:Get the number of elements to be sorted.
Step 3:Get the elements from the user.
Step 4:For iteration=1 to n,
            a. Assign the first element to the variable “i” and the second element to the variable “j”.
            b.Compare the two elements.
            c.If the first element is greater than the second element then swap the two values. Else break.
Step 5:Continue the steps 4 until there is no interchange in the current iteration.
Step 6;No interchange indicates the elements are sorted.
Step 7: Stop the process.

PROGRAM:

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
template <class t>
class bubble
{
t a[25];
public:
void get(int);
void sort(int);
void display(int);
};
template <class t>
void bubble <t>::get(int n)
{
int i;
cout<<"\nEnter the array elements:";
for(i=0; i<n;i++)
cin>>a[i];
}
template <class t>
void bubble <t>::display(int n)
{
int i;
cout<<"\nThe sorted array is:\t";
for(i=0;i<n;i++)
cout<<a[i]<<setw(10);
}
template <class t>
void bubble <t>::sort(int n)
{
int i,j;
t temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void main()
{
int n,m;
bubble<int> b1;
bubble<float> b2;
clrscr();
cout<<"\n----Bubble Sort on Integer Values----";
cout<<"\nEnter the size of array:\n";
cin>>n;
b1.get(n);
b1.sort(n);
b1.display(n);
cout<<"\n\n----Bubble Sort on Float values----\n";
cout<<"\nEnter the size of array:\n";
cin>>m;
b2.get(m);
b2.sort(m);
b2.display(m);
getch();
}

OUTPUT:

----Bubble Sort on Integer Values----
Enter the size of array: 5

Enter the array elements: 3 6 1 2 9

The sorted array is:    1         2         3         6         9

----Bubble Sort on Float values----

Enter the size of array: 5

Enter the array elements: 1.2 5.7 3.6 2.0 4.6

The sorted array is:    1.2         2       3.6       4.6       5.7

RESULT:
Thus the program to implement Bubble Sort using templates was executed.

CREATING LINKED LIST USING TEMPLATES


CREATING LINKED LIST USING TEMPLATES
AIM:
To  write a program top perform various operations  in  a linked list using templates.

ALGORITHM:

Step1: Start the process.
Step2: Declare template class T and include the necessary header files.
Step3: Create a class for linked list node.
Step4: Declare the necessary member function, insert, display ,quit.
Step5: Initialize the head node to NULL.
Step6: Adding values to the tail or end of the list.
a.       check the head node for NULL. If it is null then new node will be assigned  to       head node .And the given input is given to the head node.
b.      If it is not null, a new node will be  created, while the new link node  is not equal to null then new value will be appeared to the new node.
Step7: Display the list.
            a. print the nodes if the node is not null.
Step 9: Inside main, call and invoke the function and pass the different data types.
Step 10:Display the results.
Step 11:Stop the process.

PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<process.h>
template<class T>
class list
{
 private:
    int data;
     list *next;

 public:
   list()
   {
 data = 0.0;
next = NULL;
}
list(int dat)
{
data =dat;
next = NULL;
}
void insert(list *node);
void display(list *);
};
template<class T>
void list<T>::insert(list<T> *node)
{
list *last = this;
while(last ->next)
last = last ->next;
last->next = node;
}
template<class T>
void list<T>::display(list<T> *first)
{
list *traverse;
for(traverse=first;traverse;traverse=traverse->next)
cout<<traverse->data;
cout<<endl;
}
void main()
{
int choice1,data;
list<int> *first = NULL;
list<int> *node;
while(1)
{
cout<<"\n Linked list using Templates";
cout<<"\n 1. Insert the element";
cout<<"\n 2. Display the elements:";
cout<<"\n 3. Quit";
cout<<"\n Enter your choice:";
cin>>choice1;
switch(choice1)
{
case 1:
cout<<"\n enter data:";
cin>>data;
node=new list<int>(data);
if(first==NULL)
first = node;
else
first -> insert(node);
break;
case 2:
first ->display(first);
break;
case 3:
exit(1);
}
}
}






OUTPUT:

 Linked list using Templates
 1. Insert the element
 2. Display the elements:
 3. Quit
 Enter your choice:1

 enter data:12

 Linked list using Templates
 1. Insert the element
 2. Display the elements:
 3. Quit
 Enter your choice:1

 enter data:23

 Linked list using Templates
 1. Insert the element
 2. Display the elements:
 3. Quit
 Enter your choice:1

 enter data:10

 Linked list using Templates
 1. Insert the element
 2. Display the elements:
 3. Quit
 Enter your choice:2
12 23 10

 Linked list using Templates
 1. Insert the element
 2. Display the elements:
 3. Quit
 Enter your choice:3

RESULT:
Thus the program for linked list using template was executed using C++ .

OVERLOADING NEW AND DELETE OPERATOR


OVERLOADING NEW AND DELETE OPERATOR

AIM:
To write a C++ program to implement the overloading of new and delete operators to provide dynamic allocation of memory

ALGORITHM:

Step 1: Start the program.
Step 2: Create a class ‘op’;
Step 3: Declare the overloaded member functions new & delete.
Step 4: Invoke the base class constructor and passing arguments for the new function.
Step 5: The inbuilt identifiers __FILE__ and __LINE__ will get the current file which under processing and the line which currently executing the __LINE__ will be passed.
Step 6: Using malloc the memory will be allocated for the current processing file.
Step 7: Invoke the overloaded operated function delete and pass the pointer variable free the memory which is already allocated.
Step 8: Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class op
{
public:
            void *operator new(size_t size, char const *file,int line);
            void operator delete(void *p);
};
void *op::operator new(size_t size, char const *file,int line)
{
 void *p = malloc(size);
 cout<<"\n New called the file:"<<file<<"\n line:"<<line<<"\n size:"<<size<<"\n p:"<<p<<endl;
 return p;
 }
void op::operator delete(void *p)
{
 cout<<"\n Delete called p:"<<p<<endl;
 free(p);
 }
void main()
{
clrscr();
op *X = new(__FILE__,__LINE__)op;
delete X;
getch();
}

OUTPUT:

 New called the file:NEWDEL1.CPP
 line:24
 size:1
 p:0x8fba0df6
 Delete called p:0x8fba0df6

RESULT:
Thus the C++ program to implement the overloading of new and delete operators to provide dynamic allocation of memory was executed.

Matrix Class with Constructor, Destructor, Copy constructor and Assignment operator overloading


Matrix Class with Constructor, Destructor, Copy constructor and Assignment operator overloading

Aim:
            To implement Matrix class with dynamic memory allocation with constructors, destructor, and overloading of assignment operator.
           
Algorithm:
  1. Start the process.
  2. Create a class matrix.
  3. Declare and define default, parameterized, copy constructor.
  4. Define member functions getmatrix and showmatrix.
  5. Define destructor.
  6. Get the size of the matrix and the elements of the matrix.
  7. Depends on the size of the matrix, the memory is allotted for the matrix dynamically.
  8. Get the elements of the matrix.
  9. Call the copy constructor and copy the values of object m1 to m2.
  10. With the help of assignment operator, we are assigning the values of m1 to m3.
  11. Display the result.
  12. Stop the process.

Program :
#include<iostream.h>
#include<conio.h>
class matrix
{
            int **m;
            int row, col;
public:
            matrix()
            {
                        row=col=0;
                        m=NULL;
            }
            matrix(int r ,int c);
            ~matrix();
            void getmatrix();
            void showmatrix();
            matrix(matrix &m2); //copy constructor
            matrix& operator=(matrix &m2);

};

matrix::~matrix()
{
            for(int i=0;i<row;i++)
            delete m[i];
            delete m;
}

matrix::matrix(int r ,int c)
{
            row=r;
            col=c;
            m=new int*[row];
            for(int i=0;i<row;i++)
            m[i]=new int[col];
}
matrix::matrix(matrix &m2)
{
            cout<<"\nCopy constructor invoked\n";
            row=m2.row;
            col=m2.col;
            m=new int*[row];
            for(int i=0;i<=row;i++)
            m[i]=new int[col];
            for(i=0;i<=row;i++)
            for(int j=0;j<=row;j++)
            m[i][j]=m2.m[i][j];
}
matrix& matrix::operator=(matrix &m2)
{
            cout<<"\nAssignment Operator Overloading\n";
            row = m2.row;
            col = m2.col;
            m = new int*[row];
            for(int i=0;i<=row;i++)
            m[i]=new int[col];
            for(i=0;i<=row;i++)
            for(int j=0;j<=row;j++)
            m[i][j]=m2.m[i][j];
            return *this;
}
void matrix::getmatrix()
{
            for(int i=0;i<row;i++)
            for(int j=0; j<col; j++)
                        cin>>m[i][j];
}
void matrix::showmatrix()
{
            for(int i=0;i<row;i++)
            {
                        for(int j=0;j<col;j++)
                                    cout<<"\t"<<m[i][j];
                                    cout<<"\n";
            }

}
void main()
{
            int r,c;
            clrscr();
            cout<<"\nEnter rows and columns of the matrix:\n";
            cin>>r>>c;
            matrix m1(r,c);
            cout<<"\nEnter the matrix elements one by one:\n";
            m1.getmatrix();
            cout<<"\nEntered matrix is:\n";
            m1.showmatrix();
            //invoking copy constructor
            matrix m2=m1;
            cout<<"\nResult of the copy constructor is:\n";
            m2.showmatrix();
            matrix m3;
            m3=m1;
            cout<<"\nResult of assignment operator overloading:\n";
            m3.showmatrix();
            getch();
}

OUTPUT 1:

Enter rows and columns of the matrix:
3
2

Enter the matrix elements one by one:
2
3
1
4
5
7

Entered matrix is:
        2       3
        1       4
        5       7

Copy constructor invoked

Result of the copy constructor is:
        2       3
        1       4
        5       7

Assignment Operator Overloading

Result of assignment operator overloading:
        2       3
        1       4
        5       7

Output 2:

Enter rows and columns of the matrix:
3 3

Enter the matrix elements one by one:
12 23 34 45 56 67 78 89 90

Entered matrix is:
        12      23      34
        45      56      67
        78      89      90

Copy constructor invoked

Result of the copy constructor is:
        12      23      34
        45      56      67
        78      89      90

Assignment Operator Overloading

Result of assignment operator overloading:
        12      23      34
        45      56      67
        78      89      90

Result:
Thus the program for constructor, destructor, copy constructor and assignment operator overloading was executed.