15-110 Spring 2013 [Kaynar/Gunawardena]

Problem Set 2 - due Friday, February 1 in class

Reading Assignment

Read sections 2.1-2.4 in chapter 2 of the textbook Explorations in Computing and read pages 19-42 of chapter 2 of the book Blown To Bits.

Instructions

Exercises

  1. (2 pts) For each of the following Ruby expressions, show how they are evaluated by evaluating one operation at a time and showing the reduced expression until you have a final answer. For example, if the Ruby expression is 3 * 4 + 5, you answer would look like this:

    3 * 4 + 5
    12 + 5
    17
    
    HINT: You can check your final answers with irb!!!

    1. 1 + 2 * 3 / 4 - 5

    2. 4 * 3 ** 2 * 5

    3. 6 * 9 % 5

    4. 2+10 * 8-5

  2. (2 pts) A simple pendulum consists of a mass suspended by a string with a specific length. The period of this simple pendulum can be computed using the following Ruby method (function) which requires the length of the string in meters and the acceleration due to gravity in meters per second per second :

    def compute_period(length, gravity_accel)
        # computes period of simple pendulum given the string length in meters
        # and the acceleration due to gravity in meters/sec/sec (e.g. 9.8)
        return 2 * Math::PI * Math.sqrt(length / gravity_accel)
    end
    
    1. Suppose we use this function to compute the period of a pendulum with a string length of 10 meters and an acceleration due to gravity of 9.8 meters/second/second:

      compute_period(10, 9.8)

      Will we get an integer result or a floating point result? Why?

    2. Suppose we want to do the computation in part (a) but we call our function with the length of the string only:

      compute_period(10)

      Does the method use a default value for the acceleration due to gravity or does Ruby complain about this function call? Explain.

    3. Suppose we want to do the computation in part (a) but we call our function with the arguments in reverse order:

      compute_period(9.8, 10)

      Does Ruby report an error? Why or why not?

    4. Suppose we replace the return statement with a print statement as shown below:

      def compute_period(length, gravity_accel)
          # computes period of simple pendulum given the string length in meters
          # and the acceleration due to gravity in meters/sec/sec (e.g. 9.8)
          print 2 * Math::PI * Math.sqrt(length / gravity_accel)
      end
      

      What value is stored in the variable period if we execute the following instruction? Why?

      period = compute_period(10, 9.8)

  3. (2 pts) For each of the following invalid Ruby expressions, run them in irb and explain the errors that you see.

    1. Math.sqrt(-16)

    2. 32 % 0

    3. 15 + "110"

    4. return = 100

  4. (2.5 pts) Consider the following Ruby method definition that uses a loop:

    def mystery(n)
        value = 1
        for i in 1..n do
            value = value * 2
            print value
            print "\n"    # print a newline character
        end
    end
    

    1. What does this method display if we call it as follows:

      mystery(7)

    2. What mathematical function is this Ruby method computing in general if n > 0?

    3. Suppose we replace the first instruction inside the loop with the following:

      value = value * i

      What does this revised method display if we call it as follows:

      mystery(7)

    4. What mathematical function is the revised Ruby method computing if n > 0?

    5. Using irb, see what happens in the original function if we replace the print statements with a return statement instead:

      def mystery(n)
          value = 1
          for i in 1..n do
              value = value * 2
              return value
          end
      end
      

      Store the function in a file, then load it in irb and call it with different positive integers and observe the results. What do your observations suggest about how the return statement works?

  5. (1.5 pts) Besides learning how to use computational devices to solve a problem, we should understand what happens once the device solves the problem for us. Based on your reading of Chapter 2 (pages 19-42) of Blown To Bits, answer the following questions about the digital data you generate and how all of this digital data affects your privacy.

    1. In your own words, describe briefly what distinguishes a "digital footprint" from a "digital fingerprint".

    2. In your own words, describe one example from the book of how you might leave "digital fingerprints".

    3. Recall the example about the data loss that occurred in 2007 due to an error made by a staff member at the British national tax agency. What changes in technology makes it easier for people to make errors like the one in this example? Briefly discuss the remedies can you think of and their potential shortcomings.