Share
Explore BrainMass

An Example of Recursion Recursive Function in C++

Consider the following recursive function.

int mystery(int number)
{
if(number == 0)
return number;
else
return(number + mystery(number - 1));
}

a. Identify the base case.
b. Identify the general case.
c. What valid values can be passed as parameters to the function mystery?
d. If mystery(0) is a valid call, what is its value? If not, explain why.
e. If mystery(5) is a valid call, what is its value? If not, explain why.
f. If mystery(-3) is a valid call, what is its value? If not, explain why.

8. Consider the following recursive function:

void funcRec(int u, char v)
{
if(u == 0)
cout<<v;
else if(u == 1)
cout<<static_cast<char>(static_cast<int>(v) + 1);
else
funcRec(u - 1, v);
}

a. Identify the base case.
b. Identify the general case.
c. What is the output of the following statement? funcRec(5, 'A');

Solution Preview

Consider the following recursive function.

int mystery(int number)
{
if(number == 0)
return number;
else
return(number + mystery(number - 1));
}

a. Identify the base case.
Base case is
if(number == 0)
return number;

b. Identify the general case.
return(number + mystery(number - 1);

c. What valid values ...

Solution Summary

For two different recursive functions, teaches how to identify the following:
a. Identify the base case.
b. Identify the general case.
c. What valid values can be passed as parameters to the function(s)?
d. Various test cases

$2.19