# 15110 Fall 2012 [Touretzky/Kaynar]

## Lab 2 - due Thursday, September 6

### Deliverables

1. voltage.rb (demonstration)
2. circle_area.rb (demonstration)
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

• creating a lab2 directory
• Set up the .irbrc file and RubyLabs:
• starting irb within a terminal window
• creating and using functions: voltage(i,r) to calculate $$V = IR$$
• using gedit to define a function in voltage.rb
• voltage's parameters and return values
• math constants and print: circle_area(r) to calculate $$A = \pi r^2$$
• Math.sqrt(x) and Math::PI
• returning a value from circle_area(r)
• using the return value
• printing a value instead of returning

### 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?

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