Question

1, write a function to compute a for a a and b. For the recursive step 1. a recursive ab integers use the following equality a b 1 a and a 1 2. What does the following recursive function do? int mystery (int a, int b) if (b3-1) return (a) else return (a mystery (a, b 1))

*Program is in C*

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Herer is recursive function :

int power(int a, int b)
{
if (b != 0)
return (a*power(a, b-1));
else
return 1; // a^0 = 1
}

Sample program to test:

#include <stdio.h>
int power(int a, int b);
int main()
{
printf("Result is %d\n",power(2,4));

return 0;
}

int power(int a, int b)
{
if (b != 0)
return (a*power(a, b-1));
else
return 1; // a^0 = 1
}

Output:

Result is 16

Program2:

What does the recursive does is ?

return the sum of numbers

example if a = 2 and b = 4

answer is 8

How?

2 + 2 + 2 + 2 = 8

a will sum untill b times

Add a comment
Know the answer?
Add Answer to:
*Program is in C* Write a recursive function to compute a^b for integers a and b....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++, write a recursive function power that takes two positive integers base and n as...

    In C++, write a recursive function power that takes two positive integers base and n as inputs and computes base to the n power. Example power(3, 2) is 9. Do not use any loops in your program. This is what I got so far but I'm not sure what I'm doing wrong: #include <iostream> using namespace std; int calc(int x, int y); int main() {       calc(2, 3);    return 0; } int calc() {          cout...

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

  • Write a recursive function that computes the smallest integer in a given array of integers. Use...

    Write a recursive function that computes the smallest integer in a given array of integers. Use the following algorithm: int min(int[] A, int low, int high) { if (low == high) return A[low]; int mid = (low + high) / 2; int min1 = min(A, low, mid); int min2 = min(A, mid + 1, high); if (min1 > min2) return min2; return min1; }

  • Write a recursive function in C++ that displays all nodes data (integers) in an array of...

    Write a recursive function in C++ that displays all nodes data (integers) in an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this is determined by another part of the program) }

  • Write a full program that will accept any number of command line arguments and print them...

    Write a full program that will accept any number of command line arguments and print them to the screen. Suppose your program is called Print.java. When the command java Print hello goodbye you is run, the output will be hello goodbye you Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program....

  • Use the Summation recursive program you did in the class to also work with minus integers....

    Use the Summation recursive program you did in the class to also work with minus integers. For example, the sum of -3 will be -6 which is (-3)+(-2)+(-1)+0. USE THIS CODE package project5; import java.util.Scanner; public class SingleRecursion { /** Main method */ public static long sum(int n) {    if (n<0) throw    new IllegalArgumentException ("Can't calculate factorial of negative");    if (n==1)        return 1;    else if (n==0)        return 1;    else       ...

  • PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the...

    PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...

  • 5.43 (10 pts) What does the following program do? #include <stdio.h> 3 unsigned int mystery Cuns...

    5.43 (10 pts) What does the following program do? #include <stdio.h> 3 unsigned int mystery Cuns igned int a, unsigned int b): // function prototype 5 int main(void) printf("%s". "Enter two positive integers: unsigned int x: I/ first integer unsigned int y: // second integer scanf("Su%u". &x, &y); "); 12 13 14 15 II Parameter b must be a positive integer 16 to prevent infinite recursion 7 unsigned int mystery Cuns igned int a, unsigned int b) 18 printf("The result...

  • Write a complete C++ program that at least consists of the main() function and a recursive...

    Write a complete C++ program that at least consists of the main() function and a recursive function gcd() with return value. Define function gcd() with two and only two parameters that calculates the greatest common divider of two given parameters. Hint: use the difference between two parameters or the remainder obtained using one parameter divide the other. In main() Read 2 positive integers with proper prompt. Call gcd() with proper syntax. Display the result, i.e. the greatest common divider of...

  • Write a recursive C program that receives an array of positive integers and its length and...

    Write a recursive C program that receives an array of positive integers and its length and returns the largest number in the array. For example: int arr[5] = {-2, 31, 4, 12, -8}; findMax(arr, ...) -> 31 int findMax ( int * arr , int p , int max ) { }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT