CMPT 130 Assignment 2 – Splits

 

For this assignment you are going to write a program that requests and displays data about splits in a run. A run can be broken down into sections of the same length (usually miles or kilometers) called splits. The runner then records the time taken to complete each split to determine whether he / she is keeping an even pace. A common running strategy is to complete a run with a negative split. This entails completing the second half of the race faster than the first half. For more information:

§  https://www.verywellfit.com/what-are-splits-in-running-2911656

§  https://en.wikipedia.org/wiki/Negative_split

For this assignment all splits are mile splits (so a ten-mile run would be broken into 10 splits of one mile each). Similarly, we will assume that all run distances are in whole miles (which counts out marathons – 26.2 miles).

 

The assignment involves using loops, if statements and functions and you must implement the assignment as directed to receive full marks. It is divided into parts and I strongly suggest completing and testing each part before moving on to the next.

 

Your program will be marked by compiling it using g++ on Linux; therefore you should test your program with this compiler and in the Linux OS before submitting it.  If you fail to do this, and if your program does not compile it will not be marked. This assignment is worth 5% of your final grade.

 

Sample Output

This is what I would expect the output to look like from running the completed program. This will give you a good idea of what the program is intended to do. User input is shown in green.

 

Enter number of splits: Enter a value between 1 and 100: 5

 

Enter START time of split 1 in hours, minutes and seconds.

Enter a value between 0 and 24: 12

Enter a value between 0 and 60: -4

Enter a value between 0 and 60: 45

Enter a value between 0 and 60: 121

Enter a value between 0 and 60: 0

Enter END time of split 1 in hours, minutes and seconds.

Enter a value between 0 and 24: 12

Enter a value between 0 and 60: 53

Enter a value between 0 and 60: 20

 

Split 1: 12:45:0 to 12:53:20: time = 8.33333, pace = 8.33333 minutes per mile

 

Enter END time of split 2 in hours, minutes and seconds.

Enter a value between 0 and 24: 13

Enter a value between 0 and 60: 01

Enter a value between 0 and 60: 50

 

Split 2: 12:53:20 to 13:1:50: time = 8.5, pace = 8.5 minutes per mile

 

Enter END time of split 3 in hours, minutes and seconds.

Enter a value between 0 and 24: 13

Enter a value between 0 and 60: 09

Enter a value between 0 and 60: 50

 

Split 3: 13:1:50 to 13:9:50: time = 8, pace = 8 minutes per mile

 

Enter END time of split 4 in hours, minutes and seconds.

Enter a value between 0 and 24: 13

Enter a value between 0 and 60: 17

Enter a value between 0 and 60: 20

 

Split 4: 13:9:50 to 13:17:20: time = 7.5, pace = 7.5 minutes per mile

 

Enter END time of split 5 in hours, minutes and seconds.

Enter a value between 0 and 24: 13

Enter a value between 0 and 60: 24

Enter a value between 0 and 60: 26

 

Split 5: 13:17:20 to 13:24:26: time = 7.1, pace = 7.1 minutes per mile

 

First Half Split: time = 20.8333, pace = 8.33333 minutes per mile

Second Half Split: time = 18.6, pace = 7.44 minutes per mile

You ran a negative split

 

Average speed = 7.60778 mph

 

Overview

Your solution should consist of one relatively large main function that calls several smaller functions that perform calculations or print data.

 

Functions

Your program is to include the following functions (which are discussed in detail in the sections that follow).

§  getIntegerBetween

§  elapsedTime

§  pace

§  printTime

§  printSplit

 

Data Types

The data types of program variables are important in ensuring that the program returns the correct values. You will need to keep track of or calculate the following information:

 

§  number of splits – integer

§  overall distance of the run – double or integer

§  split start time in hours, minutes and seconds – integers

§  split end time in hours, minutes and seconds – integers 

§  time taken to run the first half in minutes – double

§  first half distance in miles – double

§  time taken to run the second half in minutes – double

§  second half distance in miles – double

 

The choice of data type shown above is determined by whether it makes sense for a value to have a fractional component. For example, we are recording the start and end times in hours, minutes and seconds. These values must be whole numbers (we are not recording fractions of a second). The unit for elapsed time is minutes; to allow for fractions of a minute to be recorded this value must be a double (or some other floating-point type).

 

Error Handling

You are not responsible for handling errors relating to user input other than those dealt with by the getIntegerInput function discussed in Part 1. You are also not responsible for handling obviously incorrect data such as end times (or subsequent split begin times) that chronologically precede the previous begin times.

 

Part 1 – Get Integer Input

Write a function called getIntegerBetween with two integer parameters that represent the low and high value in a range of integers; the function should return an integer within this range. The function should prompt the user to enter a value between the two parameters. If the user enters a number outside the range specified by the parameters, the user should be prompted to enter another value. See the sample run for an example. The function should contain a while loop that iterates until the user has entered a value within the range. This function is to be used to get input for hours, minutes, seconds and the race time.

 

The function does not have to deal with input type errors, that is, where the user enters an incorrect type such as a character or a value with a number after the decimal point.

 

Debug and Test

Once you've written this function write some test code in your main function and test it thoroughly. Then remove the test code.

 

Part 2 – Calculate Elapsed Time

Write a function called elapsedTime that returns a double that equals the difference, in minutes, between a start and end time; the two times should each be represented by three integer parameters – hours, minutes and seconds. For example, if the start time is 09:30:00 (9:30 am) and the end time is 10:15:30 (30 seconds after 10:15 am) the function should return 45.5 (45 ½ minutes), the end time minus the start time.

 

Debug and Test

Once you've written this function write some test code in your main function and test it thoroughly. Then remove the test code.

 

Part 3 – Calculate Pace

Write a function called pace that has two double parameters representing time in minutes and distance in miles. The function should return a double that equals the minutes divided by the distance – the time to travel a single mile at the given pace. For example, if the time is 15 and the distance is 2 the function should return 7.5.

 

Debug and Test

Once you've written this function write some test code in your main function and test it thoroughly. Then remove the test code.

 

Part 4 – Print Time

Write a void function that prints a time. The function should have three integer parameters for hours, minutes and seconds and should print these three values separated by colons. For example, if hours = 8, minutes = 23 and seconds = 39 the function should print 8:23:39.

 

Debug and Test

Once you've written this function write some test code in your main function and test it thoroughly. Then remove the test code.

 

Part 5 – Print Split Data

Write a void function that prints data about a split. The function should have two double parameters for the distance (in miles) and time taken to run the split (in minutes). The function should print the following, where the <>s should be replaced by the parameter values or calculations:

 

time = <minutes>, pace = <split pace> minutes per mile

 

For example, if distance is 5 and time is 31 the function should print:

 

time = 31, pace = 6.2 minutes per mile

 

Debug and Test

Once you've written this function write some test code in your main function and test it thoroughly. Then remove the test code.

 

Part 6 – Main Function

Your main function should perform the tasks described below. See the sample run for an example of what we are expecting.

 

§  Prompt the user to enter the number of splits. This should be a value between 1 and 100 – use your getIntegerInput function to get this input.

§  For each split do the following, use a for loop to repeat this process:

o   Set the start time in hours, minutes and seconds. If this is the first split prompt the user to enter these values and use the getIntegerInput function to get them. If it is not the first split the start time should be set to the end time of the previous split.

o   Set the end time in hours, minutes and seconds. Use the getIntegerInput function.

o   Print information about the split: the start and end time, the elapsed time and the pace. Use the other four functions to calculate and print these values.

o   You will also need to keep track of the time for the first half split and the time for the second half split during this process. Note that if the number of splits is an odd number that this will entail assigning half the time it takes to run the middle split to each of the first and second half splits.

§  Print the time and pace for the first half and second half splits as shown in the sample output; if the run had a negative split print a message indicating this.

 

Assessment

The assignment is out of 36.  Marks are assigned as follows:

§  Gets number of splits – 1

§  Repeats process for each split – 3

§  Prints start split time – 2

§  Prints end split time – 2

§  Prints split pace and time - 2

§  Prints first half pace and time – 2

§  Prints second half pace and time – 2

§  Prints if split is negative – 2

§  Handles out of range input – 2

§  Uses functions as described in assignment – 10 (2 marks each)

§  Variable naming – 4

§  Comments – 2

§  Indentation – 2

 

Submission

You should submit your assignment online to the CoursSys submission server.  You must submit a single .cpp file, please read the documentation on site for further information.  The assignment is due by 11:59 pm on Wednesday the 10th of October.

 

 

CMPT 130 Home

 

John Edgar (johnwill@sfu.ca)