Thread: How can I make this program find standard deviation?

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    7

    How can I make this program find standard deviation?

    Hi. I am required to write a program that calculates the mean and standard deviation. So far, I wrote the program so that it finds the mean. Now how can I make it add standard deviation? I've only learned up to loops, so please do not go too advanced.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    
    void
    msg1(void)
    {
         printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\… );
         printf("\n EE 150 \n" );
         printf("\n This program calculates the mean of a given");
         printf("\n data set. \n\n\n\n\n\n\n\n" );
    
    }
    
    int
    main(void)
    {
    
    
    double sum = 0, data, mean;
    int n = 0;
    char unit[20], ans='Y';
     
    msg1();
    
    printf("\n\nPlease enter the unit of the data set: >");
    scanf("%s", &unit);
    
    while (ans=='y'||ans=='Y')
    {
          printf("\n\nEnter the data point (in %s): >", unit);
          scanf("%lf", &data);
    
          n = ++n;
          sum += data;
    
          printf("\n\nDo you want to add another data point? Y for yes. N for no.\n");
          scanf("%c", &ans);
          scanf("%c", &ans);
    
    }
    
    if (ans=='n'||ans=='N')
    {
          mean = sum / n;
          printf("\n\nThe mean of the data set in %s is %g.", unit,mean);
    }
    
    return 0;
    }
    




    I've tried putting something like:
    calculation += (data - mean)(data-mean)
    stdev = sqrt (calculation/ (n-1))
    but the answer always comes out wrong.

    Thanks in advance!
    Last edited by jsp418; 09-12-2012 at 10:25 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A few things:

    1. C needs math to be explicit. It can't infer multiplication, you must use a * explicitly: calculation += (data - mean) * (data - mean). I don't know if that's a typo/omission or you actually tried coding it that way.
    2. You won't know the mean until you've read in all the data points, summed them and divided by n.
    3. You can't determine 'calculation' (I think the technical term for that is variance) without the mean. This implies you must process the data twice (once to calculate the mean, once to calculate the variance). You will need at least 2 loops.
    4. The above implies you must store all the numbers the user enters. I suggest an array.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I get one error and several warnings when I run your code:

    Code:
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\… );
    You are missing the " at the end of the string literal.

    Code:
    char unit[20], ans='Y';
    ... 
    scanf("%s", &unit);
    scanf() expects a pointer as its second argument. "unit" is already a pointer to the first element of the array, thus no need for the &-operator.
    Furthermore I suggest using
    Code:
    scanf("%19s", unit);
    to avoid buffer overflows (unit is capable of storing 20 characters, %19s reads at most 19 character, thus there is still room for the terminating "\0".)

    Code:
    n = ++n;
    ++n increments n "in place". No need to assign n to itself.

    Code:
    scanf("%c", &ans);
    scanf("%c", &ans);
    If you want to ignore any whitespace (including the newline-character) before reading the character IMHO it's simpler and more understandable to use
    Code:
    scanf(" %c", &ans);
    (notice the space before %c!).

    Bye, Andreas

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    7
    Hi! Thank you very much for the replies! I have not learned arrays yet, but I found out how to calculate stdev without arrays using a different stdev formula. I would appreciate if you could test it out and make sure everything is okay before I turn it in.

    Disregard the ugly skeleton lol.

    Code:
    /***************************************************************************//*                                                                         */
    /*                       LEEWARD COMMUNITY COLLEGE                         */
    /*                        PHYSICS AND ENGINEERING                          */
    /*                                                                         */
    /*                                                                         */
    /*  NAME:   J. Preza                                                    */
    /*                                                                         */
    /*  DATE:   09/14/12                                                       */
    /*                                                                          */
    /*  FILE:   PROG2_STDEV_PREZA.C                                            */
    /*                                                                            */
    /*  FUNCTION:  To compute the standard deviation or sample standard        */
    /*             deviation of a data set                                     */
    /*                                                                         */
    /*  INPUTS:    unit of data, data                                             */
    /*                                                                         */
    /*  OUTPUTS:   Mean(in proper unit)                                          */
    /*                                                                            */
    /***************************************************************************/
    
    
    /***********************  PREPROCESSOR DIRECTIVES  *************************/
    
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<string.h>
    
    
    
    
    
    
    /**************************  MESSAGE FUNCTION  *****************************/
    
    
    void
    msg1(void)
    {
        printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" );
        printf("\n                      EE 150  \n"                 );
        printf("\n      This program calculates the standard       ");
        printf("\n         deviation of a given data set \n\n\n\n\n\n\n\n" );
    
    
    }
    
    
                /* SOMETIMES YOU WILL PUT OTHER FUNCTIONS HERE */
    
    
    /***************************  MAIN FUNCTION  *******************************/
    
    
    int
    main(void)
    {
    
    
    /*********************  DECLARATION OF VARIABLES  **************************/
    
    
    double sum = 0, calculation = 0, data, square, stdev;
    int n = 0;
    char unit[20], ans='Y';
    
    
    /*******************THE EXECUTABLE PART OF THE PROGRAM  ********************/
    
    
    msg1();
    
    
        printf("\n\nPlease enter the unit of the data set: >");
        scanf("%s", &unit);
        
        while (ans=='y'||ans=='Y')
        {
              printf("\n\nEnter the data point (in %s): >", unit);
              scanf("%lf", &data);
              
              n = ++n;
              calculation += data*data;
              sum+=data;
              
              printf("\n\nDo you want to add another data point? Y for yes. N for no.\n");
              scanf("%c", &ans);
              scanf("%c", &ans);
              
        }
    
    
        if (ans=='n'||ans=='N')
        {
              square = sum * sum;
              stdev = sqrt(((n*calculation)-square)/(n*(n-1)));
              printf("\n\nThe Standard Deviation of the data set in %s is %g.", unit,stdev);
              }
        
     
        
        
    
    
    
    
    return 0;
    }
    
    
    /***************************************************************************/
    Thanks!!!!!!!!!!!!!!!

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by anduril462 View Post
    You can't determine 'calculation' (I think the technical term for that is variance) without the mean. This implies you must process the data twice (once to calculate the mean, once to calculate the variance). You will need at least 2 loops.
    That's not true. There are techniques to calculate variance using one pass. Admittedly those processes compute quantities that are related to the mean, but it is not necessary to use one pass to calculate the mean, and another to compute the variance. Those techniques also do not require the values the user inputs to be stored away either.

    Mathematics is a wonderful discipline
    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, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by jsp418 View Post
    Code:
    /* ... */
        scanf("%s", &unit);
    /* ... */
              n = ++n;
    /* ... */
    NO!
    You've been told before how to correct these two errors.
    Read the thread again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2011, 05:11 PM
  2. Replies: 1
    Last Post: 03-20-2011, 02:47 AM
  3. standard deviation program
    By vietkhang123 in forum C++ Programming
    Replies: 2
    Last Post: 09-29-2010, 04:01 PM
  4. Standard Deviation in C++
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2001, 11:09 AM