home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / nfsrc21.zip / AMEDIAN.PRG < prev    next >
Text File  |  1991-08-16  |  4KB  |  139 lines

  1. /*
  2.  * File......: AMedian.Prg
  3.  * Author....: Ralph Oliver,  TRANSCOM SYSTEMS
  4.  * CIS ID....: 74030,703
  5.  * Date......: $Date:   15 Aug 1991 23:05:22  $
  6.  * Revision..: $Revision:   1.1  $
  7.  * Log file..: $Logfile:   E:/nanfor/src/amedian.prv  $
  8.  * 
  9.  * This is an original work by Ralph Oliver and is placed in the
  10.  * public domain.
  11.  *
  12.  * This program uses the preprocessor #defines and #command in
  13.  * Aavg.prg by David Husnian.
  14.  *
  15.  * Modification history:
  16.  * ---------------------
  17.  *
  18.  * $Log:   E:/nanfor/src/amedian.prv  $
  19.  * 
  20.  *    Rev 1.1   15 Aug 1991 23:05:22   GLENN
  21.  * Forest Belt proofread/edited/cleaned up doc
  22.  * 
  23.  *    Rev 1.0   07 Jun 1991 23:03:20   GLENN
  24.  * Initial revision.
  25.  *
  26.  */
  27.  
  28.  
  29. /*  $DOC$
  30.  *  $FUNCNAME$
  31.  *     FT_AMEDIAN()
  32.  *  $CATEGORY$
  33.  *     Array
  34.  *  $ONELINER$
  35.  *     Find middle value in array, or average of two middle values
  36.  *  $SYNTAX$
  37.  *     FT_AMEDIAN( <aArray> [, <nStart> [, <nEnd> ] ] )
  38.  *                -> nMedian
  39.  *  $ARGUMENTS$
  40.  *     <aArray> is the array containing the elements to be averaged.
  41.  *
  42.  *     <nStart> is the first array element to include,
  43.  *     defaults to first element.
  44.  *
  45.  *     <nEnd> is the last array element to include,
  46.  *     defaults to last element.
  47.  *  $RETURNS$
  48.  *     The median average of the array elements
  49.  *  $DESCRIPTION$
  50.  *     This function sorts the elements of a numeric array and
  51.  *     then returns the value in the middle element of the sorted
  52.  *     array.  If there is no exact middle value, then it returns
  53.  *     the average of the two middle values.  Half of the elements
  54.  *     are > median and half are < median.  A median average may
  55.  *     more reflect a more useful average when there are extreme
  56.  *     values in the set.
  57.  *  $EXAMPLES$
  58.  *     FT_AMEDIAN( aArray )      // Return Median for entire array
  59.  *
  60.  *     FT_AMEDIAN( aArray, 2)    // Return Median for elements from 2 to end
  61.  *
  62.  *     FT_AMEDIAN( aArray, ,9)   // Return Median for 1st 9 elements
  63.  *
  64.  *     FT_AMEDIAN( aArray,8,40 ) // Return Median for elements 8 to 40
  65.  *  $END$
  66.  */
  67.  
  68. #ifdef FT_TEST
  69.  
  70. #include "directry.ch"
  71.  
  72. FUNCTION MAIN()
  73.    LOCAL var0, myarray0 := DIRECTORY(), myarray1 := {}
  74.    CLS
  75.    ? "TEST TO DEMONSTRATE EXAMPLES OF FT_AMEDIAN"
  76.    ?
  77.    AEVAL( myarray0, { |x| AADD( myarray1, x[ F_SIZE ]) } )
  78.    var0 := FT_AMEDIAN( myarray1 )
  79.    ? PADR('FT_AMEDIAN( myarray1 ) ->',35)
  80.    ?? var0
  81.    ?
  82.    var0 := FT_AMEDIAN( myarray1, 2 )
  83.    ? PADR('FT_AMEDIAN( myarray1, 2 ) ->',35)
  84.    ?? var0
  85.    ?
  86.    var0 := FT_AMEDIAN( myarray1, , 9 )
  87.    ? PADR('FT_AMEDIAN( myarray1, , 9 ) ->',35)
  88.    ?? var0
  89.    ?
  90.    var0 := FT_AMEDIAN( myarray1, 8, 40 )
  91.    ? PADR('FT_AMEDIAN( myarray1, 8, 40 ) ->',35)
  92.    ?? var0
  93.    ?
  94.    RETURN NIL
  95.  
  96. #endif
  97.  
  98.  
  99. #define FORCE_BETWEEN(x,y,z)         (y := MAX(MIN(y,z),x))
  100.  
  101. #command    DEFAULT <Param1> TO <Def1> [, <ParamN> TO <DefN> ] ;
  102.             => ;
  103.             <Param1> := IF(<Param1> == NIL,<Def1>,<Param1>) ;
  104.          [; <ParamN> := IF(<ParamN> == NIL,<DefN>,<ParamN>)]
  105.  
  106.  
  107. FUNCTION FT_AMEDIAN( aArray, nStart, nEnd )
  108.  
  109.    LOCAL nTemplen, aTemparray, nMiddle1, nMiddle2, nMedian
  110.  
  111.    DEFAULT nStart TO 1, ;
  112.            nEnd   TO LEN( aArray )
  113.  
  114.    // Make Sure Bounds are in Range
  115.    FORCE_BETWEEN(1, nEnd,   LEN( aArray ))
  116.    FORCE_BETWEEN(1, nStart, nEnd)
  117.  
  118.    // Length of aTemparray
  119.    nTemplen := ( nEnd - nStart ) + 1
  120.  
  121.    // Initialize aTemparray
  122.    aTemparray := ACOPY( aArray, ARRAY( nTemplen ), nStart, nTemplen )
  123.  
  124.    // Sort aTemparray
  125.    aTemparray := ASORT( aTemparray )
  126.  
  127.    // Determine middle value(s)
  128.    IF ( nTemplen % 2 ) == 0
  129.       nMiddle1 := aTemparray[ (nTemplen / 2) ]
  130.       nMiddle2 := aTemparray[ INT(nTemplen / 2) +1 ]
  131.       nMedian :=  INT( ( nMIddle1 + nMiddle2 ) / 2 )
  132.    ELSE
  133.       nMedian := aTemparray[ INT( nTemplen / 2 ) + 1 ]
  134.    ENDIF
  135.    
  136.    RETURN ( nMedian )
  137.  
  138.  
  139.