473,659 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding arrays

Hello,

I have a dumb question. I have two array objects of type double with 12
elements in each object. I'd like to add the two objects but dont know how
to. Any ideas?

thanks
Nov 16 '05 #1
5 15482

"Troy" wrote...
I have two array objects of type double with 12 elements in each object.
I'd like to add the two
objects but dont know how to. Any ideas?


That depends on what you mean by "adding the two objects".

1. It could mean that you want to sum up all the doubles in either array.

Then you simply loop through the arrays and add each element to a variable
for each array.

2. It could mean that you want to sum up all the doubles in both arrays
together.

Then you do as above, but after that you add the values of those two
variables as well.

3. It could mean that you want to sum up the two corresponding doubles in
each array.

This one supposes that the arrays have equal lengths, but then it's not much
more difficult than the other two variants. Simply start by creating a third
array of equal length. Make one loop through both arrays, and for each
iteration sum up the two doubles from your arrays into the corresponding
element in the third array.

// Bjorn A

Nov 16 '05 #2
Thanks Bjorn - this is what I mean.

double [] year1 = {10,20,30,40,50 ,60,70,80,90,10 0,110,120}
double [] year2 = {10,20,30,40,50 ,60,70,80,90,10 0,110,120}

cant I just do this

double [] yearTotal = year1 + year2?

my end result has to be {20,40,60,80,10 0,120,140,160,1 80,200,220,240}

"Bjorn Abelli" <bj**********@D oNotSpam.hotmai l.com> wrote in message
news:ug******** ******@TK2MSFTN GP15.phx.gbl...

"Troy" wrote...
I have two array objects of type double with 12 elements in each object.
I'd like to add the two
objects but dont know how to. Any ideas?


That depends on what you mean by "adding the two objects".

1. It could mean that you want to sum up all the doubles in either array.

Then you simply loop through the arrays and add each element to a variable
for each array.

2. It could mean that you want to sum up all the doubles in both arrays
together.

Then you do as above, but after that you add the values of those two
variables as well.

3. It could mean that you want to sum up the two corresponding doubles in
each array.

This one supposes that the arrays have equal lengths, but then it's not
much more difficult than the other two variants. Simply start by creating
a third array of equal length. Make one loop through both arrays, and for
each iteration sum up the two doubles from your arrays into the
corresponding element in the third array.

// Bjorn A

Nov 16 '05 #3

"Troy" <ma****@pushorm itchell.com> wrote in message
news:On******** *****@TK2MSFTNG P15.phx.gbl...
Thanks Bjorn - this is what I mean.

double [] year1 = {10,20,30,40,50 ,60,70,80,90,10 0,110,120}
double [] year2 = {10,20,30,40,50 ,60,70,80,90,10 0,110,120}

can't I just do this:

double [] yearTotal = year1 + year2?

my end result has to be {20,40,60,80,10 0,120,140,160,
180,200,220,240 }


No. An array is used to store, and conveniently allow access to, a number of
separate entities. Beyond that, the array has no knowledge of these
entities, so wouldn't 'know' how to perform the task you require.

As Bjorn described earlier, you need to create a new array, loop through the
existing arrays [accessing each corresponding array element in turn, and
summing their values], and update the corresponding element in the new
array.

I hope this helps.

Anthony Borla
Nov 16 '05 #4
Thanks

"Anthony Borla" <aj*****@bigpon d.com> wrote in message
news:NF******** ***********@new s-server.bigpond. net.au...

"Troy" <ma****@pushorm itchell.com> wrote in message
news:On******** *****@TK2MSFTNG P15.phx.gbl...
Thanks Bjorn - this is what I mean.

double [] year1 = {10,20,30,40,50 ,60,70,80,90,10 0,110,120}
double [] year2 = {10,20,30,40,50 ,60,70,80,90,10 0,110,120}

can't I just do this:

double [] yearTotal = year1 + year2?

my end result has to be {20,40,60,80,10 0,120,140,160,
180,200,220,240 }


No. An array is used to store, and conveniently allow access to, a number
of
separate entities. Beyond that, the array has no knowledge of these
entities, so wouldn't 'know' how to perform the task you require.

As Bjorn described earlier, you need to create a new array, loop through
the
existing arrays [accessing each corresponding array element in turn, and
summing their values], and update the corresponding element in the new
array.

I hope this helps.

Anthony Borla

Nov 16 '05 #5
Well, no... _and_ yes.

There are two ways of solving this problem. One, Anthony has already
given you: loop through the arrays and add them up.

The other is to create a class. By creating a class, what you're
saying, in effect, is "this is more than an array": the fact that there
are twelve entries has meaning; the fact that it is an array of doubles
has meaning. So, you create a new kind of "thing" that represents the
meaning.

Now, you haven't told us what the individual values mean, but let's say
they're sales figures. (You really shouldn't store monetary amounts in
doubles, but that's beside the point.... :) You could create a class:

public class YearSalesByMont h
{
private double[] sales;
public YearSalesByMont h(double jan, double feb, double mar, double
apr, double may, double jun, double jul, double aug, double sep, double
oct, double nov, double dec)
{
sales = new double[] { jan, feb, mar, apr, may, jun, jul, aug,
sep, oct, nov, dec };
}
protected YearSalesByMont h()
{
sales = new double[12];
}

public static YearSalesByMont h operator + (YearSalesbyMon th lhs,
YearSalesbyMont h rhs)
{
YearSalesByMont h result = new YearSalesByMont h();
for (int i = 0; i < 12; i++)
{
result.sales[i] = lhs.sales[i] + rhs.sales[i];
}
return result;
}
}

Now you _can_ say:

YearSalesByMont h yearTotal = year1 + year2;

Nov 16 '05 #6

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

Similar topics

7
3604
by: David | last post by:
I have an array that contains numbers in string elements , i want to convert this to integers, i have made a for loop that uses the number of elements in the array and then adds 0, thereby converting it to an integer. //$chars is the array of strings for ($i=0; $i<$numpoints; $i++) { $array = array($chars + 0); } I dont know though how to add all these elements to the 1 array, it
0
4596
by: Björn | last post by:
Hello, Starting to code a robust routine to add two arrays element by element $new = $a + $b ... I thought that this should already be done and searched CPAN for a sutiable module. Despite Math:::Matrix I could not realy find much and the ::Matrix module seemed to be more that I needed...
6
2404
by: Jamie Fryatt | last post by:
Hi everyone, here's what id like to do. I have a table with 2 fields, name and value I need to be able to add multiple records quickly, for example I need to add name value abc 1 abc 2 abc 3
34
4164
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
23
31079
by: Rick | last post by:
Hi, This is probably simple byt when you never did pointers and being used to luxery strings like in Delphi or Visual Basic, C can get though. What I'm trying to do is to add chars to a string. I looked in the string.h file but I didn't find that kind of function (that string library is more than 7 years old) so I decided to write that function on myself. It seems to work although when I'm trying to do printf's between those actions I...
6
10794
by: Maheshkumar.R | last post by:
How i can store images in array @ runtime..? What are the possbile ways.. Thankz.. - Mähésh Kumär. R
1
1976
by: mtchatchez | last post by:
I have several 2-dimensional arrays which I want to sum the values of, returning an array that has for each of its values the sum from all the others (if that makes sense?) ... Okay ... maybe an example will make it clearer! ;-) $array1 = array(array(1,1,1), array(1,1,1), array(1,1,1)); $array2 = array(array(2,2,2), array(0,0,0), array(3,3,3)); ... So is there a clever / fast way to add up the values of the two arrays above to return the...
11
3141
by: dennis.sprengers | last post by:
Consider the following multi-dimensional array: --------------------------- $arr = array( array(3, 5, 7, 9), array(2, 4, 6, 8), array(1, 3, 5, 7) ); function add_arrays($arr) { for ($row = 0; $row < count($arr); $row++) {
6
6553
by: santiago | last post by:
I guess one cannot do this: arraytot = arraytot + arraydet; So, what's the trick to adding arrays like this? Thanks.
0
8427
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
8332
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,...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6179
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
5649
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
4175
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
2750
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
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
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.