Jump to content

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

From IdeaWazaWiki
wikademia>Mickraus
m Standard Devaition moved to Standard Deviation: 'Deviation' was mis-spelt 'Devaition'
wikademia>Mickraus
m Incorrect preprocessor lines fixed. Implemented <source> element. Other changes to make code more robust
Line 1: Line 1:
<math>Insert formula here</math><source lang="c">#include<stdio.h>
<math>Insert formula here</math>
#include<math.h>
<source lang="c">#include <math.h>
 
#include <stdio.h>
#include <stdlib.h>
#define  NUM_MAX  10
#define  NUM_MAX  10
 
/* main: main processing of standard deviation */
/* NOTE: on UNIX you may have to compile with the -lm option
* at the *end* of the command line
*/
int main(void)
int main(void)
{
{
     int n, i, d, a[NUM_MAX];
     int n, i, d, a[NUM_MAX];
     float x = 0, sd, s = 0, m, sum = 0;
     double x = 0, sd, s = 0, m, sum = 0;
 
     printf("enter how many [max %d]: ", NUM_MAX);
     printf("enter how many [max %d]: ", NUM_MAX);
     scanf("%d", &n);
     scanf("%d", &n);
 
     if ( n > NUM_MAX )
     if ( n > NUM_MAX )
     {
     {
         printf("n > %d\n", NUM_MAX);
         printf("You entered a value greater than %d\n", NUM_MAX);
         return 0;
         exit(EXIT_FAILURE);
     }
     }
 
     for(i = 0; i < n; i++)
     for (i = 0; i < n; i++)
     {
     {
         printf("enter element %d of %d: ", i + 1, n);
         printf("enter integer element %d of %d: ", i + 1, n);
         scanf("%d", &a[i]);
         scanf("%d", &a[i]);
     }
     }
 
     for(i = sum = 0; i < n; i++)
     for (i = sum = 0; i < n; i++)
     {
     {
         x = x + a[i];
         x = x + a[i];
Line 31: Line 37:
         sum += d * d;
         sum += d * d;
     }
     }
 
     s = sum / n;
     s = sum / n;
     sd = sqrt(s);
     sd = sqrt(s);
Line 37: Line 43:
     printf("their mean is m    = %f\n", m);
     printf("their mean is m    = %f\n", m);
     printf("standard deviation  = %f\n", sd);
     printf("standard deviation  = %f\n", sd);
 
     /* indicate success */
     /* indicate success */
     return 0;
     exit(EXIT_SUCCESS);
}
}
</source>
</source>


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

Revision as of 12:19, 26 June 2007

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

  1. include <stdio.h>
  2. include <stdlib.h>
  1. define NUM_MAX 10

/* main: main processing of standard deviation */ /* NOTE: on UNIX you may have to compile with the -lm option

* at the *end* of the command line 
*/

int main(void) {

   int n, i, d, a[NUM_MAX];
   double x = 0, sd, s = 0, m, sum = 0;

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

   if ( n > NUM_MAX )
   {
       printf("You entered a value greater than %d\n", NUM_MAX);
       exit(EXIT_FAILURE);
   }

   for (i = 0; i < n; i++)
   {
       printf("enter integer 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 */
   exit(EXIT_SUCCESS);

} </source>