The problem statement, all variables and given/known data:
Two problems I need solving please. I created a script where the user types in 7 numbers as standard input and each one is then stored in an array. Now I need to perform the following calculations on those numbers:
1) Use a while loop to determine the largest number in the range.
2)Use an If statement to calculate the median within the range.
Please provide each suggestion with the simplest command structure possible.
Many thanks for your help!
Relevant commands, code, scripts, algorithms:
If statement for question 2
While statement for question 1
-gt option
{arr[0]} array
The attempts at a solution (include all code and scripts):
1)
While [{arr[0]} -gt {arr[1-6]}
do
Done
2)
Sort {arr[0-6]}
#Absolutely stuck after sort
Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Canterbury Christ Church University
Canterbury, Kent
United Kingdom
Professor name: Richard Henson
Course name: Operating Systems Assignment 1
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
declare -a num
num=("3" "4" "1" "8" "2" "9" "7")
limit="`echo ${#num[@]}`"
check=0
i=0
while [ $i -lt $limit ]
do
if [ $check -lt ${num[$i]} ]; then
check="`echo ${num[$i]}`"
fi
let i=i+1
done
echo "Largest number is:$check"
Hi man. Many thanks for writing that long script but I must refuse to use it. It is far too complex and I would greatly like understand what scripts im adding to my assignment. Please give me a more simplified answer so I may interpret, understand it and then implement it. Thanks for your help again
@javahater / @jjb1989
I am not here to get marks on my scripting skills... they are doing fine for me.
As i already said this was just a quick draft .. so i just kept adding lines rather than trying make it a beautiful looking small script. In my days there was no internet for help .... ahaha
idea is to give u a direction and u choose ur own path / scripts.
I don't write pure bash and I don't think we have seen your code to populate the array. Might be worth posting that script.
Remember that array elements are numbered from zero. In your case zero to six (i.e. seven elements).
Here's a bit of test code to get you started on how to walk the array and how to refer to the elements in shell script. You will need to change variable names to match the rest of your script and maybe change "echo" to "printf" depending on what you have been taught.
The idea is to build your script in simple stages and test each stage.
Once you can read back you array of the seven numbers the user typed you are in a position to insert code to do more (like find the highest value). Finding the median will be interesting.
echo "Read back test array in the same order it was created"
counter=0
counter_max=6
while [ ${counter} -le ${counter_max} ]
do
echo "Array element ${counter} = ${num_array[${counter}]}"
counter=$((counter+1))
done
Wow, some of the above examples defy simplicity to the nth+1 degree.
#!/bin/bash
let count=0
let largest=-999999
while read input; do
let number[$count]=$input;
if [ $input -gt $largest ] ; then
let largest=$input;
fi;
let sum=sum+number
let count=count=+1
done
The trouble I have difficulty with is "finding median with an if statement". BASH doesn't deal with floating point, so I'm not sure how to accomplish this. We could use sort but that would obviate the need for an if clause. We could iteratively throw away the highest and lowest values, but that's a lot of work (computationally).
If we can assume that the seven numbers contain no duplicates, the median is the number with exactly 3 numbers larger than it. This entails a double while loop and at least 2 if's, but no floating point is needed.