15110 Fall 2012 [Touretzky/Kaynar]

Lab 2 - due Thursday, September 6

Deliverables

  1. voltage.rb (demonstration)
  2. circle_area.rb (demonstration)
  3. answers.txt
  4. cone_vol.rb
  5. my_func.rb (time permitting)

Place these files in a lab2 folder. Before leaving lab, zip up this folder, and hand it in.

CA Demonstration

Activities

  1. Type each of the following expressions into irb. What value do each of the following Ruby expressions evaluate to? Is that value an integer or a floating point?

    1. 100
    2. 29 % 4
    3. 2.5e2
    4. 3e5
    5. 3 * 10**5
    6. 15 + 35 * 2
      Why is this different from (15 + 35) * 2?
    7. 3 / 2 * 4.0
    8. 2 * 4.0 / 3
      Why is this different from 3 / 2 * 4.0?
    9. 24 - 6 * 2 - 9
      Is this different from ((24 - 6) * 2) - 9 and/or 24 - ((6 * 2) - 9)? Why?

    Write your answers in the file answers.txt. (Review Unit 2A lecture slides if necessary.)

  2. In cone_vol.rb, define a Ruby function cone_vol(r,h) that calculates and returns the volume of a cone with a radius r and height h. This can be calculated using the formula:

    \[ V = \frac{1}{3}\pi r^2h\]

    Place in answers.txt a copy of irb lines in which you call cone_vol(r,h) to compute the volume of cone with a radius and height of, respectively, 5 and 10, and irb shows you the result.

  3. Pick a mathematical formula that is used in your major. In a file called my_func.rb, create a Ruby function that calculates some value using that formula.

    At the beginning of your my_func.rb include a comment (one or more lines of English text that start with "#" and are ignored by irb) that describes the fomula the function computes and what units the parameters and results are in.