- [Peggy] An array is a tool used
in most programming languages that allow us to store
and work with multiple values of the same data type,
but the trick is using a single variable name.
Starting on line eight,
I'm declaring an array called seasons.
It's a string array and I'm giving it some initial values.
It has the values fall, winter, spring, and summer.
If I didn't use an array, how many variables would I need
to identify each season?
Four, right?
One to hold fall, one to hold winter, one to hold spring,
and one to hold summer.
So it's nice
'cause now I have one variable name called seasons
and I can access the individual elements in that array.
Right below that I haven't nether example
of how to declare an array.
This is a numeric array.
This one I have int numbers, is the name
of the array, square bracket, and I have a size of 10,
close square bracket.
When you're declaring an array,
you then declare it in two ways.
You can use the empty brackets like you see in line eight
if you have an initializer list,
like I do saying fall, winter, spring, and summer.
On line nine, I'm showing how to declare an array
that has a size value.
So it has to be one of those two.
You can't declare an array such as int numbers
and don't put a size in there.
An array is a fixed size.
So in this case, the size of 10.
Let's write a for loop that will ask the user
to enter in values and I'll store them in my numbers array.
So when you're working with arrays,
a lot of times you will use a for loop
to either navigate through the values or to ask for values
and put values into the array.
So I'm gonna declare int i = 0
as long as i is less than 10; i++.
And inside of my for loop, I'm gonna ask the user
to enter a number.
When they do, I wanna read that number into my array.
Let me stop here for a second.
A couple of things I wanna point out.
The first thing is that to access
an element in an array, you use what we call an index value.
And arrays always start with the index value zero.
So on line eight, the value fall is at position zero
or index value zero in the seasons array.
Winter is in position one.
Spring is in position two, and summer's in position three.
So this array has four elements
and the index values are zero to three.
So on my for statement on line 10,
I start with zero 'cause that's the very first element
and I wanna go to nine, so that's why I have i less than 10
and not i less than or equal to 10,
because if I try and access the 10th element,
that's gonna give me an out of bounds error.
All right, so now in line 13,
I'm gonna read in the value the entered
into the numbers array.
And the way that I tell it
where to put it, is I use the square brackets.
And in this case, I'm going to use i
to reference each index value of each element
in my numbers array.
So the first number will go in position zero,
second will go in position one, et cetera.
When I'm all done, I'm gonna go ahead
and print out my array.
So I'll go ahead and add a second for loop.
Let's do int i = 0
while i is less than 10; i++
And right after that, I'm just gonna say,
cout<<numbers[i].
Because I only wanna do the one thing,
I don't have to use curly brackets
around the cout statement.
So for that for loop,
it will execute the cout command every time until I get
to the number 10.
All right, let's just go ahead and run this
and see how this works.
So we're gonna enter in some numbers.
So I'll just do 3, 45, 6, 2,
45 again, 36, 7,
9, 11 and 12.
And it printed them out.
Now they're all smashed together
'cause I didn't have any spaces,
but I just want you to see when I entered the numbers in,
it stored it into the array.
All right, let me hit Enter and go back to the program.
I'm gonna close my Output window,
so I can see more of my program at once.
All right, that's how you could print it out.
I'm gonna comment out this for loop.
Now let's say we have an array of numbers
and we wanna find the smallest one.
That is also something that's very common
in programming, is to take an array of numbers
and find maybe the minimum value or the maximum value,
or maybe you'll even create a function
to calculate the average of all the numbers.
Also too, arrays can be doubles, floats, characters,
so you can have an array of any of the data types.
So let's do one more thing.
Let's find the minimum value entered by the user.
What I need to do is that for loop is actually going
to ask the user for the values.
Now I'm gonna create a variable to hold the smallest one
and I'm gonna set it equal to the very first element
in my numbers array.
I don't know if that's gonna remain the smallest one,
but for right now, if I'm just looking at that number,
it has to be the smallest one.
So now I can do my for loop.
And again, this time I'm gonna do int i =,
but I wanna start at one 'cause I already have the value
of the very first element in my variable called min
and I wanna continue all the way through the loop.
And what I wanna do is I wanna go and look at each element
and I'm gonna say, if the element in the numbers array,
so if numbers[i]
is less than the current minimum.
So I'm checking against the value that I have as my min,
and if it is smaller, then now I have a new min.
So I'm gonna say min = numbers[i].
Let me scroll down a little bit.
And that's gonna continue through
and look at each number in the array.
And when I'm all done,
I will print out the new minimum value.
The smallest number entered was,
and we'll print out min.
Okay, I'm gonna go ahead and run this.
And again, it's gonna ask us to enter 10 numbers.
So let's see, I'm gonna just do five, four, three, two, one,
and then I'll do nine, eight, seven, six and five.
And the smallest member entered was the number one,
which is correct.
So we can also access each element
in the seasons array the same way.
We would do seasons [0].
And if we wanted to, we could print that out
and it would have the word fall.
So let's just take a look what would happen if I did try
and go past the end of my array.
So I'm gonna do a cout statement and I'm gonna say season,
I'm actually gonna make an error here.
I'm gonna say seasons and I'm gonna try
and get at the fourth element in that array.
Remember, their positions
of the index values are zero to three
'cause the size is four,
so this is going to give me an error.
So let me compile it.
It compiles fine.
This is actually gonna be what we call a runtime error.
So when I run it, that's when it tries to access that value.
First I have to get past my numbers.
Now you can see it says ArrayExamples.exe
has stopped working, it caused a problem.
So I'm gonna close the program
and you can see that I was trying
to access storage outside
of where the four values are stored in memory.
And this is very dangerous in a C++ program.
The benefit of using arrays is that allows us
to store multiple values with a single variable name
and then we can access those values
using the square brackets and the index value.
(upbeat music)