WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000 00:00:04.645 --> 00:00:06.020 CARTER ZENKE: Hello, one and all. 00:00:06.020 --> 00:00:10.610 And welcome to CS50's very first super section for week 1. 00:00:10.610 --> 00:00:12.005 My name is Carter Zenke. 00:00:12.005 --> 00:00:13.130 I'm the course's preceptor. 00:00:13.130 --> 00:00:14.990 I'm joined here by many of our wonderful staff members 00:00:14.990 --> 00:00:16.698 that are going to stand up and say hello. 00:00:16.698 --> 00:00:19.140 [APPLAUSE] 00:00:19.140 --> 00:00:21.680 Yeah, a round of applause for these folks. 00:00:21.680 --> 00:00:22.430 They're wonderful. 00:00:22.430 --> 00:00:25.650 They'll be helping you through this super section today. 00:00:25.650 --> 00:00:27.740 This is our first super section for the course. 00:00:27.740 --> 00:00:29.990 On a normal week, you actually attend your own section 00:00:29.990 --> 00:00:31.310 online by course's head teaching fellow. 00:00:31.310 --> 00:00:32.509 You signed up for those online at Harvard. 00:00:32.509 --> 00:00:34.290 And you'll attend those starting next week. 00:00:34.290 --> 00:00:35.998 But this week, given the odd schedule, we 00:00:35.998 --> 00:00:38.940 thought we'd come together for one large section overall. 00:00:38.940 --> 00:00:40.710 So here are these details here. 00:00:40.710 --> 00:00:44.040 You're going to email if you've got any questions, heads@cs50.harvard.edu. 00:00:44.040 --> 00:00:46.880 And all of our slides and intros for today will be at this URL 00:00:46.880 --> 00:00:51.205 down here on the course website, going to the super section page there. 00:00:51.205 --> 00:00:53.330 To kick things off, though, I thought we'd actually 00:00:53.330 --> 00:00:57.208 have you all talk to each other and think about these two questions. 00:00:57.208 --> 00:00:59.750 So think back to lecture and think about this first question. 00:00:59.750 --> 00:01:02.180 What did you find exciting from that lecture? 00:01:02.180 --> 00:01:04.610 And what are you still confused about? 00:01:04.610 --> 00:01:06.480 What do you still have questions about? 00:01:06.480 --> 00:01:10.520 So turn to somebody, perhaps who you don't know and talk about those two 00:01:10.520 --> 00:01:12.020 questions for 2 minutes here. 00:01:12.020 --> 00:01:13.700 One, what are you excited about? 00:01:13.700 --> 00:01:17.210 Two, what do you still have questions about? 00:01:17.210 --> 00:01:19.910 Let's come back from these conversations. 00:01:19.910 --> 00:01:23.100 So glad you are having this wonderful discussion here. 00:01:23.100 --> 00:01:25.610 And let's hear from a few different groups 00:01:25.610 --> 00:01:29.400 on what you were thinking about for these questions here. 00:01:29.400 --> 00:01:30.410 Thank you, all. 00:01:30.410 --> 00:01:33.455 Let's actually turn to this left-hand section. 00:01:33.455 --> 00:01:35.330 Could one group over here tell me what you're 00:01:35.330 --> 00:01:37.997 excited about for this week and one thing you're still wondering 00:01:37.997 --> 00:01:39.578 or you have questions about? 00:01:39.578 --> 00:01:41.120 One group from this side of the room? 00:01:46.960 --> 00:01:49.460 Can I narrow it down to somebody from the front row perhaps? 00:01:49.460 --> 00:01:50.670 What are you interested in? 00:01:50.670 --> 00:01:52.878 What do you still have questions about for this week? 00:01:59.620 --> 00:02:01.540 Yeah, go ahead. 00:02:01.540 --> 00:02:06.850 STUDENT: I think it's more of the-- and less of the code, the syntax itself. 00:02:06.850 --> 00:02:10.580 Because that, you can kind of pick up quite quickly. 00:02:10.580 --> 00:02:16.450 I think the hardest thing is probably to how you construct efficient code. 00:02:16.450 --> 00:02:18.988 You sometimes just have to think in a different way. 00:02:18.988 --> 00:02:19.780 CARTER ZENKE: Nice. 00:02:19.780 --> 00:02:23.550 STUDENT: That's something that I guess, we'll 00:02:23.550 --> 00:02:26.043 really take some time to come to terms. 00:02:26.043 --> 00:02:27.210 CARTER ZENKE: Totally, yeah. 00:02:27.210 --> 00:02:29.130 So there's a question, how do you write efficient code? 00:02:29.130 --> 00:02:31.440 And later today, we'll actually work on the course's lab, 00:02:31.440 --> 00:02:33.440 this practice problem to help you see how we can 00:02:33.440 --> 00:02:35.130 write efficient code in that instance. 00:02:35.130 --> 00:02:38.297 You can work together on this practice problem and get ideas from each other 00:02:38.297 --> 00:02:39.150 as we go through. 00:02:39.150 --> 00:02:41.580 Let's hear from maybe one more group over on this side, 00:02:41.580 --> 00:02:44.160 maybe somebody in the second row back here, if you mind. 00:02:44.160 --> 00:02:46.650 One thing you're interested in, yeah, go ahead. 00:02:46.650 --> 00:02:49.830 STUDENT: I'm really interested in also this kind of stuff, 00:02:49.830 --> 00:02:54.030 but also creating stuff, just coding to have a the final product. 00:02:54.030 --> 00:02:58.350 And now I'll get opportunities to make a final product. 00:02:58.350 --> 00:03:00.570 And somebody else to think about lecture, 00:03:00.570 --> 00:03:02.910 I feel like there's so much syntax to learn. 00:03:02.910 --> 00:03:05.820 And that's sort of giving me pause. 00:03:05.820 --> 00:03:08.750 It's like, I don't even know what's out there. 00:03:08.750 --> 00:03:13.640 I think somebody said, actually in the orientation, 00:03:13.640 --> 00:03:15.860 sometimes like students will go something 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 00:03:18.110 --> 00:03:18.770 already. 00:03:18.770 --> 00:03:20.030 CARTER ZENKE: Yeah, exactly. 00:03:20.030 --> 00:03:22.573 So there's this idea of how you take this idea you have 00:03:22.573 --> 00:03:24.740 and what you want to build and turn into the syntax, 00:03:24.740 --> 00:03:27.380 like actually write to make the computer do what you want it to do, right? 00:03:27.380 --> 00:03:29.100 So all these are good questions. 00:03:29.100 --> 00:03:31.740 And here are a few we'll actually dive in to today. 00:03:31.740 --> 00:03:33.980 The first is, why are we using C? 00:03:33.980 --> 00:03:37.280 Why we're using this ancient language to start off programming with? 00:03:37.280 --> 00:03:39.802 And how do we learn and read its syntax here? 00:03:39.802 --> 00:03:43.010 How can we actually learn how to create and read these variables, these loops 00:03:43.010 --> 00:03:44.750 and conditionals, and so on. 00:03:44.750 --> 00:03:48.260 As we go through that, we'll talk about this particular instance of a data type 00:03:48.260 --> 00:03:53.100 because C requires us to tell what type each pieces of our data are. 00:03:53.100 --> 00:03:56.150 We'll also talk about compiling C programs, taking them from source code 00:03:56.150 --> 00:03:57.140 to machine code. 00:03:57.140 --> 00:03:59.015 And then finally, at the very end, we'll talk 00:03:59.015 --> 00:04:02.720 about how many years it will take to double our number of llamas 00:04:02.720 --> 00:04:04.087 that we have in the lab. 00:04:04.087 --> 00:04:06.920 So to kick things off here, we can go back to actually lecture zero, 00:04:06.920 --> 00:04:09.510 I think, back to our contact application. 00:04:09.510 --> 00:04:13.820 So in this application we stored names and phone numbers and other things too. 00:04:13.820 --> 00:04:16.430 What else could we store in a contact's application? 00:04:16.430 --> 00:04:19.430 Could I ask maybe somebody from the second row, in this middle row here? 00:04:19.430 --> 00:04:23.330 What can we store in a contact's application? 00:04:23.330 --> 00:04:23.962 Yeah, go ahead. 00:04:23.962 --> 00:04:24.920 STUDENT: Phone numbers. 00:04:24.920 --> 00:04:27.128 CARTER ZENKE: Phone numbers, right, other things too. 00:04:27.128 --> 00:04:29.142 Maybe somebody in the row up now. 00:04:29.142 --> 00:04:30.238 STUDENT: Emails. 00:04:30.238 --> 00:04:31.280 CARTER ZENKE: Emails, OK. 00:04:31.280 --> 00:04:31.880 Other things? 00:04:31.880 --> 00:04:33.240 Let's go one row up again. 00:04:33.240 --> 00:04:34.250 What else can we store? 00:04:34.250 --> 00:04:35.120 STUDENT: Addresses. 00:04:35.120 --> 00:04:35.990 CARTER ZENKE: Addresses, nice. 00:04:35.990 --> 00:04:38.390 So all these things we can store in our contact's application. 00:04:38.390 --> 00:04:40.130 But let's say you want to store the number of times 00:04:40.130 --> 00:04:43.040 I want to actually have made a call before in my application, on my phone, 00:04:43.040 --> 00:04:43.540 right? 00:04:43.540 --> 00:04:47.180 So here we have an instance of a variable, some number or some value 00:04:47.180 --> 00:04:48.110 that can change. 00:04:48.110 --> 00:04:50.870 And I'm going to give a name to it, in this case, calls, 00:04:50.870 --> 00:04:52.522 number of times I called somebody here. 00:04:52.522 --> 00:04:54.980 So this is our mental representation of what a variable is. 00:04:54.980 --> 00:04:57.080 It's some name for a value that can change. 00:04:57.080 --> 00:05:00.382 But in C, we have a particular syntax I'll use to make variables. 00:05:00.382 --> 00:05:02.840 As you all go through and work on the course's problem set, 00:05:02.840 --> 00:05:05.840 build your own programs, you really should get familiar with this syntax 00:05:05.840 --> 00:05:07.560 and how to read and write it here. 00:05:07.560 --> 00:05:11.395 So here we have the syntax in C to create this variable named calls. 00:05:11.395 --> 00:05:14.520 And there are some components to it that we should actually dive into here. 00:05:14.520 --> 00:05:16.910 So the first part is the variable's name. 00:05:16.910 --> 00:05:18.530 This name is calls. 00:05:18.530 --> 00:05:21.380 Notice how it's in the middle of this sentence here. 00:05:21.380 --> 00:05:25.010 We also have the variables type, this int type. 00:05:25.010 --> 00:05:27.577 And what does this int type stand for? 00:05:27.577 --> 00:05:29.660 What kind of number are we going to store in here? 00:05:29.660 --> 00:05:30.980 Could I ask somebody from down here? 00:05:30.980 --> 00:05:31.580 Yeah, go ahead. 00:05:31.580 --> 00:05:32.413 STUDENT: An integer. 00:05:32.413 --> 00:05:34.580 CARTER ZENKE: An integer, so a whole number, right? 00:05:34.580 --> 00:05:37.310 Then we have the value, in this case, 4, that value we're 00:05:37.310 --> 00:05:38.990 putting inside this variable here. 00:05:38.990 --> 00:05:43.460 But there's one piece we're missing, we haven't talked about yet. 00:05:43.460 --> 00:05:45.420 What haven't we named here yet? 00:05:45.420 --> 00:05:47.670 Could I ask somebody from maybe this side of the room? 00:05:47.670 --> 00:05:49.370 Take a guess? 00:05:49.370 --> 00:05:52.940 We talked about the name of the variable, the type, the value. 00:05:52.940 --> 00:05:55.430 What syntax haven't we shown yet? 00:05:55.430 --> 00:05:56.150 Yeah, go ahead. 00:05:56.150 --> 00:05:58.040 STUDENT: [INAUDIBLE] semicolon [INAUDIBLE].. 00:05:58.040 --> 00:05:59.498 CARTER ZENKE: The semicolon, right? 00:05:59.498 --> 00:06:03.200 So this is a statement in C. And every closing statement that we have, 00:06:03.200 --> 00:06:05.130 we want to include a semicolon there. 00:06:05.130 --> 00:06:07.130 The other thing we haven't talked about, the one 00:06:07.130 --> 00:06:08.390 piece we haven't talked about here, could I 00:06:08.390 --> 00:06:10.220 ask somebody from down here now? 00:06:10.220 --> 00:06:13.100 We have the semicolon, the value, the name, the data type. 00:06:13.100 --> 00:06:14.000 One more thing, yeah. 00:06:14.000 --> 00:06:14.660 STUDENT: The equals sign. 00:06:14.660 --> 00:06:17.810 CARTER ZENKE: The equals sign, and is it an equals sign, could I ask you? 00:06:17.810 --> 00:06:18.710 No, you're shaking your head. 00:06:18.710 --> 00:06:19.100 STUDENT: It's and assignment. 00:06:19.100 --> 00:06:21.142 CARTER ZENKE: It's an assignment operator, right? 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 00:06:24.290 --> 00:06:26.150 created for this variable named call. 00:06:26.150 --> 00:06:28.010 So if you say this in English here, we're 00:06:28.010 --> 00:06:30.540 going to create an integer variable. 00:06:30.540 --> 00:06:32.540 Notice how this type aligns with this type here. 00:06:32.540 --> 00:06:34.820 Named calls, the name lines up here. 00:06:34.820 --> 00:06:39.240 That gets, or that kind of stores this value 4, in this instance. 00:06:39.240 --> 00:06:41.360 So let's try again with another value here. 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. 00:06:47.300 --> 00:06:49.430 How do we say we create a what? 00:06:49.430 --> 00:06:50.150 STUDENT: Integer. 00:06:50.150 --> 00:06:52.025 CARTER ZENKE: (CLASS REPEATING AFTER) Integer 00:06:52.025 --> 00:06:56.190 that called x that gets the value 50. 00:06:56.190 --> 00:06:56.690 Nice. 00:06:56.690 --> 00:06:58.470 That's what it would be in English here. 00:06:58.470 --> 00:06:58.970 Amazing. 00:06:58.970 --> 00:07:01.280 Yeah, high fives all around. 00:07:01.280 --> 00:07:04.888 So why does C care so much about data types though? 00:07:04.888 --> 00:07:07.430 The very first thing we say here is not the name of the error 00:07:07.430 --> 00:07:09.870 but actually the data type. 00:07:09.870 --> 00:07:12.800 So we saw briefly this idea in lecture, but let's 00:07:12.800 --> 00:07:14.690 go ahead and talk to the person next to you. 00:07:14.690 --> 00:07:17.545 Why do you think C cares so much about these data types? 00:07:17.545 --> 00:07:19.670 Why is it the first thing we tell the computer when 00:07:19.670 --> 00:07:20.795 we make this variable here? 00:07:20.795 --> 00:07:22.940 We'll come back in just a minute. 00:07:22.940 --> 00:07:27.230 OK, let's come back and hear some ideas for why does C 00:07:27.230 --> 00:07:29.240 care so much about data types? 00:07:29.240 --> 00:07:32.990 Why is it the very first thing we put in a given line of code 00:07:32.990 --> 00:07:35.460 to make a variable in this case? 00:07:35.460 --> 00:07:38.060 And can we go back to maybe this side of the room now. 00:07:38.060 --> 00:07:40.430 And anyone in particular like to share what 00:07:40.430 --> 00:07:44.580 your group talked about, what ideas you had for this question here? 00:07:44.580 --> 00:07:45.900 Yeah, go ahead. 00:07:45.900 --> 00:07:48.660 STUDENT: Basically, we said that C wants to know data type. 00:07:48.660 --> 00:07:51.670 That's in order restrict the values that we input. 00:07:51.670 --> 00:07:54.685 For instance, if we're talking about [INAUDIBLE] input 00:07:54.685 --> 00:07:56.610 3.5, so for [INAUDIBLE]. 00:07:56.610 --> 00:07:58.257 So it limits [INAUDIBLE]. 00:07:58.257 --> 00:08:00.840 CARTER ZENKE: Yeah, there's some value for specificity, right? 00:08:00.840 --> 00:08:02.880 I want to be able to make sure that I'm not 00:08:02.880 --> 00:08:06.240 putting in some value I couldn't store in this variable, like a text, 00:08:06.240 --> 00:08:07.620 for example. 00:08:07.620 --> 00:08:08.610 Other reasons too. 00:08:08.610 --> 00:08:10.230 Could we go to maybe down here? 00:08:10.230 --> 00:08:11.085 Yeah. 00:08:11.085 --> 00:08:13.210 STUDENT: The computer needs to know how much memory 00:08:13.210 --> 00:08:15.940 to allocate to the [INAUDIBLE]. 00:08:15.940 --> 00:08:18.965 CARTER ZENKE: Yeah, and why would that matter? 00:08:18.965 --> 00:08:20.590 Can I ask you, get a little deeper too? 00:08:20.590 --> 00:08:21.850 Why does this size matter? 00:08:21.850 --> 00:08:27.160 STUDENT: Because I think an integer is a lot less size than-- 00:08:27.160 --> 00:08:29.800 a lot less [INAUDIBLE]. 00:08:29.800 --> 00:08:31.180 CARTER ZENKE: Totally, so using-- 00:08:31.180 --> 00:08:33.815 or storing different values makes that value take away 00:08:33.815 --> 00:08:35.440 different space in a computer's memory. 00:08:35.440 --> 00:08:38.710 So integers take up maybe 32 bits, but a character 00:08:38.710 --> 00:08:40.115 might take up only eight bits. 00:08:40.115 --> 00:08:41.990 And so it tells the compiler how much memory, 00:08:41.990 --> 00:08:44.200 how much space to reserve here too. 00:08:44.200 --> 00:08:47.560 One other reason that we saw a little bit in lecture as well is we 00:08:47.560 --> 00:08:52.870 have some binary here, these eight bits that can represent a certain, well, 00:08:52.870 --> 00:08:55.260 really anything. 00:08:55.260 --> 00:08:56.580 What does this represent here? 00:08:56.580 --> 00:08:59.050 Anyone know off the top of their head? 00:08:59.050 --> 00:08:59.670 Yeah. 00:08:59.670 --> 00:09:00.740 STUDENT: No, sorry. 00:09:00.740 --> 00:09:02.085 CARTER ZENKE: Oh, no, OK. 00:09:02.085 --> 00:09:03.210 I'd be surprised if you do. 00:09:03.210 --> 00:09:08.822 But one thing this represents is the number or the integer 65, right? 00:09:08.822 --> 00:09:10.530 But as we saw in lecture a little bit too 00:09:10.530 --> 00:09:14.880 is this can also represent the letter A. Those same binary bits can represent 00:09:14.880 --> 00:09:18.150 both 65 and this character A. So it's important we 00:09:18.150 --> 00:09:22.210 tell C which thing we're talking about here by giving us our data types. 00:09:22.210 --> 00:09:24.690 So that's the reason we have these data types here. 00:09:24.690 --> 00:09:28.860 If we want to go further into variables, we can update them if we'd like. 00:09:28.860 --> 00:09:34.230 And here we have on this very first line the declaration and initialization 00:09:34.230 --> 00:09:35.610 of this variable calls. 00:09:35.610 --> 00:09:38.260 And down below we just update that value. 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. 00:09:41.922 --> 00:09:44.880 And why would that be, if I can ask somebody from this side of the room 00:09:44.880 --> 00:09:45.510 now? 00:09:45.510 --> 00:09:50.360 Why is it on that top line but not that second line? 00:09:50.360 --> 00:09:52.225 Yeah, go ahead. 00:09:52.225 --> 00:09:54.600 STUDENT: On the first line you're initiating the variable 00:09:54.600 --> 00:09:58.160 and telling the computer what the data type is. 00:09:58.160 --> 00:10:01.160 On the second line, you're reassigning it to the new value, 00:10:01.160 --> 00:10:03.458 and it already knows that that variable [INAUDIBLE].. 00:10:03.458 --> 00:10:06.500 CARTER ZENKE: Yeah, so the computer is pretty good at remembering things, 00:10:06.500 --> 00:10:07.000 right? 00:10:07.000 --> 00:10:10.010 And once you've told it that this variable has a certain type, no need 00:10:10.010 --> 00:10:10.550 to tell it again. 00:10:10.550 --> 00:10:11.540 It already knows that. 00:10:11.540 --> 00:10:13.680 We can just keep going as we go. 00:10:13.680 --> 00:10:16.880 So here we're going to initialize and declare the variable calls. 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. 00:10:20.870 --> 00:10:23.630 Now, if we wanted to change that value, we could do it like this. 00:10:23.630 --> 00:10:25.730 We also have some operators at our disposal. 00:10:25.730 --> 00:10:28.820 We could add 1 to it using this plus sign. 00:10:28.820 --> 00:10:31.070 We could even subtract some numbers. 00:10:31.070 --> 00:10:34.340 We could maybe multiply some numbers using that star operator. 00:10:34.340 --> 00:10:36.360 We could even divide some numbers, and so on. 00:10:36.360 --> 00:10:37.610 So feel free to take a look at those. 00:10:37.610 --> 00:10:40.360 If you go through the course, I'm sure these come top of your head 00:10:40.360 --> 00:10:41.220 as we go through. 00:10:41.220 --> 00:10:47.240 But when it comes to assigning some variable, what looks like a function, 00:10:47.240 --> 00:10:49.357 right, that's where things get a little bit weird. 00:10:49.357 --> 00:10:52.190 We saw in lecture we're trying to get some input from a user, right? 00:10:52.190 --> 00:10:56.870 And we didn't say that maybe string name equals quote "Carter." 00:10:56.870 --> 00:11:00.200 We said it equals this, maybe, function, get string. 00:11:00.200 --> 00:11:03.290 Or here, if we wanted to store a value in calls, get int. 00:11:03.290 --> 00:11:06.560 And in this case, before we're reading left to right. 00:11:06.560 --> 00:11:09.210 But in this case, it's actually better to read right to left 00:11:09.210 --> 00:11:12.120 when you see this function call on the right-hand side. 00:11:12.120 --> 00:11:14.120 So what's happening here is, when you have 00:11:14.120 --> 00:11:16.245 this function on the right-hand side, this function 00:11:16.245 --> 00:11:19.370 is wanting to run and then give us back some return 00:11:19.370 --> 00:11:23.100 value that will be stored inside this variable on the left-hand side. 00:11:23.100 --> 00:11:26.300 So before anything else, we run this function on the right-hand side 00:11:26.300 --> 00:11:29.720 with some certain arguments, some input to that function 00:11:29.720 --> 00:11:31.550 tell it what exactly to do. 00:11:31.550 --> 00:11:32.840 That function runs. 00:11:32.840 --> 00:11:35.760 Maybe it asks the user for this integer number here. 00:11:35.760 --> 00:11:40.110 And we then store that value right where that function was called. 00:11:40.110 --> 00:11:42.660 And it then goes into that variable. 00:11:42.660 --> 00:11:46.290 And now we're back to simple variable assignment here. 00:11:46.290 --> 00:11:50.682 So usually, if you're simply assigning numbers, you can read left to right. 00:11:50.682 --> 00:11:53.390 But if you have these function calls, best to read right to left, 00:11:53.390 --> 00:11:56.390 and knowing that your function runs first and then assigns that value 00:11:56.390 --> 00:11:58.110 as we go through. 00:11:58.110 --> 00:12:00.335 So questions on this before we dive even deeper. 00:12:10.640 --> 00:12:12.510 Seeing none. 00:12:12.510 --> 00:12:16.160 So now that we have these values stored in these variables, 00:12:16.160 --> 00:12:19.160 it's probably worthwhile to think about how we print them to the screen. 00:12:19.160 --> 00:12:21.590 And here's what we saw a little bit in lecture as well, 00:12:21.590 --> 00:12:26.340 this % and then this i here. 00:12:26.340 --> 00:12:28.310 And what is this doing for us? 00:12:28.310 --> 00:12:30.695 Could I ask somebody from this middle section here. 00:12:30.695 --> 00:12:32.570 Let's go to the first row, if you don't mind. 00:12:32.570 --> 00:12:33.070 Yeah. 00:12:33.070 --> 00:12:35.732 STUDENT: It's a placeholder for our variable call. 00:12:35.732 --> 00:12:37.690 CARTER ZENKE: Yeah, placeholder for your calls. 00:12:37.690 --> 00:12:39.120 And why is it %i? 00:12:39.120 --> 00:12:39.620 Do you know? 00:12:39.620 --> 00:12:41.968 STUDENT: Because it's an integer [INAUDIBLE].. 00:12:41.968 --> 00:12:42.760 CARTER ZENKE: Yeah. 00:12:42.760 --> 00:12:44.630 STUDENT: i represents [INAUDIBLE]. 00:12:44.630 --> 00:12:46.010 CARTER ZENKE: Right, so %i. 00:12:46.010 --> 00:12:48.440 This % is kind of saying like, hey, this is a placeholder. 00:12:48.440 --> 00:12:50.810 And that i specifies what kind of variable 00:12:50.810 --> 00:12:52.830 can go inside of that placeholder there. 00:12:52.830 --> 00:12:55.460 So because call is an integer, it's %i. 00:12:55.460 --> 00:12:58.670 And that's called, more specifically a format code, as we might have said. 00:12:58.670 --> 00:13:01.520 And we have this value that we can store in that format code there. 00:13:01.520 --> 00:13:04.817 We could have multiple format codes and multiple values. 00:13:04.817 --> 00:13:06.150 We just separate them by commas. 00:13:06.150 --> 00:13:11.060 So you can imagine, for example, I have %i here and maybe %i later on. 00:13:11.060 --> 00:13:13.860 I could have calls and then comma, some other variable. 00:13:13.860 --> 00:13:15.980 And those would go inside those placeholders one 00:13:15.980 --> 00:13:21.020 by one, aligning with whatever order I put them in as we go through. 00:13:21.020 --> 00:13:24.590 Now, it's not worth memorizing any of these, 00:13:24.590 --> 00:13:27.725 but there are a great number of format codes for different data types. 00:13:27.725 --> 00:13:29.600 And as you work with a variety of data types, 00:13:29.600 --> 00:13:31.640 best you can maybe reference these once in a while. 00:13:31.640 --> 00:13:33.110 So here on the left-hand side, we have number 00:13:33.110 --> 00:13:35.690 format codes, like ints and longs, floats and doubles, 00:13:35.690 --> 00:13:38.420 and the right-hand side some chars and strings, 00:13:38.420 --> 00:13:40.100 where chars are individual characters. 00:13:40.100 --> 00:13:43.770 And strings are collections of characters as we go through. 00:13:43.770 --> 00:13:49.440 One thing we haven't quite seen as much yet is this long and double. 00:13:49.440 --> 00:13:54.100 Anyone want to hazard a guess as to what that might be here? 00:13:54.100 --> 00:13:56.620 Could I ask somebody from this middle row again? 00:13:59.145 --> 00:14:00.895 What is this long and double doing for us? 00:14:05.710 --> 00:14:06.400 Yeah, go ahead. 00:14:06.400 --> 00:14:11.834 STUDENT: Does the long [INAUDIBLE] more room [INAUDIBLE] 00:14:11.834 --> 00:14:16.023 you have to have the long to [INAUDIBLE]?? 00:14:16.023 --> 00:14:17.190 CARTER ZENKE: Yeah, exactly. 00:14:17.190 --> 00:14:22.242 So here we had an integer that could store up to four billion values. 00:14:22.242 --> 00:14:23.950 But when that's not enough, we might want 00:14:23.950 --> 00:14:27.150 to have a longer value, in this case, called a long, 00:14:27.150 --> 00:14:29.410 that will have twice as much space for us. 00:14:29.410 --> 00:14:32.322 We can store twice as many different combinations as we go through. 00:14:32.322 --> 00:14:33.280 Really, more than that. 00:14:33.280 --> 00:14:36.310 We're just having twice as many bits to represent that information. 00:14:36.310 --> 00:14:39.330 And similarly, for the float, that's a decimal number. 00:14:39.330 --> 00:14:43.030 But this double is called a double precision floating point. 00:14:43.030 --> 00:14:46.260 So we'd have twice as many decimals, twice many binary digits 00:14:46.260 --> 00:14:48.630 representing that decimal number there for us 00:14:48.630 --> 00:14:52.610 as well, so just more space for us to use as we go through. 00:14:52.610 --> 00:14:54.740 Now, what we'll do here is a brief exercise 00:14:54.740 --> 00:15:01.107 and invite you to load up code.cs50.io on your own laptop here. 00:15:01.107 --> 00:15:03.440 And you should see something that looks a bit like this. 00:15:03.440 --> 00:15:06.125 Maybe you have a File Explorer on the left-hand side. 00:15:11.083 --> 00:15:13.250 But you should certainly have a terminal down below. 00:15:13.250 --> 00:15:13.958 I see a question. 00:15:13.958 --> 00:15:14.753 Yeah. 00:15:14.753 --> 00:15:17.200 STUDENT: I'm not sure if this was a typo to be like this. 00:15:17.200 --> 00:15:18.778 But if you go back to the slide-- 00:15:18.778 --> 00:15:19.820 CARTER ZENKE: Yeah, yeah. 00:15:22.950 --> 00:15:26.514 STUDENT: So the double says f, and the float says f. 00:15:26.514 --> 00:15:27.450 Is that correct? 00:15:27.450 --> 00:15:28.700 CARTER ZENKE: That is correct. 00:15:28.700 --> 00:15:31.770 So both the float and the double have the same format code, yeah. 00:15:31.770 --> 00:15:32.550 Good question. 00:15:32.550 --> 00:15:35.847 STUDENT: So how does the computer [INAUDIBLE] 00:15:35.847 --> 00:15:38.508 recognize between [INAUDIBLE]? 00:15:38.508 --> 00:15:40.800 CARTER ZENKE: How does the computer recognize each one? 00:15:40.800 --> 00:15:45.160 In this case, it doesn't quite matter because, in this case, 00:15:45.160 --> 00:15:47.580 the float and the double are both decimal numbers. 00:15:47.580 --> 00:15:51.390 And so the computer knows when it sees %f, I'm printing a decimal number. 00:15:51.390 --> 00:15:53.950 It just so happens to be that the double is twice as long. 00:15:53.950 --> 00:15:56.400 And so it just prints that many more-- 00:15:56.400 --> 00:15:59.310 well, it can print that many more spaces after the decimal point 00:15:59.310 --> 00:16:01.230 if you'd like it to. 00:16:01.230 --> 00:16:03.450 Yeah, other questions here before we move on? 00:16:08.070 --> 00:16:10.950 All right, so we'll work on a brief exercise here. 00:16:10.950 --> 00:16:15.260 And once you've loaded up your IDE in code.cs50.io, 00:16:15.260 --> 00:16:18.380 you go ahead and create an application called phonebook.c. 00:16:18.380 --> 00:16:21.800 And the goal is to prompt the user for these three things 00:16:21.800 --> 00:16:24.680 and then print them back out to the user as confirmation 00:16:24.680 --> 00:16:27.540 that this data is stored in your program. 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. 00:16:32.810 --> 00:16:35.225 And I would make a new file. 00:16:35.225 --> 00:16:37.100 What would I do to make a new file down here? 00:16:37.100 --> 00:16:40.730 Could I ask somebody from maybe this right-hand bottom side? 00:16:40.730 --> 00:16:44.720 How can I make a new file in this program here? 00:16:44.720 --> 00:16:45.290 Yeah. 00:16:45.290 --> 00:16:46.838 STUDENT: Code and file name. 00:16:46.838 --> 00:16:48.630 CARTER ZENKE: Yeah, code and the file name. 00:16:48.630 --> 00:16:51.860 So I could do code, if it will load here. 00:16:54.510 --> 00:16:55.160 Let me refresh. 00:16:55.160 --> 00:17:00.270 But on your own, you could certainly do code and in the file name here. 00:17:00.270 --> 00:17:04.520 And for our purposes, I'll do code space maybe phonebook.c. 00:17:04.520 --> 00:17:09.123 But you could call your program whatever else you'd like to call it here. 00:17:09.123 --> 00:17:10.790 And maybe yours is doing the same thing. 00:17:10.790 --> 00:17:12.290 We'll be back in just a minute here. 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? 00:17:22.008 --> 00:17:23.550 What's the first thing you might add? 00:17:23.550 --> 00:17:25.560 We saw this in lecture. 00:17:25.560 --> 00:17:26.280 Yeah, go ahead. 00:17:26.280 --> 00:17:27.000 STUDENT: The header. 00:17:27.000 --> 00:17:29.375 CARTER ZENKE: The header files or these libraries, right? 00:17:29.375 --> 00:17:32.150 And do you remember the syntax for that? 00:17:32.150 --> 00:17:37.330 STUDENT: Hashtag include, and then the caret thing. 00:17:37.330 --> 00:17:41.080 CARTER ZENKE: Yeah, hashtag include this little caret thing. 00:17:41.080 --> 00:17:42.700 Let's see if this one is loaded now. 00:17:42.700 --> 00:17:46.030 Not quite, but this hashtag include is saying, 00:17:46.030 --> 00:17:49.690 I'm going to try to get some file in my computer's memory 00:17:49.690 --> 00:17:54.130 that has these functions declared for me that I will then use in my own program 00:17:54.130 --> 00:17:54.830 here. 00:17:54.830 --> 00:18:01.990 So what two header files might you want to include in your program here? 00:18:01.990 --> 00:18:02.590 Yeah. 00:18:02.590 --> 00:18:04.592 STUDENT: Standardio.h [INAUDIBLE]. 00:18:04.592 --> 00:18:07.300 CARTER ZENKE: Nice, so two off the bat that you often want to use 00:18:07.300 --> 00:18:11.440 are standardio.h or stdio.h cs50.h. 00:18:11.440 --> 00:18:13.720 Both of these have some pretty common functions 00:18:13.720 --> 00:18:15.880 you'll be using as you write your own program. 00:18:22.030 --> 00:18:26.770 And we'll wait for this to continue connecting, and what we'll actually do 00:18:26.770 --> 00:18:29.770 is, now that you maybe hopefully have your file open, 00:18:29.770 --> 00:18:33.340 you have your header files at the top, we'll take some time, 00:18:33.340 --> 00:18:35.670 let's say maybe 10 minutes, to work on this on our own. 00:18:35.670 --> 00:18:36.820 I'll put the slide back up. 00:18:36.820 --> 00:18:39.070 And we'll come back in those 10 minutes and share 00:18:39.070 --> 00:18:40.510 how you approached this problem. 00:18:40.510 --> 00:18:43.000 Maybe if the staff would like to run around and help you as you work. 00:18:43.000 --> 00:18:44.208 Feel free to raise your hand. 00:18:44.208 --> 00:18:46.070 We'll come around and help you out. 00:18:46.070 --> 00:18:47.980 All right, let's come back. 00:18:47.980 --> 00:18:51.148 And we'll be working in a slightly different environment 00:18:51.148 --> 00:18:53.690 just because the internet is not quite what we want it to be. 00:18:53.690 --> 00:18:56.773 But we're going to make here our own phone book file where we can actually 00:18:56.773 --> 00:18:58.310 store the data we want to store. 00:18:58.310 --> 00:19:00.545 So if you remember our slide back here, we 00:19:00.545 --> 00:19:02.170 wanted to store a few different things. 00:19:02.170 --> 00:19:07.630 You wanted to store the user's name, their phone number, 00:19:07.630 --> 00:19:09.460 and I think it was their address. 00:19:09.460 --> 00:19:14.980 Let me actually take a look at that slide again, if we go back over here. 00:19:14.980 --> 00:19:17.390 A name, an age, and a phone number. 00:19:17.390 --> 00:19:21.550 So here we have this template for our program, 00:19:21.550 --> 00:19:23.650 but what are we missing right now? 00:19:23.650 --> 00:19:25.720 We have our header files, these libraries where 00:19:25.720 --> 00:19:27.220 I have functions we're going to use. 00:19:27.220 --> 00:19:29.665 What else are we missing in this program right here? 00:19:29.665 --> 00:19:30.790 Could I ask somebody from-- 00:19:30.790 --> 00:19:31.450 Yeah, down here. 00:19:31.450 --> 00:19:32.350 STUDENT: Int main void. 00:19:32.350 --> 00:19:33.808 CARTER ZENKE: Int main void, right. 00:19:33.808 --> 00:19:37.330 So I'll type int main and void up here. 00:19:37.330 --> 00:19:39.340 And this just symbolizes-- this is the kind 00:19:39.340 --> 00:19:41.650 of when flag clicked block in Scratch. 00:19:41.650 --> 00:19:43.900 This is going to be the main part of our program here. 00:19:43.900 --> 00:19:46.330 We're defining this new function called Main that 00:19:46.330 --> 00:19:49.160 will be the main part of our program. 00:19:49.160 --> 00:19:51.340 Now, the first thing we want to do is probably 00:19:51.340 --> 00:19:54.200 prompt the user for some information. 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 00:20:00.100 --> 00:20:01.420 we're trying to story here? 00:20:01.420 --> 00:20:02.020 Could I ask over here? 00:20:02.020 --> 00:20:02.650 Yeah, go ahead. 00:20:02.650 --> 00:20:03.400 STUDENT: Get string. 00:20:03.400 --> 00:20:04.442 CARTER ZENKE: Get string. 00:20:04.442 --> 00:20:07.855 And how did you make your variable in this case? 00:20:07.855 --> 00:20:11.450 STUDENT: So because our variable is a string, I put string. 00:20:11.450 --> 00:20:12.928 And then I titled my variable name. 00:20:12.928 --> 00:20:13.720 CARTER ZENKE: Nice. 00:20:13.720 --> 00:20:18.958 STUDENT: And then I said equals get string what's your name. 00:20:18.958 --> 00:20:20.750 CARTER ZENKE: Get string, what's your name. 00:20:20.750 --> 00:20:21.708 So something like this? 00:20:21.708 --> 00:20:23.320 STUDENT: With a space after the name. 00:20:23.320 --> 00:20:25.820 CARTER ZENKE: Yeah, and why would you have that space there? 00:20:25.820 --> 00:20:26.540 STUDENT: Just for readability. 00:20:26.540 --> 00:20:27.240 CARTER ZENKE: Just readability, right? 00:20:27.240 --> 00:20:29.900 If we don't have that space there, if we maybe remove this, 00:20:29.900 --> 00:20:31.970 the user will be typing in their name right up 00:20:31.970 --> 00:20:33.887 against that question mark there, which is not 00:20:33.887 --> 00:20:36.000 what we want to have happen here. 00:20:36.000 --> 00:20:39.080 So it seems pretty logical to represent a name with a string. 00:20:39.080 --> 00:20:40.873 But how about the age here? 00:20:40.873 --> 00:20:42.540 Can I go maybe to this side of the room? 00:20:42.540 --> 00:20:47.840 How did you all work on getting the users the contact's age? 00:20:47.840 --> 00:20:48.440 Yeah. 00:20:48.440 --> 00:20:57.780 STUDENT: I did int age equals get int, and then in brackets I had-- 00:20:57.780 --> 00:21:00.450 CARTER ZENKE: What's your age with a space, right? 00:21:00.450 --> 00:21:02.980 And then the semicolon. 00:21:02.980 --> 00:21:03.480 Perfect. 00:21:03.480 --> 00:21:05.200 So that'll close that statement. 00:21:05.200 --> 00:21:07.800 And again, this function on the right will be called. 00:21:07.800 --> 00:21:10.590 It will run and then give us back some return value, which is 00:21:10.590 --> 00:21:15.900 whatever the user typed in, and store it inside of this variable named age. 00:21:15.900 --> 00:21:17.460 And then comes one with some-- 00:21:17.460 --> 00:21:19.300 question here? 00:21:19.300 --> 00:21:27.280 STUDENT: Can we also write string age [INAUDIBLE]?? 00:21:27.280 --> 00:21:29.906 CARTER ZENKE: Yeah, so did you try that one here? 00:21:29.906 --> 00:21:34.283 STUDENT: Yeah, what's the [INAUDIBLE]? 00:21:34.283 --> 00:21:35.450 CARTER ZENKE: Good question. 00:21:35.450 --> 00:21:38.330 So we could do either of these here. 00:21:38.330 --> 00:21:39.520 One is an integer. 00:21:39.520 --> 00:21:41.290 One is a string. 00:21:41.290 --> 00:21:45.140 But you could imagine, if I later wanted to do something like this, 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, 00:21:49.780 --> 00:21:51.520 maybe it's their birthday next year. 00:21:51.520 --> 00:21:56.107 So I say age equals age plus 1. 00:21:56.107 --> 00:21:58.190 Is that going to work with a string, do you think? 00:21:58.190 --> 00:21:58.930 STUDENT: No. 00:21:58.930 --> 00:22:04.750 CARTER ZENKE: No, because I'm going to add 1 to this quote unquote maybe 40, 00:22:04.750 --> 00:22:05.650 for example. 00:22:05.650 --> 00:22:07.713 And I can't add numbers to strings. 00:22:07.713 --> 00:22:09.130 That's not going to happen for me. 00:22:09.130 --> 00:22:11.800 So it's best when you're making these to just think 00:22:11.800 --> 00:22:15.650 about what you want to do with them as you work in your program here. 00:22:15.650 --> 00:22:18.340 So I want to maybe change my age as I go through. 00:22:18.340 --> 00:22:24.550 So I'll actually maybe make this an integer here and say get int instead. 00:22:24.550 --> 00:22:25.830 And now for the phone number. 00:22:25.830 --> 00:22:28.560 If you go to this middle section here, how did you all 00:22:28.560 --> 00:22:32.090 choose to represent the phone number? 00:22:32.090 --> 00:22:33.500 Yeah. 00:22:33.500 --> 00:22:36.670 STUDENT: Long number is equal to get long. 00:22:36.670 --> 00:22:39.610 CARTER ZENKE: Long number is equal to get long. 00:22:39.610 --> 00:22:41.485 And you'd ask maybe what's your phone number. 00:22:41.485 --> 00:22:42.710 STUDENT: Yeah. 00:22:42.710 --> 00:22:44.668 CARTER ZENKE: And I think I'm most curious here 00:22:44.668 --> 00:22:46.840 about why you chose a long. 00:22:46.840 --> 00:22:49.550 Why would you have that? 00:22:49.550 --> 00:22:50.540 Yeah. 00:22:50.540 --> 00:22:53.279 STUDENT: Because it's more than 2 billion, 00:22:53.279 --> 00:22:56.703 so you need the extra [INAUDIBLE]. 00:22:56.703 --> 00:22:58.620 CARTER ZENKE: Yeah, so a phone number is what? 00:22:58.620 --> 00:23:01.060 Maybe 10 digits long. 00:23:01.060 --> 00:23:05.580 And so if I had any phone number that begins with anything higher than two, 00:23:05.580 --> 00:23:07.447 that's going to be higher than two billion, 00:23:07.447 --> 00:23:10.030 which is the highest positive number I can have as an integer. 00:23:10.030 --> 00:23:11.760 So I want to actually have a long here. 00:23:11.760 --> 00:23:12.390 Yeah, question. 00:23:12.390 --> 00:23:14.360 STUDENT: Could you use a string to-- 00:23:14.360 --> 00:23:16.360 CARTER ZENKE: You absolutely could use a string. 00:23:16.360 --> 00:23:20.983 And why would maybe you advocate for using a string? 00:23:20.983 --> 00:23:24.280 STUDENT: [INAUDIBLE] 00:23:24.280 --> 00:23:26.140 CARTER ZENKE: Nice, so hyphens. 00:23:26.140 --> 00:23:30.130 You could also include, if you're maybe in different countries, 00:23:30.130 --> 00:23:34.510 plus 1 for this country code plus other country codes as well. 00:23:34.510 --> 00:23:37.990 You could even have parentheses if you'd like to put the area code in front. 00:23:37.990 --> 00:23:39.070 Yeah. 00:23:39.070 --> 00:23:44.110 STUDENT: Also, I think that if you use an integer or a long, 00:23:44.110 --> 00:23:48.960 you are supposed to be able to calculate stuff based on that number. 00:23:48.960 --> 00:23:52.630 And usually, you don't add stuff or divide the phone number. 00:23:52.630 --> 00:23:58.008 So there is no actual purpose to ask for a [INAUDIBLE].. 00:23:58.008 --> 00:23:59.300 CARTER ZENKE: Yeah, definitely. 00:23:59.300 --> 00:24:01.758 So we tend not to add things to phone numbers. 00:24:01.758 --> 00:24:04.550 So why store it as number we could add things to or subtract things 00:24:04.550 --> 00:24:06.500 to or divide or whatever we'd like, right? 00:24:06.500 --> 00:24:08.990 Good question there. 00:24:08.990 --> 00:24:10.760 So we have it as a string here. 00:24:10.760 --> 00:24:14.120 The string also preserves things like leading zeros. 00:24:14.120 --> 00:24:17.388 Maybe there's an odd chance that somebody has a 000 number, 00:24:17.388 --> 00:24:20.180 and we could store that with a string but not, for example, a long. 00:24:20.180 --> 00:24:23.750 We tend to remove those leading zeros in that case. 00:24:23.750 --> 00:24:24.770 Yeah. 00:24:24.770 --> 00:24:27.690 STUDENT: But [INAUDIBLE] to use the long [INAUDIBLE] one 00:24:27.690 --> 00:24:29.520 that doesn't really require that? 00:24:29.520 --> 00:24:31.860 CARTER ZENKE: If I were to say maybe long age here? 00:24:31.860 --> 00:24:32.970 STUDENT: Yeah. 00:24:32.970 --> 00:24:36.150 CARTER ZENKE: And maybe get long to have parity. 00:24:36.150 --> 00:24:38.280 Nothing bad would happen here. 00:24:38.280 --> 00:24:41.940 I'm probably just using twice as much space as I would need to. 00:24:41.940 --> 00:24:45.490 So generally, I don't think people have an age above two billion. 00:24:45.490 --> 00:24:49.380 And so I would probably want to just use a regular integer to store that value. 00:24:49.380 --> 00:24:51.420 Nice. 00:24:51.420 --> 00:24:55.230 Other questions on these representations of this information here? 00:24:58.700 --> 00:25:00.920 OK, so we'll go with this integer. 00:25:00.920 --> 00:25:03.392 And then just real quickly to review our format code, 00:25:03.392 --> 00:25:05.600 so let's say I wanted to print out some of this data. 00:25:05.600 --> 00:25:07.160 I could say printf. 00:25:07.160 --> 00:25:11.100 And let's say, maybe age is-- 00:25:11.100 --> 00:25:14.090 what would I then say? 00:25:14.090 --> 00:25:15.590 Maybe just call it out. 00:25:15.590 --> 00:25:16.425 STUDENT: %i. 00:25:16.425 --> 00:25:17.300 CARTER ZENKE: %i, OK. 00:25:17.300 --> 00:25:18.050 Age is %i. 00:25:18.050 --> 00:25:20.720 I'm going to put period here. 00:25:20.720 --> 00:25:24.830 Name is %s. 00:25:24.830 --> 00:25:32.430 And let's go ahead and say phone number is %s, all right? 00:25:32.430 --> 00:25:34.500 And now to store this, as we saw earlier, 00:25:34.500 --> 00:25:37.587 I could simply just put these variables in the same order 00:25:37.587 --> 00:25:39.670 I want them to show up in those placeholders here. 00:25:39.670 --> 00:25:41.450 So I could say, OK, age is first. 00:25:41.450 --> 00:25:44.170 So I'll say age, then name. 00:25:44.170 --> 00:25:45.000 So I'll say name. 00:25:45.000 --> 00:25:47.880 And then finally number, so I'll say number and close everything out 00:25:47.880 --> 00:25:49.590 with the semicolon here. 00:25:49.590 --> 00:25:53.580 And I were to make this, I just type make phone book and run it. 00:25:53.580 --> 00:25:57.240 I would then see this information printed back out to me on the screen 00:25:57.240 --> 00:25:58.620 as such. 00:25:58.620 --> 00:26:00.945 So questions on this before we move on. 00:26:04.510 --> 00:26:10.590 All right, so one of the other building blocks 00:26:10.590 --> 00:26:12.990 that we have besides these variables and representations 00:26:12.990 --> 00:26:14.853 here are these conditionals and these loops. 00:26:14.853 --> 00:26:16.770 And we saw these in lecture as well, but we'll 00:26:16.770 --> 00:26:19.780 use these now for the course's lab to work on together. 00:26:19.780 --> 00:26:24.180 So first thing here, we have this question. 00:26:24.180 --> 00:26:27.600 Have I called less than one time? 00:26:27.600 --> 00:26:32.280 And if I have, then I'll say call more often. 00:26:32.280 --> 00:26:34.500 But just to get some vocabulary down here, 00:26:34.500 --> 00:26:38.880 this on the inside of that conditional is our Boolean expression. 00:26:38.880 --> 00:26:40.170 It's either yes or no. 00:26:40.170 --> 00:26:43.750 Have I called less than one time or not? 00:26:43.750 --> 00:26:46.100 On the outside here is that conditional itself. 00:26:46.100 --> 00:26:49.550 So conditionals have these Boolean expressions on the inside of them. 00:26:49.550 --> 00:26:51.550 I could make this a little more advanced though, 00:26:51.550 --> 00:26:55.970 and I could have this else in the middle. 00:26:55.970 --> 00:26:58.540 And what would that do for me? 00:26:58.540 --> 00:27:02.260 Could I ever execute these two things at the same time? 00:27:02.260 --> 00:27:03.930 Could I ask over here? 00:27:03.930 --> 00:27:06.025 Could I ever do these two things at the same time? 00:27:06.025 --> 00:27:06.900 Shaking your head no. 00:27:06.900 --> 00:27:07.230 Right. 00:27:07.230 --> 00:27:09.170 So these two things are mutually exclusive. 00:27:09.170 --> 00:27:12.780 If I have this else here, I'm saying it's either one or the other. 00:27:12.780 --> 00:27:15.450 If calls is less than 1, I will do that first thing. 00:27:15.450 --> 00:27:18.420 If it's not, I will do that second thing. 00:27:18.420 --> 00:27:21.660 You can chain these statements together if you saw-- even in Scratch. 00:27:21.660 --> 00:27:25.140 You could say if and then else if and then else if, 00:27:25.140 --> 00:27:26.400 and finally, it's long else. 00:27:26.400 --> 00:27:30.480 In general, we have these if statements followed by these else 00:27:30.480 --> 00:27:33.870 if statements, followed by if you need them, that final else to say, 00:27:33.870 --> 00:27:37.815 like a catch all for everything else that we could happen have happen here. 00:27:37.815 --> 00:27:40.440 For loops though, if you wanted to do something multiple times, 00:27:40.440 --> 00:27:43.300 we have something that looks a bit like this, this while loop. 00:27:43.300 --> 00:27:45.450 And what would this print to the screen? 00:27:45.450 --> 00:27:47.760 Could I ask somebody from maybe this side of the room? 00:27:47.760 --> 00:27:49.968 What would you see on the screen with this loop here? 00:27:56.110 --> 00:27:56.740 Yeah, go ahead. 00:27:56.740 --> 00:27:59.660 STUDENT: 0, 1, 2, 3 up to 9. 00:27:59.660 --> 00:28:01.410 CARTER ZENKE: Up to 9, and why not the 10? 00:28:01.410 --> 00:28:03.598 STUDENT: Because it's while i is less than 10. 00:28:03.598 --> 00:28:04.390 CARTER ZENKE: Nice. 00:28:04.390 --> 00:28:07.660 Yeah, so we wouldn't see the 10 because we're going to stop while-- 00:28:07.660 --> 00:28:11.840 we're going to stop if this is not true. 00:28:11.840 --> 00:28:14.650 And so 10 is not less than 10, so we would stop 00:28:14.650 --> 00:28:17.560 and we wouldn't do the code on the inside, right? 00:28:17.560 --> 00:28:21.500 So to break things down even further, we have our initialization of this loop. 00:28:21.500 --> 00:28:23.560 I is first set to 0. 00:28:23.560 --> 00:28:25.303 Then we have our Boolean expression, this 00:28:25.303 --> 00:28:27.220 question we're going to ask, either yes or no. 00:28:27.220 --> 00:28:29.000 Is this case true or is it not? 00:28:29.000 --> 00:28:33.460 And then finally, this implementation, changing that value as we go through. 00:28:33.460 --> 00:28:36.010 And finally, when our Boolean expression is no longer true, 00:28:36.010 --> 00:28:38.865 we will not be inside of that loop there. 00:28:38.865 --> 00:28:39.740 This is really handy. 00:28:39.740 --> 00:28:43.150 We can actually try to make our code run a certain number of times. 00:28:43.150 --> 00:28:46.270 And this is so handy that C actually has our own syntax 00:28:46.270 --> 00:28:48.950 to write this kind of loop in even abbreviated format. 00:28:48.950 --> 00:28:53.770 So here we call a for loop that has all those same elements but now just 00:28:53.770 --> 00:28:54.910 on one clean line. 00:28:54.910 --> 00:28:58.670 We have this initialization, this Boolean expression, 00:28:58.670 --> 00:29:00.910 and then this incrementation here. 00:29:00.910 --> 00:29:03.160 And we'll do whatever a piece of code is on the inside 00:29:03.160 --> 00:29:06.640 as long as this Boolean expression is true. 00:29:06.640 --> 00:29:11.070 So questions on this kind of syntax for these loops? 00:29:11.070 --> 00:29:12.775 Pretty good. 00:29:12.775 --> 00:29:15.150 And finally, I just wanted to get a little more advanced. 00:29:15.150 --> 00:29:16.440 We have this-- yeah, go ahead. 00:29:16.440 --> 00:29:19.290 STUDENT: I have a question [INAUDIBLE]. 00:29:19.290 --> 00:29:25.193 Why int [INAUDIBLE] parentheses [INAUDIBLE]?? 00:29:25.193 --> 00:29:26.360 CARTER ZENKE: Good question. 00:29:26.360 --> 00:29:27.360 So you saw the int here? 00:29:27.360 --> 00:29:28.180 STUDENT: Yeah. 00:29:28.180 --> 00:29:29.972 CARTER ZENKE: And why is that there, right, 00:29:29.972 --> 00:29:34.540 because previously, we had the int i already done for us, right? 00:29:34.540 --> 00:29:37.280 So these are actually two different contexts. 00:29:37.280 --> 00:29:41.230 So here, I have created this variable named i, 00:29:41.230 --> 00:29:43.570 and I'm going to use it inside of this loop. 00:29:43.570 --> 00:29:46.900 And this variable i, because it's outside of this while loop, 00:29:46.900 --> 00:29:48.940 will persist as we go through my program. 00:29:48.940 --> 00:29:51.430 So I could use this variable i again. 00:29:51.430 --> 00:29:56.200 In the for loop though, if I were to create this variable i anew, 00:29:56.200 --> 00:29:58.900 I can only use it inside this for loop. 00:29:58.900 --> 00:30:01.340 I could not use it outside of that for loop. 00:30:01.340 --> 00:30:04.030 So as long as it's inside for loop, I can't use whatever 00:30:04.030 --> 00:30:05.860 variable I create here outside of it. 00:30:05.860 --> 00:30:09.040 It has to be limited to the inside of that for loop there. 00:30:09.040 --> 00:30:10.420 Does that help? 00:30:10.420 --> 00:30:11.433 Great. 00:30:11.433 --> 00:30:12.475 Other questions here too. 00:30:15.810 --> 00:30:18.713 STUDENT: I see no semicolon at the end of the for loop. 00:30:18.713 --> 00:30:20.630 CARTER ZENKE: Yeah, why is there no semicolon? 00:30:20.630 --> 00:30:24.680 Well, probably a rule of thumb is this. 00:30:24.680 --> 00:30:27.770 Semicolons come after complete statements. 00:30:27.770 --> 00:30:31.970 And this for loop is kind of setting up some set of statements. 00:30:31.970 --> 00:30:35.550 It's saying, I'm going to eventually have something I want to do, 00:30:35.550 --> 00:30:36.750 but it's not there yet. 00:30:36.750 --> 00:30:38.960 So I'm going to actually have these braces 00:30:38.960 --> 00:30:43.140 to tell what statements I want to execute sequentially as I go through. 00:30:43.140 --> 00:30:46.670 So you'll get used to this as you go through and write more code. 00:30:46.670 --> 00:30:49.250 But in general, statements end with a semicolon, 00:30:49.250 --> 00:30:51.530 but things that set up statements generally don't. 00:30:51.530 --> 00:30:55.120 They might have these braces, yeah. 00:30:55.120 --> 00:30:56.203 Yeah. 00:30:56.203 --> 00:30:59.584 STUDENT: So you mentioned [INAUDIBLE] variable 00:30:59.584 --> 00:31:02.010 i only exists within the parameter. 00:31:02.010 --> 00:31:03.270 CARTER ZENKE: Exactly. 00:31:03.270 --> 00:31:07.730 STUDENT: Does that mean when you do multiple for loops in a row 00:31:07.730 --> 00:31:09.592 not with [INAUDIBLE]? 00:31:09.592 --> 00:31:11.300 CARTER ZENKE: Yeah, it's a good question. 00:31:11.300 --> 00:31:17.570 So let's say if I go back to my actual programming environment here, 00:31:17.570 --> 00:31:20.270 let's say I have a loop. 00:31:20.270 --> 00:31:25.040 And I'm going to say four int i equals 0. i is less than 10. 00:31:25.040 --> 00:31:26.930 i++. 00:31:26.930 --> 00:31:31.370 So now, I can access i inside of these two braces. 00:31:31.370 --> 00:31:32.750 That's something called scope. 00:31:32.750 --> 00:31:33.958 It's a scope of the variable. 00:31:33.958 --> 00:31:35.900 It persists in this space here. 00:31:35.900 --> 00:31:38.310 I can't use i out here. 00:31:38.310 --> 00:31:42.577 So I can't access it there, but I could, for example, have another for loop 00:31:42.577 --> 00:31:43.160 on the inside. 00:31:43.160 --> 00:31:47.030 I could say for int j equals i. 00:31:47.030 --> 00:31:49.670 That's totally valid because i's inside this loop. 00:31:49.670 --> 00:31:53.420 Maybe j is less than 10 in this case, and then j++. 00:31:53.420 --> 00:31:55.760 And I do something else in that. 00:31:55.760 --> 00:31:59.720 And actually, j is not accessible here. 00:31:59.720 --> 00:32:03.560 I can't do that, but it is accessible inside that loop there. 00:32:07.878 --> 00:32:08.920 Other questions here too? 00:32:08.920 --> 00:32:10.108 Yeah. 00:32:10.108 --> 00:32:11.400 STUDENT: I'm a little confused. 00:32:11.400 --> 00:32:15.910 Where's the part that, I guess, senses what i is? 00:32:15.910 --> 00:32:19.600 It just appears [INAUDIBLE]. 00:32:19.600 --> 00:32:23.380 CARTER ZENKE: Yeah, can you tell me what you mean by senses what i is? 00:32:23.380 --> 00:32:29.715 STUDENT: Right, so because it's like a Boolean expression, 00:32:29.715 --> 00:32:36.540 right, whether i is greater than 1 or less than 1, like that kind of thing. 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? 00:32:41.260 --> 00:32:44.030 CARTER ZENKE: Yeah, so probably best to think of it like this, 00:32:44.030 --> 00:32:46.630 where my code is read top to bottom. 00:32:46.630 --> 00:32:50.870 And when it gets to this for loop, it's read kind of left to right. 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 00:32:55.330 --> 00:32:57.190 and set it to 0. 00:32:57.190 --> 00:33:01.480 Then I'm going to ask the question, is i less than 10? 00:33:01.480 --> 00:33:04.930 OK, it is, so I'll actually go inside this for loop 00:33:04.930 --> 00:33:08.740 and maybe run this code on the inside. 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. 00:33:15.880 --> 00:33:16.660 And I'll do this. 00:33:16.660 --> 00:33:18.987 I'll increase i by 1. 00:33:18.987 --> 00:33:20.320 And I'll ask the question again. 00:33:20.320 --> 00:33:21.170 Is i less than 10? 00:33:21.170 --> 00:33:21.670 It is. 00:33:21.670 --> 00:33:23.230 OK, I'll do this code again. 00:33:23.230 --> 00:33:26.635 Once it finishes, I'll increase i by 1 again, ask the question, keep going, 00:33:26.635 --> 00:33:27.260 and keep going. 00:33:27.260 --> 00:33:29.167 Does that help? 00:33:29.167 --> 00:33:30.000 STUDENT: OK, got it. 00:33:30.000 --> 00:33:34.240 So the input was i equals 0 first, and then [INAUDIBLE].. 00:33:34.240 --> 00:33:36.790 CARTER ZENKE: Exactly, this is our initialization. 00:33:36.790 --> 00:33:40.870 This is the first time for loop runs, set it to this value. 00:33:40.870 --> 00:33:44.860 Every time this code on the inside finishes running, ask-- 00:33:44.860 --> 00:33:49.150 or actually, before we start running this code, ask this question. 00:33:49.150 --> 00:33:51.810 If this is true, run this code. 00:33:51.810 --> 00:33:55.380 And we get to the bottom, increase i by 1 or do whatever is here, yeah. 00:33:55.380 --> 00:33:56.880 STUDENT: This should do it 10 times? 00:33:56.880 --> 00:33:58.880 CARTER ZENKE: This should do it 10 times, right? 00:33:58.880 --> 00:34:03.420 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. 00:34:03.420 --> 00:34:07.366 It won't do 10 because that is not less than 10. 00:34:07.366 --> 00:34:09.570 STUDENT: Is this called a do while? 00:34:09.570 --> 00:34:13.050 CARTER ZENKE: It's called a for loop just because of the 4 on the-- right 00:34:13.050 --> 00:34:13.792 there. 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 00:34:17.100 --> 00:34:18.699 here. 00:34:18.699 --> 00:34:22.560 The do while loop has the words do while in it, right? 00:34:22.560 --> 00:34:27.360 And what this is going to do is actually not ask a question first. 00:34:27.360 --> 00:34:32.010 Just unconditionally, run this piece of code and then ask the question later. 00:34:32.010 --> 00:34:34.860 And this is handy for trying to get some input from the user 00:34:34.860 --> 00:34:37.739 because it'll always ask the user that question. 00:34:37.739 --> 00:34:39.270 What input do you want to give me? 00:34:39.270 --> 00:34:43.230 And then maybe reprompt them, depending on whether that input is invalid or not 00:34:43.230 --> 00:34:44.790 what you want it to be, all right? 00:34:44.790 --> 00:34:47.790 So down here we would say this is the condition under which our input is 00:34:47.790 --> 00:34:48.540 invalid. 00:34:48.540 --> 00:34:52.620 And we should go back up and do it all over again. 00:34:52.620 --> 00:34:55.520 So questions on these kinds of loops still. 00:34:59.870 --> 00:35:00.870 Yeah. 00:35:00.870 --> 00:35:04.890 STUDENT: Would this be good, the do-while loop if you were to-- 00:35:04.890 --> 00:35:07.595 you were asking a question within the while 00:35:07.595 --> 00:35:10.383 loop that didn't say you're doing [INAUDIBLE] 00:35:10.383 --> 00:35:15.330 same with [INAUDIBLE] while loop give a given number? 00:35:15.330 --> 00:35:18.840 Would there be an easier way to terminate the function if say 00:35:18.840 --> 00:35:21.133 you don't [INAUDIBLE] the right age? 00:35:21.133 --> 00:35:23.175 CARTER ZENKE: And by terminate do you mean like-- 00:35:23.175 --> 00:35:24.840 STUDENT: Terminate the funct-- 00:35:24.840 --> 00:35:26.100 like it just stops. 00:35:26.100 --> 00:35:30.580 If you use-- say you're looking for the age range from 25 to 30, 00:35:30.580 --> 00:35:35.360 and then if it's in there, you go to the while loop. 00:35:35.360 --> 00:35:37.640 Would you then have the while loop-- would there 00:35:37.640 --> 00:35:42.210 be a way to stop the while loop from [INAUDIBLE]?? 00:35:42.210 --> 00:35:43.960 CARTER ZENKE: And so, if I'm understanding 00:35:43.960 --> 00:35:47.140 what you're asking correctly, it's often better 00:35:47.140 --> 00:35:51.280 to check for what we don't want to have happen than to check for what 00:35:51.280 --> 00:35:52.880 it is we do want to have happen. 00:35:52.880 --> 00:35:55.370 So for example, I might not want to do this. 00:35:55.370 --> 00:36:00.160 I might not want to say int age is get int age, here. 00:36:00.160 --> 00:36:06.940 And then ask the question, if, let's say, age is between-- what did you say, 00:36:06.940 --> 00:36:08.320 like 15 and 20? 00:36:08.320 --> 00:36:09.100 STUDENT: Yeah. 00:36:09.100 --> 00:36:15.970 CARTER ZENKE: If age is between 15 and 20, then do the rest of my program 00:36:15.970 --> 00:36:19.420 because what I'm doing here is I'm separating my program from-- 00:36:19.420 --> 00:36:22.000 I'm indenting it by a lot, and maybe I have more conditions. 00:36:22.000 --> 00:36:25.360 Maybe I also want to say, if the name starts with A, 00:36:25.360 --> 00:36:28.270 and then it keeps getting more and more indented. 00:36:28.270 --> 00:36:31.040 So what I could do instead is I could do something like this. 00:36:31.040 --> 00:36:34.730 I could say, well, let me do this. 00:36:34.730 --> 00:36:37.210 Let me create this variable named age. 00:36:37.210 --> 00:36:40.690 Let me get some value for it. 00:36:40.690 --> 00:36:41.360 Whoops. 00:36:41.360 --> 00:36:43.160 Let me get some value for it. 00:36:43.160 --> 00:36:47.740 And while the age is not what I want, let me keep reprompting. 00:36:47.740 --> 00:36:49.550 Is that answering your question? 00:36:49.550 --> 00:36:50.800 STUDENT: Yeah, i got it, yeah. 00:36:50.800 --> 00:36:57.000 CARTER ZENKE: OK, so age is like less than 15, or age is greater than 20. 00:36:57.000 --> 00:37:00.830 Let me reprompt, right? 00:37:00.830 --> 00:37:01.330 OK. 00:37:01.330 --> 00:37:02.366 Yeah. 00:37:02.366 --> 00:37:11.560 STUDENT: Is that [INAUDIBLE] to write age less, equal, between? 00:37:11.560 --> 00:37:17.200 Or is it better time to say age less [INAUDIBLE]?? 00:37:17.200 --> 00:37:19.430 CARTER ZENKE: Probably depends on the context. 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 00:37:23.530 --> 00:37:25.090 or equal to? 00:37:25.090 --> 00:37:28.930 In general, when you're writing for loops, where you just want something 00:37:28.930 --> 00:37:31.240 to repeat a certain number of times, we tend 00:37:31.240 --> 00:37:34.330 to use the less than and always start at 0. 00:37:34.330 --> 00:37:36.490 So let's say I want something to repeat five times. 00:37:36.490 --> 00:37:41.530 I'll say int i equals 0, i is less than 5, i++. 00:37:41.530 --> 00:37:44.290 And the math on this just works, right? 00:37:44.290 --> 00:37:47.950 0, 1, 2, 3, 4, that's five times. 00:37:47.950 --> 00:37:52.210 If I were to do something like this, let's say int i is 1, 00:37:52.210 --> 00:37:54.880 then I'd have to do less than or equal to 5. 00:37:54.880 --> 00:37:58.960 And because programmers start counting at 0, meaning this absence of anything. 00:37:58.960 --> 00:38:00.610 It's starting from the very basics. 00:38:00.610 --> 00:38:03.527 We're not going to actually have this less than or equal to sign here. 00:38:03.527 --> 00:38:04.660 Is that helpful? 00:38:04.660 --> 00:38:06.330 Yeah. 00:38:06.330 --> 00:38:06.830 OK. 00:38:10.860 --> 00:38:15.340 All right, so with these building blocks of these loops, 00:38:15.340 --> 00:38:19.240 we'll spend some time working on our lab for the next 10 minutes or so. 00:38:19.240 --> 00:38:23.620 In our lab-- if I go back to my slides here-- 00:38:23.620 --> 00:38:26.680 we're going to be asking this question of how many llamas 00:38:26.680 --> 00:38:28.030 do we currently have? 00:38:28.030 --> 00:38:31.600 And how many years will take us to get to an aspirational number of llamas, 00:38:31.600 --> 00:38:34.160 some gold number of llamas for ourselves here? 00:38:34.160 --> 00:38:37.675 So if we look at this, you can find the lab at this URL right here. 00:38:37.675 --> 00:38:38.800 It's on the course website. 00:38:38.800 --> 00:38:40.160 Go to the lab page. 00:38:40.160 --> 00:38:43.090 And in that lab, you'll find these questions. 00:38:43.090 --> 00:38:44.080 Actually, sorry. 00:38:44.080 --> 00:38:48.040 Within this lab page, you're going to work on this problem all together. 00:38:48.040 --> 00:38:51.650 But we'll get started working on this up front here. 00:38:51.650 --> 00:38:53.530 So the first thing that we'll do for this lab 00:38:53.530 --> 00:38:56.710 is think about how we're going to work an example ourselves, 00:38:56.710 --> 00:38:59.740 and then walk through and write our code. 00:38:59.740 --> 00:39:03.460 When you're working on your problems, often good to think 00:39:03.460 --> 00:39:05.950 about how you're going to write your algorithm first, 00:39:05.950 --> 00:39:07.840 and then write your code. 00:39:07.840 --> 00:39:11.830 So these seven steps can help you, whether you're working on the lab, 00:39:11.830 --> 00:39:13.640 in the problem set, or so on. 00:39:13.640 --> 00:39:16.810 And notice how more than half of these are 00:39:16.810 --> 00:39:20.500 about writing things down, thinking through them, not necessarily 00:39:20.500 --> 00:39:22.120 programming. 00:39:22.120 --> 00:39:26.630 But these final three steps are all about translating that idea into code. 00:39:26.630 --> 00:39:28.450 So we'll do that here today. 00:39:28.450 --> 00:39:31.112 If we look at our llamas, we have a certain number of llamas. 00:39:31.112 --> 00:39:33.070 We want to figure out how many years it'll take 00:39:33.070 --> 00:39:35.140 to get to another number of llamas. 00:39:35.140 --> 00:39:39.490 And we know that every year, maybe a third of our llamas are born, 00:39:39.490 --> 00:39:43.300 and 1/4 of our llamas sadly pass away, right? 00:39:43.300 --> 00:39:46.640 And let's take a look at this particular example here. 00:39:46.640 --> 00:39:48.970 We have 12 llamas. 00:39:48.970 --> 00:39:53.770 And so in this current year, 12 over 3 new llamas will be born, 00:39:53.770 --> 00:39:56.480 and 12 over 4 llamas will pass away. 00:39:56.480 --> 00:40:00.550 And the question is, how many years will it take to get to 13 llamas? 00:40:00.550 --> 00:40:03.940 So to work this example, we have these 12 llamas. 00:40:03.940 --> 00:40:05.380 And now, what would we do? 00:40:05.380 --> 00:40:08.290 We want to birth a 1/3 llamas. 00:40:08.290 --> 00:40:09.760 So how many more will we add here? 00:40:09.760 --> 00:40:10.605 STUDENT: Four. 00:40:10.605 --> 00:40:12.105 CARTER ZENKE: Four, OK, let's add 4. 00:40:14.740 --> 00:40:16.330 And how many would pass away? 00:40:16.330 --> 00:40:17.230 STUDENT: Three. 00:40:17.230 --> 00:40:20.080 CARTER ZENKE: Three, so we'll take away 3 here. 00:40:20.080 --> 00:40:22.900 And we are at 13 llamas now, right? 00:40:22.900 --> 00:40:24.760 And that was one year overall. 00:40:24.760 --> 00:40:27.730 We birthed our four more llamas, and three llamas passed away. 00:40:27.730 --> 00:40:30.230 Now, we're at 13. 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. 00:40:33.850 --> 00:40:37.150 We want to first prompt the user for this starting number of llamas, 00:40:37.150 --> 00:40:40.930 ask them for a goal number of llamas, and then do some math perhaps 00:40:40.930 --> 00:40:43.330 in a loop to figure out how many years it 00:40:43.330 --> 00:40:46.030 will take to get to that goal number of llamas, 00:40:46.030 --> 00:40:48.950 and finally, print out that number of years. 00:40:48.950 --> 00:40:55.390 So if you are in your code space, you might go over to your environment here. 00:40:55.390 --> 00:41:01.450 You might type code population.C to open up that file. 00:41:01.450 --> 00:41:03.100 Here I have mine. 00:41:03.100 --> 00:41:07.060 You might then type, include standardio.h to print something 00:41:07.060 --> 00:41:08.470 to the screen later on. 00:41:08.470 --> 00:41:13.730 And you might also do the cs50 library, cs50.h here as well. 00:41:13.730 --> 00:41:16.890 What else do we need for this boilerplate code? 00:41:16.890 --> 00:41:17.662 STUDENT: Int main. 00:41:17.662 --> 00:41:19.120 CARTER ZENKE: Int main void, right? 00:41:19.120 --> 00:41:23.800 So I'll say int main void. 00:41:23.800 --> 00:41:26.830 And I'll leave this rest up to you, but make sure you're asking-- 00:41:26.830 --> 00:41:32.110 your prompting the user for a starting number of llamas, 00:41:32.110 --> 00:41:37.090 prompting them for an ending number of llamas, and then 00:41:37.090 --> 00:41:41.170 finally, maybe using some kind of loop to figure out how many years it 00:41:41.170 --> 00:41:43.795 will take to get to the goal. 00:41:47.430 --> 00:41:56.220 And keep in mind that every year a 1/3 of our current llamas are born 00:41:56.220 --> 00:42:00.080 and 1/4 pass away. 00:42:00.080 --> 00:42:05.900 All right, so work on this for, let's say, 10 minutes or so. 00:42:05.900 --> 00:42:08.960 And we'll come back and work the example together. 00:42:08.960 --> 00:42:12.410 OK, so I hope you've made some good progress on this lab. 00:42:12.410 --> 00:42:15.440 Let's come back and work the example together. 00:42:15.440 --> 00:42:18.980 We had a few steps to work through, the first one being prompting the user 00:42:18.980 --> 00:42:21.740 for a starting number of llamas. 00:42:21.740 --> 00:42:26.000 And could I ask maybe a group from this side of the room, maybe 00:42:26.000 --> 00:42:30.350 towards the back, if you don't mind, how did you prompt the user 00:42:30.350 --> 00:42:33.890 for your starting number of llamas? 00:42:33.890 --> 00:42:34.820 Yeah, go ahead. 00:42:34.820 --> 00:42:40.220 STUDENT: So the first [INAUDIBLE] start. 00:42:40.220 --> 00:42:43.290 And we ran a do-while loop. 00:42:43.290 --> 00:42:48.000 So it says starts equals get int and then start size. 00:42:48.000 --> 00:42:51.360 And then the condition was while the start was less than 9. 00:42:51.360 --> 00:42:54.780 CARTER ZENKE: Gotcha, so while start is less than 9. 00:42:54.780 --> 00:42:57.810 And why did you choose less than 9 for that case? 00:42:57.810 --> 00:43:00.960 STUDENT: Because the lab said that the minimum number that the start 00:43:00.960 --> 00:43:04.770 size could be was 9 or else the population would fail to grow. 00:43:04.770 --> 00:43:07.710 CARTER ZENKE: Right, so we want to reprompt the user if they give us 00:43:07.710 --> 00:43:10.262 a number less than 9, right? 00:43:10.262 --> 00:43:11.220 That seems pretty good. 00:43:11.220 --> 00:43:14.970 And then similarly, how could we prompt them for an ending number of llamas? 00:43:14.970 --> 00:43:17.580 Could I ask maybe in the middle this time, 00:43:17.580 --> 00:43:19.920 somebody from maybe the back few rows, how 00:43:19.920 --> 00:43:22.935 did you try to prompt for an ending number of llamas here? 00:43:26.900 --> 00:43:28.640 Yeah, go ahead. 00:43:28.640 --> 00:43:31.768 STUDENT: I created an int variable called n. 00:43:31.768 --> 00:43:32.560 CARTER ZENKE: Nice. 00:43:32.560 --> 00:43:35.552 STUDENT: And then I used a do-while loop. 00:43:35.552 --> 00:43:42.015 [INAUDIBLE] had n equals get int, and then end size. 00:43:42.015 --> 00:43:48.647 And then the condition for the while was while end was less than start. 00:43:48.647 --> 00:43:50.480 CARTER ZENKE: Yeah, and then just to ask you 00:43:50.480 --> 00:43:53.380 why would you have that in your conditional down there? 00:43:53.380 --> 00:43:57.510 STUDENT: Because in the lab it wanted us to keep 00:43:57.510 --> 00:44:02.930 prompting if the ending size that the user inputs is less than starting 00:44:02.930 --> 00:44:03.523 point. 00:44:03.523 --> 00:44:04.690 CARTER ZENKE: Yeah, totally. 00:44:04.690 --> 00:44:07.923 So we can't have fewer llamas than we began with. 00:44:07.923 --> 00:44:10.090 So just checking to make sure that is the case here. 00:44:10.090 --> 00:44:13.260 And if not, we reprompt the user. 00:44:13.260 --> 00:44:17.130 And then our final bit down here was keeping track of how many years 00:44:17.130 --> 00:44:20.310 it will take to get to our goal number of llamas now stored 00:44:20.310 --> 00:44:22.440 in this variable called end. 00:44:22.440 --> 00:44:25.380 And how did you all choose to represent the year, 00:44:25.380 --> 00:44:30.090 or how many years have passed in your program? 00:44:30.090 --> 00:44:33.230 Could I ask somebody from this side of the room now? 00:44:33.230 --> 00:44:36.290 What kind of variable did you use to count the number of years here? 00:44:39.250 --> 00:44:42.190 Maybe somebody from the first few rows. 00:44:42.190 --> 00:44:42.910 Yeah, go ahead. 00:44:42.910 --> 00:44:43.868 STUDENT: As an integer. 00:44:43.868 --> 00:44:47.035 CARTER ZENKE: As an integer, and did you give it a special name or anything? 00:44:47.035 --> 00:44:48.370 STUDENT: i called it int years. 00:44:48.370 --> 00:44:49.570 CARTER ZENKE: Int years, OK. 00:44:49.570 --> 00:44:51.880 And what did you set it first to? 00:44:51.880 --> 00:44:52.720 STUDENT: Zero. 00:44:52.720 --> 00:44:54.700 CARTER ZENKE: Zero, OK. 00:44:54.700 --> 00:44:57.550 And now we have some math to do. 00:44:57.550 --> 00:44:59.983 If we look maybe-- 00:44:59.983 --> 00:45:01.150 this group [INAUDIBLE] mind. 00:45:01.150 --> 00:45:03.730 How did you all add llamas, subtract them? 00:45:03.730 --> 00:45:07.060 How did you adjust your population of llamas as the years went on? 00:45:12.910 --> 00:45:17.695 Any ideas for how you might do it or how you did do it? 00:45:17.695 --> 00:45:18.320 Yeah, go ahead. 00:45:18.320 --> 00:45:20.392 STUDENT: Just add 112 llama each year. 00:45:20.392 --> 00:45:22.100 CARTER ZENKE: Added 112 llamas each year. 00:45:22.100 --> 00:45:22.600 Nice. 00:45:22.600 --> 00:45:25.322 So can you tell me, if we look at our program here, 00:45:25.322 --> 00:45:28.280 we have a starting number of llamas, so how would you adjust that here? 00:45:31.320 --> 00:45:33.772 STUDENT: You would go, like you said, at start-- 00:45:33.772 --> 00:45:34.605 CARTER ZENKE: Start. 00:45:34.605 --> 00:45:37.450 STUDENT: It would be start equals-- 00:45:37.450 --> 00:45:38.830 I'm sorry, plus equals. 00:45:38.830 --> 00:45:40.163 CARTER ZENKE: Start plus equals. 00:45:40.163 --> 00:45:41.410 STUDENT: Start divided by 12. 00:45:41.410 --> 00:45:43.930 CARTER ZENKE: Start divided by 12. 00:45:43.930 --> 00:45:46.150 And how did you get start divided by 12? 00:45:46.150 --> 00:45:48.820 What kind of math did you do to get there? 00:45:48.820 --> 00:45:51.105 STUDENT: Common denominator of 1/3 and 1/4. 00:45:51.105 --> 00:45:53.530 And then you're adding 1/3 and subtracting 1/4. 00:45:53.530 --> 00:45:55.655 CARTER ZENKE: Nice, so ultimately, every year we're 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. 00:45:59.680 --> 00:46:03.430 It comes out to 1/12 new llamas every year. 00:46:03.430 --> 00:46:08.050 But we need some kind of loop to run this. 00:46:08.050 --> 00:46:10.300 Anyone want to help me out here with this final piece? 00:46:10.300 --> 00:46:13.520 How do we have the loop to increase this-- 00:46:13.520 --> 00:46:16.460 llama, know when to stop too. 00:46:16.460 --> 00:46:17.030 Yeah. 00:46:17.030 --> 00:46:21.729 STUDENT: We could do while start is less end [INAUDIBLE] start. 00:46:21.729 --> 00:46:26.457 And after that [INAUDIBLE] add [INAUDIBLE].. 00:46:26.457 --> 00:46:28.165 CARTER ZENKE: Yeah, and so add 1 to year? 00:46:28.165 --> 00:46:28.870 STUDENT: Yeah. 00:46:28.870 --> 00:46:33.730 CARTER ZENKE: Yeah, so we could say years++ for increase years by 1. 00:46:33.730 --> 00:46:37.940 And at the very end we ultimately want to print out the number of years. 00:46:37.940 --> 00:46:40.840 So I'll say printf %i. 00:46:40.840 --> 00:46:44.410 And maybe just to clarify, years percent and then years 00:46:44.410 --> 00:46:49.100 down at the bottom here, with a backslash n at the end. 00:46:49.100 --> 00:46:56.410 And if I make this make population, run ./population, 00:46:56.410 --> 00:46:59.310 I'll type in maybe 12 and 13. 00:46:59.310 --> 00:47:02.460 And I get one year, as we expected. 00:47:02.460 --> 00:47:03.645 Yeah, question or comment. 00:47:03.645 --> 00:47:07.503 STUDENT: What is the plus equal [INAUDIBLE] start plus equal start? 00:47:07.503 --> 00:47:08.670 CARTER ZENKE: Good question. 00:47:08.670 --> 00:47:11.260 This is basically shorthand for doing this. 00:47:11.260 --> 00:47:17.430 I could say start equals start + start over 12 because I'm trying 00:47:17.430 --> 00:47:19.750 to add to start as we go through. 00:47:19.750 --> 00:47:22.620 So this plus equals is simply saying, let 00:47:22.620 --> 00:47:27.298 me add to what start currently is this value on the right-hand side. 00:47:27.298 --> 00:47:28.215 Does that makes sense? 00:47:28.215 --> 00:47:28.870 STUDENT: Yes, thanks. 00:47:28.870 --> 00:47:29.745 CARTER ZENKE: Gotcha. 00:47:34.460 --> 00:47:39.120 Now, there's one question here, which is, let's say, I have-- oh, yeah, 00:47:39.120 --> 00:47:40.370 go ahead, somewhere over here. 00:47:40.370 --> 00:47:41.560 Yeah. 00:47:41.560 --> 00:47:44.900 STUDENT: I have a question regarding this start in line 12. 00:47:44.900 --> 00:47:49.370 We have one part that is the [INAUDIBLE] form 00:47:49.370 --> 00:47:53.130 and one part that is [INAUDIBLE] our time. 00:47:53.130 --> 00:47:58.790 Therefore, you need to round the first part and then round the second part 00:47:58.790 --> 00:48:04.910 because you cannot have half a llama die and 1/3 being alive. 00:48:04.910 --> 00:48:09.920 So in some cases, where the number is divisible by one part of it 00:48:09.920 --> 00:48:13.040 and not divisible by the other, could create some differences. 00:48:13.040 --> 00:48:17.300 CARTER ZENKE: Yeah, so maybe it is better for us to add start over 3 00:48:17.300 --> 00:48:21.067 and then subtract maybe start over 4 00:48:21.067 --> 00:48:22.400 STUDENT: Yeah, but what if you-- 00:48:22.400 --> 00:48:23.150 CARTER ZENKE: --and keep going. 00:48:23.150 --> 00:48:24.950 STUDENT: --round these numbers? 00:48:24.950 --> 00:48:30.050 I guess divided by 3 [INAUDIBLE] or 3.3333. 00:48:30.050 --> 00:48:34.080 So you need to round it 3 because, otherwise, 00:48:34.080 --> 00:48:36.678 you cannot have 1/3 of a llama [INAUDIBLE].. 00:48:36.678 --> 00:48:38.970 CARTER ZENKE: Yeah, we don't want 1/3 third of a llama. 00:48:38.970 --> 00:48:41.230 So we got to round this in some way. 00:48:41.230 --> 00:48:44.400 And I think what we want to do is always round down, 00:48:44.400 --> 00:48:48.720 which brings us to some feature or, in some cases, a bug in C-- 00:48:48.720 --> 00:48:51.210 you might run into this-- called truncation. 00:48:51.210 --> 00:48:54.900 And we didn't see this as much in lecture, which to highlight it here, 00:48:54.900 --> 00:49:00.940 in C, when you divide two integers, you will always get back an integer. 00:49:00.940 --> 00:49:07.050 So if I divide perhaps maybe 2 and 3, 2 divided by 3, both integers, 00:49:07.050 --> 00:49:08.760 I won't get 2/3. 00:49:08.760 --> 00:49:10.260 I'll get 0. 00:49:10.260 --> 00:49:13.240 Always round down, cut off the rest of that data there. 00:49:13.240 --> 00:49:19.440 So to show an example of this, I might do code truncation.c. 00:49:19.440 --> 00:49:25.480 And I will have the same boilerplate code, standardio.h. 00:49:25.480 --> 00:49:28.420 I'll include cs50.h. 00:49:28.420 --> 00:49:30.850 And I'll say int main void. 00:49:30.850 --> 00:49:35.680 And I'll type in maybe int a is, let's say, 2. 00:49:35.680 --> 00:49:37.540 Int b is 3. 00:49:37.540 --> 00:49:42.020 And let's print out the result of dividing them like this. 00:49:42.020 --> 00:49:46.210 Let's say-- actually, sorry, int c is a divided by b. 00:49:46.210 --> 00:49:51.100 And I'll say this gets the result c here. 00:49:51.100 --> 00:49:58.300 I'll say make truncation and do ./truncation. 00:49:58.300 --> 00:50:02.380 And I'll get 0 and not 2/3. 00:50:02.380 --> 00:50:04.300 So always rounding down here. 00:50:04.300 --> 00:50:07.150 If I do want the decimal though, what I can do 00:50:07.150 --> 00:50:12.170 is I can convert on the fly one of these to a decimal. 00:50:12.170 --> 00:50:16.240 So I could say, actually, I want a to be a float 00:50:16.240 --> 00:50:18.650 and then complete this division. 00:50:18.650 --> 00:50:20.980 And as long as one of your numbers is a float, 00:50:20.980 --> 00:50:22.940 you'll get a float in the very end. 00:50:22.940 --> 00:50:25.660 So I'll say maybe %f here. 00:50:25.660 --> 00:50:27.950 I'll do make truncation. 00:50:27.950 --> 00:50:28.450 Whoops. 00:50:28.450 --> 00:50:32.980 Let me say this is actually now a float in the end. 00:50:32.980 --> 00:50:41.440 Make truncation, I'll say ./truncation, and I will get that 0.6667. 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. 00:50:52.270 --> 00:50:55.000 So just something to keep in mind as we go through here. 00:50:55.000 --> 00:50:58.337 Happy to stick around and answer any questions that you all have, 00:50:58.337 --> 00:51:01.420 but this should bring us to the end of our lab today and our super section 00:51:01.420 --> 00:51:01.850 as well. 00:51:01.850 --> 00:51:02.540 Thank you all for coming. 00:51:02.540 --> 00:51:03.832 It's wonderful to see you here. 00:51:03.832 --> 00:51:06.060 Hope to see you next week in section.