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

ANSWER IN C. one soure code please Write a recursive function power(base, expone

ID: 3863451 • Letter: A

Question

ANSWER IN C. one soure code please

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. (Recursive Greatest Common Divisor) The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd 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. (Find the Minimum Value in an array) Write a recursive function recursive Minimum 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 <stdio.h>

int power(int base,int exponent)
{
    if(exponent == 1)
    return base;
    else
    return base * power(base,exponent-1); // recursive call to power until exponent = 1
}
int gcd(int n1,int n2)
{
    if(n2 == 0)
     return n1;
     else
       return gcd(n2, n1%n2); //recursive call until n2 = 0

}
int recursiveMinimum(int *arr,int size)
{
   static int i=0, min=999;

   if(size == 0)
   return -1;
   else if(size == 1)
   return *(arr+0);
   if(size > 0)
   {
      if( min > *(arr+i) )
      {
         min = *(arr+i);
      }
      i++;
      recursiveMinimum(arr, size-1);// recursive call until size = 1
   }
return min;
}
int main(void)
{
    int x,y,num1,num2,array[10],i;
  
   printf("Enter integers to compute exponentiation : ");
   scanf("%d %d",&x,&y);
   printf(" %d raise to power %d = %d",x,y,power(x,y));
  
  
   printf(" Enter two numbers to find the Greatest common divisor : ");
   scanf("%d %d ",&num1,&num2);
   printf(" The greatest common divisor of %d and %d = %d",num1,num2,gcd(num1,num2));
  
   printf(" Enter the elements of array");
   for(i=0;i<10;i++)
   {
        scanf("%d",&array[i]);
   }
   printf(" The minimum element in the array = %d",recursiveMinimum(array,10));
  
   return 0;
}


Output:

Enter integers to compute exponentiation : 3 4

3 raise to power 4 = 81

Enter two numbers to find the Greatest common divisor : 8 12

The greatest common divisor of 8 and 12 = 4

Enter the elements of array 34 56 12 8 67 44 54 9 10 33

The minimum element in the array = 8

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