CPE 101
Spring 2007
Laboratory 10

Due Date

Friday 8 June 5:00pm
You must turn in your source electronically on hornet using the handin command – instructions are provided below.

Objectives
Resources
Orientation...

This lab will involve developing a small C program which handles data about a running "log book".  You will be creating your own record type in order to store the run information used by the running log.  You will need to develop a variety of functions to process this data.  (Note that part of this lab is intended to help you practice using output parameters (pointers), so all of your functions should be void functions.)  Read this entire lab before you begin working.

Part 0: Creating the data type and reading in data...

Start out by defining a record to store run data.  This record must be named run_t and must include:
The lab10skeleton.c file should compile once you define your run_t struct.

You will also be writing a variety of functions which operate on the run data.  You will read an initial 'run' database from file, however, every time the user goes for a run, the program data must change to reflect this.  The run database will hold at most 30 different runs. 

Write a function readFile to read the run data from an input file named run.data and store it in your internal data structure.  This function must populate your array of runs and return the number of runs read from the file (either via the return value or an output parameter).  You must develop this entire function yourself.  You are not given a prototype to work from.

Note that the strings representing the run names have underscores instead of spaces in them.  This program will be much more difficult if you try to implement it with strings that have spaces in them.  Use an underscore whenever you mean to put a space in the run name.

good:  los_osos_loop
bad: los osos loop

Part 1: Creating a function to print the data...

Write a funcion to print out the run data.  The print function should use the PR seconds to print the PR time in hours, minutes, and seconds.  Additionally, it should use the PR seconds and distance to compute the PR mile pace.  (Note that the pace does not print hours because it is highly unusual for a runner to take a runner longer than 59 minutes to run a mile.)  The initial input should print out as follows:

Name                 Distance   PR       Pace    Count
------------------------------------------------------
diablo_loop               1.00  0:08:52   8:52      12
calpoly                   6.20  1:07:30  10:53       7
prefumo_canyon           11.40  1:55:45  10:09       2
los_osos_loop             4.50  0:42:30   9:26       3


Remember to make this a void function. Tip: a placeholder of "%02d" will place leading zeroes in a field of width 2.

void displayRuns(const run_t list[],      /* input - run list */
                 int numRuns); /* input - number of runs in the list */


In order to print the PR and Pace, you need to use a function that will convert seconds to hours, minutes, and seconds.  The function takes the total seconds as an input paramenter and hours, minutes, and seconds as output paramters.  This function is written for you.

void secsToHMS(int totalSecs, /* input - total seconds */
               int *h,        /* output - hours */
               int *m,        /* output - minutes */
               int *s);       /* output - seconds */

Part 2: Creating a function to add a run to the list...

Next you will be writing a function to edit the run list.  There are two possible ways to edit the list.  Either the user will add a new run to the list or update an existing run.

void addRun(run_t list[],       /* input - run list */
            int *numRuns); /* input/output - number of runs in the list */


First, prompt the user for the name of the run.  Then call a function findRun to determine if the run is already in the database:  It  takes as input the array of run data, the current number of runs in the list, and the run to search for as input parameters.  It takes the index location were the run is found as an output parameter.  The function sets the index to the position in the list of the target run or a -1 if the run is not found.  This function is written for you.

void findRun(const run_t r[],     /* input - run list */
             int numRuns, /* input - number of runs in the list */
             const char name[], /* input - run to search for */
             int *index); /* output - position of found run */

If the run is not in the list...
If the run exists in the list...
Each time the database is updated, please print it out again.

Part 3: Saving the data back to the data.txt file...

A database is useless if the data is not saved between runs of the program.  You will call a function to write the run list out to the run.data file before the user quits the program.  This function is written for you.

void writeFile(const run_t list[],      /* input - run list */
               int numRuns); /* input - number of runs in the list */

Part 4: Create a main function to drive your program...

Create a main function to orchestrate how your program runs.  It would be useful to work on this function as you write the above functions to test your work.

Using the functions above, this function should...
The following is an example run.  The user input is in bold for illustration only.  Also note that the % sign is an example prompt on hornet.  Do not print the %.

% a.out

Run Report:

Name                 Distance   PR       Pace    Count
------------------------------------------------------
diablo_loop               1.00  0:08:52   8:52      12
calpoly                   6.20  1:07:30  10:53       7
prefumo_canyon           11.40  1:55:45  10:09       2
los_osos_loop             4.50  0:42:30   9:26       3
(a)dd run or (q)uit: a

Run Name: calpoly
Time (hh mm ss): 1 10 45

Run Report:

Name                 Distance   PR       Pace    Count
------------------------------------------------------
diablo_loop               1.00  0:08:52   8:52      12
calpoly                   6.20  1:07:30  10:53       8
prefumo_canyon           11.40  1:55:45  10:09       2
los_osos_loop             4.50  0:42:30   9:26       3
(a)dd run or (q)uit: a

Run Name: los_osos_loop
Time (hh mm ss): 0 41 23

Run Report:

Name                 Distance   PR       Pace    Count
------------------------------------------------------
diablo_loop               1.00  0:08:52   8:52      12
calpoly                   6.20  1:07:30  10:53       8
prefumo_canyon           11.40  1:55:45  10:09       2
los_osos_loop             4.50  0:41:23   9:11       4
(a)dd run or (q)uit: a

Run Name: dog_park
Distance in Miles: 2.3
Time (hh mm ss): 0 22 10

Run Report:

Name                 Distance   PR       Pace    Count
------------------------------------------------------
diablo_loop               1.00  0:08:52   8:52      12
calpoly                   6.20  1:07:30  10:53       8
prefumo_canyon           11.40  1:55:45  10:09       2
los_osos_loop             4.50  0:41:23   9:11       4
dog_park                  2.30  0:22:10   9:38       1
(a)dd run or (q)uit: q

Happy trails!
% a.out

Run Report:

Name                 Distance   PR       Pace    Count
------------------------------------------------------
diablo_loop               1.00  0:08:52   8:52      12
calpoly                   6.20  1:07:30  10:53       8
prefumo_canyon           11.40  1:55:45  10:09       2
los_osos_loop             4.50  0:41:23   9:11       4
dog_park                  2.30  0:22:10   9:38       1
(a)dd run or (q)uit: q

Happy trails!
%

Part 5: Handing in Your Source Electronically...

1.      Transfer the source code file lab10.c to hornet as you’ve done for previous labs.

2.      Log on to hornet using the Secure Shell Client program (or your favorite equivalent).

3.      Change directory (cd-command) to the directory containing the source file or files to hand in.

4.      Execute the following command – be sure to replace the x in cs101-x with your section number:

handin cs101-x lab10 lab10.c

5.      You should see messages that indicate handin occurred without error.  You can (and should) always verify what has been handed in by executing the following command:

handin cs101-x lab10