INLAB-10 (tested-100% working)
A workshop has a variety of cars. The details of the car such as car number, miles driven, and gallons of gas used in each car, are stored in a file.
Car Number Miles Driven Gallons used
54 250 19
62 525 38
71 123 6
85 1,322 86
97 235 14
Write a C++ program that reads the data in the file created and displays the car number, miles driven, gallons used, and the miles per gallon (mileage) for each car.
Input Format:
Name of the file
Contents of the input file.
car number, miles driven and gallons (separated by a tab) of car1 in line1
car number, miles driven and gallons (separated by a tab) of car2 in line2
….
car number, miles driven and gallons (separated by a tab) of car-n in line-n
Output Format:
car number, miles driven, gallons and mileage of car1 (separated by tab)
car number, miles driven, gallons and mileage of car2 (separated by tab)
…
car number, miles driven, gallons and mileage of car-n (separated by tab)
Total miles driven (by all the cars)
Total gallons used (by all the cars)
average miles per gallon for all the cars
SOLUTION-
#include<iostream>#include<fstream>
#include<iomanip>
using namespace std;
int main()
{
char name[20];
cin>>name;
ifstream fin;
fin.open(name,ios::in);
int no,n;
float m,g=0,tm=0,tg=0,avg=0;
for(int i=0;i<5;i++)
{
fin>>no>>m>>g;
tm=tm+m;
tg=tg+g;
avg=avg+(m/g);
cout<<no<<"\t";
cout<<fixed<<setprecision(2)<<m<<"\t"<<g<<"\t"<<m/g<<"\n";
}
cout<<tm<<"\n"<<tg<<"\n"<<avg<<"\n"<<avg/5;
fin.close();
return(0);
}
No comments:
Post a Comment