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...

  • Write a full program that will accept exactly  three command line arguments that can be interpreted as...

    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. Suppose your program is called Add.java. Then running the command java Add 1 2 3 will output 6, and running java Add, for example, will cause the program to shut down. Consider the following recursive method: public static int mystery (int n)...

  • Please write program in C language. 2.Write a recursive int function to return the number of...

    Please write program in C language. 2.Write a recursive int function to return the number of even integers in a linked list. Each node in the list has an integer number (declared as int number) and a pointer to the next node (declared as NODE *next). The function is defined as int count (NODE *); and is called as follows: x = count(head);

  • 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) }

  • Find a) overall run time T(n) and b) worst case run timeO(N) of this recursive function....

    Find a) overall run time T(n) and b) worst case run timeO(N) of this recursive function. int mystery (int n) { if (n<= 1) {return 1 } else { return mystery(n-1) + mystery(n-2); } }

  • 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 +...

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