The SDL Component Suite is an industry leading collection of components supporting scientific and engineering computing. Please visit the SDL Web site for more information....



IndexOfNearestArrayValue


Unit:SDL_dstruct
Class: none
Declaration: [1]function IndexOfNearestArrayValue (Value: integer; ArrOfValues: array of integer): integer;
[2]function IndexOfNearestArrayValue (Value: double; ArrOfValues: array of double): integer;{Pascal}
[1]int __fastcall IndexOfNearestArrayValue(double Value, const double * ArrOfValues, const int ArrOfValues_Size);
[2]int __fastcall IndexOfNearestArrayValue(int Value, const int * ArrOfValues, const int ArrOfValues_Size); {C++}

The function IndexOfNearestArrayValue finds the index of the array element of the array ArrOfValues which is closest to the value of the parameter Value. Since the parameter ArrOfValues is passed as an open array, the returned index is relative to the lowest actual index. Thus you have to add the value of the lowest array index in order to obtain the correct index.

Hint: The declaration of IndexOfNearestArrayValue in C++ slightly differs from the Pascal declaration (note the extra parameter ArrOfValues_Size which specifies the highest index of the ArrOfValues array).

Example: The following code assigns a value of 14 to the variable idx, since 13.98 (element 2 of the array, if counted from zero) is the nearest value to 13.9. In order to obtain the actual index you have to add the first index (low(arr1) = 12) to the returned index:
const
  arr1 : array [12..16] of double = (12.3, 13.4, 13.98, 15.9, 16.5);

...
...
idx := low(arr1)+IndexOfNearestArrayValue (13.9, arr1);
...
...



Last Update: 2012-Oct-20