You are on page 1of 30

PRACTICAL-4

O.] Write a c++ program for adding two distances using objects as argument in classes.

#include<math.h>
#include<iostream.h>
using namespace std;
class Distance
{
float feet , inch;
public: Distance()
{
feet=0.0;
inch=0.0;
}
void read_dist();
void disp_dist();
void add(Distance , Distance);
};
void Distance::read_dist()
{
cout<<“Enter distance(feet and inches):”;

cin>>feet>>inch;
}
void Distance::disp_dist()
{
cout<<“Distance Feet:”<<feet<<“, Inches:”<<inch<<“\n”;
}
void Distance::add(Distance x,Distance y)
{
inch=x.inch+y.inch;
feet=x.feet+y.feet;
if(inch>=12.0)
{
feet=x.feet+y.feet+(inch/12.0);
inch=(int)inch%12;
}
}
main()
{
Distance d1,d2,d3;
cout<<“Enter first measure:\n”;
d1.read_dist();
cout<<“Enter second measure:\n”;
d2.read_dist();
d3.add(d1,d2);
d3.disp_dist();
}
OUTPUT:-
Enter first measure:
Enter distance(feet and inches): 25 36
Enter second measure:
Enter distance(feet and inches): 25 14
Distance Feet:54, Inches:20
PRACTICAL-13
Q.] Write c++ array based program for merging two arrays elements .

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr1[50], arr2[50], size1, size2, size, i, j, k,
merge[100];
cout<<"Enter Array 1 Size : ";
cin>>size1;
cout<<"Enter Array 1 Elements : ";
for(i=0; i<size1; i++)
{
cin>>arr1[i];
}
cout<<"Enter Array 2 Size : ";
cin>>size2;
cout<<"Enter Array 2 Elements : ";
for(i=0; i<size2; i++)
{
cin>>arr2[i];
}
for(i=0; i<size1; i++)
{
merge[i]=arr1[i];
}
size=size1+size2;
for(i=0, k=size1; k<size && i<size2; i++, k++)
{
merge[k]=arr2[i];
}
cout<<"Now the new array after merging is :\n";
for(i=0; i<size; i++)
{
cout<<merge[i]<<" ";
}
getch();
}

OUTPUT:-
Enter Array 1 size : 5
Enter Array 1 element : 1 2 3 4 5
Enter Array 2 size : 6
Enter Array 2 element : 9 8 7 6 5 4
New Array after merging is : 1 2 3 4 5 9 8 7 6 5 4
PRACTICAL:-15
Q.] program to multiply two Array of elements (Matrix multiplication) using 2- dimensional array .

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mat1[3][3], mat2[3][3], mat3[3][3], sum=0,
int i,j,k
cout<<"Enter first matrix element (3*3) : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>mat1[i][j];
}
}
cout<<"Enter second matrix element (3*3) : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>mat2[i][j];
}
}
cout<<"Multiplying two matrices...\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
sum=0;
for(k=0; k<3; k++)
{
sum = sum + mat1[i][k] * mat2[k]
[j];
}
mat3[i][j] = sum;
}
}
cout<<"\nMultiplication of two Matrices : \n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout<<mat3[i][j]<<" ";
}
cout<<"\n";
}
getch();
}

OUTPUT:-
Enter first matrix element (3*3) : 1 2 3
12 3
123
Enter second matrix element (3*3) : 1 2 3
123
123
multiplying two matrix…!!!
Multiplication of two matrix :
6 12 18
6 12 18
6 12 18
PRACTICAL :-14
Q.] Write a c++ program for Array – insertion and deletion of elements .

INSERTION

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], size, insert, i, pos;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Enter element to be insert : ";
cin>>insert;
cout<<"At which position (Enter index number) ? ";
cin>>pos;
for(i=size; i>pos; i--)
{
arr[i]=arr[i-1];
}
arr[pos]=insert;
cout<<"Element inserted successfully..!!\n";
cout<<"Now the new array is : \n";
for(i=0; i<size+1; i++)
{
cout<<arr[i]<<" ";
}
getch();
}

OUTPUT:-
Enter Array size : 10
Enter Array elements : 1 2 3 5 6 7 8 9 10 11
Enter the element to be inserted : 4
element inserted successfully…!!Now the new Array is : 1 2 3 4 5 6 7 9 8 10 11
DELETION

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], size, i, del, count=0;
cout<<"Enter array size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Enter element to be delete : ";
cin>>del;
for(i=0; i<size; i++)
{
if(arr[i]==del)
{
for(int j=i; j<(size-1); j++)
{
arr[j]=arr[j+1];
}
count++;
break;
}
}
if(count==0)
{
cout<<"Element not found..!!";
}
else
{
cout<<"Element deleted successfully..!!\n";
cout<<"Now the new array is :\n";
for(i=0; i<(size-1); i++)
{
cout<<arr[i]<<" ";
}
}
getch();
}
OUTPUT:-
Enter Array size : 10
Enter Array element : 1 2 3 4 5 6 7 8 9 10
Enter the element to be deleted : 6
Element is deleted successfully…!!!
now the new Array is: 1 2 3 4 5 7 8 9 10

PRACTICAL:-16
Q.} program for stack-Array implementation .
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};
 main()
{int ch;
stack st;
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}

output:-
1.push 2.pop 3.display 4.exit
Enter your choice: 2
stack under flow
1.push 2.pop 3.display 4.exit
Enter your choice :1
enter the element : 2
inserted 2
1.push 2.pop 3.display 4.exit
Enter your choice : 1
enter the element : 3
inserted 3

practical-11
q.] Write a menu driven program which includes the binary and linear search.
#include<iostream.h>
#include<stdio.h>
int lsearch(int a[20] , int key , int n);
int bsearch(int a[20] , int key , int n);
int main()
{ int a[20] , k ,n , ch , ans;
cout<<”enter the tital number of elements\n”;
cin>>n;
while(1){
cout<<”\nmenu|n”;
cout<<”1.linear search\n”;
cout<<2.binary search\n”;
cout<<3.exit\n”;
cout<<”enter choice\n”;
cin>>ch;
switch(ch)
{ case 1 : cout<<”enter the elements\n”;
for(int i=0;i<n;i++)
{ cin>>a[i]; }
cout<<”enter key elements\n”;
cin>>k;
ans=lsearch(a,k,n);
if(ans!=-1)
cout<<”element\t”<<k<<”\t isnot fount\n”;
else
cout<<”element”<<k<<:\t is found\n”;

break;
case 2 : cout<<”enter the elements\n”;
for(int i=0;i<n;i++)
{ cin>>a][i]; }
cout<<”enter the key elements\n”;
cin>>k;
ans=bsearch(a,k,n);
if(ans!=-1)
cout<<”element”<<k<<”\t is found \n”;
else
cout<<”elements\t”<<k<<”\t not found\n”;
break;
case 3 : exit(0);
default : cout<<”invalid entry \n”;
}}
}
int lsearch(int a[20] , int key , int n)
{ int k;
for(int i=0; i<n;i++)
{ if (key==a[i])
return(i);
else
return(-1);
}
intbsearch(int a[20] , int key , int n)
{ int high, low mid;
low=0;
high=n-1;
while(low<=high)
{ mid =(low+high)/2;
if(a[mid}==key)
return(mid);
elseif(a[mid]<key)
low=mid-1;
else
high=mid-1;
}
return(-1);
}

OUTPUT

enter the total number of elements


5
menu
1.linear search
2.binary search
enter choices
1
enter the element
15674
enter the key element
7
element 7 is found

practical-8
Q.] Write a program to read contents from a file and separate digit, alphabet and
special character into three different files.
#include<iostream.h>
#include<fstream.h>
#include<ctype.h>
void main()
{
ifstream fin(“data.txt”);
ofstream fout(“digit.txt”);
ofstream fout1(“alpha.txt”);
ofstream fout2(“special.txt”);
char ch;
while(fin)
{
fin>>ch;
if(is digit(ch));
fout<<”digits are”<<ch;
else if(is alpha(ch));
fout1<<”alphabets are”<<ch;
else
fout2<<”special chars are”<<ch;
}
fin.close():
fout.close():
fout1.close();
fout2.close();
}

output;-
file content :- geeks123hut the @#
123
geekshutthe
@#

PRACTICAL-12
Q.] Write a menu driven program for different sorting techniques.
#include<iostream.h>
void accept(int Arr[ ] , int s);
void display(int Arr[ ] , int s);
void isort(int Arr[ ] , int s);
void ssort(int Arr[ ] , int s);
void bsort(int Arr [ ] , int s);
int main()
{ int Arr[ ] , n , choice;
cout<<”enter size of array”;
cin>>n;
do
{ cout<<”\n\nMENU”;
cout<<”\n`1. accept element s of array”;
cout<<”\n2. display elements of arrays”;
cout<<”\n 3.insertion sort”;
cout<< “\n4.selection sort”;
cout<<”\n5.bubble sort”;
cout<<”\n.exit”;
cout<<”\nenter choices 1-6:”;
cin>>choice;
switch(choice)
{
case 1 : accept(Arr , n);
break;
case 2 : display(Arr,n);
break;
case 3 : isort(Arr , n);
break;
case 4 : ssort(Arr , n);
break;
case 5 : bsort(Arr , n);
break;
case 6 : break;
default : cout<<”\nInvalid choice”;
}while(choice!=6);
return(0);
}
void accept(int Arr[ ] , int s)
{ for(int i=0;i<=s;i++)
{ cin>>Arr[i]; }
}
void display(int Arr[ ] , int s)
{
cout<<”the elements of thearray are:\n”;
for(int i=0;i<s;i++)
cout<<Arr[i]<<” “;
}
void isort(int Arr[ ] , int s)
{ int temp , I ;
Arr[0]=INT_MIN;
for(int i=0;i<s;i++)
{ temp=Arr[i];
j=i-1;
while(temp<Arr[j])
{ Arr{j+1]=Arr[j];
j--;
}
Arr[j+1]=temp ;
}
}
void ssort(int Arr[ ] , int s)
{
int small , pos , temp;
for(int i=0; i<s; i++)
{ small=Arr[i];
pos=I ;
for(int j=i+1 ; j<s ; j++)
{ if (Arr[j]<small
{ small=Arr[j]; pos= j; }
]
temp=Arr[i];
Arr[i]=Arr[pos];
Arr[pos]=temp;
}
void bsort( int Arr [ ] , int s)
{ int temp , ctr=0;
for(int i=0 ; i<s ;i++)
for(int j=0 ; j<s-i-1; j++)
{ if(Arr[i]>Arr[j+1])
{ temp=Arr[j];
Arr[j]= Arr[j+1];
Arr[j+1]=temp;
} }}

PRACTICAL-9
Q.]Write the following program:
(A) display the number of “this” and “these” word in a text file.
(B) count the number of words having 5 char in a text file .

A]
#include<iostream.>
#include<fstream.h>
void disp()
{
int count1=0,count2=0;
ifstream fin(“data.txt” , ios :: in);
char word[80];
while(fin)
{
fin>>word;
if (strcmp(word,”this”)==0)
count1++;
else if(strcmp(word,”these”)==0)
count2++;
}
cout<<”number of this present are”<<cont1;
cout<<”number of these present are”<<cont2;
fin.close();
}

B]
#include<iostream.h>
#include<fstream.h>
void count()
{ ifstream fin(“red.txt” , ios :: in);
char word[80]; int l;
int count=0;
while(fin)
{ fin>>word;
l=strlen(word);
if( l==5)
{count++; }
)
cout<<”\n no of word:”<<”\n”;
fin.close();
}

OUTPut:-

file contents :- this is a great opportunity to everyone to be a part of party, I saw


various complex answers
and this is the reason. this is a big party for those who have these passes.
A]
number of this present are: 3
number of these present are :1

B]
no of word : 5

PRACTICAL-6
Q.] write a program to show functioning for multiple inheritance.

#include<iostream.>
#include<stlib.h>
class base1
{ protected :
int a;
public :
base 1(int x)
{ a=x;
cout<<”constructing base 1\n”;
~base1()
{ cout<<”destruting base 1\n”; }
};
class base 2
{ protected :
int b;
public :
base 2(int y)
{ b=y; cout<<”consteructing base 2\n”;
~base 2()
{ cout<<”destructing base 2\n”; }
};
class derived: public base 2 , public base 1
{ int c ;
public :
derived (int i , int j , int k ) : base 2(i) , base 1(j)
{ c=k;
cout<<”constructing derived \n”; };
~derived( )
{cout<<”destructing derived \n”; }
void show()
{ cout<<”1.”<<a<<”\t 2.”<<b<<”\t.3”<<c<”\n”; }
};
int main()
{ derived ob(14 , 15 , 16);
ob.show();
return 0;
}

output
constructing base 2
constructing base 1
constructing derived
1.a 14 2.b 15 3.c 16

PRACTICAL-10
Q.] Write a function in c++ to search and display the details of all flight whose desrination is “Mumbai”
from a binary file “flght.dat”. assuming the binary file is containing objets of class.

class FLIGHT
{
int fno;
char from[20];
char to[20];
public:
char* GetRemake() { return from; }
char* GetTo()
{ cin>>fno; gets(from); gets(to); }
void display()
{ cout<<fno<<”:”<<from<<”:”<<to<<endl; }
};

void read()
{ FLIGHT f;
ifstream fin(“FLIGHT.DAT”,ios::in|ios::binary);
while(fin)
{ (fin.read(char*)&f,sizeof(f));
if(strcmp(f.GetTo(),”Mumbai”)==0)
f.display();
}
fin.close();
}

OUTPUT:-

PRACTICAl-17
Q.] Write a function definition insert to insert a new node in the stack .

#include<iostream>
using namespace std;

// Creating a NODE Structure


struct node
{
int data;
struct node *next;
};

// Creating a class STACK


class stack
{
struct node *top;
public:
stack() // constructor
{
top=NULL;
}
void push(); // to insert an element
void pop(); // to delete an element
void show(); // to show the stack
};
// PUSH Operation
void stack::push()
{
int value;
struct node *ptr;
cout<<"\nPUSH Operationn";
cout<<"Enter a number to insert: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";

// POP Operation
void stack::pop()
{
struct node *temp;
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
}
temp=top;
top=top->next;
cout<<"\nPOP Operation........\nPoped value is "<<temp->data;
delete temp;
}

// Show stack
void stack::show()
{
struct node *ptr1=top;
cout<<"\nThe stack is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL\n";
}

// Main function
int main()
{
stack s;
int choice;
while(1)
{

cout<<"\n---------------------------------------------------------
--";
cout<<"\n\t\tSTACK USING LINKED LIST\n\n";
cout<<"1:PUSH\n2:POP\n3:DISPLAY STACK\n4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:
return 0;
break;
default:
cout<<"\nPlease enter correct choice(1-4)!!";
break;
}
}
return 0;
}

 OUTPUT:

-----------------------------------------------------------
STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 1
PUSH Operation
Enter a number to insert: 12

New item is inserted to the stack!!!


-----------------------------------------------------------
STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 1

PUSH Operation
Enter a number to insert: 5

New item is inserted to the stack!!!


-----------------------------------------------------------
STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 2

POP Operation........
Poped value is 5
-----------------------------------------------------------
STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 3

The stack is
12 ->NULL

-----------------------------------------------------------
STACK USING LINKED LIST

1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 4

practical-18
Q.) write a function definition for queue array implementation.

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
 
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;
return;
}
queue1[++rear]=x;
cout <<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted" <<queue1[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<queue1[i]<<" ";
}
};
 
main()
{
int ch;
queue qu;
while(1)
{
cout <<"\n1.insert 2.delet 3.display
4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
qu.insert(ch);
break;
case 2: qu.delet(); break;
case 3: qu.display();break;
case 4: exit(0);
}
}
return (0);
}

output:

practical-19
Q.] Write a program for Queue using linked list implementation.

#include<iostream>
using namespace std;

struct node
{
int data;
node *next;
}*front = NULL,*rear = NULL,*p = NULL,*np = NULL;

void push(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if(front == NULL)
{
front = rear = np;
rear->next = NULL;
}
else
{
rear->next = np;
rear = np;
rear->next = NULL;
}
}
int remove()
{
int x;
if(front == NULL)
{
cout<<"empty queuen";
}
else
{
p = front;
x = p->data;
front = front->next;
delete(p);
return(x);
}
}
int main()
{
int n,c = 0,x;
cout<<"Enter the number of values to be pushed into queuen";
cin>>n;
while (c < n)
{
cout<<"Enter the value to be entered into queuen";
cin>>x;
push(x);
c++;
}
cout<<"nnRemoved Valuesnn";
while(true)
{
if (front != NULL)
cout<<remove()<<endl;
else
break;
}
return 0;
}
 

OUTPUT:
Enter the number of values to be pushed into queue
5
Enter the value to be entered into queue
5
Enter the value to be entered into queue
3
Enter the value to be entered into queue
2
Enter the value to be entered into queue
9
Enter the value to be entered into queue
1

Removed Values

5
3
2
9
1

--------------------------------

practical-20
Q.] Write a program for Circular queue.

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class cqueue
{
int q[5],front,rare;
public:
cqueue()
{
front=-1;
rare=-1;
}
void push(int x)
{
if(front ==-1 && rare == -1)
{
q[++rare]=x;
front=rare;
return;
}
else if(front ==(rare+1)%5 )
{
cout <<" Circular Queue over flow";
return;
}
rare= (rare+1)%5;
q[rare]=x;
}
 
void pop()
{
if(front==-1 && rare==-1)
{
cout <<"under flow";
return;
}
else if( front== rare )
{
front=rare=-1;
return;
}
front= (front+1)%5;
}
 
 
 
 
void display()
{
int i;
if( front <= rare)
{
for(i=front; i<=rare;i++)
cout << q[i]<<" ";
}
else
{
for(i=front;i<=4;i++)
{
cout <<q[i] << " ";
}
for(i=0;i<=rare;i++)
{
cout << q[i]<< " ";
}
}
}
};
 
main()
{
 
int ch;
cqueue q1;
while( 1)
{
cout<<"\n1.INSERT 2.DELETE 3.DISPLAY 4.EXIT\nEnter ur
choice";
cin >> ch;
switch(ch)
{
case 1: cout<<"enter element";
cin >> ch;
q1.push(ch); break;
 
case 2: q1.pop(); break;
case 3: q1.display(); break;
case 4: exit(0);
}
}
}

OUTPUT:
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice 1
enter element 4
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice 1
enter element 5
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice1
enter element 3
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice 3
453
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice2
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice3
53
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice4

You might also like