Question

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 << calc(int x, int y) << " ";

  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int calc(int x, int y);

int main() {
    cout << calc(3, 2) << endl;
    return 0;
}

int calc(int x, int y) {
    if (y == 0)
        return 1;
    else
        return x * calc(x, y - 1);
}
Add a comment
Know the answer?
Add Answer to:
In C++, write a recursive function power that takes two positive integers base and n as...
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
  • Data structure c++ Write a recursive function, power, that takes two integers x and y as...

    Data structure c++ Write a recursive function, power, that takes two integers x and y as parameters such that x is nonzero and returns x'. You can use the following recursive definition to calculate xy: If y 20, y = 0; 1, power(x,y)= x, ( xx power(x, y-1), If y<0, y>1. power(x, y) = power(x,-y) Also, write a main function to test your function.

  • C++ Write a recursive function that reverses the given input string. No loops allowed, only use...

    C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sherry";    reverse(name);    cout << name << endl; //should...

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

  • c++ Write the following 2 functions and test them. Both of them need to be recursive...

    c++ Write the following 2 functions and test them. Both of them need to be recursive functions. int sum(int n); // recursive version to calculate the sum of 1 + 2 + ..... + n int str_length(char s[]; // Returns length of the string s[] and the null character, '0\', is not counted in the length). Example of program execution; Enter a positive integer: 10 (user input) The sum of 1+ 2+....+10 is: 55 Enter a sentence: Hello World! (user...

  • In a recursive solution, the base case terminates the recursive processing. In the below code, which...

    In a recursive solution, the base case terminates the recursive processing. In the below code, which of the following calls to recurse() would stop further recursive calls? #include <iostream> void recurse(int x, int y) { if (y > 0) { x++; y--; std::cout << x << " " << y << " "; recurse(x, y); std::cout << x << " " << y << std::endl; } } 1recurse(1, 2); 2recurse(1, 0); 3recurse(1, 1); 4recurse(0, 1);

  • I am creating a recursive function in c++ and have the following code so far. However,...

    I am creating a recursive function in c++ and have the following code so far. However, can you help me fix it so that it doesn't crash when negative numbers are entered. If negative numbers are entered i want to let the user know negative numbers are not accepted and to try again #include <iostream> using namespace std; int multiplication(int x, int y) { int sum = y; //if value of x is 1, then result of x*y will be...

  • C++ question I'm trying to write a code that takes in n number of variables between...

    C++ question I'm trying to write a code that takes in n number of variables between 00 and 99. The program should be able to output the 1st and 2nd lowest digits as well as the highest and second highest digits. So if the input is 4 - 50 20 10 70 it should output 10 20 50 70. I think I've gotten the basic code but I can't seem to get the last digit to shift. #include <iostream> #include...

  • Write a recursive function sum-odds that takes a non-empty list of integers

    in python Part I: Sum of Odd Integers Write a recursive function sum-odds that takes a non-empty list of integers as an argument and returns the sum of only the odd integers in the list. In class we explored a recursive function called rsum that recursively computes the sum of a list of integers use it as a model to get started. Your function must be recursive and must not use any loops

  • // the program attempts to sort three integers // in increasing order, it is incomplete //...

    // the program attempts to sort three integers // in increasing order, it is incomplete // Mikhail Nesterenko // 9/3/2009 #include <iostream> using std::cin; using std::cout; using std::endl; int main(){ // inputs the numbers cout << "Enter three numbers: "; int x, y, z; cin >> x >> y >> z; int tmp; // orders x and y if (x > y){ tmp = x; x = y; y = tmp; } // orders y and z if (y >...

  • Write a C++ function, parsePhrases(...), that takes a string and replaces every comma ‘,’ and period...

    Write a C++ function, parsePhrases(...), that takes a string and replaces every comma ‘,’ and period ‘.’ with the newline character ‘\n’. Important: the function should edit the string (i.e. there should be no cout << calls within the function). Example Program: #include <iostream> using namespace std; // TODO: implement function parsePhrases (...) - no cout calls; int main() { string text = "In theory there is no difference between theory and "; text += "practice. In practice there is....

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