Write a function that calculates the annual cost of commuting to work for an arr
ID: 3763425 • Letter: W
Question
Write a function that calculates the annual cost of commuting to work for an array of five distances. The function should take as arguments an array that contains the distances, the miles per gallon of the vehicle, and the number of days a year that the commute is made. Assume gas is $3.00 a gallon. Print the annual cost of the gas for each distance calculation.
Use the formula:
annualCost = ((distance / mpg) * cost per gallon) * days commuting
Use the header:
void calculateCost(int distanceArray[], float mpg, int daysPerYear);
For example:
int distancesArray[5]={150,60,120,90,270};
float mpg=30.0;
int daysPerYear =15;
Would print out:
$225
$90
$180
$135
$405
in C++
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
void calculateCost(int distanceArray[], float mpg, int daysPerYear)
{
double cost;
for(int i=0;i<5;i++)
{
cost = (distanceArray[i]/mpg)*3*daysPerYear;
cout << "$" << cost << endl;
}
}
int main()
{
int distancesArray[5]={150,60,120,90,270};
float mpg=30.0;
int daysPerYear =15;
calculateCost(distancesArray,mpg,daysPerYear);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.