15110 Spring 2013 [Kaynar/Gunawardena]

Lab 3 - Thursday, January 31, 2013

Deliverables

  1. Paper containing:
    1. trace of doomday algorithm
    2. "legal actions" flow chart and table
  2. doomsday.rb
  3. triangular.rb

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.

CA Demonstration

Activities

  1. 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:

    1. Let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"].
    2. Let y be the last two digits of the year. (How do you compute this using the modulo (%) operator?)
    3. Let a be the integer quotient when you divide y by 12.
    4. Let b be the integer remainder when you divide y by 12.
    5. Take the remainder from the previous step and divide it by 4, keeping just the integer quotient as c.
    6. Add up a, b, and c to form the value d.
    7. Compute e, representing the day of the week for the doomsdays, by adding 2 to d and then dividing by 7, keeping the remainder only. Note that e should be a value between 0 and 6. (Why?)
    8. Print out days[e] (followed by a new line character).

    1. Trace this algorithm on paper for 2012. What day do you get? Check a calendar online to verify that you got the correct answer.

    2. 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.

  2. Program Logic

    Recall that a variable can store different kinds of data. A variable can hold a Boolean (logical) value of true or false.

    1. 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.

    2. 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.

    3. 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.

      OutputAgeMale?US Citizen?
      DINK, VOTE & DRAFTED25truetrue
      DRINK & VOTE only    
      DRINK & DRAFTED only    
      VOTE & DRAFTED only    
      DRINK only    
      VOTE only    
      DRAFTED only    
      No output    

  3. 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.