Assignment

Contents:

  • Overview
  • Internet Requirements
  • Practice Problems
  • Problems to be Submitted
  • Extra Credit

  • Overview

    Topic: Algorithms
    Related Reading: Ch. 6.1, 9.5, and pp. 518-525
    Due:

    Internet Requirements

    You may want an Internet connection for several practice problems. However you do not necessarily need the connection for completing the assignment, other than for submission.

    Practice Problems

  • Exercise 7 of Ch. 6 (p. 182); answer in back of text

  • Exercise 13 of Ch. 6 (p. 182); answer in back of text

  • Exercise 14 of Ch. 9 (p. 315); answer in back of text

  • Exercise 16 of Ch. 9 (p. 315); answer in back of text

  • Consider the following sorted list of numbers:

    2 4 7 10 13 16 17 20 24 28 31 38 41 43 45

  • What sequence of 'middle' values are compared to the target when performing binary search with target 28?

  • What sequence of 'middle' values are compared to the target when performing binary search with target 16?

  • What sequence of 'middle' values are compared to the target when performing binary search with target 5?
  • Note: The solution for the above questions can be found by running a Binary Search Demonstration with parameter "#items: 15" and "seed: 15849" and with target set appropriately.


  • Problems to be Submitted (20 points)

    Our goal for this homework is to have you demonstrate a good understanding for the recursive procedure Binary Search, as discussed in Chapter 9.5. But more generally, we want you to gain an understanding recursion as a general programming technique.

    1. (10 points)
      Consider the following sorted list of numbers:

      2 4 9 16 19 21 25 27 32 33 35 38 41 42 43

      1. What sequence of 'middle' values are compared to the target when performing binary search with target 4?

      2. What sequence of 'middle' values are compared to the target when performing binary search with target 17?

      3. What sequence of 'middle' values are compared to the target when performing binary search with target 22?

      4. What sequence of 'middle' values are compared to the target when performing binary search with target 34?

      5. What sequence of 'middle' values are compared to the target when performing binary search with target 41?

    2. (5 points)
      What value or sequence of values will be printed when running the following recursive routine, called originally as TestA(1):
        define TestA(count)
           if (count<5) then
              print count
              TestA(count+1)
           end
        end
      

    3. (5 points)
      What value or sequence of values will be printed when running the following recursive routine, called originally as TestB(1):
        define TestB(count)
           if (count<5) then
              TestB(count+1)
              print count
           end
        end
      
    Overall, please type your answers to all of the problems in a single document to be submitted electronically. Please see details about the submission process.

    Extra Credit (2 points)

    The goal here is to really challenge your understanding of recursive procedures. A classic problem, known as the Towers of Hanoi can be solved with the following recursive procedure:
    define Towers(n,A,B,C)
      if (n>0) then
        Towers(n-1,A,C,B)
        Print "Move ring <n> from <A> to <C>"
        Towers(n-1,B,A,C)
      end
    end
    

    To prove your understanding, please describe the precise output which is generated when a call is made as Towers(4,X,Y,Z).


    Last modified: 24 March 2003