Hello and welcome.
In this lecture, I want to talk about the concept of an array.
An array is a concept that allows you to store many different values
in a single variable essentially.
Arrays are common in most programming languages.
They help simplify and make your program a lot more efficient.
So we're going to talk about creating and using arrays in c.
So an array.
It's very common in a program to store many data values of a specified type.
In a sports program, for example, you might want to store the
scores of all the games or the scores for each player.
Up until this point, we could easily implement that, but we'd have to write
a different variable for each score.
So imagine if there's a ton of games.
I mean in baseball there's 162 games.
If we were to write a program that was a baseball program that wanted
to store some stats or something, we would have to create 162 variables
for each game to keep the score.
Now that is a pain in the butt.
It would be very tedious.
and it's really just too much.
So this is where array comes into play, an array
will solve this problem.
You don't have to create 162 variables.
You can create a single variable and access it a certain way.
So arrays allow you to group values together under a single name.
You don't need to separate variables for each item of data.
So it's going to allow you to become a much more efficient programmer.
An array is a fixed number of data items they're all the same type.
So only one issue with an array is that you can't
change the size of the array.
Once you create the array, it's always going to be that size.
And the other restriction is that you can you can't
store multiple types of data.
If you declare an array and it's still going to store numbers
and ints, it has to store ints.
So a couple limitations.
How do you declare an array.
Remember that when you declare a variable what are you doing?
You're saying that this variable exists in the program.
And when you're declaring these primitive data types,
you're allocating memory form.
When you declare an array, the data items in array are
referred to as elements.
So this is another concept when you think of an array.
If I’m saying the term element that means I’m referring
to an item in the array.
Because remember an array is going to have many, many different
values but with the same name.
And as we mentioned the elements in the array have to be the same type,
you can't mix and match data types.
You can't have an array of 5 ints and 5 longs.
There's no such thing as a single array of multiple data types.
Declaring a use an array in a program is similar to a
normal variable declaration.
All you have to do is specify the name and then use brackets.
So that's the only difference, this idea of square
brackets following the name.
Here's an example this looks like a normal variable
declaration, but what you see here is you have these brackets.
That's the one thing that's not normal, and there's
a number inside them.
The number inside the brackets are specifying the size.
So here we're just saying we're going to create an array named
numbers of type long that's going to contain 10 elements.
So it's pretty straightforward.
This is a number a declaration because the semicolon is at the end,
it's just like a variable declaration but you have those brackets and
a number, and this is the size of the array or the array dimension.
Once you've declared an array, you're then going to want to start using it.
You're going to want to start accessing the data inside
of it or writing data to it.
In order to do that, you have to understand how the array is
stored in memory and how it's actually going to be accessed.
So an array with each of the elements in the array, each of the data items,
they're accessed using the same name.
So you're still going to access it with the variable name.
However, if you want a specific value in that ray, you're now going
to have to use something called an index or a subscript, both of these
terms are used interchangeably.
This index or subscript value is going to be a number and it's going
to be inside the brackets as well to access an element in the array or
right to that spot in the array write a value, following the array name.
So index values are sequential integers that start from 0.
So when we talk about an array and it's elements and we talk about the
indexes in an array or the subscripts, all those subs can start from 0.
This is basically saying that their rays are 0 based.
So index values for elements in an array of size 10 would be from 0 to 9.
0 is going to represent that first array element, and 9
is going to refer to the last element in an array of size 10.
So you have to understand this concept of 0-based arrays.
Intuitively, you might think oh I want to access the first spot in
the array, okay I provide index one, no because they start at 0.
So it's very, very common to mistakenly try to access an
array and be off by 1 because you think that they start at
one and they don't start at 0.
This is referred to as an off by 1 error.
You can use a simple integer to explicitly reference the
element that you want to access.
For example, if you want to access the 4th value in an array, you
would say the array name and then inside the brackets you would say 3.
So array name sub-3 is going to access the 4th value because
the array name is 0-based.
So you just have to provide a number for the element
that you want to access.
And again, remember that it's 0 base.
So to access the 4th element, it's 3.
If you want to access the 8th element, you do index 7.
You can also specify an index for an array element by using expression.
So you now only have to use a number.
You can use an expression in square brackets following the name as well.
However the expression has to result in an integer value, that corresponds
to one of the possible index values.
So if you have an array of size 10 and you use an expression,
that expression has to evaluate to either 0, any numbers from 0 to 9.
It's very common also when you're storing elements in an array to use
a loop to access them and we're going to throw an example of that as well.
So for example here, we have a for loop and we're initializing our loop
control variable to 0 I and we're exiting once I is less than 10.
And what we're doing here is we're just displaying
the numbers in the array.
And so we're saying printf number is percent d and then
numbers sub-I, I is a variable.
You can use a variable in a variable expression as long as that
variable represents a number from 0 to 9, and in this case it does.
It's very common to use for loops to iterate over an array
to either access the elements or write data to those elements.
So to summarize, here's a nice little illustration that I found.
This is how an array is structured.
If I have an array called numbers, you're going to have
the name of the array followed by brackets which identify
individual elements in the array.
So I can declare an array by just specifying the type and the name and
then specify the size inside brackets.
And then when I want to access it, I just use the array name
followed by the index value.
And so for an array that has a size 10, you're going to have
indexes that are valid from 0 to 9.
So what happens if you try to access an element in the array with an
invalid index, for example trying to access an array of size 10 using
11 as an index or 10 is an index.
You're going to have an array out of out of bounds error.
So if you use an expression or a variable or a constant for an
index value that is outside the range of the array and outside
of the range array means what?
Well, it means it's either a negative number or it's a number that's greater
than the size, really greater than the size minus one because there's 0 base.
So if you have an array of size 10, size minus 1, if it's greater
than 9, you're going to have an array out of bounds error.
And the problem with an array of bounds error, it can cause
your program to crash, or even worse, it can cause your program
to look like it's acting normal, but then it doesn't act normal.
So it doesn't produce consistent results.
Sometimes you'll have garbage data, data that doesn't
mean anything in the array.
Again, it's very unpredictable when you have an array of bounds error.
And the issue is that the arrayed out of bounds error cannot be
checked by the compiler because the compiler is going to not going to
know if you're assigning something greater or if you're accessing
something greater than the size.
It's still going to compile even though you have this
area out of bounds error.
Essentially the array of bounds error is a runtime error.
You're not going to find out this error until you run the program.
And again it can cause your program to be very unpredictable.
So you want to be very, very cognizant of how you're accessing
the array, what index values you're using and how you're writing data
to the array with the index value.
Pay very close attention to the array indexes and also the bounds.
If you want to write data to an array, it's very, very simple.
You're going to do basically just use the index.
So a value can be stored in array element simply by specifying the
array element on the left-hand side of the equal sign and then
using the brackets to specify the element that you want to write to.
So for in this example, we have an array named grades, we don't know
what size it is, it's got to be bigger than 100 or else we'd have
an array out of bounds error here.
But in this example, we're saying grade sub 100 is equal to 95.
The value 95 is stored in the element number 100 of the
grades array, very simple.
You just specify the index and then whatever value
you want to sign you do.
You can also use variables to assign values to an array.
So 95 could be stored in a variable, just like you assign other variables.
Now let's go through an example of using an array.
Here's an example of using an array that is going to sum read
in 10 numbers and then give me the average of those 10 numbers.
As we're reading them in we're going to calculate the sum then
we're going to display the average.
So at this point, I want to take this example and I want to put
it in code blocks and try to run through and show you how it works.
We're in our normal view, we're in a simple project and we're going to show
you how an array works in code blocks.
You'll notice that we're in our main function.
This is the entry point to the program.
And on line 12, we're declaring an array.
We're going to declare an array of type int of size 10.
The size is inside the brackets, we end it with a semicolon.
This is an array that's going to store 10 values.
If we didn't have this array stored what we'd have to do
is we'd have to say grades 1 int, grades 2 int, grades 3.
You see how problematic that is and how very tedious it is.
It's very nice that we can simply create a single variable
that stores 10 items, and they're grouped together.
When you store items in an array, you don't want the values of the data to
be different, all different things.
Like in this example, we're storing grades, so they're all grades.
You don't want to name an array something and then say I’m going
to store my grades, I’m going to store my...
my... my... whatever, my age in there and anything.
Make sure you store the same type of data when you're an array.
The next variable in line 13 is a count variable.
It's equal to 10.
It's going to be the number of values to be read because
we're going through the array.
Sum is a long, we're going to sum the numbers as we go through the
values and then average is going to be a float because we want to
get the average of the numbers.
First thing we're going to do on line 17 is we're going to tell
the user that we want to them to enter 10 grades and then we're
going to read in the values.
So on lines 20 you notice how I’m indenting and I’m using good naming,
indent and use good blocking of code for blocks of code indent.
Here on line 20, I’m declaring my loop control variable.
I’m running an older compiler, c compiler 89, so I can't declare
it and initialize the same line.
Normally, you would just do this.
But when I do it because I’m running C89 compiler getting an error.
So I declare my variable on line 20, and then I use a for loop.
We're familiar with for loops from our previous lectures
allows you to repeat code.
Here we're just saying we're going to have a counter-controlled for loop,
we're going to start at 0 and we're going to go all the way until 10.
So we're going to go through 10 iterations, but
you notice the index is 0.
That's important because we're going to be accessing an
array, using that variable I.
And then you'll notice in the end of the for loop, we modify the loop
control variable by incrementing it.
So we're going to print out the value of I plus 1 as we go
through the array to show you where you're at for the numbers.
So it's going to basically say like enter grade 1 grade 2 grade 3 grade 4.
It's then going to ask you to enter in the grade using scanf.
We're going to store it in our array.
You'll notice that when you store it in the array, it's just
like a normal variable access.
You just use grades, and then you specify the index.
Here we're using I as our index.
So that's going to be equal to 0 the first iteration, 1 the second
iteration, 2 the third iteration.
And it's good we started with 0 because arrays are 0 based and
then we just use the address of operator, the ampersand.
As we're going through, we're going to sum the grades together.
So we're going to say sum equals sum plus grade sub I.
Each grade is going to basically calculate a total.
And then once we get out of the loop after running 10 times, we're going
to set our average equal to the sum divided by the count and cast
it to a float, we're casting here.
This is changing the data type.
Remember, sum divided by count, sum is it long and count as an int and
we want to cast that to a float.
And then at the very end, we're going to print out the average.
So let's go ahead and run this and make sure we get no errors, which
we have 0 error and 0 warnings.
So now let's run it.
And so it's going to do exactly what I mentioned.
It's going to say enter the 10 grades and then it puts a 1 in a little mark
there so we know what number we're at.
It's going to put in like 95 32 55, it’s going to go in and
just put any numbers in here.
They don't make sense necessarily, that's okay, and my last number 23.
The average of those 10 grades is going to be 51.20.
But it's really nice because we don't have to create 10 variables.
We access the array using the subscript, and we can assign values
to the array using the subscript.
This is actually assigning when we do the scanf.
We're assigning data to that to that subscript.
Now if we were to change this and say we started at 1 and
instead of going to count which is 10, we actually set it equal.
So actually let's not change that to 1.
Let's say we're going to start at 0, but now we're going to
go all the way to index 10.
Remember, index 10 is out of bounds because the size of the array
is 10, but the index value is 0 based so that the max index is 9.
And again, we're going to be able to compile this because compilers
do not detect out of bounds errors, the problem is when we run it.
Now when we run the program, we're going to have a big problem
we try to enter in 11th grade.
So we're going to be all good.
When we enter 10 grades in, the problem is going to happen
when we when we get to 11.
Now we have 11, and we're going to try to store data.
It nothing bad happened, but our average is not going to be correct.
And it's very unpredictable behavior when you have an out of bounds error.
So you want to make sure that you have the eye lesson count and
you want to make sure that when you access the information, you
don't go out of bounds as well.
And this is the basics of the array, declaring the array, specifying
the size just data type and then the size and brackets, accessing
the array or storing data in the array, assigning values, using the
subscript I inside the indexes.
And we could even print out the indexes using subscript as well.
Arrays are going to be used quite a bit.
We're going to have many examples with arrays because it simplifies things.
Just remember arrays are fixed size.
You can't change the size of the grades.
It's always going to be 10 as you run the program.
So if you assign any data above that, it's not going to expand.
And also remember that grades have to be the same data type.
I can't store strings or words in grade, I can only store ints.
I can't store floats in there, only store ints.
Thank you.