1 00:00:00,210 --> 00:00:00,930 ‫Welcome back. 2 00:00:01,020 --> 00:00:06,420 ‫Before we get started with anything else, I have a little challenge for you, and that is to take this 3 00:00:06,420 --> 00:00:07,200 ‫for loop here. 4 00:00:07,230 --> 00:00:13,950 ‫This nested for loop and adjust it so that our console right here or this for loop in general will not 5 00:00:13,950 --> 00:00:19,560 ‫print out all elements of our matrix, but only the ones that are odd numbers. 6 00:00:19,980 --> 00:00:23,130 ‫So only the two to 4 to 6 and the eight. 7 00:00:23,670 --> 00:00:26,220 ‫So please adjust the code so that it does it. 8 00:00:26,220 --> 00:00:32,250 ‫And just a little hint you will need to use if statements as well as the modulo operator. 9 00:00:33,860 --> 00:00:34,210 ‫Okay. 10 00:00:34,220 --> 00:00:40,610 ‫So I hope you passed the video and you tried it for yourself to get this running and to make the code 11 00:00:40,610 --> 00:00:43,250 ‫do exactly as we like it to do. 12 00:00:43,250 --> 00:00:53,420 ‫And what we all need is an if statement the checks the matrix at the i j position for modulo two because 13 00:00:53,420 --> 00:00:59,600 ‫if something is modulo two zero, then it is going to be an even number. 14 00:00:59,750 --> 00:01:11,570 ‫So what this basically does is it compares if the modulo remainder when you divide something by two 15 00:01:11,870 --> 00:01:12,920 ‫is going to be zero. 16 00:01:12,920 --> 00:01:16,190 ‫So if the remainder is zero, then we know we have an even number. 17 00:01:16,700 --> 00:01:20,570 ‫Okay, so basically that's going to be it. 18 00:01:20,570 --> 00:01:27,560 ‫So if this is the case, then print the number and otherwise else. 19 00:01:28,240 --> 00:01:33,190 ‫We could also add a statement here where it's just console, right? 20 00:01:34,120 --> 00:01:38,320 ‫Like soul and just an empty space, nothing more. 21 00:01:39,120 --> 00:01:39,270 ‫Okay. 22 00:01:39,330 --> 00:01:42,120 ‫So this will now print it out. 23 00:01:43,140 --> 00:01:48,270 ‫And print specifically the even numbers two, four, six, eight. 24 00:01:48,570 --> 00:01:50,190 ‫So that's what I meant with. 25 00:01:50,220 --> 00:01:53,490 ‫We all need an F statement as well as a modulo operator. 26 00:01:53,910 --> 00:01:57,330 ‫So this is the modulo operator and it works with remainders. 27 00:01:57,360 --> 00:02:03,420 ‫Now, by the way, if one is the result here, so if the remainder is one, then we know that we have 28 00:02:03,420 --> 00:02:04,440 ‫an odd number. 29 00:02:04,710 --> 00:02:05,970 ‫So let's run it again. 30 00:02:05,970 --> 00:02:09,990 ‫And you see, we only get the odd numbers from our matrix. 31 00:02:10,740 --> 00:02:16,190 ‫So this was just a little challenge for you to get the juices flowing. 32 00:02:16,200 --> 00:02:23,010 ‫So now let's look at how we can print the diagonal elements of a matrix. 33 00:02:23,310 --> 00:02:25,980 ‫So one, five and nine, for example. 34 00:02:25,980 --> 00:02:27,780 ‫Or seven, five and three. 35 00:02:27,810 --> 00:02:29,010 ‫Or the other way around, actually. 36 00:02:29,010 --> 00:02:30,000 ‫Three, five, seven. 37 00:02:30,150 --> 00:02:33,000 ‫So how can we actually achieve that? 38 00:02:35,470 --> 00:02:46,690 ‫So if we look at our nested for loop that we created here, then we realize that here I is zero and 39 00:02:46,690 --> 00:02:47,590 ‫J is zero. 40 00:02:47,620 --> 00:02:57,490 ‫If we look at I and J at this case, I is one and J is one, and in this case I is two and J is two. 41 00:02:57,520 --> 00:03:03,340 ‫So if we want to have a diagonal, we know that I and J have to be the same. 42 00:03:03,340 --> 00:03:08,830 ‫So at least for this diagonal here, four one, five and nine, four, seven, five and three, it's 43 00:03:08,830 --> 00:03:10,150 ‫going to be a different story. 44 00:03:10,150 --> 00:03:17,590 ‫But for the one five at nine, we can basically do something very simple, and that is to just check 45 00:03:17,620 --> 00:03:18,880 ‫if the matrix. 46 00:03:19,810 --> 00:03:21,340 ‫Well, actually a challenge for you. 47 00:03:21,370 --> 00:03:21,760 ‫Try. 48 00:03:21,910 --> 00:03:22,960 ‫Try it for yourself. 49 00:03:25,000 --> 00:03:25,280 ‫Okay. 50 00:03:25,300 --> 00:03:26,620 ‫So I hope you checked it. 51 00:03:27,190 --> 00:03:28,030 ‫So what? 52 00:03:28,030 --> 00:03:29,350 ‫We need to check this. 53 00:03:29,650 --> 00:03:31,480 ‫Is I equal J. 54 00:03:32,400 --> 00:03:36,480 ‫And if that's the case, then print out the value. 55 00:03:37,290 --> 00:03:40,290 ‫Otherwise, print out an empty space. 56 00:03:41,070 --> 00:03:42,600 ‫So let's run that real quick. 57 00:03:43,880 --> 00:03:47,330 ‫And we will see that we get one, five and nine. 58 00:03:50,510 --> 00:03:52,760 ‫I now I want this to look a little better. 59 00:03:52,760 --> 00:04:02,090 ‫What we actually also can do is we can use a control bright line statement with an empty space and then 60 00:04:02,090 --> 00:04:03,410 ‫it would look like this. 61 00:04:03,410 --> 00:04:06,560 ‫So one, five, nine for aesthetic reasons. 62 00:04:07,070 --> 00:04:09,440 ‫So that's something that we can achieve here. 63 00:04:09,470 --> 00:04:13,310 ‫You can, of course, create a significantly more complex logic. 64 00:04:14,060 --> 00:04:17,710 ‫So what you could do is you could replace the diagonal values. 65 00:04:17,720 --> 00:04:21,890 ‫Now, for example, instead of just printing them out, you could assign them. 66 00:04:21,890 --> 00:04:26,030 ‫So you could assign a different value for them saying something like. 67 00:04:26,850 --> 00:04:29,400 ‫All of them should be a one, for example. 68 00:04:29,940 --> 00:04:32,250 ‫So that could be something that you could do. 69 00:04:32,250 --> 00:04:38,150 ‫And then let's actually do the for each statement underneath it. 70 00:04:38,160 --> 00:04:39,360 ‫Let's run this again. 71 00:04:40,080 --> 00:04:45,030 ‫And you will see that now it will be one, two, three, four, one, six, seven, eight, one. 72 00:04:45,280 --> 00:04:49,020 ‫Okay, so basically replaced this five and this nine with a one. 73 00:04:49,020 --> 00:04:54,780 ‫So now this is just an example of how you can do it using a nested for loop. 74 00:04:54,870 --> 00:05:00,390 ‫The thing is, you can actually do it using just one for loop. 75 00:05:00,420 --> 00:05:04,460 ‫It's a little easier, of course, and a little challenge for you. 76 00:05:04,470 --> 00:05:06,630 ‫Try to do it with just one for loop. 77 00:05:08,470 --> 00:05:09,060 ‫Okay. 78 00:05:09,070 --> 00:05:13,180 ‫So basically it's just going to be this here. 79 00:05:13,810 --> 00:05:21,640 ‫So we just use this for loop where it goes through the matrix get length, so through the column length, 80 00:05:21,640 --> 00:05:25,760 ‫so to speak and it just prints out I. 81 00:05:25,990 --> 00:05:30,760 ‫So basically saying one or in this case, zero zero. 82 00:05:30,770 --> 00:05:32,320 ‫In this case one one. 83 00:05:32,320 --> 00:05:34,730 ‫In this case, two, two. 84 00:05:35,290 --> 00:05:39,670 ‫So this will be the position of the numbers that I just showed you. 85 00:05:40,720 --> 00:05:41,090 ‫Okay. 86 00:05:41,110 --> 00:05:43,570 ‫If we run this, we should also get one, five, nine. 87 00:05:43,570 --> 00:05:46,450 ‫Let's look at it and we can see we get 159. 88 00:05:47,000 --> 00:05:50,290 ‫Okay, now what if we want to get three, five and seven? 89 00:05:50,620 --> 00:05:57,760 ‫So now I would like you to pause the video and find a logic that will print out three, five, seven 90 00:05:57,760 --> 00:05:58,390 ‫instead. 91 00:06:00,500 --> 00:06:00,690 ‫Okay. 92 00:06:00,740 --> 00:06:08,900 ‫This will be a little more complicated because we now need to think where we need to start our eye and 93 00:06:08,900 --> 00:06:09,710 ‫where we need to start. 94 00:06:09,710 --> 00:06:16,880 ‫RJ Because basically what we need to do is our eye still starts at the beginning and goes up by one. 95 00:06:16,880 --> 00:06:18,890 ‫So we go through one row at a time. 96 00:06:19,280 --> 00:06:20,630 ‫But RJ. 97 00:06:21,430 --> 00:06:27,490 ‫He's going to go down from the maximum value in this case from two. 98 00:06:28,540 --> 00:06:29,140 ‫Okay. 99 00:06:29,140 --> 00:06:30,010 ‫So. 100 00:06:32,220 --> 00:06:32,480 ‫Okay. 101 00:06:32,490 --> 00:06:41,000 ‫So and now I would like to show you a little trick how you can go down or basically show the diagonal 102 00:06:41,040 --> 00:06:42,750 ‫signal from the other way around. 103 00:06:42,750 --> 00:06:51,030 ‫So from three, five and seven, and therefore you can use something where a four loop is going to be. 104 00:06:52,770 --> 00:06:55,020 ‫Having to counter variables. 105 00:06:55,800 --> 00:06:58,250 ‫So usually let's look at a for loop. 106 00:06:58,260 --> 00:07:00,900 ‫We can just enter four and then press tab twice. 107 00:07:00,900 --> 00:07:03,060 ‫This is how a four loop usually looks like. 108 00:07:03,060 --> 00:07:03,300 ‫Right? 109 00:07:03,300 --> 00:07:09,390 ‫So you have a counter variable ie and then you have the length and in our case the length will be predefined 110 00:07:09,390 --> 00:07:11,940 ‫is three because we will hardcoded this time. 111 00:07:11,940 --> 00:07:14,700 ‫We know that we will just have three lines here. 112 00:07:14,730 --> 00:07:19,950 ‫Of course you could also say matrix get length like so matrix get length. 113 00:07:20,820 --> 00:07:23,640 ‫For the zeroth dimension. 114 00:07:24,870 --> 00:07:33,480 ‫And now what you also can do, by the way, is you can add a comma here and say that J should start 115 00:07:33,480 --> 00:07:37,650 ‫as two and then you decrement J. 116 00:07:38,390 --> 00:07:39,660 ‫For each iteration. 117 00:07:39,680 --> 00:07:40,460 ‫Like so. 118 00:07:40,580 --> 00:07:43,370 ‫So now what we have is we have to counter variables. 119 00:07:43,370 --> 00:07:49,910 ‫So we have the eye counter available and at the same time the j count the variable, but we are only 120 00:07:49,910 --> 00:07:51,800 ‫limiting based on the eye value. 121 00:07:51,800 --> 00:07:59,630 ‫So this will only work properly for a matrix that has the same amount of columns as it has amount of 122 00:07:59,630 --> 00:08:05,360 ‫rows, because otherwise you would have to use a nested for loop as we've done before. 123 00:08:06,380 --> 00:08:16,580 ‫So this will not work and we can just go ahead and write the matrix position I and J to our console. 124 00:08:16,610 --> 00:08:20,030 ‫So let's look at it and we can see it shows three, five and seven. 125 00:08:20,150 --> 00:08:24,110 ‫So this time it went from top right to bottom left. 126 00:08:27,610 --> 00:08:31,780 ‫But this is just an example of what you can do with a follow up. 127 00:08:31,810 --> 00:08:35,950 ‫This is not very common practice, so be careful when you use it. 128 00:08:35,950 --> 00:08:41,350 ‫This is a little more difficult to read and this is also something that you should consider when developing. 129 00:08:41,350 --> 00:08:45,400 ‫So if you use it in a simple example, it's going to be probably fine. 130 00:08:45,400 --> 00:08:51,340 ‫But if the example becomes more complex, then this can become quite confusing, or it could potentially 131 00:08:51,340 --> 00:08:57,040 ‫become quite confusing when double checking your application, or when a third developer or a second 132 00:08:57,040 --> 00:08:59,170 ‫developer comes in and checks your code. 133 00:08:59,170 --> 00:09:03,220 ‫So always take that into consideration when writing your code. 134 00:09:03,220 --> 00:09:06,640 ‫Make it as straightforward as possible so that it stays readable.