Sale!

CSCE 121 HOMEWORK 2 solved

$35.00 $21.00

Category:

Description

5/5 - (1 vote)

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
others)
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.411665n2+ 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

#define _USE_MATH_DEFINES

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.

OPTIONAL EXTRA CREDIT

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.