 Hey everybody, this is Brian and today we're discussing multi-dimensional arrays. Now for the record, this is about the fifth time I've recorded this because I keep screwing up. The reason why I keep screwing up is because, yes, even with a lot of programming experiences, this is a very difficult topic to really master. What is a multi-dimensional array? A multi-dimensional array is an array of arrays. Why would you use it? To be honest, there's very few real-world applications that use these. Trying to think of a very simple example. You can make a chessboard, a tic-tac-toe game, things of that nature. Alright, let's just dive right in. You've seen the array structure before. We're making a simple array, five items in it. Now what we're going to say is you have five positions in line. Each position in line has two pieces of identification. You've got five people. Each person in each position should have two pieces of ID on them. What does that look like when we try to program it? Line, zero, position zero equals, and we'll just count backwards here, 99. If you have a grid, position zero, zero. In other words, the first position in the first array and the first position in the second array is 99. Let's just do line zero, item one. So the second item in the first position, 98. And let's just do a little copy and paste magic here, 97 and 96. Now let's explain this a little bit here. We're only doing the first two positions in line just so we can fit everything on the screen. The first position, these two pieces of identification are 99 and 98. See, position zero, item zero, item one. The second person, position one, item zero is 97. Position one, item one, 96. Now one thing you cannot do is try to access that directly. For example, if we try to say the first position is 100. Well, you can't do that. Let's actually compile this so you can see what happens. Would you like to continue? No. What it's saying is cannot convert from int to int brackets. The brackets denote an array. So if you're sitting there going, what does that mean? It means an array. In plain English, this is saying cannot convert from integer to an array. The reason why is because each of these items is an array. So it's expecting an array item. Makes sense? Well, if it doesn't just rewind, listen a couple of times to see if you can figure it out. All right, now how to output this. See out. And let's just work a little copy and paste magic here. And let's run that. First off, before I run this, what do you think it's going to print out? The one position, item zero. If you guessed 97, you are absolutely correct. Now one thing you should know is that you can do something like this. Now, looking at that, you're going to go, now Brian, that's going to generate an error message. Because we've only got two positions. Well, no, C++ allows us to make multi-dimensional arrays of varying sizes. So you can have the first one be, you know, two positions, the second to be 300, the third one to be four, whatever you want to do. And what this does is it automatically creates that variable, or sorry, creates that value and puts it in that position to prove that. Let's just run this. Sure enough, there's a 93. Now what happens if we try to grab the third position, which we haven't created yet because it's right here? Well, what C++ does is it generates that position because it had to expand the array out to accommodate four positions, but it didn't assign a variable. So when we run this, it's going to generate an error message. A variable line being used without it being initialized. And of course, we get some random garbage that which is at that memory location. To fix that, you would simply add that in here. Sure enough, there's our 55. Now you have to be careful with that because if you start iterating through this array, you're going to find certain bugs in your program and random crashes. So be very careful with that. Now, how would you go about initializing an array like this? Let's just do this. Let's say 0, 1. Whoops. I hope if I could spell today. 0, 1, 0, 1. So what we're doing here is we are saying, shove all these values into the line, into the item. So this is the first item. This is the second item. I know that looks confusing, but let's just run this and find out. Persistent 0. Okay, so let's actually change some values around here so we can see exactly what's going on. What we're saying is the second position in line, the 1, we want that to be 99. Let's run this and see what happens here. Sure enough, 99. Now it looks confusing, and albeit it is confusing. Another way of looking at it would be something along this line. This is for illustrative purposes only. So what you're saying is you have an array of items. This is the first, this is the second. That's how the compiler sees it. So this is Brian. Now we covered multi-dimensional arrays. Something that, I'll be brutally honest, I don't see a whole lot of real-world applications using. There are better ways of doing it. I'm just covering this because it's a rite of passage. You need to know this, and you will run into this all in the real world, and you need to understand how it works and why. So I hope you found this video educational and entertaining. Thank you for watching.