billcu34 wrote:
>
>
> --- In [email protected] <mailto:c-prog%40yahoogroups.com>,
> Christopher Coale <chris95...@...> wrote:
> >
> > Bill Cunningham wrote:
> > >
> > >
> > > I want to write a function to take so many numbers (doubles) and
> divide
> > > their total number (int) and get a arithmetic average. This probably
> > > sounds
> > > simple to all the C pros here but here's my parameter.
> > >
> > > double Sma (double *, int);
> > >
> > > That's the prototype I have. The thing is I want to enter as many
> > > number as
> > > I want, two decimals to the right of the . or %.2f\n as printf would
> > > put it.
> > > I don't want a buffer overflow but I can take the numbers I put in a
> > > divid
> > > them by the second parameter. To make things more complex I could
> write
> > > double Sma (double *) and let the body of the function see how many
> > > args are
> > > entered. For this the for or while statements are going to be needed
> > > right?
> > >
> > > Bill
> > >
> > >
> > I'm confused as to what the problem is.
> >
> > double Sma(double *ds, int size)
> > {
> > double res = 0;
> > for (int i = 0; i < size; i++)
> > res += ds[i];
> >
> > return ds / (double)size;
> > }
>
> I have lost some precision with this code. But I would like to take it
> a step farther.
>
> double Sma(double *);
>
> Is my prototype. Instead of entering the value to divide by how can I
> get the program to count the number of numbers entered, can I get the
> code to count this itself?
>
> Bill
>
>
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.