1 00:00:07,275 --> 00:00:08,980 LEXI ROSS: All right, hi, everyone. 2 00:00:08,980 --> 00:00:11,930 Welcome to the first CS50 supersection. 3 00:00:11,930 --> 00:00:13,800 This will be the only week of supersections. 4 00:00:13,800 --> 00:00:17,130 After this, you'll be going to regular section with your 5 00:00:17,130 --> 00:00:18,530 assigned TF. 6 00:00:18,530 --> 00:00:20,280 But this week, we're doing it in a little bit of a different 7 00:00:20,280 --> 00:00:23,530 format, although the actual format, how it's going to go 8 00:00:23,530 --> 00:00:25,950 for the next hour and a half or so, is going to be pretty 9 00:00:25,950 --> 00:00:27,700 similar to what you'll be seeing for 10 00:00:27,700 --> 00:00:29,350 the rest of the semester. 11 00:00:29,350 --> 00:00:33,630 >> So what we're going to do is start by going over the 12 00:00:33,630 --> 00:00:35,920 section part of your problem set. 13 00:00:35,920 --> 00:00:38,940 So if you've read over the p set, which hopefully you have, 14 00:00:38,940 --> 00:00:41,120 you'll see that there's a part called "A Section of 15 00:00:41,120 --> 00:00:43,190 Questions", and these are meant to be gone 16 00:00:43,190 --> 00:00:44,180 over in your sections. 17 00:00:44,180 --> 00:00:47,510 So you have an advantage by being here in that you get to 18 00:00:47,510 --> 00:00:50,680 go over these more conceptual-based questions. 19 00:00:50,680 --> 00:00:52,780 And after we do that, we're going to go over the last two 20 00:00:52,780 --> 00:00:55,070 section questions, which are more dive 21 00:00:55,070 --> 00:00:56,330 into coding type questions. 22 00:00:56,330 --> 00:00:58,470 So we're actually going to have you guys take a few 23 00:00:58,470 --> 00:01:01,040 minutes for each question, try to solve them on your own, and 24 00:01:01,040 --> 00:01:03,590 then we're going to come together using the new CS50 25 00:01:03,590 --> 00:01:06,670 Spaces tool and go over the answers so you all have the 26 00:01:06,670 --> 00:01:09,130 correct code to review. 27 00:01:09,130 --> 00:01:10,790 >> So a couple of announcements. 28 00:01:10,790 --> 00:01:12,690 Don't forget that there's office hours every night this 29 00:01:12,690 --> 00:01:16,500 week in Annenberg from 8 to 11, so do take 30 00:01:16,500 --> 00:01:17,410 advantage of those. 31 00:01:17,410 --> 00:01:21,660 And also, don't start the p set too late into the week. 32 00:01:21,660 --> 00:01:24,640 Start tonight or tomorrow night at the latest. 33 00:01:24,640 --> 00:01:26,770 You will regret it if you wait too long to start 34 00:01:26,770 --> 00:01:27,970 and run into issues. 35 00:01:27,970 --> 00:01:31,160 So take our advice and definitely start early. 36 00:01:31,160 --> 00:01:32,860 This'll be a good way to get started with 37 00:01:32,860 --> 00:01:34,450 these section questions. 38 00:01:34,450 --> 00:01:35,810 So I'm Lexi, by the way. 39 00:01:35,810 --> 00:01:36,740 I'm a TF. 40 00:01:36,740 --> 00:01:38,455 And this is Dan, who's also a TF. 41 00:01:38,455 --> 00:01:38,940 DAN BRADLEY: Hey, guys. 42 00:01:38,940 --> 00:01:39,770 LEXI ROSS: And he's going to get 43 00:01:39,770 --> 00:01:41,370 started with the questions. 44 00:01:41,370 --> 00:01:42,410 >> DAN BRADLEY: All right. 45 00:01:42,410 --> 00:01:46,730 So I'm going to just introduce a couple little compiler 46 00:01:46,730 --> 00:01:50,350 issues that we're going to talk about and just go through 47 00:01:50,350 --> 00:01:54,550 some sort of conceptual ideas that we need to actually run 48 00:01:54,550 --> 00:01:59,120 code, either in an appliance or with our sort of fake 49 00:01:59,120 --> 00:02:01,520 appliances, Run CS50 and Spaces. 50 00:02:01,520 --> 00:02:05,550 So let's just see if anyone in the audience has any idea 51 00:02:05,550 --> 00:02:07,450 about what we're talking about. 52 00:02:07,450 --> 00:02:11,130 >> So what is a library in the context of code? 53 00:02:11,130 --> 00:02:13,680 Anyone have any idea? 54 00:02:13,680 --> 00:02:14,250 Okay. 55 00:02:14,250 --> 00:02:15,770 Sweet. 56 00:02:15,770 --> 00:02:17,876 AUDIENCE: Like a stock of functions that someone else 57 00:02:17,876 --> 00:02:19,690 already programmed that we can build up? 58 00:02:19,690 --> 00:02:20,230 DAN BRADLEY: Exactly. 59 00:02:20,230 --> 00:02:24,180 So a library is a stock of functions that somebody else 60 00:02:24,180 --> 00:02:27,100 has already programmed that we don't have to 61 00:02:27,100 --> 00:02:28,480 re-implement ourselves. 62 00:02:28,480 --> 00:02:32,140 So we have things like the standard I/O library, the 63 00:02:32,140 --> 00:02:38,720 cs50.h library, that are things that the world, the 64 00:02:38,720 --> 00:02:41,870 Internet, the staff have made that makes our 65 00:02:41,870 --> 00:02:43,670 lives easier as coders. 66 00:02:43,670 --> 00:02:46,410 So how do you print things to a screen? 67 00:02:46,410 --> 00:02:49,910 I have no idea, printf does it for me. 68 00:02:49,910 --> 00:02:52,290 It's something somebody else has already made that is 69 00:02:52,290 --> 00:02:54,640 awesome and works quite well. 70 00:02:54,640 --> 00:02:55,440 >> Okay. 71 00:02:55,440 --> 00:02:59,160 So to use libraries, on the other hand, however, you have 72 00:02:59,160 --> 00:03:02,450 to actually do some extra things with your code. 73 00:03:02,450 --> 00:03:07,400 So you have to use things like this #include. 74 00:03:07,400 --> 00:03:09,190 What does that do? 75 00:03:09,190 --> 00:03:13,010 What role does that play when you have it in a program? 76 00:03:13,010 --> 00:03:15,128 AUDIENCE: It lets you make use of any functions that are 77 00:03:15,128 --> 00:03:16,040 [INAUDIBLE] 78 00:03:16,040 --> 00:03:19,515 library by simply using the definitions that have already 79 00:03:19,515 --> 00:03:20,425 [INAUDIBLE] 80 00:03:20,425 --> 00:03:20,880 library. 81 00:03:20,880 --> 00:03:21,680 DAN BRADLEY: Okay. 82 00:03:21,680 --> 00:03:25,930 It lets you pull library functions into your code, but 83 00:03:25,930 --> 00:03:29,320 it does it in a way that isn't exactly the same as like 84 00:03:29,320 --> 00:03:32,020 -lcs50, which we'll get to in a minute. 85 00:03:32,020 --> 00:03:36,010 It just lets the compiler know that hey, there are these 86 00:03:36,010 --> 00:03:40,550 functions that are going to come up in your code. 87 00:03:40,550 --> 00:03:44,240 Be aware that if they aren't fully described in the code 88 00:03:44,240 --> 00:03:47,110 that's in front of you, there's some library function 89 00:03:47,110 --> 00:03:49,510 that is going to take care of it. 90 00:03:49,510 --> 00:03:53,010 >> So in the case of printf, which we'll go over a couple 91 00:03:53,010 --> 00:03:56,400 times today, is the standard I/O library. 92 00:03:56,400 --> 00:04:00,010 So while I don't define printf anywhere in my code ever, the 93 00:04:00,010 --> 00:04:05,340 standard I/O takes care of it for me, and I'll do #include 94 00:04:05,340 --> 00:04:13,780 stdio.h, and it will pull in these functions that I 95 00:04:13,780 --> 00:04:15,400 haven't ever defined. 96 00:04:15,400 --> 00:04:20,019 So what is the difference, then, between this #include 97 00:04:20,019 --> 00:04:22,320 and -lcs50? 98 00:04:22,320 --> 00:04:25,890 What does -lcs50 do when I pass it to the 99 00:04:25,890 --> 00:04:29,430 compiler, like Clang? 100 00:04:29,430 --> 00:04:32,670 Anyone have any idea? 101 00:04:32,670 --> 00:04:34,631 Okay. 102 00:04:34,631 --> 00:04:35,605 Yeah? 103 00:04:35,605 --> 00:04:39,020 AUDIENCE: It lets you access the CS50 library? 104 00:04:39,020 --> 00:04:42,120 DAN BRADLEY: It lets you access the CS50 library in 105 00:04:42,120 --> 00:04:46,610 that it tells the compiler, Clang, that hey, this is where 106 00:04:46,610 --> 00:04:50,930 you actually go to get these functions. 107 00:04:50,930 --> 00:04:55,010 So the #include says there are 108 00:04:55,010 --> 00:04:58,510 these functions somewhere. 109 00:04:58,510 --> 00:05:02,680 The -lcs50 says, go to this spot in your computer-- 110 00:05:02,680 --> 00:05:05,770 that's the cs50 library-- 111 00:05:05,770 --> 00:05:09,990 and pull up the actual code for these functions. 112 00:05:13,110 --> 00:05:14,370 Does that make sense, everyone? 113 00:05:14,370 --> 00:05:17,930 >> So these are all things that are to help your compiler 114 00:05:17,930 --> 00:05:22,490 along with making the code actually run. 115 00:05:22,490 --> 00:05:23,350 Quick question. 116 00:05:23,350 --> 00:05:24,870 Do people know what a compiler is? 117 00:05:28,220 --> 00:05:29,700 Good sense? 118 00:05:29,700 --> 00:05:33,160 It's basically taking the written code that you have, 119 00:05:33,160 --> 00:05:35,380 converting it to binary, converting into machine code, 120 00:05:35,380 --> 00:05:39,370 so your computer can actually understand it. 121 00:05:39,370 --> 00:05:43,330 And without further ado, we're going to go into the more 122 00:05:43,330 --> 00:05:46,640 actual coding exercises that you guys will have some fun 123 00:05:46,640 --> 00:05:49,850 sinking your teeth into, and Lexi will go over that. 124 00:05:49,850 --> 00:05:51,110 >> LEXI ROSS: Okay. 125 00:05:51,110 --> 00:05:51,455 Awesome. 126 00:05:51,455 --> 00:05:55,470 So we're going to go over two questions here. 127 00:05:55,470 --> 00:06:01,170 So the first one is actually, you can see, it's a little bit 128 00:06:01,170 --> 00:06:03,240 small right here, but essentially it's asking you to 129 00:06:03,240 --> 00:06:04,930 write a program to take some user input. 130 00:06:04,930 --> 00:06:09,058 I'm not sure why there's a black square there. 131 00:06:09,058 --> 00:06:10,000 It's mysterious. 132 00:06:10,000 --> 00:06:12,180 It should be fine, though. 133 00:06:12,180 --> 00:06:16,000 So it wants you to take a user input in Celsius, convert that 134 00:06:16,000 --> 00:06:18,270 to Fahrenheit, and then tell the user what the temperature 135 00:06:18,270 --> 00:06:19,720 would be in Fahrenheit. 136 00:06:19,720 --> 00:06:25,530 So you can see a sample input/output sequence here. 137 00:06:25,530 --> 00:06:28,770 The user inputs--after Celsius, the bold refers to 138 00:06:28,770 --> 00:06:29,300 user input. 139 00:06:29,300 --> 00:06:32,460 So the user inputs 100, and the program outputs 140 00:06:32,460 --> 00:06:34,530 Fahrenheit, 212.0. 141 00:06:34,530 --> 00:06:36,700 And an important distinction in this program is that we're 142 00:06:36,700 --> 00:06:41,770 going to only have one decimal place, so we'll see how to do 143 00:06:41,770 --> 00:06:43,020 that as well. 144 00:06:43,020 --> 00:06:45,780 And so another thing to note is that we're using a.out 145 00:06:45,780 --> 00:06:49,960 because Clang, the compiler, automatically names its binary 146 00:06:49,960 --> 00:06:53,460 files a.out unless you specify it with a certain flag to give 147 00:06:53,460 --> 00:06:55,410 it its own name, but for now we're just going to use the 148 00:06:55,410 --> 00:06:57,070 default, a.out. 149 00:06:57,070 --> 00:07:01,320 >> So let's actually take a few minutes and try to write out 150 00:07:01,320 --> 00:07:02,570 some code for this yourselves. 151 00:07:05,900 --> 00:07:07,380 So it's 4:15. 152 00:07:07,380 --> 00:07:10,470 So at 4:20, we are going to come back and--oh, right. 153 00:07:10,470 --> 00:07:12,960 Spaces. 154 00:07:12,960 --> 00:07:13,980 Cool. 155 00:07:13,980 --> 00:07:16,500 This is sort of like what we call skeleton code in that 156 00:07:16,500 --> 00:07:19,870 it's a skeleton, but you have to fill in the meat of the 157 00:07:19,870 --> 00:07:21,590 program to actually make it run. 158 00:07:21,590 --> 00:07:25,980 So you can see the #include directives up there to include 159 00:07:25,980 --> 00:07:28,040 cs50 and stdio. 160 00:07:28,040 --> 00:07:32,010 I noted in a comment the actual formula for converting 161 00:07:32,010 --> 00:07:35,310 Celsius to Fahrenheit, in case you don't remember from high 162 00:07:35,310 --> 00:07:36,740 school science or whatnot. 163 00:07:36,740 --> 00:07:38,450 And then this is just a skeleton for 164 00:07:38,450 --> 00:07:39,450 the actual main function. 165 00:07:39,450 --> 00:07:41,680 So everything you write is going to go here. 166 00:07:45,640 --> 00:07:50,490 And in order to actually get to this space-- 167 00:07:50,490 --> 00:07:56,105 so CS50 Spaces can be accessed using this URL, which Dan is 168 00:07:56,105 --> 00:08:02,505 going to write on the board, and I'll dictate as well. 169 00:08:02,505 --> 00:08:12,093 DAN BRADLEY: cs50.net/spaces-- 170 00:08:12,093 --> 00:08:12,580 LEXI ROSS: /qWLc-- 171 00:08:12,580 --> 00:08:13,830 I'm sorry, y. 172 00:08:18,806 --> 00:08:20,790 DAN BRADLEY: Y?. 173 00:08:20,790 --> 00:08:21,290 LEXI ROSS: No, no, no, no. 174 00:08:21,290 --> 00:08:22,540 That's fine. 175 00:08:26,088 --> 00:08:27,338 qYLcYWfg. 176 00:08:38,590 --> 00:08:41,150 So I should see everyone's names kind of popping up here. 177 00:08:41,150 --> 00:08:41,350 >> Awesome. 178 00:08:41,350 --> 00:08:42,209 This looks great. 179 00:08:42,209 --> 00:08:44,700 This means people are joining the space. 180 00:08:44,700 --> 00:08:46,130 So if you haven't gotten a chance to play around in 181 00:08:46,130 --> 00:08:47,990 Spaces yet, this is actually what you're going to be using 182 00:08:47,990 --> 00:08:50,750 with your TFs in section all semester. 183 00:08:50,750 --> 00:08:52,810 How it works is kind of everybody writes their own 184 00:08:52,810 --> 00:08:56,510 code, and nobody can see it until you press the Save 185 00:08:56,510 --> 00:08:59,284 button, which I'm pointing to right now. 186 00:08:59,284 --> 00:09:03,140 So I'm going to press Save, and if you all go to my name, 187 00:09:03,140 --> 00:09:06,670 which you should be able to see it in People in this 188 00:09:06,670 --> 00:09:09,510 Space, which is Lexis Beryl Ross. 189 00:09:09,510 --> 00:09:12,790 So how it's going to look is you click on my name, and 190 00:09:12,790 --> 00:09:15,170 you're going to see Revision 5, which 191 00:09:15,170 --> 00:09:16,070 is the latest revision. 192 00:09:16,070 --> 00:09:18,930 Copy only Revision 5 into your space by clicking on it. 193 00:09:22,570 --> 00:09:23,420 >> All right. 194 00:09:23,420 --> 00:09:27,310 So let's go through this problem together, and I'm 195 00:09:27,310 --> 00:09:29,645 actually going to write the code up here on the screen. 196 00:09:29,645 --> 00:09:31,420 You can zoom in a little bit. 197 00:09:31,420 --> 00:09:34,300 And then once I'm done, you can copy my latest revision 198 00:09:34,300 --> 00:09:37,930 into your space, if you want to see the final solution. 199 00:09:37,930 --> 00:09:41,840 Now, Spaces has the capability for me to go in and see any of 200 00:09:41,840 --> 00:09:43,910 the code that you guys have written, but just because it's 201 00:09:43,910 --> 00:09:46,470 such a large section and we don't really have the same 202 00:09:46,470 --> 00:09:50,150 kind of discussion capabilities as a smaller 203 00:09:50,150 --> 00:09:51,890 section, we're just going to go through and I'm just going 204 00:09:51,890 --> 00:09:53,140 to write the code. 205 00:09:53,140 --> 00:09:55,250 But definitely stop me with questions at any point. 206 00:09:55,250 --> 00:09:57,290 We're going to go through it kind of step by step to make 207 00:09:57,290 --> 00:10:00,032 sure we understand each part of what's going on here. 208 00:10:03,800 --> 00:10:06,710 Can everyone see this text here? 209 00:10:06,710 --> 00:10:07,960 Does anyone want it bigger? 210 00:10:10,240 --> 00:10:12,860 >> So what's the first thing we're going to do here? 211 00:10:12,860 --> 00:10:14,840 Well essentially, since we want to get input from the 212 00:10:14,840 --> 00:10:17,460 user and we want it to be kind of a good experience for the 213 00:10:17,460 --> 00:10:19,750 user, we actually want to tell them what we're looking for. 214 00:10:19,750 --> 00:10:23,120 So let's print out exactly what we want form the user. 215 00:10:23,120 --> 00:10:26,540 So just don't bother raising your hand, just rattle it off. 216 00:10:26,540 --> 00:10:29,210 What is the command to print something to the screen? 217 00:10:29,210 --> 00:10:30,100 AUDIENCE: printf. 218 00:10:30,100 --> 00:10:31,247 LEXI ROSS: Awesome. 219 00:10:31,247 --> 00:10:33,393 Okay, so printf. 220 00:10:33,393 --> 00:10:34,339 >> AUDIENCE: Hey, Lexi? 221 00:10:34,339 --> 00:10:35,589 [INAUDIBLE]? 222 00:10:39,080 --> 00:10:40,130 LEXI ROSS: Sure. 223 00:10:40,130 --> 00:10:43,580 So printf, as we've seen frequently in lecture, is the 224 00:10:43,580 --> 00:10:47,230 command to print something to the standard output stream. 225 00:10:47,230 --> 00:10:52,370 So we want to ask the user to input a 226 00:10:52,370 --> 00:10:53,620 temperature in Celsius. 227 00:10:58,180 --> 00:11:01,180 And now, we actually want to take the input that they give 228 00:11:01,180 --> 00:11:03,710 us and store it into a variable. 229 00:11:03,710 --> 00:11:06,415 So we've learned a little bit about types. 230 00:11:06,415 --> 00:11:12,090 Can someone remember what type we would want for a variable 231 00:11:12,090 --> 00:11:13,170 with a decimal place in it? 232 00:11:13,170 --> 00:11:13,910 AUDIENCE: Float. 233 00:11:13,910 --> 00:11:14,180 LEXI ROSS: Float. 234 00:11:14,180 --> 00:11:15,480 Yeah, awesome. 235 00:11:15,480 --> 00:11:16,510 It's a float. 236 00:11:16,510 --> 00:11:17,760 So. 237 00:11:21,860 --> 00:11:24,953 And how are we going to--big hint--get the 238 00:11:24,953 --> 00:11:25,430 float from the user? 239 00:11:25,430 --> 00:11:25,923 AUDIENCE: GetFloat. 240 00:11:25,923 --> 00:11:27,173 LEXI ROSS: Yeah. 241 00:11:33,320 --> 00:11:34,730 Cool. 242 00:11:34,730 --> 00:11:38,970 And the user could potentially be mean and try to give us a 243 00:11:38,970 --> 00:11:41,080 negative number, but let's not worry about that for this 244 00:11:41,080 --> 00:11:41,710 problem now. 245 00:11:41,710 --> 00:11:43,390 You're going to worry about that a little bit more on your 246 00:11:43,390 --> 00:11:45,570 homework on the problem set. 247 00:11:45,570 --> 00:11:48,970 >> So let's assume they give us a good number, and now we 248 00:11:48,970 --> 00:11:51,590 actually have to convert that float from Celsius to 249 00:11:51,590 --> 00:11:52,400 Fahrenheit. 250 00:11:52,400 --> 00:11:56,730 So let's consult our formula up here, and let's create a 251 00:11:56,730 --> 00:12:00,290 new variable and call it Fahrenheit. 252 00:12:00,290 --> 00:12:01,130 I studied the spelling. 253 00:12:01,130 --> 00:12:04,640 It's kind of crazy, I know. 254 00:12:04,640 --> 00:12:08,880 And let's count on the order of operations to kind of do 255 00:12:08,880 --> 00:12:14,340 its job here, but we'll use parentheses just to be 256 00:12:14,340 --> 00:12:17,762 completely verbose and clear. 257 00:12:17,762 --> 00:12:24,453 So Celsius times 9 divided by 5. 258 00:12:24,453 --> 00:12:26,740 I'm going to zoom out just a little bit so this can fit on 259 00:12:26,740 --> 00:12:29,390 one line, but I'll zoom in again for those who can't see, 260 00:12:29,390 --> 00:12:30,640 farther in the back. 261 00:12:38,150 --> 00:12:40,060 Sorry about that. 262 00:12:40,060 --> 00:12:42,680 Strange cursor issues. 263 00:12:42,680 --> 00:12:51,688 Plus 32. 264 00:12:51,688 --> 00:12:54,780 Okay, does anyone have any questions about how I put in 265 00:12:54,780 --> 00:12:56,460 the formula, or any issues with that? 266 00:12:56,460 --> 00:12:56,700 Yeah. 267 00:12:56,700 --> 00:12:59,580 >> AUDIENCE: Do we need to put printf before the float 268 00:12:59,580 --> 00:13:01,980 celsius, float fahrenheit script lines? 269 00:13:01,980 --> 00:13:05,910 Does it matter which order they're in? 270 00:13:05,910 --> 00:13:08,580 LEXI ROSS: So the question was, do we need to printf 271 00:13:08,580 --> 00:13:11,820 before we actually ask for the float? 272 00:13:11,820 --> 00:13:13,070 The answer is that we do. 273 00:13:13,070 --> 00:13:16,830 That way, that the cursor that the user gets to input their 274 00:13:16,830 --> 00:13:20,310 float will occur after the printed c, right? 275 00:13:20,310 --> 00:13:23,480 And it makes sense if you think about logically, like 276 00:13:23,480 --> 00:13:26,660 you're asking the user a question, and then you're 277 00:13:26,660 --> 00:13:27,800 receiving an answer. 278 00:13:27,800 --> 00:13:30,320 So it prints c, and then there's a cursor, then the 279 00:13:30,320 --> 00:13:32,470 user can type in the value, as we'll see 280 00:13:32,470 --> 00:13:34,200 later on when we demo. 281 00:13:34,200 --> 00:13:34,874 Yes? 282 00:13:34,874 --> 00:13:37,660 >> AUDIENCE: Why do we use a float and not a double? 283 00:13:37,660 --> 00:13:40,030 LEXI ROSS: So a double, essentially, as the name 284 00:13:40,030 --> 00:13:42,180 implies, gives us double the amount of 285 00:13:42,180 --> 00:13:44,040 precision as a float. 286 00:13:44,040 --> 00:13:48,490 But typically in C, in this course, for a number of 287 00:13:48,490 --> 00:13:50,340 reasons, we're going to use floats. 288 00:13:50,340 --> 00:13:53,330 And this is really all the precision we need just for 289 00:13:53,330 --> 00:13:56,200 kind of basic temperature conversions. 290 00:13:56,200 --> 00:13:59,800 Typically, doubles are more used in the Java language, 291 00:13:59,800 --> 00:14:05,700 floats are more common in C. All right, awesome. 292 00:14:05,700 --> 00:14:06,470 >> DAN BRADLEY: One more. 293 00:14:06,470 --> 00:14:07,530 LEXI ROSS: One more. 294 00:14:07,530 --> 00:14:08,708 Yes? 295 00:14:08,708 --> 00:14:10,684 AUDIENCE: Where do you put the Space Bar? 296 00:14:10,684 --> 00:14:13,648 Like, [INAUDIBLE] the Space Bar [INAUDIBLE]? 297 00:14:13,648 --> 00:14:15,140 Does it matter? 298 00:14:15,140 --> 00:14:15,560 LEXI ROSS: Oh, no. 299 00:14:15,560 --> 00:14:17,040 So it doesn't matter. 300 00:14:17,040 --> 00:14:19,910 I actually should have put another space here. 301 00:14:19,910 --> 00:14:21,990 But what does matter is that you're consistent 302 00:14:21,990 --> 00:14:22,960 throughout your code. 303 00:14:22,960 --> 00:14:25,510 So if you decide to use one style of spacing, make sure to 304 00:14:25,510 --> 00:14:27,530 keep it throughout your whole code, or you may get style 305 00:14:27,530 --> 00:14:28,610 points deducted. 306 00:14:28,610 --> 00:14:29,822 So consistency is key. 307 00:14:29,822 --> 00:14:31,590 DAN BRADLEY: I think he was talking about that space. 308 00:14:31,590 --> 00:14:33,190 LEXI ROSS: Oh, you're talking about that space? 309 00:14:33,190 --> 00:14:34,920 By the actual C? 310 00:14:34,920 --> 00:14:35,335 AUDIENCE: Yeah. 311 00:14:35,335 --> 00:14:37,910 LEXI ROSS: Oh, so that's just for looks, right? 312 00:14:37,910 --> 00:14:41,280 So that way when we run the code, if we didn't have this 313 00:14:41,280 --> 00:14:43,730 space here, if it looked like this, the user would start 314 00:14:43,730 --> 00:14:45,690 typing in the number and it would be squashed up right 315 00:14:45,690 --> 00:14:48,800 against the colon after the C, and it would look a little bit 316 00:14:48,800 --> 00:14:50,770 better if there was a space. 317 00:14:50,770 --> 00:14:52,470 And you can see that a little bit better once we actually 318 00:14:52,470 --> 00:14:53,970 run this code, when we get it finished. 319 00:14:57,060 --> 00:14:58,200 Awesome. 320 00:14:58,200 --> 00:14:59,330 Yeah, one more question. 321 00:14:59,330 --> 00:15:01,658 >> AUDIENCE: Do I have to put a decimal point after one of the 322 00:15:01,658 --> 00:15:04,000 [INAUDIBLE]? 323 00:15:04,000 --> 00:15:06,770 LEXI ROSS: So you don't, because of the order of 324 00:15:06,770 --> 00:15:07,500 operations. 325 00:15:07,500 --> 00:15:11,020 So because Fahrenheit is already a float, as is 326 00:15:11,020 --> 00:15:14,050 Celsius, you don't need to explicitly cast any of these 327 00:15:14,050 --> 00:15:15,990 other numbers as floats, although in other 328 00:15:15,990 --> 00:15:18,180 circumstances, when you're dealing with ints, you may 329 00:15:18,180 --> 00:15:21,280 have to put a point to explicitly cast it as a float. 330 00:15:24,160 --> 00:15:24,595 Yeah? 331 00:15:24,595 --> 00:15:26,629 AUDIENCE: I was just wondering if capitalization matters in 332 00:15:26,629 --> 00:15:29,150 these commands like GetFloat? 333 00:15:29,150 --> 00:15:30,160 LEXI ROSS: Yeah, good question. 334 00:15:30,160 --> 00:15:33,530 So the question was, does capitalization matter in 335 00:15:33,530 --> 00:15:34,800 commands like GetFloat? 336 00:15:34,800 --> 00:15:36,650 The answer is yes, it does. 337 00:15:36,650 --> 00:15:39,720 So GetFloat is, as Dan was describing earlier, a library 338 00:15:39,720 --> 00:15:44,000 function in the CS50 library, and it was written as capital 339 00:15:44,000 --> 00:15:47,360 G, capital F. So if you don't write it with those capitals 340 00:15:47,360 --> 00:15:50,150 there, the compiler is not going to understand what 341 00:15:50,150 --> 00:15:52,750 function you're referring to. 342 00:15:52,750 --> 00:15:53,130 Okay. 343 00:15:53,130 --> 00:15:53,815 Yeah? 344 00:15:53,815 --> 00:15:55,446 >> AUDIENCE: Is the argument for main always void? 345 00:15:55,446 --> 00:15:57,920 LEXI ROSS: Yeah, that's another great question. 346 00:15:57,920 --> 00:16:01,850 So the code that you copied from my space mistakenly had 347 00:16:01,850 --> 00:16:02,500 some more arguments. 348 00:16:02,500 --> 00:16:06,530 So the question was, is the argument to the main function 349 00:16:06,530 --> 00:16:10,620 always void, which essentially means nothing? 350 00:16:10,620 --> 00:16:11,890 The answer is no. 351 00:16:11,890 --> 00:16:13,920 You may notice that the code you copied from my earlier 352 00:16:13,920 --> 00:16:17,700 revision had what may look like nothing that really makes 353 00:16:17,700 --> 00:16:21,060 sense right now, some char*, argv, argc. 354 00:16:21,060 --> 00:16:23,630 Don't worry about that for now, but you'll find out later 355 00:16:23,630 --> 00:16:25,060 on they do make a difference. 356 00:16:25,060 --> 00:16:27,480 But if you're not using that sort of added functionality 357 00:16:27,480 --> 00:16:28,940 which you'll learn about later, void 358 00:16:28,940 --> 00:16:32,085 is completely fine. 359 00:16:32,085 --> 00:16:32,530 >> All right. 360 00:16:32,530 --> 00:16:34,690 And we have one more step, which is that we want to 361 00:16:34,690 --> 00:16:38,840 actually show the user what the converted temperature is. 362 00:16:38,840 --> 00:16:46,730 So again, we're going to use printf, and let's say f, just 363 00:16:46,730 --> 00:16:48,000 for consistency's sake. 364 00:16:48,000 --> 00:16:50,780 And now we're going to do something a little bit tricky. 365 00:16:50,780 --> 00:16:57,190 So we're going to use a flag, or a special kind of notation, 366 00:16:57,190 --> 00:17:02,510 to tell printf to only print one decimal place. 367 00:17:02,510 --> 00:17:05,240 And does anyone happen to know what that's going to look 368 00:17:05,240 --> 00:17:06,810 like, before I type it out? 369 00:17:06,810 --> 00:17:07,089 Yeah? 370 00:17:07,089 --> 00:17:08,339 AUDIENCE: 0.2%f? 371 00:17:10,369 --> 00:17:10,880 LEXI ROSS: Yeah, yeah. 372 00:17:10,880 --> 00:17:12,960 So actually, it's really close. 373 00:17:12,960 --> 00:17:23,520 0.01f for .01%f, and then we have to remember to include 374 00:17:23,520 --> 00:17:26,950 the other argument to printf, which is the variable itself, 375 00:17:26,950 --> 00:17:28,685 which is n. 376 00:17:33,690 --> 00:17:34,580 >> One last step. 377 00:17:34,580 --> 00:17:36,880 Can someone remember what that is in our main function? 378 00:17:36,880 --> 00:17:37,265 Yeah, up there. 379 00:17:37,265 --> 00:17:38,320 AUDIENCE: Return? 380 00:17:38,320 --> 00:17:38,840 LEXI ROSS: Exactly. 381 00:17:38,840 --> 00:17:42,030 So since this is main returns in int, we want to 382 00:17:42,030 --> 00:17:43,926 also return in int. 383 00:17:43,926 --> 00:17:48,280 DAN BRADLEY: Percent needs to be on the other side. 384 00:17:48,280 --> 00:17:48,540 LEXI ROSS: Okay. 385 00:17:48,540 --> 00:17:49,290 Sorry, guys. 386 00:17:49,290 --> 00:17:50,240 That was a mistake. 387 00:17:50,240 --> 00:17:52,150 So the percent comes before 0.01. 388 00:17:52,150 --> 00:17:54,450 So it's %.01f. 389 00:17:54,450 --> 00:17:55,970 Those are kind of hard to remember. 390 00:17:55,970 --> 00:17:57,470 Many people, like me, just look them up 391 00:17:57,470 --> 00:17:58,710 whenever we use them. 392 00:17:58,710 --> 00:17:59,340 Yeah, up there. 393 00:17:59,340 --> 00:18:02,525 AUDIENCE: Why is it 0.01 and not 0.2 or 0.1? 394 00:18:05,710 --> 00:18:07,530 LEXI ROSS: I don't know, actually. 395 00:18:07,530 --> 00:18:08,130 Do you know? 396 00:18:08,130 --> 00:18:14,050 DAN BRADLEY: The reason it's 0.01 as opposed to 0.02, or 397 00:18:14,050 --> 00:18:19,380 0.2, or 0.1, is fairly arbitrary to the way that the 398 00:18:19,380 --> 00:18:21,230 programmer originally designed it. 399 00:18:21,230 --> 00:18:22,200 AUDIENCE: That's not what I meant. 400 00:18:22,200 --> 00:18:24,140 I'm not asking the reason. 401 00:18:24,140 --> 00:18:25,378 [INAUDIBLE] reason why. 402 00:18:25,378 --> 00:18:28,800 I have it written as 0.2, and it delivers two-- 403 00:18:28,800 --> 00:18:29,270 DAN BRADLEY: Yeah. 404 00:18:29,270 --> 00:18:30,410 So we were only wanting one. 405 00:18:30,410 --> 00:18:31,250 AUDIENCE: Oh, Okay. 406 00:18:31,250 --> 00:18:33,270 So you could 0.01 then. 407 00:18:33,270 --> 00:18:35,340 DAN BRADLEY: 0.1 would probably also work. 408 00:18:35,340 --> 00:18:37,430 0.01 works as well. 409 00:18:37,430 --> 00:18:39,288 LEXI ROSS: Oh, the 0 might be to show zero, if 410 00:18:39,288 --> 00:18:40,870 it's less than one. 411 00:18:40,870 --> 00:18:43,080 DAN BRADLEY: Yeah, I think that is right. 412 00:18:43,080 --> 00:18:49,220 So the 0.01 would show a zero if it doesn't have anything 413 00:18:49,220 --> 00:18:51,670 that would go there naturally. 414 00:18:51,670 --> 00:18:55,860 So if you were, say, putting in 100 for Celsius, it would 415 00:18:55,860 --> 00:19:00,230 come out to 212 normally in Fahrenheit, like 212 exactly. 416 00:19:00,230 --> 00:19:05,810 If you have the 0.01, it will show 212.0, as 417 00:19:05,810 --> 00:19:07,005 opposed to just 212. 418 00:19:07,005 --> 00:19:07,330 LEXI ROSS: That's right. 419 00:19:07,330 --> 00:19:13,000 So reading it basically tells printf, print a decimal point, 420 00:19:13,000 --> 00:19:14,760 and print a 0 if necessary. 421 00:19:14,760 --> 00:19:16,780 If not, print the first decimal position. 422 00:19:16,780 --> 00:19:18,270 DAN BRADLEY: And we'll explore some of this when we're 423 00:19:18,270 --> 00:19:19,270 actually running the code. 424 00:19:19,270 --> 00:19:20,150 LEXI ROSS: Yeah. 425 00:19:20,150 --> 00:19:21,030 AUDIENCE: So I put 0.1. 426 00:19:21,030 --> 00:19:21,910 It still prints its zero. 427 00:19:21,910 --> 00:19:23,460 DAN BRADLEY: It still prints its zero? 428 00:19:23,460 --> 00:19:23,930 AUDIENCE: Yeah. 429 00:19:23,930 --> 00:19:25,211 DAN BRADLEY: Hm, okay. 430 00:19:25,211 --> 00:19:27,666 LEXI ROSS: Cool. 431 00:19:27,666 --> 00:19:29,140 >> All right. 432 00:19:29,140 --> 00:19:33,310 More questions on this code before I compile and run? 433 00:19:33,310 --> 00:19:35,380 Does anyone see any bugs here, first of all? 434 00:19:35,380 --> 00:19:39,420 Anyone see something that should be here? 435 00:19:39,420 --> 00:19:41,920 If this were your problem set, what would be here that is not 436 00:19:41,920 --> 00:19:44,490 there right now? 437 00:19:44,490 --> 00:19:45,710 Yeah, great. 438 00:19:45,710 --> 00:19:48,200 So I didn't include comments because I'm delivering them 439 00:19:48,200 --> 00:19:50,570 orally, but if I were writing this in a problem set, I would 440 00:19:50,570 --> 00:19:54,290 definitely want to put comments to indicate what I'm 441 00:19:54,290 --> 00:19:55,820 doing, essentially, in the program. 442 00:19:55,820 --> 00:19:57,870 And you'll kind of get a feel for it, as you move through 443 00:19:57,870 --> 00:19:59,680 the semester, the right amount of commenting. 444 00:19:59,680 --> 00:20:01,520 You don't want to be too verbose and put lines and 445 00:20:01,520 --> 00:20:03,710 lines of comments for each of your lines of code, but you 446 00:20:03,710 --> 00:20:06,720 also don't want to be not verbose enough and leave the 447 00:20:06,720 --> 00:20:09,440 reader to try to figure out what the heck 448 00:20:09,440 --> 00:20:11,595 your code is doing. 449 00:20:11,595 --> 00:20:12,770 >> Okay, cool. 450 00:20:12,770 --> 00:20:14,020 Let's try running this code. 451 00:20:18,290 --> 00:20:21,650 Okay, I'm going to zoom out a little bit. 452 00:20:21,650 --> 00:20:22,020 Cool. 453 00:20:22,020 --> 00:20:24,560 So you'll notice there's a Play button here. 454 00:20:24,560 --> 00:20:25,994 Let's click Play. 455 00:20:34,915 --> 00:20:39,190 So you'll notice here, just as in the spec on the problem 456 00:20:39,190 --> 00:20:43,300 set, there is the ./a.out. 457 00:20:43,300 --> 00:20:46,510 a.out is the name of the binary executable, which is a 458 00:20:46,510 --> 00:20:50,590 long word for just a file consisting of binary code that 459 00:20:50,590 --> 00:20:51,710 your computer is actually running. 460 00:20:51,710 --> 00:20:54,390 So as we expected, we have C here. 461 00:20:54,390 --> 00:20:56,007 So call out a number. 462 00:20:56,007 --> 00:20:58,630 AUDIENCE: 23. 463 00:20:58,630 --> 00:21:05,090 LEXI ROSS: 23. 464 00:21:05,090 --> 00:21:06,360 DAN BRADLEY: Your program took too long to run. 465 00:21:06,360 --> 00:21:07,310 Just run it again. 466 00:21:07,310 --> 00:21:07,700 LEXI ROSS: Oh, my bad. 467 00:21:07,700 --> 00:21:09,450 Okay. 468 00:21:09,450 --> 00:21:10,770 Spaces is picky sometimes. 469 00:21:10,770 --> 00:21:13,060 It wants you to think on your feet. 470 00:21:13,060 --> 00:21:15,060 Okay, 23. 471 00:21:15,060 --> 00:21:16,256 Awesome. 472 00:21:16,256 --> 00:21:18,920 Does everyone believe this? 473 00:21:18,920 --> 00:21:20,600 Cool, because it works. 474 00:21:20,600 --> 00:21:22,670 >> Okay. 475 00:21:22,670 --> 00:21:24,280 Let's try a couple more. 476 00:21:24,280 --> 00:21:25,910 Let's try an edge case here. 477 00:21:25,910 --> 00:21:28,900 In computer science, when we talk about edge cases, we mean 478 00:21:28,900 --> 00:21:33,640 cases for your program to run that are sort of on the very 479 00:21:33,640 --> 00:21:38,430 edges of reasonable inputs or that test certain corners of 480 00:21:38,430 --> 00:21:41,680 the logic in your program, and this makes more sense when we 481 00:21:41,680 --> 00:21:43,080 do more complicated programs. 482 00:21:43,080 --> 00:21:45,440 Let's try 0. 483 00:21:45,440 --> 00:21:45,950 Awesome. 484 00:21:45,950 --> 00:21:48,460 We all know this. 485 00:21:48,460 --> 00:21:48,745 Cool. 486 00:21:48,745 --> 00:21:50,520 Any more questions on this before I move 487 00:21:50,520 --> 00:21:51,930 on to the next program? 488 00:21:55,560 --> 00:21:57,160 All right, awesome. 489 00:21:57,160 --> 00:22:00,066 So now, we're going to-- 490 00:22:00,066 --> 00:22:01,180 do you want to just use my computer? 491 00:22:01,180 --> 00:22:01,940 DAN BRADLEY: Yeah, that's easy enough. 492 00:22:01,940 --> 00:22:06,660 >> All right, so going to go and talk about making percentages. 493 00:22:06,660 --> 00:22:11,440 So we're essentially, in this next little exercise, going to 494 00:22:11,440 --> 00:22:16,020 take two integer inputs from the user, hoping for a 495 00:22:16,020 --> 00:22:21,250 non-negative numerator and a positive denominator, and 496 00:22:21,250 --> 00:22:24,410 basically dividing them and outputting what comes out as a 497 00:22:24,410 --> 00:22:25,820 percentage. 498 00:22:25,820 --> 00:22:33,430 So we want it to only accept non-negative numerators and 499 00:22:33,430 --> 00:22:37,930 accept only positive denominators, and keep asking 500 00:22:37,930 --> 00:22:43,286 for new ones if it hasn't gotten what it wants. 501 00:22:43,286 --> 00:22:43,710 All right? 502 00:22:43,710 --> 00:22:44,960 So give that a go. 503 00:22:48,015 --> 00:22:49,390 LEXI ROSS: Oh yeah, do you want to do the-- 504 00:22:49,390 --> 00:22:51,310 oh, maybe people should copy my code. 505 00:22:51,310 --> 00:22:51,790 DAN BRADLEY: Yeah. 506 00:22:51,790 --> 00:22:52,270 LEXI ROSS: [INAUDIBLE] 507 00:22:52,270 --> 00:22:54,994 Spaces. 508 00:22:54,994 --> 00:22:55,986 >> AUDIENCE: I have a question. 509 00:22:55,986 --> 00:22:58,466 If I'm using Spaces and I want to save [INAUDIBLE] the 510 00:22:58,466 --> 00:23:04,440 Fahrenheit conversion, do I save it and then make a new-- 511 00:23:04,440 --> 00:23:06,880 LEXI ROSS: We could just do a new Space, or you may just 512 00:23:06,880 --> 00:23:09,600 want to copy and paste it to a text file or 513 00:23:09,600 --> 00:23:11,040 something so you have it. 514 00:23:11,040 --> 00:23:12,400 DAN BRADLEY: I'll just change my name. 515 00:23:12,400 --> 00:23:15,430 LEXI ROSS: So I just saved the latest revision of 516 00:23:15,430 --> 00:23:20,700 tofahrenheit.c, so if you go into AAAA Lexis Beryl 517 00:23:20,700 --> 00:23:23,270 Ross, you can go in and copy that latest code, and then 518 00:23:23,270 --> 00:23:26,360 start on this new one, which I think Dan is going to provide 519 00:23:26,360 --> 00:23:30,540 some skeleton code for as well. 520 00:23:30,540 --> 00:23:33,430 Okay. 521 00:23:33,430 --> 00:23:36,590 DAN BRADLEY: The latest revision I have on mine, so 522 00:23:36,590 --> 00:23:40,080 it's at the very top this time, is Revision 4, and 523 00:23:40,080 --> 00:23:42,050 it'll have what you need to get started. 524 00:23:47,970 --> 00:23:49,370 LEXI ROSS: All right, and I'll copy that into mine as well. 525 00:23:49,370 --> 00:23:50,520 >> DAN BRADLEY: All right. 526 00:23:50,520 --> 00:23:53,840 I think we're going to just start going over this, if 527 00:23:53,840 --> 00:23:57,470 anyone is interested. 528 00:23:57,470 --> 00:23:59,710 LEXI ROSS: We're all interested. 529 00:23:59,710 --> 00:24:02,370 DAN BRADLEY: We are going to start out just by declaring 530 00:24:02,370 --> 00:24:08,290 some variables. So we have a numerator, and for the sake of 531 00:24:08,290 --> 00:24:13,770 now, I'm going to just start it with the value -1, 532 00:24:13,770 --> 00:24:24,070 and then I'll set up a do-while loop. While-- 533 00:24:24,070 --> 00:24:28,270 so when do I want it to loop? 534 00:24:28,270 --> 00:24:30,250 When do I want it to keep asking-- 535 00:24:30,250 --> 00:24:31,761 yeah? 536 00:24:31,761 --> 00:24:34,960 AUDIENCE: When it's less than or equal to zero. 537 00:24:34,960 --> 00:24:37,734 DAN BRADLEY: Why can't it be equal to zero? 538 00:24:37,734 --> 00:24:39,526 AUDIENCE: [INAUDIBLE]. 539 00:24:39,526 --> 00:24:39,980 DAN BRADLEY: Yeah. 540 00:24:39,980 --> 00:24:42,590 So for the numerator, it's just while the numerator 541 00:24:42,590 --> 00:24:43,840 is less than zero. 542 00:24:49,570 --> 00:24:50,480 Does that make sense to people? 543 00:24:50,480 --> 00:24:53,190 We keep looping while the numerator is less than zero, 544 00:24:53,190 --> 00:24:58,530 we keep asking for new input, and keep demanding that they 545 00:24:58,530 --> 00:24:59,520 input new things. 546 00:24:59,520 --> 00:25:10,436 So I'll just have a little printf in here, non-negative 547 00:25:10,436 --> 00:25:11,686 num--. 548 00:25:17,780 --> 00:25:21,940 And then we do the same thing as before, except we do a 549 00:25:21,940 --> 00:25:25,220 GetInt, just because we wanted to do it as 550 00:25:25,220 --> 00:25:27,600 percentages of ints. 551 00:25:27,600 --> 00:25:32,010 So numerator = GetInt. 552 00:25:36,420 --> 00:25:39,420 >> Okay, now a few things. 553 00:25:39,420 --> 00:25:41,030 Do I need this -1 here? 554 00:25:41,030 --> 00:25:41,976 AUDIENCE: [INAUDIBLE]. 555 00:25:41,976 --> 00:25:42,449 DAN BRADLEY: No. 556 00:25:42,449 --> 00:25:44,814 Why not? 557 00:25:44,814 --> 00:25:47,440 AUDIENCE: Because you're getting a random value from the user. 558 00:25:47,440 --> 00:25:48,540 DAN BRADLEY: Exactly. 559 00:25:48,540 --> 00:25:53,540 But do I want to have the declaration out here? 560 00:25:53,540 --> 00:25:53,950 Yes. 561 00:25:53,950 --> 00:25:54,220 Why? 562 00:25:54,220 --> 00:25:56,640 AUDIENCE: You have to find the outside of the loop. 563 00:25:56,640 --> 00:25:59,060 Otherwise, you're not going to have the variable for the rest 564 00:25:59,060 --> 00:26:00,520 of the program. 565 00:26:00,520 --> 00:26:01,270 DAN BRADLEY: Exactly. 566 00:26:01,270 --> 00:26:03,290 Scope. 567 00:26:03,290 --> 00:26:04,750 Sweet. 568 00:26:04,750 --> 00:26:05,790 Does that make sense to everyone? 569 00:26:05,790 --> 00:26:07,090 AUDIENCE: Can you say that one more time? 570 00:26:07,090 --> 00:26:07,410 DAN BRADLEY: Okay. 571 00:26:07,410 --> 00:26:12,000 So if I were to declare this numerator inside the loop, if 572 00:26:12,000 --> 00:26:14,850 I had this instead of numerator being int numerator, 573 00:26:14,850 --> 00:26:17,720 and I got rid of this entirely, 574 00:26:17,720 --> 00:26:19,710 numerator would disappear. 575 00:26:19,710 --> 00:26:23,540 It wouldn't exist outside of this loop, because everything 576 00:26:23,540 --> 00:26:25,370 is within the scope of these curly braces. 577 00:26:28,180 --> 00:26:28,790 Make sense? 578 00:26:28,790 --> 00:26:30,460 Okay. 579 00:26:30,460 --> 00:26:34,750 >> So for denominator, we do something fairly similar, int 580 00:26:34,750 --> 00:26:39,850 denominator =-- 581 00:26:39,850 --> 00:26:41,950 and I don't need the equals-- 582 00:26:41,950 --> 00:26:52,900 do, and for the while in this case, what do I want? 583 00:26:52,900 --> 00:26:56,338 What condition do I want it to still loop on? 584 00:26:56,338 --> 00:26:57,790 AUDIENCE: Less than or equal to 0. 585 00:26:57,790 --> 00:26:59,100 DAN BRADLEY: Less than or equal to 0. 586 00:26:59,100 --> 00:27:04,680 Denominator <= 0. 587 00:27:10,350 --> 00:27:13,630 Don't forget your semicolons, kids. 588 00:27:13,630 --> 00:27:26,950 So we do the same thing as before, printf a positive 589 00:27:26,950 --> 00:27:38,920 denominator, and we get int from here. 590 00:27:43,240 --> 00:27:44,573 There we go. 591 00:27:52,461 --> 00:27:53,711 GetInt. 592 00:27:56,585 --> 00:27:57,810 Okay. 593 00:27:57,810 --> 00:28:02,360 And then in the end, we want to print out some percentage. 594 00:28:02,360 --> 00:28:10,282 So to do this, what should our string look like? 595 00:28:10,282 --> 00:28:11,532 printf. 596 00:28:18,010 --> 00:28:20,210 Any other ideas of what I can do here? 597 00:28:20,210 --> 00:28:20,480 Yeah? 598 00:28:20,480 --> 00:28:26,204 AUDIENCE: Maybe %0.2 [INAUDIBLE]. 599 00:28:26,204 --> 00:28:27,454 DAN BRADLEY: Yep. 600 00:28:31,300 --> 00:28:32,170 Okay. 601 00:28:32,170 --> 00:28:41,680 I'll start with the first bit, %.02%%, 602 00:28:41,680 --> 00:28:42,957 quotes, comma. 603 00:28:46,370 --> 00:28:48,380 And I'm going to throw a new line in here, just because I 604 00:28:48,380 --> 00:28:50,390 like new lines when I'm doing things. 605 00:28:53,210 --> 00:28:57,945 What sort of mathematical expression should I have here? 606 00:28:57,945 --> 00:29:00,250 AUDIENCE: Parentheses and float. 607 00:29:00,250 --> 00:29:00,620 DAN BRADLEY: Good. 608 00:29:00,620 --> 00:29:01,875 Why do I want to have it in float? 609 00:29:01,875 --> 00:29:03,125 AUDIENCE: [INAUDIBLE]. 610 00:29:05,640 --> 00:29:07,960 DAN BRADLEY: Good, good. 611 00:29:07,960 --> 00:29:09,210 Float. 612 00:29:11,236 --> 00:29:12,486 AUDIENCE: Numerator. 613 00:29:14,640 --> 00:29:15,610 DAN BRADLEY: Numerator? 614 00:29:15,610 --> 00:29:18,596 Okay, so I do float numerator. 615 00:29:18,596 --> 00:29:20,524 AUDIENCE: Divided by-- 616 00:29:20,524 --> 00:29:22,142 DAN BRADLEY: Divided by? 617 00:29:22,142 --> 00:29:23,480 AUDIENCE: Denominator. 618 00:29:23,480 --> 00:29:26,290 DAN BRADLEY: Do I do just denominator, or do I do float 619 00:29:26,290 --> 00:29:26,900 denominator? 620 00:29:26,900 --> 00:29:29,410 AUDIENCE: Just denominator. 621 00:29:29,410 --> 00:29:30,335 DAN BRADLEY: Okay. 622 00:29:30,335 --> 00:29:35,285 AUDIENCE: And you can multiply that by 100. 623 00:29:35,285 --> 00:29:39,130 DAN BRADLEY: And I multiply that by 100. 624 00:29:39,130 --> 00:29:40,560 AUDIENCE: And then parentheses around the entire expression. 625 00:29:40,560 --> 00:29:41,810 DAN BRADLEY: Yep. 626 00:29:47,110 --> 00:29:48,360 >> Okay. 627 00:29:50,575 --> 00:29:52,860 Look good? 628 00:29:52,860 --> 00:29:54,110 Let's try it. 629 00:29:58,170 --> 00:30:00,800 -50. 630 00:30:00,800 --> 00:30:03,590 Good, 50. 631 00:30:03,590 --> 00:30:08,010 0, and 100, 50%. 632 00:30:08,010 --> 00:30:10,500 It worked out just like we wanted. 633 00:30:10,500 --> 00:30:13,510 Does this code make sense to people? 634 00:30:13,510 --> 00:30:17,600 I'm going to save it so you can get a copy of it. 635 00:30:17,600 --> 00:30:18,684 Yeah? 636 00:30:18,684 --> 00:30:20,055 AUDIENCE: Why don't you need to put [INAUDIBLE]? 637 00:30:22,800 --> 00:30:24,603 DAN BRADLEY: Where? 638 00:30:24,603 --> 00:30:26,076 AUDIENCE: Like, when you say denominator equals 639 00:30:26,076 --> 00:30:28,040 [INAUDIBLE]. 640 00:30:28,040 --> 00:30:30,630 DAN BRADLEY: When I have it here? 641 00:30:30,630 --> 00:30:32,510 Why would I have int before it? 642 00:30:32,510 --> 00:30:37,120 AUDIENCE: Because the other one has int before it. 643 00:30:37,120 --> 00:30:40,060 DAN BRADLEY: Well, so that wasn't in a loop, right? 644 00:30:40,060 --> 00:30:45,960 So I declare this int denominator above the loop, 645 00:30:45,960 --> 00:30:48,880 and I don't have it in the loop because I would be 646 00:30:48,880 --> 00:30:52,680 declaring it a bunch of times, and it wouldn't actually carry 647 00:30:52,680 --> 00:30:54,280 over to anywhere else in the program. 648 00:30:54,280 --> 00:30:56,520 LEXI ROSS: Yeah, the important thing to remember is that 649 00:30:56,520 --> 00:30:58,530 variables only need to be declared once. 650 00:30:58,530 --> 00:31:00,570 So imagine it's like introducing a new variable 651 00:31:00,570 --> 00:31:04,640 into the world by calling it int or float or whatnot, and 652 00:31:04,640 --> 00:31:07,170 once it's declared, all you need to do to change it is 653 00:31:07,170 --> 00:31:10,510 just call it by its name: denominator, or x, or y. 654 00:31:10,510 --> 00:31:13,347 You only need to actually call it an int just once. 655 00:31:13,347 --> 00:31:14,325 >> DAN BRADLEY: Yeah. 656 00:31:14,325 --> 00:31:16,770 AUDIENCE: Can you explain the difference between %d 657 00:31:16,770 --> 00:31:21,180 and %f and if you ever declare a 658 00:31:21,180 --> 00:31:22,790 variable as a decimal? 659 00:31:22,790 --> 00:31:25,850 DAN BRADLEY: So %d and %f in here? 660 00:31:28,950 --> 00:31:32,190 LEXI ROSS: So d actually refers to just a digit, not 661 00:31:32,190 --> 00:31:33,630 decimal, which is kind of confusing. 662 00:31:33,630 --> 00:31:36,310 There's no actual data type called decimal, and when you 663 00:31:36,310 --> 00:31:39,750 want to print out an int, you refer to it as %d, or 664 00:31:39,750 --> 00:31:44,440 %-whatever-d. F is for float, which you'd think it 665 00:31:44,440 --> 00:31:49,070 would be i, but it's actually d for int. 666 00:31:49,070 --> 00:31:49,420 >> DAN BRADLEY: Yep? 667 00:31:49,420 --> 00:31:52,180 AUDIENCE: So in the other program, why didn't we just do 668 00:31:52,180 --> 00:31:57,764 the same thing when you're saying Fahrenheit? 669 00:31:57,764 --> 00:32:00,760 Why didn't we put, like--? 670 00:32:00,760 --> 00:32:02,465 DAN BRADLEY: In the bottom here? 671 00:32:02,465 --> 00:32:03,810 You could have. 672 00:32:03,810 --> 00:32:05,900 It would have been completely legitimate. 673 00:32:05,900 --> 00:32:08,290 I like doing it this way, other people like separating 674 00:32:08,290 --> 00:32:10,450 it out into different lines of code. 675 00:32:10,450 --> 00:32:12,290 Either way works fine. 676 00:32:12,290 --> 00:32:12,760 What's up? 677 00:32:12,760 --> 00:32:13,700 >> AUDIENCE: Can you explain what the %% 678 00:32:13,700 --> 00:32:14,170 [INAUDIBLE]? 679 00:32:14,170 --> 00:32:15,520 DAN BRADLEY: Okay, so the percent, percent. 680 00:32:15,520 --> 00:32:18,520 Right, I was going to go over this. 681 00:32:18,520 --> 00:32:21,970 The first percent is called an escape character, and it just 682 00:32:21,970 --> 00:32:28,730 says the thing behind this is not actually code. 683 00:32:28,730 --> 00:32:36,230 So this first percent, right, the %.02f, all of those %.02f 684 00:32:36,230 --> 00:32:40,860 mean something to printf as a function. 685 00:32:40,860 --> 00:32:44,520 %%, I just wanted it to actually just print a 686 00:32:44,520 --> 00:32:45,670 percent sign. 687 00:32:45,670 --> 00:32:47,960 So I'm not saying there's anything important behind it. 688 00:32:47,960 --> 00:32:49,820 I'm just saying there's a percent sign behind it. 689 00:32:49,820 --> 00:32:52,345 Please print out a percent sign. 690 00:32:52,345 --> 00:32:54,010 So percent, then another percent. 691 00:32:54,010 --> 00:32:56,415 AUDIENCE: Can you show what it would look like if you have 692 00:32:56,415 --> 00:32:57,858 float based on another line? 693 00:32:57,858 --> 00:33:01,490 DAN BRADLEY: Oh yeah, sure. 694 00:33:01,490 --> 00:33:05,730 So I'm being asked to show what it would look like if I 695 00:33:05,730 --> 00:33:10,500 had this float numerator, all this math, on another line. 696 00:33:10,500 --> 00:33:16,200 So float percent =-- 697 00:33:20,630 --> 00:33:25,970 and I would get rid of this and just put in percent. 698 00:33:25,970 --> 00:33:29,400 And it's doing the exact same thing, I just 699 00:33:29,400 --> 00:33:30,763 have another variable. 700 00:33:30,763 --> 00:33:33,030 >> AUDIENCE: [INAUDIBLE]. 701 00:33:33,030 --> 00:33:34,130 DAN BRADLEY: Scroll down to what? 702 00:33:34,130 --> 00:33:35,512 AUDIENCE: So that I can see [INAUDIBLE]. 703 00:33:40,380 --> 00:33:42,016 DAN BRADLEY: What can't you see? 704 00:33:42,016 --> 00:33:43,510 AUDIENCE: Sorry, I was [INAUDIBLE]. 705 00:33:47,992 --> 00:33:48,490 DAN BRADLEY: Yeah. 706 00:33:48,490 --> 00:33:50,980 AUDIENCE: So if we're replacing the escape character, 707 00:33:50,980 --> 00:33:54,466 suppose we wanted to have a dollar sign instead, just for 708 00:33:54,466 --> 00:33:57,780 example, a dollar sign behind percent? 709 00:33:57,780 --> 00:33:58,510 DAN BRADLEY: Nope. 710 00:33:58,510 --> 00:34:01,400 It's just a dollar, just one dollar sign, because a dollar 711 00:34:01,400 --> 00:34:03,740 sign doesn't mean anything to printf. 712 00:34:03,740 --> 00:34:07,980 The question was, do I need to have an escape character for 713 00:34:07,980 --> 00:34:10,135 symbols like dollar signs? 714 00:34:10,135 --> 00:34:13,330 AUDIENCE: So sort of related, are there any other cases that 715 00:34:13,330 --> 00:34:15,466 we should know of when something like the percent 716 00:34:15,466 --> 00:34:18,820 sign might mean something [INAUDIBLE]? 717 00:34:18,820 --> 00:34:20,330 LEXI ROSS: Yes. 718 00:34:20,330 --> 00:34:24,469 Not necessarily the percent sign, but the backslash, see 719 00:34:24,469 --> 00:34:26,370 how we have backlash end here? 720 00:34:26,370 --> 00:34:29,800 Let's say we actually wanted to print a backlash, we'd have 721 00:34:29,800 --> 00:34:31,070 to escape that-- 722 00:34:31,070 --> 00:34:32,670 and this is going to sound confusing-- 723 00:34:32,670 --> 00:34:34,260 with another backslash. 724 00:34:34,260 --> 00:34:36,370 So if we wanted to print \\, we would 725 00:34:36,370 --> 00:34:40,730 just have to type that, or for additional 726 00:34:40,730 --> 00:34:42,247 backslash, another escape. 727 00:34:48,340 --> 00:34:50,774 And another one, if we want to print out a quote--so 728 00:34:50,774 --> 00:34:53,040 basically, anything that kind of denotes something special 729 00:34:53,040 --> 00:34:55,800 within a string, we want to escape. 730 00:34:55,800 --> 00:34:58,700 So let's say we actually want to have a quote here, we would 731 00:34:58,700 --> 00:34:59,840 want to do \". 732 00:34:59,840 --> 00:35:02,450 AUDIENCE: So it's usually a backslash? 733 00:35:02,450 --> 00:35:03,490 DAN BRADLEY: It's usually a backslash. 734 00:35:03,490 --> 00:35:03,740 LEXI ROSS: Yeah. 735 00:35:03,740 --> 00:35:06,962 For some reason in here, it's a percent sign. 736 00:35:06,962 --> 00:35:08,212 >> AUDIENCE: [INAUDIBLE]? 737 00:35:11,680 --> 00:35:13,400 DAN BRADLEY: This guy? 738 00:35:13,400 --> 00:35:17,570 This is just to say, the thing that's being divided here is 739 00:35:17,570 --> 00:35:20,170 going to be treated as a float for the sake of the division. 740 00:35:25,110 --> 00:35:30,370 So in the case of 50 divided by 100, if I didn't have the 741 00:35:30,370 --> 00:35:33,260 float there, it would just be 0. 742 00:35:33,260 --> 00:35:35,930 That's all that would come out, because 50 divided by 743 00:35:35,930 --> 00:35:40,510 100, if you aren't including any decimals, is 0.-- 744 00:35:40,510 --> 00:35:43,520 and everything after the decimal is just truncated off. 745 00:35:43,520 --> 00:35:44,891 AUDIENCE: Um, 746 00:35:44,891 --> 00:35:48,550 why don't we put it before denominator as well? 747 00:35:48,550 --> 00:35:51,310 DAN BRADLEY: So the reason we don't put it in front of 748 00:35:51,310 --> 00:35:56,010 denominator is because when you're dividing something by 749 00:35:56,010 --> 00:35:59,320 an integer, it works just fine. 750 00:35:59,320 --> 00:36:00,630 Having a larger number on the bottom 751 00:36:00,630 --> 00:36:02,790 doesn't change anything. 752 00:36:02,790 --> 00:36:06,120 You can divide by integers just fine. 753 00:36:06,120 --> 00:36:07,250 It just doesn't really matter. 754 00:36:07,250 --> 00:36:09,053 You could, it wouldn't change anything. 755 00:36:09,053 --> 00:36:11,368 AUDIENCE: So if you wanted three decimal places, would 756 00:36:11,368 --> 00:36:13,220 you put percent .03? 757 00:36:13,220 --> 00:36:14,100 DAN BRADLEY: Yep. 758 00:36:14,100 --> 00:36:17,440 You would put %.03 if you wanted 759 00:36:17,440 --> 00:36:20,207 three decimal places. 760 00:36:20,207 --> 00:36:23,793 >> AUDIENCE: And do commands, what's the difference between 761 00:36:23,793 --> 00:36:26,330 that and just doing while at the top? 762 00:36:26,330 --> 00:36:30,540 DAN BRADLEY: So a do-while versus a while loop are two 763 00:36:30,540 --> 00:36:32,950 fairly similar commands. 764 00:36:32,950 --> 00:36:36,810 The only difference is you run the code once, you run the 765 00:36:36,810 --> 00:36:40,510 code inside the loop one time before 766 00:36:40,510 --> 00:36:42,940 making any actual check. 767 00:36:42,940 --> 00:36:46,970 So it's also the reason I can say denominator without giving 768 00:36:46,970 --> 00:36:48,970 it a number. 769 00:36:48,970 --> 00:36:52,180 So if I said int denominator, and then while (denominator <= 770 00:36:52,180 --> 00:36:55,020 0), it might never even 771 00:36:55,020 --> 00:36:56,890 run the code inside. 772 00:36:56,890 --> 00:36:59,350 It'll make that check before-- 773 00:36:59,350 --> 00:37:00,600 so here. 774 00:37:14,590 --> 00:37:19,020 So if I had this while loop here, I would have to have 775 00:37:19,020 --> 00:37:25,280 denominator equals, say, -2 or some number less 776 00:37:25,280 --> 00:37:28,150 than or equal to 0 so it would actually try running the code 777 00:37:28,150 --> 00:37:30,130 inside first. 778 00:37:30,130 --> 00:37:33,190 So generally speaking, you want to use a do-while loop 779 00:37:33,190 --> 00:37:37,750 instead of a while loop when you're asking for user input 780 00:37:37,750 --> 00:37:41,820 because running the code once, asking for something the user 781 00:37:41,820 --> 00:37:46,920 will give you is generally your best solution, as opposed 782 00:37:46,920 --> 00:37:50,250 to just trusting whatever you put in to start with. 783 00:37:50,250 --> 00:37:53,290 AUDIENCE: And do is always followed by a while. 784 00:37:53,290 --> 00:37:56,550 DAN BRADLEY: Do is followed by some chunk of code that you 785 00:37:56,550 --> 00:38:01,470 want to execute, and then while some condition is. 786 00:38:01,470 --> 00:38:03,820 LEXI ROSS: Think of it like one command sort of broken up 787 00:38:03,820 --> 00:38:04,390 into two parts. 788 00:38:04,390 --> 00:38:06,836 It's a do-while, not like here's a do, and then I'm 789 00:38:06,836 --> 00:38:10,655 going to add a while, it's just a do-while. 790 00:38:10,655 --> 00:38:13,322 >> AUDIENCE: I understand the reason why you're using the 791 00:38:13,322 --> 00:38:15,990 two percent signs, but can you explain to me why we have to 792 00:38:15,990 --> 00:38:19,390 have the [INAUDIBLE]? 793 00:38:19,390 --> 00:38:22,690 DAN BRADLEY: Okay, so the one percentage sign is just saying 794 00:38:22,690 --> 00:38:26,150 printf, here is a command that I want you to 795 00:38:26,150 --> 00:38:28,380 fill with some number. 796 00:38:28,380 --> 00:38:31,750 So I want you to fill it with two decimal places and a 797 00:38:31,750 --> 00:38:35,080 float, and you'll find the float at the end of the 798 00:38:35,080 --> 00:38:37,548 command, it's this percent. 799 00:38:37,548 --> 00:38:39,407 Does that make sense? 800 00:38:39,407 --> 00:38:39,894 AUDIENCE: Yep. 801 00:38:39,894 --> 00:38:41,355 DAN BRADLEY: Okay. 802 00:38:41,355 --> 00:38:44,764 >> AUDIENCE: Is there something about the do-while loop that 803 00:38:44,764 --> 00:38:49,390 if the condition is not fulfilled, if you get a 804 00:38:49,390 --> 00:38:52,324 negative number for your numerator, it'll just keep 805 00:38:52,324 --> 00:38:57,200 asking and will keep asking the user? 806 00:38:57,200 --> 00:38:57,400 DAN BRADLEY: Yeah. 807 00:38:57,400 --> 00:39:02,230 So if the condition of while is not fulfilled at the end of 808 00:39:02,230 --> 00:39:04,200 the do-while, it'll run back to the start. 809 00:39:04,200 --> 00:39:05,860 It's just a loop. 810 00:39:05,860 --> 00:39:08,000 So it's like a while loop that the condition hasn't been 811 00:39:08,000 --> 00:39:09,250 fulfilled on. 812 00:39:10,930 --> 00:39:13,740 It just runs one more time. 813 00:39:13,740 --> 00:39:14,558 Yeah. 814 00:39:14,558 --> 00:39:18,302 AUDIENCE: So if you're doing this, can you, for your while, 815 00:39:18,302 --> 00:39:22,430 put two conditions in? 816 00:39:22,430 --> 00:39:24,452 DAN BRADLEY: You can. 817 00:39:24,452 --> 00:39:26,350 It depends what you're asking. 818 00:39:26,350 --> 00:39:35,150 So you can put two conditions in a while loop if you have it 819 00:39:35,150 --> 00:39:39,470 all within some sort of Boolean operator, which sounds 820 00:39:39,470 --> 00:39:44,740 ugly, but it's basically this denominator is less than or 821 00:39:44,740 --> 00:39:48,730 equal to zero, either returns true or false, right? 822 00:39:48,730 --> 00:39:51,760 It's either less than or equal to zero or not. 823 00:39:51,760 --> 00:39:55,640 So you can do other Boolean operators that'll say while 824 00:39:55,640 --> 00:39:59,020 this is also true or while this is also false. 825 00:39:59,020 --> 00:40:04,630 So it's things like and numerator is greater than 826 00:40:04,630 --> 00:40:05,770 zero, right? 827 00:40:05,770 --> 00:40:10,130 That's asking, this'll only loop while denominator is less 828 00:40:10,130 --> 00:40:12,496 than zero and you have a positive numerator. 829 00:40:12,496 --> 00:40:13,700 AUDIENCE: And you do the two ANDs, right? 830 00:40:13,700 --> 00:40:14,545 DAN BRADLEY: Two ANDs. 831 00:40:14,545 --> 00:40:17,120 LEXI ROSS: We'll cover that more later. 832 00:40:17,120 --> 00:40:20,620 >> AUDIENCE: What if someone doesn't put in a positive 833 00:40:20,620 --> 00:40:22,018 denominator? 834 00:40:22,018 --> 00:40:26,410 If they put in a negative numerator, can you prompt them 835 00:40:26,410 --> 00:40:27,540 to, say, put in a non-negative numerator and have 836 00:40:27,540 --> 00:40:30,460 a command for that? 837 00:40:30,460 --> 00:40:30,980 DAN BRADLEY: You could. 838 00:40:30,980 --> 00:40:32,066 AUDIENCE: Right now it just says non-negative numerator. 839 00:40:32,066 --> 00:40:33,690 What would be used for that? 840 00:40:33,690 --> 00:40:38,230 DAN BRADLEY: So you'd have an if condition, so like if 841 00:40:38,230 --> 00:40:42,282 numerator is less than zero. 842 00:40:42,282 --> 00:40:43,960 LEXI ROSS: You'd probably just want to break it up into more 843 00:40:43,960 --> 00:40:44,810 steps, right? 844 00:40:44,810 --> 00:40:49,660 So the first time they put in the input, we'd just say give 845 00:40:49,660 --> 00:40:50,880 us a numerator or whatnot. 846 00:40:50,880 --> 00:40:53,430 And then if they do the wrong thing, this might be the case 847 00:40:53,430 --> 00:40:55,570 for a while loop, rather than a do-while, because we have 848 00:40:55,570 --> 00:41:01,460 the first time we have the GetInt, and then while that input 849 00:41:01,460 --> 00:41:04,040 is bad, we keep asking them for further input with a 850 00:41:04,040 --> 00:41:10,560 different sort of text, like that didn't work or give us a 851 00:41:10,560 --> 00:41:12,760 non-negative numerator instead, that kind of thing. 852 00:41:17,670 --> 00:41:19,143 >> DAN BRADLEY: Yeah? 853 00:41:19,143 --> 00:41:22,089 AUDIENCE: How do you clear the stuff that [INAUDIBLE] 854 00:41:22,089 --> 00:41:23,562 the terminal? 855 00:41:23,562 --> 00:41:25,526 LEXI ROSS: Clear. 856 00:41:25,526 --> 00:41:27,140 DAN BRADLEY: Yeah, you can't type into it. 857 00:41:27,140 --> 00:41:28,710 LEXI ROSS: Actually, this is not a real terminal. 858 00:41:28,710 --> 00:41:31,170 The question was how do you clear that stuff down there? 859 00:41:31,170 --> 00:41:33,200 The answer is it's not a real terminal, but let's say you 860 00:41:33,200 --> 00:41:34,450 are in a real terminal. 861 00:41:42,420 --> 00:41:45,200 Clear. 862 00:41:45,200 --> 00:41:46,450 Oh, it's a cat. 863 00:41:53,940 --> 00:41:55,190 DAN BRADLEY: [INAUDIBLE]? 864 00:41:59,500 --> 00:42:01,120 It's that one, right? 865 00:42:01,120 --> 00:42:01,790 LEXI ROSS: No, that's all my stuff. 866 00:42:01,790 --> 00:42:03,040 DAN BRADLEY: [INAUDIBLE]. 867 00:42:05,300 --> 00:42:07,912 LEXI ROSS: There we go. 868 00:42:07,912 --> 00:42:08,340 DAN BRADLEY: Yeah? 869 00:42:08,340 --> 00:42:10,030 >> AUDIENCE: Does clear work if you have-- 870 00:42:10,030 --> 00:42:12,787 like, say you run a program [INAUDIBLE] and it's asking 871 00:42:12,787 --> 00:42:15,315 you for something, and you don't want to. 872 00:42:15,315 --> 00:42:16,080 How do you-- 873 00:42:16,080 --> 00:42:17,200 DAN BRADLEY: Control+C? 874 00:42:17,200 --> 00:42:17,780 LEXI ROSS: Yeah. 875 00:42:17,780 --> 00:42:20,640 So if you're running a program and something is not going the 876 00:42:20,640 --> 00:42:22,410 way you want it to and you just want to exit the whole 877 00:42:22,410 --> 00:42:25,560 program, Control+C. Even if you're on a Mac, not Apple+C, 878 00:42:25,560 --> 00:42:34,341 Control+C. 879 00:42:34,341 --> 00:42:35,523 AUDIENCE: Did you have the correct version in the 880 00:42:35,523 --> 00:42:36,820 revision in your profile? 881 00:42:36,820 --> 00:42:39,780 DAN BRADLEY: I think it's saved, isn't it? 882 00:42:39,780 --> 00:42:40,350 Yeah. 883 00:42:40,350 --> 00:42:41,040 LEXI ROSS: This is the space. 884 00:42:41,040 --> 00:42:42,290 Yeah. 885 00:42:49,685 --> 00:42:57,496 I think it's Revision 11. 886 00:42:57,496 --> 00:42:59,468 >> DAN BRADLEY: Yeah? 887 00:42:59,468 --> 00:43:02,426 AUDIENCE: Does C make any assumptions about what a 888 00:43:02,426 --> 00:43:05,877 variable is if you define a variable but don't assign 889 00:43:05,877 --> 00:43:07,590 anything to it immediately? 890 00:43:07,590 --> 00:43:11,380 DAN BRADLEY: So it'll only accept ints into that spot. 891 00:43:11,380 --> 00:43:13,980 So if you declare it-- 892 00:43:13,980 --> 00:43:16,842 AUDIENCE: If I just did int denominator semicolon, does it 893 00:43:16,842 --> 00:43:18,750 assume that it's equal to zero or any number--? 894 00:43:18,750 --> 00:43:20,210 DAN BRADLEY: Nope. 895 00:43:20,210 --> 00:43:25,920 So if you don't assign, say, int denominator to any value, 896 00:43:25,920 --> 00:43:28,860 it could be anything. 897 00:43:28,860 --> 00:43:31,150 It's the old memory that was in that location. 898 00:43:31,150 --> 00:43:34,050 So it could be negative 2 billion, it could be positive 899 00:43:34,050 --> 00:43:38,030 2 billion, and it could be anywhere in between. 900 00:43:38,030 --> 00:43:44,170 So if you're actually running things, make sure that you 901 00:43:44,170 --> 00:43:46,080 assign a value to the variables. 902 00:43:46,080 --> 00:43:49,050 This, it doesn't matter because we're immediately 903 00:43:49,050 --> 00:43:52,580 assigning the value, but generally speaking, yeah. 904 00:44:01,660 --> 00:44:04,640 Any other questions? 905 00:44:04,640 --> 00:44:06,300 >> LEXI ROSS: We can also just take general questions at this 906 00:44:06,300 --> 00:44:10,600 point on lecture material, p set material, anything you've 907 00:44:10,600 --> 00:44:15,363 been wondering about, this is a good time to ask those. 908 00:44:15,363 --> 00:44:16,325 AUDIENCE: So in the walkthroughs, Zamyla 909 00:44:16,325 --> 00:44:17,768 mentioned the round function. 910 00:44:17,768 --> 00:44:21,540 Would you mind elaborating on how to implement that? 911 00:44:21,540 --> 00:44:22,940 LEXI ROSS: Sure, how to implement that? 912 00:44:22,940 --> 00:44:25,700 AUDIENCE: She said it was round. 913 00:44:25,700 --> 00:44:26,580 LEXI ROSS: Oh yeah. 914 00:44:26,580 --> 00:44:29,880 So just so we all have the same vocab and stuff, 915 00:44:29,880 --> 00:44:33,070 implement generally means actually writing from scratch. 916 00:44:33,070 --> 00:44:35,530 And so I don't think she was talking about sort of writing 917 00:44:35,530 --> 00:44:36,010 the round function. 918 00:44:36,010 --> 00:44:37,820 She probably just meant how to use it. 919 00:44:37,820 --> 00:44:41,900 Implement means sort of like create. 920 00:44:41,900 --> 00:44:48,670 So generally, a time when we want to use the round function is I 921 00:44:48,670 --> 00:44:49,170 mean, sort of, for instance, 922 00:44:49,170 --> 00:44:50,260 if you want to round. 923 00:44:50,260 --> 00:44:53,140 Can you give me a little bit more context as to what the 924 00:44:53,140 --> 00:44:53,907 problem was? 925 00:44:53,907 --> 00:44:56,192 AUDIENCE: To avoid the imprecisions of decimals 926 00:44:56,192 --> 00:44:58,480 by making them a float. 927 00:44:58,480 --> 00:44:59,020 LEXI ROSS: Yeah. 928 00:44:59,020 --> 00:45:02,770 So the issue with floats that we've kind of alluded to is 929 00:45:02,770 --> 00:45:04,820 that because of the way they're stored in the machine, 930 00:45:04,820 --> 00:45:08,700 they're not 100% precise in the way that you might think 931 00:45:08,700 --> 00:45:10,060 your calculator would store them. 932 00:45:13,560 --> 00:45:16,560 It's hard for me to show an example without knowing what 933 00:45:16,560 --> 00:45:17,750 the context was from. 934 00:45:17,750 --> 00:45:18,920 I don't know, can you think of something, Dan? 935 00:45:18,920 --> 00:45:23,120 >> DAN BRADLEY: So if you're asking for, say, numbers 936 00:45:23,120 --> 00:45:26,590 greater than 10 and you're using floats, and you're adding one 937 00:45:26,590 --> 00:45:30,810 to a float every time, it will sometimes wait until you've 938 00:45:30,810 --> 00:45:34,470 gotten to 12 before it'll say this is actually a number 939 00:45:34,470 --> 00:45:36,520 greater than 10. 940 00:45:36,520 --> 00:45:42,530 Floats have some serious issues just between the 32nd 941 00:45:42,530 --> 00:45:49,970 and 33rd decimal place that'll go, so it might 9.9999999, or 942 00:45:49,970 --> 00:45:53,210 it might be 10.0000001, right? 943 00:45:53,210 --> 00:45:55,680 It doesn't really distinguish between the two, so if you're 944 00:45:55,680 --> 00:45:59,310 waiting for things greater than or equal to 10, you might 945 00:45:59,310 --> 00:46:03,440 not get there at what you think is the 10th iteration. 946 00:46:03,440 --> 00:46:07,760 You might get there in the 11th. 947 00:46:07,760 --> 00:46:08,980 LEXI ROSS: Essentially, yeah. 948 00:46:08,980 --> 00:46:11,490 When you're looping over a lot of floats, that's when the 949 00:46:11,490 --> 00:46:13,276 imprecisions happen. 950 00:46:13,276 --> 00:46:18,350 AUDIENCE: I think, so basically, it's like on the p 951 00:46:18,350 --> 00:46:24,030 set, it wants you to round the float value, and if you put in 952 00:46:24,030 --> 00:46:29,940 4.24 for the amount of change you need back, if you just do 953 00:46:29,940 --> 00:46:33,970 times 100, it'll actually change into 423-- 954 00:46:33,970 --> 00:46:34,460 DAN BRADLEY: Sometimes, yeah. 955 00:46:34,460 --> 00:46:36,974 AUDIENCE: So I think it's just like, how do you use the round 956 00:46:36,974 --> 00:46:39,390 function in the actual code to make it so it doesn't do that? 957 00:46:39,390 --> 00:46:44,390 DAN BRADLEY: So if you're rounding 424 by multiplying it 958 00:46:44,390 --> 00:46:48,320 by 100 and making it into an integer, it will sometimes 959 00:46:48,320 --> 00:46:52,130 make it 423 because of that imprecision. 960 00:46:52,130 --> 00:46:58,926 And so things you can do is just add 0.0000003. 961 00:46:58,926 --> 00:47:00,744 AUDIENCE: But I think, actually, they want 962 00:47:00,744 --> 00:47:02,400 us to use the round. 963 00:47:02,400 --> 00:47:03,250 DAN BRADLEY: The round function? 964 00:47:03,250 --> 00:47:06,310 There's probably a math function, something in the 965 00:47:06,310 --> 00:47:06,996 math library. 966 00:47:06,996 --> 00:47:07,910 AUDIENCE: #include math. 967 00:47:07,910 --> 00:47:10,150 DAN BRADLEY: #include , and there's 968 00:47:10,150 --> 00:47:10,990 probably a round function. 969 00:47:10,990 --> 00:47:12,480 AUDIENCE: Just like round()--? 970 00:47:12,480 --> 00:47:13,840 LEXI ROSS: Essentially, because you want to round to 971 00:47:13,840 --> 00:47:14,720 the nearest cent. 972 00:47:14,720 --> 00:47:16,440 DAN BRADLEY: Let's pull up the man pages. 973 00:47:16,440 --> 00:47:19,460 So a handy tool, if you don't know-- 974 00:47:19,460 --> 00:47:21,800 you pull up the terminal-- 975 00:47:21,800 --> 00:47:23,670 is if you go to man math. 976 00:47:27,272 --> 00:47:29,030 LEXI ROSS: Cool. 977 00:47:29,030 --> 00:47:34,970 >> So you can see, so man is short for manual, not man, and 978 00:47:34,970 --> 00:47:37,660 it allows you to look up more information about any given 979 00:47:37,660 --> 00:47:40,000 function or library that you might be dealing with. 980 00:47:40,000 --> 00:47:44,040 So I just typed in man math. 981 00:47:44,040 --> 00:47:45,290 Let me zoom in. 982 00:47:50,990 --> 00:47:52,610 DAN BRADLEY: We can try man round, too. 983 00:47:52,610 --> 00:47:55,060 LEXI ROSS: Sure. 984 00:47:55,060 --> 00:47:56,870 DAN BRADLEY: And this will list out-- 985 00:47:56,870 --> 00:48:00,640 man math will list everything that's in the math library, 986 00:48:00,640 --> 00:48:02,820 which we'll include. 987 00:48:02,820 --> 00:48:04,080 Yeah, we also have round. 988 00:48:04,080 --> 00:48:06,690 LEXI ROSS: So I just did man round. 989 00:48:06,690 --> 00:48:10,460 You can see here in the synopsis, it says #include 990 00:48:10,460 --> 00:48:16,510 , so that's kind of like cs50.h, stdio.h. 991 00:48:16,510 --> 00:48:19,020 And it looks like it's actually overloaded, meaning 992 00:48:19,020 --> 00:48:22,930 it takes different types of arguments, so you can round 993 00:48:22,930 --> 00:48:24,320 doubles or floats. 994 00:48:24,320 --> 00:48:26,740 DAN BRADLEY: Round versus roundf. 995 00:48:26,740 --> 00:48:27,335 LEXI ROSS: Roundf. 996 00:48:27,335 --> 00:48:30,128 Did Zamyla mention roundf versus round? 997 00:48:30,128 --> 00:48:34,120 AUDIENCE: In the walkthrough, it was just round(). 998 00:48:34,120 --> 00:48:36,780 DAN BRADLEY: Okay, so she probably was using the double, 999 00:48:36,780 --> 00:48:39,110 then, as opposed to a float. 1000 00:48:39,110 --> 00:48:43,080 Okay, so you would multiply by 100 and then round it, and 1001 00:48:43,080 --> 00:48:47,950 you'd get the number closest to it, I think. 1002 00:48:47,950 --> 00:48:50,450 >> LEXI ROSS: So the round function returns the integral 1003 00:48:50,450 --> 00:48:53,760 value nearest to x rounding halfway cases away from 0, 1004 00:48:53,760 --> 00:48:55,850 regardless of the current rounding direction. 1005 00:48:55,850 --> 00:49:00,160 So essentially, you would want to only round once you're at 1006 00:49:00,160 --> 00:49:01,830 an integer value, so you multiply by 1007 00:49:01,830 --> 00:49:03,896 100 and then round. 1008 00:49:03,896 --> 00:49:05,787 Does that make more sense? 1009 00:49:05,787 --> 00:49:10,580 AUDIENCE: To use that function in a C file, do you need to 1010 00:49:10,580 --> 00:49:15,475 put round float, like if your variable is called q, do you 1011 00:49:15,475 --> 00:49:21,216 put round (float) q, and then a parentheses, or can 1012 00:49:21,216 --> 00:49:23,770 you just put round, and then q in parentheses? 1013 00:49:23,770 --> 00:49:27,180 DAN BRADLEY: So if you were trying to use this function 1014 00:49:27,180 --> 00:49:33,360 actually, you would take that q, is q a float, 1015 00:49:33,360 --> 00:49:34,995 or how is it declared? 1016 00:49:34,995 --> 00:49:35,980 AUDIENCE: Q is a float. 1017 00:49:35,980 --> 00:49:39,080 DAN BRADLEY: So if q is declared as a float, you would 1018 00:49:39,080 --> 00:49:42,150 just take round, and then give it q. 1019 00:49:42,150 --> 00:49:45,770 You might have to declare it as a double in this case. 1020 00:49:45,770 --> 00:49:47,170 LEXI ROSS: Yeah, unless you want to use roundf. 1021 00:49:47,170 --> 00:49:50,620 AUDIENCE: I could just use roundf, and then q in quotes? 1022 00:49:50,620 --> 00:49:53,706 DAN BRADLEY: No, not in quotes, just q. 1023 00:49:53,706 --> 00:49:55,542 AUDIENCE: Sorry, not in quotes, in the parentheses. 1024 00:49:55,542 --> 00:49:56,792 DAN BRADLEY: In the parentheses, yeah. 1025 00:50:00,330 --> 00:50:02,040 LEXI ROSS: Sorry that was a little bit confusing. 1026 00:50:02,040 --> 00:50:03,067 Any more questions? 1027 00:50:03,067 --> 00:50:04,041 >> DAN BRADLEY: Yeah? 1028 00:50:04,041 --> 00:50:07,450 AUDIENCE: So round only rounds to integers? 1029 00:50:07,450 --> 00:50:08,920 LEXI ROSS: Yes. 1030 00:50:08,920 --> 00:50:11,020 DAN BRADLEY: Yeah, round only rounds to integers. 1031 00:50:11,020 --> 00:50:13,030 AUDIENCE: So if you wanted to have something exact that 1032 00:50:13,030 --> 00:50:15,146 needed to be [INAUDIBLE] multiplied by 100 and then 1033 00:50:15,146 --> 00:50:17,536 divided by 100 again if you wanted to--? 1034 00:50:20,410 --> 00:50:23,497 LEXI ROSS: So if you wanted to get it into a decimal form? 1035 00:50:23,497 --> 00:50:27,224 AUDIENCE: Well the example, for instance, [INAUDIBLE] you 1036 00:50:27,224 --> 00:50:30,732 have 1.25, and then [INAUDIBLE] 1037 00:50:30,732 --> 00:50:36,100 1.24, if you wanted to keep that as 1.25, you would 1038 00:50:36,100 --> 00:50:39,516 multiply that by 100, round it, and then divide it by 100 1039 00:50:39,516 --> 00:50:41,480 again to get 1.25? 1040 00:50:41,480 --> 00:50:41,860 LEXI ROSS: Yeah. 1041 00:50:41,860 --> 00:50:46,770 So the question is, since rounding, as the man page 1042 00:50:46,770 --> 00:50:50,470 says, rounds to the nearest integral value, does that mean 1043 00:50:50,470 --> 00:50:53,420 that one would want to multiply by 100, do the 1044 00:50:53,420 --> 00:50:56,260 rounding, and divide by 100 again to, say, get the correct 1045 00:50:56,260 --> 00:50:58,750 dollars and cents value? 1046 00:50:58,750 --> 00:51:02,000 And the answer is--? 1047 00:51:02,000 --> 00:51:02,160 DAN BRADLEY: No. 1048 00:51:02,160 --> 00:51:04,840 I mean, if you're just getting a float value, if you start 1049 00:51:04,840 --> 00:51:08,730 with a float value of 1.25, that's how it's going to be 1050 00:51:08,730 --> 00:51:10,260 interpreted. 1051 00:51:10,260 --> 00:51:13,790 So if you multiply it by 100 and then divide it by 100, 1052 00:51:13,790 --> 00:51:17,130 it's still going to be interpreted as 1.25, or 1053 00:51:17,130 --> 00:51:18,260 possibly 1.249... 1054 00:51:18,260 --> 00:51:19,440 ..999. 1055 00:51:19,440 --> 00:51:21,630 LEXI ROSS: I think the question was like, you would 1056 00:51:21,630 --> 00:51:24,850 multiply, then use round, then divide, so it's like-- 1057 00:51:24,850 --> 00:51:25,290 DAN BRADLEY: Yeah. 1058 00:51:25,290 --> 00:51:27,530 I still don't think it changes anything, though. 1059 00:51:27,530 --> 00:51:28,000 I think it's still. 1060 00:51:28,000 --> 00:51:30,550 LEXI ROSS: Because round does-- it uses integers. 1061 00:51:30,550 --> 00:51:34,080 So if you try to round 1.25, you just get 1. 1062 00:51:34,080 --> 00:51:36,570 So I think in order to get like the additional precision, 1063 00:51:36,570 --> 00:51:37,720 if that's what you're talking about. 1064 00:51:37,720 --> 00:51:42,260 >> AUDIENCE: So in what instances do you use round, then? 1065 00:51:42,260 --> 00:51:44,920 DAN BRADLEY: So in this case, if you want, say, the number 1066 00:51:44,920 --> 00:51:51,570 of cents from $1.25, you'd multiply by 100, round, get 1067 00:51:51,570 --> 00:51:56,980 125, and leave it as that, as cents, as opposed to trying to 1068 00:51:56,980 --> 00:51:58,760 convert it back into dollars and cents. 1069 00:51:58,760 --> 00:52:02,092 LEXI ROSS: Oh yeah, yeah, for sure. 1070 00:52:02,092 --> 00:52:04,948 AUDIENCE: Why do you need to round that [INAUDIBLE]? 1071 00:52:07,810 --> 00:52:10,740 DAN BRADLEY: So if you multiply it by 100 and then 1072 00:52:10,740 --> 00:52:14,060 immediately convert it to an integer, which is the more 1073 00:52:14,060 --> 00:52:17,950 useful form, that's when you deal with that 1074 00:52:17,950 --> 00:52:19,470 imprecision, right? 1075 00:52:19,470 --> 00:52:26,450 So it might be 124.999999, and so if you then convert that to 1076 00:52:26,450 --> 00:52:29,480 an integer, it just drops everything off that's below 1077 00:52:29,480 --> 00:52:29,930 the decimal. 1078 00:52:29,930 --> 00:52:31,160 AUDIENCE: So it'll give you [INAUDIBLE]. 1079 00:52:31,160 --> 00:52:34,960 DAN BRADLEY: Exactly, instead of 125, which is 1080 00:52:34,960 --> 00:52:37,074 why you want to round. 1081 00:52:37,074 --> 00:52:38,850 >> AUDIENCE: Sorry, could you repeat what you said you 1082 00:52:38,850 --> 00:52:40,680 should do as opposed to what you shouldn't do? 1083 00:52:40,680 --> 00:52:40,910 DAN BRADLEY: Okay. 1084 00:52:40,910 --> 00:52:45,725 So what you should do is if you start with 1.25, multiply 1085 00:52:45,725 --> 00:52:51,960 it by 100, and then round that whole number, that 125 or 1086 00:52:51,960 --> 00:52:56,420 124.9999, and get that to the nearest integer. 1087 00:52:56,420 --> 00:52:58,420 AUDIENCE: So if you round, that puts it 1088 00:52:58,420 --> 00:52:59,920 to the nearest integer? 1089 00:52:59,920 --> 00:53:01,420 DAN BRADLEY: Yeah. 1090 00:53:01,420 --> 00:53:07,420 AUDIENCE: Is there a way to limit, if you ask for a float 1091 00:53:07,420 --> 00:53:10,920 to be inputted, is there a way to limit how many decimal 1092 00:53:10,920 --> 00:53:13,920 places you can input a float to? 1093 00:53:13,920 --> 00:53:16,210 LEXI ROSS: Not using the CS50's implementation. 1094 00:53:16,210 --> 00:53:22,560 I mean, you can count how large the input is given by 1095 00:53:22,560 --> 00:53:25,020 the user, but that would require a new implementation 1096 00:53:25,020 --> 00:53:29,780 of GetFloat, and so you don't need to worry about that. 1097 00:53:29,780 --> 00:53:34,850 DAN BRADLEY: So if they, say, input 1.256, you could just 1098 00:53:34,850 --> 00:53:38,310 use 1.25, or you could do 1.26. 1099 00:53:38,310 --> 00:53:40,450 Either way, I think the implementation says you can-- 1100 00:53:40,450 --> 00:53:41,860 AUDIENCE: You can just have it round to-- 1101 00:53:41,860 --> 00:53:46,860 DAN BRADLEY: Whatever it wants, yeah, as long as you do 1102 00:53:46,860 --> 00:53:48,110 some sort of rounding. 1103 00:53:51,410 --> 00:53:53,950 Anything else? 1104 00:53:53,950 --> 00:53:55,200 Doesn't have to be about rounding. 1105 00:54:00,886 --> 00:54:01,374 LEXI ROSS: Yeah? 1106 00:54:01,374 --> 00:54:03,814 >> AUDIENCE: If we want to check to see if the style of our 1107 00:54:03,814 --> 00:54:06,285 code is appropriate or the best style 1108 00:54:06,285 --> 00:54:08,585 that it can be, is that the sort of thing we'd bring up in 1109 00:54:08,585 --> 00:54:11,460 section, or at office hours, or is that the sort of thing 1110 00:54:11,460 --> 00:54:15,582 we can post privately on the thing, on discussion, and then 1111 00:54:15,582 --> 00:54:17,280 ask for advice on? 1112 00:54:17,280 --> 00:54:20,400 LEXI ROSS: So we don't, at office hours or on CS50 1113 00:54:20,400 --> 00:54:23,510 Discuss, we won't actually grade your p set in advance 1114 00:54:23,510 --> 00:54:26,420 for style, but I think probably the most helpful 1115 00:54:26,420 --> 00:54:29,800 thing you could do would be to look at examples, and you 1116 00:54:29,800 --> 00:54:35,290 should be able to find that in section code, which should be 1117 00:54:35,290 --> 00:54:39,290 distributed online, as well as the lecture slides, there 1118 00:54:39,290 --> 00:54:40,730 should be examples of code. 1119 00:54:40,730 --> 00:54:43,150 But if you have individual questions about a certain 1120 00:54:43,150 --> 00:54:46,300 style issue, definitely feel free to ask that on CS50 1121 00:54:46,300 --> 00:54:49,320 Discuss, and office hours, or on your own time. 1122 00:54:49,320 --> 00:54:52,330 But if it's more a general look at my code, it's best to 1123 00:54:52,330 --> 00:54:55,610 kind of use examples and try to fit your code to that. 1124 00:54:55,610 --> 00:54:57,560 And I believe there is a style guide on-- 1125 00:54:57,560 --> 00:54:58,640 DAN BRADLEY: There's the style guide. 1126 00:54:58,640 --> 00:54:59,830 The style guide's awesome. 1127 00:54:59,830 --> 00:55:03,670 It teaches you everything you need, and it covers pretty 1128 00:55:03,670 --> 00:55:07,710 much every example known to man, or at least known to me. 1129 00:55:10,750 --> 00:55:12,000 >> Anything else? 1130 00:55:14,830 --> 00:55:15,275 All right. 1131 00:55:15,275 --> 00:55:15,840 Cool, guys. 1132 00:55:15,840 --> 00:55:16,440 LEXI ROSS: Have a great day. 1133 00:55:16,440 --> 00:55:17,690 DAN BRADLEY: Thanks for coming.