1 00:00:00,506 --> 00:00:08,576 [ Silence ] 2 00:00:09,076 --> 00:00:09,716 >> [Sound effects] Be it known through all in summary 3 00:00:09,886 --> 00:00:19,456 that Ralph Parker is here by appointed a member 4 00:00:19,456 --> 00:00:22,086 of the Little Orphan Annie secret circle and is entitled 5 00:00:22,086 --> 00:00:25,086 to all the honors and benefits occurring thereto. 6 00:00:25,086 --> 00:00:29,856 >> Signed Little Orphan Annie countersigned Pierre Andre 7 00:00:30,276 --> 00:00:31,076 in ink. 8 00:00:31,586 --> 00:00:33,936 >> Honors and benefits already at the age of nine. 9 00:00:34,516 --> 00:00:40,716 [ Sound effects ] 10 00:00:41,216 --> 00:00:42,416 >> Come on let's get on with it. 11 00:00:42,626 --> 00:00:44,846 I don't need all that jazz about smugglers and pirates. 12 00:00:44,846 --> 00:00:44,996 [Sound effects] 13 00:00:44,996 --> 00:00:48,986 >> Listen tomorrow night for the concluding adventure 14 00:00:48,986 --> 00:00:50,986 of the Black Pirate Ship. 15 00:00:51,246 --> 00:00:53,876 Now it's time for Annie's secret message 16 00:00:54,116 --> 00:00:56,316 for you members of the secret circle. 17 00:00:57,256 --> 00:00:59,156 Remember kids, only members 18 00:00:59,156 --> 00:01:03,306 of Annie's secret circle can decode Annie's secret message. 19 00:01:03,566 --> 00:01:06,856 Remember Annie is depending on you. 20 00:01:07,296 --> 00:01:08,876 [Background sound effects] Set your pens to B2. 21 00:01:08,876 --> 00:01:12,696 Here is the message 12, 11 -- 22 00:01:12,696 --> 00:01:16,696 >> [Background sound effects] I attend my first secret meeting. 23 00:01:17,666 --> 00:01:21,966 >> 14, 11, 18,16 -- 24 00:01:22,006 --> 00:01:22,546 >> [Background sound effects] Pierre was 25 00:01:22,626 --> 00:01:23,256 in great voice tonight. 26 00:01:23,256 --> 00:01:26,026 I could tell that tonight's message was really important. 27 00:01:26,026 --> 00:01:27,196 >> [Background music] 3, 25. 28 00:01:27,196 --> 00:01:30,206 That's a message from Annie herself. 29 00:01:30,206 --> 00:01:31,716 Remember don't tell anyone. 30 00:01:31,716 --> 00:01:39,226 >> 90 seconds later I'm in the only room in the house 31 00:01:39,296 --> 00:01:42,726 where a boy of 9 could sit in privacy and decode. 32 00:01:42,886 --> 00:01:49,796 [Laughter] Ah hah, B and on to the next, 33 00:01:49,796 --> 00:01:51,386 E the first word is be. 34 00:01:51,976 --> 00:01:54,566 S, it was coming easier now. 35 00:01:54,626 --> 00:01:54,816 U 36 00:01:54,816 --> 00:01:56,666 >> All come on Ralphie. 37 00:01:58,706 --> 00:02:08,316 [Background sound effects] I'll be right down Ma, gee whiz. 38 00:02:08,316 --> 00:02:08,436 >> [Background music] T,O. 39 00:02:09,026 --> 00:02:10,566 Be sure to what? 40 00:02:10,876 --> 00:02:13,086 What was Little Orphan Annie trying to say? 41 00:02:13,456 --> 00:02:13,906 Be sure to what? 42 00:02:13,906 --> 00:02:16,696 [Background sound effects] Alright mom, I'll be right out. 43 00:02:17,616 --> 00:02:22,236 >> [Background music] I was getting closer now. 44 00:02:22,236 --> 00:02:23,676 The tension was terrible. 45 00:02:24,246 --> 00:02:25,226 What was it? 46 00:02:25,226 --> 00:02:27,076 The fate of the planet may hang in the balance. 47 00:02:27,076 --> 00:02:27,356 [Sound effects] 48 00:02:27,356 --> 00:02:29,706 >> I'll be right out for crying out loud. 49 00:02:30,486 --> 00:02:32,446 >> [Background music] Almost there. 50 00:02:33,486 --> 00:02:35,416 My fingers flew. 51 00:02:35,496 --> 00:02:38,376 My mind was a steel trap. 52 00:02:38,716 --> 00:02:39,696 Every pore vibrated. 53 00:02:39,696 --> 00:02:41,836 It was almost clear. 54 00:02:42,366 --> 00:02:45,516 Yes, yes, yes, yes. 55 00:02:46,156 --> 00:02:49,506 >> Be sure to drink your Ovaltine. 56 00:02:55,416 --> 00:03:01,046 Ovaltine. A crummy commercial. 57 00:03:01,046 --> 00:03:05,576 [Background music] Son of a bitch. 58 00:03:05,576 --> 00:03:05,643 [Laughter] 59 00:03:05,643 --> 00:03:06,516 >> This is CS50. 60 00:03:06,516 --> 00:03:10,066 Welcome to the start of week 2. 61 00:03:10,066 --> 00:03:13,706 So this excerpt from A Christmas Story, which is a movie 62 00:03:13,886 --> 00:03:16,376 that plays incessantly around Christmas time 63 00:03:16,376 --> 00:03:18,336 in this country, pretty much 24-7. 64 00:03:18,676 --> 00:03:22,096 Is actually an interesting example of cryptography or code. 65 00:03:22,096 --> 00:03:24,166 And this is one of the real world domains 66 00:03:24,166 --> 00:03:26,166 that we'll introduce this week for the sake 67 00:03:26,166 --> 00:03:29,736 of motivation not only for the material of the week but also 68 00:03:29,736 --> 00:03:32,316 for problems that too will be going 69 00:03:32,316 --> 00:03:34,246 into your hands later this week. 70 00:03:34,246 --> 00:03:38,196 So what little Ralphie had there was a secret decoder ring 71 00:03:38,196 --> 00:03:38,626 of sorts. 72 00:03:38,626 --> 00:03:41,626 Essentially it had the alphabet, A through Z on one side 73 00:03:41,626 --> 00:03:43,976 of that circle and then A through Z on another. 74 00:03:43,976 --> 00:03:45,986 And what the guy on the radio told them was how 75 00:03:45,986 --> 00:03:49,676 to line those two circles up so that A might map to M 76 00:03:49,676 --> 00:03:52,896 and B might map to another letter, so it was a code. 77 00:03:52,896 --> 00:03:55,666 And he was intent on actually decoding that. 78 00:03:55,666 --> 00:03:58,596 So what you saw was actually an instance 79 00:03:58,596 --> 00:04:03,866 of what geeks have known for some time is rot13, r o t 13. 80 00:04:04,436 --> 00:04:10,016 This is a rotational cipher and if you're increasingly geekified 81 00:04:10,016 --> 00:04:12,666 by courses such as this you will understand the irony 82 00:04:13,016 --> 00:04:14,156 of a quote like this. 83 00:04:15,306 --> 00:04:15,976 Let's see if you do. 84 00:04:16,516 --> 00:04:22,666 [ Sound effects ] 85 00:04:23,166 --> 00:04:24,496 >> So rot13 to be clear is when you take the alphabet 86 00:04:24,496 --> 00:04:26,786 and you take an input and you rotate all 87 00:04:26,786 --> 00:04:28,646 of the letters 13 places. 88 00:04:31,946 --> 00:04:33,776 [Laughter] There we go. 89 00:04:34,236 --> 00:04:36,686 If it's kind of one of these moments right now, 90 00:04:36,686 --> 00:04:38,356 just ask the person next to you. 91 00:04:38,356 --> 00:04:42,226 So this was-- this is sort of a age old joke but it's an example 92 00:04:42,226 --> 00:04:44,516 of something more generally known as a Caesar cipher. 93 00:04:44,746 --> 00:04:46,256 And a Caesar cipher's something we'll look 94 00:04:46,256 --> 00:04:48,526 at in more academic detail on Wednesday. 95 00:04:48,796 --> 00:04:51,356 But the joke here is of course, I'll leave it 96 00:04:51,436 --> 00:04:53,386 to the astute thinker there. 97 00:04:53,386 --> 00:04:54,246 So a couple of announcement -- 98 00:04:54,246 --> 00:04:57,096 one new handout for today the printout of the source code 99 00:04:57,096 --> 00:04:58,176 that we'll be using for the week. 100 00:04:58,176 --> 00:05:00,066 All of this stuff is also available online. 101 00:05:00,316 --> 00:05:02,276 And also if you weren't paying attention last week 102 00:05:02,276 --> 00:05:04,486 or weren't here, do realize that so 103 00:05:04,486 --> 00:05:06,616 that you are freed intellectually not to scribble 104 00:05:06,616 --> 00:05:08,886 down most anything that is said in class. 105 00:05:09,106 --> 00:05:11,186 Do realize that we've been putting up scribe notes thanks 106 00:05:11,186 --> 00:05:13,546 to one of the teaching fellows, which are an authoritative set 107 00:05:13,546 --> 00:05:17,046 of notes so to speak in PDF format on the website. 108 00:05:17,086 --> 00:05:20,476 And also know too with regard to problem sets, 109 00:05:20,476 --> 00:05:22,506 so problem set 1 went out on Friday. 110 00:05:22,506 --> 00:05:24,726 And from some of the e-mails we got we know some 111 00:05:24,726 --> 00:05:25,546 of you dove into it. 112 00:05:25,586 --> 00:05:28,346 But it's okay if normal people are waiting 113 00:05:28,346 --> 00:05:29,666 until later this week to start. 114 00:05:29,866 --> 00:05:31,416 But do realize per the syllabus 115 00:05:31,416 --> 00:05:32,666 that the course offers you a number 116 00:05:32,666 --> 00:05:34,326 of late days for problem sets. 117 00:05:34,576 --> 00:05:39,426 Nine specifically, which means you can give yourself a 24 hour 118 00:05:39,426 --> 00:05:41,736 extension for instance at any point in the semester simply 119 00:05:41,736 --> 00:05:43,256 by cashing in one of your late days. 120 00:05:43,566 --> 00:05:45,826 Once you know who your teaching fellow is we simply expect 121 00:05:45,826 --> 00:05:47,596 that you advise him or her that you're doing it, 122 00:05:47,846 --> 00:05:49,606 so they don't start grading and then realize 123 00:05:49,606 --> 00:05:51,146 that oh you're not actually in their pile. 124 00:05:51,396 --> 00:05:53,406 But you can spend one late day, nine late days, 125 00:05:53,406 --> 00:05:56,076 we would encourage you just from our own experience 126 00:05:56,076 --> 00:05:58,386 to be conservative up front because inevitably things come 127 00:05:58,386 --> 00:05:59,296 up later in the semester. 128 00:05:59,586 --> 00:06:02,066 But also to mitigate that inevitable Pset 129 00:06:02,066 --> 00:06:04,756 that you just completely screw up for whatever personal 130 00:06:04,966 --> 00:06:08,166 or academic reason, we will also drop your lowest Pset score 131 00:06:08,426 --> 00:06:09,426 at the end of the semester. 132 00:06:09,426 --> 00:06:11,196 So realize that support structure is there. 133 00:06:11,466 --> 00:06:15,886 Super sections are taking place today, tomorrow and Wednesday, 134 00:06:15,886 --> 00:06:17,956 check the courses website for the Google calendar 135 00:06:17,956 --> 00:06:19,346 that will show you when and where they are. 136 00:06:19,566 --> 00:06:22,456 So these are just sections but they're just a few sections 137 00:06:22,456 --> 00:06:23,806 that anyone and everyone can go 138 00:06:23,806 --> 00:06:25,566 to irrespective of comfort level. 139 00:06:25,566 --> 00:06:29,006 So if you are in -- among those less comfortable let's say, 140 00:06:29,006 --> 00:06:31,666 those in between these are apps specifically targeted 141 00:06:31,666 --> 00:06:33,636 at that demographic this week so that you feel 142 00:06:33,636 --> 00:06:35,226 that you do have a support structure in place 143 00:06:35,546 --> 00:06:36,746 for the first problem set. 144 00:06:36,806 --> 00:06:39,506 One of our teaching fellows Marta led the first walkthrough 145 00:06:39,506 --> 00:06:41,166 last night, it was filmed 146 00:06:41,166 --> 00:06:42,676 and that video will go online as well. 147 00:06:42,676 --> 00:06:45,396 And the walkthroughs are meant to be an authoritative how 148 00:06:45,396 --> 00:06:47,426 to on how to begin the Pset 149 00:06:47,426 --> 00:06:50,686 where sections will focus not only on some related piece 150 00:06:50,686 --> 00:06:53,546 of material but also on lecture material 151 00:06:53,616 --> 00:06:56,326 and the stuff you should be getting more comfortable with. 152 00:06:56,496 --> 00:06:58,086 Late mid-week will you find 153 00:06:58,086 --> 00:06:59,856 out via e-mail what your assigned section 154 00:06:59,856 --> 00:07:02,786 and teaching fellow is and in a class this size inevitably we 155 00:07:02,786 --> 00:07:03,876 need to shuffle people around, 156 00:07:03,876 --> 00:07:06,426 that's okay we'll fix any mistakes that we 157 00:07:06,426 --> 00:07:07,596 or your schedules have made. 158 00:07:07,966 --> 00:07:10,646 Office hours and virtual office hours have also begun. 159 00:07:11,166 --> 00:07:13,946 Office hours are an opportunity to drop down into the basement 160 00:07:13,946 --> 00:07:14,616 of the science center. 161 00:07:14,616 --> 00:07:15,526 You go down those stairs, 162 00:07:15,526 --> 00:07:17,126 you take a left there's some white boards 163 00:07:17,126 --> 00:07:19,366 and that's generally where the TFs and CAs hang out. 164 00:07:19,636 --> 00:07:21,726 If you've got a question you write your name on the board. 165 00:07:21,726 --> 00:07:24,046 You generally write down the number of the computer station 166 00:07:24,046 --> 00:07:25,916 that you're at, they're all numbered with stickers 167 00:07:26,176 --> 00:07:28,166 and then we the staff go through the list top 168 00:07:28,166 --> 00:07:29,096 to bottom, left to right. 169 00:07:29,246 --> 00:07:33,276 And then eventually right to left, bottom to top going back 170 00:07:33,276 --> 00:07:34,896 and forth through the list crossing names off 171 00:07:34,896 --> 00:07:36,876 and then plucking off people's questions 172 00:07:36,876 --> 00:07:37,926 so realize that that exists. 173 00:07:38,176 --> 00:07:40,106 And then also this little technology, 174 00:07:40,106 --> 00:07:43,876 so sometimes it just doesn't make all that much sense to go 175 00:07:43,876 --> 00:07:47,356 into -- say trek across campus to actually get 176 00:07:47,456 --> 00:07:50,326 to the science center to just ask like a 1 minute question. 177 00:07:50,326 --> 00:07:52,566 And sometimes e-mail's not perfect for it 178 00:07:52,566 --> 00:07:54,566 because sometimes you want the staff member to see what's 179 00:07:54,566 --> 00:07:56,306 on your screen, so we've offered 180 00:07:56,306 --> 00:07:59,396 for some time now what we call virtual office hours using a 181 00:07:59,616 --> 00:08:01,776 virtual terminal room in contrast 182 00:08:01,776 --> 00:08:03,476 with the science center's physical terminal room. 183 00:08:03,786 --> 00:08:06,836 And essentially we do this by a program called Elluminate. 184 00:08:07,156 --> 00:08:09,666 It's special software that's free for all of us to use, 185 00:08:09,666 --> 00:08:12,346 it's cross platform, works Windows, MACs, Linux computers. 186 00:08:12,606 --> 00:08:14,256 And essentially when you want 187 00:08:14,256 --> 00:08:17,576 to attend virtual office hours you'll click a button like this 188 00:08:17,576 --> 00:08:20,406 under VTR, you will then log in for now 189 00:08:20,406 --> 00:08:22,446 with you FAS user name and password. 190 00:08:22,446 --> 00:08:25,886 My connection's being a bit slow but once you do so, 191 00:08:26,506 --> 00:08:30,466 you will then be led hopefully after very few, 192 00:08:30,466 --> 00:08:34,136 if any technical hiccups to a window that's going to pop up. 193 00:08:34,136 --> 00:08:35,186 The first time you use it, 194 00:08:35,186 --> 00:08:37,266 it's going to install some special Java software 195 00:08:37,476 --> 00:08:40,406 and then even if it doesn't come up right now that's fine. 196 00:08:40,406 --> 00:08:42,376 You will essentially have a glorified chat room 197 00:08:42,376 --> 00:08:44,296 so there's going to be a little portion of the screen 198 00:08:44,296 --> 00:08:46,426 where you can type back and forth with the teaching staff. 199 00:08:46,706 --> 00:08:50,656 But more compellingly there is a white board so to speak, 200 00:08:50,706 --> 00:08:52,636 which takes up most of the screen and the neat thing 201 00:08:52,636 --> 00:08:54,326 with that is that the teaching fellows 202 00:08:54,326 --> 00:08:58,176 and the CAs can take control of or share control of your screen, 203 00:08:58,416 --> 00:08:59,596 your keyboard and mouse. 204 00:08:59,596 --> 00:09:01,456 And actually help you troubleshoot something that is 205 00:09:01,516 --> 00:09:03,876 on your screen at that moment and time. 206 00:09:03,876 --> 00:09:05,556 And if you're a little freaked out by that idea, 207 00:09:06,106 --> 00:09:08,266 realize we won't necessarily have the ability 208 00:09:08,266 --> 00:09:09,786 to see your Facebook windows, 209 00:09:09,786 --> 00:09:11,476 your personal chat windows and all of that. 210 00:09:11,656 --> 00:09:15,076 It's very much an opt in thing, it's not either or so I'll let 211 00:09:15,076 --> 00:09:17,886 that load in the background and pull it up at some point. 212 00:09:18,166 --> 00:09:20,496 But for now a couple of final notes. 213 00:09:20,496 --> 00:09:23,126 We will eventually have a bulletin board 214 00:09:23,126 --> 00:09:25,596 on the courses website once we have enrollment data 215 00:09:25,596 --> 00:09:27,636 and can actually authenticate people properly. 216 00:09:27,916 --> 00:09:29,486 But for now if you have any questions 217 00:09:29,906 --> 00:09:33,916 that are appropriately e-mailed just drop a note to cs50.net 218 00:09:33,916 --> 00:09:36,926 and do get into the habit of being as clear and as precise 219 00:09:36,926 --> 00:09:38,296 and as technical as possible. 220 00:09:38,606 --> 00:09:41,476 Sending us e-mails whatever the hour with comments like, 221 00:09:41,916 --> 00:09:43,746 hi I'm having some trouble with my code. 222 00:09:43,746 --> 00:09:44,626 Do you have any advice? 223 00:09:45,486 --> 00:09:47,916 Not really, you gotta be more specific than that. 224 00:09:47,916 --> 00:09:49,996 Include your source code, specify your problems. 225 00:09:49,996 --> 00:09:51,126 Tell us what you've done. 226 00:09:51,126 --> 00:09:53,236 What did work, did not work and so forth. 227 00:09:53,356 --> 00:09:55,726 Help us help you, because realize we get a lot 228 00:09:55,726 --> 00:09:57,016 of such e-mails. 229 00:09:57,186 --> 00:09:59,696 So we like to pluck off the ones that are really clear 230 00:09:59,696 --> 00:10:00,816 and really straight forward. 231 00:10:01,306 --> 00:10:03,436 Sensor boards, if you did hacker edition on your way 232 00:10:03,436 --> 00:10:04,386 out today we've got a couple 233 00:10:04,386 --> 00:10:06,026 of cardboard boxes, just drop them off. 234 00:10:06,086 --> 00:10:09,176 But on that slip of paper write your name, ID number 235 00:10:09,176 --> 00:10:11,556 and FAS user name so we can cross you off the list. 236 00:10:11,866 --> 00:10:14,356 And then finally, this. 237 00:10:14,666 --> 00:10:19,856 So Harvard, not so rich anymore but thanks 238 00:10:19,856 --> 00:10:23,326 to some wonderfully supportive alumni who have fond memories 239 00:10:23,326 --> 00:10:25,006 of CS50 and computer science here. 240 00:10:25,296 --> 00:10:26,546 We will be fortunately able 241 00:10:26,546 --> 00:10:28,686 to continue a little tradition we began last year, 242 00:10:28,686 --> 00:10:34,006 which was lunch with David and team or et al whereby we hang 243 00:10:34,006 --> 00:10:36,136 out with 10, 20, 30 students. 244 00:10:36,136 --> 00:10:37,566 Whoever's free on Friday afternoons. 245 00:10:37,806 --> 00:10:39,556 We go to lunch somewhere, sit down, chit chat, 246 00:10:39,556 --> 00:10:40,296 talk about the course. 247 00:10:40,296 --> 00:10:42,516 Talk about past experiences at Harvard, 248 00:10:42,516 --> 00:10:44,286 whatever the case may be and it's just a chance 249 00:10:44,536 --> 00:10:46,636 to make a big class a lot more intimate. 250 00:10:46,636 --> 00:10:49,296 So if you're interested in hanging out with me and some 251 00:10:49,296 --> 00:10:53,256 of the TFs and CAs over lunch this Friday, 1:15 p.m. just go 252 00:10:53,366 --> 00:10:57,526 to cs50.net/rsvp and we'll let you know where and all 253 00:10:57,526 --> 00:10:58,436 of the logistics there. 254 00:10:58,516 --> 00:10:59,746 So it's a nice tradition that thanks 255 00:10:59,806 --> 00:11:02,246 to some generous alumni we will begin, we will be able 256 00:11:02,246 --> 00:11:03,266 to continue this year. 257 00:11:03,766 --> 00:11:05,636 So I went back and did my homework. 258 00:11:05,766 --> 00:11:11,306 I re-downloaded our surveys and I made this integrated 259 00:11:11,306 --> 00:11:13,056 into the courses website using Google maps, 260 00:11:13,176 --> 00:11:15,446 something you'll be able to do later in the semester. 261 00:11:15,446 --> 00:11:17,696 It was actually a good sanity check because we've got a couple 262 00:11:17,696 --> 00:11:18,856 of notes from students saying, 263 00:11:19,236 --> 00:11:22,056 "Hi my country is not listed here, 264 00:11:22,056 --> 00:11:23,376 my city is not listed here. 265 00:11:23,376 --> 00:11:25,326 Did I actually set my problem set correctly?" 266 00:11:25,626 --> 00:11:27,896 Sometimes the answer was yes, sometimes the answer was no, 267 00:11:27,896 --> 00:11:30,076 so it was actually a nice little way of checking as much. 268 00:11:30,406 --> 00:11:33,846 But this is again just a mash up so to speak of all 269 00:11:33,846 --> 00:11:36,086 of the interesting data that we collected. 270 00:11:36,146 --> 00:11:37,656 So more on that in the future. 271 00:11:37,916 --> 00:11:39,936 Do take notes if you haven't already then 272 00:11:39,936 --> 00:11:42,296 on the problem sets page there has been for a week, 273 00:11:42,586 --> 00:11:44,906 galleries not only from the past two years 274 00:11:44,906 --> 00:11:45,966 but from this past year. 275 00:11:46,196 --> 00:11:48,386 So it's one thing to actually do your scratch project, 276 00:11:48,386 --> 00:11:49,916 upload it and then be done. 277 00:11:50,086 --> 00:11:52,596 But if you get some time or just want to procrastinate some night 278 00:11:52,596 --> 00:11:55,546 by all means flip through the 300 plus scratch projects 279 00:11:55,576 --> 00:11:56,636 that your classmates made. 280 00:11:56,636 --> 00:11:58,906 Because there's some really nice projects 281 00:11:58,976 --> 00:12:00,336 that got uploaded there. 282 00:12:00,726 --> 00:12:04,396 And then finally, let's see if this actually came up. 283 00:12:04,396 --> 00:12:06,956 Yes. So what is -- has just popped up on the screen 284 00:12:06,956 --> 00:12:09,446 with a slight delay there is Elluminate the virtual 285 00:12:09,446 --> 00:12:10,126 terminal room. 286 00:12:10,126 --> 00:12:12,106 And this is again this glorified chat room. 287 00:12:12,106 --> 00:12:14,326 So at the top left it looks 288 00:12:14,416 --> 00:12:17,896 like teaching fellow Andrew Sellergren has perfectly timed 289 00:12:17,896 --> 00:12:20,226 this demonstration to say hello to us. 290 00:12:20,226 --> 00:12:22,546 So he is chatting with us down there. 291 00:12:23,016 --> 00:12:26,016 If I dare to indulge something like this. 292 00:12:26,156 --> 00:12:32,066 Hello Andrew, you are in Sanders right now to see 293 00:12:32,066 --> 00:12:33,206 if he's actually paying attention. 294 00:12:33,206 --> 00:12:35,756 At the top left you see who's logged in, both me and Andrew. 295 00:12:35,756 --> 00:12:37,326 Bottom left is obviously the chat window. 296 00:12:37,596 --> 00:12:39,596 The right hand side is where we can share screens 297 00:12:39,596 --> 00:12:41,796 but the workflow you will find is that if you log 298 00:12:41,796 --> 00:12:43,606 into this thing, you've got a question, 299 00:12:43,686 --> 00:12:46,066 you click this little icon here at the top left. 300 00:12:46,426 --> 00:12:47,966 The little hand icon, click that 301 00:12:48,306 --> 00:12:50,456 and then notice [Sound effects] a little 1 goes 302 00:12:50,456 --> 00:12:51,316 up next to my name. 303 00:12:51,316 --> 00:12:53,676 Which means David is first in line, which hopefully means 304 00:12:53,676 --> 00:12:56,606 that Andrew the teaching fellow will pull me into a private -- 305 00:12:57,386 --> 00:13:01,226 dare I turn around [Laughter] he'll pull me 306 00:13:01,226 --> 00:13:04,496 into a private chat room where I can then share my screen just 307 00:13:04,496 --> 00:13:07,036 with that TF or CA and so forth. 308 00:13:07,156 --> 00:13:10,306 So it's really neat, works reasonably well. 309 00:13:10,306 --> 00:13:17,996 Doesn't work so well over dial-up and we're done. 310 00:13:17,996 --> 00:13:20,176 [Sound effects] But realize it's a nice alternative to trucking 311 00:13:20,176 --> 00:13:23,126 to the science center when it's say snowing in November so just 312 00:13:23,126 --> 00:13:25,056 to ask that little quick question. 313 00:13:25,376 --> 00:13:27,286 So, why are you all here? 314 00:13:27,286 --> 00:13:33,006 So, we had 365 plus folks register 315 00:13:33,006 --> 00:13:34,786 for CS50, which is fantastic. 316 00:13:34,786 --> 00:13:38,126 We of course asked folks, why did you decide to take this CS50 317 00:13:38,176 --> 00:13:39,536 in that Pset 0 survey? 318 00:13:39,536 --> 00:13:42,106 I thought I would give you a couple of choice excerpts. 319 00:13:42,406 --> 00:13:43,636 We love the serious ones 320 00:13:43,686 --> 00:13:46,746 but frankly the funny ones are more fun to indulge in lecture. 321 00:13:47,096 --> 00:13:49,366 So the first one was an answer to question 13. 322 00:13:49,366 --> 00:13:51,626 Why did you decide to take CS50? 323 00:13:51,836 --> 00:13:52,436 Quote, unquote. 324 00:13:52,816 --> 00:13:54,836 "I feel like an idiot when I use computers." 325 00:13:55,696 --> 00:13:57,116 [Laughter] So fair enough. 326 00:13:57,386 --> 00:13:59,436 This one I kind of had to read twice 327 00:13:59,436 --> 00:14:02,746 because I wasn't sure really why one would answer in this way. 328 00:14:03,116 --> 00:14:05,666 Question 13, Why did you decide to take CS50? 329 00:14:05,666 --> 00:14:08,166 Answer, I've never taken CS50. 330 00:14:09,886 --> 00:14:12,366 [Laughter] Thank you. 331 00:14:13,236 --> 00:14:18,096 Some of these can only be shown, not read also 332 00:14:18,096 --> 00:14:19,216 in answer to the same. 333 00:14:19,616 --> 00:14:25,106 [Laughter] So it seems we're not the ones doing all of the damage 334 00:14:25,106 --> 00:14:26,626 because clearly some of the folks are coming 335 00:14:26,626 --> 00:14:27,786 into the course this way. 336 00:14:27,786 --> 00:14:30,236 And then finally this one was fun 337 00:14:30,236 --> 00:14:31,736 if only for the all capitals. 338 00:14:32,826 --> 00:14:33,326 There you go. 339 00:14:36,206 --> 00:14:39,146 [Laughter] So thank you for those choice responses. 340 00:14:39,246 --> 00:14:43,596 On a more serious note for just a moment. 341 00:14:43,846 --> 00:14:46,076 So this course has a lot of fun. 342 00:14:46,076 --> 00:14:47,366 It does have a lot of work. 343 00:14:47,606 --> 00:14:50,666 And it is nonetheless an independent sort of work 344 00:14:50,666 --> 00:14:52,746 but very much a shared experience, 345 00:14:52,746 --> 00:14:55,386 very much an opportunity in office hours and sections 346 00:14:55,386 --> 00:14:57,766 and just in your dorm rooms to talk to other students 347 00:14:57,766 --> 00:14:59,866 about the problems that -- what you're struggling with, 348 00:14:59,866 --> 00:15:01,176 material related to it. 349 00:15:01,426 --> 00:15:04,676 But realize there is a line between talking 350 00:15:04,676 --> 00:15:06,656 about the problem set, reasoning through how you're going 351 00:15:06,656 --> 00:15:08,706 to approach it and the act of saying, 352 00:15:09,116 --> 00:15:10,956 "Hey why don't you just look at what I've done 353 00:15:10,956 --> 00:15:12,596 or hey here's my code." 354 00:15:12,596 --> 00:15:15,006 Or in an e-mail hey here is an attachment. 355 00:15:15,006 --> 00:15:17,376 So realize the course's syllabus and repeat it 356 00:15:17,376 --> 00:15:19,976 on every problem set second page is the policy 357 00:15:19,976 --> 00:15:23,006 on academic honesty is very clear over the years 358 00:15:23,006 --> 00:15:24,776 as to what's appropriate, what is not. 359 00:15:24,776 --> 00:15:27,656 Frankly the rule of thumb is talk all you want in English 360 00:15:27,696 --> 00:15:31,136 but as soon as the conversation devolves into specific C code, 361 00:15:31,476 --> 00:15:34,046 you probably are starting to come close to 362 00:15:34,046 --> 00:15:35,236 or crossing that line. 363 00:15:35,556 --> 00:15:37,976 Let me make the message ever so clear with this-- 364 00:15:38,516 --> 00:15:49,976 [ Sound effects ] 365 00:15:50,476 --> 00:15:52,426 >> So in the past two years this is the number 366 00:15:52,426 --> 00:15:54,406 of students this course had aborted. 367 00:15:54,456 --> 00:15:56,166 It gives us no pleasure to do so. 368 00:15:56,386 --> 00:15:59,426 But do you realize when problem sets are submitted we cross 369 00:15:59,426 --> 00:16:01,106 compare every problem set against every other. 370 00:16:01,106 --> 00:16:03,136 We have archives of years worth of problem sets, 371 00:16:03,136 --> 00:16:04,366 we cross compare all of that stuff. 372 00:16:04,686 --> 00:16:06,886 It's not hard, we have computers with which to help us. 373 00:16:07,166 --> 00:16:09,186 So please let me make one personal plea 374 00:16:09,186 --> 00:16:10,216 at the start of this semester. 375 00:16:10,326 --> 00:16:14,206 Please do not put your time at this wonderful place at risk 376 00:16:14,596 --> 00:16:15,946 by doing something stupid. 377 00:16:15,946 --> 00:16:18,886 We are very good at what we do and we take again no pleasure 378 00:16:18,886 --> 00:16:21,886 in doing so but this does happen and we will do it. 379 00:16:22,286 --> 00:16:23,726 So please meet us halfway. 380 00:16:23,726 --> 00:16:24,916 We're happy to have a lot of fun. 381 00:16:25,076 --> 00:16:26,586 We're happy to do a lot of work 382 00:16:26,586 --> 00:16:28,426 but do realize those constraints. 383 00:16:28,676 --> 00:16:31,306 So, let me try to take the tension off 384 00:16:31,496 --> 00:16:33,286 and do some fun facts about CS50 now. 385 00:16:33,586 --> 00:16:34,986 So we looked at Pset just because we 386 00:16:34,986 --> 00:16:35,756 like playing with data. 387 00:16:35,756 --> 00:16:37,016 We did this already with the Google map. 388 00:16:37,156 --> 00:16:39,826 What are you guys carrying [Sound effects] Pre is not doing 389 00:16:39,826 --> 00:16:40,696 so well it seems. 390 00:16:40,696 --> 00:16:43,516 This was the Palm phone that recently came out but a lot 391 00:16:43,516 --> 00:16:45,306 of you have normal phones or smart phones. 392 00:16:45,306 --> 00:16:46,696 A lot of Blackberry's and iPhones. 393 00:16:46,926 --> 00:16:48,506 Who are you guys using if only 394 00:16:48,506 --> 00:16:50,616 because it's an interesting tidbit. 395 00:16:50,756 --> 00:16:53,026 Nobody's using Nextel but that's really my fault 396 00:16:53,026 --> 00:16:54,276 because I forgot they merged with Sprint 397 00:16:54,346 --> 00:16:55,256 so that kind of makes sense. 398 00:16:57,636 --> 00:16:59,476 [Laughter] Verizon though is quite popular, 399 00:16:59,476 --> 00:17:00,906 as an aside I'll point out -- 400 00:17:00,906 --> 00:17:02,766 and some Googling should answer this for you. 401 00:17:02,766 --> 00:17:04,796 So Harvard actually has some pretty good discounts on a lot 402 00:17:04,796 --> 00:17:07,036 of these carriers, so if you do pay your own cell phone bill -- 403 00:17:07,286 --> 00:17:11,286 don't e-mail cs50.net for this but Google around and you'll see 404 00:17:11,286 --> 00:17:13,696 that you can get like 10, 19 percent off some of these guys. 405 00:17:13,836 --> 00:17:15,476 Completely irrelevant to CS50. 406 00:17:15,766 --> 00:17:16,316 Who are you? 407 00:17:16,556 --> 00:17:18,356 So historically you're mostly sophomores 408 00:17:18,356 --> 00:17:21,126 but we do have a pretty good range of folks here. 409 00:17:21,126 --> 00:17:24,126 The sample size for all of these charts is roughly 350, 410 00:17:24,126 --> 00:17:27,066 400 people because we are also offering this course via the 411 00:17:27,066 --> 00:17:28,596 extension school as computer science E52. 412 00:17:28,596 --> 00:17:31,606 Hello to those of you tuning in from afar and it's 413 00:17:31,606 --> 00:17:34,366 to the extension school that we owe the very nice videos 414 00:17:34,366 --> 00:17:36,566 that are produced each week as well 415 00:17:36,566 --> 00:17:37,566 as one of our own producers. 416 00:17:37,906 --> 00:17:39,166 Oh, what else we got? 417 00:17:39,486 --> 00:17:41,226 So, dorms, mess hall. 418 00:17:41,456 --> 00:17:43,396 So I couldn't remember are students still living 419 00:17:43,396 --> 00:17:43,986 in mess hall? 420 00:17:44,266 --> 00:17:45,166 Or did they boot them all? 421 00:17:45,406 --> 00:17:47,226 Yes? But not here. 422 00:17:47,586 --> 00:17:50,176 Okay, so there's a few of them just not here today. 423 00:17:51,156 --> 00:17:54,756 So Apley is understandably-- Grays is a pretty damn big dorm 424 00:17:54,756 --> 00:17:57,776 and no one from Grays is apparently taking 50. 425 00:17:58,066 --> 00:18:00,636 But the houses, we have this demographic here 426 00:18:00,856 --> 00:18:02,356 and this is complete coincidence. 427 00:18:02,356 --> 00:18:04,626 So the most popular house in terms 428 00:18:04,626 --> 00:18:07,046 of taking CS50 this year is no longer Levritt [Assumed 429 00:18:07,356 --> 00:18:10,816 spelling] it is in fact Mather, which is perhaps -- 430 00:18:13,796 --> 00:18:15,186 [Applause] which is perhaps not surprising 431 00:18:15,186 --> 00:18:18,766 because on this same survey when asked what house would you want 432 00:18:18,906 --> 00:18:21,666 to live in if given the choice everyone said, 433 00:18:21,746 --> 00:18:22,646 "Mather" actually. 434 00:18:23,286 --> 00:18:28,146 [Applause] So what else, random little facts. 435 00:18:28,146 --> 00:18:28,916 What are you using? 436 00:18:28,916 --> 00:18:30,976 Well there's diversity even in operating systems. 437 00:18:30,976 --> 00:18:32,356 A bunch of you have clearly upgraded 438 00:18:32,356 --> 00:18:34,116 to Snow Leopard, some of you have not. 439 00:18:34,116 --> 00:18:38,026 The most common answer in other is MAC OS10.4, which is fine 440 00:18:38,026 --> 00:18:39,966 but I think you can even get upgrades from Harvard 441 00:18:39,966 --> 00:18:41,146 as well from the helpdesk. 442 00:18:41,366 --> 00:18:42,576 Why are you taking CS50? 443 00:18:42,756 --> 00:18:46,176 So certainly allots for concentration credit. 444 00:18:46,386 --> 00:18:48,966 But what's really neat to see is just how many folks honestly are 445 00:18:48,966 --> 00:18:52,876 taking it as a pure elective and then also these days for GenEd 446 00:18:52,876 --> 00:18:54,186 or formerly core credits. 447 00:18:54,536 --> 00:18:55,096 Who are you? 448 00:18:55,096 --> 00:18:57,616 So this one is actually more useful I think to know, 449 00:18:57,846 --> 00:18:59,986 to reassure perhaps at this point in the semester. 450 00:18:59,986 --> 00:19:02,166 So this is consistent with last year's number. 451 00:19:02,166 --> 00:19:05,486 There's absolutely some people in here, maybe one of the people 452 00:19:05,486 --> 00:19:08,356 or you know maybe a tenth of the person sitting next to you 453 00:19:08,356 --> 00:19:11,776 who knows more than you do, if not significantly so. 454 00:19:12,046 --> 00:19:12,676 And that's fine. 455 00:19:12,746 --> 00:19:14,636 That's why we have the hacker editions of Psets. 456 00:19:14,696 --> 00:19:17,536 That's why we push the envelope sometimes in lecture 457 00:19:17,536 --> 00:19:21,056 with some slightly more advanced material but realize 458 00:19:21,116 --> 00:19:24,596 that the meat, the sweet spot here is those in between 459 00:19:24,876 --> 00:19:26,046 and those less comfortable. 460 00:19:26,046 --> 00:19:28,706 So if you still have this mentality that this person 461 00:19:28,756 --> 00:19:31,026 and this person both know more than you in this room, 462 00:19:31,426 --> 00:19:34,396 probably not the case just based on pure statistics alone. 463 00:19:34,716 --> 00:19:36,966 Prior courses, just too really hammer this through. 464 00:19:37,186 --> 00:19:39,156 There's this misconception on campus for years 465 00:19:39,156 --> 00:19:41,816 that almost everyone who takes CS50 has prior course work 466 00:19:41,816 --> 00:19:43,916 in computer science, AP computer science. 467 00:19:44,356 --> 00:19:46,846 Not so. What are you all whispering about? 468 00:19:46,846 --> 00:19:48,136 This is all of your data you know, 469 00:19:48,776 --> 00:19:52,406 so zero courses is the most popular answer here. 470 00:19:53,206 --> 00:19:54,896 So, welcome to CS50. 471 00:19:54,896 --> 00:19:58,346 That is who you are and we hope you enjoy your stay with us. 472 00:19:58,746 --> 00:20:02,736 So how do you write a program in languages other than C just 473 00:20:02,736 --> 00:20:05,316 so that you're clear that this is not a course 474 00:20:05,316 --> 00:20:08,276 on C. This is not going to be a course on PHP or JavaScript. 475 00:20:08,276 --> 00:20:10,826 It's an introductory computer science course that just 476 00:20:10,826 --> 00:20:12,766 so happens to use those several languages 477 00:20:12,796 --> 00:20:15,496 to explore what are ultimately some really interesting 478 00:20:15,496 --> 00:20:16,976 and really fundamental topics. 479 00:20:17,246 --> 00:20:20,366 Well you don't have to necessarily use C to go 480 00:20:20,366 --> 00:20:21,936 about implementing programs. 481 00:20:21,936 --> 00:20:25,756 The canonical program for instance in Java is something 482 00:20:25,756 --> 00:20:26,906 that might look like this. 483 00:20:26,956 --> 00:20:30,156 If I want to write hello dot Java I can use -- 484 00:20:30,296 --> 00:20:32,176 actually I'll use the program familiar. 485 00:20:32,176 --> 00:20:33,246 Hello dot Java. 486 00:20:33,246 --> 00:20:35,096 So Java has different conventions, 487 00:20:35,096 --> 00:20:36,726 usually capitalize the file name. 488 00:20:36,986 --> 00:20:40,046 It ends in dot Java and frankly this is where languages 489 00:20:40,076 --> 00:20:43,726 like Java get a little annoying though Cs not much better. 490 00:20:43,726 --> 00:20:46,556 First we need to say class hello, whatever that means. 491 00:20:46,946 --> 00:20:50,986 Then we say public, static, void, main, 492 00:20:50,986 --> 00:20:53,026 string, bracket, args. 493 00:20:53,026 --> 00:20:54,786 But there's some shared features there right. 494 00:20:54,786 --> 00:20:56,236 There's this main keyword. 495 00:20:56,236 --> 00:20:57,366 There's the square brackets. 496 00:20:57,426 --> 00:21:00,856 Arg sound like argv so there's definitely some similarities. 497 00:21:01,076 --> 00:21:02,396 And now if I want to go ahead 498 00:21:02,396 --> 00:21:06,196 and say print something it can be printf 499 00:21:06,336 --> 00:21:08,846 because there is a printf function in C 500 00:21:09,096 --> 00:21:11,976 but it's usually system dot, out dot, print line 501 00:21:11,976 --> 00:21:16,316 and then hello world, exclamation point, semi colon. 502 00:21:16,556 --> 00:21:17,376 It's pretty similar. 503 00:21:17,606 --> 00:21:19,516 So I'm going to go ahead and hot control X 504 00:21:19,586 --> 00:21:21,966 to save this, Y to save yes. 505 00:21:21,966 --> 00:21:23,996 And now I'm going to run Java C, 506 00:21:24,256 --> 00:21:26,956 which is the Java compiler on hello dot Java. 507 00:21:27,406 --> 00:21:29,156 It seems to take a moment then I have 508 00:21:29,156 --> 00:21:31,716 in my directory here not only today's other source code, 509 00:21:31,716 --> 00:21:33,416 for which you have printouts but notice 510 00:21:33,496 --> 00:21:36,916 from this file came a file called hello dot class. 511 00:21:37,196 --> 00:21:39,916 Because in the world of Java you say Java and then the name 512 00:21:39,916 --> 00:21:42,916 of the file without that file extension enter viola. 513 00:21:43,066 --> 00:21:44,506 So my first program in Java. 514 00:21:44,506 --> 00:21:47,816 So just realize when we introduce certain features here 515 00:21:47,816 --> 00:21:50,366 if they're not necessarily specific to C 516 00:21:50,556 --> 00:21:53,196 and frankly the only -- as I may have said 2 weeks ago, 517 00:21:53,196 --> 00:21:56,026 the only language -- actually I said this outside 518 00:21:56,026 --> 00:21:56,776 of class to someone. 519 00:21:56,976 --> 00:21:59,376 The only languages I think I was ever taught quote, 520 00:21:59,376 --> 00:22:01,056 unquote was C in CS50. 521 00:22:01,056 --> 00:22:04,046 A little bit of C plus plus and Lisp in CS51 522 00:22:04,046 --> 00:22:07,126 and then pretty much after that everything I've learned 523 00:22:07,126 --> 00:22:08,316 since has been on my own. 524 00:22:08,316 --> 00:22:10,986 PHP, JavaScript, I never took courses in these languages. 525 00:22:10,986 --> 00:22:14,026 PHP I literally learned by IMing a friend back and forth 526 00:22:14,026 --> 00:22:16,266 over the course of a weekend asking him how do I do this, 527 00:22:16,266 --> 00:22:20,396 how do I do this and started to finally RTFM the manual myself 528 00:22:20,766 --> 00:22:22,436 when he advised me to do so. 529 00:22:23,106 --> 00:22:25,946 [Laughter] So realize that what you'll get is this ability 530 00:22:25,946 --> 00:22:28,206 to parlay a lot of what you learn in this course 531 00:22:28,446 --> 00:22:29,906 to other context all together. 532 00:22:29,906 --> 00:22:32,436 Lisp is another language scheme, something similar. 533 00:22:32,596 --> 00:22:35,876 You'll be introduced to those in different courses but Pearl, 534 00:22:36,186 --> 00:22:38,496 PHP these are languages so similar to C 535 00:22:38,756 --> 00:22:41,286 that you'll probably pick them up fairly easily. 536 00:22:41,896 --> 00:22:46,386 So bugs, it turns out and actually this is a bit of a myth 537 00:22:46,386 --> 00:22:48,746 or white lie but it turns out the first bug 538 00:22:48,746 --> 00:22:51,156 in a computer program was in fact a bug. 539 00:22:51,156 --> 00:22:54,086 There was a computer at Harvard called the Mark 2. 540 00:22:54,086 --> 00:22:55,556 Grace Hopper was the engineer 541 00:22:55,556 --> 00:22:58,256 who was among the people responsible for this machine 542 00:22:58,256 --> 00:23:00,246 and as the story goes there was a problem 543 00:23:00,246 --> 00:23:01,916 with the Mark 1 computer at one point. 544 00:23:02,196 --> 00:23:05,326 And sure enough when some humans went inside what was literally 545 00:23:05,326 --> 00:23:07,066 inside of the computer, it was in fact 546 00:23:07,066 --> 00:23:08,196 that large they found a moth 547 00:23:08,526 --> 00:23:10,266 that was wrecking some havoc inside. 548 00:23:10,506 --> 00:23:14,216 And now supposedly bug had been used for years prior 549 00:23:14,216 --> 00:23:16,636 to describe problems with machines and so forth. 550 00:23:16,786 --> 00:23:20,026 But to debug is supposedly perhaps one 551 00:23:20,026 --> 00:23:22,446 of the treasures we're left with from that particular story 552 00:23:22,446 --> 00:23:25,956 because the act of debugging this computer was literally the 553 00:23:25,956 --> 00:23:28,156 act of debugging this computer. 554 00:23:28,206 --> 00:23:30,206 But with that said let's actually look 555 00:23:30,206 --> 00:23:33,756 at what's a more modern type of bug in this program here. 556 00:23:33,756 --> 00:23:37,316 So you have this printout, this is buggy 1 dot C. Let me ask 557 00:23:37,756 --> 00:23:40,136 that you turn to the person next to you if you don't -- 558 00:23:40,276 --> 00:23:45,136 turn to a person next to you whom you do not know, say hello 559 00:23:45,296 --> 00:23:46,546 and then together figure 560 00:23:46,546 --> 00:23:48,376 out what the buggy is in this program. 561 00:23:48,446 --> 00:23:48,976 Take about a minute. 562 00:23:49,516 --> 00:25:07,056 [ Sound effects ] 563 00:25:07,556 --> 00:25:09,236 >> Okay, I've been waiting to do that for like a minute. 564 00:25:09,236 --> 00:25:12,376 Alright, so what is the bug? 565 00:25:12,376 --> 00:25:12,906 Anyone who found it. 566 00:25:12,906 --> 00:25:12,973 [Inaudible background discussion] 567 00:25:12,973 --> 00:25:15,556 So it prints 11 stars instead of the 10. 568 00:25:15,556 --> 00:25:17,776 And it's simply because programmers tend to count 569 00:25:17,776 --> 00:25:20,786 from 0, which this person has done but they're counting 570 00:25:20,836 --> 00:25:24,736 up to an equal to 10 and if you actually do the math 0 571 00:25:24,736 --> 00:25:27,766 through 10 inclusive is in fact 11 stars. 572 00:25:27,766 --> 00:25:29,016 So there's the bug there. 573 00:25:29,016 --> 00:25:30,926 Excellent, that was some pretty good chatter. 574 00:25:30,926 --> 00:25:32,366 If you weren't among those who kind 575 00:25:32,456 --> 00:25:33,876 of realized what trajectory we're 576 00:25:33,876 --> 00:25:36,336 on this second one is maybe a little easier. 577 00:25:36,336 --> 00:25:37,606 And let's just see if you can spot it. 578 00:25:38,666 --> 00:25:39,766 Don't shout it out just yet. 579 00:25:40,686 --> 00:25:41,526 Let everyone flip. 580 00:25:42,476 --> 00:25:44,416 And now what's the bug? 581 00:25:44,446 --> 00:25:44,636 Yes? 582 00:25:44,636 --> 00:25:44,703 >> [Inaudible background discussion] 583 00:25:44,703 --> 00:25:50,116 >> Sorry. Can you say it a little louder, 584 00:25:50,546 --> 00:25:53,966 just a little loud back here. 585 00:25:54,506 --> 00:25:56,256 >> Prints 11 vertically. 586 00:25:56,256 --> 00:25:59,916 >> So it prints 11 vertically but it doesn't, it supposed to. 587 00:25:59,916 --> 00:26:01,326 Why -- what's the problem here? 588 00:26:01,326 --> 00:26:01,393 >> [Inaudible background discussion] 589 00:26:01,393 --> 00:26:06,106 >> Yeah so I've forgotten the curly braces. 590 00:26:06,106 --> 00:26:08,756 So if you notice the program kind of looks okay right, 591 00:26:08,806 --> 00:26:10,996 here's a loop and yes it does prints -- 592 00:26:11,096 --> 00:26:13,236 okay 11 again so we can fix that. 593 00:26:13,236 --> 00:26:16,296 So same bug, we didn't actually fix that problem here. 594 00:26:16,476 --> 00:26:19,986 But a quick fix there gets rid of that but if this is supposed 595 00:26:19,986 --> 00:26:22,716 to print 10 stars, 1 per line -- let me go ahead and check. 596 00:26:22,716 --> 00:26:28,706 So GCC dash o buggy 2 and then buggy 2 dot C is again my 597 00:26:28,706 --> 00:26:29,616 compiler statement. 598 00:26:29,616 --> 00:26:34,126 Now I can run buggy 2 enter, they're all on the same line. 599 00:26:34,126 --> 00:26:35,226 Not one per line and yeah 600 00:26:35,476 --> 00:26:37,076 so remember even though you might indent 601 00:26:37,076 --> 00:26:38,696 because it looks pretty, you've been told it's a matter 602 00:26:38,696 --> 00:26:40,576 of good style, the computer doesn't care. 603 00:26:40,576 --> 00:26:43,046 So again this was that subtle thing that now encasing this 604 00:26:43,046 --> 00:26:45,506 in curly braces means don't just execute the next line, 605 00:26:45,706 --> 00:26:48,816 execute the next bunch of lines and this should 606 00:26:48,816 --> 00:26:51,476 if I rerun it fix that. 607 00:26:53,276 --> 00:26:53,716 [Sound effects] Okay, right. 608 00:26:53,896 --> 00:26:58,126 So again here to common goof early on, I need to recompile. 609 00:26:58,126 --> 00:27:01,366 Same exact command, now I can run buggy 2 and I do 610 00:27:01,366 --> 00:27:02,866 in fact get 1 per line. 611 00:27:02,866 --> 00:27:03,426 Excellent. 612 00:27:03,426 --> 00:27:06,456 So that's a little warm up exercise, 613 00:27:06,456 --> 00:27:07,896 they'll get a little more sophisticated. 614 00:27:08,036 --> 00:27:09,426 Let's introduce a new capability. 615 00:27:09,426 --> 00:27:11,456 So we knew from the very first week of class 616 00:27:11,856 --> 00:27:14,356 that we have this ability to represent data 617 00:27:14,356 --> 00:27:15,616 in the computer, numbers. 618 00:27:15,996 --> 00:27:18,366 And we do it with this, turn on the switch. 619 00:27:18,366 --> 00:27:19,256 Turn off the switch. 620 00:27:19,476 --> 00:27:22,246 You have some means of storing bits inside of a computer. 621 00:27:22,246 --> 00:27:24,116 From where does that come, that capability? 622 00:27:24,416 --> 00:27:26,656 Well computers are generally plugged into the wall 623 00:27:26,786 --> 00:27:29,256 or they have batteries, so there's some aspect 624 00:27:29,256 --> 00:27:31,666 of electricity underlying any computer. 625 00:27:31,906 --> 00:27:33,606 For today's purposes you can assume 626 00:27:33,606 --> 00:27:36,226 that with electricity can you represent two states 627 00:27:36,226 --> 00:27:37,056 very easily. 628 00:27:37,426 --> 00:27:38,456 On or off. 629 00:27:38,566 --> 00:27:42,276 There's either some electricity there flowing or there is not. 630 00:27:42,396 --> 00:27:45,046 Or technically there's some electricity that's tripped a 631 00:27:45,046 --> 00:27:48,296 transistor, a little switch, a tiny, tiny little switch 632 00:27:48,546 --> 00:27:49,716 that turns a state on. 633 00:27:49,976 --> 00:27:53,216 Or no such electricity flow to the transistor stayed off. 634 00:27:53,216 --> 00:27:56,216 So very simple ultimately to represent two different states 635 00:27:56,216 --> 00:27:58,226 in computers, not that useful 636 00:27:58,226 --> 00:28:00,706 if all you can express though are 0s and 1s. 637 00:28:00,886 --> 00:28:03,286 But thankfully we had our bytes of volunteers come up 638 00:28:03,286 --> 00:28:06,756 and by raising or lowering their hands those 8 bits represented 639 00:28:06,756 --> 00:28:07,346 a byte. 640 00:28:07,346 --> 00:28:11,536 But that byte, a number could be mapped to a letter by way 641 00:28:11,536 --> 00:28:16,606 of that little coding scheme called ASCII, A-S-C-I-I. 642 00:28:16,646 --> 00:28:18,546 Just a standard the world came up with years ago 643 00:28:18,546 --> 00:28:23,416 that just says capital A is 65. 644 00:28:23,416 --> 00:28:25,306 Random trivia but actually useful 645 00:28:25,306 --> 00:28:27,336 because from there can you bootstrap yourself to most 646 00:28:27,336 --> 00:28:30,196 of the other capital letters and 97 is lower case A. 647 00:28:30,506 --> 00:28:33,436 So there's clearly a way of representing letters 648 00:28:33,536 --> 00:28:36,796 with numbers but how do you the programmer get access 649 00:28:36,866 --> 00:28:38,096 to that kind of capability? 650 00:28:38,346 --> 00:28:39,836 Well through this ability called typecasting 651 00:28:39,836 --> 00:28:42,766 and we talked briefly last week about typecasting. 652 00:28:42,766 --> 00:28:45,526 But we casted things like ints to floats 653 00:28:45,526 --> 00:28:47,746 so that we could actually use a decimal point. 654 00:28:48,066 --> 00:28:50,656 But if you know, you the computer scientists know 655 00:28:50,656 --> 00:28:53,086 that this character, the capital A, 656 00:28:53,326 --> 00:28:55,196 I know this is just the number 65. 657 00:28:55,196 --> 00:28:56,446 How do I get at that number? 658 00:28:56,676 --> 00:29:00,226 Well you can tell the compiler, "Give me the numeric value 659 00:29:00,226 --> 00:29:02,516 of this character" by way of typecasting. 660 00:29:02,556 --> 00:29:06,006 To typecast one type to another means to change it 661 00:29:06,046 --> 00:29:09,616 if it's possible conceptually from one type to another. 662 00:29:09,846 --> 00:29:11,896 So here's two lines of code clearly out of context, 663 00:29:11,976 --> 00:29:14,606 clearly not a real program because we have no main function 664 00:29:14,606 --> 00:29:15,456 or anything like that. 665 00:29:15,816 --> 00:29:18,646 But here's two lines of code, int I is going 666 00:29:18,646 --> 00:29:22,976 to get the integral value of quote-unquote A and by contrast 667 00:29:22,976 --> 00:29:26,586 or conversely C, the little c variable is going 668 00:29:26,586 --> 00:29:29,876 to get the character value of 65. 669 00:29:29,876 --> 00:29:31,496 So this is just showing you one direction 670 00:29:31,736 --> 00:29:34,516 and then the other direction but notice the single quotes. 671 00:29:34,516 --> 00:29:37,166 We reserve double quotes last week for strings 672 00:29:37,466 --> 00:29:40,036 and a string is anything that has, 673 00:29:40,286 --> 00:29:42,566 for simplicity let's say two or more characters. 674 00:29:42,566 --> 00:29:45,276 But you can have strings that just have 1 character but more 675 00:29:45,276 --> 00:29:46,266 on that later this week. 676 00:29:46,436 --> 00:29:49,776 But single characters like this, like A are just single quotes. 677 00:29:49,776 --> 00:29:52,746 So let's actually make use of this information. 678 00:29:52,746 --> 00:29:55,686 Here's a little demo, ASCII 1 that's going 679 00:29:55,686 --> 00:29:57,036 to show me the mapping. 680 00:29:57,036 --> 00:29:59,296 So that's actually -- I'm kind of overwhelmed by the code there 681 00:29:59,296 --> 00:30:04,536 so let me go ahead and go back and do GCC dash O ASCII 1 682 00:30:04,826 --> 00:30:09,426 and then S. That's a little gap there, sorry. 683 00:30:09,486 --> 00:30:11,976 [Laughter] ASCII 1 and now let me go ahead 684 00:30:11,976 --> 00:30:14,206 and run ASCII 1 enter. 685 00:30:14,646 --> 00:30:17,846 Okay, interesting a little overwhelming but okay, 686 00:30:17,846 --> 00:30:19,876 looks like this is just a stupid little program 687 00:30:19,876 --> 00:30:22,396 that prints a chart of alphabetical letters 688 00:30:22,396 --> 00:30:25,786 and then their corresponding ASCII integral values. 689 00:30:25,786 --> 00:30:26,916 So how would I do this? 690 00:30:27,126 --> 00:30:29,596 Well hopefully I didn't copy and paste the same line 691 00:30:29,596 --> 00:30:32,176 of code 26 or 52 times right. 692 00:30:32,176 --> 00:30:34,696 Hopefully there's a more intelligent way and there is, 693 00:30:34,696 --> 00:30:36,926 just from last week when we introduced loops. 694 00:30:37,186 --> 00:30:40,756 So we have this ability here, we're declaring I just 695 00:30:40,756 --> 00:30:42,386 because I wanted a variable up front 696 00:30:42,386 --> 00:30:43,616 that I wanted to use twice. 697 00:30:43,616 --> 00:30:45,016 So I said, "Let's put it up front." 698 00:30:45,406 --> 00:30:47,926 I didn't bother commenting it because to put a comment 699 00:30:47,926 --> 00:30:50,156 for int I, you know you could comment it 700 00:30:50,186 --> 00:30:51,426 but there's a line there somewhere 701 00:30:51,426 --> 00:30:52,666 where it's a little excessive maybe 702 00:30:52,666 --> 00:30:53,966 to comment something like I. 703 00:30:54,346 --> 00:30:55,256 But let's see. 704 00:30:55,316 --> 00:30:56,136 What am I doing here? 705 00:30:56,136 --> 00:30:57,306 I've got comments telling me. 706 00:30:57,306 --> 00:31:03,236 So let's see, four I gets 65, I is less than 65 plus 26 707 00:31:03,236 --> 00:31:05,006 and then I plus, plus. 708 00:31:05,076 --> 00:31:06,856 But remember the order of operations there, 709 00:31:06,856 --> 00:31:09,216 so with the four loop the initialization happens, 710 00:31:09,526 --> 00:31:12,646 the condition gets checked then the stuff happens. 711 00:31:12,956 --> 00:31:15,986 Then the increment happens and then you repeat except 712 00:31:15,986 --> 00:31:18,756 for the initialization, which as the word implies only happens 713 00:31:18,756 --> 00:31:19,956 once, when you initialize. 714 00:31:20,286 --> 00:31:22,776 So it looks like this loop starts I at 65, 715 00:31:23,166 --> 00:31:26,636 it prints its character and integral value how? 716 00:31:26,636 --> 00:31:27,796 Well let's see. 717 00:31:27,796 --> 00:31:30,236 Printf here is saying print a character 718 00:31:30,236 --> 00:31:33,136 with this little format code, then print a colon literally, 719 00:31:33,136 --> 00:31:34,236 then a space literally, 720 00:31:34,236 --> 00:31:37,026 then a decimal integer and then a new line. 721 00:31:37,176 --> 00:31:39,386 Well what do I want to swap into each of those places? 722 00:31:39,546 --> 00:31:43,436 Well not just I but the character equivalent of I. 723 00:31:43,686 --> 00:31:47,316 So I typecast I but then at the end here I just want I itself 724 00:31:47,716 --> 00:31:49,146 so I just string it in. 725 00:31:49,146 --> 00:31:50,496 Alright, so pretty straight forward and then 726 00:31:50,496 --> 00:31:53,766 in fact the stuff down below is identical except I started 727 00:31:53,766 --> 00:31:55,606 from 97 and went on up. 728 00:31:55,866 --> 00:32:00,146 Now there's no need, realize to hard code things like 65 and 97. 729 00:32:00,146 --> 00:32:02,906 This particular example is just meant to demonstrate the mapping 730 00:32:03,166 --> 00:32:04,876 because we can actually generalize this. 731 00:32:04,976 --> 00:32:06,646 But all this is doing is showing me 732 00:32:06,786 --> 00:32:10,166 that in fact what we preached 2 weeks ago is clearly the case 733 00:32:10,276 --> 00:32:11,186 underneath the hood. 734 00:32:11,396 --> 00:32:13,416 Well let's look at a slightly different example. 735 00:32:13,416 --> 00:32:17,246 So this is the second version, ASCII 2 and I got a little fancy 736 00:32:17,246 --> 00:32:19,416 because what I didn't like a moment ago was 737 00:32:19,416 --> 00:32:22,026 when I ran ASCII 1, it's just unmanageable. 738 00:32:22,026 --> 00:32:24,606 The big font size, small font size it just doesn't fit. 739 00:32:24,936 --> 00:32:27,666 Let me see if I can start using my formatting strings 740 00:32:27,696 --> 00:32:29,586 to lay things out a little more cleverly. 741 00:32:29,936 --> 00:32:33,396 So let's see this time I just have 1 loop from 65 742 00:32:33,396 --> 00:32:35,126 up to 26 places later. 743 00:32:35,516 --> 00:32:37,186 And then okay I got a little creative here, 744 00:32:37,186 --> 00:32:40,656 so I have character -- a character, some space, an int, 745 00:32:41,016 --> 00:32:44,176 a bunch more space and then 3 D 746 00:32:44,276 --> 00:32:47,006 so there's no decimal point there, so what might it mean 747 00:32:47,186 --> 00:32:54,736 in a format code to say 3 and then int, the little D? 748 00:32:54,986 --> 00:33:01,826 It's not specifying number of decimal places but -- Yeah? 749 00:33:01,826 --> 00:33:02,446 >> [Inaudible background discussion] 750 00:33:02,446 --> 00:33:04,316 >> Yeah. Exactly. 751 00:33:04,316 --> 00:33:06,686 So the numbers that we're going to be playing 752 00:33:06,686 --> 00:33:08,566 with might actually over -- 753 00:33:08,656 --> 00:33:12,336 if we're starting at 97 for lower case 97, 98, 99, 754 00:33:12,336 --> 00:33:15,866 100 which is 3 digits whereas 97 is just 2. 755 00:33:16,066 --> 00:33:18,036 So if we're iterating over this loop but I want 756 00:33:18,036 --> 00:33:19,456 to exercise some control -- 757 00:33:19,456 --> 00:33:22,256 problem with playing music during class, 758 00:33:22,346 --> 00:33:23,736 just hung up on FASIT. 759 00:33:23,736 --> 00:33:26,276 [Laughter] That was my phone 760 00:33:26,276 --> 00:33:27,516 if you didn't realize what was ringing. 761 00:33:27,796 --> 00:33:29,376 Apparently I'm the only one anyway. 762 00:33:29,436 --> 00:33:32,186 No comment about the phone in the future. 763 00:33:32,356 --> 00:33:33,716 [Laughter] So what I want 764 00:33:33,716 --> 00:33:37,036 to do is keeps this formatting pretty, so let me see. 765 00:33:37,036 --> 00:33:41,666 So this going to say put an int here but reserve 3 places 766 00:33:41,666 --> 00:33:44,366 for it not just the standard 1 or 2 or 3. 767 00:33:44,586 --> 00:33:46,666 So what am I plugging in after that F character? 768 00:33:46,666 --> 00:33:49,506 So I've got 4 format codes, the first one's going 769 00:33:49,506 --> 00:33:52,416 to take the character equivalent of I, the second just I. 770 00:33:52,846 --> 00:33:55,096 Oh this is interesting, I kind of did some math 771 00:33:55,096 --> 00:33:56,056 and realized you know what? 772 00:33:56,256 --> 00:33:59,026 The lowercase letters are always the same distance 773 00:33:59,296 --> 00:34:04,396 from the uppercase 97 is 32 places away from 65 774 00:34:04,466 --> 00:34:05,586 if you just do the arithmetic. 775 00:34:05,956 --> 00:34:06,426 So you know what? 776 00:34:06,426 --> 00:34:08,906 I don't need a separate loop to do the lowercase letters, 777 00:34:08,906 --> 00:34:11,416 I can simply do both at once just 778 00:34:11,416 --> 00:34:13,526 by using a little clever arithmetic. 779 00:34:13,526 --> 00:34:16,646 An offset if you will because the first time I print I's 780 00:34:16,706 --> 00:34:21,146 character and then I but the second time I print I plus 32 781 00:34:21,186 --> 00:34:24,196 and then its character equivalent so it feels 782 00:34:24,196 --> 00:34:26,766 like what I'm going to get here is a slightly more 783 00:34:26,766 --> 00:34:27,686 interesting chart. 784 00:34:27,686 --> 00:34:30,096 So let me go ahead and run ASCII 2, 785 00:34:30,096 --> 00:34:34,286 I'm going to compile its code here, thought C, okay, 786 00:34:34,286 --> 00:34:37,176 compiles okay, ASCII 2, enter, okay. 787 00:34:37,526 --> 00:34:39,846 So baby step toward something a little more interesting 788 00:34:39,846 --> 00:34:41,646 but definitely more useful, cleaner. 789 00:34:41,836 --> 00:34:44,956 And notice now what I was getting at with the format code. 790 00:34:44,956 --> 00:34:48,356 97 just takes 2 places, then there's 98, 99, 791 00:34:48,566 --> 00:34:50,446 everything kind of lined up nicely. 792 00:34:50,666 --> 00:34:55,216 What if I had not done so, let me go back in here and sort 793 00:34:55,216 --> 00:34:57,446 of not realize this capability. 794 00:34:57,506 --> 00:35:00,536 Change that to what I know from last week, recompile, 795 00:35:01,446 --> 00:35:03,976 recompile as -- rerun ASCII 2 796 00:35:04,376 --> 00:35:06,626 and [Sound effects] it kind of gets messy. 797 00:35:06,626 --> 00:35:09,856 It gets ugly quickly and if you think now about how you're going 798 00:35:09,856 --> 00:35:12,886 to present any textual information for problem set 1, 799 00:35:13,216 --> 00:35:14,976 you can think of your terminal window. 800 00:35:15,056 --> 00:35:17,396 This SSH window for historical reasons is kind 801 00:35:17,396 --> 00:35:18,716 of a terminal window 802 00:35:19,006 --> 00:35:21,946 and by default it's typically 80 characters wide 803 00:35:22,226 --> 00:35:23,866 and 24 characters tall. 804 00:35:24,076 --> 00:35:27,466 Now on 30 inch monitors, 20 inch monitors whatever these days, 805 00:35:27,466 --> 00:35:29,296 you can clearly drag a putty window 806 00:35:29,496 --> 00:35:31,536 or drag a MAC OS terminal window to be much, 807 00:35:31,536 --> 00:35:32,696 much larger than that. 808 00:35:32,726 --> 00:35:33,986 So this is somewhat historical 809 00:35:34,216 --> 00:35:37,476 but for the first problem set we specifically 810 00:35:37,476 --> 00:35:39,246 for the Mario pyramid will tell you 811 00:35:39,246 --> 00:35:41,496 to assume only a certain screen size 812 00:35:41,736 --> 00:35:43,866 so that you can actually present the information 813 00:35:43,896 --> 00:35:45,516 in a predictable way. 814 00:35:45,806 --> 00:35:47,806 So I would argue that this is just ugly, 815 00:35:47,806 --> 00:35:50,006 you know it's not wrong but we can do better. 816 00:35:50,006 --> 00:35:51,706 And that's again something so simple. 817 00:35:51,706 --> 00:35:53,766 The format code instead of doing point 818 00:35:53,926 --> 00:35:55,086 and then a number then d -- 819 00:35:55,086 --> 00:35:58,656 rather point, a number then F. We just do a number and D 820 00:35:58,756 --> 00:36:01,976 and that just gives you the width of the place that you want 821 00:36:01,976 --> 00:36:05,046 to reserve from some -- for some variable's value. 822 00:36:05,266 --> 00:36:07,646 Alright let's take a look at a third version here, 823 00:36:07,876 --> 00:36:11,386 so this one is slightly more clever. 824 00:36:11,386 --> 00:36:14,446 I don't need it to do so much math in the second line 825 00:36:14,446 --> 00:36:16,176 and I can kind of integrate it all above. 826 00:36:16,466 --> 00:36:16,806 You know what? 827 00:36:16,806 --> 00:36:20,116 If characters are just numbers, well why am I doing this back 828 00:36:20,306 --> 00:36:21,526 and forth and back and forth? 829 00:36:21,526 --> 00:36:24,176 Why don't I just iterate over the characters themselves? 830 00:36:24,206 --> 00:36:27,136 Now arguably the first approach was pretty darn clear, 831 00:36:27,406 --> 00:36:30,566 so realize this too is meant more for instructional purposes 832 00:36:30,566 --> 00:36:32,846 than for here's how you should have done this. 833 00:36:33,166 --> 00:36:34,606 But let's see what we can do 834 00:36:34,606 --> 00:36:37,356 because C does give us this low level access 835 00:36:37,356 --> 00:36:39,136 to characters representation. 836 00:36:39,366 --> 00:36:42,596 So a 4 loop is initializing a character C to quote, 837 00:36:42,596 --> 00:36:46,446 unquote A. Turns out because C is just a number you can say, 838 00:36:46,446 --> 00:36:49,656 "Is this character less than or equal to Z", quote, 839 00:36:49,656 --> 00:36:50,806 unquote, single quotes. 840 00:36:51,096 --> 00:36:52,136 And then just to be ever 841 00:36:52,136 --> 00:36:54,576 so clear I did show the typecasting here. 842 00:36:54,886 --> 00:36:56,556 On each iteration you don't have 843 00:36:56,736 --> 00:36:58,896 to do something like I plus, plus. 844 00:36:59,296 --> 00:37:01,356 What you can really do is a whole expression 845 00:37:01,356 --> 00:37:04,736 and I will push the limits of my handwriting here. 846 00:37:04,996 --> 00:37:10,686 This I plus, plus is the same as doing I equals I, 847 00:37:11,146 --> 00:37:15,416 plus 1 so I could put either of this or this 848 00:37:15,416 --> 00:37:17,446 in the update field [Inaudible]. 849 00:37:17,766 --> 00:37:20,536 It's just the world and myself prefers the top left one just 850 00:37:20,536 --> 00:37:22,516 because it's a lot sexier, it's a lot simpler, 851 00:37:22,516 --> 00:37:24,016 it's more compact, I plus, plus. 852 00:37:24,316 --> 00:37:26,196 But realize I'm taking advantage 853 00:37:26,196 --> 00:37:28,346 of the latter approach down here. 854 00:37:28,476 --> 00:37:30,376 I'm saying C gets what? 855 00:37:30,556 --> 00:37:31,406 Well what do I want to do? 856 00:37:31,406 --> 00:37:34,576 I want to first cast C to an int to get it to a number. 857 00:37:34,856 --> 00:37:36,506 Then I want to add 1 to it, 858 00:37:36,756 --> 00:37:38,166 okay but I don't want a number now I have 859 00:37:38,166 --> 00:37:40,096 to cast the whole thing back to a char 860 00:37:40,346 --> 00:37:44,436 because I'm assigning it again to the variable C. Now it turns 861 00:37:44,436 --> 00:37:47,236 out that I actually don't need all this typecasting. 862 00:37:47,236 --> 00:37:49,276 This example has it just for clarity 863 00:37:49,276 --> 00:37:52,366 as to what the casting -- what casting is going on. 864 00:37:52,596 --> 00:37:54,586 But a lot of this typecasting can 865 00:37:54,636 --> 00:37:57,456 and will happen implicitly even if you leave it off. 866 00:37:57,776 --> 00:37:59,316 But again the takeaways for now are just 867 00:37:59,356 --> 00:38:01,196 that there is this equivalence. 868 00:38:01,256 --> 00:38:02,446 But let's do something 869 00:38:02,736 --> 00:38:07,796 that makes this slightly more compelling, so Battleship. 870 00:38:08,426 --> 00:38:11,436 So at first glance we clearly have some more code here, 871 00:38:11,436 --> 00:38:14,596 looks a little cryptic but again I see nothing on the board 872 00:38:14,596 --> 00:38:16,486 that we haven't seen in other context already. 873 00:38:16,486 --> 00:38:17,766 Some printf's, some for loops, 874 00:38:18,026 --> 00:38:21,236 some variables and really that's it. 875 00:38:21,386 --> 00:38:24,246 So a common, you know motivation in a course is -- 876 00:38:24,306 --> 00:38:27,036 you know I don't want to just write hello world, I don't want 877 00:38:27,316 --> 00:38:30,436 to just convert ASCII to integers and vice versa. 878 00:38:30,436 --> 00:38:32,926 Let me make a game or just like you did with Scratch, 879 00:38:32,926 --> 00:38:34,576 many of you wrote games not 880 00:38:34,576 --> 00:38:36,236 because it was the only thing you could do but just 881 00:38:36,236 --> 00:38:39,016 because it's more interesting than just doing you know, 882 00:38:39,016 --> 00:38:40,896 a cat bouncing back and forth. 883 00:38:41,126 --> 00:38:43,136 Well if we needed to implement a game 884 00:38:43,136 --> 00:38:45,456 in this context of C like Battleship. 885 00:38:45,586 --> 00:38:48,396 Well one of the things we might do is actually represent 886 00:38:48,446 --> 00:38:49,046 a board. 887 00:38:49,266 --> 00:38:52,046 So let me go ahead and compile Battleship and I'm going 888 00:38:52,046 --> 00:38:54,846 to start doing it the quick approach. 889 00:38:54,846 --> 00:38:57,346 So Battleship, I'm just going to type make and I'm going 890 00:38:57,346 --> 00:38:59,566 to let the computer figure out what the command is 891 00:38:59,836 --> 00:39:01,446 so now I can run Battleship. 892 00:39:01,666 --> 00:39:03,996 And let me make one note about security actually, 893 00:39:04,066 --> 00:39:06,166 since some of you might have realized this already. 894 00:39:06,476 --> 00:39:10,476 A typical computer scientist or at least Linux geek 895 00:39:10,476 --> 00:39:12,726 or MAC OS geek will write commands. 896 00:39:12,806 --> 00:39:14,036 If they've written a program 897 00:39:14,076 --> 00:39:15,666 that they know is currently stored 898 00:39:15,666 --> 00:39:18,736 in their current directory they will be ever so explicit 899 00:39:18,736 --> 00:39:22,436 and say, "Run the program that's in my current directory, 900 00:39:22,546 --> 00:39:26,026 which is symbolized as dot, slash and then Foo 901 00:39:26,026 --> 00:39:27,536 if Foo is the name of the program. 902 00:39:27,946 --> 00:39:31,196 And this is actually a security thing so that when you connect 903 00:39:31,326 --> 00:39:34,376 to a system with SSH, if you just write something 904 00:39:34,376 --> 00:39:37,496 like Foo thinking, "Oh Foo is a program in, my directory." 905 00:39:37,676 --> 00:39:40,036 Well what if Foo is actually a malicious command 906 00:39:40,036 --> 00:39:41,796 that some bad guy put on the system? 907 00:39:42,006 --> 00:39:44,466 They just tricked you into executing that version 908 00:39:44,466 --> 00:39:46,076 of Foo instead of your own. 909 00:39:46,546 --> 00:39:49,366 But for now in the course you can just write your program's 910 00:39:49,366 --> 00:39:52,386 name at the prompt whether it's ISBN or Mario just 911 00:39:52,386 --> 00:39:55,076 because that sort of adds an unnecessary complication. 912 00:39:55,076 --> 00:39:56,976 But when I'm doing it, it's just a matter 913 00:39:56,976 --> 00:39:58,156 of habit and good practice. 914 00:39:58,636 --> 00:39:59,716 So there's the game of Battleship. 915 00:39:59,966 --> 00:40:02,186 So this was that little childhood game where each -- 916 00:40:02,186 --> 00:40:04,206 you and your friend have this little laptop type thing 917 00:40:04,206 --> 00:40:06,866 with a bunch of plastic holes and you say, 918 00:40:06,976 --> 00:40:09,626 "A2" and then they put a puzzle piece in 919 00:40:09,626 --> 00:40:10,666 and then go [Sound effects] 920 00:40:10,706 --> 00:40:12,296 when there actually a ship in the hole. 921 00:40:12,456 --> 00:40:14,666 Well if I want to represent this here I've got A through J 922 00:40:14,896 --> 00:40:18,146 on the left, 1 through 10 at the top and then I went 923 00:40:18,146 --> 00:40:20,746 with the ASCII Art as it's called. 924 00:40:20,986 --> 00:40:22,926 So if you really want to geek out Google something 925 00:40:22,926 --> 00:40:26,836 like ASCII Art and the world has wasted so many hours 926 00:40:26,836 --> 00:40:29,716 over the decades writing pictures, writing -- 927 00:40:30,846 --> 00:40:34,256 depicting people in photographs just using the characters 928 00:40:34,256 --> 00:40:35,016 on the keyboard. 929 00:40:35,016 --> 00:40:36,776 So this is a little bit of ASCII Art 930 00:40:37,106 --> 00:40:39,286 but there's clearly some complexity there at least 931 00:40:39,286 --> 00:40:42,496 for out purposes today, because I've gotten letters being 932 00:40:42,526 --> 00:40:44,736 incremented on the left, that's kind of interesting 933 00:40:44,736 --> 00:40:46,926 but I also have numbers incrementing on the right. 934 00:40:47,216 --> 00:40:48,846 Everything is formatted nicely 935 00:40:48,846 --> 00:40:50,606 so let's just tease this apart slightly. 936 00:40:50,906 --> 00:40:55,396 This is Battleship dot oops, what did I do wrong? 937 00:40:55,396 --> 00:40:55,596 >> [Inaudible background discussion] 938 00:40:55,596 --> 00:40:58,966 >> Right. So I tried changing accidentally the binary, 939 00:40:58,966 --> 00:41:01,326 the 0s and 1s version not the source code. 940 00:41:01,366 --> 00:41:02,466 So here's the source code. 941 00:41:02,466 --> 00:41:04,776 Let me scroll down and just think how would you 942 00:41:04,776 --> 00:41:05,386 approach this? 943 00:41:05,666 --> 00:41:07,976 Well if the goal for a problem set, 944 00:41:07,976 --> 00:41:10,786 if this is how you're beginning is to implement something 945 00:41:10,786 --> 00:41:12,986 like this, that you have a picture in front of you 946 00:41:12,986 --> 00:41:15,436 or you have the game in front of you, you want to implement this. 947 00:41:15,756 --> 00:41:17,686 Just think through how you would implement this. 948 00:41:17,686 --> 00:41:20,966 Well this is a screen that clearly for the past week 949 00:41:20,966 --> 00:41:24,836 and a half has only let me send output to the screen from left 950 00:41:24,836 --> 00:41:26,576 to right and then from top to bottom. 951 00:41:26,576 --> 00:41:29,386 Thus far I have not been able to jump back and fix things. 952 00:41:29,386 --> 00:41:31,586 Once I print a new line, that's it I'm 953 00:41:31,586 --> 00:41:33,726 on the new line just like a typewriter. 954 00:41:34,296 --> 00:41:36,026 So what could I do first? 955 00:41:36,336 --> 00:41:38,466 Well what's the first thing I should probably print then 956 00:41:38,466 --> 00:41:41,326 in my program, if I want to replicate this? 957 00:41:42,036 --> 00:41:44,016 So probably the numbers on top, right. 958 00:41:44,016 --> 00:41:45,386 And that's probably not that hard. 959 00:41:45,386 --> 00:41:47,606 Even if you haven't even dived into problem set 1 960 00:41:47,606 --> 00:41:50,146 yet you probably know that if you copied and pasted some 961 00:41:50,146 --> 00:41:52,386 of the code from past examples, the main line 962 00:41:52,386 --> 00:41:55,286 and then the curly braces, you could probably write the 4 loop 963 00:41:55,286 --> 00:41:59,176 that iterates from I equals 1 up to and equal to 10 964 00:41:59,176 --> 00:42:01,076 and just print that thing on the top row. 965 00:42:01,326 --> 00:42:02,636 What about the next row though? 966 00:42:02,736 --> 00:42:05,096 Well if we ignore the letters it's really damn easy 967 00:42:05,096 --> 00:42:07,576 to print the grid of holes in this board, right. 968 00:42:07,576 --> 00:42:09,076 You just iterate from left to right 969 00:42:09,076 --> 00:42:10,726 and print 10 lower cases -- 970 00:42:10,776 --> 00:42:13,946 O's followed by a little white space after them. 971 00:42:14,316 --> 00:42:17,356 But the interesting part seems to be the letters on the side. 972 00:42:17,416 --> 00:42:18,636 So let's focus then on that. 973 00:42:18,996 --> 00:42:22,076 The top part here as the comment implies, print top row 974 00:42:22,076 --> 00:42:24,496 of numbers, there's really no magic there. 975 00:42:24,736 --> 00:42:25,886 It's boring at this point. 976 00:42:26,146 --> 00:42:28,156 But now this part's a little more interesting 977 00:42:28,156 --> 00:42:30,826 because it turns out you can nest in loops in C. Just 978 00:42:30,826 --> 00:42:32,996 like some of you might have nested forever loops 979 00:42:33,076 --> 00:42:35,826 or loop puzzle pieces in Scratch. 980 00:42:36,126 --> 00:42:37,136 So here we have it. 981 00:42:37,136 --> 00:42:40,616 Let's see for I, I want 10 holes anyway so I'm going 982 00:42:40,616 --> 00:42:42,476 to go from 0 up to 10. 983 00:42:42,756 --> 00:42:46,086 I could go 1 through 10, 984 00:42:46,236 --> 00:42:49,246 really just as two different approaches to the same problem. 985 00:42:49,246 --> 00:42:50,306 Now what do I want to do? 986 00:42:50,426 --> 00:42:52,746 Well the first thing I want to do on every one 987 00:42:52,746 --> 00:42:56,676 of these 10 iterations because I want 10 rows of little O's, 988 00:42:57,026 --> 00:42:58,306 I want to print out the letter. 989 00:42:58,686 --> 00:43:00,766 So lets see, character and then some space. 990 00:43:00,766 --> 00:43:01,546 What do I want to print? 991 00:43:01,736 --> 00:43:04,236 Well I want to start with A but you know what, 992 00:43:04,346 --> 00:43:05,766 I want to just do an offset. 993 00:43:05,846 --> 00:43:08,666 I don't need to hard code in A, B, C, D. Let me just start 994 00:43:08,666 --> 00:43:14,186 with A and then add to A the value of I, but if I do A plus I 995 00:43:14,186 --> 00:43:16,346 where I is 0 what do I get back? 996 00:43:17,286 --> 00:43:18,036 Just A right. 997 00:43:18,036 --> 00:43:21,326 So actually it makes sense that I started with I equals 0 here 998 00:43:21,326 --> 00:43:24,196 because I'm intentionally using it not as an increment yet 999 00:43:24,196 --> 00:43:27,796 but as an offset from the capital letter A. So this line 1000 00:43:27,796 --> 00:43:29,976 of code here, if I just do this. 1001 00:43:29,976 --> 00:43:35,436 Let me go and remove everything except that line 1002 00:43:35,436 --> 00:43:36,576 and the new line below it. 1003 00:43:36,576 --> 00:43:37,996 Let me go ahead and recompile 1004 00:43:37,996 --> 00:43:41,046 and now notice the compilers saying, "Whoa you didn't use J." 1005 00:43:41,096 --> 00:43:42,996 That's okay because I just removed the code 1006 00:43:43,086 --> 00:43:46,396 that might have used J. Let me re-run Battleship, okay. 1007 00:43:46,466 --> 00:43:48,636 So that's a nice baby step and this is not meant 1008 00:43:48,636 --> 00:43:50,076 to belittle the process of coding. 1009 00:43:50,236 --> 00:43:52,666 This is precisely the approach that someone should take 1010 00:43:52,666 --> 00:43:54,796 in this class especially those less comfortable. 1011 00:43:54,796 --> 00:43:57,216 When you're trying to tackle a problem set don't try 1012 00:43:57,216 --> 00:43:59,046 and implement the whole damn program at once, 1013 00:43:59,266 --> 00:44:00,716 just get something working right. 1014 00:44:00,716 --> 00:44:02,456 And we did, the top row was so easy. 1015 00:44:02,746 --> 00:44:04,966 Second row took a little bit of you know, thought, 1016 00:44:04,966 --> 00:44:07,066 might have taken 10 minutes, 20 minutes, half an hour 1017 00:44:07,066 --> 00:44:10,036 to get it right but now we have almost everything. 1018 00:44:10,266 --> 00:44:13,056 We just need to do a little more on that second loop 1019 00:44:13,056 --> 00:44:15,966 because we need to fill the rest of the row with O's. 1020 00:44:16,026 --> 00:44:16,996 So how did I do that? 1021 00:44:16,996 --> 00:44:19,466 Well let me put my code back and if what I'm doing 1022 00:44:19,466 --> 00:44:21,586 in the outer loop with the variable I, 1023 00:44:21,586 --> 00:44:23,966 is going from row to row to row to row. 1024 00:44:24,156 --> 00:44:26,946 Well if I need to simultaneously, before going 1025 00:44:26,946 --> 00:44:29,756 to the next row quickly run from left to right spiting 1026 00:44:29,756 --> 00:44:32,336 out some 0's well that just calls for another loop. 1027 00:44:32,526 --> 00:44:37,046 So here it is, for J equals 1, J up through and equal to 10, 1028 00:44:37,046 --> 00:44:39,836 J plus, plus you know what this was actually pretty easy. 1029 00:44:39,896 --> 00:44:43,086 Just print out the lowercase O followed by 2 spaces 1030 00:44:43,086 --> 00:44:45,016 because I decided aesthetically that looked nice. 1031 00:44:45,356 --> 00:44:48,306 And in the end I put a new line, another new line at the very end 1032 00:44:48,306 --> 00:44:49,476 to make it all very pretty. 1033 00:44:49,736 --> 00:44:54,786 And if I re-run the compiler and then re-run Battleship, 1034 00:44:54,786 --> 00:44:57,766 in fact I have this entire board here. 1035 00:44:59,156 --> 00:45:01,586 Any questions about this? 1036 00:45:02,216 --> 00:45:03,866 Well you can't help but get excited 1037 00:45:03,866 --> 00:45:05,976 when the next topic somehow involves Homer Simpson. 1038 00:45:05,976 --> 00:45:07,986 But let's go ahead and take our 5 minute break here. 1039 00:45:08,136 --> 00:45:09,006 We'll resume in 5. 1040 00:45:14,096 --> 00:45:15,656 [Sound effects] So one quick announcement. 1041 00:45:15,656 --> 00:45:17,236 The Harvard Computer Society, 1042 00:45:17,236 --> 00:45:19,476 two of whose members you met last week are going 1043 00:45:19,476 --> 00:45:22,616 to be offering one of several seminars tomorrow, 1044 00:45:22,616 --> 00:45:24,596 Tuesday at 7:00 p.m. in Maxwell dorm 1045 00:45:24,596 --> 00:45:25,756 in the computer science building. 1046 00:45:26,006 --> 00:45:28,456 The specifics are on the course website's homepage right now. 1047 00:45:28,526 --> 00:45:32,196 CS50, net but this is entitled demystifying Linux. 1048 00:45:32,566 --> 00:45:35,356 And this is meant as just a free optional seminar. 1049 00:45:35,746 --> 00:45:38,496 If you would like to join those folks tomorrow 1050 00:45:38,576 --> 00:45:41,636 and just learn a little bit more about Linux. 1051 00:45:41,796 --> 00:45:43,756 This course does not assume that you know Linux. 1052 00:45:43,756 --> 00:45:46,216 We will provide everything you need to know in the form 1053 00:45:46,216 --> 00:45:48,586 of sections and the problem sets PDF's themselves. 1054 00:45:48,826 --> 00:45:50,866 But if you like this kind of stuff, you want to learn 1055 00:45:50,866 --> 00:45:54,846 from some particularly geeky folks how you can do yet more 1056 00:45:54,846 --> 00:45:57,856 at the Linux command line do feel free to check that out. 1057 00:45:58,186 --> 00:46:00,566 But for now our focus will be on Homer here. 1058 00:46:00,566 --> 00:46:03,126 So this is perhaps one of the most annoying songs ever. 1059 00:46:03,276 --> 00:46:06,566 There's this classic Simpson's episode, which I -- 1060 00:46:06,946 --> 00:46:09,136 if ever there was a Youtube moment it would be this one. 1061 00:46:09,336 --> 00:46:12,556 Where Otto is actually driving the bus listening to 99 bottles 1062 00:46:12,556 --> 00:46:13,416 of beer on the wall and then all 1063 00:46:13,416 --> 00:46:14,576 of the sudden you realize it's only 1064 00:46:14,576 --> 00:46:15,846 in his headphones for some reason. 1065 00:46:16,256 --> 00:46:17,836 But the motivation here is 1066 00:46:17,836 --> 00:46:20,256 that annoying though this particular song is, 1067 00:46:20,656 --> 00:46:23,096 there's a pattern to it. 1068 00:46:23,096 --> 00:46:26,196 And it suggests that there's an opportunity to just deploy some 1069 00:46:26,196 --> 00:46:28,806 of these basic building blocks we've been looking at like loops 1070 00:46:28,806 --> 00:46:30,116 and variables and so forth. 1071 00:46:30,376 --> 00:46:33,656 And so let me go ahead and open up one possible approach 1072 00:46:33,656 --> 00:46:35,176 to implementing this song. 1073 00:46:35,456 --> 00:46:38,216 Right. The most naive approach would be to write a text file 1074 00:46:38,216 --> 00:46:40,796 that contains the entire song four lines at a time. 1075 00:46:41,106 --> 00:46:42,786 Copy, paste, copy, paste, copy paste 1076 00:46:42,786 --> 00:46:44,936 and just change the number manually each time. 1077 00:46:45,226 --> 00:46:48,186 But finally now do we have some programming chops 1078 00:46:48,186 --> 00:46:48,926 with which to do this. 1079 00:46:48,956 --> 00:46:51,186 So the programs are going to start getting a little longer 1080 00:46:51,506 --> 00:46:53,836 but not because they're fundamentally more complicated, 1081 00:46:53,836 --> 00:46:55,826 they just need to do a little bit more. 1082 00:46:55,826 --> 00:46:59,536 So here's my main routine, I want to print out the 99 bottles 1083 00:46:59,536 --> 00:47:01,076 of beer on the wall song. 1084 00:47:01,366 --> 00:47:03,266 Well let me go ahead and first ask just 1085 00:47:03,266 --> 00:47:06,726 because hard coding 99 is even less interesting, 1086 00:47:06,946 --> 00:47:08,926 let me at least ask the user for some input. 1087 00:47:08,926 --> 00:47:11,036 How many bottles of beer will there be on the wall? 1088 00:47:11,286 --> 00:47:13,786 So I'm going to use the CS50 library. 1089 00:47:14,026 --> 00:47:16,816 I'm calling get int, I'm putting the return value into N. 1090 00:47:16,816 --> 00:47:19,256 And how do I use the CS50 library? 1091 00:47:19,256 --> 00:47:22,086 Well again besides using standard IO, which you need 1092 00:47:22,086 --> 00:47:26,496 for print F we use CS50 dot H for functions like these. 1093 00:47:26,716 --> 00:47:28,556 Alright so the start of this program's pretty straight 1094 00:47:28,556 --> 00:47:30,216 forward just ask the user for an int, 1095 00:47:30,216 --> 00:47:33,386 I could have put any sentence here I just chose to state it 1096 00:47:33,386 --> 00:47:34,336 in the form of a question. 1097 00:47:34,636 --> 00:47:36,546 And notice the silly little aesthetics, 1098 00:47:36,546 --> 00:47:38,666 I put a space character at the end just 1099 00:47:38,666 --> 00:47:41,036 so that the blinking prompt would stay at the same line 1100 00:47:41,036 --> 00:47:42,666 and not arbitrarily go to the next one. 1101 00:47:42,746 --> 00:47:43,806 But these are just aesthetics. 1102 00:47:44,066 --> 00:47:46,426 Well here is an example of error checking. 1103 00:47:46,986 --> 00:47:49,756 Thus far we've just assumed that the user's going 1104 00:47:49,756 --> 00:47:52,646 to be cooperative but in general that's a very bad thing. 1105 00:47:52,646 --> 00:47:55,266 And most always when you are using your own MAC or PC 1106 00:47:55,436 --> 00:47:59,526 and something goes wrong, a program crashes, it hangs 1107 00:47:59,526 --> 00:48:01,466 or just something weird happens that's 1108 00:48:01,516 --> 00:48:03,246 because a programmer somewhere 1109 00:48:03,246 --> 00:48:06,286 in an office somewhere far away made some mistake. 1110 00:48:06,286 --> 00:48:08,366 Made maybe some stupid mistake because he 1111 00:48:08,366 --> 00:48:11,666 or she did not anticipate that you might hit that key 1112 00:48:11,666 --> 00:48:13,216 on the keyboard at that moment and time. 1113 00:48:13,446 --> 00:48:16,316 Or even though you were told to enter a number, 1114 00:48:16,316 --> 00:48:19,436 like a positive number, you know you accidentally make a typo 1115 00:48:19,436 --> 00:48:22,316 and some punctuation goes in or you are a malicious user 1116 00:48:22,476 --> 00:48:25,166 and you're intentionally typing in a word instead 1117 00:48:25,166 --> 00:48:26,236 of a number when prompted. 1118 00:48:26,236 --> 00:48:28,166 Well you the programmer now for this course, 1119 00:48:28,166 --> 00:48:29,296 for the problem sets and just 1120 00:48:29,296 --> 00:48:33,116 in general absolutely have to code defensively. 1121 00:48:33,316 --> 00:48:36,066 And you have to assume that the user is either an idiot 1122 00:48:36,066 --> 00:48:37,466 or just outright malicious 1123 00:48:37,706 --> 00:48:39,716 because only then can you genuinely write code 1124 00:48:39,716 --> 00:48:42,076 that would stand just ordinary use, right. 1125 00:48:42,076 --> 00:48:45,066 Even in this course, right, 300 plus students we had 1126 00:48:45,066 --> 00:48:46,766 on the little survey for problem set 0. 1127 00:48:47,056 --> 00:48:49,096 Put your FAS user name here, 1128 00:48:49,276 --> 00:48:52,176 omit the at fast at harvard.edu right. 1129 00:48:52,176 --> 00:48:53,966 If their sample size is long enough you're going 1130 00:48:53,996 --> 00:48:55,816 to have someone like doofus at FAS 1131 00:48:55,816 --> 00:48:58,376 at harvard.edu input it into that field. 1132 00:48:58,376 --> 00:48:59,376 It happens right. 1133 00:48:59,716 --> 00:49:02,156 So fortunately we have programming seals, we can strip 1134 00:49:02,156 --> 00:49:03,156 out that kind of text. 1135 00:49:03,436 --> 00:49:05,446 But again this speaks to just the need 1136 00:49:05,446 --> 00:49:09,046 to code defensively your own, you will be happier for it 1137 00:49:09,046 --> 00:49:11,346 because it's now we the humans who have to go 1138 00:49:11,396 --> 00:49:12,696 and clean up that data. 1139 00:49:12,696 --> 00:49:16,266 Much better if we had been able to defend against that with -- 1140 00:49:16,786 --> 00:49:19,416 by rejecting that kind of input at the get go. 1141 00:49:19,416 --> 00:49:20,536 But because we outsourced 1142 00:49:20,536 --> 00:49:22,956 to SurveyMonkey we didn't have that ability. 1143 00:49:23,206 --> 00:49:24,366 So here's a little sanity check. 1144 00:49:24,526 --> 00:49:26,056 A little bit of error checking. 1145 00:49:26,336 --> 00:49:30,226 If N -- if you're that person doofus at FAS please stay 1146 00:49:30,226 --> 00:49:33,276 on the course, we're not trying to pick on you. 1147 00:49:33,356 --> 00:49:36,346 [Laughter] So if N is less than 1, it's so easy to check some 1148 00:49:36,346 --> 00:49:37,146 of these inputs right. 1149 00:49:37,366 --> 00:49:39,676 Get int last week, we already saw will reject 1150 00:49:39,816 --> 00:49:41,226 non-numeric input. 1151 00:49:41,466 --> 00:49:45,306 Non-integral input because we the CS50 staff made sure 1152 00:49:45,306 --> 00:49:48,616 that it prompts you to retry, retry, retry if the user 1153 00:49:48,616 --> 00:49:51,046 as we saw tries typing in monkey instead of a number. 1154 00:49:51,326 --> 00:49:55,066 So I don't have to check against -- for word input or punctuation 1155 00:49:55,346 --> 00:49:56,946 but numeric is one thing. 1156 00:49:56,946 --> 00:49:59,956 If I actually care about the sign of that value, positive, 1157 00:49:59,956 --> 00:50:02,336 negative, 0 I gotta do that myself. 1158 00:50:02,336 --> 00:50:05,266 So if N is less than 1 I'm going to yell at the user, 1159 00:50:05,266 --> 00:50:06,496 sorry that makes no sense 1160 00:50:06,496 --> 00:50:09,016 and then I'm actually going to return 1. 1161 00:50:09,306 --> 00:50:10,856 So this is a sneak preview 1162 00:50:10,856 --> 00:50:13,566 of the capability I promised existed -- oops. 1163 00:50:13,896 --> 00:50:16,616 There is the ability in C for functions 1164 00:50:16,946 --> 00:50:19,346 like get int to return values. 1165 00:50:20,176 --> 00:50:24,146 Similarly can main the default function that every program has, 1166 00:50:24,286 --> 00:50:26,116 that's the function that just gets run by default. 1167 00:50:26,316 --> 00:50:29,386 So does main in fact have what's called a return value 1168 00:50:29,386 --> 00:50:30,856 and even though we've been sort of taking it 1169 00:50:30,856 --> 00:50:32,046 on blind faith thus far, 1170 00:50:32,276 --> 00:50:35,106 I've always been writing the word int before main, 1171 00:50:35,396 --> 00:50:38,046 which means this main function that again I've been copying 1172 00:50:38,046 --> 00:50:40,716 and pasting thus far does in fact return a value. 1173 00:50:40,956 --> 00:50:42,716 It's just I'm not doing anything with it. 1174 00:50:42,946 --> 00:50:44,486 But now think back to the real world. 1175 00:50:44,486 --> 00:50:47,126 If you've ever been using a MAC or a PC 1176 00:50:47,376 --> 00:50:49,826 and something goes wrong, often, not always 1177 00:50:49,826 --> 00:50:52,886 but often you get a little prompt saying an error occurred 1178 00:50:52,886 --> 00:50:54,686 or system error occurred or whatever. 1179 00:50:54,816 --> 00:50:56,096 And then there's usually a number 1180 00:50:56,146 --> 00:50:58,176 like a numeric identifier, maybe it's negative, 1181 00:50:58,176 --> 00:51:00,546 maybe it's positive but it's usually useless 1182 00:51:00,596 --> 00:51:03,186 to you the human unless you actually get on the phone 1183 00:51:03,186 --> 00:51:06,406 or on e-mail and report that error to the company. 1184 00:51:06,596 --> 00:51:08,056 And even then they probably don't care 1185 00:51:08,056 --> 00:51:09,446 from an individual user. 1186 00:51:09,806 --> 00:51:14,246 So that was probably a return value or an error code 1187 00:51:14,246 --> 00:51:16,236 of some sort and even you and C, 1188 00:51:16,236 --> 00:51:18,456 even with the simplest programs have this ability 1189 00:51:18,456 --> 00:51:21,546 to return error codes such as this one. 1190 00:51:21,546 --> 00:51:23,276 So because this is the first error 1191 00:51:23,456 --> 00:51:25,626 that my program might encounter I arbitrarily 1192 00:51:25,796 --> 00:51:27,096 but conventionally decided 1193 00:51:27,096 --> 00:51:29,266 that the first error is going to return 1. 1194 00:51:29,506 --> 00:51:32,336 Now I won't see that, 1 will not get printed to the screen. 1195 00:51:32,336 --> 00:51:32,956 This isn't printf. 1196 00:51:32,956 --> 00:51:36,326 This is return so only a particularly sophisticated 1197 00:51:36,326 --> 00:51:39,366 person or programmer could actually see that number. 1198 00:51:39,586 --> 00:51:40,706 But you a couple of weeks 1199 00:51:40,706 --> 00:51:43,756 from now will be using a special tool called a debugger. 1200 00:51:43,966 --> 00:51:47,586 GDB, Visual Studio you might have used in other classes 1201 00:51:47,886 --> 00:51:50,436 but this is a program that can see this information 1202 00:51:50,436 --> 00:51:51,816 and you'll find that it's quite useful. 1203 00:51:52,066 --> 00:51:53,986 So for now it's just a convention that I'm adopting 1204 00:51:53,986 --> 00:51:55,496 on blind faith, more to come. 1205 00:51:55,826 --> 00:51:57,176 So now we've done the setup, 1206 00:51:57,176 --> 00:51:59,566 half of my program really has just been devoted just 1207 00:51:59,566 --> 00:52:01,076 to the setup portion here. 1208 00:52:01,346 --> 00:52:04,246 Here's the juicy part of the program, sing the annoying song. 1209 00:52:04,436 --> 00:52:06,126 Well first aesthetically I decided 1210 00:52:06,126 --> 00:52:09,106 to print the new line right here, not that interesting. 1211 00:52:09,456 --> 00:52:10,576 Now what am I doing? 1212 00:52:10,806 --> 00:52:14,196 Well here's the for loop in which I'm counting down just 1213 00:52:14,196 --> 00:52:16,586 because that conceptually was easier for me than counting 1214 00:52:16,586 --> 00:52:17,686 up for a song like this. 1215 00:52:17,966 --> 00:52:22,186 I'm initializing I to N, I'm going to keep singing as long 1216 00:52:22,186 --> 00:52:25,646 as I is greater than 0 and then just as I can increment 1217 00:52:25,706 --> 00:52:27,996 so can I decrement as we've seen before. 1218 00:52:28,306 --> 00:52:30,286 Now what do I want to do on each of these iterations? 1219 00:52:30,556 --> 00:52:33,016 I want to sing those stanzas, so what do I want to do? 1220 00:52:33,196 --> 00:52:35,876 Printf. So how many bottles and now I'm cheating 1221 00:52:35,876 --> 00:52:37,496 on the grammar right because once I get 1222 00:52:37,536 --> 00:52:39,546 to like 1 I don't want my grammar to be wrong. 1223 00:52:39,546 --> 00:52:42,996 So I parenthesis the S as sort of a corner cutting here of beer 1224 00:52:42,996 --> 00:52:46,066 on the wall backslash N and then I plug in the value. 1225 00:52:46,266 --> 00:52:47,626 So that's old school now. 1226 00:52:47,626 --> 00:52:48,466 That's pretty easy. 1227 00:52:48,676 --> 00:52:49,936 Fortunately the next line 1228 00:52:49,936 --> 00:52:52,386 of code is pretty much the same thing verbatim so it's a copy, 1229 00:52:52,386 --> 00:52:53,776 paste with a change in the wording. 1230 00:52:54,156 --> 00:52:56,276 Take one down pass it around, that's just a string 1231 00:52:56,276 --> 00:52:57,206 that always get printed 1232 00:52:57,206 --> 00:52:59,376 so it only gets a little interesting at the last line. 1233 00:52:59,586 --> 00:53:02,576 But again I know arithmetic, I know I is a variable so print 1234 00:53:02,576 --> 00:53:05,136 out I minus 1 because the last line 1235 00:53:05,136 --> 00:53:08,066 of the song per the little canonical form here is 1236 00:53:08,096 --> 00:53:10,126 to say 98 bottles of beer on the wall. 1237 00:53:10,336 --> 00:53:12,846 And now I repeat, add nauseam until at least 1238 00:53:12,846 --> 00:53:14,596 until I bottom out at 0. 1239 00:53:14,916 --> 00:53:18,366 So if I go ahead and close this, let me make beer 1, 1240 00:53:18,366 --> 00:53:21,356 let me go ahead and run beer 1. 1241 00:53:21,666 --> 00:53:24,516 How many bottles, let's say 99, enter. 1242 00:53:25,246 --> 00:53:26,596 It's pretty damn fast right. 1243 00:53:26,596 --> 00:53:28,646 That's kind of the neat thing when you have 2 gigahertz, 1244 00:53:28,646 --> 00:53:31,886 3 gigahertz computers that allot really quickly 1245 00:53:31,886 --> 00:53:32,946 at least in this case. 1246 00:53:33,166 --> 00:53:35,346 But it seems to have worked correctly 1247 00:53:35,776 --> 00:53:37,816 but that's not the only way we could implement this. 1248 00:53:37,856 --> 00:53:39,376 Let's take a look at a slight variance. 1249 00:53:39,376 --> 00:53:40,756 So this is version 2. 1250 00:53:41,126 --> 00:53:44,436 Let's see this looks the same as before -- oh interesting. 1251 00:53:44,876 --> 00:53:46,386 So this time the -- 1252 00:53:46,386 --> 00:53:49,146 I the programmer decided [Sound effects] I like while loops. 1253 00:53:49,146 --> 00:53:49,996 I can do the same thing 1254 00:53:49,996 --> 00:53:51,356 in a while loop, let me prove as much. 1255 00:53:51,356 --> 00:53:55,336 So here I said, "Sing the song while N is greater than 0." 1256 00:53:55,466 --> 00:53:57,486 And maybe that's you kind of speaks true 1257 00:53:57,486 --> 00:53:58,656 or it just makes more sense. 1258 00:53:58,656 --> 00:54:01,686 Do the following while there are positive numbers 1259 00:54:01,686 --> 00:54:02,866 of beer on the wall. 1260 00:54:03,066 --> 00:54:05,586 So a while loop is perfectly fine, the only thing you have 1261 00:54:05,626 --> 00:54:07,136 to remember to do is 1, 1262 00:54:07,406 --> 00:54:11,836 make sure that you manually decrement N at the end there. 1263 00:54:11,836 --> 00:54:14,126 And now I am doing something slightly different. 1264 00:54:14,346 --> 00:54:18,086 In version 1 whose value was I changing on every iteration? 1265 00:54:18,646 --> 00:54:22,536 So I, I was doing minus, minus. 1266 00:54:22,946 --> 00:54:25,496 Here I'm kind of being quote, unquote destructive. 1267 00:54:25,716 --> 00:54:28,836 I'm taking the users input proffered at the very top 1268 00:54:28,836 --> 00:54:32,206 of the program by a get int, storing it at N but at the end 1269 00:54:32,206 --> 00:54:34,846 of this program I have lost track of what? 1270 00:54:34,846 --> 00:54:38,806 Like what that original value was. 1271 00:54:38,956 --> 00:54:41,646 Now maybe that's a problem if especially at the end 1272 00:54:41,646 --> 00:54:44,006 of the song you want to say something like thanks 1273 00:54:44,106 --> 00:54:46,846 for playing with 99 bottles of beer. 1274 00:54:47,096 --> 00:54:50,756 But in this case where I don't ever use that value again, 1275 00:54:51,066 --> 00:54:52,286 the short answer is who cares. 1276 00:54:52,416 --> 00:54:53,866 I had a variable, I chose 1277 00:54:53,866 --> 00:54:56,586 to decrement it destructively thereby losing track 1278 00:54:56,586 --> 00:54:58,666 of its original value but again who cares, 1279 00:54:58,666 --> 00:54:59,676 it doesn't really feel wrong 1280 00:54:59,676 --> 00:55:01,096 because I never need that value again. 1281 00:55:01,096 --> 00:55:04,196 But if I did then there would be a problem and I might need 1282 00:55:04,196 --> 00:55:05,526 to introduce another variable. 1283 00:55:05,836 --> 00:55:07,146 And one quick word on style, 1284 00:55:07,146 --> 00:55:10,906 so as much as I preached already using well named variables 1285 00:55:11,076 --> 00:55:12,606 like we did with previous programs. 1286 00:55:12,806 --> 00:55:16,186 It is completely reasonable and conventional 1287 00:55:16,446 --> 00:55:18,806 to use basic place holders 1288 00:55:18,806 --> 00:55:22,346 like N whenever you have a total number of things. 1289 00:55:22,486 --> 00:55:25,966 People like N, I is almost always used as an increment. 1290 00:55:25,966 --> 00:55:28,526 If you've used I already as we did for Battleship 1291 00:55:28,526 --> 00:55:32,896 because it's nested I went with J, then you go with K, then L, 1292 00:55:32,896 --> 00:55:35,066 then if you're nesting that deep you're probably doing 1293 00:55:35,066 --> 00:55:35,996 something foolishly. 1294 00:55:36,186 --> 00:55:39,666 At which point, yes you could go to M but things just start 1295 00:55:39,666 --> 00:55:41,066 to feel a little weird there. 1296 00:55:41,196 --> 00:55:43,496 But actually let me roll back to that just to be clear. 1297 00:55:43,496 --> 00:55:44,856 This was Battleship, recall. 1298 00:55:45,346 --> 00:55:47,816 Notice bad things would have happened 1299 00:55:47,816 --> 00:55:51,046 if I had used or reused I here, no. 1300 00:55:52,236 --> 00:55:55,546 Like if I had been taught in class, you know use I. 1301 00:55:55,666 --> 00:55:57,316 So I say for I gets that. 1302 00:55:57,766 --> 00:55:58,606 I is that. 1303 00:55:58,896 --> 00:55:59,736 I is that. 1304 00:56:00,496 --> 00:56:03,326 Okay that seems fine so let me go ahead 1305 00:56:03,326 --> 00:56:04,876 and recompile Battleship. 1306 00:56:05,386 --> 00:56:07,856 Okay unused variable J but that was to be expected. 1307 00:56:07,856 --> 00:56:10,266 Battleship and [Sound effects] broken. 1308 00:56:10,956 --> 00:56:12,716 Right. So just to realize-- 1309 00:56:12,716 --> 00:56:15,426 just realize that because I was declared already 1310 00:56:15,636 --> 00:56:17,396 if you're incrementing it in this outer loop 1311 00:56:17,396 --> 00:56:20,506 in Battleship you better make sure you know what you're doing 1312 00:56:20,506 --> 00:56:23,356 if you're also going to increment that in a nested loop 1313 00:56:23,356 --> 00:56:26,546 because clearly bead things or unintended things happen. 1314 00:56:26,546 --> 00:56:28,906 And that's why I just whipped out J instead of I 1315 00:56:29,216 --> 00:56:30,456 for that particular program. 1316 00:56:30,716 --> 00:56:34,006 But in beer 2 the only fundamental difference here was 1317 00:56:34,006 --> 00:56:34,936 that I used a while loop. 1318 00:56:35,816 --> 00:56:37,536 Why? [Sound effects] It's different. 1319 00:56:37,596 --> 00:56:38,256 It's another way. 1320 00:56:38,586 --> 00:56:39,626 Well there's yet more ways. 1321 00:56:39,676 --> 00:56:41,326 Let's take a look at beer 3. 1322 00:56:41,876 --> 00:56:45,466 Alright, so beer 3 okay, on intro -- oh interesting. 1323 00:56:45,696 --> 00:56:48,726 Little teaser here , I seem to be about to use strings. 1324 00:56:48,786 --> 00:56:50,016 Let me see in what context. 1325 00:56:50,616 --> 00:56:53,696 Fast forward, oh I got a little anal in this version. 1326 00:56:53,886 --> 00:56:55,616 So I wanted to use proper grammar, 1327 00:56:55,616 --> 00:56:58,736 which is not unreasonable in fact if you have the ability now 1328 00:56:58,736 --> 00:57:01,816 to fix what people generally punt on in software just 1329 00:57:01,816 --> 00:57:03,106 because it's easy to cut corners. 1330 00:57:03,376 --> 00:57:04,116 Why not do it? 1331 00:57:04,116 --> 00:57:05,846 And in fact I introduced this here just 1332 00:57:05,846 --> 00:57:07,956 because it introduces a neat piece of syntax 1333 00:57:08,126 --> 00:57:09,026 that you don't have to use. 1334 00:57:09,236 --> 00:57:12,006 But the more comfortable you get with this stuff the sexier lines 1335 00:57:12,006 --> 00:57:15,016 of code like this tend to feel, at least to some of us. 1336 00:57:15,406 --> 00:57:18,636 So it looks like S1 and S2 were strings because we saw 1337 00:57:18,636 --> 00:57:22,156 that up above, they were both declared up here as stings. 1338 00:57:22,356 --> 00:57:24,186 And remember a string, trivia, 1339 00:57:24,186 --> 00:57:25,676 is equivalent to what in reality? 1340 00:57:26,686 --> 00:57:27,686 Charstar right. 1341 00:57:27,686 --> 00:57:29,436 And eventually we'll take that training wheel off 1342 00:57:29,486 --> 00:57:31,616 but the CS50 library kind of hides that now 1343 00:57:31,616 --> 00:57:32,466 and calls it a string. 1344 00:57:32,756 --> 00:57:35,406 So what does S1 equal? 1345 00:57:35,856 --> 00:57:36,796 S1 equals, hmm. 1346 00:57:37,196 --> 00:57:41,126 I equal, equals 1 question mark, bottle, bottles, 1347 00:57:41,126 --> 00:57:44,206 this is a little weird and then where am I using this. 1348 00:57:44,206 --> 00:57:45,856 Okay S1 is here. 1349 00:57:46,026 --> 00:57:46,766 S1 is here. 1350 00:57:46,866 --> 00:57:47,666 S2 is there, alright. 1351 00:57:47,976 --> 00:57:50,256 It's a little confusing at first so let me go ahead 1352 00:57:50,256 --> 00:57:53,536 and save and make beer 3. 1353 00:57:53,996 --> 00:57:54,736 Compiles, okay. 1354 00:57:54,736 --> 00:57:57,706 Let me go ahead and run beer 3 and lets do it 1355 00:57:57,706 --> 00:58:00,036 with a few numbers so we can actually see everything 1356 00:58:00,036 --> 00:58:00,846 on the screen at once. 1357 00:58:01,736 --> 00:58:06,616 Oh, okay. So again bottles of -- 3 bottles of beer, 2 bottles. 1358 00:58:06,616 --> 00:58:09,106 Oh here's where it actually seems to have made a difference, 1359 00:58:09,106 --> 00:58:12,636 1 bottle of beer but 0 bottles. 1360 00:58:12,936 --> 00:58:16,336 So apparently I need to have both S1 and S2 because even 1361 00:58:16,336 --> 00:58:19,716 within the same chorus I need to spit 1362 00:58:19,716 --> 00:58:22,716 out a slightly different version of the word potentially. 1363 00:58:23,166 --> 00:58:24,076 So what's happening? 1364 00:58:24,556 --> 00:58:27,996 Well if in this sentence here, the very first where I spit 1365 00:58:27,996 --> 00:58:30,836 out S1, something bottles of beer on the wall. 1366 00:58:31,006 --> 00:58:35,086 Under what cases do I want to say bottle versus bottles? 1367 00:58:35,896 --> 00:58:37,316 When do I want to say bottle? 1368 00:58:38,576 --> 00:58:41,046 So only when the number of bottles of beer 1369 00:58:41,046 --> 00:58:42,226 on the wall is 1 right. 1370 00:58:42,226 --> 00:58:43,446 That's what's grammatically correct. 1371 00:58:43,616 --> 00:58:45,486 1 bottle of beer on the wall. 1372 00:58:45,786 --> 00:58:47,406 So what we can infer now, 1373 00:58:47,406 --> 00:58:49,186 what this new piece of syntax is doing. 1374 00:58:49,186 --> 00:58:52,046 So this is technically called a ternary operator. 1375 00:58:52,346 --> 00:58:54,896 You've seen binary operators before even though we didn't 1376 00:58:54,896 --> 00:58:55,936 slap that label on them 1377 00:58:56,146 --> 00:58:59,746 so X plus Y [Sound effects] plus is apparently a binary operator. 1378 00:58:59,746 --> 00:59:01,966 It takes something on the left and something on the right. 1379 00:59:02,306 --> 00:59:04,716 This is a fancy thing called the ternary operator 1380 00:59:04,846 --> 00:59:06,096 because it takes something on the left, 1381 00:59:06,196 --> 00:59:07,976 something in the middle and something on the right. 1382 00:59:08,296 --> 00:59:09,576 But for now that's just jargon. 1383 00:59:09,826 --> 00:59:11,716 But apparently what this is doing is kind 1384 00:59:11,716 --> 00:59:15,216 of like a condition, it's doing it all on one line. 1385 00:59:15,246 --> 00:59:16,566 S1 is being assigned the variable -- 1386 00:59:16,566 --> 00:59:20,576 the string bottle if I equals, equals 1. 1387 00:59:20,926 --> 00:59:23,956 Otherwise, else it's being assigned bottles. 1388 00:59:24,286 --> 00:59:26,586 So it turns out this slick piece 1389 00:59:26,586 --> 00:59:29,016 of syntax is actually equivalent to this. 1390 00:59:29,156 --> 00:59:37,916 If I equals, equals 1, S1 gets bottle else S1 gets bottles. 1391 00:59:38,466 --> 00:59:41,116 So this is now equivalent to this first line. 1392 00:59:41,116 --> 00:59:43,176 And then the second line is the same exact code 1393 00:59:43,176 --> 00:59:44,686 but with S2 instead of S1. 1394 00:59:44,686 --> 00:59:45,576 Why did I do it? 1395 00:59:45,576 --> 00:59:47,556 Well again it's just a matter of style. 1396 00:59:47,556 --> 00:59:49,626 This -- these four lines [Sound effects] they just take 1397 00:59:49,626 --> 00:59:51,376 up four lines for no good reason. 1398 00:59:51,376 --> 00:59:54,186 I can kind of collapse this into something much more elegant, 1399 00:59:54,226 --> 00:59:56,356 not strictly necessary but that's all this is. 1400 00:59:56,466 --> 00:59:58,796 So if you ever see this operator in any context, 1401 00:59:58,796 --> 01:00:02,526 book or staff code it's just the same thing as an if and an else. 1402 01:00:02,746 --> 01:00:07,106 And technically yes, if your mind is already racing you can 1403 01:00:07,106 --> 01:00:10,096 nest these things so you can effectively implement if, else, 1404 01:00:10,146 --> 01:00:11,896 if, else, if, else, like don't cross 1405 01:00:11,896 --> 01:00:14,026 that line then it becomes unreadable. 1406 01:00:14,316 --> 01:00:17,006 Do it for one, an if and an else and leave it at that. 1407 01:00:17,006 --> 01:00:18,446 So what's the end result? 1408 01:00:18,446 --> 01:00:19,846 Well again, how many bottles? 1409 01:00:19,916 --> 01:00:23,426 99. It's the same exact song but I fixed the grammar. 1410 01:00:23,786 --> 01:00:26,216 Now just to think outside the box for a moment, 1411 01:00:26,446 --> 01:00:28,706 why might you not want to do this? 1412 01:00:29,846 --> 01:00:33,886 What's a downside of adding this check, in version 3 as opposed 1413 01:00:33,886 --> 01:00:36,386 to just doing bottle parenthesis, S, 1414 01:00:36,566 --> 01:00:38,826 close parenthesis as I did in the first two versions? 1415 01:00:39,406 --> 01:00:46,316 Why might you not want to do this? 1416 01:00:46,526 --> 01:00:46,766 [Sound effects] Sorry. 1417 01:00:46,766 --> 01:00:48,436 >> [Inaudible background discussion] 1418 01:00:48,436 --> 01:00:49,136 >> A little louder. 1419 01:00:49,136 --> 01:00:50,716 >> [Inaudible background discussion] 1420 01:00:50,716 --> 01:00:52,246 >> So if I started with 1 -- 1421 01:00:52,246 --> 01:00:55,306 okay if I started with 1 I think it will still work. 1422 01:00:55,306 --> 01:00:56,426 How many bottles of beer? 1423 01:00:56,646 --> 01:00:58,856 So it still works but I've 1424 01:00:58,856 --> 01:01:01,086 at least generalized the code enough 1425 01:01:01,086 --> 01:01:03,746 that it will still work even in the case of input 1. 1426 01:01:03,976 --> 01:01:06,736 Even though again it's not really doing much there 1427 01:01:06,736 --> 01:01:08,066 because I've just chosen one number. 1428 01:01:08,066 --> 01:01:08,616 [Inaudible background discussion] 1429 01:01:08,616 --> 01:01:12,306 >> Yeah, so that's kind of the rub here. 1430 01:01:12,306 --> 01:01:15,806 So even though we won't worry so much about this in this course 1431 01:01:15,806 --> 01:01:18,726 because again even my laptop is 1.6 gigahertz, 1432 01:01:18,726 --> 01:01:20,506 the servers we have are like 3 gigahertz. 1433 01:01:20,506 --> 01:01:23,316 I mean even though we have really fast computers these days 1434 01:01:23,316 --> 01:01:25,886 -- and even though these things are executed as we've seen 1435 01:01:25,886 --> 01:01:27,256 in the blink of an eye. 1436 01:01:27,796 --> 01:01:31,136 You know each of these two lines of code do require 1437 01:01:31,236 --> 01:01:34,666 that the Intel CPU or whatever's inside your computer do some 1438 01:01:34,666 --> 01:01:35,966 thinking and do some checks. 1439 01:01:36,436 --> 01:01:37,636 And the sad thing is 1440 01:01:37,826 --> 01:01:40,106 if you literally run this program a hundred times 1441 01:01:40,106 --> 01:01:43,686 for 99 bottles of beer on down to 0 you're executing both 1442 01:01:43,686 --> 01:01:46,196 of those if conditions again and again and again 1443 01:01:46,196 --> 01:01:49,726 and they only change the behavior of the program once 1444 01:01:50,196 --> 01:01:51,756 out of those 99 times. 1445 01:01:52,326 --> 01:01:53,986 Now that's again not a fundamental problem 1446 01:01:54,366 --> 01:01:56,466 but it's a tradeoff in terms of performance. 1447 01:01:56,526 --> 01:01:59,106 So if later in the course you start working on things 1448 01:01:59,106 --> 01:02:01,956 like cell phone applications for final projects. 1449 01:02:01,956 --> 01:02:05,116 Or after this course you start writing code for little devices 1450 01:02:05,116 --> 01:02:07,326 in cars or robots or little things 1451 01:02:07,476 --> 01:02:09,866 where you don't have the luxury of many, many gigahertz 1452 01:02:09,866 --> 01:02:12,186 of power, all of these little cycles, 1453 01:02:12,186 --> 01:02:14,786 all of these additional statements start to add up. 1454 01:02:15,146 --> 01:02:18,746 So realize that you know maybe this isn't the best approach 1455 01:02:18,746 --> 01:02:22,206 for certain context but clearly here where we do have cycles 1456 01:02:22,236 --> 01:02:25,276 to spend it seems a very reasonable tradeoff. 1457 01:02:25,566 --> 01:02:28,516 Well lets take a look at a forth version of this here. 1458 01:02:28,806 --> 01:02:30,396 Just to see how it might differ. 1459 01:02:30,816 --> 01:02:31,546 Oh interesting. 1460 01:02:31,936 --> 01:02:34,266 I've gotten rid of the chorus altogether. 1461 01:02:34,716 --> 01:02:37,326 So this is this one new piece we'll introduce today. 1462 01:02:37,326 --> 01:02:40,716 Anytime you sense that there's some redundancy in your code 1463 01:02:40,956 --> 01:02:44,156 or there's this ability where you could outsource some piece 1464 01:02:44,156 --> 01:02:47,686 of functionality just like we've been doing for printf right. 1465 01:02:47,716 --> 01:02:50,436 You and we in class have not written a function 1466 01:02:50,436 --> 01:02:51,656 that prints stuff to the screen. 1467 01:02:51,656 --> 01:02:54,556 I don't frankly even know how I would start to print characters 1468 01:02:54,556 --> 01:02:57,076 in a grid that's 80 by 24 characters wide. 1469 01:02:57,236 --> 01:02:59,466 I just want to take for granted that someone else solved 1470 01:02:59,466 --> 01:03:01,686 that problem so I can do something more interesting 1471 01:03:01,686 --> 01:03:01,926 with it. 1472 01:03:02,166 --> 01:03:04,586 Same thing for get int as you'll see in later weeks 1473 01:03:04,586 --> 01:03:09,006 when we peel back the layer of the CS50 library 1474 01:03:09,006 --> 01:03:10,306 and you see ho we implemented it. 1475 01:03:10,306 --> 01:03:12,676 You'll see that [Sound effects] so many lines of code just 1476 01:03:12,676 --> 01:03:15,536 to get an integer from the user, really takes the fun 1477 01:03:15,536 --> 01:03:16,816 out of programming potentially. 1478 01:03:16,816 --> 01:03:18,496 And that's why you'll see throughout the course 1479 01:03:18,766 --> 01:03:21,586 and computer science programming that there's a reason 1480 01:03:21,586 --> 01:03:23,726 that people have developed libraries and all 1481 01:03:23,726 --> 01:03:24,596 of these header files. 1482 01:03:24,806 --> 01:03:26,476 Because why did we invent the wheel, 1483 01:03:26,476 --> 01:03:29,036 if you can leverage someone else's work like printf 1484 01:03:29,036 --> 01:03:32,126 and get this and build you own far more interesting stuff 1485 01:03:32,126 --> 01:03:32,856 on top of it. 1486 01:03:33,036 --> 01:03:35,636 And that's related to this tidbit here right. 1487 01:03:35,636 --> 01:03:38,596 It seems to be an opportunity for this stupid chorus 1488 01:03:38,666 --> 01:03:41,136 that the kids might sing, let me just outsource that. 1489 01:03:41,546 --> 01:03:44,556 Let me just let someone else generate all of the sentences 1490 01:03:44,556 --> 01:03:46,846 that pertain to the chorus and the only thing I'm going 1491 01:03:46,846 --> 01:03:50,276 to tell this black box, this little Oracle is the number 1492 01:03:50,276 --> 01:03:52,066 of bottles of beer currently on the wall. 1493 01:03:52,066 --> 01:03:54,626 And I want that entity, that black box 1494 01:03:54,946 --> 01:03:56,106 to do the printing for me. 1495 01:03:56,416 --> 01:03:58,066 So in fact that's exactly what I do 1496 01:03:58,066 --> 01:04:01,076 and I simultaneously introduce just a couple of new tricks just 1497 01:04:01,076 --> 01:04:01,936 for the sake of syntax. 1498 01:04:02,536 --> 01:04:05,946 So it turns out in a Boolean expression you don't have 1499 01:04:05,986 --> 01:04:09,396 to say while N is greater than 0 as I did 1500 01:04:09,396 --> 01:04:10,976 in a previous version right. 1501 01:04:10,976 --> 01:04:13,396 It's equivalent to just say while N 1502 01:04:13,856 --> 01:04:17,726 because if N keeps getting decremented so N is 99, then 98, 1503 01:04:17,726 --> 01:04:21,426 then, dot, dot, dot, then 1 then 0 at some point 1504 01:04:21,426 --> 01:04:23,856 in the story you will have while 0. 1505 01:04:24,186 --> 01:04:27,406 But 0 is false by nature so even though you don't have the less 1506 01:04:27,486 --> 01:04:30,236 than or the greater than sign in there it's equivalent 1507 01:04:30,416 --> 01:04:31,996 to just terminating the loop. 1508 01:04:32,166 --> 01:04:34,476 So that's why I can get away with this cleverness here 1509 01:04:34,666 --> 01:04:37,946 and then this cleverness is arguably confusing here 1510 01:04:37,946 --> 01:04:39,256 but lets introduce the syntax. 1511 01:04:39,706 --> 01:04:43,096 Chorus is a function so just as main is a function, 1512 01:04:43,626 --> 01:04:44,996 which you kind of have to write 1513 01:04:45,256 --> 01:04:47,196 because otherwise your program generally is not going 1514 01:04:47,196 --> 01:04:47,866 to do anything. 1515 01:04:48,156 --> 01:04:50,076 You can write your own functions just 1516 01:04:50,076 --> 01:04:51,836 as someone else wrote printf and just 1517 01:04:51,836 --> 01:04:54,306 as the staff wrote get int and get float. 1518 01:04:54,466 --> 01:04:56,856 I or the person who wrote this code decided 1519 01:04:56,856 --> 01:05:00,116 to call the function chorus, so apparently there's a function 1520 01:05:00,116 --> 01:05:01,706 in this world now called chorus 1521 01:05:02,066 --> 01:05:04,396 that if you pass it an integer it's going 1522 01:05:04,396 --> 01:05:06,146 to presumably print the chorus. 1523 01:05:06,486 --> 01:05:08,666 But you what I also just want to be clever here. 1524 01:05:08,896 --> 01:05:12,426 I didn't really love this code you know, 1525 01:05:12,426 --> 01:05:14,486 again a little anal I feel 1526 01:05:14,486 --> 01:05:18,146 like this is [Sound effects] just why the curly braces, 1527 01:05:18,146 --> 01:05:19,026 why the additional line? 1528 01:05:19,026 --> 01:05:20,146 Now it's perfectly clear 1529 01:05:20,376 --> 01:05:22,156 and it's you know, 100 percent credit. 1530 01:05:22,156 --> 01:05:25,096 Were not preaching that this is bad code, bad design, bad style. 1531 01:05:25,406 --> 01:05:27,986 Perfectly acceptable, but realize especially 1532 01:05:27,986 --> 01:05:30,476 when it comes time to read other people's code whether it's 1533 01:05:30,476 --> 01:05:33,866 in a book or in a class that this has the same effect. 1534 01:05:34,506 --> 01:05:37,176 So this is where that big ugly table of precedence 1535 01:05:37,176 --> 01:05:41,006 and operators comes in handy as a reference in the long run. 1536 01:05:41,006 --> 01:05:43,106 You can determine using that or just trial 1537 01:05:43,106 --> 01:05:44,156 and error, what's happening? 1538 01:05:44,156 --> 01:05:46,926 So in fact what happens in this context is the value 1539 01:05:46,926 --> 01:05:50,226 of N gets passed into chorus, the function just 1540 01:05:50,226 --> 01:05:51,946 as a variable would get passed into printf. 1541 01:05:52,476 --> 01:05:55,106 And then after that function is done doing its thing, 1542 01:05:55,416 --> 01:05:57,876 then N simultaneously gets decremented. 1543 01:05:58,436 --> 01:06:01,106 So it's the same as the code I just wrote and then deleted. 1544 01:06:01,316 --> 01:06:02,746 So what in the world is chorus? 1545 01:06:02,746 --> 01:06:04,566 Well here's how you write a method. 1546 01:06:04,566 --> 01:06:06,106 Wow that code actually looks familiar. 1547 01:06:06,366 --> 01:06:09,606 I apparently ripped out the code from main, put it later 1548 01:06:09,606 --> 01:06:12,626 in the file down below and I simply wrote this. 1549 01:06:13,176 --> 01:06:15,536 So I wrote chorus and then what did I do? 1550 01:06:15,536 --> 01:06:17,896 Well chorus has to be parameterized, 1551 01:06:18,036 --> 01:06:20,226 it needs to take input and inputs 1552 01:06:20,266 --> 01:06:23,136 to functions are called parameters or arguments. 1553 01:06:23,526 --> 01:06:24,886 So B, what is B? 1554 01:06:24,886 --> 01:06:25,986 B just denotes bottle. 1555 01:06:26,026 --> 01:06:29,106 I could give this parameter any name I want, 1556 01:06:29,206 --> 01:06:31,196 so it's essentially a local variable. 1557 01:06:31,416 --> 01:06:33,916 So anytime a function takes input you gotta tell the 1558 01:06:33,916 --> 01:06:36,176 compiler what do want to call that input? 1559 01:06:36,346 --> 01:06:38,276 So that you the programmer can actually use it. 1560 01:06:38,536 --> 01:06:39,726 What is the type of the input? 1561 01:06:39,726 --> 01:06:43,526 An int. So long story short, just as printf is a function 1562 01:06:43,526 --> 01:06:45,246 that takes a format string on the left 1563 01:06:45,246 --> 01:06:48,426 and then a comma separated list of variables on the right. 1564 01:06:48,826 --> 01:06:51,776 So does chorus, apparently take one argument, 1565 01:06:52,206 --> 01:06:54,266 just an int so that's past in. 1566 01:06:54,676 --> 01:06:57,876 So once it's passe in I can refer to it as B. What do I do? 1567 01:06:57,876 --> 01:07:00,596 Well this code is just ripped off from the previous example. 1568 01:07:00,596 --> 01:07:02,396 I declare a couple of strings up here, 1569 01:07:02,576 --> 01:07:05,016 then I assign them using my fancy little notation here. 1570 01:07:05,246 --> 01:07:07,526 This code is the same as before, it's the same code. 1571 01:07:07,756 --> 01:07:09,156 I've just factored it out. 1572 01:07:09,456 --> 01:07:12,406 I've hierarchically decomposed, fancy way of saying it 1573 01:07:12,406 --> 01:07:13,326 that you'll see in the textbook. 1574 01:07:13,586 --> 01:07:16,716 Decomposed my code into now this separate function 1575 01:07:16,716 --> 01:07:19,076 because you know what, it just feels a little cleaner. 1576 01:07:19,286 --> 01:07:21,546 There was this big ugly block of code in my program, 1577 01:07:21,966 --> 01:07:24,146 feels perfectly reasonable to factor that out 1578 01:07:24,176 --> 01:07:27,846 because it really collectively, the seven lines 1579 01:07:28,026 --> 01:07:31,436 of real code they accomplish a single goal 1580 01:07:31,436 --> 01:07:33,476 that can be well summarized in a sentence. 1581 01:07:33,626 --> 01:07:35,626 Sings about specified numbers of bottles. 1582 01:07:35,826 --> 01:07:37,826 So anytime there's this opportunity to factor 1583 01:07:37,826 --> 01:07:41,276 out a piece of code, a function is the direction 1584 01:07:41,276 --> 01:07:43,036 that most programmers would go in. 1585 01:07:43,296 --> 01:07:46,116 Now there's one last piece of detail here, void. 1586 01:07:46,116 --> 01:07:47,256 What do you think it means 1587 01:07:47,256 --> 01:07:50,426 if the word before the function name here is void? 1588 01:07:50,746 --> 01:07:55,066 It just means it has no return value. 1589 01:07:55,156 --> 01:07:58,906 Get int by nature returns a value right, that's a good thing 1590 01:07:58,906 --> 01:08:01,266 because you use it to get a number from the user. 1591 01:08:01,506 --> 01:08:04,656 But chorus does not need to get any input from the user. 1592 01:08:04,816 --> 01:08:06,896 It just has this quote, unquote side effect 1593 01:08:07,336 --> 01:08:09,146 of printing something to the screen. 1594 01:08:09,396 --> 01:08:10,476 So for now you can kind 1595 01:08:10,476 --> 01:08:12,116 of simplify the world into two buckets. 1596 01:08:12,116 --> 01:08:14,426 Functions that have side effects, just print stuff 1597 01:08:14,426 --> 01:08:15,806 to the screen that if you weren't watching 1598 01:08:15,806 --> 01:08:17,556 that you would never know they did anything. 1599 01:08:17,756 --> 01:08:19,846 And then functions that have return values. 1600 01:08:20,076 --> 01:08:22,516 So return value here is void as in this case. 1601 01:08:22,776 --> 01:08:25,386 Return value here is int, or string, or char, 1602 01:08:25,386 --> 01:08:26,926 or whatever the case may be. 1603 01:08:26,976 --> 01:08:28,546 We'll see we can do anything we want. 1604 01:08:28,726 --> 01:08:32,306 So this is why up here I just call chorus just 1605 01:08:32,306 --> 01:08:33,206 like I call printf. 1606 01:08:33,636 --> 01:08:36,346 I don't have to assign its return value to a variable 1607 01:08:36,596 --> 01:08:39,486 because it's not actually returning a variable at all. 1608 01:08:39,846 --> 01:08:42,736 So let me go ahead and print out the results 1609 01:08:42,856 --> 01:08:44,356 of these statements here. 1610 01:08:44,356 --> 01:08:47,536 Let me go ahead and quit. 1611 01:08:47,796 --> 01:08:50,736 Let me go ahead an make beer 4. 1612 01:08:51,586 --> 01:08:54,216 Let me go ahead and run beer 4 here. 1613 01:08:54,766 --> 01:08:59,036 99 bottles of beer and viola the same exact example. 1614 01:08:59,766 --> 01:09:01,516 So why don't we end on that note today 1615 01:09:01,516 --> 01:09:04,736 and we'll see you again on Wednesday. 1616 01:09:05,236 --> 01:09:12,610 [Applause]