 So, what I am going to do is once again I am just going to show a brief recap slides which are basically slides that you would have already as part of the video lectures and then after that the first thing that we are going to do is we are going to have a recap quiz which is really trying to test whether you have really seen and whether you really remember what you have seen. So, this will be a quiz where I will show some questions, A, B, C, D are the options. So, you have to write down your answers and then hand it over to your TA. So, in a sheet of paper, write your roll number and question one A, question two B, question three C whatever and hand it over to your TA. These, your performance in these recap quizzes will be monitored. So, that we know who all are you know diligently looking at the video lectures and who all are just skipping the video lectures. I mean if you have seen the video lectures, they should be very straight forward. They are almost straight from the video lectures. And after that we are going to look at real problems that we are going to solve in the class today. So, here is a quick recap. This is the generic iteration construct. So, in general we have some part of the program before iteration, some part after the iteration. And then depending on what construct you are looking at, you could have some initialization part and then you could have the actual repetition construct where you look at a logical condition. So, this is the generic iteration construct, the part before iteration, part after iteration and this is the iteration part. And you would have already seen these in the lectures that this part is typically called the loop. That is called the loop condition which is a logical condition which determines how long you are going to loop around. And the loop body is typically the part that you keep executing every time you iterate around the loop. So, that is fairly straight forward. And then the specific looping constructs in C plus plus that you would have seen in the lectures is the while statement where the loop is basically written like this. You have while this is a keyword, you put the loop condition here and then you put the block of statements which is the body of the while loop in there. The next construct that you would have studied in the lectures is the do while construct which is you have this do and while keyword. The looping condition has now come at the bottom at the end and you have the block of statements, the body of the do while loop in between. And the third construct is the for statement where we have some iteration initialization, some loop condition and also something to execute at the end of every iteration. We will see more about these specific parts in the subsequent lectures which we have covered on Friday. But at least for today you can think of these as you know some kinds of instructions to initialize the iteration which is kind of this part in the generic iteration construct. You set up some initial values and then there is the loop condition and then you have something to execute at the end of every iteration and you put semicolons in between them. These do not denote the end of executable statements, but they are just to separate the three parts of the for construct. And at the end you do not have the semicolon because these are just used as separators. They are not used to denote the end of any statement and then you have a block of statements which is the for loop body. So, is this clear everybody? I mean I am just showing slides that are already there in the video lectures. So, now what I would request all of you to do is to take a sheet of paper, write your roll numbers on it and then when everybody is ready I am going to start showing the recap quiz and all the TAs who are assigned to your block of students at the end of the quiz please collect the sheets and we will compile the data and we will figure out. So, everybody please write your roll number and then there will be question one, question two, question three, question four. Write down the question number, no need to write the question and the options that you think are correct. Is everybody ready? Please write down your roll numbers ok. So, here is the first question and if you have seen the video lectures this has been discussed in detail in the video lectures. So, this question asks that why not simply repeat code as many times as we need? Why do we at all need iteration constructs in a programming language? So, there are four possibilities you could indicate more than one possibility if you think if you think all of the above are true then of course, D is there, but if you think not all of the above are true then you could write A and C or. So, please write down the question number and the answer on your sheet of paper ok. We will move to the next question in which of the following constructs the loop must iterate at least once. Just write question two and whatever answer you think is appropriate to the next question. So, in a simple conversion of a do while loop to a while loop this was covered in the video lecture which of the following are true and more than one may be true. The options are the loop body must be replicated, the loop condition must be replicated, both must be replicated and one, but not both. So, we go to the next question. So, here is a for loop for assignment x condition y assignment z this loop body w. So, what is the sequence of execution evaluation for the for loop? So, it says consider the following for loop for x assigned y x greater than equal to 10 and x is x plus 1 then the body of the while loop is y is assigned y minus 2. Say suppose we start executing this loop with the values of x and y both as 10. So, how many times is the assignment x is y executed and how many times is y is y minus 2? So, this is the last question of the recap quiz. So, those of you have finished you can give it to your TAs. So, we want to compute the factorial of a non-negative integer using the looping constructs. So, what we are going to do is we are going to show you a skeleton of a code C plus plus program and you should fill in the parts there and you know different people can fill in different ways there is not necessarily one unique way to solve this problem, but we would like to understand how you have done. So, here is the skeleton. So, let me tell you what this skeleton is about. So, it has a main function there are two variables n and i which are of type integer declared there is this variable factorial n whose type I have not declared. So, these red question marks are where you have to fill in something and of course, we would like to understand why you are filling in something over there and then give n and you read n o and then for i assigned something i less than n i is i plus 1. So, note that you cannot change the part of the program that is already in black you are only supposed to fill in the parts where there are red question marks such that finally, we get that a program that computes factorial n. So, finally, the prints factorial of n is factorial n. So, clearly we want the factorial of n to be in this variable. So, you have to fill in these three parts. What should be the type of factorial n? What do you think it should be and why? What should be this initialization and what should be the code here such that does that we get factorial n then? So, first write down yourself you know take two or three minutes to write it down yourself in your copies you can write down this program no no no we want i less than n. So, you cannot change the black part of the program. In fact, that is part of the question given this code as fixed can you write the remaining parts replacing question marks with something tangible such that the program still finds the factorial n. So, now do the normal ritual of exchanging your answer with your neighbor and convincing your neighbor that your answer is right. So, how many of you had disagreement with your neighbor please raise your hands. Everybody agreed with the neighbors. You had a disagreement good. People are not bold enough to agree that they disagree. So, who won the argument? Which one of you got to one the argument finally or both were wrong? Both of you could not we are not able to find me. Very interesting. So, I will show you some answers which I have collected let us together dissect these answers. Let me read whatever is written here this is by Rishika Dasani she declares factorial n as long int. How many of you agree with this? How many of you disagree? Those who disagree keep it to yourself. I will ask you this question why. She says I should be initialized to zero and then the expression she has is factorial n equal to factorial n star i plus 1. We agree that you will find the correct factorial? Anybody who says no? Everybody says yes. So, what was the problem with people have problem with long int right? We will park that problem look at another solution. Long factorial n for i equal to 1 and factorial n is equal to i plus 1 star this. So, how many of you think this is correct? So, long factorial n people have a problem with this should it be long int? Sure you think that is the right solution. i equal to 1 is correct yeah but people also said i equal to 0 was correct. How can both be correct? So, which one is correct? i equal to 1 is correct? Will this find the factorial n correctly? Earlier solution also could find the factorial correctly not a problem. Here is another solution by Saransh Kulkarni. Our friend does not like to write long programs he just writes cryptic answers. So, fill in the blank slide. He believes that factorial n should be unsigned long. How many people disagree with this? Why do you disagree? Just because you do not like his name or what? You have to give a reason why you decide. So, what is the difference between long int and unsigned long? Let us look at it this way. If you had just said int you had some problems. So, you decided to use long. What was the objective? You wanted to increase the range of the numbers represented by that variable. So, that you have a larger range. Now between unsigned long and long which has a larger range as far as computations of factorial is concerned unsigned long gives you one more bit. Only long will not it will represent negative numbers as well which in this case is not required. How would you find the factorial of minus 27? Exactly from unsigned long it will find out. The program will give out a result. That is what I am saying because if you enter minus 27 the you learn. Okay. We shall soon learn that people who write programs to find out a factorial of an integer n are required to check after they read n and if n is negative they are required to use a c out statement saying gali, gali, gali is not correct invalid input. Now that is assumed here. We are not interested in finding out this thing. Now given the circumstances that we assume that the number given is a positive number which one is a better choice. Any other objection? Everybody agrees with initial value of i to be 1 but many people said i equal to 0 was also alright. Will you get the correct answer? Yes. And one also you will get the correct answer. So, it does not matter if I write 5 also you will get the correct answer? No, not bad. All right. So, let us go to the next question. I hope everybody has understood. So, in this problem we want to calculate the nth power of an integer m. So, basically m raised to n we want to calculate that by a looping program and once again we want you to complete the missing part of the code. Okay. So, forget about that line going in the middle and the int main I had to delete. So, assume that that is there. So, once again there are blanks here and this uses a do while loop to calculate m raised to n. The earlier was was a for loop this is a do while loop. So, the one thing that you must remember is that we have said n greater than equal to 0. So, this must also handle the case when n is equal to 0. So, how many of you have completed without taking care of the case of n is 0? I mean if I told you that n is greater than equal to 1 then how many of your solutions would work? Can you raise your hand if you think that your solution will work for n greater than equal to 1? Okay. So, let me simplify the problem. Let us say n is greater than equal to 1. So, of course, all those we have got a solution where for n greater than equal to 0 their solutions will also work for n greater than equal to 1. So, let us say n is greater than equal to 1. Then so, how many of you have not completed? Can you raise your hands and completed assuming n is greater than equal to 1? So, there are a whole bunch of them who are neither completed nor not completed. Okay. So, I think exchange your copies with your neighbors and please try to discuss as quietly as possible. One of the things that you could try to do is to just provide some simple inputs. Maybe n is 1, n is 2 and see whether it is really giving you what you expected to give. If it is not giving you then of course your answer is incorrect. So, Shweta has this solution. So, she has initialized result to 1. So, how many of you agree that that is the correct initialization? Disagree? And no opinion whether it is correct or wrong? I mean either you agree or disagree you do not have an opinion, right? Okay. So, let us proceed with result is 1. And then i has been initialized to 0. And then let us look at what is done within the do loop. So, there is an if else statement within the do loop. And if what is that n equals n equals 0 is it? So, if n is 0 then the result is 1 and then you break. So, everybody has seen the break statement in the video lectures. I mean those of you who have seen the video lectures there are some of you who haven't, right? So, you can use the break to just break out of a loop at any point. So, Shweta uses it here saying that if n is 0 then assign result 1 to result and break out. Otherwise if n is not 0 then result is result times m and increment i or i plus plus this is not even covered in those 3 lectures. So, you have gone ahead but that is okay this is i is i is assigned i plus 1 that will be covered in the next lecture actually and while i less than n. So, okay so how many of you are in agreement that this actually. So, she has done a fairly smart thing here she has taken care of the n is equal to 0 case by a separate if statement and put the break here which is a perfectly legitimate way of writing the program saying that yes. Why do we need to write the result assigned 1 it is already written. Why do we need to assign result 2 and this one in the if statement. Oh it is already there that is very good in fact that is exactly the solution I saw from somebody else. So, that is a good point I mean we do not need that and that is exactly what some of you have written. So, you can see here n equal to 0 is simply handled by saying if n is equal to 0 equal equal 0 just break because the value of the result has already been initialized to 1 you do not need to initialize and otherwise result is simply multiplied by result into m and you iterate around while i is less than n. This solution is by one cow shake. So, this is interesting because there is no else part here right and you would have seen in the lectures for conditional execution that if you do not have an else part it is like saying that it is this f this if with an else saying nothing right and then this is going to be executed. So, these two statements are outside the conditional execution right. So, this is the conditional statement if n is equal to 0 break else do nothing and then after the conditional execution you have these two statements and because she has put a break there therefore, it is effectively like putting an else there because if n is equal to 0 it will break otherwise it will fall through the conditional part and just execute the. This was from cow shake here is another one by Sheik Abdul Basit. So, he also starts with result equal to 1 i is 0 there is too many things here if I cannot even read it if i is not equal to 0 result is equal to m star result. But where is the do is before this or after this and the do simply says i equal to i plus 1 by Lail as the name. Sheik Abdul please raise your hand yeah. So, can you tell what you meant there is that entire block of code supposed to be inside in the body of the do. So, our friend actually first wrote i equal to i plus 1 then he realized many things are there. So, he started writing them on top of this unfortunately the computer has to read reverse Japanese in order to understand this from bottom to top which is not feasible. So, you should write it properly but I think this program will work. There is one more thing here a lot of drawings here. So, we will ignore them and no name apparently our drawing master does not like the name to be revealed. So, here also if n is equal to 0 break please appreciate this fact that the break statement is very powerful you will just get out if n is 0. Otherwise there is an else statement which is actually not required the else statement says this result is result into m i is equal to i plus the basic logic is simple you keep multiplying the result by m repeatedly keep increasing i and as long as i is less than n keep doing it at the end you will find the normal power alright. So, good efforts there is one more here. So, here is another the interesting part is he just says if n not equal to 0 do this which is actually equivalent very interesting contraption of saying that do this only if n is not equal to 0. This is by whom is not known no name alright. So, now we want to do this exponentiation business raise m to n, but suppose I told you that n is a power of 2. So, the only n's that we are interested in are powers of 2 and then we can use this method of repeated squaring to compute any integer raise to a power of 2. So, n squared is n times n and raise to 4 is n squared times n squared and raise to 8 is n raise to 4 times n raise to 4 right. So, in so interestingly the in the previous question also I mean it would have been correct if one of you had used repeated squaring to compute right, but interestingly none of you did that. So, let us do this what if n is not a power of 2. So, that is the next question after this how do you use repeated squaring if n is not a power of 2. So, I am not sure if you will be able to get to that question, but at least I want to show that question. So, is this clear I mean did anybody use this technique to compute m raise to n in the previous question anybody used it no ok. So, good. So, then for everybody this should be a new problem then. So, we want to do this right. So, we are required to write an iterative program that takes in n and m checks whether n is a power of 2 and if so. So, this is just the case where n is a power of 2 it computes m raise to n using repeated squaring ok otherwise the program exits with a return code of minus 1 ok. So, in this particular case I am asking you to write the entire program you already have the skeleton from the previous one right and you have to put this check whether n is a power of 2 and then if so then you have to compute m raise to n ok and for those of you who completed the next question is basically how you would extend this if n is not a power of 2, but let us just stick to the power of 2 case for the time being. So, as you can see that you know if I were to compute n raise 8 using the previous techniques that all of you did I would have to do multiplication 8 times right whereas, here I need to do multiplication only 3 times starting from n I get n raise to 4 then n raise to 8. So, I can do it a slightly fewer number of multiplications actually significantly fewer I mean this is 3 and this is 8 and we want to use that in our program to compute m raise to n where n is a power of 2. So, you also have to check whether n is a power of 2 this is input validation. So, there is no exponentiation operator in C plus plus that we have studied. So, or there is no log operator in C plus plus that we have studied. So, please do not use those. So, there are two parts to this question one is checking whether n is a power of 2 and if so computing m raise to n and how many of you have solved part of this problem either computing m raise to n using repeated squaring and how many of you have computed solved checking whether n is a power of 2 ok. So, yeah so please swap your copies with your neighbors and please for the part that you have worked. So, there is no one unique way of solving this problem we have collected a couple of copies of people who have agreed to show their solutions. So, it is actually ok if you and your neighbor have two different programs both of you may be correct or both of you may be wrong ok. So, this is one more ok. So, this is Tejesh. So, let us see what Tejesh has done he has a do loop do while loop here in which he is checking whether n is a power of 2 ok. So, how does he do that? He initializes i with 1 and c with 0 and then in this loop he keeps multiplying whatever the value of i is it keeps doubling it right. So, for example, if this loop iterates once then the value of i becomes 2 and the value of c becomes c plus plus for those of you who have seen the next three like seen this business of c plus plus, but that is the same as saying c is assigned c plus 1 this you can think of as c is assigned c plus 1. So, if you iterate this loop once the value of i becomes 2 the value of c becomes 1 if you iterate this loop 2 times the value of i becomes 4 the value of c becomes 2 if you iterate this loop 3 times i is 8 c is 3. So, you see that what Tejesh is really doing is in this loop is actually calculating i as a power of 2 and is calculating that power of 2 which is log of i base 2 in c right. So, very nice solution and then how long does he iterate as long as i is less than n right. So, he starts from 1 and he keeps iterating until i becomes less than n ok. So, and then if i is indeed a power of n then eventually i will become equal to n right and then this i not equal to n will not be satisfied and this is the part where he is calculating m raised to n by repeated squaring but if i is not a power of n then eventually this power you will get a power of 2 which will exceed n right. So, suppose i is suppose n is 5. So, i starts with 1 then becomes 2 then becomes 4 then becomes 8 right. So, when it becomes 8 then i is no longer less than n you come out of it, but then i is not equal to n and so, then you return minus 1. So, this is checking whether n is a power of 2 the problem required you to first check whether n is a power of 2 yeah. So, just see how i is increasing i is first 1 then 2 then 4 then 8 i is will doubled every time. So, when I say while i less than n it will just check whether the value of i is less than n the first time when it comes here actually the value of i is 2 the second time I mean saying i less than n here does not mean that the loop iterates n times remember the loop we said there is a loop condition as long as a loop condition holds it will iterate. So, the first time it comes here i is 2 the second time it comes here i is 4 the third time it comes here i is 8. So, this equation i reaches 8 right and then this part the next part which is here is just doing this iterative squaring and computing m raised to n and how many times do you need to square it. So, if n was 8 you need to square 3 times right n to n squared n squared to n 4 and 4 to n raised to 8 if n is 16 you need to square 4 times. So, the number of times you need to square is exactly the power of 2 the exponent of 2 and that Tejesh has already conveniently computed here in C this was exactly the power of 2. So, he is now going to iterate this loop you know he reinitializes i to 1 i is incremented each time it goes around the loop he executes this loop exactly c times and his squares m that number of times. So, very nice solution does it take care of the fact when n is 1 1 is also a power of 2 2 days to 0 is 1 right. So, does it take care of the fact when n is 1 let us see. So, in this case if n is 1 then I guess since it goes around this loop in any case right. So, you will get right. So, if n is 1 it will go around this loop i will become 2 and you will find that 2 is not less than 1 right. So, if n is 1 then when you go around this loop once i has the value 2. So, this condition will be violated and you will return minus 1. So, Tejesh there is a small problem there for the case where n is 1 so, power of 2, but I hope you see that that is because you mandated that this loop must be executed at least one time right. So, if you that is what a do while loop does a while loop does the check first and then it executes right. So, good very nice solutions. So, here are some other solutions by the way this is not the only way to solve it. For example, let me give you a suggestion for those of you who want to do it in your rooms. So, Tejesh has actually used two loops over here you can indeed do it with one loop you do not need two loops yes. So, I am done I have shown one solution if you want to leave and in fact, I should wind up. So, let me not take any more time there are a couple of other solutions that we got we will discuss them in the next class.