36.6k views
2 votes
Design a program to calculate the cost of gas for a trip. The user will be prompted for the cost of a gallon of gas, the number of miles of the trip, and the number of miles per gallon the car gets. The program should use pointers, the address operator, and the dereferencing operator for ALL input, calculations, and output. Enter, compile, link, and execute Lab7GasTripFirstInitialLastName.cpp The following is a sample output that might appear after running your program. Input by the user is shown in bold.

User Erik J
by
8.6k points

1 Answer

4 votes

Final answer:

To calculate the cost of gas for a trip, a program must convert input distances and fuel measurements to consistent units, calculate the vehicle's fuel economy in miles per gallon, and then multiply the total number of gallons by the cost per gallon.

Step-by-step explanation:

Calculating the cost of gas for a trip relies on several key inputs and applying a basic formula. When designing a computer program to perform these calculations, you will need to prompt the user for information and then process it to obtain the desired results.

In this example, a Toyota Prius Hybrid uses 59.7 liters of gasoline to travel 1300 km from San Francisco to Seattle. To convert liters into gallons, we use the conversion factor that 1 gallon is approximately 3.78541 liters. For the fuel economy, we must first convert kilometers to miles, knowing 1 kilometer is equal to 0.621371 miles.

After obtaining the car's mileage in miles per gallon, we can calculate the cost of the trip. This is done by multiplying the total number of gallons used by the cost per gallon. Given the cost of gasoline is $3.90 per gallon, this gives us the total fuel cost for the trip using the user's input within the program.

User Szeryf
by
7.9k points