1 00:00:10,136 --> 00:00:13,326 >> David: Welcome back to CS50, and welcome now 2 00:00:13,326 --> 00:00:14,956 that you are officially enrolled. 3 00:00:14,956 --> 00:00:17,096 This is the end of week one. 4 00:00:17,476 --> 00:00:19,796 So the enrollment numbers are in 5 00:00:19,796 --> 00:00:21,776 and we thought we would share some statistics, 6 00:00:21,776 --> 00:00:24,606 as we like to do in this course; being computer scientists, 7 00:00:24,606 --> 00:00:25,976 we tend to gather lots of data. 8 00:00:26,236 --> 00:00:29,106 Rather than show it to you though first in chart form, 9 00:00:29,366 --> 00:00:33,356 I thought I would show it to you in CS50 program style. 10 00:00:33,356 --> 00:00:35,926 So I wrote a little program called "chart" that, 11 00:00:35,926 --> 00:00:38,366 based on some data I happened to hardcode into the program, 12 00:00:38,366 --> 00:00:41,166 shows us their numbers for the past several years. 13 00:00:41,406 --> 00:00:43,406 You at this point don't see what I'm seeing 14 00:00:43,406 --> 00:00:44,666 because I haven't hit this button, 15 00:00:44,936 --> 00:00:46,486 but now you'll actually see 16 00:00:46,486 --> 00:00:48,316 that I've brought up a terminal window. 17 00:00:48,316 --> 00:00:52,386 So I have so-called SSH'd into the cloud.CS50.net. 18 00:00:52,386 --> 00:00:54,806 I have connected to the course's central server 19 00:00:54,806 --> 00:00:58,116 from my own laptop and via dot slash chart am I 20 00:00:58,116 --> 00:01:01,926 about to run a little program that depicts as best we can 21 00:01:01,926 --> 00:01:03,936 with what with generally call ASCII art -- 22 00:01:04,016 --> 00:01:06,926 it's really all you can do in a terminal window like this -- 23 00:01:07,176 --> 00:01:09,146 our numbers from the past few years. 24 00:01:09,146 --> 00:01:18,506 So there we were 2007, 2008, 2009, and bam. 25 00:01:18,556 --> 00:01:19,576 So this is you. 26 00:01:19,576 --> 00:01:22,236 We are honestly so happy to have you all 27 00:01:22,236 --> 00:01:23,276 in the course this semester. 28 00:01:23,276 --> 00:01:24,956 It actually seems, based on a quick glance, 29 00:01:24,956 --> 00:01:27,896 that about 225 people are still in bed today. 30 00:01:28,236 --> 00:01:30,136 But that's okay. 31 00:01:30,136 --> 00:01:33,406 This will be our last Friday lecture ever; back on Mondays 32 00:01:33,406 --> 00:01:34,496 and Wednesdays hereafter. 33 00:01:34,716 --> 00:01:36,566 But this honestly really gratifying 34 00:01:36,566 --> 00:01:37,576 for us, for the staff. 35 00:01:37,576 --> 00:01:39,676 Because we have, as you've hopefully realized, 36 00:01:39,866 --> 00:01:41,766 made a concerted effort over the past several years 37 00:01:41,766 --> 00:01:43,496 to really reach out to students on this campus 38 00:01:43,726 --> 00:01:46,636 who might not otherwise have considered this as a field 39 00:01:46,636 --> 00:01:48,786 of exploration, just a one semester course, 40 00:01:48,786 --> 00:01:51,806 or even ultimately a concentration or minor. 41 00:01:51,806 --> 00:01:54,866 So it is my promise to you and it is the huge staff's promise 42 00:01:54,866 --> 00:01:58,086 to you that we will absolutely get you through this course 43 00:01:58,086 --> 00:02:00,116 and you will have a good time doing so. 44 00:02:00,116 --> 00:02:02,196 Just to give you another several fun facts -- 45 00:02:02,196 --> 00:02:04,306 and next week, once we've looked at your Problem Set 46 00:02:04,686 --> 00:02:07,096 "Zero Submissions," which, if you haven't filled it out yet, 47 00:02:07,096 --> 00:02:08,886 you'll see asks a few demographic questions, 48 00:02:08,886 --> 00:02:12,446 a few geek-type questions so we can get a sense of the students. 49 00:02:13,916 --> 00:02:16,756 We have this breakdown in the class right now. 50 00:02:16,756 --> 00:02:19,446 So historically, the course is a lot of sophomores who, 51 00:02:19,446 --> 00:02:22,626 like me perhaps, are realizing that they finally have time 52 00:02:22,626 --> 00:02:24,506 to explore beyond their own interests. 53 00:02:24,706 --> 00:02:26,096 But it's a pretty good breakdown. 54 00:02:26,096 --> 00:02:27,096 So if you've been thinking you're, like, 55 00:02:27,096 --> 00:02:30,096 the only senior here who's taking this course: not at all, 56 00:02:30,096 --> 00:02:32,456 there's about a hundred of you, there's about a hundred juniors, 57 00:02:32,736 --> 00:02:34,346 there's a couple hundred sophomores 58 00:02:34,346 --> 00:02:35,836 and about a couple hundred freshman. 59 00:02:35,836 --> 00:02:37,236 So it's a really nice demographic. 60 00:02:37,406 --> 00:02:38,986 We also have graduate students 61 00:02:38,986 --> 00:02:40,726 from the Graduate School of Design, GSAS. 62 00:02:40,726 --> 00:02:43,396 We have 90 extension school students 63 00:02:43,396 --> 00:02:45,716 who are taking the course via distance education, 64 00:02:45,716 --> 00:02:47,916 so we have a huge group that will be joining us 65 00:02:47,996 --> 00:02:51,576 at the 2010 CS50 fair and the those photos you saw a moment 66 00:02:51,576 --> 00:02:54,266 ago depicted 2009's. 67 00:02:54,306 --> 00:02:56,946 We're also happy to say we're not quite at 50% yet, 68 00:02:57,216 --> 00:03:00,806 but the proportion of women in CS50 is now up to 38%, 69 00:03:00,806 --> 00:03:03,066 which is its highest level in 21 years. 70 00:03:03,066 --> 00:03:07,976 So welcome, ladies, to CS50. 71 00:03:08,236 --> 00:03:13,576 So we left off last time with looking at a bit of coding 72 00:03:13,576 --> 00:03:15,516 and teasing you with the upcoming problem set. 73 00:03:15,726 --> 00:03:19,196 So some support structure that's in view for the coming week. 74 00:03:19,456 --> 00:03:21,706 So rather than assign you yet to sections -- 75 00:03:21,916 --> 00:03:24,606 which we will do starting this weekend or Monday, 76 00:03:24,606 --> 00:03:26,996 based on the uptake in enrollment, we're actually going 77 00:03:26,996 --> 00:03:28,726 to rework the schedule we had in mind, 78 00:03:28,726 --> 00:03:31,186 so we will announce via email now this we have your official 79 00:03:31,396 --> 00:03:32,806 Harvard email addresses on file -- 80 00:03:33,036 --> 00:03:34,826 we'll announce via email in the next couple days how 81 00:03:34,826 --> 00:03:37,596 to officially section for a specific comfort level and such, 82 00:03:37,886 --> 00:03:40,286 but for this first week, so that we actually have a support 83 00:03:40,386 --> 00:03:43,036 structure in place, we'll have what we call "supersections." 84 00:03:43,036 --> 00:03:45,276 So these are open to students of all comfort levels. 85 00:03:45,506 --> 00:03:47,236 There are three different ones on this Sunday, 86 00:03:47,236 --> 00:03:48,346 this Monday, this Tuesday. 87 00:03:48,626 --> 00:03:50,436 Check the website under "sections" 88 00:03:50,436 --> 00:03:52,376 for the times and locations. 89 00:03:52,626 --> 00:03:54,706 And one of them will be filmed, too, 90 00:03:54,926 --> 00:03:56,996 so that if you can't actually make any of the times not 91 00:03:56,996 --> 00:03:59,486 to worry, that video will go online sometime Tuesday 92 00:03:59,486 --> 00:04:01,946 in plenty of time for Friday's pset deadline. 93 00:04:02,236 --> 00:04:04,126 And we also have starting this Sunday, 94 00:04:04,336 --> 00:04:06,096 lead by one of the course's teaching fellows, 95 00:04:06,096 --> 00:04:08,206 what we call "walkthroughs," so this is kind 96 00:04:08,206 --> 00:04:09,436 of programming lexicon. 97 00:04:09,706 --> 00:04:13,406 The idea of a "code walkthrough" refers to the process of walking 98 00:04:13,406 --> 00:04:15,396 through your code verbally or visually with 99 00:04:15,396 --> 00:04:16,756 or without other people. 100 00:04:16,756 --> 00:04:20,316 What we do in these walkthroughs for CS50 is really walk you 101 00:04:20,316 --> 00:04:22,106 through the week's problem set. 102 00:04:22,106 --> 00:04:24,186 This is targeted at the so-called standard edition, 103 00:04:24,186 --> 00:04:25,736 which we expect most students to do. 104 00:04:25,976 --> 00:04:28,316 The hacker editions -- we don't offer specific walkthroughs 105 00:04:28,316 --> 00:04:30,376 since we assume that that crowd generally is ready 106 00:04:30,376 --> 00:04:31,856 to dive into the PDF alone. 107 00:04:32,076 --> 00:04:35,126 But for the majority of folks, know that this resource exists. 108 00:04:35,436 --> 00:04:37,216 I think it's hugely helpful to show 109 00:04:37,216 --> 00:04:39,916 up having read the PDF minimally so you actually get as much 110 00:04:39,916 --> 00:04:41,336 out of this experience as possible. 111 00:04:41,596 --> 00:04:42,666 But it's really a chance, 112 00:04:42,666 --> 00:04:45,196 especially for those less comfortable, to be set off 113 00:04:45,196 --> 00:04:46,306 on a very specific path. 114 00:04:46,576 --> 00:04:49,496 So you don't pick up a document that has a whole lot of Greek, 115 00:04:49,496 --> 00:04:51,206 or what looks like Greek to you and wonder 116 00:04:51,206 --> 00:04:52,276 "Where do I even begin?" 117 00:04:52,396 --> 00:04:53,426 I mean, we'll get you started. 118 00:04:53,426 --> 00:04:56,476 And that's what these will be for on a weekly basis for each 119 00:04:56,476 --> 00:04:57,876 of the remaining problem sets. 120 00:04:58,076 --> 00:05:01,706 So that is this Sunday at 7:00 p.m. Check the website under 121 00:05:01,706 --> 00:05:04,166 "problem sets" for its location. 122 00:05:04,466 --> 00:05:04,976 All right. 123 00:05:05,436 --> 00:05:07,756 So today is about writing some programs and empowering you 124 00:05:07,756 --> 00:05:12,296 to write your own starting this coming week when Pset One goes 125 00:05:12,296 --> 00:05:15,166 out via the website tonight by 7:00 p.m. 126 00:05:15,216 --> 00:05:16,756 So how to write a program. 127 00:05:16,756 --> 00:05:18,166 It's really a three-step process. 128 00:05:18,166 --> 00:05:19,756 You open what we call the "text editor," 129 00:05:20,016 --> 00:05:22,566 the specific one we used on Wednesday was called Nano, 130 00:05:22,566 --> 00:05:24,596 which is similar to Notepad or TextEdit -- 131 00:05:24,826 --> 00:05:27,396 totally simple, kind of dumbed down but in a useful way 132 00:05:27,606 --> 00:05:29,546 so that you can just focus on writing code 133 00:05:29,546 --> 00:05:30,896 and not learning some new interface. 134 00:05:31,136 --> 00:05:34,246 The only interesting commands recall were pretty much CTRL+X 135 00:05:34,246 --> 00:05:37,736 for Save and Quit and CTRL+O for just Save. 136 00:05:37,986 --> 00:05:40,686 You'll see in sections and in some tutorials online 137 00:05:40,686 --> 00:05:42,656 that we'll point you toward that there's other key strokes 138 00:05:42,656 --> 00:05:43,846 as well that will save you time. 139 00:05:44,066 --> 00:05:45,226 And for those of you who want to pick 140 00:05:45,226 --> 00:05:48,026 up a more sophisticated text editor, we will point you 141 00:05:48,026 --> 00:05:51,766 in the direction soon of tools called Vim, or Vi, Emacs. 142 00:05:51,766 --> 00:05:55,056 And then there are other client side IDEs we'll also empower 143 00:05:55,056 --> 00:05:55,426 you with. 144 00:05:55,466 --> 00:05:56,896 So all that's to come. 145 00:05:56,896 --> 00:05:59,746 But once you've written a program, "hello world," 146 00:05:59,746 --> 00:06:02,256 in a file called hello.c, you Save and Quit. 147 00:06:02,916 --> 00:06:06,166 You then run a compiler, in our case called "GCC" -- 148 00:06:06,166 --> 00:06:07,446 one of the world's most popular -- 149 00:06:07,626 --> 00:06:10,066 and then you can run it with some number of switches, 150 00:06:10,206 --> 00:06:12,686 these command line arguments as they're called 151 00:06:12,936 --> 00:06:15,576 that somehow influences the behavior of this program. 152 00:06:15,856 --> 00:06:19,056 So this commands, number two here -- 153 00:06:19,056 --> 00:06:20,806 can someone just quickly translate 154 00:06:20,806 --> 00:06:23,656 into the English what it is doing for us? 155 00:06:24,576 --> 00:06:24,646 Yeah? 156 00:06:25,216 --> 00:06:26,816 >> Compiling the program [inaudible], 157 00:06:27,186 --> 00:06:29,906 saving it under the name "hello," in the library -- 158 00:06:31,826 --> 00:06:32,306 >> David: Okay, good. 159 00:06:32,306 --> 00:06:34,426 So close. Let me tweak a little bit. 160 00:06:34,426 --> 00:06:37,386 So GCC is compiling the source code we wrote, 161 00:06:37,386 --> 00:06:41,456 which lives in a file called -- 162 00:06:41,456 --> 00:06:43,116 which lives in a file -- oh, that's right. 163 00:06:43,116 --> 00:06:45,906 Okay, let me correct myself before I correct you. 164 00:06:47,226 --> 00:06:47,786 There we go. 165 00:06:48,476 --> 00:06:51,896 So this GCC is going to compile the source code that's 166 00:06:51,896 --> 00:06:53,876 in the file called hello.c, 167 00:06:54,156 --> 00:06:57,136 but rather than get a stupid default name like a.out, 168 00:06:57,366 --> 00:07:00,766 I'm using this switch first -o for output and hello, 169 00:07:00,826 --> 00:07:03,226 which says actually store the zeroes and ones not 170 00:07:03,226 --> 00:07:05,866 in a file called a.out but in a more reasonably named filed 171 00:07:05,866 --> 00:07:06,716 called "hello." 172 00:07:06,826 --> 00:07:07,946 And now someone else perhaps, 173 00:07:07,946 --> 00:07:10,616 why did I include this -l switch followed 174 00:07:10,616 --> 00:07:12,026 by "cs50" at the tail end? 175 00:07:13,076 --> 00:07:16,336 >> That lets the compiler know that you want 176 00:07:16,336 --> 00:07:19,956 to use some actual compiled code from that library. 177 00:07:20,516 --> 00:07:21,006 >> David: Okay, good. 178 00:07:21,126 --> 00:07:25,446 So that is a flag that tells the compiler that I want to use 179 00:07:25,446 --> 00:07:29,526 or link into my own program code that someone else wrote 180 00:07:29,526 --> 00:07:33,396 that lives somewhere else on the system whose moniker is CS50. 181 00:07:33,396 --> 00:07:36,176 And we briefly saw or mention a couple of others: 182 00:07:36,386 --> 00:07:39,356 there's -lm if you want to use the math library 183 00:07:39,356 --> 00:07:42,526 which has functions like sin and cosine and round and other 184 00:07:42,526 --> 00:07:44,436 such things that you may want to use over time, 185 00:07:44,436 --> 00:07:46,396 but we'll point those out as needed; 186 00:07:46,906 --> 00:07:49,946 there's -lncurses we saw briefly, 187 00:07:49,946 --> 00:07:51,696 which is a very simple graphic library that did 188 00:07:51,696 --> 00:07:53,336 that funkiness toward the end of Wednesday; 189 00:07:53,336 --> 00:07:54,646 and there's a whole bunch of others. 190 00:07:54,686 --> 00:07:59,206 But the take away for now is that if you don't clue -lcs50, 191 00:07:59,206 --> 00:08:00,986 you get some mention -- some error message -- 192 00:08:00,986 --> 00:08:03,816 about "undefined symbol," and that should be your clue that, 193 00:08:03,986 --> 00:08:06,786 "Oh it's undefined in that I haven't told the compiler 194 00:08:06,896 --> 00:08:08,906 that I want to link in these zeros and ones." 195 00:08:08,906 --> 00:08:10,866 And then finally, this number three command does what? 196 00:08:11,376 --> 00:08:12,976 >> Just runs it. 197 00:08:13,046 --> 00:08:13,516 >> David: Just runs it. 198 00:08:13,516 --> 00:08:16,236 "dot slash hello" runs the program what do we often see 199 00:08:16,236 --> 00:08:18,766 if you omit -- little sanity check -- the "dot slash" ? 200 00:08:20,196 --> 00:08:21,636 >> An error. 201 00:08:22,046 --> 00:08:24,066 >> David: An error message of some sort like "file not found" 202 00:08:24,066 --> 00:08:25,316 or "commands not found." 203 00:08:25,316 --> 00:08:28,396 And that's because hello.c is something I just wrote -- 204 00:08:28,396 --> 00:08:30,196 it's not something I downloaded or bought 205 00:08:30,196 --> 00:08:32,146 and installed via double-clicking or anything 206 00:08:32,146 --> 00:08:33,726 like that -- because I wrote it, 207 00:08:34,046 --> 00:08:35,786 compiled it in my current directory, 208 00:08:35,916 --> 00:08:38,086 I have to be ever-so emphatic to the computer 209 00:08:38,086 --> 00:08:39,516 that it's actually right here. 210 00:08:39,516 --> 00:08:42,936 And "." signifies my current working directory, 211 00:08:42,936 --> 00:08:45,206 the folder that I am currently in. 212 00:08:45,466 --> 00:08:49,026 All right, so let's actually write something a little more 213 00:08:49,026 --> 00:08:50,566 interesting than "hello world." 214 00:08:50,566 --> 00:08:54,686 So among your printout from Wednesday, if you have it, 215 00:08:54,946 --> 00:08:58,406 you have a file called math1.c. If you don't have this, 216 00:08:58,626 --> 00:09:00,676 the programs are short today, so it should be easy enough 217 00:09:00,676 --> 00:09:02,286 to follow along visually, but a PDF 218 00:09:02,286 --> 00:09:04,836 and the actual source code is available online 219 00:09:04,836 --> 00:09:05,486 for these things. 220 00:09:06,346 --> 00:09:09,816 So based on initial impressions, 221 00:09:10,946 --> 00:09:14,386 what does this program do when you run in it? 222 00:09:14,596 --> 00:09:14,746 Yeah? 223 00:09:15,206 --> 00:09:17,886 >> It adds, like, x to y -- 224 00:09:18,206 --> 00:09:18,396 >> David: Okay, good. 225 00:09:18,396 --> 00:09:21,556 It adds x to y and stores it into z. But if someone wants 226 00:09:21,556 --> 00:09:23,916 to be a even a little more snarky, 227 00:09:23,916 --> 00:09:24,976 what does this program do? 228 00:09:25,181 --> 00:09:27,181 >> [ Inaudible ] 229 00:09:27,346 --> 00:09:27,716 >> David: Nothing, right? 230 00:09:27,716 --> 00:09:29,946 It actually doesn't do anything at the end of the day 231 00:09:30,116 --> 00:09:31,386 because I am performing this math. 232 00:09:31,596 --> 00:09:33,136 So it's doing something underneath the hood. 233 00:09:33,316 --> 00:09:34,736 It's doing something useful for me. 234 00:09:34,916 --> 00:09:37,906 It turns out not, but it does evince the fact 235 00:09:38,116 --> 00:09:41,036 that we have some basic syntax like mathematics and equal signs 236 00:09:41,036 --> 00:09:44,686 and whatnot with which we can now start using 237 00:09:44,686 --> 00:09:45,846 for more useful purposes. 238 00:09:46,016 --> 00:09:49,156 But let's just quickly point out what this actually does. 239 00:09:49,156 --> 00:09:51,286 And actually, could one of the TFs grab the laser point? 240 00:09:51,286 --> 00:09:52,876 Oh, here it is from Barry. 241 00:09:52,966 --> 00:09:56,216 So this line here declares a variable, 242 00:09:56,216 --> 00:09:59,096 just storage space called "x" and it's of type "int," 243 00:09:59,196 --> 00:10:00,966 which means an integer has to go in it. 244 00:10:01,306 --> 00:10:05,056 The equal sign is really called the "assignment operator." 245 00:10:05,056 --> 00:10:07,966 And one of the things neophytes trip over early on is 246 00:10:07,966 --> 00:10:10,586 that we'll soon see there's not only an equal sign operator, 247 00:10:10,586 --> 00:10:12,346 there's equals equals. 248 00:10:12,346 --> 00:10:14,276 And we'll see what that means in a moment, and it's just 249 00:10:14,276 --> 00:10:15,326 because they mean different things. 250 00:10:15,596 --> 00:10:18,186 And generally in this language called "C" as well as a lot 251 00:10:18,186 --> 00:10:20,966 of others, almost all of your lines have to end 252 00:10:20,966 --> 00:10:23,886 with semicolons, but not all; just almost all 253 00:10:23,886 --> 00:10:24,816 and we'll see the difference. 254 00:10:24,816 --> 00:10:27,036 Case in point: there's no semicolon here, 255 00:10:27,036 --> 00:10:29,476 there's no semicolon here, there's no semicolon here. 256 00:10:29,696 --> 00:10:32,446 So really, the rule of thumb if you think back to Scratch is 257 00:10:32,476 --> 00:10:34,546 that any time you write a statement -- 258 00:10:34,726 --> 00:10:37,296 something that does something in one line of code -- 259 00:10:37,516 --> 00:10:39,166 you generally need the semicolon there. 260 00:10:39,206 --> 00:10:40,876 But you'll get used to the syntax before long. 261 00:10:41,146 --> 00:10:42,876 Then I do the same thing on line two 262 00:10:42,876 --> 00:10:44,326 and I declare something called "y." 263 00:10:44,386 --> 00:10:45,936 I assign it a value of 2. 264 00:10:46,166 --> 00:10:48,236 I then create a third variable called "z," 265 00:10:48,566 --> 00:10:50,836 add those two digits together, store the sum. 266 00:10:51,126 --> 00:10:54,136 But at the end of the day, this program does in effect nothing 267 00:10:54,306 --> 00:10:55,756 because I'm not printing the answer, 268 00:10:55,936 --> 00:10:57,146 I'm not saving the answer, 269 00:10:57,146 --> 00:10:58,976 I'm not doing anything with the answer. 270 00:10:59,186 --> 00:11:01,026 And so when I run this program, 271 00:11:01,796 --> 00:11:03,696 nothing all that interesting happens. 272 00:11:03,696 --> 00:11:05,746 So let's go ahead and compile this gcc math1.c. 273 00:11:06,156 --> 00:11:08,836 And for time's sake, I'm not going 274 00:11:08,836 --> 00:11:11,346 to bother changing the name all of the time, at least for now. 275 00:11:12,246 --> 00:11:13,816 It compiles okay. 276 00:11:14,046 --> 00:11:15,596 The program is called a.out. 277 00:11:15,596 --> 00:11:18,706 So I run this, Enter, and therein lies the nothing. 278 00:11:18,986 --> 00:11:20,956 So let's actually do something a little more interesting. 279 00:11:20,956 --> 00:11:23,026 Let's look at a version two of this file. 280 00:11:23,306 --> 00:11:24,576 And for reference, incidentally, 281 00:11:24,576 --> 00:11:27,166 in case you ever forget what the point of some exercise was, 282 00:11:27,336 --> 00:11:29,736 I'll almost always comment the code up top 283 00:11:29,736 --> 00:11:31,016 with a quick sentence or two 284 00:11:31,016 --> 00:11:32,806 that reminds you what this program does. 285 00:11:33,036 --> 00:11:34,716 So here's one that slightly more interesting. 286 00:11:34,716 --> 00:11:37,716 And now in a sentence, what does this one actually do? 287 00:11:38,696 --> 00:11:39,836 >> Print the answer. 288 00:11:40,046 --> 00:11:40,546 >> David: Prints the answer, right? 289 00:11:40,546 --> 00:11:41,976 So that's the marginal improvement here 290 00:11:41,976 --> 00:11:43,686 and we've seen this syntax before. 291 00:11:43,876 --> 00:11:45,756 Last time we used printf generally 292 00:11:45,756 --> 00:11:49,266 to print a static string, like a hardcoded string, like "David," 293 00:11:49,266 --> 00:11:51,036 just for example's sake. 294 00:11:51,286 --> 00:11:53,716 But remember that we did introduce a function -- 295 00:11:53,886 --> 00:11:58,046 a little utility you can use -- to ask the user for some text, 296 00:11:58,306 --> 00:12:01,006 get it back, and put it in a variable because what's nice 297 00:12:01,006 --> 00:12:03,706 about printf -- and this is what the "f" implies, 298 00:12:03,706 --> 00:12:07,266 format strings -- I can embed inside my double quotes special 299 00:12:07,266 --> 00:12:11,806 strings like "%d" for digits, "%s" for strings, 300 00:12:11,806 --> 00:12:13,656 and there's a few others that we'll see over time. 301 00:12:13,996 --> 00:12:17,816 And that's really a placeholder for the comma-separated list 302 00:12:17,946 --> 00:12:20,556 of values that I put after those quotes. 303 00:12:20,556 --> 00:12:22,766 So it's a useful way of not quite knowing 304 00:12:22,766 --> 00:12:25,086 in advance what you want to put in a string, but being able 305 00:12:25,086 --> 00:12:28,566 to create strings, phrases, sentences, dynamically. 306 00:12:28,566 --> 00:12:30,996 And that's useful because clearly I don't need to know 307 00:12:30,996 --> 00:12:33,546 in advance what the answer is going to be, 308 00:12:33,546 --> 00:12:36,106 what that string is actually going to be. 309 00:12:36,106 --> 00:12:40,406 So just to reinforce this, I could run gcc math2.c 310 00:12:40,406 --> 00:12:43,836 but I'm getting a little tired of this a.out convention 311 00:12:43,836 --> 00:12:45,886 and recall that there's this utility called "make" 312 00:12:46,146 --> 00:12:48,636 that right now doesn't really improve much, 313 00:12:48,636 --> 00:12:49,966 other than give it a better name. 314 00:12:50,116 --> 00:12:51,856 But we'll soon see when the commands you need 315 00:12:51,856 --> 00:12:53,896 to type become longer and more complicated, 316 00:12:54,056 --> 00:12:56,236 they just become annoying to type and annoying to remember 317 00:12:56,236 --> 00:12:58,646 so make generally automates this process for us. 318 00:12:58,996 --> 00:13:00,306 The only difference is I don't type 319 00:13:00,336 --> 00:13:04,146 "make math2.c" I just type math2. 320 00:13:04,316 --> 00:13:08,996 Make then assumes I'm in a file called make math2.c 321 00:13:08,996 --> 00:13:10,226 and goes and finds it. 322 00:13:10,436 --> 00:13:12,696 Now notice the command -- this is not an error message -- 323 00:13:12,906 --> 00:13:14,956 make is first showing me the commands 324 00:13:14,956 --> 00:13:16,856 that it is executing on my behalf. 325 00:13:17,356 --> 00:13:20,046 So really, once we start writing more interesting programs, 326 00:13:20,296 --> 00:13:25,116 you would have to type out long annoying commands like that 327 00:13:25,116 --> 00:13:26,976 to just compile more sophisticated programs. 328 00:13:27,276 --> 00:13:29,226 And here we already see a hint that make is going 329 00:13:29,226 --> 00:13:30,886 to simplify all of that for us. 330 00:13:31,096 --> 00:13:33,486 In fact, as a little helper, notice what it's sort 331 00:13:33,486 --> 00:13:37,156 of automatically including for me: What's this thing here? 332 00:13:37,746 --> 00:13:37,946 >> CS50. 333 00:13:38,126 --> 00:13:39,596 >> David: Yeah, so that's the CS50 library. 334 00:13:39,596 --> 00:13:42,846 So just in case I happen to use the CS50 library, 335 00:13:43,126 --> 00:13:45,416 we've configured the cloud in such a way 336 00:13:45,416 --> 00:13:48,036 that make just always provides you with access to it. 337 00:13:48,126 --> 00:13:48,856 So that's useful. 338 00:13:49,086 --> 00:13:51,316 Always provides you with access to the math library 339 00:13:51,316 --> 00:13:52,296 because that's useful, too, 340 00:13:52,296 --> 00:13:53,466 so you don't always have to remember that. 341 00:13:53,466 --> 00:13:54,936 And then there's some other flags, 342 00:13:54,936 --> 00:13:56,626 and we won't spend too much time on these here. 343 00:13:57,226 --> 00:13:58,716 But just so you've been teased with them, 344 00:13:59,016 --> 00:14:03,566 "-ggdb" is a flag that's going to enable what are going 345 00:14:03,566 --> 00:14:05,126 to be called "debugging symbols." 346 00:14:05,126 --> 00:14:06,426 We'll see this in a couple of weeks. 347 00:14:06,786 --> 00:14:09,176 But for the next week or two when you're writing programs, 348 00:14:09,176 --> 00:14:11,416 at least for the first time, generally if you're trying 349 00:14:11,416 --> 00:14:13,116 to debug them like you might have been trying 350 00:14:13,116 --> 00:14:15,806 to troubleshoot Scratch, you're probably going to reason 351 00:14:15,806 --> 00:14:17,336 through it by looking through the your code -- 352 00:14:17,336 --> 00:14:19,676 top to bottom, maybe engage a staff member for help -- 353 00:14:19,976 --> 00:14:22,006 but your friend will also be printf. 354 00:14:22,476 --> 00:14:25,156 So don't underestimate the value for the next week or two 355 00:14:25,306 --> 00:14:27,196 of just inserting printf statements 356 00:14:27,196 --> 00:14:30,536 into your code temporarily just to print out what the value 357 00:14:30,536 --> 00:14:32,686 of some variable is, or some expression is, 358 00:14:32,886 --> 00:14:34,296 so that you can just do a sanity check. 359 00:14:34,296 --> 00:14:36,196 And then you can delete it once you're sure your code is 360 00:14:36,196 --> 00:14:36,746 working right. 361 00:14:36,986 --> 00:14:40,236 So printf itself is primitive but useful debugging tool. 362 00:14:40,486 --> 00:14:43,336 This thing here hints at more sophisticated approaches we'll 363 00:14:43,336 --> 00:14:43,876 soon take. 364 00:14:44,196 --> 00:14:49,426 Of this detail here, -std=C99 this programming language called 365 00:14:49,426 --> 00:14:52,376 C has been around for some time and it's evolved over time. 366 00:14:52,376 --> 00:14:54,816 In 1999 they added some nice features to it 367 00:14:54,946 --> 00:14:57,146 that make it a lot more pleasurable to actually code in; 368 00:14:57,146 --> 00:14:58,416 they eliminated some of the headaches. 369 00:14:58,736 --> 00:15:03,066 So we are actually using the C99 version of C, 370 00:15:03,316 --> 00:15:05,646 which really means nothing useful today other 371 00:15:05,646 --> 00:15:08,326 than to realize we are using that version. 372 00:15:08,406 --> 00:15:11,026 So if you're coming from prior background, 373 00:15:11,286 --> 00:15:12,796 your code might work a little differently 374 00:15:12,796 --> 00:15:14,626 on a different system if you don't use those flags. 375 00:15:14,946 --> 00:15:18,826 And now pedagogically, all these things, -W, -W, -W, 376 00:15:19,266 --> 00:15:25,426 these are essentially telling GCC to be really nit-picky. 377 00:15:25,426 --> 00:15:28,796 This is telling GC to yell at why you as often as possible, 378 00:15:28,836 --> 00:15:31,366 even about the smallest things to really push you 379 00:15:31,366 --> 00:15:34,066 to write the best, the most secure, 380 00:15:34,066 --> 00:15:35,836 the most correct code possible. 381 00:15:35,836 --> 00:15:37,716 So if you omit flags like those, 382 00:15:37,716 --> 00:15:39,186 sometimes your code will compile fine, 383 00:15:39,426 --> 00:15:41,196 but then you actually compile it with make 384 00:15:41,196 --> 00:15:43,206 or with these so-called flags and you'll be yelled 385 00:15:43,206 --> 00:15:45,186 at a whole lot more, even though you thought your code was 386 00:15:45,186 --> 00:15:46,816 working and this is actually a good thing. 387 00:15:46,816 --> 00:15:48,566 And we'll demonstrate this over time. 388 00:15:48,566 --> 00:15:51,616 But the point for now is just to run math2, which I just did. 389 00:15:51,616 --> 00:15:53,386 And I get an answer of 3, but it seems 390 00:15:53,386 --> 00:15:54,966 to be garbled with my prompt. 391 00:15:55,596 --> 00:16:01,676 Why is this "3" running right into my Malan user name there? 392 00:16:01,676 --> 00:16:02,476 >> [ Inaudible ] 393 00:16:02,476 --> 00:16:05,006 >> David: Yeah, so I didn't have my "backslash n." 394 00:16:05,006 --> 00:16:07,806 So if I open this file, notice I just did "%d." 395 00:16:07,806 --> 00:16:09,106 There's no backslash n -- 396 00:16:09,106 --> 00:16:11,406 that would have forced the cursor on to the next row. 397 00:16:11,406 --> 00:16:13,216 So that's all; just a minor little detail. 398 00:16:13,426 --> 00:16:15,946 All right, so let's take things up slightly 399 00:16:16,546 --> 00:16:17,596 to a more interesting one. 400 00:16:17,596 --> 00:16:19,196 Let's go into the third version of this 401 00:16:19,676 --> 00:16:21,586 and then start doing something with these constructs. 402 00:16:21,586 --> 00:16:24,906 So now I'm actually doing some math, but here's some nuances 403 00:16:24,906 --> 00:16:26,196 that we're going to start to trip over. 404 00:16:26,196 --> 00:16:27,876 And this one will be particularly germane 405 00:16:27,876 --> 00:16:32,636 to a portion of Problem Set 1, at least in Problem Set 1. 406 00:16:32,916 --> 00:16:34,226 So what am I doing here? 407 00:16:34,386 --> 00:16:36,816 Well -- and I'll fix this blue next time -- 408 00:16:36,816 --> 00:16:38,986 I'm first telling the compiler I need access 409 00:16:39,046 --> 00:16:40,546 to the standard io library 410 00:16:40,626 --> 00:16:42,406 because printf is declared there. 411 00:16:42,856 --> 00:16:45,036 I'm saying "here comes my main function; 412 00:16:45,036 --> 00:16:46,486 here comes the guts of my program." 413 00:16:46,766 --> 00:16:48,716 And here, too, it's just a little baby program 414 00:16:48,956 --> 00:16:52,696 in that I'm hardcoding the expression 17 divided by 13. 415 00:16:52,986 --> 00:16:55,546 And I'm storing the answer in a variable called 416 00:16:55,596 --> 00:16:57,236 "answer" that's of type float. 417 00:16:57,636 --> 00:16:59,686 Now, a float is floating point value. 418 00:16:59,686 --> 00:17:01,646 So unlike an int, this is the type of number 419 00:17:01,646 --> 00:17:03,346 that can have numbers after a decimal place. 420 00:17:03,346 --> 00:17:04,846 And that makes sense because this is not going 421 00:17:04,846 --> 00:17:06,056 to be a whole number, right? 422 00:17:06,056 --> 00:17:10,936 What's 17 divided by 3 is roughly -- one point something, 423 00:17:11,066 --> 00:17:12,546 so one point dot, dot, dot, right? 424 00:17:12,606 --> 00:17:14,746 So why is this relevant though? 425 00:17:14,746 --> 00:17:15,866 Why is this even interesting? 426 00:17:15,866 --> 00:17:16,676 Well, let me compile this. 427 00:17:16,826 --> 00:17:21,536 This is math3, so make math3, Enter. 428 00:17:21,866 --> 00:17:24,516 Now I'm going to run math3. 429 00:17:25,046 --> 00:17:28,926 My program or this computer seems to be broken. 430 00:17:29,306 --> 00:17:31,826 Seems to think the answer is 1.00. 431 00:17:32,536 --> 00:17:33,426 Why is that? 432 00:17:33,586 --> 00:17:37,866 Here's that source code again. 433 00:17:37,866 --> 00:17:38,876 >> [ Inaudible ] 434 00:17:38,876 --> 00:17:38,943 >> David: Yeah. 435 00:17:38,943 --> 00:17:40,686 So that's some good intuition. 436 00:17:40,686 --> 00:17:43,126 We saw last time that there is this remainder operation. 437 00:17:43,126 --> 00:17:45,826 So hopefully there is indeed a way to fix this, but it looks 438 00:17:45,826 --> 00:17:49,956 as though the division operator, the single slash is division 439 00:17:50,236 --> 00:17:52,046 but -- and here's a curiosity 440 00:17:52,046 --> 00:17:56,066 about many programming languages -- because 17 is an integer 441 00:17:56,066 --> 00:17:58,716 and because 13 is an integer, why do I say that? 442 00:17:58,756 --> 00:18:00,586 There's no decimal point; there's no point zero; 443 00:18:00,586 --> 00:18:03,356 there's no floating point value, it's just a hardcoded integer. 444 00:18:03,776 --> 00:18:07,186 The result is that when you divide an int by an int, 445 00:18:07,526 --> 00:18:10,056 the answer no matter what is going to be an int. 446 00:18:10,056 --> 00:18:12,916 So even if the correct mathematical answer is 1.4 447 00:18:12,916 --> 00:18:16,666 or whatever, when you divide an int by an int, 448 00:18:16,946 --> 00:18:19,856 you only have room in that variable, in the response 449 00:18:19,926 --> 00:18:21,226 for an actual integer. 450 00:18:21,226 --> 00:18:21,916 So what happens? 451 00:18:22,226 --> 00:18:24,976 Everything from the decimal point over gets chopped off. 452 00:18:24,976 --> 00:18:26,116 It doesn't round for you. 453 00:18:26,256 --> 00:18:27,246 It rounds down maybe, 454 00:18:27,246 --> 00:18:29,036 but it doesn't round properly as you would think. 455 00:18:29,256 --> 00:18:32,686 It just throws the decimal point away and that's because, again, 456 00:18:32,876 --> 00:18:36,096 these are ints and the answer intuitively should be a floating 457 00:18:36,096 --> 00:18:38,136 point value, but I need to be more specific. 458 00:18:38,486 --> 00:18:41,086 Now as an aside, turns out you can do more interesting 459 00:18:41,186 --> 00:18:43,346 formatting with printf, and this is useful even 460 00:18:43,346 --> 00:18:44,636 for ASCII art purposes. 461 00:18:45,036 --> 00:18:46,166 This time I'm not just saying 462 00:18:46,166 --> 00:18:54,066 "%f" I'm actually saying % -- what -- %.2f. 463 00:18:54,626 --> 00:18:55,596 Now what does this mean? 464 00:18:55,696 --> 00:18:57,026 Well, you can kind of infer. 465 00:18:57,026 --> 00:18:59,056 If I go back to that code from a moment ago, 466 00:18:59,056 --> 00:19:05,246 and I just rerun math3, notice how many places printed 467 00:19:05,246 --> 00:19:06,556 after the decimal point -- two? 468 00:19:06,856 --> 00:19:10,806 So it turns out with printf you can actually control those kinds 469 00:19:10,806 --> 00:19:12,406 of aesthetics as well. 470 00:19:12,406 --> 00:19:14,076 And when that becomes relevant in a problem set, 471 00:19:14,266 --> 00:19:16,566 we'll point you at the appropriate documentation. 472 00:19:16,796 --> 00:19:17,516 So let's fix this. 473 00:19:17,556 --> 00:19:18,566 That was version three. 474 00:19:18,776 --> 00:19:20,416 Let me go ahead and fix this problem. 475 00:19:20,566 --> 00:19:23,626 Well, if intuitively the problem is the result of dividing an int 476 00:19:23,716 --> 00:19:27,266 by an int, surely a solution is: "Don't do that," right? 477 00:19:27,266 --> 00:19:30,146 Instead divide an int by a floating point value. 478 00:19:30,376 --> 00:19:32,316 And I can fix this just by changing one 479 00:19:32,316 --> 00:19:34,316 of those values to a floating point. 480 00:19:34,446 --> 00:19:38,246 Now as an aside, I'll admit this is a completely useless program, 481 00:19:38,246 --> 00:19:39,936 much like the first couple of ones we did 482 00:19:39,936 --> 00:19:41,946 because why would you write a program just to solve this? 483 00:19:41,946 --> 00:19:43,316 You could use a calculator or any number 484 00:19:43,316 --> 00:19:44,846 of other tools but we'll get there. 485 00:19:45,046 --> 00:19:50,026 But here I'm doing the exact same thing except my number here 486 00:19:50,026 --> 00:19:52,596 in the bottom in the denominator is 13.0. 487 00:19:52,596 --> 00:19:56,236 So now when I compile this fourth version with make math4 488 00:19:56,236 --> 00:20:00,606 and then run math4, I indeed get 1.31. 489 00:20:00,606 --> 00:20:02,876 And there's probably more digits after the decimal place, 490 00:20:02,876 --> 00:20:04,506 but because of my formatting string, 491 00:20:04,866 --> 00:20:05,916 I only see a couple of them. 492 00:20:05,916 --> 00:20:07,776 And that's probably useful if you're actually trying 493 00:20:07,776 --> 00:20:09,646 to format things in a nice way. 494 00:20:10,016 --> 00:20:12,886 Let's look at the fifth and final version of this. 495 00:20:13,066 --> 00:20:15,736 It turns out that in a lot of languages, C included, 496 00:20:15,946 --> 00:20:19,066 you the programmer knowing a bit about how the computer works 497 00:20:19,066 --> 00:20:19,876 and the language works, 498 00:20:20,066 --> 00:20:22,166 can exercise more fine-grained control. 499 00:20:22,166 --> 00:20:23,796 You don't need this hack of just, like, 500 00:20:23,796 --> 00:20:26,396 manually adding a decimal point just to fix that problem. 501 00:20:26,636 --> 00:20:30,296 You can compel the compiler to treat some value 502 00:20:30,546 --> 00:20:32,066 as a different type of value, 503 00:20:32,066 --> 00:20:33,786 at least if it makes intuitive sense 504 00:20:33,976 --> 00:20:35,136 that that should be possible. 505 00:20:35,446 --> 00:20:39,556 So here what I'm doing is 17 divided by 13, 506 00:20:39,556 --> 00:20:43,516 but because of this parenthetical, (float), 507 00:20:43,516 --> 00:20:45,606 because of that parenthetical -- 508 00:20:45,606 --> 00:20:46,626 that has nothing to do with math; 509 00:20:46,796 --> 00:20:48,176 this is a programming language thing -- 510 00:20:48,506 --> 00:20:50,896 that is a casting operation. 511 00:20:52,236 --> 00:20:55,106 So what that means is the compiler is actually going 512 00:20:55,106 --> 00:20:58,806 to first "cast" so to speak 13 from whatever it is 513 00:20:58,806 --> 00:21:00,956 to a float -- to a floating point value -- 514 00:21:01,266 --> 00:21:03,046 and then perform the division for us. 515 00:21:03,046 --> 00:21:06,156 So that more rigorously fixes the problem. 516 00:21:06,346 --> 00:21:07,526 Now why is this useful? 517 00:21:07,526 --> 00:21:09,536 Well, we'll talk before long about cryptography. 518 00:21:09,536 --> 00:21:12,086 And cryptography involves scrambling information, 519 00:21:12,086 --> 00:21:15,556 converting what you a human can read into sort of nonsense 520 00:21:15,556 --> 00:21:17,776 that hopefully a bad guy cannot read. 521 00:21:17,956 --> 00:21:19,646 But we know already from week zero 522 00:21:19,776 --> 00:21:21,736 that computers ultimately represent all information 523 00:21:21,736 --> 00:21:23,036 with numbers, and if they want 524 00:21:23,036 --> 00:21:25,526 to represent letters inside memory, 525 00:21:25,876 --> 00:21:27,416 well what do they do or what do they use? 526 00:21:28,836 --> 00:21:31,176 They want to represent letters and not just numbers. 527 00:21:31,636 --> 00:21:33,636 >> [ Inaudible ] 528 00:21:34,096 --> 00:21:35,976 >> David: So yeah, not binary per se but ASCII, right? 529 00:21:35,976 --> 00:21:38,976 You just need to come up with a convention that maps the numbers 530 00:21:38,976 --> 00:21:41,176 that are very easy for a computer to store. 531 00:21:41,416 --> 00:21:42,546 You have to come up with a convention 532 00:21:42,546 --> 00:21:43,786 that maps numbers to letters. 533 00:21:43,786 --> 00:21:46,146 And the thing we've seen thus far is called "ASCII." 534 00:21:46,446 --> 00:21:49,326 Now ASCII involves characters. 535 00:21:49,326 --> 00:21:51,216 We know from Wednesday if -- briefly -- 536 00:21:51,216 --> 00:21:53,986 that there's this thing called a "char" or "char," depending 537 00:21:53,986 --> 00:21:56,646 on how you want to pronounce it, which is just a single character 538 00:21:56,996 --> 00:21:58,176 but where there's also an int. 539 00:21:58,646 --> 00:22:02,466 But if we know that 65 is the int that maps 540 00:22:02,626 --> 00:22:05,586 to the capital letter "A," we've already seen verbally 541 00:22:05,586 --> 00:22:09,256 that you can convert letters to numbers and numbers to letters, 542 00:22:09,406 --> 00:22:12,056 and so here's the syntax with which we'll soon be doing that. 543 00:22:12,056 --> 00:22:14,816 If you want to convert a number like 65 to the letter A, 544 00:22:15,086 --> 00:22:18,926 you just have tell the computer "cast that int to a char." 545 00:22:18,926 --> 00:22:21,356 If you want to do the opposite process, you simply "cast 546 00:22:21,566 --> 00:22:23,246 to an int from a char." 547 00:22:23,246 --> 00:22:25,886 So this is a teaser of functionality to come. 548 00:22:26,186 --> 00:22:27,926 Now as for all these data sizes, 549 00:22:27,926 --> 00:22:30,296 let's close one outstanding question. 550 00:22:30,556 --> 00:22:32,266 Here is a program that's a little useful 551 00:22:32,496 --> 00:22:34,626 in that it's just a little cheat sheet for me. 552 00:22:34,936 --> 00:22:38,356 I've declared up top four variables: a char called "c," 553 00:22:38,356 --> 00:22:40,346 a double called "d," a float called "f," 554 00:22:40,346 --> 00:22:41,336 and an int called "i." 555 00:22:41,386 --> 00:22:43,736 And those are four of the data types we've discussed thus far. 556 00:22:44,056 --> 00:22:46,656 And I don't quite remember from reading 557 00:22:46,656 --> 00:22:49,126 or from class how big these things are, well, 558 00:22:49,126 --> 00:22:51,076 it turns out -- and this is sometimes useful, 559 00:22:51,076 --> 00:22:55,336 later on more likely than now -- but C has a size of operator 560 00:22:55,676 --> 00:22:57,356 that takes an argument in parenthesis 561 00:22:57,356 --> 00:23:00,546 and it will tell you how much space is used to store 562 00:23:00,546 --> 00:23:01,766 that particular data type. 563 00:23:02,076 --> 00:23:05,256 So I'm printing out line by line a char will take out some number 564 00:23:05,256 --> 00:23:08,336 of bytes; a double, some number of bytes; a float; and an int. 565 00:23:08,686 --> 00:23:09,996 So it's pretty much Copy and Paste. 566 00:23:09,996 --> 00:23:11,866 And the only thing that's differing is the name 567 00:23:11,866 --> 00:23:13,886 of the type and the name of the variable I'm using. 568 00:23:14,136 --> 00:23:15,726 So let's go ahead and make sizeof. 569 00:23:15,726 --> 00:23:18,166 Let's go ahead and run sizeof. 570 00:23:18,456 --> 00:23:19,546 And we should confirm some 571 00:23:19,546 --> 00:23:21,456 of the stuff we started talking about the other day. 572 00:23:21,926 --> 00:23:25,406 So a char is one byte, which is bits -- how many? 573 00:23:25,646 --> 00:23:25,976 >> Eight bits. 574 00:23:26,046 --> 00:23:26,756 >> David: So eight bits. 575 00:23:26,756 --> 00:23:27,876 So that's pretty easy now. 576 00:23:28,156 --> 00:23:31,596 A float is 4 bytes, as is an int. 577 00:23:31,596 --> 00:23:33,636 And an int is interesting 578 00:23:33,636 --> 00:23:36,966 because with 4 bytes, you get 32 bits. 579 00:23:36,966 --> 00:23:39,476 And so what's the biggest possible number you can 580 00:23:39,476 --> 00:23:41,186 represent, give or take, with 32 bits? 581 00:23:41,256 --> 00:23:41,846 >> 4 billion. 582 00:23:42,236 --> 00:23:43,166 >> David: So 4 billion. 583 00:23:43,166 --> 00:23:45,186 Or if you want to tolerate negative numbers, 584 00:23:45,186 --> 00:23:46,596 you've got to sacrifice something. 585 00:23:46,596 --> 00:23:49,176 So it's like negative 2 billion to positive 2 billion. 586 00:23:49,466 --> 00:23:51,116 So this is kind of relevant. 587 00:23:51,116 --> 00:23:53,576 So 2 billion, 4 billion -- these are big numbers, 588 00:23:53,806 --> 00:23:56,476 but there's a lot of "billions of things" in the world, right? 589 00:23:56,476 --> 00:23:57,706 There's billions of atoms. 590 00:23:57,706 --> 00:24:00,126 There's billions probably of web pages these days. 591 00:24:00,356 --> 00:24:03,466 And yet if a computer can only count up to 2 billion 592 00:24:03,466 --> 00:24:06,226 or maybe 4 billion, I mean, what do you then do? 593 00:24:06,226 --> 00:24:08,456 Well, this is actually a problem. 594 00:24:08,456 --> 00:24:10,986 And this is a little different from what happened ten 595 00:24:10,986 --> 00:24:13,686 or so years ago, but the so-called Y2K problem was 596 00:24:13,686 --> 00:24:14,686 essentially the result 597 00:24:14,686 --> 00:24:17,196 of programmers not really having the foresight to realize, 598 00:24:17,456 --> 00:24:20,266 "Maybe we shouldn't use so few bits or so few digits 599 00:24:20,266 --> 00:24:22,936 to represent a year because eventually this will be 600 00:24:22,936 --> 00:24:23,466 a problem." 601 00:24:23,686 --> 00:24:26,266 Now in fairness, that particular problem kind of happened 602 00:24:26,516 --> 00:24:29,486 because so many companies ended up running so much code 603 00:24:29,616 --> 00:24:32,596 for many more years than the programmers actually thought 604 00:24:32,596 --> 00:24:35,056 they would be running it, but the idea is still the same. 605 00:24:35,356 --> 00:24:37,546 So it turns out there's a solution to this: 606 00:24:37,816 --> 00:24:42,116 If you need more precision, more digits after the decimal point 607 00:24:42,116 --> 00:24:44,566 than a float allows, what do you go for instead? 608 00:24:44,566 --> 00:24:44,976 >> A double. 609 00:24:45,286 --> 00:24:45,836 >> David: A double. 610 00:24:45,836 --> 00:24:47,246 And a double is 64 bits. 611 00:24:47,506 --> 00:24:49,166 And now this is getting pretty big. 612 00:24:49,166 --> 00:24:52,276 This is 2 to the 64, and that's a pretty big number. 613 00:24:52,486 --> 00:24:55,636 But in society today, and we'll talk a bit about security topics 614 00:24:55,636 --> 00:24:58,476 and such throughout the course, for cryptography -- 615 00:24:58,516 --> 00:25:00,696 for encrypting things like your credit card information 616 00:25:00,696 --> 00:25:02,896 and bank accounts -- you don't use 32 bits, 617 00:25:03,416 --> 00:25:04,796 you don't use 64 bits, 618 00:25:05,036 --> 00:25:09,236 you generally use 1024 bits, 4,048 bits. 619 00:25:09,416 --> 00:25:10,796 I mean, these are huge, huge, 620 00:25:10,796 --> 00:25:13,876 numbers that humans are now using to protect their data. 621 00:25:13,876 --> 00:25:18,906 But it all kind of boils down to these basics of storage. 622 00:25:18,906 --> 00:25:19,976 [ Phone ringing ] 623 00:25:19,976 --> 00:25:21,216 Sorry, I'm getting a call. 624 00:25:21,596 --> 00:25:24,436 Not anymore. 625 00:25:25,816 --> 00:25:26,696 All right. 626 00:25:26,696 --> 00:25:27,486 So any questions? 627 00:25:27,486 --> 00:25:30,816 So a bit of uninteresting math, a bit of focus on sizes 628 00:25:30,816 --> 00:25:32,866 of types, but any questions on syntax 629 00:25:32,866 --> 00:25:36,796 or concepts thus far before we now put this to the test? 630 00:25:36,966 --> 00:25:37,086 Yeah? 631 00:25:37,826 --> 00:25:42,976 >> Is there a data type for representing 1,024 bits or -- 632 00:25:43,046 --> 00:25:43,476 >> David: Good question. 633 00:25:43,476 --> 00:25:44,476 Is there a data type? 634 00:25:44,476 --> 00:25:46,656 Is there a key word like int or char double 635 00:25:46,656 --> 00:25:50,056 with which you can represent a 1024-bit value? 636 00:25:50,426 --> 00:25:53,216 Short answer: no, at least not in C. You can construct 637 00:25:53,216 --> 00:25:56,226 such constructs in other languages and in theory even C. 638 00:25:56,506 --> 00:25:57,316 But out of the box, 639 00:25:57,316 --> 00:25:59,296 you generally just get these so-called "primitives." 640 00:25:59,406 --> 00:26:02,106 And actually, it's worth noting an int -- 4 billion -- 641 00:26:02,106 --> 00:26:03,616 we didn't even solve that problem. 642 00:26:03,856 --> 00:26:08,816 So what data type can we use to actually get more bits 643 00:26:08,816 --> 00:26:09,656 of precision than an int? 644 00:26:09,756 --> 00:26:10,596 Anyone know what it's called? 645 00:26:10,596 --> 00:26:15,456 So might have been seen long and that might be correct 646 00:26:15,456 --> 00:26:18,736 on some servers, but very, very often the data type called 647 00:26:18,736 --> 00:26:21,246 "long" is actually 4 bytes. 648 00:26:21,656 --> 00:26:24,186 So in fact, if you want a super long number -- 649 00:26:24,186 --> 00:26:25,816 let me go ahead and reopen this program -- 650 00:26:26,126 --> 00:26:27,356 you could declare long. 651 00:26:27,496 --> 00:26:29,266 So let me try this, long l, 652 00:26:29,266 --> 00:26:31,326 and then let me do a little Copy Paste here 653 00:26:31,326 --> 00:26:36,416 and say a long is actually the size of l. So let me quit 654 00:26:36,416 --> 00:26:38,176 that program; recompile. 655 00:26:38,176 --> 00:26:40,566 And here's a little trick -- and again, we promised to point 656 00:26:40,566 --> 00:26:41,676 out little tricks of the trade -- 657 00:26:41,906 --> 00:26:44,286 if I know I want to execute the last command I typed 658 00:26:44,286 --> 00:26:48,006 that started with an m, bangm will actually figure out what 659 00:26:48,006 --> 00:26:50,156 that command was and redo it for me so I don't have 660 00:26:50,156 --> 00:26:52,006 to figure it out, or you can go back 661 00:26:52,006 --> 00:26:53,686 in time in a terminal window. 662 00:26:53,946 --> 00:26:55,436 So I'm hitting the up arrow right now. 663 00:26:55,646 --> 00:26:56,866 Now I'm hitting the down arrow. 664 00:26:56,866 --> 00:26:58,406 So you can scroll back through your history, 665 00:26:58,406 --> 00:27:00,206 which is also useful for time's sake. 666 00:27:00,486 --> 00:27:03,156 So let me go ahead and rerun sizeof and hit Enter. 667 00:27:03,156 --> 00:27:04,836 Well, this is pretty useless, right? 668 00:27:04,836 --> 00:27:08,536 A long, at least on this server, which is a Linux system, 669 00:27:08,666 --> 00:27:10,016 they're the same size. 670 00:27:10,076 --> 00:27:14,576 So it turns out whoever thought of this was either being funny 671 00:27:14,576 --> 00:27:15,876 or just not creative that day. 672 00:27:16,046 --> 00:27:17,966 If you really want a longer number, 673 00:27:18,366 --> 00:27:19,846 it's called a "long long." 674 00:27:20,116 --> 00:27:21,196 It's called that ll. 675 00:27:21,266 --> 00:27:23,566 Let's do a little Copy Paste down here 676 00:27:23,566 --> 00:27:25,046 so I can print out its size. 677 00:27:25,436 --> 00:27:27,426 And if I recompile -- 678 00:27:27,486 --> 00:27:30,756 and nevermind that I'm going pretty fast; 679 00:27:30,756 --> 00:27:32,136 I'm doing the exact same thing as before -- 680 00:27:32,136 --> 00:27:34,226 hit Enter, now we have 8 bytes. 681 00:27:34,396 --> 00:27:36,386 So 8 is actually a pretty huge number. 682 00:27:36,386 --> 00:27:37,806 And this is very much relevant. 683 00:27:37,806 --> 00:27:38,806 Toward the end of the semester, 684 00:27:38,956 --> 00:27:40,556 we'll talk about database design. 685 00:27:40,556 --> 00:27:43,446 And one or two of your problem sets will actually use an actual 686 00:27:43,446 --> 00:27:45,826 database called MySQL, which is very popular -- 687 00:27:45,856 --> 00:27:47,806 Facebook uses it, all sorts of companies use it. 688 00:27:47,946 --> 00:27:49,606 And when you start to store lots and lots of data, 689 00:27:49,776 --> 00:27:52,956 you really do need this ability to count to pretty high numbers. 690 00:27:53,186 --> 00:27:56,006 And thankfully with 64-bit values can we do so, 691 00:27:56,166 --> 00:27:58,276 but sure enough will we run out of space, 692 00:27:58,306 --> 00:27:59,676 even with those types of types. 693 00:27:59,676 --> 00:28:02,626 So hopefully we will not make the same mistakes twice. 694 00:28:03,036 --> 00:28:05,406 All right, so here is a perhaps familiar 695 00:28:05,496 --> 00:28:07,386 if underwhelming formula. 696 00:28:07,386 --> 00:28:08,626 What does this thing represent? 697 00:28:09,206 --> 00:28:10,976 >> How to get Celsius from Fahrenheit. 698 00:28:11,046 --> 00:28:11,156 >> David: Yes. 699 00:28:11,156 --> 00:28:13,096 So this is how to get Celsius from Fahrenheit. 700 00:28:13,096 --> 00:28:15,496 So if you know a temperature F in Fahrenheit, 701 00:28:15,726 --> 00:28:17,046 you can output Celsius. 702 00:28:17,556 --> 00:28:19,916 This is, like, sort of classic Computer Science 703 00:28:19,916 --> 00:28:21,376 101-type question. 704 00:28:21,806 --> 00:28:24,056 It's nice, though, only in that it will give us an opportunity 705 00:28:24,056 --> 00:28:24,896 to fill in the blank. 706 00:28:24,896 --> 00:28:25,756 So here's a blank. 707 00:28:25,966 --> 00:28:28,156 If you have a piece of scrap paper with you, 708 00:28:28,466 --> 00:28:31,326 suppose that you've started your program as follows. 709 00:28:31,576 --> 00:28:32,986 Notice I've made one change. 710 00:28:32,986 --> 00:28:35,696 I was being a little lazy on Wednesday in the interest 711 00:28:35,886 --> 00:28:38,276 of simplifying the code as much as possible. 712 00:28:38,516 --> 00:28:40,406 But it turns out that as we'll see 713 00:28:40,636 --> 00:28:42,966 that functions absolutely take arguments, right? 714 00:28:42,966 --> 00:28:45,926 Printf takes arguments inside a parenthesis. 715 00:28:46,196 --> 00:28:49,216 Main, though, at least now does not take any arguments. 716 00:28:49,336 --> 00:28:52,186 So on Wednesday I just put open parenthesis, closed parenthesis. 717 00:28:52,586 --> 00:28:55,306 But it's the really correct way to specify 718 00:28:55,306 --> 00:28:57,176 that this function takes no arguments is 719 00:28:57,356 --> 00:28:59,666 to actually write this key word "void." 720 00:28:59,666 --> 00:29:01,296 So I'll now start to get into that habit, 721 00:29:01,296 --> 00:29:02,976 even though it's a bit of distracting detail, 722 00:29:03,206 --> 00:29:06,336 it's just consistent with the expectations 723 00:29:06,336 --> 00:29:07,636 that the compiler really has. 724 00:29:07,976 --> 00:29:10,476 So let me challenge you with this: 725 00:29:10,476 --> 00:29:13,556 inside of these curly braces where I have put a comment 726 00:29:13,736 --> 00:29:16,256 with slash slash -- a "comment" means nothing here gets 727 00:29:16,256 --> 00:29:18,186 compiled, it's just for the human to read. 728 00:29:18,386 --> 00:29:20,646 Programmers often write TODO with no space, 729 00:29:20,646 --> 00:29:23,216 all caps like this which means "I have to do something here." 730 00:29:23,566 --> 00:29:27,606 Go ahead and implement with the person next to you a program 731 00:29:27,786 --> 00:29:32,346 that takes as input from the user an integer called F 732 00:29:32,346 --> 00:29:37,086 and prints out the answer of the Celsius equivalent. 733 00:29:37,166 --> 00:29:40,446 So input is an int called F, print out to the screen 734 00:29:40,446 --> 00:29:42,066 or your piece of paper the value 735 00:29:42,066 --> 00:29:45,396 of C. Odds are you only need two, maybe three lines of code 736 00:29:45,396 --> 00:29:47,436 for this, but the goal ultimately will be try it 737 00:29:47,436 --> 00:29:47,906 on your own. 738 00:29:48,106 --> 00:29:50,626 Compare after 30 or 60 seconds with the person next to you 739 00:29:50,806 --> 00:29:54,016 and see if before I reveal the answer we can't get everyone 740 00:29:54,176 --> 00:29:55,526 on to the exact same page. 741 00:29:56,516 --> 00:30:26,026 [ Music ] 742 00:30:26,526 --> 00:30:27,556 >> David: We'll let it loop once more. 743 00:30:29,516 --> 00:30:58,556 [ Music ] 744 00:30:59,056 --> 00:31:00,856 >> David: Called what? 745 00:31:00,856 --> 00:31:01,206 >> [ Inaudible ] 746 00:31:01,206 --> 00:31:01,366 >> David: All right. 747 00:31:01,666 --> 00:31:03,606 So if you haven't already, just glance at the person next 748 00:31:03,606 --> 00:31:06,746 to you, or say hello if you don't know them. 749 00:31:07,026 --> 00:31:10,686 And let's just see how many pieces 750 00:31:10,686 --> 00:31:11,886 of the puzzle you got right. 751 00:31:11,886 --> 00:31:13,856 So there were a couple of hints embedded in here. 752 00:31:14,076 --> 00:31:15,766 A few of you were probably wondering, "Well, 753 00:31:15,766 --> 00:31:17,506 how do I actually get the int from the user?" 754 00:31:17,626 --> 00:31:21,456 Well, remember from Wednesday we introduced CS50's library. 755 00:31:21,456 --> 00:31:23,906 And we've only just begun using it, but it has just a bunch 756 00:31:23,906 --> 00:31:27,606 of very useful functions like GetInt, GetString, GetDouble, 757 00:31:27,976 --> 00:31:30,066 GetFloat, GetLongLong. 758 00:31:30,226 --> 00:31:32,866 And so as those names imply, you can just cull these functions 759 00:31:32,866 --> 00:31:34,686 with no arguments and you'll get back 760 00:31:35,066 --> 00:31:36,926 that data type based on its name. 761 00:31:37,226 --> 00:31:40,246 So I could -- and the hint here is that looks 762 00:31:40,246 --> 00:31:42,396 like you probably do want to use CS50's library 763 00:31:42,536 --> 00:31:45,536 because I've included the so-called header file for it. 764 00:31:45,536 --> 00:31:47,916 Stdio.h, meanwhile, I included in advance why? 765 00:31:47,916 --> 00:31:49,256 >> [ Inaudible ] 766 00:31:49,256 --> 00:31:51,666 >> David: So you need it for printf, right, 767 00:31:51,666 --> 00:31:53,216 if you actually want to print out the value 768 00:31:53,216 --> 00:31:54,296 and not just throw it away. 769 00:31:54,556 --> 00:31:56,366 So this part is the cookie cutter stuff. 770 00:31:56,366 --> 00:31:59,086 Let me go ahead and just to be a little anal add void there. 771 00:31:59,086 --> 00:32:01,566 But we'll explain in the week to come exactly why 772 00:32:01,566 --> 00:32:02,856 and when you do things like that. 773 00:32:03,156 --> 00:32:05,076 And what do you think? 774 00:32:06,036 --> 00:32:07,246 Toward the bottom here, printf? 775 00:32:07,816 --> 00:32:09,336 So you might have chosen a different sentence 776 00:32:09,336 --> 00:32:10,406 or whatnot, but that's fine. 777 00:32:10,406 --> 00:32:12,856 So temperature in Fahrenheit maybe with a space, 778 00:32:12,856 --> 00:32:14,966 just to get the aesthetics to look a little interesting. 779 00:32:15,326 --> 00:32:18,356 Then you declare a float called f and then assign it -- 780 00:32:18,406 --> 00:32:20,296 let me scroll up for the folks in front -- 781 00:32:20,896 --> 00:32:23,816 that give return value of GetFloat. 782 00:32:24,326 --> 00:32:25,066 Yea or nay? 783 00:32:25,336 --> 00:32:27,866 Most folks at least have something along those lines. 784 00:32:27,936 --> 00:32:28,456 That's okay. 785 00:32:28,656 --> 00:32:28,986 All right. 786 00:32:28,986 --> 00:32:29,716 So not a problem. 787 00:32:29,906 --> 00:32:32,016 So what is this actually doing? 788 00:32:32,016 --> 00:32:33,916 Well, again, recall that GetFloat is a function. 789 00:32:34,186 --> 00:32:37,316 It's defined in CS50's library; its sole purpose in life is 790 00:32:37,356 --> 00:32:41,156 to ask the user for a floating point value and return it. 791 00:32:41,596 --> 00:32:44,766 And it's up to me to put that return value in a variable. 792 00:32:45,036 --> 00:32:46,736 Think of it as a black box. 793 00:32:46,946 --> 00:32:48,236 I say, "Get me a float." 794 00:32:48,236 --> 00:32:49,176 It hands me a float. 795 00:32:49,296 --> 00:32:51,446 I need to now put this float somewhere, and I'll put it 796 00:32:51,446 --> 00:32:54,176 in a variable called f that you can call it whatever you'd like. 797 00:32:54,406 --> 00:32:55,756 And now I have to do the mathematics. 798 00:32:56,356 --> 00:32:59,286 So here I'm declaring a variable called c, 799 00:32:59,396 --> 00:33:01,576 and I'm pretty much translating the formula 800 00:33:01,576 --> 00:33:04,496 from the slide using the characters on my keyboard 801 00:33:04,776 --> 00:33:06,936 to a floating point value. 802 00:33:07,216 --> 00:33:08,986 But there's one potential gotcha here -- 803 00:33:08,986 --> 00:33:10,726 and this is where you have to be kind 804 00:33:10,726 --> 00:33:12,716 of piecing together the little clues along the way -- 805 00:33:12,946 --> 00:33:15,526 what have I done that's interesting here that had I not, 806 00:33:15,696 --> 00:33:18,246 I would have had a mathematical error? 807 00:33:18,246 --> 00:33:19,156 >> [ Inaudible ] 808 00:33:19,156 --> 00:33:21,556 >> David: Let's go over here. 809 00:33:21,556 --> 00:33:25,436 I think everyone over here seems to know and -- yeah? 810 00:33:26,056 --> 00:33:28,836 >> You changed to 99.0. 811 00:33:29,046 --> 00:33:30,936 >> David: Yeah, so I very deliberately changed this 9 812 00:33:30,936 --> 00:33:34,476 to a 9.0 so that my math would actually be floating point math 813 00:33:34,606 --> 00:33:37,196 involving real numbers and not just integers. 814 00:33:37,196 --> 00:33:41,066 If I left it as just 9, odds are I would very often get 815 00:33:41,066 --> 00:33:41,696 what answer? 816 00:33:41,766 --> 00:33:42,166 >> Zero. 817 00:33:42,916 --> 00:33:44,346 >> David: Right, I would get zero. 818 00:33:44,346 --> 00:33:48,606 Just take a look: if this is 9, 5 divided by 9 is always going 819 00:33:48,606 --> 00:33:51,666 to be 0 point something, and if you thus have two integers 820 00:33:51,846 --> 00:33:53,996 and you're rounding down, which is what happens 821 00:33:53,996 --> 00:33:56,426 when you do integral math we're using this operator, 822 00:33:56,426 --> 00:33:58,486 I'm going to get zero times whatever. 823 00:33:58,776 --> 00:34:00,576 So my answer is always going to be zero 824 00:34:00,576 --> 00:34:02,896 and therefore almost always going to be wrong. 825 00:34:03,076 --> 00:34:03,186 Yeah? 826 00:34:04,136 --> 00:34:06,046 >> But isn't f already a floating point? 827 00:34:06,046 --> 00:34:07,816 So why isn't this [inaudible]? 828 00:34:08,126 --> 00:34:08,946 >> David: Excellent question. 829 00:34:08,946 --> 00:34:12,236 So isn't f already a float? 830 00:34:12,496 --> 00:34:16,076 That's true, but especially because of my parenthesization 831 00:34:16,076 --> 00:34:19,156 over here, because I'm saying, "Do f minus 32, 832 00:34:19,416 --> 00:34:22,726 but then multiply it by the division on the left." 833 00:34:23,556 --> 00:34:26,156 Just as in grade school when you're doing division 834 00:34:26,156 --> 00:34:28,616 and multiplication, you do it from left to right in terms 835 00:34:28,616 --> 00:34:29,576 of order of operations. 836 00:34:29,796 --> 00:34:32,166 The 5 is going to get divided by 9 first. 837 00:34:32,436 --> 00:34:34,456 The answer is always going to be zero. 838 00:34:34,646 --> 00:34:37,546 So yes, f is a float, but that solution is too late; 839 00:34:37,886 --> 00:34:38,976 we need to fix the problem sooner. 840 00:34:39,396 --> 00:34:41,046 >> So how do I switch the parenthesis? 841 00:34:41,736 --> 00:34:42,976 Do I have to put that first? 842 00:34:43,046 --> 00:34:45,016 >> David: So, yes, you could. 843 00:34:45,076 --> 00:34:49,516 If we did that, if we moved f minus 32 over to the left, 844 00:34:49,796 --> 00:34:52,136 that would give me a floating point answer 845 00:34:52,336 --> 00:34:54,196 because f is a float. 846 00:34:54,456 --> 00:34:58,046 Then I'd be multiplying a float by 5, 847 00:34:58,046 --> 00:34:59,506 so that's a float times an int. 848 00:34:59,506 --> 00:35:00,096 That's okay. 849 00:35:00,096 --> 00:35:01,256 That's going to give me a float. 850 00:35:01,406 --> 00:35:03,336 And then I divide by an int -- 851 00:35:03,336 --> 00:35:05,956 that's okay because so long as you have a float involved 852 00:35:05,956 --> 00:35:07,946 at some point early on, you're okay. 853 00:35:07,946 --> 00:35:11,146 The problem only arises if you have an int divided by an int. 854 00:35:11,146 --> 00:35:14,716 And that operation is being performed before others. 855 00:35:15,066 --> 00:35:15,846 You'll run into errors. 856 00:35:15,846 --> 00:35:15,946 Yeah? 857 00:35:17,096 --> 00:35:17,816 >> It only works with division? 858 00:35:17,886 --> 00:35:20,946 Like, it doesn't matter, like, you have minus 32 even though -- 859 00:35:21,046 --> 00:35:21,876 >> David: F minus 32? 860 00:35:21,876 --> 00:35:23,586 No. So that's not as big a deal. 861 00:35:23,586 --> 00:35:27,696 It's really the multiplication and in this case, 862 00:35:29,106 --> 00:35:31,166 division that the problem arises. 863 00:35:31,266 --> 00:35:31,396 Yeah? 864 00:35:32,096 --> 00:35:34,276 >> Instead of putting 9.0, can you just put 865 00:35:34,476 --> 00:35:35,936 "float" in parenthesis? 866 00:35:36,046 --> 00:35:36,246 >> David: So I could. 867 00:35:36,246 --> 00:35:36,856 Absolutely. 868 00:35:36,856 --> 00:35:38,946 So that's a good application of the alternative I proposed. 869 00:35:38,946 --> 00:35:40,166 I could have just done this, 870 00:35:40,606 --> 00:35:43,396 and the only reason I didn't here is just because now I look 871 00:35:43,396 --> 00:35:45,436 at this, and now it's just getting confusing for me -- 872 00:35:45,436 --> 00:35:47,446 even for someone who's been programming for a while. 873 00:35:47,666 --> 00:35:49,046 Just too much parenthesis for my taste. 874 00:35:49,046 --> 00:35:50,026 So I went with the other approach. 875 00:35:50,066 --> 00:35:51,636 But absolutely could you do that. 876 00:35:52,256 --> 00:35:52,323 Yeah? 877 00:35:53,046 --> 00:35:54,776 >> Well, you talked last week about a lot of these. 878 00:35:55,046 --> 00:35:56,576 Why are you, like, writing out code? 879 00:35:57,076 --> 00:36:00,976 Because didn't you say you could do things like just %f? 880 00:36:01,156 --> 00:36:04,116 >> David: %f -- good question. 881 00:36:04,426 --> 00:36:06,816 So when we've talked about "printf" whose purpose 882 00:36:06,816 --> 00:36:09,046 in life is to take some input and then print it 883 00:36:09,046 --> 00:36:09,876 out to the screen, 884 00:36:10,226 --> 00:36:13,386 printf supports what are called "format strings." 885 00:36:13,386 --> 00:36:14,596 They're just placeholders. 886 00:36:15,006 --> 00:36:16,286 But this is a different context. 887 00:36:16,956 --> 00:36:20,276 Here I actually need to tell the computer, "Give me some bytes 888 00:36:20,376 --> 00:36:23,566 in ram in which to store a value, and that value's going 889 00:36:23,566 --> 00:36:24,826 to be a floating point value." 890 00:36:25,256 --> 00:36:26,666 >> So for anything that's not printf, 891 00:36:26,866 --> 00:36:27,746 you have to write out code? 892 00:36:28,296 --> 00:36:30,366 >> David: At some point in time, I'm going to have 893 00:36:30,366 --> 00:36:31,796 to have declared the variable 894 00:36:31,796 --> 00:36:34,466 as a float before I can then hand it to printf 895 00:36:35,036 --> 00:36:38,456 as the insertion value for that format strength. 896 00:36:39,116 --> 00:36:41,916 So both of these stories involve floating point values, 897 00:36:42,156 --> 00:36:45,906 but only in this case am I actually allocating memory. 898 00:36:47,036 --> 00:36:48,206 Other questions? 899 00:36:48,796 --> 00:36:50,746 All right, so let's actually run this thing. 900 00:36:50,746 --> 00:36:52,956 Glancing at the bottom, this is pretty arbitrary, 901 00:36:52,956 --> 00:36:55,236 but I chose to format my string as follows: 902 00:36:55,636 --> 00:36:58,576 so at the bottom here I'm printing out %.1f, 903 00:36:59,126 --> 00:37:01,816 which quick sanity check means print just one digit 904 00:37:01,816 --> 00:37:03,686 after the decimal place. 905 00:37:04,016 --> 00:37:05,826 Then I'm going to print a capital F just 906 00:37:05,886 --> 00:37:12,136 to be aesthetically interesting equals another %.1fc. 907 00:37:12,346 --> 00:37:15,316 So if I scroll now to the right, I should see a comma 908 00:37:15,376 --> 00:37:17,346 after the closed quote and then what two values? 909 00:37:17,346 --> 00:37:18,546 >> [ Inaudible ] 910 00:37:18,546 --> 00:37:23,676 >> David: F, c because I want to tell printf, "Use these values 911 00:37:23,946 --> 00:37:27,146 in those placeholders, these values in those format strings." 912 00:37:27,146 --> 00:37:28,776 So let's go ahead and compile this. 913 00:37:28,776 --> 00:37:33,226 So I'm going to go ahead and run make on f2c, 914 00:37:33,226 --> 00:37:35,246 which is the name I gave to this particular program. 915 00:37:35,246 --> 00:37:37,786 Now I'm going to run f2c. 916 00:37:37,786 --> 00:37:39,766 And now temperature in Fahrenheit -- 917 00:37:39,766 --> 00:37:43,146 all right, let's go ahead and pick an easy one like 212, 918 00:37:43,376 --> 00:37:46,696 Enter, and indeed equals 100 in Celsius. 919 00:37:46,736 --> 00:37:51,456 Let's do another one that I know, 32, and that equals zero. 920 00:37:51,556 --> 00:37:53,806 And presumably if we do this again and again, 921 00:37:53,916 --> 00:37:56,766 we'd get back some similarly correct answers. 922 00:37:57,496 --> 00:37:58,846 Okay. So any questions? 923 00:37:59,476 --> 00:37:59,586 Yeah? 924 00:38:00,276 --> 00:38:01,736 >> For the printf, do you use the printf for that? 925 00:38:02,306 --> 00:38:06,976 Could you also use GetString [inaudible]? 926 00:38:07,056 --> 00:38:08,446 >> David: Good question and short answer: no. 927 00:38:08,446 --> 00:38:10,646 So can you use GetString to print the answer? 928 00:38:10,686 --> 00:38:12,936 No. So we deliberately, in CS50's library, 929 00:38:13,136 --> 00:38:16,376 named these functions consistent with their behavior. 930 00:38:16,376 --> 00:38:18,266 So when you have a function called GetString, 931 00:38:18,496 --> 00:38:21,466 that means it's going to get a string from the user -- 932 00:38:21,526 --> 00:38:22,946 prompt the user for a string -- 933 00:38:23,316 --> 00:38:25,136 whereas printf is literally going to print it, 934 00:38:25,136 --> 00:38:26,726 not to paper, but to the screen. 935 00:38:27,406 --> 00:38:29,786 So it's very much the name of functions that denote 936 00:38:29,956 --> 00:38:31,186 or connote their behavior. 937 00:38:32,056 --> 00:38:32,406 All right. 938 00:38:32,616 --> 00:38:36,356 So let's go ahead and introduce a couple other capabilities 939 00:38:36,356 --> 00:38:38,366 so that we can finally start writing programs 940 00:38:38,366 --> 00:38:41,046 that maybe print out charts or interact with the user, 941 00:38:41,096 --> 00:38:41,976 play games, or the like. 942 00:38:42,136 --> 00:38:43,856 Well, most of this stuff is actually pretty similar 943 00:38:43,856 --> 00:38:44,206 to Scratch. 944 00:38:44,346 --> 00:38:47,086 So it turns out in C, you can absolutely represent the idea 945 00:38:47,086 --> 00:38:49,566 of a condition, a branch, a fork in the road. 946 00:38:49,796 --> 00:38:53,406 The syntax actually looks reminiscent to Scratch, 947 00:38:53,406 --> 00:38:55,916 where you have a puzzle piece reminiscent of this shape, 948 00:38:56,206 --> 00:38:58,846 but you have to say "if" followed by a space, 949 00:38:59,226 --> 00:39:02,446 followed by in parenthesis, the Boolean expression that you want 950 00:39:02,446 --> 00:39:03,976 to check -- more on those in a moment. 951 00:39:04,226 --> 00:39:07,666 The curly braces are then the C's way of kind 952 00:39:07,666 --> 00:39:09,256 of making a puzzle piece that looks like this 953 00:39:09,256 --> 00:39:10,466 so you can put stuff inside. 954 00:39:10,736 --> 00:39:12,266 So the open curly brace followed 955 00:39:12,266 --> 00:39:14,966 by the closed curly brace means everything inside 956 00:39:14,966 --> 00:39:17,346 of these curly braces should get executed, 957 00:39:17,346 --> 00:39:21,326 but only if that first condition is actually true. 958 00:39:21,576 --> 00:39:24,266 You can stack these things in C. So if you want to do if 959 00:39:24,386 --> 00:39:27,466 and then rather -- let me tweak this just so simplify -- 960 00:39:27,756 --> 00:39:31,806 if you want to stack these things and do this in one case 961 00:39:31,896 --> 00:39:34,596 or that in the other, can you stack them like this? 962 00:39:34,596 --> 00:39:36,826 And notice, unlike Scratch where some 963 00:39:36,826 --> 00:39:39,076 of you might have realized it starts to get a little ugly, 964 00:39:39,076 --> 00:39:41,526 you have an "if else" and then you have another here 965 00:39:41,526 --> 00:39:43,616 and another one and things start to move and move and move 966 00:39:43,616 --> 00:39:44,746 like this, it gets very messy. 967 00:39:44,996 --> 00:39:47,656 With C you can just line everything up on the left here 968 00:39:47,806 --> 00:39:48,526 because it's just text. 969 00:39:49,086 --> 00:39:51,756 So if we have a third, we can just introduce it like this 970 00:39:51,756 --> 00:39:54,696 and so things start to line up a lot more nicely. 971 00:39:54,856 --> 00:39:58,966 As an aside, if you ever just have one line of code 972 00:39:58,966 --> 00:40:00,926 that you want to execute, one puzzle piece 973 00:40:01,006 --> 00:40:02,236 in Scratch terminology, 974 00:40:02,546 --> 00:40:04,636 you actually don't need the curly braces; 975 00:40:04,636 --> 00:40:07,836 you only need the curly braces if you have two or more lines 976 00:40:07,836 --> 00:40:11,386 of code that you want to execute if that condition is true. 977 00:40:11,386 --> 00:40:13,616 But for now, it's probably simpler just always use the 978 00:40:13,616 --> 00:40:17,786 curly braces just to get into the habit. 979 00:40:18,336 --> 00:40:18,446 Yeah? 980 00:40:21,966 --> 00:40:23,316 >> You said you'd make a space between if and the parenthesis; 981 00:40:23,346 --> 00:40:23,976 does that actually matter? 982 00:40:24,356 --> 00:40:25,046 >> David: No. 983 00:40:25,046 --> 00:40:26,076 Okay, so you caught me 984 00:40:26,076 --> 00:40:28,806 in a small white lie for pedagogical sake. 985 00:40:28,806 --> 00:40:31,946 So the question is: Do you have to have this space between the 986 00:40:31,946 --> 00:40:32,996 if and the parenthesis? 987 00:40:33,316 --> 00:40:35,466 So short technical answer is no. 988 00:40:35,466 --> 00:40:39,146 As a matter of style, the chorus would staunchly preach yes, 989 00:40:39,146 --> 00:40:41,206 for the sake of readability. 990 00:40:41,206 --> 00:40:43,236 So this is a topic that will be threaded throughout the course. 991 00:40:43,236 --> 00:40:46,026 Thus far and in Scratch will pretty much focus on the idea 992 00:40:46,096 --> 00:40:49,356 of correctness: Does your code work as we asked it to 993 00:40:49,546 --> 00:40:51,036 and as you intended it to? 994 00:40:51,246 --> 00:40:53,866 But the two others axis, per the syllabus that we'll explore 995 00:40:53,866 --> 00:40:54,986 in great detail in the course 996 00:40:54,986 --> 00:40:56,356 and also these are the axis along 997 00:40:56,356 --> 00:40:58,896 with which we'll evaluate your submissions are design, 998 00:40:59,146 --> 00:41:01,516 in other words how well implemented is this? 999 00:41:01,586 --> 00:41:04,356 Did you just Copy and Paste 50 times 1000 00:41:04,706 --> 00:41:07,536 when you could have just used a loop that cycles 50 times? 1001 00:41:07,536 --> 00:41:09,466 So that would be a matter of good versus bad design. 1002 00:41:09,706 --> 00:41:11,436 And there's this third axis called "style," 1003 00:41:11,676 --> 00:41:14,206 which is really a more subjective aesthetic sense. 1004 00:41:14,636 --> 00:41:18,006 So you will see in textbooks, in programs I write, 1005 00:41:18,006 --> 00:41:20,446 in programs the teaching fellows and course assistants write, 1006 00:41:20,736 --> 00:41:23,136 very different approaches to style, for instance -- 1007 00:41:23,136 --> 00:41:26,616 and this is a good canonical example -- a lot of people, 1008 00:41:26,936 --> 00:41:30,956 especially those who write C, actually put their curly braces 1009 00:41:30,956 --> 00:41:35,666 on this first line and just intuitively someone unfamiliar 1010 00:41:35,666 --> 00:41:37,176 proposed why this might be better. 1011 00:41:37,706 --> 00:41:40,486 Because there's probably no right answer here, 1012 00:41:40,486 --> 00:41:42,466 but why might this approach be better? 1013 00:41:43,096 --> 00:41:43,226 Yeah? 1014 00:41:44,586 --> 00:41:44,956 >> It seems more [inaudible]. 1015 00:41:44,956 --> 00:41:45,916 >> David: Yeah, it's just practical, right? 1016 00:41:45,916 --> 00:41:47,836 This takes up three fewer lines. 1017 00:41:47,836 --> 00:41:49,796 And when I'm starting to write lots of lines of code, you know, 1018 00:41:49,796 --> 00:41:51,666 it's frankly kind of useful to be able to see more 1019 00:41:51,666 --> 00:41:53,526 on the screen at once without having to scroll. 1020 00:41:53,766 --> 00:41:57,316 So I would argue as a matter of practice in my own life 1021 00:41:57,316 --> 00:41:59,656 and certainly in a teaching capacity, frankly, 1022 00:41:59,656 --> 00:42:00,816 I find this much more readable. 1023 00:42:00,816 --> 00:42:03,486 And it's also much more similarly structured to Scratch. 1024 00:42:03,746 --> 00:42:05,676 But realize that with these decisions, 1025 00:42:05,816 --> 00:42:07,356 it's going to be a matter of style. 1026 00:42:07,356 --> 00:42:08,676 And so what you'll be pointed 1027 00:42:08,676 --> 00:42:13,856 to in Problem Sets One PDF is something we call the CS50 1028 00:42:14,036 --> 00:42:15,326 Style Guide. 1029 00:42:15,326 --> 00:42:18,886 You'll see highlighted in green good ways of doing things; 1030 00:42:18,886 --> 00:42:22,016 you'll see things highlighted in yellow ways you can do things -- 1031 00:42:22,016 --> 00:42:22,956 just make sure you're doing it 1032 00:42:22,956 --> 00:42:24,206 because you know what you're doing and you 1033 00:42:24,206 --> 00:42:26,886 like the way you're doing it; and things in red like this, 1034 00:42:26,886 --> 00:42:29,626 which I see surprisingly often because none of us teach this 1035 00:42:29,626 --> 00:42:30,956 and yet we see submissions like this: 1036 00:42:31,486 --> 00:42:35,766 indenting like this really helps no one as you see in red up top. 1037 00:42:35,766 --> 00:42:37,686 So you can do some crazy things because at the end of the day, 1038 00:42:37,836 --> 00:42:39,346 the computer really doesn't care. 1039 00:42:39,346 --> 00:42:41,866 The compiler is going to ignore all of this so-called 1040 00:42:41,866 --> 00:42:43,946 "pretty printing" -- all of this sort of indentation, 1041 00:42:44,196 --> 00:42:45,366 all of this white space -- 1042 00:42:45,536 --> 00:42:47,636 that's really for the human's benefits, both yours, 1043 00:42:47,636 --> 00:42:49,746 your colleagues, the teaching staff, and the like. 1044 00:42:49,956 --> 00:42:53,116 And so you'll learn over time both by seeing and by doing 1045 00:42:53,326 --> 00:42:56,246 that there are generally some good ways, some okay ways, 1046 00:42:56,396 --> 00:42:58,576 and some bad ways of actually writing code. 1047 00:42:58,676 --> 00:43:01,556 But that's a principle we'll practice over time. 1048 00:43:01,716 --> 00:43:09,656 Why don't we go ahead and take a five-minute break? 1049 00:43:09,656 --> 00:43:11,446 All right. 1050 00:43:11,596 --> 00:43:14,296 So let's actually write a program with this stuff. 1051 00:43:14,296 --> 00:43:16,746 So among your printouts are a couple 1052 00:43:16,746 --> 00:43:19,956 of files one called condition1.c. I'm going 1053 00:43:19,956 --> 00:43:21,866 to change over to my terminal window. 1054 00:43:22,196 --> 00:43:27,626 I'm going to use Nano to open conditions1.c. 1055 00:43:27,626 --> 00:43:29,066 And actually, I'm going to cheat. 1056 00:43:29,066 --> 00:43:30,316 I'm going to use my own special program 1057 00:43:30,316 --> 00:43:31,986 that will color code things for me in class, 1058 00:43:32,236 --> 00:43:34,076 but the idea is the same; it's a text editor. 1059 00:43:34,376 --> 00:43:37,006 And now let's take a look at what this program actually does. 1060 00:43:37,006 --> 00:43:40,076 So I've removed the comments, the things that start 1061 00:43:40,076 --> 00:43:41,856 with slash slash from the slides just 1062 00:43:41,856 --> 00:43:44,776 so you have an opportunity engage more intellectually 1063 00:43:44,776 --> 00:43:46,346 rather than just reading the answers off the screen. 1064 00:43:46,346 --> 00:43:47,776 Your copies realize have comments 1065 00:43:47,776 --> 00:43:49,316 that document everything that's going on. 1066 00:43:49,746 --> 00:43:51,306 So here's my code here. 1067 00:43:51,696 --> 00:43:53,076 I'm first saying printf. 1068 00:43:53,076 --> 00:43:55,936 I'd like an integer please, a colon, and a space just 1069 00:43:55,936 --> 00:43:56,986 for aesthetic reasons. 1070 00:43:57,236 --> 00:44:00,606 Then I'm declaring a variable called n, it's of type int. 1071 00:44:00,956 --> 00:44:03,386 And I'm using the so-called "assignment operator," 1072 00:44:03,446 --> 00:44:07,626 a single equals sign to store in n what? 1073 00:44:08,776 --> 00:44:09,386 >> The int. 1074 00:44:09,386 --> 00:44:09,556 >> David: The int. 1075 00:44:09,836 --> 00:44:12,596 So to store in n whatever GetInt returns. 1076 00:44:12,596 --> 00:44:14,366 Now what does it mean to cull GetInt? 1077 00:44:14,676 --> 00:44:16,966 Well, as we'll eventually show you the source code, 1078 00:44:16,966 --> 00:44:19,076 the code we wrote for GetInt itself. 1079 00:44:19,356 --> 00:44:22,686 But for now assume that GetInt just makes the cursor blink 1080 00:44:22,976 --> 00:44:25,316 or at least stay steady -- it depends on your computer 1081 00:44:25,316 --> 00:44:26,396 and operating system and such. 1082 00:44:26,616 --> 00:44:28,036 But suppose that -- think for now 1083 00:44:28,036 --> 00:44:30,616 that GetInt just makes the cursor blink or wait 1084 00:44:30,616 --> 00:44:33,626 for the user to type in an "int" and then hit Enter. 1085 00:44:33,626 --> 00:44:34,886 So that's what it means to get an int. 1086 00:44:34,886 --> 00:44:37,386 If you instead use GetString, same thing: blinking cursor, 1087 00:44:37,566 --> 00:44:39,296 the function is just going to wait for the user 1088 00:44:39,296 --> 00:44:42,296 to type a word, a sentence or whatever, and then hit Enter. 1089 00:44:42,516 --> 00:44:45,116 And we'll see if you try to mess with our functions 1090 00:44:45,176 --> 00:44:48,696 by providing a string when we want an int, or providing an int 1091 00:44:48,696 --> 00:44:51,156 when we want a string, you may very well get yelled at 1092 00:44:51,156 --> 00:44:52,476 and the user will have to retry. 1093 00:44:52,476 --> 00:44:54,936 So we've embedded some error checking, if you will, 1094 00:44:54,936 --> 00:44:56,196 into our implementations. 1095 00:44:56,426 --> 00:44:57,386 Now at this point in the story, 1096 00:44:57,386 --> 00:45:00,096 I've got a variable called n. I've stored it in int. 1097 00:45:00,386 --> 00:45:01,296 What am I now doing? 1098 00:45:01,496 --> 00:45:03,746 Well, here is one of these conditions, these branches. 1099 00:45:03,746 --> 00:45:04,806 We've used them in Scratch. 1100 00:45:04,846 --> 00:45:07,616 If n is greater than zero, I decided I would say, 1101 00:45:07,916 --> 00:45:11,336 "You picked a positive number, backslash n," so put the cursor 1102 00:45:11,336 --> 00:45:15,046 on the next line, else if n was not less than zero, I say, 1103 00:45:15,256 --> 00:45:17,676 "You picked a negative number, backslash n." 1104 00:45:17,676 --> 00:45:21,396 And notice because I have just one line of code inside of each 1105 00:45:21,396 --> 00:45:24,946 of these branches, what have I clearly omitted? 1106 00:45:24,946 --> 00:45:25,046 >> [ Inaudible ] 1107 00:45:25,046 --> 00:45:25,606 >> David: The curly braces. 1108 00:45:25,606 --> 00:45:27,446 So I actually don't need the curly braces 1109 00:45:27,686 --> 00:45:29,536 because I only have a single line of code. 1110 00:45:29,536 --> 00:45:31,806 But if I did have two lines of code or more, 1111 00:45:31,946 --> 00:45:33,516 I would in fact need to put those back. 1112 00:45:33,516 --> 00:45:34,306 Why omit them? 1113 00:45:34,556 --> 00:45:36,846 It's just a little more compact, as the gentleman there said. 1114 00:45:37,016 --> 00:45:39,816 It just frees up space on the screen for more content 1115 00:45:39,816 --> 00:45:42,356 and that may very well be a reasonable decision. 1116 00:45:42,596 --> 00:45:44,296 All right, so there's a bug. 1117 00:45:45,146 --> 00:45:46,926 The astute or the mathematicians will realize 1118 00:45:46,926 --> 00:45:49,976 that what will happen here? 1119 00:45:49,976 --> 00:45:50,046 >> [ Inaudible ] 1120 00:45:50,046 --> 00:45:50,136 >> David: Right. 1121 00:45:50,136 --> 00:45:51,926 So zero is supposed to be neither positive nor negative, 1122 00:45:51,926 --> 00:45:53,606 and here I am just simplifying 1123 00:45:53,606 --> 00:45:56,336 and saying it's actually going to be negative. 1124 00:45:56,336 --> 00:45:57,696 All right, so we can fix this, right? 1125 00:45:57,696 --> 00:46:00,556 We've seen this branching construct in Scratch, 1126 00:46:00,776 --> 00:46:03,326 we've seen it a moment ago on the slide, and see, we just need 1127 00:46:03,326 --> 00:46:06,176 to use not an else, not just an if but an else if. 1128 00:46:06,456 --> 00:46:09,676 And so here do we see we can check for equality with zero. 1129 00:46:09,906 --> 00:46:12,056 And here as promised is that other operator -- 1130 00:46:12,056 --> 00:46:12,936 looks a little weird, 1131 00:46:13,186 --> 00:46:17,026 but because assignment is already using the equal sign 1132 00:46:17,026 --> 00:46:20,016 to assign one value on the right to a variable on the left. 1133 00:46:20,406 --> 00:46:22,936 Well, the world had to come up with another symbol that looked 1134 00:46:22,936 --> 00:46:25,086 as similar as possible just for people's sanity, 1135 00:46:25,346 --> 00:46:27,206 so they went with equals equals. 1136 00:46:27,516 --> 00:46:29,466 So this is the equality operator, 1137 00:46:29,576 --> 00:46:33,356 whereas the single equal sign is the assignment operator. 1138 00:46:33,626 --> 00:46:35,906 So now we have three conditions -- three forks in the road -- 1139 00:46:36,176 --> 00:46:38,876 and this program is in fact correct, at least hopefully. 1140 00:46:38,876 --> 00:46:42,026 Let's go ahead and make conditions2, and hit Enter. 1141 00:46:42,466 --> 00:46:43,346 All right, that's good. 1142 00:46:43,346 --> 00:46:45,516 So now I'm going to go ahead and run conditions2. 1143 00:46:45,516 --> 00:46:48,016 And now notice as promised, I'm using a Mac here 1144 00:46:48,016 --> 00:46:49,636 so my cursor is not actually blinking, 1145 00:46:49,876 --> 00:46:51,896 but the cursor is waiting there for me. 1146 00:46:51,896 --> 00:46:54,346 Nothing is happening until I actually type 1147 00:46:54,346 --> 00:46:56,386 in let's say, "David." 1148 00:46:57,616 --> 00:46:59,376 Okay, obviously that's not an int. 1149 00:46:59,746 --> 00:47:02,106 I culled int and as promised, it's going to yell 1150 00:47:02,106 --> 00:47:03,406 at me by saying "retry." 1151 00:47:03,406 --> 00:47:04,816 And it will just do this ad nauseam 1152 00:47:04,816 --> 00:47:08,026 until I actually give it an int or if I realize, "Wow, 1153 00:47:08,026 --> 00:47:09,836 I really screwed up this program. 1154 00:47:09,836 --> 00:47:11,976 I can't give it what it wants," 1155 00:47:12,216 --> 00:47:14,526 know that when you're writing code and the problems 1156 00:47:14,526 --> 00:47:16,106 that will remind of this, if you absolutely have 1157 00:47:16,106 --> 00:47:17,996 to kill your program, you don't have to close the window, 1158 00:47:17,996 --> 00:47:21,186 reboot crazy stuff like that, generally you can hit CTRL+C 1159 00:47:21,186 --> 00:47:23,696 and it will just abort the program right where it is, 1160 00:47:23,696 --> 00:47:26,106 in case you ever get trapped in some awkward situation. 1161 00:47:26,106 --> 00:47:28,156 But let's actually play this game properly this time. 1162 00:47:28,466 --> 00:47:29,866 So I'd like an integer please. 1163 00:47:30,056 --> 00:47:31,726 One, two, three, Enter. 1164 00:47:32,016 --> 00:47:33,176 You picked a positive number. 1165 00:47:33,176 --> 00:47:36,066 Let's now do negative one, two, three. 1166 00:47:36,426 --> 00:47:37,566 You picked a negative number. 1167 00:47:37,566 --> 00:47:39,726 And let's run in one more time zero. 1168 00:47:40,046 --> 00:47:41,316 You picked zero. 1169 00:47:41,316 --> 00:47:41,996 So that's good. 1170 00:47:41,996 --> 00:47:43,416 So this program in fact seems 1171 00:47:43,416 --> 00:47:45,506 to be doing something helpful finally. 1172 00:47:45,506 --> 00:47:48,466 So now let's take a look at how else we could approach this. 1173 00:47:48,466 --> 00:47:51,036 Well, it turns out that if you want to check two conditions 1174 00:47:51,186 --> 00:47:52,896 and you only care that one of them is true 1175 00:47:53,136 --> 00:47:54,396 or the other one is true. 1176 00:47:54,906 --> 00:47:58,276 You might want to say in the real world, 1177 00:47:58,276 --> 00:48:02,466 if you go to a movie theater for this R-rated movie, "Are you 18 1178 00:48:02,466 --> 00:48:04,516 and over or are you with a parent?" 1179 00:48:04,606 --> 00:48:07,386 So if you would want to do this condition or this one, 1180 00:48:07,696 --> 00:48:09,676 you don't want to break those conditions up because you want 1181 00:48:09,676 --> 00:48:12,716 to let those people in or that family in either way if either 1182 00:48:12,716 --> 00:48:16,196 of those conditions is true so you can or them together. 1183 00:48:16,396 --> 00:48:18,406 In Scratch, the block you may have used 1184 00:48:18,406 --> 00:48:21,086 for this idea literally says "or" for or; 1185 00:48:21,316 --> 00:48:23,476 in C, two vertical bars. 1186 00:48:23,476 --> 00:48:25,536 And if you've never used those, they're usually 1187 00:48:25,536 --> 00:48:26,506 above your Return key. 1188 00:48:26,506 --> 00:48:28,426 You might have to hold the Shift key or something like that, 1189 00:48:28,466 --> 00:48:29,356 but they're standard keys. 1190 00:48:29,586 --> 00:48:31,826 And it's double bars -- no spaces in between them. 1191 00:48:32,026 --> 00:48:34,366 We'll eventually see that that single bar means 1192 00:48:34,366 --> 00:48:35,066 something different. 1193 00:48:35,066 --> 00:48:37,796 So double bar means if this condition or this one -- 1194 00:48:37,796 --> 00:48:40,766 and maybe even both -- are true, go down this road 1195 00:48:40,766 --> 00:48:42,036 and execute that code. 1196 00:48:42,326 --> 00:48:44,936 If you want to make sure both conditions are true 1197 00:48:45,356 --> 00:48:48,066 and they both have to be true for any code to be executed, 1198 00:48:48,266 --> 00:48:49,536 use ampersand ampersand. 1199 00:48:49,536 --> 00:48:51,056 And this conjures up the idea of and. 1200 00:48:51,056 --> 00:48:54,376 And in Scratch did you probably see the key word 1201 00:48:54,376 --> 00:48:56,126 "and" on one of the blocks. 1202 00:48:56,776 --> 00:48:58,506 So let's see how we might use this. 1203 00:48:58,676 --> 00:49:01,556 There is file now called nonswitch.c among 1204 00:49:01,706 --> 00:49:02,956 your printouts. 1205 00:49:03,346 --> 00:49:06,196 Let me scroll down and take a look at what we can now do. 1206 00:49:06,196 --> 00:49:07,856 Our program can get a little more interesting, 1207 00:49:07,856 --> 00:49:10,556 a little more judgmental based on our input. 1208 00:49:10,556 --> 00:49:13,336 So at the very top, I've coded up the same thing as before: 1209 00:49:13,616 --> 00:49:16,426 "Give me an integer between one and ten," or rather, 1210 00:49:16,496 --> 00:49:17,956 "give me an integer between one and ten" -- 1211 00:49:17,956 --> 00:49:18,926 a little different this time. 1212 00:49:19,216 --> 00:49:21,246 Then I called GetInt, which is going 1213 00:49:21,246 --> 00:49:22,936 to handle the difficult users for me. 1214 00:49:22,936 --> 00:49:25,056 If they type in bogus characters, it's going to yell 1215 00:49:25,056 --> 00:49:27,566 at them and make them retry, and eventually I'm going 1216 00:49:27,566 --> 00:49:30,156 to get handed back an int, which I'm storing in n. Well, 1217 00:49:30,156 --> 00:49:33,256 if I actually want to judge this number based on its magnitude, 1218 00:49:33,436 --> 00:49:37,876 well, I can say now, "If n is greater than or equal to one." 1219 00:49:38,176 --> 00:49:41,536 So on your keyboard, you don't have a greater than symbol 1220 00:49:41,536 --> 00:49:43,206 with a minus sign right underneath, 1221 00:49:43,316 --> 00:49:45,216 even though you might have that in Microsoft Word 1222 00:49:45,216 --> 00:49:46,526 or the like, but that's fine. 1223 00:49:46,526 --> 00:49:48,936 In C and most programming languages, if you need greater 1224 00:49:48,936 --> 00:49:51,876 than or equal to, use greater than and then right next to it 1225 00:49:51,876 --> 00:49:54,846 with no space put equal to, and that conjures up the same idea. 1226 00:49:55,266 --> 00:49:59,536 So if n is greater than or equal to 1 and n is less than or equal 1227 00:49:59,536 --> 00:50:01,916 to 3, let's just judge this thing a small 1228 00:50:01,916 --> 00:50:03,126 number arbitrarily. 1229 00:50:03,426 --> 00:50:07,356 Else if n is greater than 4 and n is less than 6 -- 1230 00:50:07,526 --> 00:50:11,866 let's call it medium -- else if n is greater than or equal to 7, 1231 00:50:11,866 --> 00:50:13,706 less than or equal to 10 -- let's call it "big" -- 1232 00:50:13,706 --> 00:50:18,166 and if the user typed in zero or negative 10 or 20 or whatever, 1233 00:50:18,326 --> 00:50:21,326 let's just use the all-inclusive else block and just say, 1234 00:50:21,326 --> 00:50:23,226 "You picked an invalid number." 1235 00:50:23,836 --> 00:50:26,706 So at this point, now the code's getting a little more 1236 00:50:26,706 --> 00:50:28,476 interesting, but it's also getting a little bloated. 1237 00:50:28,476 --> 00:50:30,676 It's kind of growing in size. 1238 00:50:30,676 --> 00:50:34,796 Are there syntactic alternatives to implementing this same idea? 1239 00:50:34,796 --> 00:50:36,316 In Scratch you may have realized that, 1240 00:50:36,436 --> 00:50:38,336 "I could implement my program this way 1241 00:50:38,336 --> 00:50:40,136 with these puzzle pieces, but it kind of feels 1242 00:50:40,136 --> 00:50:41,776 like I could use these puzzle pieces instead" -- 1243 00:50:41,776 --> 00:50:43,996 that's absolutely the case in programming. 1244 00:50:43,996 --> 00:50:46,196 Even though you'll see a whole bunch of constructs today 1245 00:50:46,196 --> 00:50:48,696 and even throughout the course, you will find 1246 00:50:49,026 --> 00:50:52,336 that there's often multiple ways to implement something, 1247 00:50:52,496 --> 00:50:55,356 and it will be up to us to guide you toward better design 1248 00:50:55,356 --> 00:50:58,366 and making in giving two different options the better 1249 00:50:58,366 --> 00:50:59,996 choice as time passes. 1250 00:50:59,996 --> 00:51:02,496 So turns out C supports what's called a switch, 1251 00:51:02,576 --> 00:51:03,796 a switching construct. 1252 00:51:04,066 --> 00:51:08,106 This thing's nice because rather than do "if" and "and and" 1253 00:51:08,106 --> 00:51:09,476 or "or or" and all of this, 1254 00:51:09,736 --> 00:51:12,226 you can instead enumerate things a little more cleanly, 1255 00:51:12,226 --> 00:51:14,756 albeit apparently at the expense of more white space. 1256 00:51:15,126 --> 00:51:16,716 So the top of this program's the same. 1257 00:51:16,716 --> 00:51:18,366 "Give me an integer between 1 and 10." 1258 00:51:18,636 --> 00:51:19,846 I get the int and store it 1259 00:51:20,096 --> 00:51:25,006 in n. The switch statement takes inside its parenthesis an int 1260 00:51:25,296 --> 00:51:28,326 or a char or some primitive type. 1261 00:51:28,776 --> 00:51:30,786 So for now assume it's got to be an int or a char. 1262 00:51:31,066 --> 00:51:34,316 And then what I get to do inside the curly braces is literally 1263 00:51:34,426 --> 00:51:38,366 line by line enumerate the cases that I want to apply 1264 00:51:38,796 --> 00:51:40,186 to the following code. 1265 00:51:40,436 --> 00:51:43,036 So this code is identical functionally 1266 00:51:43,036 --> 00:51:45,796 to the last implementation we saw, nonswitch.c, 1267 00:51:46,096 --> 00:51:47,886 but I'm just ever-more emphatically saying, 1268 00:51:47,886 --> 00:51:50,446 "In case 1," that is when n equals 1 1269 00:51:50,876 --> 00:51:52,616 or when case 2 applies -- 1270 00:51:52,616 --> 00:51:55,706 when n equals 2 or when n equals 3 do what? 1271 00:51:55,706 --> 00:51:58,916 I go ahead and print out a small number. 1272 00:51:59,116 --> 00:52:00,526 But I now have to use "break." 1273 00:52:00,876 --> 00:52:02,076 Break is another key word. 1274 00:52:02,076 --> 00:52:04,576 You might have used something similar in Scratch, maybe Stop 1275 00:52:04,576 --> 00:52:05,636 or something like that to kind 1276 00:52:05,636 --> 00:52:08,046 of stop what you were doing inside a loop or some construct. 1277 00:52:08,296 --> 00:52:10,636 I have to break because otherwise the idea 1278 00:52:10,636 --> 00:52:12,696 of a switch is that it's going to keep falling through. 1279 00:52:12,916 --> 00:52:15,286 And once one of these things applies, it's just going 1280 00:52:15,286 --> 00:52:18,416 to start executing code until you tell it to break. 1281 00:52:18,786 --> 00:52:21,646 Now at the very bottom, should we see a default case. 1282 00:52:21,646 --> 00:52:23,576 When you're all out of specific cases and you want 1283 00:52:23,576 --> 00:52:26,826 to handle everything else, you literally say "default:" 1284 00:52:27,366 --> 00:52:30,696 And here I could end the program with break, but it's kind 1285 00:52:30,696 --> 00:52:33,646 of unnecessary because once I'm at the bottom 1286 00:52:33,646 --> 00:52:35,046 of the curly braces, that's it. 1287 00:52:35,046 --> 00:52:37,356 There's nothing more that might get executed anyway. 1288 00:52:37,656 --> 00:52:38,606 Which one is better? 1289 00:52:38,966 --> 00:52:40,696 Honestly, this one looks a little ugly. 1290 00:52:40,696 --> 00:52:42,806 You have to kind of scroll down to see it. 1291 00:52:43,026 --> 00:52:45,636 But when you start writing programs, especially when we get 1292 00:52:45,636 --> 00:52:47,086 to web-based stuff where you want 1293 00:52:47,086 --> 00:52:48,276 to check the user's input -- 1294 00:52:48,276 --> 00:52:49,756 is it valid, is it an email address, 1295 00:52:49,756 --> 00:52:51,036 and all these different scenarios -- 1296 00:52:51,316 --> 00:52:53,966 it's actually often useful to be able to just enumerate them 1297 00:52:53,966 --> 00:52:57,316 or rattle them off using this switching construct instead. 1298 00:52:57,876 --> 00:52:58,216 All right. 1299 00:52:58,216 --> 00:53:00,256 Well, we saw loops in Scratch. 1300 00:53:00,596 --> 00:53:03,226 Let's see how we can now start using them like I did 1301 00:53:03,226 --> 00:53:05,176 for that little chart of enrollment. 1302 00:53:05,476 --> 00:53:06,876 So here is a for loop. 1303 00:53:07,266 --> 00:53:09,026 In C and in many languages, 1304 00:53:09,066 --> 00:53:11,476 PHP and JavaScript are pretty much going to look the same 1305 00:53:11,476 --> 00:53:14,636 to us later this semester, you implement the idea of looping 1306 00:53:14,856 --> 00:53:17,666 with any of three constructs: one's called a for loop, 1307 00:53:17,876 --> 00:53:19,816 as we're about to see; one's called a while loop; 1308 00:53:20,126 --> 00:53:21,726 and one's called a do while loop. 1309 00:53:22,036 --> 00:53:24,766 Scratch essentially had the same ideas, but they were call repeat 1310 00:53:25,126 --> 00:53:27,566 and forever and a few other options. 1311 00:53:27,816 --> 00:53:29,546 So same ideas, just different jargons, 1312 00:53:29,586 --> 00:53:30,956 slightly different implementations. 1313 00:53:31,316 --> 00:53:32,856 So what does this program do? 1314 00:53:33,126 --> 00:53:36,816 Well, main first declares a loop that starts here. 1315 00:53:37,096 --> 00:53:39,356 And then there's three parts to the parenthesis 1316 00:53:39,356 --> 00:53:40,616 after the key word "for." 1317 00:53:40,926 --> 00:53:43,116 The semicolon separates each of the parts. 1318 00:53:43,116 --> 00:53:46,496 So this thing on the left is part one, part two, part three. 1319 00:53:46,816 --> 00:53:49,536 The general structure that we're looking at can be summarized 1320 00:53:49,536 --> 00:53:52,126 as this, if this helps paint a nice mental picture. 1321 00:53:52,396 --> 00:53:54,656 The first thing is what we're going to call "initializations." 1322 00:53:54,886 --> 00:53:55,806 This is code that's got 1323 00:53:55,806 --> 00:53:57,756 to be executed before anything else happens. 1324 00:53:57,756 --> 00:53:59,956 It's how you can initialize a variable to like zero 1325 00:54:00,136 --> 00:54:01,816 for counting's sake or something like that. 1326 00:54:02,346 --> 00:54:05,316 "Condition," this thing in the middle between the semicolons, 1327 00:54:05,316 --> 00:54:08,286 is going to be checked every iteration of the loop. 1328 00:54:08,426 --> 00:54:13,276 And as soon as this condition or these conditions evaluate 1329 00:54:13,276 --> 00:54:15,186 to false, the whole loop terminates. 1330 00:54:15,566 --> 00:54:17,726 So unlike Scratch where you pretty much had to say 1331 00:54:17,726 --> 00:54:21,136 "repeat" ten times or whatever, or forever where it's just 1332 00:54:21,136 --> 00:54:22,906 "forever" with for loops, 1333 00:54:22,906 --> 00:54:26,926 can you actually specify a finite number of iterations -- 1334 00:54:26,926 --> 00:54:29,786 and as I learned 15 years ago, finite is important. 1335 00:54:30,086 --> 00:54:33,176 So finite number of times can you specify with a condition, 1336 00:54:33,176 --> 00:54:33,986 as we'll see in a moment. 1337 00:54:34,076 --> 00:54:35,206 And then finally, updates. 1338 00:54:35,476 --> 00:54:37,346 It's not a good thing if you're just looping 1339 00:54:37,526 --> 00:54:38,756 and checking some condition, 1340 00:54:38,896 --> 00:54:41,086 but never actually changing the state of the world. 1341 00:54:41,086 --> 00:54:43,466 If you're not updating some variable by incrementing it 1342 00:54:43,466 --> 00:54:45,216 or decrementing it -- if you're not changing anything, 1343 00:54:45,486 --> 00:54:48,446 presumably the conditions are never going to evaluate to false 1344 00:54:48,446 --> 00:54:50,736 or the opposite, so you're just going to have an infinite loop, 1345 00:54:50,826 --> 00:54:53,566 which might be your goal, but odds are it's not. 1346 00:54:53,566 --> 00:54:55,096 So how might we use this in practice? 1347 00:54:55,846 --> 00:54:58,226 Probably the most common approach to using a 1348 00:54:58,226 --> 00:55:02,596 for loop is just to iterate from i equals zero up to some value. 1349 00:55:02,596 --> 00:55:05,526 And in this case, it's very reasonable 1350 00:55:05,746 --> 00:55:07,606 to use a variable called "i" even though 1351 00:55:07,606 --> 00:55:09,206 that conveys very little information 1352 00:55:09,436 --> 00:55:12,436 if the whole point is to be an index, just from zero to one 1353 00:55:12,436 --> 00:55:13,496 to two, to three, to four, 1354 00:55:13,616 --> 00:55:15,816 or even downward depending on your goal. 1355 00:55:16,106 --> 00:55:20,126 So this declares an integer, a variable of type int called I, 1356 00:55:20,216 --> 00:55:22,016 and initializes it to zero. 1357 00:55:22,516 --> 00:55:25,926 This checks "Is i less than or equal to one hundred? 1358 00:55:25,926 --> 00:55:27,656 If so, proceed to loop." 1359 00:55:27,886 --> 00:55:28,976 I proceed to loop. 1360 00:55:29,146 --> 00:55:30,586 I print Percent complete. 1361 00:55:30,586 --> 00:55:33,066 And this looks a little messy, but notice what's embedded 1362 00:55:33,066 --> 00:55:34,456 in there is some familiar stuff. 1363 00:55:34,456 --> 00:55:36,206 %d means -- 1364 00:55:36,206 --> 00:55:37,196 >> [ Inaudible ] 1365 00:55:37,196 --> 00:55:39,446 >> David: Yep, so one or more digits. 1366 00:55:39,506 --> 00:55:41,746 "%%" means percent. 1367 00:55:41,946 --> 00:55:44,526 So just as we had to escape things with backslashes 1368 00:55:44,526 --> 00:55:47,856 in an awkward way, same deal with percent in this context. 1369 00:55:47,856 --> 00:55:51,416 If you want a literal percent, it's %% and then backslash n. 1370 00:55:51,816 --> 00:55:55,346 So this program, if you run it based on intuition 1371 00:55:55,346 --> 00:55:56,886 or little cheat sheets in front of you, 1372 00:55:56,886 --> 00:55:58,716 what is this program ultimately going to do for me? 1373 00:55:59,396 --> 00:56:01,976 >> It's going to count [inaudible] from zero. 1374 00:56:02,056 --> 00:56:03,116 >> David: Yeah, it's going to count from zero 1375 00:56:03,116 --> 00:56:04,286 up to one hundred percent. 1376 00:56:04,426 --> 00:56:05,516 It's going to be my sort 1377 00:56:05,596 --> 00:56:08,826 of simple implementation of a progress bar. 1378 00:56:08,826 --> 00:56:09,856 So let's go ahead and try this. 1379 00:56:09,856 --> 00:56:11,576 So I'm going to run progress1. 1380 00:56:11,856 --> 00:56:14,266 And again, it's kind of a weak implementation. 1381 00:56:14,726 --> 00:56:16,636 And we won't wait here one hundred seconds for it 1382 00:56:16,636 --> 00:56:20,106 to finish, but we're using the loop, we're updating a variable, 1383 00:56:20,106 --> 00:56:22,516 and we're formatting it in a nice way. 1384 00:56:22,626 --> 00:56:24,536 And so at the end of the day, a hundred lines later, 1385 00:56:24,536 --> 00:56:25,866 it should get to one hundred percent. 1386 00:56:26,016 --> 00:56:28,336 And because there's nothing else in the code at that point, 1387 00:56:28,536 --> 00:56:31,036 the program should just quit naturally in a good way. 1388 00:56:31,036 --> 00:56:31,156 Yeah? 1389 00:56:31,156 --> 00:56:32,726 >> Is there a way to get that first percent complete to stay 1390 00:56:32,726 --> 00:56:35,976 in the same place naturally? 1391 00:56:36,426 --> 00:56:37,206 >> David: So absolutely. 1392 00:56:37,206 --> 00:56:39,126 Can you get the percent to stay in the same place? 1393 00:56:39,126 --> 00:56:40,836 Because notice there's going to be eventually a problem. 1394 00:56:40,836 --> 00:56:43,076 You go from one, which is this wide; then it goes to ten 1395 00:56:43,136 --> 00:56:44,676 and eventually it's going to go to one hundred. 1396 00:56:44,876 --> 00:56:46,946 So if you're really being anal, things are not going to line 1397 00:56:46,946 --> 00:56:47,996 up in a perfect column. 1398 00:56:48,216 --> 00:56:49,626 It turns out that you can. 1399 00:56:49,626 --> 00:56:53,436 You can specify numbers before d just 1400 00:56:53,436 --> 00:56:55,146 as you can specify numbers before f. 1401 00:56:55,416 --> 00:56:57,846 And if you don't put a point but just put a number, 1402 00:56:58,056 --> 00:57:01,286 this specifies the width of space that will be used 1403 00:57:01,286 --> 00:57:02,756 by the number you're inserting there. 1404 00:57:02,936 --> 00:57:04,146 And for Problem Set One, 1405 00:57:04,146 --> 00:57:06,366 we'll defer to some online documentation for the specifics. 1406 00:57:06,516 --> 00:57:07,346 But short answer: yes. 1407 00:57:07,426 --> 00:57:09,606 And it boils down to modifying the format string 1408 00:57:09,936 --> 00:57:10,936 with just a simple number. 1409 00:57:11,046 --> 00:57:12,736 So you can do some neat things. 1410 00:57:13,296 --> 00:57:14,656 >> Is sleep [inaudible]? 1411 00:57:15,396 --> 00:57:16,126 >> David: Very good question. 1412 00:57:16,126 --> 00:57:17,556 So sleep -- as the name implies -- 1413 00:57:17,916 --> 00:57:20,876 simply puts the program to sleep for a second. 1414 00:57:21,016 --> 00:57:23,996 In this case because I'm saying sleep for a value of one, 1415 00:57:24,466 --> 00:57:25,606 where does that come from? 1416 00:57:25,606 --> 00:57:28,156 Well, I actually unintentionally glossed over that detail. 1417 00:57:28,156 --> 00:57:30,396 At the top of this program notice I had to whip 1418 00:57:30,396 --> 00:57:33,656 out another header file because sleep is not defined 1419 00:57:33,656 --> 00:57:35,806 in CS50's library, it's not declared 1420 00:57:35,806 --> 00:57:39,206 in the standard io library, it's apparently defined 1421 00:57:39,206 --> 00:57:40,786 in the uni standard library -- 1422 00:57:40,836 --> 00:57:44,946 unistd.h. Well, how in the world did I know that? 1423 00:57:45,166 --> 00:57:46,966 Well, as you'll see in Problem Set One -- 1424 00:57:46,966 --> 00:57:48,996 to be clear, unless you walk 1425 00:57:48,996 --> 00:57:50,296 out of here uncomfortable here today -- 1426 00:57:50,296 --> 00:57:52,116 realize that we'll very much hold your hand 1427 00:57:52,116 --> 00:57:53,966 for this first problem set through these various steps. 1428 00:57:54,266 --> 00:57:56,516 It turns out on a Linux system there's this command called 1429 00:57:56,516 --> 00:57:58,066 "man" for manual page. 1430 00:57:58,306 --> 00:58:00,286 And you can look up documentation 1431 00:58:00,286 --> 00:58:01,286 for various functions. 1432 00:58:01,566 --> 00:58:04,186 So if I type "man sleep," this is going 1433 00:58:04,186 --> 00:58:06,556 to give me the user's manual for a program called sleep, 1434 00:58:06,606 --> 00:58:09,166 but there's a gotcha and the problem set it makes clear, 1435 00:58:09,476 --> 00:58:12,156 notice that it says at top left sleep one. 1436 00:58:12,496 --> 00:58:15,276 Turns out this user's manual, just like a typical book, 1437 00:58:15,276 --> 00:58:17,326 has multiple sections or chapters 1438 00:58:17,646 --> 00:58:20,186 and Chapter One is generally about programs. 1439 00:58:20,496 --> 00:58:22,986 So it turns out there's a program called sleep, 1440 00:58:23,286 --> 00:58:25,176 just like there's a program called Nano, 1441 00:58:25,176 --> 00:58:27,306 a program called GCC -- that is not what I want. 1442 00:58:27,446 --> 00:58:30,026 I want the function, which is a little tool, 1443 00:58:30,026 --> 00:58:32,196 a line of code I can include in my own programs 1444 00:58:32,456 --> 00:58:36,086 and generally those tools live in sections two 1445 00:58:36,086 --> 00:58:38,646 or three of the manual. 1446 00:58:38,946 --> 00:58:41,076 So if I specify the number three, 1447 00:58:41,296 --> 00:58:42,316 now I'm in the right place. 1448 00:58:42,586 --> 00:58:44,266 Again, pset one will walk you through this 1449 00:58:44,266 --> 00:58:46,246 and you'll get familiar with this stuff, 1450 00:58:46,246 --> 00:58:48,576 even though it looks a little arcane at first glance. 1451 00:58:48,896 --> 00:58:51,346 But long story short, if I want to use a function called sleep, 1452 00:58:51,676 --> 00:58:55,056 the reason I, the programmer, knew to use that library -- 1453 00:58:55,056 --> 00:58:55,996 that header at the top -- 1454 00:58:56,346 --> 00:58:58,326 because it told me to in this little synopsis. 1455 00:58:58,326 --> 00:59:00,526 And I read the documentation, did what it said, 1456 00:59:00,526 --> 00:59:02,386 and that's how I got my program to compile. 1457 00:59:02,386 --> 00:59:04,816 And you'll do exactly that with some handholding 1458 00:59:04,816 --> 00:59:06,976 in Problem Sets One. 1459 00:59:07,306 --> 00:59:09,386 So let's do make this slightly sexier. 1460 00:59:09,386 --> 00:59:11,396 So I'm going to wave my hands at what one 1461 00:59:11,396 --> 00:59:13,616 of these things here does, fflush(stdout) here, 1462 00:59:13,896 --> 00:59:18,206 but I decided this is kind of a weak implementation. 1463 00:59:18,386 --> 00:59:21,046 A progress bar is not supposed to print one hundred lines just 1464 00:59:21,046 --> 00:59:22,856 to update its status; generally we 1465 00:59:22,856 --> 00:59:24,226 like to see a little animation. 1466 00:59:24,526 --> 00:59:27,116 So let me go ahead and compile progress2 instead 1467 00:59:27,116 --> 00:59:28,606 and this time run this one. 1468 00:59:29,156 --> 00:59:32,936 It turns out that you can keep the cursor on the same line 1469 00:59:33,176 --> 00:59:35,906 and update the same line of code again and again 1470 00:59:36,176 --> 00:59:39,186 by not using backslash n but instead 1471 00:59:39,186 --> 00:59:42,726 by using the more retro backslash r, 1472 00:59:42,896 --> 00:59:44,946 which doesn't move the cursor down as we said; 1473 00:59:44,946 --> 00:59:47,166 it instead moves the back to the beginning, 1474 00:59:47,416 --> 00:59:49,676 much like a typewriter would cycle back the beginning. 1475 00:59:49,896 --> 00:59:53,226 So here I'm culling printf in a loop again and again, 1476 00:59:53,416 --> 00:59:56,666 but I'm literally overwriting the previous thing that I wrote. 1477 00:59:57,026 --> 00:59:59,916 And because I'm writing out a string that's the same length -- 1478 01:00:00,196 --> 01:00:01,466 my strings aren't getting shorter -- 1479 01:00:01,706 --> 01:00:04,446 it's creating this illusion of some animation. 1480 01:00:04,476 --> 01:00:05,626 But really, the computer's just 1481 01:00:05,626 --> 01:00:08,046 so fast I'm drawing the same string 1482 01:00:08,096 --> 01:00:10,626 with just a slightly different number again and again 1483 01:00:10,626 --> 01:00:13,826 after going to sleep one second at a time. 1484 01:00:13,826 --> 01:00:13,893 Yeah? 1485 01:00:14,126 --> 01:00:16,416 >> So is there a slash letter command 1486 01:00:16,416 --> 01:00:18,406 for printf they could just 1487 01:00:18,786 --> 01:00:19,976 like type [inaudible] you know what I mean? 1488 01:00:20,056 --> 01:00:20,576 >> David: Good question. 1489 01:00:20,576 --> 01:00:21,916 Is there a command to printf? 1490 01:00:22,066 --> 01:00:24,426 Not that's as easily said as done. 1491 01:00:24,426 --> 01:00:26,766 You could absolutely implement something more interesting 1492 01:00:26,766 --> 01:00:29,426 and something even more animated like we saw on Wednesday 1493 01:00:29,426 --> 01:00:31,666 or even earlier today, but it takes a little more 1494 01:00:31,706 --> 01:00:32,726 than just a format string. 1495 01:00:33,056 --> 01:00:38,646 All right, so let's take a look at one last thing here, 1496 01:00:38,916 --> 01:00:41,356 one last version of progress. 1497 01:00:41,696 --> 01:00:43,786 So in addition to for loops like this, 1498 01:00:44,106 --> 01:00:45,456 know that there are while loops. 1499 01:00:45,526 --> 01:00:47,086 And when do you use one or the other? 1500 01:00:47,086 --> 01:00:49,996 At the end of the day, you can implement the exact same program 1501 01:00:49,996 --> 01:00:52,456 using a while loop that you can using a for loop, 1502 01:00:52,616 --> 01:00:54,376 it's just your code is going to look slightly different. 1503 01:00:54,376 --> 01:00:56,816 And odds are if a week hence, two weeks hence just 1504 01:00:56,816 --> 01:00:59,386 for fun you decide to reimplement your Scratch project 1505 01:00:59,386 --> 01:01:01,766 or some tiny piece of it, it wouldn't be surprising -- 1506 01:01:01,766 --> 01:01:04,256 or shouldn't be -- if you end of implementing the same game 1507 01:01:04,256 --> 01:01:06,576 or animation or whatever using a completely different set 1508 01:01:06,576 --> 01:01:07,336 of puzzle pieces. 1509 01:01:07,336 --> 01:01:08,586 Same deal we'd see here. 1510 01:01:08,866 --> 01:01:12,166 So with the while loop, you have a condition very similar to the 1511 01:01:12,226 --> 01:01:14,976 if blocks and the else if blocks that we've seen. 1512 01:01:15,276 --> 01:01:16,786 This is a condition that's checked again 1513 01:01:16,786 --> 01:01:18,946 and again every time you cycle around. 1514 01:01:19,256 --> 01:01:22,566 But notice unlike a for loop, if you want to declare a variable 1515 01:01:22,686 --> 01:01:24,656 or increment or decrement to variable, 1516 01:01:24,826 --> 01:01:26,016 you've got to do that yourself. 1517 01:01:26,016 --> 01:01:27,736 You've got to do it above this chunk of code, 1518 01:01:27,936 --> 01:01:29,686 you've got to do it inside this chunk of code. 1519 01:01:29,866 --> 01:01:31,246 The burden is put on you. 1520 01:01:31,386 --> 01:01:33,776 You don't get the nice little feature of a for loop. 1521 01:01:33,926 --> 01:01:34,696 So we can see this 1522 01:01:34,696 --> 01:01:39,856 in progress3.c. This is the exact same program that I had 1523 01:01:39,856 --> 01:01:42,986 in progress2.c. It's the fancier version that's actually using 1524 01:01:42,986 --> 01:01:46,046 backslash r to move things to the start of the line. 1525 01:01:46,366 --> 01:01:48,966 Again, for today let me ignore what fflush means. 1526 01:01:48,966 --> 01:01:51,956 It solves a slight bug if we don't use it at all, 1527 01:01:52,066 --> 01:01:53,606 but more on that in the future when you talk 1528 01:01:53,606 --> 01:01:55,516 about files and creating files. 1529 01:01:55,866 --> 01:01:57,126 But notice what I have to do here. 1530 01:01:57,386 --> 01:02:00,296 Because the for loop hands me placeholders 1531 01:02:00,296 --> 01:02:02,386 in between those semicolons for variables and such, 1532 01:02:02,386 --> 01:02:03,926 now I don't have those with while loops. 1533 01:02:03,926 --> 01:02:05,506 I got to roll this one myself. 1534 01:02:05,506 --> 01:02:09,056 So I have up here at the top of my program a variable called i 1535 01:02:09,056 --> 01:02:12,426 of type int, and I have to initialize it up here to zero. 1536 01:02:12,696 --> 01:02:15,676 Now I say while i is less than or equal to 100, 1537 01:02:15,816 --> 01:02:17,676 I write my code, I sleep for a second. 1538 01:02:17,976 --> 01:02:21,616 But here, as we saw in week zero with the shoe example putting 1539 01:02:21,616 --> 01:02:25,916 on socks, I have to increment this variable explicitly myself. 1540 01:02:25,916 --> 01:02:28,916 Now instinctively, prefer the for loop or the while loop? 1541 01:02:29,466 --> 01:02:29,556 >> For. 1542 01:02:30,886 --> 01:02:32,186 >> David: Yeah, I'd probably vote for for. 1543 01:02:32,226 --> 01:02:34,316 It just feels a little cleaner, but it's going to depend. 1544 01:02:34,546 --> 01:02:36,576 And you will see over time as you've seen with Scratch 1545 01:02:36,616 --> 01:02:37,976 that there's going to be different ways 1546 01:02:37,976 --> 01:02:41,286 to implement these various goals. 1547 01:02:41,606 --> 01:02:43,096 Now there's this last type of loop. 1548 01:02:43,376 --> 01:02:44,796 So with this type of loop, 1549 01:02:45,096 --> 01:02:46,996 things get a little bit reversed. 1550 01:02:47,036 --> 01:02:50,446 So the do while construct is not as useful as the other two. 1551 01:02:50,446 --> 01:02:54,426 I probably whip out a do while loop very rarely, but very often 1552 01:02:54,426 --> 01:02:58,126 when programming games or any program that takes user input 1553 01:02:58,336 --> 01:03:00,866 and has to check that user input and yell at the user 1554 01:03:00,866 --> 01:03:01,626 if they're messing with you 1555 01:03:01,626 --> 01:03:03,116 or aren't providing what's expected. 1556 01:03:03,186 --> 01:03:04,056 Now what do I mean by that? 1557 01:03:04,576 --> 01:03:07,126 The difference with the do while loop, as the syntax kind 1558 01:03:07,126 --> 01:03:09,186 of suggests, is that this construct is going 1559 01:03:09,186 --> 01:03:11,236 to do something no matter what. 1560 01:03:11,236 --> 01:03:12,216 It doesn't check a condition. 1561 01:03:12,216 --> 01:03:15,966 It does something, and then what does it do? 1562 01:03:15,966 --> 01:03:16,046 >> [ Inaudible ] 1563 01:03:16,046 --> 01:03:16,866 >> David: Then it checks the condition. 1564 01:03:16,866 --> 01:03:18,076 And if the condition is true, 1565 01:03:18,206 --> 01:03:19,806 then it goes back and does it again. 1566 01:03:19,806 --> 01:03:21,326 If it's false, it just breaks there. 1567 01:03:21,656 --> 01:03:23,756 So conceptually the key difference here, 1568 01:03:23,756 --> 01:03:25,706 besides the syntax looking slightly different, 1569 01:03:26,016 --> 01:03:28,336 it just means that if you want a chunk of code 1570 01:03:28,336 --> 01:03:30,496 to execute no matter what initially, 1571 01:03:30,756 --> 01:03:32,566 you can use the do while loop; 1572 01:03:32,766 --> 01:03:34,616 if you want to check a condition first, 1573 01:03:34,866 --> 01:03:35,946 you can use the while loop. 1574 01:03:35,946 --> 01:03:37,176 So let's see this in practice. 1575 01:03:37,176 --> 01:03:40,186 I'm going to open up a program called positive1.c. I'm going 1576 01:03:40,306 --> 01:03:42,996 to scroll down to the juicy part here. 1577 01:03:42,996 --> 01:03:44,946 It, too, is pretty short, but take a look 1578 01:03:44,946 --> 01:03:45,766 at what this one does. 1579 01:03:46,106 --> 01:03:48,406 Here, too, gets a little ugly and this is why I tend not 1580 01:03:48,406 --> 01:03:50,346 to like this construct, at least in C, 1581 01:03:50,586 --> 01:03:52,046 but sometimes it's necessary 1582 01:03:52,046 --> 01:03:53,926 or it's the simplest way to achieve this goal. 1583 01:03:54,186 --> 01:03:57,156 I declare a variable called n. I'm not giving it a value yet, 1584 01:03:57,506 --> 01:03:58,946 so I had better be careful. 1585 01:03:58,946 --> 01:04:00,666 One of the details we'll see over time, 1586 01:04:00,666 --> 01:04:02,206 especially as we discuss security: 1587 01:04:02,686 --> 01:04:06,426 failing to initialize your variables to values you know 1588 01:04:06,426 --> 01:04:09,976 about is very bad practice in general because if you forget, 1589 01:04:09,976 --> 01:04:12,466 if you're writing a program that's not 6 lines but 600, 1590 01:04:12,866 --> 01:04:16,306 leaving things in an unknown state is just asking for trouble 1591 01:04:16,306 --> 01:04:18,476 because it's these kinds of things that bad guys try 1592 01:04:18,476 --> 01:04:20,216 to take advantage of by putting data there 1593 01:04:20,216 --> 01:04:21,336 that you didn't expect. 1594 01:04:21,706 --> 01:04:23,056 But for now, let's see if it's okay. 1595 01:04:23,266 --> 01:04:24,736 Logically looks like I'm safe 1596 01:04:24,736 --> 01:04:25,846 because I know what I'm doing here. 1597 01:04:25,846 --> 01:04:28,516 So int n just gives me four bytes for an int, 1598 01:04:28,906 --> 01:04:30,426 but it doesn't put anything there yet. 1599 01:04:30,576 --> 01:04:31,496 So let's put something there. 1600 01:04:31,496 --> 01:04:33,086 Do the following: Print. 1601 01:04:33,146 --> 01:04:35,156 I demand that you give me a positive integer. 1602 01:04:35,156 --> 01:04:36,636 Okay, I was a little angry when I wrote this. 1603 01:04:36,736 --> 01:04:39,986 So next I cull the CS50 function called GetInt. 1604 01:04:40,106 --> 01:04:41,446 And what do I do with its value? 1605 01:04:41,446 --> 01:04:44,916 I put it in n. So here's where I'm pushing back. 1606 01:04:44,916 --> 01:04:47,736 I know this is okay because I'm not using n 1607 01:04:47,806 --> 01:04:49,896 until I myself put something there. 1608 01:04:49,896 --> 01:04:52,076 So it was a little risky but safe in the end 1609 01:04:52,076 --> 01:04:53,466 because I didn't do anything else with it. 1610 01:04:53,746 --> 01:04:54,566 Now what do I do? 1611 01:04:54,786 --> 01:04:56,076 At this point in the story, 1612 01:04:56,076 --> 01:04:59,726 I now check while n is less than 1, what do I do? 1613 01:04:59,996 --> 01:05:01,066 This again and again. 1614 01:05:01,486 --> 01:05:03,586 So it feels a little backward mentally perhaps, 1615 01:05:03,806 --> 01:05:06,246 but this is saying, "do this block of code 1616 01:05:06,246 --> 01:05:08,476 as long as n is less than 1." 1617 01:05:08,876 --> 01:05:11,566 So the take away is that if the user actually gives me two 1618 01:05:11,566 --> 01:05:14,456 or three or four or whatever, this is this expression 1619 01:05:14,456 --> 01:05:18,726 "is n less than 1" is going to evaluate to false 1620 01:05:18,926 --> 01:05:20,996 if it's actually two or three or four or whatever. 1621 01:05:21,256 --> 01:05:23,766 So this do while loop immediately breaks 1622 01:05:23,766 --> 01:05:26,366 out at the point of that check. 1623 01:05:26,826 --> 01:05:28,966 Now if I am difficult with the user 1624 01:05:28,966 --> 01:05:34,306 and I compile make positive1, and I then run positive1. 1625 01:05:34,916 --> 01:05:37,916 And again I say "David," well, it's going to make me retry. 1626 01:05:37,916 --> 01:05:42,016 Then I type negative1, zero, maybe negative zero. 1627 01:05:42,056 --> 01:05:42,866 Still didn't work. 1628 01:05:42,866 --> 01:05:45,146 One -- thanks for the one. 1629 01:05:45,146 --> 01:05:48,196 And so now it actually spits that out for me. 1630 01:05:48,416 --> 01:05:50,046 So what else can we do with this version? 1631 01:05:50,046 --> 01:05:53,296 Well, let's take a look at this variant, positive2.c. 1632 01:05:53,526 --> 01:05:54,466 So it's a little different. 1633 01:05:54,686 --> 01:05:56,076 But notice now I'm getting a little fancier. 1634 01:05:56,076 --> 01:05:58,096 And you might have started doing these kinds of things in Scratch 1635 01:05:58,096 --> 01:05:59,946 as your programs got more sophisticated. 1636 01:06:00,376 --> 01:06:02,896 Here I'm declaring another data type altogether. 1637 01:06:03,216 --> 01:06:05,886 There's this notion of a Boolean value named after Mr. Bool, 1638 01:06:05,886 --> 01:06:07,766 which is just a notion of true or false. 1639 01:06:08,096 --> 01:06:09,906 This doesn't actually exist in C itself. 1640 01:06:09,906 --> 01:06:12,426 We put it there via the CS50 library. 1641 01:06:12,426 --> 01:06:14,086 We'll pull back that layer before long. 1642 01:06:14,276 --> 01:06:17,166 But other languages, Java, C++, and the like all come with Bool. 1643 01:06:17,486 --> 01:06:19,056 This is a true or false value. 1644 01:06:19,296 --> 01:06:22,156 Because I was taught good style, I'm giving that variable a name 1645 01:06:22,356 --> 01:06:26,236 that conjures up the idea of its role, which is thankful or not. 1646 01:06:26,526 --> 01:06:27,786 I initialize it to false. 1647 01:06:28,256 --> 01:06:28,936 Now what do I do? 1648 01:06:29,126 --> 01:06:30,996 I print out -- and this is only wrapping 1649 01:06:30,996 --> 01:06:31,946 because of the big font -- 1650 01:06:32,136 --> 01:06:34,446 I print out, "I demand that you give me a positive integer." 1651 01:06:34,886 --> 01:06:35,586 I then do this. 1652 01:06:36,166 --> 01:06:39,896 If GetInt's return value is greater than zero, 1653 01:06:39,896 --> 01:06:41,696 what do, in plain English? 1654 01:06:41,696 --> 01:06:42,266 >> [ Inaudible ] 1655 01:06:42,266 --> 01:06:45,936 >> David: I change thankful to true. 1656 01:06:46,136 --> 01:06:47,936 Now notice couple interesting things -- 1657 01:06:47,936 --> 01:06:49,336 GetInt returns a value. 1658 01:06:49,566 --> 01:06:52,576 Apparently in this program, I don't actually care what it is, 1659 01:06:52,576 --> 01:06:57,566 I only care that it is greater than zero, so it suffices. 1660 01:06:57,566 --> 01:07:01,066 It's totally legit to cull a function, get back a value, 1661 01:07:01,066 --> 01:07:04,086 do nothing with it other than compare it to another value. 1662 01:07:04,086 --> 01:07:05,686 So you don't have to use the assignment operator. 1663 01:07:05,686 --> 01:07:07,776 You don't have to declare a special variable for it 1664 01:07:07,776 --> 01:07:09,786 if you don't care about its actual value. 1665 01:07:09,786 --> 01:07:10,986 So I said thankful to true. 1666 01:07:11,226 --> 01:07:13,156 Now down here I can use that other piece 1667 01:07:13,156 --> 01:07:14,186 of syntax we just saw. 1668 01:07:14,186 --> 01:07:17,976 I want to keep doing this while I am not thankful 1669 01:07:17,976 --> 01:07:18,906 for the user's number. 1670 01:07:19,186 --> 01:07:21,886 So in other words, if thankful is still false at this point, 1671 01:07:22,096 --> 01:07:24,666 it means this condition did not work out very well, 1672 01:07:24,846 --> 01:07:26,616 and so I'm going to do this block of code again. 1673 01:07:26,976 --> 01:07:28,856 Now this version is arguably a little better 1674 01:07:28,856 --> 01:07:30,516 in that it's just a little more clear. 1675 01:07:30,516 --> 01:07:31,326 It's a little more readable 1676 01:07:31,326 --> 01:07:33,576 because I'm using a Boolean value that tells me, 1677 01:07:33,576 --> 01:07:36,606 the programmer, this is true or this is false. 1678 01:07:36,926 --> 01:07:38,416 Well, let's clean this up slightly. 1679 01:07:38,696 --> 01:07:40,656 Turns out with a Boolean expression, 1680 01:07:40,656 --> 01:07:43,376 you don't have use the equality operator equals equals, 1681 01:07:43,636 --> 01:07:45,046 you can just say what you mean. 1682 01:07:45,046 --> 01:07:47,766 And this starts to make our code more readable, more elegant, 1683 01:07:47,986 --> 01:07:49,596 just a little more compact. 1684 01:07:50,266 --> 01:07:52,966 So same code up here -- thankful is initialized to false. 1685 01:07:53,406 --> 01:07:54,526 I do print this. 1686 01:07:55,016 --> 01:07:57,746 I check the return value of GetInt is greater than zero. 1687 01:07:57,946 --> 01:08:00,586 I then assign thankful true, if so, 1688 01:08:00,586 --> 01:08:02,276 but notice this little trick: 1689 01:08:02,326 --> 01:08:05,336 I'm going to keep doing this while I am bang thankful -- 1690 01:08:05,336 --> 01:08:08,316 bang exclamation point means "not," so this is shorthand, 1691 01:08:08,316 --> 01:08:10,116 sort of clever elegant notation for 1692 01:08:10,506 --> 01:08:13,106 "while I'm not thankful, keep doing this." 1693 01:08:13,426 --> 01:08:15,056 And so you can begin to express -- 1694 01:08:15,056 --> 01:08:16,866 even though the language is a bit arcane -- 1695 01:08:17,116 --> 01:08:20,826 your ideas in the same way that you might actually speak them. 1696 01:08:21,236 --> 01:08:23,986 So allow me to put this up on the screen now, 1697 01:08:23,986 --> 01:08:30,946 which you're now qualified to understand. 1698 01:08:30,946 --> 01:08:31,013 [ Laughter ] 1699 01:08:31,013 --> 01:08:32,706 I'm very sorry -- we're turning you into the people 1700 01:08:32,706 --> 01:08:33,776 who do understand these. 1701 01:08:34,506 --> 01:08:37,176 Problem Set One will be posted on the course's website tonight 1702 01:08:37,176 --> 01:08:39,206 by 7:00 p.m. It will very clearly walk you 1703 01:08:39,206 --> 01:08:40,306 through the week's challenges. 1704 01:08:40,306 --> 01:08:45,976 We will see you next week.