Using a multidimentional array.

Previous topic - Next topic

ckrum

I have a problem which probably has a very simple answer however I have been unable to find it.
Here is some simple code to illustrate the problem.
This code creates an array 80x80 with data.
This program will fail as it begins to run.
It wont get to the first mousewait.
I need help to understand how to create an array and yet still be able to run.
I have changed this up a lot of ways.. the common denominator to failure is to try and use the array.
Code (glbasic) Select
// --------------------------------- //
// Project: omgbug
// Start: Tuesday, October 15, 2013
// IDE Version: 10.283


// SETCURRENTDIR("Media") // go to media files

GLOBAL ArrayHeight%,ArrayWidth%,grid%,myarray[]

DIM myarray[ArrayHeight%][ArrayWidth%]

ArrayHeight% = 80
ArrayWidth% = 80

LOCAL a%,b%
FOR a%=0 TO ArrayHeight%

FOR b%=0 TO ArrayWidth%

myarray[a%][b%] = "Data"

NEXT
NEXT

PRINT "The array has data",100,100

SHOWSCREEN
MOUSEWAIT

FOR a%=0 TO ArrayHeight%

FOR b%=0 TO ArrayWidth%

myarray[a%][b%] = ""

NEXT
NEXT

PRINT "The array is empty",100,100

SHOWSCREEN
MOUSEWAIT



ckrum

oh and here is what it says when it compiles.

_______________________________________
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.9.829 SN:3e3491bf - 3D, NET
Wordcount:15 commands
compiling:

linking:
success
_______________________________________
*** Finished ***
Elapsed: 2.7 sec. Time: 19:35
Build: 1 succeeded.


fuzzy70

Couple of errors I have noticed but cant test as not on my PC at home at the moment.

Your defining the size of the array before the values have been assigned to the variables so try moving the DIM statement to below the following code.
Code (glbasic) Select

ArrayHeight% = 80
ArrayWidth% = 80


Another error is your loop, you are counting 81 elements as 0 is the start of the array. So DIM [80] for example ranges from 0 to 79 not 0 to 80 as in your code. Try changing your for loops to
Code (glbasic) Select

FOR a%=0 TO ArrayHeight%-1
FOR b%=0 TO ArrayWidth%-1

& that should hopefully fix your problem.

Hope that helps.

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

ckrum

At first glance it looks like you nailed it. Thank YOU!!
The array size is adjustable at the start of the real program I just put in some static values and used enough code to describe the problem.
The original program I am recreating in GLBasic was created a long time ago using TurboBasic.
Thank YOU!
 

ckrum

Yes you helped me get going again.
BOTH your observations are true.

mentalthink

A simple trick, becuse if you change your dimensions in your arrays will have to change in each for... this it's tedious...

For an array Unidimensional
You have in ex...
my_Number=100
Dim my_Array[my_Number]
then in the for you have to put
FOR a%=0 TO my_Number-1

Better use this:
FOR a%=0 TO len(my_Array[])-1   //If you change the dimension of the array you don't worry about to change values in the Code

For Dimensional Array, you have to use Bounds
FOR a%=0 TO Bounds(my_Array[],0)-1  This it's for the first Dimension
FOR a%=0 TO Bounds(my_Array[],1)-1  This it's for the Second Dimension

I'm not sure 100% in the ordner of the Dmensions but I think it's 0 left and 1 right...

This method it's too much comfortable, dn't have fear to try new things, at the begin seems complex but in a short time all comes very easy...  :booze:


fuzzy70

Another little tip is that if you go out of bounds in an array GLBasic just quits without an error.

Easiest way around that if your program suddenly quits is to run your program with debug activated then it shows the error in the IDE.

Lee

Sent from my C6603 using Tapatalk

"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

kanonet

@mentalthink: You should remember that in a FOR loop everything that you write behind a TO is executed every loop. This can be a reason for slow down if you have complicated functions there.
So instead of
Code (glbasic) Select
FOR a%=0 TO len(my_Array[])-1
better write
Code (glbasic) Select
LOCAL lenl% = len(my_Array[])-1
FOR a%=0 TO lenl%

or if possible, loop backwards:
Code (glbasic) Select
FOR a%=len(my_Array[])-1 TO 0 STEP -1

:P
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

mentalthink

Thanks a lot kanonet I really Don't know this trick, thanks I necer thinked in this and you have all the reason...

I think I have to change a lot of line in my code  :D :D :D  :rant: :rant: , but this it's very good...  :booze: