1 00:00:00,000 --> 00:00:07,070 [♪Buzzing♪] 2 00:00:07,070 --> 00:00:10,380 So to introduce you to what I've got up on the screen; 3 00:00:10,380 --> 00:00:12,710 it's one of the tools we developed over the summer; 4 00:00:12,710 --> 00:00:15,530 it's called CS50 Spaces, 5 00:00:15,530 --> 00:00:18,860 and it's our collaborative coding application 6 00:00:18,860 --> 00:00:20,180 that we're going to use in sections. 7 00:00:20,180 --> 00:00:22,470 It's pretty cool. 8 00:00:22,470 --> 00:00:26,040 Have any of you seen CS50 run yet? 9 00:00:26,040 --> 00:00:29,760 Let me pull that up in another tab. 10 00:00:29,760 --> 00:00:33,440 Run.cs50.net 11 00:00:33,440 --> 00:00:36,230 So 12 00:00:36,230 --> 00:00:39,700 if you go to run.cs50.net, 13 00:00:39,700 --> 00:00:43,030 you can see this is an in your browser way to 14 00:00:43,030 --> 00:00:45,940 actually code--write C code, 15 00:00:45,940 --> 00:00:47,460 and then run it, 16 00:00:47,460 --> 00:00:50,960 so it both compiles it, runs it, then returns the output to you, 17 00:00:50,960 --> 00:00:53,350 so this is kind of a cool way to experiment 18 00:00:53,350 --> 00:00:56,060 with some of the code that David goes over in lectures 19 00:00:56,060 --> 00:00:58,640 and we give to on you on problem sets 20 00:00:58,640 --> 00:01:01,900 without actually having to fire up the appliance. 21 00:01:01,900 --> 00:01:04,489 >> So what Spaces does that's different from that 22 00:01:04,489 --> 00:01:07,450 if I switch back to this tab, 23 00:01:07,450 --> 00:01:11,580 Spaces is CS50 run, like you see right here, 24 00:01:11,580 --> 00:01:15,380 but if we scroll over and look at the other side of the window, 25 00:01:15,380 --> 00:01:18,130 you see the right I'm the only person in this space, 26 00:01:18,130 --> 00:01:21,030 but what I'm going to do is ask you guys to join this Space with me 27 00:01:21,030 --> 00:01:24,590 and we'll all get to code in the same little window, 28 00:01:24,590 --> 00:01:26,070 share code with each other, 29 00:01:26,070 --> 00:01:28,900 and talk about the differences between our code. 30 00:01:28,900 --> 00:01:31,530 So it's kind of a cool thing-- 31 00:01:31,530 --> 00:01:35,770 so to join, if you would visit this link right here at the top 32 00:01:35,770 --> 00:01:38,980 that I've highlighted, 33 00:01:38,980 --> 00:01:53,860 you can just type in apps.cs50.net/spaces/vqcspSnO. 34 00:01:53,860 --> 00:01:56,610 >> All right, so to start off, 35 00:01:56,610 --> 00:01:59,720 I'm going to give you a little bit of a tour of this. 36 00:01:59,720 --> 00:02:01,720 We're gonna do some silly little coding with it at first, 37 00:02:01,720 --> 00:02:06,200 just so that everybody kinda gets a feel for what it's like, 38 00:02:06,200 --> 00:02:09,000 but what I've done-- 39 00:02:09,000 --> 00:02:11,830 or what I'm about to do right here is-- 40 00:02:11,830 --> 00:02:13,480 I'm going to take the code that I've written in here, 41 00:02:13,480 --> 00:02:15,800 which as you see right now is empty; 42 00:02:15,800 --> 00:02:17,090 it doesn't do anything. 43 00:02:17,090 --> 00:02:21,080 It's just boiler plate code, we'll call it. 44 00:02:21,080 --> 00:02:23,820 With a little to-do comment. 45 00:02:23,820 --> 00:02:27,850 I'm going to save it by hitting this Save button. 46 00:02:27,850 --> 00:02:30,190 And what that does is it 47 00:02:30,190 --> 00:02:34,400 highlighted it up here briefly--that revision save green box, 48 00:02:34,400 --> 00:02:37,570 and now, if you search for me in the Space, 49 00:02:37,570 --> 00:02:40,340 you click my name, 50 00:02:40,340 --> 00:02:43,370 you can see that I've got this revision right here, 51 00:02:43,370 --> 00:02:45,490 and if you click on that revision, 52 00:02:45,490 --> 00:02:48,240 it'll load that code into your Space, too. 53 00:02:50,510 --> 00:02:54,160 >> So has everybody loaded that code into their Space? 54 00:02:54,160 --> 00:02:57,810 All right, if you haven't yet, go ahead and do that, 55 00:02:57,810 --> 00:03:01,120 and then what I want you guys to do in this little to-do section 56 00:03:01,120 --> 00:03:05,940 is write a little bit using printf about yourself, 57 00:03:05,940 --> 00:03:08,310 so for example, I would write 58 00:03:08,310 --> 00:03:12,270 printf("My name is Nate"); 59 00:03:15,140 --> 00:03:17,700 and then I'd write 60 00:03:17,700 --> 00:03:21,660 "I graduated in 2009 61 00:03:21,660 --> 00:03:24,440 from Stanford" 62 00:03:26,050 --> 00:03:28,390 and I would love to hear about 63 00:03:28,390 --> 00:03:30,270 what year you are in school, 64 00:03:30,270 --> 00:03:33,910 whether you're at the college or the business school 65 00:03:33,910 --> 00:03:36,130 or another Harvard school, 66 00:03:36,130 --> 00:03:39,200 what you're studying if it's known yet; 67 00:03:39,200 --> 00:03:41,290 if it's not known, that's totally cool. 68 00:03:41,290 --> 00:03:45,170 I didn't decide until I was late in my sophomore year. 69 00:03:46,910 --> 00:03:49,210 and then I would also love to know 70 00:03:49,210 --> 00:03:51,610 anything crazy you want to tell me or 71 00:03:51,610 --> 00:03:54,790 something about the class or just anything else. 72 00:03:56,930 --> 00:04:00,370 When you're done writing that with your little printf, 73 00:04:00,370 --> 00:04:03,480 if you'll save the revision or rather 74 00:04:03,480 --> 00:04:06,080 first make sure that you can run it 75 00:04:06,080 --> 00:04:10,640 and that it prints out exactly what you want it to print out, 76 00:04:14,050 --> 00:04:16,399 and then if you go ahead and save that revision 77 00:04:16,399 --> 00:04:21,600 using the Revision Save button, this first little guy over here, 78 00:04:24,080 --> 00:04:27,340 and we'll kinda go through and do a little introduction that way. 79 00:04:27,340 --> 00:04:30,150 So you should just have to hit the Play button, 80 00:04:30,150 --> 00:04:32,550 which is up here on the top left, 81 00:04:32,550 --> 00:04:35,340 so this Play button on the top left will 82 00:04:35,340 --> 00:04:39,050 send your code off to our server where it gets compiled and then run. 83 00:04:40,600 --> 00:04:43,300 So what's kinda crazy about this is 84 00:04:43,300 --> 00:04:46,550 this code actually isn't running on your computer; 85 00:04:46,550 --> 00:04:49,290 it's actually being sent across the Internet. 86 00:04:49,290 --> 00:04:51,240 >> How's it going, Charlotte? 87 00:04:51,240 --> 00:04:54,810 >>I--okay. I'm--I think I'm 88 00:04:54,810 --> 00:04:57,330 having difficulties saving it 89 00:04:57,330 --> 00:05:00,530 on the server such that it can--I can run the program. 90 00:05:02,090 --> 00:05:05,780 And then when I try to run it, it says that there's an error. 91 00:05:05,780 --> 00:05:08,850 Declared as a virus, which is not in there anymore. 92 00:05:08,850 --> 00:05:11,860 Oh, let's see; can you hit Save again? 93 00:05:11,860 --> 00:05:13,770 I see it on the screen. 94 00:05:13,770 --> 00:05:15,120 >>Right, okay, so that's what I have. 95 00:05:15,120 --> 00:05:19,430 And I don't know why it doesn't let me run it. 96 00:05:22,990 --> 00:05:24,990 Oh, okay, sorry. 97 00:05:27,930 --> 00:05:31,220 Can you explain why--what was wrong--what wasn't working with it? 98 00:05:31,220 --> 00:05:34,760 >>Yeah, because I didn't see that there was a last line where there's still a variable 99 00:05:34,760 --> 00:05:37,700 that I had not declared, which I did not intend for it being there. 100 00:05:37,700 --> 00:05:38,730 Yeah, perfect! 101 00:05:38,730 --> 00:05:40,390 >>Now if I save it, I think it should work. 102 00:05:40,390 --> 00:05:42,970 Now that you've saved it, it works; awesome. 103 00:05:42,970 --> 00:05:44,570 >> So yeah, as we go through-- 104 00:05:44,570 --> 00:05:46,690 so what was going on, 105 00:05:46,690 --> 00:05:49,130 I'm just going to repeat this for the camera. 106 00:05:49,130 --> 00:05:51,760 What was happening was that this variable s 107 00:05:51,760 --> 00:05:54,310 was left in after--I assume you deleted some other stuff 108 00:05:54,310 --> 00:05:56,670 where you declared as, 109 00:05:56,670 --> 00:05:59,560 and so this was throwing things off and wasn't letting you run it. 110 00:05:59,560 --> 00:06:02,540 So this is exactly what we want to do in section 111 00:06:02,540 --> 00:06:05,690 is have people try writing different pieces of code, 112 00:06:05,690 --> 00:06:08,490 compiling it, see what works, see what doesn't, 113 00:06:08,490 --> 00:06:10,730 and then kinda talk about these things, because these are tripping points that 114 00:06:10,730 --> 00:06:12,850 I still encounter, 115 00:06:12,850 --> 00:06:15,470 I still--I do lots of silly stuff all the time, 116 00:06:15,470 --> 00:06:17,820 and I don't know why it doesn't work, 117 00:06:17,820 --> 00:06:22,960 so let's pull up Charlotte's revision 3, 118 00:06:23,980 --> 00:06:25,380 and we'll run it. 119 00:06:25,380 --> 00:06:28,950 >> So when I run, now that I've got her code in here, 120 00:06:28,950 --> 00:06:31,450 I can zoom in, hit the Run button, 121 00:06:31,450 --> 00:06:33,680 uploads it, builds it, runs it, 122 00:06:33,680 --> 00:06:36,980 says it ran successfully, and if I scroll down, 123 00:06:36,980 --> 00:06:39,440 see Charlotte is a junior at Adams House, 124 00:06:39,440 --> 00:06:42,720 psychology, secondary in economics; awesome. 125 00:06:42,720 --> 00:06:44,310 Great to meet you! 126 00:06:44,310 --> 00:06:46,550 Awesome. 127 00:06:46,550 --> 00:06:49,670 Okay, moving on, let's go to Daniel, 128 00:06:49,670 --> 00:06:52,710 go to revision 3, 129 00:06:52,710 --> 00:06:54,180 compile, run; 130 00:06:54,180 --> 00:06:56,680 why don't you tell us a little about yourself, Daniel? 131 00:06:56,680 --> 00:06:59,380 >>Call me Dan. 132 00:06:59,380 --> 00:07:01,920 I'm going to business school across from you guys, 133 00:07:01,920 --> 00:07:05,020 and I was in the Marines before here, 134 00:07:05,020 --> 00:07:08,020 and this is all new to me, so yeah. 135 00:07:08,020 --> 00:07:11,060 Don't feel bad; I'm 30 and I don't know any of this stuff. 136 00:07:11,060 --> 00:07:13,800 Awesome; thanks, Dan! 137 00:07:13,800 --> 00:07:17,300 All right, we got another Daniel. 138 00:07:17,300 --> 00:07:20,930 You go by Daniel--will you introduce yourself, Daniel? 139 00:07:20,930 --> 00:07:22,260 >> >>Sure, I'm Daniel. 140 00:07:22,260 --> 00:07:24,330 I'm a sophomore at Mather right now. 141 00:07:24,330 --> 00:07:25,840 I'm concentrating on government, 142 00:07:25,840 --> 00:07:28,760 but I wanted to take CS50 because I've heard great reviews about the class 143 00:07:28,760 --> 00:07:30,750 and I've never tried CS before. 144 00:07:30,750 --> 00:07:34,680 Scroll down here and show this--cool. 145 00:07:36,330 --> 00:07:39,090 Moving on, Jared. 146 00:07:39,090 --> 00:07:41,990 How's it going? 147 00:07:41,990 --> 00:07:43,430 >>Good, how are you? 148 00:07:43,430 --> 00:07:47,520 As you can see, I'm a junior philosophy concentrator in Eliot, 149 00:07:47,520 --> 00:07:50,260 originally from southern California, 150 00:07:50,260 --> 00:07:53,100 and I'm interested in taking CS50 because I like the logic of it, 151 00:07:53,100 --> 00:07:57,420 and it seems really cool to do things on the computer, so. 152 00:07:57,420 --> 00:07:59,570 Awesome; where in southern California are you from? 153 00:07:59,570 --> 00:08:02,340 >>About an hour from [inaudible], a small city [inaudible]. 154 00:08:02,340 --> 00:08:04,850 Oh, yeah, my dad's from Redlands. 155 00:08:04,850 --> 00:08:07,060 Cool stuff. 156 00:08:07,060 --> 00:08:12,130 >> All right, Jimmy; how's it going, Jimmy? 157 00:08:12,130 --> 00:08:15,130 >>Good, how are you? 158 00:08:15,130 --> 00:08:16,040 Doing pretty well! 159 00:08:16,040 --> 00:08:17,710 Tell us about yourself. 160 00:08:17,710 --> 00:08:20,840 >>I'm a freshman, and I'm not entirely sure what I'm majoring in 161 00:08:20,840 --> 00:08:22,820 but I'm kinda leaning towards economics. 162 00:08:22,820 --> 00:08:25,680 Like Daniel before me, I've never done any CS, but it sounds kinda cool. 163 00:08:25,680 --> 00:08:28,120 I'm originally from Chicago. 164 00:08:28,120 --> 00:08:31,640 Awesome. So check out his printout here. 165 00:08:31,640 --> 00:08:34,630 I wanted to ask, did this print out as you expected? 166 00:08:34,630 --> 00:08:37,650 >>No, I forgot to print a new line. 167 00:08:37,650 --> 00:08:41,270 I actually forgot that when I was writing my thing 168 00:08:41,270 --> 00:08:43,090 for the first time, too. 169 00:08:43,090 --> 00:08:46,330 It's kind of a pain to remember to put those in manually. 170 00:08:46,330 --> 00:08:50,560 It'd be nice if it just know that's what you wanted. 171 00:08:50,560 --> 00:08:53,630 >> Okay, Kelly. 172 00:08:53,630 --> 00:08:56,050 Wow, we have 2 Kellys; 2 Daniels, 2 Kellys. 173 00:08:56,050 --> 00:09:00,490 So Kelly--do you go by Kelly Ren? 174 00:09:00,490 --> 00:09:03,870 Okay, awesome. 175 00:09:03,870 --> 00:09:07,270 Or Kelly, I mean. 176 00:09:07,270 --> 00:09:10,260 >>So yeah, so I'm Kelly. 177 00:09:10,260 --> 00:09:12,930 I'm a sophomore in Pfoho. 178 00:09:12,930 --> 00:09:17,000 I got an e-mail today that I still have 7 weeks to declare my concentration, 179 00:09:17,000 --> 00:09:20,940 so I'm trying not to think about it, 180 00:09:20,940 --> 00:09:24,090 and I like baking, running, and Craisins. 181 00:09:24,090 --> 00:09:29,110 I don't have experience with most of CS; 182 00:09:29,110 --> 00:09:32,700 I taught myself to build Web sites when I was really young, 183 00:09:32,700 --> 00:09:35,750 but I think I probably taught myself wrong a lot of times. 184 00:09:35,750 --> 00:09:39,260 Hopefully I'll learn a little bit more 185 00:09:39,260 --> 00:09:41,450 and--yeah. 186 00:09:41,450 --> 00:09:44,100 >> Awesome; well, thank you. 187 00:09:44,100 --> 00:09:47,190 Let's go to the other Kelly; 188 00:09:47,190 --> 00:09:48,880 I don't have anything--did you say something in here? 189 00:09:48,880 --> 00:09:51,880 >> [inaudible] 190 00:09:51,880 --> 00:09:56,180 There it is! 191 00:09:56,180 --> 00:09:58,120 Sweet! 192 00:09:58,120 --> 00:10:01,400 >>All right, so I'm Kelly. 193 00:10:01,400 --> 00:10:03,980 I'm a first-year graduate student. 194 00:10:03,980 --> 00:10:05,560 I'm majoring in Mind, Brain, and Education. 195 00:10:05,560 --> 00:10:07,940 I graduated in Franklin & Marshall College in 2008 196 00:10:07,940 --> 00:10:10,840 with majors in psychology and sociology 197 00:10:10,840 --> 00:10:13,120 and a minor in French. 198 00:10:13,120 --> 00:10:15,330 I'm taking CS50 199 00:10:15,330 --> 00:10:18,080 because I'm interested in getting a PhD in Neuroscience 200 00:10:18,080 --> 00:10:21,220 and I feel like this will help me understand the technologies being used. 201 00:10:21,220 --> 00:10:23,150 Awesome. 202 00:10:26,980 --> 00:10:29,390 Moving on, Mikaela. 203 00:10:29,390 --> 00:10:31,640 Is that correct pronunciation? 204 00:10:31,640 --> 00:10:32,990 Okay, awesome. 205 00:10:32,990 --> 00:10:34,640 >> >>I'm Mikhaila. 206 00:10:34,640 --> 00:10:36,870 I'm a sophomore in Kirkland House, 207 00:10:36,870 --> 00:10:39,950 and I'm concentrating in Mechanical Engineering. 208 00:10:39,950 --> 00:10:43,180 I really like cycling, built a bike this summer. 209 00:10:43,180 --> 00:10:46,140 I'm taking CS, actually, 210 00:10:46,140 --> 00:10:48,350 as a concentration requirement, 211 00:10:48,350 --> 00:10:52,550 but you know, I think it's still cool sort of engineering class. 212 00:10:52,550 --> 00:10:55,150 Awesome--you built a bike this summer? 213 00:10:55,150 --> 00:10:56,730 >>Yeah. 214 00:10:56,730 --> 00:10:58,900 Wow, like the frame and all that? 215 00:10:58,900 --> 00:11:00,120 >> Uh-hunh (affirmative). 216 00:11:00,120 --> 00:11:01,530 Wow, that's really cool. 217 00:11:01,530 --> 00:11:06,370 >>[inaudible off-camera chat] 218 00:11:06,370 --> 00:11:10,000 [laughter] 219 00:11:12,640 --> 00:11:14,200 Yeah, it's been so long. 220 00:11:14,200 --> 00:11:16,730 You never know, right? 221 00:11:16,730 --> 00:11:19,420 Well, cool; well thanks, Mikhaila. 222 00:11:19,420 --> 00:11:21,390 >> All right, Missy. 223 00:11:21,390 --> 00:11:23,280 >>My name's Missy; I'm a freshman, 224 00:11:23,280 --> 00:11:25,500 so that means I don't know anything! 225 00:11:25,500 --> 00:11:29,180 I want to concentrate in astrophysics. 226 00:11:29,180 --> 00:11:31,220 In CS50, 227 00:11:31,220 --> 00:11:33,350 I feel like will definitely help with that. 228 00:11:33,350 --> 00:11:37,750 An odd fact about me is I'm a nationally certified firefighter. 229 00:11:37,750 --> 00:11:39,960 Wow! 230 00:11:39,960 --> 00:11:42,440 How did that happen? 231 00:11:42,440 --> 00:11:45,570 >>I attended an academy 232 00:11:45,570 --> 00:11:49,000 and went through hell for a lot of weeks 233 00:11:49,000 --> 00:11:51,200 and got my certification. 234 00:11:51,200 --> 00:11:54,090 Wow, congrats! 235 00:11:54,090 --> 00:11:57,280 That is no small feat, for sure. 236 00:11:57,280 --> 00:11:59,070 Okay, so now we'll do me. 237 00:11:59,070 --> 00:12:01,290 I'm the next one in the row. 238 00:12:01,290 --> 00:12:04,510 I'm--run it. 239 00:12:04,510 --> 00:12:07,580 >> So my name is Nate. 240 00:12:07,580 --> 00:12:11,160 I was class of 2009 at Stanford. 241 00:12:11,160 --> 00:12:13,840 I was a Symbolic Systems major, 242 00:12:13,840 --> 00:12:17,370 which is similar to the Mind, Brain, Behavior concentration here, 243 00:12:17,370 --> 00:12:19,300 but it's kind of a mix of 244 00:12:19,300 --> 00:12:22,350 computer science, linguistics, psychology, and philosophy. 245 00:12:22,350 --> 00:12:26,600 Kind of--yeah, a lot of weird stuff, but it was great, 246 00:12:26,600 --> 00:12:29,880 and then I did my master's in Computer Science there, as well. 247 00:12:29,880 --> 00:12:33,250 So I'm new to Harvard this year. 248 00:12:33,250 --> 00:12:35,750 I really wanted to get into teaching. 249 00:12:35,750 --> 00:12:39,860 I spent the last 4 years at Stanford TA-ing Computer Science classes 250 00:12:39,860 --> 00:12:43,370 and so David let me come on, 251 00:12:43,370 --> 00:12:45,550 and I'm gonna be helping out with CS50, 252 00:12:45,550 --> 00:12:47,890 so I do this full time; I'm not in grad school. 253 00:12:47,890 --> 00:12:50,330 It's been fun. 254 00:12:50,330 --> 00:12:53,130 Hopefully it'll continue to be fun; I'm sure it will. 255 00:12:53,130 --> 00:12:56,040 >> All right, Sam. 256 00:13:01,910 --> 00:13:05,470 >>My name is Sam; I'm a sophomore at the college, in law specifically. 257 00:13:05,470 --> 00:13:10,110 My major will be history of science, probably, and VES secondary, 258 00:13:10,110 --> 00:13:13,240 and my favorite color is yellow. 259 00:13:18,790 --> 00:13:21,100 No need to pass judgment or anything, but it's a good color! 260 00:13:21,100 --> 00:13:24,780 And Stella. 261 00:13:24,780 --> 00:13:26,980 >>Hi, I'm Stella; I'm a freshman in Grays. 262 00:13:26,980 --> 00:13:30,070 I have no clue what I've concentrated in yet, 263 00:13:30,070 --> 00:13:32,500 so CS50 was just kind of random, 264 00:13:32,500 --> 00:13:34,220 which is interesting. 265 00:13:34,220 --> 00:13:36,080 Fun fact, Jimmy was the first Harvard friend of mine, 266 00:13:36,080 --> 00:13:38,360 because we're both from Chicago. 267 00:13:38,360 --> 00:13:42,450 Cool, and Grace--that's supposed to be the really nice one, right? 268 00:13:42,450 --> 00:13:45,220 >>I thought [inaudible]. 269 00:13:45,220 --> 00:13:46,690 Oh, yeah. 270 00:13:46,690 --> 00:13:51,210 [inaudible/off-camera chat] 271 00:13:51,210 --> 00:13:54,420 Okay; I'm still learning, still learning. 272 00:13:54,420 --> 00:13:57,800 >> So we've got something else that's kind of interesting here. 273 00:13:59,600 --> 00:14:02,870 Yeah, I'm just wondering, do you have an idea that's going on here? 274 00:14:02,870 --> 00:14:05,890 You know how to interpret this? 275 00:14:05,890 --> 00:14:13,340 >>I was supposed to be put the semicolon after every single line. 276 00:14:13,340 --> 00:14:16,370 That's kind of a pain. 277 00:14:16,370 --> 00:14:19,100 Sometimes those semicolons can be a real pain to track down. 278 00:14:19,100 --> 00:14:21,420 So it turns out that 279 00:14:21,420 --> 00:14:22,820 the special program we're using this year to 280 00:14:22,820 --> 00:14:26,670 compile our C code is called Clang, 281 00:14:26,670 --> 00:14:29,460 and the reason we have started to use Clang this year-- 282 00:14:29,460 --> 00:14:32,030 because we haven't used that program in the past; 283 00:14:32,030 --> 00:14:35,710 we've used another one called GCC that's really popular-- 284 00:14:35,710 --> 00:14:38,420 Clang actually gives you error messages that make sense. 285 00:14:38,420 --> 00:14:41,420 This one says "expected ';' after expression", 286 00:14:41,420 --> 00:14:45,260 and it even tells you the expression after which you needed a semicolon. 287 00:14:45,260 --> 00:14:47,830 So are you fixing it? 288 00:14:47,830 --> 00:14:48,930 >>Yeah, right now. 289 00:14:48,930 --> 00:14:52,040 Awesome; let me know and we'll pull it up, and we'll run it. 290 00:14:52,040 --> 00:14:55,830 But yeah, putting these semicolons in is kind of a pain. 291 00:14:55,830 --> 00:14:59,580 >>I fixed it, so I think it should work. 292 00:14:59,580 --> 00:15:02,760 Awesome--revision 3. 293 00:15:02,760 --> 00:15:05,250 That looks like it. 294 00:15:05,250 --> 00:15:08,580 All right, so now if we run it, 295 00:15:08,580 --> 00:15:11,020 there it goes. 296 00:15:11,020 --> 00:15:13,830 Cool stuff. 297 00:15:13,830 --> 00:15:15,340 >> All right. 298 00:15:15,340 --> 00:15:18,230 So thanks for doing that. 299 00:15:18,230 --> 00:15:20,210 It's great to meet you guys. 300 00:15:20,210 --> 00:15:22,880 What we're gonna do now is spend the next little bit 301 00:15:22,880 --> 00:15:24,740 kinda diving into the section problems 302 00:15:24,740 --> 00:15:27,550 that come from the problem set from this week 303 00:15:27,550 --> 00:15:29,920 and we'll do that for most of the sections 304 00:15:29,920 --> 00:15:32,110 for the rest of the term, 305 00:15:32,110 --> 00:15:35,580 and then we'll save the last 15, 20 minutes or so 306 00:15:35,580 --> 00:15:38,490 to talk about the problem set itself 307 00:15:38,490 --> 00:15:41,420 and then just any other questions you have in general about the class. 308 00:15:43,380 --> 00:15:45,670 That said, what we really wanna do 309 00:15:45,670 --> 00:15:49,390 is have everybody participating 310 00:15:49,390 --> 00:15:51,500 using the Spaces stuff, 311 00:15:51,500 --> 00:15:53,500 throwing in code, throwing in suggestions, 312 00:15:53,500 --> 00:15:56,500 and talking about when things like this break 313 00:15:56,500 --> 00:15:58,360 or don't work, just because these are things that 314 00:15:58,360 --> 00:16:00,870 I know I came across 315 00:16:00,870 --> 00:16:03,180 when I started coding, 316 00:16:03,180 --> 00:16:05,490 and were really kinda frustrating at times 317 00:16:05,490 --> 00:16:08,690 because if it's like 2 am before your problem set is due, 318 00:16:08,690 --> 00:16:11,330 so--just for disclosure, 319 00:16:11,330 --> 00:16:14,590 I didn't start programming until I was a sophomore in college. 320 00:16:14,590 --> 00:16:17,360 It was kind of this weird, unknown thing 321 00:16:17,360 --> 00:16:20,360 that I heard was kinda cool 322 00:16:20,360 --> 00:16:23,590 and I also heard it could be a real pain in the butt 323 00:16:23,590 --> 00:16:27,610 if you miss a semicolon and that's the only error keeping your program from working 324 00:16:27,610 --> 00:16:30,890 and keeping you from going to sleep, 325 00:16:30,890 --> 00:16:32,650 so definitely, 326 00:16:32,650 --> 00:16:34,020 if you have questions like that, 327 00:16:34,020 --> 00:16:37,060 if you have concerns like that, let me know, 328 00:16:37,060 --> 00:16:40,050 because I want to make this a good experience for you guys. 329 00:16:40,050 --> 00:16:44,620 >> So have you guys all downloaded the problem set yet? 330 00:16:44,620 --> 00:16:47,160 Kinda know where it is? Okay. 331 00:16:47,160 --> 00:16:51,490 So we'll go to cs50.net, because I don't have it on this account, 332 00:16:51,490 --> 00:16:54,110 pull up the problem sets, 333 00:16:54,110 --> 00:16:57,940 pull up problem set 2, and we'll look at the standard edition today. 334 00:16:57,940 --> 00:17:01,190 We'll pull up the specification here. 335 00:17:02,640 --> 00:17:04,420 Scroll down. 336 00:17:06,790 --> 00:17:11,109 So we're gonna start looking at this section of questions. 337 00:17:14,069 --> 00:17:16,710 Have you guys watched these shorts yet? 338 00:17:16,710 --> 00:17:18,329 Okay. 339 00:17:18,329 --> 00:17:21,329 So there are shorts on loops, 340 00:17:21,329 --> 00:17:24,700 which is done by Tommy, one of the head teaching fellows. 341 00:17:24,700 --> 00:17:27,230 There's one on scope, 342 00:17:27,230 --> 00:17:30,530 I believe done by another teaching fellow named David 343 00:17:30,530 --> 00:17:33,400 and then one on Caesar Cipher also done by David. 344 00:17:33,400 --> 00:17:35,550 They're pretty cool. 345 00:17:35,550 --> 00:17:37,770 Have you guys been watching their shorts so far? 346 00:17:37,770 --> 00:17:39,270 How've they been? 347 00:17:39,270 --> 00:17:41,490 Helpful? Okay, good to know. 348 00:17:41,490 --> 00:17:44,210 If they're not, you can say so. 349 00:17:46,290 --> 00:17:49,870 But before you do, 350 00:17:49,870 --> 00:17:52,600 have you guys looked at these questions 351 00:17:52,600 --> 00:17:55,430 at all? 352 00:17:55,430 --> 00:17:57,480 >> Let's talk about this a little bit. 353 00:17:57,480 --> 00:18:00,600 So we've introduced over the last couple of weeks 354 00:18:00,600 --> 00:18:02,480 in lecture 355 00:18:02,480 --> 00:18:04,910 a number of different loops. 356 00:18:04,910 --> 00:18:06,140 There's the for loop, 357 00:18:06,140 --> 00:18:09,570 the while loop, and then the do-while loop, 358 00:18:09,570 --> 00:18:13,100 and in lecture, we've been talking a lot about 359 00:18:13,100 --> 00:18:16,010 how those loops are all 360 00:18:16,010 --> 00:18:18,320 fairly similar in a lot of ways, 361 00:18:18,320 --> 00:18:21,210 and you can do much of the same things with a while loop 362 00:18:21,210 --> 00:18:23,260 that you can with a for loop. 363 00:18:23,260 --> 00:18:26,300 You just have to kind of use the right combinations of variables and such. 364 00:18:26,300 --> 00:18:28,680 and so really 365 00:18:28,680 --> 00:18:31,200 the real distinction between all of the loops is that 366 00:18:31,200 --> 00:18:33,620 sometimes they're more useful 367 00:18:33,620 --> 00:18:36,930 to use them at one time than at another time. 368 00:18:36,930 --> 00:18:39,340 >> So for example, 369 00:18:39,340 --> 00:18:42,260 the while loop differing from a do-while loop. 370 00:18:42,260 --> 00:18:45,680 Does everybody feel like they have a good handle on when 371 00:18:45,680 --> 00:18:48,650 a do-while loop is more 372 00:18:48,650 --> 00:18:50,990 useful or what the difference is? 373 00:18:50,990 --> 00:18:53,410 >>I think the difference is a do-while loop 374 00:18:53,410 --> 00:18:56,960 runs--I don't know quite what the terminology is, 375 00:18:56,960 --> 00:18:59,750 but the series of functions within the loop. 376 00:18:59,750 --> 00:19:02,020 They automatically--it does its runs at least 377 00:19:02,020 --> 00:19:05,390 before it checks the condition, or the while loop will only run it when 378 00:19:05,390 --> 00:19:08,290 it continuously runs--whether or not the condition is being satisfied. 379 00:19:08,290 --> 00:19:11,010 Yeah, does that make sense? 380 00:19:11,010 --> 00:19:14,200 >> Let's take a look at a little bit of code. 381 00:19:14,200 --> 00:19:16,930 So just to repeat the question, 382 00:19:16,930 --> 00:19:19,230 or the answer, rather; 383 00:19:19,230 --> 00:19:22,050 the answer was that 384 00:19:22,050 --> 00:19:25,850 a while loop will check to see if the condition 385 00:19:25,850 --> 00:19:27,440 that you specify is met 386 00:19:27,440 --> 00:19:31,320 before it runs the body of the loop for the first time, 387 00:19:31,320 --> 00:19:35,000 whereas the do-while loop will automatically run the body of the loop, 388 00:19:35,000 --> 00:19:38,450 is what we call it--it's weird terminology, 389 00:19:38,450 --> 00:19:41,460 but if I ever say anything that doesn't make sense, let me know-- 390 00:19:41,460 --> 00:19:45,360 the loop body is that stuff that you put in between the curly braces. 391 00:19:45,360 --> 00:19:47,840 And in a do-while loop, 392 00:19:47,840 --> 00:19:50,220 that body get run at least once, 393 00:19:50,220 --> 00:19:54,460 and then the condition gets checked before it runs each successive time. 394 00:19:54,460 --> 00:19:56,580 >> So actually, Sam, 395 00:19:56,580 --> 00:20:00,820 do you mind writing a simple while loop 396 00:20:00,820 --> 00:20:02,840 in Spaces? 397 00:20:02,840 --> 00:20:05,600 >>How do you sort of like 398 00:20:05,600 --> 00:20:07,060 make a new...? 399 00:20:07,060 --> 00:20:09,220 I just delete whatever you have in there. 400 00:20:09,220 --> 00:20:11,300 Yeah, just clear it out. 401 00:20:11,300 --> 00:20:14,510 >>Well, I believe I'm having this-- 402 00:20:14,510 --> 00:20:16,640 which one do you want, a while, or do you want...? 403 00:20:16,640 --> 00:20:19,350 Yeah, just do a while true, print something out. 404 00:20:21,050 --> 00:20:23,140 And let's see. 405 00:20:23,140 --> 00:20:25,850 Alexander, I realized we never introduced you. 406 00:20:25,850 --> 00:20:27,510 >>Oh, that's fine. 407 00:20:27,510 --> 00:20:30,760 Do you--can you code up a do-while loop for us? 408 00:20:30,760 --> 00:20:32,520 >>Yeah, I'll try. 409 00:20:32,520 --> 00:20:34,370 All right, we'll just pull these up on the screen, 410 00:20:34,370 --> 00:20:38,240 so that you kind of have a handle on what the differences are. 411 00:20:38,240 --> 00:20:41,280 >> So here's what Sam's done. 412 00:20:41,280 --> 00:20:44,460 He's got an integer called i 413 00:20:44,460 --> 00:20:47,480 that he's initialized to 17. 414 00:20:47,480 --> 00:20:51,570 Then he says while (int i = 17) 415 00:20:51,570 --> 00:20:54,230 printf("My name is Sam."); 416 00:20:54,230 --> 00:20:57,300 So what happens when we run this code? 417 00:20:57,300 --> 00:21:00,340 There's an error; what does the error say? 418 00:21:00,340 --> 00:21:02,890 Let's pull it up and talk about it. 419 00:21:02,890 --> 00:21:05,040 An error occurred, it says. 420 00:21:05,040 --> 00:21:07,400 Let's scroll down. 421 00:21:07,400 --> 00:21:09,460 So it gave us this error, and it said, 422 00:21:09,460 --> 00:21:12,690 "Hey, I expected an expression." 423 00:21:12,690 --> 00:21:16,180 That's kinda cryptic. 424 00:21:19,160 --> 00:21:23,140 Yeah, so exactly--and again, 425 00:21:23,140 --> 00:21:27,140 there will be another little bug that we will catch in just a second. 426 00:21:27,140 --> 00:21:31,800 But this is another one of those kind of weird terminology things, 427 00:21:31,800 --> 00:21:36,150 so this int i = 17 428 00:21:36,150 --> 00:21:39,290 isn't an expression. 429 00:21:39,290 --> 00:21:41,220 What it looks like it's doing is it's 430 00:21:41,220 --> 00:21:43,410 declaring an integer called i 431 00:21:43,410 --> 00:21:45,540 and setting it to equal to 17 432 00:21:45,540 --> 00:21:48,780 or, rather, putting 17 into that int called i. 433 00:21:50,560 --> 00:21:53,350 And that doesn't make sense 434 00:21:53,350 --> 00:21:55,790 in the context of this while loop. 435 00:21:55,790 --> 00:21:58,190 It does make sense, as we'll see, 436 00:21:58,190 --> 00:22:01,250 in the context of a for loop, because for loop declarations do start off 437 00:22:01,250 --> 00:22:05,800 with that for int i equals something 438 00:22:05,800 --> 00:22:07,830 and goes on. 439 00:22:07,830 --> 00:22:09,500 >> So did you-- 440 00:22:09,500 --> 00:22:12,630 fix something, all right. 441 00:22:12,630 --> 00:22:15,270 Here it goes; awesome. 442 00:22:15,270 --> 00:22:17,920 So now things look a little different. 443 00:22:17,920 --> 00:22:19,030 A couple of things have changed. 444 00:22:19,030 --> 00:22:21,300 Can you tell us what things have changed? 445 00:22:21,300 --> 00:22:24,950 >>I took out the int, so there's no declaration [inaudible] instead of an assignment. 446 00:22:24,950 --> 00:22:28,770 The one equal sign, I put 2 equal signs, which means an equals. 447 00:22:28,770 --> 00:22:30,560 Awesome. 448 00:22:30,560 --> 00:22:33,320 Yeah, so what Sam did was he took out the int 449 00:22:33,320 --> 00:22:35,770 that was right here earlier, 450 00:22:35,770 --> 00:22:39,340 so if I just look at this--this is what it was earlier, it said int, 451 00:22:39,340 --> 00:22:41,970 which was declaring it--so we took out the type. 452 00:22:41,970 --> 00:22:44,710 There's int, char, string, short, 453 00:22:44,710 --> 00:22:47,380 those are called types. 454 00:22:47,380 --> 00:22:49,510 So you took out the type 455 00:22:49,510 --> 00:22:51,580 and then previously, 456 00:22:51,580 --> 00:22:54,190 he had just the single equals sign 457 00:22:54,190 --> 00:22:57,300 and what he said was problematic about this was 458 00:22:57,300 --> 00:22:59,580 when you have 1 equals sign, 459 00:22:59,580 --> 00:23:01,780 that's an assignment operator. 460 00:23:01,780 --> 00:23:04,930 That's not testing whether or not 2 things are equal. 461 00:23:04,930 --> 00:23:07,920 That's actually setting something equal to something. 462 00:23:07,920 --> 00:23:11,110 We're putting the number 17 into the variable i. 463 00:23:11,110 --> 00:23:13,850 So to do that kind of comparison, 464 00:23:13,850 --> 00:23:16,430 you have to use the double equals, 465 00:23:16,430 --> 00:23:19,640 which is kind of a weird thing 466 00:23:19,640 --> 00:23:22,760 but is a fairly common error. 467 00:23:23,980 --> 00:23:27,830 So one other thing I'll point out, if you don't mind, 468 00:23:27,830 --> 00:23:30,610 is that in 469 00:23:30,610 --> 00:23:33,210 our function here in int main, 470 00:23:33,210 --> 00:23:36,740 you see that the body of our function 471 00:23:36,740 --> 00:23:39,810 is offset from the left side 472 00:23:39,810 --> 00:23:42,400 by 4 spaces or a tab. 473 00:23:42,400 --> 00:23:44,730 The reason we do this 474 00:23:44,730 --> 00:23:47,000 is just for readability. 475 00:23:47,000 --> 00:23:51,620 As you can see in Sam's while loop here, 476 00:23:51,620 --> 00:23:53,120 he didn't indent at all, 477 00:23:53,120 --> 00:23:55,390 and that doesn't affect the functionality of the code. 478 00:23:55,390 --> 00:23:57,860 It will run exactly the same. 479 00:23:57,860 --> 00:24:00,950 >> So for example, if we come up to the top here 480 00:24:00,950 --> 00:24:02,680 and press our Play button, it will build it, 481 00:24:02,680 --> 00:24:04,470 it'll run it, 482 00:24:04,470 --> 00:24:06,700 and what's going to happen? 483 00:24:06,700 --> 00:24:08,360 Why does it still say running? 484 00:24:08,360 --> 00:24:10,560 >>Because it'll keep repeating hi. 485 00:24:10,560 --> 00:24:12,900 Exactly, and why does it keep repeating hi? 486 00:24:12,900 --> 00:24:15,850 >>Because the integer never changes, so it's always 17. 487 00:24:15,850 --> 00:24:18,030 Yeah, exactly, so 488 00:24:18,030 --> 00:24:20,970 let's see, after--remind me of your name again? 489 00:24:20,970 --> 00:24:24,020 Jimmy. So Jimmy said that 490 00:24:24,020 --> 00:24:27,340 the reason this keeps repeating hi over and over again 491 00:24:27,340 --> 00:24:30,510 is because this condition is always true. 492 00:24:30,510 --> 00:24:33,270 We never change the value of i. 493 00:24:33,270 --> 00:24:36,170 We never change the contents of what's stored in that variable. 494 00:24:36,170 --> 00:24:39,180 So at this point we got this error that 495 00:24:39,180 --> 00:24:42,170 your program generated too much output, 496 00:24:42,170 --> 00:24:45,100 and if we scroll down and kind of look at what's going on here, 497 00:24:45,100 --> 00:24:49,160 you can see that it is hi, hi, hi, hi, hi, hi, hi all the way through. 498 00:24:49,160 --> 00:24:51,200 Awesome. 499 00:24:51,200 --> 00:24:53,750 >> So kind of back to the original point 500 00:24:53,750 --> 00:24:57,020 that having the indentation here 501 00:24:57,020 --> 00:25:00,110 in the body of the while loop didn't affect anything. 502 00:25:00,110 --> 00:25:03,050 The program still ran just fine. 503 00:25:03,050 --> 00:25:06,790 Typically for good style-- 504 00:25:06,790 --> 00:25:10,210 and this is something that we look for in your problem sets-- 505 00:25:10,210 --> 00:25:13,930 we ask you to indent this by 4 spaces, which you can do 506 00:25:13,930 --> 00:25:16,440 by just pressing the tab key 507 00:25:16,440 --> 00:25:18,860 or by manually putting in 4 spaces yourself. 508 00:25:21,880 --> 00:25:25,050 Cool, okay so let's go back and look at 509 00:25:25,050 --> 00:25:30,460 Alexander's do-while. 510 00:25:30,460 --> 00:25:32,980 >>Just kinda having a little trouble. 511 00:25:32,980 --> 00:25:34,390 Okay, well let's work through it. 512 00:25:34,390 --> 00:25:36,400 Is that cool? 513 00:25:36,400 --> 00:25:37,430 >>Yeah. 514 00:25:37,430 --> 00:25:39,200 >> Okay, so here we go. 515 00:25:39,200 --> 00:25:41,010 This is the code that Alexander wrote. 516 00:25:41,010 --> 00:25:43,410 Do you go by Alex--okay. 517 00:25:43,410 --> 00:25:46,380 So here's the code that Alex has written. 518 00:25:46,380 --> 00:25:50,500 So, we've got our declaration of main right here, 519 00:25:50,500 --> 00:25:54,410 and then we've got this integer variable called i 520 00:25:54,410 --> 00:25:57,320 being declared right here. 521 00:25:57,320 --> 00:26:01,060 We've got a do statement 522 00:26:01,060 --> 00:26:04,600 and then the body again with these braces, 523 00:26:04,600 --> 00:26:06,520 so this is the body of the do-while-- 524 00:26:06,520 --> 00:26:09,210 let me zoom in on this so it's more readable-- 525 00:26:09,210 --> 00:26:12,730 and in the body, we're trying to print "this is the body", 526 00:26:12,730 --> 00:26:14,300 i++, 527 00:26:14,300 --> 00:26:17,710 so increment the contents of i and do this 528 00:26:17,710 --> 00:26:19,470 until i gets to be 3, 529 00:26:19,470 --> 00:26:22,440 and then our program ends. 530 00:26:22,440 --> 00:26:24,680 >> So what happens when we run this program? 531 00:26:24,680 --> 00:26:27,580 >>I expected an identifier. 532 00:26:27,580 --> 00:26:32,070 Okay, so build, run, error occurs. 533 00:26:32,070 --> 00:26:34,820 What happens? 534 00:26:34,820 --> 00:26:37,060 We scroll down here, 535 00:26:37,060 --> 00:26:40,260 we get this somewhat cryptic looking thing, 536 00:26:40,260 --> 00:26:43,930 expected semicolon after top level declaration. 537 00:26:43,930 --> 00:26:46,620 That's kinda weird. 538 00:26:46,620 --> 00:26:50,990 Does anybody have an idea of what might be going on? 539 00:26:53,080 --> 00:26:54,640 Kinda weird, yeah. 540 00:26:54,640 --> 00:26:57,640 >>[inaudible] be declared outside of the parentheses? 541 00:26:57,640 --> 00:27:03,480 So does that not apply then when you use parentheses? 542 00:27:03,480 --> 00:27:07,390 So he declared this guy outside of which parentheses? 543 00:27:09,670 --> 00:27:12,240 Oh, these guys--the curly braces? 544 00:27:12,240 --> 00:27:13,850 >>Yeah. 545 00:27:13,850 --> 00:27:15,310 Yeah, exactly. 546 00:27:15,310 --> 00:27:17,120 So it turns out that 547 00:27:17,120 --> 00:27:21,550 you can't actually declare a variable right here like this. 548 00:27:23,750 --> 00:27:26,690 What you might have been trying to do was-- 549 00:27:26,690 --> 00:27:30,110 cut it out of there-- 550 00:27:30,110 --> 00:27:34,330 you can either put it within the curly braces right here 551 00:27:34,330 --> 00:27:37,400 or another alternative 552 00:27:37,400 --> 00:27:40,350 is to put it completely outside. 553 00:27:42,480 --> 00:27:46,610 And so these--depending on where you actually put the declaration 554 00:27:46,610 --> 00:27:48,550 of that integer variable, 555 00:27:48,550 --> 00:27:53,540 you can get 2 very different kinds of variables; 556 00:27:53,540 --> 00:27:55,100 when it's outside of main, 557 00:27:55,100 --> 00:27:58,240 it's what we call a global variable. 558 00:27:58,240 --> 00:28:00,070 It's outside the scope of any of the functions-- 559 00:28:00,070 --> 00:28:03,420 it's not within a function-- 560 00:28:03,420 --> 00:28:07,050 whereas if we put it inside of main, 561 00:28:07,050 --> 00:28:10,000 like right here, then it's called a local variable, 562 00:28:10,000 --> 00:28:13,830 because it's local to the scope of main. 563 00:28:13,830 --> 00:28:16,720 And we'll talk a little bit more about what the differences are 564 00:28:16,720 --> 00:28:20,970 between the 2 types and why you use different kinds of variables, 565 00:28:20,970 --> 00:28:23,040 but for now, 566 00:28:23,040 --> 00:28:25,070 you can just keep all the variables local, 567 00:28:25,070 --> 00:28:28,320 because we don't have any need for global variables. 568 00:28:28,320 --> 00:28:31,390 >> Okay, so that's one thing right there. 569 00:28:31,390 --> 00:28:34,680 Let's try running this again, see what happens. 570 00:28:37,420 --> 00:28:40,270 Run, get another error. 571 00:28:40,270 --> 00:28:43,320 Zoom in a bit, what do we get? 572 00:28:45,110 --> 00:28:50,000 We get this expected parenthesis after do/while. 573 00:28:53,660 --> 00:28:55,260 Any ideas what's going on? Dan? 574 00:28:55,260 --> 00:28:58,880 >>Is the while then parenthesis and then-- 575 00:28:58,880 --> 00:29:01,990 I don't know if you need to redeclare int, 576 00:29:01,990 --> 00:29:04,980 but the do while (i < 3). 577 00:29:04,980 --> 00:29:06,930 Exactly, yeah. 578 00:29:06,930 --> 00:29:08,830 So it turns out that Dan's exactly right. 579 00:29:08,830 --> 00:29:11,230 We don't need to put this type here again. 580 00:29:11,230 --> 00:29:14,440 We can instead kill the type 581 00:29:14,440 --> 00:29:17,220 and just do that. 582 00:29:19,270 --> 00:29:21,640 All right. 583 00:29:21,640 --> 00:29:24,950 I'll zoom back out, try it one more time. 584 00:29:26,360 --> 00:29:29,580 Look at that; it runs successful. 585 00:29:29,580 --> 00:29:33,260 If we scroll down, "this is the body" printed out 3 times. 586 00:29:33,260 --> 00:29:35,820 That's pretty sweet. 587 00:29:35,820 --> 00:29:38,670 Well done, well done, guys. 588 00:29:38,670 --> 00:29:41,080 Well done. 589 00:29:41,080 --> 00:29:43,250 How's this going so far? 590 00:29:43,250 --> 00:29:46,200 Decent pace? 591 00:29:46,200 --> 00:29:49,200 Okay, cool. 592 00:29:49,200 --> 00:29:52,590 >> So, as we kind of back to the original question, 593 00:29:52,590 --> 00:29:55,430 we--and now if we see the difference, 594 00:29:55,430 --> 00:29:58,240 you see that in this do-while loop, 595 00:29:58,240 --> 00:30:00,030 the condition comes last. 596 00:30:00,030 --> 00:30:02,530 So as the computer reads the program 597 00:30:02,530 --> 00:30:06,050 since C programs are executed kind of in this sequential order, 598 00:30:06,050 --> 00:30:09,450 where they literally start with the first line of main, 599 00:30:09,450 --> 00:30:13,450 and then execute line 6, line 7, line 8, line 9, 600 00:30:13,450 --> 00:30:16,150 line 10, line 11, 601 00:30:16,150 --> 00:30:18,440 all this stuff at the top right here 602 00:30:18,440 --> 00:30:20,020 is going to execute once 603 00:30:20,020 --> 00:30:23,350 before we ever test this condition to see 604 00:30:23,350 --> 00:30:26,240 whether or not i--or the contents of i, rather-- 605 00:30:26,240 --> 00:30:29,750 are less than the number 3. 606 00:30:29,750 --> 00:30:33,300 And only if once we get to line 12, 607 00:30:33,300 --> 00:30:36,260 the contents of i are less than 3 608 00:30:36,260 --> 00:30:39,150 do we actually jump back up to line 7 609 00:30:39,150 --> 00:30:41,140 and then repeat the process, 610 00:30:41,140 --> 00:30:44,620 and then we test the condition again and again and again. 611 00:30:44,620 --> 00:30:47,440 >> So we can contrast this 612 00:30:47,440 --> 00:30:50,240 with--zooming back out, 613 00:30:50,240 --> 00:30:56,500 scroll down to Sam's version, 614 00:30:56,500 --> 00:31:00,760 we're gonna look at this, slightly changed so that it's no longer an infinite loop-- 615 00:31:00,760 --> 00:31:03,570 where if we zoom in, 616 00:31:03,570 --> 00:31:06,300 we see that as this program executes, 617 00:31:06,300 --> 00:31:09,120 it's going to first declare int i, 618 00:31:09,120 --> 00:31:12,030 set the contents of i be to 0, 619 00:31:12,030 --> 00:31:15,820 and then it hits the condition of the while loop first, 620 00:31:15,820 --> 00:31:19,210 so in this case, this body, this printf("hi") and i++ 621 00:31:19,210 --> 00:31:21,630 won't even execute 622 00:31:21,630 --> 00:31:24,310 unless i is actually less than or equal to 2. 623 00:31:24,310 --> 00:31:26,750 So if i is 3, for example, 624 00:31:26,750 --> 00:31:28,840 if we set this right here 625 00:31:28,840 --> 00:31:32,160 equal to 4--or I'll do 3, because I said 3-- 626 00:31:32,160 --> 00:31:34,890 and then run it, 627 00:31:34,890 --> 00:31:38,320 it'll build, it'll run totally fine, 628 00:31:38,320 --> 00:31:40,530 but it won't actually print out anything, 629 00:31:40,530 --> 00:31:43,030 because it failed that test for the first case. 630 00:31:44,780 --> 00:31:47,530 So in class, we kind of talked about why these do-while loops 631 00:31:47,530 --> 00:31:49,800 are useful sometimes 632 00:31:49,800 --> 00:31:53,140 and why we would use it as opposed to a while loop. 633 00:31:53,140 --> 00:31:56,450 >> Are there--do you guys have any suggestions 634 00:31:56,450 --> 00:31:59,090 for why a do-while loop might be more useful? 635 00:31:59,090 --> 00:32:02,560 >>Well, I think do-while loops is if you want user input, 636 00:32:02,560 --> 00:32:05,020 because that way you can let them 637 00:32:05,020 --> 00:32:07,780 put something in once; if it's correct, 638 00:32:07,780 --> 00:32:09,300 we can move on to the rest of the program, 639 00:32:09,300 --> 00:32:11,250 but if it isn't correct, then you keep on prompting the user 640 00:32:11,250 --> 00:32:13,250 until you put the right input. 641 00:32:13,250 --> 00:32:14,260 Yeah, exactly--exactly, 642 00:32:14,260 --> 00:32:16,840 so a do-while loop is useful 643 00:32:16,840 --> 00:32:18,930 because when prompting for user input 644 00:32:18,930 --> 00:32:23,000 because you need to get the user's input before you can actually check it. 645 00:32:23,000 --> 00:32:27,330 So in pset 1, in pset 2, 646 00:32:27,330 --> 00:32:29,650 where you're prompting for user input a lot, 647 00:32:29,650 --> 00:32:31,200 that's a handy thing to use 648 00:32:31,200 --> 00:32:33,740 as opposed to just a simple while loop. 649 00:32:33,740 --> 00:32:36,780 If you use a while loop, you can do the same thing-- 650 00:32:36,780 --> 00:32:38,750 you can do the same thing with a while loop 651 00:32:38,750 --> 00:32:40,580 as you do with a do-while loop. 652 00:32:40,580 --> 00:32:43,380 You just have to kind of have this duplicate process 653 00:32:43,380 --> 00:32:47,010 where you get the input first and then start your while loop. 654 00:32:47,010 --> 00:32:49,800 Whereas a do-while loop, you just have to write it once 655 00:32:49,800 --> 00:32:52,430 and it is a lot cleaner. 656 00:32:52,430 --> 00:32:54,650 >> All right. 657 00:32:55,970 --> 00:32:57,330 Any questions? 658 00:32:59,350 --> 00:33:01,380 Make sense? 659 00:33:01,380 --> 00:33:06,200 >>The return 0--that's just kind of a stall or is it--? 660 00:33:06,200 --> 00:33:08,960 Yeah, so we can-- 661 00:33:08,960 --> 00:33:11,960 well, we'll cover this a little bit more later 662 00:33:11,960 --> 00:33:13,290 but it turns out that 663 00:33:13,290 --> 00:33:15,720 this return 0 that we put 664 00:33:15,720 --> 00:33:19,090 at the end of main all the time-- 665 00:33:19,090 --> 00:33:21,980 you can kinda just take it as something we do 666 00:33:21,980 --> 00:33:24,930 for the time being. 667 00:33:24,930 --> 00:33:28,860 What we've started to talk about with functions 668 00:33:28,860 --> 00:33:31,600 kind of at the end of last week and then again this week 669 00:33:31,600 --> 00:33:33,550 is that a function 670 00:33:33,550 --> 00:33:37,680 takes inputs which are these things within the parentheses; 671 00:33:37,680 --> 00:33:41,690 these are 2 inputs, our arguments or parameters, 672 00:33:41,690 --> 00:33:44,540 what they're also called to our main function, 673 00:33:44,540 --> 00:33:48,510 and then a function produces an output 674 00:33:48,510 --> 00:33:51,290 which we call a return value. 675 00:33:51,290 --> 00:33:56,260 And the return value in the case of main is this int, 676 00:33:56,260 --> 00:33:58,320 this integer, 677 00:33:58,320 --> 00:34:01,160 and the way you tell a function, "Hey, this is what your output 678 00:34:01,160 --> 00:34:05,840 is going to be," is with this word "return", 679 00:34:05,840 --> 00:34:07,960 that keyword, rather, 680 00:34:07,960 --> 00:34:11,000 and so main returning 0 681 00:34:11,000 --> 00:34:13,389 or main outputting 0 682 00:34:13,389 --> 00:34:15,139 is standard in C programs 683 00:34:15,139 --> 00:34:18,110 because returning 0 from main means that 684 00:34:18,110 --> 00:34:21,600 "Hey, everything executed with my program just fine." 685 00:34:21,600 --> 00:34:24,639 If we were to put something other than 0, 686 00:34:24,639 --> 00:34:27,440 like 1, for example, 687 00:34:27,440 --> 00:34:31,080 that's used to tell whoever ran this program 688 00:34:31,080 --> 00:34:33,540 that something went wrong. 689 00:34:35,739 --> 00:34:39,989 So as we get more involved with stuff over the semester, 690 00:34:39,989 --> 00:34:42,670 if you're ever needing to say, 691 00:34:42,670 --> 00:34:46,650 "Oops, you know, something went wrong with this program," 692 00:34:46,650 --> 00:34:49,409 rather than having my program crash, I want to notify 693 00:34:49,409 --> 00:34:52,310 the runner of my program that something went wrong, 694 00:34:52,310 --> 00:34:54,530 you can use return value , 695 00:34:54,530 --> 00:34:57,230 as we call it, that's not 0. 696 00:35:00,180 --> 00:35:02,240 >> So we'll cover that a little bit more 697 00:35:02,240 --> 00:35:05,480 once we get into functions. 698 00:35:05,480 --> 00:35:09,030 That sort of makes sense? 699 00:35:09,030 --> 00:35:11,240 Okay. 700 00:35:11,240 --> 00:35:13,530 Functions are kind of bizarre-- 701 00:35:13,530 --> 00:35:17,270 I think for people who have lots of math background, 702 00:35:17,270 --> 00:35:20,100 it comes a little more easily, 703 00:35:20,100 --> 00:35:22,540 especially if you have a little bit more abstract math background 704 00:35:22,540 --> 00:35:24,240 and you're used to talking about functions 705 00:35:24,240 --> 00:35:27,370 and all that kind of stuff. 706 00:35:28,590 --> 00:35:30,610 But we'll try and cover them 707 00:35:30,610 --> 00:35:33,280 and go nice and slow as they come up. 708 00:35:34,730 --> 00:35:37,870 Likewise, this whole int argc, 709 00:35:37,870 --> 00:35:41,090 char* argv[], 710 00:35:41,090 --> 00:35:44,960 for right now, you can also take that as boiler plate. 711 00:35:46,650 --> 00:35:49,050 We've started to cover those a little bit in lecture 712 00:35:49,050 --> 00:35:53,060 when we talk about arguments that get passed into programs on the command line, 713 00:35:53,060 --> 00:35:55,700 but in your problem sets and your programs, 714 00:35:55,700 --> 00:35:58,760 it's totally fine just to write void and keep it simple. 715 00:35:58,760 --> 00:36:01,490 And what this void means is that 716 00:36:01,490 --> 00:36:04,630 "Hey, main is not going to accept any inputs; 717 00:36:04,630 --> 00:36:07,440 this is just a program we're gonna just run. 718 00:36:07,440 --> 00:36:09,320 We're not gonna give it any extra information 719 00:36:09,320 --> 00:36:12,420 every time we start it up running." 720 00:36:14,730 --> 00:36:16,600 So for now, you can do this; 721 00:36:16,600 --> 00:36:19,710 you can also even omit the return 0 722 00:36:19,710 --> 00:36:22,650 at the end of your programs and they'll run just fine, 723 00:36:22,650 --> 00:36:26,150 but I recommend just keeping that in. 724 00:36:26,150 --> 00:36:28,320 In a couple of weeks, we'll start to kind of 725 00:36:28,320 --> 00:36:31,340 pull back the cover a little bit and understand more about 726 00:36:31,340 --> 00:36:34,380 all those different little moving parts. 727 00:36:34,380 --> 00:36:38,160 >> All right, now let's talk about our second question here. 728 00:36:38,160 --> 00:36:43,190 So what does "undeclared identifier" usually indicate 729 00:36:43,190 --> 00:36:46,040 if outputted by Clang? 730 00:36:46,040 --> 00:36:49,290 I think we might have seen that already today. 731 00:36:51,170 --> 00:36:54,370 Can somebody give a recap of what that means? 732 00:36:54,370 --> 00:36:57,680 Let's see, Kelly--one of the Kellys. 733 00:37:02,580 --> 00:37:05,300 >>So-- 734 00:37:05,300 --> 00:37:07,080 Yeah, what does-- 735 00:37:07,080 --> 00:37:10,140 >>I think when you, like, don't declare the variables and you try to use it. 736 00:37:10,140 --> 00:37:13,310 Yeah, exactly, exactly. 737 00:37:13,310 --> 00:37:15,250 So if you ever see this output 738 00:37:15,250 --> 00:37:17,690 when you try and compile your program, 739 00:37:17,690 --> 00:37:19,470 just like Kelly said, 740 00:37:19,470 --> 00:37:24,210 it means that you used a variable that you didn't declare, 741 00:37:24,210 --> 00:37:28,460 so if I go back to our Space, 742 00:37:28,460 --> 00:37:31,150 and I do something like 743 00:37:31,150 --> 00:37:34,800 printf( 744 00:37:34,800 --> 00:37:38,260 "hello %s") 745 00:37:40,070 --> 00:37:42,440 and try and fill in that %s 746 00:37:42,440 --> 00:37:45,640 with a variable s that I haven't declared anywhere 747 00:37:45,640 --> 00:37:48,330 and I try to run it, 748 00:37:48,330 --> 00:37:51,330 you'll see what I get down at this bottom right here: 749 00:37:51,330 --> 00:37:54,010 "use of undeclared identifier 's'"; 750 00:37:54,010 --> 00:37:57,710 that just means you didn't declare your variable. 751 00:37:57,710 --> 00:38:00,750 Same thing happens if 752 00:38:00,750 --> 00:38:05,240 you have x and you try and store 7 in it 753 00:38:05,240 --> 00:38:08,720 and I didn't declare X. 754 00:38:08,720 --> 00:38:10,200 I get that same error: 755 00:38:10,200 --> 00:38:13,480 "use of undeclared identifier 'x'". 756 00:38:16,390 --> 00:38:18,250 >> All right, 757 00:38:18,250 --> 00:38:21,720 so the Caesar Cipher, not being very secure, 758 00:38:21,720 --> 00:38:24,590 I'll let you guys watch this short, kind of understand why, 759 00:38:24,590 --> 00:38:27,590 and hopefully after doing this week's problem set 760 00:38:27,590 --> 00:38:29,270 where you actually write a Caesar Cipher, you'll understand a little bit more 761 00:38:29,270 --> 00:38:32,600 about what it means to be a secure and insecure-- 762 00:38:36,180 --> 00:38:38,960 or not secure, I guess--insecure is slightly different. 763 00:38:40,560 --> 00:38:43,140 The other thing was we have these other questions here 764 00:38:43,140 --> 00:38:45,470 about functions 765 00:38:45,470 --> 00:38:47,330 and what a function is, 766 00:38:47,330 --> 00:38:52,820 and we kind of talked about that briefly when we talked about return values; 767 00:38:52,820 --> 00:38:54,560 for somebody who's been to lecture, 768 00:38:54,560 --> 00:38:57,790 can you recap who's been to lecture today? 769 00:38:57,790 --> 00:38:59,670 All right. 770 00:39:01,210 --> 00:39:04,320 Let's see; let's have--can somebody tell us what a function is? 771 00:39:04,320 --> 00:39:06,060 Kelly? 772 00:39:07,800 --> 00:39:11,260 >> >>Basically something you draw upon something that's written in the library that's 773 00:39:11,260 --> 00:39:14,460 more complicated over there, and then you just write a short form of it, 774 00:39:14,460 --> 00:39:17,360 so like you're referring back to that library, using 775 00:39:17,360 --> 00:39:19,650 all that long code and [inaudible]. 776 00:39:19,650 --> 00:39:22,680 Yeah, exactly, and then you wanna add something to that? 777 00:39:22,680 --> 00:39:27,050 >>Yeah, I think about it as similar to the concept of a formula in math 778 00:39:27,050 --> 00:39:30,130 where you just write a formula and then you can plug variables in 779 00:39:30,130 --> 00:39:32,660 and that's what a function is [inaudible]. 780 00:39:32,660 --> 00:39:35,390 >> Yeah, so we've got a couple of answers here 781 00:39:35,390 --> 00:39:38,080 that are both really good and right on. 782 00:39:38,080 --> 00:39:43,040 There's referring to code that's been written in the library somewhere 783 00:39:43,040 --> 00:39:45,390 that is more complicated 784 00:39:45,390 --> 00:39:47,130 and then you can draw upon that code 785 00:39:47,130 --> 00:39:50,480 and actually use it by just writing the short form of it-- 786 00:39:50,480 --> 00:39:54,060 or, rather, its name-- 787 00:39:54,060 --> 00:39:57,590 and then there is also this comparison to a formula in math 788 00:39:57,590 --> 00:40:00,400 or physics, all that kind of stuff 789 00:40:00,400 --> 00:40:02,980 where you have a formula 790 00:40:02,980 --> 00:40:05,690 and you can then plug in 791 00:40:05,690 --> 00:40:08,100 the variables to that formula 792 00:40:08,100 --> 00:40:11,130 to then get the result of the formula, 793 00:40:11,130 --> 00:40:14,580 so you think about it as like the quadratic equation. 794 00:40:14,580 --> 00:40:17,690 Imagine if rather than having to learn about 795 00:40:17,690 --> 00:40:21,310 what the quadratic formula is in math class, 796 00:40:21,310 --> 00:40:24,610 you just had a little calculator program that did it for you. 797 00:40:24,610 --> 00:40:28,930 Did anybody use calculator programs in math classes in high school? 798 00:40:28,930 --> 00:40:31,020 Yeah, physics classes? 799 00:40:31,020 --> 00:40:34,670 Where you can just say, "Hey, quadratic formula. 800 00:40:34,670 --> 00:40:37,740 These are the variables that I want to input. 801 00:40:37,740 --> 00:40:40,250 Now give me the answer." 802 00:40:40,250 --> 00:40:42,170 That's kind of what a function is. 803 00:40:42,170 --> 00:40:44,850 It's a way for us to 804 00:40:44,850 --> 00:40:47,720 kind of write like a small little sub program 805 00:40:47,720 --> 00:40:51,700 within our larger program 806 00:40:51,700 --> 00:40:55,090 and use it over and over again 807 00:40:55,090 --> 00:40:58,380 so that we don't have to copy and paste code, 808 00:40:58,380 --> 00:41:04,220 we can give it different inputs and get different outputs, 809 00:41:04,220 --> 00:41:07,090 it's super handy 810 00:41:07,090 --> 00:41:11,420 when you're trying to write different programs at different times. 811 00:41:11,420 --> 00:41:14,580 >> So for example, a function that we've been using over and over and over again 812 00:41:14,580 --> 00:41:17,310 throughout the course is printf. 813 00:41:17,310 --> 00:41:19,680 Printf is itself a function 814 00:41:19,680 --> 00:41:23,360 and it turns out that it's actually a pretty long and complicated, 815 00:41:23,360 --> 00:41:26,420 nasty looking, function to write. 816 00:41:26,420 --> 00:41:29,590 So as simple as it seems to us 817 00:41:29,590 --> 00:41:31,810 to print stuff out to the screen, 818 00:41:31,810 --> 00:41:34,820 because we all just did that at the beginning of section today, 819 00:41:34,820 --> 00:41:36,870 it turns out that it's actually, you know, 820 00:41:36,870 --> 00:41:39,900 a good couple of hundred lines of code. 821 00:41:42,030 --> 00:41:45,620 It's not something that I would ever want to write, for sure, 822 00:41:45,620 --> 00:41:47,480 but we get to use it every day; that's kind of cool, 823 00:41:47,480 --> 00:41:49,020 so we get to share it with each other, 824 00:41:49,020 --> 00:41:51,790 with different programs, 825 00:41:51,790 --> 00:41:54,520 so we'll be diving more into functions; 826 00:41:54,520 --> 00:41:56,090 for the time being, we're not-- 827 00:41:56,090 --> 00:41:59,150 the only function we're going to be writing is main. 828 00:41:59,150 --> 00:42:01,570 Main itself is also a function, 829 00:42:01,570 --> 00:42:04,270 and you can see it's got its output, 830 00:42:04,270 --> 00:42:06,610 its inputs like we talked about, 831 00:42:06,610 --> 00:42:08,930 and then its name, which is main. 832 00:42:08,930 --> 00:42:11,800 Main is kind of a--is a special function there. 833 00:42:11,800 --> 00:42:14,800 In your C program, there has to be a main function, 834 00:42:14,800 --> 00:42:17,900 and so for now, we've kind of omitted writing 835 00:42:17,900 --> 00:42:21,400 the prototype, or the declaration of, main. 836 00:42:21,400 --> 00:42:23,050 We've given that to you, 837 00:42:23,050 --> 00:42:25,650 and we've just had you fill in kind of the blanks. 838 00:42:25,650 --> 00:42:29,120 >> >>So for the second problem set, we don't need to 839 00:42:29,120 --> 00:42:31,350 write it in the second function? 840 00:42:31,350 --> 00:42:34,200 I don't believe so. 841 00:42:34,200 --> 00:42:37,600 I believe it's all in here. 842 00:42:37,600 --> 00:42:41,710 Yeah, so Caesar Cipher, 843 00:42:41,710 --> 00:42:45,000 you don't have to, it doesn't look like, 844 00:42:48,740 --> 00:42:53,150 and Vigenere, you don't either, 845 00:42:53,150 --> 00:42:56,400 so--and it turns out you don't ever actually have to write 846 00:42:56,400 --> 00:42:58,250 a function, right? 847 00:42:58,250 --> 00:43:01,950 You call always put all of your code within main, 848 00:43:01,950 --> 00:43:04,500 and many programmers do that. 849 00:43:04,500 --> 00:43:07,130 As we move on, 850 00:43:07,130 --> 00:43:09,480 we start introducing functions as a way 851 00:43:09,480 --> 00:43:11,530 to clean up your code, 852 00:43:11,530 --> 00:43:13,380 to make it more elegant, 853 00:43:13,380 --> 00:43:16,890 in case you have tasks--kind of like when we talked about loops, 854 00:43:16,890 --> 00:43:20,400 how sometimes there are things that you want to do again and again and again, 855 00:43:20,400 --> 00:43:23,510 and you have kind of the naive way-- 856 00:43:23,510 --> 00:43:26,360 or what we often call the "straw man" solution, 857 00:43:26,360 --> 00:43:29,390 is to just copy and paste over and over again-- 858 00:43:29,390 --> 00:43:31,000 you can do the same thing with functions where you just copy and paste 859 00:43:31,000 --> 00:43:33,360 the same code over and over again, 860 00:43:33,360 --> 00:43:35,750 but that bloats your code-- 861 00:43:35,750 --> 00:43:38,550 it ends up making your code really long-- 862 00:43:38,550 --> 00:43:41,550 and it also gets a little confusing, 863 00:43:41,550 --> 00:43:43,470 and then it means that you aren't able to like-- 864 00:43:43,470 --> 00:43:45,250 if you ever want to use that code again in another program, 865 00:43:45,250 --> 00:43:48,610 you're also stuck copying and pasting it again, 866 00:43:48,610 --> 00:43:51,170 so one of the things we'll talk about is 867 00:43:51,170 --> 00:43:54,960 kind of design and style of your programs, 868 00:43:54,960 --> 00:43:57,010 and so when it makes sense to kind of 869 00:43:57,010 --> 00:43:59,800 package something up and put it as a separate function, 870 00:43:59,800 --> 00:44:02,080 we encourage you to do that, 871 00:44:02,080 --> 00:44:04,700 and that is something that, again, 872 00:44:04,700 --> 00:44:07,790 there's no hard and fast rule. 873 00:44:07,790 --> 00:44:11,540 We rarely say that you must write a function called this, 874 00:44:12,870 --> 00:44:16,320 but it's more of one of those things you just develop over time 875 00:44:16,320 --> 00:44:19,470 as you write more and more programs. 876 00:44:21,940 --> 00:44:24,790 >> Any more questions? 877 00:44:26,340 --> 00:44:30,610 Okay, so let's now 878 00:44:30,610 --> 00:44:34,750 go back up to this guy right here, 879 00:44:34,750 --> 00:44:37,800 so I'm still in this section of questions. 880 00:44:37,800 --> 00:44:39,900 We've talked about all these different ones, 881 00:44:39,900 --> 00:44:42,050 what's a function, why to write functions, 882 00:44:42,050 --> 00:44:44,700 when you can just copy and paste? 883 00:44:44,700 --> 00:44:47,700 Hopefully we've convinced you that writing functions is a good thing. 884 00:44:47,700 --> 00:44:50,870 So now let's move on to this MySpace program, 885 00:44:50,870 --> 00:44:53,080 and we'll spend a little bit of time doing this. 886 00:44:54,600 --> 00:44:56,480 >> So, let's see, 887 00:44:56,480 --> 00:45:00,230 can I have somebody read this for me? 888 00:45:02,640 --> 00:45:04,660 Let's have Mikhaila--will you read this for us? 889 00:45:07,660 --> 00:45:08,890 >>You mean back when MySpace-- 890 00:45:08,890 --> 00:45:10,920 Back when MySpace was cool. 891 00:45:10,920 --> 00:45:12,070 >>"Back when MySpace was cool, 892 00:45:12,070 --> 00:45:14,900 it was all the rage to TyPe LiKe ThIs. 893 00:45:14,900 --> 00:45:17,320 Maybe it still is? I'm not really sure. 894 00:45:17,320 --> 00:45:19,990 In any case, using the CS50 Appliance, 895 00:45:19,990 --> 00:45:22,600 CS50 Run, or CS50 Spaces, 896 00:45:22,600 --> 00:45:25,370 write a program that prompts the user for a message 897 00:45:25,370 --> 00:45:29,350 and then outputs the message with its first letter capitalized, 898 00:45:29,350 --> 00:45:31,800 with all the other letters in alternating case, 899 00:45:31,800 --> 00:45:34,010 as per the sample output below, 900 00:45:34,010 --> 00:45:37,060 wherein boldfaced text represents some user's input. 901 00:45:37,060 --> 00:45:39,800 For simplicity, you may assume that the user 902 00:45:39,800 --> 00:45:42,810 will only input lowercase letters and spaces. 903 00:45:42,810 --> 00:45:45,900 Consider this problem an opportunity to practice; 904 00:45:45,900 --> 00:45:48,640 you won't be asked to submit this program." 905 00:45:48,640 --> 00:45:51,640 >> Awesome, thank you. 906 00:45:51,640 --> 00:45:54,750 So as Mikhaila said, 907 00:45:54,750 --> 00:45:58,850 we are going to write a program that MySpaces your code. 908 00:45:58,850 --> 00:46:02,230 So did any of you guys use MySpace? 909 00:46:02,230 --> 00:46:04,100 Any of you have MySpace accounts? 910 00:46:04,100 --> 00:46:05,920 Oh, yeah, yeah! 911 00:46:05,920 --> 00:46:08,300 It was pretty cool. 912 00:46:08,300 --> 00:46:09,950 [laughter] 913 00:46:09,950 --> 00:46:12,060 I know. 914 00:46:12,060 --> 00:46:14,500 Dating myself. 915 00:46:15,490 --> 00:46:18,300 So what we're trying to do is write a program 916 00:46:18,300 --> 00:46:21,110 that goes through 917 00:46:21,110 --> 00:46:24,490 and capitalizes the first letter 918 00:46:24,490 --> 00:46:27,140 and then every letter thereafter-- 919 00:46:27,140 --> 00:46:29,350 or every other letter thereafter, I'm sorry. 920 00:46:29,350 --> 00:46:32,910 >> So--and you can see in this example, 921 00:46:32,910 --> 00:46:35,260 this is how we run the program, 922 00:46:35,260 --> 00:46:38,780 and so this is just saying, "We're running a program called a.out. 923 00:46:41,490 --> 00:46:44,630 The input that we're going to giving it 924 00:46:44,630 --> 00:46:47,090 is going to be prompted. 925 00:46:47,090 --> 00:46:51,150 We're not gonna supply the input on the command line." 926 00:46:51,150 --> 00:46:54,100 If you've been following all the lectures 927 00:46:54,100 --> 00:46:56,710 and hearing about command line arguments, 928 00:46:57,810 --> 00:47:00,080 that's kind of where those--that funky syntax 929 00:47:00,080 --> 00:47:04,530 at main comes in with the int argc, all that stuff, 930 00:47:04,530 --> 00:47:07,630 so here we're not using that; we're not dealing with any of that. 931 00:47:09,400 --> 00:47:12,650 We're just gonna get this typed in by the user, 932 00:47:12,650 --> 00:47:15,700 and then this second line with the funky capitalization 933 00:47:15,700 --> 00:47:18,550 is what we want our program to output. 934 00:47:19,790 --> 00:47:22,860 >> So rather than using the Appliance or Run, we'll use Spaces, 935 00:47:22,860 --> 00:47:25,360 because we've got Spaces, 936 00:47:25,360 --> 00:47:28,640 and so if you guys would 937 00:47:28,640 --> 00:47:32,380 get your code to look something like this, 938 00:47:32,380 --> 00:47:35,930 so I'm going to save this revision 939 00:47:35,930 --> 00:47:38,390 just so you've got the boiler plate right here 940 00:47:38,390 --> 00:47:42,840 that looks like this, int main (void), nothing in it. 941 00:47:44,480 --> 00:47:48,460 Actually, what we'll do is we'll just delete everything all together. 942 00:47:48,460 --> 00:47:50,330 We want a blank slate, 943 00:47:50,330 --> 00:47:52,470 because we're not gonna start writing code; 944 00:47:52,470 --> 00:47:55,380 rather, we're gonna try writing some pseudocode 945 00:47:55,380 --> 00:47:58,580 which really just means we're going to write something that's not code 946 00:47:58,580 --> 00:48:01,010 that kind of describes what we want to do, 947 00:48:01,010 --> 00:48:05,180 kinda like David did on the first day of lecture, 948 00:48:05,180 --> 00:48:08,310 or I guess that was the second day of lecture when he did the whole socks demo. 949 00:48:08,310 --> 00:48:10,990 You guys remember that--seeing the socks? 950 00:48:10,990 --> 00:48:13,300 Joseph was up there kind of typing out in English? 951 00:48:13,300 --> 00:48:16,300 So we're gonna do the same thing. 952 00:48:16,300 --> 00:48:18,380 >> So when I start writing pseudocode, 953 00:48:18,380 --> 00:48:22,640 I like to start by looking at what the input is going to be. 954 00:48:22,640 --> 00:48:24,950 And I give myself a sample case, 955 00:48:24,950 --> 00:48:28,780 so like "thanks for the add!" 956 00:48:35,030 --> 00:48:38,010 And then I also write what I want the output to be, 957 00:48:38,010 --> 00:48:42,210 which in this case is something like this. 958 00:48:53,790 --> 00:48:56,050 And that kind of gives me a good, like, starting place to think about, 959 00:48:56,050 --> 00:48:59,730 "Okay, I want to take line 1 960 00:48:59,730 --> 00:49:03,560 and I want to transform it into line 2," 961 00:49:03,560 --> 00:49:06,570 and that kind of helps guide my thinking. 962 00:49:06,570 --> 00:49:09,990 This is a good place to start. 963 00:49:09,990 --> 00:49:12,190 Another way to think about it, too, as we kinda move on 964 00:49:12,190 --> 00:49:15,020 and as you do this in your own programs, is to 965 00:49:15,020 --> 00:49:18,530 think about kind of like what are the edge cases to this? 966 00:49:18,530 --> 00:49:20,740 What are kind of the weird scenarios? 967 00:49:20,740 --> 00:49:23,660 What are the weird inputs that we might get? 968 00:49:23,660 --> 00:49:26,550 And what should those outputs look like? 969 00:49:26,550 --> 00:49:29,670 For example, one weird input 970 00:49:29,670 --> 00:49:33,200 would be this one, 971 00:49:33,200 --> 00:49:35,630 creating absolutely nothing, 972 00:49:35,630 --> 00:49:37,840 like what should we output in that case? 973 00:49:37,840 --> 00:49:40,530 Something we want to think about, too. 974 00:49:40,530 --> 00:49:44,450 >> So just as a question, 975 00:49:44,450 --> 00:49:46,870 what might we want to output? 976 00:49:46,870 --> 00:49:50,740 If we don't get any input at all, what should our program be outputting? 977 00:49:53,740 --> 00:49:54,860 >>Nothing. 978 00:49:54,860 --> 00:49:57,340 Nothing? Sounds great. 979 00:49:57,340 --> 00:49:59,280 So we want to make sure that we're not writing anything out 980 00:49:59,280 --> 00:50:01,500 if we don't get anything in. 981 00:50:01,500 --> 00:50:04,320 Cool. Okay. 982 00:50:04,320 --> 00:50:07,370 So 983 00:50:07,370 --> 00:50:09,380 I'm going to call on a couple of people as we go through 984 00:50:09,380 --> 00:50:12,790 and just figure out kind of like what the general algorithm is 985 00:50:12,790 --> 00:50:15,250 and we'll kind start working through this together. 986 00:50:16,240 --> 00:50:19,710 >> So, let's see. Missy. 987 00:50:19,710 --> 00:50:21,220 How do you think we'd start? 988 00:50:21,220 --> 00:50:24,830 What would be a good place to start here 989 00:50:24,830 --> 00:50:27,900 if we want to transform, say, line 1 990 00:50:27,900 --> 00:50:29,440 into line 2? 991 00:50:29,440 --> 00:50:32,650 Can you kind of just explain to me, in English, what you're thinking? 992 00:50:32,650 --> 00:50:34,770 Don't even worry about code. 993 00:50:34,770 --> 00:50:38,250 >>First thing I would do would be to 994 00:50:38,250 --> 00:50:41,170 kind of direct the user to tell them what you want. 995 00:50:41,170 --> 00:50:44,730 Okay, so, say, 996 00:50:44,730 --> 00:50:47,450 "Hey, user. I want 997 00:50:47,450 --> 00:50:50,560 a string"--awesome. 998 00:50:50,560 --> 00:50:52,990 So let's write this down. 999 00:50:52,990 --> 00:50:56,140 Prompt the user for a string. 1000 00:50:56,140 --> 00:50:59,270 That sounds like a good start. 1001 00:51:03,920 --> 00:51:06,460 All right. 1002 00:51:06,460 --> 00:51:09,750 Let's ask Kelly Ren this time. 1003 00:51:09,750 --> 00:51:11,580 What would we then want to do next? 1004 00:51:11,580 --> 00:51:14,220 What's kind of our next--? 1005 00:51:14,220 --> 00:51:18,080 >>Collect their input and store it as a variable. 1006 00:51:18,080 --> 00:51:20,470 Great. 1007 00:51:20,470 --> 00:51:24,280 So collect the string, 1008 00:51:25,450 --> 00:51:27,880 store it in a variable. 1009 00:51:34,840 --> 00:51:36,460 So is there anything special we want to do here? 1010 00:51:36,460 --> 00:51:39,700 Do we want to check their input 1011 00:51:39,700 --> 00:51:42,260 before--? 1012 00:51:42,260 --> 00:51:43,790 >>Yeah. 1013 00:51:43,790 --> 00:51:46,330 What might we want to check for? 1014 00:51:46,330 --> 00:51:48,980 >>Well, you said we don't have to check if it's a number, right? 1015 00:51:48,980 --> 00:51:52,400 Because it'll assume that it's lowercase? 1016 00:51:52,400 --> 00:51:55,650 Yeah. 1017 00:51:56,380 --> 00:51:58,920 >> So it turns out that--yeah. 1018 00:51:58,920 --> 00:52:01,850 So getString is guaranteed to return us a string. 1019 00:52:01,850 --> 00:52:05,050 It's not going to return a number to us or anything like that. 1020 00:52:05,050 --> 00:52:09,140 There might be digit characters within that string, though. 1021 00:52:09,140 --> 00:52:13,460 And those, we can ignore as we did the capitalization. 1022 00:52:13,460 --> 00:52:16,620 But an empty string is totally fine, 1023 00:52:16,620 --> 00:52:19,900 so the case right here on line 4 and 5, 1024 00:52:19,900 --> 00:52:22,020 right, where there's no input, 1025 00:52:22,020 --> 00:52:25,190 and we've put nothing as our output. 1026 00:52:25,190 --> 00:52:28,730 We don't have to worry about making sure that there's something in the string 1027 00:52:28,730 --> 00:52:31,840 or that the string is at least 4 characters long; 1028 00:52:31,840 --> 00:52:34,190 anything is fine. 1029 00:52:34,190 --> 00:52:38,270 So this is contrasted, for example, with like the pennies programs that you wrote 1030 00:52:38,270 --> 00:52:42,340 where you had to make sure you were getting non-negative numbers; 1031 00:52:42,340 --> 00:52:46,430 you had to make sure that you were getting the correct number of days in a month. 1032 00:52:46,430 --> 00:52:49,630 And how are you guys doing that? 1033 00:52:49,630 --> 00:52:51,470 How would you do that test? 1034 00:52:51,470 --> 00:52:54,090 What did that require you to use? 1035 00:52:54,090 --> 00:52:55,650 >>Do-while loops. 1036 00:52:55,650 --> 00:53:00,840 Do-while loops; other kinds of loops, too, yeah. 1037 00:53:02,190 --> 00:53:04,520 So it turns out we don't have to use a loop in this case, 1038 00:53:04,520 --> 00:53:07,360 when we're prompting for a string, which is kind of nice. 1039 00:53:08,280 --> 00:53:10,620 >> All right, moving on. 1040 00:53:12,410 --> 00:53:14,270 So, Stella. 1041 00:53:14,270 --> 00:53:17,530 Can you tell us a little bit about what we should do next? 1042 00:53:17,530 --> 00:53:20,270 So now we've got the string, we've got it stored in a variable; 1043 00:53:20,270 --> 00:53:23,360 what do we need to do with the string? 1044 00:53:26,790 --> 00:53:33,530 >>We need to-- 1045 00:53:33,530 --> 00:53:36,550 identify the 2-- 1046 00:53:36,550 --> 00:53:39,290 Yeah, just say it in English, that's awesome. 1047 00:53:39,290 --> 00:53:42,670 >>Identify the [inaudible] capitals--or, uppercase. 1048 00:53:44,420 --> 00:53:48,130 Yeah, so you'd identify which ones are uppercase and lowercase. 1049 00:53:50,020 --> 00:53:54,180 We can't uppercase or lowercase them until we've identified which ones. 1050 00:54:03,790 --> 00:54:06,350 So we'll put that in here. 1051 00:54:06,350 --> 00:54:10,210 >> So, just thinking about this process, 1052 00:54:10,210 --> 00:54:13,200 how do we do this identification? 1053 00:54:13,200 --> 00:54:15,050 Do you know, Dan? 1054 00:54:15,050 --> 00:54:18,120 Kind of what we--what tool we'd have to use out of our toolbox? 1055 00:54:18,120 --> 00:54:22,500 >>I'd put it in somehow in like an array or each line 1056 00:54:22,500 --> 00:54:24,240 is its own little box, 1057 00:54:24,240 --> 00:54:27,240 and then uppercase it from there or not. 1058 00:54:27,240 --> 00:54:30,440 Okay, so one tool we can use is an array. 1059 00:54:30,440 --> 00:54:33,760 What do we have to look at in the string, 1060 00:54:33,760 --> 00:54:36,680 though, to figure out--we had to look at the characters. 1061 00:54:36,680 --> 00:54:40,890 >>So you've got to look at the length of the string? 1062 00:54:40,890 --> 00:54:43,880 Yeah, so part of that is getting the length of the string. 1063 00:54:50,450 --> 00:54:54,150 Just out of curiosity, what do we use to get the length of a string? 1064 00:54:54,150 --> 00:54:58,080 >>strlen? 1065 00:54:58,080 --> 00:55:02,870 Yeah, strlen; we call this "strlen" 1066 00:55:04,180 --> 00:55:08,620 All of these string functions, so strlen is a function, 1067 00:55:08,620 --> 00:55:10,670 and it's super handy, because we can use it to just say, 1068 00:55:10,670 --> 00:55:13,430 Hey, strlen: Here's a string, tell me how long it is. 1069 00:55:13,430 --> 00:55:17,350 In the lecture today, David talked a lot about how strlen is implemented, 1070 00:55:17,350 --> 00:55:20,370 what's actually going on behind the scenes. 1071 00:55:20,370 --> 00:55:25,450 But this is one of the very super handy ones. 1072 00:55:25,450 --> 00:55:29,170 There are other ones that are also in the string library, 1073 00:55:29,170 --> 00:55:34,690 such as strcmp, which is spelled "strcmp", 1074 00:55:34,690 --> 00:55:38,010 which is used to compare strings to each other. 1075 00:55:41,220 --> 00:55:45,020 Other ones you will use like strstr, 1076 00:55:45,020 --> 00:55:48,770 which is good for finding sub strings and so on. 1077 00:55:48,770 --> 00:55:53,380 For the most part, they all start with str and then go on to be something else. 1078 00:55:53,380 --> 00:55:57,080 The reason they don't actually spell them like string and so on 1079 00:55:57,080 --> 00:56:00,260 is because back in like the early days of C, 1080 00:56:00,260 --> 00:56:04,100 when people were first writing and all that, 1081 00:56:04,100 --> 00:56:07,580 they had limitations on the number of characters that you could use 1082 00:56:07,580 --> 00:56:09,950 for function names and variable names, 1083 00:56:09,950 --> 00:56:12,620 so everything is like super condensed. 1084 00:56:12,620 --> 00:56:15,750 But anytime you see something that begins with str, 1085 00:56:15,750 --> 00:56:18,600 that typically means this has something to do with a string. 1086 00:56:20,990 --> 00:56:23,350 >> Okay, so you've got to get the length of the string, 1087 00:56:23,350 --> 00:56:27,110 and then, what do we do with the characters? 1088 00:56:27,110 --> 00:56:29,020 Do you know, Sam? 1089 00:56:29,020 --> 00:56:32,410 >>I have no idea; I was trying to figure it out. 1090 00:56:32,410 --> 00:56:35,410 I really don't know--I wish there were some way to-- 1091 00:56:35,410 --> 00:56:38,990 I mean, I would think maybe we could do something with odds and evens, 1092 00:56:38,990 --> 00:56:40,860 but I don't know of any function like that. 1093 00:56:40,860 --> 00:56:42,240 Yeah. 1094 00:56:43,160 --> 00:56:45,200 >>But that--it wouldn't necessarily work, 1095 00:56:45,200 --> 00:56:48,690 because then you would get the spaces to be-- 1096 00:56:48,690 --> 00:56:51,130 you would have to have the space to get tips on how-- 1097 00:56:51,130 --> 00:56:54,310 Okay. 1098 00:56:54,310 --> 00:56:56,490 >>I don't know--I'm kind of lost at this point. 1099 00:56:56,490 --> 00:56:58,120 Yeah--no, no, no, these are good points. 1100 00:56:58,120 --> 00:57:00,420 So what we need to do is, like Sam said, 1101 00:57:00,420 --> 00:57:02,970 we need to track which letters are the odd letters, 1102 00:57:02,970 --> 00:57:05,540 and which ones are the even letters. 1103 00:57:06,650 --> 00:57:09,090 But we want to make sure we're not just looking 1104 00:57:09,090 --> 00:57:12,110 at the odd and even 1105 00:57:12,110 --> 00:57:14,030 indices in the string, 1106 00:57:14,030 --> 00:57:17,660 but rather looking at the odd and even letters in the string, 1107 00:57:17,660 --> 00:57:20,190 so we need to kinda have this distinction between 1108 00:57:20,190 --> 00:57:25,190 letters--like what's actually A, B, C, D, or so on, 1109 00:57:25,190 --> 00:57:29,090 and what things are not letters but still in our string. 1110 00:57:29,090 --> 00:57:32,390 For example here, like this exclamation point is not a letter, 1111 00:57:32,390 --> 00:57:36,060 but it's still part of our string; it's still a char. 1112 00:57:36,060 --> 00:57:38,820 Likewise, all of these spaces-- 1113 00:57:38,820 --> 00:57:41,710 it doesn't make sense to capitalize a space, right? 1114 00:57:41,710 --> 00:57:46,160 So we want to make sure that we somehow skip those 1115 00:57:46,160 --> 00:57:49,410 while still tracking odds and evens. 1116 00:57:50,900 --> 00:57:54,990 >> So kind of in our programming toolbox 1117 00:57:54,990 --> 00:57:57,270 so far, if you will, 1118 00:57:57,270 --> 00:58:01,310 we have a number of different tools that we've talked about. 1119 00:58:01,310 --> 00:58:04,640 So one, for example, are variables, 1120 00:58:04,640 --> 00:58:07,120 like we've talked about here. 1121 00:58:07,120 --> 00:58:10,730 Another tool we've talked about are functions. 1122 00:58:10,730 --> 00:58:15,800 So we've talked about variables in our pseudocode up here 1123 00:58:15,800 --> 00:58:19,760 as what we're going to use to store our string. 1124 00:58:19,760 --> 00:58:24,630 We've talked about functions to get the length of a string, like strlen. 1125 00:58:28,510 --> 00:58:31,170 We've been talking about something else, and we've talked about it 1126 00:58:31,170 --> 00:58:34,850 for a lot earlier in section today: loops. 1127 00:58:39,390 --> 00:58:42,030 >> What do you guys think about using a loop in this program? 1128 00:58:42,030 --> 00:58:44,110 Jared, what do you think? 1129 00:58:47,150 --> 00:58:50,030 Would we use a loop in this program? 1130 00:58:50,030 --> 00:58:52,500 >>Probably not, no, because you don't have to go through it more than once. 1131 00:58:52,500 --> 00:58:55,260 Okay, you don't have to go through what more than once? 1132 00:58:55,260 --> 00:58:57,190 >>The entire string. 1133 00:58:57,190 --> 00:59:00,660 Right, so you don't have to go through the string entire once. 1134 00:59:00,660 --> 00:59:03,330 Sorry that made absolutely no sense! 1135 00:59:03,330 --> 00:59:06,360 You don't have to go through the entire string more than once. 1136 00:59:06,360 --> 00:59:08,660 Okay. 1137 00:59:09,790 --> 00:59:11,920 What do you think? 1138 00:59:11,920 --> 00:59:14,110 >>I think that it's better to use them 1139 00:59:14,110 --> 00:59:16,640 so we can do like capitalize while 1140 00:59:16,640 --> 00:59:19,750 the location of it is [inaudible] number. 1141 00:59:19,750 --> 00:59:22,940 Great, and so capitalize what? 1142 00:59:22,940 --> 00:59:25,200 Okay, so we're saying that 1143 00:59:25,200 --> 00:59:28,130 we should use a loop because we want to 1144 00:59:28,130 --> 00:59:32,550 capitalize a letter while it's not a number. 1145 00:59:35,130 --> 00:59:37,140 So--sorry, did you want to add? 1146 00:59:37,140 --> 00:59:39,340 >>I was going to say that the way you treat the string 1147 00:59:39,340 --> 00:59:42,290 and it [inaudible] separate the [inaudible] on the characters? 1148 00:59:42,290 --> 00:59:43,710 Yeah. 1149 00:59:43,710 --> 00:59:45,850 >>And then we can assign the numbers to-- 1150 00:59:45,850 --> 00:59:47,780 or based on the location of the letters in the string? 1151 00:59:47,780 --> 00:59:49,720 Yeah. 1152 00:59:49,720 --> 00:59:53,640 >>And identify whether it's odd or even and to capitalize the letter itself 1153 00:59:53,640 --> 00:59:57,750 while the location of the letter is [inaudible]--sorry. 1154 00:59:57,750 --> 01:00:00,150 Great. 1155 01:00:00,150 --> 01:00:03,430 >> So just a recap. 1156 01:00:03,430 --> 01:00:05,840 Can you remind me of your name again--I'm sorry. 1157 01:00:05,840 --> 01:00:06,900 >>Charlotte. 1158 01:00:06,900 --> 01:00:08,490 Charlotte, okay. 1159 01:00:08,490 --> 01:00:11,130 So what Charlotte is saying, 1160 01:00:11,130 --> 01:00:14,950 which is totally right--gosh, this likes to auto indent on me-- 1161 01:00:14,950 --> 01:00:17,770 is that if we look at a string 1162 01:00:17,770 --> 01:00:19,160 such as this one at the top-- 1163 01:00:19,160 --> 01:00:21,590 "ThAnKs FoR tHe AdD"-- 1164 01:00:21,590 --> 01:00:24,790 as a sequence of characters 1165 01:00:24,790 --> 01:00:29,010 where the is at position 0, 1166 01:00:29,010 --> 01:00:32,260 the h is at position 1. 1167 01:00:32,260 --> 01:00:36,880 A is at 2, then we got 3, 4, 5; 1168 01:00:36,880 --> 01:00:40,830 the space is at position 6. 1169 01:00:42,380 --> 01:00:45,300 Whereas--what's the position of f? 1170 01:00:45,300 --> 01:00:47,610 7! 1171 01:00:47,610 --> 01:00:49,300 O? 8. 1172 01:00:49,300 --> 01:00:52,070 R? 9. 1173 01:00:52,070 --> 01:00:54,340 And then we've got 10, and then I can't really write it without it 1174 01:00:54,340 --> 01:00:57,430 kinda getting messy on the screen. 1175 01:00:57,430 --> 01:01:02,340 But this is actually how we can talk about strings in memory. 1176 01:01:02,340 --> 01:01:04,660 Is it a string 1177 01:01:04,660 --> 01:01:07,920 is a sequence of characters, 1178 01:01:07,920 --> 01:01:12,430 and when we refer to an individual character in a string, 1179 01:01:12,430 --> 01:01:15,280 we often do so by referring to that character's position 1180 01:01:15,280 --> 01:01:17,620 within the string, 1181 01:01:17,620 --> 01:01:19,910 probably the same way that we talk about September 1182 01:01:19,910 --> 01:01:22,870 being the ninth month of the year 1183 01:01:22,870 --> 01:01:27,730 or Monday being the first day of the week. 1184 01:01:29,280 --> 01:01:33,070 So this provides us a way to 1185 01:01:33,070 --> 01:01:37,030 figure out which ones are odd versus even 1186 01:01:37,030 --> 01:01:39,540 because you can see here, 1187 01:01:39,540 --> 01:01:43,640 if we match it up with this T, 1188 01:01:46,310 --> 01:01:49,360 you can see that all of the capitalized ones 1189 01:01:49,360 --> 01:01:53,040 are at even positions, 1190 01:01:53,040 --> 01:01:55,880 at least for the first word, 1191 01:01:55,880 --> 01:01:59,080 and then things get thrown off once we hit that space, 1192 01:01:59,080 --> 01:02:02,460 because we don't count this space as a letter, 1193 01:02:02,460 --> 01:02:05,150 so it's as if we started 1194 01:02:05,150 --> 01:02:08,930 and went 0, 1, 2, 3, 4, 5, 1195 01:02:08,930 --> 01:02:12,600 space, 6, 7, 8, space, 1196 01:02:12,600 --> 01:02:16,440 9, and then 10, 11, 12, and so on. 1197 01:02:19,300 --> 01:02:22,910 >> Cool, so we definitely will want to use some sort of loop 1198 01:02:22,910 --> 01:02:27,760 to go over each of the characters in turn. 1199 01:02:31,330 --> 01:02:34,580 So at this point, 1200 01:02:36,490 --> 01:02:38,360 we'll come back to our old pseudocode here. 1201 01:02:40,970 --> 01:02:42,650 As part of this identification of which ones 1202 01:02:42,650 --> 01:02:45,350 are uppercase and which ones are lowercase, 1203 01:02:45,350 --> 01:02:48,340 we'll want to do this in some kind of loop. 1204 01:02:51,080 --> 01:02:53,390 Loop, colon. 1205 01:02:55,070 --> 01:03:01,220 What kind of loop, out of the types of loops that we have, 1206 01:03:01,220 --> 01:03:03,670 seems to be a good one to use in this case? 1207 01:03:03,670 --> 01:03:05,610 >>I was thinking the for loop. 1208 01:03:05,610 --> 01:03:07,500 And why are you thinking of for loop? 1209 01:03:07,500 --> 01:03:10,000 >>Because it starts at zero, and then 1210 01:03:10,000 --> 01:03:13,000 when we reach the length of the string, 1211 01:03:13,000 --> 01:03:16,170 and then each iteration you can [inaudible], too? 1212 01:03:16,170 --> 01:03:18,260 Yeah, exactly. 1213 01:03:18,260 --> 01:03:21,490 So, out of the different choices of loops, 1214 01:03:21,490 --> 01:03:25,350 we have for loops, while loops, and do-while loops. 1215 01:03:25,350 --> 01:03:28,550 Where a for loop is particularly useful 1216 01:03:28,550 --> 01:03:32,120 is when you want to know 1217 01:03:32,120 --> 01:03:34,940 kind of where you're at in the loop, 1218 01:03:34,940 --> 01:03:38,520 how many times the loop has been executed so far. 1219 01:03:38,520 --> 01:03:42,490 Which is exactly what we want to know in this situation, 1220 01:03:42,490 --> 01:03:44,640 like Kelly said, 1221 01:03:44,640 --> 01:03:47,400 based off of Charlotte's earlier comment that we want to know the position 1222 01:03:47,400 --> 01:03:49,900 of the character within the string, 1223 01:03:49,900 --> 01:03:51,750 and the for loop will give us just that, 1224 01:03:51,750 --> 01:03:54,680 because if we start kind of looking back at our string, 1225 01:03:54,680 --> 01:03:57,140 if we start our for loop at 0, 1226 01:03:57,140 --> 01:04:00,350 and we go up through the length of the entire string, 1227 01:04:00,350 --> 01:04:04,740 then we can associate the letter T 1228 01:04:04,740 --> 01:04:08,110 with the 0 in our for loop, 1229 01:04:09,950 --> 01:04:13,080 letter H with the 1 in our for loop, and so on, 1230 01:04:13,080 --> 01:04:16,600 going all the way through the end of the string. 1231 01:04:18,810 --> 01:04:21,890 >> So let's start coding this just a little bit 1232 01:04:21,890 --> 01:04:24,930 just so you guys get a feel for what this looks like. 1233 01:04:24,930 --> 01:04:27,980 I'm going to save this, 1234 01:04:27,980 --> 01:04:31,230 so we've got our pseudo code in here, 1235 01:04:33,490 --> 01:04:38,120 and I'm going to delete everything. 1236 01:04:38,120 --> 01:04:42,210 So I have a blank slate. 1237 01:04:42,210 --> 01:04:45,910 I'm gonna start by writing int main (void) 1238 01:04:45,910 --> 01:04:48,440 { 1239 01:04:48,440 --> 01:04:51,380 I'm gonna put the return 0 in 1240 01:04:51,380 --> 01:04:53,320 and then the }. 1241 01:04:53,320 --> 01:04:56,130 So usually when I start to program, this is what I start with. 1242 01:04:56,130 --> 01:04:59,370 I'll save it so that you guys have it. 1243 01:05:06,290 --> 01:05:08,440 So one of the first things we talked about was 1244 01:05:08,440 --> 01:05:10,920 getting input from the user 1245 01:05:10,920 --> 01:05:13,740 and storing it in a string. 1246 01:05:13,740 --> 01:05:16,420 How do we get input from a user? 1247 01:05:16,420 --> 01:05:19,640 >>GetString. 1248 01:05:19,640 --> 01:05:21,870 GetString, all right. 1249 01:05:23,150 --> 01:05:26,820 So if I type GetString() 1250 01:05:26,820 --> 01:05:32,150 and a semicolon. 1251 01:05:32,150 --> 01:05:36,630 Let's try running this; will this run, first of all? 1252 01:05:36,630 --> 01:05:38,430 And why won't this run? 1253 01:05:38,430 --> 01:05:40,090 >>You have to declare the variable. 1254 01:05:40,090 --> 01:05:43,040 I have to declare a variable. Okay. 1255 01:05:43,040 --> 01:05:45,700 >> So how do I do that? 1256 01:05:45,700 --> 01:05:48,400 Do you mind writing something, sharing it? 1257 01:05:48,400 --> 01:05:51,270 And then we can pull it up. 1258 01:05:51,270 --> 01:05:54,630 Let's see, Dan, right, okay. 1259 01:05:54,630 --> 01:05:59,100 Or Daniel. Copy that in, string input = GetString(). 1260 01:05:59,100 --> 01:06:01,340 Awesome. 1261 01:06:01,340 --> 01:06:02,890 Will this run? 1262 01:06:02,890 --> 01:06:05,210 Let's try it. 1263 01:06:05,210 --> 01:06:08,890 Hit run, uploading source code, building, an error occurred. 1264 01:06:08,890 --> 01:06:11,040 Okay. 1265 01:06:11,040 --> 01:06:14,950 So whats going on here? 1266 01:06:14,950 --> 01:06:18,700 Use of undeclared identifier 'string'. 1267 01:06:18,700 --> 01:06:22,840 >>We have to include the libraries? 1268 01:06:22,840 --> 01:06:25,130 Yeah, so Dan's saying we have to include the libraries. 1269 01:06:25,130 --> 01:06:28,270 Is that what you were going to say, Stella? Awesome. 1270 01:06:29,450 --> 01:06:31,430 That's awesome, so Stella, can you 1271 01:06:31,430 --> 01:06:34,800 copy this code out of Daniel's Space? 1272 01:06:34,800 --> 01:06:37,580 If you click on Daniel Roland Ki, copy it, 1273 01:06:37,580 --> 01:06:41,360 and then will you put the include line in, and then save it out? 1274 01:06:46,580 --> 01:06:49,360 Let me know when you're ready. 1275 01:06:57,160 --> 01:06:59,490 So this include syntax is also kind of funky; 1276 01:06:59,490 --> 01:07:02,210 there's like that hash tag, 1277 01:07:02,210 --> 01:07:05,760 and there's the include, and then there are these angle brackets. 1278 01:07:12,490 --> 01:07:15,360 Do you remember how this works, or should I put it up on this code right here? 1279 01:07:15,360 --> 01:07:17,850 >>I'm good. 1280 01:07:17,850 --> 01:07:19,810 Okay. 1281 01:07:19,810 --> 01:07:23,260 >>I printed it--I wasn't sure which library, though. 1282 01:07:23,260 --> 01:07:25,570 Oh, okay--yeah, so it's the CS50 library. 1283 01:07:25,570 --> 01:07:27,660 >>Oh, I put stdio library. 1284 01:07:27,660 --> 01:07:30,950 >> Oh, got it--yeah, no, that's-- 1285 01:07:30,950 --> 01:07:34,380 so it turns out that string is actually something that is special to CS50. 1286 01:07:36,590 --> 01:07:41,240 There actually isn't a true string type 1287 01:07:41,240 --> 01:07:45,000 because right here, string is in the type position,- 1288 01:07:45,000 --> 01:07:49,140 we're declaring a variable called input of type string-- 1289 01:07:49,140 --> 01:07:53,320 and we're putting inside of input 1290 01:07:53,320 --> 01:07:58,440 the value that gets returned to us: the output of this GetString function call. 1291 01:07:58,440 --> 01:08:02,910 You can tell this is a function call because it's got the parentheses, just like printf does, 1292 01:08:02,910 --> 01:08:05,920 just like strlen does. 1293 01:08:05,920 --> 01:08:09,860 But string is actually not a real type 1294 01:08:09,860 --> 01:08:11,760 in the C language. 1295 01:08:11,760 --> 01:08:15,710 We've made it into a type, and we'll take about more what it actually is 1296 01:08:15,710 --> 01:08:19,000 in C, because there is a way to work with strings; 1297 01:08:19,000 --> 01:08:22,479 we've just done something to make it look a little nicer. 1298 01:08:22,479 --> 01:08:25,240 >> So here we go. 1299 01:08:25,240 --> 01:08:28,439 Here's Stella's code. 1300 01:08:28,439 --> 01:08:32,470 It's got the #include . 1301 01:08:32,470 --> 01:08:36,390 Other people will call this like a hash include sometimes or a sharp include. 1302 01:08:36,390 --> 01:08:39,580 Now we can start it, run it, 1303 01:08:39,580 --> 01:08:43,649 and we still get an error. 1304 01:08:43,649 --> 01:08:46,290 Let's see what's happening. 1305 01:08:48,069 --> 01:08:50,569 "Unused variable 'input'." 1306 01:08:50,569 --> 01:08:53,350 Okay, so that's cool, that's cool. 1307 01:08:53,350 --> 01:08:55,859 I mean, it's not really cool, 1308 01:08:55,859 --> 01:08:57,930 because we're getting an error and it's not running, 1309 01:08:57,930 --> 01:09:00,410 but what this is saying is that 1310 01:09:00,410 --> 01:09:03,130 we haven't used our variable, which makes sense. 1311 01:09:03,130 --> 01:09:06,090 We haven't tried to do anything with it. 1312 01:09:07,819 --> 01:09:10,490 This is a way that 1313 01:09:10,490 --> 01:09:14,170 Clang is trying to help us make sure that we're not just using variables 1314 01:09:14,170 --> 01:09:18,819 and then inadvertently calling them something else later in our code, 1315 01:09:18,819 --> 01:09:21,649 helping us avoid errors that way. 1316 01:09:23,130 --> 01:09:28,810 >> All right, so we've got input from user, 1317 01:09:28,810 --> 01:09:31,910 we stored it in a variable. 1318 01:09:31,910 --> 01:09:34,569 The next step was to then 1319 01:09:34,569 --> 01:09:37,609 start looking at all of the characters in the string 1320 01:09:37,609 --> 01:09:41,830 and identifying which ones are to be uppercase 1321 01:09:41,830 --> 01:09:44,600 and which ones are to be lowercase. 1322 01:09:44,600 --> 01:09:49,439 And like Kelly and Charlotte brought up, 1323 01:09:49,439 --> 01:09:52,340 we do this using a for loop. 1324 01:09:52,340 --> 01:09:55,520 So does anybody remember the syntax of a for loop 1325 01:09:55,520 --> 01:09:58,670 and would like to take a stab at just writing 1326 01:09:58,670 --> 01:10:03,170 the first line of the for loop, just the for bit? 1327 01:10:05,740 --> 01:10:07,330 Kelly, you wanna give it a try? 1328 01:10:07,330 --> 01:10:10,300 >>Yeah, so you have to initialize first, 1329 01:10:10,300 --> 01:10:13,170 so I put i = 0, 1330 01:10:13,170 --> 01:10:15,390 and then I did i < strlen(). 1331 01:10:15,390 --> 01:10:18,570 Is it in your Space--can I pull it up? 1332 01:10:18,570 --> 01:10:21,330 >>Yeah, I screwed up a little bit, but yeah. 1333 01:10:21,330 --> 01:10:24,180 >> Okay, let's pull it up. 1334 01:10:24,180 --> 01:10:26,490 Awesome. 1335 01:10:26,490 --> 01:10:29,510 >>But then I didn't know if you had that-- 1336 01:10:29,510 --> 01:10:32,180 Yeah, okay. 1337 01:10:33,020 --> 01:10:36,030 So here's what Kelly's got: let's look at it quickly. 1338 01:10:36,030 --> 01:10:40,290 So she's included the cs50 library at the top. 1339 01:10:40,290 --> 01:10:44,790 She's included the standard IO library at the top, 1340 01:10:45,550 --> 01:10:48,990 and the standard IO has functions like printf, 1341 01:10:48,990 --> 01:10:51,690 so if you ever want to use printf, you need that line. 1342 01:10:53,060 --> 01:10:56,140 And then she included the string library, 1343 01:10:56,140 --> 01:10:58,200 which we will need, 1344 01:10:58,200 --> 01:11:01,010 because the string library has strlen function, 1345 01:11:01,010 --> 01:11:04,320 which we'll get to in just a bit. 1346 01:11:04,320 --> 01:11:07,560 So if we look at what Kelly's done here, 1347 01:11:07,560 --> 01:11:10,260 can you walk us through what you did? 1348 01:11:10,260 --> 01:11:13,600 >>Yeah, so--I mean, I got errors, so-- 1349 01:11:13,600 --> 01:11:15,380 Yeah, no, it's-- 1350 01:11:15,380 --> 01:11:18,580 >>So I wanted to call the string that I got just "string". 1351 01:11:18,580 --> 01:11:22,230 I wanted to print out the string 1352 01:11:22,230 --> 01:11:25,640 and then I-- 1353 01:11:27,450 --> 01:11:30,930 I wanted to like set the string as like an array of each character 1354 01:11:30,930 --> 01:11:33,170 being different [inaudible], right? 1355 01:11:33,170 --> 01:11:38,080 And then I wanted to start at position 0 1356 01:11:38,080 --> 01:11:40,630 and then every 2 spaces, 1357 01:11:40,630 --> 01:11:42,580 I was going to capitalize, 1358 01:11:42,580 --> 01:11:45,680 so I just described it as 23, because I was looking at the ASCII table. 1359 01:11:45,680 --> 01:11:48,730 So that's what I was thinking--I know it's not right. 1360 01:11:48,730 --> 01:11:50,920 >> Got it, so this is a great start. 1361 01:11:50,920 --> 01:11:54,120 I think a lot of the framework is in place here. 1362 01:11:54,120 --> 01:11:57,500 Let's go through kind of line by line and see what's going on. 1363 01:11:57,500 --> 01:12:01,040 So the way I like to do this is just start by compiling the code 1364 01:12:01,040 --> 01:12:04,300 and letting the compiler tell me like what's wrong with my code, 1365 01:12:04,300 --> 01:12:07,450 because the compiler is really good at telling you what is wrong with your code. 1366 01:12:09,790 --> 01:12:12,780 So if we play it, building, and it doesn't build. 1367 01:12:12,780 --> 01:12:14,860 It says there was an error, 1368 01:12:14,860 --> 01:12:17,320 so if we scroll down and look at the first error, 1369 01:12:17,320 --> 01:12:20,960 it said-- 1370 01:12:20,960 --> 01:12:22,040 we scroll back up to the top, 1371 01:12:22,040 --> 01:12:26,070 we can see there are 5 errors generated down here at the bottom. 1372 01:12:26,070 --> 01:12:30,180 Oops, that's really cut off. 1373 01:12:31,810 --> 01:12:35,790 I guess that's just not going to get any better. 1374 01:12:36,970 --> 01:12:38,500 >> Here we go. 1375 01:12:39,790 --> 01:12:42,530 So there are 5 total errors generated. 1376 01:12:42,530 --> 01:12:45,140 Here I can only see the first 2, 1377 01:12:45,140 --> 01:12:49,100 so I can move this up a little bit and then kind of scroll up. 1378 01:12:49,100 --> 01:12:53,220 This is the command that was run to compile the code, 1379 01:12:53,220 --> 01:12:59,030 and then the first error was in file.c right here: 1380 01:12:59,030 --> 01:13:03,300 "expected identify or '('", and it was on this line 1381 01:13:03,300 --> 01:13:06,680 where we said string = GetString(). 1382 01:13:07,660 --> 01:13:12,920 You can see right here that it was on line 8 of file.c, 1383 01:13:12,920 --> 01:13:17,230 and then I believe this is character 12--that's the error. 1384 01:13:17,230 --> 01:13:20,390 So what's going wrong on line 8--Kelly? 1385 01:13:20,390 --> 01:13:22,810 >>You just need to call it, like, a letter or something, 1386 01:13:22,810 --> 01:13:24,400 like string s or something? 1387 01:13:24,400 --> 01:13:28,250 Yeah, so it turns out that these special type names, 1388 01:13:28,250 --> 01:13:31,770 so string, int, char, short, 1389 01:13:31,770 --> 01:13:35,740 they're all special keywords, 1390 01:13:35,740 --> 01:13:38,940 and you can't actually use them as names of your variables, 1391 01:13:38,940 --> 01:13:41,430 because the compiler looks at that and says, 1392 01:13:41,430 --> 01:13:43,680 "Oh, I know that's a type," 1393 01:13:43,680 --> 01:13:46,530 so you can't actually have a string variable called string, 1394 01:13:46,530 --> 01:13:50,800 like I'm trying to do here; this won't work. 1395 01:13:52,610 --> 01:13:58,250 But instead, we can call it something like s or st or str. 1396 01:13:58,250 --> 01:14:01,540 We also couldn't call it int. 1397 01:14:01,540 --> 01:14:03,740 We can't have a string called int. 1398 01:14:03,740 --> 01:14:06,740 We can't have a string called char; none of those will work. 1399 01:14:06,740 --> 01:14:09,420 >> So we'll just call it s for the time being. 1400 01:14:10,630 --> 01:14:13,170 Awesome. 1401 01:14:13,170 --> 01:14:17,500 So what next is going on in this line? 1402 01:14:21,390 --> 01:14:24,350 String--this line looks pretty good? 1403 01:14:24,350 --> 01:14:27,830 Let's give it another try; let's run it. 1404 01:14:27,830 --> 01:14:30,360 Okay, so we still get errors. 1405 01:14:30,360 --> 01:14:32,080 This time, we're down to 4 errors. 1406 01:14:32,080 --> 01:14:34,350 So we got one of them. 1407 01:14:34,350 --> 01:14:36,130 Let's move up. 1408 01:14:36,130 --> 01:14:38,890 "Unexpected type name 'string': expected expression," yes? 1409 01:14:38,890 --> 01:14:41,780 >>Yeah, that's because I tried to name the string. 1410 01:14:41,780 --> 01:14:44,270 Yeah, so it looks like we now also have to go fix 1411 01:14:44,270 --> 01:14:46,560 all of these references to string. 1412 01:14:46,560 --> 01:14:50,070 You can see there's a reference to string here, one here, one here, 1413 01:14:50,070 --> 01:14:53,440 so we can move all these to s. 1414 01:14:56,040 --> 01:15:00,930 Great. Okay, run it again. 1415 01:15:00,930 --> 01:15:03,260 Building--another error occurred. 1416 01:15:03,260 --> 01:15:06,900 >> Okay, now we're down to one error, 1417 01:15:06,900 --> 01:15:11,090 so int i = s and then the square brackets. 1418 01:15:11,090 --> 01:15:14,880 So can you tell us a little bit about what you were doing here, Kelly? 1419 01:15:14,880 --> 01:15:17,120 >>Yes. 1420 01:15:17,120 --> 01:15:19,410 It was on line 10. 1421 01:15:19,410 --> 01:15:22,760 >>I don't know if we needed it all, 1422 01:15:22,760 --> 01:15:25,560 but I think I was trying to-- 1423 01:15:25,560 --> 01:15:27,580 I'll save it so you can follow along. 1424 01:15:27,580 --> 01:15:30,470 >>I think I was 1425 01:15:30,470 --> 01:15:33,150 trying to tell it to start at position 0 1426 01:15:33,150 --> 01:15:35,030 at the beginning of my for loop, 1427 01:15:35,030 --> 01:15:38,370 so I think maybe that saying was unnecessary. 1428 01:15:39,690 --> 01:15:41,350 So just to recap, 1429 01:15:41,350 --> 01:15:44,850 you were saying that this--you were trying to start this at position 0 1430 01:15:44,850 --> 01:15:47,030 and get this--yeah, 1431 01:15:47,030 --> 01:15:50,460 so I think earlier, too, when you were walking through the code, 1432 01:15:50,460 --> 01:15:54,070 you were talking about how you wanted to treat the string as an array 1433 01:15:54,070 --> 01:15:57,440 so we can access each character individually. 1434 01:15:57,440 --> 01:16:00,660 I think Dan mentioned arrays, too, trying to convert strings into arrays. 1435 01:16:01,400 --> 01:16:03,300 >> It turns out that kind of under the hood, 1436 01:16:03,300 --> 01:16:06,060 a string is actually stored as an array already, 1437 01:16:06,060 --> 01:16:08,060 and you can access it like an array; 1438 01:16:08,060 --> 01:16:12,390 there's no conversion that you have to do. 1439 01:16:12,390 --> 01:16:15,770 A string isn't--so-- 1440 01:16:15,770 --> 01:16:18,200 a string isn't really an array, 1441 01:16:18,200 --> 01:16:21,940 but you can access it as such, because all of the characters are stored 1442 01:16:21,940 --> 01:16:25,160 one right next to each other in memory, 1443 01:16:25,160 --> 01:16:28,310 so if you want to get the first character of the string, 1444 01:16:28,310 --> 01:16:31,910 it's the same as getting the first integer in an integer array. 1445 01:16:31,910 --> 01:16:35,010 So we often--we'll 1446 01:16:35,010 --> 01:16:36,360 refer to the 2 like 1447 01:16:36,360 --> 01:16:38,590 oh, as being a string, as being an array, 1448 01:16:38,590 --> 01:16:40,830 even though it really--they're very different things, 1449 01:16:40,830 --> 01:16:43,410 but you can treat them very much the same. 1450 01:16:44,700 --> 01:16:47,400 So it turns out that this line actually-- 1451 01:16:47,400 --> 01:16:51,270 we don't need to do any conversion, 1452 01:16:51,270 --> 01:16:53,500 and in fact, 1453 01:16:53,500 --> 01:16:56,790 this actually isn't a valid expression, 1454 01:16:56,790 --> 01:16:59,040 which is what our compiler is telling us. 1455 01:16:59,040 --> 01:17:01,290 It's saying, "Hey, it expected an expression, 1456 01:17:01,290 --> 01:17:04,610 and what I've got here is something that's not an expression." 1457 01:17:04,610 --> 01:17:07,310 To declare an array, 1458 01:17:07,310 --> 01:17:11,780 all you need to do is something like this with a semicolon, 1459 01:17:11,780 --> 01:17:15,080 and then you tell the array how long you want it to be, 1460 01:17:15,080 --> 01:17:18,370 such as 5, so this line 10 1461 01:17:18,370 --> 01:17:23,880 will declare an array called i with 5 integers in it. 1462 01:17:23,880 --> 01:17:26,540 >> This is an aside; this isn't necessary for the code, 1463 01:17:26,540 --> 01:17:30,970 but this is--this is kind of what the declaration looks like. 1464 01:17:30,970 --> 01:17:35,310 >>How would you not use char i[strlen(s)]? 1465 01:17:35,310 --> 01:17:38,500 Char i[strlen(s)]? 1466 01:17:38,500 --> 01:17:41,500 >>char i, and then inside the brackets, strlen(s). 1467 01:17:43,900 --> 01:17:47,710 Yeah, so we could-- 1468 01:17:47,710 --> 01:17:49,460 we could do this, 1469 01:17:49,460 --> 01:17:52,160 and this would declare--what would this do? 1470 01:17:52,160 --> 01:17:55,900 >>I think it would declare an array 1471 01:17:55,900 --> 01:18:01,780 of, like--of-- 1472 01:18:01,780 --> 01:18:04,540 oh, you don't need the i--oh, 1473 01:18:04,540 --> 01:18:06,420 yeah, you definitely don't need the i. 1474 01:18:06,420 --> 01:18:10,250 You declare an array of characters 1475 01:18:10,250 --> 01:18:13,150 of length s. 1476 01:18:14,680 --> 01:18:16,770 And so what Sam's saying is that 1477 01:18:16,770 --> 01:18:20,660 this line 10--let me put some space in between it and the rest so we can isolate it-- 1478 01:18:20,660 --> 01:18:23,440 this line 10 would declare an array 1479 01:18:23,440 --> 01:18:28,520 of chars, strlen(s) worth of them, 1480 01:18:28,520 --> 01:18:32,270 but we actually do need the i, 1481 01:18:32,270 --> 01:18:35,710 and the reason we need the i is because we need to give the array a name; 1482 01:18:35,710 --> 01:18:38,900 otherwise, we wouldn't have a way to refer to the array later in the code. 1483 01:18:38,900 --> 01:18:41,180 >> So this right here is the array name 1484 01:18:41,180 --> 01:18:43,420 and inside is the length. 1485 01:18:43,420 --> 01:18:45,010 This will give us an array, 1486 01:18:45,010 --> 01:18:47,430 but this is--again, this is a declaration. 1487 01:18:47,430 --> 01:18:50,380 It's not an assignment; it's not an initialization. 1488 01:18:50,380 --> 01:18:52,330 If we declare this array like this, 1489 01:18:52,330 --> 01:18:54,490 there's actually nothing in the contents of the array. 1490 01:18:54,490 --> 01:18:57,490 It's just junk--whatever is in there. 1491 01:18:57,490 --> 01:18:59,780 And like we said, 1492 01:18:59,780 --> 01:19:01,340 we actually don't even need to do this conversion, 1493 01:19:01,340 --> 01:19:04,810 because we can already treat string s as an array. 1494 01:19:06,750 --> 01:19:09,170 I know we're running a bit long on time, 1495 01:19:09,170 --> 01:19:12,330 but I want to just really quickly get to this for loop 1496 01:19:12,330 --> 01:19:14,850 and show you how to access the individual characters in the array, 1497 01:19:14,850 --> 01:19:17,170 because that's key for our assignment. 1498 01:19:17,170 --> 01:19:20,790 And then we'll let you go from there. 1499 01:19:20,790 --> 01:19:24,610 >> So here, we've got a for loop 1500 01:19:24,610 --> 01:19:28,310 that looks almost good. 1501 01:19:28,310 --> 01:19:30,960 What's not quite almost there? 1502 01:19:30,960 --> 01:19:33,240 What's--what's not quite right about this for loop? 1503 01:19:33,240 --> 01:19:36,010 >>No curly braces? 1504 01:19:36,010 --> 01:19:38,420 No curly braces, for one. 1505 01:19:38,420 --> 01:19:42,580 Wanna make sure that we have the body of the loop with curly braces. 1506 01:19:42,580 --> 01:19:45,240 Oops, not the square brace! 1507 01:19:45,240 --> 01:19:48,660 And what is the next thing that we need to do with this loop? 1508 01:19:48,660 --> 01:19:52,640 If I try and run it, 1509 01:19:52,640 --> 01:19:56,740 whats this error that I get? 1510 01:19:56,740 --> 01:20:00,990 I get all these errors--I have to initialize i. 1511 01:20:00,990 --> 01:20:03,610 Great. 1512 01:20:03,610 --> 01:20:05,770 So this will be a loop--this isn't quite there, 1513 01:20:05,770 --> 01:20:08,680 but it's getting really close. 1514 01:20:08,680 --> 01:20:11,260 This code we can now run, 1515 01:20:11,260 --> 01:20:14,620 build, an error still occurs. 1516 01:20:17,960 --> 01:20:19,800 Why is it still complaining, 1517 01:20:19,800 --> 01:20:22,030 saying the expression is unused, 1518 01:20:22,030 --> 01:20:24,600 and that's because this line doesn't do anything. 1519 01:20:24,600 --> 01:20:27,620 We didn't store it anywhere; we didn't save the result. 1520 01:20:27,620 --> 01:20:30,210 Where should we be saving this result? 1521 01:20:30,210 --> 01:20:32,920 >>s or i? 1522 01:20:32,920 --> 01:20:35,990 Exactly, and this funky syntax, 1523 01:20:35,990 --> 01:20:38,550 this s with the square brackets 1524 01:20:38,550 --> 01:20:41,500 means access the i-th character of s. 1525 01:20:41,500 --> 01:20:45,650 That's going to be something that's really important for the assignment. 1526 01:20:45,650 --> 01:20:48,070 Unfortunately, we're going to have to stop here, 1527 01:20:48,070 --> 01:20:50,550 so that Sebastian section can come in, 1528 01:20:50,550 --> 01:20:53,980 but I'm happy to stay after outside and will take any questions. 1529 01:20:53,980 --> 01:20:56,680 So thank you guys for coming. 1530 01:20:56,680 --> 01:20:59,730 This should now upload, build, run. 1531 01:21:01,570 --> 01:21:04,320 We can enter a string like "hello" 1532 01:21:06,570 --> 01:21:08,730 and it prints "QOX". 1533 01:21:08,730 --> 01:21:11,030 [laughter] 1534 01:21:11,030 --> 01:21:14,640 >> So not quite there, but moving, getting towards those loops! 1535 01:21:14,640 --> 01:21:18,640 Thanks for coming, and I'll be outside.