Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have to write a script where i need to show the sum of entered numbers and the biggest & smallest number among them. I
I have to write a script where i need to show the sum of entered numbers and the biggest & smallest number among them. I am using array. I have solved this with normal way. but when i am using array i am getting sum, biggest number but can get the lowest number.I understand its because of my if logic for smallest number.i couldn't think anyother way.. need help.
my script:
#!/bin/bash sum=0 small=0 big=0 echo "Please enter the number" while(( n != -99 )); do read -a n arr=${#n[@]} for((i=0;i<$arr;i++))do if [ ${n[$i]} -eq -99 ]; then break elif [ ${n[$i]} -ne -99 ]; then sum=$((sum + n[$i])) if [ ${n[$i]} -gt $big ]; then big=${n[i]} elif [ ${n[$i]} -le $small ]; then small=${n[i]} fi fi done done echo "Sum: $sum" echo "Highest: $big" echo "Lowest: $small"
output:
Please enter the number 12 13 14 -99 Sum: 39 Highest: 14 Lowest: 0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started