Create a function main to test the implementation


Discuss the below:

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

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

power.cpp

#include

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;

}

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Create a function main to test the implementation
Reference No:- TGS01937055

Now Priced at $20 (50% Discount)

Recommended (93%)

Rated (4.5/5)