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