In: Computer Science
Three small airplanes flight a 1000-mile route. Write a program
that inputs the airplane make,
model, and the number of gallons of fuel used by each airplane.
After calling a calcMPG()
function once for each airplane have main determine and display
which airplane is the most
fuel-efficient, and how many miles per gallon it got. The calcMPG()
function should be passed
the distance flew and the gallons of gas consumed as arguments and
should return the miles
per gallon obtained. Code must be written in C++
Answer: Hey!! Kindly finds your solution below. Let me know if any issue.Thanks.
Copy to Code: This code has function to calculate MPG of an airplane that return MPG of airplane. In main, user input information about all three airplanes and calculate their MPG store into an array, Then compare all MPG's and find max and print info about that airplane that gives best MPG.
#include<iostream>
using namespace std;
double calcMPG(double dis,double gallons)//function calcMPG() takes
arguments
{
double miles;
miles = dis/gallons;
return miles;
}
int main()
{
string make[3],model[3];//arrays and variables
double miles[3],d = 1000,g,temp;
for(int i=0;i<3;i++)//for loop to take inputs for
three airplanes
{ cout<<"Enter airplane make:
";
cin>>make[i];//takes input
for make
cout<<"enter airplane model:
";
cin>>model[i];//takes input
for model
cout<<"Enter gallons of gas
used: ";
cin>>g;//takes input for
gallons gas
miles[i] = calcMPG(d,g);//call
function to get MPG
}
temp = miles[0];
int c =1;
for(int i=1;i<3;i++)//find Fuel-Efficient plane
(which MPG is max)
{
if(temp<miles[i])
{
temp=miles[i];
c = i;
}
}
cout<<"Fuel-Efficient airplane: ";//print info
about Fuel-Efficient plane
cout<<"\nAirplane Make: "<<make[c];
cout<<"\nModel: "<<model[c];
cout<<"\nMPG: "<<temp;
return 0;
}
Screenshot of code and output: