 Okay, time for today's mini lecture, and we're going to be discussing arrays. A lot of what I'm doing is going to be somewhat overlapping with what you will see in the video. So I suggest you, you may want to, if you haven't looked at the videos, you may want to look at the one that's about the introduction to arrays before you go and view this mini lecture. And here is what we would like to, let me close that, don't need that open at the moment, what we'd like to do. We're going to read one week's worth of average temperatures, and we'll calculate and display the minimum, maximum, and average of those values. So each day has an average temperature and we want the average of those averages. And then we will calculate how many temperatures are below, at, or above the average. Now for this part here, we don't need an array. Let's write that part first. I've written this method here called get double. We give it a prompt and an input scanner, and it just takes care of making sure that we actually give it a valid number. In fact, let's just test that real quick. Let's do a get double of input, and the prompt will be enter a number. And then let's just print out whatever they gave us. And so if I enter something like that doesn't work, that's not it either. So let's give something like 5,372.119, okay, great. So that part's working. Now since there's a week's worth of data, I'm going to need to read seven, have a loop that runs seven times. So we're going to have day is zero, day less than seven, day plus plus. I'll start counting at zero, because again, that makes things ever so much more convenient. Now I need to make a, get a number. So I'll have here, let's call it a double average temp. And that is going to be, get double, we'll give it input. And our prompt is going to be enter the average temperature for day plus day plus one, plus colon Y. Let's think about how this works. When day is zero, the prompt will say enter average temperature for day one, because although we computer programmers like to start counting at zero, the people who are using our programs don't start counting at zero, they start counting at one. So let's make them happy. Now, in order to get the minimum, the maximum, and the average, we're going to need some extra variables here. So the first thing we're going to need is a double called the minimum temperature. And we'll get set it to something later on. We have a maximum temperature, and we're also going to need the sum to get our average. And we're going to also have to have number of days. Well, let's just say it's, that's an integer, I'm sorry. You know what? Let's do this. Since we know we're going to have seven, let's have final int days per week is seven. That way we don't need another variable. And then we can have here less than days per week. Here we go. Let's first of all test this to see that it works out okay. So I'll say 30.3. Oh, that's interesting. Oh, that's the problem. These question marks are not going to compile up very well, are they? So for the moment, okay, I guess we'd better address this. So we want to set our initial minimum temperature to guarantee that whatever the user enters, it will be less than our starting value. Say, oh, well, let's just set the minimum temperature to 900 because nobody's going to enter a temperature less than 900. Yeah, except some wise guy who's probably going to go and do that just to see if our program really works. So we need a number that's bigger than any other possible number to guarantee that whatever they enter, it's going to be less. And it turns out there is such a number. It's in the double class and it's called positive infinity. Similarly, we're going to have to set the maximum temperature to be something as small as possible so that no matter what the person enters, that's going to become our new maximum. And we'll set that to be double dot negative infinity. Now see if that compiles, okay, excellent. And we can execute it here. So if average for T1 is 30.3, 40.2, 37.8, these are degrees Celsius by the way, but not 55. That would be awfully damn hot. Let's say 32.1, 27.2, 22.1, and 24. Okay, so cool. It's reading all that stuff and it does exactly seven times so I'm happy with that. Now what we're going to do is this. We're going to say if the average temperature for the day is less than the minimum temperature, then that means that the minimum temperature now becomes the average temperature. Similarly, we're going to say, okay, if the average temperature for this day is bigger than the maximum temperature so far, then that becomes the new maximum temperature. Now because I'm going to have an average and this is also called average, let's just call this the daily temperature. Now I don't want to fix all these by hand, so I'm going to make my life a little bit easier. I'm going to go to search and do a replace and I'm going to replace all average temp by daily temperature. And I want to replace all of them in the selection. That saves me a little bit of typing. And then I also have to say the sum becomes the sum plus the daily temperature. Once I'm outside the loop, I can now say minimum temperature per cent sign dot 1F. I guess I'll call it this. And then we'll calculate our average temperature is going to be the sum divided by the days per week. The average temperature is going to be percent dot 1F and that'll be average temp. I'm going to pause this because I need to write something down. Let's compile this and let's run it. Now to test this, what I want to do is I'm going to put in some simple numbers so I can tell if my minimum maximum and average are working. So let's do 10, 17, 11, 16, 12, 15, and 14, 13 rather. Well, it doesn't matter which way it goes, I guess. And the minimum was 10 and the maximum is 17. So that's good. And the average is 13.4 because 13 is pretty much in the middle there. So it looks like I've got my calculations right. Now the problem is if I want to find out how many of these are above average, how many are below and how many are at the average, I'm going to have to enter them all again because I can't calculate that until I know what the average is and I don't know the average until I finish entering all of them. So without some way to remember what I've entered, I've got to ask for those numbers all over again to figure out how many are above, below, or at the average. And that is why I'm going to need some place to store it and that's some place to store it is going to be an array. So here's what I'm going to do. I'm going to have a double array and the square brackets means this is an array. And then the name of this thing, I'm going to call it, let's say, temperatures. And it's going to be a new double array that has this many elements in it. And they're going to be numbers zero through six because our index numbers for an array always begin at zero. Let me just check to make sure that I type that correctly. Excellent idea. Now what we're going to do, we're still going to get the minimum, the maximum and the sum. But what we're also going to do is we're going to say temperatures for the given day is going to become that daily temperature. We're going to store our daily temperature at the index for our day number. Let's say my first number is 30.4. That means that 30.4 will go into index number zero. And if the next number is 32.1, the 32.1 goes into index one. In fact, let's do this so we can see that this is working correctly. This is not a print line of you entered plus temperatures of day, sub day or day. And I'm not going to add one here because this is just for us programmers to look at. This is what I would call debug output. We use this to check that the array has been set up properly. We're never going to show this to the user once we're done, but I want to make sure that everything came up. I came out well. And so let's put in numbers like 30.4, 32.1, 27.9, 26, 24.8, 27 and 22.4. And you'll notice it is now remembered all of these. Well, this is perfect. That's exactly what I need. And then here are my minimum, maximum and average. Now, rather than get rid of this, I can also do that. Essentially, I'm going to do what's commenting it out. Why? Because in case my program gets screwed up and I have to rewrite it, I can put this back in without having to rewrite all that code. Now we're going to have to figure out the last part, which is how many are at the average, how many below and how many above. We're going to have three variables. The number above will be set to zero. The number below will be set to zero. And the number which are at the average is going to also be zero. Then we're going to run a loop again. If the temperatures for the day is less than the average temperature, then the number below gets incremented. Otherwise, check to see if the temperatures for the day is greater than the average temperature. If that's the case, then the number above gets one added. And if it's not less than or greater than, then it must be at the average. And then I can print this out. So number of days below average. Yeah, I know. I see it. Now let's compile that. And let's run it. And let's do the same numbers here. 30.4, 32.1, 27.9, 26, 24.8, 27.0, and 22.4. So four days were below average, three above, and zero were at average. And the only way we can do this part of it without having the user enter everything all over again is to use an array. So that's one use of arrays is when I need to store data for later usage. By the way, there's a way to do this where I don't need to keep all three of these. I can get a little bit by with only two of them, but that's another story. Let's take a look at some other use of arrays. And to do that, we're going to have to go back to something we wrote a few days ago. And that's going to be here in our days per month. Remember how when we had given a month and year, tell how many days are in it. Let's compile that. And let's run it just to make sure that we remember how it works. So if the month is three and the year is 2022. That's 31 days. Okay, cool. You know, we should probably make a loop around this so that we can or zero to quit. And I'm going to use an n and a half times loop because I'm a little bit lazy tonight. So while the month is not equal to zero. Whoa, okay, time out. I forgot that I've got a lot of extra stuff in here. I've got my enter the year. Yeah, here's them. I was putting this in the wrong place. It's in this thing here. Otherwise. If the month is less than zero. Okay. Time out here. This is ugly. By trying to do an n and a half times loop, I have just painted myself into a corner. Let's think about the big picture here. This is what I remember I just say let's do some planning, because some planning is going to save me from a lot of trouble. I could have you sit here and experiment for hours and I still get all screwed up. Okay. So I'm going to have you enter a month. Okay. Now, as long as the month is greater than zero, and the month is less than or equal to 12. Then it's a valid month. That's not what I want either. Problem is a zero is my special number. This is a place. Remember, we had in that discussion, I had three different ways of writing it if there was an n and a half times loop. And there was a Boolean and there was a do while. This one I'm going to need a Boolean. Let's set the finished value to false. So I'll be a Boolean. And then as long as I'm not finished, I'm going to keep going through my loop. Then I can ask for the month. Yeah. Now, if the month is equal to zero, then I'm finished. Otherwise, if the month is greater than or equal to one, and the month is less than or equal to 12. Then I can ask for the year and do all my calculations. If it's not between one and 12, because somebody can give me a negative number. And the negative number is not between one and 12. So that's my else that keeps track of everything else here. Otherwise, they're out of range for month. There's the plan. So now if they give me a month of zero, I'm done with the loop. Otherwise, check to see if the month is valid. If it is, then I can ask for the year. And if it's valid, do all the calculations. If it's not zero and it's not between one and 12, it must be either bigger than 12 or less than zero, in which case, it's no bueno. Using the Boolean just seems to simplify my life enormously here. Okay. Thank you very much. Now that I have a plan, I can implement it. All right, we're going to have a Boolean finished becomes false. We're not finished yet. We haven't even started as long as we're not finished doing that where I keep doing an expanding things. I got to be more careful with the mouse. Now I'm going to grab all of this stuff here. Tab it in and pop that in there. And then here I'm going to say, if the month is equal to zero, then it's true. Otherwise, if it's between one and 12, then I can ask for the year. I think this will work now. Now let's try that. And so if I have a month two in 2023, it's 28 days month two and 2024 has 29 days. Okay, good. Now I don't have to run it a million times. I've got my loop there. But again, I really had to think through this and think what is the best way to implement this. So I don't have to rearrange all my code. And this looks like this looks like a good plan. Okay, now back to a raise. That's what we're going to do in the first place. Month two yuck. Gee, wouldn't it be nice if we could say February or January or March or whatever the month would be. Ah, we can use an array for that. Let's do the following. Let's go here and let's create a final string array. And we're going to call it month names. And we're going to initialize it to January, February. By the way, whenever you use an initializer like this, remember that it has to be followed by a semicolon because this is a statement. It's not a block. And let's just compile this to make sure that I didn't screw it up. Excellent. Now what I can do here is I'll say month percent sign s has so many days. What I'm going to say here is month name of whatever month you gave me. Oh, but wait, I just realized something. You're giving me numbers between one and 12. And my month name starts at zero. There's two ways I could fix it. I could put in an entry zero here so that January becomes month number one. Or I could still start at zero and subtract one from the month number. And I think I'll just put in the dash dash dash in there that don't make things a little bit easier. And now again, instead of putting the month number, I use that to index into my month names array. And one other thing that I need to do here. So that month two in 2023. February has 28 days. Okay, now I can make my. My output you much better instead of month percent s, I can say, comma percent D and give the year. So I'll say February 2024 I guess the comma is not really necessary there. There this is really looking nice. So my month is going to be five in 2023. And I'll say month 10. That's us right now. And deserve to quit. Things are shaping up nicely. But wait, there's more. Unless I had this whole weird business here with the month number and this nested if statement. They gave me all of my days. I could get away with from that. If I had a final int array called days per month, month zero, there is no month zero so I'll put a zero there. January has 31 days February 28 March has 31 days April has 3031 30 for June. July has 31 August has 31 September has 30 October has 31 November has 30 and December has 31. Now you guys know what about leap year how are you going to change that 28 to 29. Because remember this is final yet change it. So here's what we're going to do. We're going to say the number of days is going to be days per month. Or whatever month number you gave me. Then, if the month is two, and it's a leap year. I'll say days becomes days plus one need this anymore. And I don't need all of this stuff here. Now my program has become a lot more compact. I use the array essentially to look things up. I use the array and indexed with the month name to figure out what the name of my month is. I use the month as an index into this array to figure out how many days are in a normal month, a non leap year. And I did one if statement that's all I need now. In order to do February for leap years. So for month five. 2023. Right. Month two in 2024. Let's check that out. That's good. Month two in 2023 has 28 days. Excellent. Let's check out the boundaries here. 31 days and one at 2023. Good. And what if I put in a negative one. And a 13. Excellent. So my errors stole. Give me errors. And I'm going to use that. Is there a quit gets me out of it. So those are two important uses of arrays. The first one when I need to remember the data that somebody enters so I don't have to ask them to enter it again. I'm using temperatures here as a storage for the stuff that the user entered because I'm going to need it all again. And here. I have an array as a table of values so I can look things up very quickly without having to have a whole 12 way if statement for the month name and another complicated set of if statements for the days per month. And I think that's going to do it for today's mini lecture. Let me pause the recording just a moment. I think tomorrow I'm going to talk a bit more a little bit about displaying arrays. And copying them and traversing arrays. So that's probably what's going to happen tomorrow. And I know that there probably won't be many people here for the question and answer period because it's the midterm but that's okay. I'm just doing these and you can always watch these at your leisure. So see y'all whenever.