- [Craig] In this video, we're going to be taking a look at the use of arrays.
(uplifting piano jingle)
So an array can be thought of like a variable which can contain more than one data item.
For example, we could be storing a list of names,
and we can do that by allocating a continuous part of memory to storing that data.
So you can see here we have a number of memory locations in memory,
and starting at memory address five,
we've then started storing contiguously a list of names which will be part of our variable.
Note that lists, which are probably what you might be familiar with if you're programming in Python,
are different to arrays because they're not contiguous,
but for the purpose of your exam, you can think of arrays as storing contiguous data items.
So our program will know where in memory our arrays starts; in this case, address five.
And we can therefore use an index relative to the start point to allow us to easily access the array's contents.
So notice arrays are typically zero indexed, which means Jane is at index two, not index three.
It is in the third position in the array but because we start at zero, it's at index two.
So we're going to use some code example here from Python.
Now, just a quick note.
Python does actually use arrays, but it has methods that allow them to be used like lists by a programmer.
And that's why most people think Python only supports lists,
but this little extra level of complication is beyond the spec.
For all intents and purposes at GCSE, you can effectively think of lists and arrays as the same things,
although that's not strictly true.
So here in this code example, this line sets up a one-dimensional array of strings called countries.
It assigns it some initial values.
Angola at index zero, Austria index one, and Belgium at index two.
And again, note how the indexes typically start at zero and not one.
To output Austria from the array, we would therefore need to use index one,
as index one represents the second item in our countries array.
Arrays are a static data structure, and that means you can't change the size of them once you've set them up.
Now, this is a good example of where Python is allowing the array to behave like a list.
Although you can't tell because this has been abstracted from you,
Python is actually moving the entire data structure to a new part of the memory to ensure it's contiguous.
So with Python, we can insert an item into the new location at the end of the array using its index.
So here we've added Canada to the third index or the fourth position in the countries array.
You can also have two-dimensional arrays,
and you can visualise in your head a two-dimensional array as looking like a table,
where we now need two sets of indexes, one to index the rows and another to index the columns.
So to access Angola, you'd be going to index zero zero, as shown here.
Countries zero one contains the integer 1246700, and countries one one contains the value 83871.
So let's just recap what we've covered.
An array is a static number of related data items that are stored together in the same memory space.
Each data item has to have the same data type
and the particular data item, the element, is found using the index for the array.
Indexes usually start at zero for the first data item, known as a zero indexed array,
and arrays may be single or multidimensional.
You can visualise dimensions as a column when it's a single-dimensional array
or a table where it's a two-dimensional array.
Now a quick note and something which is often asked by students and teachers,
that when we say index one zero, we could be accessing value 112.
It depends how you actually choose to store the data behind the scenes.
There is not a fixed way of referencing the data in an array.
You don't go, for example, always down the X axis and then the Y axis.
It doesn't matter when you visualise rows or columns first,
as long as you're consistent in your program.
So read the questions carefully in an exam so you are sure which way round the data is being stored.
And here we see how arrays will be shown to you in exam papers using the OCR Exam Reference Language.
So arrays will be zero based in the exam and they'll be declared with the keyword array.
So here we say array, then we give it a name.
And this time we're calling it names.
And we've said five, meaning that we're declaring an array, it's going to have five elements to it.
And you can see there, indexes zero through four have been given names.
We can then print out a particular element by calling the name of the array,
in this case names, and the index supplied in square brackets.
You can see the syntax for a 2D array is exactly the same,
apart from now we need a comma inside the square brackets because we need to provide two indexes.
Now that's where the specification stops
because in the exam, you only need to know about one-dimensional and two-dimensional arrays.
If you're interested to know about three-dimensional or higher-dimensional arrays,
then you can watch the rest of this video, but there's no need to take notes.
And higher-dimensional arrays is something you'll probably cover at A Level.
So you already know that you can visualise as an abstraction
a one-dimensional array as a simple column and a two-dimensional array as a table.
Well, it doesn't take much of a stretch then for you to realise
that a three-dimensional array in your head could be visualised as a cube.
You could reference any item in a three-dimensional array by giving three index values,
one for the height, one for the length and one for the depth.
So here we're accessing two two two.
We're going down two, across two and two rows deep into our cube.
Now at that point, people think well surely we can't go higher than a three-dimensional array.
How could we represent a four-dimensional array?
And although that can be a difficult concept for us to wrap our heads around because we live in a three-dimensional world,
a computer doesn't care about that.
It can easily have a fourth-dimensional or fifth-dimensional or higher-dimensional array.
And actually, it's not that hard for us to visualise
even though we only live in a three-dimensional world.
So this is how you could potentially visualise a four-dimensional array.
We just take a set of cubes.
We'd have to supply four indexes to this array to access any one of the elements.
The first index specifies which cube we look at,
and then the next three specify the width, the depth and the height.
In a similar manner, we could carry on visualising fifth, sixth, seventh and higher-dimensional arrays,
although this does get a little confusing.
So here we have a fifth-dimensional array with one suggestion of how you could visualise it.
You'd have to supply five index values to access any particular element in a five-dimensional array.
The first two here could be telling us the row and the column of the cube to access,
and the final three, the row, column and depth of that cube.
Now it's unlikely you're ever going to be dealing with five-dimensional ar rays, even at A Level,
but it just shows how computers are not really limited,
and we can often take concepts to the next level quite easily once we understand some of the basics.
(uplifting piano jingle)