Create a program that can calculate and compare the weekly fuel cost for two new cars. The calculation of the weekly fuel cost must be computed using a function. Functions enable us to break down a problem into smaller sections. By using a function to calculate the cost, this could be repeated multiple times or used in other programs without having to repeat the code and it should work reliably each time because it has been debugged and tested. This program is intended to help a person to compare weekly fuel cost of two new cars. To calculate the weekly fuel cost for car you need several things: Weekly highway driving Weekly city driving Highway Miles per Gallon (MPG) City Miles per Gallon (MPG) The cost per gallon of gas. The steps to calculate the total weekly cost is: Calculate the amount of fuel used in highway driving: Take the number of highway miles driven and divide it by the highway mpg. Calculate the amount of fuel used in city driving: Take the number of city miles driven and divide it by the city mpg. Add the gallons used from each of the calculations to get the total gallons used. Take the gallons used and multiply that by the cost per gallon of gas. The program should prompt the user for the following information at the beginning of the program (this same information is used for each car): Estimated weekly highway driving. Estimated weekly city driving. Estimated Cost for a gallon of gas. It should prompt them for the following information for each car: The car make and model. Highway MPG for the car. City MPG for the car. Once the user has entered the required information, the program will pass all five pieces of information to a function and the function will calculate the weekly fuel cost and then return the information to the main program. This should happen for each car. Once each cost has been calculated, the program will then display the cost difference between the two cars to the user.
Create a
Functions enable us to break down a problem into smaller sections. By using a function to calculate the cost, this could be repeated multiple times or used in other programs without having to repeat the code and it should work reliably each time because it has been debugged and tested.
This program is intended to help a person to compare weekly fuel cost of two new cars.
To calculate the weekly fuel cost for car you need several things:
- Weekly highway driving
- Weekly city driving
- Highway Miles per Gallon (MPG)
- City Miles per Gallon (MPG)
- The cost per gallon of gas.
The steps to calculate the total weekly cost is:
- Calculate the amount of fuel used in highway driving:
- Take the number of highway miles driven and divide it by the highway mpg.
- Calculate the amount of fuel used in city driving:
- Take the number of city miles driven and divide it by the city mpg.
- Add the gallons used from each of the calculations to get the total gallons used.
- Take the gallons used and multiply that by the cost per gallon of gas.
The program should prompt the user for the following information at the beginning of the program (this same information is used for each car):
- Estimated weekly highway driving.
- Estimated weekly city driving.
- Estimated Cost for a gallon of gas.
It should prompt them for the following information for each car:
- The car make and model.
- Highway MPG for the car.
- City MPG for the car.
Once the user has entered the required information, the program will pass all five pieces of information to a function and the function will calculate the weekly fuel cost and then return the information to the main program. This should happen for each car.
Once each cost has been calculated, the program will then display the cost difference between the two cars to the user.
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 5 images