1 00:00:00,000 --> 00:00:03,353 >> [MUSIC PLAYING] 2 00:00:03,353 --> 00:00:04,842 3 00:00:04,842 --> 00:00:06,050 DAVID MALAN: Hello, everyone. 4 00:00:06,050 --> 00:00:07,777 This is CS50's supersection. 5 00:00:07,777 --> 00:00:09,860 Assigned Sections will not start for another week. 6 00:00:09,860 --> 00:00:11,580 And so today, I'm here with-- 7 00:00:11,580 --> 00:00:12,122 >> MARIA: Maria. 8 00:00:12,122 --> 00:00:14,163 DAVID MALAN: --to actually take a look at problem 9 00:00:14,163 --> 00:00:16,110 set one, the course's first weeks of material, 10 00:00:16,110 --> 00:00:19,780 get you oriented with CS50 IDE, a bit of Linux, the operating system that's 11 00:00:19,780 --> 00:00:22,290 running on IDE, as well as look at some of the transition 12 00:00:22,290 --> 00:00:25,730 from Scratch to C leading up to problem set one. 13 00:00:25,730 --> 00:00:28,340 >> So without further ado, in problem set one, 14 00:00:28,340 --> 00:00:30,520 you're going to be introduced to CS50 IDE, which 15 00:00:30,520 --> 00:00:33,230 is this web-based programming environment that we use in order 16 00:00:33,230 --> 00:00:34,280 to write programs. 17 00:00:34,280 --> 00:00:37,280 It's been designed to look very reminiscent of a typical computer 18 00:00:37,280 --> 00:00:39,430 system running an operating system called Linux. 19 00:00:39,430 --> 00:00:42,440 But it's in the so-called cloud, which means everyone in the class 20 00:00:42,440 --> 00:00:45,100 can actually have his or her own account and use it, as well. 21 00:00:45,100 --> 00:00:48,950 >> So most of you probably have never used a Linux environment before or perhaps 22 00:00:48,950 --> 00:00:50,447 even a command line environment. 23 00:00:50,447 --> 00:00:52,280 And a command line environment is different. 24 00:00:52,280 --> 00:00:53,200 It's all textual. 25 00:00:53,200 --> 00:00:54,730 It's all keyboard commands. 26 00:00:54,730 --> 00:00:59,480 And it's no mousing, no icons, no dialogue windows, no menus. 27 00:00:59,480 --> 00:01:00,212 It's purely text. 28 00:01:00,212 --> 00:01:01,920 So at first, it kind of feels like a step 29 00:01:01,920 --> 00:01:04,434 backwards in terms of sophistication. 30 00:01:04,434 --> 00:01:06,600 But it ends up being a lot more powerful, certainly, 31 00:01:06,600 --> 00:01:08,510 for a computer scientist, ultimately. 32 00:01:08,510 --> 00:01:09,540 >> So let's take a look. 33 00:01:09,540 --> 00:01:13,850 Here I am at CS50.io, via which you can log into CS50 IDE. 34 00:01:13,850 --> 00:01:17,414 And much like in Scratch, there's three primary areas to this screen. 35 00:01:17,414 --> 00:01:20,080 So on the left-hand side, we have what will be the file browser. 36 00:01:20,080 --> 00:01:22,413 And there's this folder at top, which is right now empty 37 00:01:22,413 --> 00:01:24,690 since I got rid of all my files before supersection. 38 00:01:24,690 --> 00:01:27,599 And that's where we'll save C files that we write. 39 00:01:27,599 --> 00:01:30,890 At the top right, we have a place where all of our code's going to get written. 40 00:01:30,890 --> 00:01:34,610 And indeed, if I click this plus, this is going to let me choose New File. 41 00:01:34,610 --> 00:01:38,310 And here I can start writing C code or, really, any number of other languages. 42 00:01:38,310 --> 00:01:42,670 >> And then at the bottom where it says jharvard@ide50-- and yours will 43 00:01:42,670 --> 00:01:45,080 say something a little different-- what was this called? 44 00:01:45,080 --> 00:01:48,644 45 00:01:48,644 --> 00:01:49,310 Terminal window. 46 00:01:49,310 --> 00:01:49,570 Yeah. 47 00:01:49,570 --> 00:01:51,810 So the terminal window is the so-called command line 48 00:01:51,810 --> 00:01:55,540 interface via which you can interact with the underlying operating system. 49 00:01:55,540 --> 00:01:58,220 And for now, we're going to make very little use of this-- 50 00:01:58,220 --> 00:02:01,170 just to compile, to look at error messages that we see, 51 00:02:01,170 --> 00:02:02,347 and to run our programs. 52 00:02:02,347 --> 00:02:04,680 But ultimately, we're going to do so much more, as well. 53 00:02:04,680 --> 00:02:07,490 And you can even install software and administer your workspace 54 00:02:07,490 --> 00:02:09,066 within CS50 IDE, as well. 55 00:02:09,066 --> 00:02:10,440 But more on that some other time. 56 00:02:10,440 --> 00:02:12,689 >> So let's go ahead and write a very simple program just 57 00:02:12,689 --> 00:02:15,064 to go through a warm-up of hello.c, which is perhaps 58 00:02:15,064 --> 00:02:16,480 the simplest program we can write. 59 00:02:16,480 --> 00:02:18,330 I've already opened up a new tab. 60 00:02:18,330 --> 00:02:24,310 I'm going to go ahead and say include standard io.h. 61 00:02:24,310 --> 00:02:25,325 Then int main(void). 62 00:02:25,325 --> 00:02:28,930 63 00:02:28,930 --> 00:02:35,200 And then printf ("hello, world" backslash n, close quote, 64 00:02:35,200 --> 00:02:39,480 close parentheses, and semicolon. 65 00:02:39,480 --> 00:02:39,990 >> All right. 66 00:02:39,990 --> 00:02:41,700 So now notice my window is untitled. 67 00:02:41,700 --> 00:02:44,840 So I'm very quickly going to go up to File, Save. 68 00:02:44,840 --> 00:02:46,910 And it's important that I call this not "hello." 69 00:02:46,910 --> 00:02:49,243 So a very common mistake, especially in problem set one, 70 00:02:49,243 --> 00:02:52,530 is to accidentally just call your program "water" or "greedy" or "Mario" 71 00:02:52,530 --> 00:02:53,890 or "credit" or the like. 72 00:02:53,890 --> 00:02:55,970 But you actually want to make sure you have the file extension 73 00:02:55,970 --> 00:02:58,928 because this is the only way that the compiler and also the code editor 74 00:02:58,928 --> 00:03:00,710 knows that you're actually writing C code. 75 00:03:00,710 --> 00:03:02,280 So let me go ahead and click Save. 76 00:03:02,280 --> 00:03:05,710 >> And now, too, what CS50 IDE has done for us is it's also 77 00:03:05,710 --> 00:03:07,120 syntax highlighted everything. 78 00:03:07,120 --> 00:03:08,822 So it's made everything very colorful. 79 00:03:08,822 --> 00:03:10,780 And the whole purpose of that isn't functional. 80 00:03:10,780 --> 00:03:14,290 It's just to draw my eyes to the conceptually different pieces 81 00:03:14,290 --> 00:03:15,310 of this program. 82 00:03:15,310 --> 00:03:17,570 So let's go ahead and compile this. 83 00:03:17,570 --> 00:03:19,490 And I can compile this in a couple of ways. 84 00:03:19,490 --> 00:03:22,010 And in week one, the old-school way we did 85 00:03:22,010 --> 00:03:26,250 it was literally at the command line-- "clang hello.c." 86 00:03:26,250 --> 00:03:27,596 And then I hit Enter. 87 00:03:27,596 --> 00:03:29,720 And nothing seems to happen in the terminal window. 88 00:03:29,720 --> 00:03:32,654 But what did change in the IDE? 89 00:03:32,654 --> 00:03:35,320 Where, again, IDE just means Integrated Development Environment. 90 00:03:35,320 --> 00:03:38,360 It's a fancy way of saying this integrated development 91 00:03:38,360 --> 00:03:40,620 environment for developing software. 92 00:03:40,620 --> 00:03:42,477 So what did change in the UI? 93 00:03:42,477 --> 00:03:44,310 What did you notice that's different, Maria? 94 00:03:44,310 --> 00:03:47,100 >> MARIA: I noticed something under the IDE50 folder up here. 95 00:03:47,100 --> 00:03:47,850 >> DAVID MALAN: Yeah. 96 00:03:47,850 --> 00:03:50,230 So up here, not only do we have "hello.c." 97 00:03:50,230 --> 00:03:53,120 We also have "a.out," which is kind of a dumb name for a program. 98 00:03:53,120 --> 00:03:56,430 But indeed, it's the default name for a program when you just compile your code 99 00:03:56,430 --> 00:03:59,110 and don't tell the compiler what file to output. 100 00:03:59,110 --> 00:04:01,790 So if I want to run this, I need to tell the compiler-- 101 00:04:01,790 --> 00:04:05,340 or rather, I need to tell the workspace that I 102 00:04:05,340 --> 00:04:09,780 want to run the program called "a.out." 103 00:04:09,780 --> 00:04:10,550 Enter. 104 00:04:10,550 --> 00:04:12,474 And there's "hello, world." now the "./". 105 00:04:12,474 --> 00:04:13,390 is actually important. 106 00:04:13,390 --> 00:04:16,050 Even though normally we only write the names of commands, 107 00:04:16,050 --> 00:04:19,060 when it's a program you've written, you do want to say "./" 108 00:04:19,060 --> 00:04:22,460 because that explicitly tells Linux that you want to run a program called 109 00:04:22,460 --> 00:04:26,930 "a.out" that's in this, where dot means this directory that I'm currently 110 00:04:26,930 --> 00:04:31,600 in, which happens to be called workspace, "a.out. 111 00:04:31,600 --> 00:04:35,410 Because if I just said "a.out," Enter, I'm going to get "a.out command not 112 00:04:35,410 --> 00:04:38,035 found" because the computer-- because I invented "a.out;" 113 00:04:38,035 --> 00:04:40,910 it didn't come with the computer-- doesn't know where to look for it, 114 00:04:40,910 --> 00:04:43,590 even though it's right under my nose, so to speak. 115 00:04:43,590 --> 00:04:44,090 OK. 116 00:04:44,090 --> 00:04:49,910 So we could change this to be "clang -o hello hello.c." 117 00:04:49,910 --> 00:04:53,030 And if I hit Enter, what program is this going to output for me? 118 00:04:53,030 --> 00:04:55,710 What's the name of the file? 119 00:04:55,710 --> 00:04:57,500 Yeah, in back. 120 00:04:57,500 --> 00:04:58,479 That's right. "Hello." 121 00:04:58,479 --> 00:05:00,520 So "hello" is going to be the name of the program 122 00:05:00,520 --> 00:05:04,610 because we've used a so-called command line argument, which is just 123 00:05:04,610 --> 00:05:08,120 a way of changing the behavior of the compiler to actually output 124 00:05:08,120 --> 00:05:09,450 a specific file name. 125 00:05:09,450 --> 00:05:12,400 And indeed, if I zoom out and look up here, 126 00:05:12,400 --> 00:05:17,430 now I have not only "a.out" and "hello.c" but also "hello," as well. 127 00:05:17,430 --> 00:05:22,410 So now I can do "./hello," Enter. 128 00:05:22,410 --> 00:05:23,640 And there's "hello, world." 129 00:05:23,640 --> 00:05:25,640 But lastly-- and this will now be the convention 130 00:05:25,640 --> 00:05:27,590 we use throughout the semester, typically-- 131 00:05:27,590 --> 00:05:30,070 is you can also just say "make hello." 132 00:05:30,070 --> 00:05:33,370 And indeed, it's saying it's up to date because it already exists. 133 00:05:33,370 --> 00:05:37,860 Let me go ahead and remove, with the rm command, both "a.out"-- 134 00:05:37,860 --> 00:05:39,644 and say "remove regular file a.out?" 135 00:05:39,644 --> 00:05:41,810 That just means, are you sure you want to delete it? 136 00:05:41,810 --> 00:05:42,880 I'm going to say yes. 137 00:05:42,880 --> 00:05:46,690 And then I'm going to remove "hello" but not "hello.c." 138 00:05:46,690 --> 00:05:49,920 I'm going to say "yes" explicitly, but "y" is sufficient, as well. 139 00:05:49,920 --> 00:05:52,140 >> And now notice if I type ls-- which, recall, 140 00:05:52,140 --> 00:05:55,540 means list-- it lists all of the files in my folder. 141 00:05:55,540 --> 00:05:57,850 And indeed, if I zoom out and look at top left, 142 00:05:57,850 --> 00:05:59,730 it confirms that what's in this folder, even 143 00:05:59,730 --> 00:06:04,990 though it's labeled as IDE50 up here and not workspace, it's just "hello.c." 144 00:06:04,990 --> 00:06:09,152 So now down here, if I do "make hello," Enter, 145 00:06:09,152 --> 00:06:12,360 now I see a much longer command but a more sophisticated command that's going 146 00:06:12,360 --> 00:06:13,940 to be useful in the weeks to come. 147 00:06:13,940 --> 00:06:16,720 And now I can say "./hello." 148 00:06:16,720 --> 00:06:19,257 >> So let's take a quick tour some other Linux commands. 149 00:06:19,257 --> 00:06:21,840 Then why don't we take a step back and actually look at C more 150 00:06:21,840 --> 00:06:25,020 generally, transitioning from Scratch to C, and then conclude with a look 151 00:06:25,020 --> 00:06:27,305 at the first problem set in C. All right. 152 00:06:27,305 --> 00:06:30,430 So I'm going to go ahead and just clean up my workspace with Control-L just 153 00:06:30,430 --> 00:06:31,429 to keep the screen neat. 154 00:06:31,429 --> 00:06:33,520 But that has no functional impact otherwise. 155 00:06:33,520 --> 00:06:35,820 >> Recall that we've seen a few commands now. 156 00:06:35,820 --> 00:06:39,900 So we've seen clang, which generally you won't have to execute manually anymore. 157 00:06:39,900 --> 00:06:41,650 We'll instead use make. 158 00:06:41,650 --> 00:06:45,290 But we also saw ls, which shows a listing of the files in my directory. 159 00:06:45,290 --> 00:06:48,750 And now why is there-- there's two files now, "hello" and "hello.c." 160 00:06:48,750 --> 00:06:52,240 Why is there a star or an asterisk after "hello"? 161 00:06:52,240 --> 00:06:54,900 What did that signify, based on what we saw in week one? 162 00:06:54,900 --> 00:06:58,852 163 00:06:58,852 --> 00:07:00,400 What do you think? 164 00:07:00,400 --> 00:07:02,742 What does the star signify? 165 00:07:02,742 --> 00:07:04,614 >> MARIA: For-- the program "hello"? 166 00:07:04,614 --> 00:07:05,530 DAVID MALAN: "Hello*". 167 00:07:05,530 --> 00:07:06,330 Yeah. 168 00:07:06,330 --> 00:07:07,760 Oh, yeah? 169 00:07:07,760 --> 00:07:08,639 Oh, executable. 170 00:07:08,639 --> 00:07:09,180 That's right. 171 00:07:09,180 --> 00:07:10,890 So that means that "hello" is executable. 172 00:07:10,890 --> 00:07:13,430 So indeed, that's why I can do "./hello." 173 00:07:13,430 --> 00:07:13,930 All right. 174 00:07:13,930 --> 00:07:15,179 So what else can I do in here? 175 00:07:15,179 --> 00:07:17,290 Well, it turns out I can also create directories. 176 00:07:17,290 --> 00:07:20,924 So let me go ahead and create, for instance, a "pset1" directory. 177 00:07:20,924 --> 00:07:23,590 And the problem set specification will have you do exactly this, 178 00:07:23,590 --> 00:07:25,260 so you don't have to remember today. 179 00:07:25,260 --> 00:07:27,340 But "mkdir pset1" seems to have no impact. 180 00:07:27,340 --> 00:07:30,540 But again, no error message is generally a good thing. 181 00:07:30,540 --> 00:07:34,060 So if I type "ls" now-- ah, now I have an executable file called 182 00:07:34,060 --> 00:07:36,570 "hello," a C file called "hello.c." 183 00:07:36,570 --> 00:07:40,860 And then the trailing slash, so to speak, means that this is a directory. 184 00:07:40,860 --> 00:07:42,510 >> So indeed, now I want to open it. 185 00:07:42,510 --> 00:07:45,730 And I don't double click on it like in a GUI, a graphical environment. 186 00:07:45,730 --> 00:07:48,550 I instead say "cd pset1." 187 00:07:48,550 --> 00:07:49,640 Enter. 188 00:07:49,640 --> 00:07:53,100 Nothing interesting seems to have happened except my little prompt 189 00:07:53,100 --> 00:07:57,300 here-- this is Linux's way of reminding me where I am, so what folder is open. 190 00:07:57,300 --> 00:08:00,080 It's just telling me explicitly as opposed to graphically. 191 00:08:00,080 --> 00:08:05,100 And if I type "ls," why do I see another prompt immediately, do you think, 192 00:08:05,100 --> 00:08:07,269 when I list the contents of pset1? 193 00:08:07,269 --> 00:08:09,310 MARIA: You probably don't have anything in there. 194 00:08:09,310 --> 00:08:09,630 DAVID MALAN: Yeah. 195 00:08:09,630 --> 00:08:11,510 So then we haven't obviously created anything in there 196 00:08:11,510 --> 00:08:13,150 because I just created the directory. 197 00:08:13,150 --> 00:08:17,690 Now, if I wanted to create a file-- for instance, I could create a new file. 198 00:08:17,690 --> 00:08:20,190 And then I could go to Save and save it as, 199 00:08:20,190 --> 00:08:23,090 like, "mario.c" if you're doing the standard edition of pset one. 200 00:08:23,090 --> 00:08:27,610 And then, like any Mac or PC, just choose the folder "pset1, Save. 201 00:08:27,610 --> 00:08:28,740 And now the file is empty. 202 00:08:28,740 --> 00:08:31,160 But let's zoom in again for a second. 203 00:08:31,160 --> 00:08:32,470 Let's do ls here. 204 00:08:32,470 --> 00:08:34,470 Now we see "mario.c". 205 00:08:34,470 --> 00:08:37,850 >> So there's a few other commands that are worth keeping in mind over time-- 206 00:08:37,850 --> 00:08:41,200 clear, or Control-L is what I've been hitting; mkdir we've seen; 207 00:08:41,200 --> 00:08:45,240 rm we've seen, as well, which is for removing or deleting a file. 208 00:08:45,240 --> 00:08:49,310 Beware you can also-- and if you look at online tutorials, you'll rm -rf 209 00:08:49,310 --> 00:08:52,630 is a very common way of saying delete a folder that has stuff in it. 210 00:08:52,630 --> 00:08:54,280 Just be super, super careful. 211 00:08:54,280 --> 00:08:58,780 -rf means recursively delete whatever it is 212 00:08:58,780 --> 00:09:01,150 you're trying to delete and forcibly delete it. 213 00:09:01,150 --> 00:09:04,630 So recursive means if it's a folder with a folder with a folder with a folder, 214 00:09:04,630 --> 00:09:05,690 delete all of them. 215 00:09:05,690 --> 00:09:08,680 And forcibly means don't even ask me yes or no, am I sure? 216 00:09:08,680 --> 00:09:11,960 So it's a super dangerous way of deleting lots of stuff quickly. 217 00:09:11,960 --> 00:09:17,210 But beware-- too often does an intrepid student accidentally delete, say, pset 218 00:09:17,210 --> 00:09:18,000 one, for instance. 219 00:09:18,000 --> 00:09:19,833 >> MARIA: And if they just do -r, they're going 220 00:09:19,833 --> 00:09:21,870 to ask them about every single file. 221 00:09:21,870 --> 00:09:22,790 >> DAVID MALAN: Which is moderately annoying. 222 00:09:22,790 --> 00:09:23,480 >> MARIA: Yeah. 223 00:09:23,480 --> 00:09:25,813 >> DAVID MALAN: So that's why so many of us, self included, 224 00:09:25,813 --> 00:09:27,070 am in the habit of using -rf. 225 00:09:27,070 --> 00:09:27,861 But it's dangerous. 226 00:09:27,861 --> 00:09:29,010 So buyer beware. 227 00:09:29,010 --> 00:09:31,050 And then mv is kind of interestingly named. 228 00:09:31,050 --> 00:09:33,600 So this is the move command, which feels a little weird 229 00:09:33,600 --> 00:09:37,260 because you can indeed use it to move files from one place to another. 230 00:09:37,260 --> 00:09:39,970 For instance, suppose that I messed up. 231 00:09:39,970 --> 00:09:43,660 Suppose that I created a new file for the pset. 232 00:09:43,660 --> 00:09:46,320 And I saved it as "greedy.c." 233 00:09:46,320 --> 00:09:49,970 But suppose I accidentally save it in IDE50-- so in my workspace itself 234 00:09:49,970 --> 00:09:51,580 and not in pset1. 235 00:09:51,580 --> 00:09:53,960 >> You can see as much at top left. 236 00:09:53,960 --> 00:09:55,284 There's "greedy.c." 237 00:09:55,284 --> 00:09:56,450 And there's a few solutions. 238 00:09:56,450 --> 00:09:58,324 So one, I can use the super user-friendly way 239 00:09:58,324 --> 00:09:59,740 of just dragging and dropping it. 240 00:09:59,740 --> 00:10:01,910 And that would actually fix this problem. 241 00:10:01,910 --> 00:10:04,967 But today, we're trying to look at the more arcane but powerful ways 242 00:10:04,967 --> 00:10:05,550 of doing this. 243 00:10:05,550 --> 00:10:07,880 So let me take "ls" in pset1. 244 00:10:07,880 --> 00:10:09,690 And I only see "mario.c." 245 00:10:09,690 --> 00:10:13,450 Let me go ahead and do "cd ..". 246 00:10:13,450 --> 00:10:15,320 So CD again is change directory. 247 00:10:15,320 --> 00:10:18,130 ".." means what, though? 248 00:10:18,130 --> 00:10:21,100 Last time I said "cd pset1" to go into pset1. 249 00:10:21,100 --> 00:10:24,360 So when I say "cd ..", what am I doing? 250 00:10:24,360 --> 00:10:24,910 Backwards. 251 00:10:24,910 --> 00:10:25,150 Yeah. 252 00:10:25,150 --> 00:10:26,750 >> So it's going to the so-called parent. 253 00:10:26,750 --> 00:10:29,730 Go one level up, like exiting out of a folder. 254 00:10:29,730 --> 00:10:30,500 So Enter. 255 00:10:30,500 --> 00:10:31,900 And indeed, look what it did. 256 00:10:31,900 --> 00:10:37,070 It moved me just into workspace instead of into pset1, which is in workspace. 257 00:10:37,070 --> 00:10:39,710 And now if I type "ls", there's a whole bunch of stuff. 258 00:10:39,710 --> 00:10:41,650 In there is "greedy.c". 259 00:10:41,650 --> 00:10:48,720 So now let me use mv to literally move "greedy.c" into pset1. 260 00:10:48,720 --> 00:10:51,120 And so a lot of Linux commands work exactly like this. 261 00:10:51,120 --> 00:10:54,470 They take no arguments or they take one argument or they take two command line 262 00:10:54,470 --> 00:10:55,449 arguments and so forth. 263 00:10:55,449 --> 00:10:57,240 And in this case, it's literally doing what 264 00:10:57,240 --> 00:11:01,670 it says, albeit succinctly-- move greedy.c into pset1. 265 00:11:01,670 --> 00:11:02,310 Enter. 266 00:11:02,310 --> 00:11:03,310 Nothing seems to happen. 267 00:11:03,310 --> 00:11:06,180 But if I do a ls again, greedy is gone. 268 00:11:06,180 --> 00:11:12,437 And if I do "cd pset1", Enter, and then ls again, now it's in the right place. 269 00:11:12,437 --> 00:11:14,270 As an aside, if for some reason-- especially 270 00:11:14,270 --> 00:11:16,194 if you have intermittent network connectivity 271 00:11:16,194 --> 00:11:18,360 or you're walking around campus with your laptop lid 272 00:11:18,360 --> 00:11:20,180 closed and then open it up again and your workspace 273 00:11:20,180 --> 00:11:21,730 seems somewhat out of sync, no big deal. 274 00:11:21,730 --> 00:11:23,646 Just reload your browser window, and it should 275 00:11:23,646 --> 00:11:26,339 resync so that the left-hand file browser looks exactly 276 00:11:26,339 --> 00:11:27,380 like the terminal window. 277 00:11:27,380 --> 00:11:27,921 Not to worry. 278 00:11:27,921 --> 00:11:31,460 When in doubt, reload the page, so long as you've saved the files already. 279 00:11:31,460 --> 00:11:31,960 OK. 280 00:11:31,960 --> 00:11:34,710 So mv can also be used to rename files. 281 00:11:34,710 --> 00:11:36,760 And let's take a look at this final command here. 282 00:11:36,760 --> 00:11:39,950 So suppose-- and this is super common, too, early on. 283 00:11:39,950 --> 00:11:46,400 Some students occasionally create a file called, let's say-- what's another one? 284 00:11:46,400 --> 00:11:47,730 Like "WATER.C." 285 00:11:47,730 --> 00:11:51,080 So I'll just yell, for no reason, in all caps. 286 00:11:51,080 --> 00:11:53,190 But this is not the correct name for the file 287 00:11:53,190 --> 00:11:56,660 if only because our problem set specification did not tell you 288 00:11:56,660 --> 00:11:59,430 to save the file as all caps "WATER.C." 289 00:11:59,430 --> 00:12:01,320 Instead, we expect "water.c" in lowercase. 290 00:12:01,320 --> 00:12:03,140 And, in fact, this is problematic because check50, 291 00:12:03,140 --> 00:12:04,370 a program you're going to meet in problem 292 00:12:04,370 --> 00:12:07,350 set one that automatically tests the correctness of your code 293 00:12:07,350 --> 00:12:11,615 is going to yell at you if it can't find "water.c" in all lowercase. 294 00:12:11,615 --> 00:12:12,712 >> So I need to fix this. 295 00:12:12,712 --> 00:12:14,920 So many different ways to do this, the first of which 296 00:12:14,920 --> 00:12:18,330 would be to Control-click or right-click the file name and just change Rename. 297 00:12:18,330 --> 00:12:19,740 Totally fine to do that. 298 00:12:19,740 --> 00:12:21,820 But again, today, let's do it a little fancier. 299 00:12:21,820 --> 00:12:26,680 Let's use mv to change "WATER.C" to "water.c." 300 00:12:26,680 --> 00:12:30,690 So you can also use the first argument to rename it to the second argument 301 00:12:30,690 --> 00:12:34,455 if the second argument is not, in fact, a folder or a directory name. 302 00:12:34,455 --> 00:12:35,120 >> All right. 303 00:12:35,120 --> 00:12:39,560 And lastly, a little trick-- so I'm in my workspace directory at the moment. 304 00:12:39,560 --> 00:12:43,660 If I want to get into pset one, I can certainly type "cd pset1." 305 00:12:43,660 --> 00:12:47,260 But it's so tedious to type "pset1" or longer file names or directory names 306 00:12:47,260 --> 00:12:47,980 still. 307 00:12:47,980 --> 00:12:51,960 So very often in Linux, what you can do is start typing "pse" 308 00:12:51,960 --> 00:12:56,100 and just get bored, hit Tab, and let the computer figure it out for you. 309 00:12:56,100 --> 00:12:58,190 Super helpful to get into that habit. 310 00:12:58,190 --> 00:13:01,079 The only catch is if you have multiple files or folders starting 311 00:13:01,079 --> 00:13:02,870 with the letter "p" or "ps," you might have 312 00:13:02,870 --> 00:13:05,525 to type a few more before the computer knows what to do. 313 00:13:05,525 --> 00:13:06,820 >> MARIA: We like shortcuts. 314 00:13:06,820 --> 00:13:08,695 >> DAVID MALAN: This will save you so much time. 315 00:13:08,695 --> 00:13:11,930 And also, as we said in lecture, you can scroll up in history, up and down, 316 00:13:11,930 --> 00:13:15,461 to find recently executed commands, as well. 317 00:13:15,461 --> 00:13:15,960 All right. 318 00:13:15,960 --> 00:13:20,904 So now, let's turn our attention back to a simple program, hello world. 319 00:13:20,904 --> 00:13:22,195 So we've compiled this exactly. 320 00:13:22,195 --> 00:13:24,050 And now why don't we take a look at the data types 321 00:13:24,050 --> 00:13:26,776 and then transition from some corresponding Scratch blocks to C. 322 00:13:26,776 --> 00:13:27,400 MARIA: Awesome. 323 00:13:27,400 --> 00:13:30,031 So now that you've started writing programs in C, 324 00:13:30,031 --> 00:13:32,530 we're going to start talking about data types and variables. 325 00:13:32,530 --> 00:13:37,570 So some data types that you would want to know starting now 326 00:13:37,570 --> 00:13:39,400 are the ones that are highlighted in blue. 327 00:13:39,400 --> 00:13:42,420 So we have int first, which stands for integer. 328 00:13:42,420 --> 00:13:46,075 And that holds integers, as you might have guessed-- so 1, 2, 3, 329 00:13:46,075 --> 00:13:47,950 and any other integers that you can think of. 330 00:13:47,950 --> 00:13:48,730 >> DAVID MALAN: And also negative. 331 00:13:48,730 --> 00:13:50,220 >> MARIA: And also negative ones, yep. 332 00:13:50,220 --> 00:13:51,450 And 0. 333 00:13:51,450 --> 00:13:54,250 Then we have floats, which are floating point numbers. 334 00:13:54,250 --> 00:13:58,360 So that's all the real numbers that have decimal points. 335 00:13:58,360 --> 00:14:03,960 So 5.0, 5.2, and even more digits after the decimal point, 336 00:14:03,960 --> 00:14:05,620 as well, and also negative ones. 337 00:14:05,620 --> 00:14:07,530 Then we have char which is a character. 338 00:14:07,530 --> 00:14:10,240 So I think we talked about this in lecture today. 339 00:14:10,240 --> 00:14:15,682 But we have letters, for example-- A, B, C-- that can be stored as a char. 340 00:14:15,682 --> 00:14:18,390 But then we have a lot more symbols that can be stored as a char. 341 00:14:18,390 --> 00:14:22,380 And those are stored as ASCII. 342 00:14:22,380 --> 00:14:25,280 >> And then we have bool, which are Booleans. 343 00:14:25,280 --> 00:14:29,067 And those evaluate to True or False or 1 and 0, respectively. 344 00:14:29,067 --> 00:14:31,650 DAVID MALAN: And recall that bool comes from the CS50 library. 345 00:14:31,650 --> 00:14:33,300 So it's not built into C, but it's super useful 346 00:14:33,300 --> 00:14:34,850 to have the notion of true and false. 347 00:14:34,850 --> 00:14:36,990 And even though you might think-- fun fact. 348 00:14:36,990 --> 00:14:43,700 A bool technically needs how many bits, truly, to represent True versus False? 349 00:14:43,700 --> 00:14:47,910 How many bits do you think you would need, maximally? 350 00:14:47,910 --> 00:14:48,790 Yeah, just one. 351 00:14:48,790 --> 00:14:52,077 So it turns out that C can't give you just one bit. 352 00:14:52,077 --> 00:14:54,160 The smallest unit of measure you can get is eight. 353 00:14:54,160 --> 00:14:57,090 So it's a little wasteful they you're using an entire byte, or eight bits, 354 00:14:57,090 --> 00:14:58,340 to represent True or False. 355 00:14:58,340 --> 00:15:01,807 But that's just the way it's implemented in C by the CS50 library. 356 00:15:01,807 --> 00:15:04,890 MARIA: And then the ones that we're not going to talk about as extensively 357 00:15:04,890 --> 00:15:08,060 right now-- double, that's for bigger floats. 358 00:15:08,060 --> 00:15:09,811 Long long and short are also for integers. 359 00:15:09,811 --> 00:15:10,643 DAVID MALAN: Indeed. 360 00:15:10,643 --> 00:15:13,190 In hacker edition of pset one, we actually use long long. 361 00:15:13,190 --> 00:15:15,801 But for the standard edition, it should not be necessary. 362 00:15:15,801 --> 00:15:16,300 MARIA: Cool. 363 00:15:16,300 --> 00:15:17,361 OK. 364 00:15:17,361 --> 00:15:20,110 So operators-- you should be probably familiar with most of these. 365 00:15:20,110 --> 00:15:22,620 Addition, +; subtraction, -. 366 00:15:22,620 --> 00:15:24,070 For multiplication, we have the *. 367 00:15:24,070 --> 00:15:25,900 So not the X that we usually use. 368 00:15:25,900 --> 00:15:27,840 Division, we have the backslash. 369 00:15:27,840 --> 00:15:31,380 And modulo is our last operator that we are going to talk about right now. 370 00:15:31,380 --> 00:15:35,610 It's an operator that allows us to take the remainder of a division. 371 00:15:35,610 --> 00:15:40,630 So if we have 4 % 2, 4 divided by 2 is 2 with a remainder of 0. 372 00:15:40,630 --> 00:15:41,740 So 4 % 2 is 0. 373 00:15:41,740 --> 00:15:42,950 4 % 3 is 1. 374 00:15:42,950 --> 00:15:44,870 4 % 5 is 4, as you could have guessed. 375 00:15:44,870 --> 00:15:48,860 And remember when using all of these operators to use the PEMDAS rules. 376 00:15:48,860 --> 00:15:54,060 >> DAVID MALAN: So to be clear, if you do 4 % 2, why is that 0, exactly? 377 00:15:54,060 --> 00:15:57,480 >> MARIA: Because 4 divided by 2 is 2 with a remainder of 0. 378 00:15:57,480 --> 00:16:00,752 So modulo gives us that remainder as opposed to the result of the division. 379 00:16:00,752 --> 00:16:02,960 DAVID MALAN: And indeed, what we'll find this helpful 380 00:16:02,960 --> 00:16:05,640 for is that in some problems that's not necessarily one, 381 00:16:05,640 --> 00:16:08,040 if you want to have the effect of restricting yourself 382 00:16:08,040 --> 00:16:10,720 to a range of numbers like 0 through something, 383 00:16:10,720 --> 00:16:13,904 you can use modulo to wrap around earlier than like the 2 billion 384 00:16:13,904 --> 00:16:16,320 or the 4 billion value that we've talked about in lecture. 385 00:16:16,320 --> 00:16:16,870 >> MARIA: Yeah. 386 00:16:16,870 --> 00:16:19,429 And even in "greedy" we might. 387 00:16:19,429 --> 00:16:21,345 DAVID MALAN: Yes, in problem set one, as well. 388 00:16:21,345 --> 00:16:22,520 MARIA: Yeah, nice. 389 00:16:22,520 --> 00:16:24,340 DAVID MALAN: Good hint. 390 00:16:24,340 --> 00:16:25,750 MARIA: Sorry. 391 00:16:25,750 --> 00:16:28,590 Boolean expressions-- so for Boolean operators, 392 00:16:28,590 --> 00:16:31,770 we are going to talk about all of these that you see here. 393 00:16:31,770 --> 00:16:35,310 So right now, we have two equal signs listed for equals. 394 00:16:35,310 --> 00:16:38,650 So those are for comparison. 395 00:16:38,650 --> 00:16:40,830 So far, we've only seen one equals sign. 396 00:16:40,830 --> 00:16:44,870 That's when we assign a value to a variable. 397 00:16:44,870 --> 00:16:50,160 So if we've seen int n=5, then we've assigned 5 to the variable n. 398 00:16:50,160 --> 00:16:55,600 But if we want to use == for comparison, we can check if n == 5. 399 00:16:55,600 --> 00:16:58,310 And if n == 5, then that's true. 400 00:16:58,310 --> 00:17:01,950 >> So Boolean operators allow us, basically, 401 00:17:01,950 --> 00:17:04,670 to evaluate Boolean expressions to True or False. 402 00:17:04,670 --> 00:17:07,819 So not equals-- we have the exclamation mark and equals. 403 00:17:07,819 --> 00:17:14,240 So we could also check if n doesn't equal 5-- so n != 5. 404 00:17:14,240 --> 00:17:17,640 Less than, less than or equal to, greater than, greater than or equal to, 405 00:17:17,640 --> 00:17:20,720 and then we have the logical AND and the logical OR. 406 00:17:20,720 --> 00:17:24,060 And those allow us to evaluate multiple Boolean expressions together 407 00:17:24,060 --> 00:17:27,930 to basically come together as one whole Boolean expression. 408 00:17:27,930 --> 00:17:34,970 So if we want to find a number, say, that is greater than 5 but less than 15 409 00:17:34,970 --> 00:17:39,810 at the same time, we would use the logical AND operator to see if n is 410 00:17:39,810 --> 00:17:42,937 greater than 5 && n less than 15. 411 00:17:42,937 --> 00:17:44,770 DAVID MALAN: And here, too, it's really easy 412 00:17:44,770 --> 00:17:47,290 early on to accidentally use just a single ampersand 413 00:17:47,290 --> 00:17:48,572 or a single vertical bar. 414 00:17:48,572 --> 00:17:51,280 And hopefully the compiler will yell at you because they actually 415 00:17:51,280 --> 00:17:52,530 have a very different meaning. 416 00:17:52,530 --> 00:17:55,660 For those curious, they're used for bitwise operations, 417 00:17:55,660 --> 00:17:57,197 operating on individual bits. 418 00:17:57,197 --> 00:17:58,780 But you do want the pair of them here. 419 00:17:58,780 --> 00:18:01,560 And super important is that first one, the equal sign, 420 00:18:01,560 --> 00:18:05,270 which is the equality operator as opposed to the assignment operator. 421 00:18:05,270 --> 00:18:08,552 >> MARIA: And the vertical bar is located between the Delete and Return. 422 00:18:08,552 --> 00:18:09,260 DAVID MALAN: Yes. 423 00:18:09,260 --> 00:18:11,420 On a typical US keyboard. 424 00:18:11,420 --> 00:18:13,190 >> MARIA: Yep. 425 00:18:13,190 --> 00:18:15,890 So let's jump right into conditional statements. 426 00:18:15,890 --> 00:18:18,300 In Scratch, you've already seen, probably, 427 00:18:18,300 --> 00:18:22,730 if statements that allow you to check if something is true, then 428 00:18:22,730 --> 00:18:23,660 do something else. 429 00:18:23,660 --> 00:18:28,410 So you might have wanted to check if your sprite is 430 00:18:28,410 --> 00:18:31,410 touching some other sprite or the border of your screen. 431 00:18:31,410 --> 00:18:35,520 And then you might have wanted to end the game or do something else. 432 00:18:35,520 --> 00:18:40,000 So the syntax for that is "if (condition)." 433 00:18:40,000 --> 00:18:46,820 So if your sprite is touching something, then do is inside the curly braces. 434 00:18:46,820 --> 00:18:48,980 >> Then we have if-else statements. 435 00:18:48,980 --> 00:18:54,080 Else allows us to do something if the condition that we 436 00:18:54,080 --> 00:18:55,870 check for in the beginning is not true. 437 00:18:55,870 --> 00:19:01,530 So if the sprite is touching the border do this, else do something else. 438 00:19:01,530 --> 00:19:04,030 So otherwise do something else. 439 00:19:04,030 --> 00:19:07,530 Then we have a short example for this. 440 00:19:07,530 --> 00:19:11,551 So if (military_time < 12), we want to printf "Good morning!" 441 00:19:11,551 --> 00:19:13,300 Else we're going to printf "Good evening!" 442 00:19:13,300 --> 00:19:14,860 Basic example. 443 00:19:14,860 --> 00:19:15,680 >> DAVID MALAN: Good. 444 00:19:15,680 --> 00:19:16,760 >> MARIA: Cool. 445 00:19:16,760 --> 00:19:18,560 So now we have switch statements. 446 00:19:18,560 --> 00:19:21,960 Switch statements in general can allow us to do a lot of the same thing 447 00:19:21,960 --> 00:19:25,070 that we just talked about with if statements. 448 00:19:25,070 --> 00:19:29,440 For example, right now we have the general form of the switch statement 449 00:19:29,440 --> 00:19:33,870 that allows us to take a variable called n 450 00:19:33,870 --> 00:19:37,670 and compare it to a lot of different values, which here we 451 00:19:37,670 --> 00:19:40,010 have called constant1, constant2. 452 00:19:40,010 --> 00:19:41,150 We may have a lot more. 453 00:19:41,150 --> 00:19:42,930 And those are called cases. 454 00:19:42,930 --> 00:19:46,280 >> So if we have a switch statement-- and this works 455 00:19:46,280 --> 00:19:50,580 only for integers-- we have a variable in this case that's n. 456 00:19:50,580 --> 00:19:53,840 If the variable n is equal to constant1, we 457 00:19:53,840 --> 00:19:59,720 will execute some snippet of code or something that we want to do. 458 00:19:59,720 --> 00:20:01,280 And then we would break. 459 00:20:01,280 --> 00:20:04,820 So the break means that the switch statement 460 00:20:04,820 --> 00:20:08,120 will stop executing if n is equal to constant1, 461 00:20:08,120 --> 00:20:09,752 and then your program will continue. 462 00:20:09,752 --> 00:20:11,835 It will exit out of the switch statement and it'll 463 00:20:11,835 --> 00:20:13,520 continue to do something else. 464 00:20:13,520 --> 00:20:17,204 >> If, however, n does not equal constant1, then case for constant2 465 00:20:17,204 --> 00:20:17,870 will be checked. 466 00:20:17,870 --> 00:20:21,750 So if n equals constant2, that other snippet of code will be executed. 467 00:20:21,750 --> 00:20:23,850 And then it'll break if that is equal to it. 468 00:20:23,850 --> 00:20:27,580 And then we can have also-- not necessarily, though-- a default case, 469 00:20:27,580 --> 00:20:32,290 which will happen if n does not equal any of the cases that you have listed. 470 00:20:32,290 --> 00:20:35,400 And in the default case, we also don't need a break necessarily 471 00:20:35,400 --> 00:20:39,271 because the switch statement will know that it needs to end after the default 472 00:20:39,271 --> 00:20:40,270 case if that's the case. 473 00:20:40,270 --> 00:20:42,140 >> DAVID MALAN: But stylistically, we always put it in there. 474 00:20:42,140 --> 00:20:42,540 >> MARIA: Yeah. 475 00:20:42,540 --> 00:20:42,710 >> DAVID MALAN: Yeah. 476 00:20:42,710 --> 00:20:44,750 So especially when starting out, especially if among those less 477 00:20:44,750 --> 00:20:47,458 comfortable, I would personally just recommend stick with the ifs 478 00:20:47,458 --> 00:20:51,564 and ifs-elses and if-else-if-elses and so forth if only 479 00:20:51,564 --> 00:20:53,480 because they're a little more straightforward. 480 00:20:53,480 --> 00:20:56,142 This is a nice optimization, or sometimes even aesthetically it 481 00:20:56,142 --> 00:20:57,350 makes the code more readable. 482 00:20:57,350 --> 00:20:59,599 And in fact, probably mid-semester we'll see a problem 483 00:20:59,599 --> 00:21:02,504 set where it just looks better and easier to read for the humans 484 00:21:02,504 --> 00:21:03,670 by using a switch statement. 485 00:21:03,670 --> 00:21:05,460 But don't worry about clinging to this too early. 486 00:21:05,460 --> 00:21:05,680 >> MARIA: Yeah. 487 00:21:05,680 --> 00:21:08,840 If you have a lot of if statements, it might just not be very readable. 488 00:21:08,840 --> 00:21:10,965 It might not be very fast for you to go through it. 489 00:21:10,965 --> 00:21:13,230 So switch statements can be very useful, as well. 490 00:21:13,230 --> 00:21:17,900 And also, don't remember-- don't forget to put the breaks in there. 491 00:21:17,900 --> 00:21:20,330 >> DAVID MALAN: Don't remember this. 492 00:21:20,330 --> 00:21:24,890 >> MARIA: Because then you might fall through from one case to another. 493 00:21:24,890 --> 00:21:27,930 So let's say that case constant1 didn't have a break statement in it. 494 00:21:27,930 --> 00:21:31,920 Then we might fall through inside case constant2. 495 00:21:31,920 --> 00:21:35,070 And we don't want to do that if we've already reached case constant1 496 00:21:35,070 --> 00:21:37,830 and it's equal to n. 497 00:21:37,830 --> 00:21:45,100 >> So, for example, if we have a variable n that contains a class number, 498 00:21:45,100 --> 00:21:48,990 and we want to see what that class is-- if n equals 50, 499 00:21:48,990 --> 00:21:51,957 we're going to print "CS50 is Introduction to Computer Science I." 500 00:21:51,957 --> 00:21:53,040 Then we're going to break. 501 00:21:53,040 --> 00:21:53,920 And that's it. 502 00:21:53,920 --> 00:21:55,950 If, however, n is equal to 51, we're going 503 00:21:55,950 --> 00:21:58,494 to print "CS51 is Introduction to Computer Science II." 504 00:21:58,494 --> 00:21:59,910 Then, again, we're going to break. 505 00:21:59,910 --> 00:22:05,080 However, if then we put 124, 61, any other number that you might think of, 506 00:22:05,080 --> 00:22:06,890 or program is not going to recognize that. 507 00:22:06,890 --> 00:22:09,010 So it's going to say, "Sorry, I'm not familiar with that class." 508 00:22:09,010 --> 00:22:10,009 And it's going to break. 509 00:22:10,009 --> 00:22:12,847 DAVID MALAN: So you can really see the if, else if, else idea here. 510 00:22:12,847 --> 00:22:15,430 It's just different syntax for expressing the exact same idea. 511 00:22:15,430 --> 00:22:17,690 >> MARIA: Exactly. 512 00:22:17,690 --> 00:22:19,480 Now we have our ternary operators. 513 00:22:19,480 --> 00:22:24,900 So ternary operators have three parts-- a condition, 514 00:22:24,900 --> 00:22:27,220 something to do if that condition is true, 515 00:22:27,220 --> 00:22:29,730 and then something to do if that condition is false. 516 00:22:29,730 --> 00:22:33,400 So you see, basically, the syntax of that here. 517 00:22:33,400 --> 00:22:37,792 We have the question mark, and then the-- what do we call this? 518 00:22:37,792 --> 00:22:38,750 DAVID MALAN: The colon. 519 00:22:38,750 --> 00:22:39,100 MARIA: Colon. 520 00:22:39,100 --> 00:22:39,599 Thank you. 521 00:22:39,599 --> 00:22:41,880 Sorry. 522 00:22:41,880 --> 00:22:45,800 So let's look at our example to see if we can make sense of this-- 523 00:22:45,800 --> 00:22:47,840 class_num == 50. 524 00:22:47,840 --> 00:22:52,580 So here we see our Boolean operator == that compares the variable class_num 525 00:22:52,580 --> 00:22:53,100 to 50. 526 00:22:53,100 --> 00:22:55,150 So class_num stores an integer. 527 00:22:55,150 --> 00:22:58,180 And if that integer equals 50, then we're 528 00:22:58,180 --> 00:23:02,810 going to store "David Malan" inside the string professor. 529 00:23:02,810 --> 00:23:05,912 If class number does not equal 50, "not David Malan" 530 00:23:05,912 --> 00:23:07,120 is going to be the professor. 531 00:23:07,120 --> 00:23:08,630 >> DAVID MALAN: Thank you. 532 00:23:08,630 --> 00:23:12,110 And so this seems equivalent to what, would you say, at first glance? 533 00:23:12,110 --> 00:23:17,930 534 00:23:17,930 --> 00:23:19,960 >> MARIA: To me it seems like if-else statements. 535 00:23:19,960 --> 00:23:20,709 >> DAVID MALAN: Yeah. 536 00:23:20,709 --> 00:23:24,020 And in fact, this is kind of a pretty one liner, 537 00:23:24,020 --> 00:23:27,520 so to speak, for implementing exactly the logic of if-else 538 00:23:27,520 --> 00:23:29,010 but doing everything in one step. 539 00:23:29,010 --> 00:23:29,860 >> MARIA: Exactly. 540 00:23:29,860 --> 00:23:30,360 Yeah. 541 00:23:30,360 --> 00:23:33,520 So if-else statements might take up a lot of space. 542 00:23:33,520 --> 00:23:36,283 They might be too long for something as simple as this. 543 00:23:36,283 --> 00:23:38,350 So this can be very succinct and very nice. 544 00:23:38,350 --> 00:23:40,880 545 00:23:40,880 --> 00:23:46,820 So now we're going into looking at executing something again and again, 546 00:23:46,820 --> 00:23:50,060 so checking for a condition multiple times 547 00:23:50,060 --> 00:23:53,510 and then continuing to do something while that condition is true. 548 00:23:53,510 --> 00:23:56,740 So this brings us nicely into while loops. 549 00:23:56,740 --> 00:23:59,270 >> To the left, we have our first while loop. 550 00:23:59,270 --> 00:24:03,840 So while something is true, do something again and again. 551 00:24:03,840 --> 00:24:08,850 So make sure to notice that our condition here is validated at the top. 552 00:24:08,850 --> 00:24:14,150 Whereas with our second loop-- we call that a do-while loop-- we do something, 553 00:24:14,150 --> 00:24:15,890 then we check for a condition. 554 00:24:15,890 --> 00:24:17,980 And if that condition continues to be true, 555 00:24:17,980 --> 00:24:20,150 we go back and do the thing again. 556 00:24:20,150 --> 00:24:23,360 So the main difference is where the condition is checked for. 557 00:24:23,360 --> 00:24:29,260 And the code may never be executed if the condition is not 558 00:24:29,260 --> 00:24:30,430 true with the while loop. 559 00:24:30,430 --> 00:24:33,050 Whereas with the do-while loop, we have the code 560 00:24:33,050 --> 00:24:35,380 that is always executed at least once. 561 00:24:35,380 --> 00:24:37,470 And then while the condition continues to be true, 562 00:24:37,470 --> 00:24:39,960 we can go back and iterate again inside. 563 00:24:39,960 --> 00:24:43,625 So why do you think we would use a do-while loop over a while loop? 564 00:24:43,625 --> 00:24:48,221 565 00:24:48,221 --> 00:24:48,720 Right. 566 00:24:48,720 --> 00:24:52,600 So if we want to prompt the user for some sort of input, 567 00:24:52,600 --> 00:24:55,400 if we want to ask them to enter their name, 568 00:24:55,400 --> 00:24:57,342 we want to at least ask them once. 569 00:24:57,342 --> 00:24:59,050 And if they do enter that name, we're not 570 00:24:59,050 --> 00:25:00,740 going to ask them again because we already know it. 571 00:25:00,740 --> 00:25:02,656 But if they don't enter their name, or if they 572 00:25:02,656 --> 00:25:05,787 enter something that's obviously not a name, 573 00:25:05,787 --> 00:25:08,120 we still want to continue asking them on for their name. 574 00:25:08,120 --> 00:25:10,710 >> DAVID MALAN: And in lecture we had one like this with the get positive int 575 00:25:10,710 --> 00:25:13,550 example, where there's nothing to check at first because you haven't even 576 00:25:13,550 --> 00:25:14,190 gotten an int. 577 00:25:14,190 --> 00:25:16,680 So we want to do this-- get an int from the user-- 578 00:25:16,680 --> 00:25:18,860 then check it, perhaps, again and again and again. 579 00:25:18,860 --> 00:25:21,030 >> MARIA: Exactly. 580 00:25:21,030 --> 00:25:22,060 For loops-- OK. 581 00:25:22,060 --> 00:25:25,842 For loops can allow us to do almost the exact same thing, as well. 582 00:25:25,842 --> 00:25:27,300 It's actually the exact same thing. 583 00:25:27,300 --> 00:25:30,190 So there's nothing that you can do with for loops 584 00:25:30,190 --> 00:25:32,840 that you would not be able to do with while loops. 585 00:25:32,840 --> 00:25:36,900 But for loops may seem a little bit more complicated syntactically 586 00:25:36,900 --> 00:25:39,730 because they have three parts inside what 587 00:25:39,730 --> 00:25:42,990 was before just the condition with while loops. 588 00:25:42,990 --> 00:25:48,260 So the first part that you sea, the left-most, we have "int dwarves = 0." 589 00:25:48,260 --> 00:25:51,490 So this is where we initialize our variable. 590 00:25:51,490 --> 00:25:55,210 >> Then we have a semicolon and "dwarves < 7." 591 00:25:55,210 --> 00:25:57,100 So this is where out condition actually is. 592 00:25:57,100 --> 00:26:00,170 So that is what we would have put just in the while loop-- 593 00:26:00,170 --> 00:26:01,880 "while dwarves < 7." 594 00:26:01,880 --> 00:26:05,380 Here, that goes in the middle of our for loop. 595 00:26:05,380 --> 00:26:07,190 So "dwaves < 7." 596 00:26:07,190 --> 00:26:11,350 And then our last part is "dwarves ++," which is where we update our variable. 597 00:26:11,350 --> 00:26:15,470 So the important thing to realize is that this 598 00:26:15,470 --> 00:26:22,010 is going to go through this for loop seven times and execute seven times. 599 00:26:22,010 --> 00:26:24,020 So we have seven dwarves, and they're all 600 00:26:24,020 --> 00:26:26,020 going to say, "I'm here to help you, Snow White" 601 00:26:26,020 --> 00:26:28,860 because they are ready to help Snow White. 602 00:26:28,860 --> 00:26:33,600 >> With while loops, we would have done the initialization and the updating 603 00:26:33,600 --> 00:26:37,490 not inside the condition, again, but either before or inside the while loop 604 00:26:37,490 --> 00:26:40,210 to make sure that we-- because we always need those parts. 605 00:26:40,210 --> 00:26:42,835 So to make sure that we have them, we would've still added them 606 00:26:42,835 --> 00:26:44,580 on, just not inside the parentheses. 607 00:26:44,580 --> 00:26:47,288 >> DAVID MALAN: And so it looks like-- in the lecture, for instance, 608 00:26:47,288 --> 00:26:50,250 I almost always use, like, i and n and pretty boring variables. 609 00:26:50,250 --> 00:26:53,050 It looks like you can use more cleverly named variables, too. 610 00:26:53,050 --> 00:26:56,300 >> MARIA: Yeah, it's actually very nice to use a bit more descriptive variables 611 00:26:56,300 --> 00:26:59,400 because especially if someone else is reading your code-- maybe you're grader 612 00:26:59,400 --> 00:27:00,940 or if you're collaborating with somebody-- 613 00:27:00,940 --> 00:27:03,481 you want to make sure that they understand what you're doing. 614 00:27:03,481 --> 00:27:05,750 615 00:27:05,750 --> 00:27:06,400 >> OK. 616 00:27:06,400 --> 00:27:11,435 So this is something very funky-- for loop inside a for loop. 617 00:27:11,435 --> 00:27:13,060 I don't know if we've seen this before. 618 00:27:13,060 --> 00:27:13,830 Probably not. 619 00:27:13,830 --> 00:27:17,750 But we can actually have this-- so loops instead of loops. 620 00:27:17,750 --> 00:27:25,130 So does anyone want to maybe walk me through what's going on here? 621 00:27:25,130 --> 00:27:28,240 622 00:27:28,240 --> 00:27:29,490 DAVID MALAN: I'll take a stab. 623 00:27:29,490 --> 00:27:29,970 MARIA: Cool. 624 00:27:29,970 --> 00:27:30,636 DAVID MALAN: OK. 625 00:27:30,636 --> 00:27:34,690 So, spoiler-- we want to print the stuff that's in the bottom right-hand corner 626 00:27:34,690 --> 00:27:35,190 there. 627 00:27:35,190 --> 00:27:35,450 >> MARIA: Right. 628 00:27:35,450 --> 00:27:35,710 Yeah, yeah. 629 00:27:35,710 --> 00:27:38,043 >> DAVID MALAN: So we've just put that sample output there. 630 00:27:38,043 --> 00:27:42,590 So I can infer from the topmost loop that you're 631 00:27:42,590 --> 00:27:45,680 iterating over rows the outermost loop, so to speak. 632 00:27:45,680 --> 00:27:48,107 And you're iterating over columns with the innermost loop. 633 00:27:48,107 --> 00:27:50,190 And intuitively, this should hopefully makes sense 634 00:27:50,190 --> 00:27:52,070 because by nature of every program we've seen 635 00:27:52,070 --> 00:27:55,570 before, printf, which is the function we're using, ultimately, 636 00:27:55,570 --> 00:27:58,190 has the ability to print stuff out essentially line by line. 637 00:27:58,190 --> 00:27:59,960 Like, once you've outputted a new line, there's 638 00:27:59,960 --> 00:28:02,876 no doubling back and printing something higher in the screen, at least 639 00:28:02,876 --> 00:28:04,530 not using printf like this. 640 00:28:04,530 --> 00:28:07,750 >> And so in that regard, it makes sense that the outermost loop 641 00:28:07,750 --> 00:28:10,930 should be referring to the rows because for each given row, 642 00:28:10,930 --> 00:28:13,650 you're going to want to print out XXXXX, and then 643 00:28:13,650 --> 00:28:15,520 move on to the next row, XXXXX. 644 00:28:15,520 --> 00:28:17,317 So rows come first. 645 00:28:17,317 --> 00:28:19,150 And then within each row, you print columns. 646 00:28:19,150 --> 00:28:21,160 If you tried to do it the opposite, it would probably not 647 00:28:21,160 --> 00:28:21,690 come out as you intend. 648 00:28:21,690 --> 00:28:22,190 >> MARIA: Yeah. 649 00:28:22,190 --> 00:28:24,950 We just could not go back to the previous row with printf. 650 00:28:24,950 --> 00:28:27,658 >> DAVID MALAN: And what's interesting per today's chat about scope, 651 00:28:27,658 --> 00:28:31,600 actually, is that row is an int that's declared in the topmost loop. 652 00:28:31,600 --> 00:28:34,030 But notice that it is still inside of, so to speak, 653 00:28:34,030 --> 00:28:35,982 the curly braces that immediately follow, 654 00:28:35,982 --> 00:28:38,440 even though it's not technically within those curly braces. 655 00:28:38,440 --> 00:28:41,750 So row is in scope for the entirety of the snippet of code, 656 00:28:41,750 --> 00:28:44,690 both inside of the outer for loop and inside of the inner for loop. 657 00:28:44,690 --> 00:28:48,050 But by contrast, where is column in scope? 658 00:28:48,050 --> 00:28:49,810 The variable column? 659 00:28:49,810 --> 00:28:51,310 Yeah, only in the inside loop. 660 00:28:51,310 --> 00:28:54,280 And that's OK because we don't access it outside of its curly braces. 661 00:28:54,280 --> 00:28:57,280 All we do is print out a new line at the very end there. 662 00:28:57,280 --> 00:28:58,670 So that in fact is OK. 663 00:28:58,670 --> 00:29:03,740 >> So this has the effect, it looks like, of doing three rows and four columns. 664 00:29:03,740 --> 00:29:04,370 >> MARIA: Right. 665 00:29:04,370 --> 00:29:06,270 So first we go through our first row. 666 00:29:06,270 --> 00:29:10,520 And just in our first row, we do the four columns inside the first row. 667 00:29:10,520 --> 00:29:11,980 So we print out four X's. 668 00:29:11,980 --> 00:29:15,800 And then we can exit the for loop, since we've already printed out four X's. 669 00:29:15,800 --> 00:29:17,100 And we print a new line. 670 00:29:17,100 --> 00:29:19,920 And then we go through the same process for two more rows 671 00:29:19,920 --> 00:29:21,170 to make a total of three. 672 00:29:21,170 --> 00:29:23,530 >> DAVID MALAN: And it's worth noting it's just an artifact of the font, the fact 673 00:29:23,530 --> 00:29:25,680 that the sample output seem so much taller, 674 00:29:25,680 --> 00:29:27,520 as though there's more rows than columns. 675 00:29:27,520 --> 00:29:29,824 But it's just because the X is taller than it is wide. 676 00:29:29,824 --> 00:29:32,740 That's all that's happening there, plus the white space between lines. 677 00:29:32,740 --> 00:29:33,281 >> MARIA: Right. 678 00:29:33,281 --> 00:29:34,792 Cool. 679 00:29:34,792 --> 00:29:35,750 DAVID MALAN: All right. 680 00:29:35,750 --> 00:29:39,830 So a quick look at problem set one, take any questions, and then adjourn? 681 00:29:39,830 --> 00:29:40,330 All right. 682 00:29:40,330 --> 00:29:43,164 So in problem set one, there's three primary challenges, ultimately. 683 00:29:43,164 --> 00:29:44,955 But first, you'll find that the problem set 684 00:29:44,955 --> 00:29:47,900 specification, as with many the psets this fall, is going to walk you 685 00:29:47,900 --> 00:29:50,691 through a few warm-up exercises, point you out a few resources 686 00:29:50,691 --> 00:29:52,440 that you may or may not have seen already. 687 00:29:52,440 --> 00:29:54,440 For instance, CS50 has a suite of shorts, 688 00:29:54,440 --> 00:29:56,481 which are short videos, not unlike this, but that 689 00:29:56,481 --> 00:29:58,460 are very topic specific-- maybe five minutes, 690 00:29:58,460 --> 00:30:02,360 10 minutes in length on loops or on conditions or on algorithms 691 00:30:02,360 --> 00:30:04,125 or later on more advanced topics, as well. 692 00:30:04,125 --> 00:30:06,000 And we generally embed those into the problem 693 00:30:06,000 --> 00:30:07,950 sets so that students have a resource with which 694 00:30:07,950 --> 00:30:10,840 to review material that might have already come up in lecture or section. 695 00:30:10,840 --> 00:30:13,220 But this way it's more focused and more at their fingertips. 696 00:30:13,220 --> 00:30:15,928 >> We also tend to embed in problem sets things called walkthroughs. 697 00:30:15,928 --> 00:30:20,790 So almost all of the examples I do in lecture, on stage here, we also 698 00:30:20,790 --> 00:30:24,565 have shot on camera in advance a more slower, 699 00:30:24,565 --> 00:30:28,760 a more intimate walkthrough by me on my laptop of that code, line by line 700 00:30:28,760 --> 00:30:31,427 so that in class, we'll often skim through something or someone 701 00:30:31,427 --> 00:30:33,260 will interject with an answer to a question. 702 00:30:33,260 --> 00:30:35,820 But it doesn't necessarily sink in for everyone in the audience. 703 00:30:35,820 --> 00:30:38,230 So you'll find code walkthroughs for most of the examples 704 00:30:38,230 --> 00:30:40,950 that we do here in lecture so that you can walk through it at your own pace 705 00:30:40,950 --> 00:30:43,580 and rewind or fast forward or skip altogether, if you'd like. 706 00:30:43,580 --> 00:30:45,356 >> There's often a few warm-up questions that 707 00:30:45,356 --> 00:30:47,105 ask you to sort of reinforce this material 708 00:30:47,105 --> 00:30:50,250 and make sure that you're comfy before you proceed with the rest of the pset. 709 00:30:50,250 --> 00:30:51,700 And then, of course, there's the pset itself. 710 00:30:51,700 --> 00:30:53,450 And one of the things we very deliberately 711 00:30:53,450 --> 00:30:57,202 do in CS50 is almost every uninteresting or intellectually uninteresting 712 00:30:57,202 --> 00:30:58,910 mechanical step that you might have to do 713 00:30:58,910 --> 00:31:00,519 is almost always very well documented. 714 00:31:00,519 --> 00:31:03,560 Over the course of the term, we'll start to ask more rhetorical questions 715 00:31:03,560 --> 00:31:06,187 like remember how to do this or that? 716 00:31:06,187 --> 00:31:08,520 But generally, you'll find that the problem sets get you 717 00:31:08,520 --> 00:31:10,269 through the mechanics of something so that 718 00:31:10,269 --> 00:31:14,160 the interesting, intellectual challenges are ultimately left to you the student. 719 00:31:14,160 --> 00:31:17,390 >> With that said, Zamyla, to whom we kept referring today in lecture, 720 00:31:17,390 --> 00:31:20,190 is one of our longtime staff member who also holds walkthroughs 721 00:31:20,190 --> 00:31:24,520 on specific pset problems like mario and greedy, though not water this year. 722 00:31:24,520 --> 00:31:26,540 And it's in those walkthroughs that she often 723 00:31:26,540 --> 00:31:29,450 offers some tips and tricks for how to proceed, never telling you 724 00:31:29,450 --> 00:31:31,810 exactly what to do, but rather-- like a condition, 725 00:31:31,810 --> 00:31:35,200 if you will-- giving you at least a couple of ideas so that it's up to you, 726 00:31:35,200 --> 00:31:37,830 ultimately, to decide how to approach them. 727 00:31:37,830 --> 00:31:41,200 >> MARIA: Sort of like a high-level understanding of the logic of what 728 00:31:41,200 --> 00:31:42,220 we are asking you to do. 729 00:31:42,220 --> 00:31:42,485 >> DAVID MALAN: Exactly. 730 00:31:42,485 --> 00:31:44,190 And, in fact, Zamyla's walkthroughs are meant 731 00:31:44,190 --> 00:31:46,190 to answer the frequently asked question of where 732 00:31:46,190 --> 00:31:48,610 do I began, especially when these long specifications are 733 00:31:48,610 --> 00:31:51,960 a little daunting given all the text and imagery that they have in them. 734 00:31:51,960 --> 00:31:54,890 >> So water.c, you'll find after you've completed it, 735 00:31:54,890 --> 00:31:56,920 is actually relatively straightforward. 736 00:31:56,920 --> 00:31:58,882 Absolutely are a bunch of you going to bang 737 00:31:58,882 --> 00:32:01,840 your heads against the wall trying to figure out why it's not compiling 738 00:32:01,840 --> 00:32:03,450 or why it's not running correctly. 739 00:32:03,450 --> 00:32:05,040 But once you're done with it and once you've 740 00:32:05,040 --> 00:32:07,620 spent some time wrestling with any of the bugs that you might have, 741 00:32:07,620 --> 00:32:09,490 you'll find that it's a very short program. 742 00:32:09,490 --> 00:32:11,770 It can be done in just a few lines of code, most of which 743 00:32:11,770 --> 00:32:15,030 we've seen here in lecture already, if you assemble the right building blocks. 744 00:32:15,030 --> 00:32:16,810 >> And as we suggest here, it's going to ask 745 00:32:16,810 --> 00:32:22,250 you to specify how many minutes someone is taking a shower on campus. 746 00:32:22,250 --> 00:32:24,779 We specify in the problem set the rate of flow of water 747 00:32:24,779 --> 00:32:27,320 in, like, a low-flow shower head, much like the Seinfeld clip 748 00:32:27,320 --> 00:32:29,180 we saw yesterday or the opposite thereof. 749 00:32:29,180 --> 00:32:30,971 And then you just have to do a bit of math, 750 00:32:30,971 --> 00:32:34,710 really-- arithmetic using C to tell us how many, roughly, bottles of water 751 00:32:34,710 --> 00:32:37,720 is that equivalent to if we're taking an n-minute shower. 752 00:32:37,720 --> 00:32:41,389 >> Now, in mario.c, this is going to be a slightly longer program. 753 00:32:41,389 --> 00:32:42,930 It's still not going to be this long. 754 00:32:42,930 --> 00:32:45,130 Just a few lines longer than water.c. 755 00:32:45,130 --> 00:32:48,230 But it's going to be opportunity to recreate the old school Mario 756 00:32:48,230 --> 00:32:50,670 pyramid from Super Mario Brothers or some follow-on. 757 00:32:50,670 --> 00:32:53,510 It's not going to look as prettier or as colorful as that one there. 758 00:32:53,510 --> 00:32:56,468 We're just going to use little hashtags like we have here on the screen 759 00:32:56,468 --> 00:32:58,120 using ASCII text. 760 00:32:58,120 --> 00:33:00,362 But it will approximate the same idea. 761 00:33:00,362 --> 00:33:02,570 And it's going to be an exercise that at first glance 762 00:33:02,570 --> 00:33:04,720 seems pretty simple-- just print a simple pyramid. 763 00:33:04,720 --> 00:33:07,345 But there's a couple characteristics here that are interesting. 764 00:33:07,345 --> 00:33:12,400 Notice that the rightmost edge of the pyramid actually has width two. 765 00:33:12,400 --> 00:33:14,570 So there's two equal height columns, which 766 00:33:14,570 --> 00:33:16,804 makes it require a little bit of thought to make 767 00:33:16,804 --> 00:33:19,720 sure you get that exactly right, as opposed to just a perfectly angled 768 00:33:19,720 --> 00:33:20,220 line. 769 00:33:20,220 --> 00:33:23,340 So that's a bit of a corner case but matches the actual game. 770 00:33:23,340 --> 00:33:27,610 >> And it's also non-obvious at first glance how to print the white space. 771 00:33:27,610 --> 00:33:29,430 So when I look at the sample output here-- 772 00:33:29,430 --> 00:33:32,440 and it's also in the spec-- it kind of looks like a rectangle, 773 00:33:32,440 --> 00:33:35,120 but a diagonal of the rectangle has been chopped off, 774 00:33:35,120 --> 00:33:37,320 and it's just white spaces, so to speak. 775 00:33:37,320 --> 00:33:40,100 And so a frequently asked question here is always, well, 776 00:33:40,100 --> 00:33:43,302 how do I move the hashtags over to the right? 777 00:33:43,302 --> 00:33:44,760 Or how do I print the blank spaces? 778 00:33:44,760 --> 00:33:46,860 And it's actually easier than most students think. 779 00:33:46,860 --> 00:33:47,360 Right? 780 00:33:47,360 --> 00:33:51,620 You can approximate it visually by just hitting the space bar once or twice 781 00:33:51,620 --> 00:33:52,700 or three times. 782 00:33:52,700 --> 00:33:56,260 >> And so even though with printf we almost always print out a string or an int 783 00:33:56,260 --> 00:33:59,950 or a word like "hello, world" or series of of words, 784 00:33:59,950 --> 00:34:03,560 you can also just print quote, space, unquote. 785 00:34:03,560 --> 00:34:06,320 And that will actually give you a white space there. 786 00:34:06,320 --> 00:34:08,330 So keep that in mind and don't overthink this. 787 00:34:08,330 --> 00:34:10,570 You really have to decide row by row by row, 788 00:34:10,570 --> 00:34:14,280 not unlike your example a moment ago, how many of those 789 00:34:14,280 --> 00:34:18,420 columns should be white spaces and how many of them should be hashtags. 790 00:34:18,420 --> 00:34:21,560 It will take a bit of time, but it's ultimately a logic puzzle of sorts. 791 00:34:21,560 --> 00:34:22,060 >> MARIA: Yeah. 792 00:34:22,060 --> 00:34:25,060 But the logic of going row by row is going to be very useful here. 793 00:34:25,060 --> 00:34:25,810 DAVID MALAN: Yeah. 794 00:34:25,810 --> 00:34:28,976 I think the sample code you gave-- even though it wasn't a complete program. 795 00:34:28,976 --> 00:34:34,247 You still need int and main and void and #include stdio.h, 796 00:34:34,247 --> 00:34:35,580 a lot of the stuff from lecture. 797 00:34:35,580 --> 00:34:37,629 But the building blocks seem to be there. 798 00:34:37,629 --> 00:34:39,920 And then lastly is something a little more algorithmic. 799 00:34:39,920 --> 00:34:43,210 So it turns out that any time you go into CVS or any convenience store 800 00:34:43,210 --> 00:34:47,810 and someone hands you at the cashier bills or coins as change, 801 00:34:47,810 --> 00:34:50,620 it turns out that they, humans, whether or not they know it or not, 802 00:34:50,620 --> 00:34:53,300 are probably using what's called a greedy algorithm, 803 00:34:53,300 --> 00:34:58,170 whereby if you're owed, say, $21 in change because for some reason 804 00:34:58,170 --> 00:35:01,580 you bought something very inexpensive with a very big bill at CVS, 805 00:35:01,580 --> 00:35:05,910 it would be really annoying if the cashier gave you 21 singles or, worse 806 00:35:05,910 --> 00:35:07,629 yet, a whole lot of coins. 807 00:35:07,629 --> 00:35:10,670 Rather, what a reasonable person is probably going to do is they're going 808 00:35:10,670 --> 00:35:14,224 to grab a $20- and then a $1-bill, and hand you just two bills in that case. 809 00:35:14,224 --> 00:35:17,390 MARIA: So they're seeking to minimize the change that they give back to you. 810 00:35:17,390 --> 00:35:17,850 DAVID MALAN: Exactly. 811 00:35:17,850 --> 00:35:19,540 And same deal with coins, as well. 812 00:35:19,540 --> 00:35:23,040 If you're owed, say, $0.50, hopefully you don't want 50 pennies. 813 00:35:23,040 --> 00:35:25,500 You instead want two quarters, for instance. 814 00:35:25,500 --> 00:35:27,540 Now, this does assume that the cashier has 815 00:35:27,540 --> 00:35:29,940 enough of every denomination that he or she might want to give you. 816 00:35:29,940 --> 00:35:32,148 But we do allow you to assume as much in the problem. 817 00:35:32,148 --> 00:35:35,590 And the goal, ultimately, is to implement in C code a greedy algorithm. 818 00:35:35,590 --> 00:35:37,344 So the user is allowed to type in how much 819 00:35:37,344 --> 00:35:40,510 change he or she is owed with dollars and cents, some kind of floating point 820 00:35:40,510 --> 00:35:41,590 value probably. 821 00:35:41,590 --> 00:35:45,530 And then you have to do the math and figure out algorithmically, well, 822 00:35:45,530 --> 00:35:49,020 how many coins can I give you minimally to give you 823 00:35:49,020 --> 00:35:50,270 exactly that amount of change. 824 00:35:50,270 --> 00:35:53,470 But there's going to be a few tricky parts here, right? 825 00:35:53,470 --> 00:35:56,236 Like there's the whole imprecision issue. 826 00:35:56,236 --> 00:35:56,860 MARIA: Exactly. 827 00:35:56,860 --> 00:35:59,114 So floating point values have imprecision. 828 00:35:59,114 --> 00:36:00,780 Did we talk about this in lecture today? 829 00:36:00,780 --> 00:36:02,340 >> DAVID MALAN: We did the last time in lecture. 830 00:36:02,340 --> 00:36:03,506 We talked about imprecision. 831 00:36:03,506 --> 00:36:08,840 And you don't want to cheat the user of how much change he or she is owed. 832 00:36:08,840 --> 00:36:11,290 And so the walkthrough, in the problem set specification, 833 00:36:11,290 --> 00:36:15,990 give some thought, ultimately, as to how you can mitigate those imprecision 834 00:36:15,990 --> 00:36:17,280 errors, potentially. 835 00:36:17,280 --> 00:36:21,050 It is avoidable, certainly, for the inputs we're talking about. 836 00:36:21,050 --> 00:36:23,590 And, in fact, pennies-- maybe we'll defer to Zamyla, 837 00:36:23,590 --> 00:36:25,136 I think, for the tricks there. 838 00:36:25,136 --> 00:36:27,760 So ultimately, you'll find a progression of problems this week, 839 00:36:27,760 --> 00:36:30,700 the first of which is pretty small, then medium, then a little larger. 840 00:36:30,700 --> 00:36:33,200 But all of them use the building blocks from this past week, 841 00:36:33,200 --> 00:36:35,280 from this supersection, problem set specification 842 00:36:35,280 --> 00:36:36,710 puts you at innumerable resources. 843 00:36:36,710 --> 00:36:41,000 But still, if at all struggling, especially among those less comfortable 844 00:36:41,000 --> 00:36:42,917 with no prior background, come to office hours 845 00:36:42,917 --> 00:36:44,958 on Monday and Tuesday and Wednesday and Thursday. 846 00:36:44,958 --> 00:36:46,950 Go to CS50 Discuss via the course's website, 847 00:36:46,950 --> 00:36:49,080 via which you can chat with staff and classmates. 848 00:36:49,080 --> 00:36:51,670 >> But ultimately, I think the best advice is just start early. 849 00:36:51,670 --> 00:36:54,711 It's not the kind of class that you should be starting psets on Wednesday 850 00:36:54,711 --> 00:36:56,640 night, or worse Thursday night. 851 00:36:56,640 --> 00:36:59,067 >> MARIA: My best advice is to have started by Monday. 852 00:36:59,067 --> 00:36:59,900 DAVID MALAN: Monday. 853 00:36:59,900 --> 00:37:02,490 So if you haven't started already-- no. 854 00:37:02,490 --> 00:37:04,732 But even if not Monday, then Tuesday. 855 00:37:04,732 --> 00:37:05,690 The earlier the better. 856 00:37:05,690 --> 00:37:08,605 And that's the reason the course has so many late days is 857 00:37:08,605 --> 00:37:10,535 to give you a bit of a psychological pressure 858 00:37:10,535 --> 00:37:13,684 to start earlier but still let things slip when things take longer 859 00:37:13,684 --> 00:37:14,350 than you expect. 860 00:37:14,350 --> 00:37:17,950 >> MARIA: And you want to make use of office hours as much as you can, too. 861 00:37:17,950 --> 00:37:20,291 >> DAVID MALAN: Any questions? 862 00:37:20,291 --> 00:37:20,790 All right. 863 00:37:20,790 --> 00:37:22,930 Well, why don't we adjourn here? 864 00:37:22,930 --> 00:37:23,487