Melokuhle is a very young intelligent and adventurous individual. He has recently bought 4 quantums (taxi) to transport people around the country. As a result, he is planning a trip to Upington. Therefore, he has hired you to write a program in pseudocode as well as in C++ to evaluate the fuel consumption on his 4 cars during the planned trip. The kilometers and fuel level in the tank at the start and end of the journey should be entered by the end-user. Calculate for each car, the fuel used, kilometers traveled, and the overall fuel consumption in kilometers traveled per litre of fuel.
#include <iostream>
using namespace std;
double kilometersTraveled(double startKilometers, double endKilometers)
{
return (endKilometers - startKilometers);
}
double calculateUsedFuel(double startFuel, double endFuel)
{
return (startFuel - endFuel);
}
double calculateUsedFuelPerKilometers(double startFuel, double endFuel, double startKilometers, double endKilometers)
{
return (calculateUsedFuel(startFuel, endFuel) / kilometersTraveled(startKilometers, endKilometers));
}
int main()
{
double startKilometers[4];
double endKilometers[4];
double startFuelLevel[4];
double endFuelLevel[4];
for (int i = 0; i < 4; i++)
{
cout << "Enter " << i + 1 << " car: " << endl;
cout << "Enter start kilometers in the car: ";
cin >> startKilometers[i];
cout << "Enter end kilometers in the car: ";
cin >> endKilometers[i];
cout << "Enter start fuel level in the tank: ";
cin >> startFuelLevel[i];
cout << "Enter end fuel level in the tank: ";
cin >> endFuelLevel[i];
cout << endl;
}
for (int i = 0; i < 4; i++)
{
cout << "Used fuel for " << i + 1 << " car: " << calculateUsedFuel(startFuelLevel[i], endFuelLevel[i]) << endl;
}
cout << endl;
for (int i = 0; i < 4; i++)
{
cout << "Kilometers traveled for " << i + 1 << " car: " << kilometersTraveled(startKilometers[i], endKilometers[i]) << endl;
}
cout << endl;
for (int i = 0; i < 4; i++)
{
cout << "Used fuel per kilometers for " << i + 1 << " car: " << calculateUsedFuelPerKilometers(startFuelLevel[i], endFuelLevel[i], startKilometers[i], endKilometers[i]) << endl;
}
return 0;
}
Comments
Leave a comment