C-SHARP - 4.1 Advanced tables
Hello everyone and welcome to this new video. In this video, we are going to start a more advanced notion of arrays. I will deepen your knowledge through theory but also a lot of practice. Before starting, let's remember what an array is and how to create it in C#. An array represents a fixed number of variables, values of the same type (ie int, string, etc ) So let's start by creating two arrays of different types. An array containing 3 int type values and an array containing 3 string type values. To create a painting, remember? You type the type of values you want to store in your array then you put square brackets, you give it a name, here for example we will call it values, then you do = then new to allocate memory to our array then we put the type of our array back to be int and then we put square brackets. And in these brackets, we put the number of values we want to store in our array. So we put 3 here. Now, there are 2 ways to initialize our array either just after the square brackets, we put braces and we enter all our values there if we already know them. Otherwise we can assign them later as to initialize a variable For the first table I will use the first method, and for the second, I'm going to use the other story to refresh your memory So in the first array I'm going to leave the braces and I'm going to put the values 1 2 3. Then I'm going to do my second string array, so I type the type string brackets, I give it a name for example word, then I do = new string brackets and I will put in the brackets the number of values that I want to put in my array. Let 3 and ; then to assign my values in the array, what do I have to do, remember? I have to type the name of the array either word then I put square brackets and inside I put the index of my first element of the array. The index of the first element of the array is 0 so I put 0 between the brackets. And after to assign the value I do = and I give it the value I want to assign to it here it is a string so I do quotes and inside my word which is hello. And so on for the other 2 elements. I retrieve the index of the location and I assign the value I want to it. Now we have our 2 paintings. Nothing new so far. In C#, there are two types of arrays, one-dimensional arrays are those we have seen so far like the example above and there are also 2 or even 3-dimensional arrays. A classic table contains a single line of several columns, it can be likened to a list. However, an array can contain variables, but also other arrays. In this case we speak of a multidimensional array. Having multiple rows and multiple columns. The most common is the 2-dimensional array, which makes it possible to very well represent the arrays that are manipulated in the physical world. A multidimensional array is an array that itself contains arrays. A 2-dimensional array is therefore an array whose elements are themselves arrays of values. For example, a class is divided into 3 groups of 3 students. In C# it will give a : You will type var, here the word var makes it possible to declare local variables, without having to explicitly write its type because the compiler will determine this type from the initialization value which is mandatory. Then we give a name to our array for example tab_dim then = new and we give the type of values we want to store. Here we are going to store first names so we put string and after, it is here that you are going to see something new. Instead of there being 1 digit in the brackets, there will be 2 . The first will denote the rows and the 2nd the columns, unlike the array has a dimension which contained a number which traversed columns. Here we are going to make a table containing 3 rows and 3 columns so between the brackets we put 3, 3 Now to create our table I use braces I go to the line and I will directly create 3 rows. For a I then braces, braces, and to finish braces I now have my 3 lines but it is underlined in red? Why ? It's because I haven't put my 3 values in my table yet because here we tell it we want 3 columns or 3 values per line but we don't initialize them. So the editor sends us errors directly. So I'm going to enter my values by copying and pasting what I did beforehand because otherwise it will take too long. And here we have created our two-dimensional array. Now we will try to display a value from our array and then we will display our complete array. So first, I'm going to do a Console.WriteLine() and inside the parentheses I'm going to put my array. Then to retrieve a value from a one-dimensional array, an index was put in order to retrieve the value found at this location. Bah, it's the same thing except that, we will have to give the line and the column. A single number will not be enough since we are in a 2-dimensional array, you will have to put 2. Now I want the value to be retrieved Tom What numbers should I put between the square brackets to succeed in displaying it? Well I have to put the line here there are 3 lines but you know that we start at 0 so the second line is equivalent to 1 so we put a 1 and then we put a comma to specify in which column Tom is located So we start from 0 1 2 the index of tom is 2, so after the comma I put 2. I remember the first digit specifies the row and the second specifies the column. Now if I launch the program, we have our first name Tom which is displayed. If now I change the line for example, I put 2 and I restart. This time we have Marc showing up. You now know how to display a value from a two-dimensional array. Now we would like to display all the first names in this table. And you remember that to loop through the elements of an array, you had to loop the length of the array and display the elements one by one. Bah here to traverse this table It will be a little different, we will use the loops but this time It will be a loop in a loop which is called a nested loop The first loop will traverse the lines and the second being al interior will traverse the columns. We will create the loop that will manage the lines. So here we are going to do a for then we are going to do an int i = 0; after that we will put the condition that as long as i < the number of lines is 3; and after we put the incrementation to move forward. Then we will do a small Console.WriteLine() in order to have something formatted as output. Inside the parentheses I make a string.Format() and between the quotes I put Group n and I put braces 0 to retrieve the ia each round of the loop. Like this, we will display group 1, group 2, etc. since the is increments. Then I put a backslash n to have a small return to the line between the group and the first names. And after the comma I put i +1 to avoid having a Group n 0. And now this is where I'm going to make another loop to go through the columns So I'm going to type for parentheses and there I'm going to put int j = 0; since i we use it for rows you can call it whatever you want, it's just that developers often use these names to make demonstrations Then the condition is that as long as j is less than the number of columns is 3 then you loops and we do not forget our incrementation of J. and it is in these braces that we will display our first names. We are going to do a Console.WriteLine(), in the parentheses we do as for Tom we put our array, brackets and here instead of putting numbers, we are going to put the i and the j. Why ? Because they are the ones who will have the role of index to retrieve the values during the iterations of the loops. At each turn, they increase and move forward in the table so if we put fixed numbers we will never have all the first names in our table The program will display the same first names to us each time. So before launching the program I will make a small summary history that it is clear for you! Here we have created two loops, one that will allow you to browse the rows and the other the columns. At the first iteration of the first for, we are going to display which group we are going to display and then we are going to use a loop to browse all the columns or more precisely all the elements of the table found in this line. Let i = 0 we are at the first line and here we will loop over all the elements of the first line. j is worth 0 so we will display the first name located on line 0 and column 0, that is Jason Then since the j is always less than the number of columns, you will increment it and we will display the element located at again line 0 since it has not yet been incremented, however it is, so we will display the element located in line 0 but in column 1. Let Thibaut and so on until reaching the last element of the line once arrived at the end j fulfilled the condition of the for so we exit the loop, we have executed all the instructions of the iteration of the first for so is incremented. This time, it is worth 1, or we go to the second line and we re execute the instructions contained between the braces of the first for. Either we will redo a loop again which will browse the elements of each column since we have reinitialized the ja zero, but this time at the second line. And we will start again until the condition of the first forum is fulfilled. Now if we run the program. We have our complete table displayed. To summarize, it is as if we had a large table comprising 3 small tables. And since each time it is the same instructions which are repeated roughly, we go through each element of a small table then we start again for each small table until the end. And once we have reached the last table then the program stops. I know it might be a little vague on your side, but if we take an example that will talk a little more, it's like having a game. The character must climb 10 stairs to get to a floor so we are going to make a loop that executes the up action 10 times to reach a floor. Once arrived at a floor, the steps are reset to 0. To end the game, you must reach the 5th floor. So on each floor he must climb 10 stairs or we will make a loop that will repeat 5 times the loop that makes the character climb 10 times. I think with this example it is now clearer. Anyway, that's all for this video. I hope this video has you more and that everything has been clear for you. See you next time in a new video.