All right, so moving on to a big new topic, we are now going to talk about our first type of container
in C++ and that will be something called an array.
So I have admitted the title screen here, but we're going to get right into it and define what an array
is.
So an array is a container that can hold multiple values of the same data type.
It has a fixed size.
It is contiguous and memory the same thing as saying it is continuous like a chunk.
It is homogenous.
Accessing elements in an array is done through sub scripting, and we will get into that and I'll show
you what that means.
And at the moment of creation of an array, a chunk of that contiguous memory is put aside for the entire.
So this is us when we define it in our program, that is what happens in the memory.
When we are running the program, there's going to be access to that entire contiguous block of memory
that we have defined.
So how to declare an era?
So you notice here on the left, we have the data types so similar to how we would make a variable rate,
we need to first put the data types so this could be in float devel, char string, etcetera, then
we have the name of our array variable.
So this will be the name of the array.
And immediately after that, we need to put some brackets here, some square brackets, and inside of
there, we need to declare the size decorator, which is how many elements are array will have.
So how many things will we have in our array, for example?
This was an integer.
How many integers are we going to be storing in this array?
So this all needs to be declared a front.
So let's take a look at how this might look in memory.
So an example is an array with seven elements.
We need to define this as a constant size.
So you could just put the number seven here, but we are here defining a variable that is a constant.
So it's something that's not going to change.
It has to be set and the data type is in and we call it the size.
And so this seven is essentially being stored here for the size decorator.
And remember, on the previous page, we said data type the name of the array.
So like the array variable is going to have a name and then the size decorators zone with these square
brackets, right?
So that's what's happening right here.
We have a type is the data type as integer.
We're calling it my array as a variable name.
We have some square brackets and then we're putting sides here, which is essentially just that number
seven.
What does it look like, though?
That's what I wanted to show was here are those addresses that I just talked about.
So here this is a little different.
These are you seen 12 digits here.
So this could be representative of a 64 bit address, but it's only showing the 12 hex places right
there.
But you noticed that the red part here shows the difference in the bytes as we go up.
Remember these little parking spaces?
They're kind of upset, so we're counting up.
So let's take a look at that and what this really means here.
So we have a contiguous block, so contiguous chunk here.
We're having a bunch of integers stored at these question marks in this example.
Here we have the labeled location of our like little parking spot we were talking about in memory.
This first one has a read zero on the end.
We're counting up.
So let's go to the next space.
Right to the right of it, we noticed that this changed from zero to four.
Well, why does it do that?
That is because like, for example, for a 64 bit Windows application, an integer would be four bytes.
So that's why we are counting up from zero to four here.
You know, as it goes from four to eight and then it goes to sea and then 10 and 14 and 18, so it's
going up by four each time.
You also should notice that there are.
One, so one, two, three, four, five, six, seven spots here.
But you notice in this example that we start at Element zero, we're not calling this element one.
So we are going to talk about that in just a moment.
So more things about the raid declaration, like I mentioned, we must know the size before we declare
the array.
The size variable must be a constant because the array size cannot change.
That's why you noticed in this example, it said Konst, and size equals seven instead of just end size
equals seven.
We also could have just put the number seven down here.
With this size variable is.
But the point is that the science cannot change, so that's the important bit.
So once declared, an array will have the same size until the program finishes, hence that fixed size.
So if you do in a raid, a declaration without the initialization, so you're not really having some
sort of actual data to the right of this that you're saying is in there, you're just declaring it.
And you might add stuff into the spots in the array later than you would do it like this.
So just like in the previous example, we say constant and we're seeing signs already this time equals
five.
We must know the type of data.
So we say and my array and we put South Array in here, but we're not explicitly saying what is stored
there.
So we kind of just have these question marks, right?
We have all this space allocated for us in this contiguous zone, but we don't know what stored there
yet.
So that means the array elements will be initialized to garbage in this case.
It's just kind of whatever's in that section of memory because we haven't really defined what that is
in this case.
So here's another example.
We have an array with five elements, this was like the previous example here.
So if we declared it like this in our code, we said here is the size and here is us making the array.
We're just declaring the array, the data type, the variable name and the size and the brackets.
Then it would be something like this in memory.
We're still using an inch, so you notice that we're counting up in fours here in Hex, and once again
it's starting at zero, going up to four.
So we have or sorry, five.
So it goes up to four.
We have five spots one two three four five.
That's because we said the size was fine.
So here is us defining exactly what is going to be stored there.
Unlike the previous example, we kind of cut it off right here and we just put a semicolon.
So if we have a list of numbers that we want to put right next to each other, list of integers.
One, two, three and four.
Then we can do kind of what we did before, but we can also add this little bit on the right.
So in this example, we'll say constant size equals four because we have four elements that we want
to be in the array, right?
One two three four.
So we do what we did before.
But then, rather than finishing it off with a semicolon we say equals and then we use the curly braces.
And in between the curly braces, we write down a list, a comma separated list of the elements that
we want in the array.
And notice that in this case, we have put we've said that the size is four and we have put four things
inside of the curly braces here.
So one, two three four.
So now it'll be looking like this in the memory.
So rather than having the question marks here, we see we have one two three four things.
They're integers.
So we notice that the addresses that each one of these are stored at is kind of counting up sequentially
one two three four by four each time.
So two zero four eight c. as far as the Hex.
And you actually are having the things in this list represented here in one contiguous block of memory.
So another thing that we can do is define the array explicitly with values, but only partially so this
would be called partial array initialization.
So when you initialize an array, you don't necessarily have to initialize all of its elements.
You can just initialize the first few elements so the array elements will be initialized in order starting
from position zero, which is the first position.
The first thing.
Onwards to as many elements as you are providing.
The rest of the elements, though, will be initialized to something depending on the data type.
An integer will be initialized to zero.
Things like string or A will be initialized to what they respectively will be based on the C++ compiler
and the language.
So here we have consent.
Size equals 10.
So that means that we have set aside a contiguous block of memory with 10 parking spots.
All right.
But you notice here that we're not giving it 10 numbers to put in there, we're saying zero one two
three four five.
So that's six numbers, right one two three four five six things comma separated in between the curly
braces, but this is the number 10 right here.
So what happens?
Well, let's take a look at what the memory will look like.
So we notice we have a six or six things right here and this one, we've actually added.
So this actually has a little typo here.
We have an actual number six as well.
So you can just pretend I have a comma and a six there.
So it's actually seven things.
So let's take a look right here.
So same thing with China by force, because this is integers, right?
But we have zero one two three four five six, and that was all we provided when I said, there's a
little table here, so you can just imagine there's a comma and a six before the curly brace closes.
But we said that it was 10, right, so the last three things are just initialized to zero since we
said that the array was going to hold integers.
They're just kind of considering this as the empty version of an integer, right?
It's just the basic thing is is zero if you're talking about integers.
So we have one two three four five six seven eight nine 10 things.
Only seven were provided in that little initialize or list.
So, yeah, only seven were provided, so the last three of that 10 are going to be automatically defined
as zero since we said it was type integer.
So array declaration using implicit resizing, this means that you are not providing a size in here
and you must know all the elements that you want to store in the array beforehand so that you need to
put this curly brace list no matter what.
And in a race initialization list, cannot have more values than the array has elements.
I think cannot exceed that the size no longer has to be declared, but it's known by you as in you provide
the literal list of stuff here.
So that's why it is possible to leave this blank in between square brackets where we would put the size
decorator.
But instead, we are using this explicit initialization list here with these curly braces and providing
the numbers.
So let's move on to how these are actually labeled.
The positions inside of this array and how do we actually access things inside the array once we've
declared?
So let's look at this example right here.
The previous example where we didn't really provide a size, but the array is going to store one two
three four in each one of those parking spots because we've explicitly defined it in this list between
the curly braces right here.
So rolling forward with that example, the positioning of these arrays always starts counting from position
zero.
So if we want to refer to what thing is stored at what parking spot and we're not using the actual hex
addresses, we're just talking to one another and saying, well, what in your array at the.
First position, we're not really going to say first position, we're going to say the zeroth position,
that's what should represent the first thing.
So we start counting from zero.
So you notice here, if we have that example with one, two, three four.
The first thing is it position zero, second thing, position one, third thing, position two and fourth
thing and position three.
So there are four items in here, but we're counting them and labeling them kind of imaginatively as
items zero item one, item two and three.
So this provides you a way to access each element of the array based on the position it occupies.
And how do we do that?
Well, since this is at position zero, we're going to use the name of the array and then we're going
to use square brackets and in between the square brackets, kind of similar to how we put the size of
the array.
Sometimes here, we didn't write just for this example because we had it explicitly initialized, but
previously we put a size in between the square brackets, right?
So we're going to use the variable numbers and we're going to put the position we're interested in inside
of the square brackets and that all of this together well, I'm circling right here will give us access
to this value right here.
So if I was to do this in code, it would give me back one right here, right?
Same thing right here, let's say I say numbs and square bracket, too close square bracket that is
going to refer to three, so it gives me a way to access this number three in here because it's at position
two, and I referred to the position correctly with this index.
So like I just said, use the name of the array, followed by the square brackets and inside the square
brackets, you put the position of the element like this, the name of the array, so variable name
and then the position inside of these square brackets and of course, finished with the semicolon there.
So this is called subscript.
When you use the square brackets to access something with a position that is called subscript.
So an important thing like just to reiterate, always starts at zero with the positioning, right?
Zero one two three.
The positioning of these four numbers in here, the last element in the array is always found at subscript
size minus one.
Well, let's take a look at that.
What is the size of this array right here?
Well, I'm not going to count starting at zero for the size, I'm only counting zero for the position
labeling.
The straight up fact is that there's four things in here, right?
One two three four.
So four things in here.
So we're talking about what is the last element in the array?
Well, it's three, which is size minus one, right?
Because there's four things in the array four minus one is three.
The reason that it will always be size minus one to access.
The last thing is because we start counting the positions at the zero, which is like an offset of one,
right?
We started at one.
The last thing in the array would just be at the size of the array, right?
It would be a fourth.
We have four items.
The last thing is going to be the fourth position.
But in this case, since we started zero, you notice it's all shifted by one.
That's why the last thing is going to be always outsized minus one.
So the balance of the array will always be zero and size minus one, respectively, for the front of
the array and the back of the array.
The very first position and the very last position for all arrays.
Anything less than zero or greater than size minus one will be considered out of bounds, and this will
often lead to an error because C++ will not want you to access stuff outside of balance because if this
is like a chunk of memory, if you go outside of the bounds, that's kind of like some no man's land
who knows what's there.
You don't want to be accessing those parts of the computer's memory if it hasn't been explicitly defined
by the C++ program with the correct syntax.
So name array zero is red.
Name array, sub zero because we call it subscript.
Right.
So if you want to refer to it short, you want to say in syntax it name or a left bracket, zero or
right bracket, you would call that name or a subzero subsub, of course, being short for it subscript
and the zero being the position.
So some more subscript in here, we have an array of floats that as implicitly initialized with this
list.
So if you want to access something and print it out with CE out, we could do something like this,
so this would be the complete syntax.
We have the sea out and the operator, and then we put the name of the array, which is float array.
We put the subscript square brackets and inside of here we have zero.
So this would be float arrays, subzero.
What is at the zeroth position?
It's seven point one that is the first thing.
We have an in line, so this would print seven point one and then go to a new line.
We can also not only access it and print it out, but I told you it just refers to that position, right?
Floater a subzero just refers to this position here.
So rather than printing it out, we can actually change what's at that position.
So I could also do something like this float array subzero.
I'm referring to it in this manner now.
Floater eight seven zero equals one point zero two.
Well, we were referring to this right here, right?
We use 0th item in their fluttery.
Some zero is seven point one.
Once I say equals one point zero two, what does the array look like now?
Well, this is no longer seven point one is now one point zero two and haven't changed the other items.
I've just changed what was at position zero.
Another cool thing is being able to find out the size of the array when you do not know the size of
the array.
So this is only really used when you've declared and fully initialize the array using the implicit array,
sizing something like this where you provide this list or if you want to, of course, just have the
size of very available in a variable, but you may not necessarily know it.
Maybe it's just not known to you at the time which you're writing that code.
So you want a story that computed size and available for later use?
You have to use a special thing called size of.
So remember that the array size needs to be a constant.
So that's why we're putting in constant array size.
Right?
And rather than saying constant array size equals seven or something like that, we can get this size
of an array.
So let's just say we have this array right here, right?
We didn't really say what the size was.
It was just empty brackets, but we put the amount of things in here, right?
And we can see there's four.
And we said, there's four.
So we know that there's four.
But if we want to save the size and a variable, so let's say we wanted to access something at the size
minus one, which would be the last, right?
If we want to make a variable that is the size so we can somehow do float array sub size minus one,
so size minus one.
And here we need to make a variable size first, right?
And we don't know the sizes and to find here, we haven't already made a variable.
So how do we do that?
We have to use this special size of thing, which is actually a function, and we're going to get into
functions later on.
So don't concern yourself too much with that.
Just kind of memorize the syntax of this and what we need to do here.
So it says size of one word has a little parentheses.
And inside here, we essentially need to divide.
We need to take the array name, right?
So this is the actual array, the variable.
We didn't divide the size of the array by the size of one of the elements, so this divides the number
of bytes allocated by the entire array with or by the number of bytes allocated for only one element
of the array.
Right.
So it's kind of like, let's use some other little analogy, let's just say you have like a stack of
cookies, right?
And you want to know, you know, how tall is the stack of cookies?
All right.
But you don't you don't really know like you don't know how tall the stack of cookies is.
And you don't really know.
You know, maybe you know how to find out how big one cookie is, right?
So if you want to if you want to just know, let's say, let's say you have a stack of cookies, but
you just you don't really know how many cookies are there, right?
Like how many cookies are on the stack of cookies.
But someone says, OK, well, I can give you the size of the stack of cookies and height, right?
So I can say, you know, the height of the cookies is blah blah blah and whatever you're going to say,
let's say bites, right?
Let's just say we're measuring it in bytes.
I know that doesn't make sense.
You probably measure it in like inches or centimeters or something like that.
Let's just say the height of the cookies or like, let's say you had the cookies stacked like horizontally
on a table and they were somehow standing up.
Maybe you have them in a container and they're just going horizontally.
Either way, if you want to think of it that way, if it's easier like an array you say, like, well,
the total stack of cookies is like this many bytes, so you'd want to you could find that out with this
right?
How long is the stack of cookies?
Well, size of the stack of cookies, right?
Let's just call it stack of cookies in here.
If you wanted to figure out how many cookies there were, you would basically have to divide the length
of the stack of cookies, right?
So we have like the length and bytes of the stack of cookies by how long one cookie is right or the
size of one cookie, because that would give you the amount of cookies.
So that's why we're doing that, right?
This is like, you know, the size of the stack of cookies divided by the size of one cookie and this
all together is going to give us.
How many cookies are in that stack, right?
So that's why we're doing this, the size of the array divided by the size of.
One item in the array.
All right.
So you can imagine a stack of something in your head, you need to know the size of one of the items
and you need to know the size of all the items stacked together.
And that will give you that will give you access by doing this formula to know how many items are in
there, how many items are in that stack.
So here is how you access one item, right?
If they're all the same type, right?
All the cookies are the same size.
It doesn't really matter which one you access, right?
Doesn't matter which one you refer to.
You could be like Cookie zero, cookie three cookie rule like one, whatever.
They're all the same size.
You just need to know the size of one of them.
So that's why we're doing this here.
Array name Sub-Zero.
We could do a rename serve one, but like, why do that?
Why don't we do the very first item in the array, which is at positions zero, right?
We don't really know how big the array is.
The array could only have one item in it.
Right?
If the array only has one item in it, then we don't want to do something at array name sub one.
Because a Ray sub, someone would have to mean that there's two items in it, right, because we start
counting at zero.
The first position is always zero.
So imagine you just have like a curly brace and then an item in another curly brace.
You can't refer to one one is like outside of the curly braces, if there's only one item.
The only subscript is zero, the only position is zero.
That's why it's the safe way is to just do the name of the array sub zero once you know the size of
that item at the very front.
Then you can use the size of the whole array divided by that size of one item, and that will, of course,
give you how many things are in the array.
We're calling it array size, but it's really like how many things are in the array.
You know, it's size seven.
It has seven items in it, right?
So kind of long winded explanation there, but I felt like it was necessary to really break it down
because this might be a little too confusing, too confusing to just look at all of this at once and
kind of absorb it.
So if we want to print out everything in an array, we've kind of already done this right.
We printed out something opposition position, whatever we can print out.
You know, this will all print in a line, basically because we're just putting a semicolon at the end,
but we say, see out array zero.
What would that print?
Will it print 10, right?
Then we say, see out my array one.
We haven't done a new line, so it would just we just have a space here.
So it would be like 10 space, 20 space, 30 space, 40, right?
Because they're zero one to three and we're printing out my race of zero, my weight race, a one mile
race of to my race of three.
So this is where I'm going to kind of leave off, I don't want to push it too much with the amount of
information, this might be one of those videos where you might need to watch it through a second time
if it's just too much to absorb and one long push.
And that's totally fine.
We have covered a lot, but these are very important and we're going to move forward with containers
to be able to make more complex programs and do more interesting things, right?
That's the point of all this.
So with that, I will see you in the next lecture and we will start using these arrays.