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

Write a recursive function power(base, exponent) that when invoked returns base^

ID: 3819285 • Letter: W

Question

Write a recursive function power(base, exponent) that when invoked returns base^exponent For example, power(3, 4) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function god that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows: If y is equal to 0. then gcd(x, y) is x; otherwise gcd(x. y) is gcd(y. x % y). where % is the remainder operator. Write a recursive function that takes an integer array and the array size as arguments and returns the smallest element of the array. The function should stop processing and return when it receives an array of one element. Write the int main(void) function as a driver program and call the above three functions result with sample Input/Output.

Explanation / Answer

#include <iostream>

using namespace std;

int power(int base, int exponent)
{
if (exponent != 1)
return (base*power(base, exponent-1));
else
return 1;
}

int recursiveMinimum(int array[], int size)
{
if (size <= 0) {
return -1;
}
if (size == 1) {
return 0;
}
int m = 1 + recursiveMinimum(array + 1, size- 1);
if (array[0] < array[m]) {
return 0;
} else {
return m;
}
}

int GCD(int first, int second)
{
if (second != 0)
return GCD(first, first%second);
else
return first;
}

int main()
{
/*int base;
int exponent;
int first, second;
cout<<"Enter base ";
cin >> base;
cout<<"Enter exponent ";
cin>> exponent;
int finalval = power(base,exponent);
cout<<"Power is "<< finalval<<" ";
cout<<"Enter first no to find GCD ";
cin>>first;
cout<<"Enter second number to find GCD ";
cin>>second;
int gcd = GCD(first,second);
cout<<"GCD is "<<gcd<<" ";
*/
int size;
cout<<"Enter size of array ";
cin>>size;
cout<<"Enter element of array ";
int array[] = { };
//cout<<"Enter array of "<<size<<" elements ";
for (int g = 0; g < size; g++)
{
cout <<"Enter array element ";
cin >> array[g];
}
int min = recursiveMinimum(array,size);
cout<<"minimum element from array is "<<array[min]<<" ";
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote