 Welcome back in the last segment we talked about histogram computation and that showed off how the index which decides which element we are accessing can be computed in a somewhat clever manner. Now we are going to take another variation on the marks display problem. So in this variation the roll numbers are not consecutive in the range 1 through 100 or 0 through 99 for that matter but the roll numbers occupy a larger range. So maybe the roll numbers are whatever 10 digits or whatever many digits that you have in your institution and of course in many institutions the roll numbers can contain alphabets even. But let us keep things simple for now let us say that the roll numbers only contain digits and furthermore let us say they are at most 9 digits long. So they are actually numbers and in fact numbers which can fit in an int variable. So in this case what is the marks display problem? Well the teacher now does not have to just type in the marks but the teacher also has to type in the roll number because there are no there is no implicit roll number for which the ith mark is being typed. So the teacher has to type 100 pairs of numbers roll number followed by mark roll number followed by mark the teacher has to do this 100 times let us say that the number of students in the class still remains 100 and later on the idea is as before students arrive and each student types in the roll number and the program must print out marks if R is a valid number. If R is minus 1 then the program must stop. So what is the program idea? Well we should use an array for storing marks but we now need an array for storing roll numbers as well and our idea will be that the ith roll number that the teacher enters will be stored in element roll number i which is our array for storing roll numbers and ith marks will be entered into marks of i. So the data corresponding to student i will be present in roll number i and marks of i. So when the student arrives we have to examine each element of the roll number array and see if it equals R. If so we should print the corresponding marks otherwise maybe we should print out some message. So here is the program it is a reasonably simple program so let us try to just write it. So first we will declare the two arrays that we want and then here is the code that reads in the roll number and the mark. So earlier we were just reading the marks now we are also reading the roll number and the roll number is going to be typed in first which is sort of the natural thing to do. Next this is the loop where the program waits for the students to type in their roll number. So this time we have dispensed with the message but I guess we could have a message and we are just reading in the roll number into a variable called R and we said that if R is minus 1 then we should exit. So that is what this is going to do. Next we now are going to do something a little bit more elaborate we are going to check whether R is present in the array roll number. If it is present then we should print out the marks corresponding to the index in which it is present but if it is not present then we would like to print out a message. So this found variable will come in handy for that purpose. So here is the step where we go through all the entire roll number array to see whether the roll number R that the user typed in is present in that array. So if roll number of i equals R then we can print out the corresponding marks and we will set found to be true. So this is going to say that look I did find a roll number and because I have found a roll number I do not need to do any of the future iterations so I can break over here. So that is really the end of that for statement as well. So now observe what happens so if the program if the control arrives at this statement how could the control have arrived over here? It could have arrived either because it came from this break statement so from this break statement. If it came from this break statement it is because the program found a roll number and printed it out in which case it has also sent a set found to true and therefore this condition will not be true and therefore this message will not get printed and the program will just exit from here as it should. On the other hand suppose it went through all these iterations it went through the entire array roll number but did not find the find a roll number R anywhere in that array. So then that means this condition would never be true and therefore a found would never become true. So it would come to this point and found would still be false but if found is false then not found will be true and so now the program will print a message saying the roll number that you typed in is not found. So that is sort of a polite message to say rather than tell the user nothing. So that is what this program is doing it is either printing out the marks or it is printing out that the roll number that you typed is incorrect. So that is the program. So let us see a demo this program is in a file called generalrollnumbers.cpp. Now this program is pretty much the same as we had earlier except for one change. So what is that change? Earlier I said that the roll numbers would be typed in by the teacher from the keyboard and the students would also type in their roll numbers from the keyboard. I am going to make a slight change. Let us say that the teacher has earlier typed in all the roll numbers and the marks into this file called generalrollnumbers.dat. So let me show you that file. So this is a file that you can create however you wish by using an appropriate text editor. So this contains some roll numbers and the corresponding marks and in this case there are only 10 roll numbers and 10 marks. So this program is going to read the roll numbers and marks information not from the keyboard but from this file and reading from a file in C++ is very simple. So first of all let me take you to the reading statement. So if you remember in this loop you are reading the marks and you had C in over here. So that was saying reading from the keyboard. Now you have something here called marks file. What is this marks file? Well it has been defined over here. So marks file is the name that you are going to use inside the program and it is going to be something called a stream. It is going to be something called an input stream. So inside the program C++ likes to think of files as streams of information. Because somehow things come to you flowing and those are input streams. So in fact this is a special kind of an input stream. It is an input file stream. So you are declaring something called a marks file which for your program is going to be an input file stream and which is going to be for the external purposes it is going to be this file. So this statement sets up the connection between marks file which is appearing over here and the file that you have on your system called general roll numbers dot that. So instead because marks file appears over here what you already typed into that file will be read in and that will be taken into roll number marks of I and so on. So basically in the first iteration the first word in that file will be placed into roll number I and the second word in that file will be placed into marks of I. So let me show you the file again. So because of that loop the first time around this would be become roll number 0 then this would become marks 0. This would become in the next iteration this would be placed in mark in roll number 1 and this would be placed in marks 1 and so on. So reading from a file is a useful idea especially in situations like this where you have 2 roles you have a teacher role and a student role and you can put the marks of one role into a file because that often is very convenient. And I should point you to this header file that is included f stream. So you need to include this file f stream if you want to use this name I stream. So if you want to have files if you want to have input files in your stream you should have this header f stream. S stream also allows you to use output files and we will use output files at some point whenever we need them. But I am not going to worry about files as such this is something just incidental and you can see that it is actually very simple and from now on you can start using files in this manner if you wish and they are also discussed in the book by the way not in this chapter but in a later chapter. So the rest of the code as I said is as before I have put in a variable or a constant rather a name called nstud for number of students and I have used 10 I do not want to have too many numbers to type in and anyway the rest of the code is exactly like this. So that is about it. So let us see. So let us compile this and let us run it. So now what has happened is that the file that I showed you has been read in and now I really should type in some roll number and see whether my program is printing out exactly that roll number. Well I have forgotten the roll number so what I am going to do is I am going to print type in minus 1. If you remember minus 1 says stop everything and quit the program so it did. So just to remind myself what was in that file let me type out that file. So these were the numbers in that file. So now if I execute dot slash a dot out the program will expect me to type in some roll number which is present over here if so it will print the marks otherwise if I print in a non-existent roll number it will say that the roll number is not valid. So let us see. So suppose I type 1, 9, 0, 3, 7, 0, 0, 5, 1 let us see. So this is present over here so I expect the program to type 91 so indeed it is doing that. But let us say I type in a roll number like 1, 9, 0, 3, 8, 0, 0, 5, 1 and this roll number is not present. So it does say roll number not found so I could keep going in this manner but let me just stop and I can do this by typing a minus 1. So let us get back to the presentation. So a small exercise for you please modify the program so that there are marks for two subjects. So when the user when the user types in a roll number let us say you should print the marks for both one after another. So what have we discussed in this segment? We have said that if the roll numbers are not in the range 0 through n minus 1 where n is the number of students then we need to explicitly store the roll numbers. So for that we need an additional array and if we want to know the marks of a certain roll number then we have to search through that additional array. So here we are storing roll numbers and marks and usually we are going to be given the roll number and we might want to print out the marks. So in such cases the roll number is sort of called the key to this data that we are storing. And indeed this whole EDM that you are given a key and you go through your data and you see if that key is present. So if the key is the roll number you check the roll number array to see if the given key is present over there and if so print out the other parts of the data. So that EDM is a very common EDM. Then we also use this found variable the bool found variable just to see just to record whether we have found the element that we wanted because we were going to the end of the program either after finding that element or without finding that element. And when we go to that last statement we would like to know did we come here did we actually find that element or did we not find it and so if we have an element bool it records what we did earlier and that will help us decide at the last statement whether we should print out a message saying you typed in the roll type in a invalid roll number which is what we should do if that roll number was not found. So this bool variable found tells us exactly this kind of information. Next we are going to look at polynomial multiplication which happens to use arrays in a rather nice manner but before that we will take a small break.