1 00:00:00,000 --> 00:00:00,982 2 00:00:00,982 --> 00:00:11,293 >> [MUSIC PLAYING] 3 00:00:11,293 --> 00:00:13,580 >> ROB BOWDEN: It's me, Rob. 4 00:00:13,580 --> 00:00:16,540 Let's walk through how to implement Mario. 5 00:00:16,540 --> 00:00:21,040 So the first thing we need to do is prompt the user for the input. 6 00:00:21,040 --> 00:00:24,440 >> We need to ask them exactly how tall the pyramid should be. 7 00:00:24,440 --> 00:00:27,110 So here, we see we're doing height = GetInt. 8 00:00:27,110 --> 00:00:32,479 An And remember that the GetInt function is implemented in the CS50 9 00:00:32,479 --> 00:00:38,060 library, so up top, we need to remember to #include cs50.h. 10 00:00:38,060 --> 00:00:41,360 >> So why do we have this wrapped in a do-while loop? 11 00:00:41,360 --> 00:00:45,080 Well, we also need to remember that the user's input has to be valid. 12 00:00:45,080 --> 00:00:46,910 What is an invalid input? 13 00:00:46,910 --> 00:00:51,460 Well the pset spec specifically says that a height less than 0 or greater 14 00:00:51,460 --> 00:00:54,530 than 23 is invalid. 15 00:00:54,530 --> 00:00:59,030 >> So up here, we see that we're defining a constant called MAX_HEIGHT with the 16 00:00:59,030 --> 00:01:00,750 value of 23. 17 00:01:00,750 --> 00:01:06,380 This do-while loop will continue while height is less than 0 or MAX_HEIGHT is 18 00:01:06,380 --> 00:01:11,870 less than height, which means that 23 is less than the height. 19 00:01:11,870 --> 00:01:15,390 So if height is 24 or greater, we're going to continue looping. 20 00:01:15,390 --> 00:01:18,300 Remember that do-while loops are pretty helpful whenever we want to get 21 00:01:18,300 --> 00:01:22,070 user input and then validate it, since we inevitably need to ask the user at 22 00:01:22,070 --> 00:01:25,010 least once for the value they want. 23 00:01:25,010 --> 00:01:28,500 >> So once we have their input, we can now build the pyramid. 24 00:01:28,500 --> 00:01:31,940 One of the tricks of this problem set is that we have to start at the top of 25 00:01:31,940 --> 00:01:32,750 the pyramid. 26 00:01:32,750 --> 00:01:36,800 You can't printf the bottom of the pyramid and then build your way up. 27 00:01:36,800 --> 00:01:38,830 So let's look at the example from the pset spec. 28 00:01:38,830 --> 00:01:41,530 29 00:01:41,530 --> 00:01:45,430 >> We see here that when we enter a height of 8, the very bottom of the 30 00:01:45,430 --> 00:01:48,660 pyramid prints nine hashes. 31 00:01:48,660 --> 00:01:52,990 One level up from that prints one space and eight hashes. 32 00:01:52,990 --> 00:01:58,250 One level up from that is two spaces and seven hashes, all the way until we 33 00:01:58,250 --> 00:02:03,050 get to the top of the pyramid, which is eight levels up, which prints seven 34 00:02:03,050 --> 00:02:06,000 spaces and two hashes. 35 00:02:06,000 --> 00:02:08,810 So remember that we have to do this top level first. 36 00:02:08,810 --> 00:02:11,620 37 00:02:11,620 --> 00:02:18,500 >> Here we're iterating from the top level, row 8, continuing 38 00:02:18,500 --> 00:02:22,150 until row reaches 0. 39 00:02:22,150 --> 00:02:25,820 So how many spaces did we need to print in that top row? 40 00:02:25,820 --> 00:02:29,310 We printed seven spaces and two hashes. 41 00:02:29,310 --> 00:02:34,450 So the number of spaces we want is the row that were on minus 1. 42 00:02:34,450 --> 00:02:39,310 >> If the top row is 8, 8 minus 1 gives us seven spaces. 43 00:02:39,310 --> 00:02:43,770 Then we have a loop that will print out each space one at a time. 44 00:02:43,770 --> 00:02:47,450 So when spaces is 7, this loop seven times, printing 45 00:02:47,450 --> 00:02:50,300 seven individual spaces. 46 00:02:50,300 --> 00:02:54,672 >> So now we need to print these hashes at the end of the pyramid. 47 00:02:54,672 --> 00:02:57,930 So here, we need to calculate the number of hashes. 48 00:02:57,930 --> 00:03:01,930 We see that we're doing height minus row plus 2. 49 00:03:01,930 --> 00:03:04,170 So how did we get that? 50 00:03:04,170 --> 00:03:08,630 >> Remember that the top of the pyramid is row 8, and the height is 8. 51 00:03:08,630 --> 00:03:10,890 And we still printed two hashes. 52 00:03:10,890 --> 00:03:15,420 So at the very least, 8 minus 8 plus 2 gives us the right answer. 53 00:03:15,420 --> 00:03:19,170 And then consider the bottom of the pyramid, row 1. 54 00:03:19,170 --> 00:03:24,020 Height minus row will give us 7, and then plus 2 gives us nine hashes, 55 00:03:24,020 --> 00:03:26,620 which is exactly the number of hashes that we printed. 56 00:03:26,620 --> 00:03:29,880 So this is the formula we want to use to calculate the number 57 00:03:29,880 --> 00:03:32,220 hashes in each row. 58 00:03:32,220 --> 00:03:36,020 >> Using that number, we then have another for loop, very similar to the 59 00:03:36,020 --> 00:03:41,270 for loop that we used for the spaces, that iterates number of hashes times 60 00:03:41,270 --> 00:03:43,720 printing a single hash each time. 61 00:03:43,720 --> 00:03:46,010 On the top row, that'll print two hashes. 62 00:03:46,010 --> 00:03:48,390 On the bottom row, that'll print nine hashes. 63 00:03:48,390 --> 00:03:52,610 And each other row will print every number of hashes in between. 64 00:03:52,610 --> 00:03:57,340 >> And then at the very end, we need to print our new line to go to the next 65 00:03:57,340 --> 00:03:59,400 row in the pyramid. 66 00:03:59,400 --> 00:04:03,070 Finally, we need to print the new line at the end of the row in order to 67 00:04:03,070 --> 00:04:06,260 continue to the next row of the pyramid. 68 00:04:06,260 --> 00:04:08,980 And at the end of our program, we have the return 0. 69 00:04:08,980 --> 00:04:12,770 >> As per the pset spec, the return 0 isn't strictly necessary. 70 00:04:12,770 --> 00:04:15,710 But it does signify that main is done. 71 00:04:15,710 --> 00:04:17,610 My name is Rob, and this was Mario. 72 00:04:17,610 --> 00:04:22,470 >> [MUSIC PLAYING] 73 00:04:22,470 --> 00:04:25,558