Starting from:
$35

$29

Project 4 Functions Solution

1. Use Newton-Raphson iteration to calculate the square root of a
float given on the comand line. Try using N as a starting guess.
Print out the intermediate values on the way to convergence. You
should also check for non-convergence (like if the intermediate values
are beginning to diverge, so you don't loop forever or overflow the
stack). Implement the function 2 ways: first using iteration (i.e. a
loop), then re-implement the function using recursion. Print out both
results, and compare to the value determined by the built-in sqrt()
function in the <cmath library.

NewtonRaphsonSqrt 50
iter: 2, est: 50
iter: 4, est: 25.5
iter: 6, est: 13.7304
iter: 8, est: 8.68597
iter: 10, est: 7.22119
iter: 12, est: 7.07263
iter: 14, est: 7.07107
[iterative] sqrt(50)=7.07107

recursive estimate: 50
recursive estimate: 25.5
recursive estimate: 13.7304
recursive estimate: 8.68597
recursive estimate: 7.22119
recursive estimate: 7.07263
recursive estimate: 7.07107
[recursive] sqrt(50)=7.07107

[built-in] sqrt(50)=7.07107
[verifying] 7.07107^2 = 50


(Just for fun: try modifying the code to calculate cube-root of x,
or ln(x).)



2. Write a program that generates all subsets of a list of items given
on the command line. Make two versions of the program: an iterative
version (subsetsI), and a recursive version (subsetsR). Each version
should have a function that, given a vector of strings, returns a
vector of vectors containing all subsets, which your program should
then print out. (The order of lines, and of items within lines,
doesn't matter.)

(Hint: in this program, you will need to make copies of vectors.
This is easily done by assignment: "vector<string v2=v1;" makes a
copy of v1, so modifying v2 doesn't affect v1.)

subsetsR apples bananas grapes pears

apples
bananas
apples bananas
grapes
apples grapes
bananas grapes
apples bananas grapes
pears
apples pears
bananas pears
apples bananas pears
grapes pears
apples grapes pears
bananas grapes pears
apples bananas grapes pears

subsetsI A B C

A
B
A B
C
A C
B C
A B C



More products