473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

arrays

Hi, What is the best way to use an array to find moving averages. I
want to calculate a 15 day and 41 day m/a. I know my array will need
to contain 12 columns, but the rows need to be dynamic. The row size
will need to be aminium of 50 rows. So far Im thinking of either
creating 2 arrays, one for 15 and one for 41 day m/a. Keeping them
each to that row size then iterate through them creating new arrays
when new data added. Eg when 16 data point added to the 15 day m/a,
remove the first data point, add the new data point at the end. I
think im right in thinking that I would have to add the data to a new
array to do this. The other idea was to create an array 12 cols wide
and some how have dynamic rows. Add data indefinatly, then itirate
through the array in reverse, first back 15 points, summing as I go,
and the again 41 points to calculate the average. I dont know how to
iterate in reverse or create an array with set col width but dynamic
rows. Any ideas appreciated.
Regards Rober

May 12 '07 #1
8 2350
Hi,
Find below some link Displaying Arrays .. you may find a solution to your
case:
http://msdn2.microsoft.com/en-us/lib...53(VS.71).aspx

http://www.c-sharpcorner.com/UploadF...ithArrays.aspx

Husam Al-A'araj

"Ro********@yahoo.co.uk" wrote:
Hi, What is the best way to use an array to find moving averages. I
want to calculate a 15 day and 41 day m/a. I know my array will need
to contain 12 columns, but the rows need to be dynamic. The row size
will need to be aminium of 50 rows. So far Im thinking of either
creating 2 arrays, one for 15 and one for 41 day m/a. Keeping them
each to that row size then iterate through them creating new arrays
when new data added. Eg when 16 data point added to the 15 day m/a,
remove the first data point, add the new data point at the end. I
think im right in thinking that I would have to add the data to a new
array to do this. The other idea was to create an array 12 cols wide
and some how have dynamic rows. Add data indefinatly, then itirate
through the array in reverse, first back 15 points, summing as I go,
and the again 41 points to calculate the average. I dont know how to
iterate in reverse or create an array with set col width but dynamic
rows. Any ideas appreciated.
Regards Rober

May 12 '07 #2
Rober,

Do the "columns" in your data have any significance, or are they just
sets of 15 values? If they are sets of 15 values, then I would create a
List<int[](or List<decimal[]>, or List<double[]>), creating your arrays,
and then adding them to the list, then have one method that takes your list
and calculates your average.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<Ro********@yahoo.co.ukwrote in message
news:11*********************@w5g2000hsg.googlegrou ps.com...
Hi, What is the best way to use an array to find moving averages. I
want to calculate a 15 day and 41 day m/a. I know my array will need
to contain 12 columns, but the rows need to be dynamic. The row size
will need to be aminium of 50 rows. So far Im thinking of either
creating 2 arrays, one for 15 and one for 41 day m/a. Keeping them
each to that row size then iterate through them creating new arrays
when new data added. Eg when 16 data point added to the 15 day m/a,
remove the first data point, add the new data point at the end. I
think im right in thinking that I would have to add the data to a new
array to do this. The other idea was to create an array 12 cols wide
and some how have dynamic rows. Add data indefinatly, then itirate
through the array in reverse, first back 15 points, summing as I go,
and the again 41 points to calculate the average. I dont know how to
iterate in reverse or create an array with set col width but dynamic
rows. Any ideas appreciated.
Regards Rober

May 12 '07 #3
Hi, thankyou for your replys. Plenty to read up on. The columns relate
to 12 different shares, and the values represent changes updated every
minute. So over a day there can be many values, whence the need for
dynamic rows.
Regards Robert

May 12 '07 #4
On Sat, 12 May 2007 02:49:56 -0700, Ro********@yahoo.co.uk
<Ro********@yahoo.co.ukwrote:
Hi, What is the best way to use an array to find moving averages. [...]
In answer to the basic question, I would say that two good solutions would
be to either use the Queue<generic class, or implement a circular buffer
yourself with an array. Using the Queue<class involves a little more
overhead "behind the scenes", but allows your own code to most closely
match what it is you're actually doing. A circular buffer is slightly
more efficient, but the implementation in your own code would be less
direct.

Now, that said, your question has a bunch of other pieces of information
that may or may not be relevant to those of us trying to answer it. If
that information is relevant, IMHO you should do a better job of
explaining why. The information about the length of the moving average
(15 and 41 days) seems useful, but I'm left having no idea why you say "my
array will need to contain 12 columns", nor do I know why you say "the row
size will need to be a minimum of 50 rows". These statements may well be
correct, but we don't have enough context from you to understand why.

As an example of what I mentioned above, here are a couple of
implementations of a moving average you might consider using as an outline
for your own needs:

// Circular buffer

void MovingAverage(double[] pricesHistory, int cdayAverage)
{
double priceSum, priceMovingAverage;
double[] pricesMoving = new double[cdayAverage];
int ipriceCur, ipriceCircle, cpriceAveraged;

ipriceCur = 0;
ipriceCircle = 0;
cpriceAveraged = 0;

while (ipriceCur < pricesHistory.Length)
{
double priceCur = pricesHistory[ipriceCur];

priceSum += priceCur;

if (cpriceAveraged < pricesMoving.Length)
{
cpriceAveraged++;
}
else
{
ipriceCircle = ipriceCircle % pricesMoving.Length;
priceSum -= pricesMoving[ipriceCircle];
}

pricesMoving[ipriceCircle++] = priceCur;

priceMovingAverage = priceSum / cpriceAveraged;
}
}
// Queue

void MovingAverage(double[] pricesHistory, int cdayAverage)
{
double priceSum, priceMovingAverage;
Queue<doublepricesMoving = new Queue<double>(cdayAverage)
int ipriceCur;

ipriceCur = 0;

while (ipriceCur < pricesHistory.Length)
{
double priceCur = pricesHistory[ipriceCur];

priceSum += priceCur;

if (!(pricesMoving.Count < cdayAverage))
{
priceSum -= pricesMoving.Dequeue();
}

pricesMoving.Enqueue(priceCur);

priceMovingAverage = priceSum / pricesMoving.Count;
}
}

In either algorithm, the input data is passed in with "pricesHistory", the
number of days to average is pased in with "cdayAverage", and the
"priceMovingAverage" variable contains, at the end of each loop, the
current moving average given the input data.

Hope that helps.

Pete
May 12 '07 #5
Thankyou for your reply Peter, After reading my posts , yuor right its
not very clear. My data is for 12 shares, and I was thinking Id need 1
column for each share. Then when I collected the data,

May 12 '07 #6
Thankyou for your reply Peter, After reading my posts , yuor right its
not very clear. My data is for 12 shares, and I was thinking Id need 1
column for each share. Then when I collected the data, Id add the
result for each share to its relevent column. Id have to have a new
row for each set of new data. The min 50 rows was to make sure that
the 41 day average was calculated correctly. The 15 and 41 day would
be calculated on the same data.In my program I iterate through each
share, updating the data to a row, andhopefully to an array to do the
ave calcs. Thanks for your code example.
Regards Robert
May 12 '07 #7
On Sat, 12 May 2007 13:25:20 -0700, Ro********@yahoo.co.uk
<Ro********@yahoo.co.ukwrote:
Thankyou for your reply Peter, After reading my posts , yuor right its
not very clear. My data is for 12 shares, and I was thinking Id need 1
column for each share.
If you want to keep all of your data in a single two-dimensional array,
then yes...I see now why you would want 12 columns.

That said, I think it might make more sense to just maintain a per-share
moving average data structure, rather than trying to combine everything
into a single array.
Then when I collected the data, Id add the
result for each share to its relevent column. Id have to have a new
row for each set of new data. The min 50 rows was to make sure that
the 41 day average was calculated correctly.
Well, you only need 41 rows (or whatever) for that.
The 15 and 41 day would
be calculated on the same data.
Well, part of the question if implementation depends on exactly how you're
going to be using the data. The code I posted would be most useful if you
intended to generate a daily moving average value for every day in a
series of days for which you have data.

However, if you are only going to generate the "moving average" on request
for a particular day, then it makes more sense to just retrieve the
maximum number of days (41 in this case) up to and including the day for
which you want the calculation, and then calculate the averages directly
for that day only. This would correspond more closely to your "rows and
columns" description, especially if you are retrieving the data from a
database in a way that creates such a two-dimensional array.

Pete
May 13 '07 #8
Thanks again for your reply. The more I delve into this, I think your
suggestion of maintaining a per share data structure is the best
route. The m/a are calculated throughout the day. Eg 41 minutes into
the day the first 15/41 point is calculated, then from that point
onwards its maintained for each share. The calcs itself are easy
enough, its the 12 different share arrays/variables I struggle with.
Regards Robert

May 13 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.