1 00:00:00,000 --> 00:00:09,250 2 00:00:09,250 --> 00:00:11,070 >> ZAMYLA CHAN: Let's jump into Mario. 3 00:00:11,070 --> 00:00:14,850 Mario is a program where we'll make our own, albeit primitive, version of 4 00:00:14,850 --> 00:00:17,760 a classic Super Mario Brothers game background. 5 00:00:17,760 --> 00:00:21,100 For this problem set, we're going to recreate the half-pyramid for 6 00:00:21,100 --> 00:00:23,000 Mario to jump on. 7 00:00:23,000 --> 00:00:27,330 Our program will be interactive, so it will ask the user to input a certain 8 00:00:27,330 --> 00:00:28,660 height for the pyramid. 9 00:00:28,660 --> 00:00:32,920 And the program will print out a half-pyramid of that height, where the 10 00:00:32,920 --> 00:00:37,390 bottom left hash aligns with the bottom left corner of the terminal 11 00:00:37,390 --> 00:00:39,710 output window. 12 00:00:39,710 --> 00:00:44,870 >> Let's break this problem into two parts, one, get user input, and two, 13 00:00:44,870 --> 00:00:47,040 print out the pyramid. 14 00:00:47,040 --> 00:00:51,150 Remember that though the GetInt function will retrieve an integer, you 15 00:00:51,150 --> 00:00:56,260 have to make sure that this input makes sense for your program, so in 16 00:00:56,260 --> 00:00:59,690 Mario, it won't do just a product for an integer. 17 00:00:59,690 --> 00:01:03,440 You'll need to make sure that the user inputs an integer that's within the 18 00:01:03,440 --> 00:01:06,985 lower and upper bounds of the pyramid that you can print. 19 00:01:06,985 --> 00:01:12,300 Per this spec, this is 0 and 23 inclusive. 20 00:01:12,300 --> 00:01:16,410 If the user inputs an integer outside of our accepted bounds, then we want 21 00:01:16,410 --> 00:01:20,840 to prompt them again and again until they give us a valid integer. 22 00:01:20,840 --> 00:01:25,990 >> One way to ensure correct user input is by using a Do-While loop, which is 23 00:01:25,990 --> 00:01:28,100 very similar to a While loop. 24 00:01:28,100 --> 00:01:32,580 The Do-While loop executes the code inside the body once, and then checks 25 00:01:32,580 --> 00:01:35,270 whether the condition is met or not. 26 00:01:35,270 --> 00:01:38,830 This is useful for obtaining user input because you know that you need 27 00:01:38,830 --> 00:01:41,805 to prompt them at least once. 28 00:01:41,805 --> 00:01:45,940 If the condition is not met, the program will execute the line after 29 00:01:45,940 --> 00:01:47,270 your Do-While loop. 30 00:01:47,270 --> 00:01:50,950 If the condition is met, though, the loop will repeat. 31 00:01:50,950 --> 00:01:55,560 >> A Do-While loop for validating user input will look something like this. 32 00:01:55,560 --> 00:02:02,920 I declare a variable n, GetInt, and repeat until n is valid. 33 00:02:02,920 --> 00:02:06,270 Remember that when you declare your variable, it needs the appropriate 34 00:02:06,270 --> 00:02:08,449 scope, just like in Scratch. 35 00:02:08,449 --> 00:02:12,510 If I declare n inside my Do-While loop, the rest of the program won't be 36 00:02:12,510 --> 00:02:13,750 able to access it. 37 00:02:13,750 --> 00:02:16,100 It's limited to the confines of the curly braces. 38 00:02:16,100 --> 00:02:19,010 39 00:02:19,010 --> 00:02:23,090 >> All right, so now that we've validated the user's input, we actually need to 40 00:02:23,090 --> 00:02:25,020 draw this half-pyramid. 41 00:02:25,020 --> 00:02:29,700 It's made up of printed characters, so let's make a half-pyramid in a simple 42 00:02:29,700 --> 00:02:31,480 text editor. 43 00:02:31,480 --> 00:02:35,920 If we want to make a Mario style pyramid of height three that's aligned 44 00:02:35,920 --> 00:02:41,370 on the left side of our window, then we'd type two hashes, click Enter, 45 00:02:41,370 --> 00:02:47,180 then type three hashes, click Enter, and then type four. 46 00:02:47,180 --> 00:02:51,090 >> But in this problem set, our half-pyramid has to be right aligned. 47 00:02:51,090 --> 00:02:55,550 Using a standard keyboard, how might you modify this file to move hashes 48 00:02:55,550 --> 00:02:57,210 over to the right side? 49 00:02:57,210 --> 00:03:03,190 I might use to underscores, two on the top row and one on the second. 50 00:03:03,190 --> 00:03:07,690 That doesn't look as nice, though, so let's replace the underscores with 51 00:03:07,690 --> 00:03:12,450 spaces, and there we have a half-pyramid of height three. 52 00:03:12,450 --> 00:03:16,330 >> Try and remember this text editor example as you start thinking back to 53 00:03:16,330 --> 00:03:20,100 C. Let's try and figure out some kind of pattern that we can 54 00:03:20,100 --> 00:03:22,750 put in a loop construct. 55 00:03:22,750 --> 00:03:27,570 Take an example height, say eight, and see if you can start to 56 00:03:27,570 --> 00:03:29,470 come up with a pattern. 57 00:03:29,470 --> 00:03:34,710 The first row will have seven spaces followed by two hashes. 58 00:03:34,710 --> 00:03:40,090 The second row will have six spaces and three hashes, and so on until the 59 00:03:40,090 --> 00:03:41,440 eighth row. 60 00:03:41,440 --> 00:03:45,210 >> But how do you would represent the nth row? 61 00:03:45,210 --> 00:03:48,170 Remember that programming convention is zero indexed. 62 00:03:48,170 --> 00:03:51,870 That means that you start counting at zero, so the first row is technically 63 00:03:51,870 --> 00:03:57,110 row number zero with two hatches, row number one has three hashes, row 64 00:03:57,110 --> 00:04:01,860 number two has four hashes, so following this pattern for any row 65 00:04:01,860 --> 00:04:05,800 number n, there are n plus 2 hashes. 66 00:04:05,800 --> 00:04:09,270 I'll leave it to you, though, to find out the pattern for the spaces. 67 00:04:09,270 --> 00:04:12,240 Remember that your pattern will be slightly different if you choose to 68 00:04:12,240 --> 00:04:16,649 start your count from one and not zero. 69 00:04:16,649 --> 00:04:19,560 >> Now, you have a pattern for any abstract row n. 70 00:04:19,560 --> 00:04:25,190 You know how many spaces to print and how many hashes, so for each row, that 71 00:04:25,190 --> 00:04:26,270 number will change. 72 00:04:26,270 --> 00:04:30,700 But the same process of printing a single character, either a space or a 73 00:04:30,700 --> 00:04:33,200 hash, is repeated. 74 00:04:33,200 --> 00:04:37,470 So all you have to do is repeatedly print that character for as many times 75 00:04:37,470 --> 00:04:40,120 as your pattern determines. 76 00:04:40,120 --> 00:04:42,522 >> So how do we repeat processes? 77 00:04:42,522 --> 00:04:45,160 In Scratch, we used the repeat block. 78 00:04:45,160 --> 00:04:48,580 In C, we'll use a For loop. 79 00:04:48,580 --> 00:04:51,290 Let's look at the syntax for For loops. 80 00:04:51,290 --> 00:04:57,140 >> Each For loop is composed of three parts, an initialization, a condition, 81 00:04:57,140 --> 00:04:58,592 and an update. 82 00:04:58,592 --> 00:05:01,550 Your For loop will initialize a variable when it first 83 00:05:01,550 --> 00:05:02,960 enters the For loop. 84 00:05:02,960 --> 00:05:07,100 If the condition is met, the body of the loop will execute. 85 00:05:07,100 --> 00:05:10,070 Afterwards, the update will execute. 86 00:05:10,070 --> 00:05:13,630 >> If the condition is still met, the loop will execute and update and 87 00:05:13,630 --> 00:05:18,580 repeat as long as your condition evaluates to True. 88 00:05:18,580 --> 00:05:21,450 You're condition must eventually valuate to False, though, because 89 00:05:21,450 --> 00:05:24,490 unlike Scratch, we don't have any Forever loops. 90 00:05:24,490 --> 00:05:28,270 Your program must end eventually. 91 00:05:28,270 --> 00:05:32,330 >> Here's an example of a For loop that you might want to use for Mario. 92 00:05:32,330 --> 00:05:36,790 The initialization declares an integer i with value 0. 93 00:05:36,790 --> 00:05:40,750 As long as i is less than the height, the body of the loop will execute and 94 00:05:40,750 --> 00:05:44,980 increase i by one and repeat until i is greater than or 95 00:05:44,980 --> 00:05:47,220 equal to the height. 96 00:05:47,220 --> 00:05:49,140 >> Now, my For loop is zero index. 97 00:05:49,140 --> 00:05:52,270 The Int i starts at zero, not one. 98 00:05:52,270 --> 00:05:55,320 If I chose one, then my condition would also have to be different for 99 00:05:55,320 --> 00:05:58,740 the loop to execute the same number of times. 100 00:05:58,740 --> 00:06:03,490 Remember this carefully when you're choosing your initialization and your 101 00:06:03,490 --> 00:06:08,660 condition, and consult this chart to double check. 102 00:06:08,660 --> 00:06:13,430 >> One of the great things about computer science is that there's just so many 103 00:06:13,430 --> 00:06:15,490 ways to execute things. 104 00:06:15,490 --> 00:06:19,450 You can choose whether you start at zero or one. 105 00:06:19,450 --> 00:06:22,380 If you don't want to use a For loop, you can also use a While loop to 106 00:06:22,380 --> 00:06:26,530 repeat statements as long as you keep the essence of a For loop, 107 00:06:26,530 --> 00:06:31,430 initialization, condition, and update. 108 00:06:31,430 --> 00:06:36,890 Once we figure out the For loop that we're most comfortable with, then we 109 00:06:36,890 --> 00:06:38,450 can complete Mario. 110 00:06:38,450 --> 00:06:41,540 >> We first checked for user input and then identified a 111 00:06:41,540 --> 00:06:43,580 pattern for any row n. 112 00:06:43,580 --> 00:06:49,990 So for each row from zero to n minus 1 will print the appropriate number of 113 00:06:49,990 --> 00:06:55,340 spaces, then the appropriate number of hashes according to our pattern, and 114 00:06:55,340 --> 00:06:57,180 then a new line. 115 00:06:57,180 --> 00:06:59,640 With that, you have your pyramid. 116 00:06:59,640 --> 00:07:02,630 My name is Zamyla, and this was Mario. 117 00:07:02,630 --> 00:07:11,765