ESc 014
Assignment 6 -- The Duck Race Revisited
Due Tuesday, May 21, 2002
Objectives
- To practice using the get command to parse an
input file
- To practice sorting
Introduction
Remember the Duck Race assignment from ESc 013? It involved Union's
traditional senior class fundraiser. People contribute a small
amount a money to sponsor an artificial duck. The ducks are
then raced in a stream flowing through Jackson's Gardens.
Last term, you were asked to produce two reports about who sponsored
which duck, and the order in which the ducks finished. You'll be
redoing those two reports here, but using the more advanced C++
concepts you've learned up to now. This will make your program more
efficient and better able to handle more generalized input.
Your Mission
Download the input file ("duckrace.txt") from
BlackBoard. Each
line of this file contains a person's name,
the number of the duck s/he sponsored, and the place
that the duck finished in the race. For example,
part of the input file may look like this:
Jimmy Carter 11 6
Prince 3 8
Samuel L Jackson 5 2
This means that Jimmy Carter sponsored duck #11 that finished in
sixth place, while Samuel L Jackson's duck #5 came in second.
Note that the names of the sponsors could take one of three forms:
- a first name and a last name separated by a space
- a first name only, like Prince above
- a first name, followed by a space, followed by a middle
initial (a single character), followed by a space, followed
by a last name
Your job is to write two reports to two separate output files.
Both are tables of sponsors, duck numbers, and duck finishing
places, similar to what you see below. The first report is
sorted by duck number and lists the sponsor's last name only
(or first name in the case of only a single name).
The second report is sorted by duck
finishing place and lists the sponsor's full name.
Report 1 - Duck Order
Duck # |
Name |
Place |
3 |
Prince |
8 |
5 |
Jackson |
2 |
11 |
Carter |
6 |
... |
|
|
|
|
Report 2 - Place Order
Place |
Name |
Duck # |
2 |
Samuel L Jackson |
5 |
6 |
Jimmy Carter |
11 |
8 |
Prince |
3 |
... |
|
|
|
|
The Details
You are to use a single array of structs to hold the data.
You may assume that the array will hold no more than 50
sponsors. Each
struct will have the following components:
- a string for the first name
- a character for the middle initial
- a string for the last name
- an int for the duck number
- an int for the finishing place of the duck
The algorithm for your program (that is, the step-by-step process of
what you need to do) follows:
- Initialize the array of structs. Set int variables to zero,
character variables to a space, and string variables to the
empty string ("" -- that's two double quotes with nothing in
between).
- Read the input from the file into the array. You are
required to use the get command to read the file
in character by character.
- Use an in-place sort routine (SelectionSort is fine) for
sorting
the structs in the array by duck number.
- Write out the first report. Since the array is now sorted
by duck number, you just need to write out the array items in order.
- Resort the array (in-place) by duck finishing place.
- Write out the second report.
You are required to use the following functions:
- an initializeArray function to initialize
the array of structs. It will
take the array as a parameter. This will accomplish
step (1) in the algorithm.
- a createArray function to read in the input
file into the array. This will do step (2)
in the algorithm. This function should take the array
as a parameter and return the number of structs
in the array (the number of duck sponsors) since you'll need this figure
for the sorting and printing functions below.
- a sortArray function to accomplish the sort (steps (3) and
(5)). It will take the array and the number of structs in the
array as parameters. You may need two such functions to accomplish the
two
different sorts.
Extra Credit Alert: Using the same function
twice would be preferable.
- a printReport function that takes three
parameters: the array, the number of structs in the array, and
an output filestream pointer. This function will print the
contents of the sorted array to the specified output file.
(steps (4) and (6)).
This one function should be used to write out both reports.
As always, you are encouraged to use other functions too, but
the ones above are required.
The Hard Part
The most maddening part of this assignment will be parsing the
input file in the correct way in the createArray
function. Write the function down in English first. You
may want to create other "helper" functions
such as getFirstName or
getLastName that break the task down even further.
Use the character functions in the <cctype>
library (p. 356) to check for different cases. Test as you go.
Grading
50 points will be divided as follows:
- 20 points for a working program
- 10 points for a proper sorting routine
- 10 points for appropriate parsing of the input file
- 10 points for appropriate documentation, spacing,
testing, and C++ language usage.
As always, paste in your test results at the bottom of your
source code and turn in a hard copy of this assignment along with an
electronic copy on BlackBoard.
Administrative statement
This is an individual project. I encourage you to
talk to
others about the general nature of the project and ideas about how to
pursue it.
However, the technical work, the writing, and the inspiration behind
these must
be substantially your own. If any person besides you contributes in any
way to
the project, you must credit their work in your report. Similarly, if
you
include information that you have gleaned from other published sources,
you must
cite them as references. Looking at, and/or copying, other people's
programs or
written work is inappropriate, and will be considered cheating.
Return to Assignment Index