Custom Search

Monday, 3 September 2012

Write a linux Shell Script to find Greatest and Smallest number in an Array, shell script for maximum & minimum value

Write a linux Shell Script to find Greatest and Smallest number in an Array, shell script for maximum & minimum value


Program
echo "please enter size of an array"
read n

#for loop for taking input from user
for((i=0;i<n;i++))
do
echo " enter $((i+1)) number"
read nos[$i]
done

#for loop for printing the entered number
echo "number entered are"
for((i=0;i<n;i++))
do
echo ${nos[$i]}
done

#main loop
small=${nos[0]}
greatest=${nos[0]}
for((i=0;i<n;i++))
do
#logic for smallest number
if [ ${nos[$i]} -lt $small ]; then
small=${nos[$i]}

#logic for greatest number
elif [ ${nos[$i]} -gt $greatest ]; then
greatest=${nos[$i]}
fi
done

#printing smallest and greatest number

echo "smallest number in an array is $small"
echo "greatest number in an array is $greatest"

No comments:

Post a Comment

Laptops