1 00:00:00,506 --> 00:00:09,826 [ Silence ] 2 00:00:10,326 --> 00:00:11,206 >> All right. 3 00:00:11,636 --> 00:00:12,956 Welcome back. 4 00:00:12,996 --> 00:00:15,626 This is week two of CS50. 5 00:00:15,916 --> 00:00:19,746 By now if you've dived in to problems sets one, 6 00:00:19,746 --> 00:00:22,506 you probably encountered at least one of these. 7 00:00:22,566 --> 00:00:27,446 So this is a photograph of an actual logbook of an actual 8 00:00:27,446 --> 00:00:31,256 and in fact the first known bug in the computer program. 9 00:00:31,256 --> 00:00:33,946 This was plot, this moth here under the yellow tape 10 00:00:34,226 --> 00:00:36,696 from the Harvard Mark II computer, the successor 11 00:00:36,696 --> 00:00:39,476 to the thing that's in the science center back in the 1940s 12 00:00:39,476 --> 00:00:40,696 and an annotation was made 13 00:00:40,756 --> 00:00:44,626 that this was indeed the first known bug literally to be found 14 00:00:44,626 --> 00:00:46,696 in a computer and apparently the term stuck. 15 00:00:46,936 --> 00:00:49,606 Next time you're down in D.C. you can actually see that bug 16 00:00:49,666 --> 00:00:51,746 in our own Smithsonian so realize 17 00:00:51,746 --> 00:00:54,976 that it has entered our own nation's history here. 18 00:00:55,546 --> 00:00:59,106 But, your own bugs are perhaps a little less, 19 00:00:59,216 --> 00:01:03,446 less worthy of enshrining but you will get better this week 20 00:01:03,446 --> 00:01:06,156 and beyond at tackling these new topics. 21 00:01:06,156 --> 00:01:09,146 So a very rapid review of some of these things actually looked 22 00:01:09,146 --> 00:01:11,906 at last week and then we'll dive in to some actual examples. 23 00:01:11,906 --> 00:01:15,246 So this was representative of a pair of Boolean expressions 24 00:01:15,246 --> 00:01:16,506 and softball question. 25 00:01:16,506 --> 00:01:19,226 The double vertical bar is just to know what type of Boolean. 26 00:01:20,056 --> 00:01:22,506 So oaring two things together, so either the thing 27 00:01:22,506 --> 00:01:24,996 on the left has to be true or the thing on the right 28 00:01:24,996 --> 00:01:27,646 and if so, then you will, "Do this". 29 00:01:27,856 --> 00:01:30,766 This by contrast was a Boolean and which meant both left 30 00:01:30,766 --> 00:01:32,336 and right had to be true. 31 00:01:32,506 --> 00:01:34,096 We saw similar constructs in scratch 32 00:01:34,486 --> 00:01:36,446 and then we introduced conditions as well. 33 00:01:36,446 --> 00:01:39,376 So you can have one fork in the road, two, three, four, 34 00:01:39,576 --> 00:01:43,136 any number really but if you kind of extrapolate mentally 35 00:01:43,136 --> 00:01:44,776 from some sample code like this, 36 00:01:44,966 --> 00:01:47,586 you can imagine perhaps this getting a little unwieldy once 37 00:01:47,586 --> 00:01:51,696 you have five or six or seven or so forth conditions. 38 00:01:51,906 --> 00:01:54,206 So it turns out there's another alternative syntax 39 00:01:54,456 --> 00:01:56,966 that functionally achieves exactly the same thing 40 00:01:56,966 --> 00:01:59,656 and this indeed is consistent with this reality in programming 41 00:01:59,656 --> 00:02:01,886 that you can do almost anything in multiple ways. 42 00:02:01,886 --> 00:02:04,336 This is the so-called switch statements and we'll see 43 00:02:04,336 --> 00:02:07,716 that it's actually a bit more limited than if else if else 44 00:02:07,716 --> 00:02:12,566 if because everyone of the cases can only as well see be an inch 45 00:02:12,566 --> 00:02:13,516 or a charr essentially. 46 00:02:13,516 --> 00:02:16,366 You don't want to use other data types but it simply allows you 47 00:02:16,366 --> 00:02:19,586 to enumerate in a slightly more human friendly way several 48 00:02:19,586 --> 00:02:21,686 different cases that you might want to apply. 49 00:02:21,996 --> 00:02:24,896 And then lastly last time we looked at loops and we looked 50 00:02:24,896 --> 00:02:27,276 up perhaps the most syntactically complex one here, 51 00:02:27,486 --> 00:02:28,256 this four loop. 52 00:02:28,506 --> 00:02:30,926 And if you looked up during the music a bit ago, 53 00:02:30,926 --> 00:02:34,756 you'll see that Foxtrot cartoon which actually was playing off 54 00:02:34,756 --> 00:02:37,416 of this idea before loop, before loop has three segments 55 00:02:37,416 --> 00:02:38,936 where you can initialize some variables, 56 00:02:39,206 --> 00:02:41,916 a condition that you check every iteration and then one 57 00:02:41,916 --> 00:02:44,936 or more updates that happens at the end of each iteration. 58 00:02:44,936 --> 00:02:47,496 But we also have while loops which are little simpler 59 00:02:47,496 --> 00:02:48,796 but you need some way of breaking 60 00:02:48,796 --> 00:02:51,626 out of them potentially, so we'll see examples of those. 61 00:02:51,916 --> 00:02:53,546 And then lastly, and this is the one 62 00:02:53,546 --> 00:02:55,706 where maybe your curiosity should be peeked just a little 63 00:02:55,706 --> 00:02:59,156 bit that might come in handy with in particular with Pset one 64 00:02:59,396 --> 00:03:01,966 where you want to do something and then maybe do 65 00:03:01,966 --> 00:03:04,356 that same thing again and again and again. 66 00:03:04,356 --> 00:03:07,096 For instance this is a very common snippet of code to use. 67 00:03:07,306 --> 00:03:09,276 If you want to prompt the user for some inputs 68 00:03:09,486 --> 00:03:10,476 and they might be difficult. 69 00:03:10,516 --> 00:03:12,446 They might type in a negative number. 70 00:03:12,446 --> 00:03:14,796 They might type in "monkey" as I did last week and you want 71 00:03:14,796 --> 00:03:18,016 to again, again and again tell them to do something 72 00:03:18,016 --> 00:03:20,576 until they have satisfied some conditions. 73 00:03:20,576 --> 00:03:23,416 So we'll see this construct in action as well. 74 00:03:23,946 --> 00:03:27,316 So without further ado, let me go ahead and pull 75 00:03:27,316 --> 00:03:33,126 up the CS50 appliance here and this is a buggy 1.C program 76 00:03:33,126 --> 00:03:35,236 so if you're following along at home or on your laptops, 77 00:03:35,286 --> 00:03:37,586 this is included in today's PDF and .zip file 78 00:03:37,866 --> 00:03:39,746 but this is indeed a buggy program. 79 00:03:39,746 --> 00:03:42,216 It's supposed to print out 10 stars on the screen. 80 00:03:43,286 --> 00:03:45,906 But why does it not in fact print 10 stars 81 00:03:45,906 --> 00:03:47,166 and what does it actually print? 82 00:03:48,136 --> 00:03:48,406 >> Eleven. 83 00:03:48,406 --> 00:03:49,506 >> Okay so 11. 84 00:03:49,556 --> 00:03:51,166 So the consensus is 11, why is that? 85 00:03:51,436 --> 00:03:52,826 So this is the sort of mistake 86 00:03:52,886 --> 00:03:54,766 that should hopefully get easier to spot. 87 00:03:54,906 --> 00:03:57,416 We're starting counting at zero and that sort of good forms, 88 00:03:57,416 --> 00:03:58,596 they're not strictly required. 89 00:03:58,816 --> 00:04:01,876 The problem is though that we're counting up to an equal to 10. 90 00:04:02,156 --> 00:04:03,716 So if you just start counting on your fingers 91 00:04:03,716 --> 00:04:05,876 that means this iterate this loop is going to iterate 92 00:04:05,926 --> 00:04:08,486 for I=zero, one, two, three, four, five, six, 93 00:04:08,536 --> 00:04:10,706 seven, eight, nine, 10. 94 00:04:11,016 --> 00:04:12,736 So that's actually my 11th finger 95 00:04:12,736 --> 00:04:14,896 and so that's indeed one too many star. 96 00:04:14,896 --> 00:04:16,376 So an easy fix here might be what? 97 00:04:17,766 --> 00:04:19,066 Just make it less than 10. 98 00:04:19,066 --> 00:04:21,696 You could just delete this if you really wanted to, 99 00:04:21,696 --> 00:04:23,016 you could do something like this 100 00:04:23,146 --> 00:04:25,886 but again the former is probably preferred among programmers is 101 00:04:25,926 --> 00:04:28,116 to by convention start at zero. 102 00:04:28,116 --> 00:04:30,536 All right so that's buggy 1.c. Let me go ahead 103 00:04:30,536 --> 00:04:32,176 and open up another one here. 104 00:04:32,386 --> 00:04:35,036 This one is supposed to print 10 stars, 105 00:04:35,266 --> 00:04:37,736 one per line but it does not. 106 00:04:38,226 --> 00:04:39,046 What's the bug here? 107 00:04:39,046 --> 00:04:39,113 [ Inaudible Remark ] 108 00:04:39,113 --> 00:04:47,846 >> The line, the line break needs to be in the colon star. 109 00:04:48,046 --> 00:04:51,376 >> Exactly, so the line break needs to either the inside 110 00:04:51,726 --> 00:04:55,876 of the first printf here so that it actually gets applied again 111 00:04:55,876 --> 00:04:57,266 and again after every star 112 00:04:57,456 --> 00:05:00,096 because even though I've been dented in this nice pretty way, 113 00:05:00,286 --> 00:05:01,866 remember that any time you have a condition 114 00:05:01,866 --> 00:05:03,176 like a four statements 115 00:05:03,366 --> 00:05:05,926 or a while statements or an if statements. 116 00:05:06,136 --> 00:05:08,756 Id you have one line of code, you don't need the curly braces, 117 00:05:08,756 --> 00:05:10,596 if you have two or more, you do. 118 00:05:10,596 --> 00:05:12,786 So it does not matter how nicely you indent things, 119 00:05:12,846 --> 00:05:15,346 you could also fix this problem by including 120 00:05:15,346 --> 00:05:18,586 that curly brace there, another one here, for good style. 121 00:05:18,586 --> 00:05:21,046 Let me go ahead and move the cursor back where it belongs. 122 00:05:21,266 --> 00:05:23,636 And this too would work, so again another example 123 00:05:23,636 --> 00:05:24,726 of two different ways to do ti. 124 00:05:24,786 --> 00:05:26,006 Which one might you prefer? 125 00:05:26,576 --> 00:05:28,976 Yeah, the one-liner is kind of nice 126 00:05:29,036 --> 00:05:30,726 to be honest putting the backslash and right 127 00:05:30,726 --> 00:05:32,746 after the star because you don't need the curly braces, 128 00:05:32,746 --> 00:05:34,296 you don't need the second line of code, you don't need 129 00:05:34,296 --> 00:05:36,496 to call printf sort of foolishly twice 130 00:05:36,496 --> 00:05:39,276 and so there's multiple ways again to do something like this. 131 00:05:39,416 --> 00:05:43,776 All right, how about one more here, so let me go ahead 132 00:05:43,776 --> 00:05:47,456 and open up buggy 3 and in this case, 133 00:05:47,976 --> 00:05:50,606 I am going to just defer this because I was not suppose 134 00:05:50,606 --> 00:05:52,326 to open buggy 3 at this point in the story. 135 00:05:52,746 --> 00:05:54,246 So we'll come buck to buggy 3 there. 136 00:05:54,676 --> 00:05:57,126 So buggy 3, it's going to be pretty damn interesting. 137 00:05:57,196 --> 00:06:00,606 So let me go ahead instead and open up something else just 138 00:06:00,606 --> 00:06:01,976 as a sort of warm up here. 139 00:06:02,066 --> 00:06:05,476 And again, raise your hands or look confused as visibly 140 00:06:05,476 --> 00:06:07,956 as you can if you have any questions and we'll pause 141 00:06:07,956 --> 00:06:09,176 or slow down at any point. 142 00:06:09,466 --> 00:06:10,916 So this is a really stupid program. 143 00:06:10,916 --> 00:06:12,086 It computes the total 144 00:06:12,086 --> 00:06:14,686 of two numbers mathematically but that's it. 145 00:06:14,686 --> 00:06:16,556 And I point this out because one, 146 00:06:16,556 --> 00:06:18,106 it's actually syntactically correct, 147 00:06:18,106 --> 00:06:19,456 but when I try to compile this. 148 00:06:19,756 --> 00:06:21,446 I'm not sure GCC is gonna like it. 149 00:06:21,446 --> 00:06:25,446 So notice the familiar now includes standard IO.h. Now 150 00:06:25,446 --> 00:06:26,696 technically I don't even need 151 00:06:26,696 --> 00:06:28,486 that because I'm not using printf here 152 00:06:28,486 --> 00:06:30,176 so this really is a useless program. 153 00:06:30,516 --> 00:06:32,346 So I've got int main void 154 00:06:32,346 --> 00:06:34,226 and that's just copy pasted from last week. 155 00:06:34,496 --> 00:06:38,416 Now I'm declaring x as one, y as two and z is x plus y. 156 00:06:38,646 --> 00:06:40,576 So that actually looks pretty reasonable. 157 00:06:40,786 --> 00:06:43,366 I'm declaring variables on the left and assigning them values 158 00:06:43,366 --> 00:06:47,696 on the right but let me go into this directory and I'm going 159 00:06:47,696 --> 00:06:50,496 to go ahead and type make math 1 160 00:06:50,996 --> 00:06:53,076 and unfortunately GCC is yelling at me. 161 00:06:53,106 --> 00:06:57,366 Error, unused variable Z than some crazy looking dash W thing, 162 00:06:57,366 --> 00:06:58,886 all warnings being treated as errors. 163 00:06:59,116 --> 00:07:00,376 But what's going on here? 164 00:07:01,206 --> 00:07:02,146 Yeah 165 00:07:02,146 --> 00:07:02,916 [ Inaudible Remark ] 166 00:07:02,916 --> 00:07:06,296 >> Yeah exactly. 167 00:07:06,296 --> 00:07:08,966 I may very well have computed Z and it's correct. 168 00:07:09,186 --> 00:07:11,506 It's the sum of x and y but what was the point 169 00:07:11,506 --> 00:07:12,436 of this whole exercise? 170 00:07:12,666 --> 00:07:15,896 So one of the features of GCC that we've enabled by default 171 00:07:15,896 --> 00:07:18,406 and the interests of pushing you toward correct code 172 00:07:18,756 --> 00:07:21,356 as well designed code as possible is to yell at you 173 00:07:21,356 --> 00:07:23,116 when you do something that's just a waste of time. 174 00:07:23,116 --> 00:07:25,986 If you're not actually gonna use z as you might, 175 00:07:25,986 --> 00:07:27,556 by using something like printf. 176 00:07:27,836 --> 00:07:30,936 Printf presents what would it be for an integer? 177 00:07:30,936 --> 00:07:31,066 >> D. 178 00:07:31,706 --> 00:07:34,526 >> Present D for an integer may be backslash n for good measure. 179 00:07:34,736 --> 00:07:37,066 Now this might make the compiler more happy 180 00:07:37,246 --> 00:07:39,026 because now I'm not only declaring z, 181 00:07:39,186 --> 00:07:41,716 I'm using z even though the program itself is 182 00:07:41,716 --> 00:07:42,536 still underwhelming. 183 00:07:42,746 --> 00:07:45,496 But if I now go back down here, let me zoom in 184 00:07:45,706 --> 00:07:50,296 and rerun make math 1 now it's indeed happy and if I go ahead 185 00:07:50,296 --> 00:07:53,176 and run math 1 I get back the answer three which is 186 00:07:53,176 --> 00:07:54,476 of course one plus two. 187 00:07:54,746 --> 00:07:57,086 So just to point out this error message that even 188 00:07:57,086 --> 00:07:58,316 when you see things at first glance 189 00:07:58,316 --> 00:08:00,666 that look a little cryptic just read into it at least the part 190 00:08:00,666 --> 00:08:02,636 that frankly does not look overwhelming and see 191 00:08:02,636 --> 00:08:04,276 if you can infer where the error is. 192 00:08:04,516 --> 00:08:06,496 In fact if I go ahead and break this again 193 00:08:06,806 --> 00:08:10,596 and delete the printf, let me zoom in and recompile math 1, 194 00:08:10,866 --> 00:08:12,986 notice that it's actually being a little more helpful 195 00:08:12,986 --> 00:08:14,976 than spitting out this message here. 196 00:08:14,976 --> 00:08:19,326 Unused variable z yes, but it's actually telling me GCC that's 197 00:08:19,326 --> 00:08:21,446 in math 1.c that I screwed up. 198 00:08:21,746 --> 00:08:24,986 It specifically at line 19 199 00:08:24,986 --> 00:08:27,376 and then somewhat less usefully specifically, 200 00:08:27,376 --> 00:08:29,516 look at character number 9, give or take, 201 00:08:29,516 --> 00:08:31,296 so nine characters into that row. 202 00:08:31,526 --> 00:08:33,876 So it's actually pointing you toward what's probably 203 00:08:34,226 --> 00:08:34,736 the error. 204 00:08:34,916 --> 00:08:37,106 Now realize this isn't a golden rule. 205 00:08:37,106 --> 00:08:39,256 If you actually have multiple errors, 206 00:08:39,546 --> 00:08:41,476 you might actually be informed that you've got an error 207 00:08:41,476 --> 00:08:44,156 at line 15 when actually it's on line 14 208 00:08:44,216 --> 00:08:45,896 because you've done something wrong above it. 209 00:08:46,166 --> 00:08:48,926 So sometimes the compiler can get confused but again look 210 00:08:48,926 --> 00:08:52,666 for these clues as you try to diagnose these initial bugs. 211 00:08:53,356 --> 00:08:55,616 All right so let me open up math 2, 212 00:08:55,946 --> 00:08:57,616 which is now trying to do a bit more. 213 00:08:57,616 --> 00:09:02,016 So in math 2.c I actually am fixing this problem. 214 00:09:02,666 --> 00:09:03,616 So we already did that. 215 00:09:03,616 --> 00:09:05,396 So you have a copy of the correct code there. 216 00:09:05,676 --> 00:09:09,576 In math's 3.c, we're gonna go ahead now 217 00:09:09,576 --> 00:09:10,716 and use a floating point. 218 00:09:10,716 --> 00:09:12,626 But this two is actually a little broken. 219 00:09:13,266 --> 00:09:14,386 So let's take a look at the code. 220 00:09:14,386 --> 00:09:17,506 I've got standard IO for the sake of printf in main void 221 00:09:17,506 --> 00:09:20,656 so that's void our plates and now I'm doing float answers 222 00:09:20,656 --> 00:09:22,716 so a float just as a recap, it's how many bits? 223 00:09:22,856 --> 00:09:24,266 >> Thirty-two. 224 00:09:24,266 --> 00:09:25,146 >> So it's 32 bits. 225 00:09:25,146 --> 00:09:27,376 It's not the biggest possible floating point value 226 00:09:27,376 --> 00:09:30,266 but 32 bits is still a lot surely we can represent 227 00:09:30,266 --> 00:09:31,566 something as relatively small. 228 00:09:31,566 --> 00:09:34,656 It's 17 divided by 13 which should be one point something. 229 00:09:34,916 --> 00:09:36,616 You would like to think that 32 bits is enough 230 00:09:36,666 --> 00:09:38,426 to represent the number one, give or take, 231 00:09:38,776 --> 00:09:41,556 but then I go ahead and divide 17 by 3 here. 232 00:09:41,786 --> 00:09:44,566 I store the results in answer but I'm 233 00:09:44,866 --> 00:09:46,886 in printing it out dot 2f. 234 00:09:46,886 --> 00:09:48,386 The dot 2 denotes what? 235 00:09:48,386 --> 00:09:49,116 >> Two decimal places. 236 00:09:49,116 --> 00:09:50,136 >> So just two decimal places. 237 00:09:50,306 --> 00:09:52,916 Percent f generally means give me a floating point value 238 00:09:52,916 --> 00:09:55,236 but point two means only show me two numbers 239 00:09:55,236 --> 00:09:56,256 after the decimal point. 240 00:09:56,456 --> 00:09:57,716 What am I plugging an answer? 241 00:09:58,016 --> 00:09:59,476 So this actually feels okay. 242 00:09:59,476 --> 00:10:02,306 But let me go down here now and let me zoom in 243 00:10:02,966 --> 00:10:04,976 and run make math three. 244 00:10:05,046 --> 00:10:08,736 >> It does compile okay but when I now run math three, 245 00:10:09,916 --> 00:10:14,366 I'm pretty sure 17 divided by 13 is not 1.00. 246 00:10:14,416 --> 00:10:16,426 Frankly I don't quite remember exactly what it is. 247 00:10:16,426 --> 00:10:18,176 Pretty sure it's not 1.00. 248 00:10:18,696 --> 00:10:19,986 So what's going on here? 249 00:10:20,526 --> 00:10:21,346 What's the problem? 250 00:10:21,586 --> 00:10:25,656 >> We divide the two integers [inaudible]. 251 00:10:26,276 --> 00:10:28,496 >> Yeah so this is something we haven't talked about explicitly 252 00:10:28,496 --> 00:10:31,286 but indeed, when you divide two integers 253 00:10:31,326 --> 00:10:32,696 in C, what do you get back. 254 00:10:33,006 --> 00:10:35,566 Well you're gonna get back an integer by nature 255 00:10:35,566 --> 00:10:40,026 of mathematical operations in C. So 17 is an integer. 256 00:10:40,066 --> 00:10:41,816 Why? Well it's obviously an integer 257 00:10:41,816 --> 00:10:44,516 but more specifically it does not have a decimal oint in it. 258 00:10:44,516 --> 00:10:46,966 So C views 17 as an int. 259 00:10:47,746 --> 00:10:50,796 13 meanwhile also looks like an integer. 260 00:10:50,796 --> 00:10:53,246 There is no decimal point so it too is treated as an int. 261 00:10:53,246 --> 00:10:54,906 And that's actually a bit of a white lie. 262 00:10:54,906 --> 00:10:57,406 It's treated as along typically but the results are the same. 263 00:10:57,406 --> 00:11:00,006 It is not considered a floating point value. 264 00:11:00,286 --> 00:11:03,186 So 17 divided by three gives me one point something. 265 00:11:03,336 --> 00:11:06,336 Well that something can't fit into an integer. 266 00:11:06,336 --> 00:11:09,846 It can't fit into an int and so the point something, something, 267 00:11:10,136 --> 00:11:11,056 it's just thrown away. 268 00:11:11,606 --> 00:11:13,896 This is called-- this was called truncation. 269 00:11:14,036 --> 00:11:16,046 So the number is just truncated and you lose that. 270 00:11:16,296 --> 00:11:18,236 So even though I'm then taking the answer 271 00:11:18,236 --> 00:11:20,696 and I'm saying it is afloat, it's too late, 272 00:11:20,946 --> 00:11:22,756 you've already thrown away everything 273 00:11:22,756 --> 00:11:25,276 after the decimal point when you did the original division. 274 00:11:25,846 --> 00:11:27,886 So if the problem then is that answer, 275 00:11:27,976 --> 00:11:31,156 even though I'm calling it afloat is actually storing the 276 00:11:31,156 --> 00:11:34,226 result of an integer divided by an integer which-- 277 00:11:34,226 --> 00:11:36,176 whose results is an integer, how do I fix this? 278 00:11:36,956 --> 00:11:39,266 Yeah in the back? 279 00:11:39,266 --> 00:11:40,696 [ Inaudible Remark ] 280 00:11:40,696 --> 00:11:43,716 >> Yeah so I can actually introduce this other feature 281 00:11:43,716 --> 00:11:47,836 of C known as typecasting or more-- so simply to cast and-- 282 00:11:47,936 --> 00:11:51,716 a variable from one data type to another is to cast it 283 00:11:52,026 --> 00:11:54,686 so what you can do is what simple parenthesis, 284 00:11:54,686 --> 00:11:58,296 you can do something like this, yes, 13 is actually written 285 00:11:58,296 --> 00:12:01,246 as an integer but I would like the compiler to treat it 286 00:12:01,296 --> 00:12:02,446 as though it's afloat. 287 00:12:02,526 --> 00:12:04,066 And this is actually legitimate 288 00:12:04,066 --> 00:12:06,646 because you're not converting apples to oranges. 289 00:12:06,646 --> 00:12:09,846 You can reasonably convert an integer to afloat, 290 00:12:09,846 --> 00:12:13,016 you just add a decimal point, essentially point 0000000. 291 00:12:13,276 --> 00:12:18,826 So now this is effectively 17 divided by 13.00000 or be it 292 00:12:18,826 --> 00:12:19,776 with some imprecision. 293 00:12:20,006 --> 00:12:22,606 So now when the division happens, I get back afloat 294 00:12:22,606 --> 00:12:26,336 on storing that float in a float called answer and so let's see 295 00:12:26,336 --> 00:12:29,066 if my answer is now any better to make sure to save always 296 00:12:29,066 --> 00:12:30,996 with control S or the file menu. 297 00:12:30,996 --> 00:12:33,696 Let me go ahead and recompile math 3. 298 00:12:33,996 --> 00:12:35,736 No errors, that's good and let me go ahead 299 00:12:35,736 --> 00:12:37,516 and rerun math three enter. 300 00:12:37,926 --> 00:12:39,966 Ah, that's the number I was hoping for. 301 00:12:40,216 --> 00:12:42,816 So this might be sort of low level detail 302 00:12:42,816 --> 00:12:45,606 but this is absolutely gonna be important even if he set one 303 00:12:45,886 --> 00:12:48,796 where you're for instance trying to provide a user with change 304 00:12:48,896 --> 00:12:52,466 and you're dividing some total amount by 25 cents or 10 cents 305 00:12:52,466 --> 00:12:53,266 or something like that. 306 00:12:53,516 --> 00:12:56,146 You're gonna need to beware, be mindful of this kinds of issues 307 00:12:56,326 --> 00:12:58,976 that you might illiberally be throwing pennies away. 308 00:12:59,066 --> 00:13:01,126 In fact if you think now, if you ever see 309 00:13:01,126 --> 00:13:04,326 in the classic office space or the precursor superman three, 310 00:13:04,326 --> 00:13:06,956 you might recall that the scam that they waged 311 00:13:06,956 --> 00:13:09,406 in office space was to kind of skim the fractions 312 00:13:09,406 --> 00:13:11,756 of pennies off of the dollars that we're going 313 00:13:11,756 --> 00:13:13,086 through Inatech, the company. 314 00:13:13,316 --> 00:13:14,756 Well this actually relates, right? 315 00:13:14,756 --> 00:13:16,936 Those guys were programmers and what they were referring 316 00:13:16,936 --> 00:13:19,466 to was one, this inherent imprecision 317 00:13:19,686 --> 00:13:22,936 of floating point values that we saw last week and also the fact 318 00:13:22,936 --> 00:13:25,596 that sometimes these remainders are just potentially 319 00:13:26,016 --> 00:13:26,836 thrown away. 320 00:13:26,836 --> 00:13:28,866 If you've never seen this movie, I do recommend. 321 00:13:30,026 --> 00:13:30,316 Yeah? 322 00:13:30,316 --> 00:13:31,296 [ Inaudible Question ] 323 00:13:31,296 --> 00:13:33,896 >> Good question. 324 00:13:33,896 --> 00:13:37,386 Why do you have to cast the 13 as afloat and not the 17? 325 00:13:37,566 --> 00:13:40,846 Well first, it suffices to only due one of them so that's fine 326 00:13:40,846 --> 00:13:42,326 and notice what happens here. 327 00:13:42,326 --> 00:13:46,786 If I do this and save then recompile with make math 3 328 00:13:46,996 --> 00:13:49,486 and then rerun so that actually would fix it as well. 329 00:13:49,486 --> 00:13:51,266 I just chose one arbitrarily. 330 00:13:51,266 --> 00:13:53,456 I could do two and frankly there's an even 331 00:13:53,506 --> 00:13:54,416 simpler approach. 332 00:13:54,416 --> 00:13:57,486 You could certainly just avoid this whole new concept afloat 333 00:13:57,636 --> 00:13:59,516 of casting rather and just say fine. 334 00:13:59,516 --> 00:14:02,456 Make it 13.0 and make this 17.0. 335 00:14:02,456 --> 00:14:03,806 That might work as well. 336 00:14:04,596 --> 00:14:04,686 Yeah? 337 00:14:04,686 --> 00:14:06,496 [ Inaudible Question ] 338 00:14:06,496 --> 00:14:08,726 >>So you can it looks a little weird 339 00:14:08,726 --> 00:14:09,956 but you could also do this. 340 00:14:10,056 --> 00:14:11,826 See it's perfectly content with that if you just 341 00:14:11,826 --> 00:14:13,686 like the elegance of no trailing zeros. 342 00:14:14,636 --> 00:14:16,556 Any other questions on floats? 343 00:14:17,246 --> 00:14:17,966 Yeah? 344 00:14:17,966 --> 00:14:22,416 >> We have done the typecasting, so is answer a float or an int? 345 00:14:22,416 --> 00:14:24,976 >> It's a good question so before we did the typecasting. 346 00:14:25,026 --> 00:14:27,156 Is float an answer or an int? 347 00:14:27,156 --> 00:14:29,956 It is a float because we explicitly made it a float 348 00:14:30,406 --> 00:14:32,846 but it was assigned an integer value 349 00:14:32,846 --> 00:14:35,136 so what happens there is actually called implicit, 350 00:14:35,136 --> 00:14:37,516 it's casting or implicit conversion. 351 00:14:37,876 --> 00:14:39,666 So it is indeed a float 352 00:14:39,666 --> 00:14:42,666 and conceptionally the point zero is just append to it 353 00:14:42,666 --> 00:14:45,296 when the equal sign moves the value from right to left. 354 00:14:45,746 --> 00:14:46,786 Any question over here? 355 00:14:46,786 --> 00:14:47,546 Yeah? 356 00:14:47,546 --> 00:14:48,606 >> Both of them need decimal places? 357 00:14:49,116 --> 00:14:51,076 >> No, only one of them needs decimal places, 358 00:14:51,076 --> 00:14:55,146 so essentially the sort of more powerful number takes control. 359 00:14:55,146 --> 00:14:57,666 So while floating point number with precision 360 00:14:57,666 --> 00:15:00,796 with the decimal point sort of supersedes the weakness 361 00:15:00,796 --> 00:15:03,036 of an int and so you just need just one of them. 362 00:15:03,246 --> 00:15:04,686 So again, the takeaway here is not 363 00:15:04,686 --> 00:15:07,356 that the next time you divide one constant value by another, 364 00:15:07,356 --> 00:15:08,286 you should be mindful of this. 365 00:15:08,496 --> 00:15:10,626 But rather when you're taking arbitrary user input, 366 00:15:10,626 --> 00:15:13,006 maybe dollars and cents, these are the kinds 367 00:15:13,006 --> 00:15:14,246 of issues you need to be careful of. 368 00:15:14,246 --> 00:15:15,416 And certainly in the real world, 369 00:15:15,416 --> 00:15:18,026 if you're the person implementing the next automated 370 00:15:18,026 --> 00:15:20,056 teller machine, these are absolutely the kinds 371 00:15:20,056 --> 00:15:23,696 of issues you need to be mindful of when dealing with any kind 372 00:15:23,696 --> 00:15:25,816 of currency and fractions thereof. 373 00:15:26,266 --> 00:15:29,696 Alright, so let's transition now to this file here. 374 00:15:29,696 --> 00:15:32,396 This is a file called nonswitch.c, 375 00:15:32,576 --> 00:15:35,016 just to convey the idea that's it's actually not using 376 00:15:35,016 --> 00:15:35,686 a switch. 377 00:15:35,976 --> 00:15:37,806 Let me scroll down just a little bit here and see 378 00:15:37,806 --> 00:15:39,136 if we can't read through this program. 379 00:15:39,136 --> 00:15:42,366 At the very top, I'm using the CS50 library, standard I/O, 380 00:15:42,366 --> 00:15:44,516 so that's a hint that I'm probably using printf, 381 00:15:44,516 --> 00:15:48,076 I'm probably using get int or get string, something from CS50. 382 00:15:48,076 --> 00:15:49,996 And now here is my main routine. 383 00:15:50,316 --> 00:15:52,506 And the comments kind of reveal what's going on here. 384 00:15:52,766 --> 00:15:55,136 So in that first line, I'm asking the user for an integer. 385 00:15:55,446 --> 00:15:58,806 Notice that just aesthetically, I'm not using a backslash N 386 00:15:58,876 --> 00:15:59,756 at the end of the line. 387 00:15:59,956 --> 00:16:01,616 This is purely a stylistic decision. 388 00:16:01,616 --> 00:16:03,396 I just want the cursor to blink to the right 389 00:16:03,506 --> 00:16:05,356 of the prompt rather than put it on a new line. 390 00:16:05,426 --> 00:16:06,726 But that's not strictly necessary. 391 00:16:07,076 --> 00:16:09,476 The next line we use last week ints and gets GetInt. 392 00:16:10,106 --> 00:16:12,596 So it's relatively straightforward now hopefully. 393 00:16:12,596 --> 00:16:15,066 In future weeks, we'll actually look inside of GetInt 394 00:16:15,066 --> 00:16:16,026 as to how it's working. 395 00:16:16,316 --> 00:16:18,086 But for now, it's your-- how-- 396 00:16:18,086 --> 00:16:21,526 welcome to be blissfully content just assuming it gets an int 397 00:16:21,646 --> 00:16:22,756 and hands it back to you. 398 00:16:23,126 --> 00:16:26,596 So now we're finally using Boolean expressions in C 399 00:16:26,596 --> 00:16:29,966 that are actually using the and expression here. 400 00:16:30,196 --> 00:16:32,816 So in short, this seems to be a simple program that says, 401 00:16:32,996 --> 00:16:37,406 if the user inputted a number that's between 1 and 3, go ahead 402 00:16:37,676 --> 00:16:40,476 and you tell them, you picked a small number. 403 00:16:40,476 --> 00:16:43,066 Else-if it's between 4 and 6, you pick the medium number, 404 00:16:43,066 --> 00:16:46,056 else if you pick 7 through 10, it's a big number 405 00:16:46,116 --> 00:16:49,046 and anything else, negative 0 over something huge, 406 00:16:49,246 --> 00:16:51,306 it's gonna say you picked an invalid number. 407 00:16:51,566 --> 00:16:54,956 So here too, just to point out in FAQ were a common mistake. 408 00:16:55,526 --> 00:16:57,926 Realize that when you end two things together, 409 00:16:58,306 --> 00:17:01,946 you don't necessarily want to say for instance, 410 00:17:01,996 --> 00:17:08,016 if N is greater than 5, and end N is less than 4, do this, 411 00:17:08,796 --> 00:17:10,316 'cause if you just think through that logically, 412 00:17:10,316 --> 00:17:12,886 make sure that the two ranges you're comparing are actually 413 00:17:12,886 --> 00:17:16,456 inclusive and not something that's greater than 5 and less 414 00:17:16,456 --> 00:17:18,526 than 4, which is obviously impossible. 415 00:17:18,616 --> 00:17:19,686 So just think about it. 416 00:17:19,686 --> 00:17:20,936 If you're ever running into a bug 417 00:17:20,936 --> 00:17:22,976 where you're using these Boolean expressions, 418 00:17:22,976 --> 00:17:26,206 just consider what your actual logic might be. 419 00:17:26,676 --> 00:17:28,866 So this is starting to feel a little ugly, right? 420 00:17:28,866 --> 00:17:31,386 I've got 4 different conditions, it's a little hard to read 421 00:17:31,386 --> 00:17:33,796 with all the ands and the greater than, equal signs. 422 00:17:34,036 --> 00:17:36,596 And just to be clear to express the notion of greater than 423 00:17:36,596 --> 00:17:40,266 or equal, in a-- on paper, you would say, angle bracket 424 00:17:40,266 --> 00:17:41,276 and then the line under it, 425 00:17:41,486 --> 00:17:42,936 there's no such key on the keyboard. 426 00:17:43,176 --> 00:17:46,686 So the convention is angled bracket and then an equal sign. 427 00:17:46,886 --> 00:17:48,556 Two separate characters with no space. 428 00:17:49,186 --> 00:17:55,546 So let me improve this, debatably, with a switch.c, 429 00:17:56,156 --> 00:18:01,936 nonswtich-- switch-- let me go ahead 430 00:18:01,936 --> 00:18:03,436 and improve this as follows. 431 00:18:04,046 --> 00:18:08,436 So rather than do this, I could actually say switch 432 00:18:08,436 --> 00:18:10,156 and then what do you wanna switch on? 433 00:18:10,156 --> 00:18:11,846 Well, whatever the value of N is, 434 00:18:12,286 --> 00:18:14,126 and now I've got some placeholder text here 435 00:18:14,126 --> 00:18:15,376 and what I can start to do is say, 436 00:18:15,376 --> 00:18:17,306 alright, in case 1, do this. 437 00:18:17,306 --> 00:18:19,816 And then I'm gonna steal my text from down here, 438 00:18:20,316 --> 00:18:24,136 you pick a small number, and then I'm actually gonna do this. 439 00:18:24,286 --> 00:18:25,406 I need to say break. 440 00:18:25,956 --> 00:18:28,616 But then, I can do this-- oops, don't do that. 441 00:18:29,136 --> 00:18:32,696 Then I can do this, I'm gonna go up to 4, we'll come back 442 00:18:32,736 --> 00:18:33,476 to the ones in the middle. 443 00:18:33,476 --> 00:18:35,266 You picked a medium number 444 00:18:35,916 --> 00:18:38,716 and then I'll do one more for good measure. 445 00:18:39,136 --> 00:18:43,126 Case, let's see, I think it was 4, 5, 6 or 7 was the next one. 446 00:18:43,126 --> 00:18:44,486 You picked a big number. 447 00:18:44,866 --> 00:18:48,626 And then just to make sure I capture every possible scenario, 448 00:18:48,916 --> 00:18:49,866 I'm actually gonna have 449 00:18:49,866 --> 00:18:52,586 at the very bottom here what's called the default case 450 00:18:53,086 --> 00:18:57,046 where you picked an invalid number. 451 00:18:57,746 --> 00:18:59,456 And then I'm gonna close the curly brace. 452 00:18:59,496 --> 00:19:02,026 So again, we'll fix the fact that we're skipping 2, 453 00:19:02,026 --> 00:19:03,656 and 3 and 5 and 6 in just a moment. 454 00:19:03,846 --> 00:19:05,046 But this is just another syntax 455 00:19:05,046 --> 00:19:06,516 to achieve exactly the same thing. 456 00:19:06,516 --> 00:19:08,656 It doesn't have all of these angle brackets and such. 457 00:19:08,886 --> 00:19:11,176 Instead the switch statement says, look at the value of N 458 00:19:11,176 --> 00:19:13,746 and then check this list of possible cases 459 00:19:13,746 --> 00:19:16,856 and whichever case actually applies, go ahead and do 460 00:19:16,856 --> 00:19:18,586 that one and only that one. 461 00:19:18,896 --> 00:19:20,606 So I can actually now round this out. 462 00:19:20,646 --> 00:19:21,756 You can have multiple cases. 463 00:19:21,756 --> 00:19:23,706 I can say case 2 and case 3. 464 00:19:24,016 --> 00:19:30,376 Down here I can say case 5 and I can say case 6 and then lastly 465 00:19:30,376 --> 00:19:34,306 down here, I can say case 7, case 8, case 9, 466 00:19:34,646 --> 00:19:38,756 and I think I had 10 before, so case 10 and that's it. 467 00:19:38,946 --> 00:19:41,476 So arguably, this too is now starting to devolve 468 00:19:41,476 --> 00:19:43,656 in something a little messy but at least, 469 00:19:43,656 --> 00:19:45,016 it's a little more schemable. 470 00:19:45,046 --> 00:19:47,336 Rather than have to parse something visually, 471 00:19:47,586 --> 00:19:49,246 that's kind of a big jumble of text. 472 00:19:49,246 --> 00:19:51,726 It's pretty wide, it kind of-- it take-- requires more thought. 473 00:19:51,916 --> 00:19:54,006 What's nice about switch statements, just as a feature 474 00:19:54,006 --> 00:19:56,086 of the language is that bang, bang, bang, bang, bang. 475 00:19:56,086 --> 00:19:58,616 You can see what the applicable cases are. 476 00:19:58,616 --> 00:20:01,656 So just realize this is an option, but the only gotcha is 477 00:20:01,656 --> 00:20:03,776 that what you switch on generally has 478 00:20:03,836 --> 00:20:06,036 to be an int or has to be a char. 479 00:20:06,866 --> 00:20:08,276 There're a couple of other things you can use 480 00:20:08,276 --> 00:20:09,446 so it's somewhat limited. 481 00:20:09,886 --> 00:20:09,976 Yeah. 482 00:20:10,166 --> 00:20:12,166 [ Inaudible Remark ] 483 00:20:12,316 --> 00:20:16,096 >> Exactly, case 4 or 5 or 6 are all gonna do the same thing. 484 00:20:16,096 --> 00:20:17,986 They're gonna say you pick the medium number. 485 00:20:18,346 --> 00:20:21,296 But this fact, I've introduced, notice this new keyword break 486 00:20:21,296 --> 00:20:23,596 which you can actually use in a loops as well. 487 00:20:23,836 --> 00:20:25,916 Notice what happens if I don't have that break. 488 00:20:26,066 --> 00:20:27,626 So I think I got my syntax right. 489 00:20:27,626 --> 00:20:31,226 I'm gonna ahead and save this in my terminal window down here. 490 00:20:31,226 --> 00:20:32,856 I'm gonna go ahead and make, 491 00:20:33,026 --> 00:20:36,446 this is still called nonswitch even though we've changed it 492 00:20:36,446 --> 00:20:37,166 to a switch. 493 00:20:38,246 --> 00:20:40,336 Compiles okay, so that's always a good sign. 494 00:20:40,386 --> 00:20:42,686 Nonswitch, give me an integer let's say 5, 495 00:20:43,076 --> 00:20:44,016 you pick the medium number. 496 00:20:44,016 --> 00:20:45,806 Let's say 1, you pick the smaller number. 497 00:20:45,806 --> 00:20:48,436 Let's say monkey, retry. 498 00:20:48,436 --> 00:20:50,766 So that's the CS50 library catching that 0, 499 00:20:50,806 --> 00:20:52,286 that's my own program catching 500 00:20:52,286 --> 00:20:54,086 that saying it's an invalid number. 501 00:20:54,356 --> 00:20:57,086 But notice what happens if I forget this break statement 502 00:20:57,086 --> 00:20:59,266 which literary does what it says. 503 00:20:59,266 --> 00:21:01,776 Break out of this chunk of code. 504 00:21:01,776 --> 00:21:05,166 If I forget this and I forget this-- oops, not that. 505 00:21:05,566 --> 00:21:09,726 If I forget this, and this one, leaving out the breaks, 506 00:21:09,726 --> 00:21:12,376 which is easy to do and now recompile my code 507 00:21:12,376 --> 00:21:15,256 and make nonswitch and then run switch and type 508 00:21:15,256 --> 00:21:18,196 in 1, what's happened here? 509 00:21:18,336 --> 00:21:20,226 Right, this is absolutely a bug but that's 510 00:21:20,226 --> 00:21:22,246 because again C being a computer pro-- 511 00:21:22,246 --> 00:21:25,236 being a computer language needs to be told exactly what to do 512 00:21:25,236 --> 00:21:28,546 and what this case statements mean is the instant you match 513 00:21:28,816 --> 00:21:32,096 one o these cases, the codes just starts executing everything 514 00:21:32,096 --> 00:21:32,866 you've written below it, 515 00:21:33,006 --> 00:21:37,006 and only once you hit type break semicolon does it break 516 00:21:37,006 --> 00:21:38,396 out of the whole switch statement. 517 00:21:38,396 --> 00:21:40,856 So this is both an advantage sometimes and a disadvantage, 518 00:21:41,056 --> 00:21:43,426 and advantage in that we can consider multiple cases 519 00:21:43,706 --> 00:21:46,056 but a disadvantage if you leave out that break. 520 00:21:46,196 --> 00:21:50,306 Strictly, it's not necessary to have a break under default, 521 00:21:50,666 --> 00:21:52,716 but perhaps as a matter of good style and consistency, 522 00:21:52,716 --> 00:21:53,496 it's good to have it there. 523 00:21:53,496 --> 00:21:54,606 But it doesn't need to be at the bottom. 524 00:21:54,606 --> 00:21:54,806 Yeah. 525 00:21:55,516 --> 00:22:04,546 [ Inaudible Remark ] 526 00:22:05,046 --> 00:22:07,016 >> It's a good question. 527 00:22:07,016 --> 00:22:09,976 So what is it about switch that makes it the case that all 4 528 00:22:09,976 --> 00:22:11,296 of those outputs are printing? 529 00:22:11,476 --> 00:22:13,226 It's literally the definition of switch. 530 00:22:13,396 --> 00:22:14,806 When you match a case, 531 00:22:14,926 --> 00:22:17,546 start executing code until you see break. 532 00:22:17,966 --> 00:22:20,566 So the subsequent case statements are ignored. 533 00:22:20,796 --> 00:22:23,016 So the compiler is smart enough to realize, oh I've seen case, 534 00:22:23,016 --> 00:22:25,926 I've seen case, I've seen case but until it sees break, 535 00:22:25,926 --> 00:22:28,306 it's gonna keep executing everything that I happen 536 00:22:28,306 --> 00:22:29,456 to have indented here. 537 00:22:29,546 --> 00:22:31,056 So it's just the definition of the statement. 538 00:22:31,686 --> 00:22:32,326 Other questions? 539 00:22:32,546 --> 00:22:32,976 Yeah, in the back. 540 00:22:33,516 --> 00:22:39,616 [ Inaudible Remark ] 541 00:22:40,116 --> 00:22:41,356 >> Ah, good question. 542 00:22:41,416 --> 00:22:44,476 Is there a way to switch to consider a big range of numbers? 543 00:22:44,476 --> 00:22:47,216 No, you need to go back to the if condition and your use 544 00:22:47,216 --> 00:22:50,076 of greater than or less than if you actually wanna consider 545 00:22:50,076 --> 00:22:50,566 a range. 546 00:22:50,776 --> 00:22:53,346 So you wouldn't wanna use a program like this for 1 to 100 547 00:22:53,526 --> 00:22:56,036 because frankly, then this switch implementation would just 548 00:22:56,036 --> 00:22:58,056 be messier and uglier and much longer 549 00:22:58,356 --> 00:23:01,516 than the corresponding if-else approach, so again, 550 00:23:01,516 --> 00:23:02,956 just another tool for the toolkit, 551 00:23:02,956 --> 00:23:06,246 another way in which you can solve these kinds of problems. 552 00:23:06,856 --> 00:23:08,296 So let's-- let me go ahead now and open 553 00:23:08,296 --> 00:23:10,066 up one more example here. 554 00:23:10,456 --> 00:23:14,186 So this one is called positive1.c 555 00:23:14,186 --> 00:23:16,376 and it's the first version of a little program 556 00:23:16,376 --> 00:23:20,296 that actually uses that feature of a loop called do while 557 00:23:20,556 --> 00:23:23,016 to actually prompt the user again and again and again, 558 00:23:23,016 --> 00:23:24,876 especially if he or she is being difficult. 559 00:23:25,096 --> 00:23:27,666 So there's a couple of interesting nuances here though. 560 00:23:27,926 --> 00:23:31,056 So notice first, that again the program starts in a familiar way 561 00:23:31,056 --> 00:23:33,426 with these two libraries and a bunch of comments up top. 562 00:23:33,616 --> 00:23:35,736 Int main void is the same as usual. 563 00:23:35,916 --> 00:23:37,636 But now notice a couple of new things. 564 00:23:37,976 --> 00:23:41,576 So 1, do and then notice there's nothing parenthetically 565 00:23:41,576 --> 00:23:42,096 to right. 566 00:23:42,096 --> 00:23:44,096 It's just do and then open curly brace, 567 00:23:44,416 --> 00:23:46,476 because what's interesting about do while is 568 00:23:46,476 --> 00:23:48,426 that these lines are always going 569 00:23:48,426 --> 00:23:50,106 to get executed no matter what. 570 00:23:50,306 --> 00:23:51,246 So it's an opportunity 571 00:23:51,246 --> 00:23:53,986 to do something before you even check some condition. 572 00:23:54,496 --> 00:23:58,806 But at the bottom of the loop, you're only going to do it again 573 00:23:59,096 --> 00:24:00,436 if that condition checks out. 574 00:24:00,956 --> 00:24:05,706 So in other words, if the user here provides a number that's 575 00:24:05,706 --> 00:24:08,556 less than 1, I'm gonna go through the do chunk 576 00:24:08,556 --> 00:24:11,636 of code not only the first time, but again and again and again 577 00:24:11,636 --> 00:24:13,236 until the user finally cooperates 578 00:24:13,236 --> 00:24:16,966 and gives me something that is not less than 1, like 2 or 3 579 00:24:16,966 --> 00:24:20,236 or 4 or 1 itself, at which point the loop will terminate. 580 00:24:20,726 --> 00:24:23,066 And then, it's finally just gonna say thank you-- 581 00:24:23,066 --> 00:24:25,766 thanks for the 2 or whatever it is I typed in. 582 00:24:26,036 --> 00:24:27,456 So, just to be clear, let me go ahead 583 00:24:27,456 --> 00:24:28,746 and run this at the bottom here. 584 00:24:29,176 --> 00:24:32,896 So this again is positive 1, so make positive 1 585 00:24:33,636 --> 00:24:37,336 and I'll zoom in, enter, compiles okay, positive 1, 586 00:24:37,876 --> 00:24:39,936 I demand that you give me a positive integer. 587 00:24:39,936 --> 00:24:42,486 I'm gonna give it negative 1, it's gonna do it again, 588 00:24:42,486 --> 00:24:46,986 negative 2, negative 3, negative 100, 0 still not cooperating, 589 00:24:46,986 --> 00:24:49,866 but if I finally give it 1, thanks for the 1. 590 00:24:50,216 --> 00:24:52,566 And this is a way again of maybe in say piece 591 00:24:52,566 --> 00:24:54,596 at 1 actually doing something again and again, 592 00:24:54,906 --> 00:24:56,156 but notice what I did. 593 00:24:56,836 --> 00:24:59,956 What's kind of interesting about my declaration of N here 594 00:24:59,956 --> 00:25:01,756 that I've not done in past examples? 595 00:25:02,166 --> 00:25:04,166 [ Inaudible Remark ] 596 00:25:04,576 --> 00:25:06,266 >> Yeah, I kinda declared it up top 597 00:25:06,266 --> 00:25:08,536 and this was why I think we proposed this last Friday 598 00:25:08,536 --> 00:25:10,876 as an option but then decided uh, just do it all 599 00:25:10,876 --> 00:25:12,536 at once 'cause it's more elegant, right. 600 00:25:12,536 --> 00:25:16,016 Every example thus far we've not had int end declared way 601 00:25:16,016 --> 00:25:16,386 up there. 602 00:25:16,386 --> 00:25:17,776 We did something like this. 603 00:25:18,016 --> 00:25:19,536 But this introduces a problem now. 604 00:25:19,876 --> 00:25:23,546 So N is clearly a number that I want to use in this program. 605 00:25:23,546 --> 00:25:25,436 Not only getting it inside of the loop, 606 00:25:25,526 --> 00:25:27,046 but I wanna use it obviously in the printf 607 00:25:27,046 --> 00:25:28,586 at the very bottom of the program. 608 00:25:28,876 --> 00:25:30,316 But here is a gotcha of C, 609 00:25:30,586 --> 00:25:35,186 when you declare a variable it only exists within the confines 610 00:25:35,296 --> 00:25:37,566 of the most recently opened curly braces. 611 00:25:37,996 --> 00:25:41,006 Those curly braces define buzz word called scope. 612 00:25:41,416 --> 00:25:43,576 So int N as declared here 613 00:25:43,576 --> 00:25:45,936 between those curly braces only exists 614 00:25:45,936 --> 00:25:48,976 in your computer's memory while you're executing code 615 00:25:48,976 --> 00:25:50,566 in between those 2 curly braces. 616 00:25:50,726 --> 00:25:53,366 The moment you pop out of that loop, for instance, 617 00:25:53,366 --> 00:25:56,246 at the very last line, N is gone, the RAM, 618 00:25:56,246 --> 00:25:58,956 the memory that it was occupying, those 32 bits, 619 00:25:59,176 --> 00:26:00,496 have been taken away from you, 620 00:26:00,546 --> 00:26:03,256 reclaims for some other random purpose in the computer, 621 00:26:03,566 --> 00:26:06,026 and you no longer have access to those bits. 622 00:26:06,026 --> 00:26:08,396 And thus N is out of "scope." 623 00:26:08,396 --> 00:26:11,996 So what you'll see if you make this mistake is the following. 624 00:26:11,996 --> 00:26:13,496 Let me go ahead and recompile, 625 00:26:13,886 --> 00:26:17,336 now that I've saved this file we'd make positive 1 626 00:26:18,616 --> 00:26:21,246 and already, there's a whole bunch of errors. 627 00:26:21,246 --> 00:26:22,676 Again, this shouldn't necessarily mean you've made a 628 00:26:22,676 --> 00:26:24,256 whole lot of mistakes, you made one 629 00:26:24,506 --> 00:26:25,886 that actually has a ripple effect 630 00:26:25,886 --> 00:26:26,896 on the rest of your programs. 631 00:26:26,896 --> 00:26:27,986 So what's going on? 632 00:26:27,986 --> 00:26:30,146 So first of all, look for the first sort 633 00:26:30,146 --> 00:26:31,696 of reasonably phrase line, alright. 634 00:26:31,696 --> 00:26:34,216 So in function main, that's apparently where my issue is. 635 00:26:34,526 --> 00:26:39,656 This denotes line 22 of positive 1.c unused variable N. 636 00:26:39,866 --> 00:26:43,426 So this is the error we saw before and it is unused now 637 00:26:43,426 --> 00:26:46,366 because I'm declaring N inside those curly braces. 638 00:26:46,806 --> 00:26:47,916 But then, I'm not using it, 639 00:26:47,916 --> 00:26:50,196 it's like Z in that silly example earlier. 640 00:26:50,436 --> 00:26:51,886 But there's something more here-- 641 00:26:52,126 --> 00:26:54,426 down here, then it's saying confusingly 642 00:26:54,426 --> 00:26:57,596 on line 24 which is down here. 643 00:26:57,596 --> 00:27:01,476 N undeclared, first use in this function. 644 00:27:01,886 --> 00:27:04,506 So GCC is being consistent with this story. 645 00:27:04,506 --> 00:27:07,146 The moment you exit curly braces N is gone. 646 00:27:07,506 --> 00:27:11,206 So on line 24, the bottom of the program N no longer exists 647 00:27:11,206 --> 00:27:13,876 and so the compiler recognizes this much and yells at you 648 00:27:13,876 --> 00:27:17,186 and doesn't even let you compile or run this program. 649 00:27:17,636 --> 00:27:20,496 So to just roll back, what's the fix here, what's the solution? 650 00:27:20,496 --> 00:27:21,066 [ Inaudible Remarks ] 651 00:27:21,066 --> 00:27:26,296 >> Yeah, quite simply, you declare N outside 652 00:27:26,296 --> 00:27:27,766 of the scope of that loop. 653 00:27:27,896 --> 00:27:30,076 And even though frankly, it looks, or might start 654 00:27:30,076 --> 00:27:33,696 to feel a bit ugly to kind of put this variable 655 00:27:33,696 --> 00:27:34,826 in no man's land up there. 656 00:27:35,066 --> 00:27:36,836 That is indeed the correct solution. 657 00:27:36,836 --> 00:27:39,476 It's still inside of main, so that means every part 658 00:27:39,476 --> 00:27:41,986 of main has access to it including inside 659 00:27:41,986 --> 00:27:44,176 of the do loops curly braces. 660 00:27:44,446 --> 00:27:48,036 But at least now, it's within scope for the entire function. 661 00:27:48,326 --> 00:27:51,016 Now just as a teaser technically and those of you 662 00:27:51,016 --> 00:27:52,986 with prior programming background and especially 663 00:27:52,986 --> 00:27:54,476 in Java, might be in the habit 664 00:27:54,476 --> 00:27:56,556 of putting what are called global variables. 665 00:27:56,556 --> 00:27:59,416 Where you declare variable outside of main even, 666 00:27:59,686 --> 00:28:02,486 the advantage of this as we'll see in future examples is then, 667 00:28:02,736 --> 00:28:04,736 every function you write, not just main, 668 00:28:04,736 --> 00:28:06,106 you can write your own functions just 669 00:28:06,106 --> 00:28:07,766 like you can write your own scripts and scratch. 670 00:28:08,116 --> 00:28:10,366 Every function you write will have access to N 671 00:28:10,616 --> 00:28:13,966 but this is generally considered very bad, very messy practice. 672 00:28:14,286 --> 00:28:16,156 So we'll come back to this, but for now, 673 00:28:16,156 --> 00:28:18,026 the correct solution is indeed 674 00:28:18,026 --> 00:28:21,446 to put it right inside there outside of the loop. 675 00:28:22,726 --> 00:28:22,806 Yeah. 676 00:28:23,516 --> 00:28:32,246 [ Inaudible Remark ] 677 00:28:32,746 --> 00:28:34,336 >> So you could. 678 00:28:34,336 --> 00:28:38,806 So if you wanted to have a more specific yelling at the user 679 00:28:38,806 --> 00:28:41,016 like that is negative, I don't want that, or some kind 680 00:28:41,016 --> 00:28:43,236 of informative message, absolutely you could do it. 681 00:28:43,376 --> 00:28:45,896 But you have to redo this loop. 682 00:28:46,076 --> 00:28:48,666 The advantage of do while loop is that if you're content 683 00:28:48,876 --> 00:28:50,636 in just saying the same thing again and again, 684 00:28:50,826 --> 00:28:52,246 mean look how easy it is to do that. 685 00:28:52,546 --> 00:28:54,846 But the downside is you can't provide more informative 686 00:28:54,846 --> 00:28:57,546 messages like, that's 0, or that's negative. 687 00:28:57,806 --> 00:29:00,246 To do that, you're probably gonna wanna introduce some 688 00:29:00,296 --> 00:29:02,046 if conditions inside of the loop. 689 00:29:02,216 --> 00:29:05,006 And maybe even not use a do while, just use a while loop 690 00:29:05,186 --> 00:29:07,286 or for loop as we'll soon see and yell 691 00:29:07,286 --> 00:29:08,566 at the user appropriately. 692 00:29:08,816 --> 00:29:08,906 Yeah. 693 00:29:08,906 --> 00:29:09,676 [ Inaudible Remark ] 694 00:29:09,676 --> 00:29:14,496 >> Good question, is there a way to destroy a variable 695 00:29:14,496 --> 00:29:15,446 within the scope of the program? 696 00:29:15,446 --> 00:29:17,406 No. But we will have that feature in PHTP. 697 00:29:18,116 --> 00:29:21,526 In the back. 698 00:29:21,526 --> 00:29:21,866 [ Inaudible Remark ] 699 00:29:21,866 --> 00:29:23,786 >> Does N have a default value? 700 00:29:23,786 --> 00:29:25,426 Short answer is it depends. 701 00:29:25,816 --> 00:29:27,676 Let me-- we'll come back to that before long. 702 00:29:27,966 --> 00:29:32,526 So for now, assume, that if you do not give a variable a value, 703 00:29:32,986 --> 00:29:34,846 it's gonna contain what we're gonna call literally a 704 00:29:34,846 --> 00:29:35,636 garbage value. 705 00:29:35,636 --> 00:29:37,366 It's gonna contain some unknown value 706 00:29:37,616 --> 00:29:39,996 and indeed that's the common case for it to be some piece 707 00:29:40,216 --> 00:29:45,706 of junk, some random pattern of bits that just 708 00:29:47,706 --> 00:29:51,886 so happens to be there. 709 00:29:52,296 --> 00:29:55,286 Oh, let's go here. 710 00:29:55,286 --> 00:29:55,856 [ Inaudible Remark ] 711 00:29:55,856 --> 00:29:56,806 >> Okay. 712 00:29:56,806 --> 00:29:58,676 [ Inaudible Remark ] 713 00:29:58,676 --> 00:29:59,256 >> Good question. 714 00:29:59,326 --> 00:30:00,516 Is there any limit on the number 715 00:30:00,516 --> 00:30:03,526 of logical conjunctions you can use in this parenthetical here? 716 00:30:03,716 --> 00:30:06,896 If you wanted to say while N is great or is less than 1 717 00:30:06,966 --> 00:30:11,776 or N is greater than say 99, if you wanted to only be 718 00:30:11,776 --> 00:30:13,406 at digit from 1 through 99. 719 00:30:13,726 --> 00:30:16,526 No, you could keep oaring things together ending things together, 720 00:30:16,526 --> 00:30:17,556 you might wanna use parenthesis 721 00:30:17,556 --> 00:30:19,226 to make sure the logic is all correct. 722 00:30:19,516 --> 00:30:22,116 The only limit really is on readability. 723 00:30:22,116 --> 00:30:25,076 And what a reasonable programmer would sort of be comfortable 724 00:30:25,076 --> 00:30:30,446 with or yell at you at if it's just becoming unwieldy frankly. 725 00:30:30,486 --> 00:30:34,556 Alright, yeah. 726 00:30:34,556 --> 00:30:34,623 [ Inaudible Questions ] 727 00:30:34,623 --> 00:30:37,656 >> Yes, within the confines of the curly braces, 728 00:30:37,656 --> 00:30:41,186 you could keep reassigning N of their value in different lines 729 00:30:41,186 --> 00:30:42,416 of code again and again and again, 730 00:30:42,656 --> 00:30:43,756 that is perfectly correct. 731 00:30:43,756 --> 00:30:45,846 The only thing you don't want to do as we-- 732 00:30:45,846 --> 00:30:49,006 as we discussed on Friday, is you don't want to redeclare 733 00:30:49,006 --> 00:30:51,526 that variable by saying int again. 734 00:30:51,526 --> 00:30:54,846 Once you've declared it stick with it, don't specify int 735 00:30:54,996 --> 00:30:56,496 on the left hand side again. 736 00:30:56,836 --> 00:30:58,746 So here's a slight variant of this. 737 00:30:58,746 --> 00:31:03,996 So this is a version called positive2.c and its example now 738 00:31:03,996 --> 00:31:05,886 of introducing a Boolean variable. 739 00:31:06,046 --> 00:31:09,056 Bool is now a type in C that's built into C, 740 00:31:09,056 --> 00:31:11,876 it does exist n C plus plus, in PHP in other languages. 741 00:31:12,076 --> 00:31:13,836 But the CS50 Library gives you access 742 00:31:13,836 --> 00:31:16,046 to a Boolean data type that's just true 743 00:31:16,046 --> 00:31:17,376 or false in lower case. 744 00:31:17,706 --> 00:31:20,056 And notice that just with this modification, 745 00:31:20,056 --> 00:31:22,076 you can actually make the code a little more readable, 746 00:31:22,076 --> 00:31:23,686 a little more like English. 747 00:31:23,866 --> 00:31:27,676 So at the top here, I've this time declared a Boolean-- 748 00:31:28,036 --> 00:31:30,766 called thankful, just because I wanted to sound kind of cute 749 00:31:30,766 --> 00:31:31,786 and read like English. 750 00:31:31,976 --> 00:31:34,086 And I'm initializing that Boolean value to false, 751 00:31:34,086 --> 00:31:35,876 and again, false is in all lower case. 752 00:31:36,276 --> 00:31:38,766 Then I've got a do loop, and I've got printf, 753 00:31:38,766 --> 00:31:40,596 I demand that you give me a positive integer 754 00:31:40,806 --> 00:31:42,036 and I'll notice a difference 755 00:31:42,036 --> 00:31:44,386 in this program is I am getting an int from the user, 756 00:31:44,386 --> 00:31:46,546 but what am I doing with it? 757 00:31:47,416 --> 00:31:49,566 I mean really nothing, I'm comparing it, 758 00:31:49,606 --> 00:31:51,906 so I'm kind of using it for something but I'm throwing 759 00:31:51,906 --> 00:31:54,876 that value away as soon as that line of code is done executed. 760 00:31:55,176 --> 00:31:58,346 But in English, those 2 lines if get int greater than zero, 761 00:31:58,346 --> 00:32:00,806 thankful equals true, what's the implication? 762 00:32:01,006 --> 00:32:03,626 Well, it just means that this program is meant to be thankful 763 00:32:03,626 --> 00:32:06,786 when the user finally provides it with a positive number. 764 00:32:06,996 --> 00:32:08,186 Now why is this useful? 765 00:32:08,356 --> 00:32:11,036 Because now, in my wild condition inside 766 00:32:11,036 --> 00:32:12,556 of those parenthesis down here, 767 00:32:12,796 --> 00:32:14,296 now we can introduce another piece 768 00:32:14,296 --> 00:32:18,316 of syntax namely the equal, equal sign that says, well, 769 00:32:18,316 --> 00:32:21,416 while, thankful is false, while I'm not thankful, 770 00:32:21,626 --> 00:32:23,896 do this loop again and again and again. 771 00:32:24,056 --> 00:32:26,496 We've seen Boolean expressions before, if you have a greater 772 00:32:26,496 --> 00:32:29,306 than sign, or a less than sign, alright, just logically, 773 00:32:29,306 --> 00:32:31,796 that's gonna evaluate to a bool, true or false? 774 00:32:31,796 --> 00:32:34,496 X is less than Y, true or false, yes or no? 775 00:32:34,826 --> 00:32:36,996 Well, you can literally put a Boolean value there 776 00:32:36,996 --> 00:32:40,386 like thankful and compare it against a Boolean value 777 00:32:40,836 --> 00:32:44,806 like false, or "true" to express the exact same thing. 778 00:32:45,526 --> 00:32:47,906 But we can do with this slightly more elegantly 779 00:32:48,686 --> 00:32:49,956 but we should first tease apart this. 780 00:32:50,706 --> 00:32:54,516 Why am I doing equals, equals instead of equals, do you think? 781 00:32:55,516 --> 00:33:02,846 [ Inaudible Answer ] 782 00:33:03,346 --> 00:33:03,976 >> Exactly. 783 00:33:04,516 --> 00:33:08,696 [ Inaudible Remark ] 784 00:33:09,196 --> 00:33:09,566 >> Exactly. 785 00:33:09,636 --> 00:33:12,246 So if you think back to what single equal sign means, 786 00:33:12,476 --> 00:33:14,606 this is the so-called assignment to operator, 787 00:33:14,606 --> 00:33:16,096 I might keep saying equal sign. 788 00:33:16,336 --> 00:33:18,796 But in programming, this is the assignment operator 789 00:33:18,796 --> 00:33:20,206 and it puts the value on the right 790 00:33:20,206 --> 00:33:21,656 into the thing on the left. 791 00:33:22,046 --> 00:33:23,876 Now the problem is the world kinda painted itself 792 00:33:23,876 --> 00:33:25,856 into a corner because the first time someone needed 793 00:33:25,856 --> 00:33:28,776 to write a program that needed to express the comparison 794 00:33:28,946 --> 00:33:32,546 of 2 values where in English or in math, we would say equals 795 00:33:32,546 --> 00:33:33,526 to compare it to values. 796 00:33:33,996 --> 00:33:36,946 Kind of out of luck 'cause we already used the equal sign 797 00:33:36,946 --> 00:33:38,876 on our keyboard to know assignment 798 00:33:39,056 --> 00:33:40,306 of values from right to left. 799 00:33:40,416 --> 00:33:42,116 So the world decided that well, fine, 800 00:33:42,116 --> 00:33:43,476 let's just put 2 equal signs. 801 00:33:43,806 --> 00:33:46,796 So equals, equals does an actual comparison, 802 00:33:47,126 --> 00:33:49,956 equals does a single assignment. 803 00:33:50,426 --> 00:33:52,636 So realize what could happen here. 804 00:33:53,136 --> 00:33:54,706 What if I accidentally do this? 805 00:33:54,706 --> 00:33:57,036 And this 2 is a very common mistake early on. 806 00:33:57,476 --> 00:34:04,706 This code will compile, but what's gonna happen now? 807 00:34:04,706 --> 00:34:05,216 [ Inaudible Answer ] 808 00:34:05,216 --> 00:34:05,926 >> Exactly. 809 00:34:06,236 --> 00:34:09,766 These mistake, stupid as it is this 1 character I made the 810 00:34:09,766 --> 00:34:13,156 mistake with my keyboard is automatically every time the 811 00:34:13,156 --> 00:34:15,716 while condition executes, it's going to assign 812 00:34:15,936 --> 00:34:17,886 to the variable called thankful, what value? 813 00:34:18,466 --> 00:34:18,836 >> False. 814 00:34:18,836 --> 00:34:20,926 >> False. And then it's gonna evaluate it. 815 00:34:20,976 --> 00:34:22,826 So again, just like in math, order of operations. 816 00:34:22,856 --> 00:34:24,956 The stuff inside the parenthesis happens first, 817 00:34:25,216 --> 00:34:27,296 then the big parenthesis themselves happen, 818 00:34:27,296 --> 00:34:29,016 so thankful gets a value of false, 819 00:34:29,336 --> 00:34:31,286 then the question is asked, well, 820 00:34:31,396 --> 00:34:36,076 while false what's gonna happen then? 821 00:34:36,276 --> 00:34:38,666 Well, false is false which means it's gonna break out. 822 00:34:38,866 --> 00:34:43,496 So in other words, if you have while thankful, gets false, 823 00:34:43,976 --> 00:34:46,026 this things is never gonna loop more than once, 824 00:34:46,126 --> 00:34:47,726 because no matter what the user does, 825 00:34:47,726 --> 00:34:50,686 as soon as you hit the while condition thankful is gonna get 826 00:34:50,686 --> 00:34:52,446 a value of false, and bam, 827 00:34:52,686 --> 00:34:54,566 the while loops not gonna repeat this itself, 828 00:34:54,566 --> 00:34:57,646 because again it only repeats if it is true. 829 00:34:58,836 --> 00:35:02,866 Alright, so 1 last example here, let me scroll 830 00:35:02,866 --> 00:35:07,136 down to let's say positive 3 just 831 00:35:07,136 --> 00:35:08,886 to introduce one more piece of syntax. 832 00:35:10,286 --> 00:35:13,556 For those who remember what the previous example looks like, 833 00:35:13,556 --> 00:35:15,246 what's the one difference in this program? 834 00:35:15,716 --> 00:35:16,446 >> Exclamation point. 835 00:35:16,666 --> 00:35:18,156 >> Yes, so there's this new piece of syntax, 836 00:35:18,156 --> 00:35:21,126 the exclamation point otherwise known as bang in programming 837 00:35:21,366 --> 00:35:25,246 and bang thankful here just means not thankful, 838 00:35:25,336 --> 00:35:27,686 it inverts the value of a bully in expression 839 00:35:27,916 --> 00:35:29,816 so I actually don't need to be so verbose. 840 00:35:29,816 --> 00:35:34,636 I don't need to say wow, thankful, equals equals false. 841 00:35:34,636 --> 00:35:39,016 I can instead just say, more idiomatically while not thankful 842 00:35:39,136 --> 00:35:41,096 and that's how you would pronounce that, not thankful 843 00:35:41,096 --> 00:35:43,086 and it just inverts whatever the thankful value is 844 00:35:43,326 --> 00:35:46,856 so just logically now you can read it quite easily do print 845 00:35:46,906 --> 00:35:50,736 that, get an int from the user, while you're not thankful 846 00:35:50,916 --> 00:35:52,406 and you're only gonna become thankful 847 00:35:52,406 --> 00:35:53,666 when the user does what? 848 00:35:54,086 --> 00:35:55,046 Final [inaudible] check? 849 00:35:55,046 --> 00:35:55,596 [ Inaudible Remark ] 850 00:35:55,596 --> 00:35:57,856 >> Gives you a positive integer, that's it. 851 00:35:57,856 --> 00:35:59,896 So again, three different ways this time 852 00:35:59,896 --> 00:36:01,476 that we've done something but what's nice now is 853 00:36:01,476 --> 00:36:03,086 that we have the ability to express ourselves 854 00:36:03,376 --> 00:36:05,846 with just slightly new pieces of syntax, 855 00:36:06,166 --> 00:36:08,026 but these programs are somewhat underwhelming to run, 856 00:36:08,026 --> 00:36:10,446 let's add something that's just a little graphical now. 857 00:36:10,666 --> 00:36:12,796 So here's one other example called progress. 858 00:36:13,366 --> 00:36:16,896 Progress 1 actually introduces another header file 859 00:36:17,026 --> 00:36:18,976 and another function that you won't generally use 860 00:36:18,976 --> 00:36:21,426 but it does hint at fancy features to come. 861 00:36:21,676 --> 00:36:25,046 Notice that because I read the manual, it turns out I was told 862 00:36:25,046 --> 00:36:29,166 to include uni-standard.h, that header file, 863 00:36:29,436 --> 00:36:31,176 if I wanna use the function called sleep 864 00:36:31,176 --> 00:36:33,486 and you can probably guess what sleep does, it puts the program 865 00:36:33,486 --> 00:36:36,086 to sleep for one second in this case. 866 00:36:36,496 --> 00:36:38,376 So what is this thing seem to do? 867 00:36:38,376 --> 00:36:40,116 Well, it's apparently gonna simulate progress 868 00:36:40,116 --> 00:36:41,596 from zero to 100 percent. 869 00:36:41,866 --> 00:36:43,486 So notice this is not a bug this time. 870 00:36:43,516 --> 00:36:47,316 In my four loop, I deliberately want to go from zero to 100, 871 00:36:47,536 --> 00:36:50,876 because I want to count from zero up through 100 in this case 872 00:36:51,226 --> 00:36:52,716 and apparently it's gonna print 873 00:36:52,716 --> 00:36:55,526 on every iteration percent complete, percent D, 874 00:36:55,796 --> 00:36:58,016 that's an integer, we're gonna plug in the value of Y, 875 00:36:58,106 --> 00:37:00,376 and now this looks a little cryptic, but take a guess, 876 00:37:00,436 --> 00:37:02,526 why do I have percent D, percent, percent? 877 00:37:02,526 --> 00:37:02,593 [ Inaudible Remark ] 878 00:37:02,593 --> 00:37:05,836 >> Yeah exactly. 879 00:37:05,836 --> 00:37:08,546 So this is what we generally call escaping a character. 880 00:37:08,546 --> 00:37:10,286 Here too is one of these painting yourselves 881 00:37:10,286 --> 00:37:10,916 into a corner. 882 00:37:10,916 --> 00:37:13,586 If you have decided as a world to use percent 883 00:37:13,796 --> 00:37:16,326 to mean here comes the format code for print 884 00:37:16,326 --> 00:37:20,386 out like percent S, percent D, percent F, well you're kinda 885 00:37:20,386 --> 00:37:22,466 out of luck if you want to actually say percent 886 00:37:23,086 --> 00:37:26,236 because the compiler apparently has been trained to know 887 00:37:26,236 --> 00:37:29,116 that anytime you see percent, another letter is gonna come 888 00:37:29,206 --> 00:37:31,096 after it, but if you want a literal percent, 889 00:37:31,316 --> 00:37:33,796 the way of doing that is to say percent percent. 890 00:37:34,206 --> 00:37:36,556 Now you can imagine this actually becomes messy, 891 00:37:36,556 --> 00:37:39,186 how would you do percent percent? 892 00:37:39,186 --> 00:37:40,356 [ Inaudible Remark ] 893 00:37:40,356 --> 00:37:42,576 >> Percent, percent, percent, percent, right, 894 00:37:42,576 --> 00:37:45,996 or 3 percents is 6 percent, 4 percent is 8 percent, 895 00:37:46,226 --> 00:37:47,996 but thankfully this is not a common case, right? 896 00:37:47,996 --> 00:37:49,936 Odds are this is not a common program to write 897 00:37:49,936 --> 00:37:51,656 so the world just made a judgment call. 898 00:37:52,316 --> 00:37:54,396 Fine, that might happen but it's very rare. 899 00:37:54,776 --> 00:37:56,356 So, what's this actually gonna do? 900 00:37:56,356 --> 00:37:58,986 It's gonna print one per line, once per second, 901 00:37:59,246 --> 00:38:00,426 this kind of progress bar. 902 00:38:00,426 --> 00:38:03,776 So let's try this, let me open up a bigger terminal window here 903 00:38:03,776 --> 00:38:06,966 so that we can see more at once and now we compile progress 1. 904 00:38:07,076 --> 00:38:07,886 It compiles okay. 905 00:38:07,886 --> 00:38:09,856 And again, for the-- to be a clear, 906 00:38:09,856 --> 00:38:12,756 all I did was I opened a separate program that's called 907 00:38:12,756 --> 00:38:14,866 terminal, this little black icon there, 908 00:38:14,866 --> 00:38:17,026 and it just lets me do the same thing full screen just 909 00:38:17,026 --> 00:38:18,686 so it's a little bit prettier, enter. 910 00:38:19,386 --> 00:38:23,966 Alright, so it's getting interesting. 911 00:38:23,966 --> 00:38:27,116 Alright, you can really see and feel this 4 loop. 912 00:38:27,236 --> 00:38:28,976 Frankly, I'm already getting a little bored 913 00:38:28,976 --> 00:38:32,296 so why don't we go ahead and hit Control C to kill this program 914 00:38:32,296 --> 00:38:36,056 but let's assume that it's actually go up through 100 915 00:38:36,056 --> 00:38:37,916 which it should because according to the code, 916 00:38:37,916 --> 00:38:39,826 we have less than or equal to 100, 917 00:38:40,036 --> 00:38:42,626 but this is obviously not a very user friendly progress bar, 918 00:38:42,626 --> 00:38:42,716 right? 919 00:38:42,716 --> 00:38:45,106 It would be nice if it's just the number updated itself. 920 00:38:45,106 --> 00:38:46,196 But notice the take away here. 921 00:38:46,256 --> 00:38:48,656 Print F is pretty limited, print F is like a kinda 922 00:38:48,656 --> 00:38:51,256 like an old school typewriter, once you type out a word 923 00:38:51,256 --> 00:38:53,936 and do back slash and enter, that's it. 924 00:38:53,936 --> 00:38:56,526 You can't really scroll back and change what you typed 925 00:38:56,776 --> 00:38:58,666 because it's already emblazoned on the page. 926 00:38:58,896 --> 00:39:00,236 So in this case here it'd be nice 927 00:39:00,276 --> 00:39:03,286 to somehow more elegantly just change the percent sign 928 00:39:03,646 --> 00:39:04,816 so we can actually do that. 929 00:39:04,896 --> 00:39:08,256 I mean, go ahead and open up version 2 of this, progress 2. 930 00:39:08,576 --> 00:39:11,366 Now, it's a little fancier 'cause I had to fix a bug 931 00:39:11,366 --> 00:39:13,166 but notice the difference now. 932 00:39:13,536 --> 00:39:17,466 What am I actually using to print out the percent? 933 00:39:17,466 --> 00:39:18,556 I'm not using percent-- 934 00:39:18,856 --> 00:39:21,026 backslash N, I'm instead using what? 935 00:39:21,026 --> 00:39:21,646 >> Percent R. 936 00:39:21,846 --> 00:39:24,086 >> So percent R. So this is one 937 00:39:24,086 --> 00:39:25,196 of the silly computer is [inaudible] 938 00:39:25,196 --> 00:39:27,816 and triple multiple times during the semester. 939 00:39:27,816 --> 00:39:30,386 Long story short, in the world of Unix and Linux, 940 00:39:30,476 --> 00:39:33,306 these older operating systems and modern operating systems 941 00:39:33,476 --> 00:39:34,846 that are mostly command line based, 942 00:39:34,916 --> 00:39:35,736 but just like the appliance 943 00:39:35,736 --> 00:39:37,646 that now a days do have graphical environments, 944 00:39:37,966 --> 00:39:41,156 the world decided that to end a line in a text file, 945 00:39:41,596 --> 00:39:44,116 another is when you hit enter, what actually goes in the file, 946 00:39:44,366 --> 00:39:46,606 well, [inaudible] backslash N. Now, unfortunately, 947 00:39:46,606 --> 00:39:48,586 the folks at Microsoft decided years ago 948 00:39:48,586 --> 00:39:50,136 that when you hit enter on your keyboard, 949 00:39:50,136 --> 00:39:51,666 what actually gets saved in the file? 950 00:39:51,856 --> 00:39:53,316 It's not just backslash N, 951 00:39:53,696 --> 00:39:56,766 it's something called backslash R backslash N. 952 00:39:56,986 --> 00:39:59,196 So 2 characters get saved in the file. 953 00:39:59,406 --> 00:40:02,246 Those of you who actually remember typewriters might know 954 00:40:02,246 --> 00:40:04,276 that on a typewriter, the old mechanical thing 955 00:40:04,276 --> 00:40:06,386 when you hit enter on the keyboard, not only does 956 00:40:06,386 --> 00:40:08,886 that roll that scroller kind of scroll this way, 957 00:40:09,146 --> 00:40:11,326 also the printing head moves back to the left, 958 00:40:11,326 --> 00:40:12,976 so that's what Microsoft is being consistent with. 959 00:40:13,516 --> 00:40:17,556 >> Backslash N moves the curl of the roll thing upwards 960 00:40:17,926 --> 00:40:22,406 and carriage return, CR, backslash R, moves the head back 961 00:40:22,406 --> 00:40:23,836 to the left of the screen. 962 00:40:24,066 --> 00:40:25,736 So technically, Microsoft is kind 963 00:40:25,736 --> 00:40:28,306 of implementing the old school typewriter the most accurately, 964 00:40:28,506 --> 00:40:29,536 but then Apple came along. 965 00:40:29,536 --> 00:40:31,856 He decided to hell with you we're only going 966 00:40:31,856 --> 00:40:35,846 to use backslash R. So we have 3 different operating systems, 967 00:40:35,846 --> 00:40:37,786 major operating systems, 3 different ways 968 00:40:37,786 --> 00:40:42,056 and this is thankfully mostly irrelevant except when you try 969 00:40:42,056 --> 00:40:45,296 to open a file that you created on the Mac on the PC, 970 00:40:45,296 --> 00:40:48,446 a text file or when you open a file on a PC that you created 971 00:40:48,446 --> 00:40:50,446 on the Mac or Unix or a Linux machine. 972 00:40:50,616 --> 00:40:53,226 If you've ever opened a text file on windows in particular 973 00:40:53,226 --> 00:40:56,246 with no pad.exe and even though it's suppose to be a lot 974 00:40:56,246 --> 00:40:58,716 of text is all on one crazy long line, 975 00:40:59,066 --> 00:41:02,456 that simply because no pad [inaudible] backslash R, 976 00:41:02,456 --> 00:41:05,646 backslash N, and if it doesn't see it, which it might not 977 00:41:05,646 --> 00:41:08,346 if you made it on a Linux computer or a Mac, with I-- 978 00:41:08,346 --> 00:41:10,646 all together, you're just gonna get some big mess. 979 00:41:10,946 --> 00:41:15,106 Wordpad by contrast familiar, knows about just backslash N. 980 00:41:15,236 --> 00:41:16,386 So long story short, we'll come back 981 00:41:16,386 --> 00:41:17,376 to that with web programming. 982 00:41:17,576 --> 00:41:19,686 But for now, we're talking advantage of this as follows. 983 00:41:19,986 --> 00:41:24,016 Backslash R has the effect conceptually of moving the head 984 00:41:24,296 --> 00:41:24,886 of that's being-- 985 00:41:24,886 --> 00:41:28,036 of the printing head back to the left without doing what? 986 00:41:29,516 --> 00:41:30,766 Moving to a new line. 987 00:41:30,766 --> 00:41:33,046 So it just moves the printing head back to the left. 988 00:41:33,306 --> 00:41:35,786 So the effect now is that on every iteration, 989 00:41:35,826 --> 00:41:39,036 I'm gonna reprint percent complete colon 990 00:41:39,176 --> 00:41:40,056 and then a number. 991 00:41:40,476 --> 00:41:43,356 But because I'm printing the same exact words, 992 00:41:43,356 --> 00:41:45,736 and only changing the number, we have sort 993 00:41:45,736 --> 00:41:48,036 of a nice simulation of animation here. 994 00:41:48,036 --> 00:41:52,776 If I make progress 2 and run now, progress 2 notice 995 00:41:52,776 --> 00:41:56,426 that we actually have a fairly pretty looking implementation 996 00:41:56,426 --> 00:41:58,746 now of a progress bar that's still a little slow 997 00:41:58,746 --> 00:41:59,446 for our taste. 998 00:41:59,836 --> 00:42:03,556 But now we're exercising more control over the screen. 999 00:42:04,066 --> 00:42:06,306 Now, can we do a little better here, 1000 00:42:06,306 --> 00:42:07,816 this last version of progress. 1001 00:42:09,146 --> 00:42:13,996 If I go in here, down to progress 3.c notice 1002 00:42:13,996 --> 00:42:15,746 that I can do this also in different way. 1003 00:42:16,006 --> 00:42:17,826 So how am I doing this differently? 1004 00:42:18,066 --> 00:42:21,556 Before I did a 4 loop, and frankly, I would say 8 times 1005 00:42:21,556 --> 00:42:23,906 out of 10 when I write loops in any programming language, 1006 00:42:23,906 --> 00:42:25,696 I typically by default use 4 loops. 1007 00:42:25,876 --> 00:42:26,956 Even though they're a little uglier, 1008 00:42:26,956 --> 00:42:29,606 they're let you express yourself a little more succinctly 1009 00:42:29,856 --> 00:42:30,946 but we can do this otherwise. 1010 00:42:30,946 --> 00:42:33,456 I could declare I as an [inaudible] up here. 1011 00:42:34,546 --> 00:42:37,736 I can then say while I is less than or equal 100 1012 00:42:37,926 --> 00:42:39,996 and then what do I have to do on each iteration? 1013 00:42:40,026 --> 00:42:42,176 I can actually manually increment I 1014 00:42:42,526 --> 00:42:43,526 with this plus, plus. 1015 00:42:43,936 --> 00:42:46,206 So again, I would actually argue this is a little messier 1016 00:42:46,206 --> 00:42:48,636 of an implementation 'cause look, I have I at the top, 1017 00:42:48,636 --> 00:42:49,886 I've got this I plus, plus. 1018 00:42:50,206 --> 00:42:52,146 And again, recall what I did a moment ago. 1019 00:42:52,446 --> 00:42:54,036 This just feels a little more compact. 1020 00:42:54,216 --> 00:42:56,756 Everything is more tightly knit, but again, this hammer is 1021 00:42:56,756 --> 00:42:58,866 on the point that you can implement these things 1022 00:42:58,866 --> 00:43:01,146 in many different ways just as in scratch 1023 00:43:01,556 --> 00:43:04,526 with different pieces of puzzle pieces. 1024 00:43:05,106 --> 00:43:07,586 Any questions, yeah? 1025 00:43:08,516 --> 00:43:25,676 [ Inaudible Remark ] 1026 00:43:26,176 --> 00:43:28,156 >> Oh yeah, say it once more? 1027 00:43:29,091 --> 00:43:31,091 [ Inaudible Remark ] 1028 00:43:31,166 --> 00:43:33,826 >> Oh, good question. 1029 00:43:33,826 --> 00:43:35,646 So, I'm actually kind of cheating 1030 00:43:35,646 --> 00:43:37,456 in making this seem a lot easier than it is 1031 00:43:37,456 --> 00:43:40,756 because the numbers I'm printing are actually going up fro 0 to 1 1032 00:43:40,756 --> 00:43:42,886 to 2 to 3 to 100, they're getting bigger. 1033 00:43:43,076 --> 00:43:45,066 If we actually counted in the other direction, 1034 00:43:45,066 --> 00:43:46,346 you can see what would happen here. 1035 00:43:46,346 --> 00:43:49,646 Let me go back to progress 2, let me actually initialize this 1036 00:43:49,646 --> 00:43:53,286 to 100, let me go ahead and say this should do this while I is 1037 00:43:53,346 --> 00:43:56,666 greater than or equal to 0, and what do I wanna change this to? 1038 00:43:57,256 --> 00:43:58,436 Minus, minus, alright. 1039 00:43:58,536 --> 00:44:01,476 So, same idea just slightly different syntax. 1040 00:44:01,716 --> 00:44:04,186 Let me recompile this, let me rerun it, 1041 00:44:04,826 --> 00:44:08,236 and now I can't quite see it 'cause my cursor is there. 1042 00:44:08,956 --> 00:44:10,566 Let's-- let me rerun this again. 1043 00:44:11,216 --> 00:44:14,146 Let's actually, I'm gonna teach this 1044 00:44:14,146 --> 00:44:15,346 so you can see what's going on. 1045 00:44:15,346 --> 00:44:16,866 I'm gonna put a couple of spaces at the end 1046 00:44:16,866 --> 00:44:19,156 so that my green cursor is not overlapping. 1047 00:44:19,646 --> 00:44:22,856 So if I rerun this, now notice that-- 1048 00:44:22,856 --> 00:44:26,066 oh and actually think-- oh I can't do that. 1049 00:44:26,956 --> 00:44:28,666 I just fixed the problem by adding those spaces. 1050 00:44:30,286 --> 00:44:32,536 Alright. So, we're gonna have to look 1051 00:44:32,536 --> 00:44:34,046 through my green cursor here to see this 1052 00:44:34,046 --> 00:44:35,116 in this particular program. 1053 00:44:35,576 --> 00:44:37,346 So notice what's happening. 1054 00:44:37,676 --> 00:44:40,676 That projector cooperates, that's a bug, right? 1055 00:44:40,716 --> 00:44:43,256 So the green cursor is just an artifact of the gooey program. 1056 00:44:43,456 --> 00:44:45,006 But I should not still have a percent there 1057 00:44:45,006 --> 00:44:46,036 but I do because why? 1058 00:44:46,036 --> 00:44:47,306 >> Because the number-- 1059 00:44:47,306 --> 00:44:48,946 >> The number used to be 100. 1060 00:44:48,946 --> 00:44:52,056 Now, if we re-- and there was, that's a 0 previously there. 1061 00:44:52,276 --> 00:44:54,706 And so now we have a percent overriding part 1062 00:44:54,706 --> 00:44:56,806 of that big number, but now at all of it. 1063 00:44:56,806 --> 00:45:00,046 So if we let this actually go down monotonously down to 9, 8, 1064 00:45:00,046 --> 00:45:03,976 7, we'll see what also yet another percent sign. 1065 00:45:03,976 --> 00:45:06,826 So in short I kinda cheated here to make it seem easier 1066 00:45:06,826 --> 00:45:08,066 but we could fix this obviously 1067 00:45:08,066 --> 00:45:09,816 by just hitting the space bar or some other trick. 1068 00:45:09,816 --> 00:45:12,496 But by default the line is not erased, it only prints 1069 00:45:12,496 --> 00:45:13,886 out what you tell it to do. 1070 00:45:14,426 --> 00:45:17,206 Alright, why don't we go ahead here 'cause it's a lot 1071 00:45:17,206 --> 00:45:19,306 to digest, why don't we go ahead and take a 2 minute break, 1072 00:45:19,306 --> 00:45:21,166 I will let this countdown, play some music 1073 00:45:21,166 --> 00:45:21,786 and then we'll resume. 1074 00:45:21,906 --> 00:45:23,906 [ Pause ] 1075 00:45:24,026 --> 00:45:28,766 >> Alright, we are back. 1076 00:45:29,556 --> 00:45:34,026 So as promised, bug 3 percent signs was not the 1077 00:45:34,026 --> 00:45:35,156 desirable outcome. 1078 00:45:35,156 --> 00:45:37,356 Know that we could address this in a couple of ways. 1079 00:45:37,356 --> 00:45:39,556 One, there is the slightly hackish approach 1080 00:45:39,556 --> 00:45:41,436 where I just hit the space bar a few times. 1081 00:45:41,686 --> 00:45:42,896 But you can actually do better. 1082 00:45:43,136 --> 00:45:46,906 And just to plant a seed, know that just as with percent F 1083 00:45:47,426 --> 00:45:50,336 where you can specify how many numbers should follow the 1084 00:45:50,336 --> 00:45:53,626 decimal point, also with percent D you can do what's called 1085 00:45:53,626 --> 00:45:55,336 specifying the width of a number, 1086 00:45:55,606 --> 00:45:57,776 how many spaces it will take up even 1087 00:45:57,776 --> 00:45:59,546 if it doesn't need that many digits. 1088 00:45:59,876 --> 00:46:01,686 So long story short, know that with printf 1089 00:46:01,896 --> 00:46:03,856 for which there is documentation online that I'll pull 1090 00:46:03,856 --> 00:46:06,346 up in a moment, there are these additional format codes 1091 00:46:06,346 --> 00:46:08,736 and tricks so that you can tell printf, 1092 00:46:08,916 --> 00:46:11,566 make sure you're using it least this much space even 1093 00:46:11,566 --> 00:46:14,096 if you don't need it for the size of the number. 1094 00:46:14,206 --> 00:46:14,306 Yeah. 1095 00:46:14,306 --> 00:46:14,966 [ Inaudible Remark ] 1096 00:46:14,966 --> 00:46:19,136 >> And the F flush 2 is the other loose end here. 1097 00:46:19,136 --> 00:46:21,176 So in general when you call printf, 1098 00:46:21,376 --> 00:46:23,206 what's really happening is you're handing a bunch 1099 00:46:23,206 --> 00:46:25,346 of characters to the operating system and saying, 1100 00:46:25,536 --> 00:46:26,896 "Please print this to the screen." 1101 00:46:27,136 --> 00:46:31,386 However for efficiency reason, slightly historical reasons, 1102 00:46:31,616 --> 00:46:34,006 the operating system typically will not display those 1103 00:46:34,006 --> 00:46:34,926 characters on the screen 1104 00:46:34,926 --> 00:46:37,926 until it actually receives a backslash N as well. 1105 00:46:38,196 --> 00:46:39,816 The idea being years ago, 1106 00:46:40,046 --> 00:46:41,976 it's kind of expensive computationally 1107 00:46:41,976 --> 00:46:43,666 to print a character, print a character. 1108 00:46:43,666 --> 00:46:45,036 So the operating system wanted a way 1109 00:46:45,036 --> 00:46:46,536 and do a whole bunch of them at once. 1110 00:46:46,836 --> 00:46:49,066 Obviously, in this program I wanted to print 1111 00:46:49,146 --> 00:46:52,876 without using a backslash N so F flush which stands 1112 00:46:52,876 --> 00:46:55,666 for file flush, this just means it's a message 1113 00:46:55,666 --> 00:46:57,836 to the operating system saying, "Please, whatever I sent you, 1114 00:46:57,836 --> 00:47:00,176 just print it now, print it now, print it now." 1115 00:47:00,176 --> 00:47:02,976 In general, you don't need to do that because in almost all 1116 00:47:02,976 --> 00:47:05,136 of our examples we've actually used backslash N. 1117 00:47:05,436 --> 00:47:09,866 So this is a rare case but good to at least remember here. 1118 00:47:10,156 --> 00:47:12,276 So in terms of documentation, you'll see a couple 1119 00:47:12,276 --> 00:47:13,616 of things in the weeks to come. 1120 00:47:13,906 --> 00:47:16,086 First of all, know that built into Linux and built 1121 00:47:16,086 --> 00:47:18,626 into this command line environment is what's called man 1122 00:47:18,626 --> 00:47:22,516 pages, manual pages so that almost always if you wanna look 1123 00:47:22,516 --> 00:47:25,156 up some technical details, you can type something 1124 00:47:25,156 --> 00:47:27,976 like man printf for manual printf. 1125 00:47:28,406 --> 00:47:32,576 And what happens once you hit Enter is you get back a list 1126 00:47:32,576 --> 00:47:33,666 of instructions here 1127 00:47:33,836 --> 00:47:35,826 and actually this one is slightly a corner case. 1128 00:47:35,826 --> 00:47:36,276 Let me do this. 1129 00:47:36,646 --> 00:47:40,106 Man in chapter 3 of printf and notice what I get, 1130 00:47:40,206 --> 00:47:41,966 the Linux programmer's manual 1131 00:47:42,186 --> 00:47:44,166 and this is a little overwhelming frankly 1132 00:47:44,166 --> 00:47:46,786 at first glance which is why we'll show you a more user 1133 00:47:46,786 --> 00:47:47,896 friendly version in a moment. 1134 00:47:48,116 --> 00:47:49,826 But long story short, this documentation 1135 00:47:49,826 --> 00:47:52,216 which we'll come back to later in the term tells you one 1136 00:47:52,216 --> 00:47:55,346 under synopsis what header file you need to use this function. 1137 00:47:55,666 --> 00:47:58,656 It mentions related functions and this we won't get 1138 00:47:58,656 --> 00:48:02,436 to for a while and not even all of them but printf, F printf, 1139 00:48:02,436 --> 00:48:04,296 S printf, turns out there is a bunch of them. 1140 00:48:04,596 --> 00:48:07,396 But if you ever wanna know the precise definition 1141 00:48:07,396 --> 00:48:10,066 of something even if its way more information than you cared 1142 00:48:10,066 --> 00:48:13,006 to know, it's almost always in this so called man pages. 1143 00:48:13,056 --> 00:48:17,206 Now, when we want you to look at here in PDFs of the problems 1144 00:48:17,206 --> 00:48:19,756 as we'll generally tell you what to type man something, 1145 00:48:19,816 --> 00:48:22,536 something so that you don't know it need to go around guessing. 1146 00:48:22,776 --> 00:48:24,506 But know that in the meantime frankly, 1147 00:48:24,506 --> 00:48:28,256 the more user friendly thing to do is if you go to CS50.net 1148 00:48:28,256 --> 00:48:31,126 and click Resources, what you'll see on this page is a bunch 1149 00:48:31,126 --> 00:48:32,566 of recommended resources. 1150 00:48:32,566 --> 00:48:35,516 These are the free alternatives to the recommended textbooks 1151 00:48:35,776 --> 00:48:38,526 and typically based on the week, we'll expand different parts 1152 00:48:38,526 --> 00:48:39,946 to just draw your attention to things 1153 00:48:39,946 --> 00:48:40,986 that we think would be useful. 1154 00:48:41,336 --> 00:48:43,766 In this particular this one up here, See Reference. 1155 00:48:44,206 --> 00:48:47,936 Again, on the resources page, this is a fairly simple website 1156 00:48:48,206 --> 00:48:51,056 that actually has much more user friendly documentation 1157 00:48:51,056 --> 00:48:52,516 for a whole lot of functions. 1158 00:48:52,556 --> 00:48:55,066 Sometimes you have to kind of know what you're looking 1159 00:48:55,066 --> 00:48:57,456 for 'cause they're categorized here on the left hand side. 1160 00:48:57,696 --> 00:48:59,306 So I don't see printf immediately. 1161 00:48:59,576 --> 00:49:02,566 But I do know that printf now is related to input 1162 00:49:02,566 --> 00:49:04,686 and output otherwise known as IO. 1163 00:49:04,936 --> 00:49:09,146 So if I click on CIO, there's a whole laundry list 1164 00:49:09,146 --> 00:49:10,516 of functions we've never looked at. 1165 00:49:10,516 --> 00:49:13,796 And most of these we won't even bother because you don't need 1166 00:49:13,796 --> 00:49:15,656 to know all the functions to make good use 1167 00:49:15,656 --> 00:49:16,886 of a language but there is printf. 1168 00:49:17,036 --> 00:49:19,336 And if you scroll down here, okay, 1169 00:49:19,336 --> 00:49:21,856 anything that is bob is probably fairly user friendly. 1170 00:49:21,856 --> 00:49:25,266 And indeed this is slightly more accessible descriptions 1171 00:49:25,266 --> 00:49:26,096 of how it works. 1172 00:49:26,096 --> 00:49:28,326 Now, some of the syntax we haven't gotten through just yet. 1173 00:49:28,576 --> 00:49:32,066 But notice in this purple box it's telling us standard IO.h. 1174 00:49:32,066 --> 00:49:34,696 It's telling us that printf and we'll come back to this, 1175 00:49:34,936 --> 00:49:37,086 returns an int even though we're not using this 1176 00:49:37,086 --> 00:49:38,706 yet just like main. 1177 00:49:39,056 --> 00:49:42,306 And it takes a constant char format comma dot, dot dot. 1178 00:49:42,566 --> 00:49:44,926 So when in doubt, just kind of ignore for now the things 1179 00:49:44,926 --> 00:49:46,476 that you don't quite understand but look 1180 00:49:46,476 --> 00:49:48,246 for the examples in particular. 1181 00:49:48,246 --> 00:49:50,526 And down here you'll see a chart of all 1182 00:49:50,526 --> 00:49:51,916 of the available format codes. 1183 00:49:51,916 --> 00:49:54,636 We've talked about percent D, we've talked about percent F 1184 00:49:54,956 --> 00:49:56,916 but it turns out there is a whole bunch of others. 1185 00:49:56,916 --> 00:49:59,636 And generally only a few of these do you need but know 1186 00:49:59,636 --> 00:50:02,206 that this documentation, the CA reference 1187 00:50:02,416 --> 00:50:06,136 on CS50.net is a wonderful place to start when you have questions 1188 00:50:06,286 --> 00:50:08,946 as well as any of the books that you might be dabbling in. 1189 00:50:09,456 --> 00:50:11,336 >> So a few FYIs. 1190 00:50:11,766 --> 00:50:14,166 One, Scratch is officially behind is. 1191 00:50:14,166 --> 00:50:16,096 If you have scratch boards at any point this week, 1192 00:50:16,096 --> 00:50:17,816 just drop them with the TFs who tend to hang 1193 00:50:17,816 --> 00:50:19,236 out in the front of the lecture hall. 1194 00:50:19,506 --> 00:50:23,076 Lunch, you are all cordially invited to lunch this Friday 1195 00:50:23,076 --> 00:50:24,876 and in future Fridays, if this is of interest, 1196 00:50:25,156 --> 00:50:27,556 go to CS50.net RSVP sometime today. 1197 00:50:27,896 --> 00:50:31,156 Typically, we'll limit the number of seats to 20 or 30 just 1198 00:50:31,156 --> 00:50:33,696 so that it's a reasonably intimate opportunity to chat 1199 00:50:33,696 --> 00:50:34,566 with me, the TFs, the CAs 1200 00:50:34,916 --> 00:50:36,596 and we'll generally go somewhere near 1201 00:50:36,596 --> 00:50:37,916 in the square or in the house. 1202 00:50:37,916 --> 00:50:41,736 So this Friday will be 1:15 P.M, if of interest this URL 1203 00:50:41,926 --> 00:50:43,436 and we'll announce that in future weeks. 1204 00:50:43,686 --> 00:50:46,726 So definitely some of you kind of forgotten the section, 1205 00:50:46,806 --> 00:50:49,176 so what we thought we do is rather than turn the system off 1206 00:50:49,176 --> 00:50:52,826 at noon today, 5 P.M. So please if you forget the section, 1207 00:50:52,826 --> 00:50:54,886 at least get to the course's home page. 1208 00:50:55,146 --> 00:50:57,976 Click the link there to section before 5 P.M today. 1209 00:50:58,326 --> 00:51:00,396 Super sections, we filmed one last night. 1210 00:51:00,396 --> 00:51:03,286 Super section is a temporary word for sections open 1211 00:51:03,286 --> 00:51:05,416 to everyone, real sections will start next week. 1212 00:51:05,716 --> 00:51:07,766 The video from last night's appears online. 1213 00:51:07,766 --> 00:51:10,226 If you're like to go in person, they're today and tomorrow 1214 00:51:10,226 --> 00:51:12,566 at 6 P.M. The rooms will be announced shortly 1215 00:51:12,566 --> 00:51:15,826 on the courses home page once the scheduling folks let 1216 00:51:15,826 --> 00:51:16,296 us know. 1217 00:51:16,596 --> 00:51:19,096 The Walkthrough 1 was last night, that is online 1218 00:51:19,206 --> 00:51:20,686 on the problem sets page. 1219 00:51:20,686 --> 00:51:21,436 This is a walkthrough 1220 00:51:21,436 --> 00:51:23,386 of the problem set standard edition itself, 1221 00:51:23,506 --> 00:51:25,566 and now some fun facts. 1222 00:51:25,566 --> 00:51:27,716 You submitted problems at zero where we survey-- 1223 00:51:27,716 --> 00:51:30,246 we ask a number of survey questions really to get sense 1224 00:51:30,246 --> 00:51:33,006 of demographics and also for just for fun sort 1225 00:51:33,006 --> 00:51:36,436 of geeky type things like what you have and what you use. 1226 00:51:36,746 --> 00:51:40,046 So it turns out that 33 percent of you this year have an iPhone, 1227 00:51:40,606 --> 00:51:43,826 17 percent a BlackBerry-- sorry, 17 percent an Android, 1228 00:51:43,826 --> 00:51:47,006 13 percent BlackBerry and 33 percent of you are normal 1229 00:51:47,236 --> 00:51:50,236 and just had sort of non smart phones and 3 percent have other 1230 00:51:50,236 --> 00:51:51,486 for some definition of other. 1231 00:51:51,816 --> 00:51:56,336 Just if curious, 39 percent of you have AT&T followed 1232 00:51:56,336 --> 00:51:58,616 by Verizon followed by T-Mobile. 1233 00:51:58,616 --> 00:52:00,416 No one is using Sprint anymore apparently 1234 00:52:00,776 --> 00:52:02,876 and then other is 4 percent. 1235 00:52:03,296 --> 00:52:06,916 In terms of the demographics, so as is typically the case, 1236 00:52:06,916 --> 00:52:08,976 sophomores are winning this year followed 1237 00:52:08,976 --> 00:52:12,016 by freshman, juniors and seniors. 1238 00:52:12,016 --> 00:52:13,756 Realize seniors, you're in very good company. 1239 00:52:13,756 --> 00:52:15,626 There is almost a hundred of you so let's do feel 1240 00:52:15,626 --> 00:52:18,546 like you're the oldest person in the room, you are not. 1241 00:52:18,546 --> 00:52:21,556 We have folks though from GSAS, HPS, extension school 1242 00:52:21,556 --> 00:52:24,376 and those local high schools so we have quite the diversity 1243 00:52:24,456 --> 00:52:26,146 of students this year. 1244 00:52:26,146 --> 00:52:27,946 I was a little disappointed to see 1245 00:52:27,946 --> 00:52:30,026 that Mather was nowhere close to number 1 this year. 1246 00:52:30,296 --> 00:52:33,346 But Adam's House has the most aspiring computer scientist. 1247 00:52:33,656 --> 00:52:35,656 [ Applause ] 1248 00:52:35,966 --> 00:52:38,676 >> Followed by Elliot House and then-- 1249 00:52:38,676 --> 00:52:38,743 [ Noise ] 1250 00:52:38,743 --> 00:52:41,936 >> And then there's a lot of noise in there. 1251 00:52:41,936 --> 00:52:44,126 So my own Mather did not win this year. 1252 00:52:44,786 --> 00:52:47,346 Though in past years the data might have been artificially 1253 00:52:47,346 --> 00:52:48,416 inflated to make Mather win. 1254 00:52:48,956 --> 00:52:52,776 So laptops, most of you have, this is not surprising. 1255 00:52:52,776 --> 00:52:54,376 A few of you do indeed have desktops 1256 00:52:54,436 --> 00:52:56,036 but that's several hundred laptops floating 1257 00:52:56,036 --> 00:52:57,436 around on campus. 1258 00:52:57,436 --> 00:53:00,726 And then more technically interesting, 1259 00:53:00,966 --> 00:53:03,276 here is the distribution of OS's if you're curious. 1260 00:53:03,366 --> 00:53:05,366 And if you're thinking you're the only one still running 1261 00:53:05,366 --> 00:53:06,946 Vista, you're not. 1262 00:53:06,946 --> 00:53:07,726 There are a few of you. 1263 00:53:07,726 --> 00:53:08,156 [ Laughter ] 1264 00:53:08,156 --> 00:53:13,346 >> And so here is a rough sense of numbers, Windows 7 followed 1265 00:53:13,346 --> 00:53:16,046 by Mac OS 10.6 otherwise known as Snow Leopard. 1266 00:53:16,046 --> 00:53:19,066 So just realize this is the diversity of operating systems. 1267 00:53:19,066 --> 00:53:21,436 But when you aggregate it together, Macs are winning now 1268 00:53:21,436 --> 00:53:26,126 by 56 percent, Windows, 41, and Linux, 4 percent. 1269 00:53:26,586 --> 00:53:29,806 So more [inaudible] 50, let me send home this message. 1270 00:53:29,806 --> 00:53:31,276 So we were really thrilled to see this 1271 00:53:31,276 --> 00:53:33,606 in particular not only have we retained the same percentage 1272 00:53:33,606 --> 00:53:34,966 of more comfortable students, 1273 00:53:35,216 --> 00:53:37,356 those with prior programming backgrounds been programming 1274 00:53:37,356 --> 00:53:38,776 since you're 6 years old and the like. 1275 00:53:39,026 --> 00:53:40,996 But also for the first time, these segments 1276 00:53:40,996 --> 00:53:45,346 of those less comfortable is now the largest and also is larger 1277 00:53:45,346 --> 00:53:46,986 than last year's 46 percent. 1278 00:53:47,246 --> 00:53:49,326 So this is wonderful and we're so pleased to have all 1279 00:53:49,326 --> 00:53:53,336 of you 55 percent, 35 and 10 percents and it is now our vow 1280 00:53:53,476 --> 00:53:57,166 to keep you all on track and succeed by terms end, 1281 00:53:57,166 --> 00:53:59,756 in terms of experience too unless you do not believe what I 1282 00:53:59,756 --> 00:54:04,456 say, 54 percent of you have no, no prior programming 1283 00:54:04,456 --> 00:54:06,686 or computer science experience whatsoever, 1284 00:54:07,006 --> 00:54:10,316 41 percent [inaudible] and 5 percent said a lot. 1285 00:54:10,516 --> 00:54:14,996 So, a word now on psets 1 and onwards so-- 1286 00:54:14,996 --> 00:54:16,266 oh, on, on the CS50 Appliance. 1287 00:54:16,266 --> 00:54:18,906 So in the interest of wording off some FHUs, 1288 00:54:18,906 --> 00:54:22,476 know that there is an increasing amount of documentation online, 1289 00:54:22,476 --> 00:54:25,986 these URLs are sighted both in the PDFs of pset 1 as well 1290 00:54:25,986 --> 00:54:27,236 as linked on all those pages. 1291 00:54:27,236 --> 00:54:30,556 But this is the reference page where for any in all questions 1292 00:54:30,556 --> 00:54:32,276 for installing the CS50 appliance, 1293 00:54:32,496 --> 00:54:34,836 know that this now links to this additional page 1294 00:54:35,046 --> 00:54:36,076 that has a whole lot 1295 00:54:36,076 --> 00:54:38,136 of troubleshooting tips essentially almost all 1296 00:54:38,136 --> 00:54:39,446 of the problems folks might have run 1297 00:54:39,446 --> 00:54:40,906 into with getting the appliance up 1298 00:54:40,906 --> 00:54:42,556 and running is documented there. 1299 00:54:42,556 --> 00:54:44,846 There are very common things that people run into on Macs 1300 00:54:44,846 --> 00:54:47,196 and PCs alike, so consult these URLs. 1301 00:54:47,196 --> 00:54:51,906 And then lastly, if you have a PC, generally that's 1302 00:54:51,906 --> 00:54:53,256 within a few years old. 1303 00:54:53,256 --> 00:54:56,276 Most PCs actually support something called Hardware 1304 00:54:56,276 --> 00:54:57,176 Virtualization 1305 00:54:57,176 --> 00:54:59,096 or Hardware-Assisted Virtualization 1306 00:54:59,366 --> 00:55:01,006 or VTX or similar. 1307 00:55:01,256 --> 00:55:03,306 These are just words saying that built 1308 00:55:03,306 --> 00:55:06,116 into modern CPUs is the ability 1309 00:55:06,326 --> 00:55:09,206 to run virtual machines particularly fast. 1310 00:55:09,776 --> 00:55:12,056 So some of you have found when installing the Appliance, 1311 00:55:12,056 --> 00:55:14,696 it's just downright unusable because it's so slow and that's 1312 00:55:14,696 --> 00:55:17,896 because some manufacturers turn this feature off by default. 1313 00:55:17,896 --> 00:55:21,046 So typically if you go to the Here, if you're a PC user 1314 00:55:21,046 --> 00:55:23,136 and you're just finding the Appliances just deathly slow, 1315 00:55:23,136 --> 00:55:24,166 something has gotta be wrong 1316 00:55:24,166 --> 00:55:26,246 because you got a 2 gigahertz computer, lots of RAM. 1317 00:55:26,736 --> 00:55:28,156 Any program should run well. 1318 00:55:28,506 --> 00:55:30,106 Go here, follow the instructions 1319 00:55:30,106 --> 00:55:31,786 and odds are you can enable this feature 1320 00:55:31,786 --> 00:55:34,356 at which point things just become much more pleasurable. 1321 00:55:34,536 --> 00:55:36,996 On Mac, this is a feature enabled automatically 1322 00:55:36,996 --> 00:55:37,556 by the default. 1323 00:55:37,676 --> 00:55:40,226 So this is only for Windows and Linux folks. 1324 00:55:40,556 --> 00:55:43,566 Regarding grades now, as you might have seen 1325 00:55:43,566 --> 00:55:44,776 on the second page of the pset 1326 00:55:44,776 --> 00:55:46,206 and this will recur throughout the term, 1327 00:55:46,346 --> 00:55:50,056 50 grades problem sets fairly coarsely and its qualitatively 1328 00:55:50,056 --> 00:55:53,326 as possible whereby the TFs will really prioritize providing you 1329 00:55:53,326 --> 00:55:56,906 with hand written or typed qualitative feed pack focusing 1330 00:55:56,906 --> 00:55:58,946 less on plus 1 minus 1 and the like. 1331 00:55:59,186 --> 00:56:01,846 In fact, the 4 axis that we focused 1332 00:56:01,846 --> 00:56:05,216 on when evaluating work is one scope whereby we mean just, 1333 00:56:05,216 --> 00:56:07,626 did you do the pset or did you cut a lot of corners? 1334 00:56:07,626 --> 00:56:09,046 Did you do half of it or the like, 1335 00:56:09,046 --> 00:56:11,566 how much of it did you actually bite off correctness. 1336 00:56:12,246 --> 00:56:14,176 Does it do what it's supposed to do? 1337 00:56:14,176 --> 00:56:16,716 Or, do you have 3 percent signs appearing in your program? 1338 00:56:16,716 --> 00:56:18,676 Or, are there other bugs correctness. 1339 00:56:19,036 --> 00:56:20,626 Design is harder to define 1340 00:56:20,626 --> 00:56:22,306 and it's something you'll get better at overtime. 1341 00:56:22,306 --> 00:56:24,676 It's how well is your program design? 1342 00:56:24,676 --> 00:56:27,776 Do you have loops and conditions that are nested 1343 00:56:27,956 --> 00:56:30,716 like 10 layers deep such that the wrapping on the screen 1344 00:56:30,716 --> 00:56:32,366 and you could have just done something simpler? 1345 00:56:32,666 --> 00:56:35,086 Or, is it nice and compact and elegant? 1346 00:56:35,086 --> 00:56:37,466 And again, this is a message we'll send recurringly 1347 00:56:37,466 --> 00:56:39,446 throughout the term that you'll get better at. 1348 00:56:39,446 --> 00:56:41,926 But design against [inaudible] not just does it work, 1349 00:56:41,926 --> 00:56:43,886 but how well was it implemented. 1350 00:56:43,886 --> 00:56:45,906 And that's where the qualitative feedback comes into play. 1351 00:56:46,166 --> 00:56:48,346 And style frankly is one of the easiest things 1352 00:56:48,376 --> 00:56:51,876 to get right 'cause all you have to do is mimic the style 1353 00:56:51,876 --> 00:56:55,396 of code you see in sections and lectures, the CS50 style guide 1354 00:56:55,396 --> 00:56:56,636 which is mentioned in pset 1. 1355 00:56:56,896 --> 00:56:58,946 Style refers to the simple things like, 1356 00:56:59,166 --> 00:57:01,526 did you include comments in your code that tell you 1357 00:57:01,526 --> 00:57:02,716 and the reader what you're doing? 1358 00:57:02,956 --> 00:57:05,276 Did you indent your lines of code properly? 1359 00:57:05,276 --> 00:57:07,466 Did you give variables nice names? 1360 00:57:07,466 --> 00:57:08,806 And this is always the access 1361 00:57:08,806 --> 00:57:11,956 where people just throw away possible points frankly 1362 00:57:12,206 --> 00:57:14,166 because it's sort of the last thing you think about 1363 00:57:14,166 --> 00:57:15,146 and so you just don't worry 1364 00:57:15,146 --> 00:57:16,996 about hitting the spacebar properly, honestly. 1365 00:57:17,206 --> 00:57:20,406 Not only is it good style, it also helps you chase down bugs. 1366 00:57:20,466 --> 00:57:23,026 The worse thing to do is if your code is a big jumble 1367 00:57:23,126 --> 00:57:25,586 of indentation and you're like, "Ugh, I'll fix that later." 1368 00:57:25,886 --> 00:57:28,666 It's best to do it now because if you keep things nicely 1369 00:57:28,666 --> 00:57:30,756 and anally indented like I try to do in class, 1370 00:57:31,176 --> 00:57:33,366 you'll actually be able to spot potential bugs 1371 00:57:33,456 --> 00:57:34,966 and logical errors more easily. 1372 00:57:34,966 --> 00:57:36,526 So do try to get into that habit. 1373 00:57:36,786 --> 00:57:39,956 In terms of how we evaluate, it's really very course buckets 1374 00:57:39,956 --> 00:57:41,546 where 1 is poor, 5 is best. 1375 00:57:41,896 --> 00:57:43,926 So early on in the semester, the target range 1376 00:57:43,926 --> 00:57:47,926 for most students know is in the range of 2s, 3s, maybe some 4s, 1377 00:57:47,986 --> 00:57:52,176 generally not so much 5s but know that 3 is indeed good. 1378 00:57:52,176 --> 00:57:54,866 It does not translate a 3 out of 5, that's 60 percent, oh my god, 1379 00:57:54,866 --> 00:57:56,446 I got a D on the first pset. 1380 00:57:56,766 --> 00:58:00,676 3 is good and the course has this abstraction between letter, 1381 00:58:00,676 --> 00:58:01,876 grades and numbers because again, 1382 00:58:01,876 --> 00:58:04,126 we do focus very much on the qualitative. 1383 00:58:04,546 --> 00:58:07,126 So if I may, I have to put on a scary voice for just a moment. 1384 00:58:07,416 --> 00:58:10,096 The course goes to great length in the syllabus. 1385 00:58:10,096 --> 00:58:13,036 And on page 2 of every problem set to emphasize the rules 1386 00:58:13,036 --> 00:58:15,996 on academic honesty which frankly essentially reduce 1387 00:58:15,996 --> 00:58:18,356 to your welcome and you're encouraged to talk 1388 00:58:18,356 --> 00:58:20,766 about problem sets, go up at white boards with friends, 1389 00:58:20,796 --> 00:58:22,476 discuss things in the D-holes and the like. 1390 00:58:22,706 --> 00:58:24,796 So long as those conversations are in pseudo code 1391 00:58:24,796 --> 00:58:27,616 or in English, and so long as they stop at the point 1392 00:58:27,616 --> 00:58:29,596 of actually communicating in code. 1393 00:58:29,596 --> 00:58:32,576 The line is crossed when the conversation becomes, "Oh, 1394 00:58:32,706 --> 00:58:33,996 can you send me your file?" 1395 00:58:33,996 --> 00:58:35,666 or "Can I see what's on your screen?" 1396 00:58:35,936 --> 00:58:38,226 When the line is crossed again, when code, 1397 00:58:38,226 --> 00:58:39,736 the actual code is involved. 1398 00:58:39,736 --> 00:58:42,136 And again, just to send this message home 1399 00:58:42,326 --> 00:58:43,456 and it gives us no pleasure 1400 00:58:43,456 --> 00:58:44,976 to cite these statistics every year. 1401 00:58:45,516 --> 00:58:52,826 [ Pause ] 1402 00:58:53,326 --> 00:58:55,256 >> So this is a slide I keep updating every year 1403 00:58:55,256 --> 00:58:57,436 and it is the number of students this course alone has add 1404 00:58:57,436 --> 00:59:00,316 boarded in the past 4 years, and it gives us no pleasure 1405 00:59:00,316 --> 00:59:03,736 to give people mandatory time off from the college. 1406 00:59:03,736 --> 00:59:07,096 But realize it happens, but realize it happens only 1407 00:59:07,096 --> 00:59:08,036 in this egregious cases 1408 00:59:08,036 --> 00:59:10,056 where again the conversations have evolved into, 1409 00:59:10,376 --> 00:59:11,366 "Oh my god, its 2 A.M." 1410 00:59:11,366 --> 00:59:13,766 Just send me your code and I will submit that. 1411 00:59:13,766 --> 00:59:16,156 Or it's over sharing or it clearly you're sitting side 1412 00:59:16,156 --> 00:59:17,616 by side typing up the same thing. 1413 00:59:17,816 --> 00:59:19,546 Again, it's very easy to navigate these waters 1414 00:59:19,546 --> 00:59:22,566 and pedagogically by all means converse with classmates, 1415 00:59:22,606 --> 00:59:24,106 white boards, dining halls and the like. 1416 00:59:24,416 --> 00:59:26,336 But again the line is crossed per the syllabus 1417 00:59:26,336 --> 00:59:27,626 and the fine print in each project 1418 00:59:27,966 --> 00:59:32,616 when code is actually shared visually or electronically. 1419 00:59:33,336 --> 00:59:34,196 Okay, non-scary voice. 1420 00:59:34,656 --> 00:59:39,226 Typecasting, so typecasting, is this notion that we saw earlier 1421 00:59:39,226 --> 00:59:40,766 of converting one type to another, 1422 00:59:40,766 --> 00:59:43,196 but it's actually more useful than just these simple things 1423 00:59:43,196 --> 00:59:45,256 of [inaudible] and the like because one 1424 00:59:45,256 --> 00:59:46,576 of the domains we'll be focusing 1425 00:59:46,576 --> 00:59:49,126 on this week is that of cryptography. 1426 00:59:49,186 --> 00:59:52,146 And for those slightly familiar, what is cryptography in English? 1427 00:59:52,146 --> 00:59:53,356 [ Inaudible Answer ] 1428 00:59:53,356 --> 00:59:54,456 >> Coding, yeah. 1429 00:59:54,736 --> 00:59:58,886 So it's the art of encrypting or scrambling information, right? 1430 00:59:58,886 --> 01:00:00,616 So back in grade school if you wanted 1431 01:00:00,616 --> 01:00:03,566 to pass your crush a secret note and you want to hide it 1432 01:00:03,606 --> 01:00:06,726 from the teacher, you might say like some message 1433 01:00:06,726 --> 01:00:08,376 but then you might rotate all of the letters 1434 01:00:08,376 --> 01:00:08,976 by some number of places. 1435 01:00:09,176 --> 01:00:12,036 >> So if you wanted to stay in the letter A, B, 1436 01:00:12,036 --> 01:00:15,966 C to your crush, you would say B, C, D or something simple 1437 01:00:15,966 --> 01:00:18,266 like that but enough sufficiently complex 1438 01:00:18,266 --> 01:00:20,396 that when the teacher grabs the piece of paper from you 1439 01:00:20,396 --> 01:00:22,786 and intercepts it, it's not clear what it is unless he 1440 01:00:22,786 --> 01:00:23,626 or she actually tries 1441 01:00:23,626 --> 01:00:26,646 to decipher the actual encryption you were using 1442 01:00:26,646 --> 01:00:27,076 back then. 1443 01:00:27,076 --> 01:00:28,986 Now, thankfully fast forward to modern day, 1444 01:00:29,756 --> 01:00:31,626 you can do much more sophisticated things 1445 01:00:31,626 --> 01:00:33,336 and encryption is something we take for granted. 1446 01:00:33,336 --> 01:00:36,176 Anytime you buy something online, every time you log 1447 01:00:36,176 --> 01:00:39,506 in to Facebook using https and word of Firesheep and all 1448 01:00:39,506 --> 01:00:41,746 of your friends trying to hijack your accounts, 1449 01:00:41,746 --> 01:00:42,906 you are using encryption. 1450 01:00:43,126 --> 01:00:46,146 Even things like the 33 percent of you with iPhones 1451 01:00:46,146 --> 01:00:47,546 and 17 percent with Androids. 1452 01:00:47,906 --> 01:00:50,796 Typically, the iPhone these days is automatically encrypted 1453 01:00:50,966 --> 01:00:52,066 with a secret key. 1454 01:00:52,256 --> 01:00:55,096 And so if your phone is ever lost and you enable 1455 01:00:55,096 --> 01:00:57,546 that feature called remote wipe where you can send the message 1456 01:00:57,546 --> 01:00:59,266 to Apple and they can send the message to your phone 1457 01:00:59,266 --> 01:01:01,976 that says Delete It, all the phone is doing these days is 1458 01:01:01,976 --> 01:01:04,046 just forgetting that secret key. 1459 01:01:04,236 --> 01:01:06,776 And without that key, all it looks like is random zeros 1460 01:01:06,776 --> 01:01:08,916 and ones to the bad guy that just stole your phone. 1461 01:01:08,916 --> 01:01:10,626 So there is a lot of applications just 1462 01:01:10,626 --> 01:01:13,346 to very useful consumer things and we'll introduce this week 1463 01:01:13,346 --> 01:01:16,256 and in problem set 2, this arts of cryptography. 1464 01:01:16,646 --> 01:01:22,046 And it pertains to a little something called typecasting 1465 01:01:22,046 --> 01:01:22,676 in this case. 1466 01:01:22,676 --> 01:01:24,436 I'm gonna go ahead and open 1467 01:01:24,536 --> 01:01:28,176 up ASCII.c. Another example that's been prepared here 1468 01:01:28,456 --> 01:01:29,546 that apparently does this. 1469 01:01:29,826 --> 01:01:32,016 We got a loop at the top, a for loop, 1470 01:01:32,186 --> 01:01:34,676 and we got some printf then another for loop. 1471 01:01:34,676 --> 01:01:37,136 And notice inside of each loop, this is just 1472 01:01:37,136 --> 01:01:41,616 for demonstration purposes, I've hard coded the number 65 and 26. 1473 01:01:41,876 --> 01:01:44,296 Well, 26 is probably the English alphabet. 1474 01:01:44,676 --> 01:01:46,716 What was 65 if you recall from last week? 1475 01:01:46,716 --> 01:01:47,086 [ Inaudible Answer ] 1476 01:01:47,086 --> 01:01:50,586 >> Yeah, so capital A. So this is just a simple little 1477 01:01:50,586 --> 01:01:51,936 diagnostic program I wrote 1478 01:01:52,056 --> 01:01:55,576 to show me what the number equivalent is of every character 1479 01:01:55,576 --> 01:01:57,866 on my keyboard, at least the alphabetical characters. 1480 01:01:58,126 --> 01:02:01,166 So I seem to have a loop that is starting at 65, it's not zero 1481 01:02:01,166 --> 01:02:03,456 but it's totally legit to start somewhere else it makes more 1482 01:02:03,456 --> 01:02:06,706 sense, I'm iterating up to 65 plus 26 1483 01:02:06,976 --> 01:02:10,206 so that'll give me all the way to Z hopefully, I plus plus. 1484 01:02:10,206 --> 01:02:11,666 And now notice what's happening here. 1485 01:02:11,946 --> 01:02:13,476 I'm printing out percent C 1486 01:02:13,476 --> 01:02:16,706 which is a character then a colon then percent D, 1487 01:02:16,706 --> 01:02:17,786 and what am I plugging in? 1488 01:02:18,076 --> 01:02:20,736 Well, notice I'm plugging an I both times 1489 01:02:21,006 --> 01:02:25,396 but the very first time I am "casting it" to a char 1490 01:02:25,496 --> 01:02:28,936 because I wanna actually see I not as the number it is 1491 01:02:28,986 --> 01:02:31,426 but as the char, the character it represents 1492 01:02:31,676 --> 01:02:36,656 so I might see A 65, B 66 and so forth 1493 01:02:36,776 --> 01:02:39,686 and then take a guess here, 97 represents what? 1494 01:02:39,686 --> 01:02:40,566 [ Inaudible Answer ] 1495 01:02:40,566 --> 01:02:42,766 >> Lower case A, so let's see this in action. 1496 01:02:42,766 --> 01:02:44,296 So let me make this a little bigger. 1497 01:02:44,296 --> 01:02:48,946 This is again called ASCII 1 and ASCII again is just the acronym 1498 01:02:48,946 --> 01:02:51,836 that denotes the code use to map letters, to numbers 1499 01:02:51,836 --> 01:02:55,696 and vice versa so make ASCII 1, everything compiled okay. 1500 01:02:55,696 --> 01:02:57,826 Let me go and run ASCII and indeed, 1501 01:02:57,826 --> 01:02:59,436 it's a little long of a list. 1502 01:02:59,616 --> 01:03:01,536 But if I scroll all the way back up, 1503 01:03:01,856 --> 01:03:03,666 that's exactly what I had promised. 1504 01:03:03,876 --> 01:03:05,786 So we can now see this conversion here. 1505 01:03:05,976 --> 01:03:06,986 Now what's the relevance? 1506 01:03:06,986 --> 01:03:09,066 Well again, think back to grade school if you ever did send one 1507 01:03:09,066 --> 01:03:13,096 of this silly notes, well you had it rotate or convert letters 1508 01:03:13,316 --> 01:03:15,916 to some other letters, well, how did you really do that? 1509 01:03:15,916 --> 01:03:19,056 Well you shifted them one, A becomes B, B becomes C, 1510 01:03:19,346 --> 01:03:21,506 but how do you express that if you want a computer 1511 01:03:21,506 --> 01:03:24,146 to do it not-- and do it way more sophisticatedly 1512 01:03:24,146 --> 01:03:25,706 than a small kid could, well, 1513 01:03:25,706 --> 01:03:27,776 you probably wanna convert the letter like A 1514 01:03:27,776 --> 01:03:30,376 to a number then do your math like plus one 1515 01:03:30,376 --> 01:03:32,076 and then covert it back to a letter 1516 01:03:32,076 --> 01:03:34,316 where we already have that capability here. 1517 01:03:34,316 --> 01:03:36,466 Let me go ahead and open up a variance of this. 1518 01:03:36,556 --> 01:03:41,396 If I go and open up ASCII 2, we'll see this. 1519 01:03:41,396 --> 01:03:45,016 In ASCII two, I'm doing something a little prettier 1520 01:03:45,016 --> 01:03:46,466 'cause they've got a little overwhelming 1521 01:03:46,466 --> 01:03:47,916 to have it all display on the screen. 1522 01:03:48,136 --> 01:03:51,906 So again, just to mention that format code I mentioned earlier, 1523 01:03:51,906 --> 01:03:55,436 present 3D means no matter what put three-- 1524 01:03:55,436 --> 01:03:57,976 use three characters here to represent the number 1525 01:03:57,976 --> 01:04:00,006 and just prepend it with white space, 1526 01:04:00,206 --> 01:04:02,216 just the spacebar character automatically. 1527 01:04:02,496 --> 01:04:03,386 So what is this gonna do? 1528 01:04:03,386 --> 01:04:06,146 Well this is just a different aesthetic, I'm gonna go ahead 1529 01:04:06,146 --> 01:04:09,596 and make ASCII 2, the code is almost exactly the same 1530 01:04:09,596 --> 01:04:12,196 and then I'm gonna run ASCII 2, and what happens? 1531 01:04:12,446 --> 01:04:13,616 Well now I just got a chart. 1532 01:04:14,486 --> 01:04:17,006 So again fairly simple, esthetic modification 1533 01:04:17,006 --> 01:04:18,516 but its a little more user friendly now, 1534 01:04:18,756 --> 01:04:22,176 but I wanted everything to line up so notice again, percent C, 1535 01:04:22,176 --> 01:04:24,926 percent D but notice on the right hand side, 1536 01:04:24,926 --> 01:04:26,996 I actually said percent 3D. 1537 01:04:27,136 --> 01:04:28,496 Why did I use 3D? 1538 01:04:28,496 --> 01:04:30,996 Do you think for the third column and not for the first? 1539 01:04:30,996 --> 01:04:32,216 [ Inaudible Answer ] 1540 01:04:32,216 --> 01:04:35,546 >> Okay, I couldn't make any of that up. 1541 01:04:35,546 --> 01:04:37,446 But yes, so I was probably correct, right? 1542 01:04:37,446 --> 01:04:38,506 It's because the numbers 1543 01:04:38,556 --> 01:04:41,486 in the third column there actually are three 1544 01:04:41,486 --> 01:04:42,296 digits potentially. 1545 01:04:42,296 --> 01:04:44,186 Yes you start at 97, 98, 1546 01:04:44,186 --> 01:04:46,016 99 but then you get three digit numbers, 1547 01:04:46,216 --> 01:04:48,256 but for the capital letters, it was all single digit's 1548 01:04:48,526 --> 01:04:50,766 so I could have said percent 2D 1549 01:04:50,946 --> 01:04:53,116 but it wasn't necessary 'cause I know they're all gonna be two 1550 01:04:53,116 --> 01:04:54,076 digit's, but that's all. 1551 01:04:54,076 --> 01:04:56,286 So we're just now massaging the output aesthetically 1552 01:04:56,576 --> 01:04:59,386 by using this desperate format codes. 1553 01:04:59,676 --> 01:05:01,226 Well, let see if we can do something a little more 1554 01:05:01,226 --> 01:05:03,146 interesting than just printout this letters. 1555 01:05:03,356 --> 01:05:05,546 So this is not a complete implementation 1556 01:05:06,006 --> 01:05:09,226 but it is a demonstration of a game 1557 01:05:09,226 --> 01:05:11,256 that you might recall known as Battleship. 1558 01:05:11,816 --> 01:05:14,616 So Battleship, remember, actually even 1559 01:05:14,616 --> 01:05:15,226 if that sounds cool, 1560 01:05:15,226 --> 01:05:16,676 you're gonna be very underwhelmed in a second. 1561 01:05:17,076 --> 01:05:19,916 So if I make Battleship and then run Battleship, 1562 01:05:20,196 --> 01:05:20,996 that's all you're getting today. 1563 01:05:21,496 --> 01:05:23,676 So we have the layout of a battleship board. 1564 01:05:23,676 --> 01:05:24,636 This is that like little game 1565 01:05:24,636 --> 01:05:26,256 with the plastic board that's got all the holes, 1566 01:05:26,256 --> 01:05:29,696 you put the pegs in and a little fake ships and you say, 1567 01:05:29,776 --> 01:05:31,626 "A2" and you sank your battleship, 1568 01:05:32,236 --> 01:05:36,156 that kind of game, feeling familiar. 1569 01:05:36,156 --> 01:05:37,706 If-- yeah, [laughs] so, it's really-- 1570 01:05:37,706 --> 01:05:39,546 I sound stupid, I sounded saying that. 1571 01:05:39,546 --> 01:05:42,136 So with Battleship, we have this opportunity though 1572 01:05:42,136 --> 01:05:44,796 to format this output in a little interesting way, right? 1573 01:05:44,796 --> 01:05:47,276 I clearly want a grid of some sort and I also need 1574 01:05:47,276 --> 01:05:49,326 to be careful with like the number 10 'cause all 1575 01:05:49,326 --> 01:05:51,556 of a sudden my grade could get screwed up because all 1576 01:05:51,556 --> 01:05:53,006 of a sudden I have a two digit number. 1577 01:05:53,246 --> 01:05:55,166 So just to see how we did this, let's scroll 1578 01:05:55,166 --> 01:05:57,676 up to the code here, it's starting to look more complex 1579 01:05:57,776 --> 01:05:59,236 but it's just a repetition 1580 01:05:59,476 --> 01:06:01,266 of this same constructs from before. 1581 01:06:01,486 --> 01:06:03,706 So I'm first gonna print the top row of numbers. 1582 01:06:03,966 --> 01:06:05,966 So I printout back slash N space, space, 1583 01:06:05,966 --> 01:06:08,406 space and I just figured that out honestly by trial and error. 1584 01:06:08,406 --> 01:06:10,296 I want a little white space and then I want 1585 01:06:10,296 --> 01:06:11,666 to indent the board a little bit. 1586 01:06:11,986 --> 01:06:12,866 What do I do next? 1587 01:06:13,196 --> 01:06:17,446 I wanna print one through ten and so here I have percent D, 1588 01:06:17,446 --> 01:06:19,426 again and again and notice a couple of white spaces 1589 01:06:19,426 --> 01:06:20,676 after each for esthetic. 1590 01:06:20,676 --> 01:06:23,886 So that's actually pretty straight forward but now here, 1591 01:06:23,886 --> 01:06:25,296 notice we have our first example 1592 01:06:25,296 --> 01:06:28,136 of a nested loop whereby have an outer loop 1593 01:06:28,136 --> 01:06:30,616 where convention says use I as your counter. 1594 01:06:30,616 --> 01:06:33,206 So I is zero, go up to 10, I plus plus, 1595 01:06:33,636 --> 01:06:35,066 notice now, what do I wanna do? 1596 01:06:35,526 --> 01:06:36,776 What am I doing here? 1597 01:06:37,466 --> 01:06:39,726 Percent C, so remember the output 1598 01:06:39,726 --> 01:06:41,196 of the screen did this for me. 1599 01:06:41,296 --> 01:06:43,776 I had all the letters of the alphabet on the left hand side, 1600 01:06:44,096 --> 01:06:46,236 well, what am I doing then with this percent C? 1601 01:06:46,236 --> 01:06:48,436 That's how I'm getting every letter of the alphabet. 1602 01:06:49,066 --> 01:06:50,996 Let me scroll this back here. 1603 01:06:51,466 --> 01:06:53,556 Why am I doing A plus I? 1604 01:06:53,666 --> 01:06:56,856 This feels a little weird. 1605 01:06:57,606 --> 01:06:57,706 Yeah. 1606 01:06:57,706 --> 01:06:58,496 [ Inaudible Remark ] 1607 01:06:58,496 --> 01:07:01,086 >> Yeah, exactly. 1608 01:07:01,086 --> 01:07:03,796 I wanna change the letter I'm printing on each iteration 1609 01:07:03,796 --> 01:07:05,986 by adding one to it again and again. 1610 01:07:06,366 --> 01:07:09,456 So A, I know is gonna be represented 1611 01:07:09,456 --> 01:07:11,866 by some number, it happens to be 65. 1612 01:07:12,136 --> 01:07:14,376 It turns out that even though in the previous ASCII examples, 1613 01:07:14,376 --> 01:07:15,726 I was explicitly casting. 1614 01:07:15,726 --> 01:07:18,286 You actually don't need to explicitly cast C 1615 01:07:18,286 --> 01:07:19,976 in the compiler or smart enough to know 1616 01:07:20,156 --> 01:07:22,226 that a char is an int and an int is a char. 1617 01:07:22,226 --> 01:07:25,296 At the end of the day, the only difference is how you interpret 1618 01:07:25,296 --> 01:07:26,326 those zeros and ones. 1619 01:07:26,676 --> 01:07:29,086 Now how am I gonna interpret the zeros and ones here? 1620 01:07:29,206 --> 01:07:31,086 Well printf is gonna print the character. 1621 01:07:31,276 --> 01:07:33,966 So it doesn't matter if I past in a char specifically 1622 01:07:33,966 --> 01:07:36,816 or an int, it's going to be displayed as the character 1623 01:07:37,076 --> 01:07:39,316 so I can now use the short hand notation of start 1624 01:07:39,316 --> 01:07:43,206 with the letter A, capital A, whatever that is plus I to it 1625 01:07:43,206 --> 01:07:44,956 which initially is gonna be zero. 1626 01:07:45,236 --> 01:07:46,346 So what's A plus zero? 1627 01:07:47,596 --> 01:07:50,466 It's just A 'cause at 65 plus zero is 65 and it's printed 1628 01:07:50,466 --> 01:07:52,776 as a char so it's A, but on the next iteration, 1629 01:07:52,776 --> 01:07:55,036 I is 1 and then 2 and then 3, dot, dot, dot, 1630 01:07:55,036 --> 01:07:58,186 25 and so in this way, can I increment even letters 1631 01:07:58,186 --> 01:08:01,796 of the alphabet from A to Z. Now notice this in-- 1632 01:08:02,146 --> 01:08:03,776 nested loop so to speak. 1633 01:08:03,776 --> 01:08:06,056 Nested in the sense that it's indented in inside 1634 01:08:06,056 --> 01:08:09,426 of the outer most curly braces, so J gets 1, 1635 01:08:09,616 --> 01:08:11,776 J is less than 10, what is this doing? 1636 01:08:12,196 --> 01:08:14,946 This is just printing out zero, zero, zero, zero, zero. 1637 01:08:15,406 --> 01:08:17,086 So what's really going on here? 1638 01:08:17,086 --> 01:08:18,936 This is actually a very common example 1639 01:08:19,536 --> 01:08:21,046 of using 2 nested for loops. 1640 01:08:21,046 --> 01:08:24,076 If you want to printout rows and columns, 1641 01:08:24,076 --> 01:08:26,316 you essentially need two loops, generally for loops 1642 01:08:26,356 --> 01:08:30,556 by convention and so the outer most loop is doing what? 1643 01:08:30,626 --> 01:08:32,966 Iterating from right to left or top to bottom? 1644 01:08:32,966 --> 01:08:35,256 >> Top to bottom. 1645 01:08:35,826 --> 01:08:37,636 >> So it's actually doing top to bottom. 1646 01:08:37,636 --> 01:08:40,616 So the outer most loop involving I is top to bottom 1647 01:08:40,676 --> 01:08:43,586 and the inner most loop is doing the columns. 1648 01:08:43,586 --> 01:08:44,806 And you can infer this again 1649 01:08:44,806 --> 01:08:46,236 because if I'm printing the holes 1650 01:08:46,236 --> 01:08:49,576 by using this lower case O, that's happening inside the loop 1651 01:08:49,576 --> 01:08:51,876 and this make sense because if this thing is like a typewriter, 1652 01:08:52,166 --> 01:08:54,596 you better start from the top and go right, right, right, 1653 01:08:54,596 --> 01:08:57,376 right because the moment you go down, that's it. 1654 01:08:57,376 --> 01:08:59,656 You can't go back and add anything to the previous line 1655 01:08:59,656 --> 01:09:01,586 as we discuss earlier with backslash N. 1656 01:09:01,876 --> 01:09:04,606 So the outer most loop I is essentially gonna iterate 1657 01:09:04,606 --> 01:09:08,246 over our rows but on each iteration of a row, 1658 01:09:08,246 --> 01:09:10,606 we wanna go dot, dot, dot, dot, dot from left to right 1659 01:09:11,166 --> 01:09:12,836 to generate all of these zeros. 1660 01:09:12,996 --> 01:09:15,316 Now this is not by means of full-fledged game, 1661 01:09:15,316 --> 01:09:16,856 it's just the esthetics of the board 1662 01:09:16,856 --> 01:09:19,806 and the very last line is just to printout some blank space 1663 01:09:19,886 --> 01:09:22,946 but it does demonstrate now how we can nest these kinds 1664 01:09:22,946 --> 01:09:23,376 of things. 1665 01:09:24,126 --> 01:09:26,136 So perhaps a little more familiar-- 1666 01:09:26,136 --> 01:09:30,126 a little more familiar is this example here. 1667 01:09:30,716 --> 01:09:33,796 So you might recall this incredibly annoying song 1668 01:09:33,796 --> 01:09:36,866 from great schools as well or school bus rides. 1669 01:09:37,266 --> 01:09:40,186 So 99 bottles of beer on the wall, 99 bottles of beer, 1670 01:09:40,186 --> 01:09:43,476 take one down, pass it around, 99 bottles of beer on the wall. 1671 01:09:43,476 --> 01:09:44,586 I also thought stupid saying that. 1672 01:09:44,856 --> 01:09:46,836 But there is the song that you might remember saying is 1673 01:09:46,906 --> 01:09:47,596 from childhood. 1674 01:09:47,806 --> 01:09:49,396 What's nice for our purposes today is 1675 01:09:49,396 --> 01:09:52,126 that it's obviously very cyclical which contributes 1676 01:09:52,126 --> 01:09:54,996 to it's annoyance but it's also an opportunity 1677 01:09:54,996 --> 01:09:56,966 to decrement this number again and again 1678 01:09:56,966 --> 01:09:58,856 and again while still doing a lot 1679 01:09:58,856 --> 01:10:00,826 of the same thing at the same time. 1680 01:10:01,266 --> 01:10:04,486 >> So let's see how this might be implemented at the end. 1681 01:10:04,706 --> 01:10:08,946 If I make beer1 and zoom in on this and hit Enter 1682 01:10:09,276 --> 01:10:12,606 and then type beer1, it's gonna prompt me for a number, 1683 01:10:12,606 --> 01:10:14,186 I'll say, "99" Enter, 1684 01:10:14,456 --> 01:10:16,636 it's pretty fast implementation of the song, right? 1685 01:10:16,636 --> 01:10:18,296 We can make it really tedious by inserting-- 1686 01:10:18,296 --> 01:10:19,136 [ Laughter ] 1687 01:10:19,136 --> 01:10:22,276 >> We can make it really tedious by inserting SLIP 1688 01:10:22,276 --> 01:10:23,046 or something like that. 1689 01:10:23,046 --> 01:10:25,456 But if I scroll up, I have indeed started all the way 1690 01:10:25,456 --> 01:10:27,956 up at the top 99 and counted it all of way down. 1691 01:10:28,266 --> 01:10:30,926 So let's just see one way in which we can do this. 1692 01:10:31,446 --> 01:10:33,996 So let me zoom in on my code, the very top says, 1693 01:10:33,996 --> 01:10:35,706 "How many bottles of beer will there be?" 1694 01:10:35,706 --> 01:10:36,916 and then I use get Ns. 1695 01:10:36,946 --> 01:10:41,596 So this is kind of old hat now, now here's a common convention. 1696 01:10:42,076 --> 01:10:44,716 If the user does not cooperate, you could pass 1697 01:10:44,716 --> 01:10:45,626 through them again and again 1698 01:10:45,826 --> 01:10:48,546 but I decided just a design decision, 1699 01:10:48,786 --> 01:10:51,076 if the users not cooperating, I'm not just gonna quit. 1700 01:10:51,466 --> 01:10:54,696 And if you wanna quit a program and see, you return a value 1701 01:10:54,696 --> 01:10:57,936 from main and you should return anything, except what? 1702 01:10:58,256 --> 01:10:58,646 >> Zero. 1703 01:10:58,646 --> 01:11:00,506 >> Zero, right, so zero generally means success 1704 01:11:00,686 --> 01:11:03,136 and that should be returned by default at the end 1705 01:11:03,136 --> 01:11:05,506 or if explicitly by you, if all went well, 1706 01:11:05,676 --> 01:11:07,036 but if something did not go well 1707 01:11:07,036 --> 01:11:08,646 like the user was just not cooperating, 1708 01:11:08,866 --> 01:11:10,986 you should probably return something other than zero 1709 01:11:10,986 --> 01:11:12,856 and the convention is at least return one 1710 01:11:13,096 --> 01:11:15,136 of if you wanna really have fine [inaudible] in control 1711 01:11:15,136 --> 01:11:17,566 to return one for the first possible error, 1712 01:11:17,566 --> 01:11:19,236 two for the next, three for the next 1713 01:11:19,526 --> 01:11:20,856 but anything other than zero. 1714 01:11:21,036 --> 01:11:24,066 Now if I don't return, I keep executing. 1715 01:11:24,256 --> 01:11:26,736 So if the user did give me a positive number, 1716 01:11:26,926 --> 01:11:29,766 I start singing this song, and how might I do this? 1717 01:11:30,066 --> 01:11:32,146 Well notice how we might structure this with the loop. 1718 01:11:32,436 --> 01:11:34,566 So I'm first gonna just printout a new line, 1719 01:11:34,836 --> 01:11:38,946 then I'm gonna iterate from I equals N down to zero, 1720 01:11:39,116 --> 01:11:41,446 so we did this before with our progress bar 1721 01:11:41,666 --> 01:11:44,786 and I'm gonna printout percent D bottles of beer on the wall I, 1722 01:11:45,166 --> 01:11:49,026 percent bottles of beer a beer, I, take one down pass it around, 1723 01:11:49,026 --> 01:11:51,226 that's just the same thing and I'll notice here, 1724 01:11:51,226 --> 01:11:54,966 here's how I go from 99 to 98, then the loop repeats, 1725 01:11:55,196 --> 01:11:57,136 then the loop repeats, dot, dot, dot. 1726 01:11:57,806 --> 01:11:59,216 But this is far from perfect. 1727 01:11:59,216 --> 01:12:01,746 Where are some opportunities for improvement here would you say? 1728 01:12:03,116 --> 01:12:03,206 Yeah. 1729 01:12:03,206 --> 01:12:05,086 [ Inaudible Answer ] 1730 01:12:05,086 --> 01:12:07,606 >> Yeah, this is really me cutting more corners, right? 1731 01:12:07,606 --> 01:12:09,296 This is me avoiding the issue 1732 01:12:09,296 --> 01:12:11,566 that at some point this program is gonna get down all the way 1733 01:12:11,566 --> 01:12:14,796 to the bottom and say one bottles of beer on the wall 1734 01:12:14,896 --> 01:12:16,686 and I didn't wanna look like I'd made a bug 1735 01:12:16,686 --> 01:12:20,406 so I just parenthesize the S for 98 other conditions just to say, 1736 01:12:20,406 --> 01:12:21,836 "Now, I'm handling both scenarios." 1737 01:12:22,086 --> 01:12:23,576 But clearly, we can do this better. 1738 01:12:23,576 --> 01:12:26,336 We could have some kind of the condition inside of this loop 1739 01:12:26,546 --> 01:12:31,266 and say if I equals equals one printed 1740 01:12:31,266 --> 01:12:36,136 as bottle otherwise printed as bottles, what else could we do? 1741 01:12:36,136 --> 01:12:36,366 [ Inaudible Answer ] 1742 01:12:36,366 --> 01:12:37,026 >> What's that? 1743 01:12:37,026 --> 01:12:37,386 [ Inaudible Answer ] 1744 01:12:37,386 --> 01:12:41,016 >> Okay, so we could have the loop, just go down to one 1745 01:12:41,056 --> 01:12:45,226 and then special case as we'll see the last chunk of this song 1746 01:12:45,386 --> 01:12:48,486 and just printout verbatim one bottle of beer on the wall 1747 01:12:48,486 --> 01:12:51,646 or zero whatever the lower case is that you might wanna include. 1748 01:12:52,466 --> 01:12:53,146 Any other thoughts? 1749 01:12:53,146 --> 01:12:54,926 [ Inaudible Remark ] 1750 01:12:54,926 --> 01:13:01,886 >> So you could even be a little more efficient than instead 1751 01:13:01,886 --> 01:13:04,076 of conditionally spitting out one sentence or the other, 1752 01:13:04,076 --> 01:13:06,876 you could just special case the S, printout the singular 1753 01:13:07,166 --> 01:13:10,616 and then just printout the yes optionally yes or no. 1754 01:13:10,876 --> 01:13:12,836 So if I go back in here, let's see. 1755 01:13:12,836 --> 01:13:16,486 Beer1, the biggest number we can represent 1756 01:13:16,486 --> 01:13:21,176 with int is roughly 2 billion-- oh, dammit [laughs], alright? 1757 01:13:22,096 --> 01:13:25,676 That's a command line arguments more on that next time. 1758 01:13:25,866 --> 01:13:29,606 1, 2, 3, 4, 5, 6, 7, 8, 9-- alright. 1759 01:13:30,926 --> 01:13:38,916 Why don't we leave it there and we'll see you on Wednesday. 1760 01:13:39,416 --> 01:13:42,280 [ Noise ]