0

I have two arrays of different sizes that I am trying to loop through and perform calculations on, but I am ending up with incorrect outputs.

Here are the arrays:

int[] array1 = [5, 10, 2]

int[] array2 = [11, 23, 4, 6, 5, 8, 9]
    int[] array1 = {5, 10, 2};
    int[] array2 = {11, 23, 4, 6, 5, 8, 9};
    ArrayList<Integer> calculationsArray = new ArrayList<Integer>();
    int calculations = 0;

    for(int i = 0; i < array1.length; i++) {
        for(int j = 0; j < array2.length; j++) {
        calculations = ((array1[i] + array2[j]) % 10);
    }
    calculationsArray.add(calculations);
    }

I expect the output of [6, 3, 6, 1, 0, 8, 1]. Example calculations for 4 loops would be:

  1. (11 + 5) % 10 = 6
  2. (23 + 10) % 10 = 3
  3. (4 + 2) % 10 = 6
  4. (6 + 5) % 10 = 1

But the actual output is: [4, 9, 1]

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Have you tried using a debugger to see where is the error? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – bracco23 Jun 26 '19 at 15:49
  • How are you getting 7 numbers in the expected output? Wouldn't there be 21 outputs? – Nexevis Jun 26 '19 at 15:51
  • Did you mean for the fifth element in the output to be 5 because (10 + 5) % 10 = 5? – templatetypedef Jun 26 '19 at 15:57

2 Answers2

2

Let's first talk about why you're seeing what you're seeing.

I've reposted your code here, with the indentation changed to reflect the nesting:

for(int i = 0; i < array1.length; i++) {
    for(int j = 0; j < array2.length; j++) { // <--- This loop
        calculations = ((array1[i] + array2[j]) % 10);
    }
    calculationsArray.add(calculations);
}

Let's take the inner loop, the one that I've marked above. Notice that on each iteration of the loop, you set calculations to a new value. This overwrites the value that was computed earlier, so that when the inner loop finishes running, the value of calculations will be equal to the last value that's computed in that loop. And that accounts for what you're actually seeing, because

  • when i = 0, the inner loop's last calculation is (array1[0] + array2[6]) % 10, which is (5 + 9) % 10 = 4,
  • when i = 1, the inner loop's last calculation is (array1[1] + array2[6]) % 10, which is (10 + 9) % 10 = 9,
  • when i = 2, the inner loop's last calculation is (array1[2] + array2[6]) % 10, which is (2 + 9) % 10 = 1.

And hey, look! There's your [4, 9, 1] that you're seeing.

The next question is how to go about fixing this. The major issue I believe that you're running into here is the fact that doubly-nested loops probably isn't the way to go here. Specifically, a double for loop here will run array1.length × array2.length times, which is way more than the number of times that you need it to run.

So a first question - how many times should the loop run? Well, you want your output array to have the length of the longer of the two input arrays, which is Math.max(array1.length, array2.length) times. Could you make a single loop that counts up to that number?

You then need to handle the fact that your loop may need to cycle through each input array multiple times. In the example you've given, you'll read the values of array1 multiple times because array2 is longer, but you just as easily could have had to read the values of array2 multiple times if array1 were longer.

As a hint for how to do this, see if you can use the mod operator % to wrap around when the indices get too big.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

What about somenthing like (not optimized):

ArrayList<Integer> calculationsArray = new ArrayList<Integer>();
int calculations = 0;
int j = 0;
while (j < array2.size()) {
     for (int i = 0; i < array1.size(); i++) {
           calculations = ((array1.get(i) + array2.get(j)) % 10);
          calculationsArray.add(calculations);
     }
     j++;
}
  • I don't believe that this will produce the output the OP wants. They'd like an array with seven elements for the example that they've posted, but your code will produce one with 21 elements. Additionally, I don't believe that this would be consistent with the calculations outlined in their question. – templatetypedef Jun 26 '19 at 16:05