 ഇഠ൏ കിുകോകൎ നിയ്ൃനിക്്കിത്കിക്്െന൉ക്ു ഩാ൅ി്േകിോക്ക്ക്ക്േൃന്കകിൃക്േക്കേെ നിനിക്േകെക്േകിനാ്േ്ക� and some specific advantages of modernization for specific activities. I will also be distributing a set of sample problems illustrating the use of iterative constructs. Incidentally, these problems were set by a very smart teaching assistant who was not an M.Tech or PhD student but was a third-year student of M.Tech program who was the TA for the first-year course. A student who had tremendous interest in number theory primarily, so most of his problems reflect that particular affinity to numbers and numerical things. But there are a good set of problems. These were given to our students as practice problems. And these could be used for example as a test problem or an exam problem as well depending upon what level you want to deploy the problems in the examination. So these will be distributing after this discussion before the tea break. Couple of things. As I mentioned, we invariably use slides of colleagues who all release them necessarily in open source regularly but they are open sourced in so far as the other colleagues in the institute are concerned. So for example whenever I would compose a lecture and if I use some slides from my colleagues, it is customary to say that some ideas come from C. Prof. Millen Soni and Prof. Abhinam Ranade. Yesterday we discussed that very briefly the need to recognize the contributors in whichever way they are and that is a must. I will comment on it, a comment that even Prof. Ranade has made later in his class. Okay. Iterative solutions using account, explaining the four statements in C++, then using the loop for estimating the value of the logarithm. Then we discussed the Hemchandra numbers. Now they are called Virahanka numbers. How many of you have heard of these? Hemchandra numbers or Virahanka numbers? None. That is the problem. There is an Indian legacy which through Arab scholars reached Europe and they are now known after a European scientist named. Save numbers. I will illustrate that later. So this is the standard iterative thing that if you are given some n numbers and you want to find there some, you illustrate that you need a block of statement to execute a fixed number of times and the counting mechanism is explained briefly. This is all standard practice. So I illustrate that by some kind of a flow chart. Note that vile is introduced before this. So people are aware of vile something, what is to be done. But that condition under vile which is count less than equal to n, the initialization count equal to 1 and count is equal to count plus 1 after executing the block. So this for example was my way of illustrating that ordinarily when you use a vile loop, you just have a single condition and that condition is checked. However by appropriately putting that condition, by doing an initialization before the loop and by incrementing the count within the loop, you effectively get a motivation for type of iteration. So then the standard program is given. This is not very important. All of you can construct this. A factorial n problem is solved iteratively. So this again is a standard program. Nothing very special about it. A repetitive execution for counting is now introduced using the for construct and the way it is stressed is that the for instruction permits us to define all the three actions related to the control of that loop in one place. So this again is a standard thing. Indication is given of the syntax. And then using the for statement you do exactly the same problem. So this sort of people appreciate because earlier a similar problem has been solved using the vile construct and so on. Now what is more important is, now this portion by the way is related to CC++ programming. Now this portion of some 8 or 10 slides is not strictly modularized. The concept of a for loop is relevant whether I am teaching C or Python or Fortran or whatever because there are similar loop there. So it would be nice if while explaining that concept I avoid using syntax or closely related syntax of the C program so that that becomes a sort of conceptual module and then that can be expanded into the particular programming language that I use. These are general formats, additional specifications. Now I come to this problem where modularization is very easily possible. So the conceptual modularization is that if I take a problem of finding out the natural logarithm numerically then this problem has nothing to do with a specific programming language. It does not matter what programming language that you use. It is then meaningful to create a 5 minute or 7 minute module describing just this problem and its solution. So please note we are doing program design and not algorithm design. So algorithm has to be explained. The standard method of finding out the area by approximating that area by squares or whatever. Now that entire portion should exist as a separate module in our digital contents that we create. If you were to use this set of slides completely you will find the whole thing is a single monolithic lecture and it will be very difficult for you to extract this slide here and that slide there and prepare your own explanation for example through a program which you yourself would write not necessarily in CC plus. Now in our workshop subsequently you will find such modular assets being prepared and being put on the model so that all our colleague teachers including you of course can use those modules and fit them now tell them into your own teaching the way you wish. So here are some observations for example that a computer cannot integrate. So we first give an idea that we have to calculate the area under this curve 1 by x between 1 and a to find out the natural logarithm of a and since the computer cannot integrate we will have to find that integral value out by calculating the area and to estimate the area we will have to approximate the area because the area is under the curve. So there is a diagram that is given here which says that calculate the area under the curve y is equal to 1 by x between 1 and a and that will approximate the value of logarithm. Then the standard practice how do you calculate the area under the curve we do not have formula for calculating area under a curve so we approximate it by a series of rectangles and then we calculate the sum of all rectangles areas of all rectangles between 1 and a now here what is obvious is that the way the rectangles are drawn the total area will be slightly larger than the actual area because you will find that each approximating rectangle is actually overstepping the exact area under so this is a point which typically some student some smart student will raise and what we should do or at least what I do is I ask them to hold on and subsequently when we extend this problem we find how better approximation can be for example if I calculate the area at the midpoint of every rectangle so that the rectangle is located at the midpoint and not at the left corner then there will be some portion of the rectangle below the curve some portion of the rectangle above the curve the other idea is to calculate the area under a trapezoid that means you connect these two points by a straight line in fact that works out to be the closest approximation in this particular case it may not always work now these discussions are numerical approximation discussions and again you will notice that they have nothing to do with a particular programming language but it is important to conduct these discussions and emphasize them to the same extent that we emphasize the actual programming and that is because the algorithm becomes clearer in the minds of people coding is actually a very small portion of the mental activity then so there we explained that each rectangle has a fixed width what is the height of that rectangle so we show that if there are n rectangles of equal width the width is a minus 1 by n simply because a is this point one is this point a minus 1 by n is the width so we know the width using n rectangles this method is called Riemann integral incidentally so you know that the x coordinate the bottom of the i-th rectangle okay first rectangle x is 1 second rectangle x is 1 plus w third rectangle x is 1 plus 2 w so i-th rectangle will be x 1 plus i minus 1 this formulation is relatively easy for people to understand they appreciate this then you say the height now the height of the i-th rectangle since that height is exactly equal to the value of that function at that point value of the function at that point is 1 upon x and x we know because that is the coordinate of the leftmost point so 1 upon that is actually the coordinate so it is 1 upon x which is 1 upon 1 plus i minus 1 into x so the area of the i-th rectangle is width into height and therefore it is width into 1 by x and therefore it is w into all of that so you have reduced the formulation for calculating the area of one rectangle in terms of the constant value w and now it is very obvious to people that they just have to add 1, 2, 3, 4, 5, 6 and such rectangle since we have already introduced how to run an iteration by counting using for loop from 1 to n it then becomes a straight forward matter to write the computer program some questions how many rectangles we should take should we take 10, should we take 100, should we take 1000 should we take 10,000 and i would suggest that even in a programming course a little discussion on this makes lot of sense you are motivating them to consider larger issues in numerical computation subsequently so approximations and close this of the value etc etc later on the same thing will recur when for example we consider finding out roots of a function using let us say bisection method or using any standard method for finding out root where you determine at what point the function crosses the x-axis and you try to determine that as a root anyway so here an arbitrary value of 1000 is taken and then the width of all rectangles taken together is a-1 so therefore this is the w we formulate the x coordinate for the rectangle and then the area of rectangle is formulated now these formulations are important to be written down by pencil or pen by the students before they write the program and invariably this does not happen now this does not happen because we the teachers also did not do it when we did our programming when we were students we simply started writing the program directly but we know for example that if this habit continues it will be an impediment when our students try to construct larger programs and therefore we insist that students should do these steps before writing a program not as a documentation after the program is written that happens in 90% in spite of our telling that the students will do it but it is therefore important that while we illustrate a problem we show these steps explicitly the point again is that if you are going to spend 15 minutes describing a program for solving a problem out of that 15 minutes 5 to 6 minutes only should be spent on describing the program rest of the time should be spent on how the derivation of the algorithm for that program is done particularly in the numerical computation so this is a program now you just collect the number whose logarithm is to be found 1000 is the number of rectangles that we arbitrarily assume and you simply construct this program please note that most of such programs are actually very compact and short program the programs are not very lengthy but the computations that they do are very illustrative now here is one question having done this what kind of a quiz I should ask people so I would like you to think for some 3-4 minutes and write down a multiple choice quiz question related to a. this problem and b. the program which solves that problem it could be either one of them so try to construct a multiple choice simple question you will suddenly find there is much easier to explain this algorithm and write the program and explain it but it is difficult to set a quiz problem this program of course you can mentally reconstruct but I will just keep that previous slide here so please feel free to construct a question which is either directly related to this problem or any extension of it that you want people to consider any aspect of programming or any aspect of algorithm absolutely feel free so any success so far the question is in the far initialization if the initialization is i is equal to 0 instead of i is equal to 1 what will be the exit condition if i is equal to 0 then what will be the exit condition and what are the possible multiple answers if it is multiple answers are there then i can may say that it is i is less than 1000 i is less than or equal to 999 like that so this is a good question but i will tell you the problems associated with it so first of all the question emphasizes the correct understanding of setting the three parameters associated with for loop and that's an important exercise his contention is if here instead of i is equal to 1 i start i with i equal to 0 then what should be the exit condition such that the loop is actually executed 1000 times if i change the value of i here okay it will impact my algorithm because the value of i is being used in computation of the rectangle itself so what should be the choices so that if i start with i equal to 0 the correct transfer as far as the count is concerned is that i should be less than equal to 999 or i should be less than 1000 these are the two possible choices but shouldn't one of the choice say none of the above because changing this will impact the algorithm now that may not be very obvious but that's a very important point which has come up now i would like to illustrate the advantage of this approach of setting up these questions usually i am alone in my institution when i am setting a paper or a test i will set up a question thinking that i have thought out all possible dimensions of that question but it may not happen as we just noticed so somebody has pointed out the algorithm will not work this is the advantage of group discussion invariably in our institute because we have a set of TAs for every test we collect the senior TAs and they set a series of questions through such group discussions of course the TAs are students themselves and somebody might be tempted to share the questions that he has said with the students in the hostel so what we do is we ask these students to form different groups and give large number of questions and then finally two or three of us teachers select which particular question to ask in the exam invariably it helps but more important point is discussion on every aspect of teaching with some colleagues immensely helps it will help correct smaller things second point whenever we set multiple choice questions it is our practice to just point out what is the correct answer in a setting for a student if a correct answer is announced at the end of the exam i know that either i got the answer correctly or i know that i did not get the answer correctly but if there are four or five choices and if i have given one of the four choices which are wrong i do not know exactly why my choice was wrong it is therefore mandatory that when we set multiple choice questions we write the possible choice of answers ABCD and for every choice we write an additional explanatory line why this choice is right or wrong this is a small but important step which is not done by most of us and since it is not done there is no written documentary material available later for the students to understand the various meanings of different choices for a student it is important that the student understands why a particular choice is wrong and also the student understands why a particular choice is correct see i might have got that choice by some either apadapi for the wrong reasoning i would have got the correct answer but even if i get a correct answer it is worthwhile to let me know that this was the reason for this answer to be correct now you appreciate the use of technology for enhancing the effectiveness of this communication to the students ordinarily what will you do you will have to deal with suppose there are five choices or four choices in a class you have to explain each choice and say why it is right or wrong but in an automated setting when a student completes the quiz the student can be given a choice to go through that question and look at the reasoning each individual student can do it at one's own pace if these answers are online after the completion of the quiz and these answers can be kept online if we have prepared these possible answers ahead of time what i mean to say is that to create digital contents for any online course or any online delivery of even a small mundane thing like an online quiz requires roughly double the effort that it requires to prepare a paper quiz you will have in the group activity you will have an opportunity for each group to construct such quiz questions for a topic that has been allocated for i equal to 1 i is less than equal to 1000 in the free statement 1000 is floating point constant whereas in the for loop it is integer constant and what is the such type of questions we can ask as a quiz question that is also an interesting point that is also an interesting point it emphasizes again or it examines again the understanding of the student on the evaluation of expression so for example the point that he is making is that we have this as an integer constant and we have used this as a floating point constant and the objective is that the value of w would generally be a fractional number it will not be an integer and since w is float i will get an absurd value if the division is integer therefore it has to be float there is no point in keeping this 1000 as float because it is actually a count and therefore it is an integer count incidentally even if i put less than or equal to 1000.0 the loop will still work correctly and the explanation would be that look the for comparison will always happen with an integer equivalent of the right hand now that is also a good problem but again what his observation illustrates is that no matter what particular topic we are discussing it is possible to keep pointing out to something that students have learnt earlier and to check whether they have understood that concept or not now here i will quickly go through these slides here to illustrate my point of packaging of concepts this entire set of slides which deal with computing of natural logarithm as approximation of area under the curve by a series of rectangles as i said has absolutely nothing to do with programming or programming language therefore computers came the remand time there were no computers but remand did calculate by simply counting or simply adding the sums which were counted by hand and subsequently by calculate therefore this entire discussion right up to this point you will agree that is independent of any programming language but it embodies an important concept the steps of the algorithm are very clearly brought up would you agree that if i were to create a small package of digital assets comprising of these slides alone and comprising maybe a few lines of explanation how to use these slides in the context of anything else that you do so if this module existed on let's say a web somewhere as an open resource as we are planning and suppose this was prepared by Prof. Ranadeh Lysen i want to use this in my slides what further generalization would you expect from these slides if any so one thing i will point out you will notice that these slides have Dr. Deepak B. Phatak written here session 6 written here sequence number is 25 here none of these would make sense if these are to be used in the context of a lecture to be given by some other colleague so while we do expect other colleagues to acknowledge that these slides or this package of concept comes from this resource the resource must be made available in an easy to use fashion and therefore the name should not be there these things should not be there but these things should be kept somewhere as metadata associated in this concept such modularization will permit modular teachers to use their own creativity by combining such conceptual packages some programming package concept and put something of my own together and create a complete set to be given as a lesson or as a theme or something like this is not a new notion by the way packaging the concepts and making them into modules such that different modules can be used by people this was first initiated many many years ago in the context of digital assets only but much before massive online courses started these are the early days when digital contents were being used by people and one professor Sidney Barras at Rice University thought of creating such package concepts so how many of you have heard of the site called Connections not many in fact I may be able to show you Connections is a site which was created by the Rice University enthusiasts to offer open source digital packaged modules for teaching and learning for anybody to use and they tried to create modules initially those modules also reflected the traditional way of teaching which is one and a half hour lecture or multiple one hour lectures consisting of a module comprising of six lectures or seven lectures but that was the first introduction of the notion of a module so I will tell you a very interesting incident what that site permits is if I want to write a book on a particular subject and I find let us say that book should have some nine chapters and out of these nine chapters I notice that six modules on the connection site which somebody else has created not one person but multiple people would become my chapter one chapter four, chapter five, chapter six chapter eight itself the remaining three I write myself I upload my three modules which I have written onto the same connection site and then I click on a tool saying that please create a book which I will become the editor of that book and which will contain these nine chapters what that tool does is it pulls out the digital assets of each of the packaged module and prepares in a book format in a chapter format chapter five, chapter six what have you put the author of that chapter is actually the author of that module so due credit is given that chapter is written by that author so out of nine six chapters would be written by six authors three which I have written my own name will appear and on the top of the book my name will appear as editor and all this composition if all the modules are present can be done in about two hours time so one particular professor who had to give a series of lectures to some management people or IT management people in Malaysia he was invited and the course was to run in about twenty days time in Malaysia he was in United States and he says that I would like all the participants to have printed books which book do you follow no unfortunately I don't follow any book but I'll ensure that people will get a book he use the connection sites he actually wrote two or three modules created using that tool created edited book connection sites has a connection to an actual publisher in United States and the book that is created is created in such a digitally formatted way that the whole package is sent to the publisher publisher actually can print that book so publisher printed that book some fifty copies were shipped to Malaysia and they arrived in Malaysia before the professor reached Malaysia to start that course so you notice how simplified the logistics could be today you have e-books it was not fashionable those days in fact all of you have e-books and you already know the problems associated with that e-book you are here your e-book is in your home or office the book by Rana Dayan now they have agreed that they will permit to load that book on two devices but invariably if I am a teacher in your place I would like one device to be maybe my desktop in the office and one device which is my laptop but if I come to Mumbai I have neither then what do I do ideally a PDF book which I can carry anywhere would be idea the PDF book unfortunately can be easily given to anyone a physical book cannot be given to anyone because I may need it a digital copy can be given anywhere there is a dilemma which the publishing industry has not figured out how to handle by them Amazon books actually permit you to read the book anywhere but you need to be connected to internet classroom like this where Wi-Fi is gobbled up you cannot read it if you are travelling by bus or train long distance in India you still have good internet connectivity but sometimes there is a problem but I digress the point being made is that digital contains that we now create should actually benefit from the efforts and experiments that people have done and we should insist on creating such content now here is the point I make suppose I am one of you and at my own institution I am preparing digital contents in this fashion independent of whether I feel that anybody else will ever use these contents or not that design of those contents should be done by me like that one so that in the eventuality somebody wants to use it it would be easier to use alright let me go over to the Hemchandra problem so this is the problem suppose I want to build a wall of length 4 feet I have two kinds of bricks one brick is a longer brick which is 2 feet long and another brick is a shorter brick which is 1 foot long so 1 foot long brick and 2 foot long brick forget that I mean let us say that the width doesn't matter I just want to construct a wall with length this now in how many ways I can lay the bricks so that I fill the 4 feet simple combinatorial problem actually but stated in terms of wall so the possibilities are I use 4 bricks of 1 foot each I bricks 1 brick of 2 feet and 2 bricks of 1 foot 1 2 1 1 1 2 and 2 2 these are the possible combinations so in how many ways can this be done if the length of the wall is 3 feet or if the length of the wall is 5 feet how many different ways I can do it if the length is 4 feet I can do it in 1 2 3 4 5 different ways the possibilities if the length of wall is 3 4 or 5 feet are indicated here in a 3 feet wall I can lay it as 1 1 1 2 1 1 2 these are the only possible in a 4 feet wall I can lay it as 1 1 1 1 2 1 1 2 1 1 1 2 2 2 in a 5 feet wall I can do exactly the expansion of this the point that then I ask the students to note is that the possibilities are actually recurrences of a previous group with something added so for example if 3 feet wall has these many possibilities a 4 feet wall will have these plus either an additional 1 foot brick or for some of these possibilities where there was no 2 foot brick so this is the difference between 4 feet wall number of cases and 3 feet wall similarly I draw the 5 feet wall and then I suddenly notice that the possibilities for 5 feet wall is equal to possibilities for 4 feet wall plus the possibilities for 3 feet wall so the total number of classes is equal to the number of classes for the sum of the number of classes for the previous 2 possibilities actually Hemchandra was not solving a brick problem this I have constructed in professor Ranade's book you will find an explanation so you see here this is this recurs here and then there is something additional then this whole thing recurs here and then there is something additional and what is additional is very clearly here mark for a 4 feet wall you take all these things and add 1 foot thing so because 4 plus 1 is 5 feet in addition you take all the 3 feet wall concepts here classes here and add a 2 foot brick so all possibilities of 3 feet wall plus any 2 feet brick will give you 5 feet this is how you come to the next the Hemchandra's actual problem was in a poetic meter the poetic meter is made of short syllables and long syllables the long syllables is 2 beats short syllable is 1 beat those of you I am not very well versed music but I think this is common sense that you have a short beat and a long beat so this is understood by people he has given an example of Yakundan to Tusharara Dhaula or something like that this is from his book now suppose I am designing a poetic meter with 8 beats how many ways are there of filling 8 beats the reason I chose the brick example was that the model students probably not very well initiated in music could relate to the count of bricks much more easily but you then expand on to this and then you tell them that the way you handle it is so Hemchandra's solution Hemchandra's solution is recorded by the way in the mathematical scripts of India in those days by the method of Pingala it is enough to observe that the last beat is long or short if I observe that then I can find out the whole solution that was a very critical observation what Ranade also points out is that Pingala was a mathematician poet from 200 BC Hemchandra is giving credit to someone who lived more than 1000 years before him I have found incidentally that this slide is something that the students cherish and remember because this slide is not so much about the oldness of this solution but it is about this copy if necessary and if permitted but always give credit and I will tell you you cannot overemphasize this to our students the whole of the nation cuts a very sorry figure in the global community of human being when it comes to the ease with which people tend to copy things from others without giving credit and we need to correct ourselves and the entire society and as teachers we have this primary responsibility the point is that this was this observation this is merely an observation the solution is constructed like this an 8 bit pattern can be made by take any 7 bit pattern and add a short bit or take any 6 bit pattern and add a long bit same thing like those brick a 5 feet long brick wall can be made by taking any possible 4 feet wall and adding one brick and any possible 3 feet combination the number of 8 bit patterns is equal to number of 7 bit patterns plus number of 6 bit patterns now this actually results in an recursive relationship but we don't want to introduce recursion at this type so we say hn is number of patterns with n bits so h8 is h7 plus h6 in general hn is equal to hn plus 1 plus hn minus this is the recursive relation does this help us to compute h8 now please note that here I do not use the word recursion I merely say that we will come across recursion later but this is called a recursive relation but we don't know recursion so how do we compute it so we say that we can compute h8 if we know h7 and h6 but to know these we need h5 and so on so we write a series of relations here starting with h1 h1 is number of patterns with 1 bit h2 is number of patterns with 2 bits there are 2 one is either s and s or one is l so I know h1 and h2 h3 will therefore be equal to h2 plus h1 which is 2 plus 1 which is 3 and here I illustrate the 3 possible combinations subsequently I need not do that I say h4 is h3 plus h2 h5 is h4 plus h3 et cetera et cetera et cetera and this can go on so given the value of n I can find the nth himchandra number okay and then I use the iteration again to program to compute this so I say that in each of the formulation at any time I require 2 previous values to calculate the present value and once I calculated the present value then that present value becomes the immediate previous value and the previous value becomes the previous previous value and the iteration can carry on like this and I will agree that this explanation of h priv and h current and h next and then extending this did not go easily when I use these slides in the subsequent years what I did is I would go on to the board or a white board and explain by showing 2 boxes for the previous and the very previous and the next et cetera and then show how iteratively you would change these values and so on the program itself is not very difficult so this is mathematics from poetry and incidentally this is a natural thing the golden ratio is defined as the ratio of consecutive terms these are now known as Fibonacci numbers all of you are familiar with Fibonacci every textbook on computer programming will deal with Fibonacci numbers it took the nation 30 years after independence to emphasize on the minds of global mathematicians to recognize this contribution from India first we had to prove that much before Fibonacci the Arab scholars knew this and this was proven by the documentation which Fibonacci himself has been did he says that when he had gone to Arabia he actually met with scholars there and who actually were dealing with these problems they explained this to me and during our conversation they said that they have learnt it from the Indians scholars then our people traced back one of the persons incidentally is a professor in our humanities department he has spent he is of course a professor of humanities but he is an ardent mathematician and he has taken upon himself to go at various places and find out those mathematical inventions which are effectively Indian for which there is a documentary evidence available you will notice that by including this example in his book professor Rana Day has solved two or three issues one he has introduced recursion without talking about recursion but given an iterative solution that is the standard way that we also deal with second by introducing a problem which relates to mathematicians you will find that your students appreciate this quite third and more important a slightly more complex iterative solution than the normal one because in all normal solutions you have to contend with one previous value which you increment modify and take to the next value here you need two previous value and then if you have to compute sums of very many series where the subsequent term does not depend only on the previous term but multiple previous terms you will be able to construct iterative solutions using this the problem that I gave I have constructed a problem sir the problem looks realistic I tell people that there are two trucks they contain packages obviously the packages have different weights we assume that the weights are integer weights now one truck is more heavily loaded another truck is less heavily loaded so we want to balance the load and we want to be more comfortable and I pose an additional artificial problem given the weights of individual packages in both the trucks can I find at least exactly one pair exactly one pair of packages one from one truck another from another truck such that if I exchange these two packages the trucks would become balanced its an interesting problem to pose I have two trucks which are carrying packages లిస్ర్నాచింణికాస్తాఆస్నికిన్ప్రిదార్దాకమాన్నిట్కి. but I want an exact balance and I want that exact balance to be achieved if possible by exchanging exactly one pair. How will I solve this problem? So this requires searching for the packages with specific weights in two arrays which will be designed to hold the package weights of the two tracks. That much is obvious to all of us. The way we introduce this is we say first of all how should we represent the problem data? Since we will be required to compare weights of packages from each track with those in the other, the best way would be to read the respective load values in two arrays. We assume that maximum number of packages in each track is 100. So we will use integer arrays a and b of size 100 each having m and n elements respectively. So the assumption is there are maximum 100 packages but there would be less than 100 packages in each of the tracks and there could be different number of packages. Please note that to be able to find a single pair which I can exchange it is not necessary that the number of packages in each track be same. They could be different. So this is the general formulation. Please note the word used is programmed design. What is to be done should be generally obvious or people should be generally able to figure out what is to be done. So what should our program find? There may exist pairs of elements of the type x and y such that x is an element of a and y is an element of b and if these are swapped across the arrays, then the resultant arrays of the property that sum of elements of new array a is equal to sum of elements of new array b. So what I have done? I have converted the truck load balancing problem into a mathematical formula. So I have two arrays with two sets of numbers, m numbers and n numbers here. Each array will have certain sum which represent the sum of the total weight in one truck, sum of the total weight in other truck. I want to balance these and I want to swap two values. So I want to find out one element x in an array a and an element y in array b such that if x come to b and y goes to a, then the new sum is equal. This is not very difficult for students to comprehend. So one possible algorithm I say is to iterate on one array a and for each element of a which represents a weight x, find which is the permissible weight y to be exchanged from the other truck. Then iterate on the array b to find if b has an element whose value is y. So I look at the one element, say its value is x. I know the sums of both the arrays. So I know what would be the difference and what if this x has certain value, what value it should be swapped with in order to balance. Now I will search for that value in the other array. If I don't find anything, I'll go to the next element in array a. It will have a different weight. I will find what should be now the weight of the other side which should be swapped and so on. So these two iterations should get me the result. So here is a program to find out the solution of exchanging pairs such that the load is balanced between two. So I input the values of m and n and the output will be weights of one package from each truck such that their exchange will leave both trucks with same load. Here is an array. Sum one and sum two are sums of the weights in each truck. X and Y are weights which can be potentially exchanged. It is useful by the way that even when I write illustrative programs on board or if I use in slides, I should write the programs in a way in which I would expect my students to write. If the program which I show does not contain any comment, then it will be wrong on our part to expect that the students will write comment. They will if we insist but they will invariably write the program first and then write the comment. In our tutorial discussion, we actually first write only comments, leaving space for code, leaving space for declaration and so on, only comments. In fact that is considered a good way of program design. So you actually do the different parts of the program, you design them in your mind, write English comments on what that part will be and then write the code. So here is what I do. I collect the input data. After that input data, I find the sum of elements of each array. So I have sum 1 and sum 2. Now I find out the absolute difference between sum 1 and sum 2 and that is diff. I conclude one important thing. If the diff is not an even number, will it ever be possible to swap two packages? If the difference is not even, I will never be able to find it. It can be mathematically shown. If I eliminate that case, the difference of sums is not even, desired pair of packages does not exist. But if the difference is even, then I know that if X is an element of A and Y is an element of B, we need to find X and Y such that if they are exchanged, the resulting sums match. So sum 1 plus Y minus X should be equal to sum 2 minus Y plus X if X and Y are exchanged. And given an X, I have to locate Y. I have to go on using this logic. So please note that the logic itself is written as commands as part of the program. It is called inline documentation. And the documentation is not so much about structuring of the program, but also about the essential elements of the algorithm that you have implemented. Then this program is of course kid stuff. You can subsequently, all that you have to do is for a particular ith value, one value of Y is the possible replacement of T1I. Means ith element of T1, you have to find out what is the replacement. Okay. Then we say this is the sample load data in two trucks. There are four packages here. There are three packages here. And then people can show execution. We can even show execution screens, etc. The rest of it is routine technique. This is one way of doing it. The second problem I wanted to show you, actually correlates with the intention clearly is to suggest a binary search, but correlate that binary search with an iterative method of finding roots that we would have already discussed much before introducing arrays in the class. So this searching for a value in an array, this is a mundane routine stuff. You get roll numbers and marks. And given the marks, you find out the roll number, etc., etc., or given the roll number, find out the marks. Okay. I will just skip this. This is not really relevant. But I now revisit finding a root by bisection method. Finding a root by bisection method, what you do is you use a midpoint thing. Although we have not discussed it here, but all of you would be familiar with the midpoint root finding. And this problem is assumed to have been discussed before this session. So people are familiar with this. Now why am I using this in the context of introducing binary search is because of the logical similarity of the concept. But if this problem is well understood by students, then by correlating the solution of this problem with the binary search, I have found students understand and appreciate it much more easily. This was first done by Prof. Millen Soni in his lectures and I have used that approach since then. Our slides are different, but essentially that is the approach we are taking. So finding a root by bisection method, this is what I do. This is just I am recapitulating whatever root finding mechanism that we had done earlier. The midpoint search for a root, I just show them by drawing by hand something here. And then what I show is, consider x axis as discrete points. So suppose these are so many points here. Now at each of the points, there is a function value. You can I suppose guess what I am leading to. Each point is an index in the array. And the value of the function at that point is the value in the array. So I am introducing that notion by extending the midpoint search method. By saying suppose I consider x axis as discrete points, then the desired root is here, but the function values will be this, this, this and so on. And then I simply turn it over. And I say similarity with the sorted array. So here are these points. This is the 0th element, 1st element, 2nd element, 3rd element, 4th element, etc. And the function values are nothing but the values that I am looking at. 1001, 1002, 1003, 1004, 1006, 1008, 1009, 10011. These are values of roll numbers. And the roll numbers are in ascending order. So if I have to search for a given roll number, so that somebody gives me a roll number says how many marks. So that roll number has to be searched. The marks are not in ascending order, ascending order. The marks correspond to each student. But the roll numbers are in descending order, ascending order. So if they are in ascending order, can I apply this midpoint search technique here? And believe me, students lap it up because they have earlier understood the midpoint search very easily. Very often we introduce the midpoint search by calling it a dictionary search. It is a correct example. The problem is no human being ever does an exact midpoint search in the dictionary. Have you ever gone exactly to the middle first? You are searching for example some words starting with B. You will invariably open up something in the earlier part. You are looking at something with X. You will invariably go to the lower part. Because in your mind you have actually made portions of dictionary so much A, B, C, D, etc. So that is not intuitive to lead directly to the midpoint search. But this point leads to it better. Although people understand of searching in that fashion. Basically the mechanism is go forward, come backward, go forward, come backward, etc. till you hit the point. Incidentally there is a very interesting attack. A security lapse if the grant permissions in an SQL database are not properly set. You want to find out what is the salary of a person. But you do not have access to that person's salary. What SQL query you will write to determine that person's salary. And for running that what rights should you have except the right to explicitly read that particular row. It is an interesting problem. Some of you are interested in databases can ponder over it later. But I digress. So having shown this similarity I then introduce the binary search. I say low, high, mid. I said low equal to 0, high equal to n minus 1. Notice that here I actually considered low, high and mid as the points. And since later on I am going to introduce this. When I talk about the midpoint search for solving the root. I use the same term low and high there. I am going to use that term subsequently in the context of an index here. It is not necessary that you follow this method. Why I am explaining this is that it is possible to reuse a concept from our previous teaching. And connect it with a very similar or close concept. And show the previous working out that we have done while developing a solution for something. Our experience is that students lap it up more easily. Then of course there is a standard binary search. Then a slightly better algorithm etc. Remember yesterday I showed you a glimpse of how you encourage students to think about execution time. So we say execution behavior of binary search algorithm. And here what we do is we give this and writing iteration low, high, mid. We actually execute that algorithm step by step. And we show that how quickly we will converge and get the results. So here is another quiz. By the way for us a quiz is not just one quiz question. In those days quiz used to be conducted formally for one hour. And there will be multiple questions. So question 4 of the quiz was this. Given an array containing integer numbers write a program to find a subsequence with minimum or maximum sum. Please note that integer numbers could be positive or negative. So the consecutive numbers it is not that if you add one more consecutive number the sum will always increase. It may decrease. So this question makes sense. This has no direct relevance to a real world kind of problem. It is a purely mathematical or numerical problem. But I had analyzed this. So these are the possible numbers. There was a algorithm suggested by Prof. Rajit Diwan. I was trying to introduce this particular course but that was no. I don't have the execution. Okay. Here is a question. How do you encourage people to learn the behavior of programs which handle large number of elements? Or in conventional algorithmic sense where I want to distinguish. I want people to understand that order n algorithm, order n square algorithm and order log n algorithm will take drastically different time. So if I give them three programs and a data set and ask them to run all the three programs with that time command. They are able to visually watch that one program takes maybe fraction of a second. Another program takes five seconds and a third program takes half a minute. Solving the same problem. Okay. This is very useful for students to appreciate what you call alternative ways of algorithms and how efficiency in an algorithm is important. Otherwise talking about efficiency in a theoretical sense does not sufficiently impress them. Some smart people are able to relate but most of the people don't understand. Just like every one of us understands how painful it is to sit in front of a monitor when nothing is happening. So that way time is very easily understood by people. All right. Thank you so much.