Author Topic: Using a multidimentional array.  (Read 3756 times)

Offline ckrum

  • Mc. Print
  • *
  • Posts: 15
    • View Profile
Using a multidimentional array.
« on: 2013-Oct-16 »
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


Offline ckrum

  • Mc. Print
  • *
  • Posts: 15
    • View Profile
Re: Using a multidimentional array.
« Reply #1 on: 2013-Oct-16 »
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.


Offline fuzzy70

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 828
  • Look left, Look right, LOOK OUT!!
    • View Profile
Re: Using a multidimentional array.
« Reply #2 on: 2013-Oct-16 »
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)

Offline ckrum

  • Mc. Print
  • *
  • Posts: 15
    • View Profile
Re: Using a multidimentional array.
« Reply #3 on: 2013-Oct-16 »
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!
 

Offline ckrum

  • Mc. Print
  • *
  • Posts: 15
    • View Profile
Re: Using a multidimentional array.
« Reply #4 on: 2013-Oct-16 »
Yes you helped me get going again.
BOTH your observations are true.

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3375
  • Integrated Brain
    • View Profile
Re: Using a multidimentional array.
« Reply #5 on: 2013-Oct-16 »
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:


Offline fuzzy70

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 828
  • Look left, Look right, LOOK OUT!!
    • View Profile
Re: Using a multidimentional array.
« Reply #6 on: 2013-Oct-16 »
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)

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1137
    • View Profile
    • My GLBasic code archiv
Re: Using a multidimentional array.
« Reply #7 on: 2013-Oct-16 »
@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[])-1better 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

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3375
  • Integrated Brain
    • View Profile
Re: Using a multidimentional array.
« Reply #8 on: 2013-Oct-17 »
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: