 Welcome to this session on arrays in C++. You will recall that we had seen the need to handle a large number of values. In particular, we wanted to have a single name to represent the set of variables and individual elements should be accessible using just an index. In this lecture, we shall study the C++ array data structure and its properties. We will also see how elements are accessed in an array. In particular, we will look at a couple of examples of using arrays in C++ programs. So, an array in C++ is a collection of elements of the same type. It has a name which can be chosen by us and it has a fixed size which represents the total number of elements that the array can hold. To declare an array, we use our standard declaration. In marks, in square brackets 500 means marks is no more a simple variable, but it is an array of size 500. Roll underscore numbers 500 denotes another array called roll numbers which has 500 elements. Float distances declares a third array distances with 25 elements, but each of these 25 elements will hold a value of type float. We note that only one element participates in any operation, whether it is input or output operation. It can participate as an operand in an expression or it can also appear as a location on the left hand side of an assignment statement. That means, individual elements can be assigned values. We further look at the notion of index expression which is used to refer to an element. Written in square brackets immediately after the array name wherever we want to refer to an element, the value of index expression is actually the reference to the element. For example, when we use a i, if i is equal to 5 at that point, a i refers to fifth element. If i is equal to 123, a i refers to 123rd element. There is one hitch however. Notice that in our discussion on arrays, we assume that the index will start from 1, 2, 3, 4. Unfortunately, the index in C plus plus always starts with 0. That means the very first element in the array is actually 0th element, then first element, second element, etcetera. This is pictorially represented here. If I declare an array A as int A 100, then it would be allocated consecutive memory locations, but the very beginning element will be A 0, then A 1, A 2, A 3, etcetera. Suppose, the array stores 5 values in its first 5 locations, then effectively it means that A 0 contains 53, A 1 contains 79, etcetera. At this point, please also note that the value 100 appearing in the declaration is not an index. It is actually the size of an array. This is prescribed only once in the declaration. Subsequently, whenever I write anything inside square brackets and associate it with the array name, it always means a particular element of the array. The index can be any expression. That index is first evaluated and the resulting integer value is used to identify a particular element. An index expression naturally must result in a value in the range 0 to size minus 1. Size is as declared in the definition. For example, if I have declared an array of size 100, the index must range in the region 0 to 99. Please remember that if we do not do that, then the results in C plus plus are completely unpredictable. So, it is our responsibility to maintain an index at any time to be between these limits. An index for first element is the 0, next element is 1 and so on. For example, A j is a valid expression. For j equal to 86, it will be 86 element. Suppose, I use an expression k minus m plus 52 and if k is 1200, m is 1240, then the index value evaluates to 12. I may even say A x slash 5.0. This is actually a floating point expression. If x is 7.0, the expression evaluates to 1 because the result is always converted to an integer value. Similarly, for x equal to 22.5, it will evaluate to 4. Of course, it does not make sense to use floating point expressions in index expression. Let us now look at a program written to find the sum of marks of n students. We have already seen such a program except that this time, we wish to preserve the marks that we input for all the students. We declare an array marks which are 600 element, may be because we have up to 600 students in the class. We initialize sum to 0. We use the same count variable and n as the maximum number of students whose marks are going to be input. So, we read the input value for n. As last time, we set up an iteration. This iteration will work using the FOR statement. It will work with count equal to 1 all the way up to count less than equal to n. So, this iteration will be executed n times like we saw last time. All that we do is we read the counteth element of marks and then add it to sum. We expect that at the end, we shall have found the correct sum. But way, there is one problem. While the iteration that we have written, the FOR loop control for the iteration will definitely execute the iteration for 1 to n. However, please note that we are additionally using this count variable as an index of the array as well. Unfortunately, marks, the array starts with index 0 and not with 1. This will work of course. Marks 0 will not be read. However, when the last iteration is executed, n will go to n plus 1th element. We do not want this. So, we can correct this by simply running the loop from 0 to n minus 1. So, observe that by setting count equal to 0, count less than n. We automatically convert the same iteration control into a different starting point and a different ending point. Notice that now when count is used as an index, it will move correctly from 0, 1, 2, 3, 4 up to n minus 1. Thus, reading the first n marks into the first n elements of the array and this will find the sum correctly. Please remember this kind of structure, this kind of control structure or this kind of usage of for statement to control iterations is almost always deployed whenever you are dealing with all the elements of an array. Here is another example which finds the value of the largest element. You are familiar with how to find out maximum of given numbers. This time we read all the numbers in an array and then find out the maximum. So, here is an example where an array a is declared with 1000 elements. We have a variable called max. n again is the number of elements whose maximum which we wish to find out. i is used here as a counter instead of count, but the effect is exactly same. We read the value of n. We set up an iteration for i equal to 0 to n minus 1 and read all the elements of a from 0, 1, 2, 3, 4 up to n minus 1. Now, we want to find out the maximum. So, we assign the 0th element of array to the variable max and now we compare this max successively with all the remaining elements. Whenever we find an element of a which is larger than max, we replace the value of max. This is a well known algorithm. Notice this iteration which starts with i equal to 1. I already have accounted for the 0th element. I want to examine the remaining elements from 1 to n minus 1 which is what this iteration does. In this iteration, if a i is greater than max, I simply reset max to the a i. At the end, you can very easily conclude that when I come out of this iteration, the maximum value will be located in max. In short, we have learnt how to declare arrays in C plus plus. We note that we must declare its name and size and size must be an integer value. Please note that it cannot be a variable or expression in declaration because those many locations have to be allocated by the compiler even before execution starts. We have learnt how to use an array using an index expression. We must evaluate index expression to an integer value that is always between 0 and size minus 1. We can now iterate over an index variable to successfully scan, use, process all elements of the array as we like. Thank you.