In this video, we'll look at the syntax for accessing individual array elements.
The syntax is probably very much like what you expected.
We use the name of the array followed by square brackets
with the index of the array element we want inside the square brackets.
This is called array subscripting. For example,
test score sub 1 refers to the element at index 1
in the test scores array. In the code example at the bottom,
you can see that we've declared the test scores array of five integers
and initialize those integers.
Then we have five output statements that each displays one of those array elements.
Remember, that array subscripting or indexing starts at 0.
So the first element of an array is at index 0.
We use the same syntax for storing values in array elements.
In this example,
we're getting input from the user via the console and updating the individual array elements
from index 0 to 4.
We could then display them as we did in the previous slide.
We'll talk about assignment statements in an upcoming section.
But here you can see that I'm assigning 90 to the first element of the test scores array.
Notice that the syntax is the same.
That pretty much covers the basics of arrays,
but you might be wondering how all this works behind the scenes, let's see.
In c++ the name of the array represents the location
or address in memory of the first element in the array
that is the element with index 0.
When we access an array element using the subscript operator
and provide an index in the square brackets, the compiler knows
that you want the element that is at that offset from the beginning of the array.
So all the compiler has to do is a pretty simple calculation.
For example, if you want the element at index two,
all the compiler has to do is go two integers away from the beginning of the array
and there it is or two doubles or two whatever's.
The compiler uses the size of operator
to determine where the element will be.
This is one reason why raw arrays in c++ are so efficient.
But remember, there is no bounds checking.
So be careful, if you have an array of five integers and you ask the compiler for the integer at the
tenth index, it will gladly do the calculation
and give you whatever happens to be at that location.
It's your responsibility to be sure that you're within the bounds of the array.
Now let's head over to CodeLite and use what we learned and write some code with arrays.
Okay. So now I'm in CodeLite.
And I'm in the section 7 workspace and the arrays project,
and here's my main cpp file.
What I've done is I've created some examples where I'm
declaring and initializing some arrays.
I'm accessing array elements, I'm reading from them, storing values in them and so forth.
And that way we can go through this step by step so you can really understand what's going on.
I've also commented a bunch of this out and what I'll do is I'll uncomment as I go
and we'll build on this little by little.
So the first thing we're doing here is I'm creating
an array right here
named vowels.
That's the name of my array vowels. And you'll notice that right in those
square brackets, I'm not providing any information.
So what I'm telling the compiler is hey,
you figure out how big this is based on the initializer.
Okay. So in this case I've got five characters that I'm initializing this to.
So the array will be size five, so what's going to happen is I'll have an array
of five characters
and I'm initializing them to a, e, i, o and u.
Right, the name of this array is vowels. So I can treat it as such,
and it's indexed from 0 to 4.
Remember, there is no 5 here.
So don't let that confuse you.
Fine now, in this case, I'm saying the first vowel is and
I'm using an output statement and I'm grabbing vowel sub zero.
Well, that's this guy right there. That's the little a.
So I expect this to print out the little a.
In this case, I'm using vowel sub four,
which is the little u right here, the lowercase u.
Right so let's run this and see what we get.
So what I'm going to do is I'm pressing ctrl F5 to build and run.
And the output is the first vowel is a, and I'll move this over here so you can see.
The first vowel is a lowercase a, the last vowel is the lowercase u., exactly what we expect.
Now what would happen,
let me clear this real quick, what would happen if
I try to read from the console
and I want to store whatever character I read into
vowels sub 5.
Recall, there is no vowel sub 5, right. It goes 0 1 2 3 4.
What happens here really depends on a lot of things.
Obviously, I'm going to read a character and store it in a location that's not part of the array,
so there could be something important there.
and I just blew it away. My program will likely crash
depending on what operating system you're on and
what you're running. Your crash may look different from mine.
So let me run this and hopefully, it'll crash. It may not, you never really know with this.
Okay. There's first vowel is little a, second vowel is lowercase u.
Now I'm going to read. So I'm going to type in, let's say, capital A.
And when I press enter, that's my crash.
Hopefully, you can see that it says arrays.exe has stopped working,
close program depending on again what operating system you're working on it may look different.
But in this case, this is a good thing. My program crashed. I'm able to fix it.
And obviously, what happened here was,
and I'll comment this out and I'll leave it commented out so it won't keep crashing. Oops let me do that again.
And I'll write something like "out of bounds"
that way when you download in the example you'll know what's going on.
Right, I'll say "don't do this"
but be very careful with that.
Okay. So that gives you an idea of what happens. It could be a pretty serious problem,
and the real problem is that it may not happen every time.
You may clobber some information that's just not even important.
But you may clobber some information that's real important.
Okay, so let's move on. In this example here,
let me uncomment this code, and I'll walk you through it.
In this example here, I'm doing something very similar. I'm creating
an array
called hi_temps,
and you can see that the name of the array is right there: hi_temps.
Again, I'm not providing the size of the array because I'm letting the compiler figure it out
based on my initializer list. There are 1, 2, 3, 4
initializers. So I was going to create an array of four doubles,
the first one being 90.1,
then second one being 89.8 and so forth.
Here I'm saying print out hi_temps sub zero,
right. Remember, this would be 0, 1, 2 and 3.
So in this case, this will print out 90.1.
Now I'm saying hi_temp sub zero is 100.7.
So what I just did was I just changed the 90.1
to 100.7.
This is an assignment statement. We're going to talk a lot about assignment set statements in an
upcoming section, but they're really really straightforward when you see them like this.
All it says is move this guy into that guy.
Okay, so that's what we're doing is we're just overriding
that 98.1 to 100.7. And then when I display it,
it's going to display 100.7
now. Okay so what I expect here is the first time I output
that guy it's going to give me 90.1,
now I'm going to get 100.7.
So let's try this out and see if that's exactly what we get.
We build and run. The first high temperature is 90.1.
The first high temperature is now 100.7.
So here you can see some examples of declaring, initializing
and accessing array elements.
Now here's another example here and I'm going to comment this out as well.
In this case right here,
what I've done is I've asked the compiler again
to give me an array of five integers and call it test scores.
What's happening here is I'm getting an array of five integers,
just like that, it's called test scores.
Again, they're indexed from 0, 1, 2, 3 and 4.
However, I never initialized them. So what you have in here is junk
or garbage memory.
Whatever happens to be in there, whatever bit pattern happens to be in there is still in there.
So what this code right here is doing is it's displaying
each one of those array elements.
It's starting at the the element at index 0 and going all the way through the element
index 4.
This is what we'll do later on with looping.
We can do all this in one statement with looping, and we'll learn how to do that a little bit later.
Now when I do this, I'm expecting junk.
I have no idea what's in those cells so I don't know what's going to print out. So let's try it and see.
And you can see the junk right here 4199136
and so forth. There's a 0 in there.
The point is you cannot rely on those values. You really don't know what's in there. Okay.
So what do we do? Well, we can just put a couple of
curly braces there in the initializer,
that will initialize everything to 0. So if I run this again,
I expect to see all 0s, I do. That's exactly what I want.
Always initialize your arrays.
Never assume that they've got any value in there until you put the value in yourself.
I could also put a hardcoded 0 in there and I'll have the same effect.
But what if I do a 100, this is this piece that confuses beginning programmers sometimes.
You may think that it's going to put 100 in all of those elements.
But that's not the case. Let me run it and you'll see what happens.
You see that it put 100 in the first element and then the rest are 0-ed out.
That's the behavior when you put in less initializers than you have,
so much now if I put a 90 here,
then what's going to happen is it's going to set this element right here.
It's going to set this element right here to 100.
This element to 90 and then 0, 0, 0.
Okay. So that's the behavior when you provide less than the size of the array
as far as initializers are concerned.
So again, I'll run this real quick,
and what you'll see is 100, 90, 0, 0, 0.
Let me clear this screen real quick. In this case,
what I'm doing here is I'm reading integers from the console
and storing them into those array elements from 0 to 4.
So whatever was in here before is going to be removed now.
So I'm going to initialize this to 100. Let's just go straight down by 10,
70 and 60.
So 100, 90, 80, 70, 60 is how I'm initializing these two.
I have a 5 here. I don't really need that 5 there. I could just
get rid of it like that. I did it before so you can see how it initializes 0s,
so I'll just get rid of that 5 right there.
It's still going to create an array of five integers and initialize them to
190, 80, 70 and 60.
Now what I'm going to do is I'm just going to enter different numbers and then
we'll display those new numbers right here with this code,
which is the same code as we used before. Okay. So let's do that real quick.
We'll build and run.
So there's our original array 100, 90, 80, 70, 60.
So suppose I want to go now 10, 20, 30,
40 and 50.
And I print them out. Now you can see that arrays of 0 is 10
and the next one is 20, 30, 40 and 50. So you can see that those values are
read from the console and stored in the array.
The last thing we'll do is
this may give you a little bit of a different effect than you thought.
Suppose that I want to output just the name of the array.
Now you'll notice test scores right here,
I'm not saying test scores sub 0 or test score sub 1.
I'm just saying test scores.
Let's run this and then I'll show you
exactly what's going on when we come back. So I'm going to run this.
And again let's just enter those scores again
and notice what happens here.
It says: "Notice what the value of the array name is". You get some hexadecimal number.
That number is actually the memory address or the storage location
where the array is located.
This is the way that c++ works or the arrays, the name of the array
is the location of the array.
Now that's a strange number to write. So I'll just use a 1000 for it, that way we can
make it a little bit easier to explain.
So let's take a look at how this works. I've created that array,
right, test scores. And test scores is
5 integers just like we saw before.
Again, indexed 0, 1, 2, 3 and 4.
This is called test scores.
Now when I say test scores, what's it going to do?
Well, it's going to take the address of that variable.
That variable is an address. So let's say that the address is 1,000.
And then this address is 1,004, assuming that we have four byte integers.
and this is a 1008, this is a 1012 and so forth.
What prints out here will be 1000.
And the calculation that c++ is doing that we mentioned in the slides.
So for example, when I say test scores sub 1 as I'm saying here,
what it's going to do is it's going to take test scores which is a 1000
and it's going to add one integer to it or in other words go one integer away from it.
An integer is 4 bytes.
There you go. That's the address. That's the calculation it's doing.
This is why when test score sub-5 is referenced.
It's going to do the calculation and come up with that number right there
and it's going to try to put to work with this guy.
Well, this guy's not part of the array, right? The array ends here.
But there's no real boundaries around the array. It's not like protected or anything.
So in this case, something is going to change
and it may be a bad thing. You might crash your program.
Okay. So hopefully, this example helps you understand a little bit about
the syntax of arrays, how to initialize them, how to use them and a little bit more
about how they're laid out in memory and some of the issues that you can have with them.