ssgopalganj.inssgopalganj.in/documents/studymaterial/qno4.docx  · web viewvalues of book new...

22
Q.No. 4(A) 01. (a) Observe the program segment given below carefully and fill the blanks marked as Line 1 and Line 2 using fstream functions for performing the required task. #include<fstream.h> class Stock { long Ino; // Item Number char Item[20]; // Item Name int Qty; // Quantity public: void Get(int); Get(int);// Function to enter the content void Show( ); // Function to display the content void Purchase(int Tqty) { Qty+ = Tqty; // Function to increment in Qty } long KnowIno( ) { return Ino; } }; void Purchaseitem(long PINo, int PQty) // PINo -> Info of the item purchased // PQty -> Number of items purchased { fstream file; File.open(“ITEMS.DAT”,ios::binary|ios::in|ios::cut); int Pos=-1; Stock S; while (Pos== -1 && File.read((char*)&S, sizeof(S))) if (S.KnowInc( ) == PINo) { S.Purchase(PQty); // To update the number of items Pos = File.tellg()- sizeof(S);

Upload: hoangque

Post on 04-Mar-2018

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

Q.No. 4(A)

01. (a) Observe the program segment given below carefully and fill the blanks marked asLine 1 and Line 2 using fstream functions for performing the required task.

#include<fstream.h>class Stock{long Ino; // Item Numberchar Item[20]; // Item Nameint Qty; // Quantitypublic:void Get(int);Get(int);// Function to enter the contentvoid Show( ); // Function to display the contentvoid Purchase(int Tqty){Qty+ = Tqty; // Function to increment in Qty}long KnowIno( ){ return Ino;}};void Purchaseitem(long PINo, int PQty)// PINo -> Info of the item purchased// PQty -> Number of items purchased{fstream file;File.open(“ITEMS.DAT”,ios::binary|ios::in|ios::cut);int Pos=-1;Stock S;while (Pos== -1 && File.read((char*)&S, sizeof(S)))if (S.KnowInc( ) == PINo){S.Purchase(PQty); // To update the number of itemsPos = File.tellg()- sizeof(S);//Line 1 : To place the file pointer to the required position______________________________________;//Line 2 : To write the objects on the binary file______________________________________;}if (Pos == -1)cout<<“No updation done as required Ino not found...”;File.close( );} 2009

Page 2: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

02.(a) Observe the program segment given below carefully and fill the blanksmarked as Statement 1 and Statement 2 using seekg( ), seekp( ), tellp( ) and tellg()functions for performing the required task.

# include <fstream.h>class PRODUCT{int Pno; char pname [20]; int qty;public :void ModifyQty();// The function is to modify quantity of a PRODUCT};void PRODUCT :: ModifyQty ( ){fstream Fil;Fil.open(“PRODUCT.DAT”,ios::binary | ios::in | ios::out);int MPno;cout<<“Product No to modify quantity :”; cin>>MPno;while( Fil.read ((char*) this, sizeof (PRODUCT))){if (MPno ==Pno){cout<<“present quantity:” <<Qty<<endl;cout<<“changed quantity:”; cin>>Qty;int position = ________; //Statement 1___________________; //Statement 2Fil.write ((char*) this, sizeof (PRODUCT)); // Re-writing the Record}}Fil.close( );} 2011

03.(a) Write name of two member functions belonging to fstream class. 1998

04.(a) Differentiate between read() & write() functions. 1999

05.(a) Name two member functions of ofstream class. 2000

06. (a) Distinguish between serial files and sequential files. 2001

07. (a) What is the difference between get() and read() ? 2002

Page 3: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

08. (a) Assuming that a text file named FIRST.TXT contains some text written into it,write a function named vowelwords( ), that reads the file FIRST.TXT and createsa new file named SECOND.TXT, to contain only those words from the fileFIRST.TXT which start with a lowercase vowel (i.e. with ‘a’, ‘e’, ‘i’, ‘o’, ‘u’). Forexample if the file FIRST.TXT contains Carry umbrella and overcoat when it rainsThen the file SECOND.TXT shall contain umbrella and overcoat it 200409.(a) Observe the program segment given below carefully, and answer the question thatfollows:class Book{int Book no;char Book_name[20];public://function to enter Book detailsvoid enterdetails();// function to display Book detailsvoid showdetails();//function to return Book_noint Rbook_no (){return Book_no;}} ;void Modify(Book NEW){fstream File;File.open(“BOOK.DAT”,ios::binary|ios::in|ios::out);Book OB;int Recordsread = 0, Found = 0;while (!Found && File.read((char*)&OB, sizeof (OB))){Recordsread ++ ;if (NEW.RBook_no() = = OB.RBook_no)){______________ //Missing StatementFile.write((char*)&NEW, sizeof (NEW));Found = 1;}elseFile.write((char*)&OB, sizeof(OB));}if (! Found)cout<<" Record for modification does not exist”;File.close();}

Page 4: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

If the function Modify( ) is supposed to modify a record in file BOOK.DAT with thevalues of Book NEW passed to its argument, write the appropriate statement forMissing Statement using seekp( ) or seekg( ), whichever needed, in the above codethat would write the modified record at its proper place. 2005

10. (a) Observe the program segment given below carefully, and answer the questionthat follows:class PracFile{intPracno;char PracName[20];int TimeTaken;int Marks;public:// function to enter PracFile detailsvoid EnterPrac( );// function to display PracFile detailsvoid ShowPrac( ):// function to return TimeTakenint RTime() {return TimeTaken;}// function to assign Marksvoid Assignmarks (int M){ Marks = M;}};void AllocateMarks( ){ fstream File;File.open(“MARKS.DAT”,ios::binary|ios::in|ios::out);PracFile P;int Record = 0;while (File.read(( char*) &P, sizeof(P))){if(P.RTime()>50)P.Assignmarks(0)elseP.Assignmarks(10)______________ //statement 1______________ //statement 2Record + + ;}File.close();}If the function AllocateMarks () is supposed to Allocate Marks for the recordsin the file MARKS.DAT based on their value of the member TimeTaken.Write C++ statements for the statement 1 and statement 2, where,statement 1 is required to position the file write pointer to an appropriate placein the file and statement 2 is to perform the write operation with the modifiedrecord . 2007

Page 5: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

11.(a).Observe the program segment given below carefully and fill in the blanksmarked as Statement 1 and Statement 2 using tellg ( ) and skeep ( ) functions forperforming the required task.

#include <fstream. h>class c1ient{long Cno ; char Name [20]; Email [30] ;public ://Function to allow user to enter the cno,Nme , Email.void Enter ( ) ;// Function to allow user to enter (modify) Email.void modify ( ) ;long ReturnCno( ) { return Cno ; }} ;void changeEmail ( ){ Client C ;fstream F ;F. open (“INFO.DAT” , ios :: binary |ios :: in|ios :: out);long Cnoc ; //Client’s no. whose Email needs to be changedcin >> Cnoc ;while (F. read (( char*) &C, sizeof (C))){if (Cnoc = = C.Returncno( )){C.Modify( ) ;//Statement 1int Pos = __________//To find the current position of//file pointer//statement 2_________________ //To move the file pointer to write//the modified record back into the//file for the desired cnocF.write ((char*) &C, sizeof(C));}}F.close( ) ;} 2010

Page 6: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

12.(a)Observe the program segment given below carefully and fill the blanks marked as Statement 1 and Statement 2 using seekg() and tellg() functions for performing the required task.

#include <fstream.h> class Employee { int Eno;char Ename[20]; public: //Function to count the total number of records int Countrec(); }; int Item::Countrec() { fstream File; File.open(“EMP.DAT”,ios::binary|ios::in); ______________________ //Statement 1 int Bytes = ______________________ //Statement 2 int Count = Bytes;sizeof(Item); File.close();

return Count; } (sp)

13. (a) Observe the program segment given below carefully and fill the blanks marked as Statement 1 and Statement 2 using seekp() and seekg() functions for performingthe required task.#include <fstream.h>class Item{int Ino;char Item[20];public://Function to search and display the content from a particular//record numbervoid Search(int );//Function to modify the content of a particular record numbervoid Modify(int);};void Item::Search(int RecNo){fstream File;File.open(“STOCK.DAT”,ios::binary|ios::in);______________________ //Statement 1File.read((char*)this,sizeof(Item));cout<<Ino<<”==>”<<Item<<endl;File.close();}

Page 7: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

void Item::Modify(int RecNo){fstream File;File.open(“STOCK.DAT”,ios::binary|ios::in|ios::out);cout>>Ino;cin.getline(Item,20);______________________ //Statement 2File.write((char*)this,sizeof(Item));File.close();} (sp)

14.(a) Observe the program segment given below carefully and fill the blanks marked asStatement 1 and Statement 2 using seekp() and seekg() functions for performingthe required task. #include <fstream.h>class Item{int Ino;char Item[20];public://Function to search and display the content from a particular//record numbervoid Search(int );//Function to modify the content of a particular record numbervoid Modify(int);};void Item::Search(int RecNo){fstream File;File.open(“STOCK.DAT”,ios::binary|ios::in);______________________ //Statement 1File.read((char*)this,sizeof(Item));cout<<Ino<<”==>”<<Item<<endl;File.close();}void Item::Modify(int RecNo){fstream File;File.open(“STOCK.DAT”,ios::binary|ios::in|ios::out);cout>>Ino;cin.getline(Item,20);______________________ //Statement 2File.write((char*)this,sizeof(Item));File.close(); (s.p)

Page 8: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

Q.No. 4(B)

01.(b) Assuming the class FLOPPYBOX. write functions in C++ to perform the following: (i) Write the object of FLOPPYBOX to a binary file.(ii) Read objects of FLOPPYBOX from the binary and display on the screen

class FLOPPYBOX{int size;char name[10];public :void getdata(){cin >> size;gets (name);}void showdata (){cout << size << " " << name <<endl;}}; 1998

02.(b) Write a function COUNT_DO( ) in C++ to count the presence of a word „do‟ in atext file “MEMO.TXT”. Example :If the content of the file “MEMO.TXT” is as follows:I will do it, if you request me to do it.It would have been done much earlier.The function COUNT_DO( ) will display the following message:Count of -do- in flie: 2009

03.(b) Write a function in C++ to count the no. of “Me” or “My” words present in atext file “DIARY.TXT”. If the file “DIARY.TXT” content is as follows :My first book was Me and My family. It gave me chance to be known to the world.The output of the function should be Count of Me/ My in file : 201104.(b) Assuming the class EMPLOYEE given below, write functions in C++ to perform following: (i) Write the objects of EMPLOYEE to a binary file. (ii) Read the objects of EMPLOYEE from binary file and display them on screen.

Page 9: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

class EMPLOYEE { int ENO; char ENAME[10]; public : void GETIT() { cin >> ENO; gets (ENAME); } void SHOWIT() { cout <<ENO << ENAME ;cout<<endl; } }; 1998

05.(b) Assuming the class FLOPPYBOX. write functions in C++ to perform the following: (i) Write the object of FLOPPYBOX to a binary file. (ii) Read objects of FLOPPYBOX from the binary and display on the screen. class FLOPPYBOX { int size; char name[10]; public : void getdata() { cin >> size; gets (name); } void showdata () { cout << size << " " << name ;cout<<endl; } }; 1999

06.(b) Assuming the class? DRINKS defined below, write functions in C++ to perform the following: i. Write the objects of DRINKS to a binary files.ii. Read the objects of DRINKS from binary file and display them on the screen when DNAME has value "INDY COLA".

Page 10: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

class DRINKS { int DCODE; char DNAME[13]; // Name of the drink int DSIZE; // Size in liters float DPRICE; public: void GETDRINKS() { cin >> DCODE >> DNAME >> DSIZE >>DPRICE; } void SHOWDRINKS () { cout<< DCODE << DNAME << DSIZE << DPRICE << endl; } char *GETNAME () { return DNAME; } }; 2000

07.b) Consider the class declaration: class BUS { int bus_no; char destination[20]; float distance; public : void Read(); // To read an object from the keyboard void Write (); // To write an object into a file void Show (); // To display the file contents on The monitor }; Complete the member functions definitions. 2001

08.b) Write a C++ program, which reads one line at a time from the disk file TEST.TXT, and displays it to a monitor. Your program has to read all the contents of the file. Assume the length of the line not to exceed 80 characters. You have to include all the header files if required. 2002

09.(b) Assuming the class Computer as follows : class computer{char chiptype[10];

Page 11: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

int speed;public:void getdetails(){gets(chiptype);cin>>speed;}void showdetails(){cout<<“Chip”<<chiptype<<“ Speed= “<<speed;}};Write a function readfile( ) to read all the records present in an already existingbinary file SHIP.DAT and display them on the screen, also count the number ofrecords present in the file. 2004

10.b) Write a function in C++ to count and display the number of lines starting withalphabet ‘A’ present in a text file “LINES.TXT”. Example:If the file “LINES.TXT” contains the following lines,A boy is playing there.There is a playground.An aeroplane is in the sky.Alphabets and numbers are allowed in the password.The function should display the output as 3. 2005

11.(b) Write afunction in C++ to print the count of the word is as an independent wordin at text file DIALOGUE.TXT. 2For example, if the content of the file DIALOGUE. TXT isThis is his book. Is this book good?Then the output of the program should be 2. 2007

12.(b) Write afunction in C++ to print the count of the word is as an independent wordin at text file DIALOGUE.TXT.

For example, if the content of the file DIALOGUE. TXT isThis is his book. Is this book good?Then the output of the program should be 2. 2010

12. (b) Write a function in C++ to read the content of a text file “DELHI.TXT” and diaplay all those lines on screen, which are Either starting with ‘D’ or starting with ‘M’. 2012

Page 12: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

13.(b) Write a function in C++ to count the number of alphabets present in a text file “NOTES.TXT”. S P 1

14.(b) Write a function in C++ to count the number of lines present in a text file“STORY.TXT”. S P 2

15.(b) void CountLine(){ifstream FIL("STORY.TXT");int LINES=0;char STR[80];while (FIL.getline(STR,80))LINES++;cout<<"No. of Lines:"<<LINES<<endl;f.close();}(½ Mark for opening STORY.TXT correctly)(½ Mark for initializing a counter variable as 0)(½ Mark for correctly reading a line from the file)(½ Mark for correctly incrementing the counter) S P 3

Page 13: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

Q.No. 4(C)

1. (c) Write a function in C++ to search for a laptop from a binary file“LAPTOP.DAT” containing the objects of class LAPTOP (as defined below).The user should enter the Model No and the function should search anddisplay the details of the laptop. class LAPTOP{long ModelNo;float RAM, HDD;char Details[120];public:void StockEnter ( ) {cin>>Model>>No>>RAM>>HDD; gets(Details);}void StockDisplay( ){cout<<ModelNo<<RAM<<HDD<<Details<<endl;}long ReturnModelNo ( ) { return ModelNo ;}}; 2011

2. (c) Write a function in C++ to read and display the detail of all the users whose statusis „A‟ (i.e. Active) from a binary file “USER.DAT”. Assuming the binary file“USER.DAT” is containing objects of class USER, which is defined as follows:class USER{int Uid; // User Idchar Uname[20]; // User Namechar Status; // User Type: A Active I Inactivepublic:void Register( ); // Function to enter the contentvoid show( ); // Function to display all data memberschar Getstatus( ){return Status;}}; 2009

Page 14: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

3. (c) Given a binary file STUDENT.DAT, containing records of the following classStudent type.class Student{char S_Admno[lO]; //Admission number of studentchar S_Name[30]; //Name of studentint Percentage; //Marks Percentage of studentpublic:void EnterData(){gets(S_Admno);gets(S_Name);cin>>Percentage;}void DisplayData(){cout<<setw(12)<<S_Admno;cout<<setw(32)<<S_Name;cout<<setw(3)<<Percentage<<endl;}int ReturnPercentage(){return Percentage;}};Write a function in C++, that would read contents of file STUDENT.DAT and displaythe details of those Students whose Percentage is above 75. 2005

4.(c) Given a binary file GAME.DAT, containing records of the following structureType.struct Game{char GameName [20];char Participant [10] [30];};Write a function in C++ that would read contents from the file GAME.DAT andcreates a file named BASKET.DAT copying only those records from GAME.DATwhere the game name is “Basket Ball” 2007

5.(c) Write a function in C++ to search and display the details of all flights, whosedestination is “Mumbai” from “FLIGHT.DAT”. Assuming the binary file iscontaining objects of class. class FLIGHT{int Fno; //Flight Numberchar From[20] ; //Flight Starting pointchar To[20] ; //Flight Destination

Page 15: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

public :char* GetFrom( ) {return From ;}char* GetTo( ) {return To ;}void Enter( ) {cin >> Fno ; gets (From) ;gets(To) ; }void Display( ) { cout << Fno<< “:” << From << “:” << To << endl ;}}; 2010

6.(c) Write a function in C++ to add new objects at the bottom of a binary file “STUDENT.DAT”, assuming the binary file is containing the objects of the following class. class STUD { int Rno;char Name[20];public: void Enter(){ cin>>Rno;gets(Name);} void Display(){ cout<<Rno<<Name<<endl;} }; void Addnew() { fstream FIL; FIL.open(“STUDENT.DAT”,ios::binary|ios::app); STUD S; char CH; do { S.Enter(); FIL.write((char*)&S,sizeof(S));

Page 16: ssgopalganj.inssgopalganj.in/Documents/studymaterial/Qno4.docx  · Web viewvalues of Book NEW passed to its ... Write afunction in C++ to print the count of the word is as ... Write

cout<<”More(Y/N)?”;cin>>CH; } while(CH!=’Y’); FIL.close(); } SP1

7.(c) Write a function in C++ to search for a BookNo from a binary file “BOOK.DAT”,assuming the binary file is containing the objects of the following class.class BOOK{int Bno;char Title[20];public:int RBno(){return Bno;}void Enter(){cin>>Bno;gets(Title);}void Display(){cout<<Bno<<Title<<endl;}}; SP2

8.(c) Write a function in C++ to search for a BookNo from a binary file "BOOK.DAT",assuming the binary file is containing the objects of the following class.class{int Bno;char Title[20];public:int RBno(){return Bno;}void Enter(){cin>>Bno;gets(Title);}void Display(){cout<<Bno<<Title<<endl;}}; SP3