[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Adding elements from two different arrays

santiago

10/21/2008 6:34:00 AM

I guess one cannot do this:

arraytot[x][y] = arraytot[x][y] + arraydet[x][y];

So, what's the trick to adding arrays like this?

Thanks.

6 Answers

acehreli

10/21/2008 6:56:00 AM

0

On Oct 20, 11:33 pm, santiago <santiago10...@aol.com> wrote:
> I guess one cannot do this:

You need to be more specific because what you show is fine:

> arraytot[x][y] = arraytot[x][y] + arraydet[x][y];

That adds arraydet[x][y] to arraytot[x][y]. Written more readably:

arraytot[x][y] += arraydet[x][y];

> So, what's the trick to adding arrays like this?

Are you really trying to add two arrays? Is it defined as adding
corresponding elements? Then you can do it by adding the elements one
by one in a loop.

Ali

santiago

10/21/2008 12:29:00 PM

0

On 2008-10-21 02:55:48 -0400, acehreli@gmail.com said:

> On Oct 20, 11:33 pm, santiago <santiago10...@aol.com> wrote:
>> I guess one cannot do this:
>
> You need to be more specific because what you show is fine:
>
>> arraytot[x][y] = arraytot[x][y] + arraydet[x][y];
>
> That adds arraydet[x][y] to arraytot[x][y]. Written more readably:
>
> arraytot[x][y] += arraydet[x][y];

Yea. I tried that. It didn't work either.
>
>> So, what's the trick to adding arrays like this?
>
> Are you really trying to add two arrays? Is it defined as adding
> corresponding elements? Then you can do it by adding the elements one
> by one in a loop.


Actually, C++ will not let you add two array elements like that (I am
doing it from a loop, but
just pulled out the line that C++ does not like). The error it generates is:

error C2110: '+' : cannot add two pointers

What I'm trying to do is accumulate totals from one array (storing the
result in a total array, so it'd be more accurate to say I'm trying to
add the elements together.



>
> Ali


jt

10/21/2008 12:36:00 PM

0

On Tue, 21 Oct 2008 08:29:08 -0400, santiago wrote:

> On 2008-10-21 02:55:48 -0400, acehreli@gmail.com said:
>
>> On Oct 20, 11:33 pm, santiago <santiago10...@aol.com> wrote:
>>> I guess one cannot do this:
>>
>> You need to be more specific because what you show is fine:
>>
>>> arraytot[x][y] = arraytot[x][y] + arraydet[x][y];
>>
>> That adds arraydet[x][y] to arraytot[x][y]. Written more readably:
>>
>> arraytot[x][y] += arraydet[x][y];
>
> Yea. I tried that. It didn't work either.
>>
>>> So, what's the trick to adding arrays like this?
>>
>> Are you really trying to add two arrays? Is it defined as adding
>> corresponding elements? Then you can do it by adding the elements one
>> by one in a loop.
>
> Actually, C++ will not let you add two array elements like that

Actually it will...

> (I am doing it from a loop, but just pulled out the line that C++ does
> not like). The error it generates is:
>
> error C2110: '+' : cannot add two pointers

Pointers??? How have you declared your arrays? A minimal program
demonstrating the problem would really help us to help you.

--
Lionel B

Default User

10/21/2008 4:26:00 PM

0

santiago wrote:


>
> Actually, C++ will not let you add two array elements like that (I am
> doing it from a loop, but just pulled out the line that C++ does not
> like). The error it generates is:
>
> error C2110: '+' : cannot add two pointers
>
> What I'm trying to do is accumulate totals from one array (storing
> the result in a total array, so it'd be more accurate to say I'm
> trying to add the elements together.


Don't describe your code, post it. You have a mistake.




Brian

Salt_Peter

10/21/2008 5:02:00 PM

0

On Oct 21, 8:29 am, santiago <santiago10...@aol.com> wrote:
> On 2008-10-21 02:55:48 -0400, acehr...@gmail.com said:
>
> > On Oct 20, 11:33 pm, santiago <santiago10...@aol.com> wrote:
> >> I guess one cannot do this:
>
> > You need to be more specific because what you show is fine:
>
> >> arraytot[x][y] = arraytot[x][y] + arraydet[x][y];
>
> > That adds arraydet[x][y] to arraytot[x][y]. Written more readably:
>
> > arraytot[x][y] += arraydet[x][y];
>
> Yea. I tried that. It didn't work either.
>
>
>
> >> So, what's the trick to adding arrays like this?
>
> > Are you really trying to add two arrays? Is it defined as adding
> > corresponding elements? Then you can do it by adding the elements one
> > by one in a loop.
>
> Actually, C++ will not let you add two array elements like that (I am
> doing it from a loop, but
> just pulled out the line that C++ does not like). The error it generates is:
>
> error C2110: '+' : cannot add two pointers
>
> What I'm trying to do is accumulate totals from one array (storing the
> result in a total array, so it'd be more accurate to say I'm trying to
> add the elements together.
>

Umm, no. If you are getting 'cannot add two pointers' then you arent
adding elements together. Maybe you are trying to add the contents at
those pointers together? In which case one dereferences the pointer:
*(arraytot[x][y])

I'm baffled as to how you've exposed your problem.

That is:
arraytot[x][y];
is not legal (assuming x and y are integer constants).

But these are:
int arraytot[x][y];
int* arraytot[x][y];

Which is it?

Fraser Ross

10/21/2008 6:52:00 PM

0

This function call will work, assuming the element type is int:

std::transform(arraytot.begin(), arraytot.end(), arraydet.begin(),
arraytot.begin(), plus<int>());


Fraser.