Finding range of values in an array

I have an array containing distances in ascending order, for example:

distances = 100 120 150 170 200 280 300 ....

I have a number, let's say v = 170 and a variation value, let's say var = 100 . I want to return the array indexes for which the distances cover the range (v - var) to (v + var).

For example if var = 200, want to find the indexes in the array that cover

70 to 270

The result will be an array having values

result = 1 2 3 4 5 

I'm coding in Fortran but code in C is fine.

Unless there is something special about the input data, brute force is the most likely candidate

int *find_em(int *in, int *out, const int start, const int end)
{
     int i=0;
     while(*in < start) in++;
     while(*in <= end )
     {
         out[i++]=*in;
         in++;
     }
     out=-1  /* mark the end of legitimate values */
      return out;
}     

Data will be in ascending order, so I can just get the beginning and last index.

This is a very important propriety you should take advantage of. Use binary search to find the indexes. You could with one search located the lowest index and performs a second search on the remaining elements to find the highest index.

HTH, Lo�c