Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
April 3rd, 2013, 12:59 AM
 Gogandantes919
Registered User

Join Date: Apr 2013
Posts: 1
Time spent in forums: 16 m 40 sec
Reputation 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
April 3rd, 2013, 03:05 PM
 LKBrwn_DBA
Contributing User

Join Date: Sep 2006
Posts: 816
Time spent in forums: 1 Week 18 h 15 m
Reputation Power: 388

Quote:
 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?
__________________

 Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Finding the Average