Question: This problem considers several ways to compute x n for some n >= 0. a. Write an iterative function power1 to compute x n for
This problem considers several ways to compute xnfor some n >= 0. a. Write an iterative function power1 to compute xnfor n >= 0. b. Write a recursive function power2 to compute xn by using the following recursive formulation: x0 = 1 xn = x * xn-1 if n > 0 c. Write a recursive function power3 to compute xn by using the following recursive formulation: x0 = 1 xn = (xn/2)2 if n > 0 and n is even xn = x * (xn/2)2 if n > 0 and n is odd d. How many multiplications will each of the functions power1, power2, and power3 perform when computing 332? 319? e. How many recursive calls will power2 and power3 make when computing 332? 319?
A friendly note of the algorithm. The value of x is a double and the value of n is an integer.
Name the function in part a. as power1 and save it to a file named power1.cpp. Name the function in part b. as power2 and save it to a file named power2.cpp. Name the function in part c. as power3 and save it to a file named power3.cpp.
For parts d. and e. create a file called q19.txt (notice its a text file, not a .cpp) and type detailed solutions.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
