When we have declared and initialized a dynamic array, we'll of course want to use it. In order to do this, we can either use the array globally, or element by element. To use an array globally, it isn't very frequent, but I can be useful occasionally, we'll simply globally assign an existing array to the array that we want to manipulate. So this will be done simply with the usual assignment syntax (=). Let's take an example. Suppose that we have an array tab1, that we've declared beforehand, and initialized here with three values. It's an array of integers. So this array tab1 will contain, at the beginning, three integers, which have as values 1, 2, 3, because of this initialization here. We then declare here a dynamic array tab2, which is the empty array. The declaration we have done here, as it has nothing after it, declares an array initialized to an empty array. It's an array without elements. We'll then make different computations, and finally, at a certain point, we'll maybe need to make a copy of tab1 in tab2, so we'll make a copy of tab1 in tab2, which means that now, tab2 will contain the elements 1, 2, 3. Once again, this is a copy, so starting from this line, tab1 and tab2 will continue to live separately, if I modify tab1, there won't be any modifications on tab2. If a modification is made here, there isn't any modification in tab2. tab1 and tab2 are two completely separated arrays. That's for the global assignment of an array. But in general, we'll rather manipulate arrays element by element. To designate the element of an array, we'll use the following syntax : the name of the array, followed by, in brackets ( [ ]), the index of the element that we want to select. Be careful, the indexes of an array's elements, are between zero and the array's size minus one, they begin at zero and go to the array's size minus one. If for example, I have an array with five elements, that contains 4, 3, 2, 7, 1, the element 4 here, is the element with the position zero in the array. If this array is named tab, I'll use tab[0], to designate this 4. Then, I have tab[1], here I have tab[2], tab[3], tab[4]. This array has a size of five. The last element of the array is tab[5 minus one], namely tab[4]. The indexes do go from zero to the array's size minus one. The first element of the array is therefore indeed designated by tab[0], and the tenth element of the array is designated by tab[9], as we begin at zero. Be also careful with the fact that there is no control at all if you are in a situation of array overflow. If for example, you write with the previous example tab[10], nothing will tell you you have overflowed here the array's size. So be careful when you write something like tab[i], to be sure that i is between zero and the array's size minus one, or is strictly less than the array's size, or else you may encounter this fatal error, that we call segmentation fault, which indicates a memory access error. I'm telling you, you may encounter it, because it isn't even guaranteed that the segmentation fault happens, the error is there, but it isn't even sure that the program will detect it. Anyway, if the detection happens, at that moment, you'll have a segmentation fault, with here the typical example, that we encounter a lot in beginner programmers' programs, which is the following : we declare a dynamic array, of doubles, which I name v here, that is initialized to empty, as there's nothing after the declaration, so we initialize an empty array, so this array is strictly empty, there's nothing in it. It's an array with zero elements. In general, the programmer who have written a program, will rarely make the mistake just after it, but the error is still of this type, he'll write something like v[0] = with an assignment, to try and put 5.4 in the array, but as this array is empty, we don't even have a v[0], v[0] doesn't exist in the empty array, so here you'll get a segmentation fault. A common way of accessing the elements of an array, is to iterate over the array. In C++, we have three ways of iterating over an array. Since the new C++ 2011 standard, we have a way that let's us iterate simply over all the values. We'll detail it in a moment. You could also, of course, with the iteration you have seen until now, the standard for loop, iterate over the different explicit indexes of the array, so if I designate these indexes using i, l'll do a loop iterating on different values for i, i will begin with the value 0, and will go for example one by one, till the last element of the array, the last element, let me remind you, if t is the array's size, has the index t - 1. So here we'll do a loop that goes from 0 to strictly less than the array's size. The question is how are we going to represent this size, how are we going to know this size, I'll detail that to you in a moment, too. I'll also explain what this new size_t type represents. Finally, third way, much more advanced, which will not be presented in this course, is to use what are called iterators. Once you want to iterate over an array, the question you'll ask yourself, is which of these two previous ways to choose. In most cases, I recommend you to choose the first way of proceeding, to use the C++ 2011 iterations, which we will now detail. If you can't, in this case you use a standard iteration. So, let's move on to the iterations introduced in the new C++ 2011 standard. These iterations also use the key-word for, as well as parentheses (). But in parentheses, we'll this time use the colon separator (:) instead of the semicolon, and we'll have two parts, instead of three parts. Before the colon, we'll have the declaration of the variable that we want to use to iterate over the array, and after the colon, simply the name of the array that we want to iterate over. If the iteration over this array must modify the elements, you'll have to add between the reserved key-word here, auto and the variable name, add the modification symbol, like we used to indicate to a function f that its parameters could be modified by the function. Let me remind you that if for example we have a function f, that takes a parameter x, which we want to modify, what you must do, is to pass it by reference, so here we used the reference symbol to indicate a pass by reference. It's exactly the same thing here in this for loop, we'll use the reference symbol to indicate that the iterated array can be modified through the variable used to iterate it. Let's go to an example. Let's suppose we want to manipulate an array of integers, which we will name ages here, in the plural form, which is a dynamic array containing integers, and which we initialize at the beginning with five integers all with the null value. At the beginning, we'll want to fill this array with values requested from the user. To achieve this, we iterate over the array with a for loop. But as we are going to modify the array, it is important to iterate over the for loop here, with the modification symbol, with a reference to the array. The syntax is therefore the following, we'll have the key-word for, followed by a parenthesis, followed by the array's name, ages, that is after the colon, and before we'll find again auto, and the name of a variable, so here we declare a new variable age in the singular form, that will iterate over the array. Then, in the block associated with the for, you can simply use this variable age, it's the name we have given to the variable which will store the different values of the array. Like any variable, it is going to alternately iterate over the different elements of the array, it is going to iterate over the array step by step. Every step will print the intended message, the age of the next employee, to ask the age, and stored in this variable age here, which, alternately, will go over every element of the array. What will happen is that the first time the loop is executed, the variable age will indicate the first element of the array, and we'll ask a value for this variable. Let's suppose that the user inputs the value 5, at that moment, we'll put 5 in this cell of the array. We then redo the iteration, its the role of the for loop to iterate, and at that moment, age will move to the second entry of the array. Let's suppose that here, the user inputs 7, at this moment we'll have 7 that goes here, and so on, until age has iterated over the entire array, until we fill the whole array. We'll then be able to print it using another iteration, so we begin here by displaying a message to say that we are going to print the array of ages, then we redo an iterative loop with the key-word for, the parenthesis, the colon which separates on one side the array's name, ages in plural form, and on the other side, a variable. which I can call again age, it's another variable age, it has another scope, it will have the scope of this for loop here, while the previous age variable had the scope of this first for, here. And we use this age variable to iterate over the array, like any other variable, simply, what will happen is that at the beginning, this age variable will indicate the first element of the array, and then alternately, will take the value of the different elements of the array, so we'll begin by printing 5 here, then we'll print 7... until we have iterated over the entire array. This way of writing loops is therefore very useful because it is compact, it suffices to simply write the key-word for to indicate that we want to iterate, to put the name of the variable, a colon, the array's name, like we had written previously, simply write for "(auto", indicate that we have a variable age which iterates over the array of ages. Voilà , nothing more to write, so it's very compact. This way of iterating doesn't allow you to iterate over several arrays at a time. Indeed, we here have a single array over which we can iterate, so we couldn't for example compare arrays or insert an array in another... We can't access several elements either, because at one single moment, we have a single variable that indicates a single entry. So we can't shift for example the elements of an array by taking an element and its following element in a same loop. Finally, we can't skip elements of the array. This way of iterating goes over the different elements of the array one after the other, we can't skip an element to go to the next element. So if we need one of these three cases, iterating over several arrays, or accessing several elements, or skipping elements during an iteration, we'll have to use another for loop, use the for loops we saw in the course about control statements. In this type of for iteration, we'll typically need to indicate the array's size, for example to indicate the last element, which has the index size minus one. So how can we indicate the array's size? To achieve this, we'll use what are called specific functions, in object-oriented languages, we speak of methods, which can return the array's size. a specific function is simply a function that is associated to a variable, and that we use in the following way, if I have a variable, a dynamic array named tab, to know its size, I'll use tab.size(), the function size is applied to the variable tab, tab being the array's name. The return type of this specific function, size, is a new type which is known as size_t, it's just a particular int, which has the garantueed property, by the standard, to always be positive. As the standard says that the return type of the specific function size, of the dynamic arrays, is size_t, you have to declare, when you are using variables to store such a size, you have to declare them of type size_t. For example, we'll write with a standard for, as we have seen in the course about control statements, with three parts separated by semi-colons, the declaration of a variable of type size_t, as we are going to store in it the size, which we name here i, which will iterate until the array's size, so here strictly less than the array's size, as the indexes must go to the array's size minus one, we'll stop just before tab.size(). And finally in the iterative part we'll go one by one, so if I take the same example as before, with the array, ages, initialized with five integers, all null, to input them, we'll for example, with a standard loop, write "for (", the declare size_t i, a a positive integer, and initialize it with zero, that is, we'll begin with the first element of the array, so the index is zero. Then, the "test" part of the iteration, while i is strictly less than ages.size(), as the index of the last element must be the array's size minus one, we'll write strictly less than ages.size(), for example, we'll iterate one by one. And what are we going to do? We'll ask the question, (what is) the employee's age. Here humans rather count from one to n, instead of from zero to n minus one, we'll print here i + 1, which will print 1 first, then 2, etc. until n if n is the array's size, and we'll get the element at the index i of the array ages, with the syntax on the array's access that we have seen. That's it ! Perhaps all this is a bit too much, the next episode will illustrate these cases with typical examples.