Sonsivri

Electronics => Pic Basic Languages => Topic started by: Husanto on November 27, 2007, 10:37:50 10:37



Title: Array
Post by: Husanto on November 27, 2007, 10:37:50 10:37
Hi,

    i want to ask about using array in microbasic. example i have a code:

dim temp1 as byte[4]
const temp as byte[3][4] =((1,2,3,4),(5,6,7,8),(7,5,3,2))
main()
     for i=0 to 3
          temp1 = temp[2]
     next i
end.

why the result always (1,2,3,4) not ( 7,5,3,2) ????can give information about using array two dimension to use in looping function.


Title: Re: Array
Post by: robban on November 28, 2007, 01:06:53 01:06
This is actually better solved in C, but since You do it in Basic, let's see...
Proton does a 2-dim array this way:

' Display text from two LDATA tables
' Based on their address located in a seperate table
      
        ' Use a 14-bit core device
       
        Dim ADDRESS as Word
        Dim LOOP as Word
        Dim DATA_BYTE as Byte
       
        Delayms 200                     ' Wait for PICmicro to stabilise
        Cls                           ' Clear the LCD
        ADDRESS = LREAD ADDR_TABLE         ' Locate the address of the first string
          While 1 = 1                     ' Create an infinite loop
           DATA_BYTE = LREAD ADDRESS      ' Read each character from the LDATA string
            If DATA_BYTE = 0 Then Break      ' Exit if NULL found
           Print DATA_BYTE               ' Display the character
            Inc ADDRESS                  ' Next character
        Wend                        ' Close the loop
EXIT_LOOP:
      
        Cursor 2,1                     ' Point to line 2 of the LCD
      ADDRESS = LREAD ADDR_TABLE + 2      ' Locate the address of the second string
          While 1 = 1                     ' Create an infinite loop
           DATA_BYTE = LREAD ADDRESS      ' Read each character from the LDATA string
            If DATA_BYTE = 0 Then Break    ' Exit if NULL found
           Print DATA_BYTE               ' Display the character
            Inc ADDRESS                  ' Next character
        Wend                        ' Close the loop
EXIT_LOOP2:
        Stop
       
       
ADDR_TABLE:                           ' Table of address's
      LDATA WORD STRING1,WORD STRING2
STRING1:
      LDATA "HELLO",0
STRING2:
      LDATA "WORLD",0
******************************************************************************************
From here You would be able to add one more table and get the job done


Title: Re: Array
Post by: th_sak on November 28, 2007, 12:51:13 12:51
You have only one pointer for your 2 dimension array, but you should have 2 pointers.
This should work.

dim temp1 as byte[4]
const temp as byte[3][4] =((1,2,3,4),(5,6,7,8),(7,5,3,2))
main()
     for i=0 to 2      ; this is pointer for rows [3]
        for j=0 to 3   ; this is pointer for columns [4]
          temp1 = temp[i,j]
        next j
     next i
end.


Title: Re: Array
Post by: robban on November 28, 2007, 04:33:55 16:33
As I say, less is more(though I haven't tested it yet)