WEBVTT

00:00.300 --> 00:06.840
And to complete this programming section, let's have a look at the race you have seen that you can

00:06.840 --> 00:08.640
store data into valuables.

00:09.060 --> 00:14.290
Now, what if you need to store 10 values, for example, 10 symbols you got from a census?

00:14.880 --> 00:17.430
And what if you need to store 8000 votes?

00:17.790 --> 00:23.450
You can guess that declaring thousand valuables in your code is not practical at all.

00:23.870 --> 00:27.900
So this is where you will use arrays are what we can also call lists.

00:28.290 --> 00:29.510
What is an array?

00:29.730 --> 00:36.280
An array is simply a collection of values that are related and share the same data type.

00:36.840 --> 00:43.200
For example, you could have an array to store all students name from a classroom, another array to

00:43.200 --> 00:47.310
store the values you read from a photoresistor, etc..

00:47.670 --> 00:51.420
So let's create an array here in the global scope.

00:51.990 --> 00:57.090
For example, we want to store list of temperatures as integers to make it simple.

00:57.390 --> 01:03.270
But first you need the data type of the values you are going to store in the array and then you are

01:03.270 --> 01:09.840
going to create a name for the array, just like you would create a name for variable temperature.

01:10.350 --> 01:15.960
So one common practice, if you want to start the list of temperature, then you can simply go to a

01:15.990 --> 01:18.820
temperature list or temperature.

01:18.990 --> 01:19.390
All right.

01:19.440 --> 01:24.600
OK, this is basically the same thing and this will make sense for you in the future.

01:24.990 --> 01:32.940
Then you open close brackets and inside brackets, you will need to define the size of the OK.

01:32.940 --> 01:41.010
Once you declare an array in C and C++ for Arduino, you first need to put a size and this size can't

01:41.010 --> 01:42.390
be modified after what?

01:42.720 --> 01:50.400
Let's say I want five elements in my array and here I have declared a three with five integer elements,

01:50.730 --> 01:53.960
but I have not initialized the value yet.

01:54.450 --> 01:55.470
Let's compile.

01:56.600 --> 01:57.410
The Rai's.

02:00.010 --> 02:06.670
OK, and now what I can do is I can initialize so I can initialize later on in the code or directly

02:06.670 --> 02:07.240
here.

02:07.930 --> 02:12.210
So one option I have is simply to put zero into brackets.

02:12.610 --> 02:19.900
This will initialize the array so all of the elements of the array to zero and then I can modify them

02:20.530 --> 02:21.040
if I want.

02:21.040 --> 02:24.160
I can also directly set a value for each element.

02:24.520 --> 02:25.750
So we have five elements.

02:25.750 --> 02:32.310
Let's say twenty three, twenty four, fifty six, 12 and 14.

02:33.670 --> 02:43.030
And this will put all of those values in all of the indexes of the array and know how to walk with this

02:43.030 --> 02:43.330
array.

02:43.330 --> 02:46.610
So here you can initialize it to work with the array.

02:46.660 --> 02:49.160
Well I'm going to do Sanyal.

02:50.690 --> 02:51.600
But being.

02:54.920 --> 03:04.190
And let's say we want to print the print, Ellen, we want to print the value 24 fonderie to get access

03:04.430 --> 03:06.530
to an element of the array.

03:06.560 --> 03:12.980
You first need to write the name of the right temperature, the right, and then you put some brackets

03:12.980 --> 03:16.910
and you write the index of the element you want.

03:17.060 --> 03:21.090
And in programming, we start to count at zero.

03:21.170 --> 03:23.690
If you'll remember, we don't start to count at one.

03:24.080 --> 03:31.820
So here you have five numbers, but in the index for this number is zero one, two, three and four.

03:32.300 --> 03:40.640
So if I want to have the value here, I need the index one and let's load and check in the same monitor.

03:43.110 --> 03:49.230
Yes, I have the volume 24, which is the second element of the array, which is the index number one.

03:49.800 --> 03:56.420
So now pay attention to that when you use the index to make sure you don't go after the last you here,

03:56.430 --> 03:59.690
the last index will be basically the size minus one.

04:00.060 --> 04:03.360
So don't try to go over the floor then.

04:03.360 --> 04:11.310
If you want to modify a value inside the array, whether you simply to the temperature array, you put

04:11.760 --> 04:14.940
the index to, let's say I want to modify the first one.

04:15.970 --> 04:23.860
So actually, index number one would be the second element, and let's put it this will put this value

04:23.860 --> 04:28.810
inside the element of the rate which corresponds to index one.

04:29.650 --> 04:35.500
If I print again after that, you would see that the value has changed.

04:35.980 --> 04:39.560
It was so twenty four and then it is thirty.

04:40.480 --> 04:46.570
OK, so you can see how to get access to one element, how to modify one element.

04:46.840 --> 04:53.110
And then if you want to go through all of the elements of the array, well you can use the for loop

04:53.110 --> 04:54.760
that we have just discovered before.

04:55.450 --> 04:59.820
So for int I is equal to zero.

05:00.340 --> 05:07.540
So we are going to start with an index at zero and then I is lower than the size of the area.

05:07.570 --> 05:10.540
So five so here is strictly lower.

05:10.570 --> 05:10.760
OK.

05:10.780 --> 05:16.630
This is very important because you don't want to get to the value five because five will be outside

05:16.630 --> 05:24.340
of the array because we start to count at zero and then I guess plus you close the parentheses, you

05:24.340 --> 05:27.790
open a new block of code and let's say serial that.

05:28.930 --> 05:34.450
Brent L.N., so what you can do is you can do temperature.

05:36.230 --> 05:44.920
All right, open the brackets and then the index here will be high so you can use actually the counter

05:45.350 --> 05:51.410
index we use in the fall, you can use it in the block of code because this is a valuable you create.

05:51.420 --> 05:52.850
So this is a local valuable.

05:52.850 --> 05:58.250
You create all the block of code that followed so you can use the eye.

05:58.430 --> 06:03.620
And I would be equal to zero one, two, three and four and then exit.

06:03.830 --> 06:05.180
So don't forget this.

06:06.020 --> 06:11.510
I'm going to comment those two lines here so we can print out of death.

06:12.140 --> 06:15.710
Let's upload the program and let's see what we get.

06:17.880 --> 06:24.390
OK, you can see 23, which is the first one, 30, which is the second value that was modified here,

06:24.810 --> 06:26.750
and then fifty six, 12 and 14.

06:26.770 --> 06:31.040
So we have just gone through all of the elements in the area.

06:31.350 --> 06:37.620
And these we are going to use the exact same structure later on when we use multiple entities in our

06:37.620 --> 06:38.060
program.

06:38.280 --> 06:39.960
So that would be malpractice for you.

06:40.260 --> 06:48.600
OK, to rig up an array is a collection of related valuables sharing the same data to create an array,

06:48.600 --> 06:53.150
you first need to declare it and give it a size which cannot be changed afterwards.

06:53.580 --> 06:58.430
Then you can access and modify any value from using an index.

06:58.860 --> 07:05.400
Finally, using full loop is a pretty efficient way of going through all the values from an array.
