KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Sample Method for generating a percentile range from an Array
PRODUCT: 4D | VERSION: 14.0 | PLATFORM: Mac & Win
Published On: October 6, 2014

Below is a sample method to obtain from an array of numbers and array values in a percentile range.

//Declare Variables and Parameters
C_POINTER($1;$2)
C_LONGINT($3;$4;$size;$lowPos;$highPos;$counter)
C_REAL($lowPer;$highPer)
ARRAY LONGINT($arrayIn;0)
ARRAY LONGINT($arrayRes;0)

If (Count parameters=4)
 If($3<$4)
  $size:=Size of array($1->)

//Calculate Range of values
  $lowPer:=$3/100
  $highPer:=$4/100

  $lowPos:=($lowPer*$size)+1
  $highPos:=$highPer*$size

  COPY ARRAY($1->;$arrayIn)
  SORT ARRAY($arrayIn)

//Generate Array of values within percentile
  For ($counter;$lowPos;$highPos)
   APPEND TO ARRAY($arrayRes;$arrayIn{$counter})
  End for

  COPY ARRAY($arrayRes;$2->)

 End if
End if

Saving the method as Array_Percentile, an example using the method is shown below.

//Declare Variables
ARRAY LONGINT($array;60)
C_LONGINT($counter)
ARRAY LONGINT($arrayRes1;0)
ARRAY LONGINT($arrayRes2;0)
ARRAY LONGINT($arrayRes3;0)

//Generate sample data
For ($counter;1;60)
 $array{$counter}:=Mod(Random;100)
End for

Array_Percentile(->$array;->$arrayRes1;0;10)
Array_Percentile(->$array;->$arrayRes2;25;100)
Array_Percentile(->$array;->$arrayRes3;25;75)


The method above will result in the following:
-$arrayRes1 will contain the lower 10th percentile of values in $array
-$arrayRes2 will contain the upper 25th percentile of values in $array
-$arrayRes3 will contain the middle 50th percentile of values in $array which contains values greater than the lower 25th percentile and values lower than the upper 75th percentile.