On June 5, 2011 14:57, in comp.lang.c,

(E-Mail Removed) wrote:

> On June 5, 2011 14:25, in comp.lang.c, (E-Mail Removed)d wrote:

>

>> I have some code here and a snippet of unfinished, untested code

>> which

>> is an attempt at a function called stddev. This is of course meant to

>> calculate a standard deviation.

> [snip]

>> double stddev(double mean, double *prices)

>> {

>> double price = 0.0;

>> int i = 0;

>> for (; i < prices; ++i) {

>> if (prices[i] > mean) {

>> price = prices - mean;

>> return prices;

>> } else if (prices[i] < mean) {

>> price = mean - prices;

>> return prices;

>> }

>>
FWIW, from the algorithm and data given on the Wikipedia page, I coded this

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

double StdDev(unsigned int samplesize, double population[])

{

double sum, mean, spread;

unsigned int index;

if (samplesize == 0) return 0.0; /* catch obvious error */

/* compute mean of sample population */

for (index = 0, sum = 0.0 ; index < samplesize; ++index)

sum += population[index];

mean = sum / samplesize;

/* compute variances */

for (index = 0, sum = 0.0 ; index < samplesize; ++index)

{

double delta;

delta = population[index] - mean;

sum += (delta * delta);

}

return sqrt(sum/samplesize); /* standard deviation */

}

/*

** Population values taken from the Wikipedia example

*/

int main(void)

{

double pop[] = {2,4,4,4,5,5,7,9};

unsigned int popsize = (sizeof(pop) / sizeof(pop[0]));

printf("The standard deviation = %f\n",StdDev(popsize,pop));

return EXIT_SUCCESS;

}

When I compile and run this code

$ cc -lm -o stddev stddev.c

$ stddev

The standard deviation = 2.000000

$

I get the same Standard Deviation value as the Wikipedia article's example

HTH

--

Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576

Me:

http://pitcher.digitalfreehold.ca/ | Just Linux:

http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------