CS302 -- Lab 2 - Golf Handicaps



In this lab, you will write a non-trivial application that will make you use trees and fields.

In the game of golf, players can rank themselves with a handicap. This is a number that basically tells you how good you are at the game. The lower your handicap, the better you are.

The US Golf Association has a very lengthy description of how you calculate a handicap. It may be found at http://www.usga.org/handicap/, if you're interested. I'm going to simplify it a bit. Here is how you calculate a handicap for a golfer:

For example, suppose I have played 20 rounds of golf from the white tees at Three Ridges golf course, whose rating is 69.3 and whose slope is 119. On ten of the rounds, I shot 88 and on the other ten, I shot 85. Then I would compute my handicap as follows:

Your Job

Your job is to write the program handicap. It should take two command line arguments:
handicap score-file course-file
The score-file is a file that contains scores. Each line of this file is in the following format:
Month Day Year Name Score Course
Month is a number between 1 and 12. Day is a number between 1 and 31. You do not have to error check for legal month/day combinations (i.e. don't worry about 2/30). Year is the year (i.e. 1999, 2000 -- no Y2K bugs on this program). Name is a one-word name of a golfer, and Score is an integer score. Course is the name of a course, and may contain any number of words separated by white space. The scores can be in any order, and there can be any number of golfers in the score file.

There are example score files in /home/parker/courses/cs302/labs/lab2:

The course-file is a file that contains golf courses plus their ratings and slopes. This file has a more loose structure than the score file. It contains three kinds of lines:
  1. Course Lines: These start with the word ``Course'' and then contain the name of the golf course. Again, the name is words separated by white space.
  2. Rating/Slope Lines: These have the word ``Rating'' then the rating (which is a floating point number), then the word ``Slope'' and then the slope (which you should also treat as a floating point number). When you encounter this line, it is the rating and slope for the course named on the most recently encountered ``Course'' line.
  3. All Other Lines: All other lines should be ignored.
An example (the one you should use for your testing) is in courses. Look at the first four lines:

Course Three Ridges -- White Tees
Rating 69.3 Slope 119
Par     72

This says that there is a course that's called ``Three Ridges -- White Tees'' with a rating of 69.3 and a slope of 119. You ignore the ``Par'' line, and the blank line after the ``Par'' line.

In both files (scores and courses) you should create a string for a course that is composed of each word separated by a space. For example, the following course specifications should be equivalent:

Course Three Ridges -- White Tees

Course      Three          Ridges          -- White            Tees

Now, your program must read in both of these files, and exit if it sees any errors. If both files are ok, and all golfers have at least 20 scores, then it should compute the handicap for each golfer, and then print out each golfer and his or her handicap ordered by handicap (lowest first). Print out the handicap first (padded to 5 characters and two decimal places), and then the golfer's name.

For example:

UNIX> handicap score1 courses
14.31 Jim
UNIX> handicap score2 courses
 3.70 Phil
14.31 Jim
UNIX> handicap score3 courses
 3.70 Phil
14.31 Jim
UNIX> handicap bigscore courses
 2.58 Tiger
 8.26 Phil
 8.31 Sergio
 9.63 David
 9.77 Anika
10.12 Jose
18.12 Ernie
18.21 Colin
18.50 John
18.88 Se-Ri
39.55 Karrie
UNIX> handicap scorebad1 courses
Jim: Less than 20 scores (only 4)
UNIX> handicap scorebad2 courses
scorebad2 Line 1: Course `` Three Ridges -- Ebony Tees'' not found in course file
UNIX> handicap scorebad3 courses
scorebad3 Line 1: Bad Month
UNIX> handicap scorebad4 courses
scorebad4 Line 1: Bad Score
UNIX> handicap scorebad5 courses
scorebad5 Line 1: No course declaration
Should be month day year name score course
UNIX>                  
A working executable is available in /home/parker/courses/cs302/labs/lab2. If you set the environment variable PRINTDIFFS to be "yes", then the program will also print out each golfer's differential and date number (defined in step 4 below) or each score. You can use this to test yourself in case your computations do not seem to match those here: (note, you do not have to implement this feature. It is just included it so that you can help test your own code).
UNIX> setenv PRINTDIFFS yes
UNIX> handicap score1 courses
Jim
  Dnum: 743660   Differential: 17.76
  Dnum: 743661   Differential: 17.76
  Dnum: 743691   Differential: 14.91
  Dnum: 743693   Differential: 14.91
  Dnum: 743722   Differential: 17.76
  Dnum: 743725   Differential: 17.76
  Dnum: 743753   Differential: 14.91
  Dnum: 743757   Differential: 14.91
  Dnum: 743784   Differential: 17.76
  Dnum: 743789   Differential: 17.76
  Dnum: 743815   Differential: 14.91
  Dnum: 743821   Differential: 14.91
  Dnum: 743846   Differential: 17.76
  Dnum: 743853   Differential: 17.76
  Dnum: 743877   Differential: 14.91
  Dnum: 743885   Differential: 14.91
  Dnum: 743908   Differential: 17.76
  Dnum: 743939   Differential: 14.91
  Dnum: 743970   Differential: 17.76
  Dnum: 744001   Differential: 14.91

14.31 Jim
UNIX> setenv PRINTDIFFS no
UNIX> handicap score1 courses
14.31 Jim
UNIX> 


What To Submit

You should submit the following files:

  1. handicap.h: Even though you are using only one .c file for this assignment you should still place your structs/typedefs in a .h file. Doing so will give you good practice for future labs.
  2. handicap.c
  3. makefile
Remember, you submit labs using the special script discussed on the TA web site.

Suggested steps for writing this program:

Below are outlined suggestions for how to go about writing this program, which you should find helpful.