Start New Discussion within our Software Development Community

I am trying to write a program that reads an external file and does a few statistics but it does not return the correct values for the mean and standard deviation. Can someone help point point out what I did wrong?? Below is my codes:-

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;
class Statistics{
      double x;
      int n;
      double sum;
      double sum2;

      public:
             Statistics(double _x, int _n);
             void Accumulate(double data);//(This should a data item to the accumulateds tatistics)
             ~Statistics();
             double mean()const;
             double sigma()const;
             void Print() const;

};


int main(){
    const int n=200;
    double sum=0.0,sum2=0.0,data;
    ifstream in("file");
    for(int i=0; i < n ; i++){
    in >> data;
    sum+=data;
    sum2+=data*data;    
    }
    in.close();

    Statistics a(data, 200);
    a.Print(); 

    system("PAUSE");
    return 0;
}

Statistics::Statistics(double _x, int _n):x(_x),n(_n){}
void Statistics::Accumulate(double data){
     sum+=data;
     sum2+=data*data;
     }

Statistics::~Statistics(){
cout<<"Statistics destructor called"<<endl;
     }

double Statistics::mean()const{
     return sum/n;
     }

double Statistics::sigma()const{   
     return sqrt( (sum2 - (sum*mean()))/(n-1));
     }

void Statistics::Print()const{

     cout.precision(8);
     cout << " N     = " << n <<endl; 
     cout << " Mean  = " << mean()<<endl;
     cout << " Sigma = " << sigma()<<endl ;
}

Please use code tags. (Press the code button before you paste).
We need to know more:
What are each of the calculations? (I know we can see this in the code, but it's just to check the logic).
What do you input?
What is the expected output?
What is the actual output?

Thanks :)

This article has been dead for over six months. Start a new discussion instead.