two arrays

I have two c style arrays that are passed in and added together

My issue is that if, for example, x = 111 and y = 2, the sum ends up being 211 instead of 112.

I have though of somehow adding zeros to the beginning of y temporarily so the indexes line up and are able to add but I am not sure how effecting that'd be.

Any advice?
Last edited on
Maybe start at the end of the arrays and sum the digits in reverse order?
I tried changing it to decrement instead of increment but still got the same result

1
2
3
4
//changed line 21 to:

    for(int i = max - 1; i >= 0; i--)
Last edited on
Each of your arrays have different maximums, though, right? So you'd need to have two different counters: one to keep track of which digit you are on in each array.
Line 54 should be sum.number[sum.currentLength] = 1;. In other words, set it to the value 1, instead of the ASCII value of the character '1'.

Also at line 54, you need to check if you have to resize sum.

Finally, the code will be faster if you change line 41 to currentSum -= 10. Since it can never be more than 19, this is equivalent to the modulo function.
Topic archived. No new replies allowed.