Multidimensional Arrays

Contents

keyboard_tab
Introduction
1
Course Overview
PREVIEW1m 25s
Arrays and Vectors
2
Built-in Arrays
PREVIEW18m 31s
Conclusion

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
1h 25m
Students
18
Ratings
5/5
starstarstarstarstar
Description

This course explores arrays and vectors which are types of data structures in C++. We'll focus on built-in arrays, which C++ inherits from the C programming language, and a couple of sequence containers including the array template class and the vector template class, which are part of the standard template library or STL.

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • College students and anyone studying C++

Prerequisites

To get the most out of this course, you should have a basic understanding of the fundamentals of C++.

 

Transcript

In the previous lectures, we have discussed three different options for storing sequential collections of data. First, we discussed built-in arrays, then we talked about the array class and finally, the vector class. All of those data structures are one dimensional. What if we want to represent something two dimensional, like a chess board or a maze or a table of data or maybe even higher dimensions than that? We aren't really limited on dimensions in C++, as long as we have the memory and processing power to work with them, but the most common form of so called multidimensional arrays are 2D arrays or two dimensional arrays. With these, we don't specify a single index but rather, two values to indicate a position within the two dimensional array. We typically view a 2D array as having rows and columns. The rows go from left to right, that is, they're horizontal. The columns go up and down, that is, they're vertical. Let's see what the code looks like for 2D arrays. Let's create a project called 2DArrayFun. Create a project, empty project, 2DArrayFun. Hit 'Create', and let's see what we can do. So, we're going to create a main file and add the code for a skeleton programme. Let's not do that yet. int main(), return 0; All right. Now for the 2D array, what we're going to do is, we're going to do this. So, we're going to say, int myNums [2][3]; Now, this will indicate a 2D array, that is, a 2x3 array. So, it has two rows and three columns. So, we're going to actually give it some data, though. I'm going to do this, close curly brace on that line. So, what we're going to do is we're going to actually, do this. Now, you could put it all on one line, but this makes it a little more visually appealing and understandable. {4, 5, 6}, like that, and I'm going to indent that. Maybe add some spaces to make it look cleaner. Now, you notice that of this 2x3 here, there are two rows, and there are three columns: 1, 2, 3, because I'm making it look like this, visually. Now, I could have all of this on one line, it wouldn't really matter. But you notice the first element is an array of three items and the second element is an array of three items, so it's really an array of arrays. So, furthermore, let's do this. I'm going to print out a single value myNums[0][2]. So, that will print out the element at row zero, column two. So, this is row zero and this is column 0, 1, 2. So, this should print out a three. So, let's see what happens... If we run this. So we get a three, not surprising, right? So, reiterating some of the things we have discussed; pay close attention to how we've initialized this 2D array and what the implications are. What a 2D array really is, fundamentally, again, is a one dimensional array whose elements are one dimensional arrays themselves. Isn't that brilliant? So, each element of the outer array, if you will, referenced by the rows... So, there will be two rows in this, indices zero and one; each of those is an array itself. So, when I say I want the array at zero, it's actually going to be a full array here. And if I say I want it at [0][2], it gives me the element. This is the element of the outer array at zero and 0, 1, 2, that would be the column. That's why it prints out a three, because it gives me the element two which is the third element in that single row. So you'll notice, although not necessary, I have visually, again, added this white space around this, so you can see the first row which is a one dimensional array and the second row which is also another one dimensional array with {4, 5, 6} inside of it. So, to show how to access individual integer elements in the array, I give an example where we access the element at row zero, column two. So, this goes to the first row, row zero, and then to the third column, column two, because remember, we're indexing from zero. The value that's printed is, again, three, of course. Let's change one of the values, say, the value at row one, column zero, from a 4 to a 14. Now, I'm not going to do it in the initialization, I'm actually going to do it down here. So, I'm going to say myNums[1][0] is now going to be a 14 instead of a four. So, that's the second row, row one, row zero, row one, and then column 0, 1, 2. So, that would be the number four here, I'm actually changing its value to a 14. And if we print out the element [1][0], then I'm going to show you what that does. Oops, says there's build errors, that's what happens when it doesn't work, right? So myNums, I put a b in there like myNumbs, which is not good, okay? So, let's see what we get. We got the three from the original value up here, and we get the 14 from the changed value in the 2D array. So, how might we print out all of the elements of the array? Since we can use a plain old for loop to print out the elements in a one dimensional array, can we use a loop inside of another loop and print out the rows and the columns? The answer is a resounding yes. So, what we're about to use is called a nested loop where we have one loop placed inside of another loop. We'll talk about what's going on a little bit more, in just a minute. So, I'm going to take or go past this, rather. We'll leave it, I guess. (int row), I'm going to actually spell these out, instead of using i and j, but you could use i and j, no problem. So, we're <2 and row++, and then I'm going to have the inner for loop that's going to take care of columns, and this one's going to print out myNums[row][col], c-o-l, and then with spaces in between. And then, after each iteration of all the column cells, the values and the columns, I'm going to then, put outside of the center loop but inside the outer loop and endl. So, let's run it and see what is going on here. So this does, in fact... you'll notice the first two elements that we talked about, and then we have these elements, right here. 1, 2, 3, and then remember we changed the 4 to a 14, so 14, 5 and 6. First, looking at the code, note that we could use variable names for the loops like i and j again, instead of row and col, but I figured it would make it more obvious what's going on if we use the names row and column, instead. So, one question we might ask when analyzing this code is which loop runs more often? And the answer would be the inner loop, because for every iteration of this outer loop, every time this one iterates, this one will have gone through three iterations; 0, 1, 2, then 0, 1, 2. Also note, in the inner loop we put single spaces between elements, and then I put an endl outside the inner loop, near the bottom of the outer loop, so that the rows would be printed on their own separate lines, sort of like I created the visualization when I declared the array. Pretty cool, huh? Well, guess what? I have a challenge for you. Use the same project we've been working, and you don't have to create a new one. You could if you wanted to, but print a couple new lines after the nested loop that's already present. Then, I want you to use a nested loop technique to print the numbers in the 2D array, backwards. In other words, in reverse. So, 6, 5, 14, and then on the next line, it will say 3, 2, 1. See if you can figure out how to do this one. It shouldn't be too hard if you really think about it. So, you sketch it out, take your time, don't be in any rush to get it done. It's important that you get quality more so than quantity, of getting a bunch of lectures done. So, pause the video, come back when you're done or if you need some help. How did that work out for you? Were you able to solve the challenge? That's absolutely awesome if you were able to, and I'm super proud of you and even if you didn't accomplish them, I'm proud of you for trying. So, let's do it together, so you can see how it might be written. And again, this isn't the only way. But first, like the requirements had suggested, I'm going to create two new lines here. I'm missing an l at the end there, so I'm going to put that there, and then I'm going to put another for loop here. Now, I am going to go by the rows, the rows still go slowest, but this time, I start them at row one which is the highest row. Notice, we don't start at two because that would be out of bounds. When we declare the sizes, that is a 2x3, but the highest index, again, for each of these dimensions, is one less than the actual size. So, you start the row at one, and as long as the row is greater than or equal to zero, we actually go down.  So, row--. It might be different than what you expected originally, or maybe you figured it out. The columns, we start at column two, and then that would be the third column, and as long as the column is greater than or equal to zero, we come down with the columns again. And very easily, this doesn't get reversed. This is, essentially, the same thing. We put spaces in between there and, of course, we put a new line character there. Excellent. So, let's see what we've got. We're going to start this, see if it printed it reverse, like we expect. So initially, it was 1, 2, 3; 14, 5, 6, now it's 6, 5, 14, which is indeed the second row backwards, and then the first row backwards 3, 2, 1 instead of 1, 2, 3. Excellent. Excellent. Excellent. So, I hope that this was fun for you. We learned a lot in this lecture and this section, for that matter. We're done with the new material for this lecture and just need to do some projects, so that you can solidify your skills. Up next, I will give you a project dealing with regular built-in arrays, so let's get going on that.

 

About the Author
Students
297
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics