Shop OBEX P1 Docs P2 Docs Learn Events
Multi dimensional arrays in Propeller? — Parallax Forums

Multi dimensional arrays in Propeller?

Jimmy W.Jimmy W. Posts: 112
edited 2007-12-27 03:50 in Propeller 1
OK, I swear I have looked!
I am looking to define a multi dimensional array and the compiler expects an end line after the first endbracket, is there any way to create a multi dimensional array in spin?
· long OWControllerDev[noparse][[/noparse]16][noparse][[/noparse]4]
· long OWTempDev[noparse][[/noparse]16][noparse][[/noparse]2][noparse][[/noparse]4]

OR, define a bunch of vars?
· long OWControllerDev1[noparse][[/noparse]4]...long OWControllerDev16[noparse][[/noparse]4]

I am hoping there is some way for the former and not to have to do the latter!

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2007-12-26 03:34
    Jimmy W. said...
    is there any way to create a multi dimensional array in spin?!
    Of course not! Funny idea...
    SPIN has no "data structures"
  • Jimmy W.Jimmy W. Posts: 112
    edited 2007-12-26 04:19
    I was hoping it might from the high level coding.

    Heres to declaring 472 variables woo!

    EDIT: Nothing happened here......

    Posted from my blackberry on the drive home and when I got home and re-read it. Hope no one saw it.

    EDIT 2: looks like·I was too late for the ever·present and quick responding deSilva

    PS: deSilva·you rock [noparse]:)[/noparse]

    Post Edited (Jimmy W.) : 12/26/2007 4:40:06 AM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-26 04:29
    Jimmy,
    (a) Also: No smile.gif
    (b) There is no use of guessing. I think there will be no other way but you thoroughly read through chapter 4 of the Manual. You will discover many extremely usefull things. The general rule is: There is a rich control structure and many - also unusual - operators; however there is no data structure (only the concept of contiguous memory cells of either 1, 2 or 4 bytes each), and there is a sometimes tricky memory allocation concept, split into DAT and VAR sections.

    Have fun!
  • Mike GreenMike Green Posts: 23,101
    edited 2024-03-11 09:30
    The simple answer ... There are no multidimensional arrays in Spin. You have to implement them yourself by using linear arrays ... not hard to do.

    For example, to create a 3 x 4 array, you do a "long array[3*4]" in the VAR section.

    To access element <i,j>, you use "array[ i*3+j ]" where i ranges from 0 to 2 and j ranges from 0 to 3.

    Post Edited (Mike Green) : 12/26/2007 7:24:06 AM GMT
  • J. A. StreichJ. A. Streich Posts: 158
    edited 2024-03-11 09:32
    There are two different approaches to implement 2 dimensional arrays:
    - Row Major Order
    - Column Major Order

    array[i*n+j] or array[i+j*n]

    The alternative is an "array" of addresses to other "arrays", but this takes up far to much memory overhead for most uses. The advantage of this is that you can create dynamic arrays and/or linked lists. I suppose if it weren't something time critical you could implement this using memory off the propeller chip...

    More interesting, an easy way to implement a balanced binary tree:
    don't use (or at least as part of the tree) the first element of the array. Then for each node, the children of the node are array[index*2] and array[index*2+1]. So array[0] isn't part of the tree (use it for something else like the count of used nodes or something), the root is array. The children of the root (array) is array[1*2] (array) and array[1*2+1] (array 3). The children of array is array[2*2] (array) and array[2*2+1] (array).
Sign In or Register to comment.