473,734 Members | 2,812 Online

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.