Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> using namespace std; int R_power(int count, const int & base

ID: 652504 • Letter: #

Question

#include <iostream>

using namespace std;

int R_power(int count, const int & base)

{

       if (count == 0)

              return 1;

       else

              return base * R_power(count-1,base);

}

int I_power(int count, const int & base)

{

       int multiend = 1;

       while (count > 0)

       {

              multiend *= base;

              count--;

       }

       return multiend;

}

int main()

{

       int count = 10;

       int base = 2;

       cout<<R_power(count,base)<<endl;

       cout<<I_power(count,base)<<endl;

       return 0;

}

Question 1:Please explain the operation of the functions

Explanation / Answer

The R_power function finds the value of a base raised to an exponent recursively.
The recursive path works as:
x^n can be written as:
x^n = x * x^(n - 1) = x * x * x^(n - 2) ......
Similarly, the function works.
If the value of power = 0(), the 1 is returned. Otherwise, P_power is called to find the value of base raised to the power count - 1

The function l_power also finds the of a number raised to a power, but in an iterative manner
just keep a variable to hold the result and multiply it to the base until the loop finishes.