 Welcome back, in the previous segment, we discussed how character strings are passed to functions, the notion of constant character strings and we also did example of lexicographic order, a function to compare strings by lexicographic order. In this segment, we are going to discuss two dimensional arrays. Now two dimensional arrays are quite nice because you can store matrices or tables using two dimensional arrays and also other things which we will see in a minute. So this is how you might declare a two dimensional array. So this is a two dimensional array of doubles, what does it do? Well it creates m times n variables and these variables are accessed or you can think they have names x, y, z square bracket i bracket complete, square bracket j bracket complete where i must be between 0 and m and j must be between 0 and n, the upper bounds not inclusive. So what does this do? So it creates m times n variables and you can think of this, think of these as organized in a table with rows and columns. So the i-throw of x, y, z is the one in which the first index is i, the other index can be anything at all. So it can take all possible values 0 through n minus 1. So this second index can take all these values. So that is the i-throw and the j-th column of x, y, z is what you get if you hold the second index fixed to j and allow all the first index to take arbitrary values. So in fact it is natural to think of these elements as arranged in two dimensions in a table so to say. So this is the element 0, 1, then this you can think of the element, sorry this is the element 0, 0, you can think of the element 0, 1 as sitting over here, then 0, 2 as sitting over here, all the way till 0, 0, n minus 1 and in this direction the first index is going to change. So this is going to be index element 1, 0, this is going to be element 2, 0 and this is going to be element m minus 1, 0 and in the last column similarly you are going to get the element m minus 1 and n minus 1. So this will be the second row and this will be the second column, so that is what it is. So on a computer you get m times n variables and for your visualization purpose we are thinking of these being organized as rows and columns, but the computer does not really think about them as being rows and columns. Well you may ask well how does the computer store them? So the computer stores these variables in what is called a row major order. So let me write that down. So the computer will store these variables. So row 0 first, so say 0, 0, then 0, 1, all the way till 0, n minus 1, then it will store the first row. So this is 1, 0, then 1, 1 and so on. So this is increasing address, address in memory. So it allocates a block of memory, at the very beginning of the block 0, 0 is used, is stored. Then the next 0, 1 is stored. So all the elements of row 1 are stored, then all the elements of row 0 are stored, then all the elements of row 1 are stored, then all the elements of row 2 are stored and so on. So you do not really need to know, but this is how it is stored. For programming purposes it is not quite necessary, well we will see in a way it is useful to know this. Now then you define two dimensional arrays, you can simultaneously initialize them as well. So for example you can put, here you have an array PQR of two rows and three columns. So you give the initialization for the 0th row and then the first row and that is put itself put in braces and inside that you should supply three values because the second dimension is 3 and second, so the number of columns is 3, so 3 and 3 are given over here. I could have put them in the same, I could have put the whole thing one after another also, but I just wanted to show you that this is the, this is one row and this is the second row. And yeah, so the values are picked up from the initialization list, also in row measure order, the order in which the data is stored on the computer as well. Now two dimensional arrays that I am describing right now are a little bit, are not really all that useful. So we will see why in a minute and later on in the course we will talk about nicer two dimensional arrays, but anyway many of these concepts like having two indices or yeah or even the fact that a two dimensional array really can be thought of as a one dimensional array followed by another one dimensional array are actually important and so this is something that we get from the language C and we might as well learn it because these ideas are important for the nicer ideas that will come later anyway. So some examples, so this says create a 10 by 10 matrix and initialize it to the identity. So value 1 in those elements whose indices are the same, so the diagonal elements and 0 elsewhere. So first of all the matrix will have to be declared in this manner. So 10 rows and 10 columns and then for i going from 0 to 10 and say this is the row index and this is the column index and if i and j happen to be the same then you are going to set aij to 1, if they happen to be different you will set aij to 0, so that is exactly what you want when you want to create an identity matrix. Here is another example, this time you want to create an array m to store the marks of 10 students in phi tests and then you are asked to read the marks and store them into m and you can ask that the marks be given in a convenient manner for you. So we want marks of 10 students in phi tests, so 50 marks in all and 10 students phi tests, so we should have a row in which there are 5 marks, so a row for every student so 10 rows, so 10 rows each row consisting of 5 columns, so that is what we have here. Then for every row or for every student we are going to print them as say give me the marks of student i and then we are simply going to read the marks, so there are 5 marks to be read, so we are going to have another loop and this time we are reading the jth mark of student i and so whatever comes in whatever is typed in will be placed in m ij, that is it. One more example, so here we have an array of characters, so character arrays are again slightly different from other arrays, even 2 dimensional arrays are slightly different. So these are different in the sense that of course they have 3 rows and 20 columns and if you had not initialized then that would be the same as before but the initialization is slightly different. So here you are initializing it so that you can supply all the data but again just as we initialized 1 dimensional character arrays the initialization is slightly different. So here you are going to find as many character string constants as there are number of rows and each string constant is going to be initializing the corresponding row. So this will create an array countries with 3 rows of 20 characters and an interesting idea is that a 2 dimensional array is a collection of 1 dimensional arrays. So notice if you remember when I said that the 2 dimensional arrays are stored in the row major order, so they are stored one row at a time, one row following another row and so each of those rows can be thought of as an independent array and that is sort of exploited in these char arrays. So countries 3 is used to denote the third row and so this will go into the 0th row, this will go into the first row, this will go into the third row. So countries i consists of countries i0 to i19 or it is the ith row. So row i is initialized using the ith character string in this right hand side. So what happens? So row 0 gets India followed by the null character, row 1 gets Nepal followed by the null character and row 2 gets China followed by the null character. And inside each row say the 0th character over here will go to the 0th column, this will go to the first column and so on and we can access individual characters as well. So if I write countries of ij then it will give me the jth character of the ith row or the jth character of the ith character string which is being stored. So 1, 1 for example will mean so the first word, so this is first, this is 0, this is first and the first character. So this is the 0th character, this is the first character, so it will mean this E. So if I print, I can print countries of 2, so although countries has 2 indices, I can just use one of them and that will be in just that row, that entire row. So the second row, this is the 0th row, first row, second row, so the second row stores the character string China and therefore if I say see out countries 2 that character string will get printed followed by the new line. Now two dimensional arrays are useful also because they allow us to define polygons in simple CPP. So here is the way polygons are expected to be defined, so you write the keyword polygon, then you write the name that you want to give to the polygon and then you give two coordinates which are the rotation center of that polygon and then you give the vertices of the polygon and this is going to be a two dimensional array and you will have to tell how many vertices there are. So for that you specify n. So the vertices is an n by 2 array but there is something interesting or something unusual about arrays in C++ which is that you really cannot specify or it is not customary to specify the second dimension. You can but usually these two dimensional arrays are useful only when the second dimension is sort of a fixed number. So here the second dimension is a fixed number, you know that in on the plane you always have to give two coordinates. So this is going to have n rows and each row is going to have two columns, one column which gives the x coordinate and the other column which is going to give the y coordinate. So here the how many columns are there does not need to be given because we sort of know that. So whenever we define this polygon you are expected to give an array of this kind. So what is not known in this is n and that is what you have to specify over here. So here is an example. So this is going to draw what is a house shape. So maybe we should draw a picture of this. So what does this look like? So this is let us say this is CX, CY which is the origin about which these coordinates have to be given. So minus 50 0 is going to be, so x coordinate is minus 50 with respect to this. So it will be say somewhere over here and 0 because it is at the same level, the y coordinate is at the same level. Then the next point is 50 0, so it is at the same level over here but 50 on this side. So this is 0.0, this is 0.1. Then the next point is 50 minus 100, so 50 is this and minus 100, well when we write coordinates we have to remember that the y axis is actually going in this direction. So if this is level 0 then this is minus 100. So this is the second coordinate. So this is vertex 2. Then after that we have 0 minus 50, so it is somewhere over here and minus 50 somewhere over here, so this is the next coordinate and minus 50 100, this is the next coordinate. So the polygon that will get drawn is this. So well as you can see it resembles sort of a very, very schematic house. So this command polygon house 250, 250 says that I want this house to come up at screen coordinates 250, 250. So with respect to these screen coordinates, these coordinates are going to get measured. So this coordinate really is going to be 200, 250 if you look at the screen coordinates. Now polygons can be translated, rotated, scaled, given color and things like that exactly as in chapter 5 of the book. So let us see this, let us do a quick demo of this. So this is our array what we just had there and this is the creator polygon and notice that I have put in it canvas at the beginning because otherwise this would be an error. So you need to have in it canvas called because that is where the polygon is going to be drawn. So let us do something interesting after that just for fun. So what we are going to do is we are going to rotate this so that we show you where the rotation center is. So maybe I will wait for one second so that you get the chance to see the original position and then I am going to rotate. So repeat say maybe say 36 times and what I will do is I will rotate sorry I will say house dot rotate by 10 degrees and we will wait say for say 0.1 second so that it does not all happen too fast and that is it. So let us compile this and see what happens. So see it is rotating so I think I am turning it a bit too much but anyway you see how it is rotating. So maybe let us just try to change this a little bit. So how is that house dot rotate 10 degrees, repeat 36 times, wait so it may be oh I know what happened. So it should really be saying say house dot left or house dot right. Rotate takes the angle in radians so that is why it rotated too fast by too much. See this it is rotating about the center and it will come back. Well houses are not supposed to rotate but the point of this was just to show you that the center the rotation center was here. This is the point with respect to which we specified the coordinates and that is the point which we can use to rotate as well. So you can do fun things with this kind of rotation if you want. So as an exercise in two dimensional arrays I would like you to write a program which multiplies two matrices say A and B and computes their product in a matrix C and for this you may remember the formula for matrix multiplication. So Cij is equal to Aik times Bkj but you have to sum this over all the values of k. So what we are going to do look at next is how do you pass two dimensional arrays to functions. So this is of course something that is desirable to have because of course you might want to have functions say a function which multiplies an m by n matrix by an n by p matrix. So for any m and p and the natural idea would be to pass the starting address of the array the number of rows the number of columns. Unfortunately C++ does not quite do this right. So it requires you to specify the number of columns as a fixed number at the time of writing the function. So there is a technical reason for this and I am not going to explain that to you because it is kind of a silly reason. And well it is kind of a complicated, there is a complicated reason for it. But the upshot is that the second dimension or the number of columns has to be given in a fixed manner. So I will give you an example of this just to make it clear. This is definitely a C++ language limitation. But the more modern thing that we will talk about later on will not have this limitation and so this will be taken care of later on. Just to tell you what I mean this is what it looks like. So the second dimension has to be a constant. So if I want to write it, if I want to have a function for a different length 25 over here sorry this function will not work. If I want to pass an array with a different length this function will not work. Anyway, let us just see what this might be doing. So here we have what are we doing? We are getting an array with some unknown number of columns but there is an argument n which is going to specify how many columns that is. And the function then simply prints all the strings in C. So this is the main function, this is our old array so it has three strings in it and if you call it in this manner then all the three names will get printed. So what did we discuss in this segment? So we discussed the basics of two-dimensional arrays. We said that they can be used for storing tables, matrices and multiple character strings. We also said that a two-dimensional array can be regarded as an array of one-dimensional arrays. Next we are going to come to command line arguments but we will take a break first.