Spring 2000

Ileana Streinu

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.

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

- (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)

{

a[0]=0;

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

a[i] = a[i-1] + i;

}

else

{

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

a[i]=i;

}

....

- (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 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**<**.

- (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**CustomerRecord**s, 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.

**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.- (2 points) The Student class description should be in a file named
.........................

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

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

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

- (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).

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

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

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

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

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

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

- (2 points) The Student class description should be in a file named
.........................
- (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.

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

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

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

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:
- All files for the
**CookieJar**class. They should be named**CookieJar**, with the appropriate extension. - The main program, named
**CookieDispenser**. - The Makefile for compiling your program.
- A typescript showing what you have accomplished.
- All submitted by 10:20 as
**submit Mid**.

Last modified March 8, 2000.

Ileana Streinu