473,831 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4893
"Stuart Shay" <ss***@yahoo.co mwrote in message
news:64******** *************** ***********@mic rosoft.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.co mskrev i meddelandet
news:u$******** *****@TK2MSFTNG P05.phx.gbl...
"Stuart Shay" <ss***@yahoo.co mwrote in message
news:64******** *************** ***********@mic rosoft.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.co m>
wrote:
>"Stuart Shay" <ss***@yahoo.co mwrote in message
news:64******* *************** ************@mi crosoft.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.com wrote in message
news:e5******** ******@TK2MSFTN GP04.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******@coldm ail.comwrote in message
news:m9******** *************** *********@4ax.c om...
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
4027
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 default is 4 letters). This is the Fed. Everything is a TLA (three-letter acronym). Therefore, since I'm building a PORTABLE web application, changing MySQL's default settings for fulltext index querying is completely undoable and unrealistic, so...
1
10000
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 number. That number will appear in a seperate box at the bottom. So basically whatever you choose has a corresponding number associated with it (except for the text input, which you enter whatever number) and those numbers are added and produced in...
53
5762
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 difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
1
6095
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 Cart</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link rel="StyleSheet" href="css/style.css" type="text/css">
1
474
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 database1 (db1) located on server1(S1), 2nd part contains the subreport which uses database2 (db2) located on server2 (S2), and I've used (parameter field & a certain fields) to link the subreport to the mainreport. The subreport is composed of 2...
2
560
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. Currenty I hvae four webform textboxes. I collect dollar abounts and want to show the total as they change from box to box with some client side script. I then will insert these numbers with other text boxes into a database via an insert...
3
2575
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 keeping track of total disk space allocation and usage. For the most part I have the data keyed in but
3
1654
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 " etc which is the total leave taken in month January here the total is 5 now I need to calculate this automatically after entering it in the form and assign this value to Jantotal how is that possible I tried to convert it to an array and assigned...
1
2275
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 any part of "Make Additional Donation" add to the total. When I do get that part to work, then the 1st part for "Lunch" doesn't work. You can view the source, but I left out the javascript that didn't work for me. Thanks.
0
9794
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
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
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10496
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
10210
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...
0
9319
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7750
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
5622
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...
2
3967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.