Final answer:
The DrivingCost() function calculates the cost of driving given the miles driven, vehicle's fuel efficiency, and gas price. Output formatted to two decimal places is achieved with setprecision(2) in cout statements.
Step-by-step explanation:
The function DrivingCost() calculates the cost of driving a certain number of miles based on the vehicle's fuel efficiency (miles per gallon) and the price of gasoline (dollars per gallon).
Function Definition
double DrivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon) {
return (drivenMiles / milesPerGallon) * dollarsPerGallon;
}
Example of Use
When provided with a car that runs 20 miles per gallon and gasoline at $3.1599 per gallon, the cost for driving
- 10 miles is $1.58,
- 50 miles is $7.90, and
- 400 miles is $63.20.
These calculations are made by calling the function
DrivingCost
with the appropriate parameters and returning the cost. Cout statements should use
fixed
and
setprecision(2)
for formatting the output.