Question

Write a program that uses a function power(base,exponent) which when invoked return sbase exponent C++

Write a program that uses a function power(base,exponent) which when invoked return sbase exponent C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;
int power(int base, int exponent)
{
   int tmp;
   if (exponent == 0) {
      return 1;
   }
   else if (exponent % 2 == 0) {
      tmp = power(base, exponent / 2);
      return tmp*tmp;
   }
   else {
      tmp = power(base, exponent / 2);
      return base*tmp*tmp;
   }
}

int main() {
   int base, exponent;
   cout<<"Enter base: ";
   cin>>base;
   cout<<"Enter exponent: ";
   cin>>exponent;
   cout<<"Result: "<< power(base, exponent);
   return 0;
}
Add a comment
Know the answer?
Add Answer to:
Write a program that uses a function power(base,exponent) which when invoked return sbase exponent C++
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
  • (C PROGRAM PLEASE) Write a recursive function power( base, exponent ) that when invoked returns baseexponent...

    (C PROGRAM PLEASE) Write a recursive function power( base, exponent ) that when invoked returns baseexponent For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. Hint: The recursion step would use the relationship baseexponent = base * base exponent–1 and the terminating condition occurs when exponent is equal to 1 because base1 = base.

  • IN C LANGUAGE: (Recursive Exponentiation) Write a recursive function power( base, exponent ) that when invoked...

    IN C LANGUAGE: (Recursive Exponentiation) Write a recursive function power( base, exponent ) that when invoked returns baseexponent For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. Hint: The recursion step would use the relationship baseexponent = base * baseexponent–1 and the terminating condition occurs when exponent is equal to 1 because base1 = base

  • write a recursive method power( base, exponent )that , when called returns base exponent for example...

    write a recursive method power( base, exponent )that , when called returns base exponent for example , power (3,4) 3*3*3*3*. assume that exponent is an integer greater than or equal to 1. Hint: the resursion step should use the relationship base exponent = basec . base exponent. -1 and the terminating condition occurs when exponent is equal to 1, because base1 = base Incroprate this method into a program that enables the user to enter the base and exponent

  • c++ Write a function that uses recursion to raise a number to a power. The function...

    c++ Write a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the function in a program. Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will...

  • A) (C#) Write a function integerPower(base, exponent) that returns the value of                             &nbsp

    A) (C#) Write a function integerPower(base, exponent) that returns the value of                                         base exponent For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero integer and that base is an integer. The function integerPower should use for or while to control the calculation. Do not use built-in functions. B) Write a C# console program that calls the function in A) to calculate and display the value of 1+2+4+8+…+210.

  • Hem 1 def expo(base, exponent): # Write your function here Python's pow function returns the result...

    Hem 1 def expo(base, exponent): # Write your function here Python's pow function returns the result of raising a number to a given power. Define a function expo that performs this task, and state its computational complexity using big-o notation. The first argument of this function is the number, and the second argument is the exponent (nonnegative numbers only) 4 def main(): ***Tests with powers of 2.*** for exponent in range (5): print(exponent, expo(2, exponent)) 000 9 if _name__ ==...

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

  • Create a CPP file for a C++ Program. Write exactly these functions, power(x,y) function and a...

    Create a CPP file for a C++ Program. Write exactly these functions, power(x,y) function and a print(text, number) function and the main() function. The power(x,y) function returns an integer result that is calculated by raising a number x (integer) to a power y (integer). The second argument y (exponent) of the function can not exceed 100. If the second argument exceeds 100, the function should return -1. Your power(x,y) function must be able to take either 1 or 2 integer...

  • C language: Write a program that uses a function to check if a user entered string...

    C language: Write a program that uses a function to check if a user entered string is a palindrome. Based on the return value of the function, the results are printed in the main() function. (do not print results in function to check for palindrome) A palindrome reads the same forward as backward for example tacocat or civic.

  • Programming Exercise 11.4 m | Instructions expo.py + An alternative strategy for the expo function uses...

    Programming Exercise 11.4 m | Instructions expo.py + An alternative strategy for the expo function uses the following recursive definition: 1 def expo (base, exponent): 2 expo.calls += 1 # Used to track recursive call count 3 # Write you recursive expo function here 4 5 expo.calls = 6 7 def main(): ***"Tests with powers of 2."" 9 for exponent in range (5): 10 print (exponent, expo(2, exponent)) 11 12 if name == "_main_": 13 main() 14 expo(number, exponent) =...

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