New: Here are the diagrams from the homework discussion in Wednesday's lecture (10/13).
In this assignment, you will write a program to estimate the time required for a car trip. The estimated travel time depends on a variety of factors including the trip distance, time of day that the trip starts (i.e. rush hour or otherwise), and the likelihood that any drawbridge along the route is raised.
overview
Your program will compute travel time estimates for each part of a two-part car trip. The estimated travel time for each trip part will be computed from the following data:
The user will provide the start time for the first part of the trip. The second part of the trip starts immediately after the first part; i.e. the start time for the second part is the start time for the first part plus the estimated travel time for the first part.
If part of a trip starts during a rush hour period, that part will take longer than if it started during a non-rush period. (Note that it is entirely possible for one part of the trip to start during rush hour and for the other not.) A longer distance will result in a longer travel time estimate. Finally, if a bridge is raised, the travel time estimate is raised by a fixed delay. Details on how each of these data are used to compute the travel time estimate are given in the next subsection.
For the sake of simplicity, we will assume that each of the trip parts has exactly one bridge. A bridge probability of 0.0 means that there's no chance that the bridge will be up; a probability of 0.25 means that there's a 1 in 4 chance that it'll be up; and a probability of 1.0 means that it's sure to be up.
travel time estimate formulas
For a trip part that starts during a non-rush period, the following formula estimates the travel time in minutes, based on the distance in this part and the average travel speed in miles per hour:
estimated time = (distance / avg. speed) * 60 minutes per hour
If the trip starts during a rush-hour period, a different formula is used:
estimated time = (distancek / avg. speed) * 60 minutes per hour + distance * additional delay factor
where k is a constant slightly greater than 1.0 and the additional delay factor is some number of minutes per mile. (The values for this and other constants for this program are given in "additional data" below.)
When computing the above time estimate formulas, you should just drop fractional minutes (i.e. drop off the decimal part by casting to int), as we do in the example below. (The difference of a fraction of a minute is insignificant, considering these are estimates anyway.)
If the bridge in a part of the trip is raised, an additional 15 minutes is added to the estimate. The bridge probability is the likelihood that this delay is added to the estimate.
additional data
Use the following values for the constants in these computations:
example computation
Try running the sample program with inputs matching this example. To force the bridges to be up or down to match this example computation, replace the bridge probabilities with 0.0 (for bridge always down) or 1.0 (for bridge always up).
Since the start time for the trip's first part is not during a rush hour period, we use the first travel time estimate formula: estimated time = (22.0 miles / 18.0 m.p.h.) * 60 minutes per hour = 73 minutes, dropping the fractional part. This is the estimate, not yet taking into account a possible bridge delay, of which there is a 50% chance.
Suppose that the bridge is not raised for the first part. In this case, the travel time for this part is 73 minutes (1 hour, 13 minutes). The end time for this part of the trip is 405 + 73 = 478 minutes past midnight (7:58).
The starting time for the second part of the trip is the ending time for the first part. Since this start time is during one of the rush hour periods, we use the second formula this time: estimated time = ((7.3 miles)1.02 / 18.0 m.p.h.) * 60 minutes per hour + additional delay factor = 25.320254 minutes + additional delay factor. Computing the additional delay factor part of the formula separately in units of minutes, we get additional delay factor = 7.3 miles * 1.0 minute per mile = 7.3 minutes. Adding this to the 25.320254 minutes and dropping the fractional part yields 32 minutes as the time estimate for the second part of the trip.
Suppose we are unlucky and the bridge is up. (There's a 20% chance.) In this case, we add another 15 minutes of delay for a total of 32 + 15 = 47 minutes, the estimated travel time for the second part. Adding 47 minutes to the second part's starting time yields 478 + 47 = 525 minutes past midnight (8:45).
The total distance is 22.0 + 7.3 = 29.3 miles, and total travel time is 73 + 47 = 120 minutes (2 hours). Note that fractional parts of distance values are not dropped.
Before we get into the detailed specification for your program, let's discuss how to handle storing time. Choosing a method for representing data is an extremely important (and often surprisingly difficult) step of developing a program. For this program, we'll make some decisions for you, but you should understand the reasons explained here.
In this case, we need to read, store, and print times. In real life, we usually represent time as a pair of numbers, separated by a colon, e.g. "10:21" for twenty-one minutes past ten o'clock. Moreover, we often add "am" or "pm" to indicate whether the time is before or after noon.
So far, we've learned types char, int, and double, but we haven't learned a type that's convenient for storing times. For this homework, we will choose a standard way of representing times using a type we already know.
For this program, you should store time data as an int in units of minutes. For a particular time of day, you should store the number of minutes past midnight, e.g. 2:30 am would be the int 150, which is 2 * 60 minutes plus 30 minutes. Durations of time should be stored similarly, e.g. 1 hour and 15 minutes would be the int 75, which is 60 plus 15 minutes.
So why store times as ints in minutes?
Notice that this program will have to perform addition on times. For example, once you compute an estimate of how long a part of a trip will take, the starting time for the next part will be the starting time for the first part plus the estimated time. If both the starting time and travel time estimate are stored as ints in units of minutes, this is as simple as adding the two values together. (If, instead, hours and minutes were stored separately, note that you'd have to add the hours and minutes separately, then adjust the figures if the total number of minutes ends up greater than 59.)
While using the int representation makes computing easier, when the program prints time data, it should use a format that is more easily understood by humans. Can you quickly compute, for instance, what time 852 minutes past midnight is? It's 14:12 in 24-hour hh:mm (hours:minutes) format, or 2:12 pm in 12-hour format.
We have provided a function called PrintTime for printing time data in hh:mm format. PrintTime has one int parameter, time in minutes, and prints (using printf) the time in 24-hour hh:mm format. (See the starter source file hw2_orig.c, linked below, for more details.)
(As always, read this section especially carefully and study the sample executable. Make sure your program satisfies the requirements specified in this document.)
Write your program to perform input, output, and computations in the following order:
After printing your name and student number, print the start and end times of the am and pm rush hour periods in hh:mm format (by calling PrintTime).
Next, read an int for the start time of the trip's first part in minutes past midnight.
Then, read data for the trip's first part: distance (a double in units of miles) and bridge probability (a double).
Before reading data for the second part of the trip, your program should compute and print the following data for the first part:
You should compute the estimated travel time in minutes and drop any fractional part, i.e. you do not have to do any rounding off.
Next, read distance and bridge probability for the second part of the trip.
Then, just as above, compute and print start time, estimated travel time, and end time for the second part of the trip.
Finally, before exiting, compute and print the total distance and the total estimated travel time, i.e. the sum of the estimated travel times for the two parts.
Please be sure to adhere to the following additional technical requirements:
functions
You are required to implement and call at least the functions described in this section. Please use the function names provided here. (Parameter names and order are up to you.) The functions specified below should not contain any input or output (I/O), i.e. printf or scanf calls.
BridgeIsUp
If a randomly generated number from 0.0 to 1.0 is strictly less than the bridge-up probability, the bridge is up and BridgeIsUp should return 1; otherwise, it should return 0. We have provided a function called Random() that has no parameters and returns a randomly selected double in the range 0.0 to 1.0. BridgeIsUp should not be called directly from main.
NormalTimeEstimate
Write these functions to compute estimates using the formulas specified above in "task description." These `functions should not compute bridge delays or be called directly from main.
For the rush hour time estimate function, you will need to use the math library function pow. This function has two parameters of type double and returns the result of raising the first parameter to the second parameter power. In order to use this function, you must have the line
#include <math.h>
after your #include <stdio.h> line at the top of your program.
example usages of pow function
pow(5.0, 2.0) evaluates to
25.0
pow(2.0, 3.0) evaluates to 8.0
pow(3.1416, 2.0) evaluates to
9.86965056
TimeEstimate
This function should be called from main to compute the travel time estimate for a single part of the trip.
This function conditionally calls one of the above estimate functions (NormalTimeEstimate or RushTimeEstimate) as part of the computation of the estimated travel time for a single part of the trip. The choice of function is determined by whether the start time is during a rush hour period. This function should also call the BridgeIsUp function and add the bridge delay if the return value indicates the bridge is raised.
#define constants
Use well-named #define constants for the constants described in "task description" above. Make sure to use the minutes-after-midnight format for the rush hour period start and end times.
style
Remember to choose good variable and function names that are informative and descriptive, but not too long. Make sure to comment your code to describe groups of statements, functions (purpose, parameter(s), return value), and other sections of code, e.g. #defines, function prototypes.
input verification (not required)
Any program for real-world use verifies that data that the user inputs makes sense, i.e. is of the appropriate type and falls within a sensible range. For instance, distance should be a positive number, and probability should be a double from 0.0 to 1.0. For this assignment, however, you may assume that all input is of the appropriate type and value. You do not have to write any code to check inputs; we will only test your programs with sensible input.
As always, be sure to read the homework submission guidelines, linked on the course home page in the Announcements section. You will have to submit your work (1) via the web using the page linked below and (2) on paper.
self-extracting archive, including starter C file
hw2.exe - sample executable
hw2_sol.c - sample solution
With any large project, it's a good idea to start by trying to break it down into small pieces that you can work on one at a time, then put together gradually. Here are some suggestions for this homework:
Try writing and testing some of the smaller functions alone before working on the whole project. Good candidate functions for this approach are BridgeIsUp, NormalTimeEstimate, and RushTimeEstimate. To test these functions individually, you can write a program that just reads values for the function arguments and prints the return value.
When you're ready to tackle the main program, try to get a simplified version of the program working first, e.g. without consideration for bridges or rush hour. Once that's compiling and working perfectly, save a copy just in case you want to go back to it later, and try adding another aspect of the program. Keep working like this and you'll save time in the long run and always have something working.
If any clarifications or changes need to be made for this homework, they will be posted to the cse142-announce email list and linked here.
Save paper. Read documents on the web and, if you must print, print two-sided. Recycle me when you're done.