1

I have two arrays containing, each one, values of the coordinates. In other words, the first array contains the values of the x and the second array contains the values of the y. The goal consists of not having equal coordinates, which means that every coordinate must be different from all the others. I tried to do this:

for (i=0; i<len(lrs)-1; i++) {
    for (j=0; j<len(lrs) ; j++) {
        if ((pos.x[j]==pos.x[i+1])&&(pos.y[j]==pos.y[i+1]))
           printf("1");
    }
}

However, there's a moment where the values of the "j" and "i" are the same and, therefore, the condition is verified, which is not intended. Maybe I'm not thinking the right way, but I just can't figure it out.

4
  • 1
    Only check the condition if i != j and done.
    – user529758
    May 4, 2013 at 16:38
  • check if(i != j) condition
    – ridoy
    May 4, 2013 at 16:40
  • If the number of elements is large this is very inefficient.
    – ouah
    May 4, 2013 at 16:42
  • If your values are float or double (and according to source of data), comparing them this way may be inacurate. May 4, 2013 at 16:53

1 Answer 1

3

It is better to make inner loop for j > i only:

for (i=0; i<len(lrs); i++) {
    for (j=i+1; j<len(lrs) ; j++) {
        if ((pos.x[j]==pos.x[i])&&(pos.y[j]==pos.y[i]))
           printf("1");
    }
}

In this case you will never check condition i==j. More over you will check each pair only once.

1
  • Thank you, that's exactly what I wanted!
    – Raphm
    May 4, 2013 at 17:11

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.