 |
|
Sample midterm questions
|
- 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
- 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.
- Analyze the running time of the maximum function you just wrote. How
many comparisons does it perform?
- 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.
- Implement the member functions of the StopWatch class.
- 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.
- Write a function that is given a string containing an arithmetic
expression in postfix form and that returns the value of that expression.
- 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.
- Implement the member functions of the Die class.


