| | | 6.6 Cross Country Trip Simulation |
6.6 Cross Country Trip Simulation
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.
This means that your car can go 400 miles between gas stations.
The basic parameters for the trip are as follows. They should be
#define
d
in your program so that they are easy to change.
- The car gets 20 miles to the gallon.
- The gas tank holds 20 gallons.
- The tank starts out with 20 gallons in it.
- You can run the tank "dry" as you pull into the next station.
- The trip is 3000 miles long.
At the beginning of your program print out:
- Your name
- CIS265 Louis Taber
- Lab: 6.3: Cross Country Trip
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! This is NOT a recommendation.)
Each time that you purchase gas print the following (in a table):
- The current distance.
- How much gas you purchased.
- The cost of the gas.
- How much gas was in the tank at prior to the purchase.
- How much gas is in the tank at after the purchase.
At the end of the trip, print out the following:
- Total gas purchased (gallons and dollars).
- Average cost of the gas per gallon.
- Miles per dollar.
- Remaining gas in tank. (Should be zero).
Read the data from a file.
In my data file, the information 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.)
The data set follows:
20 1.25
300 0.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://lt.tucson.az.us/pub/c/trip.dat
An example program for reading the data file is available at:
ftp://lt.tucson.az.us/pub/c/read-gas.c
Here is the example program:
/* Louis Taber 3/26/2001
*
* -- Read trip info
*
* Second example program for fscanf()
*
* */
#include <stdio.h>
#include <errno.h>
int extern errno;
int main(int argc, char *argv[])
{
FILE *fp;
int milepost;
float price;
if( argc == 1 ) /* No arguments -- use std input */
{
fp= stdin;
}
else
{
if((fp = fopen( argv[1], "r")) == NULL)
{
perror(argv[1]);
printf(" Error number: %d\n", errno);
printf(" Error: %s\n", strerror(errno));
printf("%s(line %d): can't open: %s\n",
__FILE__, __LINE__, argv[1]);
return 1;
}
}
do
{
if( fscanf( fp, "%d %f \n",
&milepost, &price) <0 ) break;
printf(" %6d %8.3f\n ",
milepost, price);
} while( milepost != 0 );
fclose(fp);
return 0;
}
Turn in:
- A listing of your program,
- The output of the program with the above data set
mark with:
- Your name
- TABER CIS265
- Lab 6.6: Cross Country Trip
- The compiler(s) and OS(s) that you used.
- The size in bytes of the executable program(s)
Please turn your lab in to Pima Community College employee
in Santa Rita Building room A-115.
Ask them to place it into the burgundy colored
"CIS265 -- C Programming" folder in Louis Taber's mailbox.
Instructor: ltaber@pima.edu ** My new Home at GeoApps in Tucson ** The Pima College Site
| | | 6.6 Cross Country Trip Simulation |