CS 112b
Spring 2000
Ileana Streinu

Midterm Exam

Thursday, March 9, 2000 8:00 - 10:20am

Write your name and class account on this exam sheet NOW.

General description

There are 2 parts. Part I is theory, and you have to write the answers on the exam sheet. This part takes about 1 hour.Part II is done on the computer, and submitted in the directory Mid by 10:20.

Be generous with comments on your Part II program. Part of the grade will be based on how readable and well organized your code is.

Part I
Basic C++ programming concepts and Basic data structures (50 points)

  1. (3 points) What is the following (partial) program doing? (Explain in a few short sentences, in English).

    int a[20];
    int n = 10;
    int state;

    .... // the state variable gets a value somewhere here

    int i;
    if (state == 0)
    for (i = 1 ; i < n ; i++)
    a[i] = a[i-1] + i;

    for (i = 0; i < n ; i++)


  2. (3 points) Write (in the space below) the definition and full code for a C++ function (NOT a main program) to compute the maximum of an array of n elements. The function returns the maximum value (not index), and it has two parameters: the array and its number of elements.

  3. (3 points) Templates. Extend the previous C++ function to a template, capable of finding the maximum of a general array whose elements are of a type that can be compared with the usual C++ comparison operator <.

  4. (5 points) Function parameters. Extend the previous C++ function to a template taking a comparison function as a parameter. The purpose is to be able to find the maximum of even more general arrays, including some types whose elements cannot be directly compared with the usual C++ comparison operator.

    First, you do not need to call this function or produce the concrete Comparison functions: just write the code for the FindMax template function. Then, separately, write C++ code (which can be part of some other program, i.e. it doesn't have to be completed to a full main program) to invoke this function for finding the maximum balance of an array of CustomerRecords, where a CustomerRecord is a struct with two fields: int id and float balance. For this part, you have to rpovide the appropriate function for comparing two balances.

  5. Classes. (2 points) Define a class named Student. Each Student has an id (integer), year (an integer between 1-4, for the year in college) and a grade (integer). In the space below, start writing the class definition, as it would appear in the header file - only the class variables, no functions yet.

    Now finish the definition of the class, as well as make use of this class, by answering the following questions. Use the space below each question to write the answer.
    1. (2 points) The Student class description should be in a file named .........................

    2. (2 points) The implementation of the functions of the Student class should be in a file named .........................

    3. (3 points) Describe the structure of a Makefile for compiling and testing your class Student. The Makefile should obey the rules of structured object-oriented programming, which each class compiled separately.

    4. (3 points) Describe the structure of a Makefile for compiling and testing the class Student, assuming that you are only writing the testdriver and get the Student class from a library of pre-compiled code. More precisely, you should assume that I have written and compiled the Student class separately, and have made available to you the necessary files for using it.

    5. (2 points) Write the code for an empty Student constructor. By "empty", I mean that it is meant to be used in constructing an object of this type without giving it any specific values provided by the user - but rather initializing the variables with some defaults values (e.g. all 0's).

    6. (2 points) Write the code for a Student constructor that takes in three integers (corresponding to id, year and grade values) and initializes the current object with those values.

    7. (2 points) Write the code for a Print method, which prints on the standard output the values of the three variables of the current Student object.

    8. (2 points) Write a statement that declares a variable of type Student.

    9. (2 points) Write a statement that declares a variable of type Student with initial values (35, 2, 97).

    10. (2 points) Write a statement that applies the method Print on the object of type Student, which has been created at the previous step.

    11. (2 points) Write a C++ function to assign a grade to the current Student object. Separately, write a statement showing how you would use this function on the variable defined at step 7 above.

  6. (3 points) Sorting Run the Selection Sort algorithm on the following array of integers:

    { 4, 13, 2, 5, 21, 3}

    Explain whether you are using the min or max version of the algorithm.

    I mean: I do not want to see only the final sorted array!! You should work in stages, following the Selection Sort algorithm. At each stage, write down how the array currently looks. You do not need to explain what the algorithm does. You do not need to write the code for the algorithm. Just show the array, after each stage.

  7. (3 points) Arithmetic expressions in postfix form. Write the postfix form for the following arithmetic expression: (((a+b)*(c-d))/((a/c)-(b+c)))

  8. (4 points) Evaluating arithmetic expressions in postfix form. Find he value of the following arithmetic expression written in postfix form (assumming that we only use one-digit decimal integers): 12+3*23+- Apply the algorithm we discussed in class, and which you implemented for Hw6. Trace the execution of the algorithm, and show in sequence how the stack looks like at each step (i.e. after each push and pop). Redraw the stack, do not overwrite.

Part II: hands-on programming (50 points)

The spaceship Enterprise has a funny Cookie jar. All the cookies put into it are stamped with an integer value. They are very precious and the crew would like to keep track of them. The cookies are pretty big, and the jar is like a narrow container. Once a cookie gets into the jar, it can be removed only after all the cookies that have been put above it are removed. And everything you add to the jar, is added on top of what is already there.

The children on board love these cookies, but they would get sick if they ate too many of them. So I built a tiny programmable robot to keep track of what it in the jar. This allows the crew to put in more cookies, and the robot would dispense them to children upon request. The robot has a C++-programmable brain. Your task is to write the program for it.

You can reuse any code that you have written so far in this class, but you should create a class named CookieJar and a menu-driven main program named CookieDispenser for working with it.

You should design a friendly interface for the cookie-jar uses (assume the children on the spaceship do not know programming, but they can read simple instructions in English and press the keys of the computer keyboard). The program would display a list of options (add a cookie to the jar, remove a cookie, see if there in anything left, see if there is room for more). If there are more that 3 requests for cookies, one after another, the robot should dispense only three, than give an admonition. Dispensing is resumed only after some crew member has added new cookies to the jar. If the jar has become empty, the robot goes to sleep (the program exits), and it can only be revived by captain Picard (by running the program again).

    To submit:
  1. All files for the CookieJar class. They should be named CookieJar, with the appropriate extension.
  2. The main program, named CookieDispenser.
  3. The Makefile for compiling your program.
  4. A typescript showing what you have accomplished.
  5. All submitted by 10:20 as submit Mid.

Last modified March 8, 2000.

Ileana Streinu