c practice problem Declare a single structure data type suit

c++ practice problem.

Declare a single structure data type suitable for a motorbike structure of the type illustrated below: Write a program using the data type you declared to interactively accepts the above data into an array of five structures. Once the data have been entered, the program should create a report listing each motorbike number and miles per gallon achieved by the motorbike. At the end of the report include the average miles per gallon achieved by the complete fleet of motorbikes.

Solution

#include<iostream>
#include<string.h>
using namespace std;

struct MotorBike
{
int motorBikeNumber;
double miles,gallons;
};

int main()
{
MotorBike bikes[5];
double milePerGallon;
double sum = 0;
for(int i=0;i<5;i++)
{
cout << \"Enter below details for motor bike \" << i+1 << endl;
cout << \"Enter the bike number : \";
cin >> bikes[i].motorBikeNumber;
  
cout << \"Enter the mile driven : \";
cin >> bikes[i].miles;
  
cout << \"Enter the gallons used : \";
cin >> bikes[i].gallons;
}
  
cout << \"Bikes Report : \" << endl;
cout << \"BikeNumber\\tMiles per Gallons\" << endl;
  
for(int i=0;i<5;i++)
{
milePerGallon = bikes[i].miles/bikes[i].gallons;
sum = sum+milePerGallon;
cout << bikes[i].motorBikeNumber << \"\\t\\t\\t\" << milePerGallon << endl;
}
  
cout << endl;
cout << \"Average miles per gallons achieved is : \" << sum/5.0;
return 0;
  
}

OUTPUT:

Enter below details for motor bike 1
Enter the bike number : 17

Enter the mile driven : 1230

Enter the gallons used : 60

Enter below details for motor bike 2
Enter the bike number : 21

Enter the mile driven : 3660

Enter the gallons used : 139

Enter below details for motor bike 3
Enter the bike number : 42

Enter the mile driven : 1771

Enter the gallons used : 64

Enter below details for motor bike 4
Enter the bike number : 57

Enter the mile driven : 2960

Enter the gallons used : 101

Enter below details for motor bike 5
Enter the bike number : 69

Enter the mile driven : 2113

Enter the gallons used : 76

Bikes Report :
BikeNumber   Miles per Gallons
17           20.5
21           26.3309
42           27.6719
57           29.3069
69           27.8026

Average miles per gallons achieved is : 26.3225

c++ practice problem. Declare a single structure data type suitable for a motorbike structure of the type illustrated below: Write a program using the data type
c++ practice problem. Declare a single structure data type suitable for a motorbike structure of the type illustrated below: Write a program using the data type

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site