15110 Fall 2011 [Cortina/von Ronne]

Written Homework 2 - due Friday, September 16 in class

NOTE THE CORRECTION IN BOLD TO THE RUBY METHOD IN PROBLEM 2. IT ACTUALLY DOESN'T AFFECT THE ANSWERS BUT IT DOES AFFECT YOUR B.M.I.!

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: Check your final answers with irb!!!

    1. (7 + 9 * 8) / (4 - 1 + 5)

    2. 2 ** 2 ** 2 ** 2

    3. 15100 % 2000 % 300 % 45

    4. 18 * 4+5 / 5-2

  2. (2 pts) Consider the following Ruby method that returns the body mass index (BMI) of an individual given the person's height in inches and weight in pounds.

    def compute_bmi(height, weight)
        return weight * 703 / height ** 2     # correction to formula here
    end
    
    1. Suppose Alice has a height of 63 inches and a weight of 132 pounds. If we call this function to compute her BMI as follows:

      compute_bmi(63, 132)

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

    2. Suppose Bob has a height of 72 inches and a weight of 180 pounds, but we call this function with these values mixed up:

      compute_bmi(180, 72)

      Does Ruby report an error? Why or why not?

    3. Suppose we only know that Carol's height is 65 inches, and we try to compute her BMI as follows:

      compute_bmi(65)

      Does a default weight get used in our function or does Ruby complain about this function call? Explain.

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

      def compute_bmi(height, weight)
          print weight * 703 / height ** 2        # correction to formula here
      end
      

      Why does the following computation fail?

      average_bmi = (compute_bmi(63,132) + compute_bmi(72,180) + compute_bmi(65,147)) / 3.0

      NOTE: Don't just say how to fix the function. Explain why the computation fails based on the current function definition.

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

    1. 15 % 0

    2. Math.sqrt(-1)

    3. "2000" + 11

    4. end = "December"

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

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

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

      mystery(6)

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

      Now what mathematical function is this Ruby method computing if n > 0?

    4. 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 * i
              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. (2 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. Give two examples of how you might leave "digital footprints" while driving your vehicle, based on what you read in the chapter. (Write 2-3 sentences for each example you cite.)

    2. Briefly describe one example from the reading where a person's confidential medical records were discovered by linking the anonymized records with another data set.

    3. Identify one example of how stores might digitally track your purchases so they can suggest new items for you to purchase, based on what you read in the chapter. (Write 2-3 sentences to explain how the tracking is done.)