CSC 311

Winter 2002

Sample midterm questions

  1. Write a function called ilog that calculates the integer base-2 logarithm of a positive integer.  Use the following recursive definition:

        ilog(1) = 0
        ilog(n) = 1 + ilog(n/2), if n > 1
  2. Write a recursive function for finding the maximum value in an integer array.  Note that the function should be given the array and two indices and should then find the maximum value among the values between the two indices.
  3. Analyze the running time of the maximum function you just wrote.  How many comparisons does it perform?
  4.  
  5. Define a class called StopWatch that simulates the behavior of a stop watch.  It should have a member function to reset the watch, start it, stop it, advance it one second, and read off the time in minutes and seconds.
  6. Implement the member functions of the StopWatch class.
  7. Write a function that is given a string containing (among other characters) parentheses and square brackets.  It should return true if the parentheses and brackets are balanced and nested correctly and false otherwise.  
  8. Write a function that is given a string containing an arithmetic expression in postfix form and that returns the value of that expression.
  9. Define a class called Die that simulates the behavior of a die.  It should (at least) provide member functions to allow you to roll the die and read off the number on the top.
  10. Implement the member functions of the Die class.