 Good morning and welcome back after the break. So what you have here is a slide depicting most often used library functions. As I mentioned these are all contained in stdio.h. So you have for example f open to open a file f close to close a file. Here is an example if you want to open a text file for reading you can simply say fp is equal to f open my file dot text comma r by the way this returns a pointer but if for some reason the file cannot be opened for example you have wrongly typed the name of the file or the file does not exist on the disk then you will get a null pointer and that is what you will have to test whether you got an error or the file was opened properly. As we saw yesterday scanf and printf are the commands or the functions that we use for input and output of data. These automatically work with stdin or standard input but if you want to read from a file you use the variants called f scanf and f printf. Incidentally C also permits a character string to be used as if it was a file after all character string is also a stream of file terminated by a null character. So just as you can read input from a stream of bytes in a file you can also read input from a string. Similarly instead of writing bytes in a formatted fashion onto an output file or on std out you can also write it to a string. So you consequently have scanf and sprintf as additional commands. What is also important to note is that you have individual character reading and character writing capabilities. So you can say getC or putC that is get character input character for which again you have variations as f getC or s getC and so on. Similarly you have getString or putString. Incidentally a very rich resource a very good resource for describing all the stdio functions is in an online C++ tutorial. You might say why am I referring to C++ the point is that while the tutorial is on C++ this particular reference is to a C library it is called Cstdio in C++. Additionally for direct access files or random access files you have a command called seek. So you can seek this is the way to set the file pointer to a specific position you can define the position and that position is defined using an offset. So when you say f seek position pointer pass is actually taken to point to the particular location given by the offset with respect to some point that with respect to some point can be starting point end point or the current point and that is why you have a third parameter here called vents. So vents could be set there are three constants specifically name constants defined in a stdio which correspond to starting position existing current position or end position and with respect to that you can define an offset. So consequently the positioning pointer pass can be set anywhere in the file that you wish for reading or writing operation. How do you read from a binary file? Well the binary file is always read at a position indicated by current position pointer. We have seen through seek command how the position pointer can be positioned. There are other commands also available but most frequently use commands for positioning the position pointer is seek and for reading and writing is f read and f write. So f read for example has the following parameters pointer, size, count and stream. The pointer is to a block of memory with a minimum size of size into count bytes. What the size represent? Size represents size in bytes of each element to be read and count represents the number of elements each one with the size of size bytes. Consider for example you want to read an equivalent of 10 consecutive structures which are stored in a file and each structure is 60 bytes long. Then the size will be 60 and you want to read 5 structures so count will be 5 and 60 into 5 that is 300 bytes will be read by this f read operation. In fact it is possible to read an entire file if you know exactly how many bytes are there in a file in just one shot. Just say f read once and all blocks are read provided you of course have memory to store all the blocks. Since somebody has asked me to demonstrate the use of images and in file read write and tomorrow I am going to describe and demonstrate the use of fingerprints as an application a fingerprint typically would be read in this entirety. So although it will be stored in a separate file you would not read it byte by byte because you do not want to interpret it byte by byte. Image does not have any significance in terms of individual bytes. Collectively the bytes represent a digitized image. So you tend to read the entire image in one shot by giving an appropriate size and count. Stream of course a pointer to the file object that specifies an input stream. So you will use here for example fp if fp is your file pointer and so sorry stream you will use fp as a file pointer if you want to input from a file fp. This ptr of course is a pointer to block of data. We shall see the use of these commands in an example that I have constructed. In exactly similar way you can write to a binary file through f write command. So in f write command the meaning of all these parameters is exactly the same. Here is an example for example 5 star p file care buffer equal to x y z. So it is a character array of three elements it is initialized which is permitted in C and therefore the size of this buffer is 3 bytes 1 2 and 3. Now if I want to write all these three bytes in a file I can say p file equal to f open my file dot bin comma w b. Notice that the file is open for writing and it is specified as a binary file. Dot bin extension is my choice. I can give any extension by the way I can cheat everybody by saying my file dot txt to imply that it is a text file. By writing any arbitrary extension the file nature does not change the nature depends upon what is being written there. So here it is a binary file and when I use f write command in from the buffer which is the pointer here mentioned as the first parameter data has to be written the buffer is exactly equal to 3 bytes. So I want to write all the 3 bytes although I could have written smaller number if I wanted to size of buffer defines the size that I need to write count defines how many such sizes I want to write. So there is one unit of buffer so I say size of buffer 1 p file and this write command will write the characters x y and z as 3 bytes 1 to the file. I have put a comment here to say f write buffer comma 1 comma size of buffer comma p file this will also work correctly because size of buffer into 1 is still same as 1 into size of buffer. However the correct sequence is the one which I have mentioned this commented f write command is found in the example given on the online resource I do not agree really with the semantics although it will work correctly just as I can f open a file I can f close a file which is much simpler just as I can have an error while opening a file it is possible that I have an error while closing a file you may wonder why exactly such an error could occur I will leave it for the time being but just as f open returns a pointer similarly f close also returns a pointer and it is possible f close also returns a value and it is possible to test that value and check whether the file is properly closed or not. There are some peculiarities of binary file as opposed to the text files the data which is stored in binary files cannot be seen through our editors or word pads or not word pads that is normal text handling mechanisms. For example fingerprint files digital photographs or audio video files even structured records of useful information may be stored in internal binary formula file consider the structured record that we just described for an employee some time ago. Notice that while we had character strings describing the employee name and so on we had a salary as float we had a spin code as int these are internal representations ordinarily I will output it in a text format in a formatted output on a file or I will read from a text file into my memory. However if I want to store large pieces of information for large number of employees or for that matter for students or for that matter participants of a course or whatever I can actually create a binary file and store all these records such files will have to be declared and processed as binary files and it is preferable to have fixed size records for these files so that the calculation of the positioning pointer is made easy. So that you can always write in terms of integer number of count of records five records ten records one record seven records whatever one we will illustrate this using the sample data of the students that we had defined here is this input data file is created a batch file. So what is this batch file is an artificial data there are names of students there is a role number of student and let us say this is hostile number of course the data which I would like to store for each student could be much more it could be for example marks obtained in the exam grade obtained in a course and so on. But what I have prepared is a limited input based on the information available to me let us say these are the students who are attending my programming course and I am going to conduct multiple tests so I will have many marks or different tests to be given to each student but when the semester begins I will have only the students name the role number and perhaps the hostel in which he or she lives let us say I have six students. So I have prepared a text file at the beginning of the semester giving six as a number initially as the in the first slide demonstrating or depicting the number of students in that file or number of records in that file and then I have one line describing one student each name role number and hostel I would like to create a database file of all students who are in my class I would then like to read any arbitrary record of the database in a random fashion I would like to be able to update the information in that database for example suppose some student changes the hostel I would like to change the hostel number in my database initially all students will have no marks. So maybe we set them to zero but as students give examinations they get marks I would like to insert those marks appropriately against the corresponding student at the end of the semester I would like to give a grade in my subject to each student. So this is the purpose for which I want to maintain good records of my students in a database file I want it to be a random access file because I would like to go to any student directly this poses some problem because a student is identified through the role number however I am using a serial number to demonstrate a student sorry can I have that question again in previous slide writing a binary file there is an error f right pointer and the stream are interchanged oh I see okay I will correct that probably in the example it is correctly stated I am sorry for the error in fact I will go back and check the web resource from which I copied the sample functions thanks for pointing this out we will check the example and if there is an error in example please do point that out also certificate corrected so in order to create a binary file first I define a record that will go into the binary file not one record but as many records as I have a students I define that in a separate header file called let us say student info dot h or something like that or I can include it in my main program that is my choice this is just to illustrate that the C compiler is flexible enough you need not have the entire source code of your program in a single file you can have header files differently you can even have function files separately and one such structure which is defined here is name of a student character name 31 so 30 character name padded by of course backslash 0 roll number which is 8 character roll number padded again by backslash 0 hostel is defined to be integer as I explained in IIT Bombay all hostels are numbered 1 to 13 currently then I have floating point marks I have put an array of 5 because I expect to conduct 5 different evaluations a quiz an assignment a mid semester exam a project an end semester exam and so on and at the end I will give the student a 2 character grade a a a b b c d d f a whatever and therefore I have a Cal grade 3 notice that this structure totally occupies how many bytes 31 plus 9 40 plus 4 because in text 4 44 plus 5 49 sorry 5 into 20 so 44 plus 20 is is 64 plus 4 and plus 367 am I missing something 24 then 40 plus 24 is 64 and 3 bytes yes 67 bytes is the record length here. So, this would be termed as a record length observe that not all components are in externally visible fashion available hostel is coded as an internal integer number marks are coded as internal floating point numbers of course while inputting marks will be inputting them as text while creating an output of the file we would like to see the marks as text and we will have to do a formatted conversion while giving our input and while collecting the output but internally in the file I propose to store this information in exactly this fashion I start with the function which I write at the beginning which merely prints any students data so the parameter that is being passed on to it is struck student info s and int serial number remember what I said earlier in order to make direct access to any student record I am giving a hypothetical serial number to every student every student attending my course has a serial number 1 2 3 4 5 6 7 8 like that continuous. So, if I have 800 students I have serial number of course I have record elsewhere to say this serial number 23 as this roll number this name and so on now I am just somebody passes on information about a student to this function along with a serial number and this particular function merely prints out the data there is nothing very special about it this is what I meant by converting internal binary format information into an external formatted. For example, s name and s roll is of the character type but s hostile is integer and therefore through a percent d format specification I will print it out as a visible decimal number similarly marks which are floating point numbers are converted to percent a through percent f specification and I can see them in a readable fashion the grade of course is a character string which is printed a percent s. Now, here is the main program which creates the binary file. So, it has a whole lot of definitions first struct student info s this is one typical student. So, I want to hold one student's data somewhere I define this variable called s here and struct student info will say s is of type this structure. Since I may want to hold in the previous example what changes should be made to the code to read data why from buffer oh ok the question here is in the previous example what changes should be made to the code to read the data why from the buffer let us go back to the previous example I am sorry to interrupt this flow the question is if I just want to write read data why then obviously I should position my file pointer to the second byte and just read one byte please note that I might have this structure inside my computer's memory as far as the file is concerned it is merely a sequence of byte as long as I know what interpretation I am going to make of that byte I can read any byte. So, consequently the instead of the size of buffer I will specify one and instead of the number of bytes to be read I will specify one, but the positioning will have to be done appropriately through a seek command we shall see an example by the way of such reading in this particular illustration itself so please wait for some time ok we were I think we were defining the main program for a file. So, I have two structures defined I am defining a position pointer please note that the position pointer is always a long int in fact even a long int is often not sufficient for very large files today and that is why modern sea libraries define a special internal type which is equivalent of a still longer it, but as far as we are concerned we will take it to be long end in our case as we saw a long end and int unfortunately is still 4 bytes and not 8 and 4 bytes as we would have expected anyway here is some care option because of a query language that I have tried to define here does not concern us right now I have various variables I have a variable called a rec size or record size I will see how exactly this record size is utilized now I define the batch input file dot txt as the name of a input file and I define student db dot bin as the name of the database file that I want to create as the extension suggest I want this to be a text file and this text file is nothing but the sample file that we saw some time ago and I want to create a binary file so I give this extension when I say rec size equal to size of s whatever is the 6567 bytes is the size will be calculated by the compiler by the execution of the instruction and it will be allocated to rec size which is a variable of type int you will you would have guessed that this rec size is what I will use to describe the amount of data in the amount of data in one unit of record which I want to read or write obviously I am going to write at a time one record and I am going to read at a time one record but the size of that record is important which is captured here I open this file now and this particular thing is different it is opening this file name in R s more that means it want I want to read strings from this file which is this file is the batch input file why am I reading this as a string as opening this to read string and not just simple I want to read the entire string and internally process the string to extract different pieces of data here. So I have f scan f I read the integer value n first then I print f the data of so many students in this batch file will be read I have opened the files I will check whether the file pointer is null in which case I will know that there was some problem in reading that file and all that I am doing is using f scan f to read all the data what data do I have in the file I have the name I have the hostel and I have a serial number however I do not have the marks because they do not exist so I am deliberately putting all five marks to zero similarly I do not have grade the grade will be allocated later to indicate that no grade has been allocated I am either I could put blanks there or I could just leave null there but I am putting as an arbitrary choice two stars in that grade and finally I am printing that data by invoking the print student function which we had written earlier and this I assemble in student list which was an array of 800 or 100 or 500 or whatever is appropriate for me and I close the file this file opening and closing of the input text file now I want to put create a database file of course the core that you see above this else comes from whether the file pointer for the input file was open correctly or not if not I will print these error otherwise if I come out alright I will print that data has been read from the batch file and output binary file will be created now here is how I create the binary file I have in students so I will put a iteration from zero to n zero to n minus one actually and from student list I will take one student's record at a time into the structure s by saying s is equal to student list I and then I will say f write and s size of struct info comma one comma db so you are very right this db is the file pointer and s is the string pointer I do not see any problem there I thought this is exactly what it was in the earlier case but it was different than this is the right way my friend would like to observe whether this is the correct way of writing file it appears correct at least my c compiler liked it because it executed it properly notice what am I doing I am saying and s size is of struct student info comma one db this will write one unit of size of struct student info that means one record at a time but because I mean a loop it will keep writing one two three four five six as many students as I have so n records will be returned to this binary file I can subsequently close this file and be done with it I will then have to actually use this database file to update records of my students so how do I update the database here is how I update the database again I have defined struct student info long n db pos etcetera etcetera x size now I say give a key value for a student please note that in this example we have not constructed our file or indexed our file to be able to read from a roll number f p is used in f scan f and then it is checked for oh very good point very good point because I did have a file there was no error but what our friend is pointing out is first I am using f p and then I am checking whether f p returned a null pointer or not well we have a very a person with very sharp programming eyes amongst our mates next time please send your name or through this chat session so that I can mention it to all our colleagues but thank you very much for pointing this out anyway let's go ahead so what we are saying is we will actually like to read a record randomly now as I said I am arbitrarily allocating a serial number to every student of mine and I know which serial number stands for which student therefore I should be able to get retry the data of the desired student directly by giving a serial number that is what I am calling it a key value so I ask for the input on the key value and I read that key value which is what I call serial number then I say which test and what marks are to be updated in my database so I read the test number and test marks obviously I would have conducted one test at a time so I will have let's say test number one marks 17.5 or whatever so I read that data for a student in short what I have done here I have a database file which is separate in that I have data for all 800,000, 10,000 schools that I have I want to update the marks of one student whose serial number I take as input and I want to know which marks are to be updated and for which test so I read these three values serial number test number and marks having read this I open the database file please note that this is open for reading and writing because the plus sign and B stands for binary so I open the DB file here I read the student's record I say first DB file pass or DB file position is equal to size of struct student info star SNO obviously my serial number starts with zero because the first record will not have any displacement the second record will be size of struct away the third record will be two size of structs away and so on so I can calculate the DB file pass like this and then when I say F seek FP comma DB file pass comma seek undersource set what this does is this sets up the internal file pointer to a position which points to the record of the student with the serial number that is given why because the record of the student with that serial number corresponds to serial number into so many bytes away from starting point this seek undersource set is that flag which I mentioned which is used which is one of the three flags which is used in FC remember FC can be used to set the file position pointer either with respect to the beginning of the file or with respect to the end of the file or with respect to the current position of the file so this particular FC is going to set the file position with respect to the beginning of the file and beginning of the file is represented by seek underscores set so consequently I will move the positioning pointer of that file to DB file pass bytes away which is exactly where I will find the record of my desired student with serial number then I will read the record of that student into the structure S the amount of bytes to be read is size of struct student info and exactly one such unit has to be read from the file called F P which I have opened as a binary file I will use the print command to print that students record now I will change the marks of that student I have read the record from the file it is in my memory so the structure S actually contains the record of that student who has the serial number how do I change the test mark very simple suppose I am giving the marks for second test since the test arrays of five elements the second test will be reprinted by index one that is why I subtract one from test number and use it as an index to update the marks in the test so as what marks this is equal to test mark I would now like to rewrite the updated record as I have updated the record in my memory and I would like to rewrite it to this it is very very important to tell your students by the way that when we talk about updating the records on the disk there is no mechanism for us to directly go on the disk and write you cannot write individual bytes on the disk arbitrarily by assuming that disk is your memory conceptually it may be similar the only way you can write on to the disk is first I mean for updating purposes is to first read that data into your memory update your memory and rewrite that data back that is precisely what has been done here in the previous slide we saw that how a desired students record is read first by fc positioning the pointer of that position to the appropriate point on the disk file array and then reading so many bytes this gets you the data of that student in the memory structure for s after which you update that record in s in memory here we are updating only the test marks but we could have changed hostel number we could have done we could have changed grade whatever whatever and after that we want to rewrite it please note we are again saying fc fp comma comma seek set what do you want to do we want to set it to the current position itself and then when we write this it would have written that in exactly the same position confirm changes in the file I will again fc db file pause I am again reading it and I am after reading this I am printing the data why is this statement merely to ensure that if your students execute this sample program they can actually confirm that correct update was done to the desk so the previous statements read the record from a disk change the marks and rewrite it but how do you know whether what is written on the disk is correct or not well simple you read that record again and just print it using your print command that is how you can confirm that the file is properly updated here is a portion to print all db records so once in a while you would like to print records of all students very simple you open the file if there are six students you say for i equal to one to six read one record at a time and print that record and at the end close the file if you have six you will print it for n a more general approach would of course be the more correct approach would be not to use any constant such as six or n because at any point dynamically during a semester you do not know how many students are there some student might have left the institute some two more students have joined and the file would have got updated by inserting those records or by deleting those records so you will say you will use the f read command to check whether end of file has reached or not we have discussed this end of file in the context of text files exactly the same e o f concept exists while reading any file whether text file or a binary file so essentially you are reading a random excess file in a sequential fashion which is perfectly possible and is doable and is in fact done so this then is a simple example of handling a binary file it also illustrates the use of database notion of a database of students later on when some of the students study databases study structured query language and so on they will probably remember some of these concepts that are covered or are illustrated through an example in an introductory course I would like to discuss one particular example that I have constructed here as I said the examples are multi precision arithmetic for very large numbers and for solving a set of linear equations using Gauss elimination I have found them to be important examples we may not have time tomorrow to discuss this if we have I will discuss these briefly but I will be including the slides illustrating these examples and also the corresponding programs in C which will be put up on the website as well as on your model so you can examine them during your lab session later I wish to just consider an example of finding roots of an equation using bisection method you might wonder why am I considering an additional example of numerical computation particularly when we already discussed an example of finding roots through Newton Raphson and so on there is a special purpose because I want to use I want to show you how you can use this example to motivate students to understand binary search better and that is the reason why I have included this example here let us quickly in the next 10 minutes or so go over through this particular example of finding roots of equation through a bisection method the basic method is very simple if we have an equation that equation is represented by fx equal to something for example this is typically a cubic equation something like a x cube plus b x square plus c x plus d equal to 0 now a cubic equation as we know always has one real root it might have three real roots or one real root and two complex conjugate roots so assume that the real root is somewhere here this is the desired root which we wish to find we do not know it at the beginning so what we do is we typically start with a guess of one value which is on one side of the root and another value which is on other side of the root if here is the root then on this side the function takes negative value so I arbitrarily choose a point L o or low on this side similarly on this side the function takes positive values so I will arbitrarily choose a point here which I call high it is not difficult by the way to find out low and high points all that you need to determine is that the function value at high is positive and function value at low is negative so if there is a function value negative and function value positive then in between low and high it must be crossing zero which will be my root that is the premise so I will explain to the students you can tell them that you will make some assumptions about low and high values which you can either read or arbitrarily assign and then how to calculate the root well you have to reach the root iteratively now there are multiple ways of doing so I can start with one point say high point and go point by point a point zero one plus point zero one plus point zero one but I will require a very large number of iterations a much faster technique we should say is that use the bisection method in which we just find out the average of low and high and take this point as the midpoint once we have a midpoint we determine the function value at that point if the function value is positive as illustrated here obviously the root is on this side if the function value is negative somewhere here then the root will be on the other side what is important is that we are illustrating to our students that by this technique we are able to reduce the space in which we are searching for the root by half just in one shot originally this particular was the space from low to high so this was the region that we are searching for the root but once we determine the midpoint and calculate the function value at that midpoint we immediately know whether we have to search for the root on this half or this half you will agree that this process will converge much faster than any other process going one by one at a point and this is exactly what we shall use later to motivate our students to understand binary search better so start with low and high values such that f into low into f into high f of low into f of high is less than 0 what it means is these two function values are of opposite sign one positive one negative so the product will be negative now I compute mid and I compute f mid once I have calculated f mid I will just check whether the value of f mid by the way I will put some threshold value because the function value f mid is here sometimes it could be here sometimes it could be here but when f mid becomes 0 that means I have found out the root now how will I determine whether I have got exact 0 or not in floating point arithmetic it is very difficult to get an exact 0 and therefore we talk about having some threshold value so if the f mid function value is greater than that threshold then I have not found the root so now I have found out the next interval the next interval is either between low and mid or between mid and high so it is a very simple concept our students will find it easy to understand it and therefore the program that we write for it can be written easily so here we first write a logic many of you will recognize this some kind of a pseudo code here there are different ways of explaining to our students the basic steps of our algorithm this particular example uses these slides incidentally are courtesy my colleague Prasam Millen Soni who is a computer science expert here and when he was teaching CS101 he had used this example I liked it and I have modified it I will show you what modification I have made so this pseudo code says we write a program to solve the cubic AX cube plus BX square plus CX plus D so we take in input A, B, C, D take in input low, high and TOL which is at tolerance remember I mentioned the tolerance around the function value at mid result of size of struct and in such case how can we set the pointer at required position the result of the size of struct is returned by the compiler as exactly how many bytes are occupied by that struct notice that we mentioned that there could be some padding bytes depending upon a particular implementation ordinarily however the size of struct is simply equal to the total number of bytes that are required to represent different data values so for character strings it will be as many bytes as we have declared for intent float it will be the size of intent float and so on so we can actually calculate the size however we should never depend upon our manual calculation for any struct the size of will return you the actual value if you want you can print that value but more important that is the value that you use so when you have that value that is the value that you use to move in that many chunks from one position to the next position inside the file by moving the pointer by those many bytes okay let us get back to this question so this is not very difficult to understand I find out the midpoint I check if the value of f at midpoint is greater and then the tolerance which means I still have to search then I will locate the next interval to be either this or this and I return mid whenever this while loop ends because that means I have reached close to zero in terms of the function value at that mid that means f mid becomes less than tolerance this f mid is looked at as absolute value because the function value around that root may be either just slightly positive or just slightly negative and on when we describe the binary search using this we want to worry about this because we are specifically we will be looking at discrete data and not a real number line so here is a program this program reads a b c d low high as values first a b c d then tolerance and then subsequently the low and high cases then it calculates the function value at low and function value at high so this is a into low q plus b into low square plus c into low plus d and this is a into high q plus b into high square plus c into high plus d now I calculate whether the sign of these two is greater than zero if it is then I have given a wrong initial estimates and I will simply terminate the program because there is no point in progressing further there is no such possible however if that is not so then I will write the main part of the code this I have described in a separate slide this main part of the code will through bisection method reach close to the root of the function which I will then print which will be the value of the mid itself obviously the value of mid will keep on getting improved through the iterative process so here is the process initial values for mid and f mid I calculate mid as low plus high by 2 and calculate f mid exactly using the function definition now this is my while loop so while absolute value of f mid is greater than tolerance I have to keep iterating in each iteration what do I do I first check whether a flow into f mid is greater than zero what does that mean if it is greater than zero that means both are of the same side so if a flow and f mid are of the same side then I have to push the low to the mid point so I will say low is equal to mid and f flow is f mid because I already calculated the function value otherwise if they are of opposite side that means f mid is towards f high and therefore I have to bring the high point to the mid so I am saying high is said to mid and f hi is said to f mid so please observe what we are doing is we are either shifting low to the middle point or we are shifting high to the middle point in either case we are reducing the search range by half and we again calculate the mid point again calculate the f mid and then we go back to the iteration what we are doing in this iteration we are simply checking whether the new f mid function value at f mid is greater than tolerance if it is I will repeat the iteration and so on so this then is the main part of the code which will appear at the place where I indicated in the program is the previous slide this is where I will write this main part of the code now this example as I said is an additional example illustrating numerical computations of finding the roots there are n number of methods of finding the roots the purpose of describing the bisection method has an advantage the advantage is that it converges in a logarithmic fashion of course the word logarithmic fashion etcetera will not be clear to the students immediately but here is the small illustration that I have constructed to demonstrate this fact after showing this program I will say that this will help me in understanding binary search so this is where I can explain the binary search to students the best example that I have found by the way is to tell them that if the result of a university exam are published and all people who have passed their role numbers are printed in the newspaper and you have to find your role number how will you find it if there are thousands of students who have passed will you search for every role number the students will say no they will quickly go to that role number then you ask them to elaborate how they go and then explain to them that what they are doing is actually roughly equivalent of a binary search however quite independently after having introduced arrays and particularly sorted arrays to demonstrate how the search can be faster this is the way to motivate the students to the binary search now we once again emphasize that in this particular search technique for the root that is the bisection method we are looking for the root by first looking at the midpoint of the range and in the process the search space is reduced by half once this point is made clear we can tell the students that imagine that x axis was made up of discrete points like this 1 2 3 4 5 6 and imagine that we are searching at each of the discrete points now one way to search for a root is to examine the function value at each of these points that will be equivalent of a sequential search till we hit upon the desired root of course in a cubic equation a root always exists but in a search operation the desired role number or desired employee code or whatever may not exist in the array so that is another problem in this we have no root but still the time that it takes to examine each point will be much longer and binary search permits us to divide this range into two parts that is the point we are making now how do we show that this process can be considered to be exactly equivalent to an array search well I just invert this diagram in this fashion and I say that this is my function consider this function to be equivalent to array content so these are the points 1 2 3 4 5 6 7 8 now I can search for a desired person consider this that I have a sorted array 0th element is role number 1001 first element is role number 1002 and so on notice that these are not consecutive after role number 1004 the next number is role number 1006 and so on which is entirely possible and these are let's say marks obtained by the students now having the students in sorted array if I want to search for a specific student say 1009 now one way is to blindly go after 1 2 3 4 5 6 7 8 and reach here in this particular case the best illustration would be to say we are looking for 1002 and one way is to start from this point search this is it 1002 is it 1002 so I have to search all of them and that is exactly equivalent to searching for a root by looking at each individual point on this line and evaluating the function value instead if we do an equivalent of a bisection search then just as for the root determination we went to the midpoint and that permitted me to converse faster towards the root similarly I can go to the midpoint here and if this is not the desired number I can check whether this number is larger or smaller than what I am looking for and consequently move the low and high appropriately to mid in case of an array the low is exactly equal to 0 which is the first element high is exactly equal to the maximum index which is the last element believe me I have tried various ways of motivating people to understand binary search in a programmatic way and this construction has helped me to find that my students understand it better because they usually know the bisection method or they will study they can understand it better because they are familiar with functions and roots but by putting this illustration we make it comparable to an array by showing discrete points here you can of course improve upon this diagram and if any colleague teacher has a better mechanism high and all other colleagues would be glad to know about it to find out how we can explain this better last time we had a discussion on whether we should discuss the notion of time complexity at all or not as I mentioned it is worthwhile discussing for two reasons because A there are at least some students who would be very capable of understanding the notion of time complexity and B for all other students it would help if they understand by that by being careful their programs will run faster here is one more slide to demonstrate the analysis of binary search in an array so if an array has 1024 elements a sequential search would require on an average half the number of elements to be compared which means about 512 searches however if we use the equivalent of bisection that is binary search it reduces the number of elements to be searched to 512 on either side of the midpoint in the next iteration we search only upper or lower half further reducing the search space to half and successively this range is reduced to 256 then to 128 then to 64 then to 32 then to 16 etc and finally we look at only one element either that element is the desired element in which case we say search has succeeded and this is the index at which the number is found or there is no desired value there in which case we say the search has failed but whether it has succeeded or not can be pinpointed in how many operations we can easily ask our students to count from these lines so first comparison is when you are 1024 elements then 512 then 256 and so on and we conclude that in just 10 steps we have concluded our search and then we observe to our students that 10 happens to be logarithm of 1024 to the base 2 so as by the sequential search will take order in comparisons 512 to be precise in this case a binary search will take order log n to the base 2 comparisons I will conclude this session with it with the request that all of you could keep thinking about how to construct examples similarities and what should I say a conceptual framework for explaining some concepts more easily to the students what I have discussed here is how to use a bisection method of finding routes to illustrate the meaning and the impact and importance of binary search in terms of program efficiency my request is all colleagues whether they have used such similar techniques to illustrate some other point or if they think of something better through this effort of this workshop and through the collaborative mechanism that we are setting up let us bring out many such notions illustrations diagrams if possible maybe what you call animations and so on to make our students understanding better in this subject so with this we will break 40 thank you very much