Lab #2 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;    10% of total class grade.

 

Topic objectives:   Data structures using structs

                              Pointers

                              Dynamically allocated memory

                              Control structures

                              Arrays

                              Strings

                              Global constants

                              Searching and sorting

                              Recursion

                              Enumerated types and unions

                              Algorithms

                              Error checking

                              Programming style

                              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–7 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), continue, or linked lists.

 

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

 

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 #2 you will be improving and expanding 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 #2 assignment will be:

 

¨              Introduce the health tracking system to a new user.

¨              Create a one-dimensional array of structs to store the exercise data in and then populate the array with data.

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

1) Using the data in the array,

A)  Merge sort the exercise data array by

      i.    Length of time

      ii.   Exercise code

      iii.  Exercise name

      B)  Binary 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

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 arrays–this will take the user to a submenu for doing updates

3) End the program

 

 

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) A maximum number of exercises of each type will be given

c) 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.

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

 

 

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 one-dimensional array of structs to store the exercise data in and then populate the array with data.

For the lab #2, 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 #2, the exercise data will be stored in a struct. You must declare the struct type to hold all the information described in detail below. Then you must declare an array of MAXEX of these structs to be used in the rest of the lab. MAXEX should be a global constant which has the value 25. The data will be input from a file. Each line of the file will contain the following data.

 

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.

 

For the lab #2, 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 and no more than 25 exercises. Since the user has a choice, then the first piece of data that the program needs from the user is a count of how many exercises they are entering. The program should check to make sure this count is between 12 and 25. [Note for development: start with a smaller number of exercises then increase to 12 when program is working well ] If the user’s number exceeds 25, the program should inform the user that 25 is the maximum number of inputs allowed and have them reenter the count. Then your program must loop for count number of times (index= 0, 1, …count-1) to read and store input. The program must allow data file input and either the individual data method or line of data input method.

 

As the data for each exercise is read in, it should be stored into one struct element in the struct array that you have declared. All the data for any one exercise should be stored in the same struct together. The index will be the index for the array of structs.

 

[Data inputs are the same as for lab #1 except for string input as second item.]

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 array at [index]

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 at [index] 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 array at [index]

d.   For the remaining data values and then store them in the appropriate members of the struct array at [index]. 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

 

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.

 

Your program will read the elements on the input line and store them in the appropriate members of the current struct at [index]. Your program should read in as many lines of input for exercises as were originally specified by the count up to the maximum of 25 lines of input (exercises).

 

Data file (File of lines) input method:

You may create a file that contains between 12 and 25 lines of exercise data and you may read the data from this file. 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 should have only one integer on it which is the count of the number of lines of exercise data which follow in the file.

 

 

Input verification:

When the user has entered count number of exercises, 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 arrays.

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 arrays your program should give the user a screen menu with the following choices: (use any number scheme you wish)

 

i.      Sort the exercise data array by length of time

ii.     Order the array by exercise code

iii.    Arrange the array by exercise name

iv.    Search for a type of exercise to perform

v.     Find an exercise whose average length of time is closest to a given amount of time using the sorted arrays

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.    List all the exercises that meet a certain criteria of strength, endurance or flexibility

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

xi.    List all the exercises and all their respective data

xii.   Update the data in the arrays

xiii.  End the program

 

The sorts (i, ii, iii) must be implemented with a merge 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 array.

 

The searches (iv, v, vi) for an exercise must use binary search. A submenu should ask the user if they wish to search for an exercise type, search for the exercise closest to a given time (closest match), or search for a body part exercise. For whichever type of search is chosen, the user must enter the data they wish to search for. The search function should use binary search to return the first matching element. if any. printing the code and the name of the exercise. 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.

 

The list by criteria function (ix) 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 (x) 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 (xi) is the same as the print function described for input verification.

 

The update option (xii) should take the user to a second screen to allow them to update information in the array. This screen should ask for a exercise code from the user and search for that exercise. Once the correct exercise is determined save its [index] and give the user a menu of the following options:

 

o               Change Type of exercise at [index]

o               Change Name of exercise at [index]

o               Change Body part exercised at [index]

o               Change Kind of equipment at [index]

o               Change Setting, recent setting or target setting at [index]

o               Change Measurement, recent measurement or target measurement at [index]

o               Change Time per measurement at [index]

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 at [index].

 

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

 

Implementation requirements:

 

WRITE A DESIGN ON PAPER FIRST

 

The program should use the following data structures:

Struct data type to hold exercise information

One dimensional array of exercise structs

 

The program should use the following control structures:

Function calls to perform tasks

Recursion in the merge sort

Loop to iterate through struct array

 

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, two for merge sorting, one for a binary 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:   (64%)

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

Comments (line comments and block comments)       (5 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!) (5 points)

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

Correct declaration of struct and global constant (8 pts)

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

Correct manipulation of the array of structs (8 points)

Correct use of required algorithms for binary search (6 points)

Correct use of required algorithms for recursive merge sort (7 points)

Correct function structure as required (4 points)

Proper implementation of input error checking (7 points)

Output:      (36%)

            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 (3points)

            Update tasks perform correctly (5 points)

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

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

            Output contains all the given test data and one additional data set (5 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)

            Use of linked lists will result in 50 (fifty) point deduction per use

            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