-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingComputations.sh
More file actions
executable file
·61 lines (54 loc) · 1.21 KB
/
SortingComputations.sh
File metadata and controls
executable file
·61 lines (54 loc) · 1.21 KB
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
#!/bin/bash -x
function sortAscending(){
size=${#computArr[@]}
for (( i=0 ; i<size ; i++ ))
do
for(( j=$(($i+1)) ; j<size ; j++ ))
do
if [[ ${computArr[$i]} -gt ${computArr[$j]} ]]
then
temp=${computArr[$i]}
computArr[i]=${computArr[$j]}
computArr[j]=$temp
fi
done
done
echo "Array sorted in Ascending order : ${computArr[@]}"
}
function sortDescending(){
size=${#computArr[@]}
for (( i=0 ; i<size ; i++ ))
do
for(( j=$(($i+1)) ; j<size ; j++ ))
do
if [[ ${computArr[$i]} -lt ${computArr[$j]} ]]
then
temp=${computArr[$i]}
computArr[i]=${computArr[$j]}
computArr[j]=$temp
fi
done
done
echo "Array sorted in descending order : ${computArr[@]}"
}
read -p "Enter 3 numbers : " a b c
echo $a $b $c
#Store computations into dict
declare -A comput
comput['a+b*c']=$(( a + b * c ))
comput['a*b+c']=$(( a * b + c ))
comput['c+a/b']=$(( c + a / b ))
comput['a%b+c']=$(( a%b+c))
echo "Computations stored in dictionary"
#simulteneously store values into array
c=0
for i in ${!comput[@]}
do
echo "[$i] : ${comput[$i]}"
computArr[$c]=${comput[$i]}
(( c++ ))
done
#show stored values in array
echo "Array of computations = [ ${computArr[@]} ]"
sortDescending
sortAscending