CS 112b
Spring 2004
Ileana Streinu
Midterm Exam
Thursday, March 11, 2004 10:30-11:50am and 1:00-2: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 II: hands-on programming (40 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 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.
After that, the information is sorted twice: once by the major,
then by the time, and printed after each sorting.
Up to here, a perfect program is worth 30 points. When you are done with this part, submit as mid1.cpp, then
continue with the rest.
Finally, the actual processing is done. In the array sorted by major, you must add up
all the hour values corresponding to the same major id number, and the number
of students who reported to having that major. In other words, for each major id i, you
compute the total number of reported hours t[i], and the total number s[i] of students who
reported those numbers. Finally, you compute the average a[i] of reported hours, for each major.
When done, print the resulting array of averages.
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.
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 is 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 last version of your code which works.
- If you have not finished debugging, but have more code than in the last version that works,
submit the undebugged code in a file named midx.cpp. This should be the last version of you code,
as of 2:50pm.
- A typescript showing what you have accomplished.
- All submitted by 2:50 as submit Mid.
Ileana Streinu