Thread: Trying to calculate MEAN & STANDARD DEVIATION!!!

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    5

    Trying to calculate MEAN & STANDARD DEVIATION!!!

    I've been trying to calculate the Second standard deviation but the average in the second loop isn't calculating correctly which is causing the standard deviation (method 2) to not calculate correctly. I can't find anything wrong. please help

    Code:
    #include <iostream>#include <iomanip>
    #include <string>
    #include <fstream>
    #include <cmath>
    
    
    usingnamespacestd;
    
    
    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" ;
    
    cout <<"\nEnter the name and location of the 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" ;
            exit(0);
        }
    
    cout <<"Enter the name and location of the output file:   ";
        string file_output ;
        getline(cin, file_output);
        ofstream fout( file_output.c_str() );
    
    cout << "\nReading values first time. . .\n" ;
        fout << "\nReading values first time. . .\n" ;
    
        int num;
        int count = 0 ;
        double total = 0 ;
        int Min = 100;
        int Max  = 0;
        double totalSq=0;
        double avg=0;
        double stdDev=0;
    
        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 ;
                totalSq+= num*num;
                fin >> num;
            }
        }
    
         avg = total / count ;
    
        if (count>0)
        {
    
            stdDev = sqrt((totalSq/count) - pow(avg,2));
    
            cout << fixed << setprecision(3);
    cout << "\n\nNumber of values read:  " << count << endl;
            cout << setw(40) << right << "Mean of the values:  " << avg << endl;
    cout << setw(40) << right << "Standard deviation using method 1:  " << stdDev << endl;
            cout << setw(40) << right << "Greatest value:  " << Max << '\n' ;
            cout << setw(40) << right << "Least value:  " << Min << '\n' ;
    
            fout << fixed << setprecision(3);
            fout << "\n\nNumber of values read:  " << count << endl;
            fout << setw(40) << right << "Mean of the values:  " << avg << endl;
            fout << setw(40) << right << "Standard deviation using method 1:  " << stdDev << endl;
            fout << setw(40) << right << "Greatest value:  " << Max << '\n' ;
            fout << setw(40) << right << "Least value:  " << Min << '\n' ;
            fin.close();
        }
    
    
        fin.open(file_input.c_str());
    
    cout << "\nReading values second time. . ." << endl;
        fout << "\nReading values second time. . ." << endl;
        stdDev=0, totalSq=0;
        total=0;
        avg = total/count;
    
        count = -1;
        fin >> num;
        while(!fin.eof())
        {
    
            if(++count%3 == 0)
            {
                cout << setw(3) << num;
                fout << setw(3) << num ;
                total += num;
                totalSq+= (num-avg) * (num-avg);
            }
            fin >> num;
    
        }
    
        count = (int)(count/3) + 1;
        if (count > 0)
        {
            stdDev = sqrt(totalSq/count);
    
    cout << "\n\nNumber of values read:  " << count << endl;
            fout << "\n\nNumber of values read:  " << count << endl;
    cout << setw(40) << right << "\n\tStandard Deviation using method 2:  "<< stdDev <<endl;
            fout << setw(40) << right << "\n\tStandard Deviation using method 2:  "<< stdDev <<endl;
    
        }
        return 0;
    
    }


  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why are you computing avg before you've even read the numbers in? It will always be zero.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    In the second loop?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rephlex View Post
    In the second loop?
    That's the one I was looking at, yes.

    Code:
        total=0;
        avg = total/count;

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    That is correct but if I put it at the end of the loop then it won't work either because avg is included in the calculation for totalSq and I tried that but calculations are still incorrect

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rephlex View Post
    That is correct but if I put it at the end of the loop then it won't work either because avg is included in the calculation for totalSq and I tried that but calculations are still incorrect
    Calculating standard deviations using the (x-mu)^2/(n-1) formula is inherently a two-pass algorithm, one pass to find mu, and then another to calculate and add the (x-mu)^2 bits. (This is why no one uses it.)

    Depending on the parameters of your assignment, you might be allowed to re-use avg from the first pass (although since you seem to be only looking at every third number you probably shouldn't). That means you must pass through the data twice in part two (ie three times total); the first time accumulating the average, and then the second time accumulating the (x-mu)^2 terms.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should past your code as text

    also
    Code:
      fin >> num;
     while(!fin.eof())
        {
    ...
     fin >> num;
     }
    should be
    Code:
    while(fin >> num)
    {
    ...
    }
    In this case read failure due to error and not EOF will be also handled instead of causing infinite loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    Quote Originally Posted by tabstop View Post
    Calculating standard deviations using the (x-mu)^2/(n-1) formula is inherently a two-pass algorithm, one pass to find mu, and then another to calculate and add the (x-mu)^2 bits. (This is why no one uses it.)

    Depending on the parameters of your assignment, you might be allowed to re-use avg from the first pass (although since you seem to be only looking at every third number you probably shouldn't). That means you must pass through the data twice in part two (ie three times total); the first time accumulating the average, and then the second time accumulating the (x-mu)^2 terms.
    I am supposed to do that, this is sample output of the program assignment:

    PHP Code:
    This program will produce statistics (MeanStandard DeviationMaximum                                                and Minimum values of the list) for list of integer values.  The user
    will provide the names of input 
    and output files.
                        
    Enter the name of the input fileSTATNUMS.DATEnter the name of the output fileSTATNUMS.out
                        Reading values first time
    . . .
    10 11 8 9 6 21 54 89 2 15
    34 5  8
                        Number of values read
    13
                        Mean of the values 
    xxx.xxx
                              Standard deviation using method 1
    xxx.xxx
                            Greatest value 
    xxx.xxx
                               Least value 
    xxx.xxx
                        Reading values second time
    10 9 54 15 8
                        Number of values read 
    5
                              Standard deviation using method 2
    xxx.xxx                             End of program

  9. #9
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    I have noticed the problem. The average for the second loop works fine but I've noticed there's an issue with the value of totalSq in the second loop which is causing the standard deviation to become incorrect too. I tried everything to get the correct value of it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Standard deviation
    By Dontgiveup in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2009, 01:46 PM
  2. How to calculate standard deviation
    By hobilla in forum C Programming
    Replies: 13
    Last Post: 03-14-2009, 06:41 AM
  3. help with standard deviation
    By belkins in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 11:04 PM
  4. standard deviation in need help!!!
    By voltare in forum C Programming
    Replies: 2
    Last Post: 03-01-2004, 06:46 AM