Trying to calculate average & standard deviation!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Trying to calculate average & standard deviation!

  1. #1
    Join Date
    Oct 2013
    Posts
    5

    Trying to calculate average & 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>
    
    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" ;
        
        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
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: Trying to calculate average & standard deviation!

    Quote Originally Posted by rephlex View Post
    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
    What have you done to debug your program? Are the values read from file correctly? Is the sum and sum of squared calculated correctly?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Oct 2013
    Posts
    5

    Re: Trying to calculate average & standard deviation!

    Quote Originally Posted by D_Drmmr View Post
    What have you done to debug your program? Are the values read from file correctly? Is the sum and sum of squared calculated correctly?
    Yes everything is correct except the last loop when calculating standard deviation, it wasn't coming out correctly. Now I have noticed that totalSq in the second loop is the problem but I don't know what to do with it or why it is coming out like that

    Sample output
    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.DAT 
    Enter the name of the output file
    STATNUMS.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 

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Trying to calculate average & standard deviation!

    Now I have noticed that totalSq in the second loop is the problem but I don't know what to do with it or why it is coming out like that
    You need to be able to debug your programs to find problems like this. The ability to debug programs is an essential skill.

    Code:
    total=0;
    avg = total/count;
    As total is 0, avg will be always be 0!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Trying to calculate average & standard deviation!

    Quote Originally Posted by rephlex View Post
    Yes everything is correct except the last loop when calculating standard deviation, it wasn't coming out correctly. Now I have noticed that totalSq in the second loop is the problem but I don't know what to do with it or why it is coming out like that
    As others have mentioned -- use the debugger that comes with the compiler. Step through your program using the debugger and watch the values of the variables at each step.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center