Вы находитесь на странице: 1из 85

ARRAYS

Q1. Write a program to illustrate the two types of searches


in an integer type array. Make use of functions and make a
menu driven program.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int Lsearch(int[],int,int);
int Bsearch(int[],int,int);
int main()
{ char ch1;
int ch;
int AR[50],i,item,N,index;
do
{ clrscr();
cout<<"\n"<<"S E A R C H

M E N U";

cout<<"\n1. Linear Search";


cout<<"\n2. Binary Search";
cout<<"\n3. Exit"<<"\n";
cout<<" Enter your choice (1,2 or 3): ";
cin>>ch;
if( ch==1|| ch==2)

{ cout<< "Enter desired array size(max.50).....:";


cin>>N;
}
switch(ch)
{ case 1 :cout<<"\nEnter array elements:\n";
for(i=0;i<N;i++)
{cin>>AR[i];}
cout<<"\nEnter element to be searched for........:\n";
cin>>item;
index=Lsearch(AR,N,item);
if(index== -1)
cout<<"\n*^*^* Sorry!! Given element could not be found
*^*^*\n";
else
cout<<"\nElement found at index : "<<index<<", Position:
"<<index+1<<endl;
break;

case 2 : cout<<"\nEnter array elements(must be sorted in asc


order) :\n";
for(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<<"\n*^*^* Sorry!! Given element could not be found


*^*^*\n";
else
cout<<"\nElement found at index : "<<index<<", Position:
"<<index+1<<endl;
break;

case 3 :break ;
default : cout<<"!!!!!!Wrong choice!!!!!!!\n";
break ;
}
cout<<"\nPress a key to continue......\n";
ch1 = getchar() ;
} while(ch>=1 && ch<=2);
return 0 ;
}
int Lsearch(int AR[],int size,int item)
{ for (int i=0;i<size;i++)
{ if (AR[i]==item)

return i;

}
return -1;
}
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;

OUTPUT
S E A R C H

M E N U

1.

Linear Search

2.

Binary search

3.

Exit

Enter your choice:


1
Enter desired array size (max.50):
3
Enter array elements :
1
4
6
Enter elements to be searched for ..:
4

Element found at index : 1, Position: 2


Press a key to continue.

Q2. Write a program to illustrate deletion (with position)


in an array (the position can be the beginning or the end or
in middle anywhere at a specified place, so all the cases
should be handled). Write functions for all three
separately. And make the program menu driven and user
friendly.
#include<iostream.h>
#include<conio.h>
#include<process.h>
int size,a,ch,AR[50];
char ans;
void delbeg(int AR[],int size)
{for(int i=1;i<size;i++)
{AR[i-1]=AR[i];
}
size=size-1;
{cout<<"\n The array after making changes"<<endl;
for(i=0;i<size;i++)
cout<<AR[i]<<" ";
}}

void dellast(int AR[],int size)


{size=size-1;
cout<<"\n The array after making changes"<<endl;

for(int i=0;i<size;i++)
cout<<AR[i]<<" ";
}

void delany(int AR[],int size,int P)


{for(int i=P;i<size;i++)
{AR[i-1]=AR[i];
}
size=size-1;
{cout<<"\n Array after making changes:"<<endl;
for(i=0;i<size;i++)
cout<<AR[i]<<" ";
}}
void main()
{cout<<"Enter the size of the array"<<endl;
cin>>size;
cout<<"Enter the elements of the array";
for(int i=0;i<size;i++)
cin>>AR[i];
{ do
{cout<<"Element to be deleted"<<endl;
cout<<"1.From beginning"<<endl;
cout<<"2.From end"<<endl;
cout<<"3.From anywhere "<<endl;
cout<<"Enter your choice"<<endl;

cin>>ch;
switch(ch)
{case 1:delbeg(AR,size);

break;
case 2:size=size-1;
dellast(AR,size);
break;
case 3:cout<<"Enter the position of the element";
cin>>a;
delany(AR,size,a);
break;
default:cout<<"Invalid choice"<<endl;
}
cout<<"\n Want to continue?(y/n)"<<endl;
cin>>ans;}
while(ans=='y');getch();
}}

OUTPUT:
Enter the size of the array
4
Enter the elements of the array
1

Element to be deleted

1.From beginning
2.From end
3.From anywhere
Enter your choice
1
The array after making changes
2

Want to continue?(y/n)
N
Q3. Write a program to illustrate all the three sorts
Insertion, Selection and Bubble sort in an array. Make use
of functions to do the same. Make a menu driven and user
friendly program.*/
#include<iostream.h>
#include<conio.h>
void selectionSort(int Array[10]);
int bubblesort(int array[10]);
void insertion_sort(int x[],int length);
void main()
{ clrscr();
int a[10];
cout<<"Enter the numbers in the array:";
for(int i=0;i<10;i++)
{ cin>>a[i];
}
int ch;

do
{cout<<"\nMENU"
<<"\n1.selection sort"
<<"\n2.insertion sort"
<<"\n3.bubble sort"
<<"\n4.exit\n" ;
<<"\nenter your choice(1-4):";
cin>>ch;
switch(ch)
{case 1: selectionSort(a);
break;
case 2: bubblesort(a);
break;
case 3: insertion_sort(a,10);
break;
}
}while(ch<4);
getch();
}
void selectionSort(int A[10])
{
int i, j, min, min1;
for(i = 0; i<10; i++)
{

min1 = i;

min = A[i];

for(j = i+1;j<10;j++)
{
{

if(min > A[j])


min1 = j;

min = A[j];
}
}
int temp = A[i];
A[i] = A[min1];
A[min1]=temp;
}
cout<<"Array After selection Sorting is:"<<endl;
for(int k=0;k<10;k++)
cout<<A[k]<<" ";

}
int bubblesort(int array[10])
{
int len=10;
int temp;
for(int i=1;i<len-1;i++)
{
for(int j=0;j<len;j++)
{
if(array[j]>array[j+1] && (j+1)<len)

// then swap

{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}

cout<<"Array After Bubble Sorting is(ascending


order):"<<endl;
for(int k=0;k<len;k++)
cout<<array[k]<<" ";
return 0;
}
void insertion_sort(int x[],int length)
{
int key,i;
for(int j=1;j<length;j++)
{
key=x[j];
i=j-1;
while(x[i]>key && i>=0)
{
x[i+1]=x[i];

i--;
}
x[i+1]=key;
}
cout<<endl<<"array after insertion sort is:"<<endl;
for(i=0;i<length;i++)
{
cout<<x[i]<<" ";
}
}

OUTPUT
Enter number:1
9
7
5
0
2
4
6
3
8
MENU
1.

selection sort

2.

insertion sort

3.

bubble sort

4.

exit

enter your choice(1-4):1


array after selection sorting is :
0 1 2 3 4 5 6 7 8 9
MENU
1.

selection sort

2.

insertion sort

3.

bubble sort

4.

exit

enter your choice(1-4):2


array after bubble sorting is(ascending order) :
0 1 2 3 4 5 6 7 8 9

MENU
1.

selection sort

2.

insertion sort

3.

bubble sort

4.

exit

enter your choice(1-4):3


array after insertion sort is :
0
1
2

3
4
5
6
7
8
9
MENU
1.

selection sort

2.

insertion sort

3.

bubble sort

4.

exit

enter your choice(1-4):4

Q4. Write a program to sum the elements above and below the
main diagonal of a matrix.*/

#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
do
{
clrscr();

int a[20][20], i, j, m, n , csum, dsum;


//Input rows and column of Matrix A\n";
cout<<"\n Input row and column of Matrix A\n";
cin>>m>>n;
cout<<"\n Input Matrix A\n";
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"\n Matrix A \n";

// Display Matrix-A\par

for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
cout<<" "<<a[i][j];
cout<<"\n";
}
csum=0;
//Add

elements above the main diagonal\par

for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{
if(i<j)
csum+=a[i][j];
}
}
cout<<"\n Sum of elements\n Above the main diagonal is:";

cout<<csum<<"\n";
dsum=0;
//Add elements below the main diagonal
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{if(i>j)
dsum+=a[i][j];
}
}
cout<<"\n Below the main digonal is:";
cout<<dsum<<"\n";
cout<<"\n Want to continue (y/n)?:";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
getch();
} //end of main

OUTPUT
Input row and column of Matrix-A
2
3
Input Matrix-A
1
2
3

4
5
6
Matrix A
1 2 3
4 5 6
Sum of elements
Above the main diagonal is:11
Below the main diagonal is:4
Want to continue(y/n)? : n
Q5. Write a program to merge two given arrays into the third
array C.
Use switch case and implement the same with at least 4
different
combinations. Make use of functions.
#include<iostream.h>
#include<conio.h>
#include<process.h>

//FUNCTION TO MERGE IF ONE IS ASCENDING AND ONE IS


DESCENDING
void merge(int[],int,int[],int);
//FUNCTION TO MERGE IF BOTH IS ASCENDING
void merge1(int[],int,int[],int);
//FUNCTION TO MERGE IF BOTH IS DESCENDING
void merge2(int[],int,int[],int);

int f_asc(int a[])


{
int M;
cout<<"How many elements do u want to enter in the first
array?[ascending]\n";
cin>>M ;
cout<<"\nEnter first arrays element..\n";
for(int i=0;i<M;i++)
cin>>a[i];
return M;
}

int f_desc(int a[])


{
int M;
cout<<"How many elements do u want to enter in the first
array?[descending]\n";
cin>>M;
cout<<"\nEnter first arrays element..\n";
for(int i=0;i<M;i++)
cin>>a[i];
return M;
}
int s_asc(int a[])
{
int M;

cout<<"How many elements do u want to enter in the second


array?[ascending]\n";
cin>>M ;
cout<<"\nEnter second arrays element..\n";
for(int i=0;i<M;i++)
cin>>a[i];
return M;
}
int s_desc(int a[])
{
int M;
cout<<"How many elements do u want to enter in the second
array?[descending]\n";
cin>>M ;
cout<<"\nEnter second arrays element..\n";
for(int i=0;i<M;i++)
cin>>a[i];
return M;
}
void main()
{
int a[50], b[50], M, N, choice;
menu:;
clrscr();
cout<<"\nMERGING ARRAYS\n";
cout<<"1. Both Arrays Ascending\n";

cout<<"2. Both Arrays Descending\n";


cout<<"3. Fisrt Ascending, Second Descending\n";
cout<<"4. Fisrt Descending, Second Ascending\n";
cout<<"0. Exit\n";
cout<<"Enter your choice:\t";
cin>>choice;
switch(choice)
{
case 1 : M=f_asc(a);
N=s_asc(b);
merge1(a,M,b,N);
break;

case 2 : M=f_desc(a);
N=s_desc(b);
merge2(a,M,b,N);
break;
case 3 : M=f_asc(a);
N=s_desc(b);
merge(a,M,b,N);
break;

case 4 : M=f_desc(a);
N=s_asc(b);
merge(b,N,a,M);

//SWITCHES CASE AS PER USERS CHOICE

break;

case 0 : exit(0);

default : cout<<"Wrong Choice!!\n";


getch();
break;
};
goto menu;
}

void merge(int A[],int M, int B[],int N)


{
int C[50],MN=M+N;
int a,b,c,i;
for(a=0,b=N-1,c= 0;a<M && b>=0;)
{
if(A[a]<=B[b])
C[c++] = A[a++];
else
C[c++] = B[b--];
}
if(a<M)
{while(a<M)
C[c++] = A[a++];

}
else
{ while(b>=0)
C[c++] = B[b--];
}
cout<<"\n\nThe merged array is as shown \n";
for(i=0;i<MN;i++)
cout<<C[i]<<" ";
cout<<endl;
getch();
}
void merge1(int A[],int M, int B[],int N)
{
int C[50],MN=M+N;
int a,b,c,i;
for(a=0,b=0,c= 0;a<M && b<N;)
{
if(A[a]<=B[b])
C[c++] = A[a++];
else
C[c++] = B[b++];
}
if(a<M)
{while(a<M)
C[c++] = A[a++];

}
else
{ while(b<N)
C[c++] = B[b++];
}
cout<<"\n\nThe merged array is as shown \n";
for(i=0;i<MN;i++)
cout<<C[i]<<" ";
cout<<endl;
getch();
}
void merge2(int A[],int M, int B[],int N)
{
int C[50],MN=M+N;
int a,b,c,i;
for(a=M-1,b=N-1,c= 0;a>=0 && b>=0;)
{
if(A[a]<=B[b])
C[c++] = A[a--];
else
C[c++] = B[b--];
}
if(a>=0)
{while(a>=0)
C[c++] = A[a--];

}
else
{ while(b>=0)
C[c++] = B[b--];
}
cout<<"\n\nThe merged array is as shown \n";
for(i=0;i<MN;i++)
cout<<C[i]<<" ";
cout<<endl;
getch();
}

OUTPUT
MERGING ARRAYS
1.

Both Arrays Ascending

2.

Both Arrays Descending

3.

First Ascending, Second Descending

4.

First descending, second ascending

0.

Exit

Enter your choice : 1


How many elements do you want to enter in the first array?
[ascending]
5
Enter first arrays element..

1
3
5
7
9
How many elements do you want to enter in the second array?
[ascending]
5
Enter second arrays element..
2
4
6
8
10
The merged array is as shown
1 2 3 4 5 6 7 8 9 10

MERGING ARRAYS
5.

Both Arrays Ascending

6.

Both Arrays Descending

7.

First Ascending, Second Descending

8.

First descending, second ascending

1.

Exit

Enter your choice : 0

Q6. Write a program to illustrate insertion (with position)


in an array( the position can be the beginning or the end or
in middle anywhere at a specified place)(all three cases are
to be handled).Write functions for all three separately. And
make the program menu driven and user friendly.*/

#include<iostream.h>
#include<conio.h>
#include<process.h>

int FindPos(int[], int, int);

void main()
{ char s;
do
{ int AR[50], ITEM, N, index; //array can hold maximum 50
elements
clrscr();
cout<<"How many elements do you want?(max. 50)...";
cin>>N;
cout<<"\nEnter Array elements (in increasing order)\n";
for(int i=0; i<N; i++)
cin>>AR[i];
char ch='y';
while((ch=='y')||(ch=='Y'))
{ cout<<"\nEnter Element to be inserted...";
cin>>ITEM;

if(N==50)
{ cout<<"Overflow!!";
exit(1);
}
index=FindPos(AR, N, ITEM);
for(i=N; i>index; i--)
AR[i]=AR[i-1];
AR[index]=ITEM;
N+=1; //Number of elements updated
cout<<"\n Want to insert more elements? (y/n): ";
cin>>ch;
}
cout<<"\nThe array is now as shown below.....\n";
for(i=0;i<N;i++)
cout<<AR[i]<<"

";

cout<<endl;
cout<<"\nWant to continue with new array? (y/n): ";
cin>>s;
}while((s=='y')||(s=='Y'));
getch();
}

int FindPos(int AR[], int size, int item)


determine the position for new element
{ int pos;

//function to

if(item<AR[0])
pos=0;
else
{ for(int i=0; i<size-1; i++)
{

if(AR[i]<=item && item<AR[i+1])

{ pos=i+1;
break;
}
}
if(i==size-1)
pos=size;
}
return pos;
}

OUTPUT
How

many elements do you want?<max.

Enter Array elements

50>. . .3

<in increasing order>

1
6
9
Enter Element to be inserted. . .5
Want to insert more elements?

<y/n>:

Enter Element to be inserted. . .8


Want to insert more elements?
The array is now
1

<y/n>:

as shown below . . . .

Want to continue with new array ?

<y/n>:

Q7. Write a program to wap in c++ to implement stack as an


array.

#include<iostream.h>
#include<conio.h>
#include<process.h>

int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);

const int size=50;

void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do

cout<<"\nChoose from the following : \n\n"


<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display"
<<"\n 4. Exit"
<<"\n\nEnter your choice : ";

cin>>k;

switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{

cout<<"\nEnter the element : ";

cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cout<<"\nThe stack formed is : \n\n";
display(stack,top);
cout<<"\n\n\nWant to enter again ?: ";
cin>>ch;
}
break;

case 2: ch='y';

while(ch=='y'||ch=='Y')
{ res=pop(stack,top);
if(res==-1)
{
cout<<"\nUnderflow !!!!";
exit(1);
}
else
{
cout<<"\nThe deleted Element is : "<<res<<endl;
cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : ";
cin>>ch;
}
break;

case 3: cout<<"\nThe resultant stack is

: ";

display(stack,top);
break;

case 4: exit(0);
break;

default: cout<<"\nPlease enter desired keyword : ";

// end of switch

cout<<"\n\nChoose from the menu again ? : ";


cin>>m;

}while(m=='y'||m=='Y');

// end of do-while

loop
getch();
}

// end of main()

int push(int stack[],int &top,int el)


{
if(top==size-1)
return -1;
else
{
top++;
stack[top]=el;
return 0;
}
}

int pop(int stack[],int &top)


{
int ret;

if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}

void display(int stack[],int top)


{
cout<<stack[top]<<"<--";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"<--";
}

Output:

Choose from the following :


1. Push
2. Pop
3. Display
4. Exit

Enter your choice : 1

Enter the element : 1


The stack formed is :

Want to enter again ?: y

Enter the element : 2


The stack formed is :

Want to enter again ?: y

Enter the element : 3


The stack formed is :

Want to enter again ?: y

Enter the element : 4


The stack formed is :

Want to enter again ?: N

Choose from the menu again ? : y

Choose from the following :


1. Push
2. Pop
3. Display
4. Exit

Enter your choice : 2


The deleted Element is : 4
The resultant stack is :

Want to delete again ? : y

The deleted Element is : 3


The resultant stack is :

Want to delete again ? : n

Choose from the menu again ? : y

Choose from the following :


1. Push
2. Pop
3. Display
4. Exit

Enter your choice : 3

The resultant stack is

2 1

Choose from the menu again ? : n

CONSTRUCTOR DESTRUCTOR
Q1. Write a program using constructor and destructor to find
the area of rectangle.

#include <iostream.h>
using namespace std;

// A class CRectArea with a constructor and descructor


// to determine the area of a rectangle.
class CRectArea {
int *width, *height;
public:
CRectArea (int,int);
~CRectArea ();
int areaofrect () {
return (*width * *height);
}
};

// Constructor
CRectArea::CRectArea (int x, int y) {

//Dynamically allocate some memory


width = new int;

height = new int;

*width = x;
*height = y;
}

// Destructor
CRectArea::~CRectArea () {
//Delete the allocate memory
delete width;
delete height;
}

int main () {
CRectArea myrectangle (2,2);
cout << The area of the rectangle is: <<
myrectangle.areaofrect() << endl;
return 0;
}

Q2.Define a class Outfit in C++ with the following


descriptions :
Private members :

OCode

Of type string

OType

Of type string

OSize

Of type integer

OFabric

Of type string

OPrice

Of type float

A function InitPrice( ) which calculates and assigns the


values of OPrice as follows :
For the value of OFabric DENIM,
OType

OPrice(Rs)

TROUSER

1500

JACKET

2500

For fabric other than DENIM the above mentioned OPrice


gets reduced by 25%.

Public members:

A constructor to assign initial values of OCode, OType


and OFabric with the word NOT INITIALISED and OSize and
OPrice with 0.

A function Input ( ) to input the values of the data


members OCode, OType, OSize and OFabric and invoke the
InitPrice( ) function.

A function Display ( ) which displays the content of all


the data members for an Outfit.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class Outfit
{ private:
char OCode[20];
char OType[20];
char OFabric[20];
int OSize;
float OPrice;
void InitPrice()
{if((strcmp(OFabric,"DENIM")==0)||(strcmp(OFabric,"denim")==
0))
{
if((strcmp(OType,"TROUSER")==0)||(strcmp(OType,"trouser")==0
))
OPrice=1500;
else if
((strcmp(OType,"JACKET")==0)||(strcmp(OType,"jacket")==0))
OPrice=2500;
}

else

{
if((strcmp(OType,"TROUSER")==0)||(strcmp(OType,"trouser")==0
))
OPrice=1500-0.25*1500;
else
if((strcmp(OType,"JACKET")==0)||(strcmp(OType,"jacket")==0))
OPrice=2500-0.25*250;
}
}

public:
Outfit()
{
strcpy(OCode,"NOT INITIALISED");
strcpy(OType,"NOT INTIIALISED");
strcpy(OFabric,"NOT INTIALISED");
OSize=0;
OPrice=0;
}
void Input()
{

cout<<"\n Enter Outfit Code :";

cin>>OCode;
cout<<"\n Enter Outfit type :";
cin>>OType;
cout<<"\n Enter Outfit Fabric :";
gets(OFabric);

cout<<"\n Enter Outfit Size :";


cin>>OSize;
InitPrice();
}
void Display()
{
cout<<"\n outfit code :"<<OCode;
cout<<"\n outfitt type :"<<OType;
cout<<"\n outfit size :"<<OSize;
cout<<"\n outfit price :"<<OPrice;
cout<<"\n output fabric :"<<OFabric;
}
}O1;
void main()
{ clrscr();
O1.Input();
O1.Display();
getch();
}

OUTPUT:

Enter Outfit Code: 34


Enter Output type: trouser

Enter Output Fabric: DENIM

Enter Output Size: 40

Outfit code: 34
Outfit type: trouser
Outfit size: 40
Outfit price: 1500
Output fabric : DENIM

Q3. Program defining a class TravelPlan with the given


specifications and testing this class */
#include<iostream.h>
#include<conio.h>
#include<string.h>
class TravelPlan

{ long PlanCode;
char Place[25];
int Number_of_travellers;
int Number_of_buses;
public:
TravelPlan()

{ PlanCode=1001;
strcpy(Place,"Agra");
Number_of_travellers=5;
Number_of_buses=1;
}
void NewPlan()
{ cout<<"Enter plancode";
cin>>PlanCode;
cout<<"Enter place";
cin>>Place;
cout<<"Enter number of travelers";
cin>>Number_of_travellers;
int buses=0;
if(Number_of_travellers<20)
buses=1;
else if(Number_of_travellers<40)
buses=2;
else
if(Number_of_travellers>40)
buses=3;
Number_of_buses=buses;
}
void ShowPlan()
{ cout<<"Plancode:"<<PlanCode<<endl;
cout<<"place:"<<Place<<endl;

cout<<"no. of travelers:"<<Number_of_travellers<<endl;
cout<<"no. of buses:"<<Number_of_buses<<endl;
}
~TravelPlan()
{ cout<<"destructor \n";
}
};
void main()
{clrscr();
TravelPlan T1;
T1.NewPlan();
T1.ShowPlan();
getch();}

OUTPUT
Enter plancode 1234
Enter place delhi
Enter number of travelers 10
Plancode : 1234
Place :delhi
No. of travelers : 10
No of buses :1

Q3. Write a program to print student details using


constructors and destructors.

#include<iostream.h>
#include<conio.h>
class stu
{
private: char name[20],add[20];
int roll,zip;
public: stu ( );//Constructor
~stu( );//Destructor
void read( );
void disp( );
};
stu :: stu( )
{
cout<<This is Student Details<<endl;
}
void stu :: read( )
{
cout<<Enter the student Name;
cin>>name;
cout<<Enter the student roll no ;
cin>>roll;

cout<<Enter the student address;


cin>>add;
cout<<Enter the Zipcode;
cin>>zip;
}
void stu :: disp( )
{
cout<<Student Name :<<name<<endl;
cout<<Roll no

is

:<<roll<<endl;

cout<<Address is

:<<add<<endl;

cout<<Zipcode is

:<<zip;

}
stu : : ~stu( )
{
cout<<Student Detail is Closed;
}

void main( )
{
stu s;
clrscr( );
s.read ( );
s.disp ( );
getch( );
}

OUTPUT-:
Enter the student Name
James
Enter the student roll no
01
Enter the student address
Newyork
Enter the Zipcode
919108

Student Name : James


Roll no is : 01
Address is : Newyork
Zipcode is :919108

FUNCTION OVERLOADING
Q1. Write a program to calculate the area of
circle,rectangle and triangle using function overloading.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
public:
void area(int);

//circle

void area(int,int);

//rectangle

void area(float ,int,int);

//triangle

};

void fn::area(int a)
{

cout<<"Area of Circle:"<<pi*a*a;
}

void fn::area(int a,int b)


{
cout<<"Area of rectangle:"<<a*b;
}
void fn::area(float t,int a,int b)
{
cout<<"Area of triangle:"<<t*a*b;
}

void main()
{
int ch;
int a,b,r;
clrscr();
fn obj;
cout<<"\n\t\tFunction Overloading";
cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area
of Triangle\n4.Exit\n:;
cout<<Enter your Choice:";
cin>>ch;

switch(ch)
{
case 1:
cout<<"Enter Radious of the Circle:";

cin>>r;
obj.area(r);
break;
case 2:
cout<<"Enter Sides of the Rectangle:";
cin>>a>>b;
obj.area(a,b);
break;
case 3:
cout<<"Enter Sides of the Triangle:";
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4:
exit(0);
}
getch();
}
Output:

Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit

Enter Your Choice: 2

Enter the Sides of the Rectangle: 5 5

Area of Rectangle is: 25

1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4

Q2) Program to illustrate working of function overloadind(as


compared to default arguments).Calculate interest amount
using function overloading. */

#include <iostream .h>


#include<conio.h>
Void amount(float

princ, int

time, float

rate)

{
Cout <<\nPrincipal Amount:<<princ;
Cout <<\tTime:<<time<<years;
Cout<<\tRate:<<rate;
Cout<<\nInterest Amount:<<(princ*time*rate)<<\n;
}
Void

amount(float princ,int time)

{
Cout<<\nPrincipal Amount:<<princ;
Cout<<\tTime:<<time<<years;
Cout<<\tRate:0.08;
Cout<<\nInterest Amount:<<(princ*time*0.08)<<\n;
}
Void amount(float princ,float rate)
{
Cout<<\nPrincipal Amount :<<princ;
Cout<<\tTime:2 years;
Cout<<\tRate:<<rate;
Cout<<\nInterest Amount:<<( princ*2*rate)<<\n;
Void amount(int time,float rate)
{
Cout<<\nPrincipal Amount:2000;
Cout<<\tTime:<<time<<years;
Cout<<\tRate:<<rate;
Cout<<\nInterest Amount:<<(2000*time*rate)<<\n;
}
Void amount(float princ)
{
Cout<<\nPrincipal Amount:<<princ;
Cout<<\tTime:2 years;
Cout<<\tRate:0.08;
Cout<<\nInterest Amount:<<(princ*2*0.08)<<\n;

}
Int main()
{
Clrscr();
Cout<<Case 1;
Amount(2000.0F);
Cout<<Case 2;
Amount(2500.0F,3);
Cout<<Case 3;
Amount(2300.0F,3,0.11F);
Cout<<Case 4;
Amount(2,0.12F);
Cout<<Case 5;
Amount (6,0.07F);
Retun 0;
}

OUTPUT
Case 1
Principal Amount : 2000
Interest Amount : 320

Time : 2 years

Rate : 0.08

Case 2
Principal Amount : 2500
Interest Amount : 600

Time : 3 years

Rate : 0.08

Case 3
Principal Amount : 2300
Interest Amount : 759

Time : 3 years

Rate : 0.11

Case 4
Principal Amount : 2000
Interest Amount : 480

Time : 2 years

Rate : 0.12

Case 5
Principal Amount : 2000
Interest Amount : 840

Time : 6 years

Rate : 0.07

Q3) Write a program to demonstrate how function overloading


is carried out for swapping of two variables of the various
data types, namely integer, floating point number and
character types

#include<iostream.h>
#include<conio.h>
void swap(int &ix,int &iy);
void swap(float &fx,float &fy);
void swap(char &cx,char &cy);
void main()
{
int ix,iy;
float fx,fy;
char cx,cy;
clrscr();
cout<<"Enter 2 integers:";

cin>>ix>>iy;
cout<<"Enter 2 floating point no:s:";
cin>>fx>>fy;
cout<<"Enter 2 characters:";
cin>>cx>>cy;
cout<<"\nIntegers:";
cout<<"\nix="<<ix<<"\niy="<<iy;
swap(ix,iy);
cout<<"\nAfter swapping";
cout<<"\nix="<<ix<<"\niy="<<iy;
cout<<"\nFloating point no:s";
cout<<"\nfx="<<fx<<"\nfy="<<fy;
swap(fx,fy);
cout<<"\nAfter swapping";
cout<<"\nfx="<<fx<<"\nfy="<<fy;
cout<<"\nCharacters";
cout<<"\ncx="<<cx<<"\ncy="<<cy;
swap(cx,cy);
cout<<"\nAfter swapping";
cout<<"\ncx="<<cx<<"\ncy="<<cy;
getch();
}
void swap(int &a,int &b)
{
int temp;

temp=a;
a=b;
b=temp;
}
void swap(float &a, float &b)
{
float temp;
temp=a;
a=b;
b=temp;
}
void swap(char &a, char &b)
{
char temp;
temp=a;
a=b;
b=temp;
}

Output:

Enter 2 integers: 100 200


Enter 2 floating point no:s :-11.11 22.22
Enter 2 characters: s t

Integers:
Ix=100
Iy=200
After swapping
Ix=200
Iy=100
Floating point no:
Fx=-11.11
Fy=22.22
After swapping
Fx=22.22
Fy=-11.11
Characters
Cx=s
Cy=t
After swapping
Cx=t
Cx=s

CLASSES & OBJECTS


Q1. WRITE A CLASS TO REPRESENT A VECTOR( 1-DIMENSIONAL
NUMERIC ARRAY).
INCLUDE THE FOLLOWING MEMBER FUNCTIONS:
1. FOR VECTOR CREATION.
2. FOR MODIFICATION OF A GIVEN ELEMENT.
3. FOR DISPLAYING THE LARGEST VALUE IN THE VECTOR.
4. FOR DISPLAYING THE SMALLEST VALUE IN THE VECTOR.
5. FOR DISPLAYING THE ENTIRE VECTOR.

#include<iostream.h>
#include<conio.h>
class vector_{public :int ver[10],l[10];int
i,j,k,x,ind,large;
void create();
void modify();
void displ();
void display();
};
void vector_ :: create()
{cout << "Enter the vector :" << endl;
for (i=0;i<10;i++)

{cin >> ver[i];}cout << "Entered vector :";


cout << endl;for (i=0;i<10;i++){cout << " " << " " <<
ver[i];}}
void vector_ :: modify()
{cout << endl << endl << "Enter the index :";
cin >> ind;
cout << endl << "Enter the modify value :";
cin >> x;
ver[ind] = x;
}
void vector_ :: displ()
{ large=ver[0];
for(i=0;i<10;i++)
{
if(large<ver[i])
{
large=ver[i];
}
}
cout<<"\n Largest value is:-"<<large;

}
void vector_ :: display()
{
for (j=0;j<10;j++){cout << " " << " " << ver[j];

}
}
void main()
{
vector_ ve;
clrscr();
ve.create();
ve.modify();
ve.display();
ve.displ();
getch();
}

OUTPUT:
Enter the vector:
1
2
3
4
5
6
7
8
9
7

Entered vector:
1

Enter the index: 7


Enter the modify value: 1
1

Largest value: 9

Q2. Program modelling a ticketselling booth with a class


called ticbooth and testing this class.

#include<iostream.h>
#include<conio.h>
class ticbooth {

int people;

float amount;
public:
ticbooth();
void notsold();
void sold();
void disptotal();
};
ticbooth::ticbooth()
{ people=amount=0;
}
void ticbooth::notsold()
{ people++;

}
void ticbooth::sold()
{ people++;
amount+=2.5;
}
void ticbooth::disptotal()
{ cout<<"people="<<people<<"\n"<<"amount="<<amount;
}

void main()
{ clrscr();
ticbooth t1;
int i=1,n;
char ch;
do
{ cout<<"Person:"<<i<<"\n";
cout<<"Want ticket?(1for yes,0 for no):";
cin>>n;
if(n==1)
t1.sold();
else if (n==0)
t1.notsold();
i++;
cout<<"more people???(y/n):";
cin>>ch;

}
while (ch=='y'||ch=='Y');
t1.disptotal();
getch();
}

OUTPUT:
Person 1:
Want ticket?(1 for yes, 0 for no):
1
More people???(y/n):
y
Person 2:
Want ticket?(1 for yes, 0 for no):
1
More people???(y/n):
n
people=2
amount=5

Q3. Write a program to define the classes PERSON, GAME and


STUDENT & to access the essential data using multiple
inheritance.*/

#include<iostream.h>
#include<stdio.h>
#include<conio.h>

class person{ char name[21];


int age;
public:
void indata()
{cout<<"\n\nEnter the name of Student: " ;
gets(name);
cout<<"\n\nEnter the age : ";
cin>>age;
}
void outdata();

};

void person::outdata() // since the function contains loop


so it is not made inline
{
cout<<"\n\n";
for(int i=0; i<79; i++)

cout<<"-";
cout<<"\n\nName of the student is: "<<name;
cout<<"\n\nAge of the student is : "<<age;

class game {
char game_name[20];
public:
void input()
{
cout<<"\n\nEnter the game name : ";
cin.get();cin.getline(game_name,20);
}
void output()
{
cout<<"\n\nGame opted by the student is :
"<<game_name;
}
};

class student: public person, public game


{ float Tmarks;
int rollno;
public:

char calgrade()
{if(Tmarks>90)
return 'A';
else if(Tmarks>80&&Tmarks<=90)
return 'B';
else if(Tmarks>70&&Tmarks<=80)
return 'C';
else if(Tmarks>60&&Tmarks<=70)
return 'D';
else
return 'E';
}
void enter()
{
indata(); // indata() of class person called here

cout<<"\n\nEnter the roll number: "; cin>>rollno;

input(); // input() of class game called here

cout<<"\n\nEnter total marks (out of 100) : ";


cin>>Tmarks;

}
void display()

{
outdata();
cout<<"\n\nRoll number : "<<rollno;
output();
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};

void main()
{ clrscr();
student A;
A.enter();
A.display();
getch();
}
Output:

Enter the name of Student: Divay Aneja

Enter the age : 17

Enter the roll number: 06

Enter the game name : Tennis

Enter total marks (out of 100) : 95

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

Name of the student is: Divay Aneja

Age of the student is : 17

Roll number : 06

Game opted by the student is : Tennis

Total marks are : 95

Grade = A

Q4) Write a C++ program to perform various operations on a


string class without using language supported built-in
string functions. The operations on a class are:
(a) Read a string
(b) Display the string
(c) Reverse the string
(d) Copy the sting into an empty string
(e) Concatenate two strings

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class strn{
char a[20],flag;
int i,j,k;
public:
void read();
void rev(strn &obj2);
void copy(strn &obj2);
void merge(strn &obj2);
void display();
strn()
{
flag='y';
}

};
void strn::read()
{
gets(a);
}
void strn::rev(strn &obj2)
{
int l = strlen(a);
int j=0;
for(i=l-1;i>=0;i--)
{
obj2.a[j]=a[i];
j++;
}
obj2.a[j]='\0';
getch();
}
void strn::copy(strn &obj2)
{
cout<<"\n\tThe entered string";
puts(a);
for(i=0;a[i]|='\0';i++)
obj2.a[i]='\0';
}
void strn::merge(strn &obj2)

{
char c[40];
cout<<"\n\t";
for (i=0;a[i]|='\0';i++)
c[i]=a[i];
int k=i;
for(i=0;obj2.a[i]|='\0';i++)
{
c[k]=obj2.a[i];
k++;
}
c[k]='\0';
cout<<"\n\tConcatenated String is";
for(i=0;c[i]|='\0';i++)
cout<<c[i];
}
void strn::display()
{
puts(a);
}
void main()
{
strn x,y;
clrscr();
cout<<"\n\n\n\t";

cout<<"This program is meant to accept a string,reverse";


cout<<"it, check if it\n\t is palindrome or not and then
display";
cout<<"it in alphabetical order \n\n\t";
cout<<"\n\n\n\tPress any key to continue....";
getch();
clrscr();
cout<<"\n\t";
cout<<"\n\tEnter the string";
x.read();
cout<<"\tEntered string is ";
x.display();
x.rev(y);
cout<<"\tThe reversed string is";
y.display();
x.copy(y);
cout<<"\tThe copied string is:";
y.display();
cout<<"\tEnter the string for concatenation:";
y.read();
x.merge(y);
cout<<"\n\n\t\tbye|";
getch();
}

OUTPUT
This program is meant to accept a string ,reverse it ,check
if it is palindrome or not and then display in alphabetical
order
Press any key to continue.
Enter the string- peter
Enter string is peter
The reversed string is retep
The entered string- peter
The copied string is:
Enter the string for concatenation: sarah
Concatenated string is petersarah
Bye!

Q5. Write a program to create a binary file and write objects of class WORKER to
it and display worker details on screen after reading from the binary file. */
#include<fstream.h>
#include<conio.h>
class WORKER { char Wname[20];
int Wno;
int HRWK; // Hours work
float wgrate; // wage rate
float totwage;
float calcwage (int HRWK, float wgrate)

{
totwage=HRWK*wgrate;
return totwage;
}
public:
WORKER ()
{
Wno=1;
wgrate = 500;
HRWK =5;
}
void INDATA();
void OUTDATA();
};
void WORKER::INDATA ()
{
cout<<"\n\n\nEnter Worker's name : ";
cin.get();
cin.getline(Wname,80);
cout<<"\n\nEnter worker Number : ";
cin>>Wno;
cout<<"\n\nEnter wage rate of worker : ";
cin>>wgrate;
cout<<"\n\nEnter hours work : ";

cin>>HRWK;
}

void WORKER :: OUTDATA()


{
cout<<"\n\nWorker's name => "<<Wname;
cout<<"\n\nWorker number => "<<Wno;
cout<<"\n\n\nWage rate = "<<wgrate;
cout<<"\n\nHours Work = "<<HRWK;
cout<<"\n\nTotal Wage = "<<calcwage (HRWK,wgrate);
}

void main()
{
clrscr();
int n;
l:
cout<<"\n\nEnter the total number of workers : "; cin>>n;
if(n<=0)
{
cout<<"\n\nEnter again. ";
goto l;
}
WORKER w[80],z[80];

fstream o;

for(int i=0; i<n; i++)


{

w[i].INDATA();
o.open("www.dat",ios::binary|ios::out);
o.write((char*)&w[i],sizeof(w[i]));
o.close();

o.open("www.dat",ios::binary|ios::in);
o.read((char*)&z[i],sizeof(z[i]));
o.close();
}

cout<<"\n\n\n\n--------------------------------------------------- # Worker Details # -------------------------------------------------------";


for(i=0; i<n; i++)
{
w[i].OUTDATA();
cout<<"\n\n";
}

getch();
}

Output:

Enter the total number of workers : 2

Enter Worker's name : Shivam


Enter worker Number : 1
Enter wage rate of worker : 120
Enter hours work : 5

Enter Worker's name : Gopal


Enter worker Number : 2
Enter wage rate of worker : 150
Enter hours work : 8
--------------------------------------------------------------- # Worker Details # ----------------------------------------------------------------Worker's name => Shivam
Worker number => 1
Wage rate = 120
Hours Work = 5
Total Wage = 600
Worker's name => Gopal
Worker number => 2
Wage rate = 150
Hours Work = 8
Total Wage = 1200

Q 6. Write a program in c++ to define a class BANK and to perform its basic
functions .
#include<iostream.h>
#include<conio.h>
#include<process.h>
int n, am;
class bank { char name[80];
int acc_no;
enum acctype{S,C};
void account_type(int n);
float balance;
public:
float add_bal(float n);
float withdrawl (float k);
void initial();
void display();
}acc;
float bank:: add_bal(float n)
{
cout<<"\n\nYour balance is : "<<balance;
balance+=n;
return balance;
}
float bank::withdrawl (float k)
{ m: cout<<"\n\nYour balance is : "<<balance;
if((balance-k)<1000)
{cout<<"\n\n\nUnable to withdraw money, as minimum of Rs. 1000
has to be in account.";
cout<<"\n\n\t\t Enter the amount to be withdrawn : "; cin>>k;
goto m;
}
balance-=k;

return balance;
}
void bank :: initial()
{cout<<"Enter your name : ";
cin.getline(name,80);
cout<<"\n\nEnter your acc. number : ";
cin>>acc_no;
l:cout<<"\n\nEnter your acc. type (S/N) -> '1 for S' and '2 for N' : ";
cin>>n;
if(n==1||n==2)
{}
else
goto l;
m:cout<<"\n\nEnter your acc. balance : ";
cin>>balance;
if(balance<1000)
{
cout<<"\n\nInitial balance cannot be less than 1000.\n\n";
goto m;
}
}
void bank :: account_type(int n)
{
switch(n)
{
case S+1 : cout<<" Saving account."; break;
case C+1 : cout<<" Current account."; break;
}
}
void bank::display()
{
cout<<"\n\nName of the account holder => "<<name;

cout<<"\n\nAccount number => "<<acc_no;


cout<<"\n\nAccount type => ";account_type(n);
cout<<"\n\nBalance in account = "<<balance;
}
void main()
{clrscr();
int ch; char choice;
acc.initial();
cout<<"\n\n\nWhat do you want to do : ";
do{ cout<<"\n\n";
cout<<"\n1. Add balance ";
cout<<"\n2. Withdraw balance ";
cout<<"\n3. Display account details and its status ";
cout<<"\n4. Exit ";
lb : cout<<"\n\nEnter your choice : "; cin>>ch;
switch(ch)
{ case 1: cout<<"\n\n\t\t Enter the amount to be deposited : ";
cin>>am;
cout<<"\n\nNow balance is : " <<acc.add_bal(am);
break;
case 2: cout<<"\n\n\t\t Enter the amount to be withdrawn : ";
cin>>am;
cout<<"\n\nNow balance is : " <<acc.withdrawl(am);
break;
case 3: cout<<"\n\n------------------------------------ # Account Details # -----------------------------------------";
acc.display();
break;
case 4: for(int i=0; i<500; i++);
exit(0);
break;
default : cout<<"\n\nPlease enter desired keyword . ";
goto lb;

}
cout<<"\n\n\n\n\nWant to chose again : "; cin>>choice;
} while(choice=='y'||choice=='Y');
getch();
}//end of main
Output:
Enter your name : Rahul Verma
Enter your acc. number : 20073312
Enter your acc. type (S/N) -> '1 for S' and '2 for N' : 1
Enter your acc. balance : 5000

What do you want to do :


1. Add balance
2. Withdraw balance
3. Display account details and its status
4. Exit
Enter your choice : 1

Enter the amount to be deposited : 5000


Your balance is : 5000
Now balance is : 10000

Want to chose again : y

1. Add balance
2. Withdraw balance
3. Display account details and its status
4. Exit
Enter your choice : 2

Enter the amount to be withdrawn : 9500

Your balance is : 10000

Unable to withdraw money, as minimum of Rs. 1000 has to be in account.


Enter the amount to be withdrawn : 6000

Your balance is : 10000


Now balance is : 4000

Want to chose again : y

1. Add balance
2. Withdraw balance
3. Display account details and its status
4. Exit
Enter your choice : 3

--------------------------------------------------------------- # Account Details # --------------------------------------------------------Name of the account holder => Rahul Verma
Account number => 19296
Account type => Saving account.
Balance in account = 4000

Want to chose again :y


1. Add balance
2. Withdraw balance
3. Display account details and its status
4. Exit
Enter your choice : 4

Вам также может понравиться