CONSTRUCTORS, DESTRUCTORS & COPY CONSTRUCTOR.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class test
private:
int x;
public:
test(int x1)
cout<<”\n Parameterized Constructor”;
x=x1;
test(const test &t2)
cout<<”\n Copy Constructor”;
x=t2.x;
int getx()
cout<<”\n Normal Function”;
return x;
~test()
{
cout<<”\n Destructor”;
int main()
Clrscr();
test t1(7);
test t2=t1;
cout<<”\n t1.x=”<<t1.getx();
cout<<”\n t2.x=”<<t2.getx();
getch();
return 0;
}
OUTPUT:
Parameterized Constructor
Copy Constructor
Normal Function
t1.x=7
Normal Function
t2.x=7
Destructor
Destructor
RESULT:
Thus the program is executed successfully and the result has been verified.
EX NO: 02(A)
FRIEND FUNCTION AND FRIEND CLASS FRIEND FUNCTION.
PROGRAM:
#include<iostream.h>
class A
int a;
public:
A() {a=0;}
//global friend function
friend void showA(A&);
};
void showA(A & x)
//Since showA() is a friend,it can access
//private members of A
std::cout<<”A::a=”<<x.a;
int main()
A a;
showA(a);
return 0;
OUTPUT:
A::a = 0
RESULT:
Thus the program is executed successfully and the result has been verified.
EX NO : 02(B)
FRIEND CLASS
PROGRAM:
#include<iostream>
class A
private:
int a;
public:
A() {a = 0;}
friend class B; // Friend Class };
class B
private:
int b;
public:
void showA(A& x)
Std::cout<<”A::a=” <<x.a; } };
int main()
A a;
B b;
b.showA(a);
return 0;
OUTPUT:
A::a=0
RESULT:
Thus the program is executed successfully and the result has been verified.
EX NO: 03(A)
CONCEPT OF SINGLE INHERITANCE.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class vehicle
int no;
char name[10];
public:
void getdata()
cout<<”Enter no,Name:”;
cin>>no>>name;
cout<<”\n\tvehicle Details”;
cout<<”\n\tNo:”<<no<<”\n\tName:”<<name;
};
Class bike: public vehicle
public:
void display()
cout<<”\n\tprice:”<<55000;
};
void main()
clrscr();
bike b;
b.getdata();
b.display();
getch();
}
OUTPUT:
Enter no,Name: 201 yamaha
vehicle Details
No:201
Name:Yamaha
Price:55000
RESULT:
Thus the program is executed successfully and the result has been verified.
EX NO: 03(B)
CONCEPT OF MULTIPLE INHERITANCE.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class student
protected:
int rno,m1,m2;
public:
void get()
cout<<”Enter the Roll no:”;
cin>>rno;
cout<<”Enter the test Mark:”;
cin>>m1>>m2;
};
class sports
protected:
int sm;
public:
void getsm()
cout<<”Enter the sports mark:”;
cin>>sm;
}
};
class statement:public student,public sports
int tot,avg;
public:
void display()
tot=(m1+m2+sm);
avg=tot/3;
cout<<”\n\tRoll no :”<<rno<<”\n\tTotal :”<<tot;
cout<<”|n\tAverage :”<<avg;
};
void main()
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch():
OUTPUT:
Enter the Roll no: 100
Enter the test Mark: 80 90
Enter the sports mark: 70
Roll no: 100
Total: 240
Average:80
RESULT:
Thus the program is executed successfully and the result has been verified.
EX NO: 03(C)
CONCEPT OF HIERARCHICAL INHERITANCE.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class A
public:
int a,b;
void getnumber()
cout<<”\n\nEnter Number=\t”;
cin>>a;
};
class B:public A
public:
void square()
getnumber();
cout<<”\n\n\tSquare of the Number=\t”<<(a*a);
};
class C : public A
public:
void cube()
getnumber();
cout<<”\n\n\tCube of the Number=\t”<<(a*a*a);
};
void main()
clrscr();
B b1;
b1.square();
C c1;
c1.cube();
getch();
OUTPUT:
Enter Number = 5
Square of the Number = 25
Enter Number = 6
Cube of the Number = 216
RESULT:
Thus the program is executed successfully and the result has been verified.
EX NO: 04(A)
POLYMORPHISM
PROGRAM:
#include<iostream.h>
#include<conio.h>
class base
public:
virtual void print()
cout<<”print base class”<<endl;
void show()
cout<<”show base class”<<endl;
};
class derived:public base
public:
void print ()
cout<<”print derived class”<<endl;
void show ()
cout<<”show derived class”<<endl;
}
};
int main()
base*bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
return 0;
OUTPUT:
print derived class
show base class
RESULT:
Thus the program is executed successfully and the result has been verified.
EX NO: 04(B)
CONCEPT OF FUNCTION OVERLOADING.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#dedfine pi 3.14
class obj
public:
void volume(int a);
void volume(float r);
void volume(float r,float h);
void volume(float r,int h);
};
void obj::volume(int a)
int v;
v=a*a*a;
cout<<”\n The Volume of Cube is:”<<v;
void obj::volume(float r)
float v;
v=4/3.0*pi*r*r*r;
cout<<”\n The Volume of Sphere is:”<<v;
void obj::volume(float r,float h)
{
float v;
v=1/3.0*pi*r*r*h;
cout<<”\n The Volume of Cone is:”<<v;
void obj::volume(float r,int h)
float v;
v=pi*r*r*h;
cout<<”\n The Volume of Cylinder is:”<<v;
void main()
clrscr();
obj o;
int a,ch;
float r,h;
cout<<”\n Output\n”;
do
cout<<”\n 1.Cube\n 2.Sphere\n 3.Cone\n 4.Cylinder\n 5.Exit”;
cout<<”\n Enter your choice”;
cin>>ch;
switch(ch)
{
case 1:
cout<<”\n Enter the side of cube in integer”;
cin>>a;
0.volume(a);
break;
case 2:
cout<<”\n Enter the radius of sphere in float”;
cin>>r;
0.volume(r);
break;
case 3:
cout<<”\n Enter the radius and height of cone in float”;
cin>>r>>h;
0.volume(r,h);
break;
case 4:
cout<<”\n Enter the radius of cylinder in float”;
cin>>r;
cout<<”\n Enter the height of cylinder in integer”;
cin>>h;
0.volume(r,h);
break;
case 5:
break;
default:
cout<<”\n wrong choice”;
}
while(ch!=5);
getch();
OUTPUT:
1.Cube
2.Sphere
3.Cone
4.Cylinder
5.Exit
Enter your choice 1
Enter the side of cube in integer 3
The Volume of Cube is: 27
1.Cube
2.Sphere
3.Cone
4.Cylinder
5.Exit
Enter your choice 3
Enter the radius and height of cone in float 3.2 3.3
The Volume of Cone is: 35.368961
Enter your choice 2
Enter the radius of sphere in float 3.6
The Volume of Sphere is: 195.333
RESULT:
Thus the program is executed successfully and the result has been verified.
EX NO: 05
VIRTUAL FUNCTION
PROGRAM:
#include<iostream>
//using namespace std;
class base
public:
virtual void print()
cout<<”print base class”<<endl;
void show()
cout<<”show base class”<<endl;
};
class derived : public base {
public:
void print()
cout<<”print derived class”<<endl;
void show()
cout<<”show derived class”<<endl;
}
};
int main()
base* bptr;
derived d;
bptr =&d;
//virtual function, binded at runtime
bptr->print();
//Non-virtual function, binded at compile time
bptr->show();
OUTPUT:
print derived class
show base class
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:06(A)
OVERLOAD UNARY OPERATOR USING MEMBER FUNCTION
PROGRAM:
#include<iostream>
class Number
Private:
int x;
Public:
Number(int p)
{ x=p; }
void operator-()
{ x=-x; }
void display()
cout<<”x=”<<x;
};
int main()_
Number n(10);
-n;
n.display();
return 0;
OUTPUT:
x=-10
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:06(B)
OVERLOAD BINARY OPERATOR USING NON-MEMBER FUNCTION
PROGRAM:
#include<iostream>
//using namespace std;
class Complex
Private:
float real;
float imag;
public:
Complex(){}
Complex(float r, float i)
real=r;
imag=I;
void display()
Cout<<real<<”+I”<<imag;
friend Complex operator+(Complex &,Complex &);
};
Complex operator+(Complex &c1,Complex &c2)
Complex temp;
temp.real=c1.real+c2.real;
temp.imag=c1.imag+c2.imag;
return temp;
int main()
Complex c1(3,4);
Complex c2(4,6);
Complex c3=c1+c2;
C3.display();
Return 0;
OUTPUT:
17+i10
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:07(A)
FUNCTION TEMPLATES AND CLASS TEMPLATE
FUNCTION TEMPLATES
PROGRAM:
#include<iostream>
//using namespace std;
template<typename T>
T myMax(T x,T y)
Return(x>y)?x:y;
int main()
cout<<myMax<int>(3,7)<<endl;//Call myMax for int
cout<<myMax<double>(3.0,7.0)<<endl;// call myMax for double
cout<<myMax<char>(‘g’,’e’)<<endl;// call myMax for char
return 0;
OUTPUT:
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:07(B)
CLASS TEMPLATES
PROGRAM
#include<iostream>
//using namespace std;
template<class T, class U>
class A
T x;
U y;
public:
A()
cout<<”Constructor Called”<<endl;
};
int main()
A<char, char>a;
A<int, double>b;
OUTPUT:
Constructor Called
Constructor Called
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:08(A)
EXCEPTION HANDLING
PROGRAM:
#include<iostream>
float division(int x, int y)
if(y==0)
throw “ Attempted to divide by zero!”;
return (x/y);
int main()
int I = 25;
int j = 0;
float k = 0;
try
k = division(i,j);
cout<<k<<endl;
catch (const char* e)
Cerr<<e<<endl;
return 0; }
OUTPUT:
Attempted to divide by zero!
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:08(B)
USER-DEFINED EXCEPTIONS
PROGRAM:
#include<iostream>
#include<exception>
using namespace std;
class MyException : public exception
public:
const char * what() const throw()
return “Attempted to divide by zero!\n”;
};
int main()
Try
int x,y;
cout<<”Enter the two numbers:\n”;
cin>>x>>y;
if(y==0)
MyException z;
Throw z;
}
else
{ cout<<”x/y=”<<x/y<<endl;
Catch(exception& e)
Cout<<e.what();
}
OUTPUT:
Enter the two numbers :
10
x/y = 5
Enter the two numbers:
10
Attempted to divide by zero!
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:09
STANDARD TEMPLATE LIBRARY CONCEPT
PROGRAM:
#include<iostream>
#include<iterator>
#include<vector>
//using namespace std;
int main()
vector<int>ar = {1,2,3,4,5};
vector<int>::iterator ptr;
cout<<”The vector elements are :”;
for(ptr = ar.begin();ptr<ar.end();ptr=++)
cout<<*ptr<<””;
return 0;
}
OUTPUT:
The vector elements are : 1 2 3 4 5
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:10
FILE STREAM CLASS
PROGRAM:
#include<fstream>
#include<iostream>
//using namespace std;
int main() {
char input[75];
ofstream os;
os.open(“testout.txt”);
cout<<”Writing to a text file:”<<endl;
cout<<”Please Enter your name:”;
cin.getline(input,100);
os<<input<<endl;
cout<<”Please Enter your age:”;
cin>>input;
cin.ignore();
os<<input<<endl;
os.close();
ifstream is;
string line;
is.open(“testout.txt”);
cout<<”Reading from a text file:”<<endl;
while(getline(is,line))
Cout<<line<<endl;
}
is.close();
return 0;
OUTPUT:
Writing to a text file:
Please Enter your name:Nakul Jain
Please Enter your age: 22
Reading from a text file: Nakul Jain
22
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:11(a)
THE QUEUE USING AN ARRAY
PROGRAM:
#include<iostream>
Using namespace std;
Int queue[100],n=100,front = -1,rear= - 1;
Void Insert()
Int val;
If(rear == n-1)
Cout<<”Queue Overflow”<<endl;
else {
if(front == - 1)
front=0;
cout<<”Insert the element in queue:”<<endl;
cin>>val;
rear++;
queue [rear]=val;
Void Delete()
If (front == - 1|| front>rear) {
Cout<<”Queue Underflow”;
return;
}
else
Cout<<”Element deleted from queue is : “<<queue[front]<<endl;
front ++;;
Void Display ()
If (front == - 1)
Cout<<”Queue is empty”<<endl;
else {
cout<<”Queue elements are : “;
for (int I = front; i<=rear;i++)
cout<<queue[i]<<” “;
cout<<endl;
Int main ()
int ch;
cout<<”1)Insert element to queue”<<endl;
cout<<”2)Delete element frm queue”<<endl;
cout<<”3)Display all the elements of queue”<<endl;
cout<<”4)Exit”<<endl;
do
{
cout<<”Enter your choice:”<<endl;
cin<<ch;
switch (ch)
case 1:
Insert();
break;
case 2:
Delete();
break;
case3:
Display();
break;
case 4:
cout<<”Invalid choice”<<endl;
while(ch!=4);
return 0;
}
OUTPUT:
1)Insert element to queue
2)Delete element from queue
3)Display all the elements of queue
4)Exit
Enter your choice : 1
Insert the element in queue:4
Enter your choice : 1
Insert the element in queue:3
Enter your choice : 1
Insert the element in queue:5
Enter your choice : 2
Element deleted from queue is : 4
Enter your choice : 3
Queue elements are:3 5
Enter your choice : 7
Invalid choice
Enter your choice : 4
Exit
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:10
EX.NO:11(b)
PROGRAM INFIX TO POSTFIX EXPRESSION USING STACK (ARRAY)
PROGRAM:
#include<iostream>
#include<string>
#define MAX 20
Using namespace std;
Char stk[20];
Int top=-1;
//Push function here,inserts value in stack and increments stack top by 1
Void push (char oper)
If(top==MAX -1)
Cout<<”stackfull!!!”;
else
top++;
stk[top]=oper;
//Function to remove an item from stack. It decreases top by 1
char pop()
char ch;
if(top==-1)
Cout<<”stackempty!!!!”;
else
ch=stk[top];
stk[top]=’\0’;
top--;
return(ch);
return 0;
int priority (char alpha)
if (alpha == ‘=’|| alpha ==’-‘)
return (1);
if(alpha ==’*’||alpha ==’/’)
return(2);
if(alpha ==’$’)
return(3);
}
return 0;
String convert(string infix)
int i=0;
string postfix = “”;
while(infix[i]!=’\0’)
if(infix[i]>=’a’&&infix[i]<=’z’||infix[i]>=’A’&&infix[i]<=’Z’)
Postfix.insert(postfix.end(),infix[i]);
i++;
else if(infix[i]==’(‘|| infix [i]==’{‘||infix[i]==’[‘)
Push(infix[i]);
i++;
else if(infix[i]==’)’||infix[i]==’}’||infix[i]==’]’)
if (infix[i]==’)’)
while(stk[top]!=’(‘)
postfix.insert(postfix.end(),pop());
}
pop();
i++;
if (infix[i]==’]’)
while(stk[top]!=’[‘)
postfix.insert(postfix.end(),pop());
pop();
i++;
if(infix[i]==’}’)
while(stk[top]!=’{‘)
Postfix.insert(postfix.end(),pop());
pop();
i++;
else if(priority(infix[i])<=priority(stk[top])){
postfix.insert(postfix.end(),pop());
while (priority(stk[top])==priority(infix[i])){
postfix.insert(postfix.end(),pop());
if (top<0) {
break;
Push(infix[i]);
i++;
while(top!=-1)
Postfix.insert(postfix.end(),pop());
Cout<<”The converted postfix string is: “<<postfix;//it will print postfix conversion return postfix;
int main()
int cont;
string infix,postfix;
cout<<”\n Enter the infix expression : “; //enter the expression
cin>>infix;
postfix=convert(infix);
return 0;
}
OUTPUT:
Enter the infix expression: a=b*2+5
The converted postfix string is: ab*=+25
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:12(a)
ARRAY IMPLEMENTATION OF STACK
PROGRAM:
#include<iostream>
//using namespace std;
int stack[100],n=100,top=-1;
void push(int val) {
if(top>=n-1)
cout<<”Stack Overflow”<<endl;
else {
top++;
stack[top]=val;
void pop() {
if(top<=-1)
cout<<”Stack Underflow”<<endl;
else {
cout<<”The popped element is “<<stack[top]<<endl;
top--;
void display() {
if(top>=0) {
cout<<”stack elements are:”;
for(int i=top; i>=0;i--)
cout<<stack[i]<<” “;
cout<<endl;
} else
Cout<<”Stack is empty”;
int main() {
int ch,val;
cout<<”1) push in stack”<<endl;
cout<<”2)pop from stack”<<endl;
cout<<”3)Display stack “<<endl;
cout<<”4)Exit”<<endl;
do {
cout<<”Enter choice:”<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<”Enter value to be pushed:”<<endl;
cin>>val;
push(val);
break;
case 2: {
pop();
break;
Case 3: {
Display();
break;
}
case 4: {
cout<<”Exit”<<endl;
break;
default: {
cout<<”Invalid Choice”<<endl;
}while(ch!=4);
return 0;
};
OUTPUT:
1)Push in stack
2)Pop from stack
3)Display stack
4)Exit
Enter choice:1
Enter value to be pushed: 2
Enter choice:1
Enter value to be pushed: 6
Enter choice:1
Enter value to be pushed: 8
Enter choice:1
Enter value to be pushed: 7
Enter choice:2
The popped element is 7
Enter choice:3
Stack elements are:8 6 2
Enter choice:5
Invalid Choice
Enter choice:4
Exit
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:12(b)
POLYNOMIAL ADDITION USING LINKED LISTS
PROGRAM:
#include<iostream.h>
#include”stdio.h”
#include”conio.h”
Class poly
private:
Struct polynode
float coeff;
int exp;
polynode*link;
}*p;
public:
poly();
void poly_append(float c,int e);
void display_poly();
void poly_add(poly &11,Poly &12);
̴poly();
};
Poly :: poly()
p=NULL;
}
void poly :: poly_append(float c,int e)
polynode *temp = p;
//cout<<”p value =”<<p;
If ( temp == NULL)
temp = new polynode;
p= temp;
else
While ( temp -> link ! = NULL)
temp = temp -> link;
temp -> link = new polynode;
temp = temp->link;
temp -> coeff = c;
temp -> exp=e;
temp -> link = NULL ;
void poly :: display_poly( )
Polynode * temp = p;
int f = 0;
cout << endl;
while ( temp ! = NULL)
if ( f! =0)
if ( temp -> coeff>0)
cout <<”+”;
else
cout <<” “;
if ( temp ->exp ! = 0)
cout << temp -> coeff <<”xˆ”<<temp->exp;
else
cout<<temp->coeff;
temp = temp->link;
f = 1;
void poly :: poly_add( poly & 11,poly &12)
Polynode *z;
if ( 11.p == NULL && 12.p == NULL )
return;
polynode *temp 1,*temp 2;
temp 1=11.p;
temp 2= 12.p;
while(temp 1 ! =NULL && temp 2 ! = NULL )
if (p == NULL )
p = new polynode;
z = p;
else
z-> link = new polynode;
z = z -> link;
if ( temp 1 -> exp <temp 2 -> exp )
z -> coeff = temp 2 -> coeff;
z -> exp = temp 2 ->exp;
temp 2 = temp 2 -> link;
else
if ( temp 1 -> exp > temp 2 -> exp )
z -> coeff = temp 1 -> coeff = temp 2 -> coeff;
z -> exp = temp 1 -> exp ;
temp 1 = temp 1 -> link ;
temp 2 = temp 2 -> link ;
}
While ( temp 1 ! = NULL )
if ( p == NULL )
p = new polynode;
z=p;
else
z -> link = new polynode ;
z = z -> link ;
z -> coeff = temp 1 -> coeff ;
z -> exp = temp 2 -> exp ;
temp 2 = temp 2 -> link ;
z -> link = NULL;
Poly :: ̴̴ poly ( )
Polynode * q;
while ( p ! NULL )
q = p -> link ;
delete p;
p=q;
int main ( )
int a;
poly p1;
p1.poly_append ( 1.4 , 5 );
p1.poly_append (1.5 , 4 );
p1.poly_append ( 1.7, 2 );
p1.poly_append ( 1.8, 1 );
p1.poly_append ( 1.9, 0 );
cout << “\nFirst polyniomial:”;
p1.dislay_poly ( );
poly p2;
p2.poly_append ( 1.5, 6 ) ;
p2.poly_append ( 2.5, 5 );
p2.poly_append ( -3.5, 4 );
p2.poly_append ( 4.5,3 );
p2.poly_append ( 6.5, 1 );
cout << “\nSecond polynomial:”;
p2.display_poly ( );
poly p3;
p3.poly_add ( p1 , p2 ) ;
cout << “\nResultant polynomial : “;
p3.display-poly( );
getchar( );
return 0;
OUTPUT:
First polynomial
1.4x˄5 + 1.5x˄4 +1.7x˄2 + 1.8x˄1+ 1.9
Second polynomial:
1.5x˄6 + 2.5x˄5-3.5x˄4 + 4.5x˄3 + 6.5x˄1
Resultant polynomial:
1.5x˄6 + 3.9x˄5 – 2x˄4 + 4.5x˄3 + 1.7 x˄2 + 8.3x˄1 + 1.9
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:13
BINARY TREE TRAVERSALS
PROGRAM:
#include<iostream>
#include<stdlib.h>
//using namespace std;
struct st
int data;
struct st *left;
struct st *right;
};
struct st *root=NULL,*temp;
void insertElements();
viod preorder(struct st *);
viod inorder(struct st *);
viod postorder(struct st *);
int main()
int ch;
while(1)
cout<<”\n 1.insert Elements \n 2.preorder \n 3.inorder \n 4.postorder \n 5.exit”;
cout<<”\n enter ur choice”;
cin>>ch;
switch(ch);
case 1:insertElements();break;
case 2:perorder(root);break;
case 3:inorder(root);break;
case 4:postorder(root);break;
case 5:exit(1);break;
default:cout<<”invalid operation”;
viod insertElements()
struct st *nc,*pNode;
int v;
cout>>”\n enter the value”;
cin>>v;
temp=new st:
temp->data=v;
temp->left=NULL;
temp-right=NULL;
if(root==NULL)
root=temp;
else
nc=root;
while(nc!=NUUL)
{
pNode=nc;
if(v<nc->data)
nc=nc->left;
else
nc=nc->right;
if(v<pNode->data)
pNode->left=temp;
else
pNode->right=temp;
void preorder(struct st *temp)
if(temp!NULL)
cout<<””<<temp->data;
preorder(temp->left);
preorder(temp->right);
void inorder(struct st *temp)
{
if(temp!=NULL)
inorder(temp->left);
cout<<””<<temp->data;
inorder(temp->right);
viod postorder(struct st *temp)
if(temp!=NULL)
postorder(temp->left);
postorder(temp->right);
cout<<””<<temp->data;
OUTPUT:
1.insert Elements
2.preorder
3.inorder
4.postorder
5.exit
enter your choice 2
5 1 3 2 4
1.insert Elements
2.perorder
3.inorder
4.postorder
5.exit
enter your choice 3
1 2 3 4 5
1.insert Elements
2.preorder
3.inorder
4.postorder
5.exit
enter your choice 4
2 4 3 1 5
1.insert Elements
2.preorder
3.inorder
4.postorder
5.exit
enter your choice 5
exit.
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO: 14(a)
IMPLEMENTATION OF SEARCHING AND SORTING ALGORITHMS.
PROGRAM:
#include<iostream>
using namespace std;
int search(int arr[],int n,int x)
int i;
for(i=0;i<n;i++)
if(arr[i]==x)
return i;
return-1;
int arr[]={2,3,4,10,40};
int n=sizeof(arr)/sizeof(arr[0]);
int result=search(arr,n,x);
(result==-1) ? cout <<”Elements is not present in array”
:cout<<”Elements is present at index”<<result;
return 0;
OUTPUT:
Element is present at index
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:14(B)
IMPLEMENTATION OF BUBBLE SORT
PROGRAM:
#include<bits/stdc++.h>
using namespace std;
void swap(int *xp, int *yp)
int temp=*xp;
*xp=*yp;
*yp=temp;
void bubblesort(int arr[],int n)
int i,j;
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(arr[j]>arr[j+1])
swap(&arr[j],&arr[j+1]);
void printArray(int arr[],int size)
int I;
for(i=0;i<size;i++)
cout<<arr[i]<<””;
cout<<endl;
int main()
{
int arr[]={64,34,25,12,22,11,90};
int n=sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr,n);
cout<<”Sorted array:\n”;
printArray(arr,n);
return 0;
OUTPUT:
Sorted array:
11 12 22 25 34 64 90
RESULT:
Thus the program is executed successfully and the result has been verified.
EX.NO:15
DIJKSTRA’S SHORTEST PATH ALGORITHM
PROGRAM:
#include<limits.h>
#include<stdio.h>
int minDistance(int dist[],bool sptSet[])
int min=INT_MAX,min_index;
for(int v=0;v<V;v++)
if(sptSet[v]==false && dist[v]<=min)
min=dist[v],min_index=v;
return min_index;
void printSolution(int dist[])
Printf(“Vertex\t\t Distance from Source\n”);
for(int i=0;i<V;i++)
printf(“%d\t\t%d\n”,i,dist[i]);
void dijkstra(int graph[V][V],int src)
int dist[V];
bool sptSet[V];
for(int i=0;i<V;i++)
dist[i]=INT_MAX,sptSet[i]=false;
dist[src]=0;
for(int cout=0;cout<V-1;cout++) {
int u=minDistance(dist,sptSet);
sptSet[u]=true;
for(int v=0;v<V;v++)
if(!sptSet[v] && graph[u][v] && dist[u] !=INT_MAX
&& dist[u]+graph[u][v]<dist[v])
dist[v]=dist[u]+graph[u][v];
printSolution(dist);
int main()
int graph[V][V]={ { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 },
dijkstra(graph, 0);
return 0;
}
OUTPUT:
Vertex Distance from Source
0 0
1 4
2 12
3 19
4 21
5 11
6 9
7 8
8 14
RESULT:
Thus the program is executed successfully and the result has been verified.