{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ch05 - 1.1 Trace the execution of the call mystery(4 for...

Info icon This preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
1.1 Trace the execution of the call mystery(4) for the following recursive method using the technique shown in Figure 5.2. What does this method do? public static mystery(int n) { if (n == 0) return 0; else return n * n + mystery(n 1); } mystery(4) ( condition false, recursive case ) | mystery(3) ( condition false, recursive case ) | | mystery(2) ( condition false, recursive case ) | | | mystery(1) ( condition false, recursive case ) | | | | mystery(0) ( condition true, base case ) | | | | return 0 | | | return 1 2 + 0 | | return 2 2 + 1 2 + 0 | return 3 2 + 2 2 + 1 2 + 0 return 4 2 + 3 2 + 2 2 + 1 2 + 0 This function returns the sum of squares of all integers up to the input integer.
Image of page 1

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

View Full Document Right Arrow Icon
1.2 Answer Exercise 1 above using activation frames.
Image of page 2
1.3 Trace the execution of printChars("tic") (Example 5.2) using activation frames.
Image of page 3

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

View Full Document Right Arrow Icon
1.4 Trace the execution of printCharsReverse("toc") using activation frames. 1.5 Prove that the printChars method is correct. The base case is recognized and solved correctly in the lines: if (str == null || str.equals("")) return; The recursive case does make progress towards the base by sending a smaller string for each recursive iteration: printChars(str.substring(1)); If the smaller problems are solved correctly, the original problem is also solved:
Image of page 4
System.out.println(str.charAt(0)); In this line, the first character is printed, and then in the next statement, the rest of the string is sent of the function. 1.6 Trace the execution of length("tictac") using a diagram like Figure 5.2. 1.7 Write a recursive algorithm that determines whether a specified target character is present in a string. It should return true if the target is present and false if it is not. The stopping steps should be: a. a string reference to null or a string of length 0, the result is false b. the first character in the string is the target, the result is true The recursive step would involve searching the rest of the string. isPresent(c, s) if s is null or the empty string, return false if the first character of s is c, return true recursively call isPresent(c, substring of s without the first character)
Image of page 5

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

View Full Document Right Arrow Icon
2.1 Does the recursive algorithm for raising x to the power n work for negative values of n ? Does it work for negative values of x ? Indicate what happens if it is called for each of these cases. The algorithm does not work for negative values of n; it goes into an infinite loop. It works fine for negative values of x. 2.2 Trace the execution of fibonacciStart(5) using activation frames. 2.3 Trace the execution of the following using activation frames. gcd(33, 12) gcd(12, 33) gcd(11, 5)
Image of page 6
2.4 For each of the following method calls, show the argument values in the activation frames that would be pushed onto the run-time stack. a. gcd(6, 21) b. factorial(5) c. gcd(31, 7) d. fibonacci(6) e. fibonacciStart(7) a.
Image of page 7

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

View Full Document Right Arrow Icon
b. c. d. e.
Image of page 8