Skillrack IL 10

Solutions for Skillrack CSE1002 Inlab 10

Total time : 20 mins

Challenges : 1

Question

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

Note : Test cases have not been verified

#include< iostream >
#include< fstream >
#include< iomanip >
using namespace std;
int main()
{
	char file_name[20];
	cin>>file_name;
	ifstream fin;
	fin.open(file_name,ios::in);
	int no;
	float miles,gallons,total_miles=0,total_gallons=0,total_avg=0;
	for(int i=0;i < 5;i++) 
	{ 
		fin>>no>>miles>>gallons;
		total_miles+=miles;
		total_gallons+=gallons;
		total_avg+=miles/gallons;
		cout<<no<<"\t";
		cout<<fixed<<setprecision(2)<<miles<<"\t"<<gallons<<"\t"<<miles/gallons<<"\n";
	}
	cout<<total_miles<<"\n"<<total_gallons<<"\n"<<total_avg<<"\n"<<total_avg/5;
	fin.close();
	return(0);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s