C++ Revision Tour

Question

Write a definition for function Economic() in C++ to read each record of a binary file ITEMS.DAT, find and display those items, which costs less than 2500. Assume that the file ITEMS.DAT is created with the help of objects of class ITEMS, which is defined below:

class ITEMS
{
	int ID;char GIFT[20]; float Cost;
public:
	void Get()
	{
		cin>>CODE;gets(GIFT);cin>>Cost;
	}
	void See()
	{
		cout<<ID<<':'<<GIFT<<':'<<Cost<<endl;
	}
	float GetCost()
	{
		return Cost;
	}

};

Answer

void Economic()
{
	ITEMS I;
	ifstream fin('ITEMS.DAT',ios::binary);{

	while (fin.read((char *)&I,sizeof(I)))
	{
		if(I.GetCost()<2500)
			I.See();
	}
	}
	fin.close();
}

Sponsor Area

Some More Questions From C++ Revision Tour Chapter

Aditi has used a text editing software to type some text. After saving the article as WORDS.TXT, she realised that she has wrongly typed alphabet J in place of the alphabet I everywhere in the article.
Write a function definition for JTOI() in C++ that would display the corrected version of the entire content of the file WORDS.TXT with all the alphabets “J” to be displayed as an alphabet 'I' on screen.
Note: Assuming that WORD.TXT does not contain any J alphabet otherwise.
Example: If Aditi has stored the following content in the file WORDS.TXT :

'WELL, THJS JS A WORD BY JTSELF. YOU COULD STRETCH THJS TO BE A SENTENCE'

The function JTOI() should display the following content:

'WELL, THIS IS A WORD BY ITSELF. YOU COULD STRETCH THIS TO BE A SENTENCE'

Write a definition for function COUNTDEPT( ) in C++ to read each object of a binary file TEACHERS.DAT, find and display the total number of teachers in the department MATHS. Assume that the file TEACHERS.DAT is created with the help of objects of class TEACHERS, which is defined below:

class TEACHERS
{
	int TID; char DEPT[20];
public:
	void GET()
	{
		cin>>TID;gets(DEPT);
	}
	void SHOW()
	{
		cout<<TID<<':'<<DEPT<<endl;
	}
	char *RDEPT(){return DEPT;}
};

Find the output of the following C++ code considering that the binary file BOOK.DAT exists on the hard disk with a data of 200 books.

class BOOK
{
	int BID;char BName[20];
public:
	void Enter();void Display();
};

void main()
{
	fstream InFile;
	InFile.open('BOOK.DAT',ios::binary|ios::in);
	BOOK B;
	InFile.seekg(5*sizeof(B));
	InFile.read((char*)&B, sizeof(B));
	cout<<'Book Number:'<<InFile.tellg()/sizeof(B) + 1;
	InFile.seekg(0,ios::end);
	cout<<' of '<<InFile.tellg()/sizeof(B)<<endl;
	InFile.close();
}

Write the type of C++ tokens (keywords and user defined identifiers) from the following:
(i) new
(ii) While
(iii) case
(iv) Num_2

Anil typed the following C++ code and during compilation, he found three errors as follows:
(i) Function strlen should have prototype
(ii) Undefined symbol cout
(iii) Undefined symbol endl
On asking, his teacher told him to include necessary header files in the code.Write the names of the header files, which Anil needs to include, for successful compilation and execution of the following code.

{
	char Txt[] = 'Welcome';
	for(int C= 0; C<strlen(Txt); C++)
		Txt[C] = Txt[C]+1;
	cout<<Txt<<endl;
}

Rewrite the following C++ code after removing any/all syntactical errors with each correction underlined.
Note: Assume all required header files are already being included in the program.

void main()
{
	cout<<'Enter an Alphabet:';
	cin>>CH;
	switch(CH)
	case 'A' cout<<'Ant'; Break;
	case 'B' cout<<'Bear' ; Break;
}

Find and write the output of the following C++ program code:
Note: Assume all required header files are already included in the program.

#define Diff(N1,N2) ((N1>N2)?N1-N2:N2-N1)
void main()
{
	int A,B,NUM[] = {10,23,14,54,32};
	for(int CNT =4; CNT>0; CNT--)
	{
		A=NUM[CNT];
		B=NUM[CNT-1];
		cout<<Diff(A,B)<<'#';
	}
}

Find and write the output of the following C++ program code:
Note: Assume all required header files are already being included in the program.

void main()
{
	int *Point, Score[]={100,95,150,75,65,120};
	Point = Score;
	
	for(int L = 0; L<6; L++)
	{
		if((*Point)%10==0)
			*Point /= 2;
		else
			*Point -= 2;
		if((*Point)%5==0)
			*Point /= 5;
		Point++;
	}

	for(int L = 5; L>=0; L--){
		cout<<Score[L]<<'*';
	}
}

Look at the following C++ code and find the possible output(s) from the options (i) to (iv) following it. Also, write the maximum values that can be assigned to each of the variables N and M.

Note:
● Assume all the required header files are already being included in the code.
● The function random(n) generates an integer between 0 and n-1

void main()
{
	randomize();
	int N=random(3),M=random(4);
	int DOCK[3][3] = {{1,2,3},{2,3,4},{3,4,5}};
	for(int R=0; R<N; R++)
	{
		for(int C=0; C<M; C++)
			cout<<DOCK[R][C]<<' ';
		cout<<endl;
	}
}
(i)  (ii) 
1 2 3
2 3 4
3 4 5
1 2  3   
2 3  4
(iii) (iv)
1 2 
2 3
1 2 
2 3
3 4

Polina Raj has used a text editing software to type some text in an article. After saving the article as MYNOTES.TXT, she realised that she has wrongly typed alphabet K in place of alphabet C everywhere in the article.

Write a function definition for PURETEXT() in C++ that would display the corrected version of the entire article of the file MYNOTES.TXT with all the alphabets “K” to be displayed as an alphabet “C” on screen.

Note: Assuming that MYNOTES.TXT does not contain any C alphabet otherwise.
Example:

If Polina has stored the following content in the file MYNOTES.TXT:

I OWN A KUTE LITTLE KAR.
I KARE FOR IT AS MY KHILD.
The function PURETEXT() should display the following content:

I OWN A CUTE LITTLE CAR.
I CARE FOR IT AS MY CHILD.