 Hey everybody this is Brian and this is our 9th C++ tutorial. Today we're covering arrays. Now I'm going to cheat and show you the C++.com tutorials. They define an array as a series of elements of the same type placed in contagious memory locations that can be individually referenced by adding an index to a unique identifier. That is a mouthful. What they're really saying is an array is like a line. A line has multiple spots. Let's say there's five people standing in line. The first one is zero. Second one is number one. Now why these are zero based is because they're positions in memory. The zero position is the current and then one, two, three, four. So there's five elements. First one is zero. That's what you need to understand. Their example they use the variable name Billy. I think that's a bad representation because you're sitting here going what in the world are they talking about Billy? So we're going to actually just ignore that tutorial for now. Jump in here and actually write some code. Let's go int line. Let's say this is line at the bank and we've got five positions in that line. And we want to know how much money each person has. So we will say line zero. First person in line. Let's say he has three dollars to his name. Not very not very rich. And then line we'll say the second person. Whoops. See how they get kind of tricky with that zero base. The second person. We'll say he has eight dollars to his name. And then the line third position. We will simply say he has four dollars to his name. And for the second time I'm just going to start copying and pasting here. And we'll say six and two. That is how you assign elements to an array. Notice how it's variable name bracket position in bracket and then equals value. Now it kind of has a different uh different syntax than what you're used to if you've never really worked with arrays before. So let's review. You have a type in this case an integer. You have the variable name line bracket and the number of elements you're going to put in there. That's the total number of elements. Then in bracket and then semicolon. Then when you want to assign these you say variable name bracket position. Remember it's zero based. In bracket equals and then the value. Not to access this. You can just say end L. We'll say person. Whoops if I could spell. We'll say person two has and let's say line two. All sorts of errors today. All right let's compile this and see if this runs. I'm sure enough it runs. Say person two has four. Well what I really should have said is person that position two has four. Let's see if that's true. Two four. Yep so what happens if you try to access an element outside of your array. Notice the upper bound is a four. We're trying to access nine. Let's see what happens here. Well runtime check fail. The variable line is being used without being initialized. You get the same thing as if you try to make a variable before initializing it. Because that doesn't exist. It just does not exist in memory and of course your output is going to display some crazy number. That just happened to be the value that was in memory next to position nine would have been in memory. It gets into a little bit of math that we're not going to cover. You just need to understand how arrays work. Now the same thing happens if you just don't initialize one of these. Let's take this out and set it to three and the exact same error. So if you don't initialize you're going to have a problem. Now it doesn't want to stop. We made it angry. Okay so now you see how arrays work. Now let's just flip back over to the tutorial here. And you can see they're covering pretty much everything that I just explained to you. Now there is a way to initialize an array. Its syntax is you know the variable type, the variable name, how many, and then you have equals and then these squiggly brackets and the values. What that does is it creates the array and then it says this is the first position or zero. This is the second or one and you can see how it correlates here. So that's a very quick way of initializing an array. So let's actually do that. Whoops. And let's just get rid of that. And we're going to say we want the value of the third. Let's run this. Now before I run it can you take a guess what it's going to be? I'm sure some of you are going to go come on, run it already. So we'll run it and person two has four. Well that's misleading because it's actually person three. So zero, one, two, three. That's the third value. Now remember this is zero base so if you want the first one you do a zero. I know that seems a little bit confusing if you've never learned to raise before but I'm going through this very quickly making the assumption that this is not your first programming language and if it is excuse me I implore you go learn another language first it'll be much easier on you. I'm doing this mostly for review so we can get to more advanced things. This is Brian. I hope you found this video educational and entertaining and thank you for watching.