Question
C++ / PuTTY / WinSCP Problem 1: Round to certain decimal places In scientific computation, you sometimes need to manipulate the precision of a real
C++ / PuTTY / WinSCP
Problem 1: Round to certain decimal places
In scientific computation, you sometimes need to manipulate the precision of a real number so it has the desired precision. For example, when you add 3.9 to 2.16 in physics or chemistry, your result should have one significant figure (decimal places after the decimal point) and you will need to round the result to the first decimal places. Thus, you will get 6.1 as the result.
In this project you need to write a program to round a given real value to certain decimal places and show the result.
Sample run
Please input the value you want to round: 5.2346 Please input the number of decimal places to round to: 2 Your round value is: 5.23
WARNING: It is not correct to only display with certain number of decimal places. The change of the value is required. If you force display more decimal places in the example, you should see 5.230000.
To round a number at the decimal point in C++, you can add 0.5 to the double value and convert it to int and then cast back as a double value. double value = static_cast
Put all logics in your main function and store in a single file. Name your file round.cpp! Incorrect naming will cause the compilation using make to fail! The file will be compiled using the make command make round to an executable named round, and you can run it using ./round to test.
Hint:
- Move the decimal point, round, and move the decimal points back.
- How to move the decimal points back and forth?
- To keep the project simple, no validation of input values is required.
Problem 2: Modular scientific computational program
Now let's make a modular solution to the last problem. We will create a small math library with one function called roundToDecimal. The function prototype looks like: double roundToDecimal(double value, int numDecPlaces);
Create a pair of file called my-math.hpp and my-math.cpp and put the function into these two files correctly.
Create another driver program called main.cpp to use the function from the my-math library and drive the run.
Your program should run exactly the same way as that in the problem 1.
Your program will be compiled using the make command make main to a program called main and you can test run with ./main command.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started