The function power(base, exponent) returns the value of ...



The function power(base, exponent) returns the value of baseexponent. For example, power(3, 4) = 3 * 3 * 3 * 3. Assume that exponent is a positive non-zero integer, and base is an integer.

a) Write the function power(base, exponent) using iteration.

b) Write the function power(base, exponent) using recursion. Hint: The recursion step would use the relationship baseexponent = base * baseexponent - 1 and the terminating condition occurs when exponent is equal to 1 because base1 = base.

Sample Solution

a) Iterative Solution

int power(base, exponent)

{

int i, result = 1;

for (i = 0; i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download