Introducing Arrays

//Introducing Arrays
Introducing Arrays 2016-12-23T10:02:22+00:00

Arrays – a new type of variable

So far all the variables you have used can contain only one value at a time.  This type of variable is a called a Simple Type variable.  High-level languages such as Small Basic provide programmers with a variety of additional ways of organising data into more complex and useful structures.  One of the most important is the ARRAY.

An array is a set of data items grouped together using a single identifier (variable name).  Individual items in the set are accessed using an Index or pointer to a specific element in the array.  The number of indexes needed to access an element of an array is called its dimension.  Using one index gives us a single dimensional array using two gives us a two dimensional array, and so on.  Arrays can have many dimensions but for the GCSE courses you only need to be able to use arrays with one dimension or index.  These are also knows as linear lists.

As already stated, the array’s identifier (name), and an index is used to refer to each of the array’s elements.

Array Visualisation

A way to visualise how arrays work.

The image above shows an Array with the identifier: StudentNames.  This array has 5 elements numbered 1 to 5.  StudentNames[1] contains the value “Anna”.

An example of the use of ARRAYs is in storing a list of names as shown above.  Using an ARRAY we can access each element of it using a loop to give us the required index value.   See below:

Example Array

In Small Basic an array is coded as follows:

Arrays in Small Basic

Arrays in Small Basic code.

The Identifier follows the same rules as for all variables, but is followed by a pair of square brackets.  Inside the brackets you place the Index value for the required element.  The graphic above shows the same Small Basic array as on the right.

Index values can start at any value; it doesn’t have to be 1 and there is effectively no limit on the number of elements (other than the amount of memory available to you in your computer).  Although the index values don’t have to be consecutive, for the time being it is wise to make them so as this enables you to use counted loops to assign and read their values.

Three examples using an array to store the names of the month…

Program using arrays

Using Arrays to store month names and output this month.

The program above sets up an array called ‘MonthName’ with 12 elements.  These elements have index values from 1 to 12.  The program then makes use of the Clock object to find out the number of the current month.  This is assigned to the variable ‘MonthNumber’.  This variable is then used to extract the correct element from the array and display a statement on the screen.

Although this might seem like a lot of programming for a simple task, it is an important aspect of developing your programming skills.  One variable contains 12 different values; it could just as easily contain 12000 different values, the program could still function.  Later you will learn how to assign all these values more efficiently.

Using a loop to access an array

Using a loop to access an array

This is the same program as before but it has now been modified to use a loop to access each element of the array.  The loop ‘iterates‘ through each element of the array and prints 12 simple statements on the TextWindow – Month No ‘x’ is ‘MonthName[x]’.  Whenever we use an array, the chances are we will also use a loop to access it.  The next tab shows this in action again…

Create the program and save it.

Using arrays to collect days in a month

Code to collect the number of days in each month.

This program shows how, once the initial array has been created, a second array can be created as the program runs providing additional information about each month.  In this case, the program asks the user to enter the number of days in each month.

Notice the use of prompts (using TextWindow.Write() to make sure that the cursor stays on the same line as the prompt.  Also notice that I add an extra space at the end of the prompt to make sure it is clear.  I also use TextWindow.ReadNumber() to make sure that I get numerical answers.

Create this program and save it.

Red Cube

In Small Basic Arrays have some specific methods that can be used.  Only some are useful.  Others perform tasks that can be done directly in most situations.  This pop-up provides a description of these methods for completeness.

This extension task uses an array to store how many times each number is thrown on a die, when it is thrown 100 times.  It would work equally well whenever a tally count is being created.

If, for example, you were counting vehicle types in a traffic survey, each element of the array could be used to represent the different types of vehicle.  Do have a go at programming this task – you will be asked to do one later in the course so get a head start now.

End of Topic Tasks