r/Basic • u/CharlieJV13 • Feb 15 '23
BAM Update: For one dimension arrays: 🆕 Array initialisation 🆕 Constant Arrays
https://basicanywheremachine-news.blogspot.com/2023/02/for-one-dimension-arrays-array.html
3
Upvotes
View all comments
Show parent comment
1
u/zxdunny Feb 15 '23
Two-dimensional arrays are very, very handy for holding coordinates. As an example, here's a sierpinski triangle:
10 DIM p=0,SCRh-1,SCRw-1,SCRh-1,SCRw/2,0:x=p(1),y=p(2)20 p=INT(RND*3)*2+1: PLOT x+=(p(p)-x)/2,y+=(p(p+1)-y)/2: GO TO 20As you can see, the first line creates a one-dimensional array to hold the points, but the drawback is that you have to access them as p(n) and p(n+1) and multiply your offsets by 2.
Re-writing that for a 2-dimensional array:
10 DIM p(3,2)=(0,SCRh-1),(SCRw-1,SCRh-1),(SCRw/2,0):x=p(1,1),y=p(1,2)20 p=INT(RND*3)+1: PLOT x+=(p(p,1)-x)/2,y+=(p(p,2)-y)/2: GO TO 20...which is much more readable.
Anyway, the other thing I wanted to mention is that constants are not variables that are write-protected - they serve a much higher purpose which, at least in an interpreter, is speed. You don't need to look them up at runtime, they're injected directly into the code as their values are static. I don't support const arrays, however, but I'm considering it - they're quite hard to do, as although their values are static their index may not be.