 Hello, and welcome to the NPTEL course, An Introduction to Programming through C++. I am Abhiram Ranade and this lecture is about arrays, the reading for this is chapter 14. So in the last lecture as well we talked about arrays and so far we have said that you can define an array by writing something like this, give that name, give the type of the array, then give the name and give a size and this creates so many variables, each of type, LM type that you have mentioned in the definition and a name is the name of the array and informally a name denotes the entire collection of variables a name 0 through a name size minus 1. Now in this lecture, lecture sequence we are going to look at how the computer or how C++ views arrays, what exactly happens when we do array operations. So for example, where are the elements stored in memory, how does a computer index into an array and what happens if you give an index out of range, then we are going to talk about how function calls can be made on arrays and we will talk about a function for sorting an array. By sort I mean rearrange the elements so that they are in non-decreasing or non-increasing order. The order that we are going to use is going to be non-decreasing. So what is the computer's view of an array definition? So let us say we have this array Q with integer elements and 4 integer elements and these being the initial values. So in what I am going to discuss next, I am going to make the assumption that a single int uses one 4 byte word. So if I want to store this entire array I need 4 4 byte words or 4 and they have to be consecutive and so 4 consecutive words of memory are allotted to this array Q and in that allocated memory we are going to store the variables Q0, Q1, Q2, Q3 in order and also when this statement executes the initial values will be stored in the allocated regions. So here is a possible outcome. So I have shown here the address. So 104 at address 104 I am going to start storing Q0. Q0 uses 4 words. So 1 0 4, so byte 1 0 0 4, byte 1 0 0 5, byte 1 0 0 6, byte 1 0 0 7 is going to be used for Q0 and in that we are going to store the content 11. So the data stored in these bytes 1 0 0 4 through 1 0 0 7 is going to be 11 because we said that Q0 should contain the value 11. Then Q1 will be stored likewise in bytes 1 0 0 8, 9, 10 and 11 and these 4 bytes will contain the binary equivalent of 12. Then similarly 12, 13, 14, 15 the addresses or the corresponding bytes will be used for storing Q2 and 16, 17, 18, 19 will be used for storing Q3 and again they will also be initialized with the appropriate bit strings representing in binary or actually in binary 2's complement the numbers 13 and 14. Now this is fine, this is what this is these are details but now let us come to how does a computer or how does C++ think about the array name? So how does C++ think about Q? So the array name is thought of as not as the collection of all these variables but simply the starting point of this region. So this region is allocated. So what is the region that is given to us for storing the array queue? It starts at 1 0 0 4 and the last byte for this is 16, 17, 18, 19 so 1 0 1 9. So this area from 1 0 0 4 through 1 0 1 9 is what is given to this array queue. So the array name C++ has decided is going to be is going to mean or is going to have the value rather equal to the address of the allocated region. So in our example or it is also the address of the 0th element and in our example Q will then mean 1 0 0 4. So this is natural in a way and not natural in a way because it is so what would be sort of the right thing to have done over here? So we could have said that Q really means the entire area from 1 0 0 4 through 1 0 1 9 but instead of that C++ sort of makes it simple. It just wants to associate 1 value with the symbol Q and so it says that it is going to be the starting address. So the starting address of the entire area or the address of the 0th element. So the address of the 0th element is really the same as the address of the allocated region. So Q is going to be 1 0 0 4. Now the value of Q has not mattered until so far until so long until now. It has not mattered so far but we will see that it will matter soon enough. It will be used soon enough and we also have a type associated with this name. So the type associated with this name is going to be int star. Well that is consistent with its value. Its value is 1 0 0 4 which is an address and therefore the type of an address to an int variable is int star and so the type is defined in consistency with our first decision. So that is how C++ views Q its value and its type. And the array name is a pointer. So it is an address or a pointer but its value cannot change. So when we talked about pointers, when we talked about functions, our pointers could be changed but this pointer cannot be changed. So I cannot say in my program Q equals 1 0 0 8 because in fact you can think of Q as being a const. So you remember that we can declare variables as being const. So Q is actually a const pointer. So if you write something like this C++ will flag it as an error. The idea is that look Q really is referring to this array and while it is not telling you till how long the array extends it is telling you where the array begins and so please do not change it. That is what this whole, the rational of this whole discussion is. In general what happens? So if you have lm type a name a length, a block of memory of s time, s time a length is allocated and s has to be the size of a single lm type variable. And so a name then is the starting address of the zeroth element or the address of the allocated block just like our Q example. And the value of a name cannot be changed and the type of a name is lm type star or pointer to lm type. So this is just a generalization about of what we said for our original definition in Q4. And of course the type of a name of i is lm type. The type of just this is lm type star and the type of a name of i is lm type and that applies to Q as well. The type of Q of 0 is integer and the type of Q was in star. So an exercise for you figure out what is printed when the following executes and in this you are expected to make reasonable assumptions if you wish or if you do not want to make any assumptions say that that value cannot be predicted. And let me tell you what reason it assumption might be that you may say that oh so as the compiler is making is allocating memory it sort of allocates memory in increasing order of addresses. So that might be a reasonable assumption. Here is one more so again you are given a fragment and you are supposed to point out mistakes in the program fragment. Alright so what have we discussed in this segment? We said how memory is allocated for an array basically consecutive addresses are assigned consecutive regions of contiguous region of memory is given to you. And we also said that if an array has name a name then the type of the name is t star where the array elements have type t and the value of the name a name is the address of the zeroth element of the array or the address of the starting or the starting address of the allocated region for this array. In the next segment we are going to see how a computer interprets the expression a name of index. But before that we will take a small break.