 Hello and welcome to the NPTEL course on an Introduction to Programming through C++. I am Abhiram Ranade and this lecture is going to be on arrays. The reading for this is chapter 14 of the text. So let me begin with an obvious observation. So computers should generally deal with large problems, large computations and large data. So for example, we may want to simulate what happens when many balls are moving around in the box. Of course, these are not real balls. They might be gas molecules and we may be simulating a gas and what kinds of effects it has on the balls or pressures and things like that. When altitudes of various points in a lake find how much water is there given the water level. So this might well be a computation that we might want to do or given the road map of India find the shortest route between two cities and this is probably something that you might already have used, you might already have used a trip planning program. So computers handle a lot of data and how do they do that? So there is a very, very basic problem. If you want to have lots of data, you want to have lots of variables, presumably. So if you need lots of variables then just writing out the variable names to use would be really tiring. So maybe you want 1000 variables to represent pressures at 1000 points then you may have to write something like this, double pressure 1, pressure 2, pressure 1000. Nobody would really want to do that. So arrays solve this problem and actually a little bit more and arrays come to us from C which is the precursor of C++ and in C++ there is a more modern somewhat nicer solution to this problem called a vector class. So we will discuss this a little bit later. But arrays are in some sense simpler to understand and they do most of the work anyway. So whatever we are going to do in this lecture sequence will be definitely useful in any case. So here is what we are going to do. We are going to talk about how to create arrays and how to access their elements and then why do we use arrays? So well we are going to use arrays for storing sequences, storing sets, storing something which you might call a queue and storing multiple graphics objects. And we will do lots of example programs along the way. So that is the outline for this lecture sequence. Now let me begin with a definition of arrays or rather an example. So if I want to define the pressure variables which I mentioned in the last slides, in the last few slides, then I just need to write this. Now this essentially defines 1000 variables and these 1000 variables are called array elements and together those variables comprise what is called an array. The variables you can think of as being named not pressure 01 word but pressure square bracket 0 close square bracket as the first variable, pressure square bracket 1 close square bracket as the second variable and so on till pressure 999 square bracket open 999 square bracket closed. That is effectively the name of the last variable we are creating using this statement. Notice that the numbering starts at 0 and not at 1. So the variables are not named pressure 0 through, pressure 1 through pressure 1000 but they are named pressure 0 through pressure 999. All within the indices the 01 to 999 are in square brackets. So the general form is the type of the element data type, array name and the size. The size is also called the length. So in our previous example the data type was double, every element was of type double. The name of the entire array was pressure and it had 1000 elements and the moment you say it has 1000 elements you are also saying that the numbering goes from 0 to 999 and it is customary to say that array name square bracket index gives the index variable where of course index goes from 0 to 999 or in general index goes from 0 to size minus 1. Index has to be smaller than size. Now arrays actually end up being a little bit more than just a collection of variables. So this is because the index can be given as an expression. So I do not have to say 999 or I do not have to say 47. I can put another variable name there and the value of that name will be taken during execution. So this makes arrays really powerful and not only variable names but I can put in whole expressions there as I wish and we will see examples of all of these things. So what can you do with arrays? Well suppose we have declared our array in this manner then I can read into its elements. So if I say C in and instead of putting in the name of some simple double variable I put in this pressure 0 which is also a variable. So this would cause whatever the user types the first thing to go into pressure 0 and the second thing to go into pressure 2. So you can use array elements basically wherever you can use ordinary variables. So I could write this for example this is just saying that pressure 1 so the element of the array pressure at index 1 is assigned the value which is the mean of the values of pressure 0 and pressure 2 and as I said we could have the index be a variable. So here I have a for loop with the control variable I going from 0 through 999 and we are reading consecutive words that the user types into pressure 0 pressure 1 pressure all the way to 999. So notice that this could not be written in general if we had to write a constant in place of that square bracket I square bracket. So it is really important that that number be coming out of a variable. So in any case this will take the 1000 values that the user types and place those 1000 values in pressure 0 through pressure 999. In the 0th iteration the value received will be placed in pressure 0 then the value of I will increment the next value received will be placed in pressure 1 and so on. And I can print out the values as well and again I could have written something like p times 3.33 and for any variable instead of any variable I can put in any array element reference. Here is another interesting statement now this time I is going from 1 and not to 999 to 998 and so what is this doing? So it is saying pressure of I equals pressure of I minus 1 plus pressure of I plus 1 upon 2. So the pressure in every element is made equal is being made equal to the average of the pressure in the previous and the next element. This is not a real computation but I am just giving this as an example to show what kind of expressions we can write. And here I cannot use the array index I to start at 0 because if I did that then for I equals 0 this value would be 0 but this value would be minus 1 and that is not allowed. So we want the index to always be between 0 and the size, so less than the size. So the size is 1000 so this had better be less than the size and so had this be. So if in particular I had been 999 then this would be 1000 and that would not be allowed. So therefore we have carefully chosen the bounds of this loop to be 1 and 999. So the important point which I have noted is that the array index can be an expression say for example here I minus 1 and I plus 1 or even just simple plane I is an expression which will be evaluated during execution and then the corresponding element will get used. Now if we just had written pressure 0, pressure 1, pressure 2 without the square brackets we actually had taken the pain of writing down all the 1000 names we would not be able to write something like this loop because over there if your variable is pressure 0, 1 word then you cannot replace a part of a name by the value of a variable. So this is this square bracket notation and the way we define arrays is really important. Now here we have again the same definition and suppose I write pressure 1000 equals 1.2 to look at it it does not look bad. However we said that the index or whatever appears in the square brackets must be in the range and the range is 0 through 1 less than the number of elements. So this number is supposed to be smaller than 1000. So if you put a 1000 over here that is not good that is a mistake that is an error. So here I have put the number minus 5 and that is also a mistake. So both of these mistakes are set to be caused because the array index is outside the allowed range and if the array index is out of range then it is a mistake and nothing really is guaranteed. The program may run but of course it cannot run correctly and so it may produce wrong results or it may halt with a message. So you really have to be very careful when using arrays. You have to be very sure that the indices are within the specified range. Now when you define arrays you can initialize them if you wish. So for example this is an integer array called squares with 4 elements and I want those elements to be 0, 1, 4 and 9. So I can write them in this manner. So squares of 0 will be 0, squares of 1 will be 1, squares of 2 will be 4, squares of 3 will be 9 or I have put down cubes and I have not specified the size. But C++ will look at how many values you have specified and make cubes have that size. So here the size 5 will be inferred by C++ even if you have not put it down and you can mix things. So I am in this single statement I am defining an ordinary variable X, then I am defining an array PQR with 200 elements and I am defining an array Y with 4 elements but I have not specified its size and C++ will infer it. So some quick exercises for you, so define arrays of in various ways. So please do try them. So what have we discussed so far? We have said that an array is a collection of variables of the same type. So if the number of elements in the array is n, then the array, the elements are named A0, A1, An minus 1. So A is the array name and I is the, I is called the index. Now the important point is that the index can be specified by an expression and the expression is evaluated and the result is used to determine which variable of these of this collection you mean. So we saw how to declare arrays with and without initialization, how to get values into and out of array elements and next we are going to take some examples of use of arrays. So we will take a quick break.