473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Adding elements from two different arrays

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.

Oct 21 '08 #1
6 6531
On Oct 20, 11:33*pm, santiago <santiago10...@aol.comwrote:
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
Oct 21 '08 #2
On 2008-10-21 02:55:48 -0400, ac******@gmail.com said:
On Oct 20, 11:33*pm, santiago <santiago10...@aol.comwrote:
>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

Oct 21 '08 #3
On Tue, 21 Oct 2008 08:29:08 -0400, santiago wrote:
On 2008-10-21 02:55:48 -0400, ac******@gmail.com said:
>On Oct 20, 11:33Â*pm, santiago <santiago10...@aol.comwrote:
>>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
Oct 21 '08 #4
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
Oct 21 '08 #5
On Oct 21, 8:29 am, santiago <santiago10...@aol.comwrote:
On 2008-10-21 02:55:48 -0400, acehr...@gmail.com said:
On Oct 20, 11:33 pm, santiago <santiago10...@aol.comwrote:
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?
Oct 21 '08 #6
This function call will work, assuming the element type is int:

std::transform(arraytot.begin(), arraytot.end(), arraydet.begin(),
arraytot.begin(), plus<int>());
Fraser.
Oct 21 '08 #7

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...
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...
4
by: Deniz Bahar | last post by:
Hello all, Often times programs in C have arrays used as buffers and shared among different sections of code. The need arises to have position indicators to point to different parts of an array...
5
by: Troy | last post by:
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
3
by: Newcomsas | last post by:
Hello, I'm trying to solve a problem with JS textbox array without success. I have two buttons in my page: PLUS and MINUS; at every click on PLUS a new textbox named 'dear' is generated. So, if...
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
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 =...
14
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be...
1
by: joor | last post by:
Hi I am a beginner and currently trying to create a small program. I seem to be stuck on the multiplication of their elements. Eg. I have an Array with 4 different prices for 4 different...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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
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,...

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.