 Hello and welcome back. In the previous segment we saw a few simple programs involving arrays and we discussed some idioms such as filtering and accumulating and we are going to do more examples. So this example that we are going to do is sort of a rather interesting examples. It shows some clever ways in which the array index can be used. So what is the problem? The problem is that we are supposed to read in marks as before but we should print how many students scored between 0 and 9, print how many students scored between 10 and 19, how many between 20 and 29 and so on. This is often done by statisticians to get a general sense of how well the people have scored. Rather than looking at every mark which is lots of numbers to look at they break it up into ranges and see how many marks fall into the different ranges. So in this case now our output is going to be 11 numbers. So one number for the range 0 through 99, another number for the range 10 through 19, 20 through 19 all the way to 90 through 19 and also for the range starting at 100. Of course there is no range but since a person can get 100 marks we need to have a way to say that some person has got 100. So there are 11 buckets so to say including the last rather small bucket or the last bucket which may think of it, think of it as being between 100 and 109 but of course nobody is going to get more marks than 100 let us say. So we have 11 possible numbers to output or 11 buckets so to say into which the marks are supposed to be put. So we declare an array called hist of length 11 and hist of i is going to store number of students getting marks between 10 i and 10 i plus 1 minus 1. So if I use i equals 0 then this becomes 0 and 10 minus 1 and 9 so that is indeed the first range. If I make this say i equals 5 then it becomes 50 and 60 minus 1 or 59. So those are exactly the ranges that we want and those are the buckets that we want as they are often called. So what should we be doing? So if I read a certain mark from the keyboard the user is going to type the marks from the keyboard maybe the teacher is going to or somebody is going to then we have to decide which of these ranges it belongs to and we are going to add 1 to hist of that i. So that is what we are supposed to do. So given a mark v which of these elements should be increment. So let us take an example suppose I am given the mark 58 so what should I do? So I should increment the bucket corresponding to the range 50 through 59 which is the bucket with index 5. So we have chosen these indices conveniently. So in fact the most significant digit of your mark or rather the digit the number obtained by dropping the least significant digit gives you the index of the element of hist that you are supposed to increment. So for example if I get 23 then I should increment hist of 2. If I get 47 I should increment hist of 4 because hist of 4 and this 47 is going to lie in the range 40 through 49 and that is and how many marks lie in this range is exactly what is supposed to be stored in hist of 4. So if I get a new mark in this range then this number should go up. So how do we do this? Given a number in the range 40 to 49 how do we get a 4 out of it? Well that is very easy. So we just do integer division. So we assume that we convert V to an integer if it is not already an integer and we divide by 10 and we know that if we divide an integer by another integer we get the truncated division. So if we indeed divide say 47 by 10 then we will get 4 because the remainder 7 will be thrown out. So that is all there is to this. So our program is that we start with so we have 11 buckets first of all so we are going to make all those buckets PCO. So at this point we have not read any marks and therefore the ranges all the ranges containing contain 0. So no mark has been read. Now here we are going to read one mark after another and when we read a mark so we are supposed to figure out which bucket we should increment and increment it. So as we said we should divide the marks by 10 but however notice that our marks are double. So of course our marks could be 47.5 for all you know. So if I write int of marks then I get first of all I get the integer part and then I get the integer division. So I get now by doing the integer division I get to know which element of his I have to increment and so I just go ahead and increment that element that is all there is to it. So after this I can print out the values. So let us see a quick demo. So this file I have made a little bit different from what you have seen. So first of all instead of putting in numbers such as 10 and 11 which are confusing to the reader it is much more customary to give these numbers names. So again instead of using 100 students we are going to use 10 students because I mean you do not really want to worry about 100 students in a simple example. So n students is going to represent how many students there are and therefore in the rest of the code if you look at n students will know that this number is actually representing the number of students. Then also there are n buckets. So n buckets is a variable which is going to be 11 because we wanted 11 buckets. So we declare an array with 11 buckets and this is the plan that we already had. So nothing has really changed. So then we set all the histograms to be initially 0, all the buckets to have 0 elements initially and then we are going to read in all the marks and this time instead of saying 100 over here or 10 over here or whatever it is we are going to say n students because we just want to alert the reader that look this is being done once for every student. Then we read in the marks and then we divide it by 10 but only after converting it to an integer. So I could have this 10 I could have written as what is the width of a bucket and all of that. I really should do it but I am being a little bit sloppy here. And then finally I am going to print out the contents of all the buckets. So I am going to print a message saying in the range i times 10 to i times 10 plus 9 there are so many, so many marks. So that is what it is. So let us do this, let us run it. So let us first compile it. So s plus plus hist and then I am going to use the same set of numbers as I had earlier. So let me show you the numbers, so let me do this more as dot that. So these are the numbers and for these numbers this was the histogram. So let us read that, let us check if it is correct. So the histogram that we computed says that there are no numbers in the range 0 through 9. Indeed there are no such numbers. Nothing in fact all the way till 40 to 49 and there are 2 in the range 40 to 49, is that true? Let us see there is 42 and yes there is only 44. So there are 2 here, nothing in the range 50 through 59, 2 in the range 60 through 69. So there is 65 and 66, yes that is correct, 70 to 79 there is 1, so there is 78, 80 through 89 there is 88 and 83 and 100 to 100 and sorry 90 to 99 there are 3, 91, 91 and 91 and nobody gets marks in the range 100 to 109, so it has indeed done the right thing. So what have we discussed? So we have discussed this idea of histogram computation which I said is an important statistical operation. So by looking at the histogram you can get a rough sense of how people have done. So for example in our histogram there were no students who were getting marks in the range 0 through 10, 10 through 19, 20 through 29 and all those heroes said that look most students have sort of got good marks. So you can get sort of a rough estimate, so that is what statisticians often like to do and the key ideas are that the index for accessing the array element is calculated in an unusual manner and integer division plays an important role in this. Next we are going to go along with this theme of marks and displaying marks and we are going to look at another variation of that problem. So we will take a quick break.