473,396 Members | 1,766 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,396 software developers and data experts.

Calculate Running Total from Array

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

Oct 3 '07 #1
6 4852
"Stuart Shay" <ss***@yahoo.comwrote in message
news:64**********************************@microsof t.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;
}

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

Oct 3 '07 #2
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" <mi**@nospam.comskrev i meddelandet
news:u$*************@TK2MSFTNGP05.phx.gbl...
"Stuart Shay" <ss***@yahoo.comwrote in message
news:64**********************************@microsof t.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;
}

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


Oct 3 '07 #3
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
Oct 3 '07 #4
TonyJ wrote:
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[i] = 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.
Oct 3 '07 #5
"TonyJ" <jo*****************@telia.comwrote in message
news:e5**************@TK2MSFTNGP04.phx.gbl...
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
Oct 7 '07 #6
"rossum" <ro******@coldmail.comwrote in message
news:m9********************************@4ax.com...
I suspect you meant: monTotal[i] = 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.
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

Oct 7 '07 #7

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

Similar topics

2
by: Phil Powell | last post by:
Relevancy scores are normally defined by a MySQL query on a table that has a fulltext index. The rules for relevancy scoring will exclude certain words due to their being too short (minimum...
1
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
1
by: bin_P19 P | last post by:
the code i have got is as follows and now im stuck <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Shopping...
1
by: Manal/report designer | last post by:
Thank you in advance for any suggestions... I'm using crystal reports version 8 & SQL server. I've created a report that is composed of 2 parts: 1st part contains the mainreport which uses...
2
by: Davisro | last post by:
I am wondering if it is possible to have a running total of four textboxes so that when any text box is changed I could then calcuate the total of the four boxes and show this on the webform. ...
3
by: Tonij (with a J) | last post by:
Hi all, I have an Access 2003 database that has been a work in progress for a while, it's basically a system inventory and issue tracking system. Recently I have added a seperate area for...
3
by: haridharmajan | last post by:
in Access I have a table called Leaves in which Jan, Feb, Mar....Dec and Jantotal, Febtotal, Martotal.......Dectotal are fields now lets say field Jan contains some data like " 01,07,21,29,31 "...
1
by: Bruce | last post by:
I had a form with a running total working until I was asked to add some checkboxes. Here is what I have: http://www.bearzilla.net/test/Untitled-1.html The first section works, but I can't get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.