473,465 Members | 1,366 Online
Bytes | Software Development & Data Engineering Community
Create 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 15462

"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,100,110,120}
double [] year2 = {10,20,30,40,50,60,70,80,90,100,110,120}

cant I just do this

double [] yearTotal = year1 + year2?

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

"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:ug**************@TK2MSFTNGP15.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****@pushormitchell.com> wrote in message
news:On*************@TK2MSFTNGP15.phx.gbl...
Thanks Bjorn - this is what I mean.

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

can't I just do this:

double [] yearTotal = year1 + year2?

my end result has to be {20,40,60,80,100,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*****@bigpond.com> wrote in message
news:NF*******************@news-server.bigpond.net.au...

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

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

can't I just do this:

double [] yearTotal = year1 + year2?

my end result has to be {20,40,60,80,100,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 YearSalesByMonth
{
private double[] sales;
public YearSalesByMonth(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 YearSalesByMonth()
{
sales = new double[12];
}

public static YearSalesByMonth operator + (YearSalesbyMonth lhs,
YearSalesbyMonth rhs)
{
YearSalesByMonth result = new YearSalesByMonth();
for (int i = 0; i < 12; i++)
{
result.sales[i] = lhs.sales[i] + rhs.sales[i];
}
return result;
}
}

Now you _can_ say:

YearSalesByMonth 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
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...
0
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...
6
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...
34
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...
23
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....
6
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
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...
11
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 =...
6
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
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,...
1
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...
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...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.