Note: In using this to help prepare for the midterm in any given semester, please keep in mind that the material covered in your semester might differ somewhat from that covered when this exam was given.

Your name __________________________

Harvey Mudd College

CS 60 Mid-Term Exam

Spring semester, 1995

Five Problems

Open book and notes

The exam is planned for 1 hour, 15 mins., ending at 4:00 p.m. You may take up to 15 minutes longer if the room is not being used for something else. I am sorry that I can't be with you, but a death in the family has taken me off campus this afternoon. Please give your exam to Nancy in the CS office, 240 Olin, when you are finished.

Please provide answers to the problem groups directly on these pages. It is suggested that you look over the exam first to get an idea of how to apportion your time. Work so as to maximize total points; do not get stuck on one problem at the expense of others.

During the exam, you may refer to any of your own reference materials, but consulting with others or using their materials is forbidden.

When code is requested, exhibiting the correct idea is more important than syntactic precision.

The answers to these problems need not be exceptionally long. Please think through your solution before you plunge into a long exposition that might not address the main point of the question.


1. [20 points]

The function asc (for "ascending") creates from a flat list of numbers a list of lists, where the lists are the longest possible ascending sub-sequences of the original list. For example,

asc( [1, 3, 4, 5, 2, 5, 6, 1, 3, 2, 5, 7] )

==> [[1, 3, 4, 5], [2, 5, 6], [1, 3], [2, 5, 7]]

This function can be used to create a more efficient merge sort than the one in the notes, since it capitalizes on already-sorted sub-sequences.

Give a recursive definition of asc using rex rules or, if you prefer, polys in C++. Make sure your rules work with the example given. [Hint: Use an auxiliary function to simplify your presentation.]


2. [20 points]

Ring structures of the type below are sometimes used as a basis for text editors. Construct a reasonably efficient C++ procedure which will accomplish linking of two structures, in the manner shown below:

before linking:

after linking:

Give nominal definitions for the cell structs that you use, so that the code makes sense.

Make sure your code works for the example given.


3. [20 points]

Binomial trees b(N), N = 0, 1, 2, ... are defined inductively as follows:

Basis: b(0) consists of a single node.

Induction step: b(N+1) consists of two copies of b(N), such that the root of one copy is the root of the overall tree and is the parent of the root of the other.

The induction step is suggested by the following diagram:

Thus the first few binomial trees appear as follows:

Evidently, binomial trees are "multi-ary" trees in the sense of the class notes, and can thus be represented as lists: The list is identified with the root of the tree and the elements of the list with the subtrees immediately below the root. Therefore b(0) is the empty list [ ], since the root has no children. b(1) is [ [ ] ] since it is a root with one immediate sub-tree b(0), etc.

a. Give the list representations of b(2) and b(3).

b. Give a rex function which gives b(N) as a list, for arbitrary N.


4. [15 points]

The Smithsonian wishes to use object-oriented programming techniques to organize information about its large collection of vehicles (automobiles, bicycles, motorboats, sailboats, submarines, airplanes, hover craft, balloons, horse-drawn carts, etc.). It wants to structure the collection using classes. In addition to having the above types of vehicles as classes, it might have the following classes: vehicle, motor_powered_vehicle, nature_powered_vehicle, personally_powered_vehicle, animal_powered_vehicle, land_vehicle, air_vehicle, water_vehicle, amphibious_vehicle, sumbmarine_vehicle, etc.

a. Show, using a diagram, how inheritance could be used to organize these classes. Be on the lookout for any cases of multiple inheritance.

b. Where would you include in the class definitions a "speed" attribute, giving the nominal speed for that class of vehicle? (Note that for some types of vehicles, there might be more than one speed, e.g. an amphibious vehicle might have a different land speed and water speed.)


5. [25 points]

For a certain real-time computer input device, it is desired to construct a combinational switching circuit which can translate two digits of BCD (binary-coded decimal) into straight binary. Thus the range of numbers to be handled is 0 to 99. A sketch of the device is shown below.

a. What is the smallest possible value of k (one less than the number of bits in the binary representation)?

b. Express the most significant output bit ck as a function of {a0, a1, a2, a3, b0, b1, b2, b3}. Use Karnaugh maps to make your expression as simple as possible. (Don't forget to exploit any don't-care situations).