adding arrays

  • Thread starter Thread starter Troy
  • Start date Start date
T

Troy

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
 
...
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
 
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}
 
Troy said:
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
 
Thanks

Anthony Borla said:
Troy said:
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
 
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 = lhs.sales + rhs.sales;
}
return result;
}
}

Now you _can_ say:

YearSalesByMonth yearTotal = year1 + year2;
 
Back
Top