Monday, June 21, 2021

Why Are Arrays So Hard For Beginners?

Beginner programmers often, dare I say usually, struggle with arrays. Sometimes it is hard for experienced programmers like myself to understand why but with years of using them in many languages they’re second nature for us

Beginners have trouble differentiating between the index and the contents of a part of an array.That seems to be the most common issue. Somehow the idea that the index is, in some sense, part of the name and not the value being stored is hard for some to grasp. Though come to think of it, I never talked about an index as being like a name. Perhaps I should but as a very experienced programmer with a background in Assembly language words like index and offset are natural to me. Beginners should not need to know how arrays are stored and accessed at the Assembly or machine language levels. Perhaps I will discuss indexes as part of the name next time I teach arrays. What do others think? Or what sort of language do you use?

While vocabulary is important, and students should understand words like offset and index, perhaps we need vocabulary that is meaningful to beginners before we teach them the vocabulary of experts. It’s a thought.

And then there is the whole “index values start at zero” thing which I have written about in the past. Zero Zero Indexing Considered Harmful

The other think I have been thinking about that seems hard  for some beginners is the need to initialize an array. They tend to assume that if one declares an array the elements in it should be assigned some sort of default value. Of course some programming languages do this for some data types. Numeric types get set to zero for example. It’s seldom a good idea to depend on this action and most of us do teach students to initialize the elements in an array. It’s a great way to tie arrays and loops together early in the learning process. I wonder why there are not library routines for this sort of thing. The .NET Framework has Array.Sort, among other things, so why not Array.Initialize? That’s probably not practical for arrays of many types but for some of the built in types why not?

On Twitter last week someone suggested that perhaps the problem was with arrays themselves. That seems reasonable but solving that problem is for people smarter than me. I’m just asking questions.


Karyn Voldstad said...

Arrays with indices relates very closely to sequences in Algebra - aside from that pesky 0 start. I usually introduce it that way. Most learn the 0 start easily since it’s strange, and makes them feel part of a select group: programmers.

Doug said...

That's an interesting notion, Alfred. I've always found that it was one of those make or break moments for aspiring programmers so I probably spent a disproportionate amount of time one it.

I always approached it not as a programming topic but as a logical problem. I introduced it as solving a problem going door to door in an apartment building where the locations were all in one building but units were distinguished just by a number. It seemed to work well and extends nicely into 2D arrays.

I'd be willing to bet that most CS teachers struggle with effectively introducing the concept. Thanks for bringing it forward.