And to complete this programming section, let's have a look at the race you have seen that you can
store data into valuables.
Now, what if you need to store 10 values, for example, 10 symbols you got from a census?
And what if you need to store 8000 votes?
You can guess that declaring thousand valuables in your code is not practical at all.
So this is where you will use arrays are what we can also call lists.
What is an array?
An array is simply a collection of values that are related and share the same data type.
For example, you could have an array to store all students name from a classroom, another array to
store the values you read from a photoresistor, etc..
So let's create an array here in the global scope.
For example, we want to store list of temperatures as integers to make it simple.
But first you need the data type of the values you are going to store in the array and then you are
going to create a name for the array, just like you would create a name for variable temperature.
So one common practice, if you want to start the list of temperature, then you can simply go to a
temperature list or temperature.
All right.
OK, this is basically the same thing and this will make sense for you in the future.
Then you open close brackets and inside brackets, you will need to define the size of the OK.
Once you declare an array in C and C++ for Arduino, you first need to put a size and this size can't
be modified after what?
Let's say I want five elements in my array and here I have declared a three with five integer elements,
but I have not initialized the value yet.
Let's compile.
The Rai's.
OK, and now what I can do is I can initialize so I can initialize later on in the code or directly
here.
So one option I have is simply to put zero into brackets.
This will initialize the array so all of the elements of the array to zero and then I can modify them
if I want.
I can also directly set a value for each element.
So we have five elements.
Let's say twenty three, twenty four, fifty six, 12 and 14.
And this will put all of those values in all of the indexes of the array and know how to walk with this
array.
So here you can initialize it to work with the array.
Well I'm going to do Sanyal.
But being.
And let's say we want to print the print, Ellen, we want to print the value 24 fonderie to get access
to an element of the array.
You first need to write the name of the right temperature, the right, and then you put some brackets
and you write the index of the element you want.
And in programming, we start to count at zero.
If you'll remember, we don't start to count at one.
So here you have five numbers, but in the index for this number is zero one, two, three and four.
So if I want to have the value here, I need the index one and let's load and check in the same monitor.
Yes, I have the volume 24, which is the second element of the array, which is the index number one.
So now pay attention to that when you use the index to make sure you don't go after the last you here,
the last index will be basically the size minus one.
So don't try to go over the floor then.
If you want to modify a value inside the array, whether you simply to the temperature array, you put
the index to, let's say I want to modify the first one.
So actually, index number one would be the second element, and let's put it this will put this value
inside the element of the rate which corresponds to index one.
If I print again after that, you would see that the value has changed.
It was so twenty four and then it is thirty.
OK, so you can see how to get access to one element, how to modify one element.
And then if you want to go through all of the elements of the array, well you can use the for loop
that we have just discovered before.
So for int I is equal to zero.
So we are going to start with an index at zero and then I is lower than the size of the area.
So five so here is strictly lower.
OK.
This is very important because you don't want to get to the value five because five will be outside
of the array because we start to count at zero and then I guess plus you close the parentheses, you
open a new block of code and let's say serial that.
Brent L.N., so what you can do is you can do temperature.
All right, open the brackets and then the index here will be high so you can use actually the counter
index we use in the fall, you can use it in the block of code because this is a valuable you create.
So this is a local valuable.
You create all the block of code that followed so you can use the eye.
And I would be equal to zero one, two, three and four and then exit.
So don't forget this.
I'm going to comment those two lines here so we can print out of death.
Let's upload the program and let's see what we get.
OK, you can see 23, which is the first one, 30, which is the second value that was modified here,
and then fifty six, 12 and 14.
So we have just gone through all of the elements in the area.
And these we are going to use the exact same structure later on when we use multiple entities in our
program.
So that would be malpractice for you.
OK, to rig up an array is a collection of related valuables sharing the same data to create an array,
you first need to declare it and give it a size which cannot be changed afterwards.
Then you can access and modify any value from using an index.
Finally, using full loop is a pretty efficient way of going through all the values from an array.