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