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

Fall semester, 1995

Seven 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.

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. [10 points]

Using any model for lists you prefer (rex, C++, Polya, etc.) construct code for a function which returns the average of a list of numbers. If given the empty list, this function should return 0.


2. [15 points]

Convert the following C++ program to rex using McCarthy's transformation:

long g(long X, long Y) 
{ 
while( X > Y ) 
{ 
Y = Y + 1; 
X = X - Y; 
} 
return Y; 

3. [15 points]

Recall that an array may be viewed as a kind of function, namely, one which maps a set of indices {0, 1, 2, ...., n-1} into a set of values (which includes the values stored in the arrays). Explain, in your own words, how the representation of 2-dimensional arrays in C++ as an array of arrays is related to the idea of a "Curried" function.


4. [10 points]

The n-th "Fibonacci tree" is the value of F(n), where

F(0) => 1;

F(1) => 1;

F(n) => [F(n-2), F(n-1)];

Give a clean sketch of F(5) as a tree diagram, using any tree representation for lists you prefer. (It is recommended that you sketch recursively, rather than building the list F(5) then converting it.)


5. [10 points]

Carefully code a C++ function str_append which creates a string which consists of the argument strings concatenated together. For example, str_append("mudd", "cs") returns the string "muddcs". For this problem, do not assume that the polya library is available. It is not necessary to use linked lists. The prototype for str_append is:

char* str_append(char* x, char* y); 

6. [15 points]

Assume that mailing lists are represented the following way: There is a list of all mailing lists. Each list begins with the name of the list, followed by the people in the list. (Assume that lists can't appear on lists.) So, for example, if the lists were "students", "faculty", and "staff", the overall list of lists might be:

[["students", "Joe", "Heather", "Phil"],

["faculty", "Patti", "Sam", "John", "Cathy"],

["staff", "Phil", "Cathy", "Sheldon"]]

Specify a grammar for the correct construction of such an overall list of lists (with an arbitrary number of lists and list members). Use S for your start symbol. Your grammar need not account for whitespace.

(Note: There are two types of answer: An ordinary grammar would require that you specify each symbol, including brackets, commas, and quote marks. An abstract grammar (corresponding to abstract syntax) gives just the structure without the specific punctuation marks. It is ok to give an abstract grammar, but if you do so, please use it to develop an ordinary grammar as well.)


7. [25 points]

Using the setup from the previous problem, consider an issue arising with email lists. A given person can be on more than one list. It is also common to send mail to several lists in one command. Often several people get duplicate mail as a result. We wish to avoid this by creating a function which, given a list of mailing lists, creates a list of the people on those lists so that no person is in the list more than once.

Construct a function send which will take two arguments:

M: A list of names of lists to which to send mail

D: A list of lists, such as the one in problem 6

and which returns a list of the people who are to get the message, such that each person is listed only once. For example,

send(["students", "staff"], 
     [["students", "Joe", "Heather", "Phil"], 
      ["faculty", "Patti", "Sam", "John", "Cathy"], 
       ["staff", "Phil", "Cathy", "Sheldon"]]) 

will return a list such as

["Joe", "Heather", "Phil", "Cathy", "Sheldon"]

This function is to be completely general, not limited to the example lists shown.

Keep your definitions clean and readable. Use the clearest program structure possible.