Purchase Solution

C++ recursion program

Not what you're looking for?

Ask Custom Question

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;
}

Purchase this Solution

Solution Summary

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

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

Purchase this Solution


Free BrainMass Quizzes
Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Basic Networking Questions

This quiz consists of some basic networking questions.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.