Function to Compute Average of the Elements in an Array

Last reviewed: June 27, 1995
Article ID: Q106700
The information in this article applies to:
  • Microsoft FoxPro for Windows, versions 2.5, 2.5a, and 2.5b
  • Microsoft FoxPro for MS-DOS, versions 2.0, 2.5, 2.5a, and 2.5b

SUMMARY

The user-defined function (UDF) shown below uses a DO WHILE loop to sum the elements of the passed array and then divides the total by the number of elements in the array to obtain the average.

MORE INFORMATION

   CLEAR
   SET CURSOR OFF
   SET TALK OFF
   DECLARE TheArray[10]
   TheArray[1]  = 1
   TheArray[2]  = 2
   TheArray[3]  = 3
   TheArray[4]  = 4
   TheArray[5]  = 5
   TheArray[6]  = 6
   TheArray[7]  = 7
   TheArray[8]  = 8
   TheArray[9]  = 9
   TheArray[10] = 10
   TheAvg =Aavg(@TheArray,10)
   WAIT WINDOW "The average of the array is " + ALLTRIM(STR(TheAvg,8,2))
   RETURN


**********************************************************************
   * Function.....: AAVG
   * Notes........: This function computes the average of the elements
   *                in an array.
   * Parameters:    Aname     - The name of an array containing
   *                            numeric elements to average.
   *                Aelements - The numeric of elements to average.
**********************************************************************
   FUNCTION AAVG
   PARAMETERS Aname, Aelements
   STORE 0 TO Atotal, Avg
   STORE 1 TO Acount
   DO WHILE Acount <= Aelements
       Atotal     =  Atotal + Aname[Acount]
       Acount  =  Acount + 1
   ENDDO
   RETURN(Atotal/Aelements)
* To return the sum of all the elements in the array, use RETURN(Atotal).


Additional reference words: FoxDos FoxWin 2.00 2.50 2.50a 2.50b
KBCategory: kbprg kbcode
KBSubcategory: FxprgGeneral


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 27, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.