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

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    31

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why not just perform a search of the array?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    31
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    31
    They array is not sorted, but it could be. Could you tell me what "binary search" is?

    Thanks again,

    mc61

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I felt that there should be a better way.
    You can break out of the loop as soon as the first match found
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    31
    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).

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

    Thanks again,

    mc61

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM