# ArrayAvg

Returns the average of the values in the specified array.

### Syntax

    ArrayAvg(array)

##### array

Name of the array containing values you want to average.

### Example

    <!--- This example shows the use of ArrayAvg --->

    <!---------------------------------------------------------------------
    This following six lines of code keep track of the form fields that can
    be dynamically generated on the screen. It uses the Fieldnames variable
    with the ListLen function to determine the number of fields on the form.
    ----------------------------------------------------------------------->

    <CFSET FormElem = 2>
        <CFIF Isdefined("Form.Submit")>
            <CFIF Form.Submit is "More">
                <CFSET FormElem = ListLen(Form.Fieldnames)>
            </CFIF>
        </CFIF>

    <HTML>
    <HEAD>
    <TITLE>ArrayAvg Example</TITLE>
    </HEAD>

    <BODY>
    <H3>ArrayAvg Example</H3>
    <P>
    This example uses ArrayAvg to find the average of the numbers that you
    enter into an array.<BR>
    If you would like to enter more than two numbers press the <b>more</b>
    button.
    </P>

    <FORM ACTION="arrayavg.cfm" METHOD="post">

    <!---------------------------------------------------------------------
    The following code initially creates two fields and then adds fields
    if the user presses the MORE button. Note that FormElem is initialized to
    two at the beginning of this code to indicate that the form has two
    fields.
    ----------------------------------------------------------------------->

    <INPUT TYPE="submit" NAME="submit" VALUE="more">

    <table cellspacing="2" cellpadding="2" border="0">
    <CFLOOP index="LoopItem" from="1" to="#FormElem#">
        <tr>
            <CFOUTPUT>
                <th align="left">Number #LoopItem#</th>
                <td><INPUT TYPE="text" NAME="number#LoopItem#"></td>
            </CFOUTPUT>
        </tr>
    </CFLOOP>
    </table>

    <INPUT TYPE="submit" NAME="submit" VALUE="get the average">
    </FORM>

    <!--- create an array --->

    <CFIF IsDefined("FORM.submit")>
        <CFSET myNumberArray = ArrayNew(1)>
        <CFSET Count = 1>
        <CFLOOP index="ListItem" list="#Form.Fieldnames#">
             <CFIF Left(ListItem,3) is "Num">
                <CFSET myNumberArray[Count] = Val(Evaluate("number#Count#"))>
                <CFSET count = IncrementValue(Count)>
            </CFIF>
        </CFLOOP>

        <CFIF Form.Submit is "get the average">
            <!--- use ArrayAvg to get the average of the two numbers --->
            <P>The average of the numbers that you entered is
            <CFOUTPUT>#ArrayAvg(myNumberArray)#.</CFOUTPUT>
        <CFELSE>
            <CFOUTPUT>Try again. You must enter at least two numeric
    values.</CFOUTPUT>
        </CFIF>
    </CFIF>
    </BODY>
    </HTML>