Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This assignment is for c++ and I need help creating the function that will round the number to the number of decimal digits given. I

This assignment is for c++ and I need help creating the function that will round the number to the number of decimal digits given. I know how to round off with the given number of digits, but I am not sure how to create a function that can round off with different number of digits. Below are the instructions and the coding I have done so far:

You will be have to round numbers very often so you decided to create your own function round_off() that will receive the number to be rounded and the number of decimal digits that the number should be rounded to and will return the value rounded to the specified number of decimal digits. You need to create a program to test the function. It will ask the user to enter the double precision real number to be rounded and a whole number indicating the number of decimal digits. It will then display the original number with ten digits and the rounded value (also a double precision real number) with the number of digits specified by the user plus 2 more. This assignment will be completed in two steps: 1) First you will implement the algorithm shown below in which the rounding will be done in main() 2) Once you have this working you will need to modify your solution so you: Declare the prototype of the function above main() Call the function in main() to do the rounding Define the function below main()

The coding I have so far:

#include // to use cin and cout #include // to be able to use operator typeid #include // Include here the libraries that your program needs to compile

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 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 << "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 = floor((valuero * 100.0) + 0.5 / 100.0; // 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) << "The original number is " << value; // Display on the screen, using 23 columns, the message // "The original number is ", value

cout << fixed; decdig + 2; // Format the output to display the numbers in fixed format with the number of decimal digits specified plus 2

cout << setw(23) << "The rounded number is " << valuero; // 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; }

//************************ Function definition ************************* // Read the handout carefully for detailed description of the functions that you have to implement

// Rounds the value received in the first parameter to the number of digits received in the second parameter

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions