1 00:00:00,000 --> 00:00:05,830 2 00:00:05,830 --> 00:00:08,650 >> JASON HIRSCHHORN: Welcome to CS50 section, everyone. 3 00:00:08,650 --> 00:00:11,430 My name is Jason Hirschhorn. 4 00:00:11,430 --> 00:00:15,850 I am a lot of yours TFs, but a lot of people will hopefully be joining us 5 00:00:15,850 --> 00:00:20,420 online, extension students or students trying to look through section if they 6 00:00:20,420 --> 00:00:22,830 missed their section that week or just catch up if they wanted 7 00:00:22,830 --> 00:00:24,510 to review some material. 8 00:00:24,510 --> 00:00:30,060 >> So as per the syllabus, which all of you have read, correct? 9 00:00:30,060 --> 00:00:31,390 I see a lot of head nods. 10 00:00:31,390 --> 00:00:34,220 Sections provide you with opportunities to explore the course's 11 00:00:34,220 --> 00:00:37,420 material in a more intimate environment, as well as dive into 12 00:00:37,420 --> 00:00:38,810 hands-on activities. 13 00:00:38,810 --> 00:00:41,230 >> So that's basically what we're going to be doing every week. 14 00:00:41,230 --> 00:00:45,090 We're going to go through what we have determined to be the most important 15 00:00:45,090 --> 00:00:49,170 topics covered in lecture, and the ones probably also most useful and 16 00:00:49,170 --> 00:00:52,780 most likely to come up on problem sets and in quizzes, and in your coding 17 00:00:52,780 --> 00:00:53,970 life outside of CS50. 18 00:00:53,970 --> 00:00:57,260 So we're going to go into those topics, discuss them, go over them, 19 00:00:57,260 --> 00:01:00,660 answer any questions you guys have, and then we're going to do a lot of 20 00:01:00,660 --> 00:01:02,250 practice problems in coding. 21 00:01:02,250 --> 00:01:04,239 >> You all brought laptops, which is fantastic. 22 00:01:04,239 --> 00:01:05,710 This is a computer science class. 23 00:01:05,710 --> 00:01:10,070 However, a lot of the exercises we do, you won't need your laptops for. 24 00:01:10,070 --> 00:01:11,720 You can do them on a sheet of paper. 25 00:01:11,720 --> 00:01:13,580 That's how you're going to do them on the quizzes. 26 00:01:13,580 --> 00:01:16,450 And indeed, that's a great way to really know that you how to do 27 00:01:16,450 --> 00:01:19,720 something if you don't have Check 50 to rely on or Make to rely on, telling 28 00:01:19,720 --> 00:01:20,620 you made any errors. 29 00:01:20,620 --> 00:01:25,670 >> So you're welcome to code on the appliance or run.cs50.net, which is 30 00:01:25,670 --> 00:01:29,770 another tool that allows you to code online, but I also would recommend, if 31 00:01:29,770 --> 00:01:32,280 you haven't tried it, using a sheet of paper and trying these practice 32 00:01:32,280 --> 00:01:33,640 problems on a sheet of paper. 33 00:01:33,640 --> 00:01:35,330 Because again, that's ultimately what you're going to be 34 00:01:35,330 --> 00:01:37,430 asked to do on quizzes. 35 00:01:37,430 --> 00:01:44,080 >> So let's start with our agenda for today. 36 00:01:44,080 --> 00:01:47,710 We're going to go over some norms for our section, then we're going to dive 37 00:01:47,710 --> 00:01:51,180 into arrays, functions, and command line arguments, which is what you guys 38 00:01:51,180 --> 00:01:56,200 have covered this past week in lecture and then Monday, and what you'll cover 39 00:01:56,200 --> 00:01:57,330 more of tomorrow. 40 00:01:57,330 --> 00:01:59,060 And then finally, we're going to talk about pset2. 41 00:01:59,060 --> 00:02:03,640 If I could get a show of hands if you haven't looked at the problem set two 42 00:02:03,640 --> 00:02:04,890 specification. 43 00:02:04,890 --> 00:02:07,060 44 00:02:07,060 --> 00:02:08,120 We'll say everyone. 45 00:02:08,120 --> 00:02:09,419 Fantastic. 46 00:02:09,419 --> 00:02:12,070 >> We'll talk about problem sets in a bit, but if you read the problem set 47 00:02:12,070 --> 00:02:14,340 specification, that's all you need to do. 48 00:02:14,340 --> 00:02:18,370 If you read that before you come to class and section on Tuesday, you will 49 00:02:18,370 --> 00:02:19,220 be a great position. 50 00:02:19,220 --> 00:02:21,680 We can go through the problem set, which is what we're going 51 00:02:21,680 --> 00:02:22,090 to do at the end. 52 00:02:22,090 --> 00:02:23,500 We're going to go through the logic, make sure 53 00:02:23,500 --> 00:02:24,710 everybody's on the same page. 54 00:02:24,710 --> 00:02:28,650 >> I want you to be able to leave Tuesday feeling very comfortable with what you 55 00:02:28,650 --> 00:02:31,560 need to do for this week's problem set, and if you don't, I will happily 56 00:02:31,560 --> 00:02:33,050 stay after and work with you. 57 00:02:33,050 --> 00:02:36,850 But that is one thing that you have to do but I think would be really smart, 58 00:02:36,850 --> 00:02:40,500 just to read the problem set specification by class on Tuesday, 59 00:02:40,500 --> 00:02:42,640 which it sounds like a lot of you guys did. 60 00:02:42,640 --> 00:02:45,810 >> I'll start with an agenda each class so you know where we're going. 61 00:02:45,810 --> 00:02:49,520 This is also helpful for people watching online because each section 62 00:02:49,520 --> 00:02:53,540 is color coded, so if you want to come back and just watch 10 minutes rather 63 00:02:53,540 --> 00:02:56,950 than the whole 90-minute section, you can just go to the color coded section 64 00:02:56,950 --> 00:02:57,890 and look at that part. 65 00:02:57,890 --> 00:03:01,510 So I've tried to break them up into nice modules that will allow you to-- 66 00:03:01,510 --> 00:03:03,960 if you want to come back and review, or other students who are just 67 00:03:03,960 --> 00:03:05,350 watching a section to review-- 68 00:03:05,350 --> 00:03:08,130 to find the specific thing they want to pay attention to 69 00:03:08,130 --> 00:03:10,480 and learn more about. 70 00:03:10,480 --> 00:03:11,390 Sweet. 71 00:03:11,390 --> 00:03:12,180 >> Norms. 72 00:03:12,180 --> 00:03:13,680 I'm a FOP leader on campus. 73 00:03:13,680 --> 00:03:14,900 I don't know if anybody did FOP. 74 00:03:14,900 --> 00:03:16,280 Did anybody do FOP? 75 00:03:16,280 --> 00:03:17,100 One person did FOP. 76 00:03:17,100 --> 00:03:18,430 Two people did FOP. 77 00:03:18,430 --> 00:03:19,250 Awesome. 78 00:03:19,250 --> 00:03:22,360 So FOP is the First year Outdoor Orientation Program. 79 00:03:22,360 --> 00:03:27,240 We take freshmen into the woods and make the most magical experiences of 80 00:03:27,240 --> 00:03:28,830 their lives, correct? 81 00:03:28,830 --> 00:03:29,220 Yeah. 82 00:03:29,220 --> 00:03:30,080 Sounds about right. 83 00:03:30,080 --> 00:03:36,360 >> So I just wanted to start our very first section by discussing some norms 84 00:03:36,360 --> 00:03:38,860 for the class and our relationship because I think 85 00:03:38,860 --> 00:03:40,010 they're very important. 86 00:03:40,010 --> 00:03:43,630 First and foremost, as those of you who are my section have received and 87 00:03:43,630 --> 00:03:47,150 has been discussed in that email, I love this. 88 00:03:47,150 --> 00:03:48,130 This is my favorite part of the year. 89 00:03:48,130 --> 00:03:49,100 I've been so excited. 90 00:03:49,100 --> 00:03:50,040 I've been here for hours today. 91 00:03:50,040 --> 00:03:54,560 I haven't left this building except to go eat because I love CS50 section, I 92 00:03:54,560 --> 00:03:58,620 love teaching, I love working with you guys. 93 00:03:58,620 --> 00:04:02,000 >> One of my highest priorities, if not my number one priority, not only for 94 00:04:02,000 --> 00:04:05,130 this class but including my other classes and extracurriculars, is to 95 00:04:05,130 --> 00:04:09,350 support you guys, and help you guys, and do my best to make sure that you 96 00:04:09,350 --> 00:04:13,780 guys have a wonderful, fun, productive, engaging, exciting 97 00:04:13,780 --> 00:04:15,250 experience in this class. 98 00:04:15,250 --> 00:04:16,190 That's what I'm committed to. 99 00:04:16,190 --> 00:04:17,959 That's why I love to do this. 100 00:04:17,959 --> 00:04:20,190 That is my number one thing, supporting you guys. 101 00:04:20,190 --> 00:04:25,110 If you have any questions about lecture, the problem set, section, 102 00:04:25,110 --> 00:04:28,830 life, even if you want to talk about life, please feel free to reach out. 103 00:04:28,830 --> 00:04:31,210 >> You have my cell phone number, you have my email 104 00:04:31,210 --> 00:04:32,910 address, you have my GChat. 105 00:04:32,910 --> 00:04:37,540 You can contact me at any or all of those whenever, but probably won't get 106 00:04:37,540 --> 00:04:38,720 a response whenever. 107 00:04:38,720 --> 00:04:41,020 I try and get back within 24 hours. 108 00:04:41,020 --> 00:04:44,760 Oftentimes it will be within 20 minutes or 24 minutes. 109 00:04:44,760 --> 00:04:46,110 I should have said that to make it parallel. 110 00:04:46,110 --> 00:04:48,840 But you will get a response pretty quickly. 111 00:04:48,840 --> 00:04:54,220 >> If you text me an hour before the problem set's due, I will calm you 112 00:04:54,220 --> 00:04:57,660 down and do deep breathing exercises with you, but if you haven't started, 113 00:04:57,660 --> 00:05:00,270 you're probably not going to finish, and I can't really help you out there 114 00:05:00,270 --> 00:05:01,710 even though I'll try my best. 115 00:05:01,710 --> 00:05:03,920 So I will do everything I can to support you. 116 00:05:03,920 --> 00:05:07,980 Please feel free to reach out whether it's in section again, at Office Hours 117 00:05:07,980 --> 00:05:09,150 if you see me, or even outside. 118 00:05:09,150 --> 00:05:11,800 I'm happy to set up a time to meet and work through problems 119 00:05:11,800 --> 00:05:14,410 with or go over material. 120 00:05:14,410 --> 00:05:15,990 So that is number one, support. 121 00:05:15,990 --> 00:05:17,940 I will do everything I can to support you. 122 00:05:17,940 --> 00:05:21,520 >> Number two, meeting in the middle. 123 00:05:21,520 --> 00:05:24,760 As much as I would like to be, I'm not omnipotent or all-powerful or 124 00:05:24,760 --> 00:05:28,410 all-knowing, so I don't know all the time if what I'm doing is getting 125 00:05:28,410 --> 00:05:33,410 across, if the way I'm teaching the topic helps you learn it, or if you 126 00:05:33,410 --> 00:05:36,120 are confused on a topic but don't raise your hand or ask me 127 00:05:36,120 --> 00:05:37,390 or send me an email. 128 00:05:37,390 --> 00:05:42,150 Or even if you, again, are confused about something on the problem set. 129 00:05:42,150 --> 00:05:44,240 If you don't reach out to me, I can't know that for sure. 130 00:05:44,240 --> 00:05:46,950 I'm going to do my best to check in on you guys, make sure everything is 131 00:05:46,950 --> 00:05:51,170 going well, try to teach things in a way that will be conducive to most of 132 00:05:51,170 --> 00:05:54,910 your learning styles, but if there's anything I can do, let me know. 133 00:05:54,910 --> 00:05:58,520 >> Along those lines, if you want some help with the problem set, I'm more 134 00:05:58,520 --> 00:05:59,700 than happy to help you with the problem set. 135 00:05:59,700 --> 00:06:02,300 We're going to do a lot of that towards the second 136 00:06:02,300 --> 00:06:03,460 half of this section. 137 00:06:03,460 --> 00:06:06,040 But if you haven't read the problem sets back and haven't watched the walk 138 00:06:06,040 --> 00:06:09,310 through and haven't watched the shorts and expecting me to teach you 139 00:06:09,310 --> 00:06:13,260 everything that has been videotaped online or that you can find at CS50 140 00:06:13,260 --> 00:06:16,720 study, that's not really fair to me or to your other peers. 141 00:06:16,720 --> 00:06:20,440 142 00:06:20,440 --> 00:06:25,730 >> So please, I'm more than happy to help you, but you guys got to meet me in 143 00:06:25,730 --> 00:06:27,230 the middle. 144 00:06:27,230 --> 00:06:30,100 We have tons of resources for you available on CS50. 145 00:06:30,100 --> 00:06:31,230 I mentioned some of them. 146 00:06:31,230 --> 00:06:32,960 Please take advantage of them. 147 00:06:32,960 --> 00:06:35,140 If you're confused, I'm more than happy to help you explain them in 148 00:06:35,140 --> 00:06:39,290 another way, but it's not fair if students show up at office hours, 149 00:06:39,290 --> 00:06:42,530 haven't started anything, and it's Thursday night and they're expecting 150 00:06:42,530 --> 00:06:45,880 the TF to walk them through every single thing for the problem set due 151 00:06:45,880 --> 00:06:47,140 at noon the next day. 152 00:06:47,140 --> 00:06:48,300 Make sense? 153 00:06:48,300 --> 00:06:50,890 Again, please meet us in the middle. 154 00:06:50,890 --> 00:06:53,230 >> And finally, high expectations. 155 00:06:53,230 --> 00:06:58,010 I'm going to be grading everybody who's in my section, or scoring your 156 00:06:58,010 --> 00:06:59,020 problem sets. 157 00:06:59,020 --> 00:07:01,240 You haven't gone back problem set zero or one yet. 158 00:07:01,240 --> 00:07:02,250 We're sorry about that. 159 00:07:02,250 --> 00:07:04,970 It took us awhile to finally settle sections, and now 160 00:07:04,970 --> 00:07:05,560 we can start grading. 161 00:07:05,560 --> 00:07:09,560 >> I'll get them back to you as soon as possible. 162 00:07:09,560 --> 00:07:11,890 Those are some of my advisees out there. 163 00:07:11,890 --> 00:07:14,150 I'll get them back to you as soon as possible. 164 00:07:14,150 --> 00:07:18,046 And for problem set two, we'll be on schedule and we'll start getting them 165 00:07:18,046 --> 00:07:20,440 to you pretty quickly after you turn them in. 166 00:07:20,440 --> 00:07:24,130 >> When you get your problem set back, you will get scores, as you know from 167 00:07:24,130 --> 00:07:28,150 reading the syllabus-- correctness, design, scope, and style scores. 168 00:07:28,150 --> 00:07:29,760 You'll also get comments. 169 00:07:29,760 --> 00:07:32,160 The comments are more important than the scores. 170 00:07:32,160 --> 00:07:33,780 I think it's safe to say that. 171 00:07:33,780 --> 00:07:37,930 >> Scores are just a number from one to five. 172 00:07:37,930 --> 00:07:38,930 It's hard to read into those. 173 00:07:38,930 --> 00:07:40,720 It's hard to improve from those. 174 00:07:40,720 --> 00:07:44,460 But the comments will point out specific things you did well or areas 175 00:07:44,460 --> 00:07:48,360 you can improve on and give you some ideas for the future or alternative 176 00:07:48,360 --> 00:07:49,160 ways of doing things. 177 00:07:49,160 --> 00:07:52,270 >> So please, please, please read through the comments. 178 00:07:52,270 --> 00:07:54,190 I put a lot of time into those comments. 179 00:07:54,190 --> 00:07:57,190 Please read through them and don't just scroll down to the bottom of the 180 00:07:57,190 --> 00:08:01,890 PDF, look for your score, or go online and find your score, and then ignore 181 00:08:01,890 --> 00:08:02,800 the comments. 182 00:08:02,800 --> 00:08:03,910 That's not how you're going to get better. 183 00:08:03,910 --> 00:08:06,550 That's not how you're going to increase your scores ultimately, just 184 00:08:06,550 --> 00:08:07,340 by looking at the scores. 185 00:08:07,340 --> 00:08:08,760 Please look at those comments. 186 00:08:08,760 --> 00:08:13,240 >> And along the line of high expectations, again, like I said, you 187 00:08:13,240 --> 00:08:14,260 can be scored from one through five. 188 00:08:14,260 --> 00:08:17,120 Twos, threes, and fours are the vast majority of scores 189 00:08:17,120 --> 00:08:17,940 you're going to be getting. 190 00:08:17,940 --> 00:08:20,180 Ones and fives are both really rare. 191 00:08:20,180 --> 00:08:21,720 There's always things we can do better. 192 00:08:21,720 --> 00:08:23,490 Whenever I code a program, there's always something I 193 00:08:23,490 --> 00:08:24,800 could have done better. 194 00:08:24,800 --> 00:08:28,740 >> So rather than saying you get a five all the time on everything and did 195 00:08:28,740 --> 00:08:31,960 everything perfect, I don't think any of us really believe that we-- or 196 00:08:31,960 --> 00:08:34,830 hopefully, we don't do everything perfect all of the time. 197 00:08:34,830 --> 00:08:37,010 So don't be worried if you didn't get a five. 198 00:08:37,010 --> 00:08:40,470 A five does not map onto an A, a four does not map onto a B, a three does 199 00:08:40,470 --> 00:08:42,950 not map onto a C. That's not how the scores work. 200 00:08:42,950 --> 00:08:48,140 >> Like David has said many times, at semester's end all of your scores and 201 00:08:48,140 --> 00:08:53,390 input from me, your TF, come together and determine a final grade for you. 202 00:08:53,390 --> 00:08:56,760 But again, it's not five, A, four, B, et cetera. 203 00:08:56,760 --> 00:09:01,150 So I'm going to have high expectations for you guys. 204 00:09:01,150 --> 00:09:03,670 I'm going to expect good problem sets and I'm going to give 205 00:09:03,670 --> 00:09:05,010 you feedback as such. 206 00:09:05,010 --> 00:09:07,430 And it's not going to mean you're going to get a worse grade. 207 00:09:07,430 --> 00:09:09,760 You are graded in terms of your progress from week 208 00:09:09,760 --> 00:09:11,260 zero to course's end. 209 00:09:11,260 --> 00:09:17,190 >> And finally, in terms of high expectations in class, I love when 210 00:09:17,190 --> 00:09:18,460 people participate. 211 00:09:18,460 --> 00:09:23,180 I understand some people feel more or less comfortable participating. 212 00:09:23,180 --> 00:09:26,050 Other people are participating not by raising their hand and 213 00:09:26,050 --> 00:09:27,320 talking all the time. 214 00:09:27,320 --> 00:09:30,570 But we in this class are going to be doing a lot of small problems, a lot 215 00:09:30,570 --> 00:09:33,770 of mini questions, and then some larger coding problems. 216 00:09:33,770 --> 00:09:36,760 So definitely work these exercises out when we do them on your computer or on 217 00:09:36,760 --> 00:09:37,900 a sheet of paper. 218 00:09:37,900 --> 00:09:40,933 >> But I'll also give you some time before anybody ever has to answer some 219 00:09:40,933 --> 00:09:42,220 of the questions I ask. 220 00:09:42,220 --> 00:09:44,930 So because I will give you some time to think about it, because we're going 221 00:09:44,930 --> 00:09:49,380 to do things in partners sometimes, I will expect everybody 222 00:09:49,380 --> 00:09:52,150 to be engaged somewhat. 223 00:09:52,150 --> 00:09:56,030 And for lack of a better term, there will probably be cold calling, which 224 00:09:56,030 --> 00:09:59,045 is where everybody will have a chance to think about the answer, and then 225 00:09:59,045 --> 00:10:01,150 I'll call on you, and then you'll say the answer. 226 00:10:01,150 --> 00:10:05,200 >> Don't worry or be scared about that. 227 00:10:05,200 --> 00:10:08,610 I've done it in the past and we've found that it's much more successful, 228 00:10:08,610 --> 00:10:11,810 also read educational research on it to determine whether or not it was a 229 00:10:11,810 --> 00:10:12,640 good tactic. 230 00:10:12,640 --> 00:10:15,790 It is very helpful, especially if you give students time to think about the 231 00:10:15,790 --> 00:10:20,070 answers and don't make them just come out and say it right off the bat. 232 00:10:20,070 --> 00:10:22,620 But if you don't feel comfortable answering, I'm also pretty good at 233 00:10:22,620 --> 00:10:25,370 sensing that out, and we're not going to put you on the spot, especially on 234 00:10:25,370 --> 00:10:28,240 the camera, even though none of you are on camera right now. 235 00:10:28,240 --> 00:10:32,250 >> So those are the norms I have. 236 00:10:32,250 --> 00:10:34,670 One more thing along the lines of meeting in the middle. 237 00:10:34,670 --> 00:10:36,490 I'm going to give you guys a lot of feedback. 238 00:10:36,490 --> 00:10:38,310 Please give me feedback as well. 239 00:10:38,310 --> 00:10:46,190 There's a website, sayat.me/cs50. 240 00:10:46,190 --> 00:10:48,380 So it's an honest feedback. 241 00:10:48,380 --> 00:10:50,730 If you want to give it during section, I get an email whenever it happens. 242 00:10:50,730 --> 00:10:53,820 We'll take a break in a bit, so I'll look through the feedback then. 243 00:10:53,820 --> 00:10:55,970 If you ever have any feedback, please give it to me there. 244 00:10:55,970 --> 00:11:01,380 >> If you give me feedback on the Q, that's wonderful, but that's the guide 245 00:11:01,380 --> 00:11:02,570 that you do at the end. 246 00:11:02,570 --> 00:11:05,690 But that will be at semester's end so I won't be able to take any of that 247 00:11:05,690 --> 00:11:08,620 feedback and turn it into teaching you guys better. 248 00:11:08,620 --> 00:11:11,020 So please, if you have any feedback, give it to me. 249 00:11:11,020 --> 00:11:12,010 I love feedback. 250 00:11:12,010 --> 00:11:14,490 Back to FOP, we get feedback all the time. 251 00:11:14,490 --> 00:11:18,610 >> You can give me pluses, good things, deltas, things I can improve upon. 252 00:11:18,610 --> 00:11:20,600 You can comment on my outfits. 253 00:11:20,600 --> 00:11:24,170 That won't be as helpful, but you're welcome to comment on any and 254 00:11:24,170 --> 00:11:24,570 everything. 255 00:11:24,570 --> 00:11:26,280 Please, please give me feedback. 256 00:11:26,280 --> 00:11:28,740 I value it a lot and it means a lot to me. 257 00:11:28,740 --> 00:11:31,760 258 00:11:31,760 --> 00:11:33,280 >> One more norm, fun. 259 00:11:33,280 --> 00:11:34,530 We're going to have a lot of fun. 260 00:11:34,530 --> 00:11:36,930 Hopefully in this course, you'll have a lot of fun. 261 00:11:36,930 --> 00:11:38,780 We'll have candy at section. 262 00:11:38,780 --> 00:11:40,040 We'll have music. 263 00:11:40,040 --> 00:11:45,510 We will be hopefully getting to know one another somewhat well, so I want 264 00:11:45,510 --> 00:11:47,690 this to be a fun, engaging experience. 265 00:11:47,690 --> 00:11:50,890 >> I know some of you have fallen asleep in the lectures you had earlier today, 266 00:11:50,890 --> 00:11:53,750 so hopefully that won't be the case now. 267 00:11:53,750 --> 00:11:55,960 And if it is the case now, just don't snore because that might 268 00:11:55,960 --> 00:11:57,580 be caught on camera. 269 00:11:57,580 --> 00:11:58,770 Sweet. 270 00:11:58,770 --> 00:12:04,020 Any questions, comments, or concerns before I move on about anything we've 271 00:12:04,020 --> 00:12:05,270 talked about? 272 00:12:05,270 --> 00:12:08,620 273 00:12:08,620 --> 00:12:11,350 Great. 274 00:12:11,350 --> 00:12:14,830 >> So we're going to go into arrays first. 275 00:12:14,830 --> 00:12:17,140 This is how you make an array. 276 00:12:17,140 --> 00:12:18,410 We've seen this before. 277 00:12:18,410 --> 00:12:21,830 You have a data type, you have a name, and then you have a size. 278 00:12:21,830 --> 00:12:24,430 Right here, I create two arrays. 279 00:12:24,430 --> 00:12:30,360 The first one is an array of characters, and it has 26 spaces. 280 00:12:30,360 --> 00:12:33,790 The second one is an array of scores, and it has three 281 00:12:33,790 --> 00:12:35,390 spaces for three integers. 282 00:12:35,390 --> 00:12:35,827 Yes? 283 00:12:35,827 --> 00:12:38,200 >> STUDENT: Are all of these slides available somewhere for us? 284 00:12:38,200 --> 00:12:38,850 >> JASON HIRSCHHORN: Great question. 285 00:12:38,850 --> 00:12:40,650 The question was, are all of these slides available? 286 00:12:40,650 --> 00:12:40,920 Yes. 287 00:12:40,920 --> 00:12:43,420 All of these slides will be available, and in the future, I'll email them to 288 00:12:43,420 --> 00:12:44,640 you before section. 289 00:12:44,640 --> 00:12:47,340 But today, because this is the first week, we had a late start. 290 00:12:47,340 --> 00:12:49,675 So usually, you'll get these slides, but they will be available online and 291 00:12:49,675 --> 00:12:52,420 I'll email them to you right after class. 292 00:12:52,420 --> 00:12:54,140 Any other questions? 293 00:12:54,140 --> 00:13:01,330 >> So again, arrays are boxes, for lack of a better word, that hold multiple 294 00:13:01,330 --> 00:13:06,420 elements of one data type, and they're next to one another in memory. 295 00:13:06,420 --> 00:13:11,370 And so again, this is how you can make two types of arrays. 296 00:13:11,370 --> 00:13:15,090 >> So after you've made your array with that first line-- int scores, bracket 297 00:13:15,090 --> 00:13:17,430 three, close bracket-- you want to initialize it. 298 00:13:17,430 --> 00:13:19,050 You want to put some values in there. 299 00:13:19,050 --> 00:13:24,660 And this is how you put some values in there, just choosing the indice and 300 00:13:24,660 --> 00:13:25,670 writing the number. 301 00:13:25,670 --> 00:13:27,130 Let's draw this on the board. 302 00:13:27,130 --> 00:13:28,270 Can everybody see? 303 00:13:28,270 --> 00:13:29,030 Everybody can see. 304 00:13:29,030 --> 00:13:30,790 Sweet. 305 00:13:30,790 --> 00:13:36,550 >> So that's going to be my array called Scores. 306 00:13:36,550 --> 00:13:37,730 As you can see, it's size three. 307 00:13:37,730 --> 00:13:39,210 There are three boxes in there. 308 00:13:39,210 --> 00:13:43,120 Each one can fit what type of data type? 309 00:13:43,120 --> 00:13:43,790 >> STUDENT: Integer. 310 00:13:43,790 --> 00:13:45,250 >> JASON HIRSCHHORN: An integer in this one. 311 00:13:45,250 --> 00:13:48,790 We wrote int scores, and the first word is the data type that's going to 312 00:13:48,790 --> 00:13:50,910 go in the boxes in that array. 313 00:13:50,910 --> 00:13:52,160 So I can fit three. 314 00:13:52,160 --> 00:13:54,880 315 00:13:54,880 --> 00:13:57,000 Why do I have zero up there on the board? 316 00:13:57,000 --> 00:13:59,240 Why does it say score zero equals 1? 317 00:13:59,240 --> 00:14:02,090 318 00:14:02,090 --> 00:14:03,180 Remind me of your name. 319 00:14:03,180 --> 00:14:03,470 >> STUDENT: Kurt. 320 00:14:03,470 --> 00:14:04,070 >> JASON HIRSCHHORN: Kurt. 321 00:14:04,070 --> 00:14:04,480 >> STUDENT: Yes. 322 00:14:04,480 --> 00:14:04,920 >> JASON HIRSCHHORN: Why? 323 00:14:04,920 --> 00:14:07,230 >> STUDENT: Because the first index is zero, we start counting at zero. 324 00:14:07,230 --> 00:14:08,480 >> JASON HIRSCHHORN: So we start counting at zero. 325 00:14:08,480 --> 00:14:09,580 That's exactly right. 326 00:14:09,580 --> 00:14:12,340 This is the confusing part and will trip you up, especially when you're 327 00:14:12,340 --> 00:14:14,900 trying to iterate through arrays, which we'll do in a bit. 328 00:14:14,900 --> 00:14:15,980 We start counting at zero. 329 00:14:15,980 --> 00:14:19,170 So if I want to refer to the first box, it's not box one. 330 00:14:19,170 --> 00:14:22,290 Unfortunately, it's box zero. 331 00:14:22,290 --> 00:14:26,460 The second one is box one, and in this case, the third one is box two. 332 00:14:26,460 --> 00:14:28,700 >> What about box three? 333 00:14:28,700 --> 00:14:30,670 Where is box three? 334 00:14:30,670 --> 00:14:34,750 Or if I want to do scores, bracket three, close bracket, what's that? 335 00:14:34,750 --> 00:14:37,650 336 00:14:37,650 --> 00:14:39,760 So you said the null character. 337 00:14:39,760 --> 00:14:41,600 Any other thoughts? 338 00:14:41,600 --> 00:14:44,590 What is score bracket three bracket? 339 00:14:44,590 --> 00:14:48,045 >> STUDENT: It doesn't really exist because you only defined as scores, 340 00:14:48,045 --> 00:14:49,370 bracket three. 341 00:14:49,370 --> 00:14:51,400 >> JASON HIRSCHHORN: So you say it doesn't really exist. 342 00:14:51,400 --> 00:14:54,230 In some cases, when we're dealing with strings, generally we'll put a null 343 00:14:54,230 --> 00:14:54,790 character at the end. 344 00:14:54,790 --> 00:14:56,100 We'll get to that in a second. 345 00:14:56,100 --> 00:15:01,020 But in this case, there is no third box here. 346 00:15:01,020 --> 00:15:06,390 So the last box in any array is size minus 1. 347 00:15:06,390 --> 00:15:11,310 So if we have an array of size n, the first box is at zero, the last one is 348 00:15:11,310 --> 00:15:12,700 at n minus 1. 349 00:15:12,700 --> 00:15:14,970 And in this case, it doesn't exist. 350 00:15:14,970 --> 00:15:17,970 There is something here in memory but it's not ours. 351 00:15:17,970 --> 00:15:18,460 We don't own it. 352 00:15:18,460 --> 00:15:19,570 We didn't ask for it. 353 00:15:19,570 --> 00:15:22,606 So if we try to access it, which we'll do in a bit, we're probably going to 354 00:15:22,606 --> 00:15:24,970 get into trouble. 355 00:15:24,970 --> 00:15:29,180 >> In this case, I'm initializing my array with three variables. 356 00:15:29,180 --> 00:15:36,195 We're putting a one in box zero, a two in box one, and a three in box two. 357 00:15:36,195 --> 00:15:39,280 Does anybody have any questions about initializing an array? 358 00:15:39,280 --> 00:15:44,540 359 00:15:44,540 --> 00:15:46,970 >> So we went over this just now. 360 00:15:46,970 --> 00:15:52,725 Scores, bracket three, close bracket, that doesn't exist. 361 00:15:52,725 --> 00:15:58,130 We're zero indexed in computer science, so we start counting at zero. 362 00:15:58,130 --> 00:16:01,860 The last box available to us, it is the third box, but it's called box 363 00:16:01,860 --> 00:16:03,110 number two. 364 00:16:03,110 --> 00:16:05,790 365 00:16:05,790 --> 00:16:09,710 >> Down below, this is an alternative way of initializing arrays. 366 00:16:09,710 --> 00:16:13,410 So instead of creating the array on one line, filling the first box, then 367 00:16:13,410 --> 00:16:17,070 the second box, then the third box, you can do it all in one line. 368 00:16:17,070 --> 00:16:19,040 So in that line, both arrays. 369 00:16:19,040 --> 00:16:23,460 This creates the same exact array as what we created right here, but it 370 00:16:23,460 --> 00:16:24,710 just does it in one line. 371 00:16:24,710 --> 00:16:29,740 372 00:16:29,740 --> 00:16:31,360 >> Now we have created our array. 373 00:16:31,360 --> 00:16:34,100 Usually once you create an array, you want to do something with it, whether 374 00:16:34,100 --> 00:16:38,430 that's fill it or get things out of it and print them out on the screen. 375 00:16:38,430 --> 00:16:42,270 In this case, we're going through our scores array, which we initialized on 376 00:16:42,270 --> 00:16:44,740 the first line up here in gray. 377 00:16:44,740 --> 00:16:49,600 And then on the second line, we start iterating through the array. 378 00:16:49,600 --> 00:16:52,640 Does anybody have any questions about the structure of a for loop? 379 00:16:52,640 --> 00:16:55,250 380 00:16:55,250 --> 00:16:56,400 >> So this is the for loop. 381 00:16:56,400 --> 00:16:59,530 We have our initial condition, and then our final condition, and then we 382 00:16:59,530 --> 00:17:01,160 iterate, i, each loop. 383 00:17:01,160 --> 00:17:05,579 And in the curly braces in this for loop, what happens if I were to run 384 00:17:05,579 --> 00:17:06,290 this program? 385 00:17:06,290 --> 00:17:10,410 Can anybody tell me what we would get? 386 00:17:10,410 --> 00:17:11,589 [? Matu? ?] 387 00:17:11,589 --> 00:17:13,819 >> MATU: Print all the scores on each line. 388 00:17:13,819 --> 00:17:14,900 It would print each score on a separate line. 389 00:17:14,900 --> 00:17:16,300 >> JASON HIRSCHHORN: What exactly would it print? 390 00:17:16,300 --> 00:17:19,790 >> STUDENT: It would print one, then two, then three. 391 00:17:19,790 --> 00:17:20,235 >> JASON HIRSCHHORN: Exactly. 392 00:17:20,235 --> 00:17:23,490 It would print one, then two, then three, each on separate lines, because 393 00:17:23,490 --> 00:17:27,810 we're going through the array and you'll see that second, the printf 394 00:17:27,810 --> 00:17:29,275 statement, scores i. 395 00:17:29,275 --> 00:17:32,880 So on the first round through our for loop, i equals zero, which means we're 396 00:17:32,880 --> 00:17:34,640 getting the value at score zero-- 397 00:17:34,640 --> 00:17:36,900 which, if we go back to our diagram, is one-- 398 00:17:36,900 --> 00:17:38,630 and filling in-- 399 00:17:38,630 --> 00:17:39,700 oh, it says c. 400 00:17:39,700 --> 00:17:41,680 That should be %d. 401 00:17:41,680 --> 00:17:43,970 I will fix that in a bit when I hand that out to you guys. 402 00:17:43,970 --> 00:17:48,670 But we're presenting an integer so it should be %d right here, not %c. 403 00:17:48,670 --> 00:17:50,340 What is %c for? 404 00:17:50,340 --> 00:17:50,790 >> STUDENT: Character. 405 00:17:50,790 --> 00:17:52,680 >> JASON HIRSCHHORN: Character, exactly. 406 00:17:52,680 --> 00:17:53,690 What is the other one? 407 00:17:53,690 --> 00:17:56,260 If you don't want to do %d for an integer, what else can you do? 408 00:17:56,260 --> 00:17:57,000 >> STUDENT: i. 409 00:17:57,000 --> 00:17:59,180 >> JASON HIRSCHHORN: %i, exactly. 410 00:17:59,180 --> 00:18:01,660 So any questions about iterating through the array? 411 00:18:01,660 --> 00:18:05,500 412 00:18:05,500 --> 00:18:07,800 So what happens if we do this? 413 00:18:07,800 --> 00:18:10,290 Instead of doing i is less than three, we do i is less 414 00:18:10,290 --> 00:18:11,390 than or equal to three. 415 00:18:11,390 --> 00:18:13,420 What happens when we run this code? 416 00:18:13,420 --> 00:18:17,510 417 00:18:17,510 --> 00:18:18,940 Yes, Annie? 418 00:18:18,940 --> 00:18:22,030 >> STUDENT: They're going to print out weird stuff on the fourth line. 419 00:18:22,030 --> 00:18:23,850 >> JASON HIRSCHHORN: So you said, is it going to print out weird stuff on the 420 00:18:23,850 --> 00:18:24,390 fourth line? 421 00:18:24,390 --> 00:18:25,750 Does anybody have an answer to her question? 422 00:18:25,750 --> 00:18:28,940 423 00:18:28,940 --> 00:18:33,160 So the odds are that when we get to the-- 424 00:18:33,160 --> 00:18:36,700 so on the fourth round through our for loop, just like you thought, Annie, 425 00:18:36,700 --> 00:18:39,430 it'll be scores bracket three, which we've already figured out doesn't 426 00:18:39,430 --> 00:18:41,990 exist, and we're going to try and access that, print it 427 00:18:41,990 --> 00:18:43,350 out, use it some way. 428 00:18:43,350 --> 00:18:46,060 So the computer will probably get mad at us. 429 00:18:46,060 --> 00:18:46,760 You're exactly right. 430 00:18:46,760 --> 00:18:48,830 >> It might quit the program altogether. 431 00:18:48,830 --> 00:18:52,222 It might print out something really weird. 432 00:18:52,222 --> 00:18:53,420 You can't say for sure. 433 00:18:53,420 --> 00:18:56,210 Odds are it'll probably get mad at you and just quit right away. 434 00:18:56,210 --> 00:19:00,100 >> But this is a common mistake you'll have where you're just off by one at 435 00:19:00,100 --> 00:19:05,110 the end, and this is a bug that will plague you throughout your coding. 436 00:19:05,110 --> 00:19:09,350 437 00:19:09,350 --> 00:19:14,150 One way I like to do is just on a whiteboard, go through each iteration 438 00:19:14,150 --> 00:19:18,190 of the loop on hand or on paper to see what's happening, and then you 439 00:19:18,190 --> 00:19:20,440 generally realize, oh wait, I shouldn't have gone to less than or 440 00:19:20,440 --> 00:19:20,870 equal to three. 441 00:19:20,870 --> 00:19:24,730 I should have stopped at two, which is what happens if your condition is less 442 00:19:24,730 --> 00:19:26,206 than three. 443 00:19:26,206 --> 00:19:29,120 Any questions so far? 444 00:19:29,120 --> 00:19:31,010 Sweet. 445 00:19:31,010 --> 00:19:33,360 >> So you all have your computers open. 446 00:19:33,360 --> 00:19:36,760 We're going to give you two to three minutes to write a program that 447 00:19:36,760 --> 00:19:40,460 creates an array with the integers one through five and then prints out each 448 00:19:40,460 --> 00:19:41,720 integer on a new line. 449 00:19:41,720 --> 00:19:44,330 450 00:19:44,330 --> 00:19:46,000 And if you have any questions, raise your hand. 451 00:19:46,000 --> 00:19:48,140 I'm happy to come by. 452 00:19:48,140 --> 00:19:50,630 Like I said, let's take three minutes to write this. 453 00:19:50,630 --> 00:20:41,300 454 00:20:41,300 --> 00:20:45,110 Is everyone OK if I go to a new window and close this? 455 00:20:45,110 --> 00:22:13,420 456 00:22:13,420 --> 00:22:15,520 >> I'm sorry if you haven't had a chance to finish, but we're going to do it 457 00:22:15,520 --> 00:22:17,310 together right now. 458 00:22:17,310 --> 00:22:23,640 So up on the screen, I have #include cs50.h, #include stdio.h. 459 00:22:23,640 --> 00:22:26,090 Can somebody tell me what those two lines do? 460 00:22:26,090 --> 00:22:32,250 461 00:22:32,250 --> 00:22:33,155 Michael? 462 00:22:33,155 --> 00:22:40,030 >> STUDENT: They include the libraries that CS50 made and that Steve has 463 00:22:40,030 --> 00:22:44,210 saved for you to use to make your life easier. 464 00:22:44,210 --> 00:22:46,820 >> JASON HIRSCHHORN: So you said they include the libraries, one that CS50 465 00:22:46,820 --> 00:22:51,230 made, cs50.h, and one that is just standard, comes with 466 00:22:51,230 --> 00:22:53,510 the C package, stdio.h. 467 00:22:53,510 --> 00:22:54,890 What do you mean include the libraries, though? 468 00:22:54,890 --> 00:22:55,770 Can you be a bit more specific? 469 00:22:55,770 --> 00:23:01,180 >> STUDENT: It makes it so that you can use certain code words to have a 470 00:23:01,180 --> 00:23:05,710 function happen without having to go and make the function c yourself. 471 00:23:05,710 --> 00:23:08,730 >> JASON HIRSCHHORN: Can you give me an example? 472 00:23:08,730 --> 00:23:11,160 >> STUDENT: Like printf is one you can use. 473 00:23:11,160 --> 00:23:11,860 >> JASON HIRSCHHORN: Printf. 474 00:23:11,860 --> 00:23:14,680 So you said printf is one of those functions where rather than having to 475 00:23:14,680 --> 00:23:19,840 write printf on your own, printf is included actually in stdio.h, so you 476 00:23:19,840 --> 00:23:22,010 can just use it and assume that it will work. 477 00:23:22,010 --> 00:23:28,270 In stdio.h, if you opened up that file, would it include how printf 478 00:23:28,270 --> 00:23:31,620 works, all of the code for printf? 479 00:23:31,620 --> 00:23:33,105 Why are you shaking your head? 480 00:23:33,105 --> 00:23:35,710 >> STUDENT: Because the header file just includes the function declaration. 481 00:23:35,710 --> 00:23:36,400 >> JASON HIRSCHHORN: Exactly right. 482 00:23:36,400 --> 00:23:39,680 The header file just includes the function declaration. 483 00:23:39,680 --> 00:23:42,950 So basically, when you compile this, hit Make Count, which we'll do in a 484 00:23:42,950 --> 00:23:48,070 second, the compiler knows that if you use printf, printf exists. 485 00:23:48,070 --> 00:23:49,370 It's going to be defined somewhere. 486 00:23:49,370 --> 00:23:51,200 It's not a problem for you using it. 487 00:23:51,200 --> 00:23:55,490 And then later on, it'll include the ones and zeros that actually contain 488 00:23:55,490 --> 00:23:58,360 printf's implementation and merge them with your code. 489 00:23:58,360 --> 00:24:01,935 How do you make sure it includes the ones and zeros for, say, get string? 490 00:24:01,935 --> 00:24:05,670 491 00:24:05,670 --> 00:24:07,210 >> STUDENT: Get string is in cs50.h. 492 00:24:07,210 --> 00:24:07,830 >> JASON HIRSCHHORN: Right. 493 00:24:07,830 --> 00:24:11,110 So that says get string exists, the function declaration. 494 00:24:11,110 --> 00:24:15,410 But then the ones and zeros, the binary file that actually includes the 495 00:24:15,410 --> 00:24:17,370 implementation of get string? 496 00:24:17,370 --> 00:24:19,310 How do you make sure that goes with your program? 497 00:24:19,310 --> 00:24:23,220 >> STUDENT: Pass the linker in the command line argument. 498 00:24:23,220 --> 00:24:25,430 >> JASON HIRSCHHORN: Pass the linker in the command line argument. 499 00:24:25,430 --> 00:24:26,700 How do you do that? 500 00:24:26,700 --> 00:24:29,730 >> STUDENT: It's the -l line in the clang 501 00:24:29,730 --> 00:24:30,480 >> JASON HIRSCHHORN: Exactly right. 502 00:24:30,480 --> 00:24:32,900 You say -l cs50, right? 503 00:24:32,900 --> 00:24:34,940 That includes the ones and zeros. 504 00:24:34,940 --> 00:24:36,460 >> STUDENT: But Make does that for you, right? 505 00:24:36,460 --> 00:24:37,010 >> JASON HIRSCHHORN: Exactly. 506 00:24:37,010 --> 00:24:40,390 Make does that for you, but eventually, you won't have Make, or 507 00:24:40,390 --> 00:24:43,220 you'll have to make a Make on your own, so that's a 508 00:24:43,220 --> 00:24:43,940 good thing to remember. 509 00:24:43,940 --> 00:24:47,350 If you were going to use one of these other libraries, not only do you have 510 00:24:47,350 --> 00:24:49,490 to write #include and then the library. 511 00:24:49,490 --> 00:24:51,420 You also need to link it in. 512 00:24:51,420 --> 00:24:54,480 And those two things are different. -l cs50 is different 513 00:24:54,480 --> 00:24:59,260 than #include cs50.h. 514 00:24:59,260 --> 00:25:02,870 >> Then we have int main void curly braces, and now we're going to code 515 00:25:02,870 --> 00:25:03,590 inside main. 516 00:25:03,590 --> 00:25:07,090 The first line I have here is a comment that says, create an array 517 00:25:07,090 --> 00:25:08,820 with ints one through five. 518 00:25:08,820 --> 00:25:12,600 What is a good word to describe what I have right now on the screen, just 519 00:25:12,600 --> 00:25:14,170 comments inside main? 520 00:25:14,170 --> 00:25:15,722 What do we call that? 521 00:25:15,722 --> 00:25:16,820 >> STUDENT: Pseudocode. 522 00:25:16,820 --> 00:25:17,840 >> JASON HIRSCHHORN: Pseudocode. 523 00:25:17,840 --> 00:25:18,420 Thank you, Jeffrey. 524 00:25:18,420 --> 00:25:19,540 That is exactly right. 525 00:25:19,540 --> 00:25:20,005 Pseudocode. 526 00:25:20,005 --> 00:25:22,854 Have you guys seen pseudocode before? 527 00:25:22,854 --> 00:25:28,340 Raise your hand if you've used pseudocode before problem set one. 528 00:25:28,340 --> 00:25:29,400 About half of you. 529 00:25:29,400 --> 00:25:32,700 >> I would highly recommend using pseudocode to break down problems into 530 00:25:32,700 --> 00:25:35,210 smaller, more easy to bite off pieces. 531 00:25:35,210 --> 00:25:38,630 Also, if you do the pseudocode, you have your comments already largely 532 00:25:38,630 --> 00:25:40,760 built in, so you don't have to go back and try to figure out 533 00:25:40,760 --> 00:25:42,450 what you were doing. 534 00:25:42,450 --> 00:25:44,730 So can somebody give me this first line to create an array with ints one 535 00:25:44,730 --> 00:25:45,980 through five? 536 00:25:45,980 --> 00:25:48,700 537 00:25:48,700 --> 00:25:49,130 You? 538 00:25:49,130 --> 00:25:53,350 >> STUDENT: Int and then you can call whatever number, and then square 539 00:25:53,350 --> 00:25:54,830 bracket five. 540 00:25:54,830 --> 00:25:57,820 >> JASON HIRSCHHORN: So int numbers, square bracket five. 541 00:25:57,820 --> 00:25:59,160 >> STUDENT: Equals, and then curly braces. 542 00:25:59,160 --> 00:26:00,830 >> JASON HIRSCHHORN: Equals, curly braces. 543 00:26:00,830 --> 00:26:01,830 >> STUDENT: One, two, three, four. 544 00:26:01,830 --> 00:26:05,566 >> JASON HIRSCHHORN: One, two, three, four, five. 545 00:26:05,566 --> 00:26:07,390 >> STUDENT: And then curly braces and then semicolon. 546 00:26:07,390 --> 00:26:09,240 >> JASON HIRSCHHORN: And curly braces and semicolon. 547 00:26:09,240 --> 00:26:10,330 Excellent. 548 00:26:10,330 --> 00:26:12,210 Print out each int on a new line. 549 00:26:12,210 --> 00:26:17,140 Can somebody else give me that code, please? 550 00:26:17,140 --> 00:26:17,530 [? Matu? ?] 551 00:26:17,530 --> 00:26:18,560 Go ahead. 552 00:26:18,560 --> 00:26:20,440 >> STUDENT: For ( 553 00:26:20,440 --> 00:26:21,750 >> JASON HIRSCHHORN: For ( 554 00:26:21,750 --> 00:26:23,740 >> STUDENT: Int i=0 555 00:26:23,740 --> 00:26:26,070 >> JASON HIRSCHHORN: Int i=0 556 00:26:26,070 --> 00:26:28,190 >> STUDENT: Semicolon i less than five. 557 00:26:28,190 --> 00:26:31,680 >> JASON HIRSCHHORN: Semicolon, i less than 5. 558 00:26:31,680 --> 00:26:33,600 >> STUDENT: Semicolon, i++. 559 00:26:33,600 --> 00:26:35,140 >> JASON HIRSCHHORN: Semicolon, i++. 560 00:26:35,140 --> 00:26:37,720 >> STUDENT: Closed parentheses, then open brace. 561 00:26:37,720 --> 00:26:39,410 >> JASON HIRSCHHORN: Closed parentheses and two braces. 562 00:26:39,410 --> 00:26:40,510 Inside the braces? 563 00:26:40,510 --> 00:26:47,640 >> STUDENT: And then printf "%d/n" 564 00:26:47,640 --> 00:26:48,695 >> JASON HIRSCHHORN: Sorry, hold on. 565 00:26:48,695 --> 00:26:52,330 Printf "%d/n, closed quote. 566 00:26:52,330 --> 00:26:55,980 >> STUDENT: And then closed quote, comma, count, bracket i. 567 00:26:55,980 --> 00:26:58,350 >> JASON HIRSCHHORN: Comma, count, bracket i. 568 00:26:58,350 --> 00:26:59,970 >> STUDENT: Sorry, numbers bracket i. 569 00:26:59,970 --> 00:27:01,416 >> JASON HIRSCHHORN: Numbers, bracket i. 570 00:27:01,416 --> 00:27:03,200 >> STUDENT: Sorry. 571 00:27:03,200 --> 00:27:04,750 And then closed parentheses, semicolon. 572 00:27:04,750 --> 00:27:07,156 >> JASON HIRSCHHORN: Closed parentheses, semicolon. 573 00:27:07,156 --> 00:27:08,680 I'm going to save that. 574 00:27:08,680 --> 00:27:12,670 We're going to make count. 575 00:27:12,670 --> 00:27:14,742 And how do I run count? 576 00:27:14,742 --> 00:27:16,470 >> STUDENT: Backslash. 577 00:27:16,470 --> 00:27:17,180 >> STUDENT: Dot forward slash-- 578 00:27:17,180 --> 00:27:18,050 >> JASON HIRSCHHORN: ./count, exactly. 579 00:27:18,050 --> 00:27:19,380 Thank you. ./count. 580 00:27:19,380 --> 00:27:21,060 One, two, three, four, five on new lines. 581 00:27:21,060 --> 00:27:22,110 Looks like it worked. 582 00:27:22,110 --> 00:27:23,200 Yes, Noah? 583 00:27:23,200 --> 00:27:26,660 >> STUDENT: You could also do less than or equal to four, like i is less than 584 00:27:26,660 --> 00:27:27,630 or equal to four? 585 00:27:27,630 --> 00:27:31,190 Is there any advantage to doing less than five, or just a different way of 586 00:27:31,190 --> 00:27:31,610 writing it? 587 00:27:31,610 --> 00:27:35,280 >> JASON HIRSCHHORN: So the question is you could have done i is less than or 588 00:27:35,280 --> 00:27:37,450 equal to four instead of i is less than five. 589 00:27:37,450 --> 00:27:38,935 Would that have worked the same? 590 00:27:38,935 --> 00:27:39,760 >> STUDENT: It should. 591 00:27:39,760 --> 00:27:39,860 >> JASON HIRSCHHORN: Yes. 592 00:27:39,860 --> 00:27:41,660 It totally would have work the same. 593 00:27:41,660 --> 00:27:44,780 Stylistically, it's generally less than, and you'll see in a second when 594 00:27:44,780 --> 00:27:48,160 we do strings, you can use something called strlen, which you've seen 595 00:27:48,160 --> 00:27:50,380 before, to get the length of the string. 596 00:27:50,380 --> 00:27:54,090 And you can't do less than or equal strlen. 597 00:27:54,090 --> 00:27:56,500 You could do less than or equal strlen minus one. 598 00:27:56,500 --> 00:27:59,260 So it's just nicer to do less than strlen. 599 00:27:59,260 --> 00:28:00,240 Yes? 600 00:28:00,240 --> 00:28:03,270 >> STUDENT: And in the actual quizzes, do we have to provide comments on our 601 00:28:03,270 --> 00:28:04,620 code as well? 602 00:28:04,620 --> 00:28:06,130 >> JASON HIRSCHHORN: The question was in quizzes, do you 603 00:28:06,130 --> 00:28:07,760 have to provide comments? 604 00:28:07,760 --> 00:28:12,050 So we'll talk more about quizzes when they come up, but generally, a good 605 00:28:12,050 --> 00:28:14,530 rule of thumb is whenever you're writing code, provide comments. 606 00:28:14,530 --> 00:28:18,130 If you get the code wrong but the comment right, you might get some 607 00:28:18,130 --> 00:28:22,280 credit both from me if I was grading you on a problem set or on a quiz. 608 00:28:22,280 --> 00:28:24,660 So comments are always good to provide. 609 00:28:24,660 --> 00:28:25,040 Sweet. 610 00:28:25,040 --> 00:28:25,460 Count. 611 00:28:25,460 --> 00:28:26,720 Excellent job. 612 00:28:26,720 --> 00:28:28,570 Let's move on. 613 00:28:28,570 --> 00:28:29,920 >> Strings. 614 00:28:29,920 --> 00:28:30,630 Strings. 615 00:28:30,630 --> 00:28:31,730 We've seen this before in lecture. 616 00:28:31,730 --> 00:28:33,390 This blew my mind when I first found this out. 617 00:28:33,390 --> 00:28:37,240 It probably didn't blow anybody's mind here, but that's OK. 618 00:28:37,240 --> 00:28:41,260 >> Strings are arrays of characters, and they end with, as you said earlier, 619 00:28:41,260 --> 00:28:45,710 the null terminator, which is this /0 character. 620 00:28:45,710 --> 00:28:47,740 If we're going to draw a string on the board, we'll draw it over here. 621 00:28:47,740 --> 00:28:53,080 622 00:28:53,080 --> 00:28:56,420 So how long is this string? 623 00:28:56,420 --> 00:28:56,680 Sorry. 624 00:28:56,680 --> 00:28:57,660 How long is this string? 625 00:28:57,660 --> 00:28:57,910 >> STUDENT: [INAUDIBLE]. 626 00:28:57,910 --> 00:28:59,010 >> JASON HIRSCHHORN: Four? 627 00:28:59,010 --> 00:28:59,880 Why is it four? 628 00:28:59,880 --> 00:29:02,240 >> STUDENT: Because the fifth spot is for the null character. 629 00:29:02,240 --> 00:29:04,070 >> JASON HIRSCHHORN: So the fifth spot is for the null character. 630 00:29:04,070 --> 00:29:07,130 So if I were to run strlen on this, it would give me four. 631 00:29:07,130 --> 00:29:07,850 Correct. 632 00:29:07,850 --> 00:29:11,640 And the array, though, if I were to initialize this array, how many spaces 633 00:29:11,640 --> 00:29:13,444 would I have to ask for? 634 00:29:13,444 --> 00:29:13,821 >> STUDENT: Five. 635 00:29:13,821 --> 00:29:14,200 >> JASON HIRSCHHORN: Five. 636 00:29:14,200 --> 00:29:17,650 I would have to ask for five, but I can only put four characters in here 637 00:29:17,650 --> 00:29:19,750 because I need to put that null terminator on the end. 638 00:29:19,750 --> 00:29:25,260 So we're going to write in "ohai" and put the null terminator in at the end. 639 00:29:25,260 --> 00:29:31,820 640 00:29:31,820 --> 00:29:34,940 We'll say this is string s equals this. 641 00:29:34,940 --> 00:29:36,610 What is s bracket zero? 642 00:29:36,610 --> 00:29:40,210 643 00:29:40,210 --> 00:29:41,550 s bracket zero, Annie? 644 00:29:41,550 --> 00:29:41,950 >> STUDENT: O. 645 00:29:41,950 --> 00:29:45,400 >> JASON HIRSCHHORN: O, exactly. s bracket one? 646 00:29:45,400 --> 00:29:48,200 s bracket five? 647 00:29:48,200 --> 00:29:52,670 648 00:29:52,670 --> 00:29:53,955 s bracket five? 649 00:29:53,955 --> 00:29:54,740 >> STUDENT: Doesn't exist. 650 00:29:54,740 --> 00:29:55,040 >> JASON HIRSCHHORN: Doesn't exist. 651 00:29:55,040 --> 00:29:57,050 Exactly right. 652 00:29:57,050 --> 00:29:57,940 Cool. 653 00:29:57,940 --> 00:30:03,120 Any questions about understanding strings as arrays of characters? 654 00:30:03,120 --> 00:30:03,870 OK, then. 655 00:30:03,870 --> 00:30:05,440 One more program for you guys. 656 00:30:05,440 --> 00:30:09,190 Write a program that asks the user for a string then prints out each 657 00:30:09,190 --> 00:30:11,880 character on a new line. 658 00:30:11,880 --> 00:30:14,160 So let's take three minutes to write this program, and then we'll code it 659 00:30:14,160 --> 00:30:15,410 as a group. 660 00:30:15,410 --> 00:31:04,730 661 00:31:04,730 --> 00:31:04,830 >> Sorry. 662 00:31:04,830 --> 00:31:06,470 Did you need to see this? 663 00:31:06,470 --> 00:31:07,410 >> STUDENT: No. 664 00:31:07,410 --> 00:31:08,660 >> JASON HIRSCHHORN: OK. 665 00:31:08,660 --> 00:31:36,390 666 00:31:36,390 --> 00:31:40,605 Let's take one more minute to code this, or at least get started on it. 667 00:31:40,605 --> 00:32:13,900 668 00:32:13,900 --> 00:32:17,020 >> So you can keep working, but I'm going to start going through it. 669 00:32:17,020 --> 00:32:22,560 So like before, I have on the screen #include cs50.h, #include stdio.h. 670 00:32:22,560 --> 00:32:24,020 int main void. 671 00:32:24,020 --> 00:32:28,880 Inside main's curly braces, I have two comments, pseudocode. 672 00:32:28,880 --> 00:32:30,980 One is ask for a string from the user. 673 00:32:30,980 --> 00:32:33,900 The second is print out each character on a new line. 674 00:32:33,900 --> 00:32:39,780 So Anna, can you give me ask for a string from the user? 675 00:32:39,780 --> 00:32:41,970 >> STUDENT: Printf. 676 00:32:41,970 --> 00:32:42,720 >> JASON HIRSCHHORN: Printf? 677 00:32:42,720 --> 00:32:45,050 >> STUDENT: Yes. 678 00:32:45,050 --> 00:32:46,165 ( 679 00:32:46,165 --> 00:32:47,610 >> JASON HIRSCHHORN: ( 680 00:32:47,610 --> 00:32:49,300 >> STUDENT: "give me a string. 681 00:32:49,300 --> 00:32:52,380 >> JASON HIRSCHHORN: "give me a str-- oops-- 682 00:32:52,380 --> 00:32:53,710 string. 683 00:32:53,710 --> 00:32:59,980 >> STUDENT: /n") semicolon. 684 00:32:59,980 --> 00:33:02,300 >> JASON HIRSCHHORN: End the string, closed parentheses, semicolon. 685 00:33:02,300 --> 00:33:03,550 >> STUDENT: Yes. 686 00:33:03,550 --> 00:33:09,080 And then next line, string s = get string. 687 00:33:09,080 --> 00:33:11,240 >> JASON HIRSCHHORN: String s = get string. 688 00:33:11,240 --> 00:33:11,665 >> STUDENT: Yes. 689 00:33:11,665 --> 00:33:13,080 And then ( ) 690 00:33:13,080 --> 00:33:14,365 >> JASON HIRSCHHORN: ( ) 691 00:33:14,365 --> 00:33:14,720 >> STUDENT: Semicolon. 692 00:33:14,720 --> 00:33:15,540 >> JASON HIRSCHHORN: Semicolon. 693 00:33:15,540 --> 00:33:16,890 Fantastic. 694 00:33:16,890 --> 00:33:19,000 So I'm going to get a string and I'm going to save it in what? 695 00:33:19,000 --> 00:33:21,830 What's the variable called that you created? 696 00:33:21,830 --> 00:33:23,290 >> STUDENT: String s. 697 00:33:23,290 --> 00:33:24,580 >> JASON HIRSCHHORN: The variable is called s. 698 00:33:24,580 --> 00:33:25,700 It's type string. 699 00:33:25,700 --> 00:33:27,200 Exactly. 700 00:33:27,200 --> 00:33:30,080 Next thing we want to is print out each character on a new line. 701 00:33:30,080 --> 00:33:31,710 Who can give me that code? 702 00:33:31,710 --> 00:33:32,110 [INAUDIBLE]? 703 00:33:32,110 --> 00:33:33,000 Can you give it to me? 704 00:33:33,000 --> 00:33:33,580 >> STUDENT: Sure. 705 00:33:33,580 --> 00:33:35,420 >> JASON HIRSCHHORN: Let's do it. 706 00:33:35,420 --> 00:33:36,080 And sorry. 707 00:33:36,080 --> 00:33:38,896 I don't like to repeat what you say because you're getting it right, but I 708 00:33:38,896 --> 00:33:42,100 have to repeat it so that when they transcribe this, they can get all of 709 00:33:42,100 --> 00:33:44,870 the text out on the transcriptions. 710 00:33:44,870 --> 00:33:47,180 So you can just keep talking. 711 00:33:47,180 --> 00:33:48,590 Unfortunately, I have to say it over you. 712 00:33:48,590 --> 00:33:49,510 I'm sorry, but-- 713 00:33:49,510 --> 00:33:50,610 >> STUDENT: That's fine. 714 00:33:50,610 --> 00:33:53,010 For int i=0 715 00:33:53,010 --> 00:33:54,170 >> JASON HIRSCHHORN: For int i=0 716 00:33:54,170 --> 00:33:56,740 >> STUDENT: I less than-- 717 00:33:56,740 --> 00:33:58,206 >> JASON HIRSCHHORN: Semicolon. 718 00:33:58,206 --> 00:34:00,650 >> STUDENT: Before this, can we put a variable so you get int name length? 719 00:34:00,650 --> 00:34:01,870 >> JASON HIRSCHHORN: Oh, you don't want to put the semicolon? 720 00:34:01,870 --> 00:34:02,310 OK. 721 00:34:02,310 --> 00:34:05,120 So what do you want to do after zero? 722 00:34:05,120 --> 00:34:09,340 >> STUDENT: I want to get the string length of s and 723 00:34:09,340 --> 00:34:10,410 set that to a variable? 724 00:34:10,410 --> 00:34:11,760 >> JASON HIRSCHHORN: OK, so how do I do that? 725 00:34:11,760 --> 00:34:17,636 >> STUDENT: So you do int length = 726 00:34:17,636 --> 00:34:20,010 >> JASON HIRSCHHORN: Int length = 727 00:34:20,010 --> 00:34:22,230 >> STUDENT: S-T-R-L-E-N. 728 00:34:22,230 --> 00:34:24,260 >> JASON HIRSCHHORN: S-T-R-L-E-N-- 729 00:34:24,260 --> 00:34:25,380 >> STUDENT: Of s. 730 00:34:25,380 --> 00:34:25,790 Brackets. 731 00:34:25,790 --> 00:34:27,920 >> JASON HIRSCHHORN: (s). 732 00:34:27,920 --> 00:34:33,040 >> STUDENT: And then I think you have to put a semicolon between the two ints. 733 00:34:33,040 --> 00:34:34,590 >> JASON HIRSCHHORN: A semicolon after zero? 734 00:34:34,590 --> 00:34:35,260 >> STUDENT: Yeah. 735 00:34:35,260 --> 00:34:40,670 Then put a semicolon after that, and you say, i less than length. 736 00:34:40,670 --> 00:34:43,250 i less than length, which is the variable. 737 00:34:43,250 --> 00:34:44,590 >> STUDENT: And then semicolon. 738 00:34:44,590 --> 00:34:45,790 >> JASON HIRSCHHORN: Semicolon. 739 00:34:45,790 --> 00:34:46,290 >> STUDENT: i++. 740 00:34:46,290 --> 00:34:46,909 >> JASON HIRSCHHORN: i++. 741 00:34:46,909 --> 00:34:47,530 There's our for loop. 742 00:34:47,530 --> 00:34:48,860 OK, inside the for loop. 743 00:34:48,860 --> 00:34:51,670 >> STUDENT: And then do printf. 744 00:34:51,670 --> 00:34:53,340 >> JASON HIRSCHHORN: Printf. 745 00:34:53,340 --> 00:34:53,889 >> STUDENT: Brackets. 746 00:34:53,889 --> 00:34:55,130 >> JASON HIRSCHHORN: Parentheses, quotes. 747 00:34:55,130 --> 00:34:59,930 >> STUDENT: Parentheses, quotes, %c/n. 748 00:34:59,930 --> 00:35:00,760 >> JASON HIRSCHHORN: %c, OK. 749 00:35:00,760 --> 00:35:01,670 >> STUDENT: /n. 750 00:35:01,670 --> 00:35:03,470 >> JASON HIRSCHHORN: /n. 751 00:35:03,470 --> 00:35:04,410 >> STUDENT: Closed quotes. 752 00:35:04,410 --> 00:35:05,270 >> JASON HIRSCHHORN: Closed quotes. 753 00:35:05,270 --> 00:35:05,810 >> STUDENT: Comma. 754 00:35:05,810 --> 00:35:06,860 >> JASON HIRSCHHORN: Comma. 755 00:35:06,860 --> 00:35:07,800 >> STUDENT: S. 756 00:35:07,800 --> 00:35:08,070 >> JASON HIRSCHHORN: S. 757 00:35:08,070 --> 00:35:08,690 >> STUDENT: Bracket i. 758 00:35:08,690 --> 00:35:10,230 >> JASON HIRSCHHORN: Bracket i. 759 00:35:10,230 --> 00:35:11,520 >> STUDENT: And then closed parentheses. 760 00:35:11,520 --> 00:35:12,540 >> JASON HIRSCHHORN: Closed parentheses. 761 00:35:12,540 --> 00:35:13,660 >> STUDENT: And then semicolon. 762 00:35:13,660 --> 00:35:13,770 >> JASON HIRSCHHORN: Semicolon. 763 00:35:13,770 --> 00:35:14,380 Fantastic. 764 00:35:14,380 --> 00:35:16,890 So let's save this. 765 00:35:16,890 --> 00:35:19,770 Make spell. 766 00:35:19,770 --> 00:35:20,040 Oh boy. 767 00:35:20,040 --> 00:35:22,275 I've got a couple of errors. 768 00:35:22,275 --> 00:35:33,120 First error on line 11, it says, expected expression, and it's giving 769 00:35:33,120 --> 00:35:36,910 me an error that the second int, there's the error. 770 00:35:36,910 --> 00:35:38,430 There some error around there. 771 00:35:38,430 --> 00:35:41,060 Does anybody know what error that is? 772 00:35:41,060 --> 00:35:41,640 Kurt? 773 00:35:41,640 --> 00:35:44,108 >> STUDENT: I think it's supposed to be a comma and not a semicolon. 774 00:35:44,108 --> 00:35:46,010 >> JASON HIRSCHHORN: OK, a comma instead of a semicolon. 775 00:35:46,010 --> 00:35:47,340 So is that all we need to fix? 776 00:35:47,340 --> 00:35:47,655 >> STUDENT: No. 777 00:35:47,655 --> 00:35:48,930 >> JASON HIRSCHHORN: What else do we need to fix? 778 00:35:48,930 --> 00:35:50,920 >> STUDENT: We have to include string.h. 779 00:35:50,920 --> 00:35:51,680 >> JASON HIRSCHHORN: Include string.h. 780 00:35:51,680 --> 00:35:53,730 Where do I need to include that? 781 00:35:53,730 --> 00:35:57,085 >> STUDENT: Same place you're including the other stuff, right 782 00:35:57,085 --> 00:35:59,560 after include stdio.h. 783 00:35:59,560 --> 00:36:01,610 >> JASON HIRSCHHORN: Let's see if this runs first because generally, I like 784 00:36:01,610 --> 00:36:06,360 to fix one thing first, and then run it, and then see the errors we get. 785 00:36:06,360 --> 00:36:11,620 So we're going to clear the terminal with control n, let's do make spell. 786 00:36:11,620 --> 00:36:15,190 787 00:36:15,190 --> 00:36:18,770 So another error on line 11. 788 00:36:18,770 --> 00:36:20,540 Expected semicolon. 789 00:36:20,540 --> 00:36:22,640 And I'm still getting errors on this line. 790 00:36:22,640 --> 00:36:25,270 So it hasn't given me an error saying it doesn't understand what strlen is, 791 00:36:25,270 --> 00:36:28,980 but it is giving me errors on this line, so what else is wrong? 792 00:36:28,980 --> 00:36:29,690 Mark? 793 00:36:29,690 --> 00:36:30,940 Marcus. 794 00:36:30,940 --> 00:36:32,570 >> STUDENT: Do you have to remove the second int declaration? 795 00:36:32,570 --> 00:36:34,990 >> JASON HIRSCHHORN: Remove the second int. 796 00:36:34,990 --> 00:36:35,710 Let's try this. 797 00:36:35,710 --> 00:36:37,600 Now clear the terminal. 798 00:36:37,600 --> 00:36:40,130 Make spell. 799 00:36:40,130 --> 00:36:40,490 Error. 800 00:36:40,490 --> 00:36:42,890 Implicitly declaring library function strlen. 801 00:36:42,890 --> 00:36:44,270 So now we do see that error. 802 00:36:44,270 --> 00:36:45,760 What do we need to include? 803 00:36:45,760 --> 00:36:46,660 >> STUDENT: String.h. 804 00:36:46,660 --> 00:36:51,880 >> JASON HIRSCHHORN: Include string.h, we'll do #include string.h at the top 805 00:36:51,880 --> 00:36:52,920 of the screen. 806 00:36:52,920 --> 00:36:55,450 Let's save, make spell. 807 00:36:55,450 --> 00:36:56,970 Now compiled. 808 00:36:56,970 --> 00:37:01,970 So again, the things we needed to remember line 12 in our for loop when 809 00:37:01,970 --> 00:37:08,010 we're setting it up, int i=0, length = strlen (s). 810 00:37:08,010 --> 00:37:09,350 >> This is a fantastic strategy. 811 00:37:09,350 --> 00:37:12,590 The style of this is great, the design of this is great. 812 00:37:12,590 --> 00:37:15,620 We can go into those reasons later, but this way you only need to call 813 00:37:15,620 --> 00:37:19,040 strlen once instead of every single time you run through the loop. 814 00:37:19,040 --> 00:37:22,255 However, you need to use a comma there and you don't need int again. 815 00:37:22,255 --> 00:37:24,180 If you're declaring both those variables together, you 816 00:37:24,180 --> 00:37:26,850 don't need int again. 817 00:37:26,850 --> 00:37:29,820 And then if you're going to use strlen, which is another library 818 00:37:29,820 --> 00:37:33,560 function not included in stdio.h or cs50.h, make 819 00:37:33,560 --> 00:37:35,310 sure you include string.h. 820 00:37:35,310 --> 00:37:35,900 Kurt? 821 00:37:35,900 --> 00:37:39,085 >> STUDENT: So then if you don't need to say what type length is, does that 822 00:37:39,085 --> 00:37:40,290 mean it has to be an int? 823 00:37:40,290 --> 00:37:41,930 You couldn't have two different types there? 824 00:37:41,930 --> 00:37:43,290 >> JASON HIRSCHHORN: So you could have two different types. 825 00:37:43,290 --> 00:37:45,730 Length is an int here, though. 826 00:37:45,730 --> 00:37:48,590 Length is an int because it's comma and then the same thing. 827 00:37:48,590 --> 00:37:50,460 >> STUDENT: So it assumes that it's going to be the same as the first one? 828 00:37:50,460 --> 00:37:52,423 >> JASON HIRSCHHORN: Exactly. 829 00:37:52,423 --> 00:37:54,970 We're not going to do this now, but you can include two variables on the 830 00:37:54,970 --> 00:37:57,080 same line using one data type. 831 00:37:57,080 --> 00:38:01,415 So int x, y would create two variables. 832 00:38:01,415 --> 00:38:05,830 833 00:38:05,830 --> 00:38:10,770 So int x, y, if we make that, it gives me an unused variable x and y because 834 00:38:10,770 --> 00:38:12,020 we don't use them. 835 00:38:12,020 --> 00:38:17,950 836 00:38:17,950 --> 00:38:21,650 So putting void before is just a fancy trick if you don't use 837 00:38:21,650 --> 00:38:22,810 something, to use it. 838 00:38:22,810 --> 00:38:26,300 But as you see, I create x and y on the same line with a comma. 839 00:38:26,300 --> 00:38:28,450 It's creating two integers right there. 840 00:38:28,450 --> 00:38:31,560 So yes, totally possible to create two things on the same line 841 00:38:31,560 --> 00:38:33,720 separated by a comma. 842 00:38:33,720 --> 00:38:35,470 Let's get rid of those, though. 843 00:38:35,470 --> 00:38:36,370 >> So there's spell. 844 00:38:36,370 --> 00:38:39,200 Thank you guys for coding it for us. 845 00:38:39,200 --> 00:38:43,040 Let's make it one more time, run it. 846 00:38:43,040 --> 00:38:44,580 Give me a string. 847 00:38:44,580 --> 00:38:45,580 [INAUDIBLE], you did most of it. 848 00:38:45,580 --> 00:38:47,910 What's the string? 849 00:38:47,910 --> 00:38:48,250 >> STUDENT: Hello. 850 00:38:48,250 --> 00:38:50,110 >> JASON HIRSCHHORN: Hello. 851 00:38:50,110 --> 00:38:52,810 H-E-L-L-O, each character on one line. 852 00:38:52,810 --> 00:38:53,700 Looks great. 853 00:38:53,700 --> 00:38:54,750 Let's run it again. 854 00:38:54,750 --> 00:38:56,050 Always going to check for corner cases. 855 00:38:56,050 --> 00:38:57,750 What's a good corner case to check? 856 00:38:57,750 --> 00:39:00,250 857 00:39:00,250 --> 00:39:00,890 >> STUDENT: A number? 858 00:39:00,890 --> 00:39:02,240 >> JASON HIRSCHHORN: A number. 859 00:39:02,240 --> 00:39:06,580 So I'll put in 1, and it prints out 1. 860 00:39:06,580 --> 00:39:08,970 It's not asking for letters, so it seems like numbers work. 861 00:39:08,970 --> 00:39:09,890 Good corner case to check. 862 00:39:09,890 --> 00:39:11,740 What's another good corner case to check? 863 00:39:11,740 --> 00:39:12,420 >> STUDENT: A blank string. 864 00:39:12,420 --> 00:39:12,910 >> JASON HIRSCHHORN: Hm? 865 00:39:12,910 --> 00:39:13,880 >> STUDENT: Like an empty string. 866 00:39:13,880 --> 00:39:14,400 >> JASON HIRSCHHORN: Empty string. 867 00:39:14,400 --> 00:39:16,730 So let's not put in anything. 868 00:39:16,730 --> 00:39:18,660 And what gets printed out? 869 00:39:18,660 --> 00:39:19,090 >> STUDENT: Nothing. 870 00:39:19,090 --> 00:39:19,870 >> JASON HIRSCHHORN: Nothing. 871 00:39:19,870 --> 00:39:20,555 Seems like it works. 872 00:39:20,555 --> 00:39:23,790 You could try more corner cases, but generally numbers, a different data is 873 00:39:23,790 --> 00:39:26,160 great to try, as well is nothing. 874 00:39:26,160 --> 00:39:29,180 Nothing, zero, negatives are always great things to try and things you 875 00:39:29,180 --> 00:39:33,140 should test, things certainly we'll test for and Check 50 will test for. 876 00:39:33,140 --> 00:39:34,720 Any questions about spell.c? 877 00:39:34,720 --> 00:39:37,752 878 00:39:37,752 --> 00:39:39,310 >> OK, let's head back. 879 00:39:39,310 --> 00:39:42,420 I have one more program for you guys to write. 880 00:39:42,420 --> 00:39:44,700 It's called students.c. 881 00:39:44,700 --> 00:39:49,020 This is a program that asks the user for five names, then randomly chooses 882 00:39:49,020 --> 00:39:51,600 and prints out one of the names. 883 00:39:51,600 --> 00:39:54,840 This program is a little bit more involved than the last two we wrote, 884 00:39:54,840 --> 00:39:58,080 so why don't we take five minutes to code it individually, and then we'll 885 00:39:58,080 --> 00:40:00,130 code it as a group. 886 00:40:00,130 --> 00:40:03,795 >> This program also involve some things you may not have seen before, so if 887 00:40:03,795 --> 00:40:06,390 you have a question about, how do I do that, raise your hand. 888 00:40:06,390 --> 00:40:08,530 I'll come over and point you in the right direction. 889 00:40:08,530 --> 00:40:10,820 But we're also doing it because this will be a good way to introduce some 890 00:40:10,820 --> 00:40:12,070 topics that are tricky. 891 00:40:12,070 --> 00:44:14,600 892 00:44:14,600 --> 00:44:15,560 >> So it works, right? 893 00:44:15,560 --> 00:44:17,580 >> STUDENT: Yeah. 894 00:44:17,580 --> 00:44:19,180 >> JASON HIRSCHHORN: Whenever you turn in a problem set, there's many ways to do 895 00:44:19,180 --> 00:44:20,790 it, but some are more elegant than others. 896 00:44:20,790 --> 00:44:29,000 897 00:44:29,000 --> 00:44:31,110 Let us dive in. 898 00:44:31,110 --> 00:44:32,880 #include cs50.h. 899 00:44:32,880 --> 00:44:34,250 #include stdio.h. 900 00:44:34,250 --> 00:44:37,240 int main void, curly braces. 901 00:44:37,240 --> 00:44:39,430 Inside the curly braces, I have two comments. 902 00:44:39,430 --> 00:44:41,500 The first one is, ask the user for five names. 903 00:44:41,500 --> 00:44:46,580 The second one is, randomly choose and print out one of the names. 904 00:44:46,580 --> 00:44:50,250 So let's do the first one, ask the user for five names. 905 00:44:50,250 --> 00:44:53,730 Who has asked the user for five names and can tell me how to do that? 906 00:44:53,730 --> 00:44:56,760 907 00:44:56,760 --> 00:44:58,570 Only two people did the first part. 908 00:44:58,570 --> 00:45:00,150 Three people did the first part. 909 00:45:00,150 --> 00:45:00,670 Four, five. 910 00:45:00,670 --> 00:45:03,140 Tom, how do you do it? 911 00:45:03,140 --> 00:45:09,940 >> STUDENT: You would declare a string and then name, bracket five. 912 00:45:09,940 --> 00:45:12,070 >> JASON HIRSCHHORN: String name bracket five. 913 00:45:12,070 --> 00:45:15,350 So what is this line doing, string name bracket five? 914 00:45:15,350 --> 00:45:17,800 >> STUDENT: You would declare an array of strings. 915 00:45:17,800 --> 00:45:19,000 >> JASON HIRSCHHORN: This is an array of strings. 916 00:45:19,000 --> 00:45:21,590 How many strings are going to fit in here? 917 00:45:21,590 --> 00:45:22,010 >> STUDENT: Five. 918 00:45:22,010 --> 00:45:22,320 >> JASON HIRSCHHORN: Five. 919 00:45:22,320 --> 00:45:22,660 Excellent. 920 00:45:22,660 --> 00:45:26,360 We're going to call it Names because we're going to get multiple ones. 921 00:45:26,360 --> 00:45:27,790 OK, next? 922 00:45:27,790 --> 00:45:29,270 >> STUDENT: And then you have a for loop-- 923 00:45:29,270 --> 00:45:30,060 >> JASON HIRSCHHORN: For loop. 924 00:45:30,060 --> 00:45:33,035 >> STUDENT: --for i=0. 925 00:45:33,035 --> 00:45:37,040 >> JASON HIRSCHHORN: For (int i=0. 926 00:45:37,040 --> 00:45:38,270 >> STUDENT: Semicolon. 927 00:45:38,270 --> 00:45:38,880 >> JASON HIRSCHHORN: Semicolon. 928 00:45:38,880 --> 00:45:39,790 >> STUDENT: I less than five. 929 00:45:39,790 --> 00:45:41,310 >> JASON HIRSCHHORN: I less than five. 930 00:45:41,310 --> 00:45:42,230 >> STUDENT: Semicolon. 931 00:45:42,230 --> 00:45:42,560 >> JASON HIRSCHHORN: Semicolon. 932 00:45:42,560 --> 00:45:43,380 >> STUDENT: i++. 933 00:45:43,380 --> 00:45:46,050 >> JASON HIRSCHHORN: i++. 934 00:45:46,050 --> 00:45:47,100 >> STUDENT: And then closed parentheses. 935 00:45:47,100 --> 00:45:48,600 >> JASON HIRSCHHORN: Closed parentheses, and then some curly braces. 936 00:45:48,600 --> 00:45:50,050 Inside the curly braces? 937 00:45:50,050 --> 00:45:51,423 >> STUDENT: And then printf. 938 00:45:51,423 --> 00:45:52,535 >> JASON HIRSCHHORN: Printf. 939 00:45:52,535 --> 00:45:56,200 >> STUDENT: Open parentheses, name %d. 940 00:45:56,200 --> 00:45:57,090 >> JASON HIRSCHHORN: Sorry? 941 00:45:57,090 --> 00:45:57,660 >> STUDENT: Name. 942 00:45:57,660 --> 00:45:58,200 >> JASON HIRSCHHORN: Name. 943 00:45:58,200 --> 00:46:01,700 >> STUDENT: And then space, and then %d. 944 00:46:01,700 --> 00:46:02,950 >> JASON HIRSCHHORN: Name, space, %d. 945 00:46:02,950 --> 00:46:05,630 946 00:46:05,630 --> 00:46:10,820 >> STUDENT: And then /n, and then closed. 947 00:46:10,820 --> 00:46:15,070 >> JASON HIRSCHHORN: So name, space, %d/n, closed parentheses. 948 00:46:15,070 --> 00:46:17,730 >> STUDENT: Comma, i + 1. 949 00:46:17,730 --> 00:46:19,390 I'm sorry. 950 00:46:19,390 --> 00:46:21,040 Closed quote, and then comma, i +1. 951 00:46:21,040 --> 00:46:24,400 >> JASON HIRSCHHORN: Closed quote, comma, i+1. 952 00:46:24,400 --> 00:46:25,850 >> STUDENT: Yes. 953 00:46:25,850 --> 00:46:27,650 And then the next line-- 954 00:46:27,650 --> 00:46:29,400 >> JASON HIRSCHHORN: i + 1. 955 00:46:29,400 --> 00:46:31,600 >> STUDENT: Yes. 956 00:46:31,600 --> 00:46:36,440 And then the next slide is names bracket i. 957 00:46:36,440 --> 00:46:39,000 >> JASON HIRSCHHORN: Next line is names bracket i. 958 00:46:39,000 --> 00:46:41,360 >> STUDENT: = get string. 959 00:46:41,360 --> 00:46:43,460 >> JASON HIRSCHHORN: = Get-- 960 00:46:43,460 --> 00:46:44,510 capital G-E-T-- 961 00:46:44,510 --> 00:46:45,530 string. 962 00:46:45,530 --> 00:46:47,800 Oops, sorry. 963 00:46:47,800 --> 00:46:49,050 >> STUDENT: Open, closed parentheses and then-- 964 00:46:49,050 --> 00:46:50,940 >> JASON HIRSCHHORN: Open, closed paren. 965 00:46:50,940 --> 00:46:51,030 >> STUDENT: Semicolon. 966 00:46:51,030 --> 00:46:52,500 >> JASON HIRSCHHORN: Semicolon. 967 00:46:52,500 --> 00:46:57,545 OK, so you initialize the array of strings, names, and then go through a 968 00:46:57,545 --> 00:47:02,720 for loop, and five times print out some prompt to the user and then store 969 00:47:02,720 --> 00:47:03,360 five strings. 970 00:47:03,360 --> 00:47:08,090 Does anybody have any adjustments to this? 971 00:47:08,090 --> 00:47:10,030 Tom, I think everybody agrees with you. 972 00:47:10,030 --> 00:47:10,880 Looks pretty good. 973 00:47:10,880 --> 00:47:11,660 OK. 974 00:47:11,660 --> 00:47:14,830 Why did you do i+1, though? 975 00:47:14,830 --> 00:47:18,000 >> STUDENT: I mean, doesn't have to be, but i+1 so that it's 976 00:47:18,000 --> 00:47:19,700 name one to name five. 977 00:47:19,700 --> 00:47:21,930 Otherwise, it would be name zero to name four. 978 00:47:21,930 --> 00:47:24,400 >> JASON HIRSCHHORN: So you did i+1 because otherwise it would have 979 00:47:24,400 --> 00:47:27,060 printed out zero through four because that's how computers think, but humans 980 00:47:27,060 --> 00:47:28,530 don't really read like that. 981 00:47:28,530 --> 00:47:30,190 That is another thing you'll encounter. 982 00:47:30,190 --> 00:47:33,210 Sometimes when you print out something to the user, you'll want to make it a 983 00:47:33,210 --> 00:47:35,733 little bit prettier, and doing i+1 in this case is a 984 00:47:35,733 --> 00:47:37,430 way to make it prettier. 985 00:47:37,430 --> 00:47:41,380 >> I also see five twice, string name is five, i is less than five. 986 00:47:41,380 --> 00:47:42,430 I know where you got the five. 987 00:47:42,430 --> 00:47:44,890 You got it from me telling you five names. 988 00:47:44,890 --> 00:47:49,000 But if we wanted to make the code a little bit more robust, one thing we 989 00:47:49,000 --> 00:47:53,110 could do is out here, outside of int, we could do something like this, 990 00:47:53,110 --> 00:47:57,070 #define, and we could do NAMES 5. 991 00:47:57,070 --> 00:48:01,580 992 00:48:01,580 --> 00:48:05,390 >> So basically, wherever I write capital N, capital A, capital M, capital E, 993 00:48:05,390 --> 00:48:08,890 capital S, that string, it will replace that with 5 in the code. 994 00:48:08,890 --> 00:48:11,530 995 00:48:11,530 --> 00:48:13,445 Or let's just call it-- we'll just call it NUMBER. 996 00:48:13,445 --> 00:48:17,190 So I'm defining all caps NUMBER to be 5. 997 00:48:17,190 --> 00:48:22,830 >> So now wherever I have the number 5, I can replace it with all caps NUMBER. 998 00:48:22,830 --> 00:48:26,880 And now, if I want to change the number of names, all I need to do is 999 00:48:26,880 --> 00:48:31,350 this one line, line four, where I have #define number 5, change that to 10, 1000 00:48:31,350 --> 00:48:34,340 or change that to 4. 1001 00:48:34,340 --> 00:48:37,400 When your code gets longer, you're not going to remember all the places you 1002 00:48:37,400 --> 00:48:41,630 put how many names you wanted, and in order for you not to miss one, this is 1003 00:48:41,630 --> 00:48:45,490 a good way to do it, to find something up top and then just use that phrase 1004 00:48:45,490 --> 00:48:46,400 everywhere throughout your code. 1005 00:48:46,400 --> 00:48:46,780 Marcus? 1006 00:48:46,780 --> 00:48:49,780 >> STUDENT: Is that stylistically preferred than globally defining a 1007 00:48:49,780 --> 00:48:51,270 constant n? 1008 00:48:51,270 --> 00:48:53,760 >> JASON HIRSCHHORN: So is that stylistically preferred than globally 1009 00:48:53,760 --> 00:48:54,980 defining a constant n? 1010 00:48:54,980 --> 00:48:59,036 So you're saying you could do, int number = 5? 1011 00:48:59,036 --> 00:49:01,300 >> STUDENT: Do they have consonants in C or no? 1012 00:49:01,300 --> 00:49:02,720 >> JASON HIRSCHHORN: Or const-- in equal. 1013 00:49:02,720 --> 00:49:04,275 So you're saying just a global variable? 1014 00:49:04,275 --> 00:49:04,990 >> STUDENT: Yeah. 1015 00:49:04,990 --> 00:49:08,670 >> JASON HIRSCHHORN: So stylistically, there's no necessarily one right 1016 00:49:08,670 --> 00:49:12,050 answer, but both of those are better than just throwing these random 1017 00:49:12,050 --> 00:49:12,960 numbers throughout your code. 1018 00:49:12,960 --> 00:49:14,650 We call them magic numbers. 1019 00:49:14,650 --> 00:49:16,450 When I look at your code or when anybody's reading through it, they 1020 00:49:16,450 --> 00:49:18,790 don't want just these random numbers that don't make sense. 1021 00:49:18,790 --> 00:49:22,420 Even the number 26, which generally is the number of letters in the alphabet, 1022 00:49:22,420 --> 00:49:28,870 it's better to define ALPHABET up top and save the number 26 in that string, 1023 00:49:28,870 --> 00:49:29,550 if that makes sense. 1024 00:49:29,550 --> 00:49:33,210 >> Or on problem set one, rather than-- again, we didn't talk about this at 1025 00:49:33,210 --> 00:49:37,020 the time, but instead of just throwing in a 25 everywhere or a 10, you can 1026 00:49:37,020 --> 00:49:41,860 save the number 25 as QUARTER or 10 as DIME, and that makes it easier for 1027 00:49:41,860 --> 00:49:44,140 whoever's reading through your code to understand why you chose that number. 1028 00:49:44,140 --> 00:49:46,540 And also for you, if you ever want to update your code, it makes it a bit 1029 00:49:46,540 --> 00:49:47,290 more robust. 1030 00:49:47,290 --> 00:49:48,070 Yeah? 1031 00:49:48,070 --> 00:49:50,830 >> STUDENT: Is this code also going to be available for us after? 1032 00:49:50,830 --> 00:49:52,453 >> JASON HIRSCHHORN: All of these codes that we have written together are 1033 00:49:52,453 --> 00:49:54,620 going to be emailed to you guys and available. 1034 00:49:54,620 --> 00:49:55,740 Yes. 1035 00:49:55,740 --> 00:49:58,040 So ask the user for five names. 1036 00:49:58,040 --> 00:50:00,280 Randomly choose and print one of the names. 1037 00:50:00,280 --> 00:50:00,940 Who did this line? 1038 00:50:00,940 --> 00:50:02,230 This is the trickiest line here. 1039 00:50:02,230 --> 00:50:02,450 Sorry, Tom. 1040 00:50:02,450 --> 00:50:05,510 You did a really good job, but this is a bit trickier. 1041 00:50:05,510 --> 00:50:06,600 Marcus, go ahead. 1042 00:50:06,600 --> 00:50:10,180 >> STUDENT: Well first, you're going to need to include stdlib.h and time.h. 1043 00:50:10,180 --> 00:50:13,200 >> JASON HIRSCHHORN: So you're saying at the top, include two more libraries. 1044 00:50:13,200 --> 00:50:18,830 You want me to include S-T-D-L-I-B dot H, Standard Library, and what 1045 00:50:18,830 --> 00:50:19,760 was the other one? 1046 00:50:19,760 --> 00:50:21,010 >> STUDENT: Time.h. 1047 00:50:21,010 --> 00:50:25,130 1048 00:50:25,130 --> 00:50:27,840 >> JASON HIRSCHHORN: #include time.h. 1049 00:50:27,840 --> 00:50:29,470 So these are two more libraries. 1050 00:50:29,470 --> 00:50:31,790 I assume now we're going to be using functions that are contained in these 1051 00:50:31,790 --> 00:50:34,240 two, which is why you needed to include them? 1052 00:50:34,240 --> 00:50:36,980 >> STUDENT: So then, first you're going to want to write srand. 1053 00:50:36,980 --> 00:50:38,770 >> JASON HIRSCHHORN: Srand. 1054 00:50:38,770 --> 00:50:42,070 >> STUDENT: (time(0) 1055 00:50:42,070 --> 00:50:42,700 >> JASON HIRSCHHORN: Sorry. 1056 00:50:42,700 --> 00:50:45,580 Srand (time( 1057 00:50:45,580 --> 00:50:46,890 >> STUDENT: 0. 1058 00:50:46,890 --> 00:50:47,230 >> JASON HIRSCHHORN: 0. 1059 00:50:47,230 --> 00:50:49,990 >> STUDENT: )), semicolon. 1060 00:50:49,990 --> 00:50:51,320 >> JASON HIRSCHHORN: Another closed paren? 1061 00:50:51,320 --> 00:50:51,650 Semicolon. 1062 00:50:51,650 --> 00:50:53,610 I'm going to just replace your zero with NULL. 1063 00:50:53,610 --> 00:50:55,980 It'll do about the same thing, but generally, you'll see NULL, which 1064 00:50:55,980 --> 00:50:57,720 we'll talk about this line in a second. 1065 00:50:57,720 --> 00:50:58,580 >> STUDENT: Stylistically better? 1066 00:50:58,580 --> 00:51:02,240 >> JASON HIRSCHHORN: Generally, people right NULL, but same thing. 1067 00:51:02,240 --> 00:51:05,010 >> STUDENT: And then int name. 1068 00:51:05,010 --> 00:51:06,490 >> JASON HIRSCHHORN: Int name. 1069 00:51:06,490 --> 00:51:07,280 >> STUDENT: = 1070 00:51:07,280 --> 00:51:07,780 >> JASON HIRSCHHORN: = 1071 00:51:07,780 --> 00:51:08,580 >> STUDENT: Rand. 1072 00:51:08,580 --> 00:51:09,340 >> JASON HIRSCHHORN: Rand. 1073 00:51:09,340 --> 00:51:10,420 >> STUDENT: () 1074 00:51:10,420 --> 00:51:11,500 >> JASON HIRSCHHORN: () 1075 00:51:11,500 --> 00:51:12,850 >> STUDENT: Modulo number. 1076 00:51:12,850 --> 00:51:14,310 >> JASON HIRSCHHORN: What's modulo? 1077 00:51:14,310 --> 00:51:14,970 The character? 1078 00:51:14,970 --> 00:51:16,000 The percent character? 1079 00:51:16,000 --> 00:51:17,790 >> STUDENT: Percent character. 1080 00:51:17,790 --> 00:51:19,521 Modulo NUMBER that you defined up top. 1081 00:51:19,521 --> 00:51:22,030 >> JASON HIRSCHHORN: Modulo NUMBER. 1082 00:51:22,030 --> 00:51:24,052 >> STUDENT: And then semicolon. 1083 00:51:24,052 --> 00:51:24,660 >> JASON HIRSCHHORN: Semicolon. 1084 00:51:24,660 --> 00:51:27,820 >> STUDENT: And then new line, printf. 1085 00:51:27,820 --> 00:51:28,460 >> JASON HIRSCHHORN: Printf. 1086 00:51:28,460 --> 00:51:29,730 >> STUDENT: ( 1087 00:51:29,730 --> 00:51:30,620 >> JASON HIRSCHHORN: ( 1088 00:51:30,620 --> 00:51:31,650 >> STUDENT: " 1089 00:51:31,650 --> 00:51:32,630 >> JASON HIRSCHHORN: " 1090 00:51:32,630 --> 00:51:35,320 >> STUDENT: %s/n 1091 00:51:35,320 --> 00:51:37,220 >> JASON HIRSCHHORN: %s/n 1092 00:51:37,220 --> 00:51:38,420 >> STUDENT: " 1093 00:51:38,420 --> 00:51:40,640 >> JASON HIRSCHHORN: %s/n" 1094 00:51:40,640 --> 00:51:43,540 >> STUDENT: Comma, space names. 1095 00:51:43,540 --> 00:51:44,350 >> JASON HIRSCHHORN: Space-- 1096 00:51:44,350 --> 00:51:46,300 >> STUDENT: Oh, I guess, what did we call it here? 1097 00:51:46,300 --> 00:51:48,640 >> JASON HIRSCHHORN: We called it names. 1098 00:51:48,640 --> 00:51:52,360 >> STUDENT: Open bracket, name. 1099 00:51:52,360 --> 00:51:55,090 >> JASON HIRSCHHORN: Open bracket, name. 1100 00:51:55,090 --> 00:51:55,850 >> STUDENT: Closed bracket. 1101 00:51:55,850 --> 00:51:56,490 >> JASON HIRSCHHORN: Closed bracket. 1102 00:51:56,490 --> 00:51:57,460 >> STUDENT: ) 1103 00:51:57,460 --> 00:51:58,800 >> JASON HIRSCHHORN: ) 1104 00:51:58,800 --> 00:51:59,440 >> STUDENT: Semicolon. 1105 00:51:59,440 --> 00:52:00,410 >> JASON HIRSCHHORN: Semicolon. 1106 00:52:00,410 --> 00:52:01,500 OK, let's save. 1107 00:52:01,500 --> 00:52:04,410 Let's make this code. 1108 00:52:04,410 --> 00:52:06,365 Students. 1109 00:52:06,365 --> 00:52:07,100 Let's run this code. 1110 00:52:07,100 --> 00:52:08,480 Students. 1111 00:52:08,480 --> 00:52:11,630 Not ./student. ./students. 1112 00:52:11,630 --> 00:52:12,870 Name one. 1113 00:52:12,870 --> 00:52:15,490 We'll say David. 1114 00:52:15,490 --> 00:52:18,070 Name two, RJ. 1115 00:52:18,070 --> 00:52:20,561 Name three, Rob. 1116 00:52:20,561 --> 00:52:22,490 Name four, Lucas. 1117 00:52:22,490 --> 00:52:26,060 Name five, Joseph. 1118 00:52:26,060 --> 00:52:28,850 And then it prints out Joseph again. 1119 00:52:28,850 --> 00:52:31,730 >> So let me run this again. 1120 00:52:31,730 --> 00:52:32,460 Let me do the same names. 1121 00:52:32,460 --> 00:52:39,760 David, RJ, Rob, Lucas, Joseph. 1122 00:52:39,760 --> 00:52:41,070 And it gave me David this time. 1123 00:52:41,070 --> 00:52:43,030 So I did the same thing, but it gave me a different thing, and that's 1124 00:52:43,030 --> 00:52:45,410 because, of course, we're asking for a random name. 1125 00:52:45,410 --> 00:52:50,950 So I think, based off of me wandering around the room, that most people 1126 00:52:50,950 --> 00:52:55,960 probably got the first part right, setting up that array and then getting 1127 00:52:55,960 --> 00:52:56,430 all the names. 1128 00:52:56,430 --> 00:52:59,320 I'm talking about lines 10 through 16. 1129 00:52:59,320 --> 00:53:02,150 But lines 18 through 21 are probably some things you haven't seen before, 1130 00:53:02,150 --> 00:53:03,750 so let's go through those. 1131 00:53:03,750 --> 00:53:10,390 >> This line, srand time NULL is essentially-- 1132 00:53:10,390 --> 00:53:14,300 sorry, let's skip down a line, rand (). 1133 00:53:14,300 --> 00:53:17,320 That gives you a random number, a random integer. 1134 00:53:17,320 --> 00:53:19,630 And if you hit man rand, which a lot of you did-- 1135 00:53:19,630 --> 00:53:21,530 M-A-N space R-A-N-D-- 1136 00:53:21,530 --> 00:53:25,910 in the terminal like this, M-A-N space R-A-N-D, hit Enter. 1137 00:53:25,910 --> 00:53:30,490 You would come up to the manual and would be able to read that the rand 1138 00:53:30,490 --> 00:53:34,260 command outputs num pseudo random bytes after seeding the random number 1139 00:53:34,260 --> 00:53:35,510 generator once. 1140 00:53:35,510 --> 00:53:38,450 1141 00:53:38,450 --> 00:53:39,700 >> So that's a description. 1142 00:53:39,700 --> 00:53:43,860 1143 00:53:43,860 --> 00:53:46,800 That is what rand does, gives you a random number when 1144 00:53:46,800 --> 00:53:48,610 you call that function. 1145 00:53:48,610 --> 00:53:53,140 Unfortunately, it's not really random because if you just call random 1146 00:53:53,140 --> 00:53:55,970 without getting it some seed, some starting point, it will always choose 1147 00:53:55,970 --> 00:53:59,100 the same one and it uses a formula to determine the next one. 1148 00:53:59,100 --> 00:54:01,730 So if you call rand, it'll just give you the same thing. 1149 00:54:01,730 --> 00:54:02,610 It will be very predictable. 1150 00:54:02,610 --> 00:54:03,790 It actually won't be random. 1151 00:54:03,790 --> 00:54:09,610 >> So what we need to do, per Marcus's instructions to me, were seed rand 1152 00:54:09,610 --> 00:54:13,810 with something else, which is what the srand function does. 1153 00:54:13,810 --> 00:54:18,055 It seeds rand with something a bit more random than the same thing, and 1154 00:54:18,055 --> 00:54:23,360 he's seeding it with time NULL, which is, just by convention, the current 1155 00:54:23,360 --> 00:54:24,650 time in seconds. 1156 00:54:24,650 --> 00:54:29,790 >> So you're assuming that everybody's not going to run the code at the exact 1157 00:54:29,790 --> 00:54:33,620 same second, so it's a safe bet that time NULL is going to be different 1158 00:54:33,620 --> 00:54:35,250 every time this code is run. 1159 00:54:35,250 --> 00:54:39,020 Safe bet that thus, srand will seed rand with something different every 1160 00:54:39,020 --> 00:54:39,990 single time. 1161 00:54:39,990 --> 00:54:44,110 So by convention, when we do want a random number, we write this line, 1162 00:54:44,110 --> 00:54:50,540 srand time NULL, to give us a much more random seed, and then we run 1163 00:54:50,540 --> 00:54:52,080 rand, which gives us a random number. 1164 00:54:52,080 --> 00:54:52,840 Yeah? 1165 00:54:52,840 --> 00:54:54,140 >> STUDENT: Can you say what you mean by seed? 1166 00:54:54,140 --> 00:54:55,630 >> JASON HIRSCHHORN: Seed as in-- 1167 00:54:55,630 --> 00:54:58,730 so rand is going to pick some number and it's going to use some 1168 00:54:58,730 --> 00:55:00,060 formula to get there. 1169 00:55:00,060 --> 00:55:03,250 So it's going to start with something and then turn it into something based 1170 00:55:03,250 --> 00:55:03,890 off of that formula. 1171 00:55:03,890 --> 00:55:06,090 If you always give it the same thing, it's always going to turn it into the 1172 00:55:06,090 --> 00:55:06,790 same thing. 1173 00:55:06,790 --> 00:55:08,450 So that first thing you give it is the seed. 1174 00:55:08,450 --> 00:55:10,880 So you want to give it something different to start out with, and so it 1175 00:55:10,880 --> 00:55:12,610 will turn it into something different. 1176 00:55:12,610 --> 00:55:15,840 >> And then finally, line 20, this % is called the 1177 00:55:15,840 --> 00:55:17,470 modulo, and that is confusing. 1178 00:55:17,470 --> 00:55:19,850 That's something that is a little bit different here that you don't really 1179 00:55:19,850 --> 00:55:22,240 have in real life math. 1180 00:55:22,240 --> 00:55:27,430 But essentially, when you take a giant number and you divide in C, what 1181 00:55:27,430 --> 00:55:29,482 happens to the remainder? 1182 00:55:29,482 --> 00:55:30,510 >> STUDENT: It gets truncated. 1183 00:55:30,510 --> 00:55:31,220 >> JASON HIRSCHHORN: It gets truncated. 1184 00:55:31,220 --> 00:55:35,200 So 20 divided by 3 is what? 1185 00:55:35,200 --> 00:55:37,950 What does that return to you, 20 divided by 3. 1186 00:55:37,950 --> 00:55:39,260 Sorry, 10 divided by 3. 1187 00:55:39,260 --> 00:55:41,062 What does that return to you? 1188 00:55:41,062 --> 00:55:41,540 >> STUDENT: [INAUDIBLE]. 1189 00:55:41,540 --> 00:55:45,170 >> JASON HIRSCHHORN: 10 divided by 3 just returns 3, not 3 remainder 1. 1190 00:55:45,170 --> 00:55:51,490 So what 10 modulo 3 returns is just the remainder, 1. 1191 00:55:51,490 --> 00:55:54,020 So if you wanted to know what 10 divided by 3 actually was, you would 1192 00:55:54,020 --> 00:55:58,950 do 10 divided by 3, you'd get your 3, and then you'd do 10 modulo 3, and 1193 00:55:58,950 --> 00:55:59,860 you'd get a 1. 1194 00:55:59,860 --> 00:56:04,630 So you'd realize that 10 divided by 3 is 3, remainder 1. 1195 00:56:04,630 --> 00:56:07,990 >> And in this line, rand could be any integer, essentially. 1196 00:56:07,990 --> 00:56:12,660 So modulo is saying, divide it by number, which is in this case 5. 1197 00:56:12,660 --> 00:56:16,420 And if you ever divide by a number, the integer always has to be less than 1198 00:56:16,420 --> 00:56:17,590 that number. 1199 00:56:17,590 --> 00:56:22,200 So if you're dividing by a number 5, the remainder is always going to be 1200 00:56:22,200 --> 00:56:23,396 zero through four. 1201 00:56:23,396 --> 00:56:25,520 That's just how the math works out. 1202 00:56:25,520 --> 00:56:29,700 >> So a large number moduloed by a smaller number will always return 1203 00:56:29,700 --> 00:56:33,530 remainder and will always return some number that's less than the number you 1204 00:56:33,530 --> 00:56:34,693 divided by. 1205 00:56:34,693 --> 00:56:37,920 Does anybody have any questions about modulo, because it will come in very, 1206 00:56:37,920 --> 00:56:41,450 very handy, so I want to make sure everybody understands it? 1207 00:56:41,450 --> 00:56:43,710 Cool. 1208 00:56:43,710 --> 00:56:46,000 And we saw that this program worked. 1209 00:56:46,000 --> 00:56:50,280 OK, moving on. 1210 00:56:50,280 --> 00:56:52,970 >> So next, we're going to go into functions, and we're going to go 1211 00:56:52,970 --> 00:56:55,580 through the rest of this stuff pretty quickly because I want to get to 1212 00:56:55,580 --> 00:56:57,150 command line arguments. 1213 00:56:57,150 --> 00:57:00,330 So functions are essentially, as you've seen, black boxes. 1214 00:57:00,330 --> 00:57:04,290 They take things in called parameters, they do something to them, either 1215 00:57:04,290 --> 00:57:06,990 change them or create some side effects, and finally, they spit 1216 00:57:06,990 --> 00:57:09,530 something out, which is the return value. 1217 00:57:09,530 --> 00:57:12,495 Can anybody give me one reason why we would use functions? 1218 00:57:12,495 --> 00:57:15,870 1219 00:57:15,870 --> 00:57:16,360 Anybody? 1220 00:57:16,360 --> 00:57:19,360 One reason why we'd use functions? 1221 00:57:19,360 --> 00:57:21,280 Anyone? 1222 00:57:21,280 --> 00:57:22,700 I saw two hands. 1223 00:57:22,700 --> 00:57:23,590 What's a good reason? 1224 00:57:23,590 --> 00:57:24,930 Marcus? 1225 00:57:24,930 --> 00:57:27,730 >> STUDENT: If you have to call the same chunk of code multiple times. 1226 00:57:27,730 --> 00:57:29,150 >> JASON HIRSCHHORN: OK, if you need to call the same chunk of 1227 00:57:29,150 --> 00:57:30,040 code multiple times. 1228 00:57:30,040 --> 00:57:32,470 Jeff, did you have a different reason? 1229 00:57:32,470 --> 00:57:34,230 >> STUDENT: Just so that you can code less. 1230 00:57:34,230 --> 00:57:35,070 >> JASON HIRSCHHORN: You can code less. 1231 00:57:35,070 --> 00:57:35,320 Right. 1232 00:57:35,320 --> 00:57:38,720 If you want to do something a lot-- 1233 00:57:38,720 --> 00:57:41,720 in the previous code, I could have written get string five times, but a 1234 00:57:41,720 --> 00:57:44,620 for loop makes it look nicer and you have to write less code. 1235 00:57:44,620 --> 00:57:45,760 Functions are a similar idea. 1236 00:57:45,760 --> 00:57:48,720 If I want to do the same thing a lot, move it to a function and call that 1237 00:57:48,720 --> 00:57:49,610 function each time. 1238 00:57:49,610 --> 00:57:51,190 That will do that thing for you. 1239 00:57:51,190 --> 00:57:53,740 That's a great reason to use functions. 1240 00:57:53,740 --> 00:57:54,870 >> So anatomy of a function. 1241 00:57:54,870 --> 00:57:56,670 A function will generally look like this. 1242 00:57:56,670 --> 00:57:59,280 It'll start with the return type, then you'll get the name. 1243 00:57:59,280 --> 00:58:02,935 It'll take the arguments, otherwise known as the parameters, and inside 1244 00:58:02,935 --> 00:58:05,290 those curly braces, you'll write your code. 1245 00:58:05,290 --> 00:58:07,150 >> We've seen one function a lot. 1246 00:58:07,150 --> 00:58:09,160 That's main. 1247 00:58:09,160 --> 00:58:13,820 Here is main superimposed onto that anatomy I had earlier. 1248 00:58:13,820 --> 00:58:15,525 What does int right here signify? 1249 00:58:15,525 --> 00:58:18,534 1250 00:58:18,534 --> 00:58:19,450 What does int mean? 1251 00:58:19,450 --> 00:58:21,026 When I write int main (void). 1252 00:58:21,026 --> 00:58:21,740 >> STUDENT: Return type? 1253 00:58:21,740 --> 00:58:22,350 >> JASON HIRSCHHORN: Sorry? 1254 00:58:22,350 --> 00:58:23,170 >> STUDENT: The return type. 1255 00:58:23,170 --> 00:58:24,820 >> JASON HIRSCHHORN: Int is the return type, exactly right. 1256 00:58:24,820 --> 00:58:25,870 Main returns an int. 1257 00:58:25,870 --> 00:58:28,678 What is main? 1258 00:58:28,678 --> 00:58:29,510 >> STUDENT: Function name. 1259 00:58:29,510 --> 00:58:30,800 >> JASON HIRSCHHORN: Main is the name of the function. 1260 00:58:30,800 --> 00:58:31,510 Exactly right. 1261 00:58:31,510 --> 00:58:31,840 And void? 1262 00:58:31,840 --> 00:58:33,190 What is void inside the parentheses? 1263 00:58:33,190 --> 00:58:36,650 1264 00:58:36,650 --> 00:58:42,030 >> STUDENT: It doesn't take any external value. 1265 00:58:42,030 --> 00:58:42,930 >> JASON HIRSCHHORN: So void-- 1266 00:58:42,930 --> 00:58:43,240 right. 1267 00:58:43,240 --> 00:58:45,940 Inside the parentheses, we put the parameters or the arguments the 1268 00:58:45,940 --> 00:58:48,480 function takes, what goes inside the black box. 1269 00:58:48,480 --> 00:58:50,760 When you write void, you're saying this takes nothing in. 1270 00:58:50,760 --> 00:58:53,802 So int main void means main's not taking anything in. 1271 00:58:53,802 --> 00:58:56,950 >> Inside the curly braces, we have some code, and then we see at the end 1272 00:58:56,950 --> 00:58:57,640 return zero. 1273 00:58:57,640 --> 00:59:01,220 What does return zero mean? 1274 00:59:01,220 --> 00:59:02,470 Well, we talked about return type. 1275 00:59:02,470 --> 00:59:03,780 Main needs to return an int. 1276 00:59:03,780 --> 00:59:04,960 That's what this line does. 1277 00:59:04,960 --> 00:59:07,330 It returns an int, zero. 1278 00:59:07,330 --> 00:59:13,600 >> Of course, we haven't seen that a lot because in the programming we do with 1279 00:59:13,600 --> 00:59:16,070 the machines we do and the compilers we use nowadays, you 1280 00:59:16,070 --> 00:59:16,990 don't need that line. 1281 00:59:16,990 --> 00:59:20,070 Main is special enough that it knows when you reach the end of main, just 1282 00:59:20,070 --> 00:59:22,060 return zero if everything went well. 1283 00:59:22,060 --> 00:59:25,560 In fact, if something doesn't go well, main will often return a different 1284 00:59:25,560 --> 00:59:28,500 number signifying that something went wrong with your program. 1285 00:59:28,500 --> 00:59:32,470 >> So generally when you see code, you don't write return zero, but it's good 1286 00:59:32,470 --> 00:59:34,770 to know that it's happening behind the scenes because whenever you write 1287 00:59:34,770 --> 00:59:36,370 another type of function, you are going to need to 1288 00:59:36,370 --> 00:59:37,400 include a return line. 1289 00:59:37,400 --> 00:59:39,710 If you're saying I'm going to return an int, you have to 1290 00:59:39,710 --> 00:59:41,250 write return something. 1291 00:59:41,250 --> 00:59:45,160 If you say void up here at the beginning, void main void, for 1292 00:59:45,160 --> 00:59:48,670 example, then it's not returning anything-- void means nothing-- 1293 00:59:48,670 --> 00:59:50,320 so you don't need that return line. 1294 00:59:50,320 --> 00:59:53,610 But you ever write something that's not void for the return type, you need 1295 00:59:53,610 --> 00:59:55,950 a line that says return and you need to put something there 1296 00:59:55,950 --> 00:59:57,650 that matches the type. 1297 00:59:57,650 --> 00:59:58,900 Any questions about that? 1298 00:59:58,900 --> 01:00:03,110 1299 01:00:03,110 --> 01:00:04,680 Sweet. 1300 01:00:04,680 --> 01:00:05,930 >> Scope. 1301 01:00:05,930 --> 01:00:07,360 This is something we've touched on as well. 1302 01:00:07,360 --> 01:00:10,720 Every variable, as we know, has a certain scope, and that's basically 1303 01:00:10,720 --> 01:00:14,160 talking about where the variable may or may not be referenced. 1304 01:00:14,160 --> 01:00:16,310 And I like to say what happens in curly braces stays 1305 01:00:16,310 --> 01:00:18,000 inside the curly braces. 1306 01:00:18,000 --> 01:00:21,490 So for example, if we look at this code, we're defining a global variable 1307 01:00:21,490 --> 01:00:24,510 outside of main, int a, up here at the top. 1308 01:00:24,510 --> 01:00:27,380 We're calling another int a inside main. 1309 01:00:27,380 --> 01:00:29,760 We have some more curly braces, and we're calling another int a. 1310 01:00:29,760 --> 01:00:31,690 >> We have three a's in code. 1311 01:00:31,690 --> 01:00:32,550 These aren't the same thing. 1312 01:00:32,550 --> 01:00:34,880 I've created three distinct variables. 1313 01:00:34,880 --> 01:00:39,370 Then inside these inner curly braces, I say a = 4. 1314 01:00:39,370 --> 01:00:44,130 Which a is that talking about, the first one, the second one, 1315 01:00:44,130 --> 01:00:47,045 or the third one? 1316 01:00:47,045 --> 01:00:47,920 >> STUDENT: The third one. 1317 01:00:47,920 --> 01:00:49,320 >> JASON HIRSCHHORN: It's talking about the third one. 1318 01:00:49,320 --> 01:00:52,850 All of those a's actually can be talked about in this particular 1319 01:00:52,850 --> 01:00:58,660 instance because their scope all goes into these innermost curly braces. 1320 01:00:58,660 --> 01:01:02,360 However, when you call things the same name, there's something called hiding. 1321 01:01:02,360 --> 01:01:09,670 So you only deal with or fix or use the most recent variable, or the one 1322 01:01:09,670 --> 01:01:11,090 closest to you. 1323 01:01:11,090 --> 01:01:14,760 So inside here, this third a is closest to that line, a = 4, so it 1324 01:01:14,760 --> 01:01:16,550 affects the third one. 1325 01:01:16,550 --> 01:01:19,890 And the last line, a = 2, which a is that talking about, the first, second, 1326 01:01:19,890 --> 01:01:20,805 or third a? 1327 01:01:20,805 --> 01:01:22,530 >> STUDENT: Second. 1328 01:01:22,530 --> 01:01:24,430 >> JASON HIRSCHHORN: It's talking about the second a. 1329 01:01:24,430 --> 01:01:27,970 Of course, the third a is closest to it in the code, but the third a 1330 01:01:27,970 --> 01:01:30,200 doesn't exist outside those curly braces. 1331 01:01:30,200 --> 01:01:32,650 Once you get to that closed curly brace, it's like it's gone, 1332 01:01:32,650 --> 01:01:33,570 essentially. 1333 01:01:33,570 --> 01:01:36,150 So the only two you have left are those first two, and of course, now 1334 01:01:36,150 --> 01:01:37,960 it's closest to the second one. 1335 01:01:37,960 --> 01:01:39,410 >> So that's a good thing to remember about scope. 1336 01:01:39,410 --> 01:01:42,900 For example, I'm sure you've encountered this in a do while loop. 1337 01:01:42,900 --> 01:01:46,680 If you initialize the variable inside the do while loop and then you try to 1338 01:01:46,680 --> 01:01:50,360 access it outside, you probably got some warning like, this 1339 01:01:50,360 --> 01:01:51,680 variable does not exist. 1340 01:01:51,680 --> 01:01:54,370 That's because of the scope idea, that things only exist 1341 01:01:54,370 --> 01:01:55,320 inside those curly braces. 1342 01:01:55,320 --> 01:01:59,320 Curly braces are like a giant hug that you're giving to your code, is another 1343 01:01:59,320 --> 01:02:01,380 way to think about it. 1344 01:02:01,380 --> 01:02:05,280 >> So this is another function, something for you guys to code, hellofunction.c. 1345 01:02:05,280 --> 01:02:08,240 We're going to skip that for now, but this is something I encourage you to 1346 01:02:08,240 --> 01:02:09,740 practice on your own. 1347 01:02:09,740 --> 01:02:12,510 Write a program in which main calls another function that prints out a 1348 01:02:12,510 --> 01:02:13,750 greeting to the user. 1349 01:02:13,750 --> 01:02:17,750 It's essentially taking hello world and abstracting that printf into 1350 01:02:17,750 --> 01:02:18,680 another function. 1351 01:02:18,680 --> 01:02:22,360 But we're not going to do that right now because we don't have as much time 1352 01:02:22,360 --> 01:02:23,350 left as I would have liked. 1353 01:02:23,350 --> 01:02:26,250 >> So function declaration. 1354 01:02:26,250 --> 01:02:28,080 We've seen this before as well. 1355 01:02:28,080 --> 01:02:31,180 If you want to write another function, you need to let the computer know and 1356 01:02:31,180 --> 01:02:32,600 main know that it exists. 1357 01:02:32,600 --> 01:02:37,140 So what we do is above main, we write the return type, the name, and the 1358 01:02:37,140 --> 01:02:42,970 parameters, and then down below main, down here, we actually copy that line 1359 01:02:42,970 --> 01:02:45,080 and then write the actual code. 1360 01:02:45,080 --> 01:02:50,990 So this way, if you call the function in main at the top, main knows that 1361 01:02:50,990 --> 01:02:54,650 hello exists, for example, if I were to call hello, and then it looks to 1362 01:02:54,650 --> 01:02:56,710 the implementation down below. 1363 01:02:56,710 --> 01:02:59,680 What happens if I don't include this line up top, void hello (void)? 1364 01:02:59,680 --> 01:03:03,820 1365 01:03:03,820 --> 01:03:06,560 >> If I don't include this line and main calls hello, I'm 1366 01:03:06,560 --> 01:03:07,640 going to get an error. 1367 01:03:07,640 --> 01:03:10,090 I'm going to get an error because the compiler doesn't know 1368 01:03:10,090 --> 01:03:13,070 that this thing exists. 1369 01:03:13,070 --> 01:03:16,680 This is similar to why we do #include cs50.h. 1370 01:03:16,680 --> 01:03:21,390 What essentially cs50.h is doing is putting get string up there. 1371 01:03:21,390 --> 01:03:23,120 It's putting get int up there. 1372 01:03:23,120 --> 01:03:26,570 It's putting all of those function prototypes up before main so that when 1373 01:03:26,570 --> 01:03:29,900 you call them in main, main knows that these things exist. 1374 01:03:29,900 --> 01:03:31,630 That's what those .h files do. 1375 01:03:31,630 --> 01:03:33,850 And so if you're going to make your own function, you've got to make sure 1376 01:03:33,850 --> 01:03:36,690 you put that prototype up there. 1377 01:03:36,690 --> 01:03:38,380 Any questions about this so far? 1378 01:03:38,380 --> 01:03:41,630 1379 01:03:41,630 --> 01:03:42,490 >> Command line arguments. 1380 01:03:42,490 --> 01:03:44,780 This is what we're going to finish on, and then hopefully we'll 1381 01:03:44,780 --> 01:03:46,180 squeeze in some pset. 1382 01:03:46,180 --> 01:03:48,700 I don't know if we'll be able to squeeze that in because of time, but 1383 01:03:48,700 --> 01:03:51,560 with you guys who are here, and if you guys want to say after for a bit, we 1384 01:03:51,560 --> 01:03:54,290 can go over it outside. 1385 01:03:54,290 --> 01:03:56,020 But we're going to finish with command line arguments 1386 01:03:56,020 --> 01:03:58,510 for the last 15 minutes. 1387 01:03:58,510 --> 01:03:59,930 We awake, Michael? 1388 01:03:59,930 --> 01:04:01,180 You're awake? 1389 01:04:01,180 --> 01:04:01,660 Good. 1390 01:04:01,660 --> 01:04:03,760 I don't know if you sleep with your eyes closed. 1391 01:04:03,760 --> 01:04:05,800 It would be tricky. 1392 01:04:05,800 --> 01:04:07,180 >> Command line arguments. 1393 01:04:07,180 --> 01:04:10,430 Those are one way to pass information into a program. 1394 01:04:10,430 --> 01:04:12,780 Eventually, we'll encounter others. 1395 01:04:12,780 --> 01:04:15,600 But essentially, we're going to stop writing int main (void) if we want to 1396 01:04:15,600 --> 01:04:16,720 take some command line arguments. 1397 01:04:16,720 --> 01:04:19,850 We're going to write this thing called int main (int argc, 1398 01:04:19,850 --> 01:04:22,430 string argv, brackets. 1399 01:04:22,430 --> 01:04:23,710 That is confusing. 1400 01:04:23,710 --> 01:04:26,070 That's a lot of letters that aren't in human English, so we're going to 1401 01:04:26,070 --> 01:04:27,680 unpack them right now. 1402 01:04:27,680 --> 01:04:30,450 >> Argc stands for argument count-- 1403 01:04:30,450 --> 01:04:32,600 and again, all of these will be available online-- 1404 01:04:32,600 --> 01:04:34,280 and that's the number of arguments. 1405 01:04:34,280 --> 01:04:37,940 Argv stands for argument vector, and those are the arguments themselves. 1406 01:04:37,940 --> 01:04:38,830 It's an array of strings. 1407 01:04:38,830 --> 01:04:40,880 We've actually seen an array of strings before today. 1408 01:04:40,880 --> 01:04:44,390 We created one in the last program we coded. 1409 01:04:44,390 --> 01:04:45,720 >> So here's an example. 1410 01:04:45,720 --> 01:04:50,230 I write ./ohai cs50 section on my command line. 1411 01:04:50,230 --> 01:04:54,090 Argc in this case equals 3. 1412 01:04:54,090 --> 01:05:01,170 Argv bracket 0 is ohai, argv bracket 1 is cs50, argv bracket 2 is section. 1413 01:05:01,170 --> 01:05:05,160 Again, argc is the number of arguments, and to trip you up a little 1414 01:05:05,160 --> 01:05:08,710 bit, we always count the name of the function as the first argument. 1415 01:05:08,710 --> 01:05:12,350 So in this case, there are three arguments, the name of the function 1416 01:05:12,350 --> 01:05:14,590 and then those two that I typed in. 1417 01:05:14,590 --> 01:05:18,900 >> Next, we're going to save those arguments themselves in argv. 1418 01:05:18,900 --> 01:05:22,690 And again, just as we saw before, we did this in the last program we coded. 1419 01:05:22,690 --> 01:05:27,110 You can move through the array of argv and get those strings. 1420 01:05:27,110 --> 01:05:28,830 That's essentially what we did at the end of our last program. 1421 01:05:28,830 --> 01:05:33,190 We picked a random name and then went to names bracket whatever and printed 1422 01:05:33,190 --> 01:05:34,580 out the string located there. 1423 01:05:34,580 --> 01:05:39,870 1424 01:05:39,870 --> 01:05:43,670 >> On top of just the one array, because strings themselves are arrays, as 1425 01:05:43,670 --> 01:05:46,320 talked about, you can have arrays of arrays, which is 1426 01:05:46,320 --> 01:05:48,530 essentially what argv is. 1427 01:05:48,530 --> 01:05:51,370 One way you can think about this is rows and columns. 1428 01:05:51,370 --> 01:05:54,750 I can draw that right now. 1429 01:05:54,750 --> 01:05:57,370 So argv-- 1430 01:05:57,370 --> 01:05:58,630 that's not erasing. 1431 01:05:58,630 --> 01:05:59,880 Let's draw under ohai. 1432 01:05:59,880 --> 01:06:09,320 1433 01:06:09,320 --> 01:06:12,490 >> So if I wrote in, ohai cs50 section, I'm going to truncate 1434 01:06:12,490 --> 01:06:14,520 section for this example. 1435 01:06:14,520 --> 01:06:29,026 But essentially, my multi-dimensional array, ohai cs50, 1436 01:06:29,026 --> 01:06:32,200 we're going to do sect. 1437 01:06:32,200 --> 01:06:34,610 So here, I can access-- 1438 01:06:34,610 --> 01:06:40,930 1439 01:06:40,930 --> 01:06:45,710 if I just give argv one number-- 1440 01:06:45,710 --> 01:06:46,390 this is wrong. 1441 01:06:46,390 --> 01:06:49,020 What's wrong in this diagram? 1442 01:06:49,020 --> 01:06:49,690 >> STUDENT: Start at zero. 1443 01:06:49,690 --> 01:06:50,350 >> JASON HIRSCHHORN: It should start with zero. 1444 01:06:50,350 --> 01:06:52,940 I should probably also rotate it, but since I've already drawn it on, I'm 1445 01:06:52,940 --> 01:06:54,120 going to leave it on there for now. 1446 01:06:54,120 --> 01:06:55,160 But it starts at zero. 1447 01:06:55,160 --> 01:06:56,410 That's right. 1448 01:06:56,410 --> 01:06:58,980 1449 01:06:58,980 --> 01:07:00,120 I'm sorry if this is small. 1450 01:07:00,120 --> 01:07:01,370 Can you guys kind of see? 1451 01:07:01,370 --> 01:07:03,620 1452 01:07:03,620 --> 01:07:06,670 So if I do argv 0, I'll get all of ohai. 1453 01:07:06,670 --> 01:07:08,555 If I do argv 1, I'll get all of cs50. 1454 01:07:08,555 --> 01:07:10,720 If I do argv 2, I'll get all of sect. 1455 01:07:10,720 --> 01:07:13,370 >> But now, as we said, strings are arrays themselves. 1456 01:07:13,370 --> 01:07:19,740 So what I can do is I can treat this as a multi-dimensional array, and I 1457 01:07:19,740 --> 01:07:24,190 can do something along the lines of argv 0, 0, which gives me just the 1458 01:07:24,190 --> 01:07:25,400 character 0. 1459 01:07:25,400 --> 01:07:28,760 Argv 0, 1, which gives me just h. 1460 01:07:28,760 --> 01:07:33,960 Argv 1, 3, which gives me just this 0 down here in cs50. 1461 01:07:33,960 --> 01:07:37,690 >> So because strings themselves are arrays, we're putting 1462 01:07:37,690 --> 01:07:38,540 that in another array. 1463 01:07:38,540 --> 01:07:42,850 We now have array of arrays, and we can index into our first array and 1464 01:07:42,850 --> 01:07:44,840 then index into our second array. 1465 01:07:44,840 --> 01:07:48,650 And as you would expect, we do that by just putting those two-- 1466 01:07:48,650 --> 01:07:51,760 we do the square brackets just right next to one another. 1467 01:07:51,760 --> 01:07:58,930 So in this example, argv 1 is cs50 and argv 1, 2, like we said, is just 5. 1468 01:07:58,930 --> 01:08:02,120 Does anybody have any questions about multi-dimensional arrays? 1469 01:08:02,120 --> 01:08:05,090 1470 01:08:05,090 --> 01:08:06,380 You guys are doing awesome. 1471 01:08:06,380 --> 01:08:07,750 Sweet. 1472 01:08:07,750 --> 01:08:11,980 >> So we have another program right here, and this is the final thing we're 1473 01:08:11,980 --> 01:08:13,690 going to do right now. 1474 01:08:13,690 --> 01:08:18,160 So I want you to write a program that takes a user's full name, two and only 1475 01:08:18,160 --> 01:08:21,649 two command line arguments, and finally, print out a greeting to the 1476 01:08:21,649 --> 01:08:24,180 user that includes their first name. 1477 01:08:24,180 --> 01:08:27,800 So why don't you take three or four minutes to do this, and then we'll go 1478 01:08:27,800 --> 01:08:29,870 over it as a group, and then we'll wrap up? 1479 01:08:29,870 --> 01:09:44,189 1480 01:09:44,189 --> 01:09:48,084 >> And again, here, when we say take a user's full name per the description 1481 01:09:48,084 --> 01:09:54,473 of this problem, it should be command line arguments, not using get string. 1482 01:09:54,473 --> 01:09:55,450 Yeah, Marcus? 1483 01:09:55,450 --> 01:09:58,260 >> STUDENT: So when you say two command line arguments, is that in addition to 1484 01:09:58,260 --> 01:10:00,000 the ./run, or-- 1485 01:10:00,000 --> 01:10:02,980 >> JASON HIRSCHHORN: Good question. 1486 01:10:02,980 --> 01:10:05,995 Do, including the ./run, three command line arguments. 1487 01:10:05,995 --> 01:10:08,550 1488 01:10:08,550 --> 01:10:11,320 Or including the ./personalized, in this case, because that's the 1489 01:10:11,320 --> 01:10:12,360 name of this file. 1490 01:10:12,360 --> 01:10:13,300 So argc would be three. 1491 01:10:13,300 --> 01:10:19,480 But I want only two arguments given to the program. 1492 01:10:19,480 --> 01:10:20,400 I should specify that. 1493 01:10:20,400 --> 01:10:23,380 I'll make that change before I post these and send them to you guys. 1494 01:10:23,380 --> 01:10:32,747 1495 01:10:32,747 --> 01:10:34,740 >> STUDENT: What if you have a middle name? 1496 01:10:34,740 --> 01:10:36,800 >> JASON HIRSCHHORN: If you have a middle name, you're out of luck. 1497 01:10:36,800 --> 01:10:38,050 I'm sorry. 1498 01:10:38,050 --> 01:10:40,320 1499 01:10:40,320 --> 01:10:43,744 Or if you only go by Shakira, you're also out of luck. 1500 01:10:43,744 --> 01:10:55,672 1501 01:10:55,672 --> 01:10:56,425 Yeah? 1502 01:10:56,425 --> 01:11:00,760 >> STUDENT: So when people don't have cs50.h, do they define argv 1503 01:11:00,760 --> 01:11:03,450 as char star argv? 1504 01:11:03,450 --> 01:11:05,160 Or is it still called string argv? 1505 01:11:05,160 --> 01:11:07,430 >> JASON HIRSCHHORN: Yes. 1506 01:11:07,430 --> 01:11:10,480 So string, again, we mask over what exactly is happening there. 1507 01:11:10,480 --> 01:11:14,040 That's going to be unpacked maybe on Wednesday, but soon. 1508 01:11:14,040 --> 01:11:16,380 It's like training wheels. 1509 01:11:16,380 --> 01:11:18,370 Because essentially, it's the same thing. 1510 01:11:18,370 --> 01:11:37,040 1511 01:11:37,040 --> 01:11:42,560 >> So let's take another minute to program this, and then we will go over 1512 01:11:42,560 --> 01:11:45,130 it as a group, and then like I said, wrap up. 1513 01:11:45,130 --> 01:11:47,770 And I think they're still extra candy, so you guys should hopefully take 1514 01:11:47,770 --> 01:11:49,790 that, or please take that. 1515 01:11:49,790 --> 01:12:20,380 1516 01:12:20,380 --> 01:12:22,560 >> Sorry if you're still writing, but I want to dive in and we 1517 01:12:22,560 --> 01:12:24,010 can finish it together. 1518 01:12:24,010 --> 01:12:30,470 So I have written in, #include cs50.h, #include stdio.h. 1519 01:12:30,470 --> 01:12:33,970 int main (void), curly brace, curly brace, inside three lines of 1520 01:12:33,970 --> 01:12:38,760 pseudocode, take a user's full name, two and only two command line 1521 01:12:38,760 --> 01:12:42,670 arguments, print out a greeting to the user that includes their first name. 1522 01:12:42,670 --> 01:12:45,810 >> So the first one, take a user's full name. 1523 01:12:45,810 --> 01:12:47,290 Who can help me take a user's full name? 1524 01:12:47,290 --> 01:12:50,882 1525 01:12:50,882 --> 01:12:52,780 How do I do that part? 1526 01:12:52,780 --> 01:12:57,685 1527 01:12:57,685 --> 01:12:59,500 Did you guys do that first part? 1528 01:12:59,500 --> 01:13:02,587 1529 01:13:02,587 --> 01:13:03,894 Take a user's full name. 1530 01:13:03,894 --> 01:13:05,860 Well, for that one command line argument, what should I write? 1531 01:13:05,860 --> 01:13:09,624 What should I change about my function declaration? 1532 01:13:09,624 --> 01:13:11,140 >> STUDENT: You don't want it to be void anymore. 1533 01:13:11,140 --> 01:13:11,410 >> JASON HIRSCHHORN: Exactly. 1534 01:13:11,410 --> 01:13:12,385 I don't want it to be void. 1535 01:13:12,385 --> 01:13:13,630 What do I want it to be? 1536 01:13:13,630 --> 01:13:14,650 >> STUDENT: Int argc. 1537 01:13:14,650 --> 01:13:15,720 >> JASON HIRSCHHORN: Int argc. 1538 01:13:15,720 --> 01:13:17,430 >> STUDENT: String argv. 1539 01:13:17,430 --> 01:13:19,055 >> JASON HIRSCHHORN: String argv. 1540 01:13:19,055 --> 01:13:19,705 >> STUDENT: Bracket, bracket. 1541 01:13:19,705 --> 01:13:20,070 >> JASON HIRSCHHORN: Bracket, bracket. 1542 01:13:20,070 --> 01:13:21,780 That's exactly right, and that's actually how I'm going to get 1543 01:13:21,780 --> 01:13:22,820 a user's full name. 1544 01:13:22,820 --> 01:13:24,070 They're going to put it in there. 1545 01:13:24,070 --> 01:13:27,720 1546 01:13:27,720 --> 01:13:28,780 So that one's done. 1547 01:13:28,780 --> 01:13:30,780 So now I want two and only two command line arguments. 1548 01:13:30,780 --> 01:13:35,065 So Noah, what is one way I can check the number of command 1549 01:13:35,065 --> 01:13:36,315 line arguments I have? 1550 01:13:36,315 --> 01:13:39,676 1551 01:13:39,676 --> 01:13:40,725 >> STUDENT: You do an if statement. 1552 01:13:40,725 --> 01:13:42,270 >> JASON HIRSCHHORN: If statement, exactly. 1553 01:13:42,270 --> 01:13:44,830 What do I want as the condition? 1554 01:13:44,830 --> 01:13:48,370 >> STUDENT: If argc greater than 3. 1555 01:13:48,370 --> 01:13:52,485 >> JASON HIRSCHHORN: If argc is greater than 3. 1556 01:13:52,485 --> 01:13:58,430 If argc greater than 3, what do I do? 1557 01:13:58,430 --> 01:13:59,400 >> STUDENT: Return 1. 1558 01:13:59,400 --> 01:14:01,856 >> JASON HIRSCHHORN: Return 1. 1559 01:14:01,856 --> 01:14:04,050 Will that ensure I have two and only two? 1560 01:14:04,050 --> 01:14:07,280 1561 01:14:07,280 --> 01:14:07,820 >> STUDENT: No. 1562 01:14:07,820 --> 01:14:13,110 If argc is greater than 3 and less than 2. 1563 01:14:13,110 --> 01:14:17,770 >> JASON HIRSCHHORN: If argc is greater than 3 and argc is less than 2. 1564 01:14:17,770 --> 01:14:21,540 What's one way I can write this in just one line, or without using that 1565 01:14:21,540 --> 01:14:23,890 and Boolean operator? 1566 01:14:23,890 --> 01:14:25,942 >> STUDENT: It's not equal to 2. 1567 01:14:25,942 --> 01:14:28,790 >> JASON HIRSCHHORN: If argc is not equal to 2. 1568 01:14:28,790 --> 01:14:29,260 Exactly. 1569 01:14:29,260 --> 01:14:34,890 So if argc is not equal to 2, that will give me this program, 1570 01:14:34,890 --> 01:14:37,250 personalized, when I run it, and that will give me how many 1571 01:14:37,250 --> 01:14:38,740 command line arguments? 1572 01:14:38,740 --> 01:14:42,110 How many will I check for? 1573 01:14:42,110 --> 01:14:42,370 >> Sorry. 1574 01:14:42,370 --> 01:14:43,400 Let me reword that. 1575 01:14:43,400 --> 01:14:46,620 If argc = 2, how many things did the user write into the line? 1576 01:14:46,620 --> 01:14:47,500 >> STUDENT: One. 1577 01:14:47,500 --> 01:14:49,120 >> JASON HIRSCHHORN: Just one. 1578 01:14:49,120 --> 01:14:50,430 So before, you were right, Noah. 1579 01:14:50,430 --> 01:14:52,192 What do I actually need to put there? 1580 01:14:52,192 --> 01:14:53,640 >> STUDENT: That equals 3. 1581 01:14:53,640 --> 01:14:55,970 >> JASON HIRSCHHORN: Argc does not equal 3. 1582 01:14:55,970 --> 01:15:00,250 I want to equal 3 because I want personalized plus the other two. 1583 01:15:00,250 --> 01:15:02,560 So if argc does not equal 3, return 1. 1584 01:15:02,560 --> 01:15:04,475 Why did you say return 1? 1585 01:15:04,475 --> 01:15:06,180 >> STUDENT: Because that means it's wrong. 1586 01:15:06,180 --> 01:15:08,420 >> JASON HIRSCHHORN: That means it's wrong, and what's that going to do? 1587 01:15:08,420 --> 01:15:10,360 >> STUDENT: Re-prompt the user. 1588 01:15:10,360 --> 01:15:11,940 >> JASON HIRSCHHORN: It's going to quit the program, so it will have to run 1589 01:15:11,940 --> 01:15:14,520 again, but we don't have anything in here to ask for them again. 1590 01:15:14,520 --> 01:15:14,840 I didn't ask for that. 1591 01:15:14,840 --> 01:15:15,990 So that's exactly right. 1592 01:15:15,990 --> 01:15:17,680 Return 1 just quits the program. 1593 01:15:17,680 --> 01:15:18,520 Great. 1594 01:15:18,520 --> 01:15:21,600 >> And print out a greeting to the user that includes their first name. 1595 01:15:21,600 --> 01:15:24,010 How can I do that? 1596 01:15:24,010 --> 01:15:25,990 Anna? 1597 01:15:25,990 --> 01:15:26,580 >> STUDENT: Printf. 1598 01:15:26,580 --> 01:15:27,785 >> JASON HIRSCHHORN: Printf. 1599 01:15:27,785 --> 01:15:28,640 >> STUDENT: (" 1600 01:15:28,640 --> 01:15:30,460 >> JASON HIRSCHHORN: ( 1601 01:15:30,460 --> 01:15:31,600 >> STUDENT: "hello 1602 01:15:31,600 --> 01:15:33,210 >> JASON HIRSCHHORN: "hello 1603 01:15:33,210 --> 01:15:34,800 >> STUDENT: %s 1604 01:15:34,800 --> 01:15:37,230 >> JASON HIRSCHHORN: %s 1605 01:15:37,230 --> 01:15:38,840 >> STUDENT: /n 1606 01:15:38,840 --> 01:15:41,030 >> JASON HIRSCHHORN: /n 1607 01:15:41,030 --> 01:15:43,120 >> STUDENT: ", 1608 01:15:43,120 --> 01:15:45,130 >> JASON HIRSCHHORN: ", 1609 01:15:45,130 --> 01:15:46,620 >> STUDENT: Argv 1610 01:15:46,620 --> 01:15:47,080 >> JASON HIRSCHHORN: Sorry? 1611 01:15:47,080 --> 01:15:47,750 >> STUDENT: Argv 1612 01:15:47,750 --> 01:15:48,840 >> JASON HIRSCHHORN: Argv 1613 01:15:48,840 --> 01:15:50,190 >> STUDENT: Square brackets 1. 1614 01:15:50,190 --> 01:15:51,230 >> JASON HIRSCHHORN: Square brackets? 1615 01:15:51,230 --> 01:15:51,870 >> STUDENT: 1. 1616 01:15:51,870 --> 01:15:54,050 >> JASON HIRSCHHORN: 1. 1617 01:15:54,050 --> 01:15:55,850 >> STUDENT: And ) semicolon. 1618 01:15:55,850 --> 01:15:58,290 >> JASON HIRSCHHORN: ) semicolon. 1619 01:15:58,290 --> 01:16:00,250 Let us run this. 1620 01:16:00,250 --> 01:16:03,730 Make personalized. 1621 01:16:03,730 --> 01:16:06,392 ./personalized. 1622 01:16:06,392 --> 01:16:07,420 It just quit. 1623 01:16:07,420 --> 01:16:10,390 Why did it quit? 1624 01:16:10,390 --> 01:16:11,310 >> STUDENT: You didn't put any arguments. 1625 01:16:11,310 --> 01:16:12,560 >> JASON HIRSCHHORN: I didn't put any arguments in. 1626 01:16:12,560 --> 01:16:16,360 1627 01:16:16,360 --> 01:16:16,920 Looks great. 1628 01:16:16,920 --> 01:16:18,130 You guys nailed it. 1629 01:16:18,130 --> 01:16:22,420 So again, you went into argv and you checked at the beginning argc. 1630 01:16:22,420 --> 01:16:24,390 Those are two important things to remember. 1631 01:16:24,390 --> 01:16:29,250 So in our last two minutes before we leave, we won't get to pset two, but 1632 01:16:29,250 --> 01:16:33,585 like I said, if you want to stay after for 10 minutes, I would he happy to go 1633 01:16:33,585 --> 01:16:35,710 over with you guys the logic for these problem sets. 1634 01:16:35,710 --> 01:16:37,930 We had a lot to cover today so I didn't get to them. 1635 01:16:37,930 --> 01:16:39,820 Generally, I like to include that at the end. 1636 01:16:39,820 --> 01:16:42,150 So if you want to stick around, stick around for 10 minutes. 1637 01:16:42,150 --> 01:16:42,990 We'll do that. 1638 01:16:42,990 --> 01:16:46,250 >> Then, as I mentioned at the beginning, pset zero and one, we'll get that 1639 01:16:46,250 --> 01:16:48,040 feedback back to you as soon as possible. 1640 01:16:48,040 --> 01:16:51,260 The goal is to get pset one feedback, the comments part, 1641 01:16:51,260 --> 01:16:52,850 before pset two is due. 1642 01:16:52,850 --> 01:16:56,730 So if you made some mistakes or stylistically, things could be better, 1643 01:16:56,730 --> 01:17:01,330 you'll have that information so you won't do the same things in pset two. 1644 01:17:01,330 --> 01:17:06,140 And then in the future, you'll get all of your pset feedback and comments 1645 01:17:06,140 --> 01:17:08,190 back before the next one is due. 1646 01:17:08,190 --> 01:17:12,695 >> Finally, if I could put in another plug for feedback. 1647 01:17:12,695 --> 01:17:13,935 This is your first section. 1648 01:17:13,935 --> 01:17:15,920 It was my first section teaching this year. 1649 01:17:15,920 --> 01:17:16,930 Go there. 1650 01:17:16,930 --> 01:17:19,800 Write stuff, good and bad, and I will use it to make 1651 01:17:19,800 --> 01:17:21,320 section better next time. 1652 01:17:21,320 --> 01:17:24,870 And last, again, if you need anything, let me know. 1653 01:17:24,870 --> 01:17:26,410 I'm more than happy to help you guys. 1654 01:17:26,410 --> 01:17:28,420 If you just want candy, I will have extra candy. 1655 01:17:28,420 --> 01:17:29,800 I'm happy to bring it to you. 1656 01:17:29,800 --> 01:17:31,190 >> This is going to be an amazing course. 1657 01:17:31,190 --> 01:17:34,900 This is one of, if not my favorite course, I've taken in my entire life, 1658 01:17:34,900 --> 01:17:37,220 and I hope it will be that wonderful for you. 1659 01:17:37,220 --> 01:17:40,090 So I want to do everything in my power to make it that awesome for you. 1660 01:17:40,090 --> 01:17:46,590 I'm going to send out an email if you're in my section or Noah, and 1661 01:17:46,590 --> 01:17:50,310 going to try and schedule with each of you guys a time to check in over the 1662 01:17:50,310 --> 01:17:53,820 next couple of weeks so I can get a sense of what your CS background is, 1663 01:17:53,820 --> 01:17:58,170 and it will also help me tailor these sections to better suit your needs. 1664 01:17:58,170 --> 01:17:59,740 So that's what I've got lined up. 1665 01:17:59,740 --> 01:18:02,260 Again, I'll see you outside if you want to be there. 1666 01:18:02,260 --> 01:18:03,700 This was week two section. 1667 01:18:03,700 --> 01:18:04,950 Thank you guys so much. 1668 01:18:04,950 --> 01:18:09,067