PA1 - Andrew.cmu.edu

advertisement
15110 Summer II 2012
[adapted from Cortina/von Ronne]
Programming Assignment 1 – due Monday, July 9 by 11:59 pm.
[email zipped folder with all homework contents to Avia at aweinste@andrew.cmu.edu]
Overview
For this assignment, you will create a source file (that is, a text file) for each of the
problems below. You should store all of these files in a folder named pa1.
Note: You are responsible for protecting your solutions to these problems from
being seen by other students either physically (e.g., by looking over your
shoulder) or electronically. In particular, since the lab machines use the Andrew File
System to share files worldwide machines, you need to be careful that you do not put
files in a place that is publicly accessible.
If you are doing the assignment on the Gates Hall Cluster machines we use in lab, our
recommendation is that you should place your pa1 directory under ~/private/15110. that
is, the new directory pa1 is inside a directory called 15110, which is inside the directory
called private, which is inside your home directory. (Every new andrew account is set up
with a private subdirectory within the account's home directory that only that account
can access.) Please refer to Setting up Directories for instructions on creating and
managing your directories.
Exercises
1. If you haven’t done so already, go to go to http://classroomsalon.org. Click on
"Sign up" and use the registration code ‘cmu’ to create an account. Quickstart -->
edit profile and upload your picture. Join our course salon
http://classroomsalon.org/redirect/redirect.aspx?action=viewSalon&id=1143. Go
to the Salon home page and click on the "Messages" tab. Click on the "view the
discussion board for this document" and type a short bio. You can also read the
bio's that are already there.
2. Let’s get acquainted with Classroom Salon:
a. Go to the Salon home page either by clicking on the words Classroom
Salon at the top left of the page or the house icon to the top right. Click
on “messages” below Recent Activity in the center of the page. Scroll
down until you find a post by Prof Guna. Click on his profile picture.
Once on his page, click on the “follow” button. Now, repeat all the
previous steps expect this time with Avia Weinstein. At this point, when
you click on ‘Feeds’ from your homepage, you will be able to follow all
posts by Prof Guna and Avia.
b. Learning how to highlight/comment on docs: Click on “Salons” on the
top right of the web page and hover down to “See All Your Salons”.
Once on this page, click “15-110N12”. On the bottom left quadrant of
this new screen, click on “Test Document” under “Tasks/Documents in
This Salon”. Please answer any 3 out of 6 of the posted questions. To
do this, click the box “allow annotation”, and highlight the blanks to the
questions you would like to respond to. Write your answers in the
comments box.
You can use this highlight method to make comments on any docs
posted, answer homework questions, or ask any questions for others
to answer.
3. Type each of the following expressions into irb. What value does each of the
following Ruby expressions evaluate to? Is that value an integer or a floating
point? Write your answers in the file answers.txt.
a.
b.
c.
d.
e.
f.
250
28 % 5
2.5e2
3e5
3 * 10**5
20 + 35 * 2
Why is this different from (20
g.
h.
3.0 * 2 / 3
2 / 3 * 3.0
Why is this different from 3.0
i.
+ 35) * 2?
* 2 / 3?
25 - 5 * 2 - 9
Is this different from ((25
- 5) * 2) - 9
and/or 25
- ((5 * 2) - 9)?
Why?
4. The distance d in meters that an object dropped in a vacuum near the surface of
the earth will fall in t seconds can be described by formula,
1
d= 9.8 t2.
2
a. Use a calculator to check the value of d when t is 2.5. You should find
that d is 30.625.
b. Now using Gedit, define a Ruby function drop_distance(t) (that is, a
function named drop_distance that takes a single parameter named t)
and prints out the corresponding value of d. Put the text of your
function in a file named drop_distance.rb in your pa1 directory.
c. After you've saved the file you can test it with irb using the commands:
irb(main):005:0> load "drop_distance.rb"
=> true
irb(main):006:0> drop_distance(2.5)
30.625=> nil
5. According to ohm's law, the current I flowing through a conductor between two
points can be calculated from the conductor's resistance R and the potential
difference V across those points by using the equation:
I=VR
If the potential difference V is measured in volts and resistance R is measured in
ohms, the resulting current I will be in amperes.
In current.rb, define a Ruby function current(v,r) that calculates and prints (in
the format shown below) the current flowing through a conductor between two
points whose potential difference is v volts when the resistance of the conductor
is r ohms. The function should return nil in all cases.
Example usage:
>> load "current.rb"
=> true
>> current(12.0,2.5)
The current is 4.8 amperes.
=> nil
>> current(110,7)
The current is 15.7142857142857 amperes.
=> nil
6. A quadratic equation ax2+bx+c=0 can be solved for x by calculating:
2
X=
−𝑏±√𝑏 −4𝑎𝑐
2𝑎
In quadratic.rb, define a Ruby function quadratic(a,b,c), which takes the
constants a, b, and c and calculates and prints the two values of x for which the
equation holds using exactly the same format as shown in the examples below.
You may assume that b2−4ac is non-negative, and the function should output the
same solution twice when b2−4ac=0. In all cases, the function should return nil.
(Hint: Math.sqrt(expr) evaluates to the square root of expr.)
Example usage:
>> load "quadratic.rb"
=> true
>> quadratic(1.0,3.0,-4.0)
x1: 1.0
x2: -4.0
=> nil
>> quadratic(4,4,1)
x1: -0.5
x2: -0.5
=> nil
7. The volume of a cone can be calculated from the radius r of its base and its
height h using the formula:
13πr2h
a. In cone.rb, write a Ruby function cone_volume(radius,height) that calculates
and returns the volume of a cone whose base's radius is radius and whose
height is height. (Use Math::PI for π.)
height is height. (Use Math::PI for π.)
b. Consider a three-dimensional object, which consists of two cones whose
heights are 20m and 30m, respectively, and which share a base, whose
radius is 7m.
In cone.rb, define a Ruby function print_object_volume() that calculates the
total volume occupied by that object (i.e., the total volume occupied by the two
cones that make up the object). It should return nil after printing the following
line of text:
The total volume of the two cones is XXXX.XXXXXXXXXXX.
(Replacing XXXX.XXXXXXXXXXX with the calculated volume.)
Your print_object_volume() function should be defined without directly
calculating the volume of the cones. Rather you should call
your cone_volume function you wrote for part a.
8. In yourName.rb, define a Ruby function yourName( name, age) which takes the string name
and constant age and prints out your name the number of times of your age (so if
you are 17 years old, your name should print 17 times). This must be done using
a for loop.
Example Usage:
>> load "yourName.rb"
=> true
>> yourName("Tom", 7)
Tom
Tom
Tom
Tom
Tom
Tom
Tom
=> 1..7
9. Pick a mathematical formula that is used in your major (or a formula that you
like). In a file called my_func.rb, create 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 what the
fomula the function computes and what units the parameters and results are in.
10. Once you’ve finished your Written Assignment 1 (and completed question 2 on
this assignment), go to Classroom Salon and open up the document Written
Assignment 1. Choose your two most confident answers and write them in the
document (highlight the black and write in the comment box).
Submisions
You should now have a pa1 directory that contains the files answers.txt,
drop_distance.rb, current.rb, quadratic.rb, cone.rb, yourName.rb,
my_func.rb each containing the corresponding function . Zip up your directory
and name it pa1. Email to aweinste@andrew.cmu.edu and in the heading write your
name and pa1.
Download