Hello,

I need help adding two arrays of different sizes [i.e. 3005 + 305 = 3310 ]. I am sorry but I do not have any code to post because I am completely lost on how to even go about starting this, so any hints or help on what to do with will be greatly appreciated.

Chris

Recommended Answers

All 9 Replies

If you mean matrix algegra then the two arrays have to be the same size.

But lets say you have two arrays

int A[] = {1,2,3,4,5};
int B[] = {2,3,4,5,6};

then array C[0] = A[0] + B[0], and C[1] = A[1] + B[1], etc.

I think you need to clarify your problem. It looks like you're simulating regular arithmetic by storing digits as array elements, and doing the math, but we have no way of being sure.

Sorry I probably should have been more specific with my question. The max array size for both arrays is 1000. The numbers in the array are entered by the user so I do not know what they are going to be. The only hint the teacher gave us was that you will need to use at least 2 for loops to add the two arrays together, so that is all I know on what has to be done in order to add 2 arrays of different sizes together.

>>so I do not know what they are going to be.
Count them as the user enters the numbers. Each time you enter a number increment the counter by 1.

>>you will need to use at least 2 for loops to add the two arrays together,
If array A contains 10 numbers and array B contains 7 numbers, then the first loop counts from 0 to 7 and the second loop only counts from 7 to 10 to get the numbers that are in A but not in B.

I think your teacher needs to further clarify the assignment. By your example, I see it as

arr1   3    0    0   5
arr2   0    3    0   5

sol    3    3    1   0

where adding the two fives in the lowest position represent adding the one's column of decimal values - carrying the ten to the next position.
This is not normal matrix math, this is simulation decimal arithmetic. But, I might be misinterpreting what's being asked of you.

Either way, I don't see two for loops being needed.

Or maybe

arr1 = 3    0    0    5       size=4
arr2=  3    0    5             size=3

// array sol of size 5 here

then we need the array solution as size 4+1. It will still be simulation of decimal addition, but in that case two loops might be required.

One that starts to add from the lowest 5's till the smaller array is exhausted and the other that simply copies the remaining elements of the larger array into the sol array.

Again the two loops may not be nested.

In your example just fill in the missing elements with 0 to make both arrays the same size, then do like vmanes mentioned, assuming that's what his teacher wants. And that would explain the need for two loops.

vmanes your example is accurate in what we are trying to accomplish. The using two for loops was only a suggestion by the teacher of how it could be done. So you are saying what we are trying to do is 'simulation decimal arithmetic', will I be able to google this and find examples that may help me in generating my own code that I can then post for help?

Better yet. Get out a piece of paper and a pencil (I know, that's so 20th Century). Try to describe/define what you think you need to do to solve this. What data do you need to get from user, how do you store it? Then, what steps would you do to solve it manually - step by step? Finally, how do you display the result?

This assignment should be applying recent material from your class - look to your notes and the text.

When you start coding, take it one part at a time. Get your data storage allocated. Then input into it. Get that working before going to next step.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.