arr(x,y)
is fine.
And better than
arr[x][y]
… from a beginner’s point of view, imho.
Which suggests it can be implemented just as Vector was, using round brackets.
arr(x,y)
is fine.
And better than
arr[x][y]
… from a beginner’s point of view, imho.
Which suggests it can be implemented just as Vector was, using round brackets.
I usually use the name arr
for declaring array variables!
So something more mouthful like makeArr2d(rows, cols)
would be safer.
The advantage of using the double [][]
brackets is clear during assignment and is a simple extension of the 1D array assignment e.g.
arr[x][y] = 3.14159; // OK works fine
println(arr[x][y]); // outputs 3.14149
if the 2D array is implemented using a class, like PVector then the equivalent might be thought of as
arr(x, y) = 3.14159; // illegal syntax will not compile
you would need something like
arr(x, y, 3.14159);
The actual syntax would be determined by the class implementation.
I assume that your students are using standard 1D array syntax []
so using [][]
for assigning and retrieving values should be a logical step in their learning. The real difficulty is understanding how to create the array in the first place
So expanding on @GoToLoop’s idea this function will create a 2D array of floats
float[][] makeArray2D(int nbrCols, int nbrRows){
float[][] a = new float[nbrCols][];
for(int i = 0; i < nbrCols; i++)
a[i] = new float[nbrRows];
return a;
}
// and using it
float [][] array;
void setup(){
array = makeArray2D(9,3);
}
so the indices are [col_nbr][row_nbr]
which is probably what you want for graphics but it is simple enough to change the implementation to get [row_nbr][col_nbr]
indices
Nice thread, some great input.
I’ll inject an observation out from left field: I feel we’re discussing solutions although the issue at hand is not yet agreed upon. All here are giving great input, but I don’t feel we’re all taking about the same problem yet.
OP posits – based on their tutoring observations – that the current array implementation is too high a barrier for novices. But what does this actually mean?
Do beginners bounce off of the square bracket syntax? Or is the core concept of an array just a way of thinking that’s not very intuitive? Is the use of indices too opaque? Is it something else? And before everything: is there evidence that this observation is a wide-spread issue at all?
Whatever the concrete problem definition is, the solution will get pulled into different directions.
I don’t think I’m overestimating just how much novices struggle with code. From my own experiences in attending creative coding courses during my studies, I can tell you that the mere presence of “strange” characters like ; and { and & put the fear in a lot of fellow pupils, e.g. even after the course, they where struggling in finding how to type a brace. Sounds stupid, but that’s a reality.
Everybody would eventually come around to “getting” the idea behind variables. They’d come to understand the difference between int and float. They’d be able to draw a smiley face using basic shapes. Maybe they’d get a basic if() statement running. But anything more complex would become a really really high barrier. I’m not kidding. (Although I will caveat, that this was a low-weighted course on the curriculum and motivations to perform… varied).
What I’m saying: for all the technical details that might need sussing out in this discussion, never forget to step out of your expert mode and go down to the level of full beginners and consider their immediate woes.
Alternatively…
There may also be a hard truth that coding can NOT be made universally accessible. As in, higher foundational concepts – like arrays – do require that the person puts in that effort necessary to rewire their brain into coding-mode. It’s clearly not for everyone and no amount of abstraction and simplification will ever make it easily accessible.
Uff, wall of text. Sorry for that.
100% @eightohnine - you are absolutely right to redirect us back to the core issue - accessibility to non-experts.
The reason I made this suggestion is that (1) I do care a lot about removing barriers, and (2) I have seen how those same beginners find arrays much easier in other languages.
I’d also like to redirect those interested in the technical discussion away from “square vs round brackets” back to the issue of every programmer in javascript has to “roll their own” arrays. That’s just not good in 2024.
By the way, I’m not singling out javascript. Python doesn’ have arrays - you have to use numpy.
Fortran 77 - yes, from 1977 - does have arrays. Eg real A(3,5)
creates a variable A which is a 2-d array of real values with dimensions 3 by 5.