On Wed, 3 Oct 2007 13:46:08 +1000, "Michael C" <mi**@nospam.com>
wrote:
>"Stuart Shay" <ss***@yahoo.comwrote in message
news:64**********************************@microso ft.com...
>Hello All:
I have a array which contains the totals for each month and from this
array I want to get a running total for each month
decimal[] month = new decimal[12];
month[0] = 254; (Jan)
month[1] = 78; (Feb)
month[2] = 34; (Mar)
ect ...
What is the best way I can easily obtain the Running Total for each month
decimal[] monTotal = new decimal[12];
monTotal[0] = 254; (Jan - Running Total)
monTotal[1] = 332; (Feb - Running Total)
monTotal[2] = 366; (Mar- Running Total)
decimal total = 0;
for(int i = 0; i < month.Length; i++)
{
total += month[i];
monTotal = total;
I suspect you meant: monTotal[i] = total;
>}
This might not be the sort of thing you'd keep in an array because you now
have duplicate data and the 2 sets of data could become inconsistant, eg if
you change month[0] but don't recalculate monTotal. I wouldn't say it is
wrong to store the running totals but you should only do it if you have a
reason.
Depending on efficiency issues it may be required to keep the running
totals in an array. An example would be where the updates were very
infrequent (once a month) but the reads much more common (many times a
day).
You are right about duplicate data, there would need to be a boolean
flag to trigger a recalculation of the running totals whenever one of
the month totals was changed. All this can be built into the relevant
class properties/indexes/methods if required.
rossum
>
>>
Thanks
Stuart