I need help writing this C program. I\'ve tried it a few differentways but can\'
ID: 3618055 • Letter: I
Question
I need help writing this C program. I've tried it a few differentways but can't get it to work correctly. Any help is appreciated,whether it be program code or just hints on how to get it done.Thanks.a) Write a C program that accepts a mileage and gallons valueand calculates the miles-per-gallon achieved for that segment ofthe trip. The mpg is obtained as the difference in mileagebetween fill-ups divided by the number of gallons of gasolinerecieved in the fill-up.
b) Modify the program written in part "a" to additionallycompute and display the cumulative mpg achieved after each fillup. The cumulative mpg is calculated as the differencebetween fill-up mileage and the mileage at the start of the tripdivided by the sum of the gallons used to that point in the trip.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
double milesdriven=0,gallons=0,MPG,totalMPG,sumofmiles,sumofgallons;
sumofmiles=milesdriven;
sumofgallons=gallons;
while(milesdriven!=-1)
{
cout<<"Please enter the miles driven:"<<endl;
cin>>milesdriven;
if(milesdriven==-1)
{
return 1;
}
else
{
cout<<"Please enter gallons used:"<<endl;
cin>>gallons;
MPG=milesdriven/gallons;
cout<<"MPG for this tankful:"<<MPG<<endl;
sumofmiles=sumofmiles+milesdriven;
sumofgallons=sumofgallons+gallons;
totalMPG=sumofmiles/sumofgallons;
cout<<"Total MPG:"<<totalMPG<<endl;
}
}
return 0;
}