Paul Herring wrote:
>  
>
> On Tue, Jul 21, 2009 at 2:40 AM, billcu34<[email protected] 
> <mailto:billcu1%40suddenlink.net>> wrote:
> >> double Sma(double *ds, int size)
> >> {
> >>     double res = 0;
> >>     for (int i = 0; i < size; i++)
> >>        res += ds[i];
> >>
> >>     return ds / (double)size;
> >> }
> >
> > My compiler complained about the ds in the return statement. It 
> wanted this I guess 'cause I changed it and it accepted.
> >
> > return *ds/(double) size;
>
> It should in fact read
> return res/size;
>
> i.e. the sum divided by the number of items (your solution, while
> fixing the warning, only divides the first number by the number of
> items.)
>
> The (double) cast is, at best documentary, at worst redundant.
>
> > It also didn't like the for statement saying int i. I changed it to 
> this.
> >
> > int i,res=0;
> > for(i=0;i<size;i++)
>
> You have an old compiler - you have the right fix here.
>
> > I don't know what kind of errors these are but I think they have 
> something to do with C89 and ISO C.
>
> -- 
> PJH
>
> http://shabbleland.myminicity.com/com 
> <http://shabbleland.myminicity.com/com>
> http://www.chavgangs.com/register.php?referer=9375 
> <http://www.chavgangs.com/register.php?referer=9375>
>
> 
Yeah, my mistake. I didn't bother testing - I just typed it in the 
e-mail quickly. 

Casting to a double is to avoid compiler warnings (which will just make 
that a cast anyways). I could have used a double as the parameter, but 
why? It needs a whole number (which generally isn't going to need to be 
64-bits, let alone 32) - int is perfect for that.

Reply via email to