April 3rd, 2013, 12:59 AM
Join Date: Apr 2013
Time spent in forums: 16 m 40 sec
Reputation Power: 0
Finding the Average
Going to print my script here as a start
for factor in $*
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))
echo " '$factor' is not numeric. Factor is ignored."
echo " Result = $total"
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.