CS140 -- Lab 6
This lab will give you practice:
- using singly linked lists, and
- checking for
error conditions in the input. Error checking can be tedious, but it is important
in the real world (sloppy error checking is often exploited by computer viruses
in staging their break-ins to your computer)
Materials
You will need the following files:
- Executables for the answers are in the directory
/home/bvz/cs140/labs/lab6 (The executables are
not yet ready). As usual, if you have
questions about how these programs should work, try these.
- A sample input file for countVotes can be found in votes.txt.
You should make up your own sample data as well.
- You will need to include fields.h and sllist.h from
/home/bvz/cs140/include.
- You will need to link in fields.o and sllist.o from
/home/bvz/cs140/objs.
- You must use my singly-linked list library, sllist, for this lab.
countVotes
Suppose the producers of American Idol want to change its format so that
there are a random number of final contestants, rather than only two final
contestants. People text the name of their favorite candidate to
the show, and the winner is the contestant with the most votes.
The producers approach you to write a vote counting
program to determine the winner. For simplicity, your program will
read a file that contains the votes. Each line will contain a contestant's
name (e.g., "Brad Vander Zanden"). Your program should tally the votes
and print the name of the winner, along with the winner's percentage of
the vote. The output should have the form:
Winner's Name xx.x%
If there is a tie, print one line for each of the top vote-getters.
You must use the fields library to read each line of
input. You should use a singly linked
list to keep track of the contestants and the number of votes that
they have received thus far. You need to use a linked list, rather than
an array, because your program does not know in advance how many
contestants there will be.
As an example, if the file votes
contains the lines:
Brad Vander Zanden
Smiley Vander Zanden
Mickey Mouse
Smiley Vander Zanden
Smiley Vander Zanden
Mickey Mouse
then the command:
<UNIX> countVotes votes
should produce the
output:
Smiley Vander Zanden 50.0%
Implementation Advice
The value for a singly-linked list node in my sllist library is a void *.
Normally a linked list node will point to a struct, but in this case you only need to
store one piece of information, the contestant's vote count, so a struct seems like overkill. Go
ahead and use a struct with one field if you wish. However, if you want to make life
easy on yourself, just malloc space for an integer, store the integer in that space,
and pass a pointer to that integer to the sllist's sll_append function. Here
is some sample code for malloc'ing space for an integer and storing an integer in that space:
int *new_int;
new_int = (int *)malloc(sizeof(int));
*new_int = 3;
Testing Suggestions
I have provided one test file in the lab6 directory, votes.txt to
test your program. However, you should test your program for boundary-type
conditions and command line issues, such as:
- An empty file
- An extremely large file
- Files with ties among two or more contestants
- Command lines with too few arguments
- Filenames that do not exist
- Files that contain names with multiple spaces between parts
of the name. For example, "Brad Vander Zanden" and
"Brad Vander Zanden" should be treated as the same name.
What to Submit
You will submit your design document in the lab when the TA calls for it.
Submit a source file named countVotes.c via the normal submission procedure.