(Rounding Numbers) An application of function floor is rounding a value to the nearest integer. The statement y = floor( x + .5 ); rounds the number x to the nearest integer and assigns the result to y. Write a program that reads several numbers and uses the preceding statement to round each of these numbers to the nearest integer. For each number processed, print both the original number and the rounded number.



What will be an ideal response?

```
// Rounding numbers using floor.
#include
#include
#include
using namespace std;

double roundToIntegers( double ); // function prototype

int main()
{
double x; // current input
double y; // current input rounded

cout << fixed; // set floating-point number format

// loop for 5 inputs
for ( int loop = 1; loop <= 5; loop++ )
{
cout << "Enter a number: ";
cin >> x;

y = roundToIntegers( x ); // y holds rounded input
cout << setprecision( 6 ) << x << " rounded is "
<< setprecision( 1 ) << y << endl;
} // end for
} // end main

// roundToIntegers rounds 5 inputs
double roundToIntegers( double value )
{
return floor( value + .5 );
} // end function roundToIntegers
```

Computer Science & Information Technology

You might also like to view...

In a letter, ________ prevents you from having to press Enter at the end of each line

A) wordwrap B) AutoWrap C) AutoReturn D) wraptext

Computer Science & Information Technology

The components of the governance process are called "____" because they enforce specific outcomes.

A. controls B. risks C. gates D. managers

Computer Science & Information Technology