Finding standard deviation in c++ with file -


write program takes input file of numbers of type double. program outputs screen standard deviation of numbers in file. file contains nothing numbers of type double separated blanks and/or line breaks. standard deviation of list of numbers x1, x2, x3, , forth defined square root of:

((x1 – a)2 + (x2 – a)2 + (x3 – a)2 + ...) / (n - 1)

where number average of numbers x1, x2, x3, , forth , number n count of how many numbers there are.

your program should take file name input user.

i have created file in computer. when finish code , try compiled it, cannot open file in compiler. don't know problem going on. can give me advices?

#include <stdio.h> #include <iostream> #include <cmath> #include <fstream> #include <string>  using namespace std;   int main() {     double next, avg,var,stddev, sum=0, sumsq=0;     int count=0;      cout << "enter filename:" << endl;     std::string filename;     cin >> filename;      ifstream in_stream;  in_stream.open(filename.c_str());      while(in_stream >> next)    {        sum+=next;        sumsq+=(next*next);        ++count;           }     avg=sum/count;    var=(count*sumsq-sum*sum) / (count*(count-1));    stddev=sqrt(var);     in_stream.close();  cout.setf(ios::showpoint); cout.setf(ios::fixed); cout.precision(3); cout << "the standard deviation " << stddev << endl;    return 0; }     [1]: http://i.stack.imgur.com/lujzx.png     [2]: http://i.stack.imgur.com/ofzgr.png    [3]: http://i.stack.imgur.com/xtfsl.png 

this need

#include <iostream> #include <cmath> #include <fstream> #include <string>  using namespace std;  int main() {   double next, avg, var, stddev, sum=0, sumsq=0;   int count=0;    cout << "enter filename: ";   std::string filename;   cin >> filename;    ifstream in_stream;   in_stream.open(filename.c_str());   if(in_stream.fail()) {     cout << "could not open file " << endl;     exit(0);   }    while(in_stream >> next) {     sum += next;     // sum square of "next"     sumsq += (next * next);     ++count;   }    avg = sum/count;   var = (count*sumsq - sum*sum)/(count*(count - 1));   stddev = sqrt(var);    cout << "the mean is: " << avg << endl;   cout << "the unbiased sample variance is: " << var << endl;   cout << "the unbiased sample standard deviation is: " << stddev << endl;    return 0; } 

compile , execute executable same directory nums.txt file. after program prompts enter filename:, enter nums.txt. example, screen grab of session in terminal is:

enter image description here

i used g++ can use clang++ compile.

hope helps.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -