15-110 Fall 2012 [Touretzky/Kaynar]

Problem Set 2 - due Friday, September 14 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.



  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
    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) In the twelve-tone well-tempered scale used in Western music, successive notes differ in frequency by a factor of \(2^{1/12}\). For example, concert A has a frequency of 440 Hz; the note one semitone above that, A#, has a frequency of \(440 \times 2^{1/12} = 466\) Hz. The note two semitones above A is B, with a frequency of \(440 \times 2^{2/12} = 494\) Hz. Given a base frequency and a number of semitones to move from the base, the following Ruby function calculates the frequency of that note.

    def compute_frequency(base, n)
        # computes the frequency of a note n semitones above the base frequency
        # in a well-tempered scale
        return base * 2 ** (n/12.0)
    1. Suppose we use this function to compute the frequency of a note 12 semitones (one entire octave) above concert A:

      compute_frequency(440, 12)

      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 base frequency only:


      What error does Ruby give?

    3. Show how to modify the function to include a default value of 0 for the second argument.

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


      Does Ruby report an error? Why or why not?

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

      def print_frequency(base, n)
          # computes the frequence of a note n semitones above the base frequency
          # in a well-tempered scale
          print base * 2 ** (n/12.0)

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

      note = print_frequency(440, 5)

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

    1. sqrt(cos(PI))

    2. 1 + "one"

    3. for = 3

    4. -1 % 0

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

    def mystery(n)
        value = 0
        for i in 1..n do
            value = value + 3
            print value
            print "\n"    # print a newline character

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


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

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


    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 + 3
              return value

      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.