Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following pseudocode function. function Cruch(x R) if x 100 then re

ID: 2942920 • Letter: C

Question

Consider the following pseudocode function. function Cruch(x R) if x 100 then return x/100 else return x + Crunch(10 x) Compute Crunch(137). Compute Crunch(53). Compute Crunch(4). What happens if you try to compute Crunch(-26)? What does this suggest about an appropriate precondition for this function?

Explanation / Answer

a. Crunch(137) gives us an input of x = 137. Since 137 > 100, we take x/100 = 1.37 and we are done. b. Crunch(53) gives us an input of x = 53. Since x < 100, we return 53 + Crunch(530). Crunch(530) = 5.3, and 53 + 5.3 = 58.3. c. Crunch(4) gives us an input of x = 4. Since x < 100, we return 4 + Crunch(40). Crunch(40) gets us 40 + Crunch(400). Crunch(400) returns 4. 4 + 40 + 4 = 48, which is our result. d. Trying to compute Crunch(-26) sends us in an infinite loop, with each iteration increasing the magnitude of what is inside (Crunch). Therefore, an appropriate precondition would be to restrict x > 0.