Author Topic: Array help  (Read 2744 times)

0 Members and 1 Guest are viewing this topic.

Offline collechess

  • LV3 Member (Next: 100)
  • ***
  • Posts: 93
  • Rating: +22/-2
    • View Profile
Array help
« on: November 02, 2011, 07:12:48 pm »
I'm trying to learn arrays.  I read Deep Thought's tutorial and I kind of understand them, but why doesn't this work?
Code: [Select]
.ARRAY
.L=length
0->L
20->X->Y
Repeat getkey(15)
ClrDraw
If getkey(54)
sub(MS,X,Y)
End
If getkey(4)
Y-1->Y
End
If getkey(2)
X-1->X
End
If getkey(3)
X+1->X
End
If getkey(1)
Y+1->Y
End
PxlOn(X-1,Y)
PxlOn(X+1,Y)
PxlOn(X,Y-1)
PxlOn(X,Y+1)
For(H,0,L)
PxlOn({H*2+L1},{H*2+L1+1})
End
DispGraph
End
Lbl MS
r1->{L1+L*2}
r2->{L1+L*2+1}
L+1->L
Return

And when exactly do you use the {} brackets?

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Array help
« Reply #1 on: November 02, 2011, 08:23:26 pm »
Remember your order of operations don't exist in Axe -- L1+L*2 is really (L1+L)*2.

Any value in braces is treated as a pointer, the address of a particular byte in the calculator's memory. Putting braces around a pointer grabs the value of the byte at that address rather than the value of the address.
« Last Edit: November 02, 2011, 08:23:35 pm by Deep Thought »




Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: Array help
« Reply #2 on: November 03, 2011, 01:38:54 am »
Also remember that For loops loop up to and including the value of the last parameter, meaning that if L is 0, then For(H,0,L) loops from 0 to 0, or one time. This is incorrect because if the length is zero, it should not loop at all. For(H,0,L−1) doesn't work either, because if L is 0, then it loops from 0 to -1 (or 65535), which is not good. In the end you should do something like the following:
Code: [Select]
:For(H,1,L)
:Pxl-On({H−1∗2+L₁},{H−1∗2+L₁+1})
:End
or slightly more optimized:
Code: [Select]
:For(H,1,L)
:Pxl-On({H∗2+L₁−2},{H∗2+L₁+1−2})
:End

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Re: Array help
« Reply #3 on: November 03, 2011, 02:03:26 am »
Slightly more optimized :P

Code: [Select]
:For(H,1,L)
:Pxl-On({H∗2+L₁−2->r6},{r6+1})
:End
In-progress: Graviter (...)

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Array help
« Reply #4 on: November 03, 2011, 02:41:03 am »
Even more optimized is to offset your entire array so it starts at the next byte. :D

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: Array help
« Reply #5 on: November 03, 2011, 02:15:49 pm »
Well in that case...
Code: [Select]
:For(H,1,L)
:Pxl-On({H∗2+L₁−2}ʳ,/256)
:End
or even
Code: [Select]
:For(H,1,L)
:Pxl-On({H∗2+L₁−2}ʳ,Asm(6C))
:End