Jump to content

Archive:Standard deviation (source code)

From IdeaWazaWiki
Revision as of 15:52, 30 January 2007 by wikademia>Mickraus (ANSI/ISO compliant; reformatted)

<math>Insert formula here</math>

#include<stdio.h>
#include<math.h>

#define   NUM_MAX   10

int main(void)
{
    int n, i, d, a[NUM_MAX];
    float x = 0, sd, s = 0, m, sum = 0;

    printf("enter how many [max %d]: ", NUM_MAX);
    scanf("%d", &n);

    if ( n > NUM_MAX )
    {
        printf("n > %d\n", NUM_MAX);
        return 0;
    }

    for(i = 0; i < n; i++)
    {
        printf("enter element %d of %d: ", i + 1, n);
        scanf("%d", &a[i]);
    }

    for(i = sum = 0; i < n; i++)
    {
        x = x + a[i];
        m = x / n;
        d = a[i] - m;
        sum += d * d;
    }

    s = sum / n;
    sd = sqrt(s);
    printf("the number of terms = %d\n", n);
    printf("their mean is m     = %f\n", m);
    printf("standard deviation  = %f\n", sd);

    /* indicate success */
    return 0;
}