473,888 Members | 1,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2371
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********@yah oo.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.co m
<Ro********@yah oo.co.ukwrote in message
news:11******** *************@w 5g2000hsg.googl egroups.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********@yaho o.co.uk
<Ro********@yah oo.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(d ouble[] pricesHistory, int cdayAverage)
{
double priceSum, priceMovingAver age;
double[] pricesMoving = new double[cdayAverage];
int ipriceCur, ipriceCircle, cpriceAveraged;

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

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

priceSum += priceCur;

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

pricesMoving[ipriceCircle++] = priceCur;

priceMovingAver age = priceSum / cpriceAveraged;
}
}
// Queue

void MovingAverage(d ouble[] pricesHistory, int cdayAverage)
{
double priceSum, priceMovingAver age;
Queue<doublepri cesMoving = new Queue<double>(c dayAverage)
int ipriceCur;

ipriceCur = 0;

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

priceSum += priceCur;

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

pricesMoving.En queue(priceCur) ;

priceMovingAver age = priceSum / pricesMoving.Co unt;
}
}

In either algorithm, the input data is passed in with "pricesHistory" , the
number of days to average is pased in with "cdayAverag e", and the
"priceMovingAve rage" 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********@yaho o.co.uk
<Ro********@yah oo.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
2862
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 > >pointer-to-array-of-size-N-of-type-T (which is fine) and not having type > >array-of-size-N-of-type-T (with some exceptions, which is curious). > > So far > >the consensus seems to be that while everyone is aware of this no one knows
21
3951
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 "browse" it). So I expected that when I run this program, I get both c1.A and c2.A pointing to the same address, and changing c1.A means that also c2.A changes too. ----- BEGIN example CODE -----------
5
29258
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); pfiles = ld.GetFiles("*.aspx.resx|") + ld.GetFiles("*.ascx.resx") + ld.GetFiles("*.master.resx"); but of course there is no + operation allowed on the FileInfo arrays returned by the GetFiles method.
3
2851
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; } *arrays; It's supposed to be a structure containing an array of chars, an array of ints and an int. I declare functions like this : arrays *parseline(char *line, int N)
1
8719
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 Framework are said to have an element type. http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
41
5003
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 the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
6
13181
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: the two arrays {"one","two","three"},{"red","green","blue} the result one red one green
1
2454
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 of different lengths (e.g. usually between 25 and 45 rows. The columns in each file have the same length). The text files have been numbered sequentially e.g. cb0, cb1, cb2 and so on. I would like to read the data from each text file into...
16
2556
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 at just comp.lang.c++. If one of these groups is too inappropriate, just take it off from where you send your replies.) Hi.
29
35566
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 multi-dimensional arrays work, b) how to call a function with a multi-dimensional array, c) how to return a multi-dimensional array from a function, or d) how to read and write arrays from a disc file. Note to C++ programmers: You should be using...
0
9800
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10439
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7990
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5824
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4245
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.