Calculate Running Total from Array

  • Thread starter Thread starter Stuart Shay
  • Start date Start date
S

Stuart Shay

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)

Thanks
Stuart
 
Stuart Shay said:
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;
monTotal = 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.
 
Hello!

First of all there are 12 month in a year and the index start at 0 so you
should have 11 not 12.
Second you could use the foreach statement instead of a for loop in this
way.
decimal total = 0;
foreach (Decimal dec in month)
{
total += dec;
}

//Tony

Michael C said:
Stuart Shay said:
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;
monTotal = 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.

Thanks
Stuart
 
Stuart Shay said:
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;
monTotal = total;

I suspect you meant: monTotal = 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
 
TonyJ said:
Hello!

First of all there are 12 month in a year and the index start at 0 so you
should have 11 not 12.

This is not correct. You want an array sized to 12, not 11. If I do:

object[] o = new object[12];

it creates 12 entries. This is independent of the index.


Example:

static void Main(string[] args)
{
decimal[] decs = new decimal[12];
string msg = "";
for (int i = 0; i < decs.Length; i++)
{
decs = decimal.Parse(i.ToString());
msg += "index: " + i.ToString() + "\n";
}
MessageBox.Show(msg);
}

Will pop up a messagebox with 12 items (index 0 through 11).


Chris.
 
TonyJ said:
Hello!

First of all there are 12 month in a year and the index start at 0 so you
should have 11 not 12.
Second you could use the foreach statement instead of a for loop in this
way.
decimal total = 0;
foreach (Decimal dec in month)
{
total += dec;
}

I disagree. In this case you won't have an index to store the total back
into an array.

Michael
 
rossum said:
I suspect you meant: monTotal = total;
Yes.
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).


That's exactly what I was saying, you would need a good reason to do this.
It shouldn't be something that is done by default.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top