MENU

Chapter 6 Data File Handling Solutions

Question - 31 : -
Given a binary file “SPORTS.DAT” containing records of the following class :

class Player
 { char PNO[10]; //player number
 char Name[20]; //Name of player
 int rank; //rank of the player
 public :
 void EnterData()
 {
 gets (PNO) ;gets (Name);cin>>rank;
 }
 void DisplayData()
 { cout<
 cout<
 cout<
 }
 int Ret_rank() {return rank;}
 } ;
Write a function in C++ that would read contents of the file “SPORTS.DAT” and display the
details of those jolayers whose rank is above 500.

Answer - 31 : -

Void show()
 {
 fstream file;
 file.open( ("SPORTS. DAT"),ios::binary:ios::in));
 player P;
 while (file.read((char*)&P,sizeof(P)))
 if(P.rank>500)
 { Pi
 P.DisplayData () ;
 P.close();
 }
 }

Question - 32 : -
Assuming the class GAMES as declared below, write a functions in C++ to read the objects of GAMES from binary file GAMES.DAT and display those details of those GAMES, which are meant for children of AgeRange “8 to 13”.

class GAMES
 {
 int GameCode; char GameName [10];
 char AgeRange;
 public:
 void Enter()
 {
 cin>>GameCode;
 gets(GameName);
 gets (AgeRange);
 }
 void Display()
 {
 cout<
 cout<
 }
 char* AgeR() {return AgeRange};
 };

Answer - 32 : -

Void READGAMES ()
 { fstream obj ;obj. open ("GAMES.DAT",ios::binary);
 Games T;
 while (obj. read ((char*)&T,size of(T) ) )
 if (T. AgeR()>=8&& T. What Age <=13) T. Display ();
 obj. closet)
 }

Question - 33 : -
Assuming the class ANTIQUE as declared below, write a function in C++ to read the objects of Antique from binary file ANTIQUE. DAT and display those antique items, which are priced between 10000 and 15000.

class ANTIQUE
 {
 int ANO;
 char Aname [10] ;
 float price;
 public :
 void BUY() {cin>>ANO;gets(Aname); cin>> price;}
 void SHOW ( )
 {
 cout<
 cout < cout<
 }
 float Get Price ( ) {return Price;}
 };

Answer - 33 : -

void Disp( )
 {
 ifstream fin;
 fin.open ("ANTIQUE.DAT", ios:: in |ios : : out | ios : : binary);
 Antique A;
 while (!fin.eof( ))
 { -
 fin.read ((char*) &A, sizeof(A));
 if (A.GetPrice ( )> 10000 & A.GetPrice( )<15000)
 A.SHOW( );
 }
 fin.close( ) ;
 }

Question - 34 : -
Assuming the class VINTAGE as declared below, write a function in C++ to read the objects of VINTAGE from binary file VINTAGE.DAT and display those vintage vehicles, which are priced between 200000 and 250000.

class VINTAGE {
 int VNO; //Vehicle Number
 char VDesc[10]; //Vehicle Description
 float price;
 public:
 void GET( ) {cin>>VNO;gets(VDesc); cin>>Price;}
 void VIEW( )
 {
 cout<
 cout<
 cout<
 }
 float ReturnPrice ( ) {return Price;}
 };

Answer - 34 : -

void Disp( )
 {
 ifstream fin;
 fin.open ("VINTAGE.DAT", ios ::in| ios : : binary);
 vintage V; while (Ifin.eof())
 {
 fin.read ((char*)&V, sizeof(V));
 if (V.ReturnPrice( )> 200000 & &V.ReturnPrice( )<250000)
 V.VIEW ( );
 }
 fin.close ( );
 }

Question - 35 : -
Write a function in C++ to search for a camera from a binary file “CAMERA.DAT” containing the objects of class CAMERA (as defined below). The user should enter the model no and the function should search and display the details of the camera,

class CAMERA {
 long ModelNo;
 float Megapixel; int Zoom;
 char Details[120];
 public:
 void Enter(){cin>>ModelNo>>Megapixel >> Zoom;
 gets (Details);}
 void Display( )
 {cout<
 };

Answer - 35 : -

void cameraSearch ()
 {
 fstreamFIL;
 FIL.openCCAMERA.DAT", ios::binary|ios :: in );
 CAMERA B;
 long bn; int Found = 0;
 cout<<"Enter the Model Number"; cin >> bn;
 while (FIL.read((char*)&B, sizeof (B)))
 {
 if (FIL.GetModelNo() = = bn)
 {
 B.Display();
 Found ++;
 }}
 if(Found = = 0)
 cout<<"Sorry ! Camera not found"<
 FIL.close( );
 }

Question - 36 : -
Write a definition for function ONOFFER()in C++ to read each object of a binary file TOYS. DAT, find and display details of those toys, which have status as “ON OFFER”. Assume that the file Toys.DAT is created with the help of objects of class TOYS, which is defined below:

Class TOYS
 {
 int TID; char Toy[20], Status[20];
 float MRP;
 Public:
 void Getinstock()
 {
 cin>>TID;gets (Toy) ; gets (Status) ; cin»MRP:}
 void View()
 {
 Cout<
 }
 Char *SeeOffer() {Return Status;}
 } ;

Answer - 36 : -

Void ONOFFER ()
 {
 TOYS T;
 Ifstream fin;
 fin.open("TOYS.DAT", ios ::binary) ;
 while(fin.read((char*)&T,size (T) ))
 {
 if (strcmp(T.see offer(),"ON OFFER")= = 0)
 T.view();
 }
 fin.close();//Ignore
 }

Question - 37 : -
Find the output of the following C++ code considering that the binary file CLIENT.DAT exists on the hard disk with a data of 1000 clients :

Class CLIENT
 {
 int Ccode;char CName[20];
 public:
 void Register();void Display();
 };
 void main()
 {
 fstream CFile;
 CFile.Open ( "CLIENT. DAT",ios::binary|ios::in); CLIENT C;
 CFile. read ( (char*) & C , sizeof(C)) ;
 cout<< "Rec: "<
 CFile. read ( (char*) & C , sizeof*(C));
 CFile . read ( (char*) & C , sizeof(C));
 cout<<"Rec:"<
 CFile.close () ;
 }

Answer - 37 : -

Rec: 1
Rec: 3

Question - 38 : -
Write the definition of a function FixPay (float Pay[ ], int N) in C+ + , which should modify each element of the array Pay having N elements, as per the following rules :

Existing Salary Value

Required Modification in Value

If less than 1,00,000

Add 25% in the existing value

If >=1,00,000 and <20,000

Add 20% in the existing value

If >=2,00,000

Add 15% in the existing value

Answer - 38 : -

Void FixPay(float Pay[],int N)
{
for(int i=0;i
{
if(Pay[i]<100000)
Pay[i]+= Pay[i]*0.25;
else if(Pay[i]<200000)
Pay[i]+= Pay[i]*0.20; 
else
Pay[i]+= Pay[i]*0.15 ;
 }
}

Question - 39 : -
Write the definition of a member function INSERT() for a class QUEUE in C+ +, to remove a product from a dynamically allocated Queue of items considering the following code is already written as a part of the program.

Struct ITEM 
{
int INO; char INAME[20];
ITEM*Link;
};
class QUEUE 
{
ITEM *R,*F;
Public:
QUEUE(){R=NULL; F=NULL;}
void INSERT();
void DELETE();
~QUEUE();
};

Answer - 39 : -

Void QUEUE::INSER()
{
ITEM*newitem = new ITEM; 
Cout<<"enter item number";
cin>>newitem → INO;
Cout<<"Enter item name";  
gets(newitem → INAME); 
newitem → Link = NULL; 
if (R==NULL)
R=F=newitem;
else
{
 R → Link=newitem; 
 R = newitem;
}

Question - 40 : -
Write the output from the following C+ + program code :

#include
#include 
void strcon(char s[])
{
for(int i=0,l=0;s[i]!='\0';i++,l++); 
fortint j=0;j
{
if(isupper(s[j]))
s[j]=tolower(s[j])+2; 
else if( islower(s[j])) 
s[j]=toupper(s[j])-2; 
else
s[j] ='@';
 }
}
void main()
{
char *c="Romeo Joliet"; 
strcon(c);
cout<<"Text="<
c=c+3;
cout<<"New Text="<
c=c+5-2 ;
cout<<"last Text= "<
 }

Answer - 40 : -

Text = tMKCM@lMJGCR
New Text = KCM@1MJGCR
Last Text = 1MJGCR

×