Standard Deviaiton issue

This is a discussion on Standard Deviaiton issue within the C++ Programming forums, part of the General Programming Boards category; I have been working on this for a few days now and I still can't wrap my head around arrays ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    30

    Standard Deviaiton issue

    I have been working on this for a few days now and I still can't wrap my head around arrays and filling in arrays to compute the Standard Deviation of an input file.

    This is what I have:

    Code:
    #include <fstream>#include <iostream>
    #include <cmath>
    
    
    using namespace std;
    
    
    double get_standard_dev( double Num [], int x );
    
    
    const int size= 100;
    
    
    int main ()
    {
        int x, size;
        double Num[x];
        ifstream in_stream;
    
    
        cout << "We are going to find the standard deviation for the file stddev.dat" << endl;
    
    
        // Opens the input file.
        in_stream.open ( "stddev.dat" );
    
    
        // If the file fails to open it will close the program and output the statement.
        if ( in_stream.fail( ) )
        {
            cout << "Please check if the file is saved properly. It could not open." << endl;
        }
    
    
        cout << get_standard_dev( Num, size) << endl;
    
    
        return 0;
    }
    
    
    double get_standard_dev( double Num [], int x )
    {
        double average, temp, mean;
        double sum = 0;
        int i;
    
    
        for( i = 0; i < x;i++ )
            {
            sum += Num[i];
            }
    
    
        average = sum / x;
    
    
        for( i = 0; i < x;i++ )
    
    
        {
        temp = Num[i] - average;
        sum += temp * temp;
        }
    
    
        mean = sum / x;
    
    
    return sqrt(mean);
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    You are computing the population standard deviation. You probably want to compute the sample standard deviation. The calculations are similar, but give slightly different results (the difference in method being related to Bessel's correction being needed when computing the sample standard deviation).

    If you don't understand what I'm saying, google is your friend.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    I don't quite understand where I am messing up in this program. It is outputting 1 and I can't figure out why. Can anyone point me in the right direction?
    Code:
    1. #include <fstream>
    2. #include <iostream>
    3. #include <cmath>
    4. using namespace std;
    5. double get_standard_dev( double Num [], int x );
    6. const int size= 100;
    7. int main ()
    8. {
    9. int next;
    10. int count = 0;
    11. double Num[size];
    12. for(int i =0; i < size; i++)
    13. {
    14. Num[i]=0;
    15. }
    16. ifstream in_stream;
    17. cout << "We are going to find the standard deviation for the file stddev.dat" << endl;
    18. // Opens the input file.
    19. in_stream.open ( "stddev.dat" );
    20. // If the file fails to open it will close the program and output the statement.
    21. if ( in_stream.fail( ) )
    22. {
    23. cout << "Please check if the file is saved properly. It could not open." << endl;
    24. return 0; // you want to end the program here. You can't calculate correctly if the file does not open
    25. }
    26. while(in_stream >> next)
    27. {
    28. Num[count]=next;
    29. count++;
    30. }
    31. get_standard_dev ( Num , count );
    32. // now read from in_stream into the Num[] array. Fill in x with the number of numbers in the file.
    33. // now close the file
    34. in_stream.close();
    35. return 0;
    36. }
    37. double get_standard_dev( double Num [], int x )
    38. {
    39. double average, s;
    40. double sum = 0;
    41. int i;
    42. for( i = 0; i < x;i++ )
    43. {
    44. sum = sum + Num[i];
    45. }
    46. average = sum / x;
    47. sum = 0;
    48. for( i = 0; i < x; i++ )
    49. {
    50. s =pow((Num[i]-average),2);
    51. //cout << s<<" ";
    52. sum = sum + s;
    53. }
    54. cout <<(sqrt (sum / x));
    55. return 0;
    56. }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-06-2010, 12:52 AM
  2. From the standard
    By Unregistered in forum C# Programming
    Replies: 23
    Last Post: 02-20-2002, 03:03 PM
  3. standard language, standard compiler?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-03-2001, 05:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21