 Welcome back. In the last segments, we have been talking about the manual program inspired by the manual algorithm and we have already discussed the main program and the RN tab, the table which gives us the translation from the name to the roll number. In this segment, we will talk about the RSM tab, the table which contains information about the marks obtained by student with a given roll number in a given subject. So, this is the second major entity in our program after the RN tab. So, this is an vector and it stores triples which consist of the roll number, subject and marks. The first function that this table or this entity or this struct is going to perform is that it will return the marks given the roll number and the subject. The implementation is going to happen in the member function lookup. So, I have used the same name lookup here should not be confused with the lookup in RN tab. Here the lookup is, we are looking up the marks given the roll number and the subject. Over there, we were looking up the roll number given the name. So, the logic of this is fairly straightforward. We are going to scan through the vector and we are going to look for entry in the vector where the roll number is in the entry is the same as the roll number in the command and the subject in the entry is also the same as the subject in the command. And if we have a match, then we just report the marks. The second function is returning the rank given a roll number. Now this is a little bit more complicated because we first need to calculate the rank. The rank is not immediately available to us. So, the implementation is done by a member function called find rank. So, the way we calculate the rank is we first calculate the average marks for all roll numbers. Again, what do I mean by this? So, a particular roll number or a student with a particular roll number will have taken several subjects potentially. So, we are going to look at the marks obtained by that student or by that roll number in all those subjects and we are going to take the average of all of those. So, that is the first step. So, for doing that for calculating the average, we have to first calculate the total number of marks. We have to find out how many subjects there are, how many subjects this particular student has taken and then we just divide the total marks by the number of subjects. We have to do this for all students because only then we will be able to compare students. So, we need an array to hold this. And our principle is that all significant entities must be structs. So, we will create another table. Let us call it RTNA tab where R denotes the roll number, T denotes the total marks, N is the number of subjects and A is the average. So, we are going to use the table to calculate the average marks for a given roll number and how does this happen? In step 1, we will calculate T and N. So, this is done by the member function insert. So, we will see this member function insert. So, it will go over RSM tab and it will look at triples of this kind and for every roll number subject mark triple, it will go to RTNA tab for that corresponding roll number and it will add the marks into this total. And we do not really need to know the name of the subject now. We just need to know how many subjects the student is taking. So, since we just added the marks for one subject into T, we will increment N the number of subjects by 1. So, this way we will keep accumulating the total marks and the total number of subjects taken by the students as we scan through RSM tab. Then in the second step, we are going to calculate the average for each roll number. So, we are going to update this A field. So, A is the average field, A is the average. So, that is what we are going to update and that update is simple. So, we already know the total marks obtained by the student from step 1 and we already know the number of subjects taken by the student. So, we just have to take the ratio T by N. So, T by N will be stored in A. And then we have this RTNA tab which contains the average marks for a given roll number and now we have to find the rank. How do we do this? Well, we look at our student and we will simply count how many other students there are whose average is higher and the average has already been calculated. So, for this we again we just have to scan through this RTNA tab once. So, we will see the details in a minute. So, now we are going to view this structure RSM tab and we will see how the details of how these functions are being performed. So, first we will look at this lookup and then we will look at find rank. So, we are going to look at this RSM tab. So, let us look at RSM tab. So, this is our structure RSM tab. As I said, it is performing two functions and the first function is the lookup function and the second function is the find rank function. The lookup function requires a roll number and a subject and the idea is actually fairly straightforward what you might guess. We are going to go through all the entries in this RSM tab. So, all the entries in the RSM tab. So, let us look at this structure RSM tab. So, what does it contain? It contains a vector of struct RSM and the vector we are going to call tab or table in this in this code. So, let us look at what this vector, the vector elements are. So, the vector elements are RSM and they are simply the triples. So, the file contain triples and this vector just contains those triples roll number, subject and marks. So, how is the file, how is this table initialized? Well, here is the constructor. So, the main program passes along the streams corresponding to this file and we are just going to read in the data into the table. So, as before we are going to read into a structure and then we are going to push back that structure onto our table. So, that takes care of how we initialize the table. Next, we are going to look it up and we are going to look at the function which does the lookup and this function is expecting a roll number to be passed and a subject name. So, basically this lookup function is going to tell us how many marks a student with a given roll number got in a given subject and the code is fairly straightforward as you might guess. We are going to go over all the entries of this table. If we find a matching entry that is if we find that the roll number in the entry matches the roll number supplied and the subject in the entry matches the subject supplied then we just return the corresponding marks. Now, these corresponding marks were stored as double and we are actually going to convert it to string and for that there is a utility function which is present in the string class and we just use that. Now, why are we not returning a double but why are we returning a string? Well, what if we do not find the roll number and the subject then we need to return something and the nicest thing to return would be a message not found. So, therefore, we have chosen the type of this lookup function to be string and so here we are returning the marks by first converting to the string and if the entry appropriate entry is not found then we will return the string not found. So, that is the lookup which is fairly easy. The more complicated thing is the rank is the find rank function find rank member function. So, here we are given the roll number and we want to find the rank of the student having this roll number. So, as we said earlier we need an additional local data structure called RTNA tab. So, this RTNA tab is going to contain all the data associated with all the students. So, it will contain that it is the structure that we are going to use to calculate the average and the way this works is we are going to insert, we are going to go over the file and we are going to insert the information about students and so roll numbers and marks. So, let us look at this RTNA tab. So, RTNA tab is as we said earlier a data structure itself can consisting of structures of a certain type. So, let us look at that. So, it contains structures of the type RTNA. So, it contains a roll number, a total number of subjects and average. So, how does it get built up? Well initially it starts of empty and now we have provided an insert member function. So, what does the insert member function do? It takes a roll number and the marks obtained by the subject, by the student and it says and usually we will just call it with the marks obtained in one subject. But occasionally we want to call it without marks obtained in any subject. So, we just want to say that look this roll number exists and do something about it. So, make an entry corresponding to that roll number. So, that is why we are going to have two arguments over here and the argument is going to be a double with marks equal to 0 and the number of subjects equal to 0. We finished discussing this look up function. Now we are going to discuss the find rank member function. So, let me remind you the find rank member function is going to figure out the rank of the given roll number. So, for that it has to calculate the average marks obtained by all students and then see where this roll number is in that list of marks. So, to do that we will need a local data structure called RTNA tab. So, let us first look at this RTNA tab and then we will see how we are going to use it. So, the struct RTNA tab contains a vector of structs RTNA. So, we have discussed this earlier RTNA is simply a quadruple. So, it contains roll number, total number of subjects seen so far and the average. So, the purpose of this is to find out for each roll number what is the total number of marks, how many subjects there are and then once we have built up these two fields we can just calculate the average by dividing this by this. So, here are the constructors for it. So, there are two constructors. So, we can create either a dummy. So, this is the default constructor and this will construct an RTNA for a given roll number. So, the roll number field the roll number member will be set to the given roll number but at this point we are just starting off this record in our structure that we are going to keep and therefore the total and the number of subjects will all be set to 0. So, let us come to this RTNA tab. So, we started off with an start off with an empty RTNA tab and then we have two insertions possible into it. So, this insertion just inserts a roll number. So, if you are just inserting a roll number this is what we need to do before we insert any marks. So, at that point we are going to push back the roll number and the empty data structure onto that roll number. So, basically RTNA VEC will contain just the roll number and other fields will be 0 and then there is the other insert function. So, in this case we are going to insert the marks for the roll number. So, how does that work? Well, we are going to go through this table this RTNA VEC and in that RTNA VEC we are going to check whether the roll number appears. So, if the roll number appears then we want to know at what position it appears. So, we have discovered this I. So, we break over here and if we do not break if we go to the end then I will be equal to RTNA VEC size. So, if it is RTNA VEC size as we check over here then we know that this roll number is not present in the vector. So, we insert it into the vector. So, for this we push back we push back this RTNA of roll number. So, this is exactly what we did over here. And now we have I pointing to the index where that roll number appears in RTNA VEC. Either it appeared it was already there or it appeared because we pushed it at the last position. So, once we know that we simply have to add the marks to the total which is being accumulated there and we have to increment n subjects. The number of subjects have to be incremented. So, that finishes this function of insertion. So, let us see how this function gets used in our find rank function. So, here is the find rank function. So, what are we going to do? Well, at position 0 or at the very beginning we make an entry for the roll number that whose rank we want to determine because we are going to quickly need the average marks for this roll number. And so, let us just place it at position 0. So, I have written it first. So, I really mean 0 because we are counting from 0 over here. So, we inserted it at roll number 0. Then we went through our table, our RSM table, tab refers to that RSM table and we took the roll numbers from it. And so, we looked at the size of our RSM table. So, remember that all this is in the context of our RSM tab. So, let me just position it properly so that you can see what this tab refers to. So, this tab is referring to this RSM tab. So, if you remember RSM tab consists of roll numbers, subjects, subject code and marks. So, we are going to go through all the entries. So, tab.roll number is the roll number of the ith entry. We are not concerned with the subject name, but we are concerned with the marks. So, the ith entry tells us that roll number, tabi of roll number has got tabi dot marks in some subject. And since we want to know what is the total number of marks that this roll number has got, we are going to insert this into RTN eta. So, this way we end up inserting all the marks got by all the roll numbers into this RTNA tab. And RTNA tab at this point will include for every roll number, what is the total number of marks and what is the total number of subjects taken by that roll number. So, we are just about ready to calculate the average. And what we do is, we will do it directly, we will just call RTNA tab of get rank of 0. So, let us go back and see that. So, get rank of 0 is going to start off with that roll number. So, that roll, well, we have not said what the roll number is, but it is a roll number in the 0th position. Now, in the 0th position, did we insert any subjects that we have to check? Well, we may or may not have. So, we are going to have to check. So, if the n subjects is 0, then in going through this, that entire RSM tab, we did not find the given roll number. So, we are immediately going to return not found. Otherwise, first we are going to calculate the average. So, here is the average being calculated. So, for all the elements in the table, we are just going to set RTNAVIC of i dot average equals the total divided by the number of subjects. So, at this point, when we come to this point of the code, RTNAVIC contains the average marks obtained by every student. And now, all that remains is to compare the marks of the 0th entry, the entry, the roll number that we are interested in, which we cleverly stored in the 0th position. So, we are going to get that entry and we are going to compare the marks of that entry with all other entries. So, we are going to go over the entire table starting at i equal to 1. And wherever we find that the marks of some other roll number are larger than the marks of our roll number, then we are going to increment rank by 1. And we started off with rank equal to 1, so that at the end of it, whenever we find higher number of people, our rank will be, higher average marks, our rank will be incremented. So, eventually we will get the appropriate rank. And again, we are going to convert it to string and return it back because we potentially also want to return not found over here. So, that completes the description of this RSM tab and internally we, it used this RTNA tab as well. So, let us return back to the presentation. So, there are a number of points to note in what we have seen. In some sense, the algorithm or the logic is quite simple or routine. I mean, there are lots of things to do, but what we are doing is not particularly clever. We are calculating averages, we are finding out how many averages are bigger than the average of the roll number of our interest. So, a lot of things to be done, but the logic itself is fairly straightforward. The key issue is how do we organize this so that the code looks easy, so easy to understand. So, the issues as always are keep the main program at a high level, details should go into functions and member functions. And we have to make structures for major entities. So, the major entity from the point of view of the main program was RN tab and also RSM tab, but while processing RSM tab, while calculating the average marks, we had to create another entity called RTNA tab. So, this was, for this again, we made a structure. And we have to try and give good names for variables. And at the end of it, I have to say that this is an art and not a science. And you may not like every step that I have taken. You may say that no, I think I can write this step better. And I will invite you to do that. And this is an art in the sense that what you like, I may not like. But the point is, you still have to appreciate these points that you have to appreciate that code has to be understandable. And at least according to your sense of what is easy or what is easy to understand or what is elegant, you should be developing the code and you should be developing this sense of what is easy and what is easy to understand. And it is of course possible that your sense of what is easy might be better than my sense of what is easy. And I will definitely invite you to improve the code that has been given. And make it more easy to understand and that is certainly an ongoing process. So, what have we discussed in all this? We have discussed a simple somewhat naive solution. It is simple and naive in the sense that it sort of repeats computation. And we have seen how the code for this can be organized. Next, we are going to see somewhat more sophisticated solutions. So, we will take a break.