0

Someone Please help with this. I want to make a class Statistics and read from an external file and find the mean and standard deviation and below is what i have so far. any suggestions?

#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);
             ~Statistics();
             Statistics Accumulate(double a);
             Statistics mean(Statistics& m);
             Statistics sigma(Statistics& s);
             void Print() const;
             
};
                                            

int main(){
    
    const int n=200;
	double sum = 0.0;
	double sum2 = 0.0;
	double data;

	ifstream in("file.txt");
	for(int i=0; i < n ; i++){
	in >> data;
	sum+=data;
	sum2+=data*data;		
	}
	in.close();
	
	double mean   = sum/n;
	double sigma = sqrt( (sum2 - sum*mean)/(n-1)); 
    Statistics a(data, 200);
    a.Print();

    system("PAUSE");
    return EXIT_SUCCESS;
}

Statistics::Statistics(double _x, int n ):x(_x){}

Statistics::~Statistics(){
cout<<"Statistics destructor called"<<endl;
}
Statistics::Statistics mean(Statistics& m){
     double sum = 0.0;
     x.m+=sum;
     return sum;
     }
Statistics::Accumulate(double a) const{
     x.a=(sum2-sum*mean)/(n-1);
     return a;
     }
Statistics::Statistics sigma(double s){
     x.s= sqrt( (sum2 - sum*mean)/(n-1));
     return s;

void Statistics::Print(){
	
	cout << " N     = " << n <<endl; 
    cout << " Mean  = " << m <<endl;
	cout << " Sigma = " << s<<endl ;
}

Edited by __avd: Added [code] tags.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by daviddoria
0

Please use code tags when posting code.

What is the problem? Please provide a sample input, the current output, and the expected output. I also suggest decoupling your file input from your algorithm. Hard code some values and make sure the statistics code works first before introducing another challenge of the file input.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.