Shell program to find second smallest number in array
Answers
Answer:
#! /usr/bin/bash
# ask user to enter number of elements in array
echo "Enter number of elements in array"
read n
# declare array
declare -a arr
# ask user to enter elements in array
echo "Enter elements in array"
for ((i=0;i<n;i++))
do
read arr[i]
done
# find second smallest number in array using a loop
smallest=${arr[0]}
secondsmallest=${arr[0]}
for ((i=0;i<n;i++))
do
if [ ${arr[i]} -lt $smallest ]
then
secondsmallest=$smallest
smallest=${arr[i]}
elif [ ${arr[i]} -lt $secondsmallest ] && [ ${arr[i]} -ne $smallest ]
then
secondsmallest=${arr[i]}
fi
done
echo "Second smallest number in array is $secondsmallest"
Explanation:
The program first ask the user to enter the size of the array. Then it ask the user to enter the elements of the array.
After that a loop traversal is made to find the second smallest element.
Mark me brainliest
