1 00:00:00,000 --> 00:00:00,500 2 00:00:00,500 --> 00:00:02,500 ZAMYLA CHAN: It's a me, Zamyla. 3 00:00:02,500 --> 00:00:06,910 Today in Mario, we're going to be drawing the half pyramid for Mario 4 00:00:06,910 --> 00:00:08,290 to climb up. 5 00:00:08,290 --> 00:00:11,570 >> So let's talk about our to-do's for this problem. 6 00:00:11,570 --> 00:00:13,610 We're going to want to prompt and validate 7 00:00:13,610 --> 00:00:18,290 the user for a valid input of how high they want Mario's pyramid to be. 8 00:00:18,290 --> 00:00:20,090 And then, we're going to draw it. 9 00:00:20,090 --> 00:00:24,870 So let's get started with prompting and validating the user for their input. 10 00:00:24,870 --> 00:00:27,640 >> We can make use of the CS50 Library Function 11 00:00:27,640 --> 00:00:31,160 get int that will ensure that the user inputs an integer. 12 00:00:31,160 --> 00:00:35,730 Any positive integers, negative integers, number 0 are all fair game. 13 00:00:35,730 --> 00:00:41,670 Otherwise, the user will be prompted to retry until they input a valid integer. 14 00:00:41,670 --> 00:00:44,210 Now although get int does a lot of the work for us 15 00:00:44,210 --> 00:00:46,730 in ensuring that the user gives us an integer, 16 00:00:46,730 --> 00:00:50,760 we still need to apply some additional constraints on that. 17 00:00:50,760 --> 00:00:56,420 After all, we can't have Mario climbing a half pyramid of height negative 12. 18 00:00:56,420 --> 00:00:59,040 >> In addition to that, the problem specification 19 00:00:59,040 --> 00:01:02,490 says that we can only allow Mario to climb 20 00:01:02,490 --> 00:01:06,940 a pyramid of heights between 0 and 23. 21 00:01:06,940 --> 00:01:11,120 OK, so that means that we need to continuously prompt the user 22 00:01:11,120 --> 00:01:14,320 to give us a valid number and only continue 23 00:01:14,320 --> 00:01:17,120 once they've given us a valid height. 24 00:01:17,120 --> 00:01:18,720 How do we do that? 25 00:01:18,720 --> 00:01:23,760 >> Well, continuous processes give us the idea of loops-- doing something 26 00:01:23,760 --> 00:01:24,720 repetitively. 27 00:01:24,720 --> 00:01:28,220 One loop in C as a while loop that will continuously 28 00:01:28,220 --> 00:01:33,480 execute the body of the loop as long as the given condition evaluates to true. 29 00:01:33,480 --> 00:01:36,200 As soon as that condition evaluates to false, 30 00:01:36,200 --> 00:01:39,770 the program will proceed to whatever comes after that. 31 00:01:39,770 --> 00:01:43,180 So while loops are one way of ensuring that we continually 32 00:01:43,180 --> 00:01:45,320 prompt the user for a valid input. 33 00:01:45,320 --> 00:01:50,070 And once they give us a valid input, we'll proceed to whatever comes next. 34 00:01:50,070 --> 00:01:54,380 We know that we're going to ask the user for input at least once. 35 00:01:54,380 --> 00:01:59,200 So now we come to a sister of the while loop, which is the do while loop. 36 00:01:59,200 --> 00:02:02,650 >> Do while loops will execute the body of the loop at least once. 37 00:02:02,650 --> 00:02:06,150 So without checking the condition, it will execute the body of the loop. 38 00:02:06,150 --> 00:02:09,750 And then check the condition to see whether it needs to repeat itself. 39 00:02:09,750 --> 00:02:13,080 This comes in handy when we're validating user input. 40 00:02:13,080 --> 00:02:15,830 We know that we're going to ask them at least once. 41 00:02:15,830 --> 00:02:18,780 So a do while loop might look something like this. 42 00:02:18,780 --> 00:02:20,090 We have an integer n. 43 00:02:20,090 --> 00:02:22,760 And inside of the do while loop, we immediately 44 00:02:22,760 --> 00:02:24,750 prompt the user for an integer. 45 00:02:24,750 --> 00:02:29,740 If n is invalid, then we'll prompt them again and again and again until they 46 00:02:29,740 --> 00:02:31,820 give us that valid integer. 47 00:02:31,820 --> 00:02:37,440 Finally, once n is a valid input, we'll proceed to the rest of our program. 48 00:02:37,440 --> 00:02:41,830 >> So let's go back to the spec and check what the conditions for a valid input 49 00:02:41,830 --> 00:02:43,670 is going to be. 50 00:02:43,670 --> 00:02:48,090 The valid heights are going to be between 0 and 23, inclusive. 51 00:02:48,090 --> 00:02:53,350 So invalid heights are going to be less than 0 or more than 23. 52 00:02:53,350 --> 00:02:56,420 So remember to design your condition carefully, 53 00:02:56,420 --> 00:02:58,660 knowing that the condition for the do while loop 54 00:02:58,660 --> 00:03:01,470 should be while n is invalid. 55 00:03:01,470 --> 00:03:05,080 Now this isn't going to be a simple single Boolean expression. 56 00:03:05,080 --> 00:03:07,630 We're going to have to combine two different expressions 57 00:03:07,630 --> 00:03:09,900 to make our whole condition. 58 00:03:09,900 --> 00:03:13,290 >> So let's just look at a truth table I've already given you the hint that we're 59 00:03:13,290 --> 00:03:15,200 going to be dealing with two Booleans. 60 00:03:15,200 --> 00:03:19,620 So here's a truth table where I have two Booleans-- Boolean 1 and 2. 61 00:03:19,620 --> 00:03:27,050 So we have the option to evaluate bool1 and bool2 or bool1 or bool2. 62 00:03:27,050 --> 00:03:31,980 And will only be true if both Booleans evaluate to true, whereas all 63 00:03:31,980 --> 00:03:37,280 or will be true as long as one of the two Booleans evaluates to true. 64 00:03:37,280 --> 00:03:41,450 OK, so take a moment, pause this video and digest this truth table. 65 00:03:41,450 --> 00:03:42,930 I'll be right here waiting. 66 00:03:42,930 --> 00:03:45,760 When you come back, see if you can piece together 67 00:03:45,760 --> 00:03:51,910 a Boolean expression for your condition of n being an invalid input. 68 00:03:51,910 --> 00:03:54,420 >> So now that we have valid user input, let's 69 00:03:54,420 --> 00:03:58,710 go ahead and talk about how we might draw the half pyramid. 70 00:03:58,710 --> 00:04:03,410 Here in this simple text editor, I've drawn a left aligned pyramid. 71 00:04:03,410 --> 00:04:07,050 But we know that we need our pyramid to be right aligned. 72 00:04:07,050 --> 00:04:08,650 So how might I do this? 73 00:04:08,650 --> 00:04:11,440 Well, I might try to push everything to the side 74 00:04:11,440 --> 00:04:14,880 by just putting a little character in between. 75 00:04:14,880 --> 00:04:16,779 And then, for the next line, I'm going to put 76 00:04:16,779 --> 00:04:20,970 some more characters to push it along, and further-- so on and so forth-- 77 00:04:20,970 --> 00:04:23,360 until I have the right aligned pyramid. 78 00:04:23,360 --> 00:04:27,780 So we do have a right align pyramid, but it doesn't look so great with the dots. 79 00:04:27,780 --> 00:04:30,680 But we still want to maintain that nice spacing. 80 00:04:30,680 --> 00:04:35,260 So I'm going to literally insert some spaces. 81 00:04:35,260 --> 00:04:39,420 >> Instead of three dots, I'll put one, two, three spaces. 82 00:04:39,420 --> 00:04:40,370 On the second line. 83 00:04:40,370 --> 00:04:42,640 I'll put one, two spaces. 84 00:04:42,640 --> 00:04:45,370 And on the penultimate line, just one space. 85 00:04:45,370 --> 00:04:48,290 And here I have a right aligned pyramid. 86 00:04:48,290 --> 00:04:52,170 From doing the example in the text editor, we have an idea for the pattern 87 00:04:52,170 --> 00:04:54,590 that we'll use to draw the half pyramid. 88 00:04:54,590 --> 00:04:58,080 For every row, what we did is we type some spaces, 89 00:04:58,080 --> 00:05:00,170 and then typed some hashes, and then typed 90 00:05:00,170 --> 00:05:03,020 the Enter key, which is creating a new line. 91 00:05:03,020 --> 00:05:07,770 So now that we have that, let's go one step further and find a pattern. 92 00:05:07,770 --> 00:05:10,170 >> So I'm going to say, for the interest of this example, 93 00:05:10,170 --> 00:05:12,480 we're dealing with a height of 8. 94 00:05:12,480 --> 00:05:17,100 The first row is going to have two hashes that follows seven spaces. 95 00:05:17,100 --> 00:05:20,020 The second-- three hashes, six spaces. 96 00:05:20,020 --> 00:05:24,260 Third row-- four hashes, five spaces-- so on and so forth 97 00:05:24,260 --> 00:05:26,350 until we get to the NTH row. 98 00:05:26,350 --> 00:05:31,540 So then, I ask you for the NTH row, how many hashes are we going to have 99 00:05:31,540 --> 00:05:33,120 and how many spaces? 100 00:05:33,120 --> 00:05:37,000 So it's up to you to figure out a formula to represent how many hashes 101 00:05:37,000 --> 00:05:42,020 and how many spaces are needed for the NTH row when you have some height. 102 00:05:42,020 --> 00:05:46,060 >> Now when you're figuring this out, be careful how you're indexing. 103 00:05:46,060 --> 00:05:49,170 What I mean by this is that in everyday life all of us 104 00:05:49,170 --> 00:05:51,540 start counting, usually by 1. 105 00:05:51,540 --> 00:05:55,950 But in CS50 and in computer science in general, we are 0 indexed. 106 00:05:55,950 --> 00:06:00,620 So the first row would be n of 0 as opposed to 1. 107 00:06:00,620 --> 00:06:04,550 Be careful of this when you're trying to figure out your pattern. 108 00:06:04,550 --> 00:06:07,570 So now let's go back to how we're going to draw our pyramid. 109 00:06:07,570 --> 00:06:12,300 For every row, we're going to want to print the spaces, print the hashes, 110 00:06:12,300 --> 00:06:14,050 and then print a new line. 111 00:06:14,050 --> 00:06:19,160 The hint here is the word "for" every row. 112 00:06:19,160 --> 00:06:21,470 In C, we have a construct called a for loop, 113 00:06:21,470 --> 00:06:25,250 which is comprised of an initialization, a condition, an update, 114 00:06:25,250 --> 00:06:26,790 and the body of the loop. 115 00:06:26,790 --> 00:06:31,360 >> Say I wanted to say, hello world, 50 times, my for loop 116 00:06:31,360 --> 00:06:32,880 would look something like this. 117 00:06:32,880 --> 00:06:35,480 I initialize my integer to 0. 118 00:06:35,480 --> 00:06:38,230 The condition is that I is less than 50. 119 00:06:38,230 --> 00:06:42,350 And then my update is just incrementing I by one every time. 120 00:06:42,350 --> 00:06:45,140 We can also use for loops to iterate over things. 121 00:06:45,140 --> 00:06:47,820 Notice here how we haven't hard coded a number, 122 00:06:47,820 --> 00:06:51,820 but rather placed the variable height instead into the condition. 123 00:06:51,820 --> 00:06:56,420 So what I'm doing here is I'm iterating over every row of the pyramid. 124 00:06:56,420 --> 00:07:00,160 I can do something for each row inside the body of my loop. 125 00:07:00,160 --> 00:07:02,350 >> What are we doing inside the body of the loop? 126 00:07:02,350 --> 00:07:07,120 Well, as we already said, we're printing spaces and we're printing hashes 127 00:07:07,120 --> 00:07:09,480 and we're printing a new line. 128 00:07:09,480 --> 00:07:11,950 So my outer for loop will look like this. 129 00:07:11,950 --> 00:07:15,070 I iterate over every row of the pyramid, using, 130 00:07:15,070 --> 00:07:18,890 in this case, height as the variable that stores the height of the pyramid. 131 00:07:18,890 --> 00:07:22,870 Inside the body of that loop, I'm going to print spaces repeatedly, print 132 00:07:22,870 --> 00:07:26,730 the hashes repeatedly, and then print a new line. 133 00:07:26,730 --> 00:07:31,010 >> So now, using all of the concepts that I've talked about in this walk-through, 134 00:07:31,010 --> 00:07:35,210 you should be able to prompt the user for input, validate that input, 135 00:07:35,210 --> 00:07:37,370 and then draw the half pyramid. 136 00:07:37,370 --> 00:07:41,510 >> My name is Zamyla, and this is CS50. 137 00:07:41,510 --> 00:07:43,167