15110 Spring 2012 [Touretzky/Kaynar]

Lab 5 - Thursday, September 27, 2012

Deliverables

  1. count.rb
  2. answers.txt
  3. area.rb
  4. multiple.rb
  5. sum_length.rb
  6. n_factorials.rb
  7. ordered.rb
  8. bcount.rb
  9. product.rb
  10. linsearch.rb
  11. double.rb

Place these files in a lab5 folder. Before leaving lab, zip up the lab5 folder and hand the zip file in.

CA-Led Activities/Demonstration

Self-Directed Activities

Each of the following programs has a bug in it. For each program, first, identify an input for which it gives an incorrect output. In answers.txt, record:

  1. what input you used to demonstrate the bug,
  2. what output you expected for each input,
  3. what output you got instead.

In addition, correct each function and save a copy of that corrected function as indicated.

  1. The area of a triangle is the half of the product of its height h and its base b as expressed by the formula

    \[area = \frac{1}{2}hb.\]

    Identify and correct the error in the Ruby function area_triangle(h,b) listed below. It should take a two parameters named h and b and is intended to return the corresponding value of area according to the formula above.

       def area_triangle(h,b)
         return 1/2 * h * b
       end
      

    Save the corrected version as area.rb

  2. Identify and correct the error in the following Ruby function, which is meant to return true if m is a multiple of n , and return false otherwise.

     
       def multipleof?(m, n)
         if m % n == 0 do
           return true
         else
           return false
         end
       end
       

    Save the corrected version of the function as multiple.rb.

  3. Identify and correct the error in the following Ruby function, which is meant to calculate the sum of the lengths of the strings in string_list.

      def sum_length(string_list)
        for s in string_list do
          sum = sum + s.length
        end
        return sum
      end
      

    Save the corrected version as sum_length.rb.

  4. Identify and correct the error in the following Ruby function, which is meant to print out a table of factorials from 0 to n:

      def n_factorials(n) 
        for i in 0..n do
          x = 1
          for j in 1..i do
            x = x * j
            print i, "! \tis\t", x, "\n"
          end
        end 
      end
    

    Save the corrected version as n_factorials.rb.

  5. The Ruby function ordered?(k, m, n) is intended to take three integers as parameters and check if k < m < n . Identify and correct the error in the Ruby function listed below.

    def ordered?(k, m, n)
      if k < m < n then 
        return true
      else 
        return false
      end 
    end
    

    Store the corrected version of the function in ordered.rb.

  6. The following function is intended to print the number of occurrences of a given key in a list by counting backwards in the list, starting from the end of the list.

    def backward_count(list, key)
      count = 0
      len = list.length
      for i in (len-1)..0 do
        if list[i] == key then
          count = count + 1;
        end
      return count
    end
    

    Save the corrected version of this function as bcount.rb

  7. The following Ruby function attempts to calculate and return the product of the numbers in list. Identify and correct the error in this function.

           
    def product(list)
      prod = 1
      i = 0
      while i <= list.length do
        prod = prod * list[i]
        i = i + 1
      end
      return prod
    end
    

    Store the corrected version of the function in product.rb

  8. Consider the following function, which uses recursion to do linear search to find out whether key appears in the array list.

    def linsearch?(list, key)
      return false if list.length - 1 == 0
      return true if list.first == key
      return search?(list[1..list.length-1], key)
    end
    

    Put the corrected function in linsearch.rb.

  9. The following Ruby functon is intended to compute 2n recursively, for positive integers n.

    def double(n)
      return 2 + double(n-1)
      return 0 if n == 0
    end
    

    Save the corrected version of your function as double.rb.