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(oldValue + 0.5). You will need further manipulation to round at other decimal places. This logic will only work with positive values. You may assume that we will only work on positive values in this project. you may also assume that no overflow will happen during the calculation.

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!