CSCI 1323 (Discrete Structures), Spring 2013:
Homework 8

Credit:
20 points.

Reading

Be sure you have read sections 1.6 and 2.3 of the textbook.

Problems

Do the following problems. You do not need to turn in answers for the ones marked ``Not to turn in''. Most such problems will be those for which the textbook provides an answer in the back of the book, so you can check your work.

  1. (Not to turn in.) Do problem 8 on p. 83 of the textbook.

  2. (Not to turn in.) Do problem 10 on p. 83 of the textbook.

  3. (5 points) Do problem 14 on p. 84 of the textbook.

  4. (Not to turn in.) Do problem 2 on p. 124.

  5. (5 points) Do problem 1 on p. 124 of the textbook.

  6. (Not to turn in.) Do problem 10 on p. 126 of the textbook.

  7. (5 points) Do problem 16 on p. 128 of the textbook.

  8. (5 points) Below is a Scala function that uses binary search to search an array for an element, returning either the index1 where the element occurs in the array, or -1 if the element is not in the array.
    // returns index where x was found, or -1 if not found
    def binarySearch(a : Array[Int], x : Int) : Int = {
      var left = 0
      var right = a.size-1
      var result = -1;
      while ((left <= right) && (result == -1)) {
        val mid = (left+right)/2
        if (x < a(mid)) 
          right = mid-1
        else if (x > a(mid))
          left = mid+1
        else
          result = mid
      }
      result
    }
    
    A slightly more formal way to say what this function does is to say that the loop establishes the following postcondition:

    If x occurs at least once in a, then a(result) equals x; otherwise result is -1.

    Answer the following questions, using the two examples from class (4/30) as models.



Footnotes

... index1
More precisely, ``an index'', since it could occur more than once.


Berna Massingill
2013-05-01