 So, today we are going to cover 6 of the video lectures, however 4 of these are you know can be gone through fairly quickly. So, we will go through those fairly quickly. So, it should take the same amount of time may be 35, 40 minutes, but we want to basically cover the logical unit ok. So, the first one is introduction to software engineering. So, software engineering is you know let us just quickly. So, there is a lot of description here you can go through that, but sort of the kind of point that I want to highlight is that in general the software that you are going to design you know probably your project is going to be the first such software that you will design. It will typically be of large size, have complex functionalities, will hopefully be used by many people and will also be developed by many people and it requires significant time and effort. So, you know this is the important point that large coordinated effort is required to build operate and maintain software and I think your project is the first such instance of that. And what we are going to see in the next few slides is some things that we have you know repeatedly said during the course, but it is now documented there and things that we expect you to follow as you are going about with your project ok. So, in your project we expect you to develop a piece of software which is doing something complex has a non-trivial effort involved. So, for this purpose these are some of the metrics that are used to understand how complex a software has become. So, one is this lines of code, the other is function points and you know depending on lines of code or number of function points there are other metrics also that characterize the complexity of a software, but lines of code is often very commonly used and we say lines of non-comment codes you do not count the comments as lines of code. So, that kind of gives you a typical idea. So, we expect your project to be around you know maybe thousand lines of code thousand lines of non-comment code written properly and the effort is often measured in terms of how much effort was put in by a person if that person was working alone how many months would that person take. So, it is person months, so two persons working for one month is two person one person working for two months is also two person months. And this involves the when we try to quantify the effort we take the whole thing into account you know it is not just writing the piece of code, but testing documentation ensuring that all the you know error handling is done properly all diagnostic messages are given out properly all of that and this is kind of you know 15 to 25 lines of code per day would I think be considered you know bare minimum that you if you are working in a professional environment. So, they said we expect around thousand lines of code for your project the amount of effort is 3 or 4 of you working over a period of one month, but of course not working full time. So, you are probably working you know whatever one hour a day something like that ok. So, this is the usual life cycle this is what we have recommended you to follow for your project. So, there is a conceptualization aspect detailing all functional requirements. So, this is kind of what we have asked you to submit by Sunday at least the conceptualization at least some detail of the functional requirements if not all details then you design the system you develop the software and then you have also got to do testing and quality assurance and finally, you know just because you have developed a software does not mean that it is ok it has to be accepted by the client for whom ever you wrote the software. In the case of your project by us if you have written your project does not mean your job is over you have to do something which makes us accept it. And then of course, you know there is maintenance you know once you write a software and deploy it you know maintenance for it is going to be an ongoing process. Maintenance includes gathering bugs so, how many of you I mean all of you have been using code blocks in the C compiler. So, have you encountered any bug with the compiler the G plus plus compiler which is running at the back end have you seen an instance where it is doing something that it is not supposed to do nobody. So, the point I am trying to say is that I have personally not encountered any bug in the GCC compiler G plus plus compiler however, I have read papers which report about bugs in this compiler and then there are websites which report about bugs. So, it is a fairly robust piece of software developed over so many years by so many users, but still that has bugs you know and so to guarantee that a piece of software has absolutely zero bugs is a fairly tall claim of tall order I mean things which have been tested developed used for so many years so many decades actually still have bugs. However as time goes on the rate of bugs keeps decreasing and the software becomes even more and more reliable. So, at least for your project we would like you to do some corner case stress testing to make sure that you know some obvious bugs have been taken care of, but still there will probably be bugs present and for those who are contesting for the AP grade I mean certainly your code will be tested for lots of corner cases to expose bugs in the code and then of course you could enhance the functionality of the code I mean you might have designed something today and then there is demand for enhancing the functionality and you could also you might also want to interface it to other software applications which are coming up. So, this typically constitutes a very large percentage you know up to 80% of the total cost of a software's entire life cycle and this sort of emphasizes the fact that we must write or we must try to write very high-quality software in the first place. So, you know I mean some very grave consequences of software errors just completely programming errors that have happened was that around 15 years back there was an Ariane 5 rocket which was launched to go to space and it just exploded a few minutes into its takeoff and this was because there was some there were sensors which were picking up you know some acceleration data from the rocket as it was moving and there was a bug in the conversion of the data from one metric system to another metric system and because of that bug and of course the software was tested through a lot of things and it was certified as okay, but there was that corner case bug and that got triggered and the entire rocket just exploded. So, there are similarly cases of when you go to a hospital and for example have any of you seen a city machine, city scan machine or at least a picture of a city scan machine? Yeah, so it's basically you know a machine through which the patient's body goes through and x-rays are passed and you take a lot of images. So, it basically shows you as the body would appear if you were to cut a section through the body without of course cutting the body, but this is actually exposing the patient to a lot of x-rays and so there is software which is controlling how much of x-rays are pumped into the patient's body as the patient's body is passing through an x-ray machine and bugs in that software have actually caused some patients to be overexposed to x-rays which have caused other kinds of complications in there. So, the more we become dependent on software the more it's important to write high quality software and of course when you're writing software coding style is important. There are recommended coding styles, variable names should be written in a certain way, type name should be written in a certain way, function name should be written in a certain way, indentation should be done in a certain way. This is to ensure that it is readable, understandable, others can take up your code and modify it if needed and there are several references which are there which you can look up. So, are there any questions about this? I mean this is more like what good programmers should do and we expect all of you to be at least moving in that direction and so we expect you to keep these in mind. So, I mean as I said you know a very large percentage of the cost of developing a software actually goes in maintaining it and if you can write code which is good quality make sure that bugs are few you actually cut down that significant part of the cost. Okay, so if there are no doubts about this let us move on to the next topic. You know our first glimpse of what is called object oriented programming and we are going to start off with structures. For those of you who have gone through the video lectures of course you know this should be fairly straightforward but for those of you who haven't let me just say that you know here we are trying to design a book checkout return claim management system of a small library like in our central library when you go you say I want to issue a book, I want to claim a book or I want to return a book. So, there are three kinds of operations that we will define checking out a book, claiming a book and returning a book. Checking out means you give a book and then the book is issued to you right and so in this case a pattern can check up to three books at any time claiming a book means if I have not already checked out three books and if I want a book and that book is currently with somebody I can place a claim for that. So, when the book is returned I am going to get it and returning a book is of course returning. So, how do we go about writing code for such a library management system? So, one option the bad option the non-recommended option is to just jump into writing the code and then keep adding variables arrays functions as in when you need. So, for each book as you will realize there are lots of information that you want to keep you know some title and authors these would be character arrays, price, accession number, checkout status, claimants ID and so on. So, what one and similarly for each pattern there are lots of you know things that one might want to remember name address number of books checked out and so on. So, what one could do is that if the library has thousand books it is a very small library. So, then I could say that well I am going to keep a thousand sized array where each element of the array is a string or a character array representing the title of a book. So, there are a thousand books. So, these are thousand sized arrays each element of the array is going to store information about a book. So, I could have an array of character arrays storing the book title and array of another array of character arrays storing the author's names and array of doubles storing the prices of the different books and so on right. But I mean you will appreciate that this is not a natural way to represent the information right. The information about the same book I am splitting it across into 6 different arrays this is probably not a very desirable way to do it. And in particular if I ask you to do the following if I can say that can you find the claimant for the book with some accession number 1, 2, 3, 4, 5, 6 then what would you have to do you would have to go and look up the accession number array and then you have to go and search that array to find out where 1, 2, 3, 4, 5, 6 is and then you have to pick up that index where you found out 1, 2, 3, 4, 5, 6. So, suppose that is the 5th index. So, then you would know that well the 5th book is the one with accession number 1, 2, 3, 4, 5, 6. Now you will have to go to the claimant array and look up what the 5th entry there is. It is a very convoluted way of figuring out who is the claimant for a book with accession number 1, 2, 3, 4, 5, 6. So, we do not want to do that. So, what we would like to do is we would like to group all the information about a book together into a book entity. So, all of it name, title, author's name, who has claimed it, what is its price everything we would like to put together into one coherent entity and then we would like to have an array of these entity. So, then if I want you to find out who is the claimant of book with accession number 1, 2, 3, 4, 5, 6 I will just search through this array and each element in this array is this entire entity containing all information about a book. So, the moment I reach an entity whose accession number matches 1, 2, 3, 4, 5, 6 for that same entity I will just read off the claimant information. So, if we could group things together then we could do this in this much nicer way and similarly I could group all information about a pattern into one pattern entity and keep an array of these pattern entities. So, this would be a more logical, more natural way to program and actually first provides us some facilities to do this. So, for example, it provides us this facility called a structure. So, you say struct then you give a name for the structure and so this is a structure book which can have all of these things related to a book. So, as we will see shortly I can actually define a variable of type book and what that variable represents is this entire collection of information, that variable will have something called a title, that variable will have something called authors, it will have something called price and so on. So, by grouping things together we are saying that let us put everything that logically appears coherent that is related to each other for purposes of our programming assigned for programming task. Let us put them together into one entity and then we can deal with these entities. So, this I believe will be very important for your projects. You know when you are doing your projects you would want to group information, related information together and so structure should be important. So, that struct is a C++ keyword and book whatever I have said here is the name of the structure type. This is just like integer character data just like the names of other types. So, essentially now we are defining our own types rather than depending only on the types provided by the C++ standard and then you know these different things title, authors, price these are called members of the structure book and members can be arrays as well as variables. So, for example, these members are variables, these two members are arrays and for a particular member the name that we give to that member is called the member name and the type is the member type. So, it is like declaring variables within a structure and then I am going to declare variables of this structure type. So, I am grouping things together and then saying let us take this whole as one logical entity and we can then declare variables arrays whatever we want to. So, how do I declare a variable of type book? You know just like the way we would declare a variable of type int we say book in place of int and then we give the name of the variables just like I would use int my int your int whatever. Similarly, for array I just like for integer I would say int roll numbers 1000. So, here I can say book library shelf 1000. So, this name book that we have used along with this struct this becomes the name of a type just like integer character double whatever. So, this is an array of 1000 elements where each element in the in this array is an object of type book. So, each element in the array has so many information pieces along with it. Each element in this array is this composite thing inside which there are so many other things right because we want to access an element in the array and we want to say that is book number 5 and associated with book number 5 there is so much information. So, we want all of that to be present in that entity itself. So, similarly for patterns I could define a structure basically I am grouping together all the relevant information of a pattern together. So, this is basically you know sort of in one slide what we mean by object oriented programming that when you are given a programming task you first identify these entities which could be physical entities like a pattern or a book or it could they could even be conceptual entities you know like a rectangle or a geometric object or you know whatever you are trying to program about. So, you identify these entities that are involved in the working of the system. So, these entities are also called objects that is why the name object oriented programming comes about and then you think of the relations between these entities. So, the overall functionality of the system can be thought of as interactions between these entities and operations on these entities. So, pattern will come and check out a book a book will get claimed by another pattern. So, these are relations interactions between entities and you also abstract away or hide the details that are not necessary to be exposed for a particular operation. So, for example, I mean we will see examples where you know when I am doing a particular maybe return operation I do not need to expose some of the details. So, I want to abstract or hide them away and of course, you know as I am building these entities and their interactions this allows me to build the overall system functionality in a modular incremental way. And we expect all of you to do your projects in this way they are not just one monolithic piece of code, but identify the entities how they interact and program your projects so that it captures that it reflects that interaction between entities. So, this is what object oriented programming is about. So, the object contains information about you know one logical coherent entity. So, it can have information which is relatively fixed usually does not change as objects interact for example, the name of a pattern or title of a book or it could also have information that changes as objects interact. So, for example, the checkout status of a book you know the moment a pattern checks out a book that information changes and of course, an object must have unambiguous well defined boundaries. It is not that at some point of time you want to store something in the object and at some other point of time you do not want to store that as part of the object it must have completely well defined boundaries. You should understand very clearly what information is within the object, what information is within another object, what is the interaction about. So, this actually I mean writing something in an object oriented way also forces you to think in a structured and disciplined way. So, this is certainly the preferred way to go about programming. Interactions between any two objects should ideally happen through very well defined cases you know as in the textbook professor Abhiram Ranade's textbook he says that when you are looking at a television right there is a lot of complicated electronic circuitry inside the television, but when you are interacting with the television you just have a remote in your hand you have a few operations that you can do and that is all by which you can interact with the television inside a lot of stuff is happening. So, when you are trying to change a channel you do not go inside and change a capacitance and change a resistance and change an inductance to change a channel. You just press a switch and internally a lot of things are happening right. So, that is what we mean by well defined interfaces pressing that switch as a well defined function it is meant to change a channel and only change a channel you take that action and inside behind the scenes a lot of stuff is happening, but that is not your concern right you have hidden away that information presented well defined interfaces and all interaction should happen through those interfaces. So, this hiding of information is also called abstraction or data encapsulation. So, we will cover this later in the course. So, are there any doubts about what we just said this is some very simple idea of how you group related information together and that is kind of the foundation stone of object oriented programming. No questions good. So, let us see some usages of this. So, here is our simple library example that we just saw here is the structure book and the structure pattern they have different kinds of members and as we said we can declare you know an array called library shelf which has 1000 books in it and an array called library patterns which is 200 patterns in it fairly small library. You know since the structure book has so many things inside it the question is if I want to find out the price of a book what should I do you know I can I may know that this is the fifth book in the array library shelf, but then how do I go and find out the price of the book or whatever the accession number of the book. So, C++ actually provides this dot operator for this purpose. So, once you have a variable my choice let us say of this type book. So, my choice is a variable of type book. So, basically this variable has all of these information title authors price accession number and everything in it. So, now if I want to access a specific member I use this dot operator. So, I say my choice dot price this access is the member named price of the object my choice ok. So, very convenient way of representing just put a dot and then you can go inside that structure and access a particular member and once you put the dot and you know sort of constructed this my choice dot price this is like any other variable in a program. Because you are now accessing this member inside the structure and that member is a double typed member. So, you can use it like any other double typed member. So, for example, I could read in my choice dot price I could add 20 to my choice dot price I could print out my choice dot price is just like any other double valued member double valued variable right. Similarly, I could access the name member which is actually a character array by just saying current pattern dot name right. So, this dot allows me to go inside the structure and access any particular member by the name of that member. So, I could write code like if current pattern dot name which is referring to a character array and if the first element in that character array is capital S then I am going to print out that name. So, this is just like using any other character array right. So, the moment I say current pattern it is referring to that object which is all of that information dot name means I have gone inside that and access that particular member name which is a character array and now I can use it just like any other character array fine. So, it is a very convenient way of accessing members of grouping related information together and then accessing the relevant information. How do I initialize structures? So, this is how we might want to initialize integers or characters just say equal and provide the value. So, in structures also we do pretty much the same thing except that you know when I am trying to initialize a structure I have to specify a value for each of these members. So, I say current pattern is a variable of type pattern it is initialized to within braces you give the values for each of these members in exactly the same sequence in which they have been defined over here. So, the first is the initial value for name the second is the initial value for address the third is the unique ID the fourth is the number of books checked out the fifth is the initial value for claimed book accession number right. So, it is pretty much the same and sort of logically if I am trying to initialize a group of things I must specify the initial values of everything there. So, I have to specify it in the same order that is important right. If you specify IIT Bombay India first and Shashi they have next then of current pattern dot name will get set to IIT Bombay India right. The computer does not know that IIT Bombay India does not look like a name whatever value you give there it will get initialized to that yeah. So, that is what I just said and then I could even copy one structure to another. So, for example, here is an example of copying integers. So, I set I to 27 and then copy I to J. So, now J gets the value 27. So, I could do a similar thing. So, I could say current pattern is whatever we initialized earlier and then I could say previous pattern is assigned current pattern you know just like we would assign one variable to another of integer or character type we can do exactly the same thing with these user defined types right and what this will do is you know. So, current pattern is an object which has lots of members previous pattern is also an object which has lots of members. So, this assignment will copy the value of each member of current pattern to the corresponding member of previous pattern. So, you will basically get an exact copy of the object on the right hand side of the assignment statement right. So, this is also natural this is to be expected when I am copying all the members here should get copied to all the members there. So, the values of all these members will be copied there. So, is that clear the dot operator? So, now let us look at a programming example with you know whatever we have learned. So, this is the library management system again. So, what we are going to do is we have this structure book and we have defined an array called library shelf which has 1000 books and let us say to begin with we will say the checkout status of every book is false and the claimant ID which is if somebody has claimed this book is initialized to minus 1. Let us say that initially when we start off the system when we run our program initially every object of library shelf has these members initialized appropriately the title author price and accession number of the book, but these two are initialized to false and minus 1. And then we have an array library patterns of patterns let us say there are 200 patterns and each such pattern of course, have all of these members and once again we will assume that when we start off num books checked out for each pattern is set to 0 and the claimed books accession number is set to minus 1. This is the how we are starting off the system and name address unique ID are whatever initialized according to the patterns details. Now we want to write a C++ function for checking out a book. So, let us say there is a variable current pattern which denotes a pattern who wants to check out a book and the book that she wants to check out is in this variable current book. So, current pattern is of type pattern current book is of type book and current pattern wants to check out current book. So, this is what we will have to do. So, we said that each pattern is allowed to check out 3 books. So, we have to first check if the pattern is already checked out 3 books. If so we will say you cannot check out any more books. Otherwise we have to check if the current book has already been checked out. If so then we have to print an appropriate message. Otherwise we have to see if the current book has not been checked out, but if it has been claimed by somebody already then we cannot issue it. So, then we will have to print an appropriate message and if the current book is not checked out and not already claimed then we can issue this book. So, in that case we will increment the value of the num books checked out member of current pattern because the current pattern will now have one more book issued to her and will update the check out status of current book to true because now this book is being checked out fine. And of course, we want these changes to these members to persist after the current book has been checked out. So, basically we will need to have functions to which we will have to pass parameters by reference. So, that even of the function returns the members of the corresponding current book and current pattern will would stay updated. So, here is how it might look like. So, in the main function I am saying check out books library. So, the ith pattern is checking out the jth book and here is check out book to which I have passed these two parameters by reference. And what do I do in the code for checking out a book? I first check if the num books checked out member of current pattern is less than 3 which means if the current pattern has not already checked out 3 books then I will allow the current pattern to check out a book. Otherwise, I will say that you know sorry you have already checked out too many books. So, what do we do here? How do we check out a book? So, if the number of books checked out is less than 3 we will have to check if the book has already been checked out by somebody. So, if the check out status of the book is true then it means the book is already with somebody. So, then we can say that well sorry this book is already checked out. So, you cannot check this book out. Otherwise, we have to do something else. What do we do? We check whether this current book is already being claimed by somebody. So, this claimant ID is a member of the current book object and if this is not minus 1 then it has the ID of a claimant. So, if current books claimant ID is not minus 1 it means somebody has already claimed this book. It could be that this current pattern itself has claimed this book. So, we will also check that that if the claimant ID is not minus 1 and if the current patterns ID is not the claimant ID which means that somebody is claiming this book and it is not the current pattern then we will have to say that there is already a pending claim you cannot get it now. Otherwise, we can allow the pattern to check out the book. So, we will just set the check out status member of current book to true increment num books checked out of current pattern by 1. So, notice that I am you know once I use the dot operator and have access to the members of the structure I am using them just like variables that is the whole purpose of using the dot operator and then if the current books claimant ID is the pattern's unique ID which means the pattern had actually claimed this book. So, the current books claimant ID is the same as the patterns ID then we will set now that the book is being issued to the pattern we will set the claimant ID of the book to minus 1 and we will set the claimed books accession number of the current pattern to minus 1. So, note that I am actually updating different members of current pattern and current book in the process of checking out a book right. Some information is stored within the current pattern because it logically makes sense to belong there claimed book accession number is something that is particular to this pattern. So, it is stored with the pattern claimant ID is something that is specific to the book. So, it is stored with the book check out status is specific to the book, num books checked out is specific to the pattern. So, I have grouped this information together and then the checkout function is updating the appropriate things appropriate members of the appropriate objects ok. So, yeah and since we pass the parameters by reference. So, whatever changes we are doing here are really changes happening in the variables the objects in the arrays that were passed from the main function. So, they are going to stay even after the function returns is this fine. So, now we will just see the last part of today's lecture which is how to implement the checkout and return functionality. So, checkout we just saw claim and return ok. So, so I want to claim a book a current pattern wants to claim a current book ok. So, what should we do? I mean so, this is kind of the design phase where you are saying that well I want to implement claiming of a book by a pattern. So, what is it that I have to do? So, I have to sort of write it down you know what is the kind of pseudo code or algorithm that I write it down and then I go ahead and implement it right. So, I have to check if the current pattern has already checked out three books or already has a pending claim. So, if she has already checked out three books then of course, no question of putting another claim and if she already has a pending claim first that claim should get cleared then she can put another claim. So, if so, we will print an appropriate message and return otherwise we will have to check if the current book is not already checked out by anybody and if the pattern is trying to claim this book. So, if the book is not already checked out it is in the library and the pattern wants to claim this book I mean why claim the pattern can just check out this book it is there in the library right. So, we can print the appropriate message that please check out there is no need to claim otherwise if the current book is not already claimed by a different pattern then if the current book is already claimed by a different pattern we cannot allow another claim on that same book you know the simple library allows only one claim on a book. But if the current book is not already claimed by a different pattern then I will have to place the claim of current pattern on current book. So, how do I do that? I store the ID of the pattern in the claimant ID of the book. So, once again say I am updating different members of the different objects to store the relevant information within the relevant object boundaries right I do not want to mix them up the claimant ID of the pattern is being the ID of the pattern is being stored in the claimant ID of the book and the accession number of the book is being stored in the claimed book's accession number of the pattern right. So, information that is specific to a pattern will stay within the pattern's structure information that is specific to a book will stay within the book structure ok. So, how do we claim a book? So, this is what we just said if the pattern has already checked out three books or if the pattern already has made a claim to another book. So, if the claim book accession number is not minus 1 which means the current pattern has already claimed some other book then we say that you cannot claim any further book otherwise if the book has not already been checked out if the book is in the library. So, the checkout status of the book is false then we say that it is not yet checked out. So, there is no need to claim you can check out the books straight away right otherwise if this book is already being claimed by somebody and that somebody is not the same as current pattern then we have to say that this book is already being claimed by a different pattern you cannot claim it. So, once again notice that I am accessing different members of different structures basically I want to access that information but depending on which object boundary it is in I have to use the appropriate object and then finally if I am trying to claim the book I set the current books accession number to the claim book accession number of pattern I set the current patterns ID to the claimant ID of the current book and I return ok. So, what you see is that this actually allows you to think more logically you are talking about the claimant ID of you know you can think of this dot operator as saying claimant ID of current book claim book accession number of current pattern right hand side of the left hand side for the dot operator right and that is what we want to say claimant ID of the current book should be set to unique ID of current pattern. So, this is exactly how we write it you see plus plus and how do we return a book. So, you know we just see if the number of books checked out for the current pattern is greater than 0 then we decrement it and set the checkout status of the book to false and here is a very strange situation. So, suppose a pattern has come to return a book, but the number of books checked out for this pattern is already 0 which means this pattern had not really checked out in a book, but the pattern is trying to return a book right. So, that is a funny situation. So, we say that well are you trying to return somebody else's book right and you know. So, this strange scenario happens if a pattern tries to return a book checked out by somebody else and why does this happen that is because we did not keep track of all the information that we could have remembered in our objects right. So, in particular when a pattern checks out a book the ID of the pattern was not recorded with the book we simply said the book has been checked out the checkout status was set to true, but we did not record who has checked out this book right and similarly the accession numbers of the books checked out by a pattern were not recorded with the pattern and that is why if a pattern comes to return a book I do not have any way to check whether this pattern had herself checked out that book right. So, if you keep track of the above information we could sort of you know avoid these strange situations and so hope you know presumably we want to add a borrower ID member to the book structure. So, that when a book is borrowed by somebody we can put the ID of that borrower there and we want to since a pattern is allowed to check out three books. So, we want to have an array of size 3 containing the borrowed books accession numbers, but the point is that now that we have defined our structure wrote some program for checking out claiming returning how do we change things now do we go back to square one and redesign everything. So, this is once again where object oriented modular programming is actually very useful we have already encapsulated things saying that a pattern has these these things a book has these these things we want to add additional things there. So, we can go ahead and add those additional things for example in the book structure I can add this additional member in the pattern structure I can add this additional member and whatever I had defined earlier library patterns is an array of size 200 of patterns that continues to stay as it is except that each object here now also has this additional member. So, no change is needed in this part of the code where I was declaring a library of 1000 books just that every book additional member because I have sort of augmented or change the definition of the structure and I could initialize those to minus one and then I could go back to the functions that I wrote and you know whenever accessing checkout status of current book those things can stay just as they were because the members that had seen earlier are still there I have added some additional members. So, I could simply write some code which operates on those additional members the previous code can still stay as it is. So, for example here when I am checking out a book I have to iterate through this borrowed accession number member array of current pattern and whenever I see an empty entry there which means borrowed accession number i is minus 1 then I am going to store the accession number of the current book that is being checked out over there and break. And similarly I will store the id of the current pattern in the borrower id of the book. So, I am just adding this additional piece of code the other code that was there is untouched right. So, this is incremental software development that you think of some feature later on do some things as that you do not need to change most of the code that you have written you just write the code that takes care of this additional functionality. And similarly you know so in the slides it is given you can similarly change claim book this was the earlier code now that becomes a special case you have to check whether the borrower id is the same as the unique id and then you can print out some message. So, this is additional code that I incrementally write to improve the functionality and similarly for returning the book. So, this main part which was there stays as it is, but there is some additional part that comes in. So, that is the summary. So, we have actually covered six lectures in 48 minutes. And I hope all of you understood I did not speed through the lectures. Okay are there any doubts about it? No? No doubts? Okay let us go to the quiz then. Okay so that is the first question consider the structure definition struct x int y char z. So, if var is a variable of type struct x the member y of var is accessed in which of the following base next question which of the following is or are true of structures defined in C plus plus all members must have the same data type cannot have arrays as members cannot have two different members with the same name cannot have more than 1000 members. So, consider the structure definition struct x int y z. So, y and z are both members of type int. So, if we declare and initialize a variable my var as x my var equals 0 comma 1 the initialized values of the members of my var are which of the following options. So, this option means y is initialized to 0 z is initialized to 0 y is initialized to 1 z is initialized to 0 and so on. Suppose x and y are two variables of struct my struct the assignment statement x is assigned y is what it is an illegal statement in C plus plus makes the addresses of x and y the same copies values of all members of y to corresponding members of x and none of the above. So, suppose struct x is a structure with an integer member named y. So, the declaration x a b c 10 declares an array of objects of type struct x. So, which of the following accesses the member y of the fifth element of the array member y of the fifth element of the array which of the four given four options accesses the member y of the fifth element of the array. So, let us quickly go through these and see what the right answers are. So, here I believe all of you have figured out var dot y is the right answer accessing member y of the variable var. So, here members need not be of the same data type it can have arrays of as members, but you cannot have two different members with the same name because when you put dot and the name there would be ambiguity about which member you are referring to and it can certainly have more than 1000 members there are no restrictions on number of 1. So, here the initialization y will get initialized to 0, z will get initialized to 1. This is certainly not an illegal statement it will simply copy the values of all members of y to the corresponding members of x and here I am accessing the fifth element of the array. So, it is a b c 5. So, this is the fifth element and then you access the member y of it. Now, let us start the practice question. So, how many of you know about binary trees? Nobody good. So, we are on the level playing field then oh you know some of you know about binary trees. So, let us see if this question becomes easy. So, a binary tree. So, as you just saw in that library example right when we are trying to design something which is a bit more complex we would want to group things together we would try to represent relations between objects and so on. So, binary tree is a data structure whereby a data structure we really mean a way of representing information in which data items are related by a parent child relationship satisfying certain conditions right. So, it is a way of representing information such that the data items have a parent child relationship and there are some conditions that are satisfied in this parent child relationship. So, what are the conditions that there is a designated data item which we will call the root which has no parent it has no parent data items every other data item has one unique parent data item. So, there is one guy who does not have any parent everybody else has exactly one unique parent every data item has at most two children data items. So, remember there is a parent child relationship. So, every data item has one unique parent it has two children data items these two children are also unique they are not the same and there are then there are designated data items called leaves which do not have any children for them. So, here is a representation of a binary tree. So, that yellow thing there is the root data item and then these two blue things are what we will call the these are the two children of that root data item. So, we will call this the left child of the root and the right child of the root this arrow does not denote any pointer or anything it is just denoting the relation that this is related to this through the this is the parent this is the left child this is the parent this is the right child and I have sort of drawn the arrow there to sort of make this very explicit visually and then of course, this in turn has two children one left one right that has further children and look at these guys they do not have any children. So, they are the leaves and you know let us call this A then everything that comes below A the children of A and their children and their children and so on are all called descendants of A. So, in this case there are four descendants of A similarly there is one descendant of B of course, everything is a descendant of the root is this clear. So, each box here is like some data item the arrows are some kind of relations between the data items and the relation is sort of organized in this way. So, this is what we called a binary tree okay is this clear okay good. So, now consider the following structure definition struct tree node int value int left child and int right child okay. So, we can think of an object of type tree node you can visually think of it as having three members one is the value one is the left child one is the right child all of these three are integer valued members okay there is a structure of the type that we just studied is this clear I would request you to just note this down because I have to move to the next slide where we will need this because just the definition of the structure struct tree node int value int left child and int right child. So, now we want to use an array A of objects of type struct tree node right. So, that was our struct tree node. So, this was kind of an object of type tree node. So, we want to use an array of these objects and let us call that array A. So, we want to use an array A of objects of type struct tree node to represent a binary tree. So, how are you going to represent it? We will say a 0 remember every object has these three members the top one is the value is the left child is the right child right. So, we will say a 0 is the root okay that is a data item that is the root and we will see what value the left child member has if the left child member has value 3 we will say a 3 is the left child of a 0 if the right child member has the value 7 we will say a 7 is a right child of a 0 is that clear. So, that is what these arrows are representing. So, if this is 3 it means the left child is a 3 the right child is a 7 a 3 itself has a value it has its own left child has its own right child if I have minus 1 for the right child of course minus 1 cannot be an index in an array it just means that there is no right child for this data item right and similarly a 7 is the right child of the root it has no left child it has a right child which is a 9. So, I hope you will agree that I can actually represent a binary tree by just filling in the values of left child and right child you are now given an array A of size 100 of re-node objects such that the values are already sorted. So, a 0 dot value is less than or equal to a 1 dot value is less than or equal to a 99 dot value and the values of left child and right child of every element is set to minus 1. So, basically these are like individual data items without any parent child relationship. However, their values are all sorted and it is sorted in this order. So, you are required to write a function we will call that function heapify which will take that array of tree nodes right and array of tree nodes is of type tree node pointer that array name is of type tree node pointer we saw it in last class. So, this function will take that array of tree nodes and it will fill in the values of left child and right child members of all elements such that using those left child and right child members I am going to get a binary tree and in that binary tree the value of every node is less than or equal to the value of all its descendants. If you remember what a descendant was children, grandchildren, great-grandchildren and so on right. So, I want the value of a node to be less than or equal to the value of everything below that node and we want you to fill in the values of left child and right child appropriately. The value fields are already sorted a 0 dot value is less than or equal to a 1 dot value is and so on. So, you can assume that the root is a 0 and of course, the absence of children is indicated by putting minus 1 in the corresponding member. Is the question clear? You are given an array of tree node objects each tree node object is like this right. So, this is an array of size 5 of tree node objects the value fields are already sorted for example, this could be 10, 11, 14, 17, 19. These are all initialized to minus 1 so you will be given this array this is the array a which will be passed to your function epify your function is supposed to go ahead and fill in values in these places the left child and right child members of all objects in that array such that so for example, right. So, this is a 0 this is a 1 this is a 2 this is a 3 and this is a 4. So, suppose I filled in 2 here and I filled in 1 there suppose just for the heck of it. So, what would this mean? It would mean that a 0 is the root because we have said that assume that the root is a 0 and its left child is a 2 its right child is a 1. So, its left child is a 2 and its right child is a 1 right what is the value for a 0 10 what is the value for a 2. So, actually let me not put 1 here let me put let us say 4 suppose I put in that over there. So, a 0 is the root its value is 10 left child is a 2 value is 14 right child is a 4 value is 19. So, if I filled in 2 and 4 here and now let us say for a 2 I fill in this as 1 and I keep that as minus 1. So, then what would this mean that its left child is a 1 and what is the value of a 1 11. Now, you see that a 2 has value 14 but its child has value 11. So, that violates the condition that we have there that the value of a node must be less than or equal to the value of its descendants. This is a descendant of that, but its value is less than or equal to its strictly less than the value of that right. But what we needed is the value of a node should be less than or equal to the value of its descendants that is violated here. So, if you fill in like this then it is not a correct answer right what could be a correct answer. For example, if I had put a 1 over here like I had done first. So, then a 1 is 11 right and let us say I put minus 1 minus 1 here and here I put 4 and 3 let us say right. So, this would be a 4 and that would be a 3 what is the value of a 4 a 4 is 9 value of a 3 is 17 and these could be minus 1 right. So, now, the value of every node is less than or equal to the values of its descendants 11 is less than or equal to 19 11 is less than or equal to 17 10 is less than or equal to the value of everything all its descendants and so on. Is this clear what we want clear everybody what we want. So, now, that is what we want you to do. So, you are going to be given an array a of size 100 where a 0 dot value till a 99 dot value are all sorted in increasing order all the left and right child that initialize to minus 1. You have to fill in the values of left child and right child members such that value of a node is less than or equal to value of all its descendants. So, there is a trivial solution to this which is a correct solution. So, please do not think very complicated there is a very very simple solution to this at least the question that we have asked as a very very simple solution and if all of you come up with that solution I am going to complicate that question a little bit more. So, here is Saransh solution it is a perfectly correct solution. So, he is saying he will just keep the right child minus 1. So, there are no right children and he is just setting the left child of a 0 to be a 1, the left child of a 1 to be a 2, left child of a 2 to be a 3 and so on. So, it is the array is already sorted. So, obviously everything I mean the only descendants of a 0 are a 1 through a 99 the only descendants of a 1 or a 2 through a 99. So, of course that condition is trivial limit right. So, you can see that Saransh solution requires 100 generations to accommodate all these 100. If you want to accommodate all these children in the least number of generations basically if you look at this tree it has a height of 100. If I want to minimize the height so that is the minimal number of generations sort of the number of descendants may be more what could be I think that is the next one. You know so basically here what in Saransh solution there is no node with two children right. So, now if I tell you that you have to maximize the number of nodes with two children which is the same as minimizing this height of the tree right. So, if you look at this solution that is the root there will be exactly one leaf which will be a 99 and come from the root to the leaf you will have to cross whatever 99 elements right. So, now the next version of the question is it is exactly the same thing, but now you have to have as many nodes with two children as possible that will automatically reduce this height of the tree is the question clear. So, for example this tree did not have one child per node there were several nodes with two children right and in fact this tree is such that with five elements there are two nodes having two children and this is the maximum that you can do maximum number of nodes with two children has to be two strategy is something like this. So, a 0 is the root and then he wants to put a 1 and a 2 then a 3 a 4 a 5 a 6 and so on. So, clearly this will maximize the number of nodes with two children's he is always giving two children to nodes as long as it is possible. And so, he has to come up with this numbering scheme says that the left child of 0 is 1, the right child of 0 is 2, the left child of 1 is 3, the right child of 1 is 4 and so on. So, if you just see this eyeball this for a few minutes you should be able to see a pattern here. So, Saransha of course done it in the following way. He has a loop here where he is saying while k is less than or equal to 99. So, he starts off with int i is 0 and in the loop he is going to set a i dot left child and a i dot right child and increment i and he is just going in this order. So, he starts off by setting k to 1 and he associates the next number. So, i dot left child is k plus plus, i dot right child is k plus plus. So, it is like saying 0, 1, 2 and then you take 1 that is 3, 4 you take 2 that is 4, 6 you take 3 that will be 7, 8 you take 4 that will be 9, 10 and so on. And then of course, he is doing some checks if k is not equal to 100. So, basically you do not want to put values here which are greater than 99. So, if they are becoming greater than 99 you put minus 1 there which means that you have exhausted all the nodes after assigning 2 children. So, another way of doing this is what Govind has done which is basically if you see the pattern here the left child of the ith node is 2 i plus 1 the right child of the ith node is 2 i plus 2 and that is exactly what you will get from here also from this loop also you will get exactly that same thing. So, that is a very simple solution if you look at it. So, what we will get at the end of it is a tree like this and as you can see every node in the tree must have a value and it must have some information about who is the left child and who is the right child and that is most conveniently done using a structure. You want to store a value and you also want to store information about who is the left child and who is the right child. So, that is most conveniently done using a structure. So, therefore, you can now represent all kinds of different relations between data items here it was a binary tree relation. You can represent different kinds of data relations between data items using structures you can use specific members of the structures to represent information about the relation. And as we just indicated as I just said that this kind of a data structure is also called a heap and in this case of course the values of A were all sorted to begin with. So, we are basically getting this heap quite easily. You have studied about selection sort and merge sort. So, there is another sorting technique called heap sort which basically tries to build a heap out from unsorted elements and the process of trying to build a heap is to basically build a binary tree which satisfies that property value of every node is less than or equal to value of all its descendants and if you can ensure that for the whole thing then you have the array sorted. So, we will stop here.