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
Why Do I Get Subscript Errors Here?
Moderators:Administrator, Global Moderator
Re: Why Do I Get Subscript Errors Here?
Max variable size is 64K (65534) bytes, or 32K (32766) elements as noted.
You aren't assigning `spots` a variable type, so it is 4-byte wide double I believe.
Plus that is a multi-dimensional array so in reality you are trying to allocate 60000 elements of 4 bytes each (2, 20000) = 3 * 20001. Thus if keeping the first dim at 2 you can only do ` spots(2,10921) AS INTEGER` for maximum elements and not one iota larger (10921 is 32766/3-1)
If left un-typed, or typed for long or double, you can only have 64K/4bytes/3dims-1=spots(2,5460) maximum.
If you just had a single dimension as in your intro text it works fine up to 16383 elements because 64K / 4 bytes...
You aren't assigning `spots` a variable type, so it is 4-byte wide double I believe.
Plus that is a multi-dimensional array so in reality you are trying to allocate 60000 elements of 4 bytes each (2, 20000) = 3 * 20001. Thus if keeping the first dim at 2 you can only do ` spots(2,10921) AS INTEGER` for maximum elements and not one iota larger (10921 is 32766/3-1)
If left un-typed, or typed for long or double, you can only have 64K/4bytes/3dims-1=spots(2,5460) maximum.
If you just had a single dimension as in your intro text it works fine up to 16383 elements because 64K / 4 bytes...
Last edited by EkriirkE on Sun Aug 07, 2016 9:51 am, edited 3 times in total.
I take things apart. I break them. Then I put things that don't belong back inside them.
Re: Why Do I Get Subscript Errors Here?
Yep. Exactly so. Thank you.
Re: Why Do I Get Subscript Errors Here?
I take things apart. I break them. Then I put things that don't belong back inside them.
-
- Newbie
- Posts:8
- Joined:Fri Sep 17, 2021 12:08 pm
Re: Why Do I Get Subscript Errors Here?
I believe you can't declare any array with the total number of bytes exceeding 64kb. It's not just the number of elements but the resulting size that matters. Since you didn't explicitly declare a data type QBasic defaults to theWhy? I should be able to go to 32000 shouldn't I? And in fact it seems to go wrong anywhere above 2000.
single precision data type where each element uses 4 bytes. You might want to add "DEFINT A-Z" at the top of your code so that each variable defaults to a 2 byte integer unless otherwise specified. This reduces the amount of memory used and is faster when doing calculations.