CS 112a
Fall 2000
Ileana Streinu
Homework 4
Thursday Sept. 28, 2000
Due Wednesday 10/4/2000
by midnight
Outline
Your task is to write and submit two programs, prog1.cpp and
prog2.cpp (described below), together with a
typescript showing that they work, and that you have
tested them thouroughly.
The first program, prog1.cpp is a direct
application of the concepts learned in class this week
to the problem done in hw 2,
and consists in creating and testing a generic
function for sorting using the Insertion Sort algorithm.
The second program prog2.cpp consists mainly of a function which
uses a sorting function to solve a "real" problem, described below.
To make the task of testing and grading this homework easier for
me and the TAs, your programs should have a very basic user
interface, described below for each program.
Each program will have the very simple structure of reading
the data from the standard input, printing
it, calling the appropriate function and then printing the result
(similar to the main program used in class and in the lab this week for testing
Selection Sort).
The internal array(s) used for both programs should have a
size of exactly 20 elements.
The typescript file should be named exactly like this, and
should be short and readable - in particular, it should
NOT contain any junk from emacs executions. It should show
just the compilation and execution of these two programs, one
after another, on appropriately chosen input arrays.
Please remember the rules for structuring your program that I
discussed in class, make your code readable and be
generous with comments. No late submissions will be
accepted.
Details below.
prog1.cpp
This program is based on the Insertion Sort algorithm which you
have implemented in Hw 2. See also the code I made available in
Lecture 6.
Your task is to extend it now to be a
generic sorting function capable of sorting not just integers and
characters, but also arrays of records (just like what we did in
class this week with Selection Sort).
The data set for testing this program should be an array
of integers (ending with a 0), an array of characters (ending
with a dot .) and an array of records (ending with 0 0). For
testing purposes, you will use the CustomerRecord struct for
records, as done in class today.
For example, a possible data file use dfor testing prog1 might
be:
8 5 3 9 2 6 1 3 4 0
r f e s d w a c z .
8 13 5 26 3 23 9 83 2 24 6 109 1 10 3 290 4 18 0 0
Warning: in the second program, prog2.cpp, you may want to use
this generic InsertionSort function on a different type of record
(struct), which you will design there. But you will have to test it
separately, and provide the appropriate
reading, printing and comparing functions for making the program
work.
prog2.cpp
The problem
Assume that the organizers of a race have asked you to write a program
that will display the sorted list of the joggers, the fastest first,
the slowest last.
The way the organizers have set the race up is as follows.
- At fixed times, they record the exact time on a sheet of paper.
For example 9:15:34:90 (hours, minutes, seconds, hundredths of a second).
We will refer to these times as the absolute time marks.
- At the time they write this absolute time down, the organizers
also start
a chronograph and begin letting the runners go. For each runner who
leaves, they record the runner's T-shirt number, and the value of the
chronograph. We refer to this second time as the relative start
time. For example
101 0:0:0:01
102 0:0:0:17
This means
that Runner 101 left one hundredths of a second after the absolute time
mark of 9:15:34:90, and Runner 102 left 17 hundredths of a second after
that same time mark of 9:15:34:90.
In other words, Runner 101 left at 9:15:34:91, and
Runner 2 at 9:15:35:07.
- When the runners arrive near the finish line, usually in
clusters, the organizers use the same process and write down an
absolute time down, start a chronograph, and record the relative time
of arrival of each runner.
What the organizers give you is a data file with the information
formatted in a very precise way. The example below will give you an
idea of the format used.
4
888 9 15 34 90
101 0 0 0 01
102 0 0 0 17
888 9 16 00 01
103 0 0 0 0
888 9 16 13 03
105 0 0 1 13
999 12 30 34 01
101 0 0 10 32
103 0 0 11 00
105 0 0 12 03
999 12 32 33 03
102 0 0 0 1
0
The example above shows that 4 runners
participated in the race. This number
will always be the first one listed in the data file.
We also see that the runners started in 3 groups, each
specified by an absolute start time (lines starting with the
number code
888), and arrived in two groups, one around 12:30:34, one
around 12:32:33. The absolute time of arrival is marked by the
number code 999 at the beginning of a line. Runner 102 arrived
1 hundredth of a second after 12:32:33:03, or at exactly
12:32:33:04. For simplicity, colons are ommitted from the data
file, and the time is recorded as four integers separated by
spaces. The last line contains just one entry - a zero - which
should suffice to denote the end of the input data.
Your assignment is to compute the amount of time each runner took to
complete the race, and to output the list of runners ordered by
completion time. The fastest runner first, the slowest last.
The program
The problem should be implemented as function called
Race. The function reads the data from the standard input (using a
function Input). The data is presented in the format
described above. Then the function does whatever computations are needed to
come up with an array of records, which are then sorted by the
running time. Each record contains the id of a jogger and the
time it took for completing the race. In the end, the list of
runners is printed, fastest first, slowest last. For sorting, you
can use either the generic (template)
Selection Sort (done in class) or Insertion Sort
(done in prog1), furnished with the appropriate Compare function
(to compare the running times of two joggers).
Submit using submit Hw4..
Last modified Sept. 25, 2000.
Ileana Streinu