Bill Cunningham wrote:
>
>
>
> ----- Original Message -----
> From: "Christopher Coale" <[email protected]
> <mailto:chris95219%40gmail.com>>
> To: <[email protected] <mailto:c-prog%40yahoogroups.com>>
> Sent: Saturday, July 25, 2009 5:50 PM
> Subject: Re: [c-prog] Re: moving average problem
>
> > Well, the short answer, is you can't. You can do it if you use some kind
> > of list that keeps the size, otherwise, there is no way to tell where
> > the array ends unless you use a sentinel value - but that wouldn't work
> > very well with a function like this.
>
> OK I see. I'm going to alter this function slightly for a geometric mean
> and a type of exponential mean if there is such a thing really. I do
> have a
> formula.
>
> Is a sentinel value like here in this std function...
>
> strtlen(length)+1; The one holding the null terminator character?
>
> Bill
>
>
The 'null' character in null-terminated strings would be a sentinel
value, yes. But, that won't work very well unless you passed an array of
pointers to doubles.
The precision seems fine for me with this code:
double avg(double *ds, int len)
{
double total = 0;
for (int i = 0; i < len; i++)
total += ds[i];
return total / len;
}
int main(int argc, char *argv[])
{
double ds[3] = {3.2, 6.4, 9.6};
printf("%f", avg(ds, 3));
return 0;
}
So, what code are you using that gives you 8.00 when you print it?