In C Create an array of five strings The strings will have f
In C++
Create an array of five strings. The strings will have five different types of cars: “Sports Coupe”, “Sedan”, “SUV”, “Minivan”, “Pickup”
Create an array to hold the number of miles each of the types of cars can drive on a full tank.
Create another array to hold the number of gallons each type of car can hold in its tank.
Finally, create an array to hold the miles per gallon for each car. 2 Initialize the miles and gallons arrays with values of your own choosing.
Use a loop to calculate the values that go in the miles per gallon array for each type of car. Print the string that matches the type of car with the lowest miles per gallon, along with its actual miles per gallon value. Do the same for the type of car with the highest miles per gallon.
Solution
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Declaring variables
string lowest,highest;
//Creating an array car names of size 5
string car_names[]={\"Sports Coupe\", \"Sedan\", \"SUV\", \"Minivan\", \"Pickup\"};
/* Creating an array of size 5 which will\\
* hold the the total no of miles on full tank
*/
int no_of_miles_onFullTank[]={200,180,190,160,150};
/* Creating an array of size 5 which will hold
* the total capacity of tank of each car in terms of gallons
*/
int no_of_gallons[]={7,6,5,8,7,6};
/* Creating an array which will hold
* miles per gallon of each car
*/
double miles_per_gallon[5];
//For loop which will finds the miles per gallon of each car
for(int i=0;i<5;i++)
{
/* calculating miles per each gallon of each car
*and populating those values into an array
*/
miles_per_gallon[i]=no_of_miles_onFullTank[i]/no_of_gallons[i];
}
//Assigning the first element of an array to min and max variables
double min=miles_per_gallon[0];
double max=miles_per_gallon[0];
for(int i=0;i<5;i++)
{
if(miles_per_gallon[i]<min)
{
min=miles_per_gallon[i];
lowest=car_names[i];
}
if(miles_per_gallon[i]>max)
{
max=miles_per_gallon[i];
highest=car_names[i];
}
}
/* Displaying each car name and miles per each gallon
* and also which car is giving highest mileage and lowest mileage
*/
for(int i=0;i<5;i++)
{
cout<<\"\ Car Name :\"<<car_names[i]<<endl;
cout<<\"Actual Miles Per Gallon :\"<<miles_per_gallon[i]<<endl;
if(lowest.compare(car_names[i])==0)
{
cout<<\"The Car which gives lowest miles per gallon is :\"<<car_names[i]<<endl;
}
if(highest.compare(car_names[i])==0)
{
cout<<\"The Car which gives Highest miles per gallon is :\"<<car_names[i]<<endl;
}
}
return 0;
}

