Recursion Answers

Recursion Answers - Recursion Answers Page 1 of 3 Answers...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Answers to Self-Study Questions Test Yourself #1 The printInt method does obey recursion rule 1; it does have a base case, namely: if (k == 0) return; It obeys recursion rule 2 in a sense; the code: printInt( k - 1 ); makes progress toward the base case as long as k is greater than zero. However, all calls with actual parameters less than zero will cause an infinite recursion. Assuming that the intention is to print only positive values, this could be fixed by changing the base case to: if (k <= 0) return; Test Yourself #2 The call printTwoInts(3) causes the following to be printed: From before recursion: 3 From before recursion: 2 From before recursion: 1 From after recursion: 1 From after recursion: 2 From after recursion: 3 Test Yourself #3 Question 1: The call factorial(3) leads to three recursive calls; when all calls are still active, the runtime stack would be as shown below (showing just the values of parameter N). The values returnedby each call are also shown.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/27/2008 for the course CS 367 taught by Professor Marvinsolomon during the Spring '08 term at Wisconsin.

Page1 / 3

Recursion Answers - Recursion Answers Page 1 of 3 Answers...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online