 I'm going to start the quiz. It's almost 10 minutes. So here is the first question Questions will not be repeated. So if you're coming late, you just miss that part of the question Okay, and we'll just provide one minute per question because these are straight from the video video lectures Okay, so that's the first question consider the declaration Int my encode int a int b which of the following are true My encode is a variable of type int my encode is a function that returns a value of type int My encode is a function that can be called only from main Mine code is a function that can be called with zero one or two parameters more than one answer may be correct You get exactly one minute to answer. Please indicate your choices We move on to the next question which of the following are true of my encode declared in the same way as in the previous question And we are formal parameters of my encode and we are treated like local variables of my encode Main can have variables a and b and within function my encode we can declare local variables named a and b One minute to answer that question and then we'll move on to the next one keeping track of the flow of control in a program with functions can be best done using First in first out access last in last out access first in last out access and last in first out access Okay, we move to the next question which of the following is stored in the call stack of a running program Number of statements in each function in the program Where to resume execution in the caller function once the callee function returns Number of times each function in the program is called Values of local variables in caller function when it calls the callee function Okay, we move to the next one Which of the following are true about activation records each function call creates a new activation record Activation record of callee is popped from the call stack when callee returns Activation record of caller is popped from the call stack when callee returns and Activation record contains information about where to resume execution after callee returns Okay, next one Which of the following are true about call by value parameter passing? Parameter values are copied from the caller's activation record the callee's activation record Changes done on local variables of the callee are lost when the callee returns Allows caller and callee to effectively share variables and both variables and constants can be passed as parameters In each question there may be more than one correct answer. You're required to indicate all the correct answers The last one which of the following are true about call by reference parameter passing Can lead to significant savings in memory required for call stack Both variables and constants can be passed as parameters Allows caller and callee to effectively share variables And cannot be used if another parameter is passed by call by value So that's the end of the quiz. So Let's quickly go through a recap of Three of the lectures the first three lectures on functions in this class We will cover functions and parameter passing and flow of control So here is one of the slides From the video lectures basically a program with functions in general looks something like this that you have declarations for the functions And then you have the main Function from within which you can call some other function So I presume all of you have seen the video lectures So you know exactly the problem that we're talking about over here we were trying to encode two quiz marks as one integer and then this function my encode has its own precondition post condition and It in turn calls another function called power which in turn has its own precondition post condition And that is how we can write that this is how the overall structure of a program with function calls looks like So here the functions are not recursive. The same function is not calling itself However, one function can call another function Okay, function is really you are encapsulating a computational sub task giving it a name and You're allowing parameters to be passed to it Okay, so the contract view of functions is that when you're using a function You may ignore How the function is doing what it is supposed to do as long as you know What the function expects you to respect and as long as you know what the function is going to guarantee if you respect It's precondition. So this is really like a contract The function says that you respect the precondition and I will respect the post condition and you need not worry about what I am doing inside Okay, and then there is this little of animation of the flow of control Which basically is trying to say that when you actually encounter a function call There is some additional bookkeeping that happens. This is the activation record that gets pushed on to the stack and then You know as you execute other instructions and get the next function call again some bookkeeping happens And then you go to that function and then when you're returning Then you actually look at the bookkeeping information and that tells you where to go back Right, and then you start executing from there and there is another function call Then you again come do the bookkeeping come to the appropriate function finish that and then go back Use the bookkeeping information to figure out where to go back to and then finally You execute the remaining instructions and whenever you're executing return You have to consult the bookkeeping information to figure out where you're coming back to so then the video lectures talked about activation records in the call stack right so that keeping track of Which function called which function and therefore where should I return this is basically maintained in a stack a stack in is a last and first out access mechanism and so in this case when Power is called from my encode the activation record for power is created the values of the function parameters here It is called by value So the values of the function parameters are copied from the activation record of the calling function to the called to the activation record of the called function The program counter value is stored. This is basically where the execution must resume After the function returns and then some other bookkeeping information is done and then this is pushed into the stack Okay, this is the call stack and similarly when The function power returns its activation record is popped out You copy the result that is whatever you're returning over here This result will be in the activation record of the called function you copy that from there to the activation record of the calling function and Then the value of the PC that was saved tells you where to resume execution in the calling function and Then you free the activation record of the callee and then you resume instruction at whatever value of PC was saved So this is straight from the video lectures Call by value parameter passing is when values of the function parameters are actually copied So you actually get a fresh copy of the values From it's copied from the activation record of the caller to the activation record of the callee, which is that animation what showed and The formal parameters of the called function are just like its local variables and they will never be confused With the variables of the calling function right, they are two different things they are local to each of those functions and So if you're doing call by value then the only way in which the callee can let the caller know What it has done is through the return value Because otherwise it gets a copy of everything and it just works on those copies It doesn't work on the actual variables of the calling function, whereas call by reference actually allows you Allows the calling and the called function to both refer to the same variables. So you have this ampersand before In both of these cases. So these are no longer local variables of the called function But they are actually aliases or references To whatever the caller used when it called this function. So the caller use these variables So these are actually references or aliases to these variables Which means that within this function whenever you're using M. You actually mean The variable that was used in place of M when you called the function that is a Similarly wherever you're using n it means the variable that was used when you call that function in place of the parameter n and So that is roughly the material that we're going to have practice problems today on Are there any doubts about it? So just function calls flow of control and parameter passing. These are the topics we'd like to discuss Okay Okay, good. So let's go to our first practice problem. So you're required to write a C++ function called int square root So let me tell you what in square root does it takes a non negative double input parameter Okay, so it takes a non negative double input parameter and it returns a double value such that The fractional part of whatever it returns is always zero. So that is why there is this int in the name Okay, so it will return a double value, but the fractional part of the double value is always going to be zero okay, and Whatever it's going to return is always going to be non negative and If I call this function with x Then the value that it returns will be such that This relation holds Basically, you can see that it is somewhat like the square root of x But it's kind of not going to give you the exact square root because x is a double number whereas the return value has only an Integer part it has no fractional part, right? So whatever it returns if you square it up, you're guaranteed to be less than or equal to x and Whatever it returned if you add one to it and basically go to the next integer and then if you square it up You're guaranteed to be greater than x. Is this clear? so for example, if I take 4.2 then this should return 2 Right because 2 squared is less than or equal to 4.2 and 4.2 is less than 2 plus 1 whole squared, which is 9 right However, if I take 4 4.0, then it should return 2 again Right because if this x is 4.0 Then if this is 2 then 2 squared is 4 which is equal to 4 And indeed that is less than 3 squared, which is 9 Okay, I cannot return 1 because 1 squared is indeed less than or equal to 4, but 4 is not less than 2 squared in square root x if x is 4 Cannot return 1 because if it did return 1 then this inequality will be violated And because 1 plus 1 squared is 4 and we require 4 to be less than 4 so that cannot Right so it's really you know a lot of words to say that Take a non-negative number Compute its square root and give me the integer part of that square root Okay But of course, I mean you know computing the integer part of the square root may be much simpler than Computing the exact square root and then taking the integer part out of it. So that's the whole purpose of this question Okay, so here is how I have sort of put the basic skeleton for this function So it's a double in square root double x Within the function, you'll have the result which should also be double because it should return double and then you can do some input validation Remember that this function only takes non-negative Otherwise you cannot compute the square root and then you have to write your code to compute the in square root And then the result should be returned. You're basically computing the integer part of the square root of a double number And this is the relation that we want Okay, so so let's look at some of the solutions and maybe several of you have similar solutions If x is less than 0 return minus 1.0. So Just returning minus 1.0 is not yeah, so you should print out some message saying that it's not correct or something Okay, but basically this is the input validation part that you're doing. Okay, good and then you have int temp And int temp 1 is x. Now, this is of double and this is of integer So did you do this intentionally or you want to take the integer part of x? So that is why you've done this assignment Okay, but I Mean using double you can represent much bigger numbers than what you can represent Using integers, right? So when you do this assignment, it may not just take the Real integer part of x, right? This is likely to have problems if the value of x is The integer part of x is much larger than what can be represented by an integer, right and Then for temp is x temp is assigned x Temp star temp is greater than equal to temp 1 Temp is assigned temp minus equal to 1. Okay So you're starting from x Decrementing it by 1 and checking whether this product is still greater than equal to 10 It's still greater than equal to x temp 1 was x Okay, so So does anybody see any problem with this So starting from x rather starting from the integer part of x is Decrementing it by 1 and checking whether the square of that is greater than equal to The x that he already had the integer part of x that he already had But I mean did you have a loop which was iterating and Repeatedly squaring to find out so this you know the primary problem that you have is over here right because when you assign a double to an integer you may not actually be able to even represent the Integral part of the double number Even if you use long end Right you you look at the lectures and look at the range of double the range of numbers that it can represent And the range of numbers that can be represented by a long end Double can represent. In fact that is one of the reasons we have double representation, right? Now here is another solution. So int i is 0 So so if x is 0 return 0, okay So you're assuming that x will not be less than 0. So if x is 0 you return 0. Otherwise you keep incrementing i Until i is less than or equal to x And if i star i becomes greater than x then result is i minus 1 But then once you have this result, why do you want to continue incrementing i and Going all the way up to i less than or equal to x. So so this solution appears correct, but What has happened here is that even after the result is calculated it will keep on iterating in this loop for no reason Okay, so it's a correct solution, but it's not going to be an efficient solution probably And so you could have put a break here after saying result is something you could have put a break So that it comes out of the loop. So I don't know why you are so reluctant. I think you almost have the right answer So for i is 0 i less than x over 2 So that's very interesting. So why did you not want to iterate beyond x over 2? Yeah, but Can the square root of a number be more than half of that number? Because here you're iterating only from i is 0 to i is less than x over 2, right? So and you're going to return the result as some i So if the square root Could be hypothetically more than x over 2 then you will not even catch it Right, but can the square root be more than x over 2? No, so oh, this is an i is an integer, okay So I so x is a x is a double so x by 2 is going to be a double quantity so i is less than x over 2 and 0 times 0 is less than square root 2 and 1 times 1 is not greater than square root 2 so he will iterate the next time So it will be 1 and 1 is less than 1. So so is that what your doubt is that 1 is less than 1.414 Okay, so so x by 2 is it correct or is it incorrect wrong, okay? So what would be an example to show that it is wrong, okay? So if x is 1.2 then what happens x over 2 becomes 0.6 okay, so this will just stay at 0 and So if it is 0 then 0 star 0 is not less than 1 0 star 0 is less than 1.2 and 1 star 1 is not greater than 1.2. So he will not execute this He will go back into the loop. I will become 1 But 1 is not less than 0.6, so it will just come out and it will return So it will return actually in square root without even assigning anything to in square root So it may come out with a garbage value right, but I think you know I Hope you see where You have made some assumptions here which which have to be validated against all the examples, right? But overall I think your logic was correct that i star i and i plus 1 star i plus 1 But here your termination conditions have to be slightly different, okay? What we'll do is now we'll go to the next one So this program Takes the coordinates of three points each of them in three-dimensional space basically it takes some x1 y1 z1 x2 y2 z2 x3 y3 z3 and It finds which one of them is farthest from the origin and which is nearest to the origin Okay, so you're given three points and this program finds which one of them is farthest from the origin and which one of them is nearest to the origin The program then prints the coordinates of the nearest and farthest points Along with the integral part of their distances from the origin, okay? And so here is where we're going to use the previous thing. So each point is represented by a triple of floating point coordinates x y z and the distance of the point x y z from the origin is the positive square root of x squared plus y squared plus z squared Right and you are required to just print the integral part of the distances from the origin So if you can compute x squared plus plus z squared and then if you had your previous function in square root Right, basically what in square root should do is it should just start from the integers start squaring them until it exceeds The input parameter x in the previous problem and that will tell you exactly What is the integer value whose square is less than the less than or equal to the input parameter? But if you add one square is greater than the input parameter So you have to use that so in this problem you can just use in square root as a black box, right? So this is programming by contract. So now knowing that in square root was discussed in the previous Question and we have some way of getting in square root All we care about is if I give it a double number which is greater than equal to 0 It will return me another double number Which is the integral part the fractional part is always 0 integral part of the square root Right, so so I believe you know this everybody is familiar with that coordinates are going to be represented by x y z and The distance of x y z from the origin is of course the positive square root of x squared plus y squared plus z squared And we want to find Which point is farthest which point is nearest? So note one thing that if I give you 5 and 6 Suppose for one point the sum of the squares of the coordinates is 5 for the other point the sum of the squares of the coordinates is 6 If you take in square root of 5 and if you take in square root of 6 both will return 2 So you cannot look at the value returned by in square root to really figure out which is nearest and which is farthest Right, so just keep that in mind. So in square root is going to give you the integral part of the square root But then we really want the nearest point and the farthest point and Just looking at in square root is not going to tell you which is nearest which is farthest So in square root is really needed to print out the integral part of the distance Otherwise you actually don't even need the square root function to figure out which is near this farthest and I mean it is possible to solve this problem using the in square root function But it's also possible to solve it without that But for printing you will need the in square root function so All the three coordinates are all the three points coordinates are read in that's good and Then the squares of the distances are calculated So this is really d1 squared and d2 squared and d3 squared probably so you should put meaningful variable names and Then he says max is d1 and if d2 is max max is d if d2 is greater than max max is d2 And if d3 is greater than max max is d3 So, okay, that's one way of finding Which is the largest of the three distance squared, right? He has calculated the distance squared over here and Then he's calculating which is the largest of the three distance squared And then see out max But see out max is not going to work See out because max is this square of the distance So here you could call in square root and say that see out in square root of max And you also have to print out the point The the point the coordinates of the point the program prints the nearest and farthest points Along with the integral part of their distances So you also have to print the point so you have to keep track of whether it's the first point of the second point of the third point And then for the min you can do the same thing. Okay, so reading in x y z then Oh, there is a function here. Wow, this is good. So there is a function which is called to get x y z and then Sum up the squares of the coordinates and it returns the sum So this is a use of a function very good. Who is this a grim? Good, so you have used a function to basically read in and compute the square and directly return the square and then you do all of these comparisons right to figure out which is the max and which is the min Somewhere here in square root is called to print it out. Okay, so I guess the the general idea Everybody seems to be working on is to use the squares and compare So certainly if the distance of one point is greater than the distance of the other point the square of this distance will also be greater than the square of that distance But somebody here use the square root But she has not completed her solution. So this problem can also be solved using the integer square root Basically if you take three points and instead of computing their distance squared you compute the square root of Their distance squared right and look at the integer part of those So if they are already ordered the integer part of the square root of one is greater than the integer part of the square root of the Other and certainly the distance of one is greater than the distance of the other But if the if the integer parts of two square roots are the same Then you can't say that the actual distances are the same So then all that you need to do is You will need to go back and check the squares Right only for those for whose for whom the integer part is the same you need to go back and check the squares I just take two three minutes to show the problem. So everybody knows this game of tic-tac-toe You draw lines and then you put crosses and tics or whatever right So we want to write a c++ program finally to play the game of tic-tac-toe I think whatever you have learned so far should be sufficient to write such a program Which which plays with you which plays with the user a game of tic-tac-toe So what is the game of tic-tac-toe you basically put zeros and ones and Alternately one player put zeros other player puts ones and whenever you have either a vertical row of the same numbers or a Horizontal row vertical column or horizontal row or diagonal then that player wins You know this this game of tic-tac-toe Can be represented using the the configuration of this grid Can be represented using nine numbers, right? So for example, I could first read the numbers like this the first row so 0 1 0 Then whatever is empty. I can put a minus 1 there so minus 1 1 minus 1 and 0 1 minus 1 So if I give you these nine numbers that corresponds to this configuration of the tic-tac-toe Right, so this this game everybody knows this is just saying how do I represent a particular configuration of this game? You can use nine integers between 0 and minus 0 1 and minus 1 to do that Okay, so I mean there were a lot of parts here But basically this is what we want your program to do, okay? So this is the grid and In the grid these various positions are numbered 1 2 3 4 5 6 7 8 9 So your program First say so your program is playing with a user. It's playing with you and you are the You are providing an input of where to put the 0 So you say so the program says give the position of 0 you say 3 which is the user input So it means that at that position 3 a 0 comes in Okay, and then your program should then print a position of 1 Okay, so for example it could say that position of 1 is 5 which means at 5 1 comes in and then again the user gives Another input so let us say the user gives 6 which means a 0 comes in there at position 6 And then the program again says well the position of 1 is 1 Which means that the position 1 1 comes in and then The user if the user is smart enough you realize that you can now get this done So you say 9 and then it is done and then the program must be gracious enough to say yes, you are the winner Okay, and not crashed or something and Okay, so so we want you to design this program Using functions. I think it's it's important to use functions here because there are several things that are happening over here Right given a configuration you have to figure out where the next available places you have to figure out whether It's a winning position or not Okay, so please try this on your own and You know if anybody comes up with an interesting solution, please show us