1 00:00:00,000 --> 00:00:04,645 2 00:00:04,645 --> 00:00:06,020 CARTER ZENKE: Hello, one and all. 3 00:00:06,020 --> 00:00:10,610 And welcome to CS50's very first super section for week 1. 4 00:00:10,610 --> 00:00:12,005 My name is Carter Zenke. 5 00:00:12,005 --> 00:00:13,130 I'm the course's preceptor. 6 00:00:13,130 --> 00:00:14,990 I'm joined here by many of our wonderful staff members 7 00:00:14,990 --> 00:00:16,698 that are going to stand up and say hello. 8 00:00:16,698 --> 00:00:19,140 [APPLAUSE] 9 00:00:19,140 --> 00:00:21,680 Yeah, a round of applause for these folks. 10 00:00:21,680 --> 00:00:22,430 They're wonderful. 11 00:00:22,430 --> 00:00:25,650 They'll be helping you through this super section today. 12 00:00:25,650 --> 00:00:27,740 This is our first super section for the course. 13 00:00:27,740 --> 00:00:29,990 On a normal week, you actually attend your own section 14 00:00:29,990 --> 00:00:31,310 online by course's head teaching fellow. 15 00:00:31,310 --> 00:00:32,509 You signed up for those online at Harvard. 16 00:00:32,509 --> 00:00:34,290 And you'll attend those starting next week. 17 00:00:34,290 --> 00:00:35,998 But this week, given the odd schedule, we 18 00:00:35,998 --> 00:00:38,940 thought we'd come together for one large section overall. 19 00:00:38,940 --> 00:00:40,710 So here are these details here. 20 00:00:40,710 --> 00:00:44,040 You're going to email if you've got any questions, heads@cs50.harvard.edu. 21 00:00:44,040 --> 00:00:46,880 And all of our slides and intros for today will be at this URL 22 00:00:46,880 --> 00:00:51,205 down here on the course website, going to the super section page there. 23 00:00:51,205 --> 00:00:53,330 To kick things off, though, I thought we'd actually 24 00:00:53,330 --> 00:00:57,208 have you all talk to each other and think about these two questions. 25 00:00:57,208 --> 00:00:59,750 So think back to lecture and think about this first question. 26 00:00:59,750 --> 00:01:02,180 What did you find exciting from that lecture? 27 00:01:02,180 --> 00:01:04,610 And what are you still confused about? 28 00:01:04,610 --> 00:01:06,480 What do you still have questions about? 29 00:01:06,480 --> 00:01:10,520 So turn to somebody, perhaps who you don't know and talk about those two 30 00:01:10,520 --> 00:01:12,020 questions for 2 minutes here. 31 00:01:12,020 --> 00:01:13,700 One, what are you excited about? 32 00:01:13,700 --> 00:01:17,210 Two, what do you still have questions about? 33 00:01:17,210 --> 00:01:19,910 Let's come back from these conversations. 34 00:01:19,910 --> 00:01:23,100 So glad you are having this wonderful discussion here. 35 00:01:23,100 --> 00:01:25,610 And let's hear from a few different groups 36 00:01:25,610 --> 00:01:29,400 on what you were thinking about for these questions here. 37 00:01:29,400 --> 00:01:30,410 Thank you, all. 38 00:01:30,410 --> 00:01:33,455 Let's actually turn to this left-hand section. 39 00:01:33,455 --> 00:01:35,330 Could one group over here tell me what you're 40 00:01:35,330 --> 00:01:37,997 excited about for this week and one thing you're still wondering 41 00:01:37,997 --> 00:01:39,578 or you have questions about? 42 00:01:39,578 --> 00:01:41,120 One group from this side of the room? 43 00:01:41,120 --> 00:01:46,960 44 00:01:46,960 --> 00:01:49,460 Can I narrow it down to somebody from the front row perhaps? 45 00:01:49,460 --> 00:01:50,670 What are you interested in? 46 00:01:50,670 --> 00:01:52,878 What do you still have questions about for this week? 47 00:01:52,878 --> 00:01:59,620 48 00:01:59,620 --> 00:02:01,540 Yeah, go ahead. 49 00:02:01,540 --> 00:02:06,850 STUDENT: I think it's more of the-- and less of the code, the syntax itself. 50 00:02:06,850 --> 00:02:10,580 Because that, you can kind of pick up quite quickly. 51 00:02:10,580 --> 00:02:16,450 I think the hardest thing is probably to how you construct efficient code. 52 00:02:16,450 --> 00:02:18,988 You sometimes just have to think in a different way. 53 00:02:18,988 --> 00:02:19,780 CARTER ZENKE: Nice. 54 00:02:19,780 --> 00:02:23,550 STUDENT: That's something that I guess, we'll 55 00:02:23,550 --> 00:02:26,043 really take some time to come to terms. 56 00:02:26,043 --> 00:02:27,210 CARTER ZENKE: Totally, yeah. 57 00:02:27,210 --> 00:02:29,130 So there's a question, how do you write efficient code? 58 00:02:29,130 --> 00:02:31,440 And later today, we'll actually work on the course's lab, 59 00:02:31,440 --> 00:02:33,440 this practice problem to help you see how we can 60 00:02:33,440 --> 00:02:35,130 write efficient code in that instance. 61 00:02:35,130 --> 00:02:38,297 You can work together on this practice problem and get ideas from each other 62 00:02:38,297 --> 00:02:39,150 as we go through. 63 00:02:39,150 --> 00:02:41,580 Let's hear from maybe one more group over on this side, 64 00:02:41,580 --> 00:02:44,160 maybe somebody in the second row back here, if you mind. 65 00:02:44,160 --> 00:02:46,650 One thing you're interested in, yeah, go ahead. 66 00:02:46,650 --> 00:02:49,830 STUDENT: I'm really interested in also this kind of stuff, 67 00:02:49,830 --> 00:02:54,030 but also creating stuff, just coding to have a the final product. 68 00:02:54,030 --> 00:02:58,350 And now I'll get opportunities to make a final product. 69 00:02:58,350 --> 00:03:00,570 And somebody else to think about lecture, 70 00:03:00,570 --> 00:03:02,910 I feel like there's so much syntax to learn. 71 00:03:02,910 --> 00:03:05,820 And that's sort of giving me pause. 72 00:03:05,820 --> 00:03:08,750 It's like, I don't even know what's out there. 73 00:03:08,750 --> 00:03:13,640 I think somebody said, actually in the orientation, 74 00:03:13,640 --> 00:03:15,860 sometimes like students will go something 75 00:03:15,860 --> 00:03:18,110 all the way out because they don't even know that there's a program for it 76 00:03:18,110 --> 00:03:18,770 already. 77 00:03:18,770 --> 00:03:20,030 CARTER ZENKE: Yeah, exactly. 78 00:03:20,030 --> 00:03:22,573 So there's this idea of how you take this idea you have 79 00:03:22,573 --> 00:03:24,740 and what you want to build and turn into the syntax, 80 00:03:24,740 --> 00:03:27,380 like actually write to make the computer do what you want it to do, right? 81 00:03:27,380 --> 00:03:29,100 So all these are good questions. 82 00:03:29,100 --> 00:03:31,740 And here are a few we'll actually dive in to today. 83 00:03:31,740 --> 00:03:33,980 The first is, why are we using C? 84 00:03:33,980 --> 00:03:37,280 Why we're using this ancient language to start off programming with? 85 00:03:37,280 --> 00:03:39,802 And how do we learn and read its syntax here? 86 00:03:39,802 --> 00:03:43,010 How can we actually learn how to create and read these variables, these loops 87 00:03:43,010 --> 00:03:44,750 and conditionals, and so on. 88 00:03:44,750 --> 00:03:48,260 As we go through that, we'll talk about this particular instance of a data type 89 00:03:48,260 --> 00:03:53,100 because C requires us to tell what type each pieces of our data are. 90 00:03:53,100 --> 00:03:56,150 We'll also talk about compiling C programs, taking them from source code 91 00:03:56,150 --> 00:03:57,140 to machine code. 92 00:03:57,140 --> 00:03:59,015 And then finally, at the very end, we'll talk 93 00:03:59,015 --> 00:04:02,720 about how many years it will take to double our number of llamas 94 00:04:02,720 --> 00:04:04,087 that we have in the lab. 95 00:04:04,087 --> 00:04:06,920 So to kick things off here, we can go back to actually lecture zero, 96 00:04:06,920 --> 00:04:09,510 I think, back to our contact application. 97 00:04:09,510 --> 00:04:13,820 So in this application we stored names and phone numbers and other things too. 98 00:04:13,820 --> 00:04:16,430 What else could we store in a contact's application? 99 00:04:16,430 --> 00:04:19,430 Could I ask maybe somebody from the second row, in this middle row here? 100 00:04:19,430 --> 00:04:23,330 What can we store in a contact's application? 101 00:04:23,330 --> 00:04:23,962 Yeah, go ahead. 102 00:04:23,962 --> 00:04:24,920 STUDENT: Phone numbers. 103 00:04:24,920 --> 00:04:27,128 CARTER ZENKE: Phone numbers, right, other things too. 104 00:04:27,128 --> 00:04:29,142 Maybe somebody in the row up now. 105 00:04:29,142 --> 00:04:30,238 STUDENT: Emails. 106 00:04:30,238 --> 00:04:31,280 CARTER ZENKE: Emails, OK. 107 00:04:31,280 --> 00:04:31,880 Other things? 108 00:04:31,880 --> 00:04:33,240 Let's go one row up again. 109 00:04:33,240 --> 00:04:34,250 What else can we store? 110 00:04:34,250 --> 00:04:35,120 STUDENT: Addresses. 111 00:04:35,120 --> 00:04:35,990 CARTER ZENKE: Addresses, nice. 112 00:04:35,990 --> 00:04:38,390 So all these things we can store in our contact's application. 113 00:04:38,390 --> 00:04:40,130 But let's say you want to store the number of times 114 00:04:40,130 --> 00:04:43,040 I want to actually have made a call before in my application, on my phone, 115 00:04:43,040 --> 00:04:43,540 right? 116 00:04:43,540 --> 00:04:47,180 So here we have an instance of a variable, some number or some value 117 00:04:47,180 --> 00:04:48,110 that can change. 118 00:04:48,110 --> 00:04:50,870 And I'm going to give a name to it, in this case, calls, 119 00:04:50,870 --> 00:04:52,522 number of times I called somebody here. 120 00:04:52,522 --> 00:04:54,980 So this is our mental representation of what a variable is. 121 00:04:54,980 --> 00:04:57,080 It's some name for a value that can change. 122 00:04:57,080 --> 00:05:00,382 But in C, we have a particular syntax I'll use to make variables. 123 00:05:00,382 --> 00:05:02,840 As you all go through and work on the course's problem set, 124 00:05:02,840 --> 00:05:05,840 build your own programs, you really should get familiar with this syntax 125 00:05:05,840 --> 00:05:07,560 and how to read and write it here. 126 00:05:07,560 --> 00:05:11,395 So here we have the syntax in C to create this variable named calls. 127 00:05:11,395 --> 00:05:14,520 And there are some components to it that we should actually dive into here. 128 00:05:14,520 --> 00:05:16,910 So the first part is the variable's name. 129 00:05:16,910 --> 00:05:18,530 This name is calls. 130 00:05:18,530 --> 00:05:21,380 Notice how it's in the middle of this sentence here. 131 00:05:21,380 --> 00:05:25,010 We also have the variables type, this int type. 132 00:05:25,010 --> 00:05:27,577 And what does this int type stand for? 133 00:05:27,577 --> 00:05:29,660 What kind of number are we going to store in here? 134 00:05:29,660 --> 00:05:30,980 Could I ask somebody from down here? 135 00:05:30,980 --> 00:05:31,580 Yeah, go ahead. 136 00:05:31,580 --> 00:05:32,413 STUDENT: An integer. 137 00:05:32,413 --> 00:05:34,580 CARTER ZENKE: An integer, so a whole number, right? 138 00:05:34,580 --> 00:05:37,310 Then we have the value, in this case, 4, that value we're 139 00:05:37,310 --> 00:05:38,990 putting inside this variable here. 140 00:05:38,990 --> 00:05:43,460 But there's one piece we're missing, we haven't talked about yet. 141 00:05:43,460 --> 00:05:45,420 What haven't we named here yet? 142 00:05:45,420 --> 00:05:47,670 Could I ask somebody from maybe this side of the room? 143 00:05:47,670 --> 00:05:49,370 Take a guess? 144 00:05:49,370 --> 00:05:52,940 We talked about the name of the variable, the type, the value. 145 00:05:52,940 --> 00:05:55,430 What syntax haven't we shown yet? 146 00:05:55,430 --> 00:05:56,150 Yeah, go ahead. 147 00:05:56,150 --> 00:05:58,040 STUDENT: [INAUDIBLE] semicolon [INAUDIBLE].. 148 00:05:58,040 --> 00:05:59,498 CARTER ZENKE: The semicolon, right? 149 00:05:59,498 --> 00:06:03,200 So this is a statement in C. And every closing statement that we have, 150 00:06:03,200 --> 00:06:05,130 we want to include a semicolon there. 151 00:06:05,130 --> 00:06:07,130 The other thing we haven't talked about, the one 152 00:06:07,130 --> 00:06:08,390 piece we haven't talked about here, could I 153 00:06:08,390 --> 00:06:10,220 ask somebody from down here now? 154 00:06:10,220 --> 00:06:13,100 We have the semicolon, the value, the name, the data type. 155 00:06:13,100 --> 00:06:14,000 One more thing, yeah. 156 00:06:14,000 --> 00:06:14,660 STUDENT: The equals sign. 157 00:06:14,660 --> 00:06:17,810 CARTER ZENKE: The equals sign, and is it an equals sign, could I ask you? 158 00:06:17,810 --> 00:06:18,710 No, you're shaking your head. 159 00:06:18,710 --> 00:06:19,100 STUDENT: It's and assignment. 160 00:06:19,100 --> 00:06:21,142 CARTER ZENKE: It's an assignment operator, right? 161 00:06:21,142 --> 00:06:24,290 So we'll say this is going to assign the value 4 to the space we've 162 00:06:24,290 --> 00:06:26,150 created for this variable named call. 163 00:06:26,150 --> 00:06:28,010 So if you say this in English here, we're 164 00:06:28,010 --> 00:06:30,540 going to create an integer variable. 165 00:06:30,540 --> 00:06:32,540 Notice how this type aligns with this type here. 166 00:06:32,540 --> 00:06:34,820 Named calls, the name lines up here. 167 00:06:34,820 --> 00:06:39,240 That gets, or that kind of stores this value 4, in this instance. 168 00:06:39,240 --> 00:06:41,360 So let's try again with another value here. 169 00:06:41,360 --> 00:06:47,300 Int x equals sign 50, and let's actually say this one all together, if we could. 170 00:06:47,300 --> 00:06:49,430 How do we say we create a what? 171 00:06:49,430 --> 00:06:50,150 STUDENT: Integer. 172 00:06:50,150 --> 00:06:52,025 CARTER ZENKE: (CLASS REPEATING AFTER) Integer 173 00:06:52,025 --> 00:06:56,190 that called x that gets the value 50. 174 00:06:56,190 --> 00:06:56,690 Nice. 175 00:06:56,690 --> 00:06:58,470 That's what it would be in English here. 176 00:06:58,470 --> 00:06:58,970 Amazing. 177 00:06:58,970 --> 00:07:01,280 Yeah, high fives all around. 178 00:07:01,280 --> 00:07:04,888 So why does C care so much about data types though? 179 00:07:04,888 --> 00:07:07,430 The very first thing we say here is not the name of the error 180 00:07:07,430 --> 00:07:09,870 but actually the data type. 181 00:07:09,870 --> 00:07:12,800 So we saw briefly this idea in lecture, but let's 182 00:07:12,800 --> 00:07:14,690 go ahead and talk to the person next to you. 183 00:07:14,690 --> 00:07:17,545 Why do you think C cares so much about these data types? 184 00:07:17,545 --> 00:07:19,670 Why is it the first thing we tell the computer when 185 00:07:19,670 --> 00:07:20,795 we make this variable here? 186 00:07:20,795 --> 00:07:22,940 We'll come back in just a minute. 187 00:07:22,940 --> 00:07:27,230 OK, let's come back and hear some ideas for why does C 188 00:07:27,230 --> 00:07:29,240 care so much about data types? 189 00:07:29,240 --> 00:07:32,990 Why is it the very first thing we put in a given line of code 190 00:07:32,990 --> 00:07:35,460 to make a variable in this case? 191 00:07:35,460 --> 00:07:38,060 And can we go back to maybe this side of the room now. 192 00:07:38,060 --> 00:07:40,430 And anyone in particular like to share what 193 00:07:40,430 --> 00:07:44,580 your group talked about, what ideas you had for this question here? 194 00:07:44,580 --> 00:07:45,900 Yeah, go ahead. 195 00:07:45,900 --> 00:07:48,660 STUDENT: Basically, we said that C wants to know data type. 196 00:07:48,660 --> 00:07:51,670 That's in order restrict the values that we input. 197 00:07:51,670 --> 00:07:54,685 For instance, if we're talking about [INAUDIBLE] input 198 00:07:54,685 --> 00:07:56,610 3.5, so for [INAUDIBLE]. 199 00:07:56,610 --> 00:07:58,257 So it limits [INAUDIBLE]. 200 00:07:58,257 --> 00:08:00,840 CARTER ZENKE: Yeah, there's some value for specificity, right? 201 00:08:00,840 --> 00:08:02,880 I want to be able to make sure that I'm not 202 00:08:02,880 --> 00:08:06,240 putting in some value I couldn't store in this variable, like a text, 203 00:08:06,240 --> 00:08:07,620 for example. 204 00:08:07,620 --> 00:08:08,610 Other reasons too. 205 00:08:08,610 --> 00:08:10,230 Could we go to maybe down here? 206 00:08:10,230 --> 00:08:11,085 Yeah. 207 00:08:11,085 --> 00:08:13,210 STUDENT: The computer needs to know how much memory 208 00:08:13,210 --> 00:08:15,940 to allocate to the [INAUDIBLE]. 209 00:08:15,940 --> 00:08:18,965 CARTER ZENKE: Yeah, and why would that matter? 210 00:08:18,965 --> 00:08:20,590 Can I ask you, get a little deeper too? 211 00:08:20,590 --> 00:08:21,850 Why does this size matter? 212 00:08:21,850 --> 00:08:27,160 STUDENT: Because I think an integer is a lot less size than-- 213 00:08:27,160 --> 00:08:29,800 a lot less [INAUDIBLE]. 214 00:08:29,800 --> 00:08:31,180 CARTER ZENKE: Totally, so using-- 215 00:08:31,180 --> 00:08:33,815 or storing different values makes that value take away 216 00:08:33,815 --> 00:08:35,440 different space in a computer's memory. 217 00:08:35,440 --> 00:08:38,710 So integers take up maybe 32 bits, but a character 218 00:08:38,710 --> 00:08:40,115 might take up only eight bits. 219 00:08:40,115 --> 00:08:41,990 And so it tells the compiler how much memory, 220 00:08:41,990 --> 00:08:44,200 how much space to reserve here too. 221 00:08:44,200 --> 00:08:47,560 One other reason that we saw a little bit in lecture as well is we 222 00:08:47,560 --> 00:08:52,870 have some binary here, these eight bits that can represent a certain, well, 223 00:08:52,870 --> 00:08:55,260 really anything. 224 00:08:55,260 --> 00:08:56,580 What does this represent here? 225 00:08:56,580 --> 00:08:59,050 Anyone know off the top of their head? 226 00:08:59,050 --> 00:08:59,670 Yeah. 227 00:08:59,670 --> 00:09:00,740 STUDENT: No, sorry. 228 00:09:00,740 --> 00:09:02,085 CARTER ZENKE: Oh, no, OK. 229 00:09:02,085 --> 00:09:03,210 I'd be surprised if you do. 230 00:09:03,210 --> 00:09:08,822 But one thing this represents is the number or the integer 65, right? 231 00:09:08,822 --> 00:09:10,530 But as we saw in lecture a little bit too 232 00:09:10,530 --> 00:09:14,880 is this can also represent the letter A. Those same binary bits can represent 233 00:09:14,880 --> 00:09:18,150 both 65 and this character A. So it's important we 234 00:09:18,150 --> 00:09:22,210 tell C which thing we're talking about here by giving us our data types. 235 00:09:22,210 --> 00:09:24,690 So that's the reason we have these data types here. 236 00:09:24,690 --> 00:09:28,860 If we want to go further into variables, we can update them if we'd like. 237 00:09:28,860 --> 00:09:34,230 And here we have on this very first line the declaration and initialization 238 00:09:34,230 --> 00:09:35,610 of this variable calls. 239 00:09:35,610 --> 00:09:38,260 And down below we just update that value. 240 00:09:38,260 --> 00:09:41,922 So notice the type is on that top line, but it's not on that next line. 241 00:09:41,922 --> 00:09:44,880 And why would that be, if I can ask somebody from this side of the room 242 00:09:44,880 --> 00:09:45,510 now? 243 00:09:45,510 --> 00:09:50,360 Why is it on that top line but not that second line? 244 00:09:50,360 --> 00:09:52,225 Yeah, go ahead. 245 00:09:52,225 --> 00:09:54,600 STUDENT: On the first line you're initiating the variable 246 00:09:54,600 --> 00:09:58,160 and telling the computer what the data type is. 247 00:09:58,160 --> 00:10:01,160 On the second line, you're reassigning it to the new value, 248 00:10:01,160 --> 00:10:03,458 and it already knows that that variable [INAUDIBLE].. 249 00:10:03,458 --> 00:10:06,500 CARTER ZENKE: Yeah, so the computer is pretty good at remembering things, 250 00:10:06,500 --> 00:10:07,000 right? 251 00:10:07,000 --> 00:10:10,010 And once you've told it that this variable has a certain type, no need 252 00:10:10,010 --> 00:10:10,550 to tell it again. 253 00:10:10,550 --> 00:10:11,540 It already knows that. 254 00:10:11,540 --> 00:10:13,680 We can just keep going as we go. 255 00:10:13,680 --> 00:10:16,880 So here we're going to initialize and declare the variable calls. 256 00:10:16,880 --> 00:10:20,870 But when we use it again, we don't have to tell it the type as we go through. 257 00:10:20,870 --> 00:10:23,630 Now, if we wanted to change that value, we could do it like this. 258 00:10:23,630 --> 00:10:25,730 We also have some operators at our disposal. 259 00:10:25,730 --> 00:10:28,820 We could add 1 to it using this plus sign. 260 00:10:28,820 --> 00:10:31,070 We could even subtract some numbers. 261 00:10:31,070 --> 00:10:34,340 We could maybe multiply some numbers using that star operator. 262 00:10:34,340 --> 00:10:36,360 We could even divide some numbers, and so on. 263 00:10:36,360 --> 00:10:37,610 So feel free to take a look at those. 264 00:10:37,610 --> 00:10:40,360 If you go through the course, I'm sure these come top of your head 265 00:10:40,360 --> 00:10:41,220 as we go through. 266 00:10:41,220 --> 00:10:47,240 But when it comes to assigning some variable, what looks like a function, 267 00:10:47,240 --> 00:10:49,357 right, that's where things get a little bit weird. 268 00:10:49,357 --> 00:10:52,190 We saw in lecture we're trying to get some input from a user, right? 269 00:10:52,190 --> 00:10:56,870 And we didn't say that maybe string name equals quote "Carter." 270 00:10:56,870 --> 00:11:00,200 We said it equals this, maybe, function, get string. 271 00:11:00,200 --> 00:11:03,290 Or here, if we wanted to store a value in calls, get int. 272 00:11:03,290 --> 00:11:06,560 And in this case, before we're reading left to right. 273 00:11:06,560 --> 00:11:09,210 But in this case, it's actually better to read right to left 274 00:11:09,210 --> 00:11:12,120 when you see this function call on the right-hand side. 275 00:11:12,120 --> 00:11:14,120 So what's happening here is, when you have 276 00:11:14,120 --> 00:11:16,245 this function on the right-hand side, this function 277 00:11:16,245 --> 00:11:19,370 is wanting to run and then give us back some return 278 00:11:19,370 --> 00:11:23,100 value that will be stored inside this variable on the left-hand side. 279 00:11:23,100 --> 00:11:26,300 So before anything else, we run this function on the right-hand side 280 00:11:26,300 --> 00:11:29,720 with some certain arguments, some input to that function 281 00:11:29,720 --> 00:11:31,550 tell it what exactly to do. 282 00:11:31,550 --> 00:11:32,840 That function runs. 283 00:11:32,840 --> 00:11:35,760 Maybe it asks the user for this integer number here. 284 00:11:35,760 --> 00:11:40,110 And we then store that value right where that function was called. 285 00:11:40,110 --> 00:11:42,660 And it then goes into that variable. 286 00:11:42,660 --> 00:11:46,290 And now we're back to simple variable assignment here. 287 00:11:46,290 --> 00:11:50,682 So usually, if you're simply assigning numbers, you can read left to right. 288 00:11:50,682 --> 00:11:53,390 But if you have these function calls, best to read right to left, 289 00:11:53,390 --> 00:11:56,390 and knowing that your function runs first and then assigns that value 290 00:11:56,390 --> 00:11:58,110 as we go through. 291 00:11:58,110 --> 00:12:00,335 So questions on this before we dive even deeper. 292 00:12:00,335 --> 00:12:10,640 293 00:12:10,640 --> 00:12:12,510 Seeing none. 294 00:12:12,510 --> 00:12:16,160 So now that we have these values stored in these variables, 295 00:12:16,160 --> 00:12:19,160 it's probably worthwhile to think about how we print them to the screen. 296 00:12:19,160 --> 00:12:21,590 And here's what we saw a little bit in lecture as well, 297 00:12:21,590 --> 00:12:26,340 this % and then this i here. 298 00:12:26,340 --> 00:12:28,310 And what is this doing for us? 299 00:12:28,310 --> 00:12:30,695 Could I ask somebody from this middle section here. 300 00:12:30,695 --> 00:12:32,570 Let's go to the first row, if you don't mind. 301 00:12:32,570 --> 00:12:33,070 Yeah. 302 00:12:33,070 --> 00:12:35,732 STUDENT: It's a placeholder for our variable call. 303 00:12:35,732 --> 00:12:37,690 CARTER ZENKE: Yeah, placeholder for your calls. 304 00:12:37,690 --> 00:12:39,120 And why is it %i? 305 00:12:39,120 --> 00:12:39,620 Do you know? 306 00:12:39,620 --> 00:12:41,968 STUDENT: Because it's an integer [INAUDIBLE].. 307 00:12:41,968 --> 00:12:42,760 CARTER ZENKE: Yeah. 308 00:12:42,760 --> 00:12:44,630 STUDENT: i represents [INAUDIBLE]. 309 00:12:44,630 --> 00:12:46,010 CARTER ZENKE: Right, so %i. 310 00:12:46,010 --> 00:12:48,440 This % is kind of saying like, hey, this is a placeholder. 311 00:12:48,440 --> 00:12:50,810 And that i specifies what kind of variable 312 00:12:50,810 --> 00:12:52,830 can go inside of that placeholder there. 313 00:12:52,830 --> 00:12:55,460 So because call is an integer, it's %i. 314 00:12:55,460 --> 00:12:58,670 And that's called, more specifically a format code, as we might have said. 315 00:12:58,670 --> 00:13:01,520 And we have this value that we can store in that format code there. 316 00:13:01,520 --> 00:13:04,817 We could have multiple format codes and multiple values. 317 00:13:04,817 --> 00:13:06,150 We just separate them by commas. 318 00:13:06,150 --> 00:13:11,060 So you can imagine, for example, I have %i here and maybe %i later on. 319 00:13:11,060 --> 00:13:13,860 I could have calls and then comma, some other variable. 320 00:13:13,860 --> 00:13:15,980 And those would go inside those placeholders one 321 00:13:15,980 --> 00:13:21,020 by one, aligning with whatever order I put them in as we go through. 322 00:13:21,020 --> 00:13:24,590 Now, it's not worth memorizing any of these, 323 00:13:24,590 --> 00:13:27,725 but there are a great number of format codes for different data types. 324 00:13:27,725 --> 00:13:29,600 And as you work with a variety of data types, 325 00:13:29,600 --> 00:13:31,640 best you can maybe reference these once in a while. 326 00:13:31,640 --> 00:13:33,110 So here on the left-hand side, we have number 327 00:13:33,110 --> 00:13:35,690 format codes, like ints and longs, floats and doubles, 328 00:13:35,690 --> 00:13:38,420 and the right-hand side some chars and strings, 329 00:13:38,420 --> 00:13:40,100 where chars are individual characters. 330 00:13:40,100 --> 00:13:43,770 And strings are collections of characters as we go through. 331 00:13:43,770 --> 00:13:49,440 One thing we haven't quite seen as much yet is this long and double. 332 00:13:49,440 --> 00:13:54,100 Anyone want to hazard a guess as to what that might be here? 333 00:13:54,100 --> 00:13:56,620 Could I ask somebody from this middle row again? 334 00:13:56,620 --> 00:13:59,145 335 00:13:59,145 --> 00:14:00,895 What is this long and double doing for us? 336 00:14:00,895 --> 00:14:05,710 337 00:14:05,710 --> 00:14:06,400 Yeah, go ahead. 338 00:14:06,400 --> 00:14:11,834 STUDENT: Does the long [INAUDIBLE] more room [INAUDIBLE] 339 00:14:11,834 --> 00:14:16,023 you have to have the long to [INAUDIBLE]?? 340 00:14:16,023 --> 00:14:17,190 CARTER ZENKE: Yeah, exactly. 341 00:14:17,190 --> 00:14:22,242 So here we had an integer that could store up to four billion values. 342 00:14:22,242 --> 00:14:23,950 But when that's not enough, we might want 343 00:14:23,950 --> 00:14:27,150 to have a longer value, in this case, called a long, 344 00:14:27,150 --> 00:14:29,410 that will have twice as much space for us. 345 00:14:29,410 --> 00:14:32,322 We can store twice as many different combinations as we go through. 346 00:14:32,322 --> 00:14:33,280 Really, more than that. 347 00:14:33,280 --> 00:14:36,310 We're just having twice as many bits to represent that information. 348 00:14:36,310 --> 00:14:39,330 And similarly, for the float, that's a decimal number. 349 00:14:39,330 --> 00:14:43,030 But this double is called a double precision floating point. 350 00:14:43,030 --> 00:14:46,260 So we'd have twice as many decimals, twice many binary digits 351 00:14:46,260 --> 00:14:48,630 representing that decimal number there for us 352 00:14:48,630 --> 00:14:52,610 as well, so just more space for us to use as we go through. 353 00:14:52,610 --> 00:14:54,740 Now, what we'll do here is a brief exercise 354 00:14:54,740 --> 00:15:01,107 and invite you to load up code.cs50.io on your own laptop here. 355 00:15:01,107 --> 00:15:03,440 And you should see something that looks a bit like this. 356 00:15:03,440 --> 00:15:06,125 Maybe you have a File Explorer on the left-hand side. 357 00:15:06,125 --> 00:15:11,083 358 00:15:11,083 --> 00:15:13,250 But you should certainly have a terminal down below. 359 00:15:13,250 --> 00:15:13,958 I see a question. 360 00:15:13,958 --> 00:15:14,753 Yeah. 361 00:15:14,753 --> 00:15:17,200 STUDENT: I'm not sure if this was a typo to be like this. 362 00:15:17,200 --> 00:15:18,778 But if you go back to the slide-- 363 00:15:18,778 --> 00:15:19,820 CARTER ZENKE: Yeah, yeah. 364 00:15:19,820 --> 00:15:22,950 365 00:15:22,950 --> 00:15:26,514 STUDENT: So the double says f, and the float says f. 366 00:15:26,514 --> 00:15:27,450 Is that correct? 367 00:15:27,450 --> 00:15:28,700 CARTER ZENKE: That is correct. 368 00:15:28,700 --> 00:15:31,770 So both the float and the double have the same format code, yeah. 369 00:15:31,770 --> 00:15:32,550 Good question. 370 00:15:32,550 --> 00:15:35,847 STUDENT: So how does the computer [INAUDIBLE] 371 00:15:35,847 --> 00:15:38,508 recognize between [INAUDIBLE]? 372 00:15:38,508 --> 00:15:40,800 CARTER ZENKE: How does the computer recognize each one? 373 00:15:40,800 --> 00:15:45,160 In this case, it doesn't quite matter because, in this case, 374 00:15:45,160 --> 00:15:47,580 the float and the double are both decimal numbers. 375 00:15:47,580 --> 00:15:51,390 And so the computer knows when it sees %f, I'm printing a decimal number. 376 00:15:51,390 --> 00:15:53,950 It just so happens to be that the double is twice as long. 377 00:15:53,950 --> 00:15:56,400 And so it just prints that many more-- 378 00:15:56,400 --> 00:15:59,310 well, it can print that many more spaces after the decimal point 379 00:15:59,310 --> 00:16:01,230 if you'd like it to. 380 00:16:01,230 --> 00:16:03,450 Yeah, other questions here before we move on? 381 00:16:03,450 --> 00:16:08,070 382 00:16:08,070 --> 00:16:10,950 All right, so we'll work on a brief exercise here. 383 00:16:10,950 --> 00:16:15,260 And once you've loaded up your IDE in code.cs50.io, 384 00:16:15,260 --> 00:16:18,380 you go ahead and create an application called phonebook.c. 385 00:16:18,380 --> 00:16:21,800 And the goal is to prompt the user for these three things 386 00:16:21,800 --> 00:16:24,680 and then print them back out to the user as confirmation 387 00:16:24,680 --> 00:16:27,540 that this data is stored in your program. 388 00:16:27,540 --> 00:16:32,810 So if I wanted to do this, I would first go down to my terminal down below. 389 00:16:32,810 --> 00:16:35,225 And I would make a new file. 390 00:16:35,225 --> 00:16:37,100 What would I do to make a new file down here? 391 00:16:37,100 --> 00:16:40,730 Could I ask somebody from maybe this right-hand bottom side? 392 00:16:40,730 --> 00:16:44,720 How can I make a new file in this program here? 393 00:16:44,720 --> 00:16:45,290 Yeah. 394 00:16:45,290 --> 00:16:46,838 STUDENT: Code and file name. 395 00:16:46,838 --> 00:16:48,630 CARTER ZENKE: Yeah, code and the file name. 396 00:16:48,630 --> 00:16:51,860 So I could do code, if it will load here. 397 00:16:51,860 --> 00:16:54,510 398 00:16:54,510 --> 00:16:55,160 Let me refresh. 399 00:16:55,160 --> 00:17:00,270 But on your own, you could certainly do code and in the file name here. 400 00:17:00,270 --> 00:17:04,520 And for our purposes, I'll do code space maybe phonebook.c. 401 00:17:04,520 --> 00:17:09,123 But you could call your program whatever else you'd like to call it here. 402 00:17:09,123 --> 00:17:10,790 And maybe yours is doing the same thing. 403 00:17:10,790 --> 00:17:12,290 We'll be back in just a minute here. 404 00:17:12,290 --> 00:17:17,200 405 00:17:17,200 --> 00:17:22,008 Now, once you have your file, what tends to go at the very top of that file? 406 00:17:22,008 --> 00:17:23,550 What's the first thing you might add? 407 00:17:23,550 --> 00:17:25,560 We saw this in lecture. 408 00:17:25,560 --> 00:17:26,280 Yeah, go ahead. 409 00:17:26,280 --> 00:17:27,000 STUDENT: The header. 410 00:17:27,000 --> 00:17:29,375 CARTER ZENKE: The header files or these libraries, right? 411 00:17:29,375 --> 00:17:32,150 And do you remember the syntax for that? 412 00:17:32,150 --> 00:17:37,330 STUDENT: Hashtag include, and then the caret thing. 413 00:17:37,330 --> 00:17:41,080 CARTER ZENKE: Yeah, hashtag include this little caret thing. 414 00:17:41,080 --> 00:17:42,700 Let's see if this one is loaded now. 415 00:17:42,700 --> 00:17:46,030 Not quite, but this hashtag include is saying, 416 00:17:46,030 --> 00:17:49,690 I'm going to try to get some file in my computer's memory 417 00:17:49,690 --> 00:17:54,130 that has these functions declared for me that I will then use in my own program 418 00:17:54,130 --> 00:17:54,830 here. 419 00:17:54,830 --> 00:18:01,990 So what two header files might you want to include in your program here? 420 00:18:01,990 --> 00:18:02,590 Yeah. 421 00:18:02,590 --> 00:18:04,592 STUDENT: Standardio.h [INAUDIBLE]. 422 00:18:04,592 --> 00:18:07,300 CARTER ZENKE: Nice, so two off the bat that you often want to use 423 00:18:07,300 --> 00:18:11,440 are standardio.h or stdio.h cs50.h. 424 00:18:11,440 --> 00:18:13,720 Both of these have some pretty common functions 425 00:18:13,720 --> 00:18:15,880 you'll be using as you write your own program. 426 00:18:15,880 --> 00:18:22,030 427 00:18:22,030 --> 00:18:26,770 And we'll wait for this to continue connecting, and what we'll actually do 428 00:18:26,770 --> 00:18:29,770 is, now that you maybe hopefully have your file open, 429 00:18:29,770 --> 00:18:33,340 you have your header files at the top, we'll take some time, 430 00:18:33,340 --> 00:18:35,670 let's say maybe 10 minutes, to work on this on our own. 431 00:18:35,670 --> 00:18:36,820 I'll put the slide back up. 432 00:18:36,820 --> 00:18:39,070 And we'll come back in those 10 minutes and share 433 00:18:39,070 --> 00:18:40,510 how you approached this problem. 434 00:18:40,510 --> 00:18:43,000 Maybe if the staff would like to run around and help you as you work. 435 00:18:43,000 --> 00:18:44,208 Feel free to raise your hand. 436 00:18:44,208 --> 00:18:46,070 We'll come around and help you out. 437 00:18:46,070 --> 00:18:47,980 All right, let's come back. 438 00:18:47,980 --> 00:18:51,148 And we'll be working in a slightly different environment 439 00:18:51,148 --> 00:18:53,690 just because the internet is not quite what we want it to be. 440 00:18:53,690 --> 00:18:56,773 But we're going to make here our own phone book file where we can actually 441 00:18:56,773 --> 00:18:58,310 store the data we want to store. 442 00:18:58,310 --> 00:19:00,545 So if you remember our slide back here, we 443 00:19:00,545 --> 00:19:02,170 wanted to store a few different things. 444 00:19:02,170 --> 00:19:07,630 You wanted to store the user's name, their phone number, 445 00:19:07,630 --> 00:19:09,460 and I think it was their address. 446 00:19:09,460 --> 00:19:14,980 Let me actually take a look at that slide again, if we go back over here. 447 00:19:14,980 --> 00:19:17,390 A name, an age, and a phone number. 448 00:19:17,390 --> 00:19:21,550 So here we have this template for our program, 449 00:19:21,550 --> 00:19:23,650 but what are we missing right now? 450 00:19:23,650 --> 00:19:25,720 We have our header files, these libraries where 451 00:19:25,720 --> 00:19:27,220 I have functions we're going to use. 452 00:19:27,220 --> 00:19:29,665 What else are we missing in this program right here? 453 00:19:29,665 --> 00:19:30,790 Could I ask somebody from-- 454 00:19:30,790 --> 00:19:31,450 Yeah, down here. 455 00:19:31,450 --> 00:19:32,350 STUDENT: Int main void. 456 00:19:32,350 --> 00:19:33,808 CARTER ZENKE: Int main void, right. 457 00:19:33,808 --> 00:19:37,330 So I'll type int main and void up here. 458 00:19:37,330 --> 00:19:39,340 And this just symbolizes-- this is the kind 459 00:19:39,340 --> 00:19:41,650 of when flag clicked block in Scratch. 460 00:19:41,650 --> 00:19:43,900 This is going to be the main part of our program here. 461 00:19:43,900 --> 00:19:46,330 We're defining this new function called Main that 462 00:19:46,330 --> 00:19:49,160 will be the main part of our program. 463 00:19:49,160 --> 00:19:51,340 Now, the first thing we want to do is probably 464 00:19:51,340 --> 00:19:54,200 prompt the user for some information. 465 00:19:54,200 --> 00:20:00,100 So what functions did you all use to get the user's name, or that contact's name 466 00:20:00,100 --> 00:20:01,420 we're trying to story here? 467 00:20:01,420 --> 00:20:02,020 Could I ask over here? 468 00:20:02,020 --> 00:20:02,650 Yeah, go ahead. 469 00:20:02,650 --> 00:20:03,400 STUDENT: Get string. 470 00:20:03,400 --> 00:20:04,442 CARTER ZENKE: Get string. 471 00:20:04,442 --> 00:20:07,855 And how did you make your variable in this case? 472 00:20:07,855 --> 00:20:11,450 STUDENT: So because our variable is a string, I put string. 473 00:20:11,450 --> 00:20:12,928 And then I titled my variable name. 474 00:20:12,928 --> 00:20:13,720 CARTER ZENKE: Nice. 475 00:20:13,720 --> 00:20:18,958 STUDENT: And then I said equals get string what's your name. 476 00:20:18,958 --> 00:20:20,750 CARTER ZENKE: Get string, what's your name. 477 00:20:20,750 --> 00:20:21,708 So something like this? 478 00:20:21,708 --> 00:20:23,320 STUDENT: With a space after the name. 479 00:20:23,320 --> 00:20:25,820 CARTER ZENKE: Yeah, and why would you have that space there? 480 00:20:25,820 --> 00:20:26,540 STUDENT: Just for readability. 481 00:20:26,540 --> 00:20:27,240 CARTER ZENKE: Just readability, right? 482 00:20:27,240 --> 00:20:29,900 If we don't have that space there, if we maybe remove this, 483 00:20:29,900 --> 00:20:31,970 the user will be typing in their name right up 484 00:20:31,970 --> 00:20:33,887 against that question mark there, which is not 485 00:20:33,887 --> 00:20:36,000 what we want to have happen here. 486 00:20:36,000 --> 00:20:39,080 So it seems pretty logical to represent a name with a string. 487 00:20:39,080 --> 00:20:40,873 But how about the age here? 488 00:20:40,873 --> 00:20:42,540 Can I go maybe to this side of the room? 489 00:20:42,540 --> 00:20:47,840 How did you all work on getting the users the contact's age? 490 00:20:47,840 --> 00:20:48,440 Yeah. 491 00:20:48,440 --> 00:20:57,780 STUDENT: I did int age equals get int, and then in brackets I had-- 492 00:20:57,780 --> 00:21:00,450 CARTER ZENKE: What's your age with a space, right? 493 00:21:00,450 --> 00:21:02,980 And then the semicolon. 494 00:21:02,980 --> 00:21:03,480 Perfect. 495 00:21:03,480 --> 00:21:05,200 So that'll close that statement. 496 00:21:05,200 --> 00:21:07,800 And again, this function on the right will be called. 497 00:21:07,800 --> 00:21:10,590 It will run and then give us back some return value, which is 498 00:21:10,590 --> 00:21:15,900 whatever the user typed in, and store it inside of this variable named age. 499 00:21:15,900 --> 00:21:17,460 And then comes one with some-- 500 00:21:17,460 --> 00:21:19,300 question here? 501 00:21:19,300 --> 00:21:27,280 STUDENT: Can we also write string age [INAUDIBLE]?? 502 00:21:27,280 --> 00:21:29,906 CARTER ZENKE: Yeah, so did you try that one here? 503 00:21:29,906 --> 00:21:34,283 STUDENT: Yeah, what's the [INAUDIBLE]? 504 00:21:34,283 --> 00:21:35,450 CARTER ZENKE: Good question. 505 00:21:35,450 --> 00:21:38,330 So we could do either of these here. 506 00:21:38,330 --> 00:21:39,520 One is an integer. 507 00:21:39,520 --> 00:21:41,290 One is a string. 508 00:21:41,290 --> 00:21:45,140 But you could imagine, if I later wanted to do something like this, 509 00:21:45,140 --> 00:21:49,780 let's say I wanted to actually increase every person's age in my phone by one, 510 00:21:49,780 --> 00:21:51,520 maybe it's their birthday next year. 511 00:21:51,520 --> 00:21:56,107 So I say age equals age plus 1. 512 00:21:56,107 --> 00:21:58,190 Is that going to work with a string, do you think? 513 00:21:58,190 --> 00:21:58,930 STUDENT: No. 514 00:21:58,930 --> 00:22:04,750 CARTER ZENKE: No, because I'm going to add 1 to this quote unquote maybe 40, 515 00:22:04,750 --> 00:22:05,650 for example. 516 00:22:05,650 --> 00:22:07,713 And I can't add numbers to strings. 517 00:22:07,713 --> 00:22:09,130 That's not going to happen for me. 518 00:22:09,130 --> 00:22:11,800 So it's best when you're making these to just think 519 00:22:11,800 --> 00:22:15,650 about what you want to do with them as you work in your program here. 520 00:22:15,650 --> 00:22:18,340 So I want to maybe change my age as I go through. 521 00:22:18,340 --> 00:22:24,550 So I'll actually maybe make this an integer here and say get int instead. 522 00:22:24,550 --> 00:22:25,830 And now for the phone number. 523 00:22:25,830 --> 00:22:28,560 If you go to this middle section here, how did you all 524 00:22:28,560 --> 00:22:32,090 choose to represent the phone number? 525 00:22:32,090 --> 00:22:33,500 Yeah. 526 00:22:33,500 --> 00:22:36,670 STUDENT: Long number is equal to get long. 527 00:22:36,670 --> 00:22:39,610 CARTER ZENKE: Long number is equal to get long. 528 00:22:39,610 --> 00:22:41,485 And you'd ask maybe what's your phone number. 529 00:22:41,485 --> 00:22:42,710 STUDENT: Yeah. 530 00:22:42,710 --> 00:22:44,668 CARTER ZENKE: And I think I'm most curious here 531 00:22:44,668 --> 00:22:46,840 about why you chose a long. 532 00:22:46,840 --> 00:22:49,550 Why would you have that? 533 00:22:49,550 --> 00:22:50,540 Yeah. 534 00:22:50,540 --> 00:22:53,279 STUDENT: Because it's more than 2 billion, 535 00:22:53,279 --> 00:22:56,703 so you need the extra [INAUDIBLE]. 536 00:22:56,703 --> 00:22:58,620 CARTER ZENKE: Yeah, so a phone number is what? 537 00:22:58,620 --> 00:23:01,060 Maybe 10 digits long. 538 00:23:01,060 --> 00:23:05,580 And so if I had any phone number that begins with anything higher than two, 539 00:23:05,580 --> 00:23:07,447 that's going to be higher than two billion, 540 00:23:07,447 --> 00:23:10,030 which is the highest positive number I can have as an integer. 541 00:23:10,030 --> 00:23:11,760 So I want to actually have a long here. 542 00:23:11,760 --> 00:23:12,390 Yeah, question. 543 00:23:12,390 --> 00:23:14,360 STUDENT: Could you use a string to-- 544 00:23:14,360 --> 00:23:16,360 CARTER ZENKE: You absolutely could use a string. 545 00:23:16,360 --> 00:23:20,983 And why would maybe you advocate for using a string? 546 00:23:20,983 --> 00:23:24,280 STUDENT: [INAUDIBLE] 547 00:23:24,280 --> 00:23:26,140 CARTER ZENKE: Nice, so hyphens. 548 00:23:26,140 --> 00:23:30,130 You could also include, if you're maybe in different countries, 549 00:23:30,130 --> 00:23:34,510 plus 1 for this country code plus other country codes as well. 550 00:23:34,510 --> 00:23:37,990 You could even have parentheses if you'd like to put the area code in front. 551 00:23:37,990 --> 00:23:39,070 Yeah. 552 00:23:39,070 --> 00:23:44,110 STUDENT: Also, I think that if you use an integer or a long, 553 00:23:44,110 --> 00:23:48,960 you are supposed to be able to calculate stuff based on that number. 554 00:23:48,960 --> 00:23:52,630 And usually, you don't add stuff or divide the phone number. 555 00:23:52,630 --> 00:23:58,008 So there is no actual purpose to ask for a [INAUDIBLE].. 556 00:23:58,008 --> 00:23:59,300 CARTER ZENKE: Yeah, definitely. 557 00:23:59,300 --> 00:24:01,758 So we tend not to add things to phone numbers. 558 00:24:01,758 --> 00:24:04,550 So why store it as number we could add things to or subtract things 559 00:24:04,550 --> 00:24:06,500 to or divide or whatever we'd like, right? 560 00:24:06,500 --> 00:24:08,990 Good question there. 561 00:24:08,990 --> 00:24:10,760 So we have it as a string here. 562 00:24:10,760 --> 00:24:14,120 The string also preserves things like leading zeros. 563 00:24:14,120 --> 00:24:17,388 Maybe there's an odd chance that somebody has a 000 number, 564 00:24:17,388 --> 00:24:20,180 and we could store that with a string but not, for example, a long. 565 00:24:20,180 --> 00:24:23,750 We tend to remove those leading zeros in that case. 566 00:24:23,750 --> 00:24:24,770 Yeah. 567 00:24:24,770 --> 00:24:27,690 STUDENT: But [INAUDIBLE] to use the long [INAUDIBLE] one 568 00:24:27,690 --> 00:24:29,520 that doesn't really require that? 569 00:24:29,520 --> 00:24:31,860 CARTER ZENKE: If I were to say maybe long age here? 570 00:24:31,860 --> 00:24:32,970 STUDENT: Yeah. 571 00:24:32,970 --> 00:24:36,150 CARTER ZENKE: And maybe get long to have parity. 572 00:24:36,150 --> 00:24:38,280 Nothing bad would happen here. 573 00:24:38,280 --> 00:24:41,940 I'm probably just using twice as much space as I would need to. 574 00:24:41,940 --> 00:24:45,490 So generally, I don't think people have an age above two billion. 575 00:24:45,490 --> 00:24:49,380 And so I would probably want to just use a regular integer to store that value. 576 00:24:49,380 --> 00:24:51,420 Nice. 577 00:24:51,420 --> 00:24:55,230 Other questions on these representations of this information here? 578 00:24:55,230 --> 00:24:58,700 579 00:24:58,700 --> 00:25:00,920 OK, so we'll go with this integer. 580 00:25:00,920 --> 00:25:03,392 And then just real quickly to review our format code, 581 00:25:03,392 --> 00:25:05,600 so let's say I wanted to print out some of this data. 582 00:25:05,600 --> 00:25:07,160 I could say printf. 583 00:25:07,160 --> 00:25:11,100 And let's say, maybe age is-- 584 00:25:11,100 --> 00:25:14,090 what would I then say? 585 00:25:14,090 --> 00:25:15,590 Maybe just call it out. 586 00:25:15,590 --> 00:25:16,425 STUDENT: %i. 587 00:25:16,425 --> 00:25:17,300 CARTER ZENKE: %i, OK. 588 00:25:17,300 --> 00:25:18,050 Age is %i. 589 00:25:18,050 --> 00:25:20,720 I'm going to put period here. 590 00:25:20,720 --> 00:25:24,830 Name is %s. 591 00:25:24,830 --> 00:25:32,430 And let's go ahead and say phone number is %s, all right? 592 00:25:32,430 --> 00:25:34,500 And now to store this, as we saw earlier, 593 00:25:34,500 --> 00:25:37,587 I could simply just put these variables in the same order 594 00:25:37,587 --> 00:25:39,670 I want them to show up in those placeholders here. 595 00:25:39,670 --> 00:25:41,450 So I could say, OK, age is first. 596 00:25:41,450 --> 00:25:44,170 So I'll say age, then name. 597 00:25:44,170 --> 00:25:45,000 So I'll say name. 598 00:25:45,000 --> 00:25:47,880 And then finally number, so I'll say number and close everything out 599 00:25:47,880 --> 00:25:49,590 with the semicolon here. 600 00:25:49,590 --> 00:25:53,580 And I were to make this, I just type make phone book and run it. 601 00:25:53,580 --> 00:25:57,240 I would then see this information printed back out to me on the screen 602 00:25:57,240 --> 00:25:58,620 as such. 603 00:25:58,620 --> 00:26:00,945 So questions on this before we move on. 604 00:26:00,945 --> 00:26:04,510 605 00:26:04,510 --> 00:26:10,590 All right, so one of the other building blocks 606 00:26:10,590 --> 00:26:12,990 that we have besides these variables and representations 607 00:26:12,990 --> 00:26:14,853 here are these conditionals and these loops. 608 00:26:14,853 --> 00:26:16,770 And we saw these in lecture as well, but we'll 609 00:26:16,770 --> 00:26:19,780 use these now for the course's lab to work on together. 610 00:26:19,780 --> 00:26:24,180 So first thing here, we have this question. 611 00:26:24,180 --> 00:26:27,600 Have I called less than one time? 612 00:26:27,600 --> 00:26:32,280 And if I have, then I'll say call more often. 613 00:26:32,280 --> 00:26:34,500 But just to get some vocabulary down here, 614 00:26:34,500 --> 00:26:38,880 this on the inside of that conditional is our Boolean expression. 615 00:26:38,880 --> 00:26:40,170 It's either yes or no. 616 00:26:40,170 --> 00:26:43,750 Have I called less than one time or not? 617 00:26:43,750 --> 00:26:46,100 On the outside here is that conditional itself. 618 00:26:46,100 --> 00:26:49,550 So conditionals have these Boolean expressions on the inside of them. 619 00:26:49,550 --> 00:26:51,550 I could make this a little more advanced though, 620 00:26:51,550 --> 00:26:55,970 and I could have this else in the middle. 621 00:26:55,970 --> 00:26:58,540 And what would that do for me? 622 00:26:58,540 --> 00:27:02,260 Could I ever execute these two things at the same time? 623 00:27:02,260 --> 00:27:03,930 Could I ask over here? 624 00:27:03,930 --> 00:27:06,025 Could I ever do these two things at the same time? 625 00:27:06,025 --> 00:27:06,900 Shaking your head no. 626 00:27:06,900 --> 00:27:07,230 Right. 627 00:27:07,230 --> 00:27:09,170 So these two things are mutually exclusive. 628 00:27:09,170 --> 00:27:12,780 If I have this else here, I'm saying it's either one or the other. 629 00:27:12,780 --> 00:27:15,450 If calls is less than 1, I will do that first thing. 630 00:27:15,450 --> 00:27:18,420 If it's not, I will do that second thing. 631 00:27:18,420 --> 00:27:21,660 You can chain these statements together if you saw-- even in Scratch. 632 00:27:21,660 --> 00:27:25,140 You could say if and then else if and then else if, 633 00:27:25,140 --> 00:27:26,400 and finally, it's long else. 634 00:27:26,400 --> 00:27:30,480 In general, we have these if statements followed by these else 635 00:27:30,480 --> 00:27:33,870 if statements, followed by if you need them, that final else to say, 636 00:27:33,870 --> 00:27:37,815 like a catch all for everything else that we could happen have happen here. 637 00:27:37,815 --> 00:27:40,440 For loops though, if you wanted to do something multiple times, 638 00:27:40,440 --> 00:27:43,300 we have something that looks a bit like this, this while loop. 639 00:27:43,300 --> 00:27:45,450 And what would this print to the screen? 640 00:27:45,450 --> 00:27:47,760 Could I ask somebody from maybe this side of the room? 641 00:27:47,760 --> 00:27:49,968 What would you see on the screen with this loop here? 642 00:27:49,968 --> 00:27:56,110 643 00:27:56,110 --> 00:27:56,740 Yeah, go ahead. 644 00:27:56,740 --> 00:27:59,660 STUDENT: 0, 1, 2, 3 up to 9. 645 00:27:59,660 --> 00:28:01,410 CARTER ZENKE: Up to 9, and why not the 10? 646 00:28:01,410 --> 00:28:03,598 STUDENT: Because it's while i is less than 10. 647 00:28:03,598 --> 00:28:04,390 CARTER ZENKE: Nice. 648 00:28:04,390 --> 00:28:07,660 Yeah, so we wouldn't see the 10 because we're going to stop while-- 649 00:28:07,660 --> 00:28:11,840 we're going to stop if this is not true. 650 00:28:11,840 --> 00:28:14,650 And so 10 is not less than 10, so we would stop 651 00:28:14,650 --> 00:28:17,560 and we wouldn't do the code on the inside, right? 652 00:28:17,560 --> 00:28:21,500 So to break things down even further, we have our initialization of this loop. 653 00:28:21,500 --> 00:28:23,560 I is first set to 0. 654 00:28:23,560 --> 00:28:25,303 Then we have our Boolean expression, this 655 00:28:25,303 --> 00:28:27,220 question we're going to ask, either yes or no. 656 00:28:27,220 --> 00:28:29,000 Is this case true or is it not? 657 00:28:29,000 --> 00:28:33,460 And then finally, this implementation, changing that value as we go through. 658 00:28:33,460 --> 00:28:36,010 And finally, when our Boolean expression is no longer true, 659 00:28:36,010 --> 00:28:38,865 we will not be inside of that loop there. 660 00:28:38,865 --> 00:28:39,740 This is really handy. 661 00:28:39,740 --> 00:28:43,150 We can actually try to make our code run a certain number of times. 662 00:28:43,150 --> 00:28:46,270 And this is so handy that C actually has our own syntax 663 00:28:46,270 --> 00:28:48,950 to write this kind of loop in even abbreviated format. 664 00:28:48,950 --> 00:28:53,770 So here we call a for loop that has all those same elements but now just 665 00:28:53,770 --> 00:28:54,910 on one clean line. 666 00:28:54,910 --> 00:28:58,670 We have this initialization, this Boolean expression, 667 00:28:58,670 --> 00:29:00,910 and then this incrementation here. 668 00:29:00,910 --> 00:29:03,160 And we'll do whatever a piece of code is on the inside 669 00:29:03,160 --> 00:29:06,640 as long as this Boolean expression is true. 670 00:29:06,640 --> 00:29:11,070 So questions on this kind of syntax for these loops? 671 00:29:11,070 --> 00:29:12,775 Pretty good. 672 00:29:12,775 --> 00:29:15,150 And finally, I just wanted to get a little more advanced. 673 00:29:15,150 --> 00:29:16,440 We have this-- yeah, go ahead. 674 00:29:16,440 --> 00:29:19,290 STUDENT: I have a question [INAUDIBLE]. 675 00:29:19,290 --> 00:29:25,193 Why int [INAUDIBLE] parentheses [INAUDIBLE]?? 676 00:29:25,193 --> 00:29:26,360 CARTER ZENKE: Good question. 677 00:29:26,360 --> 00:29:27,360 So you saw the int here? 678 00:29:27,360 --> 00:29:28,180 STUDENT: Yeah. 679 00:29:28,180 --> 00:29:29,972 CARTER ZENKE: And why is that there, right, 680 00:29:29,972 --> 00:29:34,540 because previously, we had the int i already done for us, right? 681 00:29:34,540 --> 00:29:37,280 So these are actually two different contexts. 682 00:29:37,280 --> 00:29:41,230 So here, I have created this variable named i, 683 00:29:41,230 --> 00:29:43,570 and I'm going to use it inside of this loop. 684 00:29:43,570 --> 00:29:46,900 And this variable i, because it's outside of this while loop, 685 00:29:46,900 --> 00:29:48,940 will persist as we go through my program. 686 00:29:48,940 --> 00:29:51,430 So I could use this variable i again. 687 00:29:51,430 --> 00:29:56,200 In the for loop though, if I were to create this variable i anew, 688 00:29:56,200 --> 00:29:58,900 I can only use it inside this for loop. 689 00:29:58,900 --> 00:30:01,340 I could not use it outside of that for loop. 690 00:30:01,340 --> 00:30:04,030 So as long as it's inside for loop, I can't use whatever 691 00:30:04,030 --> 00:30:05,860 variable I create here outside of it. 692 00:30:05,860 --> 00:30:09,040 It has to be limited to the inside of that for loop there. 693 00:30:09,040 --> 00:30:10,420 Does that help? 694 00:30:10,420 --> 00:30:11,433 Great. 695 00:30:11,433 --> 00:30:12,475 Other questions here too. 696 00:30:12,475 --> 00:30:15,810 697 00:30:15,810 --> 00:30:18,713 STUDENT: I see no semicolon at the end of the for loop. 698 00:30:18,713 --> 00:30:20,630 CARTER ZENKE: Yeah, why is there no semicolon? 699 00:30:20,630 --> 00:30:24,680 Well, probably a rule of thumb is this. 700 00:30:24,680 --> 00:30:27,770 Semicolons come after complete statements. 701 00:30:27,770 --> 00:30:31,970 And this for loop is kind of setting up some set of statements. 702 00:30:31,970 --> 00:30:35,550 It's saying, I'm going to eventually have something I want to do, 703 00:30:35,550 --> 00:30:36,750 but it's not there yet. 704 00:30:36,750 --> 00:30:38,960 So I'm going to actually have these braces 705 00:30:38,960 --> 00:30:43,140 to tell what statements I want to execute sequentially as I go through. 706 00:30:43,140 --> 00:30:46,670 So you'll get used to this as you go through and write more code. 707 00:30:46,670 --> 00:30:49,250 But in general, statements end with a semicolon, 708 00:30:49,250 --> 00:30:51,530 but things that set up statements generally don't. 709 00:30:51,530 --> 00:30:55,120 They might have these braces, yeah. 710 00:30:55,120 --> 00:30:56,203 Yeah. 711 00:30:56,203 --> 00:30:59,584 STUDENT: So you mentioned [INAUDIBLE] variable 712 00:30:59,584 --> 00:31:02,010 i only exists within the parameter. 713 00:31:02,010 --> 00:31:03,270 CARTER ZENKE: Exactly. 714 00:31:03,270 --> 00:31:07,730 STUDENT: Does that mean when you do multiple for loops in a row 715 00:31:07,730 --> 00:31:09,592 not with [INAUDIBLE]? 716 00:31:09,592 --> 00:31:11,300 CARTER ZENKE: Yeah, it's a good question. 717 00:31:11,300 --> 00:31:17,570 So let's say if I go back to my actual programming environment here, 718 00:31:17,570 --> 00:31:20,270 let's say I have a loop. 719 00:31:20,270 --> 00:31:25,040 And I'm going to say four int i equals 0. i is less than 10. 720 00:31:25,040 --> 00:31:26,930 i++. 721 00:31:26,930 --> 00:31:31,370 So now, I can access i inside of these two braces. 722 00:31:31,370 --> 00:31:32,750 That's something called scope. 723 00:31:32,750 --> 00:31:33,958 It's a scope of the variable. 724 00:31:33,958 --> 00:31:35,900 It persists in this space here. 725 00:31:35,900 --> 00:31:38,310 I can't use i out here. 726 00:31:38,310 --> 00:31:42,577 So I can't access it there, but I could, for example, have another for loop 727 00:31:42,577 --> 00:31:43,160 on the inside. 728 00:31:43,160 --> 00:31:47,030 I could say for int j equals i. 729 00:31:47,030 --> 00:31:49,670 That's totally valid because i's inside this loop. 730 00:31:49,670 --> 00:31:53,420 Maybe j is less than 10 in this case, and then j++. 731 00:31:53,420 --> 00:31:55,760 And I do something else in that. 732 00:31:55,760 --> 00:31:59,720 And actually, j is not accessible here. 733 00:31:59,720 --> 00:32:03,560 I can't do that, but it is accessible inside that loop there. 734 00:32:03,560 --> 00:32:07,878 735 00:32:07,878 --> 00:32:08,920 Other questions here too? 736 00:32:08,920 --> 00:32:10,108 Yeah. 737 00:32:10,108 --> 00:32:11,400 STUDENT: I'm a little confused. 738 00:32:11,400 --> 00:32:15,910 Where's the part that, I guess, senses what i is? 739 00:32:15,910 --> 00:32:19,600 It just appears [INAUDIBLE]. 740 00:32:19,600 --> 00:32:23,380 CARTER ZENKE: Yeah, can you tell me what you mean by senses what i is? 741 00:32:23,380 --> 00:32:29,715 STUDENT: Right, so because it's like a Boolean expression, 742 00:32:29,715 --> 00:32:36,540 right, whether i is greater than 1 or less than 1, like that kind of thing. 743 00:32:36,540 --> 00:32:41,260 So where's the input of whether it's going to be 1 or i what value it is? 744 00:32:41,260 --> 00:32:44,030 CARTER ZENKE: Yeah, so probably best to think of it like this, 745 00:32:44,030 --> 00:32:46,630 where my code is read top to bottom. 746 00:32:46,630 --> 00:32:50,870 And when it gets to this for loop, it's read kind of left to right. 747 00:32:50,870 --> 00:32:55,330 So the first time I get to this for loop, I'm going to make this variable i 748 00:32:55,330 --> 00:32:57,190 and set it to 0. 749 00:32:57,190 --> 00:33:01,480 Then I'm going to ask the question, is i less than 10? 750 00:33:01,480 --> 00:33:04,930 OK, it is, so I'll actually go inside this for loop 751 00:33:04,930 --> 00:33:08,740 and maybe run this code on the inside. 752 00:33:08,740 --> 00:33:15,880 Once I get to the bottom of this for loop here, I'll then go to the top. 753 00:33:15,880 --> 00:33:16,660 And I'll do this. 754 00:33:16,660 --> 00:33:18,987 I'll increase i by 1. 755 00:33:18,987 --> 00:33:20,320 And I'll ask the question again. 756 00:33:20,320 --> 00:33:21,170 Is i less than 10? 757 00:33:21,170 --> 00:33:21,670 It is. 758 00:33:21,670 --> 00:33:23,230 OK, I'll do this code again. 759 00:33:23,230 --> 00:33:26,635 Once it finishes, I'll increase i by 1 again, ask the question, keep going, 760 00:33:26,635 --> 00:33:27,260 and keep going. 761 00:33:27,260 --> 00:33:29,167 Does that help? 762 00:33:29,167 --> 00:33:30,000 STUDENT: OK, got it. 763 00:33:30,000 --> 00:33:34,240 So the input was i equals 0 first, and then [INAUDIBLE].. 764 00:33:34,240 --> 00:33:36,790 CARTER ZENKE: Exactly, this is our initialization. 765 00:33:36,790 --> 00:33:40,870 This is the first time for loop runs, set it to this value. 766 00:33:40,870 --> 00:33:44,860 Every time this code on the inside finishes running, ask-- 767 00:33:44,860 --> 00:33:49,150 or actually, before we start running this code, ask this question. 768 00:33:49,150 --> 00:33:51,810 If this is true, run this code. 769 00:33:51,810 --> 00:33:55,380 And we get to the bottom, increase i by 1 or do whatever is here, yeah. 770 00:33:55,380 --> 00:33:56,880 STUDENT: This should do it 10 times? 771 00:33:56,880 --> 00:33:58,880 CARTER ZENKE: This should do it 10 times, right? 772 00:33:58,880 --> 00:34:03,420 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. 773 00:34:03,420 --> 00:34:07,366 It won't do 10 because that is not less than 10. 774 00:34:07,366 --> 00:34:09,570 STUDENT: Is this called a do while? 775 00:34:09,570 --> 00:34:13,050 CARTER ZENKE: It's called a for loop just because of the 4 on the-- right 776 00:34:13,050 --> 00:34:13,792 there. 777 00:34:13,792 --> 00:34:17,100 Yeah, but we do have the do while loop, if we could go just quickly to that one 778 00:34:17,100 --> 00:34:18,699 here. 779 00:34:18,699 --> 00:34:22,560 The do while loop has the words do while in it, right? 780 00:34:22,560 --> 00:34:27,360 And what this is going to do is actually not ask a question first. 781 00:34:27,360 --> 00:34:32,010 Just unconditionally, run this piece of code and then ask the question later. 782 00:34:32,010 --> 00:34:34,860 And this is handy for trying to get some input from the user 783 00:34:34,860 --> 00:34:37,739 because it'll always ask the user that question. 784 00:34:37,739 --> 00:34:39,270 What input do you want to give me? 785 00:34:39,270 --> 00:34:43,230 And then maybe reprompt them, depending on whether that input is invalid or not 786 00:34:43,230 --> 00:34:44,790 what you want it to be, all right? 787 00:34:44,790 --> 00:34:47,790 So down here we would say this is the condition under which our input is 788 00:34:47,790 --> 00:34:48,540 invalid. 789 00:34:48,540 --> 00:34:52,620 And we should go back up and do it all over again. 790 00:34:52,620 --> 00:34:55,520 So questions on these kinds of loops still. 791 00:34:55,520 --> 00:34:59,870 792 00:34:59,870 --> 00:35:00,870 Yeah. 793 00:35:00,870 --> 00:35:04,890 STUDENT: Would this be good, the do-while loop if you were to-- 794 00:35:04,890 --> 00:35:07,595 you were asking a question within the while 795 00:35:07,595 --> 00:35:10,383 loop that didn't say you're doing [INAUDIBLE] 796 00:35:10,383 --> 00:35:15,330 same with [INAUDIBLE] while loop give a given number? 797 00:35:15,330 --> 00:35:18,840 Would there be an easier way to terminate the function if say 798 00:35:18,840 --> 00:35:21,133 you don't [INAUDIBLE] the right age? 799 00:35:21,133 --> 00:35:23,175 CARTER ZENKE: And by terminate do you mean like-- 800 00:35:23,175 --> 00:35:24,840 STUDENT: Terminate the funct-- 801 00:35:24,840 --> 00:35:26,100 like it just stops. 802 00:35:26,100 --> 00:35:30,580 If you use-- say you're looking for the age range from 25 to 30, 803 00:35:30,580 --> 00:35:35,360 and then if it's in there, you go to the while loop. 804 00:35:35,360 --> 00:35:37,640 Would you then have the while loop-- would there 805 00:35:37,640 --> 00:35:42,210 be a way to stop the while loop from [INAUDIBLE]?? 806 00:35:42,210 --> 00:35:43,960 CARTER ZENKE: And so, if I'm understanding 807 00:35:43,960 --> 00:35:47,140 what you're asking correctly, it's often better 808 00:35:47,140 --> 00:35:51,280 to check for what we don't want to have happen than to check for what 809 00:35:51,280 --> 00:35:52,880 it is we do want to have happen. 810 00:35:52,880 --> 00:35:55,370 So for example, I might not want to do this. 811 00:35:55,370 --> 00:36:00,160 I might not want to say int age is get int age, here. 812 00:36:00,160 --> 00:36:06,940 And then ask the question, if, let's say, age is between-- what did you say, 813 00:36:06,940 --> 00:36:08,320 like 15 and 20? 814 00:36:08,320 --> 00:36:09,100 STUDENT: Yeah. 815 00:36:09,100 --> 00:36:15,970 CARTER ZENKE: If age is between 15 and 20, then do the rest of my program 816 00:36:15,970 --> 00:36:19,420 because what I'm doing here is I'm separating my program from-- 817 00:36:19,420 --> 00:36:22,000 I'm indenting it by a lot, and maybe I have more conditions. 818 00:36:22,000 --> 00:36:25,360 Maybe I also want to say, if the name starts with A, 819 00:36:25,360 --> 00:36:28,270 and then it keeps getting more and more indented. 820 00:36:28,270 --> 00:36:31,040 So what I could do instead is I could do something like this. 821 00:36:31,040 --> 00:36:34,730 I could say, well, let me do this. 822 00:36:34,730 --> 00:36:37,210 Let me create this variable named age. 823 00:36:37,210 --> 00:36:40,690 Let me get some value for it. 824 00:36:40,690 --> 00:36:41,360 Whoops. 825 00:36:41,360 --> 00:36:43,160 Let me get some value for it. 826 00:36:43,160 --> 00:36:47,740 And while the age is not what I want, let me keep reprompting. 827 00:36:47,740 --> 00:36:49,550 Is that answering your question? 828 00:36:49,550 --> 00:36:50,800 STUDENT: Yeah, i got it, yeah. 829 00:36:50,800 --> 00:36:57,000 CARTER ZENKE: OK, so age is like less than 15, or age is greater than 20. 830 00:36:57,000 --> 00:37:00,830 Let me reprompt, right? 831 00:37:00,830 --> 00:37:01,330 OK. 832 00:37:01,330 --> 00:37:02,366 Yeah. 833 00:37:02,366 --> 00:37:11,560 STUDENT: Is that [INAUDIBLE] to write age less, equal, between? 834 00:37:11,560 --> 00:37:17,200 Or is it better time to say age less [INAUDIBLE]?? 835 00:37:17,200 --> 00:37:19,430 CARTER ZENKE: Probably depends on the context. 836 00:37:19,430 --> 00:37:23,530 So the question was, is it better style to use this less than or this less than 837 00:37:23,530 --> 00:37:25,090 or equal to? 838 00:37:25,090 --> 00:37:28,930 In general, when you're writing for loops, where you just want something 839 00:37:28,930 --> 00:37:31,240 to repeat a certain number of times, we tend 840 00:37:31,240 --> 00:37:34,330 to use the less than and always start at 0. 841 00:37:34,330 --> 00:37:36,490 So let's say I want something to repeat five times. 842 00:37:36,490 --> 00:37:41,530 I'll say int i equals 0, i is less than 5, i++. 843 00:37:41,530 --> 00:37:44,290 And the math on this just works, right? 844 00:37:44,290 --> 00:37:47,950 0, 1, 2, 3, 4, that's five times. 845 00:37:47,950 --> 00:37:52,210 If I were to do something like this, let's say int i is 1, 846 00:37:52,210 --> 00:37:54,880 then I'd have to do less than or equal to 5. 847 00:37:54,880 --> 00:37:58,960 And because programmers start counting at 0, meaning this absence of anything. 848 00:37:58,960 --> 00:38:00,610 It's starting from the very basics. 849 00:38:00,610 --> 00:38:03,527 We're not going to actually have this less than or equal to sign here. 850 00:38:03,527 --> 00:38:04,660 Is that helpful? 851 00:38:04,660 --> 00:38:06,330 Yeah. 852 00:38:06,330 --> 00:38:06,830 OK. 853 00:38:06,830 --> 00:38:10,860 854 00:38:10,860 --> 00:38:15,340 All right, so with these building blocks of these loops, 855 00:38:15,340 --> 00:38:19,240 we'll spend some time working on our lab for the next 10 minutes or so. 856 00:38:19,240 --> 00:38:23,620 In our lab-- if I go back to my slides here-- 857 00:38:23,620 --> 00:38:26,680 we're going to be asking this question of how many llamas 858 00:38:26,680 --> 00:38:28,030 do we currently have? 859 00:38:28,030 --> 00:38:31,600 And how many years will take us to get to an aspirational number of llamas, 860 00:38:31,600 --> 00:38:34,160 some gold number of llamas for ourselves here? 861 00:38:34,160 --> 00:38:37,675 So if we look at this, you can find the lab at this URL right here. 862 00:38:37,675 --> 00:38:38,800 It's on the course website. 863 00:38:38,800 --> 00:38:40,160 Go to the lab page. 864 00:38:40,160 --> 00:38:43,090 And in that lab, you'll find these questions. 865 00:38:43,090 --> 00:38:44,080 Actually, sorry. 866 00:38:44,080 --> 00:38:48,040 Within this lab page, you're going to work on this problem all together. 867 00:38:48,040 --> 00:38:51,650 But we'll get started working on this up front here. 868 00:38:51,650 --> 00:38:53,530 So the first thing that we'll do for this lab 869 00:38:53,530 --> 00:38:56,710 is think about how we're going to work an example ourselves, 870 00:38:56,710 --> 00:38:59,740 and then walk through and write our code. 871 00:38:59,740 --> 00:39:03,460 When you're working on your problems, often good to think 872 00:39:03,460 --> 00:39:05,950 about how you're going to write your algorithm first, 873 00:39:05,950 --> 00:39:07,840 and then write your code. 874 00:39:07,840 --> 00:39:11,830 So these seven steps can help you, whether you're working on the lab, 875 00:39:11,830 --> 00:39:13,640 in the problem set, or so on. 876 00:39:13,640 --> 00:39:16,810 And notice how more than half of these are 877 00:39:16,810 --> 00:39:20,500 about writing things down, thinking through them, not necessarily 878 00:39:20,500 --> 00:39:22,120 programming. 879 00:39:22,120 --> 00:39:26,630 But these final three steps are all about translating that idea into code. 880 00:39:26,630 --> 00:39:28,450 So we'll do that here today. 881 00:39:28,450 --> 00:39:31,112 If we look at our llamas, we have a certain number of llamas. 882 00:39:31,112 --> 00:39:33,070 We want to figure out how many years it'll take 883 00:39:33,070 --> 00:39:35,140 to get to another number of llamas. 884 00:39:35,140 --> 00:39:39,490 And we know that every year, maybe a third of our llamas are born, 885 00:39:39,490 --> 00:39:43,300 and 1/4 of our llamas sadly pass away, right? 886 00:39:43,300 --> 00:39:46,640 And let's take a look at this particular example here. 887 00:39:46,640 --> 00:39:48,970 We have 12 llamas. 888 00:39:48,970 --> 00:39:53,770 And so in this current year, 12 over 3 new llamas will be born, 889 00:39:53,770 --> 00:39:56,480 and 12 over 4 llamas will pass away. 890 00:39:56,480 --> 00:40:00,550 And the question is, how many years will it take to get to 13 llamas? 891 00:40:00,550 --> 00:40:03,940 So to work this example, we have these 12 llamas. 892 00:40:03,940 --> 00:40:05,380 And now, what would we do? 893 00:40:05,380 --> 00:40:08,290 We want to birth a 1/3 llamas. 894 00:40:08,290 --> 00:40:09,760 So how many more will we add here? 895 00:40:09,760 --> 00:40:10,605 STUDENT: Four. 896 00:40:10,605 --> 00:40:12,105 CARTER ZENKE: Four, OK, let's add 4. 897 00:40:12,105 --> 00:40:14,740 898 00:40:14,740 --> 00:40:16,330 And how many would pass away? 899 00:40:16,330 --> 00:40:17,230 STUDENT: Three. 900 00:40:17,230 --> 00:40:20,080 CARTER ZENKE: Three, so we'll take away 3 here. 901 00:40:20,080 --> 00:40:22,900 And we are at 13 llamas now, right? 902 00:40:22,900 --> 00:40:24,760 And that was one year overall. 903 00:40:24,760 --> 00:40:27,730 We birthed our four more llamas, and three llamas passed away. 904 00:40:27,730 --> 00:40:30,230 Now, we're at 13. 905 00:40:30,230 --> 00:40:33,850 So as you go and write this program, we'll get started up here in a minute. 906 00:40:33,850 --> 00:40:37,150 We want to first prompt the user for this starting number of llamas, 907 00:40:37,150 --> 00:40:40,930 ask them for a goal number of llamas, and then do some math perhaps 908 00:40:40,930 --> 00:40:43,330 in a loop to figure out how many years it 909 00:40:43,330 --> 00:40:46,030 will take to get to that goal number of llamas, 910 00:40:46,030 --> 00:40:48,950 and finally, print out that number of years. 911 00:40:48,950 --> 00:40:55,390 So if you are in your code space, you might go over to your environment here. 912 00:40:55,390 --> 00:41:01,450 You might type code population.C to open up that file. 913 00:41:01,450 --> 00:41:03,100 Here I have mine. 914 00:41:03,100 --> 00:41:07,060 You might then type, include standardio.h to print something 915 00:41:07,060 --> 00:41:08,470 to the screen later on. 916 00:41:08,470 --> 00:41:13,730 And you might also do the cs50 library, cs50.h here as well. 917 00:41:13,730 --> 00:41:16,890 What else do we need for this boilerplate code? 918 00:41:16,890 --> 00:41:17,662 STUDENT: Int main. 919 00:41:17,662 --> 00:41:19,120 CARTER ZENKE: Int main void, right? 920 00:41:19,120 --> 00:41:23,800 So I'll say int main void. 921 00:41:23,800 --> 00:41:26,830 And I'll leave this rest up to you, but make sure you're asking-- 922 00:41:26,830 --> 00:41:32,110 your prompting the user for a starting number of llamas, 923 00:41:32,110 --> 00:41:37,090 prompting them for an ending number of llamas, and then 924 00:41:37,090 --> 00:41:41,170 finally, maybe using some kind of loop to figure out how many years it 925 00:41:41,170 --> 00:41:43,795 will take to get to the goal. 926 00:41:43,795 --> 00:41:47,430 927 00:41:47,430 --> 00:41:56,220 And keep in mind that every year a 1/3 of our current llamas are born 928 00:41:56,220 --> 00:42:00,080 and 1/4 pass away. 929 00:42:00,080 --> 00:42:05,900 All right, so work on this for, let's say, 10 minutes or so. 930 00:42:05,900 --> 00:42:08,960 And we'll come back and work the example together. 931 00:42:08,960 --> 00:42:12,410 OK, so I hope you've made some good progress on this lab. 932 00:42:12,410 --> 00:42:15,440 Let's come back and work the example together. 933 00:42:15,440 --> 00:42:18,980 We had a few steps to work through, the first one being prompting the user 934 00:42:18,980 --> 00:42:21,740 for a starting number of llamas. 935 00:42:21,740 --> 00:42:26,000 And could I ask maybe a group from this side of the room, maybe 936 00:42:26,000 --> 00:42:30,350 towards the back, if you don't mind, how did you prompt the user 937 00:42:30,350 --> 00:42:33,890 for your starting number of llamas? 938 00:42:33,890 --> 00:42:34,820 Yeah, go ahead. 939 00:42:34,820 --> 00:42:40,220 STUDENT: So the first [INAUDIBLE] start. 940 00:42:40,220 --> 00:42:43,290 And we ran a do-while loop. 941 00:42:43,290 --> 00:42:48,000 So it says starts equals get int and then start size. 942 00:42:48,000 --> 00:42:51,360 And then the condition was while the start was less than 9. 943 00:42:51,360 --> 00:42:54,780 CARTER ZENKE: Gotcha, so while start is less than 9. 944 00:42:54,780 --> 00:42:57,810 And why did you choose less than 9 for that case? 945 00:42:57,810 --> 00:43:00,960 STUDENT: Because the lab said that the minimum number that the start 946 00:43:00,960 --> 00:43:04,770 size could be was 9 or else the population would fail to grow. 947 00:43:04,770 --> 00:43:07,710 CARTER ZENKE: Right, so we want to reprompt the user if they give us 948 00:43:07,710 --> 00:43:10,262 a number less than 9, right? 949 00:43:10,262 --> 00:43:11,220 That seems pretty good. 950 00:43:11,220 --> 00:43:14,970 And then similarly, how could we prompt them for an ending number of llamas? 951 00:43:14,970 --> 00:43:17,580 Could I ask maybe in the middle this time, 952 00:43:17,580 --> 00:43:19,920 somebody from maybe the back few rows, how 953 00:43:19,920 --> 00:43:22,935 did you try to prompt for an ending number of llamas here? 954 00:43:22,935 --> 00:43:26,900 955 00:43:26,900 --> 00:43:28,640 Yeah, go ahead. 956 00:43:28,640 --> 00:43:31,768 STUDENT: I created an int variable called n. 957 00:43:31,768 --> 00:43:32,560 CARTER ZENKE: Nice. 958 00:43:32,560 --> 00:43:35,552 STUDENT: And then I used a do-while loop. 959 00:43:35,552 --> 00:43:42,015 [INAUDIBLE] had n equals get int, and then end size. 960 00:43:42,015 --> 00:43:48,647 And then the condition for the while was while end was less than start. 961 00:43:48,647 --> 00:43:50,480 CARTER ZENKE: Yeah, and then just to ask you 962 00:43:50,480 --> 00:43:53,380 why would you have that in your conditional down there? 963 00:43:53,380 --> 00:43:57,510 STUDENT: Because in the lab it wanted us to keep 964 00:43:57,510 --> 00:44:02,930 prompting if the ending size that the user inputs is less than starting 965 00:44:02,930 --> 00:44:03,523 point. 966 00:44:03,523 --> 00:44:04,690 CARTER ZENKE: Yeah, totally. 967 00:44:04,690 --> 00:44:07,923 So we can't have fewer llamas than we began with. 968 00:44:07,923 --> 00:44:10,090 So just checking to make sure that is the case here. 969 00:44:10,090 --> 00:44:13,260 And if not, we reprompt the user. 970 00:44:13,260 --> 00:44:17,130 And then our final bit down here was keeping track of how many years 971 00:44:17,130 --> 00:44:20,310 it will take to get to our goal number of llamas now stored 972 00:44:20,310 --> 00:44:22,440 in this variable called end. 973 00:44:22,440 --> 00:44:25,380 And how did you all choose to represent the year, 974 00:44:25,380 --> 00:44:30,090 or how many years have passed in your program? 975 00:44:30,090 --> 00:44:33,230 Could I ask somebody from this side of the room now? 976 00:44:33,230 --> 00:44:36,290 What kind of variable did you use to count the number of years here? 977 00:44:36,290 --> 00:44:39,250 978 00:44:39,250 --> 00:44:42,190 Maybe somebody from the first few rows. 979 00:44:42,190 --> 00:44:42,910 Yeah, go ahead. 980 00:44:42,910 --> 00:44:43,868 STUDENT: As an integer. 981 00:44:43,868 --> 00:44:47,035 CARTER ZENKE: As an integer, and did you give it a special name or anything? 982 00:44:47,035 --> 00:44:48,370 STUDENT: i called it int years. 983 00:44:48,370 --> 00:44:49,570 CARTER ZENKE: Int years, OK. 984 00:44:49,570 --> 00:44:51,880 And what did you set it first to? 985 00:44:51,880 --> 00:44:52,720 STUDENT: Zero. 986 00:44:52,720 --> 00:44:54,700 CARTER ZENKE: Zero, OK. 987 00:44:54,700 --> 00:44:57,550 And now we have some math to do. 988 00:44:57,550 --> 00:44:59,983 If we look maybe-- 989 00:44:59,983 --> 00:45:01,150 this group [INAUDIBLE] mind. 990 00:45:01,150 --> 00:45:03,730 How did you all add llamas, subtract them? 991 00:45:03,730 --> 00:45:07,060 How did you adjust your population of llamas as the years went on? 992 00:45:07,060 --> 00:45:12,910 993 00:45:12,910 --> 00:45:17,695 Any ideas for how you might do it or how you did do it? 994 00:45:17,695 --> 00:45:18,320 Yeah, go ahead. 995 00:45:18,320 --> 00:45:20,392 STUDENT: Just add 112 llama each year. 996 00:45:20,392 --> 00:45:22,100 CARTER ZENKE: Added 112 llamas each year. 997 00:45:22,100 --> 00:45:22,600 Nice. 998 00:45:22,600 --> 00:45:25,322 So can you tell me, if we look at our program here, 999 00:45:25,322 --> 00:45:28,280 we have a starting number of llamas, so how would you adjust that here? 1000 00:45:28,280 --> 00:45:31,320 1001 00:45:31,320 --> 00:45:33,772 STUDENT: You would go, like you said, at start-- 1002 00:45:33,772 --> 00:45:34,605 CARTER ZENKE: Start. 1003 00:45:34,605 --> 00:45:37,450 STUDENT: It would be start equals-- 1004 00:45:37,450 --> 00:45:38,830 I'm sorry, plus equals. 1005 00:45:38,830 --> 00:45:40,163 CARTER ZENKE: Start plus equals. 1006 00:45:40,163 --> 00:45:41,410 STUDENT: Start divided by 12. 1007 00:45:41,410 --> 00:45:43,930 CARTER ZENKE: Start divided by 12. 1008 00:45:43,930 --> 00:45:46,150 And how did you get start divided by 12? 1009 00:45:46,150 --> 00:45:48,820 What kind of math did you do to get there? 1010 00:45:48,820 --> 00:45:51,105 STUDENT: Common denominator of 1/3 and 1/4. 1011 00:45:51,105 --> 00:45:53,530 And then you're adding 1/3 and subtracting 1/4. 1012 00:45:53,530 --> 00:45:55,655 CARTER ZENKE: Nice, so ultimately, every year we're 1013 00:45:55,655 --> 00:45:59,680 just adding 1/12 new llamas after we add a 1/3 and subtract a 1/4. 1014 00:45:59,680 --> 00:46:03,430 It comes out to 1/12 new llamas every year. 1015 00:46:03,430 --> 00:46:08,050 But we need some kind of loop to run this. 1016 00:46:08,050 --> 00:46:10,300 Anyone want to help me out here with this final piece? 1017 00:46:10,300 --> 00:46:13,520 How do we have the loop to increase this-- 1018 00:46:13,520 --> 00:46:16,460 llama, know when to stop too. 1019 00:46:16,460 --> 00:46:17,030 Yeah. 1020 00:46:17,030 --> 00:46:21,729 STUDENT: We could do while start is less end [INAUDIBLE] start. 1021 00:46:21,729 --> 00:46:26,457 And after that [INAUDIBLE] add [INAUDIBLE].. 1022 00:46:26,457 --> 00:46:28,165 CARTER ZENKE: Yeah, and so add 1 to year? 1023 00:46:28,165 --> 00:46:28,870 STUDENT: Yeah. 1024 00:46:28,870 --> 00:46:33,730 CARTER ZENKE: Yeah, so we could say years++ for increase years by 1. 1025 00:46:33,730 --> 00:46:37,940 And at the very end we ultimately want to print out the number of years. 1026 00:46:37,940 --> 00:46:40,840 So I'll say printf %i. 1027 00:46:40,840 --> 00:46:44,410 And maybe just to clarify, years percent and then years 1028 00:46:44,410 --> 00:46:49,100 down at the bottom here, with a backslash n at the end. 1029 00:46:49,100 --> 00:46:56,410 And if I make this make population, run ./population, 1030 00:46:56,410 --> 00:46:59,310 I'll type in maybe 12 and 13. 1031 00:46:59,310 --> 00:47:02,460 And I get one year, as we expected. 1032 00:47:02,460 --> 00:47:03,645 Yeah, question or comment. 1033 00:47:03,645 --> 00:47:07,503 STUDENT: What is the plus equal [INAUDIBLE] start plus equal start? 1034 00:47:07,503 --> 00:47:08,670 CARTER ZENKE: Good question. 1035 00:47:08,670 --> 00:47:11,260 This is basically shorthand for doing this. 1036 00:47:11,260 --> 00:47:17,430 I could say start equals start + start over 12 because I'm trying 1037 00:47:17,430 --> 00:47:19,750 to add to start as we go through. 1038 00:47:19,750 --> 00:47:22,620 So this plus equals is simply saying, let 1039 00:47:22,620 --> 00:47:27,298 me add to what start currently is this value on the right-hand side. 1040 00:47:27,298 --> 00:47:28,215 Does that makes sense? 1041 00:47:28,215 --> 00:47:28,870 STUDENT: Yes, thanks. 1042 00:47:28,870 --> 00:47:29,745 CARTER ZENKE: Gotcha. 1043 00:47:29,745 --> 00:47:34,460 1044 00:47:34,460 --> 00:47:39,120 Now, there's one question here, which is, let's say, I have-- oh, yeah, 1045 00:47:39,120 --> 00:47:40,370 go ahead, somewhere over here. 1046 00:47:40,370 --> 00:47:41,560 Yeah. 1047 00:47:41,560 --> 00:47:44,900 STUDENT: I have a question regarding this start in line 12. 1048 00:47:44,900 --> 00:47:49,370 We have one part that is the [INAUDIBLE] form 1049 00:47:49,370 --> 00:47:53,130 and one part that is [INAUDIBLE] our time. 1050 00:47:53,130 --> 00:47:58,790 Therefore, you need to round the first part and then round the second part 1051 00:47:58,790 --> 00:48:04,910 because you cannot have half a llama die and 1/3 being alive. 1052 00:48:04,910 --> 00:48:09,920 So in some cases, where the number is divisible by one part of it 1053 00:48:09,920 --> 00:48:13,040 and not divisible by the other, could create some differences. 1054 00:48:13,040 --> 00:48:17,300 CARTER ZENKE: Yeah, so maybe it is better for us to add start over 3 1055 00:48:17,300 --> 00:48:21,067 and then subtract maybe start over 4 1056 00:48:21,067 --> 00:48:22,400 STUDENT: Yeah, but what if you-- 1057 00:48:22,400 --> 00:48:23,150 CARTER ZENKE: --and keep going. 1058 00:48:23,150 --> 00:48:24,950 STUDENT: --round these numbers? 1059 00:48:24,950 --> 00:48:30,050 I guess divided by 3 [INAUDIBLE] or 3.3333. 1060 00:48:30,050 --> 00:48:34,080 So you need to round it 3 because, otherwise, 1061 00:48:34,080 --> 00:48:36,678 you cannot have 1/3 of a llama [INAUDIBLE].. 1062 00:48:36,678 --> 00:48:38,970 CARTER ZENKE: Yeah, we don't want 1/3 third of a llama. 1063 00:48:38,970 --> 00:48:41,230 So we got to round this in some way. 1064 00:48:41,230 --> 00:48:44,400 And I think what we want to do is always round down, 1065 00:48:44,400 --> 00:48:48,720 which brings us to some feature or, in some cases, a bug in C-- 1066 00:48:48,720 --> 00:48:51,210 you might run into this-- called truncation. 1067 00:48:51,210 --> 00:48:54,900 And we didn't see this as much in lecture, which to highlight it here, 1068 00:48:54,900 --> 00:49:00,940 in C, when you divide two integers, you will always get back an integer. 1069 00:49:00,940 --> 00:49:07,050 So if I divide perhaps maybe 2 and 3, 2 divided by 3, both integers, 1070 00:49:07,050 --> 00:49:08,760 I won't get 2/3. 1071 00:49:08,760 --> 00:49:10,260 I'll get 0. 1072 00:49:10,260 --> 00:49:13,240 Always round down, cut off the rest of that data there. 1073 00:49:13,240 --> 00:49:19,440 So to show an example of this, I might do code truncation.c. 1074 00:49:19,440 --> 00:49:25,480 And I will have the same boilerplate code, standardio.h. 1075 00:49:25,480 --> 00:49:28,420 I'll include cs50.h. 1076 00:49:28,420 --> 00:49:30,850 And I'll say int main void. 1077 00:49:30,850 --> 00:49:35,680 And I'll type in maybe int a is, let's say, 2. 1078 00:49:35,680 --> 00:49:37,540 Int b is 3. 1079 00:49:37,540 --> 00:49:42,020 And let's print out the result of dividing them like this. 1080 00:49:42,020 --> 00:49:46,210 Let's say-- actually, sorry, int c is a divided by b. 1081 00:49:46,210 --> 00:49:51,100 And I'll say this gets the result c here. 1082 00:49:51,100 --> 00:49:58,300 I'll say make truncation and do ./truncation. 1083 00:49:58,300 --> 00:50:02,380 And I'll get 0 and not 2/3. 1084 00:50:02,380 --> 00:50:04,300 So always rounding down here. 1085 00:50:04,300 --> 00:50:07,150 If I do want the decimal though, what I can do 1086 00:50:07,150 --> 00:50:12,170 is I can convert on the fly one of these to a decimal. 1087 00:50:12,170 --> 00:50:16,240 So I could say, actually, I want a to be a float 1088 00:50:16,240 --> 00:50:18,650 and then complete this division. 1089 00:50:18,650 --> 00:50:20,980 And as long as one of your numbers is a float, 1090 00:50:20,980 --> 00:50:22,940 you'll get a float in the very end. 1091 00:50:22,940 --> 00:50:25,660 So I'll say maybe %f here. 1092 00:50:25,660 --> 00:50:27,950 I'll do make truncation. 1093 00:50:27,950 --> 00:50:28,450 Whoops. 1094 00:50:28,450 --> 00:50:32,980 Let me say this is actually now a float in the end. 1095 00:50:32,980 --> 00:50:41,440 Make truncation, I'll say ./truncation, and I will get that 0.6667. 1096 00:50:41,440 --> 00:50:52,270 If I were to not do this, if I were to not do this, I would get 0 all around. 1097 00:50:52,270 --> 00:50:55,000 So just something to keep in mind as we go through here. 1098 00:50:55,000 --> 00:50:58,337 Happy to stick around and answer any questions that you all have, 1099 00:50:58,337 --> 00:51:01,420 but this should bring us to the end of our lab today and our super section 1100 00:51:01,420 --> 00:51:01,850 as well. 1101 00:51:01,850 --> 00:51:02,540 Thank you all for coming. 1102 00:51:02,540 --> 00:51:03,832 It's wonderful to see you here. 1103 00:51:03,832 --> 00:51:06,060 Hope to see you next week in section. 1104 00:51:06,060 --> 00:51:08,000