Jump to content

Archive:Standard deviation (source code)

From IdeaWazaWiki
Revision as of 11:51, 26 June 2007 by wikademia>Mickraus (Standard Devaition moved to Standard Deviation: 'Deviation' was mis-spelt 'Devaition')

<math>Insert formula here</math><source lang="c">#include<stdio.h>

  1. include<math.h>
  1. 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;

} </source>