NOTE THE CORRECTION IN BOLD TO THE RUBY METHOD IN PROBLEM 2. IT ACTUALLY DOESN'T AFFECT THE ANSWERS BUT IT DOES AFFECT YOUR B.M.I.!
Read sections 2.1-2.4 in chapter 2 of the textbook Explorations in Computing and read pages 19-42 of chapter 2 of the book Blown To Bits.
3 * 4 + 5 12 + 5 17HINT: Check your final answers with irb!!!
def compute_bmi(height, weight)
return weight * 703 / height ** 2 # correction to formula here
end
compute_bmi(63, 132)
will we get an integer result or a floating point result? Why?
compute_bmi(180, 72)
Does Ruby report an error? Why or why not?
compute_bmi(65)
Does a default weight get used in our function or does Ruby complain about this function call? Explain.
def compute_bmi(height, weight)
print weight * 703 / height ** 2 # correction to formula here
end
Why does the following computation fail?
average_bmi = (compute_bmi(63,132) + compute_bmi(72,180) + compute_bmi(65,147)) / 3.0
NOTE: Don't just say how to fix the function. Explain why the computation fails based on the current function definition.
def mystery(n) value = 1 for i in 1..n do value = value * i print value print "\n" # print a newline character end end
mystery(6)
value = value * 2
Now what mathematical function is this Ruby method computing if n > 0?
def mystery(n) value = 1 for i in 1..n do value = value * i return value end end
Store the function in a file, then load it in irb and call it with different positive integers and observe the results. What do your observations suggest about how the return statement works?