CS140 -- Lab 6

Brad Vander Zanden

This lab will give you practice:

  1. using singly linked lists, and
  2. 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)


You will need the following files:


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:

  1. An empty file
  2. An extremely large file
  3. Files with ties among two or more contestants
  4. Command lines with too few arguments
  5. Filenames that do not exist
  6. 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.