CSE 142 Homework 2

Driver's Buddy



New:  Here are the diagrams from the homework discussion in Wednesday's lecture (10/13).


introduction

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.

key concepts

task description

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.

specifications

data representation: working with time

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

program execution

(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:

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

  2. Next, read an int for the start time of the trip's first part in minutes past midnight.

  3. Then, read data for the trip's first part: distance (a double in units of miles) and bridge probability (a double).

  4. 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.

  5. Next, read distance and bridge probability for the second part of the trip.

  6. Then, just as above, compute and print start time, estimated travel time, and end time for the second part of the trip.

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

programming requirements

Please be sure to adhere to the following additional technical requirements:

submission guidelines

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.

files


a few bits of advice

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:

announcements

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.


Return to main Homework page...
Last modified: Sun Oct 31 17:51:45 PST 1999

Save paper. Read documents on the web and, if you must print, print two-sided. Recycle me when you're done.