// C++ program to read the input file and calculate the MPG for
each car and calculate and display the total miles driven, total
gallons used and average MPG for all cars
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream fin("car_readings.txt"); // open the input file, provide
full path to file
int carNumber, milesDriven, gallonsUsed;
double mpg, total_miles_driven = 0, total_gallons_used = 0, avg_mpg
= 0;
int numCars=0;
// if file can be opened
if(fin.is_open())
{
cout<<left<<setw(15)<<"Car
Number"<<left<<setw(15)<<"Miles
Driven"<<left<<setw(15)<<"Gallons
Used"<<left<<setw(10)<<"MPG"<<endl;
cout<<fixed<<setprecision(2);
// read till the end of file
while(!fin.eof())
{
fin>>carNumber>>milesDriven>>gallonsUsed;
// calculate mpg
if(gallonsUsed > 0)
mpg = ((double)milesDriven)/gallonsUsed;
else
mpg = 0;
// add mpg to avg_mpg, milesdriven to total_miles_driven,
gallonsUsed to total_gallons_used
avg_mpg += mpg;
total_miles_driven += milesDriven;
total_gallons_used += gallonsUsed;
numCars++; // increment number of cars
cout<<left<<setw(15)<<carNumber<<left<<setw(15)<<milesDriven<<left<<setw(15)<<gallonsUsed<<left<<setw(10)<<mpg<<endl;
}
fin.close(); // close the input file
// calculate average mpg
avg_mpg = avg_mpg/numCars;
// display summary data
cout<<endl<<"Total miles driven :
"<<total_miles_driven<<endl;
cout<<"Total gallons used:
"<<total_gallons_used<<endl;
cout<<"Average miles per gallon for all cars:
"<<avg_mpg<<endl;
}else
cout<<"Unable to open file :
car_readings.txt"<<endl;
return 0;
}
//end of program
Output:
Input file:
Values in a line must be separated by a single space or tab
Format of a line in the file : <car number><space><miles driven><space><gallons used>
Output:
*** USING C++ *** Create a file containing the following: car numbers, miles driven, and gallons...
Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankful of gasoline by recording miles driven and gallons used for each tankful. Develop a C++ program that inputs the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point.
A fuel-economy study was carried out for five models of cars. Each car was driven 100 miles, and then the model of the car and the number of gallons used were placed in a line of the file Mileage.txt. Table 5.11 shows the data for the entries of the file. Write a python program to display the models and their average miles per gallon in decreasing order with respect to mileage. See Fig. 5.51. The program should create a dictionary...
create an application in VISUAL BASIC that calculates a cars gas mileage. The formula for calculating the miles that a car can travel per gallon of gas is MPG = Miles/Gallon In the formula MPG is miles-per-gallon, miles is the number of miles that can be driven on a full tank of gas, and gallons is the number of gallons that the tank holds. The applications form should have TextBox controls that let the user enter the number of gallons...
python code DC Final Project Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. The following two lines are written in a File called FuelEffic.txt (you have to read these from the txt file) Miles per gallon: 20 Tank Size (in gallons): 25 Therefore, based on these...
I need trying to figure out how I can make create a code in Python with this exercise for I am having trouble doing so. Miles Per Gallon Calculator Write a GUI program that calculates a car's gas mileage. The program's window should have Entry widgets that let the user enter the number of gallons of gas the car holds, and the number of miles it can be driven on a full tank. When a Calculate MPG button is clicked,...
7. Miles-per-Gallon 6pts A car’s miles-per-gallon (MPG) can be calculated with the following formula: MPG = Miles driven / Gallons of gas used Design a program that asks the user for the number of miles driven and the gallons of gas used. It should calculate the car’s miles-per-gallon and display the result on the screen.Draw a flowchart in algorithmic language
Its an app) (Miles-per-Gallon Calculator App) In java with comments Place AnchorPane in design and set its attributes Place mileTextField, gallonsTextField and calcButton on the AnchorPane and set their attributes Place label that will show the calculated value and set its attributes Drivers often want to know the miles per gallon their cars get so they can estimate gasoline costs. Develop an app that allows the user to input the number of miles driven and the number of gallons used...
A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user's car and the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon. After doing this... Modify your...
Write a program to compute miles per gallon of a car, over a long trip. The car begins the trip with a full tank, and each fuel stop along the way fills the tank again. You will not do any math, arithmetic, or computations in the main() function. All output will be displayed from the main() function. Start the program by asking the user for the starting odometer reading. All odometer readings will be in whole miles only. The fuel...
Classwork 2-2: Input & Formatting A car's miles-per-gallon (MPG) can be calculated using the formula: MPG - miles driven/gallons of gas used Write a program that asks the user for the car's make, number of miles driven and the gallons of gas used. It should calculate the car's MPG and displays the result rounded to 2 decimal places. Example "Your Honda's MPG is 28.56" On Blackboard, submit a screenshot of successfully running and testing your code on HackerRank.com: www.hackerrank.com/csc124-chapter-2-classwork (2nd...