Why Do I Get Subscript Errors Here?
Posted: Wed May 04, 2016 5:25 am
I get subscript out of range errors in this code every time I run it with the array dimensioned for 20000.
Like : DIM spots(20000)
Why? I should be able to go to 32000 shouldn't I? And in fact it seems to go wrong anywhere above 2000.
Here's the code. Just trying to fill an array with unique x,y coords.
SCREEN 11
' FOR x = 1 TO 750
' y = x * .75
' PSET (x, y)
' NEXT x
CLS
DIM myvar AS LONG
DIM height AS LONG
DIM wide AS LONG
DIM spots(2, 2000) ' here is the problem. when i change this.
height = 600
wide = 440
myvar = height * wide
'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
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
Like : DIM spots(20000)
Why? I should be able to go to 32000 shouldn't I? And in fact it seems to go wrong anywhere above 2000.
Here's the code. Just trying to fill an array with unique x,y coords.
SCREEN 11
' FOR x = 1 TO 750
' y = x * .75
' PSET (x, y)
' NEXT x
CLS
DIM myvar AS LONG
DIM height AS LONG
DIM wide AS LONG
DIM spots(2, 2000) ' here is the problem. when i change this.
height = 600
wide = 440
myvar = height * wide
'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
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