24 Apr 2017

Inlab 10 - CSE1002

 You don't need to tick the "Request desktop site" option in your browser anymore while using a mobile device, but if you don't then the code formatting of the code given as text for copying will be disturbed.

Important Note: (although the code will run now) Neither the question, nor the output format mentioned this anywhere, and it was determined only after reverse calculation from expected output, that before the average of mileages (not average mileage *) they expect the sum of mileages in the output as well. For the sake of those who were unable to do their Inlab report this issue to your faculty since those who have been affected do not deserve to bear the brunt of yet another case of carelessness from those who set these questions.

* = {  average mileage ((m1+ m2+ m3.......mi ) / (g1+g2+g3....gi) ) is not equal to  average of mileages ( (m1/g1 + m2/g2 + m3/g3 + ... mi/gi) / i  )}   [unrelated to above explained issue]

Code (choose one):

[now verified]


[now verified]

 The second one is shorter and is based on the fact that all the cases (even the hidden ones that recently became known to me.) have exactly 5 lines of data so there is no need to check for eof, should have been varied.


 If you know how to copy paste on skillrack then you can copy the code from below
#include<iostream>
#include<fstream>
using namespace std;
int main(){
    float m,g,gt=0,mt=0,at=0;   int a,i;  char s[30];
    cin>>s;     ifstream f(s);
    for(i=0;1;gt+=g,mt+=m,at+=(m/g),i++){
        if(!(f>>a>>m>>g))break;
        printf("%d\t%.2f\t%.2f\t%.2f\n",a,m,g,m/g);}
    printf("%.2f\n%.2f\n%.2f\n%.2f",mt,gt,at,at/i);
    f.close();
}



Input:

Name of the 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:

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

Processing:

mileage= distance / fuel consumed  (= miles/gallons)

Psuedocode: 

start
input s
open file s
let ds=0,fs=0
untill end of file:
         input n,d,f
         ds=ds+d,fs=fs+f
         output n,d,f,d/f
end of loop
output ds,fs,ds.fs
stop
   

No comments:

Post a Comment