- [Craig] So in this video, we take a look at the concept of data structures and arrays.
(uplifting piano jingle)
So first of all, what actually is a data structure?
Well it's a specialised format for organising and storing data,
and you've been using a number of them already,
things like arrays, lists, files, records and tables.
You've actually been using very basic data structures ever since you first started to program
in the form of characters, strings, integers, reals and Booleans.
You already know that a string is a collection of alphanumeric characters,
whereas a char or character data type is a single character.
Although you use the string data type, behind the scenes,
it's actually a data structure made up of a series of separate character data types.
There are many much more complex or what we call higher-level data types.
And you'll start to be exposed to lots of these at A Level.
But they all work in fundamentally the same way,
in that they allow you to organise and store data in a certain way, following certain rules.
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.
Now you can visualise a two-dimensional array as a table with two sets of indexes,
one index for the rows and another for the columns.
And we can see how that's implemented here in Python
and how you might think about that in an abstract way below.
Now note that although lists in Python can support more than one data type,
in other words, heterogeneous,
for the exam, you need to understand that arrays fundamentally only support a single data type.
So we couldn't actually have what we're seeing here,
which is strings and integers in a 2D array.
Most other languages support arrays as a unique data structure,
and they only allow for a single data type to be stored.
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 before we move on and talk about records,
which is another data structure you need to be aware of for the exam,
pause the video and take some notes on what you've learned about arrays.
There's also a data structure available in many languages called a record data structure.
Now if you're programming in Python, this isn't something that's available to you,
but it's available in other languages such as VB, so it's worth knowing about.
The record structure is simply a collection of related fields where a field is a variable,
and each field in a record can have a different data type.
And we use it to collect together variables that are related to each other.
So in this example, we have a record that we've called Tcar containing six related fields.
Now you could call this record wherever you want.
And don't worry too much about the capital T.
It's a bit of a standard convention in many languages to start the name for a record with a capital letter T.
The important thing here is to see that we've mixed and matched six different variables and different data types
and collect them together under a generic record structure that we're calling Tcar.
There are three steps to be able to use the record data structure if your language supports it.
You define the record structure.
In other words, you tell the program what fields are going to be in it.
You declare a variable or an array to use the record structure.
And then finally, we can assign and retrieve data from the variables inside the record.
So let's look at each stage.
Now note because Python doesn't support the record structure,
the example code we're showing you here is from Visual Basic.
Don't get too worried about the actual code.
We're just getting you to understand the record data structure that's available in many languages.
So you can see we've declared the record structure and given it a name, Tcar.
And then we've simply listed all the variables that this record structure will contain.
We've listed the name of those variables and we've stated the data types for those variables.
And then we've written end structure to tell the program that's the end of our record definition.
Now we've got our template for our record definition,
we can now use it to set up variables.
So here I declare a variable called car1.
And I tell the computer that car1's data type will be the record structure that we defined earlier by Tcar.
So I could have as many cars as I wanted here; car1, car2.
I could even have an array full of different cars.
Each of them would have the template of Tcar.
And now I can start to assign variables to my car record structure.
So here you can see car1 dot reg plate becomes equal to a string.
And car1 dot price becomes equal to an integer.
And notice this dot syntax,
because of course, I could have set up lots of copies of this car record structure; car1, car12, car100.
And each one is containing its own structure and set of variables,
reg plate, make, model, price, engine size and petrol.
So on the screen there is just a summary of all the code from Visual Basic,
which you would use to define and then declare and then assign variables to a record data structure.
On the screen is a summary now of what we've taught you about record structures.
So pause the video again and take some notes.
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 arrays 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)