Question

In: Computer Science

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

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.

Solutions

Expert Solution

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

#include <iostream>

using namespace std;
float MPG(float,float,float,int);
void PrintTable(int , int , int,float,float ,float,int );
void MaxEconomy(int,int,int,float,float,float,float,int);
void output(float,float);
float capacity,amt,cost;
int dist;
int main()
{
float a,b,c;

int speed,smin,smax,sinc;


cout << "Fuel Economy of a Vehicle " << endl;
cout<<"Enter the values for coefficients A, B, and C: ";
cin>>a>>b>>c;
cout<<"Enter the capacity of the fuel tank (in gallons): " ;
cin>>capacity;
if(capacity<0 || capacity>20){
cout<<"Fuel tank capacity should be in range :(0 to 20) gallons.. please re-enter the value: ";
cin>>capacity;
}
cout<<"Enter the current amount of fuel in the tank (in gallons): ";
cin>>amt;
if(amt<20 || amt >100){
cout<<"Current amount of fuel in tank should be in range :(20% - 100%).. please re-enter the value: ";
cin>>amt;
}

cout<<"Enter the current speed of the vehicle (in mpg): ";
cin>>speed;
if(speed<20 || speed >80){
cout<<"Current speed of vehicle should be in range:(20 to 80) mph.. please re-enter the value: ";
cin>>speed;
}
cout<<"Enter the distance to be traveled on the current trip (in miles): ";
cin>>dist;
if(dist<0){
cout<<"Distance should be greater than 0..please re-enter the value: ";
cin>>dist;
}

cout<<"Enter the cost per gallon for gasoline: ";
cin>>cost;
if(cost<0){
cout<<"Gasoline cost should be greater than 0..please re-enter the value: ";
cin>>cost;
}

cout<<"Enter the minimum speed (Smin): ";
cin>>smin;
if(smin<20 || smin >50){
cout<<" Integer value should be (20 < Smin < 50).Re-enter the value:";
cin>>smin;
}
cout<<"Enter the maximum speed( Smax): ";
cin>>smax;
if(smax<(smin + 10) || smax >80){
cout<<"Integer value should be((Smin + 10) < Smax < 80).. re-enter the value:";
cin>>smax;
}
cout<<"Enter the speed increment, (Sinc): ";
cin>>sinc;
int dum=(int)(smax-smin)/5;
if(sinc<0 || sinc>dum ){
cout<<" Integer value should be(0 < Sinc < (Smax – Smin)/5)..re-enter the value:";
cin>>sinc;

}
float y= MPG(a,b,c,speed);
cout<<"Y: "<<y<<endl;
PrintTable(smin,smax,sinc,a,b, c,speed);
return 0;
}

float MPG(float a,float b,float c,int x)
{
float y = ((a*(float)x*x) + (b*(float)x) + c);
return y;
}
void PrintTable(int smin, int smax, int sinc,float a,float b,float c,int s)
{
cout<<" Fuel Economy Table"<<endl;
cout<<"Speed (mph) Fuel Economy (mpg)"<<endl;
float mpg[200];
for(int i=smin;i<=smax;(i=i+sinc))
{
mpg[i]= MPG(a,b,c,i);
cout<<i<<"\t\t"<<mpg[i]<<endl;
}
int mini=smin;
int maxi=smin;
for(int i=(smin+sinc);i<=smax;(i=i+sinc))
{
if(mpg[mini]>mpg[i])
mini=i;
if(mpg[maxi]<mpg[i])
maxi=i;

}
cout<<"Maximum Fuel Economy: "<<mpg[maxi]<<" mpg at "<<maxi<<" mph"<<endl;
cout<<"Current Fuel Economy: "<<mpg[s]<<" mpg at "<<s<<" mph"<<endl;
cout<<"Minimum Fuel Economy: "<<mpg[mini]<<" mpg at "<<mini<<" mph"<<endl<< endl;
cout<<"Trip Option #1: "<<endl;
MaxEconomy(smin, smax, sinc, a,b,c, mpg[maxi], maxi);
cout<<"Trip Option #2: "<<endl;
MaxEconomy(smin, smax, sinc, a,b,c, mpg[s], s);
}

void MaxEconomy(int smin,int smax,int sinc,float a,float b,float c,float mpg,int mph)
{
cout<<"Travelling "<<dist<<" miles at "<<mph<<" mph with "<< capacity<<" gallons currently in the tank and gas cost of $"<<amt<<endl;
cout<<"Fuel economy: "<<mpg<<" mpg"<<endl;
cout<<"Speed: "<<mph<<" mph"<<endl;
output(mpg,mph);
}

void output(float mpg,float mph)
{
cout<<"Fuel cost for the trip: "<<(mpg*amt)<<endl;
int gall=(dist/5);
cout<<"Number of gallons that will be used for the trip: "<<gall<<endl;
cout<<"Time to reach the destination: "<<(dist/mph)<<endl;

cout<<"Number of times to fill the tank: "<<((gall-capacity)/2)<<endl;
cout<<"Number of gallons left in the tank after the trip: "<<(gall-capacity)<<endl;
cout<<"Number of miles until the next time the tank must be filled after the trip: "<<((gall-capacity)/5)<<endl;


}

//for any modification or clarification do comments. //in ouput function, as you didn't specifiy any claculation, i have given a rough calculation.

Kindly revert for any queries

Thanks.


Related Solutions

Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The program will add, subtract, multiply, or divide 2 numbers and provide the average of multiple numbers inputted from the user. You need to define a function named performCalculation which takes 1 parameter. The parameter will be the operation being performed (+,-,*,/). This function will perform the given prompt from the user for 2 numbers then perform the expected operation depending on the parameter that’s...
How to calculate overall fuel economy of hydraulic hybrid vehicle? Know that fuel economy formula is...
How to calculate overall fuel economy of hydraulic hybrid vehicle? Know that fuel economy formula is litre per used over distance travelled. How to calculate the fuel used for both ICE and hydraulic system
C++ program to perform each of the area calculations in separate functions. Your program will take...
C++ program to perform each of the area calculations in separate functions. Your program will take in the relevant information in the main (), call the correct function that makes the calculation, return the answer to the main () and then print the answer to the screen. The program will declare a variable called “choice” of type int that is initialized to 0. The program will loop while choice is not equal to 4. In the body of the loop...
Write a C ++ program that asks the user for the speed of a vehicle (in...
Write a C ++ program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. The program should then use a loop to display the distance the vehicle has traveled for each hour of that time period. Here is an example of the output: What is the speed of the vehicle in mph? 40 How many hours has it traveled? 3 Hour Distance Traveled -------------------------------- 1           40 2           80...
Write an 8086 assembler program (using the emulator) that will perform the following calculations. All values...
Write an 8086 assembler program (using the emulator) that will perform the following calculations. All values below are decimals. You must attach Screenshot of emu8086 showing the different register values at the end of the execution A printed copy of your code (screenshot is OK) a) (6 -4) * 20 - (9 + 1)            Place the answer in the AX register b) 1000 - 4 * (3 *32)             Place the answer in the AX register
C program help 1. Write a program to compute the Mileage given by a vehicle. Mileage...
C program help 1. Write a program to compute the Mileage given by a vehicle. Mileage = (new_odometer – old_odometer)/(gallons_gas) // illustrating how ‘for’ loop works. 2. How to initialize an array of size 5 using an initializer list and to compute it’s sum How to initialize an array of size 5 with even numbers starting from 2 using ‘for’ loop and to compute it’s sum 3. Program to compute the car insurance premium for a person based on their...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
write a C++ program that : 1. Perform a rot13 substitution 2. Perform a caesar encryption...
write a C++ program that : 1. Perform a rot13 substitution 2. Perform a caesar encryption given a dictionary 3. Perform a caesar decryption given a dictionary 4. Create a random caesar cipher dictionary If user prints: -r : Perform rot13 substitution -g : generate a random caesar cipher dictionary. -e: Encrypt using the caesar cipher -d : Decrypt using the caesar cipher The format for the caesar cipher dictionary is a file with 26 pairs of letters, one per...
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the user to input two positive integers. variables: firstNum and secondNum (firstNum must be less than secondNum). Validate the user's input; prompt the user again if firstNum is not less than secondNum (use for loop). b. Output all odd numbers between firstNum and secondNum. (use for loop). c. Output the sum of all even numbers between firstNum and secondNum. (use for loop). d. Output the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT