Sample Input/Output: Please enter the numb er of cars in the experiment: 5 Pleas
ID: 3816933 • Letter: S
Question
Sample Input/Output:
Please enter the number of cars in the experiment: 5
Please enter the information for car 1:
Model: Tahoe
Miles Driven: 127
Gallons Gas used: 10
Please enter the information for car 2:
Model: Jeep
Miles Driven: 198
Gallons Gas used: 12
Please enter the information for car 3:
Model: Blazer
Miles Driven: 223
Gallons Gas used: 15
Please enter the information for car 4:
Model: Yukon
Miles Driven: 225.4
Gallons Gas used: 13.7
Please enter the information for car 5:
Model: Echo
Miles Driven: 150.2
Gallons Gas used: 11.8
Vehicle Model Miles driven Gallons used Miles/gallon Rating
1 Tahoe 127.0 10.0 12.7 Poor
2 Jeep 198.0 12.0 16.5 Good
3 Blazer 223.015.0 14.9 Good
4 Yukon 225.4 13.7 16.5 Good
5 Echo 150.2 11.8 12.7 Poor
Highest Miles per Gallon: 16.5 miles/gallon
Lowest Miles per Gallon: 12.7 miles/gallon
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
struct VehicleRecord //struct definition
{
string model;
double milesDriven;
double gallons;
double MPG;
string MPGRating;
};
int inputVehicles() //input and validate the number of cars
{
int n;
cout<<"Please enter the number of cars in the experiment:";
cin>>n;
while(n < 0)
{
cout<<" Error : Number of vehicles should not be negative. Try again. ";
cin>>n;
}
return n;
}
void readData(struct VehicleRecord vehicle[],int n) //read and validate data for n cars
{
double milesDriven,gallons;
int i;
for(i=0;i<n;i++)
{
cout<<" Please enter the information for car "<<i<<":";
cout<<" Model: ";
cin>>vehicle[i].model;
cout<<" Miles Driven: ";
cin>>milesDriven;
while(milesDriven <0)
{
cout<<" Error : Miles driven should not be negative. Try again. ";
cin>>milesDriven;
}
vehicle[i].milesDriven = milesDriven;
cout<<" Gallons Gas used:";
cin>>gallons;
while(gallons <0)
{
cout<<" Error : Gallons of gas should not be negative. Try again. ";
cin>>gallons;
}
vehicle[i].gallons = gallons;
}
}
void calculateMPG(struct VehicleRecord vehicle[],int n,double *highestMPG,double *lowestMPG)
{
*highestMPG = 0;
*lowestMPG = 9999;
int i;
for(i=0;i<n;i++)
{
vehicle[i].MPG = vehicle[i].milesDriven/vehicle[i].gallons;
if(vehicle[i].MPG > 13)
vehicle[i].MPGRating = "Good";
else
vehicle[i].MPGRating = "Poor";
if(vehicle[i].MPG > *highestMPG)
*highestMPG = vehicle[i].MPG;
if(vehicle[i].MPG < *lowestMPG)
*lowestMPG = vehicle[i].MPG;
}
}
void printData(struct VehicleRecord vehicle[],int n)
{
int i;
cout<<" Vehicle Model Miles Driven Gallons Used Miles perGallon Rating";
for(i=0;i<n;i++)
{
cout<<" "<<i<<" "<<vehicle[i].model<<" "<<vehicle[i].milesDriven<<" "<<vehicle[i].gallons<<" "<<vehicle[i].MPG<<" "<<vehicle[i].MPGRating;
}
}
int main()
{
struct VehicleRecord vehicle[50];
int n;
double highestMPG,lowestMPG;
n = inputVehicles();
readData(vehicle,n);
calculateMPG(vehicle,n,&highestMPG,&lowestMPG);
cout<<fixed<<setprecision(1);
printData(vehicle,n);
cout<<" Highest Miles per Gallon: "<<highestMPG<<" miles/gallon";
cout<<" Lowest Miles per Gallon: "<<lowestMPG<<" miles/gallon";
return 0;
}
output:
Please enter the number of cars in the experiment: 5
Please enter the information for car 1:
Model: Tahoe
Miles Driven: 127
Gallons Gas used: 10
Please enter the information for car 2:
Model: Jeep
Miles Driven: 198
Gallons Gas used: 12
Please enter the information for car 3:
Model: Blazer
Miles Driven: 223
Gallons Gas used: 15
Please enter the information for car 4:
Model: Yukon
Miles Driven: 225.4
Gallons Gas used: 13.7
Please enter the information for car 5:
Model: Echo
Miles Driven: 150.2
Gallons Gas used: 11.8
Vehicle Model Miles driven Gallons used Miles/gallon Rating
1 Tahoe 127.0 10.0 12.7 Poor
2 Jeep 198.0 12.0 16.5 Good
3 Blazer 223.015.0 14.9 Good
4 Yukon 225.4 13.7 16.5 Good
5 Echo 150.2 11.8 12.7 Poor
Highest Miles per Gallon: 16.5 miles/gallon
Lowest Miles per Gallon: 12.7 miles/gallon
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.