CS 112a
Fall 2000
Ileana Streinu
Midterm Exam
Thursday, Oct. 19, 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)
- (6 points) What is the following (partial) program doing? (Explain in
a few very short sentences, in English, on the right of
the page next to the program code).
int a[20];
int n; int x;
int choice;
....
// the choice variable gets a value somewhere here
//and the array a and the variables n and x are initialized
...
int i;
switch(choice)
case 0:
{
v=0; val=a[0];
for (i = 1 ; i < n ; i++)
if (a[i]>val) {val=a[i]; v=i;}
}
cout << val << endl;
case 1:
{
v=0;
for (i = 0 ; i < n ; i++)
if (a[i]==x) v=i;
}
cout << v << endl;
case 2:
{
for (i = 0 ; i < n-1 ; i++)
a[i]=a[i+1];
}
....
- (5 points) Write (in the space below) the definition and full code for a
C++ function (NOT a main program)
to compute the second largest element of an array of n
elements.
The function returns the second largest 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 second largest element
of a general array whose
elements are of a type that
can be compared with the usual C++ comparison operator
<.
- Classes.
(2 points) Define a class named Pumpkin. Each Pumpkin has an id (integer),
a color (recorded as an integer code) and a weight
(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 Pumpkin class description should be in a file named
.........................
- (2 points) The implementation of the functions of the
Pumpkin class should be in a file named
.........................
- (3 points) Describe the structure of a Makefile for compiling and
testing your class Pumpkin. 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 Pumpkin, assuming that you are only
writing the testdriver and get the Pumpkin class from a library
of pre-compiled code. More precisely, you should assume that I
have written and compiled the Pumpkin class separately,
and have made available to you the necessary files for using it,
but not the class code.
- (2 points) Write the code for an empty Pumpkin 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 Pumpkin
constructor that takes in three integers
(corresponding to id, color and weight 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 Pumpkin object.
- (2 points) Write a statement that declares a variable of type Pumpkin.
- (2 points) Write a statement that declares a variable of type
Pumpkin with initial values (35, 2, 97).
- (2 points) Write a statement that applies the method Print on the object
of type Pumpkin, which has been created at the previous step.
- (2 points) Write a C++ function to assign a color to the current Pumpkin
object. Separately, write a statement showing how you would use
this function on the variable defined at step 8 above.
- (3 points) Sorting
Run the Insertion Sort algorithm on the following array of
integers:
{ 5, 23, 21, 15, 13, 19}
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
arithmetic expression:
(((2-3)/(7+1))/((3+2)-(2*3)))
- (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):
34+2*23+-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 (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
decided 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 0 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.
Your task is to design a program to process and print this
data. The file is read in line by line and you compute, for each
code corresponding to a major (a number between 0 and 99) the
average time spent by a student on homework last week.
When done, you arrange the data in decreasing order of the time
spent by students and print it.
You can reuse any code that you have written so far in this
class. You can write the whole program in one file. You do not
need to use classes for this program, but you should use
struct if appropriate.
To submit:
- A file named mid.cpp containing your code.
- A typescript showing what you have accomplished.
- All submitted by 10:20 as submit Mid.
Last modified October 18, 2000.
Ileana Streinu