Question: The program below contains an incomplete recursive function raised _ to _ power ( ) . The function returns the value of the first parameter

The program below contains an incomplete recursive function raised_to_power().
The function returns the value of the first parameter numberof type floatraised to
the value of the second parameter power of type int for all values of power greater
than or equal to 0.
The algorithm used in this question to write a recursive function to raise a float
value number to a positive power uses repeated multiplication as follows:
numberpower =1 if power=0
= number x numberpower-1 otherwise
In other words, number raised to power gives 1 if power is 0;
and otherwise numberpower can be calculated with the formula:
number x numberpower-1
1. #include using namespace std;
2. float raised_to_power()
3.{
4. if (power <0)
5.{
6. cout <<"
Error - can't raise to a negative power
";
7. exit(1);
9.}
10. else if ()
11. return ();
12. else
13. return (number * raised_to_power(number, power -1));
14.}
15. main()
16.
17. float answer = raised_to_power(4.0,3);
18. cout << answer;
19. return 0;
20.}
(b) Using the fact that any value raised to the power of 0 is 1, complete the base case in line 10 and 11.
This question is based on question 1(b) in the written Assignment 4.
The program in question 1 of the written assignment, contains an incomplete recursive function raised_to_power(). This function returns the value of the first parameter number of type float raised to the value of the second parameter power of type int for all values of power greater than or equal to 0.
Based on the fact that any value raised to the power of 0 is 1, the base case for this function is
Select one:
else if (power ==0)
return (1.0);
else if (power ==1)
return (1.0);
else if (power >0)
return (1.0);
else if (power ==0)
return (number * raised_to_power(number, power -1));

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 Programming Questions!