|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
|
|
#1
|
|||
|
|||
|
how do you get 10 inputs into an array ?
very simple task, but hard concept to grasp.
im supposed to grab 10 inputs / integers from the user. what i did was this: echo "Please enter 10 scores" read -a scores ... but when i try to echo out the array scores, it only shows the first input. i don't think other 9 inputs are recorded. what am i doing wrong? echo $scores ...is the cmd i am using to display them. but it only shows the first input. can someone help plz? -ivalice |
|
#2
|
|||
|
|||
|
try changing your read to:
read score1 score2 score3 score4 score5 score6 score7 score8 score9 score10 echo $score1 echo $score2 echo $score3 echo $score4 echo $score5 echo $score6 echo $score7 echo $score8 echo $score9 echo $score10 |
|
#3
|
|||
|
|||
|
nevermind. i got it. i think it was jim or someone that posted this website:
http://www.tldp.org/LDP/abs/html/internal.html#EX33 it proves to be very useful. now if someone can tell me how i can sort these numbers in descending order? the instructor told me to use a concept called 'bubble sort', but i don't know how. i'm thinking if i can someone get those numbers into a file, and sort them with option (-d), and then bring it back into the array. i also need help calculating the sum and the average! |
|
#4
|
|||
|
|||
|
Quote:
yeah thanks. i would of done that, but i re-read the question, and it states "at least" . which is a problem.since it has to be at least 10 inputs, but unlimited in terms of maximum. thank you all the same. its hard to get a reply quickly here ![]() (especially when you are on a deadline!) -ivalice |
|
#5
|
|||
|
|||
|
okay, for the average of the array, i am trying to use 'bc'. the built-in calculator. so far i have this:
i=0 sum=0 while [ $count -ne 0 ] do sum=`expr ${scores[i]} + $sum` i=`expr $i + 1` count=`expr $count - 1` done echo the sum is equal to $sum ... this will get me the sum of the numbers inputted. how can i use 'bc' so that i can find the average ? i'm not very experienced with piping, which is what i think i must do. i had something like this line: echo `bc $sum/$count` but it won't calculate when i try to run the script. there has to be another way. thanks for any help! -ivalice |
|
#6
|
|||
|
|||
|
ah! i just found it online!
echo "$sum/$tempcount" |bc yes! found it here: http://www.shelldorado.com/newslett...2002-3-Aug.html i saw another page that showed you how to do it by setting an alias, but that seemed like a little extra legwork . especially since i've been up all night doing dam scripting@! -iliva |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > how do you get 10 inputs into an array ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|