What's Wrong With This DIM ?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

abrogard
Newbie
Posts: 2
Joined: Sun May 01, 2016 7:49 pm

What's Wrong With This DIM ?

Postby abrogard » Tue May 03, 2016 7:26 pm

I keep getting subscript out of range error when I write DIM spots(2,12000) but it works okay as DIM spots(2,2000).

Is this a qbasic limit?

Here's the whole piece FYI. You can see what I'm trying to do. Make a list of unique random points. Started having the problem when I wrote the bit to take newly created random co-ords (x,y) and check them to see if they were unique.


Code: Select all

 SCREEN 11

 CLS

DIM spots(2, 2000)

'init random

  RANDOMIZE (223)

'set 12000 random points

FOR i = 1 TO 12000
  x = INT(RND * 600)
  y = INT(RND * 440)

' search for matches

 match = 0

IF i > 4 THEN  ' this just a mad attempt to avoid subscript out of range errors.

 FOR k = 1 TO i - 2

 IF spots(1, k) = x THEN

 IF spots(2, k) = y THEN

 match = 1

 EXIT FOR

 END IF

 END IF

 NEXT k


END IF



IF match = 1 THEN

PRINT "match"; x; y
i = i - 1

ELSE

  PSET (x, y)
 
  spots(1, i) = x
  spots(2, i) = y

  PRINT i

END IF


 NEXT i

PRINT i

'put 2000 spots in an array

 
   

 '   FOR i = 1 TO 2000
 '   x = INT(RND * 600)
 '   spots(1, i) = x
 '   NEXT i


 '   FOR i = 1 TO 2000
 '   y = INT(RND * 440)
 '   spots(2, i) = y
 '   NEXT i

' print out 20 from array

'    FOR i = 1 TO 20

'    PRINT "Spot", i, "x", spots(1, i)
'    PRINT "Spot", i, "y", spots(2, i)


'    NEXT i


' match found printout

 '         IF match = 1 THEN
 '         PRINT "match found"
 '         PRINT spots(1, i)
 '         PRINT spots(2, i)
 '         END IF

User avatar
burger2227
Veteran
Posts: 2144
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: What's Wrong With This DIM ?

Postby burger2227 » Tue May 10, 2016 4:05 pm

12,000 integers at 2 bytes each is 24,000 bytes times 2 for 2 levels exceeds 32767 bytes for integers.

Your loop still uses 12,000 so it runs out of array places and that is where it errors also.

QB64 can run it with 12,000 but QB will not.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : http://www.QB64.NET
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

abrogard
Newbie
Posts: 2
Joined: Sun May 01, 2016 7:49 pm

Re: What's Wrong With This DIM ?

Postby abrogard » Tue May 10, 2016 5:10 pm

Yes, thank you. I have moved over to qb64 and all is good.

:)


Return to “QBASIC and QB64 Questions & Answers”

Who is online

Users browsing this forum: Bing [Bot] and 8 guests

cron