 Welcome back, in the previous segment we saw how to declare arrays with and without initialization and how to access the elements, how to use them in computation, how to read and read values into it and print the values. Now we are going to see some larger examples of using arrays. So we are going to start with a relatively simple, familiar sounding problem. So the problem is you are supposed to write a program that reads in marks of 100 students in a class and the marks are given in the order of the roll number. And let us say in this class the roll numbers happen to go from 0 to 999. So yes I know that roll numbers usually do not start at 0 but just for fun let us say they do start over 0, over 0. So in this class there are 100 students and their roll numbers go from 0 through 99. So that is the first part of what the program is supposed to do. After that students may arrive in any order and type their roll number using the keyboard. The program must reply to that by printing out their marks. So maybe first the student with the roll number 43 comes then the program is supposed to print out what marks student number 43 got. Then there will be 67, so marks for 67 may be printed. Then there may be 6, then the marks for the student with roll number 6 should be printed and so on. Now if at any time an illegal number is given then the program must terminate. So this is the specification, this is what our program is expected to achieve. So what does the program look like? So we clearly should have an array in which we should be storing the marks and so let us say we have an array of doubles and of 100 elements. We need 100 elements because we have 100 students. Now the first step is to read in all the values, all the marks that the students have received and we were told that the marks will be typed in this order. So first the marks were 0, then the marks were 1, then the marks were 2. So it is natural to write this for loop where in the i-thetration we are reading in the marks obtained by student i and placing that in marks of i and then there is the loop in which we are going to wait for students to come and type the roll numbers. So for this let us say we have a variable called roll number and we will print out a message saying roll number. So this will be a queue to the students to type their roll numbers. So then we will read in whatever the students are typing and now if the students have typed a valid roll number then we should print out the corresponding marks. So if roll number less than 0 or roll number greater than 99 that is an invalid number. So in that case we are going to stop execution. So we are going to break out of this loop. Otherwise we know that the roll number that was typed in lies between 0 and 99. So in fact it is a valid index as far as the array is concerned. So we are just going to print out marks of roll number and that is the end of the loop. So if you break out of it the program will terminate and that would be the end. Now let us do a slightly more interesting version of this where we are going to read in everything but we are going to print out the roll numbers of those students who got the highest marks. So here is the program. So let us say that the marks array was defined as before and we have read in the marks exactly as before. So now we want to figure out first what are the highest marks. So for this we are going to have a variable called max so far. So max so far we are going to initialize to 0. And the way to interpret this is that at the beginning of the ith iteration of this loop we are going to make sure that max so far holds the maximum of the marks between 0 and i or 0 and i minus 1. So indeed before entering the loop max so far is the value of mark 0. So it is indeed trivially the maximum amongst all say mark 0 all the way till marks of i minus 1 which is 0 itself. So it is in fact, so there is only one element in this set marks of 0 through i minus 1 because both of these will be 0 and indeed mark so far is that value. But this plan says that not only should this be true at the beginning of the ith of the first iteration when i is 1 but it should be true for all iterations. So if it is true for all iterations then our body of the loop should make it be so. So what do we need to do? So when we enter this loop we know that max so far is the maximum amongst 0 through i minus 1. So if the next number marks of i is bigger than this then our marks our max should change otherwise our max should be as before. So all that we need to do is to write max so far is the max of max so far whatever we have seen so far and whatever we see next the next marks value. So this max is a statement is a function which is already defined in C++ and in fact we know that C++ functions that C++ defines for math purposes are in this include in this header file called CMath. So indeed max comes from that header file CMath that is it. So what would be true at the end of it? So on the 99th iteration what would be true? So on the 99th iteration what would be true? So i equals 1 and i equals 99 so at that point max so far should hold the maximum of everything from 0 through 99 and indeed that means max so far will have value of the maximum possible marks. So at this point we have identified what the maximum marks so far are. So max so far now holds max value in the entire array. Now we just have to check which are the marks or which are the roll numbers really who have got that many marks. So we again go over all the elements this time even including 0 and we are going to check are the marks of i equal to max so far if so then we should print out i. So that is it. So there are two ideas which have been used in this which are worth noting. So in this first part we are doing something which might be called accumulation. So max so far is accumulating the maximum value it is keeping track of the maximum value and this is a very standard EDM. Here we are keeping track of the maximum value but you may keep track say of the sum of all the values. Again it is going to be something like that instead of max of max so far and max i it might be something like plus of something and something and marks i. So that is one EDM. And then in the second part we are going over all the elements again but we are printing out only some subset though that subset which satisfy this condition and in other words we are filtering out things and therefore this EDM is often called a filtering EDM. So let us see a demo of this and this is going to be in a file highest dot cpp which I have created already but I am going to show it to you. So let me get to that file so highest dot cpp is this. So you can see that we have used marks 100 areas before and then we are doing c in marks all of this and this is our max array. Now this is the program that you have seen. Now I am going to make a change to it because I do not want to type all the 100 numbers. So I am going to change it so that I will only use 10 numbers. So I should really change all the 100s to 10s. So let us see, so let us compile this and let us run it. Now when I am going to run this I am going to do something slightly unusual, something that you may not have seen so far, you have not seen so far in this course. So normally I am expecting, so when I run this program c++ expects me to type the values from the keyboard but if I have to test a program often then typing the values especially 10 values even is a bit cumbersome. So what I am going to do instead is that I have already typed in those values in this file highest dot tat. So maybe I should show you that first. So if I do this highest dot tat it will show you that I have already typed in these values. So these are my 10 marks. So marks for student 0, roll number 0 or 65 for student roll number 1, 42, 278 and so on. So these numbers I have already typed. Now if I do dot slash a dot out and I say less than highest dot tat then instead of taking input from the keyboard this program will take the input from that file. So let us see that. So it took the input from the file and then it has printed those roll numbers for which the marks were the highest possible. So let us check that. So if we look at this which were the highest marks? Well 91 is the highest mark and where did this appear? So this is roll number 0, this is roll number 1, 2, 3. So indeed roll number 3 got 91, 4, 5, so indeed 5 got 91, 6, 7, 8, 9. So roll number 9 also got 91. So it has indeed printed out things correctly. So here we saw that our program, this program was executed but we changed the number of students so that only 10 students were used and here the input data, sorry the data was taken from the file rather than by, rather than through the keyboard because we wrote less than highest dot tat. So a small exercise for you, usually we will have roll numbers starting at 1 rather than 0. To handle this a simple strategy is to store the marks of roll number i in marks of i minus 1. So we have written 2 programs so far. So this exercise says modify both the programs so that they follow this convention. So they follow the more usual convention that student roll number start at 1 and not at 0. So what have we discussed in this segment? Well we saw a couple of simple programs involving arrays and an important point to note is that although roll numbers are involved, we did not actually store the roll number anywhere because the index played the role of the roll numbers. So because we stored the marks of roll number 0 first then the marks of roll number 1, the marks of roll number 2, we sort of implicitly made a correspondence between the roll number and the mark. And then the program involved a few important idioms. So going through all the elements of an array and filtering out the elements to print and also the accumulating the maximum of all the elements in a variable. And we will continue with more examples but we will take a quick break.