Place the files in a lab3 folder. Before leaving lab, hand the piece of paper in to your CA, zip up the lab3 folder, and hand the zip file in.
for i in 1..3 do for j in "a".."c" do print i, j, "\n" end end
Doomsday Algorithm
In the Gregorian calendar (the one we use today), an amazing property exists. On any given year, the dates Apr 4 (i.e. 4/4), June 6 (6/6), August 8 ( 8/8), October 10 (10/10) and December 12 (12/12) all occur on the same day of the week. Also, so do May 9 (5/9) and September 5 (9/5) as well as July 11 (7/11) and November 7 (11/7). In addition, so does the last day of February. Also January 3 (1/3) on non leap-years and January 4 (1/4) on leap years. These days are known as "doomsdays".
To compute the day of the week of the doomsdays for the input year, follow this algorithm:
Trace this algorithm on paper for 2012. What day do you get? Check a calendar online to verify that you got the correct answer.
Write a Ruby function compute_day(year) in the file doomsday.rb that returns the day of the week of the doomsdays for the given year using the algorithm above.
Program Logic
Recall that a variable can store different kinds of data. A variable can hold a Boolean (logical) value of true or false.
Store the following Ruby function in the file legal.rb that prints out what is legal to do for a person in the U.S. given the person's age, gender, and citizenship based on a simplification of US law:
def legal_actions(age, male, citizen_of_USA) print "Legal to: \n" if age >= 21 then print "Drink alcohol.\n" end if age >= 18 and citizen_of_USA == true then print "Vote.\n" if male == true then print "Get drafted into the armed forces\n." end end end
Load this program into irb and call the function as follows:
legal_actions(25, true, true)
(You are seeing what a 25 year old male citizen of the USA can do legally of the three options in the function.) You should get all three actions printed out.
On the same paper you used for problem 1, draw a flowchart that shows the flow of control through the various conditions in the function.
On the same paper you used for problem 1, create a table as shown below, and fill in values for age, male and citizenship that will result in the given output. Use your function in irb to test your answers. If a particular parameter does not matter for a specific output, indicate this in the table as "anything". If a particular output is impossible, then indicate "None" in each of the age, male and citizenship columns.
Output | Age | Male? | US Citizen? |
---|---|---|---|
DINK, VOTE & DRAFTED | 25 | true | true |
DRINK & VOTE only | |||
DRINK & DRAFTED only | |||
VOTE & DRAFTED only | |||
DRINK only | |||
VOTE only | |||
DRAFTED only | |||
No output |
Nested Loops
Consider the following output as shown:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
Store the following Ruby function in the file triangular.rb and complete the missing parts so that it creates the output above. Note that the columns of numbers do not need to line up perfectly. Run your program in irb to test your work. HINT: Row i has i columns.
def triangle() value = 1 row = 1 while row <= _____ do column = 1 while column <= _______ do if column != _______ then print value, " " else print value, "\n" end value = value + 1 column = column + 1 end row = row + 1 end end
EXTRA: Try to print out a triangle with 15 rows and get the numbers to line up in neat columns.