-3

I've got a project that requires me to write a method to sum two integer arrays into a single integer array. The requirements are that each value of the array may only have one numeral in it. For example "1" but not "11" and the original two arrays may be different lengths. The idea behind the project is that these arrays are supposed to have lengths longer than the allowed length for an integer variable. We are supposed to create this new array so we can print out each position, essentially doing math with large numbers without using longs. This is what I currently have.

public static int[] add ( int[] arr1, int[] arr2) {
    int length;
    if (arr1.length >= arr2.length) {
        length = arr1.length;
    }
    else {
        length = arr2.length;
    }
    int[] smallAr = new int [length];
    int[] bigAr = new int [length];
    if (arr1.length > arr2.length) {
        for (int i = 0; i < length; i++) {
            smallAr[i] = arr2[i];
            bigAr[i] = arr1[i];
        }
    }
    else {
        for (int i = 0; i < length; i++) {
            smallAr[i] = arr1[i];
            bigAr[i] = arr2[i];
        }
    }

    int[] addition = new int[length];
    for (int i = 0; i < length; i++) {
        if (i > smallAr.length -1) {
            addition[i] = bigAr[i];
        }
        else {
            addition[i] = bigAr[i] + smallAr[i];
        }
    }
    return addition;
}

This currently gives an out of bounds error when ran. I also understand that this would not properly work if the array needed an extra space for a carry. Any help on this would be great!

smac89
  • 39,374
  • 15
  • 132
  • 179
  • *"each value of the array may only have one integer"* That makes no sense at all. Of course each array position can have only one value!! Please re-phrase. – Andreas Mar 26 '18 at 01:01
  • Why do you copy the values. There is no need for that. Throw that code away and re-think what you're doing. --- And of course the code fails. Both copy loops will iterate the full `length`, so it will exceeds the length of the smaller array. – Andreas Mar 26 '18 at 01:03
  • I have updated to give a little bit more insight into the problem. – Quinton Wallenfang Mar 26 '18 at 01:08
  • Duplicate of https://stackoverflow.com/questions/21691963/how-to-add-two-different-sized-arrays – A J Mar 26 '18 at 01:17
  • @QuintonWallenfang This is where a good example would set all straight. If I understand you now, you want to add two numbers, where the *digits* of the numbers are stored in an array. E.g. you want to add `{ 9, 4, 7 }` and `{ 8, 5 }` to give `{ 1, 0, 3, 2 }`, just like you do on paper, where digits are added from the right, carry-over when sum of digits are more than 9, and result can be longer than longest input. Is that correct? If so, please edit question and show example, to **clarify** your question. – Andreas Mar 26 '18 at 08:07
  • @QuintonWallenfang Also, if I am correct, then your code is far from the goal. Especially the "digits are added **from the right**" part, since your code adds from the left, which is of course entirely wrong. And you don't do any **carryover** logic at all, i.e. you don't check digit sum for being greater than 9. Please throw away your code, and try again, paying attention to the above, and using what you've learned from answer by Elliott, where you don't need to copy arrays first. Then create a new question if you still have trouble. – Andreas Mar 26 '18 at 08:12

1 Answers1

1

Step one, create a new array of sufficient length - find the maximum of arr1.length and arr2.length. Step two, assign the sum of each valid index from the two arrays to the output array - default the value to zero if the array length is greater than or equal to the current index. Step three, return the new array. Like,

public static int[] add(int[] arr1, int[] arr2) {
    int[] r = new int[Math.max(arr1.length, arr2.length)];
    for (int i = 0; i < r.length; i++) {
        r[i] = (i < arr1.length ? arr1[i] : 0) + (i < arr2.length ? arr2[i] : 0);
    }
    return r;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    Although that is a very fine implementation of the code for the project assignment *(so nice of you to do OPs assignment)*, it doesn't explain to OP what is wrong with the code in the question. – Andreas Mar 26 '18 at 01:05
  • Thanks for the help! I have updated the original question because I wasn't as specific as I needed to be. FIrst post on here so kind of just getting my head around it. – Quinton Wallenfang Mar 26 '18 at 01:14