 Today we are going to have some kind of a tutorial discussion where I will analyze the problems of quiz 1. As far as the quiz is concerned, the emphasis was testing your understanding of the control structures and the program execution. The last problem actually was a synthesis problem where you have to write a full program. I will quickly analyze these problems. Most of you should have got the right answers. The first question was very simple. It was to test whether you can follow the instructions of the algorithm and execute them correctly. Temp is a artificial variable. This value 161 has absolutely no bearing on the algorithm as we shall see. It says 1st and 2nd defines the thing called result, defines integer i and r and it asks you to give a number which is last two digits of your roll number. This is merely to ensure that everybody executes the algorithm with a different input. To begin with you are supposed to execute this iteration which varies i from 0 to 5 and it multiplies the value of temp incrementally by i. So originally this is the value you start with when you enter this iteration. However as i varies from 0, 1, 2, 3 and 4 the value of temp increasingly takes what value? The first time i is 0 so when you multiply temp with 0 you actually get a 0 and then it does not matter what you multiply it with it remains 0. This is just for you to figure out that this iteration leaves temp as 0 and it has no impact on anything else that happens subsequently. The input value r which you have given, let us assume r as some number. Let us assume r as 34. So i have roll number let us say which ends in last two digits 34. Now after this temp you will agree that when i say r is equal to r plus temp r remains unchanged. So if i have read r as 34, r still remains 34. Now when i calculate first what will be the value of first? The first is r by 10 it is an integer division 34 divided by 10 i will get 3. The value of second the instruction says r modulus 10. So that is a reminder after dividing it by 10 which means i will get the last digit of whatever number i have given. In this case it will be 4 and if you notice when i calculate the result i multiply 4 by 10 and add first which is 3 and then add 1 for good measure. So effectively this will give me 43 which is actually the inverted last two digits of your roll number. I add 1 to it and the result should be 44. So everybody got it? No, everybody did not get it. Somebody perhaps made a mistake in calculating temp as 0 and some got some very funny numbers. But this is where you need to be very careful about both what you write and when you read what somebody else has written how to understand and analyze that program. Let us go back to the next question. So these were the answers. The input will be last two digits of your roll number. The value of temp at the end will be 0 actually and the result will be whatever. I am thinking of suggesting to my TAs that it is possible that you have goofed up in calculating temp in which case if you have done this part correctly I will ask my TAs to consider giving partial grade for this. That will of course be held in evaluation because everybody would have a different value of temp and then adding it and then finding out whatever, whatever is not going to be very straightforward. But I will see about that. However there is absolutely no reason why anybody should goof up on this. The only way you can goof up is do not execute your instructions manually by writing down variable names, writing down values below the variable names in the rough sheet and going step by step on each iteration finding out what happens next. Question two is slightly more tricky. It has an array of five elements where a 0 is 1, a 1 is 1, a 2 is 1 and a 3 is 1. So all elements are initialized to 1 except the last one. The last one is expected as input from you and you are supposed to give the value of a 4 the last element. After this you enter into this computation. This is essentially a triple nested iteration. The outer iteration varies i from 0 to 5. The inner iteration next to it varies j from 0 to i minus 1. And the innermost iteration varies k, a third index which varies from 0 to j minus 1. i, j and k are incremented by 1 each. So seemingly very simple algorithm adjusts this edition. Nest is equal to s plus a i plus a j plus a k. Notice that what it is doing is merely adding some three elements of the array a which has actually five elements choosing different combinations at different times depending upon the values of i, j and k which are decided by this loop. A 1, A 2, A 3, A 4 are constant values 1. A 5 is a value that you given. The question asked was that if I give the value 1, if I give the value 2, if I give the value 3, if I give the value 4, do I get a sum which was I think 42 right? This problem can also be solved working out exactly the same way that you did in the earlier problem namely executing this algorithm. Of course this involves a lot of Godagiri just for us to realize what amount of work that Dumbo is doing whenever we give instructions of this kind. However we must be able to interpret Dumbo's actions correctly and therefore it is important that you understand what will happen when I finally print out the sum. I leave this detailed exercise to you but those of you who did not get the answer right I would urge them to once again try it on paper. You don't need a computer to work this out but this kind of practice must be ingrained into your mind to automatically look at what happens to different values when complex computations are done. The right answer was 3. How many of you got it? Oh large number but still some didn't get it. Perhaps the mistake would be in some simple arithmetic or index range or some such. These two problems were really simple according to me. The third problem is also simple but it requires some thinking. Here we give some start time and a duration and you are supposed to write a program which will add the duration to start time giving you the end time. The main program was like this where I read the start time from you where I read the duration from you and then I calculate a function which says calculate end time with two parameters start time duration. So with these two parameters which I have just read in here the control will go to the calculate end time function which will calculate some result and when the result is returned back here the result will replace this whole function call and that is what will be assigned here. So to go back to the previous slide this is where you are supposed to this is the function calculate end time you are supposed to write your code here which will add start time and duration and it will return R. Let's look at some examples to see what we will do here. So let us take an example let us take time as 14 45 this actually is equivalent to 2 I hope this is clearly understood 14 45 means it is beyond 12 so 14 minus 12 is 2 so it's 2 o'clock in the afternoon and 45 minutes. Suppose I give a duration which is one hour ten minutes then if I add these two I can simply add the minutes to minutes I will get 55 minutes and 14 hours to one hour I will get 15 55 which is actually equal to 355 p.m. This is no complication one can understand it easily but if one concludes that therefore if I add duration to start time it is adequate that is not correct why let us take another example where I add 14 45 which is the start time to a duration which is let us say 23 30 this time is to 45 p.m. as usual and this duration 23 30 means I am adding 23 hours and 30 minutes that is the duration so if I am today at 245 p.m. clearly I will be there somewhere tomorrow may be early afternoon it is almost 24 hours later so I will be almost to 45 p.m. 30 minutes less so the correct answer finally should be 2 is that correct if today to 45 p.m. I had 23 hours 30 minutes this is the time I should get you will agree that if I simply add these two up I will not get this answer the correct thing is from the given number from the given four-digit number you have to first discern hours and minutes separately both for the duration and for the start time then you should add only the minutes part here you will get 75 minutes clearly there is no clock time which has 75 minutes so when you get 75 minutes you should subtract 60 from it you will get the time as 15 minutes and plus one hour because 75 minutes is one hour 15 minutes so you reduce this by 60 you will get 15 minutes that will be the result minutes here as far as ours are concerned I will now have to add these hours 23 which is the duration to the original hours 14 plus this carried forward one hour which I got from here and finally I will get this as is that correct 24 as plus 23 plus 1 plus 14 38 now this 38 is beyond 24 so obviously I am talking about tomorrow but what time so I should subtract 24 from this to get 14 consequently the final answer should be 14 15 which in our conventional time sense happens to be 250 is this clear so how many of you got this oh still a good number but not as many as the previous one so this kind of tricky problems you will find many a typical calendar problem falls into this class so what is the date if you if you are told 1st January 2009 was a certain day whatever Monday Tuesday Wednesday then what was the what will be the day on 5th November 2009 now that is a tricky question because it involves arithmetic which is different month wise because there are some months with 31 days some months with 30 days so it's not very easy in this particular case this is a program that I have written I will be putting up this program on the website so you can copy it from there is just to understand that when I read my start time and end time I define intermediate variables hour start minutes start hours for duration minutes for duration and hours and minutes and the way I handle this is I calculate our start by dividing s by 100 and minutes start I get by finding out s module 100 you will agree that like in the first question I will get last two digits by doing this and the first two digits by doing this which are respectively hours and minutes I print those things out similarly I dissect the duration into hours and minutes using exactly the same formulation I print it out so far so good by this what I will do is I will get this breakup I will get 14 hours and 45 minutes and I for start time and I'll get 23 hours and 30 minutes for duration now I find final minutes by simply adding the minutes of start time and minutes of duration however as we saw earlier if the total is greater than 60 then obviously I have got one hour and some minutes so I will reduce the minutes by 60 and add one to HD which was the duration hour so whatever is the duration hours I need to get one more hour added to it in case the minutes are greater than 60 after coming out I will find out the hours of end point which is hours start point plus hours of duration which might have been modified here I am putting a print out here to say n minutes are a me and end hour are he will that be the correct result no well some cases yes n minutes will be correct but end hour may be a value greater than 24 so consequently I have to do one more check if hours at end are greater than 24 I have to reduce those hours to less than 24 that is what will get me the final hours and to compose the four digit number I do the reverse of the dissection that I did earlier I multiply he by 100 and add me to it I get the result value which I return yes very good point what he is suggesting is that if I test the condition which I tested here a me greater than 60 then only the minutes are 61 62 or 100 or whatever they will get converted correctly however if the start time was some hours 40 minutes and duration was some hours 20 minutes then the total I will get will be 60 60 also should be reduced to 0 so the correct condition is not a me greater than 60 but a me greater than 15 9 because even at 60 I must subtract 60 make it 0 0 wonderful what's your name young man nickel let's give a good hand to the kill good good problems here are some execution results for 2 3 v1 I compile as usual and execute it I have given start time as 15 30 duration as 23 45 this is dissected and the output says stars hours are 15 minutes 30 duration hours are 23 minutes 45 the end minutes are calculated as 15 the end hour is calculated as 39 which is then reduced subsequently to get n time as 15 15 observe that this program works correctly merely because the minutes total of start minutes and duration minutes are not adding to exact 60 if they did the output produce will be wrong that's right so you need an additional instruction along with finding modulo remainder to actually get the number of hours that's also correct and the reason for the hours addition again has to be modulo computing because the number of hours we are presuming that the duration will be less than 24 that assumption is correct because that is what is stated the duration is given in a 24 hour format a 24 hour format cannot contain a duration which is more than 24 hours but technically I could have to solve a problem where I have to add 72 hours or 52 hours to the start time with some minutes in which case it will not be adequate to just reduce 24 from the sum of hours good points all of you sir I yeah so this problem can be solved if instead of subtracting 24 you divide by 24 and take the remainder roughly the same thing as finding out modulo remainder and finding out how many hours are extra that is correct the problem is if the duration is stated to be much more than 24 hours say 72 hours or something you will get the value correctly but you won't know whether it is tomorrow or day after or day after unless you print the number of days as an additional output of course it was not intended to test those things here what was intended was to test whether you can correctly conclude that the minutes have to be less than 60 and hours have to be less than 24 that was the limited expectation from this problem but I'm glad that this discussion has thrown up additional possibilities this is not a very difficult question but certainly a question which requires you to think actually the question that has been put here by my TAs in a very simple form but it represents an extremely complex branch of algorithms which is called dynamic programming some of you who subsequently specialized in study of algorithms will actually come across these problems very interesting and complex problems which require a different approach to solution called dynamic program we will not go into those details now I shall subsequently suggest to you which are some of the large and complex problems towards which we are trying to lead by solving such examples so in this example there is an array of integers given and there is a sequence of numbers the example given was 1 4 1 6 5 1 7 8 12 now any subsequence of this is any continuous set of elements so for example 1 4 these are the first two elements 1 4 1 they are the first three elements 6 5 1 7 these are the fourth fifth sixth and seventh element so any contiguous elements will be a subset will be a subsequence there are obviously very large number of such subsequences depending upon the number of elements that you have in the array if the array has any elements the number of subsequences will be of what order can you guess will there be n subsequences if there are n numbers obviously not just consider each element as a independent subsequence you get n subsequences by just considering one element each so consider two elements each 1 2 2 3 3 4 1 2 1 3 1 sorry is saying n into n minus 1 by 2 that is roughly the sum of n integer numbers and clearly the number of subsequences that you will have will be much larger it is a suggestion I make please examine whether the order of magnitude of such number of six subsequences does not turn out to be factorial but I will leave it at that what problem we are solving specifically is much simpler we are not interested in listing all subsequences although in order to solve this problem you will have to traverse through each and every of such sequence because the question is that I want to find out those subsequences which add up to a given value 10 15 23 2000 whatever for example this subsequence 1 4 adds up to 5 this adds up to 6 this adds up to 6 plus 5 11 plus 12 plus 7 19 each subsequence sums up to some value the question relates to finding out which are those subsequences which add up to a given value obviously you can't determine that unless you go through each and every subsequence whether it is of the magnitude factorial or whatever now how do we do that we first find out a simple method of representing a subsequence after all if n is a 5000 then subsequences will be very large representing them would be tough so we say that every subsequence is represented by the first index value and the last index so for example if this is the subsequence then the third element is 6 sorry this is 0th element first element second element third element so if I am looking at the subsequence 6 5 1 7 I know it starts here now this element is 0 1 2 3 third element and it ends here now this element is 3 4 5 6 6 element so consequently if I say 3 comma 6 then this represents a subsequence which starts at the third element and ends at the sixth element third and sixth being stated with reference to 0 being the first element consequently I can represent all my subsequences like that if there is a subsequence of a single element the start and end point will be same the question was that while the sum of the numbers in each such subsequence will ordinarily add up to some arbitrary value here what we want is that you read an array of integers consider only positive numbers and a number r which is an integer as input and find all continuous subsequences in the form start index and end index which add up to the number r so to find out a subset of those subsequences which add up to the given value r for each such subsequence the program should output the start index and the end index that is the pair on one line and after listing all such subsequences your program should end in case there is no such subsequence which adds up to given value say 15 10 whatever then your output should be a message saying no such subsequence adding up to the given value found whatever so this is the program this was the example given in this example there were four subsequences which were adding up to value 12 and therefore your output should have been 0 3 2 4 3 5 8 8 why 8 8 let's go back 8 8 is the last element 12 which alone adds up to 12 so the subsequence is starting point this ending point this which is the earth element I hope at least the problem was understood by everybody good the solution is not very difficult I have tried to write there are actually three versions which I will be putting up on the on the web page for you to look at this is a version where I have used an outer iteration using an index I which goes from the first element of the array to the last element and then inside I use a while loop to traverse for the subsequent positions of the array starting from the first position to find out whether there is a subsequence which adds up to the given value now while I add up I might exceed the sum in which case I terminate my search there's no point in searching further because additional numbers will only increase the sum if I extend beyond that array I again stop and go to the next starting point because there's nothing more to search but if at any point in my search within the second iteration if the sum adds up to the given value then I know I have hit upon a subsequence I know the starting point where I started wherever I am currently I will term it as endpoint and print the start point and end point having done that I cannot have any other subsequence adding up to that given value starting with the same index and therefore I break out of the iteration and I go back to the next starting point this is just a part of the program which reads the data then it reads the given value an integer value I want to find out whether the sum adds up to this given value so this is the iteration I do not know whether I will find any subsequence or not so what I do is I set up the I have two indices which are defined here let's go back to the previous this thing I have defined here si ei and sum si is the start index ei is the end index and sum is the running sum that I will calculate since as I said I do not know whether I will ever get a single subsequence adding up to given value I start my iteration with ei set to minus one this minus one is arbitrary but this is some value which I will never get a sum any negative value will do therefore observe that zero will not do because the entire array if it contains zero all sums will add to zero so it has to be a negative and this minus one is justified because the problem says that I have to consider an array of only positive integers if you have to consider an array of negative and positive integers you will have to have some other mechanism like setting up a flag which will be false initially and only if you find at least one subsequence you will set that flag to true or something so that is another mechanism that you can use in here as I told you I am setting up a simple iteration at the beginning for i equal to zero to n minus one and this shall always be my starting point of the subsequence to search so I am therefore setting SI equal to one since I am looking at a new subsequence I start with sum equal to zero and I have to start summing up from this element itself onwards because remember only one element can add up to that sum so I will set the starting point for inner iteration to SI itself now what I do in the second iteration I check whether J is less than L so along the array I have to go from the start point to the end point start point is shifting whatever is the start point from there up to n minus one I have to keep adding some and how long I have to keep adding it as long as the sum is less than given value observe that if I have thousand elements and some exceeds the given value on say fifth element there's no point in going further because some will only increase so this is the condition for ending iteration either J should be less than n or the sum should be less than given value then and only then I am interested in examining subsequent elements of that subsequence to find out whether I get the sum I add AJ to sum I started with sum equal to zero the first element itself is added here yeah sorry I know that many of you are very keen to comment on many interesting and useful modifications that can be done and fortunately in this class where we have to end in exactly five minutes I am unable to take these suggestions but I have a suggestion I got some very interesting feedback on from students who have tried variations I will encourage all of you to upload such suggestions on the moodle it is not necessary that you write complete programs and upload them but even if you have such suggestion as long as your thought through those suggestions do make those sessions I will ask my tears to compile all of them and connect them to appropriate problem which is being discussed I hope that material be useful to all of you so we will take additional solutions later this is just I would say one way of solving this problem what I am doing is while J is less than n and some is less than given value I am simply adding each element to some notice this slash slash see out less less indices are I J some what is the purpose slash slash means clearly this is a command the importance of such lines is that if I remove this command I will actually get a printout after every execution of the current value of I current value of J and current value of some these are called debugging outputs meaning when I am executing the program and my program is not getting me final results correctly I would like to examine what is happening inside this is almost like executing the program manually which we did for question one that is not possible for most complex algorithms so typically while testing your programs you would like to add such output statements once you have corrected the program there is no need for them but in general instead of removing them if I put a slash slash here that means this line becomes a command it will not be executed however it will be available if later on I want to modify my program and again go back to this test there are better methods of doing this which we shall discuss later when we discuss the debuggers which are available for C++ this is the crux of this if some is equal to given value then I have found a subsequent that subsequent started with SI the starting I the end I EI is clearly the current J so I have now SI and EI which is my subsequent and for good measure I write here elements are for K equal to SI to EI I will actually print all those elements so I am not only outputting the start point and end point but I am also outputting the elements in between just to see that yes they do add up to the given value this break statement is to ensure that if some is equal to given value I do not unnecessarily go back to the next iteration it is not necessary break will get you out of the innermost loop whether it is a for iteration or while iteration in this case it is a while iteration so you will get out of that at the end of this of course before going to the next iteration I set J to J++ so that I look at the next element observe that let's go back to the previous slide observe that this iteration which is while J less than n and some less than given value can also be written in terms of for J starting with SI or I and going up to n with some additional conditions for breaking that is what I have written in another version called version one I will upload both these versions and I'll urge you to think more about it and as our friend there was suggesting you could have additional different mechanisms perhaps cleaner perhaps more elegant mechanisms to do exactly the same thing thank you so much