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 is 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 for each car.
PSEUDOCODE:
START
READ s;
READ FILE with name s in f
while(!f.eof())
f>>car>>mile>>gal;
SET avg=mile/gal;
SET total+=mile;
SET totalg+=gal;
SET tavg+=avg;
PRINT car,mile,gal,avg
end while
PRINT total,totalg,tavg,tavg/n;
STOP
CODE:
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
main()
{int car,n=0;
char s[100];
cin>>s;
float mile,gal,total=0,totalg=0,avg,tavg=0;
fstream f;
f.open(s,ios::in);
while(!f.eof())
{
f>>car>>mile>>gal;
avg=mile/gal;
total+=mile;
totalg+=gal;
tavg+=avg;
cout<<fixed<<setprecision(2)<<car<<" "<<mile<<" "<<gal<<" "<<avg<<endl;
n++;
}
cout<<fixed<<setprecision(2)<<total<<endl<<totalg<<endl<<tavg<<endl<<tavg/n;
}
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 for each car.
PSEUDOCODE:
START
READ s;
READ FILE with name s in f
while(!f.eof())
f>>car>>mile>>gal;
SET avg=mile/gal;
SET total+=mile;
SET totalg+=gal;
SET tavg+=avg;
PRINT car,mile,gal,avg
end while
PRINT total,totalg,tavg,tavg/n;
STOP
CODE:
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
main()
{int car,n=0;
char s[100];
cin>>s;
float mile,gal,total=0,totalg=0,avg,tavg=0;
fstream f;
f.open(s,ios::in);
while(!f.eof())
{
f>>car>>mile>>gal;
avg=mile/gal;
total+=mile;
totalg+=gal;
tavg+=avg;
cout<<fixed<<setprecision(2)<<car<<" "<<mile<<" "<<gal<<" "<<avg<<endl;
n++;
}
cout<<fixed<<setprecision(2)<<total<<endl<<totalg<<endl<<tavg<<endl<<tavg/n;
}
No comments:
Post a Comment