 All of you have seen the videos? Good. So on your notebooks, write a program which will print out, which will calculate the value of max and then output it. It won't take more than two minutes. You don't have to write hash include IO stream using namespace STD etc. For your benefit, I will also write down here the last statement. So assume that this part of the program is written and you have to write a few statements in between, which will operate upon the values of A and B which have been read, will assign an appropriate value to max and print it. This was already done in the video lecture, so you should be able to almost write it within like 30 seconds. Have all of you written this? Good. Yeah, everybody done? Alright, now quickly swap your notebooks and find out if there are any errors. If there are no errors, then you agree on the program that is written. Please note that there are more than one way in which this program can be written. Alright, this is one program. In A, B, max, this is all given statements C input. If A greater than B written inside the process semicolon, any problem with this statement? Yes. This statement doesn't make sense. It is not an assignment statement. It is actually a condition. A condition is never written like this. Next it says output max is A, else C out B. The problem is the intent is very clear. Person has understood what is to be done but it is not done properly. Here is another program. So this program says if A greater than equal to B, C out A and L, else C out B and L. Is there any problem with this program? The point is the person is correctly finding out which of the two is larger. Actually both can be equal which we did not really mention anywhere in the problem. If both are equal, it does not matter which number is taken as output. The only problem is that this number is not remembering which was the maximum. And therefore you have not realized the significance of my declaration which says int A, B, max although not explicitly stated in the problem. If a variable max is there, it is obviously intended for some use. Otherwise why should a location be declared at all? So what should be the program then? I should clearly use an if statement to check which one of the two is greater. For those of you are making a mistake, a condition or comparison is never written in a standalone fashion. It does not make sense. It is like writing x multiplied by y somewhere in your program as a single statement. Doesn't make sense at all. Any expression must come in the context. So only in the case of if statement you can write a condition in the bracket which will be evaluated by if. What is the condition you would like to write? And if that is so what do I want to do? I want to consider A to be the largest and assign it to max. Else? I will have to output that. So this is one way of writing it. What are the other alternative ways? I can say if B greater than A, I can say B greater than equal to A. I can check different conditions and accordingly I will have to swap my assignment state. If both the numbers are same, then depending upon the condition that I have put in the if statement, either A will get assigned or B will get assigned to max. But it doesn't matter because both numbers are identical. They have the same value. Is that clear? Alright. Now this is one way of doing it. I could of course indent it better. So you know what indentation is. For example, if I wish to say if A greater than B, then I might start this brass here but write max equal to A here and then close the brass. Then I will say else and close the brass. Same thing is written in an expanded form such that it is more readable. For very small statements such as a single assignment, it does not make a difference. But suppose if something, then you wanted to do 20 things and else you wanted to do 10 other things, then that grouping is better written in an indented fashion like this. Is that clear? Fine. So this is very simple. Now solve this problem. So you can assume the declarations. You don't have to write these. You can assume an input statement which has read in A, it has read B and it has read C. Now you have to write your statements which will actually find out the max which is largest of the three numbers A, B and C. Come on, start writing. As usual, you don't have to write the last statements which are, as you know, are going to be something like C out. So assume that this part of the program is written. By the way, while you ought to know exactly what spellings to use, where to put opening brass, where to put closing brass, etc., etc., at least in the initial days you should write full programs so that your hand gets accustomed to coordinate with your brain to write correct spelling, correct semicolons, etc. But once you do that, it is stupid every time to type in all these statements. And that is the reason why programming skills actually try to exploit the fact that some standard structures would be available from your own previous program. You should be able to copy that program and write the main logic, write the declarations which are required for other variables rather than wasting your time in copying the same old. So come on now. How many of you have finished writing this? Oh, large number. Alright, everybody done? Have you swapped your copies? Do that now and check your neighbor's answers. So here is one program which says if A greater than C, max is equal to A greater than equal to B, A or B. So what he is trying to do is, if he is checking whether A is greater than C, if A is greater than C, then he is comparing A and B. And depending upon which one is greater, he is putting it on to max. What is the possible problem with this? B can be larger than A but smaller than C because B and C is not getting compared in this. A is greater than C does not necessarily mean that B is greater than C. So you all agree that this logic is correct? Alright, consider this. If A greater than B, then if B is greater than C, then max is equal to, what is this? A. Else, if A greater than C, max is equal to A, else max is equal to C. Seems to be eating like this but still is eating properly, right? Food goes in. The program is correct. Program seems to be correct in either case. Many of you have followed this kind of logic compared to etc. If so, try to solve this problem now. Alright, let me simplify the problem. Do not output the second largest number at the moment but find out the largest number and output it. Not just output. So there are to be five variables A, B, C, D, E and a max. And you have to find out max such that it is largest of all the five. So let me write down the problem here. Now before you begin writing your program, can you not immediately sense that if you were to follow the approach that was used by many of you and which was outlined here where you compare A with B and if A is greater than B then you compare A with C. Now in this case if that is so, you will have to compare A with D. Then again you have to figure out the relation D and C and there will be how many ifs you will have to write. So forget actually writing it but can you not sense that it will take a fairly long time to write this program and that you cannot be sure whether you are getting the right value or not unless you run the program many times and test it. And if you tirelessly do that my next question will be find out the largest among 17 numbers. Clearly the strategy and approach that we thought works very well for finding out the maximum of two numbers cannot be easily extended. That approach cannot be extended. Yet we do have to find the maximum. Now here is a trick. Forget the programming part. Suppose you are given 10 numbers or 5 numbers like this. Let me just write down 15, 7, 23, 4, 18. What is the maximum? You found the maximum by before even I completed writing. How? So are you better than the computer? In what way? You are better than the computer only in one way. That the algorithm that you are using in your mind you are not asking computer to execute that algorithm. You are asking the computer to execute a program which is written in a very fancy fashion which is not the method that you use while finding out the maximum. You have to write a program for a method that you actually use. Now what is the method? The method is very simple. As I write this in your mind somewhere you keep a box. You say ah 15 so this is the largest. Now you come to 7. Is 7 the largest? Larger than 15 no. You go to 23 ah. This is larger than what I have in mind. So I keep in mind my 23 now. Next number 4. Is it larger than 23? No. I ignore. Next number 18. Is it larger than 23? I ignore. Now whatever I have in that place in my mind is the largest number. Isn't that what all of you did while finding the largest? Now if you did that then why can't you ask the computer to do exactly the simple thing like this? And what would that simple thing be? Consider this to be max. At the end of your program max should contain the largest of the value. But during execution of the program it is your choice of whatever max contains. You see in your conventional style max will not contain anything except the largest at any point. It's not necessary. What your mind does you do on your program. So for example here is my program. Do you agree that this will solve the problem? Do you also agree that this program is both logically simple for anyone to understand and also far easier to write than the complex logic that you would have entered into if you were to draw a flow chart for finding out the largest of five numbers. Can you write it in a single page? You'll have one diamond shape box. E is A greater than B. Yes. Now B greater than C. Yes. Now C greater than D. Yes. Now D greater than E. Etc. Etc. But this is an interesting attempt by one person to find out the maximum of three numbers. But anyway I figured out that you can actually write a program for finding out the largest of three numbers. But the approach that you were taking. Now here is a question. Why were most of you taking that approach of writing nested ifs? Just think about it. Why? Why were you writing nested ifs? What prompted you to write nested ifs? Yeah. Anybody? Yeah. I am now asking you to read your own mind and tell me why? What prompted you to think like that? There lies the answer in learning good programming. You were prompted by the fact that there is a conditional execution facility in C which has something of this flowchart. And there is a condition. If it is true, you come here. If it is false, you go there. Right? This is aged in your mind. Now for finding out the maximum of two numbers, this is what you did. A greater than B. This is max. This is not. So when you are solving the second problem of finding the largest of three numbers, you were automatically thinking of extending what you thought about the two numbers. That means in your mind you had prepared a diamond shape box like this where you had already said A greater than B. Now in your mind you were either here or here. And therefore you said I am here. So what should I do now? So I should compare A with C. Am I right? And therefore you completely missed out on how you would solve a problem given a larger list of numbers. As a matter of fact, even if you have given at least 100 numbers, 100, you would still get the maximum by the time you have read the 100th number. That's it. You are not faster than the computer. But you are not being intelligent enough to tell the computer that this is a better way of doing it. Now you understood? Let me ask you one more question. I have to find out the largest of five given numbers. Okay? That was the problem, right? So five given numbers. This is how I solved it. If I have to solve the problem of finding out the largest of 100 given numbers, I can extend this program all right. But I will end up using 100 variable locations of the memory. Get one number, put it there, get another number, put it there, get third number, put it there. If my objective in life is only find out and assign the largest value to max and print it, do I need these 100 locations? I can actually keep some location n for a number. I start with reading first number. I say max is equal to n. Then I read another number, not in another location, in n itself. And check if max is greater than n. If it is, I assign max to this new n. Otherwise, I carry on. Again read the next number in the same location for n. So even if I have to find out the largest of 100 given numbers, as long as my objective is only to find out the largest and do nothing else with those 100 numbers, I need not waste 100 locations of memory. We shall see that subsequently when we discuss and study the iterative solutions in the class. However, you have understood how to solve this problem. So now solve this. Write a program to input 5 numbers and output the second largest number. I would like to modify this, Nagesh, that we should say find max to or something, which is the second largest number and output it. I want them to assign it to max. Of course, it is very difficult to solve this problem without making an assignment to a special variable like this. You cannot directly output. Otherwise, you will end up writing too many output statements. But this is the second largest that you have to find. So assume that you have in A, B, C, D, E defined already. Let me give you the declarations so that you do not waste your time in writing different things. I am also giving you a hint. A variable max and max2. So keep two locations with you in your mind now. At any point, you can keep the largest in max and the second largest so far found in max2. Let me give an example of how you could approach this. Consider the same problem. I have these numbers given to me. So according to the logic that I am suggesting, no, let me not overdo it. You solve this problem. You have five minutes to solve it. Then I will give you a possible solution, which many of you should be able to find now. I have given you enough hint that if you use an additional location, additional variable apart from in max, you can also use some max2 or some such name where you can try and maintain the second largest value found so far. But use the same logic of finding out the maximum of seven numbers or five numbers or whatever, but also find out the second largest. But do you agree that without our discovering the way we solve such problems, we would not have been able to write this program properly using nested ifs and so on? Do you all agree with that? All right. Most of you have finished writing the program. So please swap your notebooks and discuss for two minutes. I am announcing an additional work to be done. And this additional work to be done is that this is the problem statement. Now what all of you have done is you have written a logic, you have written a program and you have tried to sort of execute that program in your mind to check whether A, B, C, D, etc., etc. will be properly, relatively evaluated or not. But I am sure what many of you have not done is to assume a set of seven numbers being given as input and then executing your program exactly as C++ will do and then finding out what do you get as the second mark. So I will suggest, so this is set one, this is set two. May I have your attention please? No discussion at this juncture. I hope you have your own programs back in your own hands. Now imagine you are C++ and you are executing your program. Your program only, not your neighbor's program. Execute it with these two data sets and determine whether your program correctly prints the second largest in each of the two cases. What should it print in case of set one? Sorry? 15. 15, alright. What should it print in the case of set two? Now it's a very simple exercise. You have methodically written a program. Whether it is right or wrong, you don't need a C++ compiler to find out. There are finite steps. Execute those steps as if you are C++. So when you say A greater than B, check whether 15 is greater than 10. Whatever exactly your program is asking C++ to do, you do it yourself but with these values. And then find out whether you are getting the answer 15 for set one and the answer 12 for set two. Do that exercise now. The next two minutes. Have all of you done it? Most of you have started writing your program on the general lines which appear correct. But many people are stopping halfway through because the program is becoming too large. So whenever I go, they ask me, sir, is this approach correct? Meaning, should I waste more time in writing many more statements? Please tell me if I am right, then only I will do it, otherwise not. Unfortunately, when you solve real life problems, such facility is not available. You cannot ask someone saying, am I doing right? Then only I will do the remaining Gholagiri, otherwise they will stop. That is not the way we learn. If it takes 100 lines to be written, you write those 100 lines. And if it takes 10 more minutes to execute those 100 lines with the given input, you do that. And then you discover that your program does not work correctly. And then you again read those 100 lines. There is no shortcut to hardware. If you could find out a clever implementation early enough, you may not have to examine 100 lines, you may have to examine 20 lines. But don't expect anyone, including your friends, to tell you after writing 10 lines, whether this approach is correct, so that I will write the 10 more lines. You must be willing to write 20 lines, 50 lines, 100 lines, whatever it takes to complete that program and execute it with these values. In this case, I have given you the value. In real life, when you write programs, you will have to construct such values, sample values and execute your program to check whether it works or not. Of course, when the programs become larger, you will test these using a computer, but you will have to prepare test cases like this in any case. All right. I did not realize that we are losing time here. We have hardly 10 minutes left, and I wanted to show you at least one more practice problem. But let me deliberately leave some confusion behind. There are many people who haven't got the answer right. There are many people who have got the answer right, but through different styles. There is one person, for example, who came up with an interesting algorithm that you find the max, and then for all the remaining numbers or all the numbers, in fact, you find out that number modulo max. Since max itself will divide max, you will get a remainder 0, and all other numbers will be same as they are. So effectively, now he is saying, I will compare all these numbers to find the max, but this time it will be a second max. The same effect can be achieved more easily by assigning that value as 0 directly, and then comparing the remaining value. But then you are obligatory in the value. Also, you have found out the max, but you don't know which one of those numbers is max. That memory is not available currently. When we discuss arrays later, we will understand. In short, there are multiple ways of finding it. There is one more person who pointed out that if the input is not like this, but imagine the input is set 3, which is 1, 2, 10, 10, 5, 4, 3. All of you who had found the correct answer here determine whether your algorithm or your program works correctly to find out the second largest. Now this is where the problem of interpretation comes. What is the second largest number here? Some will say 10, some will say 5. Does your program get 5? It is not always necessary that the second largest will mean 5. It will mean the second in order, and it so happens that first and second are both same. What is the interpretation depends upon the, not the way the problem is stated by me in an exam, but by the way the problem is stated in real life. For example, if I want to find out the top two performers in a class, in a quiz, and if the marks were like this, then will your max and max 2 get me the top two performers? No, because then you should have got 10 and 10, because the top two performers are the people who have got 10 and 10 marks. Otherwise you will honor one of them, say this person, and this 5, this person. You say these are the top two performers. So you have to decide, you have to interpret the problem statement in real life first. And I had said this once, half the trick in proper programming is to translate the real life problem correctly into the programming terms and to find the answer. So depending upon what interpretation you want for the problem and depending upon what interpretation your program takes, these are different, you will be solving a different problem. So I will leave this to you. What we will do is after the afternoon lecture is over, we will post a few alternate solutions to this problem. There are several other problems, but there is not much time here, so I will just show you the problem. This is an interesting problem. So just jot down this problem, we don't have time to solve it, although I would like you to solve this problem in the next 5 minutes. It could be 583, 212, 794, you are given a 3-digit number, you have to display the sum of all its digits. Now I have made a mistake in formulating these problems. Whenever I say display the sum of all its digits, I always mean that in some variable find out the sum of all the digits and display the value of that variable. So this mistake will repair in the subsequent sessions. The correct problem statement is write a program to input a 3-digit number, find sd, let us say, which is sum of all the 3 digits of that number and display or print the output of that sum. Please remember the trick is to use the modulo operator appropriately to find a digit of a number. I will give you one hint. Suppose this is the number I want to find out 3, what do I do? I divide it by 100 and find a modulo thing. So if I have a number n and if I do this operation n modulo 100, will I not get a digit here? What will be this digit? This will be 3. What do I get? What do I do to find out? What will I get? So how do I find out the other 2 digits 4 and 8? D1. What will I get? So I have got q equal to 8. The number 4 still alludes me. n divided by, I will get q1. q1 happens to be, so it is a relatively simple problem. Except that I have arbitrarily named something as D1, something as D2, something as q, something as q1. All that you need to do to organize your thoughts properly is to define variables which actually represent some kind of a sequence of digits. So you might use things like int digit 0, digit 1, digit 2, which means digit 2 is the most significant digit, digit 1 is the middle digit, digit 0 is the last digit. You can use any nomenclature which is meaningful. Do exactly similar operations and once you find the individual digits, you can calculate their sum and print, right? Not a problem. This is a harder problem given any year as a 4 digit number, not necessarily 2014, 2018, 1950, 5217, any year, just the year is given. You have to determine that what would be the day on 31st December of that. The day can clearly be only one of the seven days, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. Now this problem is not exactly a pure numerical problem. You do not know what numerical computations you have to do to get this. You have to figure out what is the algorithm that determines the day of 31st December of any year. We do not know that. But we know one thing that whenever year is given, it is a 4 digit number and through some jugaad, I have to find out a single digit number which could be 1, 2, 3, 4, 5, 6, 7 or 0, 1, 2, 3, 4, 5, 6. It is going to be some kind of a reminder. And I have to map each of these reminders to Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday as appropriate. But I do not know what that logic is. This is an example of a real life problem where the solution is not purely computational and yet as professional programmers, you would be expected to write a program to solve this problem. The correct way to solve this problem is go out to the real world, hunt out the logic of finding out what is the day for 31st December of any year. Suggestion is go to Wikipedia or just type in Google search what is the day of 31st December for any year, some such thing. And if you find it, share that with your neighbors, share that with your friends. Locate that and then write down a program to implement that one. So this is not a course in algorithm design. The algorithm is coming from somewhere else. Somebody has figured it out. But this is a course in program design. Given the logic, how do I implement? All right? So I leave this as an exercise to be attempted at home. Thank you.