Your name __________________________
Harvey Mudd College
CS 60 Final Exam
Fall semester, 1997
Closed book
3 hours
5 problems with a total of 13 parts
10 pages
250 points
Instructions
The exam is given under the conditions of the HMC honor code. Consulting with others during the examination is forbidden. You are allowed to use one 8.5" x 11" sheet, double-sided, of your own notes, but no other reference materials.
Please provide solutions to the attached problems 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.
The action items in each problem are shown in bold-face.
Use the back of the previous page if you need more space for a given problem.
There is virtue in concise answers. Think before you write.
When you are asked to sketch code, the main burden is on getting the idea across, not writing something which will compile immediately. Use ample comments to help you do that.
1. [15 points]
Show how to derive the expression for the number of switching functions of n variables.
2. [20 points]
Ratcalc is a calculator for rational number arithmetic. A rational number is simply a ratio of a natural number to a non-zero natural number. A rational number can thus be represented as a pair of natural numbers, the second of which is never 0. Results in Ratcalc are always reduced to lowest terms; for example 2/3 would be returned instead of 4/6. In the language of your choice, but aiming for simplicity and elegance, implement the following Ratcalc functions:
add(M, N) produces the sum of the numbers M and N
mult(M, N) produces the product of the numbers M and N
sum(L) produces the sum of a list of numbers L
prod(L) produces the product of a list of numbers L
For example, were we to use rex, we would have the following computations, with the interpretations added as comments in bold for clarification:
1 rex > add([2, 3], [1, 6]);
[5, 6]
interpreted as 2/3 + 1/6 = 5/6
2 rex > mult([2, 3], [3, 4]);
[1, 2]
interpreted as 2/3 * 3/4 = 1/2
3 rex > sum(map((X)=>[X, X+1], range(2, 4)));
[133, 60]
interpreted as 2/3 + 3/4 + 4/5 = 133/60
4 rex > prod(map((X)=>[X, X+1], range(2, 4)));
[2, 5]
interpreted as 2/3 * 3/4 * 4/5 = 2/5
(You may assume that the gcd function is available and that inputs have been checked for validity, e.g. no 0 denominators).
3. [110 points, 4 parts]
A database is stored as a certain type of tree structure. Each interior (i.e. non-leaf) node of the tree is an array of references, indexed starting at 0. It is desired to access the leaves of this array using Dewey decimal notation (actually a generalization of the one used in libraries), in which there is one or more decimal numerals separated by periods (with no whitespace interspersed). The numerals toward the left are used to select within nodes nearer the root of the tree. For example:
- 3
- Selects node number 3 from the nodes which are targets of the root.
- 1.20
- Selects node number 1 from the nodes which are targets of the root, then node number 20 from that node.
- 3.0.1
- Selects node number 3 from the nodes which are targets of the root, then node number 0 from that node, then node number 1 from that.
The diagram below shows how the node 3.0.1 is located, for example
a. [20 of 110 points]
Construct a grammar for the Dewey decimal notation as described above. Assume that the number of levels is arbitrary and that null string (which would identify the root) is not allowed.
b. [45 of 110 points]
Sketch the structure of a parser for the language defined in part a. Use actual code or pseudo-code. Return an object failure if the input is not valid, and a Vector, array, or list which can be used to access the database if it is valid.
c. [20 of 110 points]
Sketch the structure of a program for accessing the tree database efficiently, given the result of part b.
d. [25 of 110 points]
Analyze the complexity of accessing the tree database (i.e. finding the node represented by a Dewey decimal string) based on your program in part c, not counting the time taken to parse the string itself. Indicate what size-parameter you are using, and state clearly any assumptions you are making in your analysis.
4. [50 points, 3 parts]
Similar to the previous problem, the Dewey binary system is like Dewey decimal, except that only binary numerals are used. Thus the alphabet is just {'0', '1', '.'}. For example, this is a valid string in Dewey binary:
1.101.0
a. [15 of 50 points]
Construct a finite-state machine (an acceptor) which accepts the Dewey binary language.
b. [5 of 50 points]
Give a regular expression for the Dewey binary language.
c. [30 of 50 points]
For the finite-state machine you constructed in part a, construct a simplified switching-circuit implementation of the state-transition part. Make the logic as simple as you can.
5. [55 points, 4 parts]
Background: An inexpensive hardware multiplier for binary numerals works by using a register p containing a partial product which is shifted to the right on each step (corresponding to shifting the multiplicand y to the left) on each clock tick, and adding y to the shifted p or not, depending on the low-order bit of the multiplier x, which is also shifted right together with p.
The essence of this algorithm can be expressed as the following program, where x, y, and p are all integer values (assumed non-negative). Instead of shifting p right, we are in effect, shifting y to the left and x to the right. Recall that %2 gives the remainder of dividing by 2, and that /2 gives the integer portion of that division (with no fraction).
p = 0;while( x > 0 ) { p = p + (x%2)*y; x = x/2; y = y*2; }
a. [5 of 55 points]
Show as a table the result of hand-simulating the program for input values x = 5 and y = 7.
b. [10 of 55 points]
State the general method for proving that a program is partially correct with respect to a pair of assertions, one for the input and one for the output.
c. [30 of 55 points]
Prove that the multiplication program is correct with respect to the following assertions, assuming throughout that no quantities are negative:
input: x == x0 y == y0
output: p == x0 * y0
In order to do this, you will probably want to devise a loop invariant.
Hint 1: Such an invariant exists as an equation involving x, y, p, x0, and y0. Find this invariant and check it against your simulation in part a.
Hint 2: To simplify your proof, consider separately the two cases where x is even vs. odd. Note that integer division x / 2 in the odd case (which truncates) is the same as ordinary division (x - 1) / 2, whereas in the even case, both integer and ordinary division give the same result.
d. [10 of 55 points]
Describe how you would prove that the program terminates.
6. Extra Credit [maximum 50 points, not to exceed a total of 250 points on the exam]
If there was some question or questions on which you felt your answer was lacking, use this space to expound, using your own words, on some aspect of the course you feel you know particularly well.