56.6k views
0 votes
Which of the following statements displays

basic = 500.00
1. cout << "basic = " << setw(4) << fixed<< setprecision(2) << 500;
2. cout << "basic = " << setw(10) << fixed<< setprecision(2) << 500;
3. cout << "basic = " << fixed << setprecision(2) << setw(10) << 500.000;
4. cout << "basic = " << fixed << setw(4) << 500;
5. None of the above

1 Answer

2 votes

Final answer:

Option 2 is the correct C++ statement to display 'basic = 500.00' with a field width of 10, fixed notation and two decimal places of precision.

Step-by-step explanation:

A student has asked which statement correctly displays basic = 500.00 when using C++ stream manipulators. The right answer is:

2. cout << "basic = " << setw(10) << fixed << setprecision(2) << 500;

the output field to 10 characters, ensuring that the output is properly aligned.

The other options either have incorrect precision settings or improper width settings, resulting in incorrect output.

This statement sets the field width to 10, fixes the decimal point notation with fixed, and sets the precision to show two decimal places. Since '500' is an integer, it will be displayed as '500.00' with these manipulators. Option 2 is the correct answer because it creates the needed padding for the output to comply with the specified format.

User Stender
by
7.4k points