Академический Документы
Профессиональный Документы
Культура Документы
#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";
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;
beg=mid+1;
OUTPUT
S E A R C H
M E N U
1.
Linear Search
2.
Binary search
3.
Exit
for(int i=0;i<size;i++)
cout<<AR[i]<<" ";
}
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++)
{
{
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;
}
}
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
selection sort
2.
insertion sort
3.
bubble sort
4.
exit
MENU
1.
selection sort
2.
insertion sort
3.
bubble sort
4.
exit
3
4
5
6
7
8
9
MENU
1.
selection sort
2.
insertion sort
3.
bubble sort
4.
exit
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();
// 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
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>
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);
break;
case 0 : exit(0);
}
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.
2.
3.
4.
0.
Exit
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.
6.
7.
8.
1.
Exit
#include<iostream.h>
#include<conio.h>
#include<process.h>
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();
}
//function to
if(item<AR[0])
pos=0;
else
{ for(int i=0; i<size-1; i++)
{
{ pos=i+1;
break;
}
}
if(i==size-1)
pos=size;
}
return pos;
}
OUTPUT
How
50>. . .3
1
6
9
Enter Element to be inserted. . .5
Want to insert more elements?
<y/n>:
<y/n>:
as shown below . . . .
<y/n>:
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
void main()
{
clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
cin>>k;
switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{
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;
: ";
display(stack,top);
break;
case 4: exit(0);
break;
// end of switch
}while(m=='y'||m=='Y');
// end of do-while
loop
getch();
}
// end of main()
if(top==-1)
return -1;
else
{
ret=stack[top];
top--;
return ret;
}
}
Output:
2 1
CONSTRUCTOR DESTRUCTOR
Q1. Write a program using constructor and destructor to find
the area of rectangle.
#include <iostream.h>
using namespace std;
// Constructor
CRectArea::CRectArea (int x, int y) {
*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;
}
OCode
Of type string
OType
Of type string
OSize
Of type integer
OFabric
Of type string
OPrice
Of type float
OPrice(Rs)
TROUSER
1500
JACKET
2500
Public members:
#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()
{
cin>>OCode;
cout<<"\n Enter Outfit type :";
cin>>OType;
cout<<"\n Enter Outfit Fabric :";
gets(OFabric);
OUTPUT:
Outfit code: 34
Outfit type: trouser
Outfit size: 40
Outfit price: 1500
Output fabric : DENIM
{ 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
#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;
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
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
//triangle
};
void fn::area(int a)
{
cout<<"Area of Circle:"<<pi*a*a;
}
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
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4
princ, int
time, float
rate)
{
Cout <<\nPrincipal Amount:<<princ;
Cout <<\tTime:<<time<<years;
Cout<<\tRate:<<rate;
Cout<<\nInterest Amount:<<(princ*time*rate)<<\n;
}
Void
{
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
#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:
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
#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++)
}
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
Largest value: 9
#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
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
};
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;
}
};
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
}
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:
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Roll number : 06
Grade = A
#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";
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 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;
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();
}
getch();
}
Output:
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\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
1. Add balance
2. Withdraw balance
3. Display account details and its status
4. Exit
Enter your choice : 2
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