Re: averages



On 2009-06-28, Bill Cunningham <nospam@xxxxxxxxxxxxx> wrote:
On 2009-06-27, Bill Cunningham <nospam@xxxxxxxxxxxxx> wrote:
The m in Sma() and Ema() means "moving" as in a moving average. So if my
second parameter int count for example is 12, and 15 numbers are in the list
the 3 oldest are to be eliminated or atleast put somewhere in a file or

You should have told us from the beginning that you only wanted a running
total. It makes a *HUGE* difference in your design. Again I would suggest
using data structures rather the arrays. For a running average, a
doubly-linked array data structure would be ideal.

I already posted a example of using a singly linked data structure to
find a simple average. It could easily be modified to calculate your
running average if you also modify it to used a doubly linked list.

something for safekeeping and dropped from the average. That's how I would
know that I've hit the "end". As a 13th number is added to the beginning,
the 1st or oldest value is removed from the average. Just something more I
guess to complicate my life.

If you want to use arrays Use a standard average function but trim the
input to include. Something like (psuedo code):

/*
from a array of total_count elements, return a pointer to last wanted_count
element from the end -- returns NULL if not enough elements are available
*/
double* tail(double* data, int total_count, int wanted_count);

/*
from a data array of count number of elements, retrn the average
*/
double average(double* data, int count);

/* same argument definition as tail() */
double Sma(double* data, int total_count, int wanted_count) {
new_set = tail(data, total_count, wanted_count)
if (new_set == NULL) {
global_error_flag = ERROR;
return whatever_invalid_unable_to_indentify_as_worthless;
}
return average(new_set, wanted_count);
}

Note that you will have to define some kind of error handling system as you
cannot indicate errors using doubles.

news:PKmdnUb09fN6L9vXnZ2dnUVZ_oZi4p2d@xxxxxxxxxxxxxxxxxxxxxxxxx
So how does your function *KNOW* that 15 numbers are in the list?
That's the big question. I don't know. This is in idea stage at this
time. I am writing prototypes in a header right now.

The bottom line is that you are leaping without a clue of what you are
doing right now. Stop, clarify what you are doing, listen to
what people are telling you, then think again. Only then should you act.

A. Assuming you are using an array or singly linked list -- not a doubly
linked list -- you have to tell it where to find the first point, how
may datapoints you are passing to it, where to find the first datapoint you
want tabulated, and how many elements you want tabulated. Then you *MUST*
give it exactly what you told it you would give it or you can cause an
overun.

If you pass it a doubly linked list, then it can find the data locations
and the limits of the dataset for itself. I have already posted a simple
average routine using a singly linked list before I realized you wanted a
running average. I could be modified easily enough to calculate a running
average if you modify it to use doubly linked lists for your data
structure.

If you're passing a "double *" to the function, it can't be a linked
list. Is the double after the last number that counts set to -1776.0
as a marker?

Where did the -1776.0 come from? I didn't see it in the thread?

Any negative number would do. Even -1 that some functions return on
failure.

I am not sure what this is in reponse to?

If you are thinking of passing -1 to a function as a pointer then you
are in error as pointers are unsigned and using a constant to a pointer
is insane. If you are thinking as passing it along as a double, then
how will you determine whether a -1 is valid data or an error?

Functions which pass -1 can do so only because they have elimited negative
numbers as valid data. Doing so is usually a sign of poor design even
when it is possible (see _Code Complete_ by Steve McConnell and _Writing
Solid Code_ by Steve Maguire).
.



Relevant Pages

  • Re: Sorting an array. Fastest way.
    ... Just imagine 100 random numbers are there we sort them. ... value need to be ordered at correct place in the sorted array. ... It do not support Int. ... LinkedList, like all Lists, holds objects not primitives. ...
    (comp.lang.java.programmer)
  • Re: alignment question about pointer conversion
    ... int data_size; ... Is this going to be an array of pointers to nodes or just a memory ... linked lists are implemented using pointers to the ...
    (comp.lang.c)
  • use of static
    ... int nDaysOnMonth{ ... The function works but my doubt is if it remakes the array each time ... the type of element on the lists... ... The use of calloc assicure that each pointer will be ...
    (comp.lang.c)
  • Re: foreach enhancement
    ... int and you have a long literal, the list is IList, if it returns ints ... an array containing those values. ... way to generate a list of IEnumerables, but if someone wants that edge case ... will operate over that lists IEnumerable implementation. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Faster than link list?
    ... I'd suggest something that has less addressing overhead: stack or array. ... linked lists are still extremely fast. ...
    (comp.graphics.algorithms)