-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayOne.sh
executable file
·80 lines (76 loc) · 1.48 KB
/
arrayOne.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
readonly range=$((999-100+11))
counter=0
i=1
#function to generate three digit random number
function genThreeDigitNumber(){
echo "$(($((RANDOM%range))+100))"
}
#function to find second largest number from given array
function secondLargest(){
local first=0
local second=0
local INT_MIN=0
local arr=("$@")
local size=$(( ${#arr[@]} ))
if (( size<2 ))
then
echo "invalid input"
return
fi
for (( x=0 ; x<size ; x++ ))
do
if (( ${arr[$x]}>first ))
then
second=$(( first ))
first=${arr[$x]}
elif (( ${arr[$x]}>second )) && (( ${arr[$x]}!=first ))
then
second=${arr[$x]}
fi
done
if (( second==INT_MIN ))
then
echo "No second largest number element found"
else
echo "Second largest number is:" $second
fi
}
#function to find second smallest value from given value
function secondSmallest(){
local first=999
local second=999
local INT_MIN=999
local arr=("$@")
local size=$(( ${#arr[@]} ))
if (( size<2 ))
then
echo "invalid input"
return
fi
for (( y=0 ; y<size ; y++ ))
do
if (( ${arr[$y]}<first ))
then
second=$(( first ))
first=${arr[$y]}
elif (( ${arr[$y]}<second )) && (( ${arr[$y]}!=first ))
then
second=${arr[$y]}
fi
done
if (( second==INT_MIN ))
then
echo "No second smallest number element found"
else
echo "Second smallest number is:" $second
fi
}
while (( $i<=10 ))
do
num[((counter++))]=$( genThreeDigitNumber )
(( i++ ))
done
echo "Random number:"${num[@]}
secondLargest ${num[@]}
secondSmallest ${num[@]}