Question

C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise...

C++

Create three recursive functions that accomplish the following:

Recursive Power Function this function will 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 non-negative integer.

String Reverser function that accepts a string object as its argument and prints the string in reverse order.

Sum of Numbers this function 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 10 is passed as an argument the function will return the sum of 1, 2, 3, 4, 5 ... 10. Main should call each of the functions to show that they work correctly.

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

#include <iostream>
#include <string>
using namespace std;
float power(float x, float y)
{
   // indeterminate form
   if (x == 0 && y == 0)
   {
       return -1;
   }
   // if x is 0
   else if (x == 0)
   {
       return 0;
   }
   // if y is 0
   else if (y == 0)
   {
       return 1;
   }
   //else
   return x*power(x,y-1);
}

void reverse(const string& str)
{
   size_t len = str.size();
   if (len == 0)
   {
       return;
   }
   else if(len == 1)
   {
cout << str << endl;
   }
else
{
cout << str[len-1];
reverse(str.substr(0, len-1));
}
}

int sum(int n)
{
   // if number is negative return 0
   if (n <= 0)
   {
       return 0;
   }
   //else
   return n+sum(n-1);
}

int main()
{
   cout<<"power(0,0): "<<power(0,0)<<endl;
   cout<<"power(0,4): "<<power(0,4)<<endl;
   cout<<"power(4,0): "<<power(4,0)<<endl;
   cout<<"power(2,3): "<<power(2,3)<<endl;

   reverse("");
   reverse("ramvishvas");

   cout<<" sum(10): "<<sum(10)<<endl;
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise...
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++ 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...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • I need help writing this program in C++. Thanks so much for your help in advance...

    I need help writing this program in C++. Thanks so much for your help in advance Function 1: 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 return the sum of 1, 2, 3, 4, ... 50. Use recursion to calculate the sum. Demonstrate the function in a program. A sample...

  • python programming Design a function that uses recursion to raise a number to a power. The...

    python programming Design 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.

  • To be written in C++ write a recursive function that accepts a string object as its...

    To be written in C++ write a recursive function that accepts a string object as its argument and prints the string in reverse order. demonstrate the function in a driver program.

  • Using C++ Write two `void` functions.  These functions each take two  integer parameters.  The functions accomplish the following tasks....

    Using C++ Write two `void` functions.  These functions each take two  integer parameters.  The functions accomplish the following tasks. The first function prints out the numbers from the first argument up to and including the number passed as the second argument counting by the first argument (i.e. if the arguments are 2 and 10, the information below was given: #include <iostream> using namespace std; // function definitions// // main program int main() {   int firstNumber,secondNumber;   char countType, doAgain;   // we will do this...

  • Create a new NetBeans project from scratch named PowerDemo. Write a method that uses recursion to...

    Create a new NetBeans project from scratch named PowerDemo. Write a method that uses recursion to raise a number to a power. The method should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the method in a program. The main method will provide the testing code for using user input for the recursive method.

  • RecursiveExponent.java Write a recursive function that accepts two arguments into parameters x and y, and which...

    RecursiveExponent.java Write a recursive function that accepts two arguments into parameters x and y, and which returns the value of x raised to the power y. Hint: exponentiation is equivalent to performing repetitions of a multiplication. For example, the base 4 raised to the 6th power = 4*4*4 * 4 * 4 * 4 = 4,096. The only file that you need to create is RecursiveExponent.java. Your main method should prompt the user to supply the base and exponent values...

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