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

  1. /*
  2.  * File......: AAvg.Prg
  3.  * Author....: David Husnian
  4.  * Date......: $Date:   15 Aug 1991 23:04:54  $
  5.  * Revision..: $Revision:   1.2  $
  6.  * Log file..: $Logfile:   E:/nanfor/src/aavg.prv  $
  7.  * 
  8.  * This is an original work by David Husnian and is placed in the
  9.  * public domain.
  10.  *
  11.  * Modification history:
  12.  * ---------------------
  13.  *
  14.  * $Log:   E:/nanfor/src/aavg.prv  $
  15.  * 
  16.  *    Rev 1.2   15 Aug 1991 23:04:54   GLENN
  17.  * Forest Belt proofread/edited/cleaned up doc
  18.  * 
  19.  *    Rev 1.1   14 Jun 1991 19:50:38   GLENN
  20.  * Minor edit to file header
  21.  * 
  22.  *    Rev 1.0   01 Apr 1991 01:00:20   GLENN
  23.  * Nanforum Toolkit
  24.  *
  25.  */
  26.  
  27.  
  28. /*  $DOC$
  29.  *  $FUNCNAME$
  30.  *     FT_AAVG()
  31.  *  $CATEGORY$
  32.  *     Array
  33.  *  $ONELINER$
  34.  *     Average numeric values in an array
  35.  *  $SYNTAX$
  36.  *     FT_AAVG( <aArray> [, <nStartIndex> [, <nEndIndex> ] ] ) -> nAverage
  37.  *  $ARGUMENTS$
  38.  *     <aArray> is the array containing the elements to be averaged.
  39.  *
  40.  *     <nStartIndex> is the first array item to include,
  41.  *     defaults to first element.
  42.  *
  43.  *     <nEndIndex> is the last array element to include,
  44.  *     defaults to all elements.
  45.  *  $RETURNS$
  46.  *     The average of the specified array elements.
  47.  *  $DESCRIPTION$
  48.  *     This function is used to get a numeric average of selected or all
  49.  *     elements of an array.
  50.  *
  51.  *     This routine requires FT_ASUM().
  52.  *  $EXAMPLES$
  53.  *     FT_AAVG(aSubTotals)          // Get Average of Entire Array
  54.  *
  55.  *     FT_AAVG(aSubTotals, 5)       // Get Average of 5th Element On
  56.  *
  57.  *     FT_AAVG(aSubTotals, , 10)    // Get Average of 1st 10 Elements
  58.  *
  59.  *     FT_AAVG(aSubTotals, 5, 10)   // Get Average of Elements 5-10
  60.  *  $END$
  61.  */
  62.  
  63. #define FORCE_BETWEEN(x,y,z)         (y := MAX(MIN(y,z),x))
  64. #define IS_NOT_ARRAY(x)              (VALTYPE(x) != "A")
  65.  
  66. #command    DEFAULT <Param1> TO <Def1> [, <ParamN> TO <DefN> ] ;
  67.             => ;
  68.             <Param1> := IF(<Param1> == NIL,<Def1>,<Param1>) ;
  69.          [; <ParamN> := IF(<ParamN> == NIL,<DefN>,<ParamN>)]
  70.  
  71.  
  72.          
  73. FUNCTION FT_AAVG(aArray, nStartIndex, nEndIndex)
  74.  
  75.    DEFAULT nStartIndex TO 1, ;
  76.            nEndIndex   TO LEN(aArray)
  77.  
  78.    // Make Sure Bounds are in Range
  79.  
  80.    FORCE_BETWEEN(1, nEndIndex,   LEN(aArray))
  81.    FORCE_BETWEEN(1, nStartIndex, nEndIndex)
  82.  
  83.    RETURN (IF(IS_NOT_ARRAY(aArray) .OR. LEN(aArray) == 0, ;
  84.               0, ;
  85.               FT_ASUM(aArray, nStartIndex, nEndIndex) / ;
  86.                  (nEndIndex - nStartIndex + 1)))
  87.