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

1. ## 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 = {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. 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. 3. 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. 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. 5. 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 fixed, function, instance, quick, size 