Q1. WAP To Show Concept of Polymorphism To Calculate The Area of Circle, Triangle, Square
Q1. WAP To Show Concept of Polymorphism To Calculate The Area of Circle, Triangle, Square
#include<stdlib.h>
#include<math.h>
{ float s,ar;
s=(a+b+c)/2;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
return ar; }
{ return a*b; }
{ return a*a; }
int main()
int choice,s1,s2,s3,ar;
do
cout<<”1. Triangle\n”;
cout<<”2. Square\n”;
cout<<”3. Rectangle\n”;
cin>>choice;
cout<<”\n”;
switch(choice) {
cin>>s1>>s2>>s3;
ar=area(s1,s2,s3);
cout<<” The area is” << ar << “\n”;
break;
cin>>s1;
ar=area(s1);
break;
cin>>s1>>s2;
ar=area(s1,s2);
break;
case 4 : break;
return 0;
}
Q2. WAP to show a function with default arguments
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
display();
display('#');
display('$',5);
return 0; }
{ 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>
while (start<end)
int temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
cout<<arr[i]<<" ";
cout<<endl;
void main()
int arr[]={1,2,3,4,5,6};
clrscr();
printarray(arr,6);
reversearray(arr,0,5);
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];
cin>>size1;
for(i=0;i<size1;i++)
{ cin>>arr1[i]; }
cin>>size2;
for(i=0;i<size2;i++)
{ cin>>arr2[i]; }
for(i=0;i<size1;i++)
{ merge[i]=arr1[i]; }
size=size1+size2;
{ merge[k]=arr2[i]; }
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();
int i,j,m,n,sum=0;
cin>>m>>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]; }
sum=0; }
sum=0;
for(j=0;j<n;++j)
{ for(i=0;i<m;++i)
{ sum=sum+array[i][j]; }
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()
cin>>ICode;
gets(Item);
cin>>Price;
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 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);
cin>>no_of_traveller;
no_of_bus=buscalc(); }
{ if(no_of_traveller<20)
no_of_bus=1;
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 {
cin>>ch;
switch(ch) {
case 3 : exit(0);
} } 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(); };
{ protected:
int basic,hra;
public:
void get2(); };
{ protected:
int deptcode;
public:
void get3();
void display(); };
cin>>age;
cout<<"enter your name\n";
cin>>name; }
cin>>basic>>hra; }
cin>>deptcode; }
cout<<"\nage is "<<age;
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>
int count()
{ ifstream ifile("story.txt");
if(!ifile)
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();
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()
int getrno()
void main()
{ clrscr();
char ans='y';
while(ans=='y' || ans=='Y')
{ stud1.getdata();
cin>>ans; }
clrscr();
int rno;
long pos;
char found='f';
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";
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()
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];
gets(n);
fil.open("binary.dat",ios::in| ios::out|ios::binary);
if(!fil)
exit(0); }
else
{ filread((char*)&fileobj,sizeof(fileobj));
while(!fil.eof())
{ if(strcmp(n,fileobj.getn())==0)
{ fil.seekg(0,ios::cur);
fileobj.input();
fil.seekp(fil.tellg()
sizeof(fileobj));
fil.write((char*)&fileobj,
sizeof(fileobj)); }
else
getch(); }
fil.read((char*)&fileobj;
sizeof(fileobj)); } }
fil.close();
}
Q12. WAP to show binary search.
#include<conio.h>
#include<iostream.h>
void main()
{ clrscr();
int ar[50],item,n,index;
cin>>n;
for(int i=0;i<n;i++)
cin>>ar[i];
cin>>item;
index=bsearch(ar,n,item);
if(index==-1)
else
getch(); }
{ 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;
#include<conio.h>
void main()
{ clrscr();
int a[100],i,pos,num,item,size;
cin>>size;
for(i=0;i<size;i++)
cin>>a[i];
for(i=0;i<size;i++)
cout<<"element at position"<<i+1<<"is"<<a[i]<<endl;
cin>>pos;
--pos;
item=a[pos];
for(i=pos;i<size;i++)
a[i]=a[i+1];
size--;
for(i=0;i<size;i++)
getch();
#include<conio.h>
void main()
{ clrscr();
int ar[50],item,n,index;
cin>>n;
for(int i=0;i<n;i++)
{ cin>>ar[i]; }
bubblesort(ar,n);
for(i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<endl;
getch();
{ 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; } }
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<<"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];
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;
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";
cin>>ch;
switch(ch)
break;
break;
break;
} }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(); };
cout<<"\nEnter data:";
cin>>temp->data;
temp->next=NULL;
if(top==NULL)
top=temp;
else
{ temp->next=top;
top=temp; } }
{ if (top!=NULL)
{ node *temp=top;
top=top->next;
cout<<temp->data<<" deleted";
delete temp; }
else
{ cout<<"\nStack is empty"; } }
{ node *temp=top;
if(temp==NULL)
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";
cin>>ch;
switch(ch)
break;
break;
} 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];
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
cin>>ch;
switch(ch)
case 4 : exit(0);
} } 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
public:
queue()
{ rear=NULL;
front=NULL; }
void qinsert();
void qdisplay();
void qdelete();
~queue(); };
void queue::qinsert()
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
cin>>ch;
switch(ch)
} }while(ch!=4);
getch();