Jump to content

Archive:Standard deviation (source code): Difference between revisions

From IdeaWazaWiki
wikademia>Mickraus
ANSI/ISO compliant; reformatted
wikademia>MichaelFrey
mNo edit summary
Line 1: Line 1:
<math>Insert formula here</math><pre>#include<stdio.h>
<math>Insert formula here</math><source lang="c">#include<stdio.h>
#include<math.h>
#include<math.h>


Line 41: Line 41:
     return 0;
     return 0;
}
}
</pre>
</source>


[[Category:C source code examples]]
[[Category:C source code examples]]

Revision as of 06:48, 17 May 2007

<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>