473,734 Members | 2,812 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 15487

"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
3608
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
4601
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
2411
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
4172
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
31084
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
10797
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
1979
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
3162
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
6559
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
9452
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
9310
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
9184
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
8187
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
6737
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
4551
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...
0
4813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3262
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
3
2180
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.