Hello and welcome.
In this lecture, I want to talk about initializing an array.
We know how to use an array, we know how to create an array.
But specifically, what if you want to assign specific initial values
to the array, how do you do that?
So you'll want to assign initial values for the elements of
your array most of the time.
The reason that you want to assign initial values to an array
or even to variables is because it makes it easier to detect
when things are going wrong.
When you don't have initial data and you try to do something, you
can cause problems in your program.
So initialization is a very good thing and you always want to do it.
And using an array, there's many, many elements in array.
And so there's a specific way that you can initialize those elements.
There's different syntax that you can use.
But initialization is a good thing and you always want to do it.
I wanted to quickly talk how you can do an array because
there's different ways to do it.
So just as you can assign initial values to variables, when they
are declared, you can also assign initial values to an array's
elements, when it is declared.
To initialize an array's values, you simply provide the values in a list.
The list is separated by commas and the entire list is enclosed
inside a pair of curly braces.
It looks something like this.
Here we're creating an array named counters.
It's of type int, it has 5 elements.
And then what we're doing is we have the curly brackets and we're
not using any indexes in here.
You'll notice that we just have numbers separated by commas.
What this is doing is it's setting all 5 elements in the counters array
to 0, each of the elements are 0.
If we have another array and we name this array integers and it's also
of size 5 and then we have in our curly braces after the assignment
operator 0 comma 1 comma 2 comma 3 comma 4, what that is doing?
It's assigning integer the first element integers array to 0, the
second element to 1, the third element to 2, the fourth element
to 3 and the fifth element to 4.
It's nice syntax where you can immediately initialize the array
when you declare it by just providing numbers separated by commas inside
brackets and you want to do this.
It's also not necessary to completely initialize the entire array.
So in those 2 previous examples, we initialized all 5
elements of an array of size 5.
What if you only want to initialize some of the values?
If you provide initial values that are not equal to the number
the size of the array, then the remaining values are set to 0.
So if you do something like this where you have an array
called sample data of type flow, it contains 500 elements.
But in the curly braces, all you see here is our 3 elements.
What this is going to do is it's going to initialize the
first 3 values of the sample data array to 100 300 500.5.
And then the remaining 5 497 elements are going to be 0.
So you don't have to specify everything.
There's also this concept of a designated initializer.
This was introduced I believe in C99.
It's a nice little feature where you can pick and choose your elements
that you want to initialize.
I think in C99, they added it and then in C11, they made it optional.
So depending on what compiler you have, you may or may
not be able to do this.
But I want to mention it because it is somewhat common.
Some compilers will not allow you to do this after C99.
But it's cool because you can actually just pick and choose.
So you can enclose an element number in a pair of brackets and
then specify the array elements that can be initialized and
you can do it in any order.
So here's an example.
Again, we have our sample data array.
It's of type float.
It's going to be 500 in size.
But here inside of our curly braces, we have additional indexes.
We have our brackets in 2, says brackets 2 sub-2 equals 500.5, sub-1
equals 300.0 and sub-0 equals 100.0.
They're separated by commas.
All this is doing is it's specifying the same thing that we had in
the previous example, but it's doing it in a different order.
But you're still going to have elements 0 or the first
element equal to 100, the second element equal to 300 and the
third album equal to 500.5.
But we're specifying the what index to initialize
right in the curly brackets.
Another example here we have an int array called arr, it's of size 6.
And when we declare it, we're initializing the fifth element
which is the last element to 212, all the elements are going to be 0.
So here's an example of traditional initialization where you just
declare the name of your array and you supply many different numbers.
In this example, we're declaring an array and it’s going to store
all the days in every single month.
And so when we go through this array and we print it out which
is very common, you typically use loops to print out elements in
array or at least iterate over them.
What we're doing here is we're just going to print out each
month and how many days it has.
So let's go through this in code blocks to show you the output.
So here's our example.
We have days and months and we're initializing the array.
The first element to be 31, next 28, next 31.
You also notice up on line 10, we're using a define to create a constant.
And by the way in c, you can also use a constant keyword, which we'll
talk about and other things, that's another way to create constants.
But right now we're just talking about arrays and initializing
them, very easy to do, separated by commas inside curly braces.
So what this is going to display is it's going to go through the
array and it's going to look at index plus 1 and then days index.
So it's going to say month 1 has how many days depending
on what the values are.
So we're accessing the index by the array name and then
the subscript value, the index value and we're using an array.
So it's a variable that changes through each iteration.
So we try to run this.
The output that you're going to see is month one has 31 days, month
2 has 28 days kind of represents January February March April,
but it just goes through the array and prints the days out.
So this proves that the initialization works.
And again, you will want to initialize your data.
Now let's talk about another example and talk about repeating
initialization elements.
So here would be another example, but in this example we're going
to use a designated initializer.
We're going to specify the exact elements that we want to initialize.
So the main line that you should notice in here is the one that
says into days and months where we're doing our initialization.
You'll notice inside the curly brackets that the first element gets
31, the second 28, but then there's something inside brackets that
says 4, it's going to get value 31.
Then we have a comma, element 5 is going to get 30,
element 6 is going to get 31.
And then we have 1.
This means we're now going to overwrite the 28, and we're actually
going to put a 29, put it as 29.
So it's kind of redundant here.
We don't really need to do that, but you would really remove
the sub-1 because 31 and 28, sub-1 is already assigned here.
But it looks like we're saying sub-1 can also be value 29.
So this is just using the designated initializers.
What about if we want to repeat the same value.
Now in c, you can always initialize the entire array of
the same value if you specify 0.
You can always initialize it's like the null parameter.
But other than that, c doesn't provide any shortcut mechanisms
for initializing array elements, at least not in C89.
You may see in C11, again, where you can initialize the same value to the
entire array just using the brackets with no subscript just the number.
But right now in C89 and C99, there's no way to specify a repeat count.
There's no way to say all the elements of this ray should be 55, for example.
So if it were desired initially set all 500 values of the sample
data to 1, all 500 would have to be explicitly assigned which
would be a big pain in the butt.
So what you'll end up doing is you'll just put them inside of a loop.
Easiest way to iterate through an array use a loop.
Well here's a quick example of how you can initialize all the
elements to the same value.
In this case though, we're initializing the first
elements to 0 1 4 9 and 16.
So this is really just initializing all the remaining elements to a value.
Essentially the 6th element of the 10th element using index 5 to 9.
So that's what this for loop is doing.
The first for loop is saying that we're going to initialize elements
from 5 to 9 to be I times I.
And then the second for loop here, it should be
indented, it's harder to read.
The second for loop is actually going through the entire array
and just displaying the data to show you that the last 5 values
were actually initialized.
So whenever you want to repeat the same value or initialize to
different values, just use a loop.
Again, initialization is important.
You will want to initialize, so make sure that this syntax is second
nature for you because we will be using arrays quite a bit in c.