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

Info iconThis preview shows pages 1–8. 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.
Background image of page 1

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

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

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

View Full DocumentRight 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:
Background 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)
Background image of page 5

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

View Full DocumentRight 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)
Background image of page 6
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.
Background image of page 7

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

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

This document was uploaded on 11/01/2011 for the course COMPSCI 225 at Boise State.

Page1 / 17

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

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

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