 So, as I said last time the introductory part of the course is over and we start now in real earnest to solve real problems and we will learn how to write computer programs to solve real problems. Initially we are going to concentrate on solving numerical problems but subsequently we will extend ourselves to handle problems involving strings involving large number of values and ultimately will relate to what we call large databases and files. In today's lecture we will review the CPP Dumbo or essentially C++ programs how they are written. More specifically we will look at iterative numerical computations. We had done this kind of thing in the last lecture. We will do a review quickly of the basic commands when we look at the C++ program structure including iterations and conditional execution. But we will immediately get into solving numerical problems of the time finding out factorial of a number. We will introduce what we would like to call Hemchandra numbers. There is a more famous name associated with it. We will see that and we will conclude today's lecture with an example of finding roots of an equation using Newton-Raphson method. At the end I will very briefly describe the course and lab organization specifically relating to lab structure now onwards as well as the evaluation approach that we shall use in this course. So first the C++ program structure I always start with hash include IO stream. I always write using namespace std. Then I say int main opening bracket closing bracket and an opening brass. This is followed by the statements of our algorithm or statements of our program instructions or statements. And after we complete all instructions we shall write return 0 followed by a closing brass. This is the structure of any C++ program that you would have to write. We have hinted at the meaning of some of these lines but for the time being we shall consider all of these lines to be mandatory to be written in our program. Let us assume that our C++ dumbo cannot understand that we are giving him a program unless we write it in this structure. Of course whatever instructions we actually want to give C++ to do our computations will be contained inside this block as I have indicated. Any set of statements enclosed with an opening brass and completed with a closing brass is called a block. We shall see many such blocks inserted within themselves and so on in the subsequent slides. So is this clear to everybody this is how a C++ program structure will look like and we shall explain as the course goes on the meaning of different lines. We discussed in the introductory lectures last week some of the C++ data types. So we have seen for example that our dumbo can handle numerical values and string data. We have seen that as far as we are concerned both within our programs and while giving input to dumbo or while collecting output from dumbo we shall write or see values of this form 7.45, 12.3 E minus 12 which effectively stands 12.3 into 10 to the power minus 12. I hope you will recall that this number will be stored internally in a floating point format with the mantissa as just 0.123 equivalent and the exponent appropriately at this point. We can have strings like this. This is a special string which denotes end of life. There is a special symbol in fact which we shall see in some of our programs. So constant values are written like this by us but C++ stores these values in an internal format. Much later in the course when we shall discuss the internal intricacies of the actual computer when we shall see the hardware we shall have an occasion to look at the binary representation of all numbers or all characters that we actually deal with and then we shall know more as to what are the problems and nuances with such internal representation. However for the type being we shall take it that C++ can handle decimal values in these forms and character strings in this form. The numerical values always have an associated type. So if you look at the dumbo's programming model he has drawers for memory locations and each drawer contains a value. Each drawer has a name which is called a variable name. Not only that each drawer must have an associated type of value which the drawer can control. So you have to correspondingly declare the data type associated with every variable name. These data types for numerical values could be int, long, float or double. You will recall that in the last lecture we had looked at these data types and seen what kind of values can be stored and handled. And as I said since you have to define variables initially in your program you will have statements of the type int count which means count is a variable name which will store only integer values, float a, b, sum. So these three variable names will be able to store only floating point values double large value. These are some of the data types that we associate with our variables. The C++ statements which will form the body of our computer program will contain more than one instructions. Each instruction of our program can ordinarily convey any one of the three actions which will be executed by dumbo when dumbo executes our program. These three actions could be assignment which is the most important computational instruction because that is how we will do computations input and output. We have seen examples of all of these. For example assignment examples sum equal to a plus b semicolon. Each instruction is to be terminated by a semicolon for C++. Area is equal to x minus 1 star 1 upon x minus 1. You will recall this kind of expression. This expression has a precedence depending upon which operators come one after another. We had discussed the precedence in case of any doubt or whenever you require to override the natural precedence of operators in the evaluation of expressions, you shall use brackets. Opening and closing brackets will have the highest risk. So within this opening bracket and closing bracket for example, this x minus 1 will be evaluated completely and the resultant value will be used for multiplying whatever comes next. There is a peculiar type of assignment which we looked at where for example, we may say m is equal to m plus x divided by 2.5. m appears on both sides. We noted specifically that this is not an equation in a conventional sense but this is rather a reassignment. So there exists a value of m to begin with. That value is added to x divided by 2.5 and whatever is the resultant value is reassigned to m. So m loses its old value and from now onwards in our program, it will take the new value. As we have seen in the iterative loops, this is an extremely useful tool with us, the reassignment. A special form of reassignment is increment. For example, we want to change the value of some count by 1. We can write count equal to count plus 1 semicolon. This is an instruction where Dumbo looks at the value of count in the drawer. Whatever the value is, 25, 37. It will simply add 1 to it and put it back. So it will become 26 or 38 appropriate. This can be abbreviated in a format of this type. Count plus equal 1. So plus equal always means that on the right hand side whatever is the value and this could be a complex expression as well. That has to be added to the existing value of count and the resultant value has to be deposited back in that variable. A still shorter mechanism to represent exactly the same thing is to say plus plus count or count plus. We shall see the usage of these things more within the for loops and such things where this is not part of an expression, but this is a standalone expression. So whenever we say count plus plus, in fact count plus plus is a full fledged expression whose value at the end of execution is the previous value plus 1. So this is the increment and in general the assignment statements is how it is handled. As far as input is concerned, input is handled using the C in directive where we separate out different values to be obtained from us by C plus plus using greater greater symbols. So C in greater greater x, greater greater max value means C plus plus dumbo is looking at getting two input values. The first one that we will give will be associated with x, the next one will be associated with max value. In exactly the same fashion an output instruction can be given to C plus plus using C out. C out less less something less less something less less something etcetera is the format in which if we have a string, the string will be printed verbatim on our screen. If we have a variable name or an expression, then the value of that variable or the value of the resultant expression after computation will be put out. This is a special string as I have explained at the end of any output statement you should include this because this will go to the new line. So that if you have multiple C out instructions in the same program, the output will not appear cluttered. It will appear separated on different lines. Next we discussed in the last class conditional execution where program instructions are normally executed by C plus plus in the given sequence. First, second, third, fourth, even if you have ten thousand instructions, they will all be executed in the given sequence. You remember we said that is the beauty of computers. They are a stored program machine. That means I can write a program ahead of time, give the entire set to the computer which will store by program and will religiously execute instructions in the given sequence. However, there are occasions when we do not want C plus plus or for that matter any other programming language which the computer uses to execute instructions in the given sequence. We might conditionally want to go out of sequence and do something else. For such purposes, C plus plus is endowed with a conditional execution capability. C plus plus can examine conditions and based on the result, condition is a greater than b. Is your name equal to this? Is your hostel number safe? This of eight or whatever. So when you ask such questions, the answer is always yes or no. If the answer is yes, you would like to do something. If the answer is no, you would like to do something else. That is the most natural conditional evaluation or conditional execution of statements you would like to do. Whenever we say yes or no in computer terminology, we call these as the result of condition evaluation and the result is always represented as true or false. True and false are logical values which represent yes and no for our answer. So the way we instruct Mr. Dumbo is to say if in bracket a condition and remember last time we examined the different kind of conditions that we can write using less than, less than equal to greater than, greater than equal to, not equal to etcetera, etcetera. And we also saw that the conditions could be fairly complex. We could have any complicated expression inside as long as there is a comparison and the ultimate result cannot be a numerical value. Generally, it will be a logical value either yes or no. The answer must be yes. If the answer is yes, then the statements in group number one are executed. Else the statements in group number two are executed. If you have no else clause, if you simply want to execute some statements, if the condition is true, otherwise simply carry on. You can simply terminate this statement at this point. You need not write else. Here is an example. Based on the age of a passenger, let us say the bus ticket cost is different. Suppose it is 25 rupees 50 paisa for an adult, but for children there is a concession, 50 percent concession. So children can travel at 12 rupees 50 paisa or 12 rupees 75 paisa. Now suppose I write these statements. I get in age, the value of age is inside the computer. Now I say ticket equal to 12.75, then I say ticket equal to 25.50 and then output saying rupees so much. End L by the way is a special name which represents backslash n or new line. This special name is not to be used for any other purpose. We should avoid using a variable with this name. Actually this name belongs to what we call the global name space or standard name space which our Dumbo uses. C++ has predefined names and these names are understood whenever he is using a name space, hypothetical name space where all such names are put together and C++ is aware of it. This will explain to you the use of one sentence at the beginning of a program using name space STD. So when you say using name space STD, we are telling Dumbo that the variables that I shall use, I will define. However, I know that you have a large reportire of library names and so many other things. Please use that standard library to define all these names. Anyway, so what will this do? What will this program print? It will print 25.50 because instructions are executed in the given sequence. Suppose I do not like it and I invert that sequence. So I say ticket equal to 25.50 first and then say ticket equal to 12.75. Now independent of age, Dumbo will print 12.75. So you see the assignment to variable ticket will always be the last assigned value irrespective of age and that is what will get printed. Does this solve our problem? No. We want to print the right value of the ticket depending upon the age of the person and this is where we require to deviate from the conventional serial execution of our instruction and that is the reason why we will use the if statement. Here is what we wish to do actually. I am following this path. I have executed some instruction which is see-in or something. I get the value of age. I want to examine whether age is greater than 12. If the answer is yes or the condition is true, I want to make an assignment ticket equal to 25.50. If the answer is no on the other hand, there can be only two possible answers. So true or false, then I want to execute this instruction which says ticket equal to 12.75 and get out. To indicate this kind of execution capabilities, I shall use the if statement which we just introduced. So we shall say after we get the age, if age is greater than 12, ticket is equal to 25.50 else ticket is 12.75. Please note that either this or this will be executed. Although both of them are written one after another, the natural sequence is completely ignored because we have given an if statement here. So as a result, when I come out of this if block as we call it, at this point the value of the ticket will be appropriately set depending upon age and when I print this ticket, I will get the correct value. Consider an additional squiggle. We have considered only if age is more than 12 and adult will cost this much. If there is a child, the ticket will cost so much. But suppose there are elder citizens, like I am a senior citizen beyond 60 years and suppose I am charged only 20 rupees, then given the same value of age in the same program, I should be able to discern between elders, then that is senior citizens, adults and children. For that, I can use what is known as an if else if ladder. This ladder is like steps. So for example, here I get in the age and check if age is greater than 60. If it is, I set ticket equal to 20 rupees. If it is not, then automatically it means that age is either less than or equal to 60. So when I come to the next statement, else if, notice that I will come to this else if only if age is not greater than 60. So when I come here, I can examine the if age is greater than 12. If it is, then I will set ticket equal to 25.5 else I know the age is less than or equal to 12 and the value of the ticket should be 12.7. Are you clear about this? So there is a series and I could write if else if else if else if as many times as I want, that is why it is called a ladder. Notice however that you have to be extremely careful in the order of examining these conditions. For example, will it do to put this condition age greater than 12 at the top and age greater than 60 here? Yes or no? No, because if I put age greater than 12 here, then I will get 25.50 assigned first. If age is greater than 60, I will get the correct age assigned and then else if none of them are true, I will assign this. So what is wrong? If age is less than 60, I will leave it to you. Please go ahead and execute that these three conditions in this sequence they seem to work. You all seem to believe that if I change the sequence of condition checking, then the result will not be correct. Please examine all possible combinations and find out whether there exists at least one other sequence where you will still get the correct. I might for example instead of checking for greater than I might check for less than. It is possible to write these conditions in variety of ways. What is important is to ensure that what you require at the end is obtained through the instructions that you have given. The repetitive actions. We looked at the for loop last time and I had explained the for loop here. Let me go over to the blank page and again try to do the same thing here. There is a block of statements which I want to execute again and again and again. 5 times 6 times n times where n is some given value. The way I would like to set up an iteration is that since I want to execute it once twice thrice 4 times n times, I have to do some counting. So I will use an artificial variable called i. I will set i equal to 1 initially and in the condition I will keep examining whether i is less than equal to n. If it is less than or equal to n, that means I want to execute that body of statements iteratively. So I will come out here, execute that block and then increment the value of i by 1. So it is like count 1. So initially I start with 1, execute this block once. Then I increment i. I will become 2. I will not go further. I will go back now to this point. I will not go back here, mind you, because I will be reset to 1 and that will become a perpetual loop. So I will go back here. Again examine is i greater than n. Let us say n is equal to 5. So first time I will be 1. Then after executing this once, I will become 2. With 2 I go here. Is 2 less than equal to 5? Answer is yes. I will go down again and execute this block. Second time. I will become 3. Again I will continue. When i is 5, 5 is still less than or equal to 5. Assuming n is equal to 5, I will still execute this the fifth time. Now I will become 6. 6 is not less than equal to 5. So I will come out. So this will iterate as many times as I want. We had seen the specification of for loop instruction to C plus plus in the most general form. This particular thing is called initialization. So I can initialize a counter. I can do a whole lot of other initializations if I want. This is called condition test and this is the increment that is post execution of a block. It is shown as increment here, but again I can take any complicated expression evaluation and assignment here. So this is post execution or preparation for next iterations. I will just call it increment which is to be understood to mean that any preparation for the next iteration. So I might want to execute multiple statements or something. Generally an increment kind of operation is implied in the for instruction that we shall see. If I want to do something else for the next iteration then obviously I will have to attach it to this block itself. So within this block after doing whatever computations I want to do iteratively I might write additional statements which will prepare my algorithm for the next iteration. We shall see that example and the for statement that we had seen actually encompasses all of this. This whole thing is regarded as one block by C P P Lee Dumbo. So this is far superior to mere conditional execution. Here also there is a conditional execution, but this is conditional iterative execution. Keep on repeating some. We saw an example of this last time. We shall see one more example of computing factorial n. So here is an example. I have defined n factorial to be an integer variable. I have defined n as integer variable and I have defined i as integer variable. So let us look at this. I am going to use three variables n, i and n factorial. n is the number whose factorial I wish to calculate would be 5, 6, 10, 20 whatever. i is going to be the kind of count that I explained and n factorial is going to be the final value. The way I proceed is I initialize n factorial to a value 1. After all factorial of 1 is 1. So I assume this is the first term. Now every time I execute an iterative body, look at this execution of iterative body or the specification. For i equal to 1, i less than equal to n i plus plus. So this is the first statement i equal to 1. This is called the initialization of the loop and that will be executed only once at the beginning by Dumbo. Next the condition will be checked is i less than equal to n. If the condition is true, CPP will go into the block and will execute whatever instruction I have given. This instruction says take the existing value of factorial, multiply it by 1 and that becomes the resultant value. Initially with i equal to 1, it will multiply 1 by 1 only. When i becomes 2, it will multiply 1 by 2 and get the value 2. In the next iteration, the value will be 2 which will be multiplied by 3 and so on till i becomes not less than or equal to n. At which time it will get out and then it will execute this C out statement where factorial n is n factorial. So it will output the values of these two variables interspersed with these two strings so that I get a meaningful statement. Is this clear to everybody how this is executed? So this is the crux of any iterative execution. Last time what we had seen was very complicated example of what can this part be, what can this part be and what can this part be. Please remember in one of the slides we have said w w w x x x y y y y something like that. So this could be any complex statement assignment initialization. This could again be any complex expression. At the end it must result in true or false and there could be multiple statements separated from each other that is possible etcetera. The complete program will of course start with include i o stream using namespace std int main and then int n factorial n and i. Notice that I use a output statement first to let the dumbo tell me that it is expecting a value of n. So give value of n and n del. Notice that instead of backslash l I have written n del which is a standard name which dumbo knows provided we have told him use namespace std and then I get the value of n. This is followed by my iteration setup I start with n factorial equal to 1 for i equal to 1 up to i as long as i is less than equal to n in steps of 1. Notice the use of i plus plus I could have also written i is equal to i plus 1 both mean the same thing. So please note that this is a full fledged expression and reassignment. So it will actually change the value of i by 1 is exactly same as i equal to i plus 1 and then I calculate n factorial as equal to n factorial multiplied by i and at the end of the iteration I do this. Notice that this is the only way to specify calculation of an iterative body where the number of iterations are not known in advance. If I knew I was calculating factorial of 5 I could have even written a single expression n factorial is equal to 5 multiplied by 4 multiplied by 3 multiplied by 2 multiplied by 1 and I would have got the factorial. The reason I am writing a program is I do not know which value it is whose factorial I want to compute. Since that is the variable I have to set up an iteration. So that is the purpose of iteration is that extremely powerful programming mechanism available to us cut C C plus plus dumbo. We look at another iterative problem I mentioned Hemchandra's problem. Hemchandra lived in 12th century A.D. in India and he was concerned with actually building a meter for musical beats but translated in more simple ways suppose I have to build a wall of length 8 feet and I have bricks which are either 2 feet long or 1 foot long. So I have only 2 types of bricks some 1 foot long some 2 feet long. The question is in how many ways can I lay the bricks so that I fill the 8 feet. In how many different ways I can have bricks arranged such that I fill up the complete 8 feet. Here are some example I could for example use 4 bricks of 2 feet each. I could use all bricks of 1 foot each then I will require 8. I may use 3 bricks of 2 feet and 2 bricks of 1 foot. I may use 2 bricks of 2 feet each and 4 bricks of 1 foot. So these are the multiple ways how many different ways exist that is the question. The actual question which Hemchandra tried to answer was to design a poetic meter with 8 beats. Suppose I want to design a poetic meter with 8 beats and the meter is made of short syllabus and long syllabus exactly like 1 foot long brick or 2 feet long short and long. Short syllabus is 1 beat long syllabus is 2 beats that is the standard musical beat pattern. How many ways are there of filling in 8 beats. Here is an example of a poetic meter some of you would recognize. So this is the representation of a long beat short long long long short short long short etc. etc. If I have an 8 bit meter how many ways I can have classical. So Hemchandra says in his write up written in 12th century that by the method of Pingala it is enough to observe that the last beat is long or short. Based on that I can compute how many different classes can be there. This was a wonderful observation and this resulted in his solution to this problem which we shall see in a moment. Incidentally Pingala was a mathematician poet from 500 A.D. He lived 1500 years before Hemchandra and yet Hemchandra is giving credit to Pingala. This is just a short unsolicited advice to all of you. You are most welcome to copy things because that is how we learn fast somebody has already discovered something. But copy only if necessary and only if permitted and always give credit. The last part we often forget. Here is the Hemchandra solution I will not go through the details I will straight away go to the derivation those of you are interested in musical beat classification and the Hemchandra solution can mull over this slide later. But effectively what Hemchandra says is that if I call S as the class of 8 bit patterns with short last beat and L as class of 8 bit patterns with long last beat then effectively L plus L will give me the total number of variations. However he defines how he can calculate that one particular class by using the previous number of classes with less number of bits. Effectively he says that 8 bit patterns will be equal to all 7 bit patterns plus 6 bit patterns. And he says by this you can calculate the total number of classes. So this is called a recursive definition. I am defining the total number of 8 bit patterns in terms of number of 7 bit patterns, 7 bit patterns and 6 bit patterns. How do I define 6 bit pattern in terms of 5 bit patterns and 4 bit patterns? How do I define a 5 bit pattern, 4 bit pattern and 3 bit patterns? Can you not see that this would be a very classical simple case of applying iteration all by with some meaningful construction of the statements? Algebraically if h n is the number of patterns with n beats then what Hemchandra is saying is h 8 will be equal to h 7 plus h 6. In general h n will be equal to h n minus 1 plus h n minus 1. Does this help us to compute h 8? Well yes if I know h 7 and h 6. How do I know h 7? I can know h 7 if I know h 6 and h 5. How do I know h 6? Same h 5 and h 4 etc. But there is an end to it because ultimately I will end up with h 1, h 2 which can be actually computed absolutely. I do not need anything else and therefore if we know all these successively we can build up to h 8 or for that matter h 10, h 25, whatever we need. So if h 1 is the number of patterns with 1 bit obviously there can be only one way either short bit or long bit. There is only one class. H 2 number with 2 bits there are only 2 bits. So either I can have short and short or long. Remember if I have to construct 2 feet then either 1 and 1 or 2 I cannot have any other combination. Once I know h 1 and h 2 I know h 3 is h 2 plus h 1 which is 3. I know h 4 is h 3 plus h 2 which is 5. I know h 5 which is h 4 plus h 3 which is 8 and so on. Consequently I get h 8 as equal to 34. That is the answer obtained by Hemchandra using Pingala's method. This is the program to compute h n. We can now instruct our Dumbo C plus plus to calculate h n for us given n. So I define int n then I give a value of n which number to compute. But notice that the iteration is this time more complicated. I have to add 2 previous terms to get the next term. So I start arbitrarily by saying that my previous term is 1, current term is 2. If previous term is 1 and current term is 2 then I can set up an iteration for i equal to 3 to n where I calculate the h next term by adding previous term and current term. What will happen for the first time when I execute this iteration? H priv is 1 and h current is 2. So 1 plus 2 I will get 3 as h next. So I calculated h 3 correctly but I want to set up my calculation mechanism for the next iteration. In the next iteration whatever is the next term must become the current term and whatever is the current term must become the previous term. Notice the sequence. When I say h priv is equal to h current since h current was 2, h priv will now become 2 and then when I say h current equal to h next, h next which was 3 will become h current. Notice that if I reverse the order of these two statements I will get completely wrong results. I must first preserve the value of h current into a variable and then only change h current to h next. So consequently in the very first iteration h next will be calculated as 3 and it will now become h current and h priv will become 2. When I go out to the next iteration because of this i plus plus, i plus plus will become 4, i will become 4. It is still suppose less than n then I will again calculate h next will now be 3 plus 2 which is 5 and again the reassignment of h previous and h current will happen and I will keep on it. Are you clear about this iteration? When I complete this iteration at the end whatever is the value of h next will be the value of h n or for the nth. The code is a bit tricky so you need a command. For example, you may say at the beginning of an iteration h current is h i minus 1 right i th term if you cannot put this subscript and so on in a computer program h priv is h i minus 2 where i is the value of variable i. Can you prove this? Well using mathematical induction you can prove that basically proving h next is equal to h priv plus h current is adequate. So correct answer will be generated. There is a proof by induction which I am ignoring. You can all go through the all of your familiar with proof by induction good. So you can figure this out. What is important here is that mathematics is being generated by from poet. This series is very interesting it represents number of petals in many flowers. Most important the ratio of consecutive terms tends to a limit. You might have heard of golden ratio for example. These numbers are more commonly known as Fibonacci numbers. You are familiar with Fibonacci numbers? Yes, Hemchandra lived much before Fibonacci. Having calculated Fibonacci numbers we now apply the iteration technique to some more complex numerical problems. Suppose we want to find out the root of a given function f x. So for example, I want to find out what is the value of x such that f x is equal to 0. All of your familiar with this notion of the root of a function root of an equation 5. Now the Newton-Raphson method actually works on being able to calculate the first derivative of f x f dash x. So if f x and f dash x can be easily calculated and if a good initial guess is available for the root then I can do successive computations such that I will converge to the final root. We shall see an example of that. For example, if I want to find out the square root of some k what is the function now for square root f x is equal to x square minus k this is the generic function f dash x is 2 x the derivative. So f x and f dash x can be calculated easily they do not require more than 2 or 3 arithmetic operations. What could be the initial value? Well initial guess x not equal to 1 always works. So we start with an assumption that root is x equal to 1. We do not know what the actual root is but we will be able to find out if we start with x equal to 1. But what is the iterative mechanism that we can set up we are not very clear about that. We know f x is given we know f dash x can be calculated. We know sort of we assume that x 0 equal to 1 should be good enough as an initial guess. But what should be the next guess what should be the next guess and so on how to get better x i plus 1 given x i here is the curve f x in this curve I have started with some x i initial guess I call given x i I want to find out what should be the next x i plus 1. So that I iteratively go nearer to the root where is the root of this equation by the way it is here right wherever the function crosses x equal to 0 when f x is equal to 0 at that point that is the root of the equation. So I want to reach here I do not know exactly how this function looks like and where exactly it will cross x axis. But I am starting with some x i. So what I do I say point a which is this x i comma 0 is not this is my point a now I calculate f of x i what is f of x i at this point it is given by this function. So this will be point b whose coordinates will be x i and f of x i because f of x i will be the y axis. So this is my point b now I have reached here I want to go nearer this point by the way. So I have I now come up with a extremely interesting hypothesis that if I draw a tangent to this curve at this point b then wherever that tangent crosses my x axis should be a closer point to the root. Now what is the tangent at this point b? So I approximate this function by the tangent and calculate the intercept of that tangent on the x axis. Here is the tangent and the tangent crosses the x axis at some point which I call x i plus 1. You will agree that this x i plus 1 is a better guess to the root than what I started with namely x i. I call this point c what are the coordinates of point c? Point c is x i plus 1 comma 0 but what is x i plus 1 because the point b is x i comma f x i from that can I calculate the coordinates of point c or the new x i plus 1 here is the calculation f dash x which is the derivative but this time I am saying a b upon a c. So this is a b and this is a c a b upon a c is nothing but f of x i divided by x i minus x i plus 1. So what I am doing I am calculating the slope of this tangent law and this works out to be f of x i upon x i minus x i plus 1 or this is equal to x i plus 1. Consequently this is equal to x i minus f of x i divided by f dash of x i. The simple mathematical manipulation of this equation permits me to calculate x i plus 1 if I know x i. So consequently if I start with x i then whatever is the value of x i have chosen I can calculate the next value for iteration by using this formula. We are of course assuming that f x and f dash x both are calculable for any given value. With that assumption this value can be calculated and this becomes x i plus 1 and once I come to x i plus 1 I can repeat this process I can now find out what is the value of the function at x i plus 1 it will be somewhere here. I will draw another tangent at that point you will agree that it will intercept the x axis closer to the root. Again I can repeat this process till such time that there is no improvement between successive x i they become so close that that itself is my root. Restore we have got an iterative method to solve the roots of an equation using this method which is called Newton-Raphson technique this is a numerical computational technique to get the roots. So, to calculate square root of k let us say I say x i plus 1 is x i minus f x i upon f dash x i. Since f x is equal to x square minus k f dash x is 2 x then I know that given x i I can calculate x i plus 1 as x i minus x square minus k divided by 2 x i which is same as x i plus k upon x i divided by 2. You can do this simplification yourself effectively you have to use the same problem. The crux of the computational instruction to Dumbo however is in the fact that given an initial guess I have a method to compute the next guess. Given that guess I have the same method to compute the next guess and so on and I have the comfort that these successive guesses will move closer to the approximation of my root till I ultimately land up with that root. So, if I start with x 0 equal to 1 then I can compute x 1 then x 2 etcetera etcetera and we can get as close to square root k as required. We are not proving this we leave it to other courses in maths and so on, but computationally there could be another way here is one way I am given k I define x i and k as floating point variables please note that I must deal with floating point numbers. I input the value of k now I set up x i equal to 1 the initial guess inside that I write x i square minus k greater than 0.001 or k minus x i square is greater than 0.001 notice that I might approach this from either the positive side or negative side until error in the square root is at most 0.001. So, I am now not counting the number of iterations I am saying I do not care how many iterations I have to take, but take me as close as possible to the root which means the difference between two successive values should be as minimal as possible that difference between successive values I call as error and that is my error computation. And then I put x i as x i upon k divided by x i by 2 notice that this is the next term I am reassigning the value to x i for the next iteration. So, x i changes consequently the condition will change and this simple iteration without any other computation will get me the final root this is another method yet another way I start with x i equal to 1 and I say while x i minus k greater than 0.001 or k minus x i square is greater than 0.001 do this computation and I say c out less than x. This is an alternate way of asking Dumbo to iterate we have seen only for loop for something something till some condition is made and with this increment that is the format of for loop, but here there is no counting increment that I want I simply want to reach as close to the root as possible I can then use another variant of iterative instructions to Dumbo called while this condition is while this condition remains or is satisfied keep on doing the body the body has only one statement after this again it will check the condition if the condition is straight true again execute again execute again execute till such time that the margin between the two successive values is less than 0.001 after which you will just output value of it. So, you can see many different ways of elegant programming are possible it is up to us to imagine when to use while when to use for when to use something else etcetera. This is the while statement the while statement as the format while condition loop through the body. So, the way the action takes place check condition then execute loop body if it is true repeat. So, if loop body is a single statement then we need not use brasses, but generally it is a good idea to use opening brass and closing brass because in this particular case you have only one statement to execute in the body, but in general you could have 3 4 5 statement like for example, if you were to write the hem chandra numbers or Fibonacci series calculation then you have to prepare for the next iteration by reassigning H prev and H current. So, you will have two statements they are best enclosed in a body like this. So, you could actually translate the for statement into a while statement by writing appropriately. Please write down for a given for loop how will you implement using while and you will be able to understand this let me write down the flow chart for your benefit. So, this is how the while condition will look like the statement that I shall make. So, this is the while block and the way I will write it is to say while as simple as that while condition execute block. So, what is the entire block has many statements as you want you will notice that if before the while you put an initialization statement such as i equal to 1 and then in the while you say while i less than equal to n and after the block within the block you write i equal to i plus 1 you will automatically get an iteration which is prescribed using the for statement for i equal to 1 till i is less than equal to n in increment of i plus 1. So, both are actually equivalent when should you use while statement when should you use for statement depends if there is a control variable with some initial value and an update rule and whose value distinctly defines each loop iteration use for. So, this is a thumb rule if you have to prescribe an iteration in which there is a control variable or a counting variable then the best thing is to use for where count is initialized count is checked against some condition and the value of count is used inside the iteration. If the loop executes a fixed number of time again you can use for for i equal to 1 to n, but if the loop has to execute till a certain condition is satisfied then you can use why it is a matter of choice matter of test is this is this clear there will be lab assignments based on these numerical computations. So, let me describe very briefly the kind of labs that you are going to have now first there is homework write a program to calculate the tube route using Newton Raphson method the cube is more interesting and check how many iterations are needed to get good answers should be actually very few there are some of you incidentally who claim to have done a whole lot of programming earlier. So, I have a special problem for those people that is about the bus ticket problem you remember you have to calculate the bus ticket for an elder or for an adult or for a child we assume that the input is age for all those who know programming here is a challenge the input is given as date of birth and you know what is the date today using that do appropriate date arithmetic to find out whether somebody is more than 60 years somebody is more than 12 years or not. So, your input is today's date written in the conventional form that is 10th August 2009 now if it is August you might have a problem. So, here is the challenge we shall assume that the date of birth is written as 10 what is it is 8th month or 9th month August. So, I am giving you an integer number as the date of birth 10 0 8 2 0 0 9 the first two digits will mean date the next two digits will mean month and the last four digits will mean year. So, this is this is today's date of course I do not know what rule will apply the date of birth is same one day old child they still charge money I think I do not know, but anyway. So, let us say date of birth is some 28 December let us say 2 0 0 1 this could be 1948 or whatever please know the complex problem if you have an input of this kind the complex problem is you cannot subtract one date from another to get the age. There is a lot of complex arithmetic has to be done we shall see later how C plus plus dumbo helps us using variety of library routines to do these date computation easily, but since there are some of you who claim to have done lot of programming this would be an interesting challenge for you of course this is not part of the lab as per the lab is concerned you continue to do a rather simpler exercise. In the concluding two slides I am going to describe first the way we will approach the evaluation as you would have understood by now I do not really associate too much importance to marks and grades. However, they are real life issues and all of you have to content for good grades and I have to do some evaluation. I try to put in every evaluation sufficient learning so that every test or phase is also a learning experience because I think that is our job together to learn as much as we can, but we have to get marks and we have to get a grade. So, here is how I propose to grade the students out of a total of 100 percent weightage for the marks in this course I propose to associate 10 percent marks to the quizzes. You have had some sample quizzes earlier, but we shall have in the third week of August or maybe fourth week of August a quiz which will be a regular 20 or 25 minute quiz in one of the lectures that will be a formal quiz with multiple questions and that quiz will amount to about five marks. The remaining five marks post mid semester we shall have multiple quizzes in every lecture. You will wonder how I will conduct multiple quizzes in every lecture without wasting time. You shall see the fun. I am actually getting a gadget designed for you and I am actually making about 1000 of those. So, I will issue one to each one of you like that Kaunbanekar Karodpati gadget if you have seen. So, I will flash a quiz and I will ask you to press A, B, C or D and I will collect your answer and give you marks. These gadgets actually are available, but the Phirangis who make these gadgets offered me one at 2500 rupees each and I did not have that kind of money. So, actually got it designed in the last two months, got it fabricated and tested and now we have to test what happens when 1000 of these gadgets work. They are costing me only 700 rupees. So, we shall we shall we shall use those here. Then we will have assignments. These assignments will typically be given in the labs. So, with every lab there will be an assignment announced. You ordinarily you are supposed to submit and upload your assignment in the same lab that you conduct Monday, Tuesday, Wednesday, Thursday. However, we might consider giving extra time for loading in case you are satisfactorily conveyed to your TA that you are actually completed that assignment. You have a hand written copy or something, but you just want to polish it or whatever to submit it later. So, all the assignments that you will do in the laboratories which you will be submitting on the model will constitute a 10 percent of waiting. You shall all be doing a course project that is the most exciting part of the course in my opinion. I am introducing group projects where you will have to make groups of anywhere between 3 to 5 students together who will actually solve a larger and more challenging problem through programming. This is not an easy task because I wish you to learn how teams of programmers collaborate with each other to come out with some meaningful software which is substantially large piece of software. It is not uncommon when I used to conduct such projects earlier. It is not uncommon to have anywhere between 2000 to 5000 lines of working code written by students. Naturally, this group project will extend over a long period of time. The group projects will be announced just before the mid-same and they will continue till the end. But you will have to form the groups ahead of time. These groups must be from your same lab group. So, now that you know your lab colleagues, start identifying the people in the group. Naturally you would like to avoid sleeping partners. So, be careful that you choose the right groups. It will have as much as 30 percent weightage. The evaluation of the group project itself is slightly complex. I shall explain it to you when time comes. The mid-same exam will be 20 percent and end-same exam will be 30 percent weightage. Minimum passing grade I have fixed at 40 percent. That means any student who gets 40 percent marks or more will not fail. That is guaranteed. I hate failing people. There is another reason why you might fail. In the assignments or quizzes or exams where copying or using anybody's help is explicitly prohibited. Anybody found indulging into these activities will be immediately awarded an F grade and there shall be no discussion thereof. That is one thing I hate. So, if there is a home assignment which you are supposed to do on your own, not for a single second you must discuss that with anybody. You must submit that assignment yourself. I do not mind a blank paper submitted. I value it far more than any quick copying from anybody because the purpose here is that you learn each one of you learn. So, this is something I have tremendous allergy to. In spite of these strong warnings I had in the previous occasions 20 years ago, every year I had to fail one or two students because I could catch them copying. It is very unfortunate. I feel very bad just like those who fail, but I guarantee this. Anybody indulging in any unfair means in this course throughout will definitely get an F grade immediately upon detection of that unfair means. So, please avoid that. Keep that farthest from your mind. The purpose here is to learn and I think there are enough marks and grades available without doing any such nonsense. Is that ok? Fine. Thank you.