Skip to content

Instantly share code, notes, and snippets.

@OlafSamael
Created May 7, 2015 01:25
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 OlafSamael/e15d5e744c66d278cbd7 to your computer and use it in GitHub Desktop.
Save OlafSamael/e15d5e744c66d278cbd7 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <cmath>
using namespace std;
void readNumbersFromFile(string filename){
string line1;
float sum=0.0, value=0.0, sdev=0.0;
int counter=0;
ifstream myfile(filename.c_str());
myfile.is_open();
while(getline(myfile,line1)){
value=atoi(line1.c_str());
sum=sum+value;
counter++;
sdev=sqrt((pow(value-(sum/counter),2)/value));
}
cout << "Total: " << sum << endl;
cout << "Number of values: " << counter << endl;
cout << "Average: " << sum/counter << endl;
cout << "Standard deviation: " << sdev << endl;
}
int main(){
readNumbersFromFile("numbers.txt");
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment