CS 112b
Spring 2002
Ileana Streinu
Midterm Exam
Thursday, March 14, 2001 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-1:15 hour,
but this may vary. Part II is
done on the computer, and submitted in the directory Mid
by 10:20. You can start on Part II whenever you are done with
Part I, and you can go back to Part I later to answer a
question.
If you are done before 10:20, turn in the filled in exam sheet to
me in McConnell 210.
Otherwise I
will pick up the exams at 10:20 SHARP (no
extensions allowed, not even 5 minutes).
For Part II, part of the grade will be based on
how readable and well organized your code is.
The exam is open notes, open book, and you may even consult the
computer.
You may use and reuse computer
programs that you have written previously. But you should not
communicate with anybody during the exam: all the work should be yours.
Part I
Basic C++ object oriented programming concepts and basic data
structures (50 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) that would swap the
values of three variables a,b,c in a circular fashion, i.e. a
will get the value that originally was b's, b will get the
original value of c and c gets the original value of a.
- (3 points) Write a simple
testdriver (main program) to test that the previous function
works correctly. Do not use input/output functions: just test
everything on a few statically initialized variables.
- (3 points) Templates. Extend the previous C++ function
(from question 2) to a
template, capable of working with data of any type.
- (5 points) Functions as parameters.
Write a function to find the minimum value stored in an array
(which is not sorted). The function should work on data of any
type, which may or may not not be compared directly with
<.
Hint: use templates and a function parameter called Compare.
- (4 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.
(11 points) Define a class named Robot. Each Robot
has an id (integer),
a number of legs (recorded as an integer) and a type (denoting
whether it runs on batteries or needs an extension cord)
(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 points) For an implementation meant to lead to code reuse
and which obeys standard software engineering discipline, the
Robot class description should be in a file named
.........................
- (1 points) For an implementation meant to lead to code reuse
and which obeys standard software engineering discipline, the
code for the functions in the
Robot class should be in a file named
.........................
- (1 points) Describe the structure of a Makefile for compiling and
testing your class Robot. 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.
- (1 points) Write the code for an empty Robot
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).
- (1 points) Write the code for a Robot
constructor that takes in three integers
(corresponding to id, number of legs and type values) and initializes the
current object with those values.
- (1 points) Write the code for a Print method, which prints on the
standard output the values of
the three variables of the current Robot object.
- (1 points) Write a statement that declares a variable of type Robot.
- (1 points) Write a statement that declares a variable of type
Robot with initial values (35, 6, 1).
- (1 points) Write a statement that applies the method Print on the object
of type Robot, which has been created at the previous step.
- (1 points) Write a C++ function to assign a number of legs to the
current Robot
object. Separately, write a statement showing how you would use
this function on the variable defined at step 8 above.
- Class templates(5 points):
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 ax, int ay, int ainfo); // 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
}
Rewrite
the class as a template that will accept any data type for information.
- (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 prefix and postfix forms for the following fully parenthesized
arithmetic expression:
(((4+3)/(6-1))*((2+1)*(3+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):
23+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.
- (4 points) Stacks for function calls.
Trace the status of the stack during the execution of the following program.
Redraw the stack after each push and pop, do not overwrite.
main()
{
int x=10,y=1;
y=func1(x,x);
cout << x << y << endl;
}
int func1(int x, int &y)
{
y=x-2;
return (x+3);
}
Part II: hands-on programming (50 points)
Your friends are also college students, but they do not take
programming classes. They are arguing that they spend more time
than you do on homework assignments. You do not agree, and
decide to research the question. You send out a survey to
all houses on campus, asking each student to report her major
(recorded as an integer code between 1 and 99) and
how much time she spent last week doing her homework (in hours).
The data is collected in a file named data. Each line of
the file contains the integer code of a major and the time
reported by a student (also an integer). The last line of the
file is 0 0 and indicates the end of the data. For example:
1 5
2 10
2 15
1 20
3 14
7 3
4 6
4 25
3 12
0 0
Your task is to design a program to read, process and print this
data. The file is read in line by line and stored internally in a
data structure of your choice.
You may complete this program at one of these 4 levels. Submit
the highest level that works, as mid.cpp.
If you already have worked out part of
next level but have not finished it, submit it in a separate
file, named unfinished.cpp.
Realistically, I do not expect many of you to complete the
program all the way to the 4th level. Therefore, do your best and do not
feel that you have to finish everything in the limited time
allocated for this exam (but if you are an exceptionally well
organized programmer, you may be able to finish).
- Level 1: the program reads, stores and prints the data.
- Level 2: the information is sorted twice: once by the major,
then by the time, and printed after each sorting.
- Level 3: compute the average time per major and print
the results.
- Level 4: report the major with the maximum average
time for homework, among those reported.
You can reuse any code that you have written so far in this
class. You should write the whole program in one file. You do not
need to use classes for this program, but you should use
struct if appropriate.
The program will receive the maximum number of points if it obeys
the same rules for writing good and readable code as for the
homeworks. Remember, a program that runs but may not have all the
features implemented is better than one which has all functions
implemented but full of bugs, or does not complile. Therefore develop your
program in stages and submit the latest version that works.
To submit:
- A file named mid.cpp containing your code.
- A typescript showing what you have accomplished.
- (Possibly) a file named unfinished.cpp.
- All submitted by 10:20 as submit Mid.
Ileana Streinu