Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Calculate Standard Deviation!!!

rephlexrephlex Posts: 1Member

I am having trouble with calculations of standard deviation & variance! The calculations aren't coming out correctly! Can someone please help me

`

include

include

include

include

include

using namespace std;

int main () {

cout << "\nThis program will produce statistics (Mean, Standard Deviation, "
"Maximum and Minimum values of the list) for a list of integer values."
"The user will provide the names of input and output files.\n" ; // << endl;

cout <<"\nEnter name and location for input file:   " ;
string file_input ;
getline(cin, file_input);
ifstream fin( file_input.c_str() ) ;

if(fin.fail())
{
    cout << "Bad file name or location.\n" ;
    return 0 ;
}

cout <<"Enter name and location for output file:   ";
string file_output ;
getline(cin, file_output);
ofstream fout( file_output.c_str() );

cout << "\nReading values for first time. . .\n" ;
fout << "\nReading values for first time. . .\n" ;

int num;
int count = 0 ;
int total = 0 ;
int Min = 100;
int Max  = 0;
double sumsq = 0, variance, stdv;

fin >> num;
while( !fin.eof() )
{
    cout << num << ' ' ;
    fout << num << ' ' ;
    if( ++count%10 == 0 )

    {
        cout << '\n' ;
        fout << '\n' ;
    }

    {
    total += num ;
    if( num > Max ) Max = num ;
    if( num < Min ) Min = num ;
    fin >>num;

    }
}

if(count > 0)
{
    const double avg = double(total) / count ;
    sumsq+= (num-avg)*(num-avg);


    variance = sumsq/count;
    stdv=sqrt(variance);


    cout << "\n\nNumber of values read:  " << count;
    cout << "\nMean of the values:  " << avg << '\n' ;
    cout << "\nStandard deviation using method 1:   " << sumsq << " " << variance << " " << stdv;
    cout << "\nGreatest value:  " << Max << '\n' ;
    cout << "Least value:  " << Min << '\n' ;

    fout << "\n\nNumber of values read:  " << count;
    fout << "\n\nMean of the values :  " << avg << '\n' ;
    fout << "\nStandard deviation using method 1:   ";
    fout << "Greatest value :  " << Max << '\n' ;
    fout << "Least value :  " << Min << '\n' ;
}

return 0;

}`

Answers

  • HK_MP5KPDWHK_MP5KPDW Posts: 767Member ✭✭✭

    Well, your sumsq calculation doesn't look right. You are only doing the calculation for the last value read from the file. That part needs to be in a loop.

    If you are concerned about checking the validity of your calculations, then you should be able to look at (print) the values at your critical points and verify that they look good.

    There is a slightly easier way to do your first read-loop without resorting to an end-of-file check which are generally not done correctly by newbs but you do seem to have addressed correctly. You can get rid of the priming read and the end of loop read and just replace the eof test directly with the read operation as follows:

    while ( fin >> num )
    {
       ... rest of your loop code goes here (minus the extra read at the end)
    }
    
Sign In or Register to comment.