GIDForums  

Go Back   GIDForums > Computer Forums > Computer Software Forum - Linux
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-May-2008, 09:01
lilballeer lilballeer is offline
New Member
 
Join Date: May 2008
Posts: 16
lilballeer is on a distinguished road

Script/program that will input many numbers


Hello,

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  
Old 13-May-2008, 13:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Script/program that will input many numbers


Quote:
Originally Posted by lilballeer
I need to write a program/script
Well, what is it? A program or a script?
Quote:
Originally Posted by lilballeer
in vi editor
I use gvim. I hope that it's OK.
Quote:
Originally Posted by lilballeer
that will allow a user to input as many numbers as the user wants...
What are you going to do with them? I mean, really!

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:
#!/bin/bash while [ 1 ] do echo "Enter a number (Ctrl-C to quit) : " read x done

Here's a run:

Code:
Enter a number (Ctrl-C to quit) : 42 Enter a number (Ctrl-C to quit) : 1369 Enter a number (Ctrl-C to quit) : -12345678 Enter a number (Ctrl-C to quit) : 0 Enter a number (Ctrl-C to quit) :

If you would rather do something a little more interesting, you could try:

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<int> values;
    string prompt("Enter as many integers as you want.\n"
                  "Enter a non-integer \"something\" to quit . . .\n");
    cout << prompt;
    int x;
    int sum = 0;
    while (cin >> x) {
        values.push_back(x);
        sum += x;
    }
    cout << "Number of items entered = " << values.size() << endl;

    sort(values.begin(), values.end());
    cout << "Here they are, sorted in ascending order:" << endl;
    for (unsigned i = 0; i < values.size(); i++) {
        cout << setw(11) << values[i] << endl;
    }

    sort(values.rbegin(), values.rend());
    cout << "Here they are, sorted in descending order:" << endl;
    for (unsigned i = 0; i < values.size(); i++) {
        cout << setw(11) << values[i] << endl;
    }

    return 0;
}

Here's a run:
Code:
Enter as many integers as you want. Enter a non-integer "something" to quit . . . 42 1369 -12345678 0 z Number of items entered = 4 Here they are, sorted in ascending order: -12345678 0 42 1369 Here they are, sorted in descending order: 1369 42 0 -12345678

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  
Old 13-May-2008, 13:36
lilballeer lilballeer is offline
New Member
 
Join Date: May 2008
Posts: 16
lilballeer is on a distinguished road

Re: Script/program that will input many numbers


hello,
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:

#!/bin/sh
clear
echo "Enter a number and enter 999 to exit"
read num
while [ $num != 999 ]
do
echo "enter a number: "
read num
done 



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  
Old 13-May-2008, 14:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Script/program that will input many numbers


Quote:
Originally Posted by lilballeer
what i dont understand is how do i get the program to store all the variables...
If you are using bash, then read about bash arrays. Here's a reference: Advanced Bash-Scripting Guide

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:
#!/bin/bash largest=-999999999 # some very large magnitude negative number smallest=999999999 # some very large positive number count=0 # initialize to zero, increment for each new number sum=0 # initialize to zero, add each new number num=0 # can be anything other than 999 echo "Enter a number and enter 999 to exit" while [ $num != 999 ] do echo "Enter the next number: " read num if [ $num -ne 999 ] then count=$((count+1)) # $((xxx)) double parentheses for bash arithmetic #use bash arithmetic to add $num to sum #logic that tests whether $z is less than $smallest # if it is, then set smallest=$num #logic that tests whether $z is greater than $largest # if it is, then set largest=$num fi done echo "count=$count, sum = $sum, smallest=$smallest, largest=$largest" echo "average = $((sum/count))"

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:
echo "from bash: average = $((sum/count))" #shows integer arithmetic echo -n "from bc : average = " bc << ENDBC scale=6 $sum/$count ENDBC

Here's a run:

Code:
Enter a number and enter 999 to exit Enter the next number: 1 Enter the next number: 2 Enter the next number: 3 Enter the next number: 4 Enter the next number: 999 count=4, sum = 10, smallest=1, largest=4 from bash: average = 2 from bc : average = 2.500000

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:
largest=-999999999 # some very large magnitude negative number smallest=999999999 # some very large positive number sum=0 # initrialize to zero, add each new number while [ $i -lt $count ] do z=$((aray[$i])) #use bash arithmetic to add $z to sum #logic that tests whether this number is less than $smallest # if it is, then set smallest=$num #logic that tests whether this number is greater than $largest # if it is, then set largest=$num i=$(($i+1)) # increment array index and loop counter done # Then calculate average using $sum and $count #

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  
Old 13-May-2008, 15:23
lilballeer lilballeer is offline
New Member
 
Join Date: May 2008
Posts: 16
lilballeer is on a distinguished road

Re: Script/program that will input many numbers


hello,

thanks
here is what i got:
PHP Code:

#!/bin/bash
clear
RANGE=60
num=$RANDOM
let "num %= $RANGE"
counter=0
echo $num
while [ 1 ]
do
echo "enter a Guess"
read guess
if
 [ $guess -eq $num ]
then
echo "You are correct!!!"
elif
 [ $guess -lt $num ]
then
echo "$guess is less than the number"
elif
    [ $guess -gt $num ]
then
echo "$guess is Greater than the number"
else
echo "Enter a valid guess"
break
echo "Goodbye!!"
fi
done 



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  
Old 13-May-2008, 16:21
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Script/program that will input many numbers


Quote:
Originally Posted by lilballeer
here is what i got:
im not sure how to get it to exit loop when correct

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:
....and how to get the counter to display
It's called a counter, but you don't have it counting anything. Shouldn't you increment it after every guess? How about something like

Code:
counter=0 while [ 1 ] do echo "Enter your guess." read guess counter=$((counter+1)) #bash arithmetic to increment the counter if [ $guess -eq $num ] then echo "You are correct!!!" break; elif [ $guess -lt $num ] then #the logic and echo stuff to tell the user whether it is too high or too low . . . fi echo "Try again." done echo "Number of guesses = $counter"
Quote:
Originally Posted by lilballeer
p.s...the answer is there for now..but will be taken out later...
Good idea! I like it there for program testing.


Regards,

Dave
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 05:28.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.