Question: I have been assigned to make a function in C++ that will round off the given number based on the number of digits provided. I
I have been assigned to make a function in C++ that will round off the given number based on the number of digits provided. I have done almost everything correctly, but the only problem is that when I run the program and round the number 2.23128744 by the digit 5, I get 2.2312924 when I am actually supposed to get 2.2312900. I was told that I had to add two more digits to the rounded number and they are supposed to be zero but instead it just gives me the actual numbers. I tried using floor, but this will only make all of the decimals zeroes and its only supposed to be the last two decimals that I added with setprecision. All I need is to change the last two decimal digits to zero, everything else is correct.
This is the coding I have so far:
#include
using namespace std;
// Ignore this; it's a little function used for making tests inline void _test(const char* expression, const char* file, int line) { cerr << "test(" << expression << ") failed in file " << file; cerr << ", line " << line << "." << endl << endl; } // This goes along with the above function...don't worry about it #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))
// Insert here the prototype of the function here int round_off(double value, int decdig);
int main() { double value, valuero; // Declare variable value, valuero that hold double precision real numbers
int decdig; // Declare variable decdig that holds whole numbers
cout << "Enter the real number: "; // Prompt the user to "Enter the real number: "
cin >> value; // Read from keyboard the value entered by the user and assign it to value
cout << endl << "Enter number of digits: "; // Prompt the user to "Enter number of digits: "
cin >> decdig; // Read from keyboard the value entered by the user and assign it to decdig
valuero = round_off(value, decdig); // Round the real number to the number of decimal digits specified and assign the result to valuero
cout << fixed << setprecision(10); // Format the output to display the numbers in fixed format with ten decimal digits
cout << setw(23) << endl << "The original number is " << value << endl; // Display on the screen, using 23 columns, the message // "The original number is ", value
cout << fixed << setprecision(decdig + 2); // Format the output to display the numbers in fixed format with the number of decimal digits specified plus 2
// This is where I added the last two digits, but I couldn't change them to zero
cout << setw(23) << endl << "The rounded number is " << ((value * pow(10.0, decdig) + 0.5) / pow(10.0, decdig)) << endl << endl; // Display on the screen, using 23 columns, the message // "The rounded number is ", valuero
system("pause"); // Do NOT remove or modify the following statements cout << endl << "Testing your solution" << endl << endl; test(typeid(value) == typeid(1.)); // Incorrect data type used for value test(typeid(valuero) == typeid(1.)); // Incorrect data type used for valuero test(typeid(decdig) == typeid(1)); // Incorrect data type used for decdig /* Include these tests once the function has been implemented
test(fabs(round_off(125.123456789,2) - 125.12 ) < 0.001); // Incorrect rounding to two decimal digits test(fabs(round_off(125.123456789,4) - 125.1235) < 0.00001); // Incorrect rounding to four decimal digits test(fabs(round_off(125.987654321,0) - 126.) < 0.001); // Incorrect rounding to no decimal digits test(fabs(round_off(125.987654321, 5) - 125.98765) < 0.000001); // Incorrect rounding to five decimal digits */
system("pause");
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
