Prev Up Next
Go backward to 6.12 Constants and Limits
Go up to 6 Labs
Go forward to 6.14 Simulation #2

6.13 Cross Country Trip Simulation #1

Given a list of distances and gas prices, simulate a cross country trip in a car that has a 20 gallon tank and gets 20 miles to the gallon.

The basic parameters for the trip are as follows. They should be defined as constants in your program so that they are easy to change.

  1. The car gets 20 miles to the gallon.
  2. The gas tank holds 20 gallons.
  3. The tank starts out with 20 gallons in it.
  4. You can run the tank "dry" as you pull into the next station.
  5. The trip is 3000 miles long.
  6. If you run out of gas, You can purchase "emergency gas" using your "cell phone" at any point on the trip at $10.00 per gallon. (This is in case I made an error in the data set!)
Figure out the lowest cost trip. You can "coast" into the next gas station on "vapors". I wouldn't want to do a real trip this way. (Though, I have run out of gas at least 3 times as I pulled into a gas station!) Each time that you purchase gas print the following:
  1. The current distance.
  2. How much gas you purchased.
  3. The cost of the gas.
  4. How much gas was in the tank at prior to the purchase.
  5. How much gas is in the tank at after the purchase.
At the end of the trip, print out the following:
  1. Total gas purchased (gallons and dollars).
  2. Average cost of the gas per gallon.
  3. Miles per dollar.
  4. Miles per gallon.
  5. Remaining gas in tank.
  6. Summary of emergency gas purchases.
The data needs to be read from a file. It is in two fields per line (or record). The first field is the distance to gas station from the departure point. The second field is the cost of gas at that point in dollars per gallon. (You may note some price gouging going on on this list.) You should be able to save quite a bit of money by having this data available to you before starting out on your trip. The data set follows:
  20 1.25
 300  .90
 350 2.50
 520 4.25
 730 0.70
1060 4.17
1350 2.59
1390 1.11
1500 4.25
1530 3.24
1560 2.11
1590 1.73
1600 0.80
1800 5.97
2000 0.95
2250 2.34
2411 1.47
2549 1.09
2786 4.17
2900 0.99
The data set is available by anonymous ftp from
ftp://red.cscwc.pima.edu/pub/csc130/trip.dat
Instructor: ltaber@pima.edu** Red's Home page** The Pima College Site

Prev Up Next