Here is the program:

Code:

/* mpg.c computes the average miles per gallons for user entered number of trips */
#include<stdio.h>
int main()
{
unsigned int gals; // gallons of gas
int miles; // miles driven
float mpg; // miles per gallon per trip
//int num_trips; // total number of trips
//float tot_mpg; // avg mpg per number of trips
float grand_avg_all_trips; // average gas used for all trips
// initialize
int num_trips = 0;
float tot_mpg = 0;
// process and get data for first trip
printf("%s", "Enter gallons used: -1 to end ");
scanf("%d", &gals);
printf("%s", "Enter miles driven: ");
scanf("%d", &miles);
printf("The miles per gallon for this trip is %.2f\n", (float) miles/gals);
while (gals != -1)
{
mpg = miles / gals;
num_trips = num_trips + 1;
tot_mpg = tot_mpg + mpg;
// get data for next trip
printf("%s", "Enter gallons used: -1 to end ");
scanf("%d", &gals);
printf("%s", "Enter miles driven: ");
scanf("%d",&miles);
printf("The miles per gallon for this trip is %.2f\n", (float) miles/gals);
} // end while loop here
// termination phase if at least one trip entered
if (gals = -1)
{
grand_avg_all_trips = (float)tot_mpg / num_trips;
printf("The average mpg for %d trips is %.2f: \n", num_trips, (float)tot_mpg / num_trips);
}
else
{
printf("No data was entered.");
}
return 0;
}

/*

Enter gallons used: -1 to end 8

Enter miles driven: 125

The miles per gallon for this trip is 15.62

Enter gallons used: -1 to end 5

Enter miles driven: 115

The miles per gallon for this trip is 23.00

Enter gallons used: -1 to end -1

Enter miles driven: -1

The miles per gallon for this trip is -0.00

Enter gallons used: -1 to end ^C

Enter gallons used: -1 to end 8

Enter miles driven: 125

The miles per gallon for this trip is 15.62

Enter gallons used: -1 to end 5

Enter miles driven: 115

The miles per gallon for this trip is 23.00

Enter gallons used: -1 to end -1

Enter miles driven:

0

The miles per gallon for this trip is 0.00

Enter gallons used: -1 to end -1

Enter miles driven: -1

The miles per gallon for this trip is -0.00

Enter gallons used: -1 to end

*/