Thursday, December 15, 2022

Find highest and lowest number in an unsorted array using bash script

 #!/usr/bin/bash
#declare -a sports=([0]=football,[1]=cricket,[2]=hockey,[3]=basketball)
#sports[0]=foorball
#sports[1]=cricket
#sports[2]=hockey
#sports[3]=basketball

#echo "${sports[@]}"
echo "Please enter 5 marks of 5 students"
#for i in $(seq 0 4)
for i in {0..4}
do
read numbers[$i]
done
((max=${numbers[0]}))
((min=${numbers[0]}))
#echo "max is $max"
#echo "min is $min"
#echo "${numbers[@]}"
for i in ${numbers[@]}
do
if [ $i -gt $max ]
then
max=$(($i))
fi
if [ $i -lt $min ]
then
min=$(($i))
fi
done

echo "The highest number in the array is $max"
echo "The smallest number in the array is $min"

No comments:

Post a Comment