Thread: Quick method for finding where a value fits in an integer array of fixed size??

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    91

    Quick method for finding where a value fits in an integer array of fixed size??

    Hello...
    Can someone tell me other than doing a bunch of conditional if statements is there a quick way to have a function take an integer argument then compare it to integers within a fixed array size to see which element is the largest one just greater than the value?

    For instance:
    Code:
    d[7] = {1, 10 32, 64, 100, 256, 1000, 1024}
    parameter passed in 87....Want function to return 64 in this instance
    This is in C

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In your example, the array is sorted. For actual input, is the array guaranteed to be sorted? If it is, then a simple loop from the first to the last element, stopping at the first element that satisfies (or does not satisfy, as the case may be) the condition, will do. If the array could be much larger, then it would make sense to do a binary search for this point (i.e., rather than binary searching for an element with the exact given value, you binary search for the largest element that does not exceed the given value).

    If the array is not guaranteed to be sorted, you could sort it (e.g., with qsort) then apply the above techniques.
    Last edited by laserlight; 10-26-2020 at 05:28 PM.
    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
    Mar 2020
    Posts
    91
    Yes that is a constant array of constant size and a loop sounds good....was thinking there was some simple 'function' I could call

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, I'm assuming that it was merely for illustration, but you should be aware that besides being syntactically incorrect due to the omission of the type and the missing semi-colon, your code snippet appears to state that the array has 7 elements when it was populated by an aggregate with 8 elements.
    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
    Sep 2020
    Posts
    425
    There is no simple function that will do this in the standard C libraries. One wouldn't be hard to write though, but your requirements are a bit vague (e.g. what to do if the list is empty).

    And isn't 100 the entry that is just greater than 87, not 64?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bsearch() isn't finding an integer key in my array.
    By MilleniumFalcon in forum C++ Programming
    Replies: 20
    Last Post: 03-22-2014, 04:26 PM
  2. Use vector instead of fixed-size array
    By clegs in forum C++ Programming
    Replies: 19
    Last Post: 09-16-2007, 09:00 PM
  3. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  4. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 03:51 PM
  5. Finding the Mode of an integer array
    By -]SC[-Dargoth in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 02:27 PM

Tags for this Thread