 In this session, we are only going to discuss one-dimensional arrays. The matrices we will discuss in the subsequent session. I hope all of you have seen the videos related to arrays. So this is the first question. Very straightforward. Some philosophical comments about the use of arrays. So which is more appropriate for the use of arrays? A, to deal with large number of values. B, to get some of two numbers. C, to get average of three numbers. D, none of these. Everybody written down the answer? Anybody still thinking? Here is a conventional description of an array, not C++ description. Please remember that this is deliberately put to tell you that there could be some confusion when you read a mathematical textbook and try to implement it in C++. In maths, traditionally we represent an array by just saying A is equal to set of values. In fact, something similar is borrowed for initialization of arrays in C++ as well. Except there is a fundamental difference. That the array references are usually first element, second element, third element. A1, A2, A3, A4. So just as a recap, it is indicated that A1 is 5, so you know exactly what weight reference means. What is the sum of A2 plus A4 plus A6? Straight forward question just to pay attention to the indexes that are there. Everybody solved it? Simple problem. The following properties are true for arrays. There could be more than one. A, to store a large number of similar values. B, collection of adjacent memory locations. C, an index could be used to refer to an individual element. D, all of these. Choose an appropriate answer for arrays in C++. First statement says we must declare an array's name and size. And two, the size could be float. The choice A says only one is true. Choice B says only two is true. Choice C says both are true. Choice D says both are false. Again, those who have watched the video would know the answer. It's a straight forward answer. Everybody done? Anybody still thinking? Raise your hand. Ah, you have not watched the video? You have not watched the video? If you have not watched the video, you can't answer simple questions. You have to watch the video before coming to the classroom. Now you have to guess. Aitins are very good at that. So apply your mind and guess. We go to the next question. Now this is a C++ array declaration. An array A of size 5 is declared with these initial values. What will be the value corresponding to A in square brackets? 2 star 10 minus 3 star 6. Note the question. What is the value corresponding to A indexed by such an expression? Choices are A is 2, B is 3, C is 6, D none other. This requires a very small paper calculation and application of mind to locate what is being indicated. All done? Here is a code segment lifted straight from a lecture. It says that you have to assume marks is an array of type int which has been defined earlier. Not only define, let's say n values have been read into it. So the value of n and n elements of the marks array have been already taken in as input. This code segment starts after that. Read the code segment and comment on what does A represent. The array is marks array. A is declared as an int. A represents choices are A, standard deviation, B, average, C, sum, D, none other. Again a straight forward question. Everybody has answered. Anybody still thinking? Our friend there. I think this at least you should be able to guess if you read the left hand side properly. You are able to? Great. See following are practical aspects of programming with related to arrays of course. So A, check if the value given by user is out of bound. B, display error message if value is out of bound. C, write explanatory commands. D, none of these. I am surprised that my friend is not asking questions because you should have lots of confusion now. Isn't it? What exactly we are talking about? I use this opportunity to tell you that not only this question but there will be several questions which could be worded in such a fashion that the precise meaning of alternative terms and sentences may not be very clear. It is deliberately being done here to test how you think generically. Important that you be able to think generically. More important, anyone who thinks that the choice of sentences, phrases in any text, forget the quizzes, could have been better and could make the whole thing more meaningful, should take the initiative, write it down and send it on a discussion session or by any means. Everybody has answered this? We will post brief answers and explanations later on the course homepage. So what was your answer for this? Have all papers still been corrected? Otherwise our friend there who was guessing will correct all the answers and then submit. Please submit all the slips. So let's go back to this quiz. Suppose everybody agrees that the answer is A. Alright, I modify this question and I say I want to find the average of 3000 numbers A and C. So if the number is 3000 and you have to find the average of 3000 numbers, you need an array? Okay, suppose this was 2000, would you need an array? To find the sum, we have discussed this earlier. We can find the sum of 2000, 20,000 numbers. We need only one location for reading a new number. We read a number, accumulate it into sum. Then we read another number in the same location the previous number has been accounted for. We don't need to maintain it. So if you just have to find the sum of 20,000 numbers, you don't need an array. You need an array if you wish to do something else later in your program with the same values which you have read. That is when you require an array. You don't require an array to find the sum of two numbers or 2000 numbers or 20,000. Similarly, you don't require an array to find the average of 20,000. You just need a count in addition to the sum which you have calculated. Would you need an array if you have to find the standard deviation? Well, yes and no. You can do some clever programming and accumulate something else, not one but multiple things, by which you can actually find the standard deviation without storing all 20,000. However, that is not done. Generally when you have to do one, two, three, four different things with all values which you read, then you need to keep them in an array. This is very simple. What is the answer? So it is 9 plus 34 plus 43. Is that correct? All of these. Only one is true. Why should size not be float? Because you are declaring the size in integer numbers. But why can't C plus plus come? Suppose I write in a, in bracket 5.7. Why can't it take it as 5 or 6? If I had defined the language to permit this, it would have been possible. But the language definition does not permit it. So it is not a question of what you and I feel about it. It is how C plus plus behaves. So please remember, C plus plus behavior is explicitly and precisely stated and it says that the size must be declared as an integer value. Nothing else permitted. Can I say in my program, int n is equal to 25 and then int array say n. Can I say that? Yes, no. Technically it is incorrect. When you say array n, the value of n will be known to the program only when it is executing. The initialization of the variable n is done, which is an instruction to the compiler. But as per the language specification, the value that you write describing the size has to be a constant, an integer constant. Try this out when you get a chance. Declare a constant int and then try to use it. So what is this? This is evaluated as 10 into 220 minus 6 into 318. So the index reference becomes 2. This is a0, this is a1 and this is a2. So if the index is 2, a2 refers to the value 6. No? What is the answer? Now our friend here says it is not C. So what is the logic? Yeah. You think it is not C, right? Yeah. No, no, but what was your answer? It is important for all of us to know and find. So his answer was 5 and that is because he thinks this is the element which is indicated. Many people make that mistake. The reference to these elements in C++ is as follows. This is a of 0, this is a of 1, this is a of 2, this is a of 3 and this is a of 4. Although the array has 5 elements, in C++ they are referred to by index values 0, 1, 2, 3, 4. A5 in fact is out of bounds. So that is the importance of putting that earlier quiz which talks about the mathematical array. That is how you would write. But in C++ this is the interpretive. In fact, many a times in solving actual problems we do make this mistake. We sort of in our mind assume that the array reference is from 1 to n. It is not true. It is 0 to n minus 1. Here what is the value it is found out? A is average. Because clearly this sum is dividing by n. My friend here has a doubt, a very correct doubt. I have defined int A. A is being assigned the value sum by n. His observation is if sum may be integer, n may be integer, but the division of sum by n need not be integer. As you know in fact the average of marks need not be an integer value. This is where you start making assumptions. So if you were to write an analysis of this program, you will say that in your opinion the author of this program seems to have made the following assumptions. Number one, the array marks is of type n. Number two, i and n appear to have been assumed to have been declared as int. This is the assumption. And number three, the assumption appears to be that whatever be the input value of marks, the sum appears to be such a nice value that it is exactly divisible by n. Then and only then this program calculates average. Otherwise it will calculate a close approximation to the average. Now this all has to be written in English as commentary on this question or such thing. It is important to understand this. When you write your programs, what you are writing is probably very clear to you and hopefully to the C++ compiler. But it is often not very clear to others. And that is the reason why we have comments. That is the reason why comments have to be written properly in every program. What is the answer to this question? A, B, C, D, E, F. What is going on? Shouldn't you do each one of these things? Is there anything which is stated here which is less important than the others? Now suppose you have to choose only one. Which one will you choose? Okay. This answer reflects the standard dilemma that we all face. Three important things have to be done. Ordinarily we believe all three must be done. But in real life, suppose I have to prioritize and somebody says choose one. This is called force ranking. There are three equally important things in front of you. But you are forced to rank these three to say this is first priority, this is second priority, this is third priority. Please understand that such questions cannot have unique answers. They do not have unique answers. Somebody will say this is important, somebody will say this is important, somebody will say this is important. From that individual's perspective, that answer is correct. This is just additional inputs. Please understand programming is a very precise art. But real life is not very precise. And translating your issues in your mind for a problem into a very precise program is not a very easy thing. That is where you need what is known as problem analysis or program design. Anyway, let us quickly look at some of these things. Everybody knows that if an array is declared, array is allocated consecutive locations in the memory. This fact is very, very important. If you declare variables, even if you declare them one after another, like in i, j, k, there is no guarantee that i, j and k will occupy consecutive locations. There is no guarantee at all. Compiler does not require that. However, if you have declared an array, it is guaranteed that all elements of the array will be allocated consecutive memory locations. Later when we study pointers, this fact is exploited. In fact, it is this fact which permits compiler to implement array references very easily using what you call index expression. So what it does is it just goes through this, keeps in memory this base address. And whenever you give an index, that index multiplied by 4 because each coefficient is 4 bytes, it can find out where the next address is. That is how arrays are implemented. This program will know. I would just like to emphasize one point. There are umpteen different ways in which you could write iterative solutions to scan all elements of an array. Typically, whenever you have to scan all elements, there are n elements, let us say. Then we remember that the first element of the array is referred to by index 0. It's called 0th element. This is the answer to your question or your doubt. The last element is referred to by n minus 1. And that is why it is suggested that anytime I have to scan all elements of an array, then it is best to use the simplest structure for count equal to 0, count less than n, count plus plus, or count equal to count less. So don't have any bells and whistles inside your false statement if the main objective is to scan the array. If you have any additional computation as a good programming practice, it is best that it is done outside. Unless your computations are such that they must be included in the body of the false statement. In the class, you have seen a variety of ways in which you can write false statements. All those ways are valid. There are some very interesting and clever ways, but try to keep your program simple when it comes to scanning array elements. That's the purpose of showing this problem. Now this is the first problem that we'll solve today. The sieve of Eratosthenes. How many of you have seen this concept? One, two, three, many. All right. For those of you who are not aware of this, this is a famous algorithm to find all the prime numbers in the range 2 to n. Prime numbers are numbers which are divisible only by one and themselves. Now this fact is used in this sieve to solve the problem. You start with a list of consecutive integers. Imagine in your mind you have integers 2, 3, 4, 5, 6, 7, 8, 9, 10, 100,000, whatever. You have got a set of numbers. You put them into this sieve. There are some numbers which will drop out. What remains should be prime numbers. Assume that all these elements are unmarked. That means they are inside the sieve. They are not dropped down. If you mark an element, that element goes away. That's the assumption. So you don't consider that element anymore. Why are we not including one? Because everything is divided by one. So there is no point of considering one as a factor. Start with 2. The process is like this. Mark the lowest unmarked element. Say p. To begin with p will be 2. Because 2 will be the lowest element which is unmarked. Okay. Now look at all numbers greater than 2. Up to whatever range you have. Say 100, 200, 5000 more. And each number you divide by 2. To find out if there is any remainder other than 0. You can use a simple model operation. So if you are looking for p, you are not looking at p, but you are looking at all other numbers which are divisible by p. And if any number is divisible by p, you mark that fellow as useless. Okay. The final process is about marking numbers which are a factor of some previously. If there are no numbers which are a factor of p, they remain. All others get marked. Repeat these steps 2 and 3 unless all elements are marked. So you have considered p now. Now you consider the next unmarked number. The next unmarked number is obviously a prime because it was not divisible by p. And p is the lowest number anyway. So if you have 2, the next number will be 3. Now you are looking at 3. Actually you are looking at all subsequent numbers which are not yet marked. And if amongst them you have any divisible. So all elements which are left in the list. So it is as if you have a sieve, chhani, and you have all these numbers. And whenever you mark a number, it sort of falls down. So whatever remains are your prime numbers. Very simple and efficient algorithm. Here is an example. Suppose n is equal to 14. And therefore you want to find out all prime numbers between 2 and 14, both inclusive. So this is the sort of original input list. 2, 3, 4, 5, 6, 7, 8, 9, 10, up to 14. The output list produced by the program should be 2, 3, 5, 7, 11, 13. Because these are the prime numbers. So let's do the analysis of the problem and some program design. Is it necessary to have an array of all natural numbers which we want to examine whether they are primes or not? What is the meaning of marking and unmarking? Here is this algorithm. You mark 2 and remove 4, 6, 8, 10, 12, 14. Now the list says 2, 3, 5, 7, 9, 11, 13. The others are marked. The next unmarked thing is 3. So now you mark 3 and remove 9 because 9 is the only number which has 3 as effect. Now the list says this. We examine 5. None of them is divisible 5. 7, none of them is divisible by 7. 11, none of them is divisible by 11. So this remains. And at the end, there shall be the numbers which are unmarked. All others should be marked. How do we implement this algorithm? So here is the suggestion. I can solve this problem in two different ways, by the way. I call this the removed array. The removed array is also of size 100. So it has elements 0, 1, 2, 3, 4, up to 9. In this array, I wish to keep a 0 if the corresponding number is removed. I keep the number 1 if the corresponding number is not removed. Where is the corresponding number? I am not keeping that number anywhere in an array because that number will, I know, is either 2, 3, 4, 5, 6, 7, 8, 9, 10. In fact, I would like to set up an outer iteration which examines every number. And it is that number with respect to which you have to take a call. So that number is here. I will put all of these as zeros. I know that I need not start with 0 or 1 to examine the natural numbers. I have to examine natural numbers so I might have an iteration as indicated here. First, I am writing a code to mark all elements with 0. Code snippet 1 will contain what? If I have this array int removed 100, can I simply write, is this adequate? If in my declaration itself I put 0 as the initial value, come on, you have seen the slides. This is at least one place where I have indicated how you can actually initialize the array. Let me give another example. Look at this declaration. In array 4, 7 minus 5, 12, 16, is it a correct declaration? What will it do? It will declare an array called a-r-r. Its size is 4. Since there are 4 values given, these 4 will be assigned to 0th, 1st, 2nd and 3rd element respective. This is the initialization for the whole array where you give a set of values. Now imagine this. I do only this. I declare an array of size 4, but I give only 2 values. First of all, is it valid or invalid? It is valid. Second, what will it do? It will assign only the first 2 values to the first 2 elements of the array. So array 0 will be 7, array 1 will be minus 5. The remaining elements, the C++ language does not say anything about it. There are many people who make an assumption that all remaining elements are set to 0. There may be some implementations which may do it, but C++ language definition does not require it. Consequently, this will mean what? An array of 100 elements is declared, a-0 is set to 0. What is a-1, a-2, a-3, a-4, a-100? God and that particular implementation of the compiler, these 2 people only know. You and I don't know. So you cannot make any assumptions. That is why you are required to write a code here, code snippet 1. Let us write down this code snippet 1. The declaration is here. Int, removed, 100. There are simple code segments who will not waste too much time, but I want everybody to write it on a piece of paper. It's exactly 2 or 3 lines of code. Please state this as a question. So no discussion at this juncture. Each one of you has to write the code snippet 1. That means in this place, the int removed array that you have has to be initialized to 0. It's an absolutely trivial operation. I don't know why people are taking it. All are done. Anybody who has not written these 2 lines of code could tears go around and check it because I found yesterday that most people there enjoying the discussions but not writing anything. Every one of you should write 2, 3, 5, 10 lines of code that is required to initialize an array to value 0. All done? Anybody who has not been able to get it, please raise your hands. Ah, our friend. So you have a fundamental problem with the notion of an array itself. Right? The problem is very simple. There is an array declared with just 100 elements. You have to set all elements to 0. So there is nothing philosophical about it. I am surprised that people have doubts even about this simple operation. I don't think you have watched the... All right, let me ask one question. How many of you followed my advice that you shall not watch a video without a notebook and a pen or pencil in your hand? You did that or you did not do it? You did it. Very few people. Now this is not on. This serious business, just like in a classroom you take notes. You have to do that. Here is a simple code. For int i equal to 0, i less than 100, i plus plus. Please remember what I said some time ago. If you have to deal with an array and scan all the elements of an array, then the best bet is to write a simple for loop and the only statement inside here is set height element to 0. Is this not clear to everyone? All right. So now let us go to the next part. Now this is where I would like people to take five minutes doing this work individually. Five minutes given to you to do this work individually. Check if i is not removed. Then remove all multiples of i except i. Let me just explain this question a bit more. Please look at this carefully. Those of you who are still struggling, what exactly we are trying to do. Please remember that, unlike in the original problem description, we do not have a set of numbers stored anywhere. So on the sieve the numbers are only hypothetical. The way we are implementing it is, as far as we are concerned, the sieve is represented by the index of this array. Inside this array I have all zeroes. That means none of them are removed. If any one of these numbers becomes minus one, then that number is treated to be removed. Now note that if this is the outer iteration that has been written, what I need to find out is, for each i, look at all subsequent numbers. So i is equal to two, for example. This is what you are looking at. You have to leave two alone. Two will not be removed because it's a prime number you are starting with. Now you have to look at all subsequent numbers. How do you look at subsequent numbers? Because the array, you have to set up another iteration inside and you look at all numbers more than two. So you look at three, four, five, six, seven, eight, nine, ten. What you have to examine? Any number that you look at, whether it is divisible by two or not, is three divisible by two? No. Is four divisible by two? Yes. Then you have to go to the removed array and the fourth element you have to replace by minus one. You look at integer five. Is it divisible? No. Six. Is it divisible? Yes. So look at the sixth element and change this zero to minus one. So what you are doing is you are maintaining a kind of flag inside this array. If there is a minus one, then the number corresponding to the index is removed. This way you will remove all the index numbers and then when you come back to the outer iteration, I will become three and then you will start looking at three and then likewise we will examine all numbers larger than three. If they are divisible by three, you convert the corresponding value in the removed array to minus one. This is the logic. All right. So how many of you have done this already? One, two, three. Oh, large number. Can one of my TAs get one of the answer books please? Others are still doing it, so please complete it. No, no, keep the answer books with you. I would like them to be exchanged with others. All right. Complete this problem in five minutes. All right. I request a proper discussion to happen now. Please swap your notebooks with your neighbors and check each other's answers. If two neighbors haven't got the answer, please discuss it with the third neighbor or with your TA. Was it one or minus one? What was it supposed to be? I forgot. Anyways, non-hero. So this works. You sure it will work? When you come out, you would have all them marked. Okay, it's very similar to this, but that's okay. I think this will work. Let me see if any other... Any solution that you have... You got it? Sir, there is a follow-up here. I to Christian Cutler. And I to Christian Cutler. Then... I to Christian Cutler. Why are you doing this? Why are you doing this again? Why are you doing this again? Sir, there is a follow-up here. No, no, sir. Look, there are two different snippets. One is zero in the first one. The loop should end here. It's over. Now you will have to write this loop again. For I... Yes, don't confuse these two things. Don't do that. First, you have to define it. Then initialize it in zero. Then do it later. So take this out from here. Yes. J equal to 2. You have to see its next number. If I get 5, then what will you see from J? What will you see from 5? I or I plus 1? I or I plus 1? If I get I, then I is divisible by J. Why? Why not start? Yeah, good. Rewrite that problem. What are you trying to do? Sir, I am just... ... ... ... ... ... But here both numbers are increasing by one by one. You are not implementing that algorithm. Imagine I is equal to 2. Okay. Now your hypothetical numbers are... ... ... ... ... ... So this is not correct. The loop and you are changing this. That loop is stated. For int I equal to 2, I less than 100, I plus plus is fixed. You can't change it. Now you write whatever else you want to write. Now you write whatever else you want to write. So internally there should be a loop from J equal to I plus 1 to 100. No. What it? No, no, no. You have to do exactly what it says. Not your own algorithm. Then I am... ... ... But that loop it has been stated. For int I equal to 2, I less than 100, I plus plus. This is written. Now inside that you write whatever you want to do. This is done. This is done already. This is not connected with the second part. This part is code snippet 1. You are writing code snippet 2. After this I am doing that for int J3. Then I am saying J divided by... I, correct. Then if this is 0, then remove I. Not I, J. You are examining J. And then the idea is correct. But this is done. No. It's correct. I am looking for some... ...better solution. I have got so many correct solutions. I want the discussion to stop now. I know that some of you were in the midst of... ...getting close to understanding the problem. But I think I have understood the way the discussion is going on. It's going in the right direction. But I would like to remind you of two things. First of all... ...many people seem to be confused because... ...they presumed that there would be an array containing all the numbers... ...which they have to examine. So for example, most of you are... ...trying to see... ...where do I have 2, 3, 4, 5, etc. ...as value. They think this should be the array of numbers. And it is in this array that they are examining an individual number. Many of you expected that, right? You have an array of numbers 2, 3, 4, 5, 6, 7, 8, 100... ...in which you are examining the number. This is not the way this particular solution proceeds. There is no list of numbers. There is no list of numbers in this problem. While this way would be certainly one of the ways to solve the problem... ...and as I keep saying again and again... ...there is no unique way to solve a problem in general. There could be hundreds of different ways... ...hundreds of different algorithms. The same algorithm implemented in five different ways. The particular question that has been asked... ...expects you to solve this problem by implementing the algorithm... ...in a particular way. In that particular way, there is no such array. The numbers that you have to examine are all hypothetical... ...in your mind. They are not stored anywhere. So when you write the four iterations... ...when you write an iteration... ...this iteration as of now... ...has nothing to do with the array removed or any array or any such thing. You are examining the value i. And how are you examining the value i? You are examining whether the numbers subsequent to i... ...are divisible by i or not. That's all you are examining. So forget the array for the time being. There is a number i which is initially two. Subsequently it will become three, then it will become four, etc. You are examining whether the number two... ...divides any other number subsequent to it. Now if you have to examine all numbers subsequent to i... ...and to find out whether they are divided by two or not... ...at the minimum, you need an iteration... ...which will examine all numbers... ...starting with i plus one at least... ...because you are examining numbers subsequent to i. If i is two, you are examining three, four, five. If i is four, you are examining five, six, seven. So to examine all numbers in sequence... ...you have to set up another iteration. Which let's say says for j equal to i plus one... ...j less than hundred, j plus one. Now when you come here... ...at this juncture you have two numbers. One, the i which is the main number which you are examining. And second, one of the subsequent numbers j... ...if that j is divisible by i... ...then you have to remove j. If the j is not divisible by i, you are not to remove. How do you find out whether the j is divisible by i or not? Good. If j is divisible by i, j modulo i will be zero. In which case j must be removed from my list. Now that removal list is an array. The removal list is an array. j is not stored physically inside that array. But the jth element of that array has been mark zero... ...which means it was not removed. If j has to be removed, technically I have to tick inside that array... ...put it as minus one, something. So whatever you have to say, that's it. Nothing else is required. The inner loop will examine all numbers greater than i. And if any number is divisible by i... ...it will set that number as element in the removed array to minus one. When I complete this iteration, I will go to the next i here. There are variations possible. This is one solution given by Lavish which does exactly the same thing. He examines if removed i is not equal to minus one. That means if a number is not removed earlier... ...this is not required because you are examining these numbers. This is required because if a number is already a factor of something... ...which has been determined, I need not look at that i again at all. In fact that would have been removed. For j equal to i plus one to less than hundred, j plus plus... ...Lavish examines every j whether it is divisible by i. That means he simply puts removed j equal to minus. When he goes back, looks at the next integer. The next integer number will be three. The third element would still not be removed in that array. So three is a valid thing. But when he goes to four, he need not examine anything beyond four... ...because all of them would have been marked anyway. You will be doing double work. So this check is useful. Here is another more fruitful or slightly more efficient algorithm. The outer iteration is same as all of us have done. i equal to two i less than hundred i plus plus. If removed i equals zero. That means the number is not yet removed. Then you need to examine all subsequent numbers. But look at which subsequent numbers the person is examining. So Raunach is setting j equal to i. So examine that number itself. Is he examining that number? No. Just wait to see what he is examining. He is setting up a variable j. He starts with j equal to i. He lets j increment till... ...there is a problem here. I think you meant j less than hundred by i, right? Here is Raunach. Yeah, goof up. The c plus plus compiler is not as pardonable as human beings are. Notice very clearly what he is examining. He is simply removing all i star j. So will this algorithm work? What Raunach says is for every j he show examines, i star j is set to minus one. He is terminating at hundred by i because beyond that j into i will be beyond hundred. Notice what he is trying to do. Let me try to explain it in a slightly simplistic way. I don't know how many of you did that, but when we were young we had to mug up multiplication tables and recite them. Three ones are three, three twos are six, three threes are nine, three fours are twelve. Have any one of you done that? Most of you have done that. What he is doing is he is making c plus plus compiler recite the multiplication table of i. Suppose the number is five. So five fives are twenty-five, five six are thirty, five sevens are thirty-five, five eights are forty. He is reciting the multiplication table of five and anything which multiplies, which means that number has a factor. So therefore that number is being set as minus one. This is slightly more efficient in the earlier algorithm where you are examining every number anyway. But the numbers which are only multiples of i need to be removed. So he is doing that. There is only one problem here. You can make it slightly more efficient because there are some numbers. For example for five and j equal to six, five into six is thirty, thirty would be removed. But thirty would have been removed when he would have examined two. Thirty would have been removed when he would have examined three. So he is hammering it down by removing a number which has already been removed. Of course the only easier way to do that is if that number is not removed. So examine whether this is zero and then only reset it. So infinitely the cost of comparison and cost of assignment are not much different. So there is no harm in doing. There could be many variations, yes. Any observations? How will we include the extra condition that if it's already assigned not to check the value? That's what I said. It's not easy to do that. You can include the extra condition by saying if removed i star j is equal equal zero, then only you do that. Otherwise in some earlier iteration it would have been done. However to doing an extra check on every element it is better to reset that element again to minus. That is why there is not much cost. Alright, that understood? So one suggestion is the condition you can put in the for loop itself. j less than 100 by i and removed i star j equal equal zero. There is a discussion going on on whether this algorithm can be made more efficient and we concluded that while we can examine the value being assigned it is already minus one, we need not do that. The point I was making is the cost of examining whether that value is minus one and putting a minus one in it are exactly similar and it does not matter whether you examine it inside the loop or as a part of the loop condition because that will also be executed or comparison will be forced every time any which. There is a question to write the code to output all the primes. Now please note here again the primes are not stored anywhere in an array which is the conception many of you had. So this output is not a straightforward operation of printing some elements of an array. You have to actually examine the removed array and whatever is not removed the index value has to be printed which is the prime number. I need to make an important announcement and at least introduce the next problem. So we will not discuss this problem now but I want you to think about this problem later. This problem introduces a new structure called a queue. All of you are familiar with queues like a queue for buses, queue for train tickets, etc., etc. Imagine the queue is simulated by a C++ program in which an array is used to simulate the queue. The beginning of the array is head of queue, the end of the array is tail of queue. To simulate a real-life queue, suppose I go to a bus stand, I will stand at the end of the queue. So there is an operation defined called N queue which inserts an element at the end of the queue, end of the array. There is a corresponding operation called D queue. D queue will remove an element from the array, a person gets into the bus, that person physically goes out. So D queue operation is a costly operation because the 0th element has to be removed when you do D queue and all other elements have to be pushed up because the array must start always with the 0th element. The N queue operation is simple. Somehow you have to remember that what is the last element which was filled up last time? That plus 1 will be the next position and the next element which is queued has to go there. So as I said D queue is a deslection operation. The ends of the queue are called front and rear ends and insertion happens at the rear and deletion happens at the front end. Here is an example. So initially queue is empty. Assume that N queue and D queue are functions written by you. N queue takes an integer parameter. This is the element which has to be inserted in the queue. So when I say N queue 4, 4 gets into the array, into the 0th element of the array because the queue was empty so far. Next element is N queue 5. So 5 gets inserted next. Then come 6, 6 gets inserted. Now imagine that the next operation is D queue. D queue does not require any parameter. D queue means whatever is the head of the element, take that fellow out and put him into the bus. So throw him out. So delete that element. So when you delete the first element, 4, 5 and 6 have to be pushed up. Now that is a cost. You have to push up everything. At the end of the queue, this will be the status. Again N queue 8 and 9 will see 8 and 9 coming here at the end and D queue again will make 6, 8 and 9. This exactly behaves like a bus queue. Some people keep going out into the bus. Some people keep coming in. At any point in time the queue will increase in length, reduce in length. There will be always some queue. All the original inhabitants of queue might have long ago gone. That is how a queue works. The question is consider the queue 4, 6, 7, 8, 9, 10. Perform the following operation in the queue and check your answers with your TA. Now this is, you just noted down. We will display it on the website. You can use that. Question 2 is what is the minimum number of operations needed to remove 12 out of the queue? 12 was something which was inserted at the very end. You can look at it more carefully. This requires examination. So here eventually I would expect you to write two functions N queue and D queue. But assuming that such functions have been written, what would be the state of the queue is what you have to comment upon. Thank you.