Skip to content

Instantly share code, notes, and snippets.

@samir96
Created May 6, 2015 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samir96/18d6e5f2b67efbe2751c to your computer and use it in GitHub Desktop.
Save samir96/18d6e5f2b67efbe2751c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
float Average(float sum, int lines){
float average;
average=sum/(lines/2);
return average;
}
int main(){
ifstream file ("93cars.dat.txt");
string line;
int line_number=0;
float cityMPG=0, hwyMPG=0, midprice=0;
if (file.is_open()){
while (getline(file, line)){
line_number++;
if (line_number%2==1){
cityMPG += stof(line.substr(52,2));
hwyMPG += stof(line.substr(55,2));
midprice += stof(line.substr(42,4));
}
}
}
cout << "The average gas mileage in city is "<< Average(cityMPG,line_number) << endl;
cout << "The average gas mileage on highway is "<< Average(hwyMPG,line_number) << endl;
cout << "The average midrange price of the vehicles in the set is "<< Average(midprice,line_number) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment