 Hi everyone. In this video I'm going to be going over question 7 from the Compound Data 2 workshop. So in this question, similar to some of the previous loop and compound data questions we've done, we have some values that we want to assign to an array, and we need to work out what that pattern is so that we can do it using a loop, and in this case it's a nested loop. And it needs to be done so it can work for any size of array. That's what they have in this note. You don't need to use the size of the array. So again, that's just to encourage you to not hard code your condition when you're writing your loop. So I've already written my loops over here. I've just created a two-dimensional array with size 5 by 8, but you can do whatever you want. And this is the loop that we're going to use. Because we have two dimensions of arrays that we need to go through, there are two nested loops that we need. So we need this outer loop here, which is that standard loop that goes through just a regular array, and that will go through that main array. So in this case, or in my case, the array of five places. And then what I need is a nested loop that will go through each individual sub-array in each of those five array sections in the main array. So this is that loop that you've seen many times now, so that should be familiar. And this is the part that I find people get stuck with. It's a very similar loop, but the condition is different. The condition says that m, which is representing the index of our sub-array, m must be less than r i dot length. So that is referring to the length of the current sub-array that you're in. So that's really important to have that there, and it's often something that people forget to put in their loop. And because of that, they tend to get some index out of bounds exceptions, which we don't want. Okay, so again, what we want to do here is having a look at our indexes. Here we have two. We have the main array index and we have the sub-array index. We want to have a look at each individual index and see if we can find a pattern with them, or a relationship between an index and the value. And it's sort of easier, but also sort of harder because you've got two indexes to consider now. So in some ways that makes it easier because you have more options, but also just imagining it in your head and trying to get an understanding of what's happening in the loop can be a bit tricky. So I've got my table up over here, and I'm going to write down my patterns. So let's have a look at 7.1 first. When our indexes are 0, 0 with the value of 0, 0, 1 at 0, 0, 2 at 0. Let's have a look at 1. That's 111, 22. I've missed one down the bottom here, 2. So here we want to rewrite all of our values in terms of i, in terms of m, or a combination of the two. So if we have a look closely at this one here, you'll notice that whatever the i value is, or whatever the main array index is, is whatever the value is. So this is quite a straightforward one. We can just say that the value is equal to i. Because if you compare those two columns there, you'll see that they're equal. So 7.1 for each item, you've got i there. So what would that look like if we were going through memory? I might not do as big of an array, two-dimensional arrays, that one. Let's do 3 by 2. That'd be a bit messy. Oh goodness, that is very messy. Okay, so this is what our array looks like, and I'll move this over here so we can trace it. So we've got i is equal to 0, i is less than array dot length, i++. So i is referring to these indexes here. So the first time we look around, we're looking at i is equal to 0, which is this here, which means we're looking at this sub array. Once we're in that sub array, we need to loop through each of these values here. So for that I've got m, I've got m is equal to 0, m must be less than array i dot length. So i, sorry everyone, i is currently 0. So array 0 refers to this array here, and the length of that is 2, and then m++. So first of all we've got i is equal to 0, m is equal to 0, and array 00, which is referring to this space here, is equal to i, and i is 0 right now. Then we've got, we're stuck in this inner loop here, so that means when we get to the end of this loop, we've got m++, which means m is now equal to 1, is 1 less than array i dot length. We said the length of this sub array here was 2, so 1 is less than 2 is true. So we've got r01 is equal to i, which is 0. And then I get to the end of this sub array, and at this point I've got m++, so now m is equal to 2. I've got this 2 less than 2, false, which means I'll get to the bottom of that, I'll exit from this nested loop, and get to the bottom of my outer loop where i++. So that means that we are jumping out of this loop here. We've lost reference to m, because remember m only exists in the scope that it was declared in, and it was declared inside that inner loop, and we've exited from the inner loop now. And then we've got i++, so i was 0, we're plusing 1, so now it's 1, which means we're moving onto this one here. So hopefully that's helping you visualize what's actually happening here. And then of course we've got 1 is less than array dot length. The length of this array is 3, so 1 is less than 3, so we'll jump back down here to the inner loop, where we've got m is equal to 0. And now because we are in this sub-array, the m we are referring to are these here, or the index we're referring to. So again we've got that same loop, m is equal to 0. So we've got array 1, 0 is equal to i, i is 1, so we've got that there. Then m++, so m is equal to 1, so array 1, 1 is equal to 1. And then at this point m is equal to 2, which means that inner loop has been terminated. We've lost our reference to m, it only exists within the scope of that nested loop. We've exited here, we've gotten to the end here, and we've got i++. So i is no longer 1, it's 2. So you get the id here, so I'll speak through this. So I'm referring to this nested loop here. So m is referring to these indexes here. And I've got array 2, 0 is equal to 2. Loop around again, m++, array 2, 1 is equal to 2. Exited from our inner loop, i++, i is now equal to 3. And that no longer meets this condition here, which means we're exiting from our outer loop. But that's okay because we've already assigned all of our values. So that's what's going on behind the scenes. So hopefully that's given you a better idea of what's happening. And with that we can move on to 7.2. So 7.2, we've got 012, 012, 012. So a very similar situation here. If we have a look at our second index here, we'll notice that all of these values are equal to whatever the array value is. So we can say that array im is equal to whatever m is. You can see that pattern there, it's the same value. So those two are a little more straightforward than the other ones. The other ones are a little bit tricky. They require a combination of the values. So let's have a look at the next one here. The next one I've got 012, 1, 2, 3, 2, 3, 4. So this one isn't as straightforward and this requires a bit of trial and error. A combination of maybe the two indexes, maybe multiplying just one index. You just want to try bits and pieces here. So I'm going to try i plus m, see how that goes. So 0 plus 0 is 0, 0 plus 1 is 1, 0 plus 2 is 2, 1 plus 0, 1 plus 1, 1 plus 2, 2 plus 0, 2 plus 1, 2 plus 2. Okay, great. So I've found my, what would you call it, operation. That's probably not the right word, but you get what I'm talking about. Formula, maybe, I don't know. If you multiply, if you add the two indexes together, that's what your value is going to be. So working out what that pattern is sometimes just requires a bit of testing. Just having a look at the relationship between I am and the value. And that also comes with practice and exposure to different scenarios or different patterns. So that's that one there. And that last one is a little trickier. So let's remove these ones here. So I've got 012, 024, 036. Okay, so this one isn't as straightforward. And it requires a bit more of analyzing the values and the indexes here. So if I have a look this way for each row, it looks like we're looking at times tables here. So 012, 024, 036. And I'd imagine if you keep going, you'll do the one times tables, two times tables, three times tables. So maybe it has something to do with that. So if we have a look here, if these are the one times tables, this is one times zero, this is one times one, this is one times two. Have a look here, this is two times zero, two times one, two times two. Maybe I should be writing these down. So one times one, one times two, two times zero, two times one, two times two, three times zero, three times one, three times two. So let's have a look if we can relate that to any indexes here. So if I have a look at these values here that I'm multiplying each number by, that is the same as our m value, our subarray index. So I've got part of it. But what I don't have is the first part of it, because if you have a look at i, in this case here, when i is zero, I'm looking at one. When i is one, I'm looking at two. When i is two, I'm looking at three. So I need a bit more unpacking here. So I look at the relationship between i and that green value I've highlighted on the right. And if you have a look at each of those situations, you can see that whatever the value is, it's just i plus one. So what I can do, I'll replace this to zero plus one times, well we know that it's m, so I'll just add m. This is zero plus one, or maybe I won't do m. It's not as clear. Times zero to one, zero plus one times two, one plus one times zero. Oops, goodness. I've made this table too small. Okay, so now what I have is for each of my i values, I've now got these as my i values. And the remaining part of that expression there, expression is probably the right word, is just plus one and a multiplication. So that's consistent for each of them. So I've worked out what my formula here is, and it is i plus one times m. And you might have unpacked that and you've done mi plus m. That's fine as well, either one works. Maybe you found a different formula altogether. Either way is fine. I'm going to do this one here, so I'll say i plus one times m is my formula. So that one in particular is a lot more difficult. It requires a bit more time and testing and analyzing what the values could be. So if you get a question like that in an exam, sometimes it's helpful to have a piece of paper out so that you can draw maybe a table like this and test out some values with a pencil and an eraser and you can just have a look at different combinations. And these are definitely the more difficult questions here, so I would recommend having some practice of those.