CS 112b
Spring 2004
Ileana Streinu
Midterm Exam
Thursday, March 11, 2004 10:30-11:50am and 1:00-2:50
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 and 20
minutes, but this may vary. I will pick up the filled in exam sheets by 11:50.
Part II is done on the computer, and submitted in the directory Mid
by 2:50. You can start on Part II whenever you are done with
Part I.
I remind you that we are on the honor code: although there is a one-hour lunch
break between the two parts of your exam, you are not allowed to do any exam-related work during the
lunch break.
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++ object oriented programming concepts and basic data
structures (60 points)
- (3 points) What are the values printed by the cout statements
in the following program? Write those values on the right of
the page next to the program code. Ignore (possible) syntax
errors that this (incomplete) program may contain and concentrate
on its logic.
main(){
int a=0, b=1, c=2;
int choice;
....
// the choice variable gets a value somewhere here
...
switch(choice)
case 0: func0(a,b,c); cout << a << b << c; break;
case 1: func1(a,b,c); cout << a << b << c; break;
case 2: func2(a,b,c); cout << a << b << c; break;
}
func0(int a, int b, int c)
{
a=b;
b=c;
c=a;
}
func1(int &a, int b, int c)
{
a=b;
b=c;
c=a;
}
func2(int &a, int &b, int c)
{
a=b;
b=c;
c=a;
}
- (3 points) Write (in the space below) the definition and full code for a
C++ function (NOT a main program) to compute the position
in a sorted array of n elements where a certain value x is
stored, or to insert it (in the appropriate place,
i.e. maintaining the property that the resulting array is still
sorted), if not already in.
The function returns the index of that position, if found,
or -1 otherwise (if it was not found but has now been inserted),
and it has three parameters: the array, its number
of elements and the value x. For each parameter, specify whether
it is IN, OUT or IN/OUT.
- (2 points) Templates. Extend the previous C++ function to a
template, capable of finding the position of an element that is
of any type that
can be compared with the usual C++ comparison operator
< (such as int, char, float).
- (3 points) Functions as parameters. Extend the
previous C++ templated function to one capable of working with
other data types, which cannot be compared directly with
<. Hint: use a function parameter called Compare
and rewrite the code accordingly.
- (2 points) For the previous situation, give an example of a
specific Compare function, which will compare the ages in two
records of type CustRec, defined by the following struct:
struct CustRec{
int id;
int balance;
int age;
}
- Classes.
(2 points) Define a class named Book. Each Book
has an id (integer),
a color (recorded as an integer code) and a size (denoting its number of pages)
(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) For an implementation meant to lead to code reuse
and which obeys standard software engineering discipline, the
Book class description should be in a file named
.........................
- (2 points) For an implementation meant to lead to code reuse
and which obeys standard software engineering discipline, the
code for the functions in the
Book class should be in a file named
.........................
- (3 points) Describe the structure of a Makefile for compiling and
testing your class Book. The Makefile should obey the
rules of structured object-oriented programming, which each class
compiled separately. Do not worry about spaces and tabs, but give
the right dependencies and separate compilations.
- (3 points) Describe the structure of a Makefile for compiling and
testing the class Book, assuming that you are only
writing the testdriver and get the Book class from a library
of pre-compiled code. More precisely, you should assume that I
have written and compiled the Book class separately,
and have made available to you the necessary files for using it,
but not the class code (.cpp file).
- (2 points) Write the code for an empty Book constructor. By "empty",
I mean that it should 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 Book
constructor that takes in three integers
(corresponding to id, color and size 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 Book object.
- (2 points) Write a statement that declares a variable of type Book.
- (2 points) Write a statement that declares a variable of type
Book with initial values (35, 2, 97).
- (2 points) Write a statement that applies the method Print on the object
of type Book, which has been created at the previous step.
- (2 points) Write a C++ function to assign a color to the
current Book
object. Separately, write a statement showing how you would use
this function on the variable defined at step 8 above.
- Class templates
The following class definition restricts the type of information
associated to each object of type Point to be an integer.
class Point
{
public:
Point(); // empty constructor
Point(int a, int b, int c); // general constructor
Read(); // read from standard input
Print(); // print to standard output
Move(int a, int b); // move a point to a new location
ChangeInfo(int info); // change the information stored with the point
private:
int x,y; // x and y coordinates
int information; // other information
}
- (3 points)Rewrite
the class as a template that will accept any data type for information.
- (3 points) Give an example of how you would use int as
the data type in the declaration of a Point object
(i.e. define a variable of type Point, with int as its data type
for information).
- (3 points) Suppose you want to define two variables of type
Point: one for
which the information stored with the point is a character, the other where
this information is a record previously defined with the following
struct definition:
struct Label{
char name[10];
int nr;
}
Could you declare both variables using the templated class Point?
(Yes/No)
If YES, show here the definitions of these variables.
If NO, could you define at least one of these two variables in
this way? If yes, give the definition for that variable
and a short explanation of why this won't work for the other
variable. Otherwise, explain why none of the variables can be
defined using the templated class Point.
- (3 points) Define a specialized template for a Point with
Label information. You do not have to provide the
implementation of all the functions in the class - just the
equivalent of the header file.
(3 points) Sorting
Run the Insertion Sort algorithm on the following array of
integers:
{ 15, 3, 2, 1, 13, 9}
I mean: I do not want to see only the final sorted array!!
You should work in stages, following the Insertion 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 arithmetic expression tree, the prefix and the postfix forms for the following fully parenthesized
arithmetic expression:
(((1-5)/(3*2))+((3+1)+(2*4)))
(3 points) Evaluating arithmetic expressions in postfix form.
Find the value of the following arithmetic expression written
in postfix form (assumming that we only use one-digit decimal integers):
14*2+42--2/
Apply the algorithm we discussed in class.
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 (40 points)
This part will be distributed to you when you are done with Part I, or after lunch at 1:00pm.
Ileana Streinu