The idea of this part is to implement and debug the
code answering the final question from the midterm.
Rather than doing that literally, we'll just use that
question as the basis for a bit of programming exercise.
What follows is a slightly modified form of that question.
This is an exercise, though, and so you should feel free to
(and should) invent your own variations.
The program takes a single argument, the name of a file containing
an unknown number of lines of grade data for one student.
The program is invoked like this:
$ ./a.out gradeFile
Each line in the file has three fields,
two giving the course and one the grade. A sample input line is
CSE 303 4.0
Once the file has been read, the program sits in a loop reading
"commands" from stdin. The two commands it knows about have
these forms:
d [dept]
asks for the average GPA of all courses
taken in "dept." Example command: d PHYS.
lt [grade]
asks to list all courses in which
the grade was less than "grade." Example command: lt 3.0
The program has two logical phases. In the first, the data is read
from a file into a data structure your program can get at conveniently.
In the second, each command causes the program to traverse the now stored
data, extract some relevant portion and perhaps compute some result
from what was extracted, and print something.
It might be surprising, but I think you can get a lot of useful
experience out of doing this, even though this genre of problem
is by now undoubtedly so repetitious that you can hardly stand to
think about it. What would be especially useful would be to
implement code for the basic problem (above), and now think of little
things to add to the problem defintion that aren't just trivial
copy-paste-edit's of what you already have.
As an example, I'll offer one idea: each line in the file has not only the
three fields mentioned above, but also the quarter and year, so that
the full line would have this form:
CSE 303 4.0 Autumn 2007
Now add some command that let's the user list/compute by year.
(Once that's done, let them compute by year-interval -- from 2006
to 2008, for example.) Etc.
The point of this example is that it is minor, in the sense of not
making any wholesale change to the intent of the program, but isn't
just more of the same: the "input field" is now two fields, according
to how scanf()
works, and (more importantly) not all
commands are now two tokens long. (Another example: extend the 'd'
command to allow listing any number of departments, and print the
average GPA in all courses in any of the listed departments.)
Attempting to make changes to an existing program will help you appreciate
the distinction between "clean" code, at one extreme, and a mess, at the
other.
This might seem tedious, but one way or another you're going to go
through this process. I think exercises now will be a lot more pleasant,
on the whole, than trying to go through it as part of completing the
sequence of assignments that lies ahead (in other CSE courses, I mean -
the CSE 303 assignments will all be a joy, of course).