 Last time we looked at a program which found out the maximum of five given numbers using an iterative solution. We specifically used a statement called vile. That vile statement permitted us to specify a simple instruction which could be executed again and again We shall be dealing with the vile statement in much greater details today, not from simply a perspective of understanding how vile works, but of designing programs which means designing vile statements, how to write vile statements, how to use vile statements for iterative solutions. More specifically since we are talking about a repetition, we must know what to repeat. So we will learn designing of the repetition block. What is the block of instruction which must be repeated? Then we must know when the iteration must terminate. You will recall that in the sample program that we discussed last time, we had written vile 1. In bracket we had written vile 1 and many people were wondering what is 1 because we have stated that there should be a condition inside those parenthesis. Actually one or any other numerical value is also a condition as I told you, a numerical value of 0 is considered to be false in terms of relational comparison and a non-zero value is considered to be true. So when we write 1 wherever a condition can appear, it means that condition is always true. So essentially we constructed an infinite iteration that type. That is not the proper way. Whenever we seek iterative solutions, we cannot have a computer go on and on and on doing those iterations in finite. So we must understand how to build a terminating condition to terminate our loop. Finally, while the iteration is prescribed correctly by block of statements which are to be repeated and the terminating condition is stipulated correctly, still there is one initial entry into that block. That entry happens when the statements prior to the block are executed and then you encounter the vile statement for the first time. Now at that point in time, there ought to be certain proper initializations done to ensure that the entire program works correctly. So in short, for any iterative solution, we have to worry about designing of the repetitive block, designing of the terminating condition and designing the proper initialization. We will also see more numerical problems requiring iterative solutions. Finally, we will look at another way of prescribing iteration using some kind of a counter. There is another statement in C++ which permits us to define iterative execution or repetitive execution by saying repeat this five times, repeat this thousand times, repeat this ten times. We shall see how exactly that specification is given. We will only look at a sample today but we will discuss more details about that four statement and other associated variations of iteration in the next week. So we revisit the problem that we had solved and which you had seen in the handouts, namely how to find out maximum of five integer values. We had commented that we could, for example, just choose arbitrary variables a, b, c, d, and e. Read all the five values in these five variables and then assume the first value to be the maximum arbitrarily and then compare this maximum with each of the subsequent values changing max wherever required if any value turns out to be greater than max. At the end of these instructions, I will just print out the max value. We had ensured that this program works correctly but we had commented that if I have not five but five hundred values, will I be writing five hundred variable names? Will I be writing four hundred and ninety-nine if conditions? The program will become very unwieldy but I do want to compare all five hundred numbers if I have to find the correct maximum of five hundred number which means I must have a mechanism of instructing the computer such that my instructions are small but the actual workload is much more. Now that can be done if I have a mechanism to say that look here is a block of instructions, keep on executing it repeated. The point is that instruction block will have to be identical but it must deal with different values in every iteration. How do we ensure that? So first we notice that although we are reading values in A, B, C and D and E, if the problem is of finding out only the maximum of given numbers, the identity of individual numbers need not be retained in my memory later. After all I am just printing one value max. So whatever the individual values as long as I read them and I use them for comparison it is okay. Consequently I now see that while these instructions are similar they are not identical. This one says B greater than max, max equal to B. This one says C greater than max, max equal to C. So I cannot take one instruction and just iterate around. So I generalize these instructions and observe that since I don't need these individual values I can use the while statement. Incidentally the while statement in C++ sets up an iterative loop of this guy. There are earlier statements. Then there is a while statement with condition. Then there is a block of statements which is to be iterated. There is a end of block. So while will always be written as a good practice with an opening blast and a closing blast. And this will be followed by the subsequent statements. These are the rules. The condition written in parenthesis is a relational expression. It is evaluated at the beginning of each iteration. And if the value of the condition is true then this block is executed. So you can imagine the activity to be almost like if condition do something. So if the condition is true this will be done otherwise not done is the implication in F. However the difference in while is if the condition is true you will not only do it but at the end you won't go out of the loop but you will repeat that. So if the value of the condition is true the block is executed and the control goes back to check the condition. That is what causes repetition. Otherwise that means at any time if the condition is false then only you will come out. So that is the implication of while. While the condition is true keep on executing the block repeatedly is the mean. And it is not blind execution. At the beginning of every execution check the condition. Obviously then something must happen inside the block which will change the condition sometime or the other. If the condition never gets changed the block will become infinitely executed. And that is the reason why we have to choose this condition rather carefully. We now generalize our problem of finding out maximum of fine numbers. We say we want to find out a maximum of last number of positive integers. We still assume positive integers. We will then generalize this problem further. But we say that I might give 5 numbers, I might give 20 numbers, I might give 200 numbers, 500 numbers. Now I will ask you one question okay you will give me so many numbers but when will you how will you tell me that you have no more numbers to give. So we agree on a protocol that to my program you will keep giving numbers since they are all positive integers by your definition of the problem I will expect you to give a number zero at the end. Therefore I expect the input numbers to be given one after another. Incidentally when you say dot slash a dot out after compiling a program and the program starts execution that is the time when you feed input value because that is the time your C in statements are being executed. Now the way you give input values and the way C in statements are executed they are sort of one to one correspondence between elements which are read by C in and the values that you give. So if you say C in greater greater a one value is expected C in greater greater a greater greater b two values are expected C in greater greater a semicolon sometime later C in greater greater b two values are expected. The point is these values can be given one value on a line press enter another value on a line press enter or you can simply put those values one after another with some blank in between as long as the value does not contain a blank in between that will be a single value. Consequently you can give an input just as a single line like this 25, 13, 47, 12, 92, 265 etc etc and when you are finished with your numbers you can give zero. So this will be the input that you will give me and I am supposed to write a program which will read all these values find out the maximum amongst all values. Obviously here the maximum should turn out to be 265 and we want to use the wild statement in C++. So now we look at how we will construct this program we don't have a readymade program to look at we had looked at a sample but we are now going to construct a program that means when we are designing we are synthesizing something we need to know what we must do to implement to correctly write the wild statement or correctly use the wild statement the wild statement can be written correctly but whether it's used is correct or not. Now that usage as we saw just some time ago requires three mandatory things mandatory is compulsory. You must design the repetitive block correctly because that is the block which is going to be executed again and again and again. Then you must define a terminating condition properly because that condition is going to be evaluated at every iteration. If you don't design it properly you will end up in an infinite loop or you may end up not doing anything at all because first time itself the condition is false you will get out and finally first time when you enter the wild loop you want to ensure that internally things are executed properly all variables are initialized etc a job which must be done before the wild statement is written because it is with those initial values that the wild loop will be entered for the first time. Consequently the third mandatory thing is to design initialization. We will look at all these three in the context of this problem of finding out maximum of so many numbers. So we recall the program which we wrote for finding out maximum of five numbers we have this C in B if B greater than max max equal to B C in C C greater than max max equal to C and so on. What we were doing basically is that we are comparing a number with max and if we found it to be larger than max we are assigning its value to max now that is the crux that is the action which we are repeatedly executing and therefore we can think of the general statement of this guy not a b c d e but just any number x actually I could have written a greater than max also I could have chosen any name the name doesn't matter the point is I want to compare max with a value and if that value is greater than max I want to reassign max that value that's it this is the action I want to repeatedly execute but a new value of x must be used in every iteration I can't keep comparing x same x with that value consequently what I want to repeat is not just this instruction but an associated input instruction which must be executed just before it so I saw what I want to repeat is I input a value and compare that value with max if that value is greater than max I reset it this is the block which I would like to repeatedly execute every time I come here I take a new value compared with max greater than max reset max if not don't do anything but go back again iterate iterate iterate you will agree that if I wrote these two statements as a block and repeatedly executed them I could find out maximum as many numbers as I give as input so we have designed the repetitive block here we start writing a skeleton of our program since so far we have encountered two variables in our design x and max we just start with int x comma max then somewhere down the line we write leaving some space we write these statements which we want to execute again and again and again see in greater greater x if x greater than max max is equal to x you agree that these two statements we have written like that and what I have shown here is an artificial boundary sort of just to indicate to us that this is a block of actions to be repeated like this however we note that we don't want to repeat it infinitely that is what was happening in our earlier program the sample program that we saw we instead want a condition to be checked here at the entry point of this iteration something like this if x is not equal to zero then only you do all this come back again check if x is not equal to zero then only do this the moment x is zero get out why x is zero because we have agreed that last value that you will give me will be zero so I expect all other numbers to be positive integers and when you give me zero I don't have to do anything with it I have ended I have done all my work I get so the condition of termination of iteration will generally be hidden in the statement of the problem when do you want to stop when do you want to terminate and why whenever you want to terminate the negative of that will mean that you have to continue safe at x equal to zero you want to terminate x not equal to zero should be the condition for which you want to continue so we have set up a terminating condition first job identify the iterative block second job identify the terminating condition now we check whether any initialization is required we look at the wild statement of our program so my program now will be while x not equal to zero inside this block seen greater greater x if x greater than max max equal to x come out there will be next step I do not know what the earliest statements will be I do not know what the next statements will be now obviously the next statement is print out the maximum and finish of go home what should be the initial statement on the face of it I don't seem to require any initial statement because this repetitive block execution is adequately powerful to solve my problem but we now examine that if we did that for the first time when this iteration is entered is everything working all right when the block is executed for the first time several values are undefined imagine when you are coming here earlier statements are doing nothing significant now when you come here first problem while x not equal to zero now what is x we don't know we have not initialized it please note that x would be read inside the block and therefore subsequently x will have a proper value but when I come here for the first time x is indeterminate I do not undefined now it may so happen that the previous friend of yours who executed some program left zero in a location which got assigned to your x so the moment you get it oh x is zero get out nothing will happen you don't want that so you want you notice that this is one problem what is the other problem even you come in you are comparing x greater than max what is max we don't know suppose max was set to some two to the power 30 not a number which you are giving in that way what will happen the first number that you read x will get compared with that number in all probability you will get that arbitrary value as the maximum so we notice two things initial value of x is not defined and initial value of max is not different now please note this we are not just analyzing we are also understanding the construction mechanism we are learning design and what is the design principle once you have written a block once you have written a while statement go through all statements in the while block including the condition manually one by one but do not look at what will happen in a dynamic equilibrium and the loop is executing just concentrate on what happens for the first time for the first time x is undefined for the first time max is undefined this is what I should notice and obviously I must do something about it by initializing them to appropriate values before the while statement isn't that means the earlier statements which I have indicated here ought to correctly reflect the appropriate initialization of extend max before I enter the loop here is an attempt after all what is required what is required is ultimately I am going to read a number here and if I am giving 20 numbers all each one of those 20 will be read they will be compared with max so I really don't need the my number game is properly played what I need x for is that initially x not equal to 0 is what I am testing so let me test x to some value how do I set x to what value can I set x to 200 no because if I do then all my values if they are less than 200 I will get showed up so I must set x to a value which is not likely to occur in your input numbers then only I can guarantee that it will not affect the correctness of my algorithm now since we have said finding out maximum of given positive integers we know negative numbers will not happen in your input and we also know that every positive number is greater than a negative number consequently if I set x is equal to minus 9 9 9 9 9 arbitrary some negative value then it will ensure two things one when I come here x will not be zero and therefore I will execute this block and second it will not impact the correct determination of max because every value that I read actually from you will be greater than minus 9 consequently I decide to put this but I should write a comment saying that x is set to some negative value arbitrary second I have to initialize max I have noted earlier what should be the max initially again it could be any negative number it could even be zero because all values that you are going to give me are positive values so I could have said max equal to zero I could have said max equal to minus 10,000 minus 1 whatever I might as well set x set max the same negative number which I have put here again this is an arbitrary decision and this is the point in designing algorithms and designing programs for a given problem there is no unique program as a solution each one of you can write a variation which may be different from others we shall later on see how do you distinguish between programs whether can you define some programs are better than others and so on in the lab this week there are some problems and some pro example programs given which force you to think and compare different programs which do the same thing but they are written differently here is the case so is this is this understood if I did that then the flow chart for this program would become like this notice I am starting with x equal to minus 9999 and I am setting x to max so max is set to that value first time when I come x has a value is it not equal to zero yes so I come out here read the input value for x okay note that initial value now is obliterated the moment I read x gets a new value now which is the first value that you are giving me as input that value gets compared with max again since max was a negative number initially the first value itself will change max because any value that you give since it is a positive integer it will be greater than max it will be true you will come here and you will set max to the first value after that you will go back again if this value is let's say 12 12 is not equal to zero you will come back again read the next value let's say it is 5 max would have been set to 12 is 5 greater than 12 no you will come back again go back the current value is 5 is 5 not equal to 0 not equal to 0 will come in read the third value and so on the moment you have read the value 0 the moment you have read the value 0 it will come out here is 0 greater than max most certainly not because max would have been set to one of the positive integers that you have given so far nothing will happen to max you will come out here but now x is 0 and this is the time you will terminate the loop and come out agreed so this solution seems to work so what we have seen is not just analyzing how while statement works but understanding how to use while statement to construct programs because that is the that is the part we will be doing all the while we will be writing programs to solve a problem here is an iterative solution this is a complete program this this program the slides will be put up on the web both on the course homepage and moodle so you can look at these later now do you make sense out of this program because this is not a program which has descended from some example this is a program which we have constructed right now we write include iostream using namespace std and int main as usual without understanding the implication but we take it as gospel truth the c compiler will figure out what it is the main program that we have written is int x max x is minus 9 9 9 9 max is equal to x while x not equal to 0 c out enter the next positive integer in bracket 0 to n notice an elaborate string which has been written incidentally this is the string constant 0 5 x or any such thing that you write here less than equal to etc are not interpreted by your c compiler at all I have stated this earlier whenever in c out statement you write double code and write any crap and write another double code that entire thing is completely ignored by the c compiler c compiler is not going to look at inside what is there in the string but whenever c out statement is executed this string will be faithfully reproduced on your monitor so this is essentially a mechanism for you to make sure that your program gives you appropriate messages when the program is executed this will happen at the execution you will agree that this is a more appropriate message than just saying give a number so it is saying give next positive integer and 0 to n so it also suppose you write this program and give it to somebody and he doesn't know how to end the program will keep giving the number we will just wait say abhi what should I do I have no more number this is an indication that 0 should add anyway and then inside this block I input this x x greater than max max equal to x notice a slight difference from our design to the actual program in our design we had only said c in greater greater x if x max max equal to this c out statement is actually a further elaboration of the c in state so the c out statement should not be considered as a separate thing we are merely helping ourselves to give the corrected this curly bracket closes this opening curly bracket please note as I said all brackets including parenthesis or curly brackets are matched from in our most out almost so this corresponds to this this corresponds to this and this corresponds to this you must have matching brackets in your program otherwise the compiler will actually get completely confused you agree that this program will work correctly please note now that this program the number of lines of code that we have written how many lines does this program contain physically 1 2 3 4 5 6 7 8 9 10 11 12 13 13 line program it is capable of finding out the maximum of 5,000 numbers 20,000 numbers we have now figured out a way where we have very precise and small set of instructions but which can make the machine do far more work than what the instruction specific indeed without the ability of specifying iterative actions like this we will not be able to write decent programs to solve last problem I will just leave this thought with you for a better initialization of our wild statement what we have so far done is we have assigned some arbitrary value what is the justification for using these no justification the justification is about we wanted to start with some non-zero value of x but we also did not want it to be some positive value otherwise it will interfere with my maximum finding so I had put something at you can I be more meaningful than that now one possibility is that look I have been given some numbers why not I read the first number and treat that as maximum and in the iteration I start with the second number onwards if I did that then I will have no arbitrary initialization in fact my initialization will consist of reading the first number and assuming it to be maximum assigning it to max since first number is unlikely to be zero why because if you had no numbers you would not run the program you're running the program that means you have some positive integers to compare so the first number will be non-zero again it is unlikely that you want to find out maximum of one positive integer it is quite likely that you will have a large number of numbers therefore it makes sense to start initialization with the first number itself so we read the first number and assign it to the value of max and our iteration will now take care of input from the second number if we did that then the initialization instead of saying x equal to minus 9999 I will say c in x and max is equal to x you agree that this will be a more appropriate initialization with this my program something becomes like this so the same program define x and max hint but observe that initial values for x and max now this time we have changed my comment here in blue which says first number is read into x and max is assigned value x observe that what I have written here for the purposes of illustrating and explaining what I am doing it is a good idea to include these statements as comments in your program itself so that any reader of the program can understand why you are doing what you are doing so and notice also the slight difference between the message for the first input I give as compared to the message for subsequent inputs which will be repeatedly executed later on I am saying enter the next positive integer 0 to n but first time I say enter the first number it is more appropriate I could have said that also I need not have said anything the input statement will collect the number these are merely making your program more decent for the person who runs that program and who has to give the value so you agree that this program will be a better version again we see that this is a different program than the other one although both accomplish the same thing the exact number of times the individual instructions are executed will be different for example in the earlier program x equal to minus 9 9 9 9 and max equal to x initially executed had no relevance those two were extra instructions which are making the machine do nearly because we wanted to initialize things properly before entering the wild block whereas these statements actually are part of the work to be done to a very minute extent then I can claim that this program is more efficient than the previous program because it makes the computer do two assignments statements less not statements to assignment execution of two assignments are less in this so we'll just note this it does not really make a difference because when you are executing your program it executes in less than one second either way in fact the computer spends more time waiting for your input because you have to search on the keyboard typing and so on this is the flow chart while you have all agreed that this program works correctly when I put this up tonight I would urge you to get this flow chart execute this program with some different values particularly with different initial values and see whether it actually works or something else needs to be done here is another modification for your consideration I read the first number alright but I first check if x is greater than max then I set max equal to x and then I read the x so I read the x just before going back so that the new x is checked and only if that value is less than not zero then I come in the advantage of this modification is that if the last value is zero then in my earlier program the zero actually gets compared with max okay if you go to this earlier flow chart if the last value is zero that zero is compared with max of course it will not be greater than max so no harm will be done but actually strictly speaking the moment I get a zero value I should not do any more work I should terminate this does not affect the program execution for the problem that we have stated namely finding out maximum of positive numbers but suppose the problem was find out maximum of all non-zero numbers or find out maximum of all numbers except zero water then when the zero comes I should not be comparing it because if I give you all negative numbers only zero will be more than the maximum just think about this I'll leave it to you to ponder over this and consider whether this modified version is better than the earlier version also think whether you can further modify because as I said there is no unique solution or a unique program for solving a problem and anybody and everybody of us can actually think of a stratagem which will make this program better that's the beauty of program next we consider a different problem a computational problem which requires iterative solution it is known as a Newton-Raphson method I will spend some time in explaining this problem and solution also and we'll construct a program this is an iterative method which helps us to find a root of a function fx now given any f of x you know what root means if x is the root then f of x should be zero at that value that much is known fine now the general algorithm to solve such problems numerically we are not doing integration differentiation any such thing we're just trying to solve it numerically numerically the way the problem is solved is you start with some initial guess let's say x i calculate f of x i it will obviously be non-zero if it is zero I found the root now starting with that x i find out the next approximation call it x i plus 1 and again calculate f of x i plus 1 obviously you must do something that if let's say fx fx i was set 25 then fx i plus 1 should be set 12 or 13 fx i plus 2 should be 2.5 it should approach zero when you calculate the function that is how you will go to the root if you are found out a mechanism to approach zero appropriately and then if you execute these iterations again and again and again you will probably be able to find the root so this method works if fx and f dash x is calculated what why f dash x well as we shall see the method of determining x i plus 1 for a given x i depends upon our ability to calculate f dash x we shall see that in an exam so even calculate f dash x and fx of course is a given function if you can calculate both of them and a good initial value is available then this method is supposed to converge towards that root example is to find square root of k k is a given number 217.4 how do you find out square root of 217.4 use a calculator if you did not have a calculator how will you find out use lock tables if you did not have lock tables what would you do well you would do variety of things at newton's time when calculus itself was not just invented by newton the based on the newton raphson iterative numerically computational thing this method actually traces back its history to the solution proposed long time it says we will use this function fx is equal to x square minus square since we want to find out a square root we use this this is the artificial function but you will notice that the result the root of this equation okay will give me the square root of k because this means that where the function is 0 x square is equal to k and therefore x is equal to under root k so you find the root I have found out square root of k we said that we should be able to calculate f dash x it happens that f dash x is simply 2x so therefore it can be calculated easy notice that while calculating f of x and f dash x I require just 2 or 3 arithmetic computations so I can compute both of them straighter I now need a good initial value it so happens that in this case initial value x 0 as 1 always works now this can be proved independently we are not dealing with a mass score so we will continue with this assumption but let's see what successive iterations do to us how to get a better x i plus 1 given some x i is the problem so let's imagine that x i is this this is my function obviously this is square root of k because that's the root in fact the whole graph will move leftward or rightward depending upon that k okay if k is 0 where will this graph pass from origin 5 so I start with some arbitrary value and I have reached through my iteration some place which is let's say x i how do I determine the value of x i plus 1 which is my next iteration but which is closer to the root so here is an example first of all point a which is actually this point has coordinates x i comma 0 okay x i is the distance from origin and it is on x axis so y coordinate is 0 so point a is known now I determine f of x i this f of x i is nothing but value of this function at a and call consequently if I plot this point b then I know the value of the coordinates of point b the value of x coordinate of point b is same as this which is x i the value of y coordinate of point b is f of x i because that is the value that I have calculated agreed this straightforward so I know now the point b coordinates now I say that this function which is actually a complex function I approximate this function by the tangent that I draw here I imagine that tangent is that function if I did that then let us say the intercept on x axis which happens at this point I call this point c now if tangent is reprinting this function I can find out I can calculate the tangent at this function that is where I need f dash x because f dash x will give me the tangent and therefore I can calculate this c now this c is the new point which I want to go at the next iteration and this c point I designate as x i plus 1 as of now I don't know what is x i plus but I know x i I know f of x i I can calculate f dash x and therefore I can calculate x pi plus 1 by observing this form what is f dash x i it's like tan theta which is a b upon a c the value of a b is f of x i and the value of a c is x i minus x i plus 1 that is the difference consequently this implies that x i plus 1 is simply equal to x i minus f of x i upon f dash of x this is where the maths knowledge comes in handy so that I can calculate given an x i how to calculate x i plus 1 is known with this knowledge can I use the wide statement to iterate successfully here here here here here and go to zero but I notice one thing earlier I was dealing with positive integers where zero had a specific distinct identity and I was only comparing things here I will be doing divisions additions etc etc am I very sure that I will get exact zero ever very obviously I'll be using floating point numbers now since I cannot get exact zero I must decide to stop my iteration at some value which is close to zero noting that now I start my construction of wild statement remember what three things I have to do first I have stated the problem find the square root of a given number k so I start with f x equal to x square minus k I note that f dash x is 2 x and I note from my previous mathematical analysis that x i plus 1 is given as x i plus k upon x i entire thing by 2 because this is the this is the formula f of x divided by f dash so when I simplify this is the notation now I am saying I start with x zero equal to 1 and when I put 1 into this place 1 plus k upon 1 divided by 2 I get the next value x 1 and then x 2 and then x 3 and so on so now I identify the repetitive block again like in the last problem I need not remember the previous x i x i minus 1 etc etc my job is to find out where I am currently and from that determine the next one so consequently I identify the repetitive block to be simply x is equal to x plus k by x by 2 agreed this is the current value of x using the current value of x I use my formula to determine what should be the next value so in any iteration I come in with some value of x and this will help me calculate the value to be used for the next iteration and I keep doing this any number of times you shall see when we look at the termination condition but this is what we do in any iteration cycle take the current value of x and compute a new value of x for next it next we come to criteria for terminating the loop now we have said that we can get as close to the root which is square root of k as required how do I define a closeness I already commented that when you are doing floating point operations it is dangerous to expect exact zero to be reached and therefore we define some tolerance we want to reach as close to the root as say within plus or minus zero point zero zero zero one which is good enough for most of the engineering and scientific solutions or so we presume at the moment in which case at the root value fx will be exactly zero but we want to terminate when fx is within this please note there is a slight difference it is not necessary that the root which is calculated is exactly within this we are saying that f of x at that point is within plus or minus zero point zero zero so we define a termination criteria terminate when absolute value of f of x is less than this otherwise keep repeating here is what I have stated again so this is the curve notice that this is the root if I say I should find the root within plus or minus zero point zero zero zero zero one or whatever I am actually telling you a tolerance on the x axis but I have no way of finding that out because I don't know the real root the only thing I can find is the value of the function at any given x so I define the tolerance in terms of function of x I drawn two lines here which define some kind of an accuracy threshold which happens to be zero point zero zero zero one here if that is the threshold then my terminating condition is when absolute value of f of x becomes less than or equal to this get out and therefore my continuing condition is not of this so if the value of this absolute value of this is greater than this I must continue iteration so I got the termination criteria I got the iteration block next is initialization initialization was one this was the simplest of the problem because the fellow who told me the problem said start with x not equal to one it will always well I have a theoretical proof for it good thank you very much I don't have to waste time in deciding now this is the program so look at this program it says float k float x equal to one initial some people are asking me should I not write one point zero please remember that you can write the numbers as you please when they get assigned to the variables depending upon the type of the variable the value will be appropriately converted before assigned the only thing you have to be careful about is when you have two operands of the type integer the result is an integer and division could create a problem so you have to be careful about anyway so this is the initialization give number whose square root is to be found is an error here I will correct it later I read k notice that this is not part of my initialization of the iteration but this is the necessary initialization I must read the value of k whose square root I want to find out okay the real initialization for the iteration is just this x is equal to one fine so I have done this initialization I have read the value of k observe the condition while while x square minus k is greater than zero point zero zero zero one or two vertical bars or k minus x into x is greater than zero point zero zero zero one why this complicated condition because it should be within plus or minus one and the value of fx at any point x could be either positive or negative we don't know so therefore we take cognizance of both possibilities later on we shall see that there are built-in functions which will permit us to calculate log x sin x absolute value of x even square root etc. but we are studying how to do this using algorithms so this is all you agree that this condition is proper as long as this condition is valid I want to keep executing the iteration iteration is simply x is equal to x plus k by x by two so given an existing value of x calculate the new value of x go back again check this condition go back again do you notice how simple the program is the entire complicated method of calculating by slide rule or by lock table or calculator or whatever step by step is taken care of just by few the few of these instructions that is why this is an extremely powerful mechanism as we said the value of the root is whatever x you have found out at the end that's it you can have additional modifications even in the small program for example one of you may say that look why should I restrict the general program powerful program like this to always calculate root up to only this particular accuracy let user define the accuracy instead let somebody say that I am ok point zero one somebody has said I want plus minus point zero zero zero zero zero zero five somebody may say I want the tolerance to be between plus or minus one point zero e minus twelve I want that accuracy let user decide well if I want to include such a possibility what could I do simple I could define another variable float T let us say for threshold input the value of T and wherever I have used this absolute value I will say compare it with T and I will run on that all the only difference will be smaller the tolerance larger will be the number of iterations that will be executed but let them go to the Goda giri why should I worry all that I should worry about is that my iteration actually terminate sometime or the other so is it clear how to use wild why you will be constructing programs using wild statement in your next lab you will not be using other statements in the lab however we will continue discussion of alternative ways of prescribing iteration to that end we shall look at the need for an alternative form and we will only look at a sample of a program which employs an alternative version of iterative specification called the for statement just as we saw the wild statement in the last class but we discussed it here similarly we will only see the use of false statement today and we will discuss its usage the designing etc etc in the subsequent class here is the need if I want to repeat the iteration a given number of times notice that in both the problems how many times I want to iterate was not known what was stated is find out maximum of given positive integer find out value of the root within this rate but there are problems where I want to do a specific thing for example the very first problem we solved it said find out maximum of five given integers somebody may say find the sum of n given value n is not known at the time of writing programs so suppose somebody says look I have some values but I know how many values there I have five hundred and sixty three students I will give their marks and you have to calculate the average but five sixty three is the total number in CS 101 let us say three students are upset so at that time the marks that I will have will be five hundred and sixty the point is I want you to write a program for me where I will tell you how many marks I am going to input and you calculate the average and give it to me so my problem then is given n do something n times that is my problem similarly a question add areas of thousand rectangles with different dimensions why would one do such a silly thing like this thousand rectangles of different dimensions different height different width calculate one area second area third area add thousand times well we shall see a very meaningful problem in the next class when we discuss estimation of log of x using a method of calculating some of areas of different rectangles but such problems do happen in such cases what we need is a counting mechanism what is that counting mechanism we want the mechanism which will start with the prescribed initial value say count is one then we need a mechanism that in the iterative I am talking in the context of iterative solution so I have found out what is to be iterated upon but I want this iteration to be done fixed number of times n times so I want some count to be set to one then when I iterate every time I iterate I want that counter to be incremented and whenever count reaches my maximum value I want to come out in short I do not have any other terminating condition other than that that look boss keep counting how many times you are doing things after you have done it 10 times get out please note that I can implement this specification using my wife statement what can I do I can set a variable say count before the wife statement I initialize it to one suppose I have to execute it 10 times then I will say why count less than equal to n do this and inside the iteration block at the end if I say count plus plus at the end of the iteration count will be incremented and when I come back for the next iteration that count will be compared so I can easily implement it using wife I therefore do not need any special statement in C to do let me do this however if I know how to do this then I it would be nice if I have a concise a single statement where I can specify all the three things initial value terminating condition and increment such is the for statement C plus plus provides a statement to implement such iteration control so this is an example for in bracket count equal to one semicolon count less than five semicolon count plus plus notice that what is written here is not a condition as we wrote in wife these are very complex specification there are actually three statements first statement defines initialization second statement defines a condition during which time you have to iterate and the third statement says what is to be done at the end of an iteration so exactly what you had imagined in your mind that you will do using a while loop you will initialize outside the while in the while you put a condition for continuation and at the end of this you will increment one by count that is precisely what is stated what is to be remembered is that while the for statement is written in one short you prescribed all of these three things these three things do not happen at this point in time when you execute the program this is a specification mechanism there is a block the statements within the iterative block however when this statement is compiled the translated program works as follows it initializes the count before beginning the first iteration that is this statement is executed before the first iteration then at the beginning of each iteration it checks the condition that is more like your wife said and additionally after completing these statements within the iterative block it introduces an action which has incremented the count at the end of every all that this means is itself extremely simplified neat and elegant mechanism of doing counting without having to use your wife state I will just stop by giving you an example of an issue solution for finding out maximum of five numbers int x max count max is zero please note that in this case I do not require to initialize x why because my wife condition does not depend upon x being zero my wife condition says do it five times in fact you don't even have to give me an artificial zero at the end I am guaranteeing that I will add five numbers so I start with max equal to zero I have to initialize max because max is a part of comparison here so I am still adding five non-negative zeros and non-negative integers look at the specification for count equal to one count less than equal to five count plus plus notice a very meaningful usage of the increment operator it does not matter whether you write plus plus count and remember that this specification says that when the program is translated count equal to one is executed somewhere here then you come inside check this condition is countless than equal to five of course initially it is because you have set it to one and then you execute these statements go back but before going back count plus plus is executed somewhere here I would urge you to think of writing down a flow chart representing this for based on this explanation we will be discussing and using for in many numerical problems in the subsequent way