0

I'm working in java and having some trouble to formulate my loop properly for the task I need. I want to a add elements to a 3 element array so that these conditions are fulfilled:

array_new[0] = f(array_old[0], array_old[1])
array_new[1] = f(array_old[0], array_old[2])
array_new[2] = f(array_old[1], array_old[2])

Here array_new is the array I wish to fill up using my loop, array_old is an existing array that supplies arguments to f, which is a previously defined method. Please note here f(a,b)=f(b,a), and that I don't want repeated arguments nor repeated outputs: so no f(a,a) and no f(b,a) given that f(a,b) has already been reached in the loop. All of these individual pieces have been defined and work properly, my issue is just getting the logic of the loop right. This is what I initially tried:

for (int i=0; i<array_old.length; i++) {
for (int j=0; j<array_old.length; j++) {
for (int k=0; k<array_old.length; k++) {
    
    if (i<j) {
    array_new[k]=f(array_old[i], array_old[j]);
    }
}
}
}   

However I realised this updates array_new[0] against my wishes: first it sets array_new[0] = f(array_old[0], array_old[1]) as desired, but then it sets array_new[0] = f(array_old[1], array_old[2])

I've tried messing with the indices in a few different ways but still there always seems to be at least one repeated or incorrect value. I'd appreciate any help here

2
  • Are the arrays always of length 3? If they're not variable, I wouldn't use a loop.
    – soothsooth
    Commented Apr 3, 2021 at 15:10
  • @soothsooth For my purposes they will be variable, I'm just using length 3 to get a feel for the logic and test my loop.
    – Mike
    Commented Apr 3, 2021 at 15:12

2 Answers 2

0

This can be implemented in the following way, the size of arr_new can be calculated as a sum of progression from 1 to n - 1, where n is the length of arr_old:

int[] arr_old = {1, 2, 3};
int n = arr_old.length;

int[] arr_new = new int[n * (n - 1) / 2];
for (int i = 0, k = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++, k++) {
        arr_new[k] = f(arr_old[i], arr_old[j]);
    }
}

System.out.println(Arrays.toString(arr_new));

Assuming that f prints its arguments and returns a sum:

static int f(int a, int b) {
    System.out.println(a + " " + b);
    return a + b;
}

The output is as follows:

1 2
1 3
2 3
[3, 4, 5]
2
  • This is absolutely perfect, thank you so much
    – Mike
    Commented Apr 3, 2021 at 15:20
  • Can I ask though, how did this formulation come so quickly to you, and how would you advise someone new to programming to help them spot the correct path in situations like this?
    – Mike
    Commented Apr 3, 2021 at 15:32
0

The first thing is characterise the size of the new array - for n > 3, it will be (much) larger than the input array. You're looking for every combination of two indices into the old array, where order doesn't matter, so the first step is

 new_array <- array of size (n choose 2) = 0.5*n*(n-1)

Then you need to generate all the pairs of numbers, in an orderly fashion. I would use a doubly nested for loop, with a counter recording the current index in the new array:

 int p = 0
 for (i = 0; i < n; ++i) {
      for (j = i + 1; j < n; ++j) {
           new_array[p] = f(old_array[i], old_array[j])
           p = p + 1
      }
 }

Note how by setting initialising j correctly, we avoid wasted iterations and comparisons.

1
  • Thank you, this is really helpful. At least I can say I had already figured out I was creating an array of size (n choose 2), but your loop is very concise and well explained
    – Mike
    Commented Apr 3, 2021 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.