Archive:Standard deviation (source code): Difference between revisions
Appearance
wikademia>JWSchmidt cat |
wikademia>Mickraus ANSI/ISO compliant; reformatted |
||
| Line 1: | Line 1: | ||
<pre>#include<stdio | <math>Insert formula here</math><pre>#include<stdio.h> | ||
#include<math.h> | #include<math.h> | ||
#define NUM_MAX 10 | |||
int main(void) | |||
{ | { | ||
int n,i,d,a[ | 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); | |||
printf("enter | |||
scanf("%d",&n); | if ( n > NUM_MAX ) | ||
for(i=0;i<n;i++) | { | ||
{ | printf("n > %d\n", NUM_MAX); | ||
printf("enter | return 0; | ||
scanf("%d",&a[i]); | } | ||
} | |||
for(i=0;i<n;i++) | for(i = 0; i < n; i++) | ||
{ | { | ||
x=x+a[i]; | printf("enter element %d of %d: ", i + 1, n); | ||
m=x/n; | scanf("%d", &a[i]); | ||
d=a[i]-m; | } | ||
sum= | |||
} | for(i = sum = 0; i < n; i++) | ||
s=sum/n; | { | ||
sd=sqrt(s); | x = x + a[i]; | ||
printf("the number of terms=%d \n",n); | m = x / n; | ||
printf("their mean is m=%f \n",m); | d = a[i] - m; | ||
printf("standard deviation=%f \n",sd); | 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; | |||
} | } | ||
</pre> | </pre> | ||
[[Category:C source code examples]] | [[Category:C source code examples]] | ||
Revision as of 15:52, 30 January 2007
<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;
}