Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > averages 2

Reply
Thread Tools

averages 2

 
 
Bill Cunningham
Guest
Posts: n/a
 
      06-29-2009
I have decided on how I want to do this. Old data will be stored to a
file. I only want the functions defined as Sma or simple moving average, an
arithmetic mean; to do the calculating work. I want to be able to take as
many doubles as I want and sum them and divide by the number of doubles.
Then the function would return a value that would be saved as a text or
binary file via another function. This is what little bit I have so far.

#include "ta.h"

double Sma (double *num) {
int count=*num;

The header ta.h says this...

#include <stdio.h>
#include <stdlib.h>

double Sma (double *);

Bill


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-29-2009
"Bill Cunningham" <> writes:
> I have decided on how I want to do this. Old data will be stored to a
> file. I only want the functions defined as Sma or simple moving average, an
> arithmetic mean; to do the calculating work. I want to be able to take as
> many doubles as I want and sum them and divide by the number of doubles.
> Then the function would return a value that would be saved as a text or
> binary file via another function. This is what little bit I have so far.
>
> #include "ta.h"
>
> double Sma (double *num) {
> int count=*num;
>
> The header ta.h says this...
>
> #include <stdio.h>
> #include <stdlib.h>
>
> double Sma (double *);


*sigh*

Why does ta.h include stdio.h and stdlib.h? Does it use any
declarations from either of those headers?

In Sma, why do you store a double in an int? And how do you expect it
to know how many doubles to add together? Where are these double
values going to come from?

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-29-2009
Keith Thompson <kst-> writes:

> "Bill Cunningham" <> writes:
>> I have decided on how I want to do this.

<snip>
>> double Sma (double *num) {
>> int count=*num;

<snip>
> In Sma, why do you store a double in an int? And how do you expect it
> to know how many doubles to add together?


I suspect the answer to these are one and the same: the first double
is number of doubles to add.

--
Ben.
 
Reply With Quote
 
luserXtrog
Guest
Posts: n/a
 
      06-29-2009
On Jun 28, 9:28*pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> Keith Thompson <ks...@mib.org> writes:
> > "Bill Cunningham" <nos...@nspam.invalid> writes:
> >> * * I have decided on how I want to do this.

> <snip>
> >> double Sma (double *num) {
> >> *int count=*num;

> <snip>
> > In Sma, why do you store a double in an int? *And how do you expect it
> > to know how many doubles to add together?

>
> I suspect the answer to these are one and the same: the first double
> is number of doubles to add.
>


Well then, how about:

double Sma (double *num) {
int count=*num,i;
for (*num=0;i;i--) {
*num+=*(num+i+1);
}
return *num/count;
}

--
lxt
sooner sooner
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      06-29-2009
Bill Cunningham wrote:
> I have decided on how I want to do this. Old data will be stored to a
> file. I only want the functions defined as Sma or simple moving average, an
> arithmetic mean; to do the calculating work. I want to be able to take as
> many doubles as I want and sum them and divide by the number of doubles.
> Then the function would return a value that would be saved as a text or
> binary file via another function. This is what little bit I have so far.
>
> ...
> double Sma (double *num) {
> int count=*num;


Storing metadata (the count) in your data (the numbers to average) is
usually a Bad Idea(tm). Also, if you're expecting the caller to pass
you an array, make that clear; your prototype looks like your function
expects a pointer to one (and only one) double.

However, what you've described above is just calculating the mean, not a
moving average:

double mean(size_t count, double numbers[]) {
double total = 0;

if (!count) return 0; /* prevent division by zero */
for (size_t i=0; i<count; i++)
total += numbers[i];
return total/count;
}

To calculate a moving average, you need to specify more about exactly
what you're trying to do, i.e. how you would solve this problem with
pencil and paper. It may help to try to think of it in terms of calling
the above mean() function, though.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Isaac Jaffe
 
Reply With Quote
 
Tim Harig
Guest
Posts: n/a
 
      06-29-2009
On 2009-06-29, Stephen Sprunk <> wrote:
> Storing metadata (the count) in your data (the numbers to average) is
> usually a Bad Idea(tm). Also, if you're expecting the caller to pass
> you an array, make that clear; your prototype looks like your function
> expects a pointer to one (and only one) double.


Agreed, this guy is repeatedly refering to stratagies that anybody with
even a basic understanding of C, or even programming, in general would
reject outright.

In the previous thread:
On 2009-06-28, Barry Schwarz <> wrote:
> Well done Bill. You started implying a very simple problem, got a
> bunch of people including me to bite, and now have moved on to moving
> averages, deleting "obsolete" data, saving the obsolete data in a
> file, binary search trees, malloc, and sizeof. And you did this
> without ever telling us what you really wanted to do.
>
> Congratulations. One of your best troll baits in years.


He changes his thoughts almost instantly, his response patterns are almost
non-sequitor, and he simply seems to reject the responses that he received
in the previous thread. I am beginning to think that Barry has it pegged.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      06-29-2009
On 29 June, 05:03, Tim Harig <user...@ilthio.net> wrote:
> On 2009-06-29, Stephen Sprunk <step...@sprunk.org> wrote:
>
> > Storing metadata (the count) in your data (the numbers to average) is
> > usually a Bad Idea(tm). *Also, if you're expecting the caller to pass
> > you an array, make that clear; your prototype looks like your function
> > expects a pointer to one (and only one) double.

>
> Agreed, this guy is repeatedly refering to stratagies that anybody with
> even a basic understanding of C, or even programming, in general would
> reject outright.
>
> In the previous thread:
> On 2009-06-28, Barry Schwarz <schwa...@dqel.com> wrote:
>
> > Well done Bill. *You started implying a very simple problem, got a
> > bunch of people including me to bite, and now have moved on to moving
> > averages, deleting "obsolete" data, saving the obsolete data in a
> > file, binary search trees, malloc, and sizeof. *And you did this
> > without ever telling us what you really wanted to do.

>
> > Congratulations. *One of your best troll baits in years.

>
> He changes his thoughts almost instantly, his response patterns are almost
> non-sequitor, and he simply seems to reject the responses that he received
> in the previous thread. *I am beginning to think that Barry has it pegged.


Bill claims to have short term(?) memory problems. Though he seems to
make no attempt to overcome his alleged handicap (eg. writing down
what
people suggest to him and checking it before re-posting)
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-29-2009
luserXtrog <> writes:

> On Jun 28, 9:28Â*pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
>> Keith Thompson <ks...@mib.org> writes:
>> > "Bill Cunningham" <nos...@nspam.invalid> writes:
>> >> Â* Â* I have decided on how I want to do this.

>> <snip>
>> >> double Sma (double *num) {
>> >> Â*int count=*num;

>> <snip>
>> > In Sma, why do you store a double in an int? Â*And how do you expect it
>> > to know how many doubles to add together?

>>
>> I suspect the answer to these are one and the same: the first double
>> is number of doubles to add.

^ the (sigh)
>
> Well then, how about:


I did not want to suggest that it is a good idea. Bill: you should
pass a double * and an integer count. Integer does not mean int. I
often prefer to use an unsigned type for this (like size_t).

> double Sma (double *num) {
> int count=*num,i;
> for (*num=0;i;i--) {
> *num+=*(num+i+1);
> }
> return *num/count;
> }


Eh?

--
Ben.
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-29-2009

"luserXtrog" <> wrote in message
news:c764e4b7-1662-44cc-808e-...
On Jun 28, 9:28 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> Keith Thompson <ks...@mib.org> writes:
> > "Bill Cunningham" <nos...@nspam.invalid> writes:
> >> I have decided on how I want to do this.

> <snip>
> >> double Sma (double *num) {
> >> int count=*num;

> <snip>
> > In Sma, why do you store a double in an int? And how do you expect it
> > to know how many doubles to add together?

suspect the answer to these are one and the same: the first double
> is number of doubles to add.
>

I was unclear about using an int and double. One can be stored in the
other but not vice versa. I am not looking for only one double. With arrays
you don't have to set aside a certain amount of memory and one does not have
to use malloc. I don't know how I would see how many doubles would be put
together.

double * would take as many doubles as I gave it. (buffer control
problem??) Then they have to be simply added and divided and SMA return a
double that is the mean. How can I do this?

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-29-2009

"luserXtrog" <> wrote in message
news:c764e4b7-1662-44cc-808e-...

Well then, how about:

double Sma (double *num) {
int count=*num,i;
for (*num=0;i;i--) {
*num+=*(num+i+1);
}
return *num/count;
}

Has the i been previously declared?
The for statement I understand as a dereference.
After trying to remember what I think I once knew and looking it up for the
nth time I think something like the above is what I would come up with. I
will see if it compiles.

Bill


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
averages Bill Cunningham C Programming 48 07-14-2009 07:16 PM
moving averages Bill Cunningham C Programming 0 12-28-2008 11:52 PM
Confused about benchmarks calculating averages of large arrays. jason.cipriani@gmail.com C Programming 3 05-26-2008 06:20 PM
(off topic) GOT-STL-P (Re: Interesting developments since "Beating the averages"?) alex.gman@gmail.com C++ 1 02-11-2006 05:10 PM
Using the DOM to calculate and display averages Mick White Javascript 2 07-05-2004 06:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57