 The purpose of this video is to give you an introduction to compound data. Everything we've dealt with so far, inks and floats and even chars and booleans, are really just numbers or under the covers they're numbers, even char and boolean are under the covers, just numbers. They don't have any structure. If we think about things, for instance, in maths, when we think about XY coordinates, okay, what's the location on a two-dimensional plane? It's got two parts to it. It's got an X part and a Y part. We want to look at how we can represent that in processing. We can have a P vector, which corresponds to that, and we're going to do some more detail about it shortly. But briefly, we can see that it's got two parts to it. There's an X part to it, a PX and a PY. And we can do operations on it there. And here we're trying to work out the distance from the origin, which is the square root of X squared plus Y squared. Let's go into and have a bit more detail of what P vectors are actually about. So, as I say, with compound data, we don't just have a single number. We've got parts and the parts have names. So here with the P vector, we've got an X and a Y. There are other ways of doing this too. Examples where you can have different names for parts, depending on what your actual object is in the first place. We need to set aside memory for these things because it's not just a single number anymore. Like a P vector is two numbers. Let's look in detail a P vector. How do we say we want to create a P vector? We know if we want to set up an integer variable. We just say int X and that would be declaring X. Well here we say P vector P and that's just declaring P to be of type P vector. Before we can actually do anything useful with P, we need to allocate some memory to it. And here we have an example. And we see this keyword here, new. We'll see this again a bit later. New is asking for some memory for our data. In this case we're asking for memory for a P vector. And we're initializing this P vector with these two values here 3 and 4 which correspond to the X part and the Y part. So now we have our variable P. It's a valid P vector. And now we can do something with it. And also we can collapse these two lines into just a single line. Just like when we declare a variable, we can say int X, semicolon, and the next line we can say X equals 7. We could just combine that into a single line and we just say int X equals 7. And the same thing here, we can just combine these two lines into a single line if that's appropriate for our program. What happens in memory? Well we used to, if we have a variable say Z, we can say okay, there was a little box in memory that holds the value of an integer variable Z. But now when we've got compound data, we have a block of data somewhere that holds our compound data. So here we've got a P vector and the X part of it has value 3 and the Y part has value 4. So this is a chunk of memory here. And then here's our variable P which was declared to be a P vector variable. And inside its memory location is a reference to our block of data over here that holds the actual P vector. And we'll see this is an important distinction to make later. The P vector is not all squeezed inside P because there's no room to fit it in there. P just holds a single value which is a reference to this chunk of memory. You can think of it as an address in memory. It's essentially something like that. So let's have a look at a P vector example. Let's think about the example of an ellipse that falls down the screen. So we've got a 400 by 400 window. Let's create a P vector here. So I'm creating a P vector P and I'm going to initialize it to a particular value. So I want to start my P vector so that it's at the top of the window. So it's about halfway across and top. So x, y, 200, 0. And now I need to use the P vector down here just like if I was having variables x and y to represent where the ellipse is I'd need to be doing something with them. So down here I need to access the x part of the P vector. So P vector. And now the notation that we use is we put a dot. Dot sort of says get me this part. So P dot x says get me the x part of P. So at the moment the x part of P would be the value 200. And then the y part. You can put that in as well. So we can just run this at the moment just to see what it looks like. And we see it's not very exciting. It's not going anywhere. We need to add some animation. So we know how to do that. We want the y coordinate to increase. So we've got a P vector here. The y coordinate is the second part, the y part. So let's reference the y part. So there we go. P dot y, that's the y part. And although it looks messy P dot y, just think of it as a single variable. I can do things to it just like I would with any variable that we've been used up to now. And what I'd like you to do is just add one to it. Well, we know how to short hand way to add one to something. Let's go plus, plus, plus, right? Maybe we don't like the space there. P dot y, plus, plus. Let's just go in to increase that. So now we run this. There we go. Down the screen. And off it goes down the bottom. Okay. So let's try to make this a little bit more interesting. We want to click the mouse on our window here and have the circle start from that position again. So we need to put something down in mouse pressed for that. So I could say down here P dot x equals mouse x and P dot y equals mouse y. And that means every time I click I'm effectively resetting the center of the circle, which is given by our P vector P. I'm resetting that to be wherever the mouse is and then it'll just start falling down from there. So let's run it and see how it goes. There it goes. So I click somewhere and it just falls down from there. Okay. So that's all very straightforward. We could do this another way here. Let's just comment this out for the moment. I could say, oh well, I don't like the current value I've got for P. I can give it a new value. So I can just say P equals new P vector. And I know what value is I want to give it. I want to give it mouse x and mouse y for the two parts. So just like I initialized it up here to 200, 0, I can just initialize it here to mouse x, mouse y, and it's just going to have the same effect as before. Again, I can just click around and that works. Okay. So let's go back to the presentation here. So with P vector we had a compound data object there that had named parts. Now we want to look into another sort of compound object and array. So what's this about? So we wanted to have instead of one circle going down the screen, we wanted to have lots and lots of them. Well, then I could start writing things like this, P vector, ball 1, ball 2, ball 3, and there we go, 100 of them. Well, that's all pretty tedious to write all that. And it's also pretty tedious to write all the code to update, to draw everything and update everything. So how do we get around this? So this is the motivation for having an array. Whereas P vector had named parts, an x-part and a y-part. An array has numbered parts. So here we've got a drawing corresponding to an array with seven boxes, seven slots in it. And these slots are numbered, in this case, from zero up to six. And we always start from zero when we're numbering things for an array. So when we use a P vector, that's just good for holding numbers. An array, excuse me, an array is much more flexible than that. We can say what type of data the array is going to hold. For instance, an integer. And that applies to everything in the array. You can't have some parts of the array holding ints and other parts holding floats. Excuse me. It applies to the whole array. And we can also say how big we want the array to be. How many boxes, how many elements are in the array. So here we go declaring an array. So square brackets is used all over the place when we do stuff with arrays. When we want to declare a variable to say that it's an array of integers. So here's our variable name, DaymaxTemps. And here's int to say what the things are in the array. And the square brackets, that's what says hey, I'm an array. So int bracket bracket says this variable is an integer array. Now we need to set aside some memory for the array. And again, we use the keyword new to get some memory. And we've got a different way of saying it here. Array's got a different syntax to P vector. So we say and it's motivated by the why we declare the array here with the int with the square brackets. Now when we want to allocate some specific memory for it. Again, we set the int. But now we put a number in here to say how many elements we want our array to have. So let's say we want something for each day of the week. We've got seven days. So we've got an array of seven integers perhaps to hold the maximum temperatures for each day of the week. And just like we saw with the P vector, we can collapse that down instead of having two lines. We can just do it in a single line if we want to. And there we go. We declare our variable to be an array of integers and we allocate memory to it all in one go. And then how do we get to the parts of the array? Well, we saw with the P vector, we use that dot x and dot y notation. An array has a different type of notation. It uses square brackets again. And so we say the name of the array. Excuse me. And then we give square brackets and then we say which box we're looking at in the array. Remember we're counting from zero. So we're going 0123. It's that box we're looking at. And so this would just put 22 into box number three in the array. The next line we're saying get whatever values in box number two in the array. Add seven to it and put it into box number five. Basically, once we've got an array name and we've got square brackets and a number after it, then we can just treat these like we used to treat ordinary variables like we just got a variable x. We can just do things like x equals 22 x equals y plus seven. I mean these are just the array equivalents to that. We're just dealing with individual elements of arrays. The individual elements of arrays act just like the integer variables we're used to dealing with. How do we put numbers into array? Well, there are lots of ways you can do this. One way is to just do a whole lot of assignment statements here. We're just putting 19 into the position zero in the array, 23 into position one all the way down to 31 into position six. Remember here, our array is of length seven, but that means these numbers here, we call this the index into the array, the index range goes from zero up to six. That's zero up to six is in fact seven numbers, corresponding to the seven we've got up the top here. If we're going to initialize an array, just the whole set of numbers at the beginning, there's a shorthand way of doing this. On the line where we declare the array, and we just put the numbers in curly brackets, and that's as you can see much, much simpler than this previous slide here. The memory diagram is not hugely different to what we saw with the p vector. Again, we've got our array variable p, and here, I'm just drawing it so it's very analogous to the p vector one, I've even got the same name for the variable here. We've got a variable p, and it's got a reference to where the memory is that holds the arrays. The array, you can see it's seven numbers in memory, they're all together, and it's got these seven integers inside it there. And these numbers on the outside here, and they're not part of the memory, that's just for us humans to read, the idea, I mean, the computer knows how to get to a particular number, like if it wants to get to, for instance, the four element of the array, then it knows to just add four to, effectively, where the first box is, it'll get to the fourth box. So that's how it does it, it doesn't have these numbers written in memory, all that's in memory of the actual contents of the array elements there. We want to often know how long an array is, let me show if we've just created it, we can just see what it is, but often in your program, you're in a different place there and you don't know how long the array is. So here, similarly to the p-vector thing, we use a dot here, so the array name, dot length, tells us the length of the array. So this one, it would give us the value seven. And the big thing we do with arrays is we generally write loops, because you don't want to go through and deal with things individually, just like the balls we had at the beginning, you don't want to have to deal with each ball separately, you don't have to deal with each array element separately, so you want to have a loop to go through it. And this is very much, this first line is very much the standard sort of loop that you'd write to go through an array. Start off at zero, because we saw that array indexing starts at zero, and you'll notice here that we've got i, so i's going up by one at a time, and i less than the length. So think about this array we set up, it's got length seven, but i should just go through the range nought one, two, three, four, five, six, because as you saw back here, they're the valid index numbers there, nought up to six. Seven's the length, but i never takes the value seven, because there isn't a number seven box in that array. And then here if we want to just print out all the numbers, we'll see i goes through the values nought to six, and what we do down here, so for each value of i we print this, so initially we'll print daymax temps zero, we'll get the first number coming out, then daymax temps one, we'll get printed out, then daymax temps two, etc. So we'll just see the numbers nineteen, twenty-three, twenty-six, twenty-two, twenty-nine, thirty-three and thirty-one getting printed out. And something just a little bit more complicated if we wanted to add up all the numbers in that array, there's a fairly standard sort of thing that you do with arrays, you have another variable, a helper variable, that keeps track, it's like a partial sum, starts off at zero, and then as you go around this loop, we're doing the same loop here, this first line of the loop is exactly the same as on the previous slide, but what are we doing? When we visit each element in the array, we're just adding it into the sum. So that means initially sum is zero, and we start the loop, i is zero, so daymax temps zero is nineteen, if I remember correctly, and so sum now becomes nineteen, and then we go to, i gets bumped up to one, and then, I can't remember what the number was, what is it, it's twenty-three. So then daymax temps one is twenty-three, so twenty-three gets added to sum, so we've got twenty-three plus nineteen, which is forty-two, sum is forty-two. And then we get to the next i value, when i is two, and that'll get added to the sum. So basically this is just adding each of those numbers, nineteen, twenty-three, twenty-six to sum. When the loop is finished, sum will be in fact the total of all those numbers in the array. And then if you wanted to get the average, then you could just take the sum divided by the length of the array, and you've got the average.