 What we wish to discuss as I mentioned is a new control statement in C plus plus called the FOR statement to implement iteration by counting. Last time we had hinted at that. We will then use this FOR statement to solve two interesting problems. One is to estimate the value of logarithm of a given value, given number to the Bayesian. How many of you have heard of Riemann integral? Some people, many. So, we will be using the Riemann integral summation to calculate that value. How many of you have heard of Hemchandran numbers? None. So, that is a rather interesting problem. There are the two problems that we wish to discuss. So, first of all, finding the sum of n numbers, value of n is given as input. Now, this is different from our earlier artificial end conditions, which we used to put. You remember when we wanted to sum some given numbers or we wanted to find out the maximum given numbers, we would artificially treat some specific number as end of input, such as 0. So, we said that we will run an iterative loop till the input value rate is 0. Somebody had observed that suppose 0 itself is a value, what do we do? We said we can define a range of values and give an input which is outside that rate. Suppose we have to work with all possible values that can come in, then we did not know how to handle it. Invariably, in most counting problems, you may have a bearing on how many terms you want to add of a sequence or how many values you want to add or how many values you want to handle. When such is the case, you will know that number in advance. Suppose that number is n, n could be 5000, 1,200, whatever, but once you have the n, you know exactly how long the iteration has to run. It has to run once, twice, twice, four times, five times, etcetera and n times. Once you have finished n times, you are done. Now, if you look at the vile control structure, the vile control structure says that as long as a given condition is true, the iteration will continue. Now, you have to recast this condition mechanism in the context of some counting that you will have to do for n times doing that iteration. Ordinarily, in human life, how do we do counting? You take a child, you say you have to do this five times. So, every time the child does it once, you will say one, and you say two, and you say three, four, five, done. So, basically, the child is setting up a counting mechanism. Every time he does something, he increases the count by one, then two, then three, then four. We need such a mechanism. And what is the child's way of saying whether he has to continue or not? He will look at the current count. So, it is four. It is less than five. I have to continue. In exactly the same fashion, we need to execute a block of statements n times. Then we need a counting mechanism where we say we set some count equal to one arbitrary initial. Then we execute that iteration block, increase count by one, and repeat the block. Again, execute that block, increase count by one, and repeat it. Obviously, you have to keep repeating that block till the current value of count is less than or equal to n. When we finish it nth time, then we will get out of the iteration. How do we set this up using a wide statement? So, I have shown here some statements in dark blue. Notice that earlier we did not have anything called n. Now, we will have n. I have put it in blue, but not c in greater, greater n. Why? Because n need not always be obtained as input. I could also assign a value. n is equal to 53. So, I have to do something 53 times. I can even compute the value of n from some other expression, which based on some previous computations in the program say that I now have to do this iteration so many times. There are multiple ways of obtaining n, but having obtained n, I now have the counting mechanism statements. So, one additional statement which is artificial completely, there is nothing to do with our program, is inserted there called count equal to one. Next, it says while count less than equal to n, do the following. So, there is a block of statements which are to be repeated and after executing that block, I want to say count is equal to count plus one. Now, when I come out at the end of that block, very obviously I will go back. That is the nature of while and when I go back, while we will check the condition again, but this time count would have become two. I reiterate that again, again count will increase, I will go back, count will become three, then it will become four. Sometimes it will be equal to n, but still nth iteration I have to continue. So, I will complete it, count will be added by one and it will be greater than n. Then I go back this time, the Darvahn will say sorry, count is not less than equal to n, get out and I will get out. So, it is a very simple mechanism. Please note, I require certain artificial statements to implement iteration control by counting. The first artificial statement is assignment to n. The second artificial statement is actually the condition check. The condition has nothing to do with what you are computing. It has to do with counting. So, it is count less than equal to n, then carry on, otherwise get out. And finally, after the iteration block, the last additional artificial instruction is incrementing the count. So, I increment the count by one. There are variations possible. For example, suppose I want to execute this iteration n times, I start with count equal to one, but I say whether while count is less than equal to two star n, some arbitrary. I still want to execute the iteration n times. What will I have to do? Well, if I say count is equal to count plus two, count will increase by two, one, three, five, seven. There could however be a problem depending upon whether n is odd or even. You can just cross check that, but there are variations possible. We use this now to find some of n numbers. So, this is the program. So, it is starting with definition of integer a. Sum is equal to zero. It is initialized. n is defined. And additionally, I am defining in count. Notice that the first statement says give number of values to be added. I input n. I set count equal to one. While count is less than equal to n, what do I have to do? Read a number and add it to sum. So, that is what I am saying. Enter the next number. Input a. Sum is equal to sum plus a. That strictly is the iterative block that I have. But additionally, I say count is equal to count plus one. So, you agree that this program will do the needful? It will add n given numbers. This trick can be used to solve any problem. So, these are list of problems. Adding a fixed number of terms of a sequence, calculating average marks for a given number of students, calculating factorial n. This factorial n is an interesting problem. You all know the definition of factorial n, right? Agree? This is the definition of factorial n. The way we calculate it iteratively is very similar in principle to the way we accumulate sum. Observe that in the previous example, we had a very low object called sum and we were adding values to it. So, every iteration sum was changing. By the way, that is the reason why it is called iteration and not repetition. Repetition is you merely repeat the same action again and again. Iteration is where every repetition causes something to change. That is what is iteration and that is why the word iteration is. Now, here what should change? So, what we can do is we can have a location which will calculate, let us say we call it fact and we will have another location which will have each of these terms one by one and I will keep multiplying that with fact instead of adding it to fact. Then I will get the cumulative product. So, I need to find the product. Now, just as we start sum equal to 0, we should start fact equal to 1. Let us see if we have to find out factorial 4, what will happen here? I have to now multiply this one successively by 2, 3 and 4. Where will I get these numbers? Well, it so happens that my counting mechanism is actually generating these numbers for me. Count is initially 1, then 2, then 3, then 4, then 5 and so on. So, if I put count, let us say this is my count. Initially, this is 1. In the first iteration, I multiply 1 by 1. It is an artificial, this thing nothing will happen. This 1 will be replaced by 1 actually. Next, I will increment count by 1 to 2 and in that iteration block if I multiply 2 with fact to get a new value of fact, I will get 2 here. Next iteration, the count becomes 3. Now, I will multiply 3 with fact and replace the value of fact. So, effectively my statement is going to be fact is equal to fact star count. Agreed? That is how just as sum is equal to sum plus a value, fact is equal to fact star count and the count is internally generated. I do not have to depend on it. So, next time this 3 will become 4. I will multiply this 4. Sorry, first 3 and 2 was multiplied. So, this would have become 6. Now, count becomes 4. 4 gets multiplied by 6. It becomes 24 and suppose I was to calculate 4. This 4 will now become 5 and next time when I come back for iteration, it will say sorry 5 is greater than 4 get out. So, that is how I can simply calculate factorial. Here is a program which does that. So, you can see given number whose factorial is needed I input n. Now, I set up my counting mechanism. Count is equal to 1 while count is less than equal to n. Fact n is equal to fact n star count. I have chosen to use the name fact n to represent factorial of sum value n and I add 1 to the count. You all agree that this iteration will correctly calculate factorial n. So, it is very simple except that I need these artificial statements for setting up counting mechanism. In exactly the similar way if I want to keep on doing some counting till n, I look for some simpler mechanism. Unlike those abbreviations that I discussed where I advised please do not use them. Here is an abbreviation which is actually quite useful and very extensively used in C plus plus program. This mechanism is called the FOR statement. This FOR statement is a very special statement. What that does is it permits you to define three things in one place in your program. After all in counting mechanism what do we need? We need an initial value. We need a condition for continuation and we need an increment. If we can prescribe all these three things typically at the beginning of the block and keep in mind the logic that while these three things are specified the first thing is done before you do anything in this block that is initialization of the count. The next thing you do the checking of the condition every time you come to the iteration and the last thing that specified which is the increment you do only when you completed one iteration. So this is what it says the instruction permits us to define these three things and these actions are defined at the beginning of the iterative block. So this is the nature of the FOR statement. FOR in bracket this is important to note you write three things separated by semicolons. The first thing is count equal to one typical specification for initial action. The next thing is the condition for continuation of the count. So that is count less than equal to n iteration will continue as long as this condition is valid and the last specification is count plus plus which is count equal to count plus 1. Remember I said that standalone such post free increments can be used whether you say minus minus a plus plus count plus plus it means the same thing here say standalone and it is indeed used in this sense in almost all four statements that you see in professional program. It is a simpler thing but do you see now although I am writing all the three things in one statement at the beginning of my iterative block those actions do not take place at that point. The actions take place exactly as depicted by the earlier do while implement. So that is why the explanation is written it initializes the count before beginning the first iteration at the beginning of each subsequent iteration we check that condition and permit continued execution of the iteration only if that condition is satisfied and at the end of every iteration it will increment the count immediately after the iteration and go back. So that is the simplest implementation of iteration by counting here is a program written to add n numbers using for statement. Notice the program is very similar to what we had seen earlier which we had implemented using do while but we had artificial constraints on what number to be input so that 0 can be taken as n. Then today earlier we saw how by counting we could implement this algorithm using while statement. This is an implementation using for so you will notice that after reading the number of numbers to be read here and I am actually setting up a for iteration. So for count equal to 1 count less than equal to n count plus plus can you tell me what this will do now in the translated algorithm it will set up count equal to 1 just outside this iteration that statement will be executed next the conditional we change is count less than equal to n of course 1 may be less than equal to n because n would be 5, 10, 5000 whatever and if so the iteration will be executed. What does the iteration say enter the next number and it is added to sum. Observe that what we were earlier doing artificially by writing count equal to count plus 1 in our do while implementation need not be written because that is part of the specification of the for statement the third part here. This count equal to count plus plus will actually be executed at this point in the algorithm after completing it. So how many numbers do you wish to add inside that enter next number to be added yes because pre increment or post increment does not make any difference it is going to be a standalone operation no no not after all that after a particular expression is evaluated the expression is that itself. So the moment that is evaluated the value changes after post and pre does not apply to block off statements post and pre applies to the evaluation within an expression when the only thing in the expression is that the value will change whether it is pre or post yes I will see that later but the point is what we are stressing at this juncture is a simple counting mechanism I start with one I want to count one two three four five six n this is the mechanism agreed. So let us keep that in mind everybody understands now how to set up an iteration an important point here was the algorithm was wrong because there is no see out number which is not required because when you type it you see it yourself you are going to type it ok. The point is we are discussing counting mechanism and unfortunately we concentrated on n number of other errors which were there in the program but it just shows that you have understood how counting works can I take it for granted fine look at this program this finds maximum of n given numbers and again it is very similar to finding out some except that we are not adding anything to some but we start with a max number notice that we have n numbers given in this. So I take the first number outside the iteration to set up the right value of max and notice that there is no artificial condition here which says that find out maximum of given n positive integers or something the number that comes in could be any number and therefore I cannot make any assumption on max. So I read the first number assign it to max now I have only n minus one numbers left to read and that is why the iteration specifies for I count equal to one count less than equal to n minus one count plus some of n natural numbers in exactly the same way that we calculated factorial n I do not need any input. So I simply set up an iteration for I equal to one I less than equal to n I is equal to I plus one sum is equal to sum plus I. So instead of count I am using an object I whose value is changing because of the first statement and I am adding in every iteration that value of I to count. So count will start with sum will start with zero then one will be added two will be added three will be added up to n and when n iterations are over the control will come out printing the sum in exactly the same way the factorial can be found. Notice that in the previous example of the file iteration when we implemented factorial n we actually started with fact equal to one but we multiplied that one by one again which was not necessary. Since I presume that n is larger than two because you would not use a computer to find factorial two hopefully. So it will be a number larger than two three four so I start with fact equal to n factorial equal to one this time I am using the name n n factorial and I start the iteration count from I equal to two to n. Now what will happen is n factorial will get multiplied by the current value of I which is two at the beginning then three then four then five I will complete up to n. So this is yeah it will not have any impact how many times I have said that whether you write plus plus I or I plus plus as long as it is a standalone statement it is not part of any other expression there is no difference in the implementation plus plus I is same as I plus one I is equal to I plus one I plus plus is also same as I is equal to I plus one there is no difference between these two when it is a standalone statement it has an impact only if it is part of a larger expression three x square plus plus plus I minus something something then only it has a different meaning. So this is a standalone statement although it is part of the four specification please remember four gives three specifications the initialization part the condition part and the increment part and these three are independent specifications they have nothing to do with each other. So it is not considered part of a larger expression and therefore there is no confusion this question is important because you will find in many books programs which will use the counting mechanism as minus minus I or plus plus I and some others which will use as I plus and therefore some confusion could be possible but it should be avoided there is no confusion both will implement a increment by one is that is that ok now right now we come to the general format and semantics of the first statement but actually what you have these three parts of the specification are not necessarily three simple statements like count equal to one or count plus plus the initial part can be a series of specification called unit group the condition however is only one that is the continuation condition it can be complex condition and the last part is called increment group in that increment group several statements separated by comma can also be used in the initialization group several statements in they have separated by comma can be used a comma is a separation operator in C plus plus and it effectively works as equivalent of semicolon it finishes one statement and starts the next state please note that in the increment group if you have prescribed two three four five actions separated by comma one of those actions could still be plus plus j or j plus plus and that will still be a standalone instruction so that it will not have any impact on the counting you can read these variations at home and figure out what they do particularly you see the last one for in bracket semicolon semicolon bracket close what am I saying initial condition none condition to be checked for continuation none increment none nothing this is actually an infinite loop this is equivalent of saying while one carry on doing so this is equivalent of a infinite loop that is the C plus plus interpretation there are other multiple options shown for example if increment condition is increment statement is not written at all nothing is incremented so obviously you have to do something else to change the condition otherwise you will never get out of it anyway since most of you know Riemann integral I think we can go through this particular problem very quickly we observe that the computer cannot do integral calculus it can only do computation of value and therefore if I have to evaluate an integral I go back to the basic definition of integral integral of a function is actually the area under the curve between the two limits where I want to integrate now area can be approximated so I will have a large area for example this is my function calculate the area under the curve y equal to 1 by x you understand what is significance of y equal to 1 by x the area under this curve between 1 and a is actually the natural logarithm of a so log of a to the base e is nothing but the area under this curve and I have to calculate this area now I cannot do the measurement and the curve and so on so forth in terms of computer calculations I will have to calculate this area by approximating it I assume that there is a series of rectangles that I have and I keep adding the area of each rectangle so if I have 20, 30, 50, 100 rectangles then the sum of all the areas of those 100 rectangles will be an approximation of this area people used to do these things manually and it was impossible to calculate say sum of 1000 rectangle areas like this but machine can be used to do that so this is what I wish to do these are the rectangles whose areas I want to calculate now you can see that if this is my rectangle then this has some height and some width if all rectangles are same then I know what is the width suppose there are 100 rectangles I decide to use between 1 and a what will be the width of each individual rectangle a minus 1 by 100 a minus 1 by 1000 whatever that is the width that is fixed the height of any rectangle suppose I measure the height at this point now if I know this which is x coordinate then I know 1 upon x is this point I can do that for every such point so I can get the height and therefore I can get the width into height which is the area so this is what is shown here if there are n rectangles width of each rectangle is a minus 1 by n and to find out the height first I find out the x coordinate of a rectangle now here the x coordinate of first rectangle is 1 because this is the x coordinate of first rectangle x coordinate of second rectangle is 1 plus w third rectangle 1 plus 2 w in general for ith rectangle the x coordinate will be 1 plus i minus 1 into w agree very simple derivation and if this is the value of the x coordinate what is the height here 1 upon this so that is what we have done height h of ith rectangle which is equal to 1 by x which is 1 upon that entire expression so this is simple derivation no problem now I know the height I know the width of ith rectangle I have such n rectangles can you not see that I can set up a for iteration in every iteration I take 1 rectangle where i is 1 2 3 4 5 n and keep on adding to some it is like summing n values except that values are not being input but our values are being calculated area of ith rectangle is explicitly counted as w into h which is w into 1 by x which is w into 1 upon 1 plus i minus 1 into w this is the simple derivation and my program therefore automatically writes itself first I have to decide how many how many rectangles we say 1000 if it is 1000 width of rectangles taken together is a minus 1 therefore width of each one is a minus 1 by 1000 x coordinate is this height is this an area of the rectangle is this this is just a consolidation of whatever we have discussed so far nothing new in this law it is useful however to write such things before you start writing your program so you know exactly what you want to and now we write this program so look at this program observe that I am now generalizing the program to say I add 1000 rectangles like this so what am I doing area is 0 I define w I calculate w as a minus 1 upon 1000 I of course input the value of a then I set up the for iteration what does that for say for i is equal to 1 semicolon i less than equal to 1000 semicolon i is equal to i plus that means please do the following block of statements 1000 times starting with the value of i equal to 1 then i equal to 2 i equal to 3 etcetera it is important what value i takes because we are using i explicitly in our computation what is the computation area is equal to area plus w star 1 upon 1 plus i minus 1 star w exactly the same formula that we had worked out nothing else is to be you can wonder later on whether you will get the correct calculation because there are a lot of 1 upon this and so on and if an integer division occurs somewhere your son you will notice however when you examine this that no such problem will happen because w is defined as float and therefore while i minus 1 will be integer when it is multiplied by w it becomes float when that is added to 1 that becomes float and when 1 is divided by that whole quantity that 1 will become 1.0 however those of you who wish to be more meticulous might write every one as 1.0 just to be on the safer side there is no harm in doing but notice that my entire program although I am adding areas of 1000 rectangles does not have 1000 statement and if I wanted the machine to add 1 million rectangles my program will remain the same what will change the 1000 would it not make sense then to further generalize this program and say that instead of 1000 I will input some n from the user say how many how many rectangles you want to add tell me in fact it may be interesting I will display the results on Moodle by of this program where I execute it for 100,000 and 10000 and you can compare what is the value of actual log with the value that you get there is a further interesting what should I say modification possible because when you add a rectangle area observe that a portion of the area above the curve also gets added now you can take the height of that rectangle not to be the starting point but the end point but then you will lose so instead of taking this height which causes this much portion to be extra you can take this height but that will cause reduction in that so one suggestion is you calculate the area of the rectangle taking the first height into account calculate the area of the rectangle taking the second height into account and take the average because that will give you half of the upper rectangle portion which is likely to be closer to the truth will it make a difference not if you are 1000, 10000 rectangle you note that when you add more rectangle it is not that you are adding some meaningful portion of that curve at a later stage but you are actually making the width of every rectangle smaller and therefore the approximation will be more accurate will come in more on but do you understand how easy it is you can solve the problem of any integration between limits using this approach that is why this is the Riemann integral is important because it can be used in multiple places we will quickly look at this interesting problem the another problem called Hemchandra numbers the I am introducing this problem is not the original Hemchandra problem the problem is suppose I have to build a wall of four feet long and in that wall I have to lay bricks but I have bricks of two kinds one is one foot brick and the other is two feet brick so short brick and a long brick the question that is being posed is in how many different ways can I lay the bricks to make four feet length so here I have written the possibilities I can choose all four bricks of one foot long so I have one one one one I can choose the first brick to be two feet long and the other one short I can choose one two one I can choose one one two or I can choose two bricks which are two feet long agreed so these are the various possibilities how many possibilities do we notice here one two three four five actually in exercise in combinator but suppose I ask this question in how many ways this can be done if the length is three feet or if the length is five feet I have written down here the combinations if I have a three feet length wall then any line of bricks will have either one one one or will have two one or will have one two there is no other possibility for four feet wall we have already listed and for five feet wall I will have more combinations these are exhaustive combinations they have been written however in a special fashion so that you can identify some relationship between the previous possibilities for three feet wall and a four feet wall in the five feet wall one of those possibilities observe this in a three feet wall and a four feet wall I have all combinations of three feet wall with one one foot brick added to it that makes it four feet wall I have additionally some combinations observe the possibilities of the five feet wall I have all the possibilities of the four feet wall and added one foot brick there so that all of them becomes the possibilities for five feet wall what are the additional possibilities in the five feet wall one one one two two one two one two two notice that these three possibilities have a larger brick at the end but all the three possibilities if you remove that larger brick come from the possibilities of three feet wall three feet wall had one one one two one one two if I add two to it I get all the possibilities of five feet wall so I notice that possibilities for five feet wall is actually equal to the total number of possibilities for four feet wall by adding one foot brick plus the total number of possibilities for three feet wall by adding a two feet brick simple this is actually called a recurrence relation the original problem of the Hemchandra was a musical problem Hemchandra lived in 11th century in India and he said poetic meter which is made of short syllables and long syllables so long syllable is two beats and short syllable is one here is an example of poetic meter somebody some of you might know this poem Yakundan Dutushara Haradawala say Sardul Vikridichand so these are the beats that you see there what Hemchandra posed the problem was if I am designing a poetic meter with eight beats how many ways are there to fill up these eight beats that was his mathematical problem and then he gave a solution in 1100 something he gave a solution he says by the method of Pingal it is enough to observe that the last beat is long or short Pingal had written this mathematical sutra he lived 200 before Christ 200 years before Christ Hemchandra was 1100 years AD so this is an important point which Professor Ranade made his lecture three years ago and I make it every time copy if necessary and if permitted but give credit is a very fundamental axiom of professional life that is why you will find that when you write research papers and you quote whether somebody else has done something you always quote you cite that reference saying this I read it in this book or this place so that is I am not taking the credit somebody assisted is to be given Pingala did by the way a whole lot of things and several of his sutra have subsequently been incorporated the point of the Hemchandra solution now is that if by method of Pingala if I observe what is the last bit is long or short I can get the classification an 8-bit pattern can be made of take any 7-bit pattern and add a short bit or take any 6-bit pattern and add a long bit just like the brick problem and this gives us a recurrence relation number of 8-bit patterns is equal to number of 7-bit patterns plus number of 6-bit patterns but I can't solve this problem where I must know what are the number of 7-bit patterns what are the number of 6-bit number of 7-bit patterns in turn is defined by number of 6-bit patterns and number of 5-bit so if I start backward I say hn which is the total number of patterns with n-bits h8 will be h7 plus h6 in general hn is equal to hn minus 1 plus hn minus 2 this is called a recurrence relation and we need to know h7 and h6 for which we need to know h5 for which we need to know h4 etcetera so you start always from the first thing h1 number of patterns with one bit only one a short bit h2 number of patterns with two bits there are exactly two patterns possible ss and l nothing else is possible now you know h1 and h2 presto h3 is equal to h2 plus h1 which is 2 plus 1 h4 is equal to h3 plus h2 h5 is equal to h5 etcetera hn will be equal to h3 now I have three values one value is the current hn which I want to compute one value is a previous value and one value is the next value which I want to compute so I have next current and previous suppose I set up an iteration in which I have these three locations current previous and next I initialize some value to current some value to previous which is h1 and h2 I can now calculate next which will be h3 and if I now set up an iteration but before every iteration I keep shifting the value I will take the current value and put it in previous next value put it in current and go to the next iteration and if I keep on doing this I can generate n numbers in this m Chandrasek so this is the program the integer definition is used to define h priv and h current and there is a variable called h next I hope you understand the significance of these three names h priv is a previous value h current is the current value and h next is what I have to calculate these three locations initially contain one two and unknown one and two represent h1 and h2 respective okay now I set up an iteration for i equal to 3 i less than equal to n i plus plus I already taken n as input suppose n is 10 then the iteration will begin with i equal to 3 what will it do it will calculate h next is equal to h priv plus h current these are three locations by the way they have nothing to with i whatever with the value of i there will be only three locations what will be the value of h next at this time h next will be equal to first iteration the first iteration what will be the value here h priv is one h current is two so two plus one three h next will have a value three now for preparation of the next iteration I don't have to do anything about counting that is being done but I need to shift these values so that next time h4 is calculated next time h5 is calculated and all these calculations happen in a location called h next is the is this clear so that's it these numbers generate mathematics from poetry and represent a extremely interesting model you might have heard of golden ratio successive terms the ratio of successive terms tends to that limit these are commonly known as Fibonacci numbers how many of you had heard of Fibonacci numbers all of none of you knew about Hemchand it is not uncommon to forget your own heritage when you start believe believing that anything great can only be done by ferrants and not by us you never look for old mathematical exploitation how many of you have tried to read just read about whatever happened in this country whatever happened in the entire Middle East Fibonacci has admitted that he learned these things from an Arabian scholar in North Africa to whom he had gone to learn the Hindu numerals and such things the entire algebra for example was developed by Arabic scholars all of that is now treated as Western knowledge nothing wrong in it a whole lot of knowledge has been added by the scholars all across the world including Western scholar so the idea is not to be a little any effort but when your own heritage is so strong should it not interest you to know what is that and you don't have to read big history book just go to Wikipedia just ask Indian mathematics just for your own interest I would suggest you read it I will close this thank you very much