 Good morning. Today, we are going to discuss the mid-sem questions. I am sure by now you would have worked out the right solutions. As I had mentioned to you so many times, there is nothing like a unique answer in terms of writing a program for a problem. You could approach the problem and algorithm in multiple ways. What I am showing you are sample solutions. In some cases, we have worked out multiple alternatives, but I am sure many of you would have chosen still something else which is not visible. However, the TAs who evaluated the papers have gone through your answers and tried to understand the answers. The sad part of life for me is that even now, many of you are making very silly mistakes in writing syntax. Most of the answer books do not have proper comments. Most of the answers are still not using proper indentation. I have warned you that we will be deducting marks for those lapses. Unfortunately for us and perhaps fortunately for you, we have decided not to consider indentation and commenting while evaluating the questions or answers, but to do so in a moderation round at the end of entire correction work. Unfortunately, the correction work took much longer than what we thought. There was no time left for moderation. As a result, the possible negative marking has now been converted into the following. The lab TAs who will distribute your answer sheets back to you during your respective labs will be going through each paper for that lab batch and wherever they find that there is a reasonably good amount of documentation proper indenting, they will give bonus marks. Those bonus marks for some of you will get added. Bonus marks will be at most two of course. So it will be zero, one or two depending upon the qualitative nature of your responses. But please remember that you will soon be embarking on doing your projects and when you write programs for your projects, if you don't them write the first time correctly, you will generally not have time later to revisit them to include additional comments or to indent them properly and so on. There is something which you have to do right from the beginning correctly. Most of the questions except question four and perhaps to some extent the merging that was required within two portions of the array where there was no space to use the sentinel technique that we had used in the illustration here. All other questions were more or less similar to what you have been doing in the labs and earlier. And I was quite disappointed that people made mistakes even there. I will point out some of the observations that we have made while correcting the answer books. But first let me present some possible alternate solutions. So the first problem question one A was given a number M which has odd digits find out and prints the middle digit D. For the sake of completeness in this solution, I have assumed as is expected to be assumed that M has been read and validated. In some other alternate solutions on the slides here I have done input processing as well. But in questions which does not require you to do so, you could very well omit those portions. So here the approach that we take is first we find out the number of digits in M and after finding out the number of digits we try to extract the middle digit and output that. So here is one alternate solution which first finds the number of digits in M. I use a temporary variable in which I preserve the value of M. I start with some count arbitrarily at one and I keep dividing this time by 10. So for example, if my number temp 1 which is essentially M is say this at the beginning then let us see how through this iteration what will I get. Basically what I am trying to capture is the value of I which is essentially a power of 10. So I am trying to find out the number of digits here. So what I do is I keep dividing this by 10. Initially this is 1, 2, 3, 4, 5 for example I will be 1. The iteration that you see here while temp 1 greater than 9 essentially I am trying to check whether I am left with a single digit value. Greater than 9 will mean that I have a number which is 10 or more which is 2 digits or more digits. So I keep reducing it till it reduces to a single digit number that is the logic of this loop. So if you notice this iteration will the first iteration this will be divided by 10 reducing it to 1, 2, 3, 4 and I will increment as I plus plus to 2. In the next iteration this will reduce to 123 I will become 3. In the next iteration this will become 12 I will become 4. Observe that the number temp 1 is still greater than 9. So I will actually carry out one more iteration because temp 1 is greater than 9 and I will get here just 1 and I will get this as 5. Notice that what we have done is by successively reducing this we have got value of I incrementing from 1, 2, 3, 4, 5 essentially we are counting digits. There are people who have used arrays for storing digits. It is not required although you could do so. However there are people who have completely neglected the stipulation that M has been read and they are reading individual digits of the number. So they are reading a 0 into some digits. They are actually asking for input how many digits the number has. Then say give me individual digits. Now if I tell you how many digits the number has and if I give you individual digits you do not require any brainstorming computation to figure out what the middle digit is. That is completely not in concerns with the spirit of the question. The question required you to look at a number. The number need not have been 5 digit, need not have been 3 digit, need not have been 7 digit. It could be any number. And therefore it is appropriate that you first try to figure out how many digits that number has and then determine what next you can do. So observe the output statement here which says the digits is R which is essentially 5. A good programmer will also check whether the number of digits is even or not. Why? Because if the number of digits is even then a middle digit is not correctly defined. I do not expect you to do that in an exam of this type. The first exam is the first course. But as good programmers you would automatically think of doing this. Now all these thinking should have happened before you start writing the program. And therefore I would have expected a comment here that I will first find the number of digits if they are R then I will proceed to find out the middle digit. Some single line, two line command would be useful. Next I proceed to determine the middle digit. I know the number of digits. The middle digit is to be extracted and since I know the value of I, I know that the middle digit will be at the position I by 2 plus 1. Since I is odd 5, 7, 9, I by 2 will be 1 less than the middle point and that plus 1 will be the position of the middle digit. So to get that we first evaluate temp 2 equal to 10 raise to power I by 2. So in this case when I is 5, I by 2 will be 2. 10 to the power 2 is 100. And if I divide the 5 digit number by 100, I will actually get the middle digit at the end. Then we divide temp 1 by temp 2 to get the middle digit in the last position. So I find the power of 10 and I divide the number by the, to get the last position at the middle digit and then extract it using modulo 10. The problem could actually be solved in a much simpler fashion, but this is just one alternate way I am showing that if you wanted to proceed in this manner, first counting the number of digits, then finding out up to what point you should reduce that to get the final digit that is perfectly fine. So here for example, if I put temp 2 and j, initially I start with temp 2 as 1. In the iteration j is set to 0, temp 2 is incremented by multiplying it by 10. So I get 10 here. I continue with the iteration, j becomes 1. 1 is still less than I by 2. I by 2 is 2 because I was 5. I will once more multiply this by 10, getting 100. At this time, j will become 2. Since j now is no more, is actually not less than I by 2, this iteration will terminate here because j less than I by 2 is the condition up to which the iteration can continue. Since 2 is not less than 2, this condition is false and therefore I will come out. Observe that when I come out, I have got actually this which is 10 to the power i by 2. Now when I come out of this loop and I come out here, I calculate now temp 1 as equal to original m which is 1, 2, 3, 4, 5 divided by 100 and I get 1, 2, 3. All that I have done by doing this funny logic is I have got the middle digit in the last position of a number. Please note that it does not matter whether this is the only one digit number, three digit number, five digit number or 21 digit number. I simply take the modulo 10 of this number and I will get k is equal to 3, which I out. So this was a simple problem and there is absolutely absolutely no reason why every one of you should not have got the correct answer. Unfortunately, some of you haven't worked it out properly. Here is an alternate method of doing it. This attempts to find out the digit in a single iteration. So observe what it does. It takes the value of m as temp 1, k is equal to 1 and it evaluates k as the modulo 10 of temp 1 that is remainder of temp 1. So let us just do exactly what we did earlier. Our m which is essentially temp 1 and our k, initially temp 1 is m. In our example, we have taken it as 1, 2, 3, 4, 5, k is initially 1. Immediately after assigning 1 to k, I am reducing k to 1 percent 10, which means I will get this as 5. So observe a silly error here. This is all deliberately put. This is we have tried to process into the shoes of students who might think of this logic. Because of our penchan for initializing every variable, somebody might initialize k equal to 1. Notice it is not required because k is immediately being given another value, which is a proper value, which is the last digit of temp 1. So k will now have 1 will go out and 5 will come in its place and there is another one called temp 2, which is set to temp 1 by 10. So I get 1, 2, 3, 4. So what we are doing is actually we are taking this number, extracting the last digit of this number and putting it in k and the remainder of the number we are putting it in temp 2. Now let us see what we do inside this loop. Why temp 1 divided by 100 not equal to 0. What is temp 1 divided by 100? How much is this in the first case? 123. Is it equal to 0? No. So I will actually get inside the iteration and inside the iteration I will calculate k as temp 2 modulo 10. So k becomes temp 2 modulo 10, which means this becomes 4 now and temp 2 itself is divided by 10. So temp 2 now becomes 123. Notice I actually have got k as temp 2 divided by 10 as 4 and 1, 2, 3 here. Now I am doing another interesting thing. I am dividing temp 1 by 100. When I divide temp 1 by 100, what do I get? 1. So notice the logic that is being followed. Since I am supposed to find the middle digit and since I have odd number of digits, if I keep dividing the number by 100, 100, 100, I will approach that middle digit. So for example, first time it was 12345, next time it is 123, next time it is 1. So I am reducing that number by factors of 100. I go back to this iteration again. Temp 1 by 100 is still not equal to 0 because I have 1 here. That means I will go through this iteration exactly once more. k will be calculated as temp 2 modulo 10. So this will be calculated as 12, sorry, this will be calculated as 3 and temp 2 will be calculated as temp 2 by 10, which will become 12. I divide temp 1 again by 100, getting 0 this time. So it is very interesting logic. I am dividing temp 1 by 100 every time, but I am dividing temp 2 by 10 every time and I am extracting the so-called last digit from temp 1 here. So I got, sorry, temp 2, original temp 2 would have been 12345. So I get 543 and I stop my iteration after I approach the middle digit. So I can do n number of different ways of finding it out. Ultimately I know that when I get out of this iteration, the value of k is the middle digit and that is what I print. Again, this is another logic, not necessarily the best logic. I will leave it to all of you to work out the most elegant program. In fact, my colleague Prasar Houthi is going to announce a programming contest where you will have chance of proving yourself or an extended period of time, not under timed conditions of an examination. This question was another giveaway. We had solved many such problems at the beginning. We had looked at flow charts. All that this question asked is that if x is less than 0 and y is greater than 0, then z is equal to y minus x. If x is less than 0 and y less than equal to 0, then z is equal to this, etcetera. There are four conditions. If you look at this carefully and many of you have done that, you have figured out that the given logic adds absolute values of the given numbers. So absolute value of x plus absolute value of y is z, independent of any conditions. However, if you want to implement these conditions as they are stated using the conventional conditional execution logic of c, c, c plus plus, I start by reading the values of x and y which is not required as far as the exam was concerned because these were supposed to have been read. But look at this alternative solution one. This depicts exactly the complex logic using if then as. Say if x is less than 0, then if y is greater than 0, z is y minus x. If y is less than equal to 0, z is minus y minus x. This if condition closes and immediately this is followed by an else condition. Now this is a slightly lengthy program because the number of lines of code that you write or more although you do not write much in every line. But this is the cleanest representation of the logic that you have here. You could therefore do this. The problem did expect you to write an output statement and that is something amazing I find. We had specifically stated in the question paper that you do not need to write input. You can assume valid input to be present in the variables and arrays. However, the output statements must be present. So, there are students who have just written c out z. c out z will give you a value on your terminal when you execute that program and that could very well be value of x, y or p or q or anything. How many times we have emphasized that no output should be produced unless it is accompanied by an appropriate message saying this output is this, this, this. One of my colleagues pointed out that because I am giving you a lot of ready made programs in every lab, you are tempted to cut and paste those programs and therefore you rarely write individual lines by your hand on paper. In fact, you rarely type individual lines on the terminal as well. Now, that is probably causing this problem. It is important that you write programs by hand. It is important that you write those statements by typing them on the machine, the smaller ones. If you do not do the smaller ones, while writing the bigger programs you will be tempted to take shortcuts. That is something that needs to be avoided. Anyway, this logic can be further simplified in terms of the length of the program by simply putting your if statements like this. Exactly the same if statements. Here we observe that what you are executing after every condition checking or in the else part is a single statement. A single statement need not require braces. So, curly brackets are not required if you have a single statement. However, the logic is not as obvious as it should be unless you actually put this else indented along with this if and this else indented along with this if and the third else indented along with this if. Please note that as per a C++ compiler is concerned, I could have written all of this in a single line and that will still be a valid program. But if anybody has written that, you will get a fat zero in any programming examination because that program is a worthless program from a human reader's point of view. So, I will tell you this last time. You write programs correctly not only so that the computer can compile and correctly execute those programs, but you write programs such that those programs can be read and understood by other human readers. It is as important as writing correct programs. No less. Here is a third alternative way of solving this problem. I do not want to exercise my mind to see what are the possible combinations of if then else. Somebody has prescribed that the value of z should be calculated. This is perfectly correct solution. Absolutely nothing wrong with it. I am writing every condition explicitly. If x is less than zero and y greater than zero z is equal to y minus x. If x is less than zero and y less than equal to zero z is equal to minus y minus x. Perhaps the program is less elegant, but it is correct. It will work correct. The other problem with this program is even though the first condition is true and said evaluates to this, you will end up evaluating all the remaining three conditions because that is what the program stipulates. So, you can say it is slightly less efficient, but since correctness is more important, the program is corrected. Unfortunately, even here, people have made mistakes. For example, there are some people who are still writing a single ampersand here. Single ampersand as a logical and is a mathematical notation not C plus plus syntax. So, and and or logical and and logical or how to be and and and vertical bar, vertical bar. No other notation is permitted. So, please remember to use the correct syntax. Again, you have to output whatever the solution that you get. Here is a fourth alternative solution. As I told you, if you read the conditions carefully, you figure out that z is equal to absolute value of x plus absolute value of y. In which case, you can say you have to write this though. You have to say that I can use the standard library function absolute, but this solution requires use of standard mass library and therefore, we must include the following at the beginning of the program. This must be written as part of the command even though you are writing a portion of the code. Without this command, this code is invalid the way the program is given. Please remember you are supposed to write programs to replace those dashed lines. So, the rest of the program is as type. If you require any additional variables, you are required to declare them. If you require any additional inclusion of this kind at the beginning of the program, you are required to state that. If this was the only error, I would have been happy. Unfortunately, people have taken liberties to write z is equal to. Please understand that the semicolon will figure out what semicolon is. The c plus plus compiler figure out plus symbol equal symbol z. It can't figure out this. A very wise compiler may possibly say that one vertical bar from the or symbol is missing, but this does not make sense. So, please remember to use the proper syntax of the programming language that you use. If you are programming in Java, probably similar symbolism or syntax will be there, but if you are programming in Pascal or Algol or Fortran, you'll have to use different syntax. And as I told you right very early, by the time you graduate and go out, there will be 10 new programming languages which you may be required to use. The programming concepts will still remain exactly same at fundamental level. You must therefore be able to quickly map your understanding of this syntax into whatever syntax is available and correctly use it. That is required. Here is another question which I thought was pretty simple. We had worked out how to find out the maximum number given n numbers. This problem said that there are n numbers which are available as n elements of an array A. And you have to find out the second maximum, the second largest. This problem again can be solved in multiple ways. I have written here for the sake of completeness, a whole lot of comments. Please note that you were not required to do any one of these because all of this was stated to be assumed. You could have assumed that n numbers have been read into 0 to n minus 1 elements of array A. So, all this can be assumed. But just for the sake of completeness, if you were to write a program fully, I would expect you to write all of this, including those hash include and using namespace, STD and so on. This is to illustrate the notion of proper input and validation. So, for example, since n is constrained to be within the size of A, value of n cannot be greater than 10. Why it should not be less than 2? Because if I am finding out the second largest element, there have to be at least two elements. This particular program in the alternative solution 1 uses this example 5, 7, 24, 18, 72, 71 as values. We have gone through this umpteen time, but let me quickly go through this to find out how this logic will work. So, what is this logic? Now, this is what is important. Whatever logic you use, you are expected to write a command. Majority of the students while solving this simple problem have not written a single line of command. That is incorrect. Actually, you should have been given straight away zero marks for that question. We have not done so just because this is the kind of exam environment that you have. But please remember, if you don't write commands in exams, you will not write them while you are writing program. That's a habit that you have to pick up. So, look at these commands. The commands could have been written in different ways, in different styles, and of course, must reflect the logic that you use. Here, what we are saying is that we are using temp1 to store the largest and temp2 to store the second largest value at any stage of our array square. We are implying thereby that we will be having a iteration which will scan the entire array element by element. And in doing so, we will maintain two temporary variables, one holding the maximum value, one holding the second largest value. We start with the assumption that 0th and 1st elements are largest and second largest. If these are not in that order, we swap these. So, temp1 is a0, temp2 is a1, arbitrary assignment as we do for finding out maximum. And then, I check whether a1 is greater than a0. In this particular case, 7 is greater than 5. So, what will happen is, if I take temp1 and temp2, initially, I will have temp1 as a0, which is 5, and temp2 as 7. When I execute this condition evaluation, a1 will be found greater than temp1, and therefore, I will push this element a1, which is 7, to temp1, and element a0, which is 5, to temp2. I could have swapped these using a temporary variable, but it is not required, because I have a0 and a1 available to me anyway. The next part is scanning the array. So, look at the command, which says we examine the array from second element onward. If that element is larger than temp1, we move temp1 to temp2 and assign that element, which is now the largest to temp1. However, if that is not so, we compare the new element with temp2 and change temp to it lesser. So, let us look at again the same logic. I have started with temp1 and temp2 from my previous slide. These values are 7 and 5 respectively. So, please note that at any stage I am holding, the largest value is temp1 and the second largest in temp2. This iteration goes from i equal to 2 to less than n, which means it will examine 24, 18, 72 and 71, because this is 0th element, 1st element, 2nd, 3rd, 4th and 5th. So, what happens when I do that? I examine, let me just write down that i is equal to 2 here. So, I am examining 24th element. I compare whether 24 is greater than temp1. Yes, it is. If it is, what do I do? I push temp1 into temp2. So, this goes here, 5 is removed and the new element 24 comes in temp1. So, again true to what we said, true to what we said, we retain the largest value under temp1 and the second largest value under temp2. We go to the next element now, because i becomes now 3. I check 18. Is 18 greater than temp1? It is not. Since it is not, I come to the else part. Is it greater than temp2? It is. So, I replace 7 by 18. So, again I am maintaining the largest value here, smallest value here. Next I look at 72. Is it greater than temp1? Yes. So, I push this 24 here, removing 18 and I get here 72. So, again 72 and 24. Up to whatever point I have examined, I always maintain maximum here, second maximum here. The last element is 71. Is 71 greater than 72? No. So, the first condition does not apply. I come to else. Is it greater than temp2? Yes. 71 is greater than 24. So, I remove this and replace it by 71. At the end of this, the second maximum element will always be found in temp2, as we have seen that. Now, this is one way of doing things. You could use a single variable. Here is another approach which uses a single variable D to hold the second maximum. It starts with D equal to a0 and for i equal to 1 to n minus 1, it keeps comparing D with ai. If D is less than ai, then max2 is set to D and D is set to ai. So, it will continue glancing through that. Any number which is less than the scan number up to so far will be put in D. Almost like the previous algorithm, D will track the second largest. You will notice that in this particular case, by the time you come here, this would have been maximum. 24 would have been maximum and 18 would have been second maximum. When you come to 72, 72 would be largest and 24 will become D. Even if this is 72, D will still remain 24. So, this particular algorithm has the possibility of working out the second maximum even though if you have a series of maximum numbers, that is your 72, 72, 72, 72 and just one number which is all. The problem with this particular thing is that if all numbers are same, the entire array has only 72, 72, 72, then technically there is no second maximum. Each digit, each number is maximum. Observe what happens in this case. I am arbitrarily saying D is equal to a0 and then coming out with this iteration and then saying second maximum in the array is max2. If D which is a0 is equal to all other elements, this condition will never be satisfied and therefore you will never look at this max2 assignment at all. This assignment statement will not be executed. What will be printed then? Garbage and if that garbage happens to be 72, you will say 72 is the second largest. So, this is the danger. That is the reason why you must actually execute algorithms by hand and do the needful. Here is another alternate solution 3. I have not worked this out, but this works on a simple principle that I sought the array. Sorting the array to find out second maximum is a costly way of finding out second maximum. The other approaches were on a single scan of the array. We know that when we want to sort the array, we require two scans. So, an outer iteration and then an inner iteration to sort the entire array. But that is okay. That is a correct approach still. If you have sorted the array in descending order, a0 will be largest, so you pick up the next element a1. Many students have taken this approach. However, this approach again suffers from the same problem that if the two elements are same and they are largest, then both a0 and a1 will contain the same value and a1 will not be the second largest. There is at least one student who has taken care of checking this. So, that student checks whether the next element is same as this and then the student skips successive elements till the student finds the second largest. His solution also does not presume that all elements could be equal. So, at the end, there is some haziness. However, this is just to emphasize that even in the simplest of the problems and the most straightforward of the logic, you might actually find squeakles of this card. And this you have to be very attentive to when you solve larger problems of which such logic is a smaller but important part. This was another straightforward problem. In fact, many of my colleagues are angry with me for giving such simple problems because we had discussed the null terminated string very well. A character C was given. It is assumed to have been read. An array has been defined as str, which is character 40. That means it can hold 40 characters string. Since in a 40 character string, you can have only 39 valid characters if the string has to be null terminated. So, you cannot have the actual string length greater than 39, which means the number of elements which will occupy a given character replicated will be 0 to 38. So, we are checking whether q is greater than 38 or q is less than 0. Otherwise, we are saying the number is invalid. However, you are not required to do any one of these things for question one at least. Why I am writing this is for two reasons. One is for the sake of completeness and other to emphasize that it does not is not a bad idea even in an exam to automatically do these things because these things should be ingrained in your mind such kind of validations of input. The logic is exactly a two line logic. I set up an iteration for i equal to 0 to q minus 1 because the q times I have to repeat a character and I assign the character C to ith element of str. Having filled up the q elements of array by this character, I just put up a backslash 0 in the last element which is the qth element. So from 0 to q minus 1, I replicate C and then I assign backslash 0 to q. I do not think the amount of marks given for this problem are justified and yet I was very sadly surprised that many people have not got this answer correct. There is somebody who has written it was not the character C which was supposed to be assigned. There is a care variable in which a value is supposed to have been read. The care variable could contain a star, question mark, d, z, anything and that character was supposed to be replicated. Somebody has said is completely incorrect. You cannot make an assignment of a simple variable to an array. A simple variable can be assigned to only one element of an array and you have to successfully assign it to various elements. So there are some of you who have perhaps not got the hand of distinction between array elements and the equivalence of array elements to simple variables which must be dealt with appropriately. I would strongly recommend you go back to basics, read some more examples and do some hand coding. Here is an alternate solution. It is alternate only in the sense that it shows two ways of printing it. Either I can print individual characters or I can print directly by saying c out str. Both are valid. In one case I must terminate before the end of string. Since I know in this case the length of string because that is stipulated it is easy. But otherwise as we examine in the class in some examples you have to look for the backslash 0 which is the null character and stop printing before that. If you print character by character. However you can also print the string. Question two required you to write a complete program. You had all done lab assignments on writing functions, passing parameters to function. This was a simple problem. You were expected to take a number and calculate an appropriate check digit for that number as per given logic. The logic was very simple. Take the number. Find out the sum of all its digits. Then take a modulo 10 of that sum because check digit can only be one digit. So if the sum comes 25 modulo 25 modulo 10 is 5 and 5 becomes the check digit. This is by the way very often used in practice. So you will find book numbers which are ISBN numbers. They are actually a more stringent checking logic. The idea is that given a large number such as your such number as a part number people could make that num. Somebody may type one digit wrongly or write one digit wrongly. A single digit error is always caught by the check digit. So if you have a seven digit number which you are writing. If you make a mistake it can't be caught. But if you have a eighth take check digit attached to it and you write that eight digit string. If one of these digits is wrong whenever you recalculate the check digit. The check digit will not match the one which you have entered. You may make a mistake in entering the check digit itself in which case it will still be invalid. So you can check the validity by using this logic. That is why this is a common less problem. Coming back to programming. The programming was supposed to be more straightforward. Look at what we are doing. The name calculate underscore check underscore digit. Since you are going to do your project work now which has as much weightage as 35 marks almost twice as much as miss M. You will be expected to write professional grade programs. Which means function names such as F, G, P will not be valid. Function names have to be meaningful. This is just one example that it should clearly indicate to you what you are trying to do. This function calculates check digit for a given number. So N which is given is a parameter. I am declaring all variables that I might internally require. This is necessary because the function does not take any definitions from the main program. So here I am declaring D, I is an index variable and CD is the check digit which will be calculated. All that I need to do is I need to reduce this N by dividing it by 10 and by extracting the last digit. Exactly the same logic that we did while looking at a larger number F. Some people have tried to use an array which is perfectly fine but you do not need an array because you do not need to retain the individual digits of the number for any other purpose. You have to sum them up. So you can start with sum is equal to 0 which is initialized here and this iteration will successively extract one digit of N. So let us say N is again 1, 2, 3, 4, 5 then D will first become 5, N will become 1, 2, 3, 4, sum will become 5. Next time D will become 4 because 1, 2, 3, 4 has 4 as the last digit, N will become 1, 2, 3 and sum will be 5 plus 4, 9 and so on. So it will accumulate that sum and once you get the sum you find out modulo 10 of sum which is the calculated check digit which you have to return. Again there is absolutely nothing fabulous in this logic. You have done these kind of problems earlier but I am surprised that several people got this solution wrong. There are people who have used arrays which is alright but they have used arrays in very funny fashion. Some people have used arrays here so they have said int a 10. So they are passing a 10 digit number as an array not 10 different numbers. This was clear to our evaluators when they looked at the main program where the main program reads still funnier. It says give the number of digits in the array. So I say 5 or 7 of whatever. Then give individual digits which they read in this individual array element. Then they pass this element laboriously to this function to calculate the sum. If you have to ask so much you could already ask give me the sum of all the digits or you could have said give me the check digit. That does not solve the purpose. Here in this question it was stipulated that you are given a number. If given a number you have to calculate the check digit for that number was the objective of this function. The main program had a slightly different objective. The main program was supposed to read an encoded number. That means a number which is already augmented by a check digit and you are supposed to confirm whether it is valid or not. So here we declare an encoded number as int. We declare a check digit. We declare an and we also have a variable called cd which is rhyming with the cd variable that we have used in the function. So obviously whatever that function returns I might use a similarly rhyming name there. That's the only purpose. I could have used any name. Look at what this program does. It actually reads a encoded number. If encoded number is less than equal to 9 it says invalid encoded number. Why? An encoded number cannot be a single digit number. If there is a single digit number for any coding that you are using say 5 or 7 it must be replicated. The sum of one digit number is the number itself. It's modulo 10 is that digit itself. So 7 when encoded will become 7, 7. So you can't have a single digit encoded number. You would be expected to keep this in mind and check whether somebody is giving you a wrong encoded number. After having raised this encoded number what do you do? This is what you are supposed to do. You separate out the check digit from the encoded number. Whether it is right or wrong you do not know but separation is very simple. You take modulo 10, you extract the check digit in some variable here. For the sake of completeness we are producing the check digit to be such and such thing. Now I have an encoded number and the check digit but I need to calculate what should be the correct take digit for which purpose I require the original number which I get by dividing the encoded number by 10. So please remember whenever you are handling numbers and whenever you want to deal with reduction of numbers or extracting digits, division by 10 and modulo 10 are the common sense operations that should occur to you automatically. A division by 10 will give you the number minus the last digit. This number you are supposed to pass on as a parameter to the function. So you say calculate check digit, original number and whatever the function returns you will put it in CD. Now obviously if the original given encoded number is valid, this CD should match whatever check digit you have extracted. If it does not match the number is invalid and that is what is done here. There are some people who have found this correctly but what they do is they do not print the thing correctly. It is not adequate to say check digit is this. This check digit you are referring to is one which was given along with the encoded number or one which you have calculated for the original. The correct output expected from this program because this program says validate whether the input encoded number is correct or not. You have to announce a judgment. Is the given encoded number valid or invalid and that can be done only by writing in if statement. If the two check digits match your output that given number is valid. Otherwise you say check digit should be so and so and the number is invalid. Even if you don't give the actual check digit it is okay but you have to say that it is invalid. So as I said people are passing on parameters and in fact there is one example which was mind boggling. This function let me just write this function as f. He is reading the array so the number which is assumed to be a large number is read in arrays d0, d1, d2, d3, d4, d5, etc. and instead of passing the array what is being attempted to pass to this function is d0, d1, d2 and after some time the student has given up and he has written etc. Now etc. is invalid parameter. C++ does not understand it. So for God's sake have some mercy on the compiler. The compiler is still not capable of understanding plain English language and it is certainly not intelligent enough to figure this out. Jokes apart I think it is absolutely essential for you to understand when not to use arrays and simple variables will do. Array should be used only when you need to retain the individual identity of elements such as in the question on images but we will come to that in a short while. Again this program was a giveaway. This question 3 again did not require you to write a full program question 2 dead and in question 2 you should have therefore ensured that you have written and put output statements properly, include statement properly at the beginning, written zero, closing blast etc. Some people have not done that possibly because the hangover of question 1 which did not require you to do that so you carried on to the next question. This problem I have just reproduced whatever it was. We have discussed this problem. We have discussed the problem how to sort an array. So given an array T1 how to solve that. We have also discussed the problem that if T1 and T2 are two sorted arrays how to merge them to get a single sorted array. Technically you looked at that solution and constructed a correct solution. Sadly many people miss the fact that when you have a single array then in a single array there are certain constraints that you have to work with. Let us look at what is involved here. This is the doing you can observe. It is actually putting alternate marks into the top portion and the bottom portion. So I is varied from zero to n by 2 and there is another variable j which I am carrying on for j equal to n by 2 and I am incrementing both of them. So as I varies from 0, 1, 2, 3, 4, 5, j varies from n by 2, n by 2 plus 1, n by 2 plus 2, etcetera up to n minus. And I am just assigning different values here. The data is artificially generated anyway. We have seen this problem in the class where we are generating data for marks and roll numbers and we are sorting those arrays. We had one array which we are sorting. Here we have two arrays within the same physical array. The way to sort this is very straightforward where even I have given you that this is where you should whatever you write will appear here. And what is the difference between this sorting algorithm and the one which was actually discussed in the class. The only difference is this should terminate at k less than n by 2. The array needs to be sorted only first half separately. After doing this you will get the array sorted. The question then said proceed to sort the second part. Again the only difference between this and the previous part is the starting point is now n by 2 and not 0. But it's the same. So either you don't read your class notes themselves properly or you can't map a solution into this strategy. There is at least one person I know who has read the class notes very carefully. In fact the student probably had access to the class notes because he is not doing this n by 2. He is starting with k equal to 0 and in addition to the marks he is also exchanging roll numbers which are non-existent here. They don't exist. Now when you are copying and pasting please don't copy non-existent elements at once. That is not good. Anyway the point that I am trying to make is the sample programs both in the labs and in the class handouts are useful for you to understand how exactly a certain logic is implemented. Beyond that they are not to be used. The tough part is the merging part. How do you merge the two parts of the array into top roll? Notice that in the class we have discussed the sentinel usage. A sentinel is a large negative value which cannot occur in the marks array and therefore can be written at the end of the array to signal that I have no more elements left to be considered there. Now when we discuss the problem in the class where two arrays t1 and t2 and we presume that there is an extra element available in which I could insert such a sentinel value. Unfortunately that liberty is not available in this problem. You will notice that the array size was prescribed as 1 lakh elements. You'll also notice that you might think for example that I might not be able to use sentinel for the top portion of the array because the next portion starts immediately thereafter. But I may use a sentinel element for the second half. Observe that the example given said number of students is one lakh. That means all the marks that have been generated will completely consume all the available elements in the array. The array has no space left. So you can't assume that the array has a space unless you say that I declare an array with 1,002 elements, 1,002 elements and I re-copy the generated data from 0 to n by 2 minus 1, the first half, copy that into this 1,00,002 element array, add a sentinel value there, then copy n by 2 plus 1,00,000 last element and copy a sentinel. That is too much of a work to be done. In any case nobody has done that kind of thing and yet people have assumed the sentinels to be present or inserted sentinel. If you insert a sentinel arbitrarily at some place you're actually wiping out an existing valid data element there. That's not possible. The way to handle this could be one of the ways is this. Observe that I require sentinel merely to signal that I have exhausted a specific array for merging. Here it is not a separate array but a portion of the array. So I could have exhausted either the top portion or the bottom portion while merging. I do the same logic there. I start with P and Q as two pointers which will point to the top half and bottom half. So initially my P points to 0, 0th element and my Q points to n by 2th element. And this is my resultant array. Notice that I have to compare this element with this element to decide which one goes here. If I have to put the burst array into a sorted order. So if this element goes here for example. Then I have to increment the pointer P. Suppose this goes here. I increment the pointer P to next one. And so on. If during this process I have exhausted all elements of either this top half or bottom half. I must not increment P anymore. But comparison should start being done with the smallest possible sentinel value. So notice how that problem is solved here. I am not comparing the A P and A Q or marks P and marks Q element at all. Ordinarily for deciding which goes here I would have made a comparison of this type. Whether marks P is less than marks Q. This would have been the statement I would have used in order to decide whether the Pth element goes to the results or Qth element goes to the result. But instead of comparing the two elements of the array. I am extracting them into two temporary variables. Val 1 and Val 2. Val 1 and Val 2 will ordinarily be marks P and marks Q. However if the index P and index Q has already exceeded or already reached the last element. I know that I should not compare that. And therefore I will replace that with a sentinel. And that is the reason why a single sentinel value minus 999 has been imposed. Now this is one way of merging. You could think of different logic as well. This did require some thinking. It is not a trivial problem. And you should get this right. I would expect you to get such problems right. And we will probably have some more sample problems for you to work. Once you do that of course you here you decide whether marks P goes to top marks or marks Q goes to top marks. That depends upon Val 1 and Val 2 being compared. Once you do that this output statement is also part. Again this problem I would expect many of you to get it correctly because we actually solved a very similar problem in the class. The last problem did require some thinking. It was a new problem and you are required to look at what you call neighboring elements of a two-dimensional array at some center point. That is something that we are certainly not examined and this was the new. I am very glad to say that as many as 63 students have got the question for B part directly. Which means they could figure out how to calculate the neighbors, how to calculate the median of the neighbors and how to put that in the final value. Okay you have to apply a median filter to a digital image. Now the first part which was I thought was the simplest, even there people have made mistakes. This part required you to declare arrays, variables and read the image array. Declaration should be very simple. An image array of 1200 by 1200. A new image array of 1200 by 1200. There are people who have not declared the new image array. Although I had even specified or suggested a name. I would like you to notice this short end. Please remember that if and when you handle image data of this kind which is monochrome images where the pixel values are between 0 to 255, you would not like to squander away the space and therefore ideally and that is what is done in most professional image processing programs that the arrays are declared as care arrays. So care image 1200 by 1200 because a care can hold an integer value between 0 to 55. However if you declare the arrays as care arrays and read the elements as integers you have a problem because the C in statement will assume for a care array an individual character being given. So if you write 126 it will not read 126 as a pixel value. It will read only one as the character for that particular pixel. So if you declare it as care you have to read individual values on C in and assign those values to individual elements of the array in which case the integer would probably be convert properly be converted to care. However a good compromise is short end which is typically a two byte value. As far as the exam is concerned you could have very well said end and that is perfectly valid there is no problem this is just to explain. Now here's a point you will not think of all possible variables and arrays that you might require to work with when you are writing your program. As you write your program you might require variables. It is perfectly possible and all right for you to declare those variables and arrays as you go by or put them on to write margin something, leave a few lines at the beginning of the program and include all the declarations at the beginning. This is the style which is followed here. So I have hw which is of course stipulated as height and width ijk m etc are variables which will follow indices. I1 and J1 are other index variables I will explain why I have used them. NEB is an array short for neighbors. Why the array is 100 element law? We are stipulated only a 3 by 3 billion filter. In actual practice the filter could be 5 by 5, 7 by 7, 11 by 11, 15 by 15. So therefore this is a generalized version. hw s defines the width it is 3 by 3 so 3 is the width and the total number of pixels within that window is w size which is equal to 9. So if you have used 3 and 9 specifically as constants it is perfectly fine. But remember this whenever you are writing a program try to generalize as much as is possible. So if you believe that filtering could be done with larger arrays which may not occur to you because you are not studying image processing you would automatically try to generalize it and put a larger array here. pmax, ppost, ptem, pmed are all other temporary variables. It is not clear why we need them here but they would become clear as we expand on our idea. Reading the image matrix again very few people have written these comments. These comments were mandatory. You were required to design a file format. You are required to design a format in which data would be given in. The least you should have written is one example is here. That I will prepare a text file in which in the first line I will write two numbers one reprinting the height of the image another reprinting the width and then subsequently I will have many lines they will contain pixel values w pixels for each of h rows. And if I am writing a professional program I will put up this program on the table in which case I will give you an 8 by 8 image data kept in a file image data or text as sample. Whenever you are doing programming projects and you are stipulating some input to be given you are expected not only to define how that input should be given but you are expected to give sample data saying such will look the final data file that you will make. This was mandatory nobody did that. Very few people have written this comment. However most people have read the data but very few people have checked the validity of the data. I can understand if you don't check this I have checked w less than 3 or h less than 3 y. If I am doing median filtering by 3 by 3 window an image of the size 2 by 2 or 2 by 200 that makes sense. But it is okay if you don't check that but you must check whether w specified or h specified is greater than 1200. Sorry this should be h here not w. This is a mistake here. But you must check the bounds. After that you will go ahead and you will read individual elements. I would like you to look at this code carefully. Ordinarily you are expected to validate the data. If you find any invalid data it is perfectly right for you to say sorry data wrong I am fitting. However when you write programs to read large amount of data consider this case you have written somebody has prepared a data file for a thumb print image of 200 by 120 pixels or something. Now somebody has laboriously digitized the data and typed in the value and you find that the fourth row fifth column pixel value is mistakenly written as 1230 instead of 123. Now that is error. Suppose you quit saying sorry you have error here. Now that fellow goes back ok 1230 corrects it. Gives it again. Later on when you run now your program again you will find some error in 67th row 43rd element. So every time an error is detected the user will be required to go back check that error correct that error and bring the data back. This is not done in professional programming. In general you will scan through the entire data and validate as much data as you can giving appropriate error messages so that the user can go back and correct all the errors in one shot. So look at how this validation is being done. Again this need not have been done in the exam but I am just for the sake of completeness putting this here I am setting up an input error flag starting with 0. Now I am setting up an iteration i equal to 0 to h minus 1 j equal to 0 to w minus 1 reading individual image elements I am outputting those image elements for the sake of completeness then I am checking whether every pixel value is between 0 to 255 or not if it is not I am printing the coordinates i and j and the value that I have got it but I am not terminating my program. I am continuing with the loop however every time an error is noticed I am incrementing input error flag by value. Now I hope you can guess what we are trying to do. I have printed by 1200 elements even if there are let's say 57 errors I would have printed all those erroneous pixels giving i and j and I would have a count which is 57. So I now know exactly how many errors I have and I am therefore in a position to produce an output of this guy. If input error flag is not equal to 0 then I will say total erroneous pixels found where so much is correct the input file and rerun the program and I don't have to print the erroneous pixels here I don't have to remember them here I have done that job while reading the data itself. So erroneous pixels have been printed, total errors are printed and I am quitting now. On the other hand if everything is okay I proceed to the filter. Now this is where some thinking was required notice first of all what is happening here I have a large image and I have to put up so this is my moving window I am trying to slide this window from left to right starting from the top corner. Consequently this window will be centered on some pixel that pixel is moved by changing i and j. So I am setting up an outer iteration which will move from 0 to h-1 that means it will go from i equal to 0, 1, 2, 3, 4, 5, 6 up to h-1. For each value of i I am changing j from 0 to w-1 so that means I will go through all these pixels. I am essentially covering 1200 by 1200 or w into h pixels in the process each of those pixels will become a center point or a central pixel at which to apply the filter. Dynamically I presume this is my center pixel. What is the coordinates i, j? What are the three neighbors? What are the coordinates of these points? i-1 j-1 i-1 j and i-1 j-1 in exactly the same fashion I can find out the coordinates of these three which are i-1 j-1 and this one will be i-1 j-1 and this one will be i-1 j. Similarly I can find out the coordinates of these what am I required to do? I am required to collect these nine elements 1, 2, 3, 4, 5, 6, 7, 8 and 9 elements and get these 9 elements into separate array, sort them out find out the median and take that median and take the corresponding position to this particular element in the new image array and replace it. I have reproduced these two lines here to explain the logic that I am using. I have to collect 9 elements. So I am varying m from 0 to m less than w size. w size is 9. So m will vary from what values? 0, 1, 2, 3, 4, 5, 6, 7 and 8. Agreed? These are the values in which m will change. What I want to ensure is that the neighbors which I collect have the coordinates i-1 j-1 i-1 j, i-1 j plus 1 etc. It is perfectly alright if you used the actual values here as i-1 j-1 i-1 j etc. and directly assign those values. So you could have said neighbor m or neighbor 0 is equal to this neighbor 1 equal to this, neighbor 2 equal to this, where you stipulate i, j, i-1, j-1 etc. That is perfectly valid. However a better program would be one which will try to generalize this because if instead of 3 by 3 array your filter is 15 by 15 you will choke up writing so many statements. So look at what has been done here I am calculating an artificial variable i-1 which is i plus m by w s minus 1. W s is 3. So observe what happens to i-1 when m is equal to 0 what is i-1? m is 0 divided by w s is 0 minus 1. So basically i minus 1. When m is 1 what is the value of i-1? i. When m is 2 what is the value of i-1? i plus 1. Again look at the next one. When m becomes 3 when m becomes 3 3 divided by 3 is 1. So what is the value of i? What will be j-1 here? j-1 is j plus m modulo w s. Modulo 3 m modulo 3 is 0 1 2 0 1 2 0 1 2 agreed? So I am able to replicate some kind of a sensible movement of i and j i-1 and j-1 around this m. This will take care of my requirements for all the image elements which are within the boundaries. What about edges? The way I have attempted to solve this problem is by saying the following. I have calculated i-1 and j-1 correctly for any given i and j. i-1 and j-1 as m goes from 0 to 8 i-1 and j-1 will span all the pixels which are within that window. That much is correct. The problem is that some of these elements could lie on edges or beyond edges. So before the top edge or after the bottom edge. So here is the possibility. These are four edges. Imagine that my i-j is here. That means my i-1 will actually go below 0. So this is 0. It will go below 0. It will become minus 1. Similarly if I go beyond that this is h minus 1 pixel. This will become h. Obviously when this happens I am supposed to replicate the h pixels. Notice what am I doing here? If i-1 becomes less than 0 I increase that by 1. That means i-1 is brought back to 0. If j-1 becomes less than 0 I increase that by 1. The other edges are similarly taken care of. If j-1 is less than 0 is this but if j-1 is equal to w which is this which means I have gone beyond this edge I will reduce it by 1. By this simple strategy of four statements I am able to replicate the pixels exactly as described in the logic. Including the corner pixels. People have attempted to solve problem c and problem d in the following fashion. They have tried to assign something like nav 0 equal to image i-1 j-1 nav 1 equal to this etc. This they have done for part b and therefore many of them have got the part b corrected. In part c you have four edges 1, 2, 3, 4. So people have mistakenly written these kind of statements for this edge for example. So wherever i-1 is there they will replace it by i. So those nine statements have been written correctly for this edge. Some people got tired at that level. Some people continued and they wrote similar nine statements correctly configured for this edge. So they have written for two edges and they have said similarly for other edges. Unfortunately c++ compiler does not understand the words. Now I have given a solution in one particular fashion. Many of you would think of other alternatives. But I would like all of you to think about implementing a desired logic in multiple different ways and choose the one which is most appropriate. So this particular lecture was devoted to discussing this from Friday onwards we will proceed further. We will now look at the notion of structures arrays of structures do some data processing and other work. But last announcement this was the output. This was the part. You have to sort the array of pixels in the window and after sorting you have to find out the median you have to assign the median to new image that's it. Please note that all of this code is being executed repeatedly for different values of i and j. So at the end you would have the entire matrix covered and then you are supposed to output. Just one second. Even here exactly the same way as you rate the data that was requirement. Many people have goofed up here. So just go through I will be putting up these slides on the web on the model. Please look at them. Thank you so much.