Question

Q5. [5 marks] Consider the following recursive function: int Test (int number) //Line 1 //Line 2 if (number == 0) //Line 3 re

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a. In the Test() function the base case is Test(0). Beacuse a base case is that in which no recursion is occured . it's value is predefined. Here the value of Test(0) is already defined that is 0

b. A general case is that in which recursion is called and on calling recursion it is going close to the base case. Here in Test() function every Positive integer is a general case.Because on every call of Test() function the int number is reducing with one. And at last it is becoming Zero. which is the base case. For ex. Test(5), Test(8)

c. Test(0) is a valid call beacuse the value of Test(0) is 0 which is predefined is the function. As I already mention is section a of the same question.

d. Test(5) is a valid call, it's value is 15. Here is the explanation:

Test(5) -> 5+Test(4) -> 5+4+Test(3) -> 5+4+3+Test(2) -> 5+4+3+2+Test(1) -> 5+4+3+2+1+Test(0) -> 5+4+3+2+1+0 -> 15

e. Test(-2) is a invalid call. Beacuse on every recursion it is going far from the base case and at last it will give a segmentation fault or memory error. Here's the explanation: Test(-2) -> -2+ Test(-3) -> (-2)+(-3)+Test(-4) -> (-2)+(-3)+(-4)+Test(-5) -> ... like this. it is a never ending process. So Test(-2) is a invalid call.

Add a comment
Know the answer?
Add Answer to:
Q5. [5 marks] Consider the following recursive function: int Test (int number) //Line 1 //Line 2...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT