fill

2d Arrays & Nested Loops | C++ | Tutorial 24



Sharing buttons:

hey welcome to draft Academy my name is

Mike in this tutorial I'm gonna talk to

you guys about two separate subjects in

C++

the first subject we're going to talk

about are going to be two-dimensional

arrays and a two-dimensional array is a

situation where we have an array where

every single element inside the array is

another array so this is a pretty cool

situation and I want to talk to you guys

about one other thing which are nested

for-loops

and a nested for loop is a situation

where we have a for loop inside of

another for loop so this is gonna be

kind of cool and you'll see we can

actually use these two subjects together

two-dimensional arrays and nested for

loops in order to build a kind of a cool

program so I'm gonna show you guys the

basics of doing that stuff first thing I

want to show you is a two-dimensional

array so I'm gonna go ahead and create

an array of numbers and I'm just gonna

call it number grid and normally when we

create a regular array we're just gonna

make an open and close square bracket

but since we're making a two-dimensional

array I want to make another open a

closed square bracket after this now

remember a two-dimensional array is a

situation where we have an array where

every element inside of the array is

another array and I'm gonna show you

guys how we can go ahead and set

something like this up so I'm gonna give

this an initial value so I'm just gonna

initialize this and I'm basically just

going to put a bunch of array elements

inside of here so the first thing we're

gonna do is create the first element in

the array now normally if I was creating

an array I could just kind of type out

elements like this right you know I can

kind of type them out delineate them

with a comma and then we have our array

but with the two dimensional array

remember each individual element is

actually another array so if I want to

create one element inside this two

dimensional array I can basically go

like this right so I can make another

sort of mini array inside of here and

I'm actually just gonna format this a

little bit and so now the first element

in this number grid array is going to be

an array with elements 1 & 2 so number

grid first element is this array and

this array has two elements of its own

so I'm gonna copy this and we're gonna

make another element so I'm gonna put a

comma here because remember we need a

comma to delineate the different

elements and then down here I'm just

gonna paste so now we're gonna do three

and four so we have one two three four

and why don't we do one more and we'll

just do five six all right so

essentially what's happening here is we

have our overall array right so these

two curly brackets represent the number

grid already and inside of the number

drin array we have three elements so

this is the first element this is the

second element and this is the third

element all of which are arrays and each

one of these arrays has one two elements

inside of them so whenever we're

creating two-dimensional arrays we

always want to specify the numbers over

here so inside of this first open and

closed square bracket we want to tell

C++ how many total elements are in the

number grid array so in our case we have

three because again there's one two

three elements then over here we want to

specify how many elements are inside of

each array element so you'll notice we

have 1 2 1 2 1 2 etc so there's gonna be

two elements in each one of these arrays

and it's very important especially when

you're dealing with two-dimensional

arrays that you specify these numbers

because otherwise you can get really

confused you know and so by specifying 3

2 I know the basic layout like I know

that this array has 3 rows like you can

call these rows and then it has 2

columns so these would be like columns

essentially we're building a matrix all

right so that is a 2 dimensional array

and let's talk about how we can actually

access some of these elements so if I

want to access some of these elements

I'm actually just gonna go ahead and

print some of these out so over here I

can just say number grid and what I can

do is I can make 2 open and closed

square brackets so I can go just like

just like this and inside of these open

and close square brackets these first

ones I want to specify the row in other

words I want to specify the element

inside of number grid that I want to

access so this would be of the

at position zero this would be the

element at position one and this would

be the element at position two so let's

say we wanted to access it this element

right here I can put a zero here because

it's inside of element zero and then

over here I want to put the index of

that element inside of the actual array

so it's going to be an index position 0

1 so 0 again is corresponding to this

overall element and 1 is corresponding

to this element inside of that array all

right so now when we print this out we

should get a 2 and you'll see over here

we get that too let's try one more why

don't we try to get this 5 over here so

5 is gonna be an element 0 1 2 so we're

gonna put a 2 over here and it's gonna

be element 0 so we're just gonna put a 0

and now we should get that 5 which we do

all right so that kind of shows you a

little bit about how we can create a

two-dimensional array and actually this

same concept can apply to like n

dimensional arrays so you could have a 3

or 4 or 5 dimensional array and all

these same concepts are gonna apply

obviously the more dimensions you add

more complex it becomes but you can get

pretty complex with arrays just by using

this technique alright so that was the

first thing I wanted to show you guys

and now I want to introduce another

topic which is a nested for loop and

nested for loop is a situation where we

have a for loop inside of another for

loop and actually nested for loops can

be extremely useful because we can use

them to print out all of the elements

inside of this two-dimensional array so

basically we could iterate through all

the elements inside of here using in

nested for loop but I'm gonna show you

guys how that's gonna work so down here

I want to create a basic for loop so I'm

just gonna say for and I'm just gonna

say int I is equal to 0 again our goal

here is to be able to loop through all

of the elements in this array and you

know as I sort of create this for loop

just kind of stick with me eventually

this is all gonna make sense

but what I want to do is I want to keep

looping while I is less than 3 and I'm

using 3 here because I want this four

loop to loop through all of these

individual elements so all of the arrays

inside of the number grid array then

over here I'm just gonna say I plus plus

so this for loop is responsible for

iterating over all of these elements so

this first element the second element in

this third element one remember each of

these elements is another array so what

I want to do is for each of these arrays

I want to iterate over each of the

elements inside of those arrays so I can

create another for loop to do that so I

can say int and I'm just gonna call this

J and a lot of times when we're using

nested for-loops people will name these

IJ that's kind of a convention I'm gonna

set this equal to zero and I'm gonna

keep looping as long as J is less than

two and remember two is how many

elements are inside each of these arrays

right so this loop over here is

responsible for iterating through all of

these elements and this one down here is

responsible for iterating through all of

these elements so I'm gonna say J less

than two and then I'm just gonna say J

plus plus so now we have our basic

structure set up for every iteration of

this I loop this J loop is going to

fully iterate so it's gonna go through

all the way and down here I'm just gonna

see out and I'm basically just gonna

print out all these different elements

so I'm gonna print out number grid I J

and you guys have seen a second

basically how this is gonna work and

then real quick down here I'm just gonna

see out a new line so I'm just gonna say

see out ndele and that'll print kind of

a new line so let's go ahead and run

this and then I'll give you guys more of

an explanation as far as what it's doing

so when I run this you'll notice that

we're actually printing out all of these

elements so we're printing out one two

three four five six so I'm actually able

to go through and iterate over every

single element inside of this

two-dimensional array basically what's

happening is every time we go through

this top loop up here this I loop we're

fully iterating through the J loop so

this I loop is responsible for iterating

over each individual element so it's

responsible for iterating over this

this and then this this J loop is

responsible for iterating over each of

the elements inside of those elements

which are arrays so the J loop is gonna

loop over this and this so the first

time we go through this loop I is going

to be equal to 0 and J is gonna be equal

to 0 so we're gonna print this out then

we're gonna go through this J loop again

I is still going to be equal to 0 but J

is gonna be equal to 1 so we're gonna

print this element out and then I is

going to be equal to 1 so it's gonna be

1 0 will print this out and then it's

gonna be 1 1 so we'll print this out and

that's essentially what's happening here

so nested for loops and even like nested

while loops I mean any sort of nested

loop can be very useful and it can be

useful for looping through like I said

complex structures like that hey thanks

for watching if you enjoyed the video

please leave a like and subscribe to

drop acad to be the first to know when

we release new content also we're always

looking to improve so if you have any

constructive criticism or questions or

anything leave a comment below finally

if you're enjoying traffic Academy and

you want to help us grow head over to

draft Kadim e-comm forward slash

contribute and invest in our future