Explore BrainMass
Share

# C++ recursion program

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

1. Modify this pop() function, so that it uses recursion.

int Stack::pop()
{
if (isEmpty())
{
cerr << "Attempting to Pop from an Empty stack!!" << endl;
return -1;
}
else
{
int temp = nodes[curTop];
curTop--;
return temp;
}

}

2. Rewrite the following power() function so that it uses recursion. Provide a function main to test the implementation.

power.cpp

#include <iostream>

using namespace std;

int power(int number, int exponent)
{
int retval = 1;
for (int i=0; i < exponent; i++)
{
retval = retval * number;
}
return retval;
}

int main(int argc, char **argv)
{
cout << power(2, 3) << endl;
cout << power(3, 6) << endl;
cout << power(4, 9) << endl;

return 0;
}

https://brainmass.com/computer-science/cpp/c-recursion-program-308595

#### Solution Preview

Dear Student,

Part 2 is simple:

int power(int number, int exponent)
{
int retval = 1;

if (exponent == 1)
{
retval = number;
}
else if (exponent > -1)
{
exponent--;
retval = number * (power(number,exponent));
}
return retval;
}

As for Part ...

#### Solution Summary

C++ recursion implementation for pop() and power() functionality

\$2.19

## Creating C++ Program Functions: Recursive Functions

(1) Write a recursive function to generate a patter such as the following pattern:

*
**
***
****
****
***
**
*

The program should prompt the user to enter the number of lines in the pattern. Enter 4 should generate the above pattern.

(2) Write a recursive function to generate a patter such as the following pattern:

*
* *
* * *
* * * *
* * *
* *
*

The program should prompt the user to enter the number of lines in the pattern. Enter 4 should generate the above pattern.

(3) A palindrome is a string that reads the same both forward and backward. Write a program that uses a recursive function to check whether a string is a palindrom or not. Your program must contain a value returning recursive function that return true if the string is a palindrome and false otherwise. Do not use any global variables; use the appropriate parameters.

View Full Posting Details