Hello,
About two-dimensional arrays:
#two-dimensional array x={0,1,2,3,4,5,6,7,8,9} redim x(2,5) print "rows: " + x[?,] print "columns: " + x[,?] print "total items: " + x[?] #size #array is now x[row x 2, column x 5] like this: #0,1,2,3,4 #3,4,5,6,7 print "Test x[0,2]: " + x[0,2] #result:2 - x[row0, column2] print "Test x[1,4]: " + x[1,4] #result:9 - x[row1, column4] #To access each item in order from memory, we iterate column, then row: for row = 0 to 1 for column = 0 to 4 print x[row,column]; next column next row print #result: 0123456789 - correct
How about one-dimensional array:
#One-dimensional array x={0,1,2,3,4,5,6,7,8} print "rows: " + x[?,] print "columns: " + x[,?] print "total items: " + x[?] #size #array is now x[row x 9, column x 1] like this #0 #1 #2 #3 #4 #5 #6 #7 #8 #9 print "Test x[8,0]: " + x[8,0] #result:8 - x[row8, column0] print "Test x[3,0]: " + x[3,0] #result:3 - x[row3, column0]
So, currently BASIC-256 show to user an one-dimensional array as a array with 1 column and many rows, which is wrong. One-dimensional array is an array with one row and multiple columns.
In this case x[?,] shuld return 1 for any one-dimensional array and x[,?] shuld return number of columns (=total items/size). It make sense.
This change will have no impact on old programs, but will put things in order.
x={0,1,2,3,4,5,6,7,8,9} print x[8,0] # result: 8
should be:
x={0,1,2,3,4,5,6,7,8,9} print x[0,8] # result shuld be 8 but currently we got: ERROR on line 2: Array x index out of bounds.
Big request...
1) We know that either one-dimensional or two-dimensional arrays are in fact two-dimensional arrays (with one row or multiple rows). Keep it that way... this is good thing.
Kids will understand that one-dimensional array is an two-dimensional arrays with one row by using [?,] [,?] or [?] to any type of arry.
2) Two-dimensional arrays can be initialized (or refilled) with data by simply using Anonymous Arrays (this preserve 2 dimensional array if size is the same). This is very good also.
dim grades(3,3) grades={90,"Joe","Smith",81,"Michael","Brown",6,"Sarah","Johnson"} print grades[1,2] #result: Brown
Otherwise it would have looked like this:
dim grades(3,3) grades[0,0]=90 grades[0,1]="Joe" grades[0,2]="Smith" grades[1,0]=81 grades[1,1]="Michael" grades[1,2]="Brown" grades[2,0]=6 grades[2,1]="Sarah" grades[2,2]="Johnson" print grades[1,2] #result: Brown
3) Kids can acces data from an one-dimensional array by using two coordonates: row(0) and column(x)
REQUEST: Let's destroy the boundary between one-dimensional and two-dimensional array by accessing data in any way (no matter it is one-dimensional or two-dimensional array).
If I can fill or initialize a two-dimensional array with one-dimensional array (poit 2 above), if I can acces data from an one-dimensional array by using two coordonates (row and column) let's acces data from a two-dimensional array like an one-dimensional array when using single coordonate grades[i]
:
dim grades(3,3) grades={90,"Joe","Smith",81,"Michael","Brown",6,"Sarah","Johnson"} print grades[1,2] #result: Brown for i=0 to grades[?]-1 print grades[i] next i
If we can see number of items (size) for grades[?]
why not to can acces data in this way too regardless of whether array have one row or multiple rows.
This is very helpfull because arrays are no longer initialized with 0 or "" when we declare it.
In this case we can fill any array very easy:
dim grades(3,3) for i=0 to grades[?]-1 grades[i]=0 next i
Also, any kid will understand how items are stored in memory and how can he play with those:
dim grades(3,3) grades[0,0]=90 grades[0,1]="Joe" grades[0,2]="Smith" grades[1,0]=81 grades[1,1]="Michael" grades[1,2]="Brown" grades[2,0]=6 grades[2,1]="Sarah" grades[2,2]="Johnson" for i=0 to grades[?]-1 print grades[i] next i
or can initialize very quickly with various things (not the same):
dim x(3,3) for i=0 to x[?]-1 x[i]=rand next i
dim x(3,3) x[0]=0 for i=1 to x[?]-1 x[i]=x[i-1]+1 next i
These little improvements fit how behaves now BASIC-256.
Also bring greater flexibility and fun.
When to get an error (ERROR on line ...: Array ... index out of bounds):
dim x(3,3) x[0]=0 for i=1 to x[?]-1 x[i]=x[i-1]+1 next i print x[1,0] #3 print x[3] #3 Ok because array have 2 dimensions and we access data by using a single index (number of item) print x[2,2] #8 print x[8] #8 Ok because array have 2 dimensions and we access data by using a single index (number of item) print x[0,8] #ERROR because array have 2 dimensions and we access data by using row and column that are incorrect print x[12] #ERROR because array have less items
I hope you find these ideas useful and easy to implement.
Respectfully,
Florin Oprea
Hello,
That should not work. Bug:
That should work
Run this code:
Output:
So...
redim a(1,10)
<>redim a(10)
I fix all those things. Check attachements. The main problem is that in Interpreter.cpp x/y dimensions of an array are sometimes messy.
Y represent column number and X the row number. I mean they are inversely compared to a common array where x is the number column, and y is the number row.
Maybe it should rename all variables ydim/y to coldim/col and xdim/x to rowdim/row...
Anyway ... two attachments solve bugs. I modified version 1.99.99.49 to be easy to see the changes made.
I also added the possibility of declaring an array like this:
I find this very useful, especially as it fits with other programming languages.
Another interesting thing. Both versions below works in the current version (and older). They should be found in the documentation.
Speaking of documentation... can youadd command unassign?
Respectfully,
Florin Oprea
Getting farther and I propose you the following form for initializing arrays:
I attach the code that works (it includes proposed changes above).
Those are the final files.
Last edit: Florin Oprea 2016-07-30
1.99.99.51 - changed behaviour of 2d arrays to the "right" way. It appears to have caused no problems with sample code or test suite.
I do notfeelthe boundary between 1d and 2d arrays should be removed. It could cause much confusion when a program access the same array in different manners. If a programmer really wants to do that, they can use a 1d array and to the math to access it in any number of dimensions.