averages 2

Discussion in 'C Programming' started by Bill Cunningham, Jun 29, 2009.

  1. 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
     
    Bill Cunningham, Jun 29, 2009
    #1
    1. Advertising

  2. "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) <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"
     
    Keith Thompson, Jun 29, 2009
    #2
    1. Advertising

  3. Keith Thompson <> 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.
     
    Ben Bacarisse, Jun 29, 2009
    #3
  4. Bill Cunningham

    luserXtrog Guest

    On Jun 28, 9:28 pm, Ben Bacarisse <> wrote:
    > Keith Thompson <> 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.
    >


    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
     
    luserXtrog, Jun 29, 2009
    #4
  5. 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;
    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
     
    Stephen Sprunk, Jun 29, 2009
    #5
  6. Bill Cunningham

    Tim Harig Guest

    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.
     
    Tim Harig, Jun 29, 2009
    #6
  7. On 29 June, 05:03, Tim Harig <> wrote:
    > 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.


    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)
     
    Nick Keighley, Jun 29, 2009
    #7
  8. luserXtrog <> writes:

    > On Jun 28, 9:28 pm, Ben Bacarisse <> wrote:
    >> Keith Thompson <> 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.

    ^ 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.
     
    Ben Bacarisse, Jun 29, 2009
    #8
  9. "luserXtrog" <> wrote in message
    news:...
    On Jun 28, 9:28 pm, Ben Bacarisse <> wrote:
    > Keith Thompson <> 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?

    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
     
    Bill Cunningham, Jun 29, 2009
    #9
  10. "luserXtrog" <> wrote in message
    news:...

    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
     
    Bill Cunningham, Jun 29, 2009
    #10
  11. "Tim Harig" <> wrote in message
    news:C202m.5076$...

    [snip]

    > Other conditions, such as stupidity, would be far more difficult to
    > differentiate from trolling.


    I didn't mean to start something. I admit to laziness. I don't like to
    have to go through kandr2 again and again. 3 days after I read it I've
    forgotten it if I understood what I was reading. Maybe it is just stupidity.

    Bill
     
    Bill Cunningham, Jun 29, 2009
    #11
  12. Bill Cunningham wrote:
    > 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.


    You "put together" a number of something with an array. You could get
    the array by statically declaring one or by malloc()ing a chunk of
    memory and treating it like an array.

    > 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?


    I've already provided example code that does exactly that. Try reading
    the replies to your questions before you ask again for help that has
    already been given.

    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
     
    Stephen Sprunk, Jun 29, 2009
    #12
  13. Bill Cunningham

    Curtis Dyer Guest

    On 29 Jun 2009, "Bill Cunningham" <> wrote:

    > "Tim Harig" <> wrote in message
    > news:C202m.5076$...
    >
    > [snip]
    >
    >> Other conditions, such as stupidity, would be far more difficult
    >> to differentiate from trolling.

    >
    > I didn't mean to start something. I admit to laziness. I
    > don't
    > like to have to go through kandr2 again and again. 3 days after I
    > read it I've forgotten it if I understood what I was reading.
    > Maybe it is just stupidity.


    In, "Preface to the First Edition", in K&R2, they indicate the
    book is intended to target those with some basic programming
    experience. Perhaps you might find using a different language
    easier when learning basic programming concepts like variables,
    loops, functions, etc.

    It seems to me, one of your major problems is figuring out how
    take a problem in your head and adequately relate the solution to
    C code. Perhaps it would be easier to use a higher-level language
    to get the hang of some basic programming concepts.

    --
    int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
    o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
    ~ Anonymous (1984 IOCCC winner)
     
    Curtis Dyer, Jun 30, 2009
    #13
  14. "Curtis Dyer" <> wrote in message
    news:%Dc2m.1996$...

    > In, "Preface to the First Edition", in K&R2, they indicate the
    > book is intended to target those with some basic programming
    > experience. Perhaps you might find using a different language
    > easier when learning basic programming concepts like variables,
    > loops, functions, etc.


    I can do anything in BASIC. I could try perl but it doesn't compile.

    > It seems to me, one of your major problems is figuring out how
    > take a problem in your head and adequately relate the solution to
    > C code.


    Agreed. A big problem.

    Perhaps it would be easier to use a higher-level language
    > to get the hang of some basic programming concepts.
    >

    Mix it all with trying to learn algorithms like binary trees. I actually
    have suprised myself with C a couple of times. But now I guess it's back to
    the basics and kandr2 again. You have to use this stuff everyday to keep it
    "in memory" ;)

    Bill
     
    Bill Cunningham, Jun 30, 2009
    #14
  15. On Mon, 29 Jun 2009 03:28:12 +0100, Ben Bacarisse
    <> wrote:

    >Keith Thompson <> 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.


    If that is actually what Bill had in mind, it would be better than 90%
    proof he is trolling.

    --
    Remove del for email
     
    Barry Schwarz, Jun 30, 2009
    #15
  16. On Mon, 29 Jun 2009 11:29:54 -0400, "Bill Cunningham"
    <> wrote:

    >
    >"Tim Harig" <> wrote in message
    >news:C202m.5076$...
    >
    >[snip]
    >
    >> Other conditions, such as stupidity, would be far more difficult to
    >> differentiate from trolling.

    >
    > I didn't mean to start something. I admit to laziness. I don't like to
    >have to go through kandr2 again and again. 3 days after I read it I've
    >forgotten it if I understood what I was reading. Maybe it is just stupidity.


    So you recognize you have a problem but you don't want to bother
    taking the steps to deal with it. On the other hand, you have no
    objection to asking us to deal with it repeatedly for you. Is that
    better or worse than being a troll?

    --
    Remove del for email
     
    Barry Schwarz, Jun 30, 2009
    #16
  17. "Barry Schwarz" <> wrote in message
    news:...

    [snip]

    > So you recognize you have a problem but you don't want to bother
    > taking the steps to deal with it. On the other hand, you have no
    > objection to asking us to deal with it repeatedly for you. Is that
    > better or worse than being a troll?
    >

    I don't know if I am bringing my problems to the forum to quickly
    without trying it myself. I hoped that someone would have the answer to this
    because they have dealt with it before. I guess I'm asking someone that's
    been there to do the homework for me.

    Bill
     
    Bill Cunningham, Jun 30, 2009
    #17
  18. Bill Cunningham

    Tim Harig Guest

    On 2009-06-29, Bill Cunningham <> wrote:
    > "Tim Harig" <> wrote in message
    > news:C202m.5076$...
    >> Other conditions, such as stupidity, would be far more difficult to
    >> differentiate from trolling.

    > I didn't mean to start something. I admit to laziness. I don't like to
    > have to go through kandr2 again and again. 3 days after I read it I've
    > forgotten it if I understood what I was reading. Maybe it is just stupidity.


    By that definition, everybody has memory problems in programming. Three
    days is a long time in human terms. The neet thing about computers is that
    they tend to hold data very constantly. Your previous thread was less then
    three days old when you started this one and I bet the messages are still
    on your server. If not, there are a number of usenet sites available on
    the web. You can look back on what has been discussed. That doesn't take
    memory -- it just takes initiative. Why should the rest of us be bothered
    with your laziness?
     
    Tim Harig, Jun 30, 2009
    #18
  19. Bill Cunningham

    Tim Harig Guest

    On 2009-06-30, Bill Cunningham <> wrote:
    >
    > "Curtis Dyer" <> wrote in message
    > news:%Dc2m.1996$...
    >
    >> In, "Preface to the First Edition", in K&R2, they indicate the
    >> book is intended to target those with some basic programming
    >> experience. Perhaps you might find using a different language
    >> easier when learning basic programming concepts like variables,
    >> loops, functions, etc.

    > I can do anything in BASIC. I could try perl but it doesn't compile.


    If you like BASIC then use BASIC. PowerBASIC is a BASIC compiler based on
    the QBASIC/QuickBASIC dialect.
     
    Tim Harig, Jun 30, 2009
    #19
  20. Bill Cunningham

    Doug Miller Guest

    In article <4a4966da$0$23752$>, "Bill Cunningham" <> wrote:
    > I don't know if I am bringing my problems to the forum to quickly
    >without trying it myself.


    Yes, you are.

    >I hoped that someone would have the answer to this
    >because they have dealt with it before. I guess I'm asking someone that's
    >been there to do the homework for me.


    How do you expect to ever learn anything, by asking others to do the work for
    you?
     
    Doug Miller, Jun 30, 2009
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    327
    Gianni Mariani
    Feb 11, 2006
  2. Replies:
    3
    Views:
    263
  3. Bill Cunningham

    moving averages

    Bill Cunningham, Dec 28, 2008, in forum: C Programming
    Replies:
    0
    Views:
    271
    Bill Cunningham
    Dec 28, 2008
  4. Bill Cunningham

    averages

    Bill Cunningham, Jun 27, 2009, in forum: C Programming
    Replies:
    48
    Views:
    1,479
    Richard Bos
    Jul 14, 2009
  5. Mick White
    Replies:
    2
    Views:
    77
    Mick White
    Jul 5, 2004
Loading...

Share This Page