 Welcome back, in the previous segment we discussed various operations on structures. Now we are going to put together all these things into a somewhat longish example. So here is the problem that we are going to solve. So given end disks in the plane determine if they intersect. You may remember that we have written code for this earlier. Now with structs we are going to write the same code for the same job, but you will see that in some sense our code will be nicer. So let us start with the basic struct that we need. So this is a struct disk and instead of having point I could have had point as well. I have just put in center x, center y and radius as its three members. So let me get to the main program. So we are going to have five disks just for simplicity and so I have put in a constant end n to denote the number of disks. And now I am just going to create an array of disks. So by the way note that in the earlier case what I had to do in the main program was to create not just a single array but an array to store the x coordinate, another array to store the y coordinate and another array to store the radius. So that makes the program verbose and furthermore it does not really tell you that something is being done about disks unless you write a comment. But here very compactly you understand that yes we are doing something about disks. The first operation that we are going to perform is to read data. You will note that in the program that we wrote earlier we put the code for reading right here. But now the code is elsewhere but here in the main program itself you know that what that code is supposed to be doing is reading data and reading and in particular this looks like we are reading the various attributes of the disks. So again this makes for better readability. Then we have another function which says check all pairs. So you can tell easily that look we are going to do something with all pairs and the result is going to be printed. So that is it. The main program is quite short and sweet and it has steps which have been given names and these steps are functions and we are passing data to the functions. But the data is not, it does not look like a lot of data. I mean there are not many names, there are not many parameter arguments that we are passing. We are just really passing the main argument which is the disks and then the number of elements in it. So there is a certain kind of compactness or neatness to this entire program. So without structs so you would have to have lots of arrays being passed. So that would just look a bit more cluttered. And in some sense in this program we are encouraging thinking at a high level. So here when we write read data we are saying oh somehow it will be read. I do not want to think about exactly how that is going to happen. But I know that it is going to happen. And similarly check all pairs. How is that going to happen? Well it is given somewhere else but this is what I really want to happen. So this sort of tells you what is going on at a high level and does not force you to read all the details and then figure out. So let us go to this function. Well first of all reading the data. The reading the data is fairly natural. We go over all the elements in the array and read data into it. Then we are going to check intersections. So check all pairs or maybe I could have called it check all intersections that might have been an even nicer name. Anyway so there are end disks which are passed as arguments. Then we go over all pairs and we discussed this last time when we solved this problem earlier that we have to have two indices i and j and i should go from 0 to n minus 2 really and therefore we have written i less than n minus 1 and j should go from i plus 1 to n minus 1. And for each such pair we need to check whether those pairs intersect. If they intersect we return true. If the loops execute without finding an intersection then the control will reach this point over here and at that point we know that no intersection was found and so we can return false. Now this function contains really just one kind of an idea. So it says look I want to check intersections between every pair. So check between every pair is what is happening over here. How exactly the checking for a single pair happens that is described in the intersect function. So this comes over here and notice that the intersect function only talks about two disks. It does not have to know about the entire array. So the point is that when we write each function our view point is somehow limited and that helps us think about our job, think about whatever code is doing a little bit better. So here our rule was that we are going to look at the distance between the centers and so this whole thing is the square of the distance between the two centers and this should be smaller than the square of the sum of the radii. So that is what this check that is the check that is happening over here and the result is being returned. So let us do a quick demo of this. So here is my program. So here is the disk structure type, the intersect function, the check all pairs function read data. Now I have also added over here a show disks function. So at the end it is going to show us the disks on our canvas. So this way we will know whether our intersection answer is correct or not. In this we have the main program. So really what we had in the slides plus this additional show function. So in the main program as well we are going to print out the answer whether there is an intersection or not and then we are going to show whether the disks are intersecting or not. Let me just shift it a little bit so that you see even the first character. So let us compile this and we are going to run it and we are going to run it by redirecting the file. So .dat1 is one file. So the answer is 1 and here is the plot. So here are the circles. So clearly there is intersection and therefore the answer was 1. So let us do one more. So we do it for 2 and this time there is no intersection and in fact the answer is 0. So what have we discussed in this segment? So we discussed a detailed example and here are some important points that you should observe. So by using structures our functions could be written with fewer arguments and this makes for better readability, less clutter and of course less possibility of errors as well. We do not forget giving a function or we would not exchange two arguments. So such errors are sort of very, very easily prevented. Our overall program can be now more easily written as a bunch of small functions. If we had lots of arguments to be supplied we sort of would have said oh my god I am tired of supplying all these arguments and so you might have tried not to write small functions. But writing small functions is a good idea because in a small function you can just have one idea and therefore you can understand that function and make sure that it is correct a lot more easily. Then we also said that because we break things up into functions each function has its own limited view. So for example the main program has a very high level view. The other functions may have views at their own levels. So in the next segment we are going to talk about pointers with structures and we will also conclude this entire lecture sequence.