Lab #3 Assignment , CSE 1320-002 Spring 2009

 

Due Date:       See class website for due date

(see instructions on website for how to turn this in)

 

Grade value;    14% of total class grade.

 

Topic objectives:   Linked lists using

                                    Data structures using structs

                                    Pointers

                                    Dynamically allocated memory

                              Control structures

                              Strings

                              Searching and sorting

                              Recursion

                              Algorithms

                              Error checking

                              Programming style

                              Conditional compilation for debugging

                              Command line parameters

                              plus all topics from previous labs

 

The goal for this lab is to provide an opportunity for practice of the C programming topics covered in Chapters 1–11 in Foster and Foster. This assignment is designed to practice those concepts by creating a program.

 

Be sure to check the DEDUCTIONS section at the end of this assignment to avoid penalties. You may NOT use global variables, the exit command, goto, break (except in a switch), or continue.

 

-- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • -- • --

 

Since we have just had the start of a new year, many people have made New Year’s resolutions and many of these resolutions are about living a healthy life. Your goal for this program will be to develop a system to track activities that contribute to good health starting with a program to track the user’s exercise.

 

Throughout the course of the semester you will work on this health tracking system. For lab #3 you will be modifying the program you developed for lab #2. You will still be given a list of exercises that the user performs along with information such as how many repetitions they did of the exercise, how often, and other data such as amounts of weight used or machine settings. You will write the program to separate the data into a list based on characteristics about the exercise data and then to determine certain information from the list you create. You will have to give the program’s user a set of choices that will then guide the program in what lists are created. You will also have to report to the user the results that the program determines and the lists that are created.

 

The tasks for this lab #3 assignment will be:

 

¨              Introduce the health tracking system to a new user.

¨              Create a sorted linked list of structs to store the exercise data in.

                  Including passing a data file name as a command line parameter

¨              Create and display a screen menu of the following choices for the user.

1) Using the data in the list,

A)  Bubble sort the exercise data list by

      i.    Length of time

      ii.   Exercise code

      iii.  Exercise name

      B)  Linear search for exercise choices and data by

      i.    Type of exercise to perform

      ii.   Average time - to find an exercise that is closest to a given amount of time

      iii.  Body part exercise

C)  Calculate

      i.    Total time to perform a certain set of exercises

      ii.   Average length of time to perform any one exercise

      iii.  Potential results reduction if overdone

D)  List all the exercises that meet a certain criteria

      i.    Strength

      ii.   Endurance

      iii.  Flexibility

E)  Find a group of exercises that can be done within a given time

2) Update the data in the list–this will take the user to a submenu for doing updates

3) End the program

¨              Write the program in such a way as to be able to conditionally compile it

            with debugging turned on or

            with debugging turned off

 

 

Each of these tasks is described in more detail below. There are also some simplifying assumptions for this lab. Many of these simplifications will be eliminated in later labs.

 

Simplifying assumptions for the lab:

a) All exercises must be available at the MAC but do not have to be performed there.

b) All exercises will be uniquely named, i.e. there will not be a “Fly” exercise with free weights and a “Fly” exercise that is done on a machine – they would be “Fly_free” and Fly_machine” for example.

c) Exercise names are always only one word as a string. This word will likely have underscores in it as shown in b)

 

 

Task Descriptions:

 

¨              Introduce the health tracking system to a new user.

For this task your system must provide an introduction/welcome screen to the user. The screen should briefly describe what the system will do. You may have the welcome screen stay up for a fixed period of time or you may let the user press a key to continue on with the program. Make sure you tell the user what to do if you want them to press a key.

 

¨              Create a sorted linked list of structs to store the exercise data in.

For the lab #3, the following data will be used:

 

1.   Type of exercise

2.   Name of exercise

3.   What type of exercise it is (strength, endurance, flexibility)

4.   What part of the body it is focused on (arms, torso, legs, back, cardio, whole body)

5.   What type of equipment is needed for the exercise (weight machine, free weights, stairs, bike, mat, other, none)

6.   What settings are needed

      a.   For the equipment (amount of weight, speed of machine, amount of resistance, other, none)

      b.   Most recent settings (number related to type of setting)

      c.   Target settings

7.   What measurement is needed

      a.   For the exercise (repetitions, laps, postures, other)

      b.   Most recent measurement (number related to type of measurement)

      c.   Target measurements

8.   Estimated time per one measurement (repetition, lap, etc.)

9.   Any additional data elements you wish to add

 

For the lab #3, the exercise data will be stored in a struct. You must declare the struct type to hold all the information described in detail below. The data will be input from a file. Each line of the file will contain the data below. As each line of the file is read, your program must allocate a new struct for that data. The new struct must then be linked into the list in sorted order as described later.

 

o               Exercise code – The integer numeric code associated with a particular exercise. For this lab we are using strings but we will also still have exercises that are coded by number (ex. Recumbent Exercise Bike = 004, Free Weight Fly = 132, Yoga Sun Salutation = 488, etc.). All the other pieces of data associated with this exercise are also declared in the struct.

o               Exercise name – The name as a string associated with a particular exercise. For this lab we are using strings which will be one word with no blanks or punctuation other than underscores. The struct for the exercise should have a char pointer for the name string. The string should be read in from the file to a temporary buffer (an array used to hold the string temporarily) and then you should malloc space for the string and assign it to the char pointer in the struct. [This was done as an example in class on Mar. 9th.]

o               Type of exercise– The single character to hold the letter code for the type of exercise. The valid codes are ‘S’ for strength, ‘E’ for endurance, ‘F’ for flexibility, and ‘U’ for unknown. Make sure that the code that is entered is valid. (Don’t error check for wrong data types – those will give errors in runtime. Check for invalid char values.)

o               Body part focus– The character to hold the letter code for the body part addressed by the exercise. The valid codes are ‘A’ for arms, ‘T’ torso, ‘L’ legs, ‘B’ back, ‘C’ cardio, ‘W’ whole body, and ‘U’ for unknown. Make sure that the code that is entered is valid.

o               Equipment needed– The character to hold the letter code for the type of equipment needed for the exercise. The valid codes are ‘W’ for weight machine, ‘F’ free weights, ‘S’ stairs, ‘B’ bike, ‘M’ mat, ‘O’ other, ‘N’ none, and ‘U’ for unknown.

o               Equipment setting– The character code for the setting of the equipment needed for the exercise. The valid codes are ‘W’ for weight amount, ‘S’ speed, ‘R’ resistance, ‘O’ other, ‘N’ none, and ‘U’ for unknown.

o               Recent setting– The integer value of the most recent setting used for that particular exercise. If no setting is needed or value is unknown, this value should be -1. This value should always be positive except in cases where –1 indicates no setting.

o               Target setting– The integer value of the target setting for that particular exercise, i.e. the amount that would be the user’s goal for this exercise. If no setting is needed, this value should be -2. This value should always be positive except in cases where –2 indicates no setting.

o               Exercise measurement– An enumerated type listing each type of measurement that is valid. The valid enumerated type values should be are repetitions, laps, postures, minutes, other, and unknown. Use the following enumerated type definition: enum Exmsmt {Repetitions, Laps, Postures, Minutes, Other, Unknown};

o               Recent measurement– The value of the most recent measurement recorded for that particular exercise. This value should be stored in a union based on the enumerated type of the exercise measurement as follows: Reprtitions and postures should have integer counts, laps should have a float distance, minutes should have a double value, and other and unknown should have integer values. Check for valid values.

o               Target measurement– The value of the target measurement for that particular exercise. This value should be stored in a union based on the enumerated type of the exercise measurement as above.

o               Time per measurement– The double value of the average time in seconds (can indicate tenths and hundredths with decimal places) for ONE measurement for that particular exercise, i.e. the average amount of time for one repetition or lap or whatever for this exercise. This measure multiplied by the Recent measurement tells how long it takes to do this exercise.

o               Max effective length – A double value indicating the maximum length of time that this exercise should be performed during one workout

o               Over max increment – A float percentage indicating time increments over max that actually lead to reduced results (this would be some fraction of max effective length like 10% so if max effective length is 20 minutes, over max increment might be 2 minutes)

o               Over max dropoff – A float percentage indicating amount of dropoff results per each over max increment (for example, going 2 minutes over max might result in a 5% drop off of effectiveness)

o               Recent exercise length – This double value is time per measurement times recent measurement (This value will be calculated, not read in)

o               Pointer to an exercise struct – This is not a value in the file but it must be included as a member of the struct and given an initial value of NULL. The linked list to be created only requires a singly linked list. Extra credit will be given if two pointers are used to correctly implment a doubly linked list.

 

For the lab #3, you must implement data file input and at least one of the other two methods of input. The first method is to read lines of exercise data from a data file, the second method will allow the user to input all needed pieces of data about one exercise on one line, and the third method will prompt the user for each piece of data about an exercise individually. The three forms of the input and the input data file are described below.

 

For this lab the user must enter at least 12 exercises. There is no maximum amount of data. For file input, your program should read until it reaches the end of the file (EOF). For user input by line of data or pieces of data, you must tell the user what to enter that will tell the program that input is complete.

 

As the data for each exercise is read in, it should be stored into one struct element that you have allocated. All the data for any one exercise should be stored in the same struct together.

 

Individual data method:

a.   Ask the user for the exercise code (don’t forget to supply a list of names and codes for them.) Read in their number, make sure it is a valid code (one of the legal codes that they could choose from) and then store it in the exercise code member of the struct variable you have allocated

b.   Ask the user for the name of the exercise as a one-word string. Read in the string, malloc new space of the right size to the exercise name pointer in the struct variable you have allocated and then copy the string into the malloced space.

c.   Ask the user for the type of exercise using the single character description (don’t forget to supply a list of abbreviations for them.) Read in their char, make sure it is a valid abbreviation and then store it in the exercise type member of the struct variable you have allocated

d.   For the remaining data values and then store them in the appropriate members of the struct variable you have allocated. Don’t forget to error check the input

 

Line of data input method:

Instead of asking for each value separately, your program may ask the user to enter all the information for one exercise on the same line. This data would be an integer for exercise code, a one word string for exercise name, a char for type of exercise, body part focus, equipment needed, and equipment setting, an integer for recent setting and target setting, a char for exercise measurement, an integer for recent measurement and target measurement, and a floating point number for time per measurement. You must tell the user exactly how to enter the line of data. The data for a single exercise will be entered by the user as twelve values on one line as follows {there must be one space only between the values}:

 

> 053 Machine_Bicep_Curl S A W W 30 50 0 10 18 3.25 300 15 5

 

which represents the Machine Bicep Curl (053), a 'S'trength exercise for 'A'rms using a 'W'eight machine. Its setting is 'W'eight with a recent value of 30 and a target weight of 50. Its measurement is Repetitions (indicated by the 0) with a recent measurement of 10 reps and a target of 18. An average Machine Bicep Curl takes 3.25 seconds. The maximum length of time this exercise should be done is 300 seconds (5 minutes). Negative results start occurring at 15% time over 300 seconds (45 seconds) and the dropoff in effectiveness for an additional 45 seconds over max is 5%.

 

Your program will read the elements on the input line and store them in the appropriate members of the current struct variable you have allocated. Your program should read in as many lines of input for exercises as the user enters until the user indicates that they are finished.

 

Data file (File of lines) input method:

You may create a file that contains 12 or more lines of exercise data and you may read the data from this file. You must pass the file name into the program as a command line parameter. Then use the command line argument to manipulate the file with the file variable. To do this will requires creating file variables, opening the file and linking it to the file variable, and then reading the data from the file variable in the same way that a line of data would be read from the screen. The first line of data in the file will simply be the first record of data in the file.

 

Sorted linked list creation:

As each new exercise struct is malloced and filled with data, the new struct will need to be linked into the list in order. As the data is read in the linked list should be sorted in order by exercise code from smallest to largest. The process for this is as follows:

 

The first struct is allocated and filled with data and it becomes the head of the linked list.

The head struct is also set as the current struct for comparison

A new struct is allocated and filled with data

 

As long as the new struct is not empty (i.e. there is still data to put in the list),

            The exercise code of the new struct is compared to the code of the current struct in the list

            If the new code is larger,

                        While the new code is larger than the current code and the end of list is not reached

                                    Move through the list so that the next struct in the list becomes the current struct

            Else (if the new code is equal or smaller than the current struct code or reached end of list)

                        If not at end of list

                                    the new struct links to the current struct

                                    if the struct previous to the current struct is not NULL

                                               the previous struct links to the new struct

                                    else (the prev struct is NULL because the current struct is the head struct)

                                                the head pointer moves to the new struct because it is the new head of the list

                        Else (if at end of list – current struct pointer is NULL)

                                    the current struct links to the new struct

            A new struct is allocated and filled with data

 

Input verification:

When all of the exercise input has been read in, print out all the input exercise data in an easily readable form, ex. use a table with headings, or columns with headings or rows with labels. It is strongly suggested that this printing be written as a separate function that can be called at any time in the program to print the current contents of the list.

Since the name of the exercise is now recorded in the struct you will no longer need the name printing function. You should print the string from the struct as needed.

 

 

¨              Create and display a screen menu of the following choices for the user.

Once all the data is read into the list your program should give the user a screen menu with the following choices: (use any number scheme you wish)

 

i.      Sort the exercise linked list by length of time

ii.     Order the list by exercise code

iii.    Arrange the list by exercise name

iv.    Search for an exercise by name

v.     Find an exercise whose average length of time is closest to a given amount of time in the list

vi.    Search for body part exercises

vii.   Calculate a total time to perform a certain set of exercises

viii.  Calculate the average length of time to perform of all of the exercises

ix.    Calculate the potential results reduction if overdone

x.     List all the exercises that meet a certain criteria of strength, endurance or flexibility

xi.    Find a group of exercises that can be done within a given time

xii.   List all the exercises and all their respective data

xiii.  Update the data in the list

xiv.  End the program

 

The sorts (i, ii, iii) must be implemented with a bubble sort routine. The user should be asked if they wish to sort by length of time, by exercise code, or by exercise name (this can be done with a menu). The program should then sort the array based on what they request and print the sorted list.

 

The searches (iv, v, vi) for an exercise must use linear search. A submenu should ask the user if they wish to search for an exercise by name, search for the exercise closest to a given time (closest match), or search for an exercise by body part. For whichever type of search is chosen, the user must enter the data they wish to search for. The search function should use linear search to return the first matching element, if any, printing the code and the name of the exercise. To search by name the search should use string comparison functions. For the given time search, if two exercises are the same length of time and closest to the given time, return the one with the smaller code.

 

To calculate (vii) the total time for a certain set of exercises, the average time should be multiplied by recent measurement. Print the exercise and the total time. The average length calculation (viii) takes an average of the time lengths of all the exercises. It should print the exercise average length in seconds.

 

Calculating the potential results reduction (ix) is done with the following algorithm which requires a comparison function and a recursive calculation function:

 

The inputs to the comparison function would be the Max effective length , Over max increment , Over max dropoff, Recent measurement, Target measurement, and Time per measurement.

 

First, compare the (recent measurement * time per measurement) against the max effective length. If recent msmt is less than max effective length then print a message that recent msmt will give effective results. Then do the same comparison for target msmt and print a message if target is less than max.

 

Else if either is greater than max, then if recent msmt * time per msmt is greater than max effective length then subtract the difference and store in over max recent. Then call the recursive calculation function described below to calculate the amount of negative results derived from the most recent measurement. Print the results from the recursive calculation indicating that they are a reduction in results by X% from optimal.

 

If the target msmt * time per msmt is greater than max effective length then subtract the difference and store in over max target. Call the recursive calculation passing in this value and print output as above for over max recent.

 

Recursive calculation: This function will take as input the over max ___ value, the max effective length, the over max increment, and the over max dropoff. The function returns a percentage of total dropoff value. The algorithm is:

 

Calculate the time of the increment by over max increment * max effective length.

Subtract the time increment from the over max ___ value and save result as new over max value.

If (new over max value > 0)

            Increment a total dropoff value by adding over max dropoff to total dropoff value and call the recursive funtion with new over max value, the max effective length, the over max increment, and the over max dropoff.

Else

            Return total dropoff value of 0 (zero).

 

 

The list by criteria function (x) lets the user choose a criteria and then goes through the list and prints all exercises that meet that criteria.

 

Finding a group of exercises that can be done within a given time (xi) means asking the user for a time amount (for example, five minutes, entered as 300 seconds) and then finding a list of exercises that can all be done once in the period of time (for example, a sun salutation for 112 seconds, a bicep curl for 2 seconds, a minute of walking for 60 seconds, a minute on the recumbent bike, plus a 65 second yoga pose would equal 299 seconds of exercise.) This function should try multiple combinations using each exercise only once to try to get as close as possible but still less than the requested time.

 

The list exercises function (xii) is the same as the print function described for input verification.

 

The update option (xiii) should take the user to a second screen to allow them to update information in the list. This screen should ask for a exercise code from the user and search for that exercise. Once the correct exercise is determined save a pointer to that struct and give the user a menu of the following options:

 

o               Change Type of exercise

o               Change Name of exercise

o               Change Body part exercised

o               Change Kind of equipment

o               Change Setting, recent setting or target setting

o               Change Measurement, recent measurement or target measurement

o               Change Time per measurement

o               Return to main menu

 

For any change the user wishes to make, do the same error checking as in the original data entry section. After each change is made, print all of the exercise info for the exercise struct that was changed.

 

When the user chooses “End the program” from the main menu, print a concluding message and then gracefully end the program.

 

¨     Write the program in such a way as to be able to conditionally compile it

 

When writing the program, include substantial debugging code in the program and surround each section of debugging code with conditional compilation statements (#ifdef). At the top of your program a #define statement to control the debugging statements. When you are compiling and scripting your program you will compile it once with debugging turned on and then once with debugging turned off for the given test data doing the same tests for each script file execution.

 

 

Implementation requirements:

 

WRITE A DESIGN ON PAPER FIRST

 

The program should use the following data structures:

Struct data type to hold exercise information

Sorted singly-linked list of exercise structs

 

The program should use the following control structures:

Function calls to perform tasks

Recursion in the over maximum calculation

Loop to iterate through struct list

Preprocessor statements for conditional compilation

 

The program should NOT use:

global variables

exit

break (except in a switch)

continue

            any topic not covered in class before the lab DUE date unless approved by the instructor

 

The program should be implemented as a set of functions with a main routine and at least one function for menu operations, two for getting input, one for bubble sort, two for over max calculation, one for a linear search function, and one for calculating total time. You may use more functions than this but you must use at least this many. You also need a function for printing the exercise data.

 

The program should perform the following actions in the given order:

Declare and initialize the variables

Print a welcome screen for the user that introduces the system

Get the needed input value from the keyboard

Print the appropriate outputs

Let the user enter additional values until the user indicates that they are finished.

 

The program should have a program header which gives, at least, your name, the number of the lab assignment, your class and section, the assignment date, the due date, and a description of the program. If multiple files are used, each file should contain a similar header. See your instructor’s website for SPECIFIC instructions about the program header.

 

Each programmer-defined function, i.e. each function you write, should have a function header similar to those used in the examples in the textbook. This header should include at least the function name, the purpose of the function, and its inputs and outputs.

 

This program must be run with two different sets of test data for the exercise constant data. You must create one data set in addition to the one that I will give you and run your program with both of them. You may run it two times within a single execution or you may execute the program two different times so that you have a total of two different data sets. The sample data set that you create must meet the guidelines given in the problem definition.

 

The program output must be recorded in a script file from OMEGA using the gcc compiler. If you do not know how to create a script file, it is your responsibility to ask the TA, look for help on the class website, or OIT how to use this function.

 

Grading scale:

Code:   (59%)

Program header and function headers for all functions           (3 points)

Comments (line comments and block comments)       (3 points)

Modularity (division of the problem into small tasks, each one assigned to its own function and called from main() or from another function when appropriate--do not code the entire program in main!) (4 points)

Style (indentation, consistency, meaningful identifiers, lateral separation of code from line comments, etc.) (3 points)

Correct declaration of struct and global constant (5 pts)

Correct input to struct members using data file and other input (6 pts)

Correct creation of the sorted linked list of structs (13 points)

Correct bubble sort to resort linked list (10 points)

Correct use of required algorithms for linear search (3 points)

Correct use of required algorithms for recursive calculation algorithm (5 points)

Proper implementation of input error checking (4 points)

Output:      (41%)

            User clearly understands what is being requested for input (3 points)

            Search (find) tasks perform correctly (6 points)

            Sort task performs correctly (6 points)

            Calculate tasks perform correctly (5 points)

            Update tasks perform correctly (5 points)

            Input verification shows valid values and list of inputs correctly saved and printed (3 points)

            Output gives clear information to explain the values to the user (3 points)

            Output contains all the given test data and one additional data set (4 points)

            Output contains version of program with debug on and with debug off (6 points)

 

 

Deductions:

            Use of global variables will result in an overall grade of 0 (zero)

            Use of exit, break (not in a switch), or continue will result in an overall grade of 0 (zero)

            Late submission of softcopy of code and/or script file to appropriate TA will result in an overall grade of 0 (zero) without prior instructor approval

            Use of C language elements not yet discussed in class by the lab due date will result in potential deduction of points – discuss with instructor before using.

 

Miscellaneous:

 

Exercise code list: (Codes should all be unique now. Ignore leading zeros when typing as input)

Recumbent_Exercise_Bike = 004

Stairmaster = 005

Ski_Machine = 006

Rowing_Machine = 007

Exercise_Bike = 008

Treadmill = 009

Incline_Treadmill = 010

Machine_Fly = 050

Machine_Chest_Press = 051

Machine_Shoulder_Press = 052

Machine_Bicep_Curl = 053

Machine_Triceps_Curl = 054

Machine_Crunch = 060

Machine_Back_Extension = 055

Machine_Leg_Press = 056

Machine_Hip_Adduction = 057

Machine_Hip_Abduction = 058

Machine_Ankle_Extension = 059

Free_Weight_Fly = 132

Free_Weight_Chest_Press = 131

Free_Weight_Shoulder_Press = 133

Free_Weight_Bicep_Curl = 134

Free_Weight_Triceps_Curl = 135

Chin_Up = 136

Push_Up = 137

Crunch = 138

Run = 220

Walk = 221

Jog = 222

Stretch = 23

Yoga_Sun_Salutation = 487

Yoga_Downward_Dog = 488

Yoga_Cobra = 489