Finding range of values in an array


 
Thread Tools Search this Thread
Top Forums Programming Finding range of values in an array
# 1  
Old 11-29-2010
Finding range of values in an array

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

Code:
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

Code:
result = 1 2 3 4 5

I'm coding in Fortran but code in C is fine.
# 2  
Old 11-29-2010
Unless there is something special about the input data, brute force is the most likely candidate
Code:
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[i]=-1  /* mark the end of legitimate values */
      return out;
}

# 3  
Old 11-29-2010
Data will be in ascending order, so I can just get the beginning and last index.
# 4  
Old 11-30-2010
Quote:
Data will be in ascending order,
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create range of values and count values.

Gents, It is possible to generate a range of values according to column 1 and count the total of rows in the range. example input 15.3 15.5 15.8 15.9 16.0 16.1 16.8 17.0 17.5 18.0 output desired 15.0 - 15.9 = 4 (10 Replies)
Discussion started by: jiam912
10 Replies

2. Shell Programming and Scripting

Convert Column Values to a Range of Values

I have a list of columns with values that I need to transform into a row containing the range of each column. For example: "Column A" 1 2 3 4 10 12 14 15 16 17 18 "Column B" 1 4 5 6 (4 Replies)
Discussion started by: newbio
4 Replies

3. Shell Programming and Scripting

AWK Sorting with range values

Hello, I am looking for some help on GAWK script. I have a list of phone numbers as below. I need to sort these in the range of first 6 digits. 2402170338 2402170387 2402170478 2402170744 2403100025 2403100026 2403100027 2403100028 So for the above sample data, I require an output... (9 Replies)
Discussion started by: Diwakar9
9 Replies

4. Shell Programming and Scripting

finding file with a specific range

Hi All, Thanks in advance File is generated with following format 31000000.xml to 48999999.xml 74000000.xml to 88999999.xml Above range should be find and moved into the folder named abc and below is another range should should be find and moved into folder named xyz ... (1 Reply)
Discussion started by: sujit_kashyap
1 Replies

5. Programming

grepping a range of values

I need to return all records in a file starting with a row that says TABLE: <tabl name> lists of hexadecimal records TABLE: <some table> TABLe is a key word in the file. I know the name of the table I want to start with. I do not know the name of the table that I will end with. I just... (4 Replies)
Discussion started by: guessingo
4 Replies

6. UNIX for Advanced & Expert Users

Finding a specific range of character in file

hi, I want to store from 102 character to 128 character to a variable of header record which can be identified as 'HDR' which is the first 3 characters in the same line of a same.txt file. Please advise. Thanks (4 Replies)
Discussion started by: techmoris
4 Replies

7. Shell Programming and Scripting

Finding indices in an array nearest to a set of values

I have an two arrays. One array BINDIST consists of fences. I have another array XOFFS. Eg BINDIST = 0 10 20 30 40 50 60 XOFFS = 2 3 4 23 25 28 55 58 I want to find to find the indices of values in XOFFS that are closest to each BINDIST. My idea is to do as follows I create array... (7 Replies)
Discussion started by: kristinu
7 Replies

8. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

9. Shell Programming and Scripting

To Create range of values

Hi, I have a file with the below like values with integers only in sorted order (May or may not be in sequence) Eg: File1.txt ----------- 1 2 3 4 5 6 . . . . . 10000 My requirement here is to create a range of values out put to a temp k (4 Replies)
Discussion started by: shiva447
4 Replies

10. Shell Programming and Scripting

Finding Minimum value per Row range of data

Here is an example of a file I am working with: C 4704 CB 1318 ASP 115 BGRF 1 weak 0.0% 4.33 C 4720 OD 1322 ASP 115 BGRF 1 weak 0.0% 3.71 O 4723 OD 1322 ASP 115 BGRF 1 weak 0.0% 3.48 O 4723 CG 1321 ASP 115 BGRF 1 weak 0.0% 4.34... (3 Replies)
Discussion started by: userix
3 Replies
Login or Register to Ask a Question