The Trick Of The Mind - Representation
Written by Mike James   
Monday, 18 July 2022
Article Index
The Trick Of The Mind - Representation
Strings and Arrays
The Record

Strings & Arrays

At the moment we have the idea of a variable which is capable of storing one single value at a time. What this means is that we can use instructions like:

FirstCharacter = “H”

If you want to carry on with the full text of “Hello World” you would have to write something like

SecondCharacter = “e”
ThirdCharacter = “l”

and so on. Each variable would store the numeric code corresponding to the letter in the encoding in use. This isn’t particularly convenient but it works, even if it makes working with the complete “Hello World” a character at a time tedious.

A much better idea is to invent a new sort of variable, the array, which is a set of storage locations each one capable of storing a value. To pick which element of the array you want to refer to you simply give its position in the array or list. For example:

message[1] = “H”
message[2] = “e”
message[3] = “l”

and so on.

The advantage of this is that now you can refer to the entire set of characters with the single name message. A set of characters defined in this way is generally called a string – from string of characters. The way in which it is implemented varies greatly among different computer languages, but the idea is the same. Clearly having to store each character in each element of the array or string would be tedious so most computer language let you write:

message=”HelloWorld”


and each character is automatically converted to a code value and stored in the correct element of the string. That is, after this instruction message[1] contains H, message[2] contains e and so on.

array

Each item in an array is called an array element or just an element. The number used to select the particular element you want to work with is generally called an index. So:

message[1]

is an element of the message array and its index is 1. Notice that an index can be specified by a variable – an index variable. For example:

message[x]

is whatever element of the message array is indicated by the current value of x. This is a powerful idea because it allows you to write instructions that process every element of an array depending on the value of the index variable.

 

It is worth mentioning at this point that most computer languages start counting array elements from zero rather than one. Arrays are categorized as zero-based or one-based. This is a more general example of the fact that mathematicians count from zero. Given that most of the rest of the population count from one this leads to much confusion. So in our example the first character would be message[0] which would contain H. The real reason why programmers count from zero is nothing really to do with mathematics and more to do with the way computers work, but this isn’t really relevant to our current story.

Notice that the array of characters has a de facto order. There is a first character, a second and so on. In this sense the array is like a numbered list and indeed that is what it represents – an array is a representation of a numbered list.

Arrays of Numbers

Given we have an array of characters, why not an array of numbers, or of any other type of data that represents something? The idea of an array is completely general, as is the idea of a numbered list. You can have a numbered list of anything and you can have an array of anything.

There is a restriction, however, in most programming languages that the elements of an array all have to be of the same type – that is all of the elements of a numeric array have to be numbers and all of the elements of a string array have to be characters. This isn’t actually necessary and is the result of wanting to make arrays easier to implement rather than easier to use. Python relaxes the usual rule and has an array that it calls a List which can be used to store any type of data.

Most often an array is an array of numbers and this works in the same way as an array of characters. The only real difference is that there is often no shortcut for storing numeric values in an array and the only thing you can do is store values one at a time:

value[0]=42
value[1]=43
value[2]=44

and so on. More enlightened languages, Python, C and JavaScript for example, allow you to store values directly in the same way as a string. For example:

value = [42,43,44]

stores all three items of data in value as the elements of an array.

Arrays are representations of numbered lists and you use them for the same sorts of things. You can set up a shopping list and search it for a particular item, add new items, remove existing items and so on. You can set up a list of numbers representing the results of some experiment and you can access each value and work out statistics such as the average and so on. As we will see in the next chapter, arrays and loops go together as if they were made for one another.



Last Updated ( Monday, 18 July 2022 )