Consider the following pseudocode function. function Crunch(x is in R) if x ≥ 100 then return x/100 else return x + Crunch(10 · x) (a) Compute Crunch(5). Crunch(5) =
@e.mccormick
(b) What happens if you try to compute Crunch(−23)? What does this suggest about an appropriate precondition for this function?
crunch(5) since x is not >= 100 return x + Crunch(10 · x) = 5+crunch(10*5) = 5+crunch(50) crunch 50 will return 50+crunch(50*10) so we have 5+50+crunch(500) crunch 500 will return 500/100 = 5 so crunch(5) returns 5+50+5 = 60
I think
Crunch(−23) will always crunch a number <= 100 so it will go in infinite loop, thus x needs to be positive
Consider the following pseudocode function. function Crunch(x is in R) if x ≥ 100 then return x/100 else return x + Crunch(10 · x) (a) Compute Crunch(5). Crunch(5) = |dw:1371433544740:dw|
Join our real-time social learning platform and learn together with your friends!