Q1.
WAP to show concept of polymorphism to calculate the area of
circle, triangle, square.
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
float area(float a, float b, float c)
{ float s,ar;
s=(a+b+c)/2;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
return ar; }
float area(float a, float b);
{ return a*b; }
float area(float a);
{ return a*a; }
int main()
{ system(“ cls “);
int choice,s1,s2,s3,ar;
do
{ cout<<”\nArea Main Menu\n”;
cout<<”1. Triangle\n”;
cout<<”2. Square\n”;
cout<<”3. Rectangle\n”;
cout<<”4. Exit \n”;
cout<<” Enter your choice(1-4):”;
cin>>choice;
cout<<”\n”;
switch(choice) {
case 1 : cout<<” Enter 3 sides\n”;
cin>>s1>>s2>>s3;
ar=area(s1,s2,s3);
cout<<” The area is” << ar << “\n”;
break;
case 2 :cout<<” Enter a side \n”;
cin>>s1;
ar=area(s1);
cout<<” The area is”<<ar<<”\n”;
break;
case 3 :cout<<” Enter length & breath \n”;
cin>>s1>>s2;
ar=area(s1,s2);
cout<<” The area is”<< ar<<”\n”;
break;
case 4 : break;
default :cout<<” Wrong Choice!!\n”; }; //end of switch
} while(choice > 0 && choice < 4);
return 0;
}
Q2. WAP to show a function with default arguments
#include<iostream.h>
#include<conio.h>
void display (char = '*', int = 1);
int main()
{ clrscr();
cout<<" No arguments passed: \n";
display();
cout<<"\n First argument passed: \n";
display('#');
cout<< "\n Both argument passed: \n";
display('$',5);
return 0; }
void display(char c, int n)
{ for(int i=1; i<=n; ++i)
{ cout<<c; }
cout<<endl;
getch();
}
Q3. WAP to invoke a function that takes array as an argument and
reverse the elements of the array.
#include<iostream.h>
#include<conio.h>
void reversearray(int arr[], int start,int end)
while (start<end)
int temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
void printarray (int arr[], int size)
for (int i=0;i<size;i++)
cout<<arr[i]<<" ";
cout<<endl;
void main()
int arr[]={1,2,3,4,5,6};
clrscr();
printarray(arr,6);
reversearray(arr,0,5);
cout<<"Reversed array is" << endl;
printarray(arr,6);
getch();
Q4. WAP to merge two arrays in ascending order of array1 is in
descending and array2 is in ascending order using function.
#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();
}
Q5. WAP to display row sum and column sum of a 2D array passed as an
argument.
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
static int array[10][10];
int i,j,m,n,sum=0;
cout<<"enter the order of the matrix\n";
cin>>m>>n;
cout<<"enter the co-efficients of the matrix\n";
for(i=0;i<m;++i)
{ for(j=0;j<n;++j)
{ cin>>array[i][j]; } }
for(i=0;i<m;++i)
{ for(j=0;j<n;++j)
{ sum=sum+array[i][j]; }
cout<<"\nsum of the"<<i<<"row is ="<<sum;
sum=0; }
sum=0;
for(j=0;j<n;++j)
{ for(i=0;i<m;++i)
{ sum=sum+array[i][j]; }
cout<<"\nsum of the"<<j<<"column is =\n"<<sum;
sum=0; }
getch();
}
Q6. Define a class STOCK in C++ with following descriptions:
Private members:
Icode int type (item code)
Item string type (item name)
Price float type (price of each item)
Qty int type(quantity in stock)
Discount float type(discount% on item)
FindDisc() to calculate discount as per following rule:
If Qty<=50 Discount =0
If 50<Qty<=100 Discount =5
If Qty>100 Discount=10
Public members:
Buy() – allows user to enter values for Icode, Item, Price, Qty and call
function FindDisc() to calculate the discount.
ShowAll() – allows user to view the content of all data members.
class STOCK {
int ICode;
char Item[20];
float Price;
int Qty;
float Discount;
void FindDisc() {
if(Qty<=50)
Discount=0;
else if(Qty<=100)
Discount=5;
else
Discount=10; }
public:
void Buy()
{ cout<<"Enter item code:";
cin>>ICode;
cout<<"Enter item name:";
gets(Item);
cout<<"Enter price of each item:";
cin>>Price;
cout<<"Enter quantity of item in stock:";
cin>>Qty;
FindDisc(); }
void ShowAll() {
cout<<"Code:"<<ICode<<"\nName:"<<Item<<"\nPrice:"<<Price<<"\nQuantity:"<<Qty<<"\nDis
count percentage:"<<Discount; } };
Q7. Define a class TRAVELPLAN in C++
Private members:
Plancode long type
Place string type
No_of_traveller int type
No_of_bus int type
Public members:
--Constructor to assign initial values of Plancode as 1001, Place as
“Agra”, No_of_traveller as 5, No_of_bus as 1.
--NewPlan() allows user to enter Plancode, Place, No_of_traveller. Also
assigns number of bus as
Number of traveller Number of bus
<20 1
>=20 & <40 2
>=40 3
-- ShowPlan() to display content of all data members on screen.
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
class TRAVELPLAN {
long plancode;
char place[20];
int no_of_traveller, no_of_bus;
int buscalc();
public:
TRAVELPLAN()
{ plancode=1001;
strcpy (place,"agra");
no_of_traveller=5, no_of_bus=1; }
void newplan();
void showplan(); };
void TRAVELPLAN::newplan()
{ cout<<"\nEnter plancode:";
cin>>plancode;
cout<<"\nEnter place:";
gets(place);
cout<<"\nEnter no. of travellers:";
cin>>no_of_traveller;
no_of_bus=buscalc(); }
int TRAVELPLAN ::buscalc()
{ if(no_of_traveller<20)
no_of_bus=1;
else if(no_of_traveller>=20 && no_of_traveller<40)
no_of_bus=2;
else if(no_of_traveller>=40);
no_of_bus=3;
return no_of_bus; }
void TRAVELPLAN::showplan()
{ cout<<"\nPlancode:"<<plancode;
cout<<"\nPlace:";
puts(place);
cout<<"\nNo. of travellers:"<<no_of_traveller;
cout<<"\nNo. of bus:"<<no_of_bus; }
void main ()
{ TRAVELPLAN T;
int ch;
clrscr();
do {
cout<<"\nEnter your choice(1-3):";
cout<<"\n1.Enter a new plan \n2.Show plans \n3. Exit\n";
cin>>ch;
switch(ch) {
case 1 : T.newplan(); break;
case 2 : T.showplan(); break;
case 3 : exit(0);
default : cout<<"\nWrong choice";
} } while(ch!=3);
getch();
}
Q8. WAP to show multilevel inheritance.
#include<conio.h>
#include<iostream.h>
class person
{ protected:
int age;
char *name;
public:
void get1(); };
class emp : public person
{ protected:
int basic,hra;
public:
void get2(); };
class manager : public emp
{ protected:
int deptcode;
public:
void get3();
void display(); };
void person :: get1()
{ cout<<"enter your age\n";
cin>>age;
cout<<"enter your name\n";
cin>>name; }
void emp :: get2()
{ cout<<"enter your basic and HRA\n";
cin>>basic>>hra; }
void manager :: get3()
{ cout<<"enter your deptcode\n";
cin>>deptcode; }
void manager :: display()
{ cout<<"name is "<<name;
cout<<"\nage is "<<age;
cout<<"\nbasic and HRA"<<basic<<"\t"<<hra;
cout<<"\ndeptcode is "<<deptcode; }
void main()
{ clrscr();
manager obj;
obj.get1();
obj.get2();
obj.get3();
obj.display();
getch();
}
Q9. Write a function to count no. of “he” or “she” words present in a
text file “story.txt”.
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<process.h> // for exit()
int count()
{ ifstream ifile("story.txt");
if(!ifile)
{ cout<<"could not open story.txt file";
exit(-1); }
else
{ char s[20];
int cnt=0;
while(!ifile.eof())
{ ifile>>s;
if((strcmp(s,"He"))==0||(strcmp(s,"She")==0))
cnt++; }
ifile.close();
cout<<"count of he/she in file is :"<<cnt;
return cnt; } }
Q10. WAP to search in file having records maintain through class in
binary file.
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
class student {
int rollno;
char name[20];
char branch[3];
float marks;
char grade;
public:
void getdata() {
cout<<"rollno: ";
cin>>rollno;
cout<<"name: ";
cin>>name;
cout<<"branch: ";
cin>>branch;
cout<<"marks: ";
cin>>marks;
if(marks>=75)
{ grade='A'; }
else if(marks>=60)
{ grade='B'; }
else if(marks>=50)
{ grade='C'; }
else if(marks>=40)
{ grade='D'; }
else
{ grade='F'; } }
void putdata()
{ cout<<"Rollno: "<<rollno<<"\t name: "<<name<<"\n";
cout<<"marks: "<<marks<<"\tGrade: "<<grade<<"\n"; }
int getrno()
{ return rollno; } } stud1;
void main()
{ clrscr();
fstream fio("marks.dat",ios::in | ios::out);
char ans='y';
while(ans=='y' || ans=='Y')
{ stud1.getdata();
fio.write((char *)&stud1, sizeof(stud1));
cout<<"Record added to the file\n";
cout<<"\nWhat to enter more ? (y/n)..";
cin>>ans; }
clrscr();
int rno;
long pos;
char found='f';
cout<<"enter rollno of student to be searched :";
cin>>rno;
fio.seekg(0);
while(!fio.eof())
{ pos=fio.tellg();
fio.read((char *)&stud1,sizeof(stud1));
if(stud1.getrno()==rno)
{ stud1.putdata();
fio.seekg(pos);
found='t';
break; } }
if(found=='f')
{ cout<<"\nrecord not found in the file..!!\n";
cout<<"press any key to exit...\n";
getch();
exit(2); }
fio.close();
getch();
}
Q11. WAP to modify data in a given binary file using class.
#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<process.h>
class file
{ private:
int roll;
float age;
char name[100];
public:
void input();
void show();
char *getn()
{ return name; }
};file fileobj;
void file::input()
{ cout<<"enter the roll no,age and name:";
cin>>roll>>age;
gets(name); }
void file::show()
{ cout<<"Roll==>"<<roll<<endl;
cout<<"Age==>"<<age<<endl;
cout<<"Name==>"<<name<<endl; }
void modify();
fstream fil;
int main()
{ int opt;
while(1)
{ clrscr();
modify();
cout<<"display main menu"<<endl;
getch();
break; }
{ char n[100];
cout<<"enter name that should be searched:";
gets(n);
fil.open("binary.dat",ios::in| ios::out|ios::binary);
if(!fil)
{ cout<<"file not found";
exit(0); }
else
{ filread((char*)&fileobj,sizeof(fileobj));
while(!fil.eof())
{ if(strcmp(n,fileobj.getn())==0)
{ fil.seekg(0,ios::cur);
cout<<"enter new record.."<<endl;
fileobj.input();
fil.seekp(fil.tellg()
sizeof(fileobj));
fil.write((char*)&fileobj,
sizeof(fileobj)); }
else
{ cout<<"press any key....for search"<<endl;
getch(); }
fil.read((char*)&fileobj;
sizeof(fileobj)); } }
fil.close();
}
Q12. WAP to show binary search.
#include<conio.h>
#include<iostream.h>
int bsearch(int [],int,int);
void main()
{ clrscr();
int ar[50],item,n,index;
cout<<"enter desired array size(max. 50)...";
cin>>n;
cout<<"\n enter array elements(must be sorted in asc order)\n";
for(int i=0;i<n;i++)
cin>>ar[i];
cout<<"\nenter element to be searched for...";
cin>>item;
index=bsearch(ar,n,item);
if(index==-1)
cout<<"\nsorry!! given element could not be found.\n";
else
cout<<"\nelement found at index:"<<index<<",position:"<<index+1<<endl;
getch(); }
int bsearch(int ar[],int size,int item)
{ int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{ mid=(beg+last)/2;
if(item==ar[mid])
return mid;
else if(item>ar[mid])
beg=mid+1;
else
last=mid-1; }
return -1;
Q13. WAP to show deletion in an array.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[100],i,pos,num,item,size;
cout<<"how many elements";
cin>>size;
cout<<"enter the elements of an array";
for(i=0;i<size;i++)
cin>>a[i];
cout<<"now array is"<<endl;
for(i=0;i<size;i++)
cout<<"element at position"<<i+1<<"is"<<a[i]<<endl;
cout<<"enter position number to delete";
cin>>pos;
--pos;
item=a[pos];
for(i=pos;i<size;i++)
a[i]=a[i+1];
size--;
cout<<"deleted element "<<item<<endl;
cout<<"new array is"<<endl;
for(i=0;i<size;i++)
cout<<"element at position "<<i+1<<" is "<<a[i]<<endl;
getch();
Q14. WAP to show bubble sort.
#include<iostream.h>
#include<conio.h>
void bubblesort(int [],int);
void main()
{ clrscr();
int ar[50],item,n,index;
cout<<"how many elements do u want to create array with?(max. 50)..";
cin>>n;
cout<<"\nenter array elements...\n";
for(int i=0;i<n;i++)
{ cin>>ar[i]; }
bubblesort(ar,n);
cout<<"\n\nThe sorted array is as shown below...\n";
for(i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<endl;
getch();
void bubblesort(int ar[],int size)
{ int tmp,ctr=0;
for(int i=0;i<size;i++)
{ for(int j=0;j<(size-1)-i;j++)
{ if(ar[j]>ar[j+1])
{ tmp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=tmp; } }
cout<<"Array after iteration-"<<++ctr<<"-is:";
for(int k=0;k<size;k++)
cout<<ar[k]<<" ";
cout<<endl; } }
Q15. WAP to display transpose of matrix, using function that takes
matrix as an input.
#include<iostream.h>
#include<conio.h>
#define row 3
#define col 4
void main()
{ clrscr();
int mat1[row][col],mat2[row][col],i,j;
cout<<"enter element of matrix";
cout<<"row-wise"<<endl;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cin>>mat1[i][j];
for(i=0;i<col;i++)
for(j=0;j<row;j++)
mat2[i][j]=mat1[j][i];
cout<<"transpose of matrix is\n";
for(i=0;i<col;i++)
{ for(j=0;j<row;j++)
{ cout<<mat2[i][j]<<" "; }
cout<<endl; }
getch();
}
Q16. WAP to swap values of two integer variables by using passing
pointers.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr();
int x, y,*a,*b,temp;
cout<<"\nEnter two integers:";
cin>>x>>y;
a=&x;
b=&y;
temp=*b;
*b=*a;
*a=temp;
cout<<"\nAfter swapping:";
cout<<x<<"\n"<<y;
getch();
}
Q17. WAP to PUSH & POP using array stack using switch case.
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define size 4
#include<process.h>
class stack {
int data[size];
int top;
public:
stack()
{ top=-1; }
void push();
void pop();
void display(); };
void stack::push()
{ if(top==size-1)
{ cout<<"\nStack is full!";
return; }
else
{ top++;
cout<<"\nEnter data:" ;
cin>>data[top]; } }
void stack::pop()
{ if(top==-1)
cout<<"\nStack is empty";
else
{ cout<<data[top]<<" deleted"<<endl;
top--; } }
void stack::display()
{ int t=top;
if(t<0)
cout<<"\nNo element in stack";
while(t>=0)
{ cout<<data[t]<<endl;
t--; } }
void main()
{ clrscr();
stack st;
int ch;
do
{ cout<<"\n1. push";
cout<<"\n2. pop";
cout<<"\n3. display";
cout<<"\n4. exit";
cout<<"\nEnte your choice:";
cin>>ch;
switch(ch)
{ case '1' : st.push();
break;
case '2' : st.pop();
break;
case '3' : st.display();
break;
case '4' : exit(0);
default : cout<<"\nWrong input";
} }while(ch!=4);
getch();
}
Q18. WAP to PUSH & POP from linked stack.
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<process.h>
struct node
{ int data;
node *next; };
class stack
{ node *top;
public:
stack()
{ top=NULL; }
void push();
void pop();
void display();
~stack(); };
void stack :: push()
{ node *temp=new node;
cout<<"\nEnter data:";
cin>>temp->data;
temp->next=NULL;
if(top==NULL)
top=temp;
else
{ temp->next=top;
top=temp; } }
void stack :: pop()
{ if (top!=NULL)
{ node *temp=top;
top=top->next;
cout<<temp->data<<" deleted";
delete temp; }
else
{ cout<<"\nStack is empty"; } }
void stack :: display()
{ node *temp=top;
if(temp==NULL)
{ cout<<"\nNo element in stack"; }
while(temp!=NULL)
{ cout<<temp->data<<"\n";
temp=temp->next; } }
stack :: ~stack()
{ while(top!=NULL)
{ node *temp=top;
top=top->next;
delete temp; } }
void main()
{ stack st;
char ch;
clrscr();
do
{ cout<<"\nStack option \nP for push \nO for pop \n D for display";
cout<<"\nQ for quit \nEnter your choice:";
cin>>ch;
switch(ch)
{ case 'P' : st.push();
break;
case 'O' : st.pop();
break;
case 'D' : st.display();
break;
case 'Q' : exit(0);
default : cout<<"\nWrong choice"; }
} while(ch!='Q');
getch();
}
Q19. WAP to insert and delete from array queue.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define size 4
class queue {
int data[size];
int front, rear;
public :
queue()
{ front=-1;
rear=-1; }
void insert();
void Delete();
void display(); };
void queue::insert()
{ if(rear==size-1)
{ cout<<"\nQueue overflow";
return; }
if(front==-1)
{ front++; }
rear++;
cout<<"\nEnter data:";
cin>>data[rear]; }
void queue::Delete()
{ if(front==-1)
{ cout<<"\nQueue underflow";
return; }
cout<<"\n"<<data[front]<<" is deleted";
if(front==rear)
{ front=-1;
rear=-1; }
else
front++; }
void queue::display()
{ int temp;
if(front==-1)
{ cout<<"\nQueue is empty";
return; }
temp=front;
while(temp<=rear)
{ cout<<data[temp++]<<" "; } }
void main()
{ queue Q;
int ch;
clrscr();
do
{ cout<<"\nEnter your choice(1-4):";
cout<<"\n1. Insert \n2.Delete \n3.Display \n4.Exit";
cin>>ch;
switch(ch)
{ case 1 : Q.insert(); break;
case 2 : Q.Delete(); break;
case 3 : Q.display(); break;
case 4 : exit(0);
default : cout<<"\nWrong choice";
} } while(ch!=4);
getch();
}
Q20. WAP to insert and delete from linked queue.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{ int data;
node *next; };
class queue
{ node *rear, *front;
public:
queue()
{ rear=NULL;
front=NULL; }
void qinsert();
void qdisplay();
void qdelete();
~queue(); };
void queue::qinsert()
{ node *temp= new node;
cout<<"\nEnter data:";
cin>>temp->data;
temp->next=NULL;
if (rear==NULL)
{ rear=temp;
front=temp; }
else
{ rear->next=temp;
rear=temp; } }
void queue::qdelete()
{ if(front!=NULL)
{ node *temp=front;
cout<<front->data<<" deleted\n";
front=front->next;
delete temp;
if(front==NULL)
rear=NULL; }
else
cout<<"\nQueue empty"; }
void queue::qdisplay()
{ node*temp=front;
if(front==NULL)
cout<<"\Queue empty";
while(temp!=NULL)
{ cout<<temp->data<<" ";
temp=temp->next; } }
queue::~queue()
{ while(front!=NULL)
{ node *temp=front;
front=front->next;
delete temp; } }
void main()
{ queue Q;
int ch;
clrscr();
do
{ cout<<"\nEnter your choice(1-4):";
cout<<"\n1. Insert \n2. Delete \n3.Display \n4. Exit\n";
cin>>ch;
switch(ch)
{ case 1 : Q.qinsert(); break;
case 2 : Q.qdelete(); break;
case 3 : Q.qdisplay(); break;
case 4 : exit(0);
default : cout<<"\nWrong choice";
} }while(ch!=4);
getch();