![]() |
|
#1
|
|||
|
|||
Script/program that will input many numbersHello,
I need to write a program/script in vi editor that will allow a user to input as many numbers as the user wants... I am having trouble figuring out what commands to use for this.... i know commands like read variable.... but how do i get it to read an almost unlimited amount of numbers... thank you |
|
#2
|
|||
|
|||
Re: Script/program that will input many numbersQuote:
Quote:
Quote:
Here's a shell script that will allow the user to enter numbers until the cows come home (at which time he/she hits Ctrl-C and goes out to hook up the cows to the milking machine): Code:
Here's a run: Code:
If you would rather do something a little more interesting, you could try: CPP / C++ / C Code:
Here's a run: Code:
I respectfully suggest that for a more meaningful response you give us a better problem statement. Really. Or, putting it another way... Pretend that you are the most intelligent computer of the 22nd century. Suppose someone walked up to you and stated his/her requirements as you did in your request. What would you do? Maybe something like: 1. Go back to your archives to see what the heck a "vi" is. 2. What the heck...I hope you get the idea. Regards, Dave |
|
#3
|
|||
|
|||
Re: Script/program that will input many numbershello,
sorry bout the lack of info in description, but the script is suppose to be as follows: Write a program that will allow a user to input as many numbers as the user wants (use 999 as the choice that ends the user input). The program will then respond: Highest Number: Answer Lowest Number: Answer Sum of the numbers: Answer Average of the numbers: Answer ............. I like the 1st code you entered.... off of that i got this so far: PHP Code:
what i dont understand is how do i get the program to store all the variables...so that i can use them for calculations later.. Thank you |
|
#4
|
|||
|
|||
Re: Script/program that will input many numbersQuote:
However... Here's why I wanted a problem statement: With your requirements, why the heck do you think you need to store the numbers? See Footnote. Just keep track of things on the fly: Code:
Now, the only problem is that bash arithmetic uses integers, so if the sum is 10 and the count is 4, bash's answer will be 2, and not 2.5 If you really need non-integer values, you can use an external program such as bc to do the calculations. If it's not on your system, maybe you can use something else to get fractional arithmetic into the picture. If you have bc, then instead of the $((sum/count)) thing you could have something like the following after the loop: Code:
Here's a run: Code:
Regards, Dave Footnote: Suppose you do decide to put the numbers into an array as they are read in. The only other thing in the input loop would be to increment $count each time. Then, after the input has terminated, you would have a loop that does the following Code:
I hope you get my point: You have to do the same operations and comparisons on the stuff whether you store input values in an array or whether you keep track of the sum and the extrema as you go along. Therefore: why bother with storing the stuff? Last edited by davekw7x : 13-May-2008 at 15:12.
|
|
#5
|
|||
|
|||
Re: Script/program that will input many numbershello,
thanks here is what i got: PHP Code:
im not sure how to get it to exit loop when correct....and how to get the counter to display how many guesses it takes after the user guesses right... p.s...the answer is there for now..but will be taken out later... thanks |
|
#6
|
|||
|
|||
Re: Script/program that will input many numbersQuote:
Huh? Just put the break after your echo says that it's correct. Why the heck did you break at the bottom of the loop after the script had determined that it was the wrong answer? Quote:
Code:
Quote:
Regards, Dave |
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Scanning dilemma (fscanf and fgets) | aijazbaig1 | C Programming Language | 10 | 29-Mar-2008 04:58 |
| subscript error in coding | warborules | C Programming Language | 6 | 27-Nov-2005 17:16 |
| Linear Search | eccoflame | C Programming Language | 3 | 19-Apr-2005 08:36 |
| still problem with polynom linked list | if13121 | C Programming Language | 7 | 22-Nov-2004 23:02 |
| IP tables | rogermark100 | C Programming Language | 6 | 18-Apr-2004 07:22 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The