 Hi everyone, in this video, I'm going to be going over question 8 from the Compound Data 2 workshop. So this is quite a fun question because we're actually implementing all of these things that we're learning like passing an array into a function, designing a function and things like that. So this is quite a fun question and to do this, we have multiple steps we need to do, so we're going to plan a lot before we implement our function. It's always good to plan what you want to do before just going straight into it and coding. That can be quite dangerous and you might get some errors. So let's get started. So we're writing a function called n zero sub arrays that when passed a two-dimensional integer array returns the number of sub arrays that have only zeros in them. So there are two different ways you can go about this question. I might start with the easier way and then have a look at creating helper methods. So we know that's going to be our name. We know what the structure of a function looks like. We've got a return type, we've got a name, primitives like this, so I'll just have that just to get my structure there. I know my name and what's my return type? I want to return the number of sub arrays that have only zeros in them. So that will be an integer value because we're returning a counter for the number of items and then if we have a look at our parameters, we're being passed in a two-dimensional integer array. I'll call my data. Okay, great. So now we'll need to do some planning. So what we need to do is have a look at each individual sub array, work out if that sub array has only zeros and if it does increment a counter value that we can set up. So let's write some comments to create a structure and create some planning. So create a counter to hold the number of arrays with all zeros and at the end I want to return that counter. So I'll add that at the bottom. So what do I need to do? I need to loop through each index of the main array and then I need to loop through, let's indent this a bit, loop through each index of each sub array and this is where we're going to be doing a lot of our work here. So for each sub array we need to check if each value is zero. So as soon as we come across a value that's not zero, then we can stop looping and we don't need to check if we'll say in this one here, if we've checked this first number here, it's not a zero. So there's no point checking the remaining items in this sub array because we're only incrementing our counter if every item in that sub array is zero. So we need a condition that says stop looping once we've found a non-zero number. So perhaps we can create a boolean that will have true or false as to whether we've found any non-zero numbers. So that would be up here because it will be for each sub array. So create boolean to hold whether we've found a non-zero number and that could even be part of our loop condition. We can have two parts to our loop condition and then we need to check if the value is non-zero and if it is non-zero change our boolean. So once we've exited our inner loop here, what we need to do is have an if condition to check our boolean and our boolean is going to represent if we found a one. So, sorry, if we found a non-zero, so if that value is true or perhaps if that value is false that means that we've found only zeros and will increment our counter. Okay? So that looks like a plan. Let's start implementing these bits one by one. So first I want to create a counter to hold the number of arrays with all zeros. So that count will be an int. I'll call it total and it could be zero to start with and then I want to return that counter at the end. So I'll go return total. I want to loop through each index of the main array and we know how to do that. It's that same loop we've done many many times. Great and we need to close that there. Here I want to create a boolean to hold whether we found a non-zero number. So boolean non-zero is equal to false because at this point we haven't checked any numbers, so we haven't found any non-zero numbers. Okay, then we want to loop through each index of our sub array. So, I forgot something, data i dot length. And we've forgotten something here, this loop. We also said that we want this loop to stop looping as soon as we find a non-zero value. So we need to add another part to our condition here. So the index must be within that range within zero. It's initial value and data i dot length and we want non-zero. Perhaps let's let's change this. Let's say all zeros and it will be true initially. And so when we find a value that is non-zero then we change our boolean to false. And if it's true we increment our counter. So let's end all zero. Here I can say all zero is equal to true but all zero evaluates to a boolean already, so we don't need that equals equals true. Well we know that it is a boolean. So I'll just say all zero. So these two conditions must be true for our loop to loop and then we need a condition to check whether this value is non-zero. So I'll say if data i m if that value is not equal to zero then I want all zero to be false. So now if that was the case if the item was not equal to zero then this would be false and this loop would stop running because all zero will be equal to false. And so this whole boolean we know that whenever we have an and in our boolean expression that the whole thing if one of them is false then the whole thing will evaluate to false. So this will terminate our loop and now we need a condition here which we said if condition to check our boolean and increment our counter. So if all zero if it's true then that means that they're all zero then we increment our counter. And again as I said before all zero is true. So we don't need to say equals equals true because if all zero is false we've got false is equal to true which is false. So we don't need that equal sign there. We can just say all zero. And Yeah, this looks okay. We want to be careful. Oh, no, that's okay because each time we loop through the main array we're entering this outer loop and each time we enter the outer loop this all zero is being reset to true. So if you initialized declared and initialize this boolean variable up here you'd have some issues because you're not resetting it. So it's important to have it here. But if you needed access to it outside the scope of the loops then maybe you would need it up here and you can just reset it here because we know that any variables declared inside a certain scope only exist in that scope. So if I declared it here it would only exist within these brackets and perhaps in any in a different circumstance where I needed to return the boolean I'd need to have access to that outside of this scope. And so having it out here is I guess a little bit safer, but considering the function we are trying to design then that's not necessary. Great, so let's double check that this is working with this test value that they've provided us. So let's create this is integer array R equals this. So let's print out, well actually let's assign that to a variable and zero is equal to the output of. So that should give us three. So I'll go print line let's have a look awesome what if none of them had all zeros that should give us zero. Great, and what if they were all zeros and of different lengths? We want to create lots of different test cases just to ensure that our program is working correctly. Should be zero, should be five because there are five sub arrays here. Great, and because we were careful with how we did our loops and we made them dynamic by having data dot lengths rather than hard coding it to five because there are five sub arrays and hard coding this to two because in this particular case each sub array length was only two we've made this scalable so that it doesn't matter what dimensions our two dimensional array has this function will always be successful which is good we want to create functions that can be scalable it's good practice. So that's the first way of doing it another way of doing it would be creating a helper method a sub method because we can see that this function here there's a lot of things that it's trying to do so we can split that up into two different functions so that we have two different responsibilities. So perhaps what we want is a helper method that will return true or false as to whether each item in here is non-zero so that would take all of this out into a sub method. So it will return a boolean we'll say all zeros and its input will be just a regular array because we're only checking a sub array so what we can do is we can say four zero i is less than a dot length i plus plus and as soon as we come across a non-zero value if it's not equal to zero we can return false straight away and if we get to the end of this loop then we can return true so this is looping through each index of that sub array and if that item is not equal to zero returns false straight away otherwise returns true so now what I can do is I can remove all of this here and I can just make a function call so I can say boolean all zero can be equal to all zeros and I'll pass in data i which is that sub array there and if all zero is true total plus plus otherwise we don't so we could even make this a little simpler rather than having a boolean value called all zero we can just put this function call in as our condition so I'm making this even simpler now and that is a little simpler because it has two different responsibilities maybe my function name isn't that great but it's easier to debug now so this is still working should be five let's go back to our initial one just to make sure that everything's working so this should be three great and we could definitely have a look at some more test cases but I'm quite confident that this should work so yeah lots of different ways to implement this method either way that you do it is fine um yeah I think I prefer using helper methods just because it's easier to wrap your head around what's happening and separate the responsibilities and put your code into modules I find that's helpful