For each value read, your program should print the original value, the number rounded to the nearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hun- dredth and the number rounded to the nearest thousandth.
Function floor can be used to round a number to a specific decimal
place. The statement
y = floor( x * 10 + .5 ) / 10;
rounds x to the tenths position (the first position to the right of the decimal point). The statement
y = floor( x * 100 + .5 ) / 100;
rounds x to the hundredths position (the second position to the right of the decimal point). Write
a program that defines four functions to round a number x in various ways:
a) roundToInteger( number )
b) roundToTenths( number )
c) roundToHundredths( number )
d) roundToThousandths( number )
```
// Round numbers to integers, tenths, hundredths and thousandths.
#include
#include
#include
using namespace std;
double roundToInteger( double ); // function prototype
double roundToTenths( double ); // function prototype
double roundToHundredths( double ); // function prototype
double roundToThousandths( double ); // function prototype
int main()
{
int count; // number of values to process
double number; // current input
cout << "How many numbers do you want to process? " << fixed;
cin >> count;
// loop for inputs
for ( int i = 0; i < count; i++ )
{
cout << "\nEnter number: ";
cin >> number;
// display number rounded to nearest integer
cout << setprecision( 6 ) << number
<< " rounded to the nearest integer is: "
<< setprecision( 0 ) << roundToInteger( number ) << '\n';
// display number rounded to nearest tenth
cout << setprecision( 6 ) << number
<< " rounded to the nearest tenth is: "
<< setprecision( 1 ) << roundToTenths( number ) << '\n';
// display number rounded to nearest hundredth
cout << setprecision( 6 ) << number
<< " rounded to the nearest hundredth is: "
<< setprecision( 2 ) << roundToHundredths( number ) << '\n';
// display number rounded to nearest thousandth
cout << setprecision( 6 ) << number
<< " rounded to the nearest thousandth is: "
<< setprecision( 3 ) << roundToThousandths( number ) << '\n';
} // end for
} // end main
// roundToInteger rounds n to nearest integer
double roundToInteger( double n )
{
return floor( n + .5 );
} // end function roundToInteger
// roundToTenths rounds n to nearest tenth
double roundToTenths( double n )
{
return floor( n * 10 + .5 ) / 10;
} // end function roundToTenths
// roundToHundredths rounds n to nearest hundredth
double roundToHundredths( double n )
{
return floor( n * 100 + .5 ) / 100;
} // end function roundToHundredths
// roundToThousandths rounds n to nearest thousandth
double roundToThousandths( double n )
{
return floor( n * 1000 + .5 ) / 1000;
} // end function roundToThousandths
```
You might also like to view...
When inserting footnotes in an MLA-styled paper, it is not necessary to change the default style to agree with the MLA style
Indicate whether the statement is true or false
____ are blue dotted lines that show the boundaries of cells, but do not print.
A. Gridlines B. Borders C. Guidelines D. Cell lines
Data sources created in Word are saved in ____ format.
A. Microsoft Office Data Source B. Microsoft Office Address Lists C. Microsoft Office Mail Merge D. Microsoft Office Record Lists
Validate your tools and verify your evidence with ____ to ensure its integrity.
A. hashing algorithms B. watermarks C. steganography D. digital certificates