C++ Revision Tour

Question

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.

#define Equation(p,q) = p+2*q
void main()
{
	float A=3.2;B=4.1;
	C=Equation(A,B);
	cout<<'Output='<<C<<endl;
}

Answer

#define Equation(p,q) p+2*q
  void main()
{
       float A=3.2 , B=4.1;
       float C=Equation(A,B);
       cout<< ”Output=” <<C<<endl;
}

Sponsor Area

Some More Questions From C++ Revision Tour Chapter

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.

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

class PHOTOS
{
	int PCODE;
char PTYPE[20];	//Photo Type as “PORTRAIT”,”NATURE”
public:
	void ENTER()
	{
		cin>>PCODE;gets(PTYPE);
	}
	void SHOWCASE()
	{
		cout<<PCODE<<':'<<PTYPE<<endl;
	}
	char *GETPTYPE(){
		return PTYPE;
	}
};

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

class CLIENTS
{
	int CCode;char CName[20];
public:
	void REGISTER();
	void DISPLAY();
};
void main()
{
	fstream File;
	File.open('CLIENTS.DAT', ios::binary | ios::in);
	CLIENTS C;
	File.seekg(6*sizeof(C));
	File.read((char*)&C, sizeof(C));
	cout<<'Client Number:'<<File.tellg()/sizeof(C) + 1;
	File.seekg(0, ios::end);
	cout<<' of '<<File.tellg()/sizeof(C)<<endl;
	File.close();
}

Out of the following, find those identifiers, which cannot be used for naming Variable, Constants or Functions in a C++ program:

_Cost, Price*Qty, float, Switch,
Address One, Delete, Number12, do