C Programming Tutorial 85 - Working with 2D Arrays and Nested for Loops

Sharing buttons:

hello everyone this is your video on

multi-dimensional arrays I would

definitely encourage you to check out

the the previous videos on single

dimensional arrays as well as the video

introducing multi-dimensional arrays now

we're going to get some hands-on

practice with it but before we get

started I just want to do a quick shout

out to my C programming crash course I

think it's something you guys would all

be interested in so check out the links

in the description if that's something

you might find useful so when we create

a single dimensional array we say what

type it is and give it a name and then

use these square brackets and then you

can put some values in here and that is

how you create a single dimensional

array multi-dimensional array is very

similar you still only use one type so

every single value is going to be of the

same type so this one up here this might

be one person's grades for three

assignments so now we want to store

multiple people's grades for the three

assignments so we need to use two square

bracket groups and then we can assign in

the same way so it's gonna look

something like this I like to format it

so it looks like a table so this is the

first student and then this is going to

be the second student who's a little bit

smarter than the other one all right so

this is actually not going to compile

and this is an interesting thing that

I'm not entirely sure why maybe one of

you guys can help so when I said to make

a single dimensional array I said don't

put the size here but when you're making

a a two-dimensional array it actually

wants you to put the size specifically

you only need the size right here which

is the number of columns so you could

put a three here and that would work

just fine

but I don't know why that's like that I

don't know why you can't just say oh

there's three columns that that means we

should just give a size of three but

apparently it has to have the size and

generally when I do that I prefer to

have a variable so we could have a

column variable set to three and if

you're going to do this you actually

need to have the Const

key word here and order for it to work

like so now I should compile and we can

actually access any of these elements

fairly simply so for example let's say

we wanted to access this 89 well what we

do is we would say grades and the first

square bracket is going to talk about

the row so this is row index 0 this is

row index 1 so we would say 1 and then

we're going to do another set of square

brackets and this is going to be the

column so index 0 index 1 index 2 right


now let's compile this and just make

sure it works and run it 89 so it works

that's how you access a particular

element and this is just a normal

integer meaning you can you can change

it doing the same thing so now I'm gonna

pray before change it and then print it

after 89 30 so that's how that works now

I wanted to talk about how we print all

of the values of a multi-dimensional

array and you're actually going to need

two for loops so a nested for loop the

outer for loop is going to go through

the rows then the inner for loop is

going to go through the columns so it's

gonna look something like this now we

need the number of rows and actually

don't have this hard-coded so just for

irony let's do that so now as long as I

is less than rows and you could put rows

here if you wanted it doesn't need it

there though so I'm not gonna put it


okay now that's going to cycle through

the rows now for each row we need to

print every single column value so we

need another for loop now typically you

need you'll use another variable either

J or K so you could say into J is equal

to 0 and then J is less than columns J

plus plus so now within here we can use

these variables as the indexes for our

array to access each element

individually so basically it's going to

go I 0 and then for every isay row it's

going to go J 0 1 2 and then it's gonna

go to 1 it's gonna go

0 1 2 that made no sense then just just

print it out you'll understand it's hard

to explain this kind of stuff sometimes

so the first one is going to be I and

then the next one is going to be J okay

we have a little issue

oh here's the issue we need to change it

to grades and that's the lowercase G not

my grades so grades was the single array

up here I'm gonna get rid of that and it

prints out all the values it looks a

little funky because this printf ears is

still there we need to get rid of that I

was lookin I was like what is going on

it's not working but it's working it's

just uh just not looking right there we

go 12:23 4564 78 89 now if you want to

pray now as a table after each row you

need to print a new line like that

and that is how you print a

multi-dimensional array hopefully that

was helpful for you guys please be sure

to subscribe if you've been enjoying

this series and let me know what other

kind of content you'd like me to cover

thank you guys I'll see you in the next