#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Stuck on array problem in Liberty Basic


    The problem asks the user to enter a list of positive numbers and terminate using zero. Then it should list the greatest and lowest number entered by the user. The problem is I can't get the program to terminate when I enter the zero. I will show my code and any help would be appreciated. Thanks!


    dim num(9999)

    for i = 0 to 9999
    Input "Please enter a positive number (press 0 when selections are complete): ";num(i)
    next i


    high=0
    highNum=0

    while i <>0
    for i = 0 to 9999
    if num(i) > high then
    high = num(i)
    highNum=i
    end if
    next i

    low=9999
    lowNum = 0

    for i = 0 to 9999
    if num(i) < low then
    low = num(i)
    lowNum=i
    end if
    next i

    wend

    print "The greatest number entered was: ";highNum
    print "The lowest number entered was : ";lowNum
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,874
    Rep Power
    6351
    In your initial for loop, you never exit the for loop if num(i) is equal to zero. Put an IF check in there to exit the loop under that condition.

    You may also want to make this a do...while loop if the language supports it, keep the "i" counter separate and ++ it yourself.

    Also, make use of the [ code ] tags so we can see the whitespace in your code, basic is whitespace delimited and this code is useless without indentation.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0
    Alright I've fixed the code so it prints the array. It also prints the highest number every time, but the lowest number always comes out zero or an incorrect number. Here is the updated code:



    dim num(999)

    Input "Please enter a positive number (press 0 when selections are complete): ";num(i)

    while num(i) <> 0
    i = i+1
    Input "Please enter a positive number (press 0 when selections are complete): ";num(i)

    wend




    for i =0 to (i-1)



    if num(i) > high then
    high = num(i)
    end if

    low=99999999

    if num(i) < low then
    low = num(i)
    end if
    next i

    for i = 0 to (i-1)
    print num(i)
    next i

    print
    print "The greatest number entered was: ";high

    print "The lowest number entered was : ";low
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,874
    Rep Power
    6351
    Again, use [ code ] tags so we can see your code properly. If you don't know what that means, ASK, don't ignore it.

    You re-set low to 99999 every time through your loop. Don't do that.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo