Access over 20 million homework & study documents

6 Consider the following recursive function int mystery (int numbe

Content type
User Generated
Rating
Showing Page:
1/4
6. Consider the following recursive function: int mystery
(int number) //line 1 { If (number == 0) //line 2 return
number; //line 3 else //line 4 return (mystery(number + 1)
number; //line 5 }
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 the value? If not,
explain why.
e. If mystery(10) 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.
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 the value? If not,
explain why.
e. If mystery(10) 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.

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/4
Solution
Given that,
Int mystery(int number)
{
if(number==0)
return number;
else
return mystery(number+1)-number;
}
The base case for this function is,
if number=0 then it returns 0.
The general case for this function is,
if number !=0 then it returns [mystery(number+1)-number].
The function mystery(10) is not valid,eventhough it is
passing valid int value.
The reason for this is here the function checks for the
number value,whether it is equal to 0 or not,if not equals to
0 then it again calls the function with addion value and
subtracting the number from that.

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/4

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 4 pages?
Access Now
Unformatted Attachment Preview
6. Consider the following recursive function: int mystery (int number) //line 1 { If (number == 0) //line 2 return number; //line 3 else //line 4 return (mystery(number + 1) – number; //line 5 } 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 the value? If not, explain why. e. If mystery(10) 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. 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 the value? If not, explain why. e. If mystery(10) 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. Solution Given that, Int mystery(int number) { if(number==0) return number; else return mystery(number+1)-number; } The base case for this function is, if number=0 then it returns 0. The general case for this function is, if number !=0 then it returns [mystery(number+1) -number]. The function ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Really helpful material, saved me a great deal of time.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4