8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 1/29
6(a). BUBBLE SORT USING TEMPLATES
#include<iostream>template<class X>
void bubble(X *items,int count){X t;
for(int a=1;a<count;a++)
for(int b=count-1;b>=a;b--)if(items[b-1]>items[b]){
t=items[b-1];
items[b-1]=items[b];
items[b]=t;}
}
int main(){
int iarray[7]={7,5,4,3,9,8,6};
double darray[5]={4.3,2.5,-0.9,10.2,3.0};
cout<<"\n";cout<<" BUBBLE SORT "<<"\n";
cout<<"=============="<<"\n\n";
cout<<"Here is unsorted integer array:";for(int i=0;i<7;i++)
cout<<iarray[i]<<' ';
cout<<endl;
bubble(iarray,7);cout<<"Here is sorted integer array:";
for(int i=0;i<7;i++)
cout<<iarray[i]<<' ';cout<<endl;
cout<<"Here is unsorted double array:";
for(int i=0;i<5;i++)cout<<darray[i]<<' ';
cout<<endl;
bubble(darray,5);
cout<<"Here is sorted double array:";
for(int i=0;i<5;i++)cout<<darray[i]<<' ';
cout<<endl;
return 0;}
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 2/29
OUTPUT:
BUBBLE SORT==============
Here is unsorted integer array:7 5 4 3 9 8 6
Here is sorted integer array: 3 4 5 6 7 8 9Here is unsorted double array:4.3 2.5 -0.9 10.2 3
here is sorted double array:-0.9 2.5 3 4.3 10.2
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 3/29
6(b). INSERTION SORT USING TEMPLATES
#include<iostream>
template<class Y>void insertion_sort(Y *items,int count)
{
Y key;int i;
for(int j=1;j<count;j++)
{key=items[j];i=j-1;
while(items[i]>key && i>=0)
{
items[i+1]=items[i];i--;
}
items[i+1]=key;}
}
int main()
{int iarray[7]={7,5,4,3,9,8,6};
double darray[5]={4.3,2.5,-0.9,10.2,3.0};
cout<<"\n"<<" INSERTIION SORT"<<"\n";cout<<"====================== ";
cout<<"\n\n";
cout<<"Here is unsorted integer array:";
for(int i=0;i<7;i++)cout<<iarray[i]<<' ';
cout<<endl;
insertion_sort(iarray,7);cout<<"Here is sorted integer array:";
for(int i=0;i<7;i++)
cout<<iarray[i]<<' ';cout<<endl;
cout<<"Here is unsorted double array:";
for(int i=0;i<5;i++)
cout<<darray[i]<<' ';
cout<<endl;insertion_sort(darray,5);
cout<<"Here is sorted double array:";
for(int i=0;i<5;i++)cout<<darray[i]<<' ';
cout<<endl;
return 0;}
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 4/29
OUTPUT:
INSERTIION SORT
======================
Here is unsorted integer array:7 5 4 3 9 8 6Here is sorted integer array: 3 4 5 6 7 8 9
Here is unsorted double array:4.3 2.5 -0.9 10.2 3
Here is sorted double array:-0.9 2.5 3 4.3 10.2
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 5/29
6(c). QUICK SORT USING TEMPLATES
#include<iostream.h>#include<conio.h>
template<class T>
void swap(T &v1,T &v2){
T temp=v2;
v2=v1;v1=temp;}
template<class T>
void quicksort(T *array,int hi,int lo=0)
{while(hi>lo)
{
int i=lo;int j=hi;
do
{
while(array[i]<array[lo]&&i<j)i++;
while(array[--j]>array[lo])
if(i>j)swap(array[i],array[j]);
}while(i>j);
swap(array[lo],array[j]);
if(j-lo>hi-(j+1)){
quicksort(array,j-1,lo);
lo=j+1;}
else
{quicksort(array,hi,j+1);
hi=j-1;
}
}
}void main()
{
int iarray[5]={5,4,3,8,6};double darray[5]={4.3,2.5,0.9,8.6,6.2};
clrscr();
cout<<"\tQUICK SORT"<<"\n\n";cout<<"\nHere is unsorted integer array:\n";
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 6/29
for(int i=0;i<5;i++)
cout<<iarray[i]<<"\t";quicksort(iarray,5);
cout<<"\nHere is sorted integer array:\n";
for(i=0;i<5;i++)
cout<<iarray[i]<<"\t";cout<<endl;
cout<<"\nHere is unsorted double array:\n";
for(i=0;i<5;i++)cout<<darray[i]<<"\t";
quicksort(darray,5);
cout<<"\nHere is sorted double array:\n";for(i=0;i<5;i++)
cout<<darray[i]<<"\t";
cout<<endl;
getch();
}
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 7/29
OUTPUT:
QUICK SORT======================
Here is unsorted integer array:
7 5 4 3 9 8 6Here is the sorted integer array
9 8 7 6 5 4 3
Here is unsorted double array:4.3 2.5 -0.9 10.2 3Here is sorted double array:
10.2 4.3 3 2.5 -0.9
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 8/29
6(d). MERGE SORT USING TEMPLATES
#include <iostream>
#include <fstream>
template <class T>void merge(T numbers[], T temp[], int left, int middle, int right)
{
int i, leftSide, numElements, tempPosition;leftSide = (middle-1);tempPosition = left;
numElements = (right - left + 1);
while((left <= leftSide) && (middle <= right))
{if(numbers[left] <= numbers[middle])
{
temp[tempPosition] = numbers[left];tempPosition++;
left++;
}
else {temp[tempPosition] = numbers[middle];
tempPosition++;
middle++;}
}
while(left <= leftSide)
{temp[tempPosition] = numbers[left];
left++;
tempPosition++;}
while(middle <= right)
{temp[tempPosition] = numbers[middle];
middle++;
tempPosition++;
}
for(i=0; i<=numElements; i++){
numbers[right] = temp[right];
right--;}
}
template<class T>void mergeSort(T numbers[], T temp[], int left, int right)
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 9/29
{
int middle;if(right > left)
{
middle = (right + left) / 2;
mergeSort(numbers, temp, left, middle);mergeSort(numbers, temp, (middle+1), right);
merge(numbers, temp, left, (middle+1), right);
}}
template <class T>
void mergeSort(T numbers[], T temp[], int arrsize){
mergeSort(numbers,temp,0,arrsize - 1);
}
int main(){
int n[5]={10,2,4,3,21};int t[5];
char x[5]={'d','f','z','m','s'};
char y[5];mergeSort(n,t,5);
mergeSort(x,y,5);
{
printf(“here the sorted integer array is”); for(int a = 0; a < 5; a++)
cout << n[a] << " ";
}
cout<<endl;{
printf(“here the sorted character array is”);
for(int a = 0; a < 5; a++)cout << x[a] << " ";
}
cout<<endl;return 0;
}
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 10/29
OUTPUT:
Here the sorted integer array is 2 3 4 10 21
Here the sorted character array is d f m s z
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 11/29
7(a).STACK IMPLEMENTATION USING EXCEPTION HANDLING
#include<iostream.h>
#include<stdlib.h>int i=1;
class stack
{int element;
stack *next;
public:class emp
{
};
class full
{};
stack *push(stack*,int);stack *pop(stack*);
void stack_display(stack*);
}*head,object;stack* stack::push(stack *head,int key)
{
i++;
if(i<=10){
stack *temp,*temp1;
temp1=head;
temp=new stack;temp->element=key;
if(head==NULL)
head=temp;else
{
while(head->next!=NULL)head=head->next;
head->next=temp;
head=temp1;
}return head;
}
else{
throw full();
return head;
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 12/29
}
}stack *stack::pop(stack *head)
{
stack *temp;
if(head!=NULL){
temp=head;
if(head->next==NULL){
cout<<"\n The POPPED element from the STACK is:"<<head->element<<endl;
return NULL;}
while(head->next->next!=NULL)
head=head->next;
cout<<"\n The popped element from the stack is:"<<head->next->element;
cout<<endl;head->next=head->next->next;
head=temp;return head;
}
if((head==NULL)&&(i==0)){
throw emp();
} return head;
}void stack::stack_display(stack *head)
{
if(head!=NULL)
{while(head->next!=NULL)
{
cout<<head->element<<"\n";head=head->next;
}
cout<<head->element;cout<<endl;
}else
cout<<" The Stack is Empty\n";
}void choice()
{
int key,ch;cout<<"\n Choose the operations to be performed \n";
cout<<"\n 1.PUSH \n 2.POP \n 3.EXIT \n";
cin>>ch;
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 13/29
head=NULL;
while(1){
switch(ch)
{
case 1:cout<<"\n";
cout<<i;
cout<<"\n Enter the elementto be pushed \n";cin>>key;
head=object.push(head,key);
cout<<"\n The stack after push operation is \n";object.stack_display(head);
cout<<"\n";
break;
case 2:
cout<<i;cout<<"\n\n";
head=object.pop(head);break;
case 3:
exit(0);default:
cout<<"\n Enter the correct choice \n";
break;
}cout<<"\n Choose the operation to be performed \n";
cout<<"\n 1.PUSH \n 2.POP \n3.EXIT \n";
cin>>ch;
}}int main()
{
try{
choice();
}catch(stack::emp)
{
cout<<"\n Exception:STACK Empty \n Program terminates\n";
}catch(stack::full)
{
printf("\n Exception : Only 10 Items can be added STACK is full\n Program terminates \n");}
return 0;
}}
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 14/29
OUTPUT:
Choose the operations to be performed
1. PUSH
2. POP
3. EXIT1
Enter the elemet to be pushed
20The stack after push operation is
20
Choose the operation to be performed
1.PUSH
2.POP
3.EXIT
1Enter the elemet to be pushed
30The stack after push operation is
30
20
Choose the operation to be performed
1.PUSH
2.POP3.EXIT
1
Enter the elemet to be pushed
40The stack after push operation is
40
3020
Choose the operation to be performed
1.PUSH2.POP
3.EXIT
2
The popped element from the stack is: 40
Choose the operation to be performed
1.PUSH2.POP
3.EXIT
3
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 15/29
7(b). QUEUE IMPLEMENTATION USING EXCEPTION HANDLING
#include<iostream.h>
#include<stdlib.h>
int i=1;class stack
{
int element;stack *next;
public:
class emp{ };
class full
{ };
stack *push(stack*,int);
stack *pop(stack*);void stack_display(stack*);
}*head,object;
stack* stack::push(stack *head,int key)
{i++;
if(i<=10)
{
stack *temp,*temp1;temp1=head;
temp=new stack;
temp->element=key;
if(head==NULL)head=temp;
else
{while(head->next!=NULL)
head=head->next;
head->next=temp;head=temp1;
}
return head;
}else
{
throw full;return head;
}
}
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 16/29
stack *stack::pop(stack *head)
{stack *temp;
if(head!=NULL)
{
temp=head->next;if(head!=NULL)
{
cout<<"\n The dequeue element from the Q is:"<<head->element<<endl;return temp;
}
}if(head==NULL)
{
throw emp;
return head;
}}
void stack::stack_display(stack *head){
if(head!=NULL)
{while(head->next!=NULL)
{
cout<<head->element<<"\n";
head=head->next;}
cout<<head->element;
cout<<endl;
}else
cout<<" The Queue is Empty\n";
}void choice()
{
int key,ch;cout<<"\n Choose the operations to be performed \n";
cout<<"\n 1.INSERT \n 2.DELETE \n 3.EXIT \n";
cin>>ch;
head=NULL;while(1)
{
switch(ch){
case 1:
cout<<"\n";
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 17/29
cout<<"\n Enter the elementto be insertion \n";
cin>>key;head=object.push(head,key);
cout<<"\n The Queue after Insertion operation is \n";
object.stack_display(head);
cout<<"\n";break;
case 2:
cout<<"\n\n";head=object.pop(head);
break;
case 3:exit(0);
default:
cout<<"\n Enter the correct choice \n";
break;
}cout<<"\n Choose the operation to be performed \n";
cout<<"\n 1.INSERT \n 2.DELETE \n3.EXIT \n";cin>>ch;
}
}int main()
{
try
{choice();
}
catch(stack::emp)
{cout<<"\n Exception: Queue is Empty \n Program terminates\n";
}
catch(stack::full){
cout<<"\n Exception : Only 10 Items can be added Q is full\n Program terminates \n";
}return 0;
}
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 18/29
OUTPUT:
Choose the operations to be performed1. Insert
2.Delete
3.exit
1Enter the elemet to be insertion
20
The Queue after Insertion operation is20
Choose the operation to be performed1.Insert
2.delete
3.exit
1
Enter the elemet to be insertion30
The Queue after Insertion operation is20
30
Choose the operation to be performed
1.Insert
2.delete
3.exit1
Enter the elemet to be insertion
40
The Queue after Insertion operation is20
30
40
Choose the operation to be performed
1.Insert2.delete
3.exit
2
The dequeqe element from the Queue is :20
Choose the operation to be performed
1.Insert2.delete
3.exit
3
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 19/29
9.POLYMORPHISM & RTTI USING INHERITANCE
#include<iostream.h>
#include<conio.h>
static int x;class shape
{
public:virtual void example()=0;
};
class rectangle:public shape{
public:
void example()
{
cout<<"\n RECTANGLE\n";cout<< "******\n";
cout<< "* *\n";cout<< "* *\n";
cout<< "******\n";
}};
class triangle:public shape
{
public:void example()
{
cout<<"\n TRIANGLE\n";
cout<<" *\n";cout<<" * *\n";
cout<<" * *\n";
cout<<"* *\n";cout<<"********\n";
}
};class line:public shape
{
public:
void example(){
cout<<"\n LINE\n";
cout<<"******\n";}
};
class circle:public shape
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 20/29
{
public:void example()
{
cout<<"\n CIRCLE\n";
cout<<" ***\n";cout<<" ** **\n";
cout<<"** **\n";
cout<<" ** **\n";cout<<" ***\n";
}
};class ellipse:public shape
{
public:
void example()
{cout<<"\n ELLIPSE\n";
cout<<" ******\n";cout<<" ** **\n";
cout<<"** **\n";
cout<<" ** **\n";cout<<" ******\n";
}
};
class polygon:public shape{
public:
void example()
{cout<<"\n POLYGON\n";
cout<< " *\n";
cout<< " * *\n";cout<< " * *\n";
cout<< "* *\n";
cout<< "* *\n";cout<< "* *\n";
cout<< "*******\n";
}
};shape *generator()
{
switch(x){
case 1:
return new line;
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 21/29
case 2:
return new rectangle;case 3:
return new triangle;
case 4:
return new circle;case 5:
return new ellipse;
case 6:return new polygon;
}
return NULL;}
void main()
{
shape *p;
for(x=1;x<7;x++){
p=generator();typeid(*p)
p->example();
}getch();
}
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 22/29
OUTPUT:
Line
******
Rectangle
******
* *
* *
******
Triangle
*
* *
* *
* *
********
circle
***
** **
** **
** **
***
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 23/29
Elipse
******
** **
** **
** **
******
polygon
*
* *
* *
* *
* *
* *
*******
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 24/29
8. MINIMUM SPANNING TREE USING INHERITANCE
#include<iostream>
#define MAX 10
class vertex
{public :
int cost[MAX][MAX], tree[MAX][MAX];
int n;void readmatrix();
};
class edge:public vertex{
public:
void edgevalue(void);
int spanningtree(int);
void display(int);};
void vertex :: readmatrix()
{
int i, j;cout <<"\nEnter the number of vertices in the Graph :";
cin >> n;
}
void edge::edgevalue()
{
cout << "\nEnter the Cost matrix of the Graph\n\n";
for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)
cin >> cost[i][j];
}
int edge :: spanningtree(int src)
{int visited[MAX], d[MAX], parent[MAX];
int i, j, k, min, u, v, stcost;
for (i = 1; i <= n; i++)
{d[i] = cost[src][i];
visited[i] = 0;
parent[i] = src;}
visited[src] = 1;
stcost = 0;
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 25/29
k = 1;
for (i = 1; i < n; i++){
min = 999;
for (j = 1; j <= n; j++)
{ if (!visited[j] && d[j] < min)
{
min = d[j];u = j;
}
}visited[u] = 1;
stcost = stcost + d[u];
tree[k][1] = parent[u];
tree[k++][2] = u;
for (v = 1; v <= n; v++)if (!visited[v] && (cost[u][v] < d[v]))
{d[v] = cost[u][v];
parent[v] = u;
}}
return (stcost);
}
void edge :: display(int cost)
{
int i;
cout << "\nThe Edges of the Mininum Spanning Tree are\n\n";for (i = 1; i < n; i++)
cout << tree[i][1] << " " << tree[i][2] << endl;
cout << "\nThe Total cost of the Minimum Spanning Tree is : " << cost;}
int main()
{int source, treecost;
edge ob;
ob.readmatrix();
ob.edgevalue();cout << "\nEnter the Source : ";
cin >> source;
treecost = ob.spanningtree(source);ob.display(treecost);
return 0;
}
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 26/29
OUTPUT :
Enter the number of vertices in the Graph :2
Enter the Cost matrix of the Graph1
1
22
Enter the Source : 2
The Edges of the Mininum Spanning Tree are
2 1
The Total cost of the Minimum Spanning Tree is : 2
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 27/29
10.IMPLEMENTATION OF COMPLEX NUMBER USING FILES
#include <iostream>
#include <fstream>
class comp
{int real,img;
public:
comp(int a,int b){
real=a;
img=b;}
void show()
{
cout<<"The complex number is : ";
if(img<0)cout<<real<<img<<"i"<<"\n";
elsecout<<real<<"+"<<img<<"i"<<"\n";
}
};
int main()
{
int r,i;ofstream infile;
infile.open("rrr.txt");
do
{cout<<"Enter the Real and imaginary values [ enter real & imaginary to zero for exit ] :"<<endl;
cin>>r>>i;
if(r!=0 && i!=0){
comp c1(r,i);
c1.show();if(i<0)
infile<<r<<i<<"i"<<"\n";
else
infile<<r<<"+"<<i<<"i"<<"\n";}
}
while(r!=0 && i!=0);
infile.close();
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 28/29
char line[100];
ifstream outfile;outfile.open("rrr.txt");
cout<<" File is opened ...."<<endl;
while(outfile)
{outfile.getline (line,100);
cout << line << endl;
}outfile.close();
return 0;
}
8/4/2019 Oops Lab Pgms 6-10
http://slidepdf.com/reader/full/oops-lab-pgms-6-10 29/29
OUTPUT:
Enter the Real and imaginary values [enter real & imaginary to zero for exit] :
200
10
The complex number is: 200+10i
Enter the Real and imaginary values [enter real & imaginary to zero for exit ] :
321
11
The complex number is : 321+11i
Enter the Real and imaginary values [enter real & imaginary to zero for exit] :
4000
300
The complex number is: 4000+300i
Enter the Real and imaginary values [enter real & imaginary to zero for exit] :
0
0
File is opened....
200+10i
321+11i
4000+300i