Answered # Help with adding 2 arrays of different sizes

Ancient Dragon 5,243 vmanes 1,165 Discussion Starter buddha527 Ancient Dragon 5,243 vmanes 1,165 hammerhead 19 Ancient Dragon 5,243 Discussion Starter buddha527 vmanes 1,165 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

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.

0

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.

0

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.

0

>>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.

0

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.

0

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.

0

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.

0

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?

0

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.

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...