HOMEWORK 2 One financial planner


Rate this product


NOTE: Homework will be graded on using g++ 5.3.0 with
-std=c++14. You are free to develop your programs on Visual C++ or any other platform, but it is your responsibility to make sure your programs also compile and execute correctly on using g++ -std=c++14.

NOTE: Every program from now on must include at least the exception-handling
framework on page 152! If you add another catch clause, it must precede all
more general clauses; for example, if you add catch(Bad_area) it should come
before the other two catch clauses.

The grade for this lab will be based on style (formatting, variable names, comments, etc.), syntax (no compilation or link errors), and correctness (passes all test cases). Follow the style guide at

Your grade for this lab is:
Problem # 1 2 3 4
Style /2 /4 /4 /2
Syntax /3 /6 /6 /3
Correctness /5 /10 /10 /5
Total /10 /20 /20 /10
Grand total _____/50

1. (10 points) One financial planner recommends dividing your income into the
following budget categories:
Give away 10% (e.g., to some cause you are committed to, or to help
Save 10% (e.g., for unexpected expenses or planned large purchases)
Live on 80%

Write a program named hw2pr1.cpp which repeatedly asks for an income amount in dollars and prints out the budget amounts in dollars and cents. Round off amounts to the nearest cent. A sample run should look like this:

Income in dollars? 1000
You should give away about $100.00, save about $100.00, and live on about $800.00.
Income in dollars? 2015.37
You should give away about $201.54, save about $201.54, and live on about $1612.30.

Hint: Look up setprecision, so your program doesn’t print $201.537. Note: Don’t worry if the rounded-off amounts don’t add up to exactly the income amount; that’s why the output says “about $201.54,” etc.

2. (20 points) Write your own cube root function named double my_cbrt_1(double n) using the following approximation:
∛n ≈ -0.411665 n2 + 1.03455 n + 0.377113

and then write a main which prints n, cbrt(n), and my_cbrt_1(n) for n =
π times 10 to the kth power for k = -100, -10, -1, 0, 1, 10, and 100. Use this C++11 code:
for(auto k : {-100, -10, -1, 0, 1, 10, 100}){
n = M_PI * pow(10.0, k);
//cout goes here
Name your program hw2pr2.cpp. Note: The formula above is an approximation and so for some values of n it will not be very accurate!

Note: On Visual Studio or Xcode you may need to add a line like


at the top of your code to activate M_PI.

3. (20 points) Modify problem 2 to also print the relative error as a per cent, by adding a column relative_error_per_cent = 100 * ((my_cbrt_1(n) – cbrt(n)) / cbrt(n). Line up the columns using setw(), etc. Name your program hw2pr3.cpp.


4. (10 points) Modify problem 3 to be more accurate by defining a function
double my_cbrt_2(double n) using this pseudocode:
Set result to 1.
If n 1 then multiply result by 2 and divide n by 8; repeat until n  1.
If n < ⅛ then divide result by 2 and multiply n by 8; repeat until n  ⅛.
Return result * my_cbrt_1(n) as the answer.

The main should print n, cbrt(n), my_cbrt_2(n) and the relative error of my_cbrt_2(n) for the same values of n. Name your program hw2pr4.cpp.

Open chat
Need help?
Can we help you?