averages 2

B

Bill Cunningham

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
 
K

Keith Thompson

Bill Cunningham said:
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?
 
B

Ben Bacarisse

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.
 
L

luserXtrog

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;
}
 
S

Stephen Sprunk

Bill said:
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
 
T

Tim Harig

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:
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.
 
N

Nick Keighley

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:



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)
 
B

Ben Bacarisse

luserXtrog said:
^ 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?
 
B

Bill Cunningham

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
 
B

Bill Cunningham

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
 
B

Bill Cunningham

[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
 
S

Stephen Sprunk

Bill said:
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
 
C

Curtis Dyer

[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.
 
B

Bill Cunningham

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
 
B

Barry Schwarz

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.
 
B

Barry Schwarz

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

Bill Cunningham

[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
 
T

Tim Harig

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

Tim Harig

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.
 
D

Doug Miller

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top