 at question six from the compound data one workshop. So for this question here I've got an integer array with eight spaces and I've already got this loop that we again know goes through each item in an array and then for each of those items it assigns a random number between zero and nine. We know it's not 10 because that number is exclusive and because the random function returns a float we've got this int casting which rounds it down to the closest int. So if I run this code and it's printing out each one there when I run this code I've got eight random numbers between zero and nine but that also means that every time I run my code again I'll get a different collection of numbers because each time I run the code I'm getting a new random number for each number so you can see I've got different ones here and they will be different each time which is good because it means that we don't have to or we've got a nice way to test our code we can just run our code again and make sure that it works for any given eight numbers between zero and nine. So there are three things that we want to do let's have a look at the first one first the first one is we want to find the total of all items in array r so we want the sum of all items and we saw this question when we traced 3.4 so actually it's the same code as 3.4 what we need is a variable which you can call whatever you want I'm going to call mine total and I'll make it equal to zero and then I want to loop through my array and for each item in my array I want to add that item to my total so I can get the sum of all of them so write that same loop and then for each item or I don't want to do that I want to add that item to my total so this will go through each item in my array and then I've got total plus equals array i so we'll add that value so let's test this and make sure that it works I'll add a print line statement after here and I'll write a little comment as well total of items is and then I'll concatenate that with total because I'm going to have a few print line statements it's nice to know what each one is for so I've got the total of items or because this is a print line so I'll add a new line here cool I've got the total of items is 41 let's double check we've got four seven twelve twenty twenty nine thirty three thirty seven forty one great testing my math skills this morning but that works well and if I run that again we can make sure it works by let's get some smaller numbers that's a bit nicer four thirteen twenty twenty five twenty six twenty nine thirty one awesome so that one's working as expected great so let's have a look at question two now question two is asking for the number of items in the array that are greater than five so in this case here with these numbers this should give us two because there are only two numbers greater than five so again we want to loop through each item in our array so I'm going to copy this loop but I don't want to be adding the item to our total I want to be checking that item checking if its value is greater than five and if it is greater than five I want to add one to our counter because we've found one more item so let's create an integer variable keep track of that counter I'll call it greater than five and to check whether a number is greater than five we can use a condition so we say if the item is greater than five then we should just plus one to greater than five so if the item isn't greater than five this condition won't be run it'll be false and it'll just skip over it and it will skip to the next item in the array so let's add another print line there are greater than five numbers greater than five great let's check there are six numbers greater than five so let's check we've got one two three four five six perfect so that one looks like it's working try again there are three numbers greater than five one two three awesome so that one's working well um and then the last one here we want the maximum value in our array so the biggest value so again we're using a loop to loop through each item and compare them to find which one is the biggest one so we'll need a variable to store the biggest number and I'm going to say that to start with the biggest number is the first item and then I'll compare that first item to the second item if the second item is bigger I'll update this variable here to have the value of the first item of the second item so that means that my loop will start at one it won't start at zero because um we don't need to compare the first item with itself and there's two ways of doing this I can say with a conditional if the current max value if that max value or rather if the current item is greater than the max value then we should update max value to be the current item so that way as you go along our array it's comparing each item to the current biggest item that we've found so far as we've searched through our array so we can do this or we know that there is oops I just got something go back um there's a max method that we can use so the max method is an informed producer it takes in two or even three inputs they can be floats or ints um and then it will return the biggest of those two so what this code is doing is pretty much the same what the max val sorry what the max method is already doing so I can say max val is equal to max of itself and the current array item that we're looking at so whichever one of these is bigger is what max val is being um allocated so the biggest number is what's around this the biggest number is eight is that true yes biggest number is eight let's test it again it's good to test our code biggest number is eight all right let's get a different bigger number biggest number's eight again biggest number's nine okay so that one's a bit different great um and that's all for question six