Respuesta :
Answer:
// program in C++.
#include <bits/stdc++.h>
using namespace std;
// function to calculate cost
double DrivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon){
double cost;
//Expression to compute cost
cost = drivenMiles / milesPerGallon * dollarsPerGallon;
return cost;
}
// main function
int main()
{
double drivenMiles, milesPerGallon, dollarsPerGallon;
//initialise the variables
drivenMiles = 50;
milesPerGallon = 20.0;
dollarsPerGallon = 3.1599;
//Call function
double cost = DrivingCost(drivenMiles,milesPerGallon,dollarsPerGallon);
//Display result
cout<<"Driven miles : "<<drivenMiles<<endl;
cout<<"Miles per Gallon : "<<milesPerGallon<<endl;
cout<<"Dollars per Gallon : "<<dollarsPerGallon<<endl;
cout << fixed << setprecision(2);
cout<<"The total driving cost : "<<cost ;
return 0;
}
Explanation:
Declare and initialize drivenMiles with 50, milesPerGallon with 20.0 and dollarsPerGallon with 3.1599.Call the function DrivingCost() with these parameters,This will calculate the cost and return the value.Print the result after getting the cost.Similarly we can calculate cost for drivenMiles equals to 10 and 400 miles also.
Output:
Driven miles : 50
Miles per Gallon : 20
Dollars per Gallon : 3.1599
The total driving cost : 7.90