# when an int is not equal to any in a variable group of ints

This is a discussion on when an int is not equal to any in a variable group of ints within the C Programming forums, part of the General Programming Boards category; Hello, In my program I work with a number of 3D objects that move around in space. All these objects ...

1. ## when an int is not equal to any in a variable group of ints

Hello,

In my program I work with a number of 3D objects that move around in space. All these objects are labeled with an index i (within an appropriately defined struct Obj[i] ). For any given element i, I have an algorithm that returns an N-dimensional array of ints I[], which tells me the labels of the N nearest neighbors to Obj[i].

Now, given another object label k, I need to do something if Obj[k] *is not* one of the nearest neighbors of Obj[i]. Naively, it would be something like:

Code:
`if ( (k != I[0]) && (k != I[1]) && ... && (k != I[N-1]) );`
First question: would several "&&" within an "if" statement work as I think they would?

Second issue: Even if the several "&&" work OK, my big problem is that N changes (it is set by the user). Thus, how can I code the above without knowing the value of N ahead of time?. Moreover, because I have a program with runtime graphical output (OpenGL), I need this to be as fast and efficient as possible.

A workaround would be to consider a maximum number of nearest neighbors Nmax and write the "if" statement for this max number. Then, make the first N entries equal to the actual entries of I[N], and the rest, say, a negative int.

However, this feels like the kind of solution someone like me (i.e. with limited knowledge of C) would come up with :-) (and it is certainly not fast...)

Can anyone suggest a proper way to achieve this?

Thanks a lot,

mc61

2. Why not just perform a search of the array?

3. laserlight,

Thanks for your reply. You mean with a "for" loop of some kind? I thought of something like:

Code:
```flag = 0;
for (  j=0; j<N; j++ )
{
if ( k == I[j] ) flag = 1;
}```
So, if flag is still 0 at the end of the loop, then Obj[k] is not one of the nearest neighbors...

This would work, but again I felt that there should be a better way.

Thanks,

mc61

4. This would work, but again I felt that there should be a better way.
If the array is sorted then you can use binary search, otherwise linear search (i.e., what you wrote) is the best you can do. Incidentally, once you have set the flag to 1 you can break out of the loop.

5. They array is not sorted, but it could be. Could you tell me what "binary search" is?

Thanks again,

mc61

6. They array is not sorted, but it could be.
Then you cannot rely on it being sorted, so you should just use linear search.

Could you tell me what "binary search" is?
Search the friendly web

7. I felt that there should be a better way.
You can break out of the loop as soon as the first match found

8. laserlight and vart,

Thank you guys. It seems that for my circumstances there may not be a better way to do this (and I will definitely break the loop after the first match).

Originally Posted by laserlight
Search the friendly web
Got it. Will do that :-)

Thanks again,

mc61