### Thread: Stuck on array problem in Liberty Basic

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. 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
3. 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
4. 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