Menu

2D arrays

Help
Santiago
2015-10-24
2015-11-12
1 2 > >> (Page 1 of 2)
  • Santiago

    Santiago - 2015-10-24

    Fixed: See https://sourceforge.net/p/gcbasic/discussion/579126/thread/91219dd4/?limit=25&page=1#2670

    Hello.
    I need to do something with 2D arrays, by now i tried this way based on macros:

    #chip mega328p, 16
    
    create2Darray( my2Da, 4, 4 )  ' Create 4x4 Array called my2Da 
    create2Darray( my2Db, 5, 10 )
    
    set2Ddata( my2Da, 3, 2, 100 )  ' Set element 3,2 in my2Da to 100
    set2Ddata( my2Db, 2, 9, 217 )
    
    myVar = 0
    
    get2Ddata( my2Da, 3, 2, myVar)  ' Get element 3,2 in my2Da and put in myVar
    'Now myVar = 100
    
    get2Ddata( my2Db, 2, 9, myVar)
    'Now myVar = 217
    
    do
    loop
    
    Macro create2Darray( name, dimx, dimy )
        Dim name( dimx*dimy )
        #define name_ely dimy
    End Macro
    
    Macro set2Ddata( name, ordx, ordy, value )
        name( (ordx-1)*name_ely+ordy ) = value
    End Macro
    
    Macro get2Ddata( name, ordx, ordy, var )
        var = name( (ordx-1)*name_ely+ordy )
    End Macro
    

    Do you know any other method, easier to use or more intuitive??
    the create array macro is ok, but getting and setting elements is a bit complex.
    would be nice just doing: my2Da( x, y ) = somevalue or something like that.

    Regards.

     

    Last edit: Anobium 2015-10-26
  • Santiago

    Santiago - 2015-10-24

    Ok... forget the mentioned method... it doesn't work
    Does someone know some way to do this?

     
    • Chris Roper

      Chris Roper - 2015-10-24

      I havn't tested it but I think this logic might work:

      #define dim1 4
      #define dim2 4
      
      dim MyArray(dim1 * dim2)
      
      idx1 = 3
      idx2 = 2
      
      'to Set
      MyArray(idx1 + ((dim1 * Idx2) + idx1)) = 100
      
      'to Read
      MyVar = MyArray(idx1 + ((dim1 * Idx2) + idx1))
      

      Don't have access to the compiler or any hardware to try it on so I leave
      that up to you :)

      Cheers
      Chris

       

      Last edit: Chris Roper 2015-10-24
      • Chris Roper

        Chris Roper - 2015-10-24
         

        Last edit: Chris Roper 2015-10-24
  • Santiago

    Santiago - 2015-10-24

    Thanks Chris, i'll give it a try.

     
    • Chris Roper

      Chris Roper - 2015-10-24

      I dont think it will work, I based it on zero indexed arrays but I think
      GCB is unary indexed and the zero element is size, just rethinking the
      problem now.

      p.s. Too much beer whilst watching NZ beat my home team South Africa :(

       

      Last edit: Chris Roper 2015-10-24
      • Anobium

        Anobium - 2015-11-12

        This posting got me thinking. I have updated the Help to clarify the element zero assumption.

        This is section from the lastest help, v0.95. I have references to the usage of element zero .

        Setting an entire array at once
        It is possible to set several elements of an array with a single line of code. The example below shows how:

        Dim TestVar(10)
        TestVar = 1, 2, 3, 4, 5, 6, 7, 8, 9

        When using the method above element 0 of the arrayTestVar will be set to the number of items in the list, which in this case is 9. Each element of the array will then be loaded with the corresponding value in the list - so in the example, TestVar(1) will be set to 1, TestVar(2) to 2, and so on. Element 0 will only be set to number of items in the array when using this method upto 48 data elements.

        Array Length
        Unless using the above methog element 0 should not be used to obtain the length of the array. Element 0 will only be a consistent with respect to the length of the array ONLY when the array is set as shown above. For all other methods of populate array data see the next paragraph.

        The correct, and good, method is to use a constant to set the array size and use the constant within your code to obtain the array length in your code.

        #Define ArraySizeConstant 500
        Dim TestVar( ArraySizeConstant )
        HSerPrint ArraySizeConstant     'or, other usage
        
         
  • Santiago

    Santiago - 2015-10-24

    Finally i got the macros workig this way, a bit weird, but is something:

    #chip mega328p, 16
    
    create2Darray( my2Da, 8, 4 ) 'creates a 2x4(=8) array named my2Da
    
    for iX=1 to 2
        for iY=1 to 4
            set2Ddata( my2Da, iX, iY, iX*10+iY ) '11 12 13 14 - 21 22 23 24
        next
    next
    
    myvar = 0
    
    for iX=1 to 2
        for iY=1 to 4
            get2Ddata( my2Da, iX, iY, myVar )
            print myVar ' Should print: 11 12 13 14 21 22 23 24
        next
    next
    
    do
    loop
    
    Macro create2Darray( name, size, dimY )
        Dim name( size )
        name(0) = dimY
    End Macro
    
    Macro set2Ddata( name, ordx, ordy, value )
        _2Dindex = (ordx-1)*name(0)+ordy
        name( _2Dindex ) = value
    End Macro
    
    Macro get2Ddata( name, ordx, ordy, var )
        _2Dindex = (ordx-1)*name(0)+ordy
        var = name( _2Dindex )
    End Macro
    
     
  • Chris Roper

    Chris Roper - 2015-10-24

    Well I also finaly got my logic working too :)

    Have a look and see if it helps

    #define DimX 4
    #define DimY 4
    
    dim MyArray(DimX * DimY)
    
    Row = 3
    Col = 2
    
    'to Set
    MyArray(((Row - 1) * DimX) + Col) = 100
    
    'to Read
    MyVar = MyArray(((Row - 1) * DimX) + Col)
    

    Cheers
    Chris

     

    Last edit: Chris Roper 2015-10-24
  • Chris Roper

    Chris Roper - 2015-10-24

    Here it is as a function and tested on the PIC16F690, as it uses noting hradware spcific it should run on any GCBasic Target Hardware:

    #chip 16f690
    
    #define DimX 5
    #define DimY 3
    
    dim MyArray(DimX * DimY)
    
    res = 100
    Row = 4
    Col = 1
    
    MyArray(FnCell(Row,Col,DimX)) = res
    MyVar = MyArray(FnCell(Row,Col,DimX))
    
    if MyVar = res then PortC.0 = 1
    
    end
    
    Function FnCell(Row, Col, Cols)
      FnCell = ((Row - 1) * Cols) + Col
    end Function
    

    Cheers
    Chris

     

    Last edit: Chris Roper 2015-10-25
    • Anobium

      Anobium - 2015-10-25

      @Chris. Very nice and simle. We should move to the Help File?

       
      • Chris Roper

        Chris Roper - 2015-10-25

        If you like, but I think it should have a bit of independent testing first to make sure it holds up under different conditions. If nobody manages to break it then it should be safe.

         
  • Santiago

    Santiago - 2015-10-25

    Cool.. looks good, tomorrow iĺl try it. too late now

    Thank you.

     
  • Santiago

    Santiago - 2015-10-25

    Thanks Chris, your code works fine.

     
  • Santiago

    Santiago - 2015-10-25

    Well... looked like it was working, but actually it isn't:

    #chip mega328p, 16
    
    #define rows 2
    #define cols 3
    
    Dim myArray( rows*cols )
    
    for index=1 to rows*cols
        myArray( index ) = index
    next
    

    GcBasic creates an array of size 2:

    ;Set aside memory locations for variables
    .EQU    MYARRAY=256
    .EQU    INDEX=259
    

    I don't know if the compiler should do it, but the constant preprocessing in Dim myArray is not done.

     
    • Chris Roper

      Chris Roper - 2015-10-25

      try adding spaces in the rows*cols like thai Rows * Cols.
      We are also looking at a possible conflict on the AVR as the Rows and Cols
      variables have been used in the core libraries

       

      Last edit: Chris Roper 2015-10-25
    • Anobium

      Anobium - 2015-10-25

      Adapting the code to process the array being defined - please change as follows. I have put the major changes within ****. This should work. The script requires DimX and DimY if you change the constants you will need to change the script.

        #chip 16f690
      
        #define DimX 5
        #define DimY 3
      
        **dim MyArray( Two_Dimensional_Array )**
        dim nextArray (2)
        nextArray(1) = 1
      
        res = 100
        Row = 4
        Col = 1
      
        MyArray(FnCell(Row,Col,DimX)) = res
        MyVar = MyArray(FnCell(Row,Col,DimX))
      
        if MyVar = res then PortC.0 = 1
      
        elementtrack = 0
        for Row = 1 to Dimy
      
          for Col = 1 to Dimx
              MyArray(FnCell(row,col,DimX)) = elementtrack
              elementtrack++
              HSerPrintByteCRLF MyArray(FnCell(row,col,DimX))
              epwrite elementtrack, MyArray(FnCell(row,col,DimX))
              wait 250 ms
          next
      
        next
      
        end
      
        Function FnCell(Row, Col, Cols)
          FnCell = ((Row - 1) * Cols) + Col
        end Function
      
      **  #script
          Two_Dimensional_Array = DimX * DimY
        #endscript**
      
       

      Last edit: Anobium 2015-10-25
      • Anobium

        Anobium - 2015-10-25

        Do not forget to remove the '**!

         
        • Santiago

          Santiago - 2015-10-25

          About the dim issue, adding spaces doesn't help, for avr or pic is the same.
          Anyway it's easy to just dim the array to the desired size.

           
          • Anobium

            Anobium - 2015-10-25

            Try https://sourceforge.net/p/gcbasic/discussion/579126/thread/91219dd4/#cc80/9a19 or hard code the size, byt, the script should work for you.

             
            • Santiago

              Santiago - 2015-10-25

              Thanks Anobium, that script will help in my project.
              Just a question: this issue about dim array not accepting constat calculations is the spected behavior? or could be something wrong in my GcBasic build?

               
              • Anobium

                Anobium - 2015-10-25

                Nothing wrong.

                Is it expected behavior. The order of processing of the source file means that a script will create the correct constant value for you.

                 
  • Anobium

    Anobium - 2015-10-25

    I have added two dimensional array to the list of items to be included in the release after v0.95.

     
  • mmotte

    mmotte - 2015-10-25

    I can get it to work Hard coding the Dim But the scipt didn't work for me.

    The formula should be DimX * DimY +1 because the zero position is the length of the array, Like Chris Roper said.

     
    • Santiago

      Santiago - 2015-10-25

      Hi mmotte.
      The script works for me in Great Cow BASIC (0.94 2015-08-05)

      I'm pretty sure that gcbasic already reserves dim+1 positions.

      But i see nothing in the zero position of the array, always 0... if the length of the array should be in array(0), then something is wrong here.

       
1 2 > >> (Page 1 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.