#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Finding the Average


    Going to print my script here as a start

    count=0
    count2=0
    total=0

    for factor in $*
    do

    count=$( echo "$factor" | grep "^[0-9]*$" | wc -l )
    count2=$( echo "$factor" | grep "^[-+][0-9]*$" | wc -l )

    if [ "$count" -eq 1 -o "$count2" -eq 1 ]
    then total=$(( total + $factor))
    else
    echo
    echo " '$factor' is not numeric. Factor is ignored."
    echo

    fi

    done

    echo " Result = $total"

    exit 0


    The goal of the program is that the user can enter in as many integers as possible, entering a non-integer unit will be ignored, and the integers are then added together for the 'total' variable. THEN, the total is divided by the amount of integers entered giving me the average.


    Currently the program only works with the addition and it only displays the total.



    What I am stuck on is.... How do I get the script to recognize HOW MANY integers were entered in order to know how much to divide from the total amount.

    Any help would be appreciated.
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    857
    Rep Power
    388

    Cool


    Originally Posted by Gogandantes919
    What I am stuck on is.... How do I get the script to recognize HOW MANY integers were entered in order to know how much to divide from the total amount.
    Perhaps you could count them?

IMN logo majestic logo threadwatch logo seochat tools logo