1 00:00:00,000 --> 00:00:13,010 2 00:00:13,010 --> 00:00:18,290 >> ROB BOWDEN: Hi, I'm Rob, and let's jump into the hacker edition of Mario. 3 00:00:18,290 --> 00:00:21,760 So first thing we need to do is get the height from the user. 4 00:00:21,760 --> 00:00:26,290 Here we're asking them for a non-negative integer less than 24 and 5 00:00:26,290 --> 00:00:31,710 we're using the CS50 GetInt function to grab that integer from the user. 6 00:00:31,710 --> 00:00:35,260 We see we're inside of a do-while loop that will continue looping as long as 7 00:00:35,260 --> 00:00:38,400 height is greater than 23 or less than 0. 8 00:00:38,400 --> 00:00:42,850 And so we'll continue until the user actually gives us what we want. 9 00:00:42,850 --> 00:00:46,960 >> Once we have that height, we get to the main for loop of our program. 10 00:00:46,960 --> 00:00:49,510 So let's first look at an example from the pset spec. 11 00:00:49,510 --> 00:00:52,270 12 00:00:52,270 --> 00:00:56,940 We see in this example that when we enter a height of 4, the bottom row 13 00:00:56,940 --> 00:01:01,520 first prints four hashes, two spaces, and four more hashes. 14 00:01:01,520 --> 00:01:06,280 >> Then one row above that prints one space three hashes, two spaces to 15 00:01:06,280 --> 00:01:09,690 separate the pyramids, and then three more hashes. 16 00:01:09,690 --> 00:01:13,460 And above that, two spaces, two hashes, two spaces, two hashes. 17 00:01:13,460 --> 00:01:18,090 And finally, three spaces one hash, two spaces one hash. 18 00:01:18,090 --> 00:01:20,980 So you should start to notice the pattern here. 19 00:01:20,980 --> 00:01:22,545 >> Let's look at the code for how we're going to do that. 20 00:01:22,545 --> 00:01:25,290 21 00:01:25,290 --> 00:01:28,840 We see here that we're iterating over all rows of the pyramid. 22 00:01:28,840 --> 00:01:31,720 First we want to calculate the number of spaces. 23 00:01:31,720 --> 00:01:34,690 And remember that we have to start at the top of the pyramid and work our 24 00:01:34,690 --> 00:01:37,790 way down since we can't print the bottom then one row up and 25 00:01:37,790 --> 00:01:39,140 then one row up. 26 00:01:39,140 --> 00:01:44,030 So at the top of the pyramid, notice that the number of spaces is equal to 27 00:01:44,030 --> 00:01:45,720 height minus 1. 28 00:01:45,720 --> 00:01:50,120 We're going to print three spaces then one hash and then two spaces to 29 00:01:50,120 --> 00:01:53,350 separate and another hash. 30 00:01:53,350 --> 00:01:57,320 >> So spaces is equal to height minus row. 31 00:01:57,320 --> 00:02:03,180 If the row is 1 and our height is 4, that'll give us 3 spaces, as we want. 32 00:02:03,180 --> 00:02:06,900 Then this for loop just prints that number of spaces. 33 00:02:06,900 --> 00:02:12,630 If spaces is three, then we're going to create a single space three times. 34 00:02:12,630 --> 00:02:18,750 >> Continuing, now we want to print the hashes of the left pyramid, which is 35 00:02:18,750 --> 00:02:20,630 just equal to the row number. 36 00:02:20,630 --> 00:02:23,250 Looking back here, in row one, we print one hash. 37 00:02:23,250 --> 00:02:25,960 In row two we print two, in row three we print three. 38 00:02:25,960 --> 00:02:30,440 So we simply loop row times printing hash symbol. 39 00:02:30,440 --> 00:02:34,830 Then for all rows of the pyramid, we print exactly two spaces to separate 40 00:02:34,830 --> 00:02:36,360 those pyramids. 41 00:02:36,360 --> 00:02:39,590 >> And finally, we want to print the right side of the pyramid, which is 42 00:02:39,590 --> 00:02:42,160 again the same number of hashes as the left side. 43 00:02:42,160 --> 00:02:45,680 And so it's the same exact for loop as above here. 44 00:02:45,680 --> 00:02:49,445 Finally, we need to create a new line in order to move on to the next row of 45 00:02:49,445 --> 00:02:51,690 the pyramid and continue printing. 46 00:02:51,690 --> 00:02:53,010 And that's it. 47 00:02:53,010 --> 00:02:54,860 My name is Rob and this was Mario. 48 00:02:54,860 --> 00:03:01,718