Your name __________________________

Harvey Mudd College

CS 60 Final Exam

Spring semester, 1995

Open book and notes

3 hours

5 problems

6 pages

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 exam is given under the conditions of the HMC honor code. During the exam, you may refer to any of your own reference materials, but consulting with others or using their materials is forbidden.

In each problem, use the back of the previous page if you need more space.


1. [30 points]

The following rex function multiplies a binary numeral, least-significant digit first, by 3, as long as there are two zeroes on the end of the input to allow all digits of the product to emerge.

f(Input) => g(Input, 0). 
g([], _) => []. 
g([Digit | Digits], Carry) => 
	Prod = 3*Digit + Carry, 		%% equational guard 
	[ (Prod mod 2) | g(Digits, Prod // 2)]. 

(Recall that in rex // is integer division, as opposed to floating-point division.)

For example,

Input          representing   gives    Output representing 
[1,0,0]                   1             [1, 1, 0]         3 
[0, 1, 0, 0]              2             [0, 1, 1, 0]      6 
[0,1,1,0,0]               6             [0,1,0,0,1]      18 
[1,0,0,1,1,0,0]          25             [1,1,0,1,0,0,1]  75 

etc.

a. [10/30 points]

It is claimed that the value of Carry, under the stated input assumptions, will never exceed 2. Show that this is the case by constructing a state-diagram for g, where the states are the values of Carry, assuming that the initial value of Carry is 0.

b. [20/30 points]

Using the state diagram you constructed in part a, synthesize a sequential logic circuit which multiplies a binary numeral, least-significant digit first, by 3. State clearly how you have chosen to encode the states and give names to the state variables (flip-flops). Then provide next-state logic equations for each variable. (You do not have to show gates and flip-flops.)

When your construction is complete, show, by hand-simulation of the flip-flop values, etc., how it works on the input 6: [0,1,1,0,0].


2. [15 points]

As a function of n, derive the best possible "O" upper bound on the running time of the C++ function below. Be careful to justify your reasoning. Don't simply guess, as a wrong answer will receive no credit.

void foo(long *a, long n) 
{ 
if( n <= 1 ) 
  return; 
long i = 0, j = 0; 
while( i < n ) 
  { 
  if( a[i] > a[i+1] ) 
    a[j++] = a[i]; 
  else 
    a[j++] = a[i+1]; 
  i += 2; 
  } 
foo(a, j); // recurse 
} 

3. [25 points]

Two regular expressions are called equivalent if they represent the same language. For example, 1(01)* and (10)*1 are equivalent. Suppose we wish to construct a program for determining whether or not pairs of regular expressions are equivalent.

a. [5/25 points]

What input representation would you use for regular expressions, assuming that you wished to spend minimal time addressing this aspect? Justify your choice and give a grammar for your representation of regular expressions.

b. [20/25 points]

Describe the major phases in your equivalence-testing program. Do not write code. Just present a convincing solution method. This may require some delving into composition of finite-state machines, logical operators, etc.


4. [15 points]

Suppose we wish to add a set of "jump immediate" instructions to the ISC. Whereas currently the jump instructions all use a register to contain the target address, the new instructions would replace this register with a displacement, an integer which indicates how far forward (or backward if negative) to jump from the current instruction location.

a. [5/15 points]

How might the availability of the new instructions have simplified programming in the ISC assignment?

b. [10/15 points]

Describe the internal register-transfer steps for the instruction jltim (jump less-than immediate). How many clock ticks would be required to execute this instruction given the ISC diagram as given in the notes? (Copies of the diagram are available at the front of the room if you don't have one.)


5. [15 points]

As we have a discussed, although the nomenclature sounds otherwise, an undirected graph is most easily viewed as a special case of directed graph. That is, each edge (line connecting two nodes) of an undirected graph represents two arcs in the corresponding directed graph.

In the parlance of object-oriented design then, an undirected graph is-a directed graph. Correspondingly, suppose we have access to a C++ class directed_graph, which includes the ability to do the following:

a. Add and remove nodes dynamically.

b. Add and remove arcs dynamically, given the end-point nodes.

How would you go about constructing a C++ class undirected_graph which includes the ability to:

a. Add and remove nodes dynamically.

b. Add and remove edges dynamically, given the end-point nodes.

Your solution should exploit the availability of the class directed_graph. The more it does this, the better. Don't give all of the code, but give enough to illustrate your approach to the problem.