April 2nd, 2013, 11:59 PM
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.
April 3rd, 2013, 02:05 PM
Perhaps you could count them?
Originally Posted by Gogandantes919