 Good morning. As we discussed last time, we need to look at the mechanism to represent a large number of values, a set of values. This is implemented by a structure called array in C plus plus. We shall be looking at how arrays are defined, how they are used. We shall look at the examples of array usage, what is known as salting, a major set of algorithms in any programming endeavor. And finally, we look at how we handle non-numerical data such as names of people, names of courses, names of whatever. At the end, I will distribute a take home test. It is a sort of practice quiz, which is deliberately kept at a very simple level. This is just to give you a sample of the kind of questions that may be asked. The actual quiz on 26, of course, will have several tough problems. There will always be at least one problem, which very few people would be able to solve. And of course, all problems will be unknown. We never repeat questions in the actual exams. So, this is just for your practice. We begin by looking back at the algorithm that we wrote, the program that we wrote for finding out the maximum of certain numbers. So, suppose we want to find out the largest of four given numbers, this was the program. Everybody remembers this? So, I define four objects v 1, v 2, v 3, v 4. I read all of them. And then I say max is equal to v 1. If v 2 is greater than max, max is equal to v 2. If v 3 is greater than max, max is equal to v 3, etcetera. We have also seen an iterative version of this algorithm, where we do not read all four different values in four different locations, but we read one value at a time. And at the end, we could have the maximum value. The problem is, when your large number of values, what do we do? So, you will recall, for this particular algorithm, we remember this constraint. And we would sort of iterate around this again and again and again. Of course, we will initialize max to some first number or something like that. But effectively, if you look at your memory, it will have a location for a and it will have a location for max. Suppose, the input values are 4, 52, 17, 28. The actual values, which have been read by the program, what will you have at the end in a, 28? Somebody said 52. What will you have in max? 52. I think you have forgotten the original algorithm that we wrote. Remember, we were repeating this, while a was not equal to 0. And we had said that we will give a 0 value at the end, an artificial value. That would have been read. And therefore, when we out of the iteration, we will have the value 0 here. So, while we have found the max correctly, we have lost all the individual values. At the end of the program, we have only a, which has artificial value 0. What if we needed to use the values of all the individual numbers that were input at some later point in program? We cannot have them. We do not know. This problem comes up in various issues. So, what do we do when we have a large number of values? We use iterative control structure. We use only one location to input a new value iteratively. We get the maximum, but we lose all individual values. This is the observation. We may need to retain and use those values again. Here is another problem. Find individual digits of a given positive number and determine a number formed by reversing the digits. So, how many objects or location names should we use? Unless we know the number of digits that are likely to be present in that number, how will you determine what should be the digit names that you should use? Here I have given an example of a full digit number. A full digit number, if I want to extract individual digits, I will need 4 locations. So, I am calling them D 1, D 2, D 3, D 4. The actual number is… So, suppose this is the actual full digit number. We want D 1 to contain what? 7. D 2 to contain what? 4. D 3 to contain 9. And D 4 to contain 2. Suppose this number is n. So, this number how will you get different digits? The last digit D 4 is very simple. If I say n modulo 10, I divide 7, 4, 9, 2 by 10. The remainder is the last digit. So, this will give me D 4. How will I get D 1? D 1 is also easy. I take the number and simply divide it by 1000. 7, 4, 9, 2 divided by 1000 will be 7.492. Convert it to integer, it will be 7. So, this will give me D 1. The other ones pose a problem. How do I calculate D 2? D 2 is this digit 4. So, to get this, I may first want to remove 7. Then I will have a number 4 9.492. And then if I divide it by 100, I will get 4. So, I can do that by saying n modulo 1000. When I say n modulo 1000, what do I get? 492. And if I now divide this by 100, will I not get D 2? This is one method. This is no unique method. You can do it in variety of ways. But given that this is the nature, this is the program I have written. So, I have defined 4 objects D 1, D 2, D 3, D 4. I have defined a variable nu n, an object nu n, which will contain the reverse number. I first read a 4 digit number. Observe that I am checking whether the number is truly a 4 digit positive integer or not. As I have defined, in most practical programs, you should validate whether the input is correct or not. Rather than program ending erroneously or the operating system or the program throwing up some error, it is better to check for such thing. So, this is what I do. Notice that this if statement, the end does not have any else. It is required because of the return 0, which is included here. Indeed, in real professional programs, I will not put 0 here, but I will put 1 or some non-zero number to indicate to the operating system that the program has not ended well. The point is this if statement is implicitly breaking out of the main function, complete. It is just going back. So, therefore, no else statement is required. If at all I come here, whatever subsequent statements are executed, they will be executed only if the input is valid. Otherwise, I will not come here. Then this is how I calculate individual digits. D 1 is n by 1000, D 2 is n, modulo 1000 by 100, D 3 is similarly n, modulo 100 by 10 and D 4 is simply n, modulo 10. In fact, if you wish, you can write D 4 equal to n, modulo 10 by 1. This will tell you how you could do this job iteratively using only D as a digit. But that is not the point here. We want to retain the values of individual digits. Having found out D 1, D 2, D 3, D 4, how do I construct a value which is reverse of the original number? Instead of D 1 into 1000, I will make D 4 into 1000, D 3 into 100, D 2 into 10 and D 1 into 1. So, I will get the reverse. The problem is how do I handle a large number of individuals? So, this is not the only instance where you have to handle a large number of individuals. You may require the following problems. You may face the following problem. First of all, it is inconvenient. Suppose I had a 100 digit number. We know C plus plus does not represent values of 100 digit numbers. But let us say a special function has been written called high precision represent. High precision add, high precision subtract, high precision multiply, etcetera. Where numbers up to 100 digits are permitted? We will see in one of the examples or one of the exam problems how to represent and handle such number. Now, we have to input these individual digits. There you cannot input a number as a whole in n. You have to input individual digits because hundreds of digits cannot be stored together as a single number anywhere. What will you do? How many names will you write? D 1, D 2, D 3, D 4, D 5, D 100 and suppose the number that you are storing is 712. Then you will have to say D 1 equal to 0, D 2 equal to 0, D 3 equal to 0, D 4 equal to 0, everything. Last three digits are 7 and 2. That will be stupid. It is inconvenient. Secondly, it may be impossible to do so. You may not know exactly how many people are there. You may know a maximum limit. For example, students in this course as you know the registrations are changing every day. I do not know exactly how many students are there. They are large number all right. So, what I would like to do is I would like to let us say I would like to store values of role numbers and values of marks obtained in some one exam. Let us say the quiz. Then I would like to have some number of numbers to be represented. I will say, okay, at the most I know that there are 600 students in my course. The actual number is much less. May be 478, 480, 482. The actual number of students who take the exam may be 455. Somebody is upset. How do I handle such situation? We may have to handle arrays, arrays which are known in maths. You are familiar with arrays. I shall see some example. You are not familiar with arrays. You are familiar with matrices. That is very funny. You know matrices, but you do not know arrays. Okay. Well, let me put it this way. You know arrays, but you do not know that you know arrays. We will just figure it out. But two-dimensional matrices you know. So, matrix multiplication. Not very enthusiastic, yes. Everybody knows matrix multiplication. How do you handle them? If I have a 100 by 100 matrix, I need to individually store each element and use it in some computation. How many elements? 100 into 100 quickly? 10,000. Anybody has patience to write 10,000 different names? No. So, we need something else. We need a completely different mechanism to handle this. Okay. So, here are some examples of array notation used in maths. Now, do not tell me you are not familiar with this notation. Sum is equal to sigma ai i equal to 1 to n. Are you not familiar with this? You are familiar. Well, a is nothing but an array of values. So, what do you call a set of values? What do you call a set of values? But if it is an ordered set, you have the first element, second element, third element. So, you know fifth element is this means it has some specific. When you use such a notation, sigma ai summation from i equal to 1 to n. So, we call it the array elements or the elements of the set are summed over from i to from 1 to n. Is that clear? It is easy. Now, this is what is our notion, mathematical notion of an array or to be very clear in your terms, it is a set of numbers. Such a set of numbers could very well be represented by write x equal to this set. Some people may use curly brackets, some people may use square brackets, some people may use some brackets, but effectively you are representing a set of values. You are familiar with this notation? Why? So, when I say x is 73, 14, 3, 128, etcetera, etcetera, what it means is I am representing a set of 11 values and each element of the set can be represented by using an index notation. So, when I say x i as a subscript, I am representing the i th l. So, in this context, what is x 2 then? What is x 7? What is x 22? You cannot find out x 22. Of course, it does not exist. That should be obvious. If I have said that you have a set of 11 elements, then a 22nd element has no business to exist. So, it does not exist. What if we have to add one more value to the array? When you add one more value to the array, the array does not any more contain 11 values. It contains 12 values. x 22 will still not be defined. No, I am… See, we are discussing array as we know mathematically. We are not discussing arrays in C plus plus 8. Please understand that. You are again talking about which two arrays, mathematical arrays or C plus plus arrays. In mathematics, every array is a distinct array from the other array, not only if it has different number of elements, but even if it has same number of elements, but any element differs, it is a different set. You understand set, basic set there. If a set has n elements, then there could be infinite sets of n elements, but each one would be distinct if even one element of the set is different from other elements. So, in mathematics, all sets are defined by the set here. We represent generically an array using such a notation a or x or whatever. And what is the important point we are noticing is that we can use an index mechanism as a subscript to indicate which element we are referring. So, when we say x i, that is why I said in the context of this array with 11 elements or this set with 11 elements, x 2 will mean 14, x 7 will mean 4, 2, 3, 1. x 22 will not mean anything. x minus 3 will not mean anything. However, mathematically I can always say that I have a set of 11 elements, but my reference mechanism is from minus 3 to plus 8 or plus 7. So, I will call the first element minus third element, second element minus second element, third element minus one element, fourth element, zeroth element, fifth element, first element. It is my choice. It is maths. I can decide the convention and use it. In general, however, the common convention used by everyone is that if you have a set of elements, the first element will be called first, second will be called second, common sense. So, that is how we use arrays. Let us look at this array again. 73, 14, 3, 128, 3, 9, 2, 6, 3, 7, 4, etc. Suppose I have a problem. Given this set of values, we call it an array. We wish to rearrange elements of x such that the property given below holds. What is the property? For all i greater than j, x i is greater than x j or greater than equal to x. This is maths. So, you should know this. What does it mean? Is the present array satisfying the property? No. For example, if you take 3, this is what happens to be x 3. This is x 5. If i is 5, then i is greater than 3. 5 is greater than 3. 3, 9, 2, 6 is greater than value 3. Perfectly fine. Now take this. Here i is 10. 10 is greater than 7. So, x 10 should be greater than x 7, but it is not. Therefore, this property is not satisfied. As most of you have guessed, if this property is to be satisfied, I will get the elements rearrange in what is known as salted order. 3, 14, 73, 128, 128, 374, 825, etc. You agree that this is the salted order? Somebody last time had asked a question when we were discussing if statement. If A is equal to B, what happens? The corresponding question here is, when I have 128 followed by 128, which one of these 128 is the first one? Observe that it does not make any difference because in terms of element values, both are same. That is the reason why even in J e, the same rank is given if the scores are same. Now you cannot say which one is earlier and which one is later. All are same. Exactly the same. Whatever rules you apply at the end, there could be two people who meet all the criteria and have exactly the same score. Then of course, you can decide alphabetical order or whatever one. The point I was making is not really the J e related. The point I was making is that whenever you have same values, which one comes first and which one comes next hardly matters. They are same. Now do you agree that since I gave the example of J e, there are 400,000 students who appear for J e. There are 400,000 marks. After I do the totaling, is it not required that I sort or rearrange the marks in proper order? And is it not required that when I sort the marks, I also sort the corresponding enrollment numbers. Otherwise, only marks are rearranged. Role numbers remain same. So, the last rule number will be the top or if I arrange in ascending order, which is not correct. Now all these large number of values have to be handled without losing any individual value and that is why I require this notion. And what we have just seen is a algorithm that we will have to develop given some mechanism to handle large number of values. How do we sort? How do we search? How do we do any other systematic operations? So, C plus plus provide a structure which is called an array. It provides a single name for the entire collection and permits use of the index as we saw. There is only one problem. In our notation, we have been writing X i, A j. Can we write like this on a computer terminal? There is nothing like a superscript and except in word processing. C plus plus does not care for this. Suppose I write this as X i, but then C plus plus will treat this as a name. X i is a name. So, we have to have a special mechanism to indicate that look this is the name of the array and this is the index that we are using. The mechanism that is used in C plus plus as we shall shortly see is to use square brackets to enclose an index. So, this is what we use. This is exactly equivalent to this. There is some difference as we shall see in a moment. Is that clear? So, in a nutshell, C plus plus permits us to define an array. It gives a single name to the entire collection of the set so that we do not have to have thousands of names. But we can refer to individual element by using an index. Here is the motivation for an array structure. How exactly can C plus plus do it? Because after all different names means it can access. It knows C plus plus knows where is X, where is Y, where is Z, where is P. So, it can go to that place whenever the name appears in an expression. Imagine I have an array structure of this kind where I have different names. We need 100 integer locations. Assume that we are using only 5. We may define 100 names and use only 5 of these. Notice that in the memory I have shown various names A, A, B, A, C, etcetera, etcetera. Observe that I have to struggle to get the names. I am using only first 5, but that is my choice. How will C plus plus organize these locations in memory? Typically in the order in which you declare names, in that order they will be allocated locations, not necessary though. C plus plus can decide to take X somewhere else, Y somewhere else, etcetera, etcetera. But typically this is what will happen. Suppose that C plus plus was to allocate memory in consecutive locations. Then how will this look like? Suppose these are integer locations, how many bytes integer has? Four, typical. The first location A, A, suppose it starts at the actual address of 12,000. Address means this is 12,000 byte inside memory where the value A, A begins. Then 12,000, 12,000, 12,000, 12,000, 12,000, 12,000, 12,000, will be 4 bytes allocated to that location because 4 bytes are required for integer. If subsequent names are put in consecutive locations, the start address of A, B will be 12,004, start location for A, C will be 12,008 and so on. The start address of location D, V will be 12,396, agreed? In such a collection 12,000 can be calculated as a base address. Why is it called base? Because if there are consecutive locations and if all of them store integer type, then I know that if I know the base address, then I can calculate the address of any other number in that collection very easily. 12,000, 12,000, 12,000, 12,000, 8,000, 12,000, 12,000, etc. The first address I add 0 to 12,000. Next address I add 4 to 12,000. Third address I add 8 to 12,000. That means C plus plus can actually figure out what is x i given the value of i and given the value of base address. So, that is the motivation why people develop this abstraction of an array which could be so conveniently implemented in C plus plus when your compiler works on your program. The mechanism that is available, this is the mechanism, these are examples we shall very quickly see some rules. Int, roll NO 600, this 600 what is written here is not 600th element because it is a definition, int. It means that I have an array of maximum 600 locations. Float mark 600, what would it be? It would be an array of floating point values 400 of them. It so happens that float also requires 4 bytes, but if it requires 8 bytes, does not matter the C plus plus will know that this requires 8 bytes say double something. It can still calculate the address of all subsequent elements given the base address. So, when a compiler comes across int roll NO 600 float marks 600 etcetera such statements in the definition instead of allocating one location to the name as it usually will do in case of x y p q etcetera. In this case it will allocate 600 locations. Each location will be 4 bytes and it will remember the base address of the first location which it has assigned. Subsequently in your program if you say input roll NO 5 that means fifth element has to be read in. So, it will know fifth element how to go there this 0th, 1st, 2nd, 3rd, 4th, 5th whatever. Sum is equal to sum plus marks I notice that this exactly like sum is equal to sum plus x I with I as subscript instead of subscript the subscript is written in brackets. So, value of I must be defined earlier when you execute. How about I is 5200 meaningless for I equal to 0 sorry is a mistake here or k equal to 0 k less than equal to n minus 1 k plus plus too many mistakes. See out greater greater roll k greater greater marks k. Notice that the iteration is running from 0 to n minus 1 and this is the peculiarity of C plus plus arrays. The index does not start from 1, 2, 3, 4, 5 it starts from 0, 1, 2, 3. This is a stupidity which was considered to be a extremely simplifying assumption for the C plus plus compiler. Do you know what is the reason the base address itself becomes the address of the first element you do not have to add anything to it. So, instead of adding 0 or something there is no first element instead of subtracting 1 from the index and then calculating the address this is what they did. This is exactly like saying that I decide that in array of certain elements I will index it from minus 3 to plus 8 is my choice. C plus plus choice is that the first element is actually 0 th element then first then second then. So, if I am handling n elements the index will range from 0 to n minus. My dear friend these are this is not a program these are different statements as examples which may occur in any program. So, each statement please look at it independent if somewhere I say in roll n o 600 then it means I am defining. If somewhere in some other program I write C in roll number 5 then it means fifth element has to be ready. If somewhere else in some other program where such definitions have been done if I say for k equal to 0 to this output roll k marks k this is the usage examples. No memory is allocated by the compiler as soon as the definition is done when do you give the value you give the value when you execute the program when C plus plus allocates memory when it is compiling compilation happens before execution. So, write at the beginning he is asking a question when does C plus plus allocate memory when it appears in roll n o 600 or when the program is executed the answer is C plus plus always allocates memory while it is compiling execution comes much later. So, during the compilation time the compiler must know exactly what to allocate. So, it will allocate memory that you are defined here actual usage will start when the execution comes we will have this example. What happens when you give a floating point value for a integer location p let us say tell me what happens no not while you give input very funny things may happen if you make an assignment it will take an integer path exactly the same thing will happen here instead of a single location which is named p q r r here you have to consider the location on which you are operating which is the i th element. So, the point that he is making is that if I assign some wrong value to a location to an element of the array will the entire array be spoiled the answer lies in the fact that if I assign some illegal value to a name p q r s t etcetera not affected exactly the same fashion nothing else is affected please note that in actual operations each element of an array behaves exactly like a single location of a name is no different. The whole arrangement is only for convenience for you to address all individual elements in an iterative structure that is all, but otherwise operation is on each individual element. So, these are some of the important properties and these should be noted array is a collection of elements of the same type this is easy to imagine what will happen otherwise. Suppose I had an array of 100 elements a few floating point a few integers a few cal a few shorting there will be chaos because the compiler would not be easily able to calculate fifth element is rare it has a base address, but if every location is of different length it will not be possible to do so. Not only that all the arithmetic operational instructions generated for the elements of an array assume that all elements are of the same type that is why in the definition you say int roll 600 float marks 600 once you define that every element will be only of that type extremely important element not possible to have a mixed set of elements in an array later on when we see more abstracting like structures and arrays of structures we will see how to handle that. Next at any time only one element participates in an operation and it participates exactly like any other name does. So, like any other object you have PQR whatever are the rules for that same rules will apply to the elements of an array when they participate in the computation. Third an index expression is used to refer to an element. So, it is not necessary to say roll I marks k you can say roll k star 2 minus 3 star n in square brackets that expression will be considered as an index expression it will be evaluated like any other numerical expression value will be computed and its integer equivalent will be taken to be the reference. So, when we use a i 5 means fifth element 1 23 means 1 23rd element the array index starts with 0 this is sometimes creates a problem. So, important to remember that arrays in C plus plus start with 0 index. So, if I say j minus 5 what will it mean it will take the value of j as it exists at that point during the execution. Suppose j is 7 j minus 5 will be 2 and you will go to a 2 which is this element. Suppose j is 28 j minus 5 will be 23 it will go to 23rd element which you may not have used it may contain some garbage if you are trying to print it or use it that will happen. Suppose j is equal to 3000 then j minus 5 will evaluate to 2095 where is 2095 in this list somewhere beyond this the rule says the results are unpredictable, but can you guess what will happen these are the memory locations which compiler has assigned to an array after this there might be other arrays after this there may be a space for the translated instructions during execution time this fellow will blindly put something into array location 2000 whatever 935 assuming that the array continues it may overwrite some other variable values it may overwrite part of your program long time ago when the operating system was not strong enough and different users program were simply kept one after other to run it was not difficult for me to write a program assign something to an array element which does not exist and spoil your program completely because it will go and write in that area. Nowadays of course you have hard firewalls between the different programs and so on, but the results are unpredictable is an understatement and therefore it is vital that you pay attention to ensure that the index values that are actually used in the program are well within the range. Here is a possible program I define int n size then in my program somewhere I say c in this is a single program by the way is not separate somewhere I define c in greater greater n size then somewhere later I say float marks n size is that ok not ok ok unfortunately there are only two possibilities ok. How many of you think it is ok lot of people how many you think is not ok ok the last man tell me why it is not ok. So, this goes back to the question he says that while allocating the memory the compiler will not know the exact size and therefore it will not know how much memory to allocate goes back to the same question which was raised earlier. Please note that while the statements are executed in this order this followed by this etcetera the declaration this declaration as well as this declaration is handled by the compiler at the beginning no matter where you put your declarations in your program towards the end also if you say x y that allocation of memory to x and y has to be done by the compiler while compiling not later whereas c in greater greater n size is an input statement which will be executed during execution it is at that time you will give some value 200 300 whatever that value is not known to the compiler. So, that is the reason why we say always declare everything at the beginning so that you are also not confused even if you are c plus plus compiler will not know it will read the entire program take out all the declarations and first allocate memory during compiling now it allocates memory to n size here n size remember only memory has been allocated there is no value inside now it has to allocate memory to marks and here is a problem how many locations should it allocate does it know how many locations to allocate no it does not have the value reading of n size will happen only when you start executing therefore this is illegal this is not correct later on in the course we shall see the notion of dynamic allocation of memory dynamic allocation of memory means who is the master of memory in a computer operating system just as operating system manages your files on the desk it manages memory processor everything later on we will see how it is possible for our c plus plus program to go to the operating system request please give me some more memory because now I know that I need 500 more elements it is possible to do so in the extensions that are available as part of the abstractions and as part of the higher level facility but in basic programming principles an array cannot be defined to have a undefined size it has to have a defined size at the time of compiling is that clear index I have already said it can be any expression here are some examples so a j for j equal to 86 will mean 86 element a k minus m plus 52 this is treated exactly like any other arithmetic expression it is evaluated based on the current values of k and m suppose the values are as shown then the index value is how much k is 1200 m is 1240 k minus m is minus 40 plus 52 which is 12 so this is the 12th element 12th not in natural number count remember the first element is actually 0 a x by 5.0 is this an invalid statement the answer is no any arithmetic expression is permitted there as long as it results in a numerical value suppose x is 7.0 expression will evaluate to 1 because 7 by 5.0 is 1 point something converted to integer is 1 if x is 22.5 22.5 by 5 is 4 point something something when truncated to integer it will be everybody clear on how the index is calculated so value of index is always converted to integer and please note that the responsibility of ensuring that the final index value which is used anywhere in your program lies within the range 0 to size minus 1 if that does not happen results are unpredictable maximum number of chaotic situations occur in initial programming because of non adherence to this cross check you have to be very careful therefore with how the indexes are used yeah memory allocation is done only by declarative statements if somewhere you say x 50 equal to 58 is that a declarative statement no so compiler cannot and will not take cognizance of that his question was that if I have somewhere I have declared let us say int x 10 and then somewhere I am saying x 15 equal to say 12 what he is saying is what does compiler do unlike in the previous case x 50 is used not x n or x n size since I have said x 50 and since I have originally declared an area of only 10 elements will the compiler increase the storage that was the question the issue is that compiler does not allocate storage based on the executable statements in your program compiler allocate storage based only on your declarations it will not look at anything else your declaration says x 10 so your array will be allocated 0 1 2 etcetera etcetera 9 then you write x 50 equal to 12 the compiler will actually translate this to make some changes somewhere here some other location okay it will be wrong that is why I said that instead of asking whether the compiler will intelligently do this or do that to which the answer is absolutely no absolutely no therefore it is important to remember that the array sizes how to be declared in your declaration statements only one second whenever you use array indexes in your program it is your responsibility to ensure that the index value is always between 0 and size minus so for example your question look at 50 is this 50 between 0 and 9 no that means you are goofed up so do not ever blame the compiler as to why it was not intelligently enough itself however he has this point that tomorrow I require larger number of elements I did not know originally what was the size that is where I said that towards the end of this course we shall see the dynamic memory allocation facility we see plus plus but as the basic programming principles this stands here is a program to find maximum of n numbers this time instead of using a single location a I am using an array a with thousand elements I have of course max I do not know whether I will have exactly thousand numbers to handle so I have a variable n location n and an index I I read the value of n and then I read n values in the array elements observe how different it is from reading one value at a time that we did in the iteration I can now preserve each of the individual n values so the array x that I showed you 11 elements original elements if you have you could put them as a 1 a 0 a 1 a 2 a 3 a 4 a 10 by by this read state next to find out the maximum I said max is equal to a 0 what is a 0 first element 0th element we will call it 0th now onwards 0th element of the array now I set up an iteration which varies I from 1 to n minus 1 0th element is already done I want to compare that with first second third fourth n minus 1 0th element there are totally n l so this simple four iteration will do that for I equal to 1 I less than n I plus plus I do this if a I is greater than max max is equal to a I notice how beautifully the same logic that we had used earlier is available to us but as an array retraction I can have the existence of all individual values that I have read perpetually available with me in my program and the control structure of iteration can be used so effectively because there are no n different names to handle which I could not use in an iteration but there are n different index values to be handled which I can control by an expression so I say if a I greater than max max is equal to here we agree that this will find out the maximum is one more thing I would like to say suppose these are the values I will have here a location called max this is a what will be the value inside max max 20 I have got the value of maximum but I do not know whether this maximum value occurs in the 0th element 1st element 2nd element 3rd element 4th element I do not know that why is it important to know suppose these were marks out of 25 in an examination and there is another array called R which stands for roll number 1001, 1005, 1006, 1009, 10012 arbitrary roll numbers now if I find out the maximum I would also like to know whose score the maximum I would like to know at which position this maximum value is occurring I want to capture this number 2 somehow because 2 is the index where the maximum value I would like some location pause to actually hold this value 2 when max is 22 if max is something else I would like that pause to be like can you tell me how I can find out pause see this is the place where maximum is getting assigned a value if it all anything changes it will change here along with this if I were to say pause is equal to I that will be nice then I will have that value so this is the modified program notice what I am doing here for I equal to 1 to n this is that if state if I greater than max max is equal to here pause is equal to here any time maximum changes pause also changes so it does not matter where in my thousand values that maximum occurs the pause location will actually keep track of where it has occurred notice also that because I am beginning with a 0 it is quite possible that the maximum is the first element 0th element so I must also set pause to 0 otherwise I will find out maximum pause will remain modified so I can put an output statement maximum occurs at position pause is that clear at this stage we deviate slightly and I would like to tell you how there are two locations p and q p contains 12 q contains 371 I want to exchange or swap the values in these two locations I want to make p equal to q and I want to be equal to p if I write that what will happen p will become q which is 371 then q will become p which is now 371 so it will return 37 this is no good writing q equal to p followed by p equal to q is also no good in all such situations I need an additional location which I may call temp this temporary location is a holding place so whenever you want to exchange values between two locations you must use a temporary location and the logic to be used is first move one of the values to temp then you move the second value to this and then you move the temp value to this so your statement will be you understand this if I wrote this this will be wrong now what will happen when I said temp equal to p what will be the value in temp 12 when I say p is equal to q what will be the value in p 371 and when I say q is equal to temp what will be the value in q 12 so I kept a holding place like passing I pass this value there then take this value etc is this clear this this is the only way by if you want to swap values in locations why did I discuss this it has a bearing on what we want to do next remember the old algorithm we found out the maximum and we found out that the maximum occurred at 0 1 so the maximum would have been 22 and the pass would have been 2 we found out that second now I say that look I do not like the maximum to occur somewhere in between I want the maximum to occur at the top position now top position is already occupied by some number it cannot be overwritten so I want to exchange these two I want 22 to come at the top obviously 7 has to go somewhere so I will just exchange it because there is a place vacated by 22 this is the program to find out the maximum and exchange it with a 0 the entire program is same as you will notice if a i greater than max max is equal to a i pass is equal to r you are familiar with this it will find out maximum and position now this is the statement which arranges the swap temp is equal to a 0 a 0 is equal to a pass and a pass is equal to temp you agree that a pass and a 0 are actually the names of the two locations where I want to exchange the value and I can say maximum was found at this place exchange with 0 what is the reason why why we are doing this topic suppose we had six elements in an error 24 50 to 13 95 64 50 some arbitrary numbers when I execute that program max will be 95 this is that value and pass will be 3 because it is 0 1 2 3 when I exchange I will get this array as 95 50 to 13 24 64 50 agree 95 will come in the 0th position 54 will go into this position now suppose I say I got the maximum value and put it in 0th position now let me consider the remainder of the array except the 0th element 0th element is the maximum I repeat the process for remaining 5 elements which are the remaining 5 elements 52 13 24 64 50 if I did that what will happen these are the 5 elements this is what you will get if I do not consider the 0th element because in the remaining elements which is the maximum 64 so it will be exchanged with 52 which is the 0th element for this truncated array or the first element of the original array so if I consider the original array this will be the array now so notice in the first iteration I found maximum put it in the top place now I want to do a second iteration not starting with 0 but starting with 1 and do the same and exchange now the new maximum not with 0th element but with the first element if I repeat this process from now starting with element number 2 to end of array then 3 to end of array 4 to end of array what will I have I will have the sorted descending order this is the program which is called an exchange sort algorithm let us spend just 2 minutes in discussing this program please note that I am showing an artificial array somewhere half way through the execution of this program originally whatever I was doing was for an array from 0 to n minus 1 right I was doing it from 0 to n minus now I say that I look at a part of the array from k to n minus if I executed that algorithm assuming not 0th element but kth element to be the first then I will find out the maximum and swap it with the kth element this is the logic which is implemented in the inner iteration max is equal to a k pos is equal to k this is that statement and for i equal to k plus 1 to n minus 1 it will go through these comparisons what will it find 76 is the largest value and it will swap it with what not with a 0 but with a k agreed have you understood this that I am looking at a part of the array and exchanging the maximum element in that part with the top of that part now if I repeat this whole thing varying k from first 0 then 1 then 2 then obviously I will be first putting the maximum element in 0th position in the next iteration the next maximum in the first position in the next iteration the next maximum in the position etcetera and I will get this order is this understood not very clear so here is the homework for you go home take some array of 10 elements or not even 10 it will get complicated just 5 elements write some 5 values and hand execute this algorithm writing down i k n max and writing down how you get the array elements a 0 a 2 a 3 a 4 a 5 and see what happens when you execute this you will find that you will actually get a sorted area if there is any confusion refer to the previous slide we said this is what should happen first I get the maximum in one inner iteration then next time when you increase the value of I go back up again value of k you will you will get the second maximum and so on notice that this is a nested iteration for k equal to 0 this is one iteration inside it for i equal to k plus 1 how many times the inner iteration will be executed k plus 1 to n times every time and 1 to n times outside actually first time you will do this n minus 1 time next time n minus 2 times next time n minus 3 times n minus 1 plus n minus 2 plus n minus 3 etcetera plus 1 which is n into n minus 1 by basically it is order n square so if you are finding out maximum of 100 numbers roughly you will do an order of 10,000 comparisons if you are finding out if you are arranging thousand elements roughly you will do one lakh compare the million so it will increase by the order of square of the total number find top ten performance in an examination this algorithm is exactly same this program is exactly same as the earlier so will not waste much time on this except to say that I now have two arrays roll 1000 marks 1000 because I now have roll numbers also I will first do a validation whether the numbers given are within 0 to 999 etcetera I will read roll numbers and marks I am giving hint please submit data for each student in one line roll number one blank one or more blanks marks then press enter roll number one or more blank this is Godagiri that you will have to do while executing the program invariably there is a I have mentioned this this is a statement when you say return one the control will go back to the operating system the remainder of the program will not be executed which is the objective if the data input is not correct why do you want to execute the program you want to quit so that is what is implemented it is nothing to do with by mistake see what this is doing this is checking whether the value of n given is between 0 and 999 or not if it is not I cannot read the data suppose you give 1200 as a value I cannot read 1200 suppose you give minus 3 as a value I cannot read minus 3 numbers you have made a mistake I am telling you wrong I am going home that is all of course you will have to rerun the program you just have to say dot slash a dot out this time give a correct value you do not have to change the program program has been compiled when you run it at the run time you are giving the value of L if that value by mistake is wrong then always rerun the program but while running the program this is a pre caution let us get out but all these statements on this slide are actually validation statements and I have written them deliberately to indicate that such care has to be taken when you write programs particularly programs which handle large amount of data so you give these instructions you read roll number and marks in each individual elements there is a syntax here unfortunately Microsoft always converts stand-alone eyes to capitalize so is that clear what is happening and finally I output number and mark I have read them one by one but just as a verification I am outputting them who you see for yourself that it is and after that this is the program so what does this program do it does exactly what our earlier program did it shows the array in descending order but while sorting the array for roll it also rearranges for marks it also rearranges corresponding roll remember the sorting criteria is marks I want to find the maximum marks etc but whenever I exchange marks I should also exchange roll why because if some marks go from fifth position to zero position then the corresponding roll number must also go from fifth position to zero position and that number must come to the fifth position so whenever I exchange marks must exchange must come that is what is done when the algorithm with both inner iterations and outer iterations are completed all arrays are sorted on marks all arrays are sorted marks array sorted in descending order of marks and roll number array now contains corresponding roll number if I print the first ten roll numbers and mark I solve the problem can I make this algorithm more efficient if I have to find out the top ten observe what I said if there are n elements a thousand students okay then this will take a million iterations all or all a million iterations interval can I reduce that the problem says print the top ten students do I need to find out the entire array sorted or if the first ten max I find is good enough so the outer iteration need not run for n it can run only for two jeffin is one thousand n is one million then there is still no issue however such efficiency has to be very carefully chosen what if the tenth and eleventh person have same marks now these real life considerations do are not seen by the algorithm algorithm will do exactly what you have asked it to do first ten means it will print ten that's all if you want to handle something else you will have to do something else this part of the exercise remember I mentioned some programs may take longer some programs may take shorter depending upon n this programs may take longer or shorter it is important to understand that there is a notion of efficiency when I write programs can I take five minutes maybe I don't mind can it be done in five seconds I'll prefer that can I take five hours maybe I'll mind if it is a lab program but it's a basic major engineering design or a nuclear power plant design I don't mind if it takes not only four hours but four days can it take four years can it take forty years these computational efficiency issues become extremely important when I am handling a very large number of computing what I am doing is introducing you to a simple mechanism available in Ubuntu which permits you to measure the actual time it took so that you don't have to depend upon your watch this is done by a command called time so instead of saying dot slash a dot out if you say time blank dot slash a dot out this time is an operating system command just as p w d l s whatever what when you say time dot slash a dot out what the operating system does is ordinarily it will give control to your program and come back after completion this time it will set a timer it will go to your program the program will execute producing output whatever whatever but when it comes back operating system produces an additional message it gives you three timings one is called real the other is called user and the third is called sis the real time is roughly the clock time it takes for program to execute but that will also include the time you spent in typing in port waiting etcetera the user time is called the time spent by the program in running instructions written in the user program that is the time which determines how complex was your algorithm so in this case it says 0 minutes 2.006 of course the timings will vary depending upon the nature of your algorithm and that kind of values your hand but this is the mechanism that we have we will elaborate on this later I have spent only 5 minutes in describing the care data type we already seen this you remember the care type which we had defined a single character which was like an integer and we could define the single characters like this so this is all well known ASCII code is available but if we want to write strings and we do write strings where do we write strings in our C++ programs currently this is where we write strings right so you are familiar that some strings can be written but the moment we write some strings we have to know that C++ will be handling the string in some fashion how does C++ handle C++ handle strings by using an array of care type so look at this declaration care PQ later equal to C this is initialization in the declaration which is permitted just like an integer or float but notice care name 20 care name 20 when you say C++ arranges 20 locations 0 1 2 3 4 9 each one can hold a character and such arrays are used classically by C++ to hold a string consider this I want to put Vishwanathan and Anand as two names as two strings I declare two care arrays N1 and N2 each 100 elements I do not know how long a name will be now I can put each individual character in one element of the array so suppose I want to put Anand N2 0 N2 1 N2 3 etc etc can be assigned these characters you can see that it will contain on how do we know that the name has ended suppose it was not Anand but the name was Anand then we will put two more characters A and N but how is C++ know where your name has ended so for that C++ uses an old convention called a classical convention which says that whenever you are finished by stuffing meaningful characters you indicate the end of the string by putting an artificial value backslash 0 which is called a null value the actual is 0 because an actual 0 will never occur as a valid as key code in any name that you can think so this is the convention that means you can form strings within your program but it is your responsibility to ensure that after stuffing individual characters into array elements for care type you must put backslash 0 there you can also use it because you know that whenever C++ has an array of characters which is representing a string then you will find the backslash 0 at the end so how big is the string you can easily go through each character examine is it backslash 0 backslash 0 backslash 0 no or this is backslash how many things you have passed 7 so the length of the string is 7 you can use that backslash 0 for meaningful purpose how is input and output handled well if a name or a string does not contain a blank then the C in and C out statements permit the use of the array itself directly of course you can say n 1 i C out or C in and give individual characters remember when you give input the characters are not to be quoted in single quotation mark only the characters have to be written C in generally distinguishes values by a blank in between so if you give consecutive characters it will read consecutive character but if you if you give a blank it will omit blank but if you give n 2 then any non-blank character field string can be read so if I type anand C plus plus will assign characters to elements of n 2 and at the end it will automatically put a backslash here is a program to input a word and output the reverse give number of characters in the word and type a word no space in between C in name please note that when you say C in greater greater name any name can be written but the moment you place space for example suppose I type is my name unfortunately the first space will terminate the input because no space is permit so the name taken in will be only deeper this is the idiocy of C in it has nothing to do with array handling later on we shall see how we can read all characters including blanks comma whatever whatever there are functions in C plus but this is what it will do it will take a name type word is this now how the name has been read D is in 0th position A is in first position E is in first position second position P is in third position A is in fourth position K is in fifth position if I simply output individual characters in the reverse order I will get the name reverse but that is like finding out the max without noting the individual characters and forming in so if I want to actually do this first I want to find the length of a string I do not know how many characters are there how will I find out the length if the name is deeper V double E P A K the six characters 0 1 2 3 4 5 the sixth position C plus plus would put a backslash 0 so I hunt for backslash 0 wherever I find backslash 0 that is end of the matter so here is the program I enter a string I start with I equal to 0 while string 1 I not equal to backslash 0 I simply say I plus what I am doing I am actually moving from 0th character to first second third fourth moment I find I is backslash 0 which is null character I get out and that is the length here is another approach it uses another approach it gives an example of one name where the approach works it gives the example of another name where the approach does not work then you know what is the correction to be made and then this is the right one that you use incidentally this introduces a built in function called string length remember I told you C plus plus as a library there is a library called C string which I have included here there is something different C string has a rich set of from 25 functions which can do variety of things with your string so you do not have to do the code agree of writing your program but you can also write programs to do some more so read these programs ok thank you