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. #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
    21,401
    Why not just perform a search of the array?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,401
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,401
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470
    I felt that there should be a better way.
    You can break out of the loop as soon as the first match found
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 06:45 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21