Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to perform various calculations related to the fuel economy

ID: 3728298 • Letter: W

Question

Write a C++ program to perform various calculations related to the fuel economy of a vehicle where the fuel economy is modeling using a polynomial of the form y = Ax2 + Bx + C, where

y = fuel economy in miles per gallon (mpg)

x = speed in miles per hour (mph)

In particular:

Inputs: The user should be prompted to input the following information.

The values for coefficients A, B, and C used to model the fuel efficiency

The capacity of the fuel tank (in gallons).

The current amount of fuel in the tank (in gallons).

The current speed of the vehicle (in mpg)

The distance to be travelled on the current trip (in miles)

The cost per gallon for gasoline

The minimum speed, Smin, to be used in the table of Fuel Economy vs Speed

The maximum speed, Smax, to be used in the table of Fuel Economy vs Speed

The speed increment, Sinc, to be used in the table of Fuel Economy vs Speed

Functions: The program should use at least 4 user-defined functions (in addition to main) as described below.

MPG(A, B, C, Speed) – This function returns the fuel economy in mpg for a given speed in mph.

PrintTable(Smin, Smax, Sinc A, B, C) – This function will print a table of Speed (in mpg) and Fuel Economy (in mpg).

Use the range of speeds indicated with the speed increment indicated.

This function should call the function MPG above.

Fuel economy should be calculated using the coefficients A, B, and C provided.

Include a table heading with units.

Display speeds as integers and fuel economy with 2 digits after the decimal point (include trailing zeros).

MaxEconomy(Smin, Smax, Sinc A, B, C, MaxMPG, MaxMPH) – This function will return the maximum mpg and the corresponding speed value using the speed range and increment specified. This function should call the function MPG above.

Use at least one more useful (user-defined) function to calculate one or more of the program outputs.

Outputs: The program output should include the following:

Neatly summarize the input values

A table of Speed and Fuel Economy values (created by the PrintTable function above).

The maximum fuel economy (in mpg) and the corresponding speed (determined by the MaxEconomy function above).

The fuel economy (in mpg) at the current speed

The minimum fuel economy (in mph) and the corresponding speed. Note: This does not always occur at the minimum speed.

For the current speed, trip distance, number of gallons currently in the tank, and cost per gallon for fuel (show the value of each), display the following:

The fuel economy (in mpg)

Speed for the trip (in mph)

The fuel cost for the trip.

The number of gallons that will be used for the trip.

The time to reach the destination.

State how many times you will need to stop for gas. Assume that the tank must be filled when it is 10% full.

State the number of gallons of gas will be left in the tank at the end of the trip.

State the number of miles until the next time the tank must be filled (after the trip).

Repeat the above if you drive at the speed for maximum fuel economy. Also state how many gallons of gas were saved and how much money was saved by driving at the speed for maximum fuel efficiency.

Use a suitable number of digits for all numeric outputs and include units when appropriate.

Error Checks: The program should check for appropriate ranges for inputs and allow the user to re-enter any incorrect inputs, including:

Fuel tank capacity: 0 to 20 gallons

Current amount of fuel in tank: 20% - 100% of fuel tank capacity

Current speed of vehicle: 20 to 80 mph

Distance to be travelled: Must be > 0

Cost per gasoline: Must be > 0

Minimum speed for table (Smin): Integer value where 20 < Smin < 50

Maximum speed for table (Smax): Integer value where (Smin + 10) < Smax < 80

Speed increment for table (Sinc): Integer value where 0 < Sinc < (Smax – Smin)/5

Re-running the Program: Include a loop that will give the user the option of re-running the program.

Example: The output does not need to look exactly like this, but it should have the same information. Some results have been omitted. A = -0.004

B = 0.45

C = 16

18.0 gallon fuel tank

17.0 gallons currently in the tank

Current speed: 40 mph

100 miles to be travelled

Gasoline cost: $2.50/gallon

Let S vary from 20 mph to 80 mph in 2 mph increments

Fuel Economy Table: Speed (mph) Fuel Economy (mpg) 20 23.40 22 23.96 . . 80 26.60

Maximum Fuel Economy: 28.66 mpg at 56 mph

Current Fuel Economy: 27.60 mpg at 40 mph

Minimum Fuel Economy: 23.40 mpg at 20 mph

Trip Option #1: Travelling 100 miles at 40 mph with 17.0 gallons currently in the tank and gas cost of $2.50:

- Fuel economy: 27.60 mpg

- Speed: 40 mph

- Fuel cost for the trip:

- Number of gallons that will be used for the trip:

- Time to reach the destination:

- Number of times to fill the tank:

- Number of gallons left in the tank after the trip:

- Number of miles until the next time the tank must be filled after the trip:

Trip Option #2: Travelling 100 miles at 56 mph with 17.0 gallons currently in the tank and gas cost of $2.50:

- Fuel economy: 28.66 mpg

- Speed: 56 mph - Fuel cost for the trip:

- Number of gallons that will be used for the trip:

- Time to reach the destination:

- Number of times to fill the tank:

- Number of gallons left in the tank after the trip:

- Number of miles until the next time the tank must be filled after the trip:

- Number of gallons saved by driving at maximum fuel economy (compared to speed in Option 1):

- Amount of money saved by driving at maximum fuel economy:

Explanation / Answer

#include<iostream>

#include<cmath>

using namespace std;

double A,B,C,Capacity,Amt,Speed,Dist,CPG,Smin,Smax,Sinc;

double MPG(double A,double B,double C,double Speed)

{

return A*Speed*Speed+B*Speed+C;

}

bool check()

{

if(Capacity<=0 || Capacity>20)

{

cout<<"Error, Re-enter Tank Capacity"<<endl;

cin>>Capacity;

return false;

}

if(Amt<0.2*Capacity || Amt>Capacity)

{

cout<<"Error, Re-enter Amount of fuel in tank"<<endl;

cin>>Amt;

return false;

}

if(Speed<20 || Speed>80)

{

cout<<"Error, Re-enter Current speed"<<endl;

cin>>Speed;

return false;

}

if(Dist<=0)

{

cout<<"Error, Re-enter Distance"<<endl;

cin>>Dist;

return false;

}

if(CPG<=0)

{

cout<<"Error, Re-enter Cost per gallon"<<endl;

cin>>CPG;

return false;

}

if(Smin<=20 || Smin>=50)

{

cout<<"Error, Re-enter Smin"<<endl;

cin>>Smin;

return false;

}

if(Smax>80 || Smax<Smin+10)

{

cout<<"Error, Re-enter Smax"<<endl;

cin>>Smax;

return false;

}

if(Sinc<=0 || Sinc>(Smax-Smin)/5)

{

cout<<"Error, Re-enter Sinc"<<endl;

cin>>Sinc;

return false;

}

}

void PrintTable(double Smin,double Smax,double Sinc,double A,double B,double C)

{

cout<<"Fuel Economy Table"<<endl<<fixed<<setprecision(2)<<"MPG"<<" "<<"MPH"<<endl;

while(Smin<=Smax)

{

cout<<(int)Smin<<" "<<round(MPG(A,B,C,Smin))<<endl;

Smin+=Sinc;

}

}

double MaxEconomy(double Smin,double Smax,double Sinc,double A,double B,double C)

{

double max=MPG(A,B,C,Smin);

double q,p=Smin;

Smin+=Sinc;

while(Smin<=Smax)

{

if(q=MPG(A,B,C,Smin)>max)

{

max=q;

p=Smin;

}

Smin+=Sinc;

}

return p;

}

double MinEconomy(double Smin,double Smax,double Sinc,double A,double B,double C)

{

double min=MPG(A,B,C,Smin);

double q,p=Smin;

Smin+=Sinc;

while(Smin<=Smax)

{

if(q=MPG(A,B,C,Smin)<min)

{

min=q;

p=Smin;

}

Smin+=Sinc;

}

return p;

}

void print(double A,double B,double C,double Speed,double Capacity,double CPG,double dist,double Amt)

{

cout<<"Trip Option: "<<endl;

cout<<"Fuel Economy: "<<MPG(A,B,C,Speed)<<" mpg"<<endl;

cout<<"Speed: "<<Speed<<" mph"<<endl;

double NoG = dist/MPG(A,B,C,Speed);

cout<<"Fuel cost for the trip: "<<NoG*CPG<<endl;

cout<<"Number of gallons: "<<NoG<<endl;

cout<<"Time to reach destination: "<<dist/Speed<<" hours"<<endl;

double Number_of_fills=ceil((NoG-(Amt-0.1*Capacity))/(Capacity*0.9));

cout<<"Number of times to fill tank: "<<Number_of_fills<<endl;

cout<<"Number of gallons left after trip: "<<Number_of_fills*(Capacity*0.9)+Amt-NoG<<endl;

cout<<"Number of miles till tank must be refilled: "<<((Number_of_fills*(Capacity*0.9)+Amt-NoG)-(Capacity*0.1))*MPG(A,B,C,Speed)<<endl;

}

int main()

{

int ch=1;

while(ch==1)

{

cout<<"Enter data";

cin>>A>>B>>C>>Capacity>>Amt>>Speed>>Dist>>CPG>>Smin>>Smax>>Sinc;

while(!check());

cout<<"A = "<<A<<endl<<"B = "<<B<<endl<<"C = "<<C<<endl<<Capacity<<" gallon fuel tank"<<endl<<Amt<<" currently in tank"<<endl<<"Current Speed: "<<Speed<<endl;

cout<<Dist<<" miles to be travelled"<<endl<<"Gasoline cost: $"<<CPG<<"/gallon"<<endl<<"Let S vary from "<<Smin<<" mph to "<<Smax<<" mph in "<<Sinc<<" mph increments"<<endl;

PrintTable(Smin,Smax,Sinc,A,B,C);

double MaxEco=MaxEconomy(Smin,Smax,Sinc,A,B,C);

double MinEco=MinEconomy(Smin,Smax,Sinc,A,B,C);

cout<<"Max Fuel Economy: "<<MPG(A,B,C,MaxEco)<<" mpg at "<<MaxEco<<" mph"<<endl;

cout<<"Current Fuel Economy: "<<MPG(A,B,C,Speed)<<" mpg at "<<Speed<<" mph"<<endl;

cout<<"Min Fuel Economy: "<<MPG(A,B,C,MinEco)<<" mpg at "<<MinEco<<" mph"<<endl;

print(A,B,C,Speed,Capacity,CPG,Dist,Amt);

print(A,B,C,MaxEco,Capacity,CPG,Dist,Amt);

cout<<"Amount saved = "<<Dist/MPG(A,B,C,Speed)*CPG-Dist/MPG(A,B,C,MaxEco)*CPG<<endl;

cout<<"1. Retry 0.Exit"<<endl;

cin>>ch;

}

}

Note: if any output needs to be rounded to 2 decimal places, add <<fixed<<setprecision(2) before printing

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote