1 00:00:00,506 --> 00:00:26,546 [ Music ] 2 00:00:27,046 --> 00:00:29,706 >> [Background sound effects] Noah. 3 00:00:29,706 --> 00:00:35,086 Thou shalt build thyself an ark measuring 300 cubids in length. 4 00:00:35,086 --> 00:00:37,146 >> [Background sound effects] 300 cubids give or take. 5 00:00:38,486 --> 00:00:43,846 >> [Background sound effects] Exactly 300 6 00:00:43,846 --> 00:00:48,186 and thou shalt takest two of every creature. 7 00:00:48,186 --> 00:00:48,496 >> Two creatures. 8 00:00:48,496 --> 00:00:49,566 >> [Background sound effects] Two of every creature. 9 00:00:49,566 --> 00:00:50,576 >> Even stink beetles? 10 00:00:50,946 --> 00:00:52,836 >> [Background sound effects] Especially stink beetles. 11 00:00:55,656 --> 00:00:56,266 >> [Background sound effects] Whoa. 12 00:00:56,266 --> 00:00:58,756 Cool. God is so in your face. 13 00:00:58,756 --> 00:01:04,296 >> Yeah he's my favorite fictional character. 14 00:01:04,296 --> 00:01:10,566 >> Oh, it's so late you kids have to go to bed. 15 00:01:10,656 --> 00:01:14,146 >> But the flood's only knee high. 16 00:01:14,146 --> 00:01:18,506 At least let us watch until the midgets drown. 17 00:01:18,506 --> 00:01:20,566 >> Yeah mom come on. 18 00:01:20,746 --> 00:01:24,756 You let us stay up to watch Try McLauren 19 00:01:24,956 --> 00:01:29,756 and such other bible epics such as David versus 20 00:01:29,756 --> 00:01:33,016 and Super Goliath and Suddenly Last Supper. 21 00:01:33,016 --> 00:01:35,286 >> Go Lis. 22 00:01:35,286 --> 00:01:36,666 Way to site precedent. 23 00:01:36,666 --> 00:01:36,733 [Sound effects] 24 00:01:36,733 --> 00:01:36,966 >> Well alright. 25 00:01:36,996 --> 00:01:37,686 You can stay up late tonight 26 00:01:37,716 --> 00:01:39,036 but tomorrow everyone's going to bed at 5 o'clock. 27 00:01:39,036 --> 00:01:39,103 [Sound effects] 28 00:01:39,103 --> 00:01:39,756 >> [Background sound effects] Go forth Noah and remember, 29 00:01:39,786 --> 00:01:40,386 the key to salvation is -- 30 00:01:40,416 --> 00:01:41,856 >> You've seen the movie now be a real life Noah only this Noah 31 00:01:41,886 --> 00:01:43,116 has been accused of killing two of every animal. 32 00:01:43,146 --> 00:01:44,046 Coming up next on AM Springfield. 33 00:01:44,076 --> 00:01:44,406 >> Oh, my goodness. 34 00:01:44,436 --> 00:01:44,976 That was a long movie. 35 00:01:45,516 --> 00:02:00,606 [ Sound effects ] 36 00:02:01,106 --> 00:02:01,996 >> Lighten up Lis. 37 00:02:02,306 --> 00:02:03,976 >> Finland, let's see that native dance. 38 00:02:04,516 --> 00:02:15,846 [ Sound effects ] 39 00:02:16,346 --> 00:02:19,896 >> I head they sent a rocket to the sun once at night 40 00:02:20,606 --> 00:02:23,706 and there was that submarine with screen door. 41 00:02:23,706 --> 00:02:24,546 >> No, no, no, no. 42 00:02:24,546 --> 00:02:28,826 no. Young man you need to do some serious boning. 43 00:02:28,826 --> 00:02:28,906 [Sound effects] 44 00:02:28,906 --> 00:02:30,396 >> Oh grow up Lis. 45 00:02:30,596 --> 00:02:32,576 >> Okay Libya exports. 46 00:02:32,706 --> 00:02:36,696 >> Yes sir you American pig. 47 00:02:37,156 --> 00:02:38,276 >> [Laughter] Nice touch. 48 00:02:38,686 --> 00:02:39,176 >> [Sound effects] Let's see, 49 00:02:39,836 --> 00:02:42,616 the exports of Libya are numerous in amount. 50 00:02:42,986 --> 00:02:45,196 One thing they export is corn, 51 00:02:46,056 --> 00:02:48,416 or as the Indians call it, maize. 52 00:02:48,416 --> 00:02:51,406 Another famous Indian was Crazy Horse. 53 00:02:51,406 --> 00:02:54,236 In conclusion Libya is a land of contrast. 54 00:02:54,236 --> 00:02:54,596 Thank you. 55 00:02:54,686 --> 00:02:57,166 >> Ow, I can't breath. 56 00:02:57,166 --> 00:02:58,566 Please stop em. 57 00:02:58,566 --> 00:03:02,266 >> I'd like to, but I'm afraid you need diplomatic immunity. 58 00:03:02,426 --> 00:03:03,696 >> Point of order, if you want 59 00:03:03,696 --> 00:03:05,066 to learn anything we must respect -- 60 00:03:05,066 --> 00:03:08,696 >> Point of odor, Lisa stinks. 61 00:03:08,696 --> 00:03:08,763 [Laughter] 62 00:03:08,763 --> 00:03:09,516 >> Leave her alone. 63 00:03:09,516 --> 00:03:09,906 >> You leave her alone. 64 00:03:10,516 --> 00:03:15,056 [ Sound effects ] 65 00:03:15,556 --> 00:03:16,896 >> [Background sound effects] Order, order, order. 66 00:03:16,896 --> 00:03:19,756 Do you kids want to be like the real UN or do you just want 67 00:03:19,756 --> 00:03:24,196 to squabble and waste time? 68 00:03:24,196 --> 00:03:24,296 [Music] 69 00:03:24,296 --> 00:03:25,856 >> [Background music] Have a great weekend kids. 70 00:03:26,046 --> 00:03:28,466 Be nice to the underprivileged countries. 71 00:03:28,466 --> 00:03:29,416 >> Good luck Ralphie. 72 00:03:29,596 --> 00:03:31,276 If you're nose starts bleeding, 73 00:03:31,366 --> 00:03:34,446 it means you're picking it too much, or not enough. 74 00:03:35,606 --> 00:03:37,696 >> Okay kids, Otto's in charge. 75 00:03:38,006 --> 00:03:40,826 Remember Otto, we're trusting you 76 00:03:40,826 --> 00:03:45,556 with our greatest natural resource, the school bus. 77 00:03:45,556 --> 00:03:45,623 [Music] 78 00:03:45,623 --> 00:03:47,146 >> Water bill, third notice. 79 00:03:47,306 --> 00:03:48,686 Jury duty, third notice. 80 00:03:48,686 --> 00:03:52,526 Mortgage bill, ewh, second notice. 81 00:03:53,136 --> 00:03:55,286 Flant Crest Enterprises. 82 00:03:55,706 --> 00:03:57,326 >> Oops, that's for me. 83 00:03:57,326 --> 00:03:59,626 Flant Crest Enterprises is my home business. 84 00:03:59,666 --> 00:04:00,686 >> You liar. 85 00:04:00,996 --> 00:04:02,356 You don't have a home a business, 86 00:04:02,556 --> 00:04:04,206 why would you make up a lie like that? 87 00:04:04,446 --> 00:04:05,836 >> No it's true. 88 00:04:06,056 --> 00:04:08,326 Mom and I sell religious hook rugs over the internet. 89 00:04:08,806 --> 00:04:09,586 >> Internet eh? 90 00:04:09,586 --> 00:04:11,046 >> Yes indeedy. 91 00:04:11,166 --> 00:04:12,756 I get some good scratch too. 92 00:04:13,006 --> 00:04:13,876 >> Scratch eh? 93 00:04:13,876 --> 00:04:14,656 >> Yep. 94 00:04:15,186 --> 00:04:16,236 >> More, eh? 95 00:04:16,236 --> 00:04:16,956 >> Yep. 96 00:04:17,516 --> 00:04:22,546 [ Sound effects ] 97 00:04:23,046 --> 00:04:23,746 >> Homer what are ya doin? 98 00:04:23,746 --> 00:04:24,826 >> No time to answer that Marge. 99 00:04:25,026 --> 00:04:28,036 I'm setting up a home office for my new business enterprise. 100 00:04:28,296 --> 00:04:30,946 >> What business enterprise? 101 00:04:30,946 --> 00:04:31,556 >> [Background sound effects] Ever heard 102 00:04:31,556 --> 00:04:33,656 of a little thing called the internet? 103 00:04:33,656 --> 00:04:35,236 >> The internet, eh? 104 00:04:35,306 --> 00:04:36,556 >> Oh yeah. 105 00:04:36,556 --> 00:04:39,716 Everybody's making money off the internet except us. 106 00:04:40,006 --> 00:04:42,826 We falling behind, way behind? 107 00:04:42,946 --> 00:04:44,016 >> Is that my good butter? 108 00:04:44,276 --> 00:04:45,586 >> Can't discuss that now Marge. 109 00:04:45,786 --> 00:04:50,226 I have to write another delicious memo. 110 00:04:50,676 --> 00:04:52,926 Memo. 111 00:04:53,516 --> 00:04:58,666 [ Sound effects ] 112 00:04:59,166 --> 00:05:07,646 >> [Background sound effects] This song is driving me crazy. 113 00:05:07,646 --> 00:05:07,713 [Sound effects] 114 00:05:07,713 --> 00:05:12,026 >> I don't know why I bought this stupid cake. 115 00:05:12,026 --> 00:05:12,206 [Sound effects] 116 00:05:12,206 --> 00:05:15,976 >> All right and that is the connection to CS50. 117 00:05:16,516 --> 00:05:23,826 [ Sound effects ] 118 00:05:24,326 --> 00:05:26,416 >> Alright, welcome back to CS50, 119 00:05:26,456 --> 00:05:28,496 this is the end of week two. 120 00:05:28,496 --> 00:05:32,086 So a couple of announcements -- So if you did the hacker edition 121 00:05:32,086 --> 00:05:34,716 of Pset 0 and still have a sensor board, that's fine. 122 00:05:34,986 --> 00:05:37,226 Make sure your name, user name and ID number are on a piece 123 00:05:37,226 --> 00:05:39,526 of paper in the Ziploc bag and just hand it to one 124 00:05:39,526 --> 00:05:41,436 of the teaching fellows on your way out today. 125 00:05:42,296 --> 00:05:46,776 We've gotten plenty of responses regarding lunch with me and CA's 126 00:05:46,776 --> 00:05:49,566 and TF's and we will follow up by email sometime tomorrow 127 00:05:49,866 --> 00:05:51,126 to figure out the logistics. 128 00:05:51,126 --> 00:05:54,876 And it turns out if you're nice enough to one of the houses 129 00:05:54,906 --> 00:05:56,556 on campus, they invite you to dinner. 130 00:05:57,016 --> 00:06:01,106 So now have a dinner with David and team next Wednesday. 131 00:06:01,106 --> 00:06:02,996 It's part of their senior common room event. 132 00:06:03,566 --> 00:06:05,046 Space for that is very limited. 133 00:06:05,046 --> 00:06:09,296 The idea is to get together around dinner in the Mather SCR 134 00:06:09,296 --> 00:06:11,926 as well as the dining hall and to chat and get to know some 135 00:06:11,926 --> 00:06:14,866 of the TF's and CA's and fellow students in the class as well 136 00:06:14,866 --> 00:06:16,076 as some fellow Matherites. 137 00:06:16,076 --> 00:06:19,716 So if that is of interest, next Wednesday as a one-time thing, 138 00:06:19,796 --> 00:06:23,036 cs50.net/dinner to RSVP. 139 00:06:23,036 --> 00:06:26,016 Speaking of David there's no need certainly 140 00:06:26,016 --> 00:06:27,346 for formalities in this course. 141 00:06:27,806 --> 00:06:30,296 It is perfectly fine to call me David and as fact, 142 00:06:30,296 --> 00:06:32,626 sweet as you are, at the end of these lectures to do this things 143 00:06:32,626 --> 00:06:35,226 like this, it's really just a computer science lecture. 144 00:06:35,226 --> 00:06:38,726 So there's no need for that either. 145 00:06:39,276 --> 00:06:42,586 Sections have -- sectioning has happened thanks to Jansu 146 00:06:42,586 --> 00:06:43,086 and Yukie [Assumed spelling] 147 00:06:43,936 --> 00:06:45,546 to use their computer science skills 148 00:06:45,546 --> 00:06:48,276 to take a very large dataset from FAS and also 149 00:06:48,276 --> 00:06:50,166 from our own database, mash them together 150 00:06:50,166 --> 00:06:52,706 and email 300 plus emails out automatically. 151 00:06:53,236 --> 00:06:56,106 If you ended up by algorithmic chance 152 00:06:56,106 --> 00:06:58,146 in a section you're not comfortable in, 153 00:06:58,416 --> 00:07:01,986 whether it's too much or too little comfort, that's fine. 154 00:07:02,206 --> 00:07:06,116 Just go ahead and email sectionCS50.net no later 155 00:07:06,116 --> 00:07:06,986 than this Friday. 156 00:07:07,316 --> 00:07:09,796 Or if you simply can't make your assigned section time, 157 00:07:09,836 --> 00:07:10,936 email us as well. 158 00:07:11,276 --> 00:07:14,426 Office hours continue to be in progress, especially now 159 00:07:14,426 --> 00:07:16,656 as Friday's deadline is coming up for Pset 1. 160 00:07:16,956 --> 00:07:19,576 If you're having any discomfort or you're kind of freaking 161 00:07:19,576 --> 00:07:22,836 out because you really just don't know how to do C 162 00:07:22,836 --> 00:07:25,156 or program, it's totally fine. 163 00:07:25,156 --> 00:07:27,766 Realize that there will be plenty of one-on-one help 164 00:07:27,766 --> 00:07:28,866 in the science center. 165 00:07:29,306 --> 00:07:32,566 You take a look at CS50.net/officehours 166 00:07:32,566 --> 00:07:36,626 for the current Google calendar and this is an opportunity to, 167 00:07:36,936 --> 00:07:39,786 as you can see it all barely fits, because there's 168 00:07:39,786 --> 00:07:42,436 so many people who will be there in attendance. 169 00:07:42,666 --> 00:07:44,256 But for me at least ask this, 170 00:07:44,556 --> 00:07:47,716 Marta this past Sunday did deliver the first walk-through 171 00:07:47,716 --> 00:07:49,266 for Pset 1. 172 00:07:49,266 --> 00:07:52,146 You can find the video for that or the MP3 173 00:07:52,146 --> 00:07:54,426 on the problem sets page on the courses website. 174 00:07:54,666 --> 00:07:56,126 So if you you're asking yourself this question, 175 00:07:56,126 --> 00:07:57,406 "I have no idea where to begin. 176 00:07:57,406 --> 00:07:58,486 How do I do so?" 177 00:07:58,756 --> 00:08:00,996 Do at least start with that video as it's meant 178 00:08:00,996 --> 00:08:03,496 to answer precisely that question. 179 00:08:03,496 --> 00:08:06,636 And I realize that we go quickly in the course and that I've put 180 00:08:06,636 --> 00:08:10,086 up you know a dozen or more programs in C already. 181 00:08:10,276 --> 00:08:13,276 And a lot of it might very well feel like this, but honestly 182 00:08:13,336 --> 00:08:16,576 if not yet sat down to problem set one and plan to tonight 183 00:08:16,576 --> 00:08:19,496 or tomorrow, I think you'll find once you spend some time 184 00:08:19,496 --> 00:08:22,556 with this getting your hands dirty, it will all begin to sink 185 00:08:22,556 --> 00:08:24,586 in and not feel so cryptic. 186 00:08:24,766 --> 00:08:27,336 Finally, it turns out it's a big mistake 187 00:08:27,336 --> 00:08:30,046 to say something off the cuff to say something in lecture like, 188 00:08:30,046 --> 00:08:31,636 "Gee maybe you should all check the map 189 00:08:31,696 --> 00:08:33,556 to make sure you submitted problem set 0" 190 00:08:33,756 --> 00:08:36,626 because this induced innumerable personal emails to me 191 00:08:36,886 --> 00:08:39,626 and I then had to go through our survey data checking whether 192 00:08:39,626 --> 00:08:41,256 or not everyone in fact submitted. 193 00:08:41,256 --> 00:08:42,656 Or if it was a Google problem, 194 00:08:42,966 --> 00:08:45,466 version one of our map had a couple of bugs, 195 00:08:45,466 --> 00:08:48,386 one of which was Google induced because we tried 196 00:08:48,386 --> 00:08:50,306 to hammer their servers a little too fast. 197 00:08:50,566 --> 00:08:53,676 They don't like it when we send 350 plus requests within in 198 00:08:53,676 --> 00:08:55,736 like five seconds for GPS coordinates. 199 00:08:55,946 --> 00:08:58,186 So they essentially blacklisted us once in a while. 200 00:08:58,186 --> 00:09:00,466 That meant some of you didn't make the map and so forth. 201 00:09:00,786 --> 00:09:03,746 So anyhow, sat down last night and redid the whole thing 202 00:09:03,936 --> 00:09:06,856 and what we now have is a full-fledged little application 203 00:09:06,856 --> 00:09:09,956 here, much prettier I think than the original version, 204 00:09:09,956 --> 00:09:11,496 because I also addressed some other issues. 205 00:09:11,496 --> 00:09:14,846 I also realized though perfect this initial version originally 206 00:09:14,846 --> 00:09:19,426 was, last night around 1:00 a.m. on like my 8-core, 3 Gigahertz, 207 00:09:19,426 --> 00:09:22,726 MacPro, I then pulled out my laptop, which is far slower 208 00:09:22,726 --> 00:09:25,516 and the thing sucked; like it took multiple seconds just 209 00:09:25,516 --> 00:09:26,376 for the page to load. 210 00:09:26,586 --> 00:09:28,226 We had this map on our home page, 211 00:09:28,476 --> 00:09:31,096 didn't feel like the best idea for people's first experience 212 00:09:31,096 --> 00:09:34,166 to the site to be a little spinning globe or what not 213 00:09:34,166 --> 00:09:35,116 as the thing loads up. 214 00:09:35,116 --> 00:09:36,536 So we re-wrote the whole thing 215 00:09:36,706 --> 00:09:38,546 and now what you see are little hot spots. 216 00:09:38,676 --> 00:09:41,016 One of the reasons for the speed degradation was 217 00:09:41,016 --> 00:09:44,716 that when you try to put 350 of these little cartoon markers 218 00:09:44,716 --> 00:09:47,666 on a map, that takes a lot of CPU cycles. 219 00:09:47,666 --> 00:09:49,656 It's a lot of Java script codes to make that happen, 220 00:09:49,906 --> 00:09:51,186 but now it's a little fancier. 221 00:09:51,186 --> 00:09:54,306 So now you can zoom in and then as soon as you are closer 222 00:09:54,306 --> 00:09:56,806 to the earth, it then shows you more granularity, 223 00:09:56,806 --> 00:09:57,616 where things are. 224 00:09:57,886 --> 00:10:02,346 I can keep zooming and zooming and here we can finally get 225 00:10:02,346 --> 00:10:04,896 into the downtown Boston area. 226 00:10:05,036 --> 00:10:08,536 And in fact John and David J. are in fact 227 00:10:08,536 --> 00:10:09,926 from Boston, Massachusetts. 228 00:10:09,926 --> 00:10:11,516 So this too was an interesting bug. 229 00:10:11,786 --> 00:10:13,776 At first I didn't realize that if you try 230 00:10:13,776 --> 00:10:17,396 to plant multiple markers, i.e. multiple students or staff, 231 00:10:17,716 --> 00:10:19,396 at the exact same GPS coordinates, 232 00:10:19,626 --> 00:10:22,556 Google just pretty much shows the last person you put there. 233 00:10:22,826 --> 00:10:24,586 So this felt a little sad 234 00:10:24,626 --> 00:10:27,306 because David J. was the last one put there. 235 00:10:27,306 --> 00:10:30,326 And I realized, "Damn, I'm about to get an email from John, 236 00:10:30,326 --> 00:10:32,536 quote, unquote if I leave him off the map." 237 00:10:32,536 --> 00:10:33,536 So we fixed that too. 238 00:10:33,826 --> 00:10:35,926 We figured out what kind of algorithm it would take, 239 00:10:36,186 --> 00:10:37,636 though only a few lines of code 240 00:10:37,636 --> 00:10:41,786 to make a comma separate an English list of names like John 241 00:10:41,986 --> 00:10:45,096 and David J. putting the word -- the commas in at the end. 242 00:10:45,306 --> 00:10:46,206 So there was actually a lot 243 00:10:46,206 --> 00:10:49,316 of little bite size programming projects that arose as a result. 244 00:10:49,556 --> 00:10:52,026 But do feel free to play and I think you'll find 245 00:10:52,026 --> 00:10:54,586 that CS50 students do hail from all over the world. 246 00:10:54,586 --> 00:10:57,286 And what you can also do now, if you don't see yourself 247 00:10:57,566 --> 00:10:59,536 or if you're smack dab in the middle of the ocean 248 00:10:59,536 --> 00:11:01,896 where there's in fact no land because of a typo, 249 00:11:02,226 --> 00:11:05,086 click on Log-In at the top of the page. 250 00:11:05,316 --> 00:11:06,716 You can then click About Me 251 00:11:06,746 --> 00:11:10,996 and you can now change your own hometown and no need for all 252 00:11:10,996 --> 00:11:12,186 of the emails anymore. 253 00:11:12,186 --> 00:11:14,686 So, we've hopefully empowered you for that. 254 00:11:15,506 --> 00:11:18,106 Any questions, logistical or otherwise? 255 00:11:19,576 --> 00:11:22,056 I'll admit it's actually really kind of neat to just point 256 00:11:22,056 --> 00:11:25,166 and click around and just see from how all over students 257 00:11:25,166 --> 00:11:26,956 in this class actually are. 258 00:11:27,056 --> 00:11:28,936 So, have fun with that if you will. 259 00:11:29,866 --> 00:11:35,336 So, I thought we'd take a look at a little bite sized program 260 00:11:35,386 --> 00:11:37,946 that I actually whipped up right before class 261 00:11:38,286 --> 00:11:40,866 to address a questions or challenge that a number 262 00:11:40,866 --> 00:11:43,326 of you have run into, and that's this problem with imprecision. 263 00:11:43,636 --> 00:11:45,776 So we've been -- or you've been looking at problems that one, 264 00:11:45,776 --> 00:11:47,886 or starting tonight or tomorrow will be looking 265 00:11:47,886 --> 00:11:49,596 at some matters of imprecision. 266 00:11:49,876 --> 00:11:53,596 And remember that that just boils down to this problem of C 267 00:11:53,596 --> 00:11:57,506 and a lot of languages only using a finite number of bits 268 00:11:57,646 --> 00:11:59,516 to represent some value, some number. 269 00:11:59,516 --> 00:12:01,086 So if you've only got a finite number of bits, 270 00:12:01,136 --> 00:12:04,366 clearly there's an upper bound on just how precise you can be, 271 00:12:04,366 --> 00:12:07,856 how many numbers after the decimal point you can express. 272 00:12:08,106 --> 00:12:11,556 And a really simple way of seeing this as a footnote 273 00:12:11,556 --> 00:12:15,136 in problems that one says is to try to represent and a float, 274 00:12:15,136 --> 00:12:17,726 a very simple value of one one-hundredth. 275 00:12:17,876 --> 00:12:20,536 So .01, such a small number. 276 00:12:20,566 --> 00:12:23,256 It feels like it should be represented perfectly cleanly 277 00:12:23,256 --> 00:12:26,266 underneath the hood, because my God it's such a common, simple, 278 00:12:26,526 --> 00:12:27,876 relatively small number. 279 00:12:27,926 --> 00:12:30,426 But what you see here is that's not in fact the case. 280 00:12:30,486 --> 00:12:32,076 So I wrote this really quick program. 281 00:12:32,376 --> 00:12:37,466 A float F is initialized to 0.01 and realized that some 282 00:12:37,466 --> 00:12:38,806 of these things are just conventions. 283 00:12:38,986 --> 00:12:41,556 That's equivalent to leaving off the leading 0. 284 00:12:41,826 --> 00:12:44,766 This is not equivalent obviously mathematically, 285 00:12:45,066 --> 00:12:46,956 but I really wanted to see this value. 286 00:12:46,956 --> 00:12:49,416 So instead of just printing the value with percent F, 287 00:12:50,036 --> 00:12:55,956 I used .10 F, which even though it feels 288 00:12:55,956 --> 00:12:57,966 like I just transposed 01, 289 00:12:58,136 --> 00:12:59,516 it actually means something different. 290 00:12:59,516 --> 00:13:04,576 What does it mean if I say percent, .10 F? 291 00:13:04,876 --> 00:13:05,056 [Inaudible background discussion] Yeah, 292 00:13:05,056 --> 00:13:06,356 so 10 decimal places. 293 00:13:06,396 --> 00:13:08,696 So the point just means after the decimal point 294 00:13:08,696 --> 00:13:10,766 in this program, show me 10 decimal places. 295 00:13:10,996 --> 00:13:12,846 So what that means is I'll actually be able 296 00:13:12,846 --> 00:13:16,046 to see exactly how, or more exactly, 297 00:13:16,046 --> 00:13:18,246 how this program is represented underneath the hood. 298 00:13:18,536 --> 00:13:20,856 So let me go ahead and run GCC 299 00:13:20,986 --> 00:13:23,686 on imprecision dot C. I'm now going 300 00:13:23,686 --> 00:13:25,346 to run the default name of A dot out. 301 00:13:25,806 --> 00:13:27,696 So what I see is, "Ah hah." 302 00:13:27,886 --> 00:13:30,546 So I the programmer have been completely clear 303 00:13:30,546 --> 00:13:32,326 about what number I want to express. 304 00:13:32,836 --> 00:13:37,876 C just can't quite give it to me because of the judgments it has 305 00:13:38,126 --> 00:13:39,636 to make as to exactly 306 00:13:39,716 --> 00:13:42,246 which numbers it can represent precisely and which it's going 307 00:13:42,246 --> 00:13:44,076 to have to represent almost. 308 00:13:44,076 --> 00:13:45,626 And you can see it here. 309 00:13:45,916 --> 00:13:49,226 But had I done this realized, had I just printed it out, 310 00:13:49,736 --> 00:13:55,206 as a percent F notice that that kind of detail is kind of lost. 311 00:13:55,206 --> 00:13:57,646 So this actually looks much better by default. 312 00:13:57,916 --> 00:13:59,246 If you don't specify the point, 313 00:13:59,316 --> 00:14:01,666 something F you just get some number of decimal places. 314 00:14:01,916 --> 00:14:03,676 It looks like the number is in fact correct, 315 00:14:04,026 --> 00:14:07,256 but as some of you have realized intentionally per the problem 316 00:14:07,256 --> 00:14:10,276 set you start to trip over some subtle bugs. 317 00:14:10,276 --> 00:14:11,646 If you're trying to make change, 318 00:14:11,646 --> 00:14:13,676 especially in problem set 1 you realize 319 00:14:13,676 --> 00:14:17,216 that occasionally you're giving the wrong number of coins back 320 00:14:17,336 --> 00:14:19,046 because the rounding is off. 321 00:14:19,046 --> 00:14:22,736 Some of you have already had or solved this question already, 322 00:14:23,016 --> 00:14:23,906 well how do you round? 323 00:14:24,316 --> 00:14:26,296 Well if you can imagine the name 324 00:14:26,296 --> 00:14:29,286 of a function existing it might very well exist. 325 00:14:29,286 --> 00:14:32,456 In fact, so there is in fact a function called round. 326 00:14:32,696 --> 00:14:34,596 So what I wanted to demonstrate here is the following. 327 00:14:34,596 --> 00:14:37,546 I just want an int so I'll call it the generic I and I want 328 00:14:37,546 --> 00:14:39,456 to initialize I to the result 329 00:14:39,456 --> 00:14:42,076 of the mathematically rounding the process 330 00:14:42,076 --> 00:14:45,766 of eps decimal point two places to the right. 331 00:14:46,156 --> 00:14:47,996 And this is very similar of course 332 00:14:47,996 --> 00:14:50,196 to what you're doing in the problem set. 333 00:14:50,396 --> 00:14:55,276 So I have .01 I want to move the decimal point one, two positions 334 00:14:55,276 --> 00:14:57,466 to the right and hopefully the answer I'm going 335 00:14:57,466 --> 00:14:59,186 to get back is in fact, 1. 336 00:14:59,446 --> 00:15:01,956 But let's see what happens if I do this simple way. 337 00:15:02,026 --> 00:15:07,026 So let me actually undo the fix that I ultimately put in place. 338 00:15:07,026 --> 00:15:10,546 So I'm going to get rid of this rounding altogether. 339 00:15:10,546 --> 00:15:14,466 And I'm just going to leave it at this, so I gets F times 100. 340 00:15:14,466 --> 00:15:16,456 It's so simple, just move the decimal points. 341 00:15:16,656 --> 00:15:19,056 I know I'm going to lose the rest of the decimal points 342 00:15:19,056 --> 00:15:20,946 because if I change from a flow to an int, 343 00:15:21,256 --> 00:15:24,156 remember you passed all the way after the decimal point. 344 00:15:24,406 --> 00:15:27,056 Now notice I'm using percent D, so that's in fact correct. 345 00:15:27,356 --> 00:15:31,216 So let me go ahead and recompile it, rerun it and you know what, 346 00:15:31,296 --> 00:15:34,636 if I take .01 and move the decimal point 347 00:15:34,826 --> 00:15:36,086 on paper two places. 348 00:15:36,486 --> 00:15:38,786 I'm pretty sure I don't get 0, I get 1. 349 00:15:39,036 --> 00:15:41,556 But that's because notice if we move the decimal point 350 00:15:41,556 --> 00:15:49,696 in reality the value I'm getting is really 000.999999998. 351 00:15:50,166 --> 00:15:52,996 So if you then change that value to an int, 352 00:15:53,156 --> 00:15:55,276 by throwing away everything after the decimal point, 353 00:15:55,586 --> 00:15:57,856 obviously you lose all of those numbers 354 00:15:57,856 --> 00:15:59,566 and all have left are the things to the left 355 00:15:59,566 --> 00:16:02,366 of the decimal point, which was in fact, a zero. 356 00:16:02,716 --> 00:16:04,416 So let's go ahead and fix this. 357 00:16:04,526 --> 00:16:07,406 So it turns out there is a function called rounds. 358 00:16:07,456 --> 00:16:09,146 I express it simply by doing this. 359 00:16:09,406 --> 00:16:10,116 It's in a header -- 360 00:16:10,156 --> 00:16:13,446 it's declared in a header file not surprising perhaps called 361 00:16:13,446 --> 00:16:15,556 math dot H. We haven't used it before 362 00:16:15,556 --> 00:16:16,776 but that does in fact exist. 363 00:16:17,036 --> 00:16:20,016 It doesn't matter usually what order I include these files in, 364 00:16:20,016 --> 00:16:23,076 I just tend to be a little anal and do things alphabetically 365 00:16:23,076 --> 00:16:24,406 so I can find things more quickly 366 00:16:24,406 --> 00:16:25,786 when I get more of these things. 367 00:16:26,126 --> 00:16:28,956 But it's in math dot H so it looks like I have a solution. 368 00:16:28,956 --> 00:16:34,396 So GCC imprecision dot C enter, damn so close. 369 00:16:34,996 --> 00:16:37,276 But even though you might not have written this code yet, 370 00:16:37,276 --> 00:16:38,576 what is clearly the problem? 371 00:16:38,576 --> 00:16:43,436 Yeah so I haven't linked in so to speak the library. 372 00:16:43,436 --> 00:16:46,356 So even though standard IO as the name suggests is just 373 00:16:46,356 --> 00:16:51,146 so standard that you don't have say something like dash L STDIO. 374 00:16:51,326 --> 00:16:54,866 Because C links in the standard library automatically 375 00:16:54,866 --> 00:16:58,316 but the math even thought it's not standard so to speak, 376 00:16:58,486 --> 00:17:01,376 so I actually have tell GCC 377 00:17:01,376 --> 00:17:04,676 where to find the header file math dot H but I also have 378 00:17:04,676 --> 00:17:06,856 to tell to link in the math library. 379 00:17:06,856 --> 00:17:07,796 So I'm going to go with my gut. 380 00:17:07,796 --> 00:17:11,976 I'm going to say dash L math and realize there is 381 00:17:11,976 --> 00:17:13,456 in fact no space after this, 382 00:17:13,456 --> 00:17:15,056 and weird though it looks, hit enter. 383 00:17:15,696 --> 00:17:22,336 Hmh, cannot find dash L math, but remember the RTFM proverb 384 00:17:22,336 --> 00:17:24,896 from last week, let's just check the man page. 385 00:17:24,896 --> 00:17:26,576 So man round. 386 00:17:26,866 --> 00:17:28,756 So hopefully there's a manual, so yeah. 387 00:17:29,376 --> 00:17:31,256 The round function, it's apparently 388 00:17:31,256 --> 00:17:32,576 in chapter three of the manual. 389 00:17:32,886 --> 00:17:35,496 Notice I got this part right just by guessing. 390 00:17:35,756 --> 00:17:37,956 I see that there's new versions of round. 391 00:17:37,956 --> 00:17:39,316 The one I used is called round. 392 00:17:39,316 --> 00:17:42,976 You pass in a double or a float, which is not even as good. 393 00:17:43,246 --> 00:17:46,686 Here's the hint, uh the manual answered this question. 394 00:17:46,686 --> 00:17:47,946 So let me go ahead and fix this. 395 00:17:47,946 --> 00:17:52,326 So GCC not dash L math, dash L M, okay that's pretty good. 396 00:17:52,326 --> 00:17:55,976 Let me rerun the program A dot out and now I have the answer. 397 00:17:55,976 --> 00:17:59,246 So we'll see another trick today about how you can round 398 00:17:59,246 --> 00:18:01,176 without even knowing about this round function. 399 00:18:01,366 --> 00:18:03,746 But the exercise here is that if you think 400 00:18:03,816 --> 00:18:06,526 that there might be a function, a utility function of sorts, 401 00:18:06,696 --> 00:18:09,236 well check the man page for the name you might think it has 402 00:18:09,336 --> 00:18:12,816 and more than that, especially if you've not gone ahead 403 00:18:12,816 --> 00:18:14,476 and gotten any of the recommended books, 404 00:18:14,476 --> 00:18:17,716 which really aren't necessary you can go to the resources page 405 00:18:17,716 --> 00:18:20,556 of the course website under C. By far one 406 00:18:20,556 --> 00:18:24,776 of the best references here is this thing, C reference. 407 00:18:24,926 --> 00:18:27,226 We took this from cppreference.com, 408 00:18:27,266 --> 00:18:29,976 C plus plus reference, ripped out all the C plus plus stuff 409 00:18:30,206 --> 00:18:32,066 and left just the C stuff and you no what, 410 00:18:32,066 --> 00:18:33,976 I want some math functions. 411 00:18:33,976 --> 00:18:36,996 Alright standard C math, okay interesting. 412 00:18:36,996 --> 00:18:39,746 It looks like C gives me all of these things, 413 00:18:39,746 --> 00:18:40,656 most of which I don't need. 414 00:18:40,656 --> 00:18:41,806 Oh there's a pal function. 415 00:18:41,806 --> 00:18:43,456 You might find that useful at some point 416 00:18:43,456 --> 00:18:46,496 but not necessarily required anytime soon. 417 00:18:46,646 --> 00:18:51,586 But I want it, interesting that round is not documented here. 418 00:18:52,676 --> 00:18:54,786 Let's see display all entries. 419 00:18:55,816 --> 00:18:57,566 Okay well we won't waste time on this 420 00:18:57,626 --> 00:19:00,166 but somewhere it is actually embedded, 421 00:19:00,396 --> 00:19:02,626 for some reason not here. 422 00:19:02,626 --> 00:19:05,716 Damn, that's an interesting omission. 423 00:19:05,936 --> 00:19:07,636 So for every other function except round, 424 00:19:07,706 --> 00:19:09,076 turn to that documentation, 425 00:19:09,076 --> 00:19:11,136 which I promise you is outstanding. 426 00:19:11,306 --> 00:19:12,556 But it actually it is a good way 427 00:19:12,626 --> 00:19:13,976 of learning some new tricks right. 428 00:19:14,026 --> 00:19:16,646 We can only cover so much in lectures and even in sections. 429 00:19:16,886 --> 00:19:18,496 And frankly you'd get bored quickly 430 00:19:18,546 --> 00:19:19,856 if we spent an hour saying, 431 00:19:20,176 --> 00:19:23,016 "Here are the fifteen functions provided by the math library 432 00:19:23,016 --> 00:19:24,626 and here is fifteen examples of each." 433 00:19:24,766 --> 00:19:26,486 And that kind of stuff you can just kind of flip 434 00:19:26,576 --> 00:19:28,226 through when you care or when you need to. 435 00:19:28,336 --> 00:19:31,026 And again that's all about how one bootstraps him or herself 436 00:19:31,256 --> 00:19:33,876 to learning a bit more about a given language. 437 00:19:33,876 --> 00:19:37,096 So, let's go ahead and learn a bit more. 438 00:19:37,906 --> 00:19:41,496 So, we started last time with a bug. 439 00:19:41,626 --> 00:19:44,536 Let's start this time with another but of sorts related 440 00:19:44,536 --> 00:19:46,996 to this very simple idea of swapping two variables. 441 00:19:46,996 --> 00:19:49,736 Out of context, really not that sexy to say I want 442 00:19:49,736 --> 00:19:52,636 to take this variable and this variable and move it's value 443 00:19:52,636 --> 00:19:54,306 into this and this value into this, 444 00:19:54,306 --> 00:19:57,206 but it's a relatively simple exercise 445 00:19:57,236 --> 00:19:59,136 that triggers some problems. 446 00:19:59,196 --> 00:20:02,496 So in buggy three dot C, which is in your print out from Monday 447 00:20:02,866 --> 00:20:05,686 or if you weren't here there are extra printouts probably still 448 00:20:05,686 --> 00:20:09,036 outside I decided this week today 449 00:20:09,346 --> 00:20:11,126 to take off the training wheels so to speak 450 00:20:11,126 --> 00:20:14,366 so even though I've been very good in putting comments 451 00:20:14,366 --> 00:20:17,076 as you should in problem sets in the printout of this code, 452 00:20:17,296 --> 00:20:20,016 I wrote a little script that rips out all of the comments 453 00:20:20,016 --> 00:20:23,596 from the code displayed on the screen, only because I realized 454 00:20:23,596 --> 00:20:25,176 that if I continue asking questions 455 00:20:25,176 --> 00:20:26,906 like what does this block of code do? 456 00:20:27,196 --> 00:20:29,436 And the answer is they are expressed in that comment, 457 00:20:29,726 --> 00:20:31,086 really not all that stimulating. 458 00:20:31,086 --> 00:20:34,006 So realize you have answers in your handouts if you need 459 00:20:34,006 --> 00:20:36,126 to glance or reference later, but for the sake 460 00:20:36,126 --> 00:20:37,876 of engaging trying to focus up here 461 00:20:38,206 --> 00:20:39,936 when interesting topics come up. 462 00:20:40,016 --> 00:20:42,746 So here is a function, it's called main, 463 00:20:42,926 --> 00:20:44,126 that's my default function. 464 00:20:44,346 --> 00:20:45,456 This is kind of interesting. 465 00:20:45,456 --> 00:20:48,266 There's something interesting and new going on up here 466 00:20:48,476 --> 00:20:49,716 but more on that in a moment. 467 00:20:50,026 --> 00:20:51,106 So what does main do? 468 00:20:51,356 --> 00:20:54,146 Main declares two variables X and Y, gives 1 the value 469 00:20:54,176 --> 00:20:55,506 of 1 and 1 the value of 2. 470 00:20:55,766 --> 00:20:57,776 This program is going to printfF in each in value, 471 00:20:58,046 --> 00:20:59,706 not very interesting and then it's going 472 00:20:59,706 --> 00:21:01,816 to say swapping dot dot dot. 473 00:21:01,876 --> 00:21:02,886 And then it does what? 474 00:21:02,886 --> 00:21:06,866 Well in English what is this line of code doing? 475 00:21:07,226 --> 00:21:08,496 Like how would you express now 476 00:21:08,496 --> 00:21:09,786 that you're getting a little more comfortable 477 00:21:09,786 --> 00:21:12,476 with programming and see, what is that line doing in English. 478 00:21:13,156 --> 00:21:16,876 Okay so I'm calling a function and the function is called swap. 479 00:21:16,876 --> 00:21:21,706 Presumably it's meant to swap X and Y because if you kind 480 00:21:21,706 --> 00:21:24,006 of think ahead where this program is probably going, 481 00:21:24,226 --> 00:21:27,566 I'm hopefully -- I'm trying to demonstrate that X and Y are 1 482 00:21:27,566 --> 00:21:31,056 and 2 but then I call swap and now they are 2 and 1. 483 00:21:31,256 --> 00:21:33,346 Okay so let's see if that is in fact the case. 484 00:21:33,346 --> 00:21:35,766 Let me go ahead and this is buggy 3. 485 00:21:35,826 --> 00:21:39,926 So make buggy 3 enter, and now notice incidentally one 486 00:21:39,926 --> 00:21:42,876 of the upsides of using make even at this point instead 487 00:21:42,876 --> 00:21:45,596 of running GCC manually, which is also instructive, 488 00:21:45,886 --> 00:21:47,536 realize that all the stuff it spits 489 00:21:47,536 --> 00:21:51,406 out automatically has among other things linker flags 490 00:21:51,486 --> 00:21:51,996 like this. 491 00:21:52,276 --> 00:21:56,316 So we sort of aggressively provide you with flags 492 00:21:56,386 --> 00:21:58,696 like dash L M just because we figure 493 00:21:58,696 --> 00:22:00,776 if you're running make you don't want to have to think 494 00:22:00,776 --> 00:22:02,516 about details like that but realize 495 00:22:02,516 --> 00:22:05,236 if you've been using make some of the magic has been happening 496 00:22:05,236 --> 00:22:07,546 because of our automation of that here. 497 00:22:07,816 --> 00:22:09,986 So now I have a program called buggy 3. 498 00:22:10,106 --> 00:22:11,296 It's in my current directory. 499 00:22:11,296 --> 00:22:14,166 I'm going to start getting pedantic with dot slash 500 00:22:14,196 --> 00:22:18,846 to say this directory, a program called buggy 3 enter apparently 501 00:22:18,846 --> 00:22:19,626 it doesn't work. 502 00:22:19,886 --> 00:22:20,656 It's broken. 503 00:22:20,986 --> 00:22:21,556 It's buggy. 504 00:22:21,756 --> 00:22:22,946 So why is that? 505 00:22:22,986 --> 00:22:23,876 So let's take a quick look. 506 00:22:23,876 --> 00:22:26,306 I'm going to -- this code looks pretty good. 507 00:22:26,306 --> 00:22:28,676 It's hard to find but maybe you have found it. 508 00:22:28,676 --> 00:22:31,806 But it's hard to find any fault logically with the code in main. 509 00:22:32,216 --> 00:22:34,726 So mainly presumably in it's this swap function. 510 00:22:34,726 --> 00:22:35,996 Maybe it just doesn't do anything. 511 00:22:35,996 --> 00:22:36,516 So let's look. 512 00:22:36,516 --> 00:22:39,486 So if I scroll down, okay alright somewhat 513 00:22:39,486 --> 00:22:40,596 of a new approach alright. 514 00:22:40,726 --> 00:22:42,806 But hereto, realize even though I'm calling some 515 00:22:42,806 --> 00:22:45,566 of these barriers A, B and temp, which would seem 516 00:22:45,566 --> 00:22:47,736 to violate this principle of good style, 517 00:22:48,096 --> 00:22:52,336 well realize there are some context like iterating in a look 518 00:22:52,536 --> 00:22:54,746 or just calling the variable I, so simple. 519 00:22:54,746 --> 00:22:55,606 It's very reasonable. 520 00:22:55,606 --> 00:22:58,216 Calling a number a counter N, very reasonable. 521 00:22:58,506 --> 00:23:01,906 Calling a three-line -- four line functions variables A and B 522 00:23:01,906 --> 00:23:03,526 because that's really all they have -- 523 00:23:03,916 --> 00:23:05,436 the function doesn't have to do all that much. 524 00:23:05,786 --> 00:23:07,636 Realize sometime that it's completely legit 525 00:23:07,636 --> 00:23:09,606 to use very simply named variables. 526 00:23:09,996 --> 00:23:12,426 So in this case how do I swap A and B? 527 00:23:12,426 --> 00:23:15,766 Well if I've got A here and B here, I can't just move -- 528 00:23:15,766 --> 00:23:18,516 copy A to B. Because what happens 529 00:23:18,516 --> 00:23:21,796 if I just do B equals A semicolon. 530 00:23:22,886 --> 00:23:25,716 I lose B. I'm kind of doing too much at once. 531 00:23:25,956 --> 00:23:27,276 So what would you do in reality? 532 00:23:27,276 --> 00:23:29,926 Well you can't just move this here and then that here 533 00:23:29,926 --> 00:23:32,646 because of that problem so let me just move B 534 00:23:32,646 --> 00:23:35,036 into a temporary variable, a little place holder. 535 00:23:35,036 --> 00:23:37,116 Then I can clobber his value -- 536 00:23:37,116 --> 00:23:40,246 change his value by doing A against B 537 00:23:40,246 --> 00:23:42,546 and then finally I can restore B 538 00:23:42,946 --> 00:23:46,796 by using per this last line here that tenth variable. 539 00:23:47,066 --> 00:23:49,296 So there's a trade-off here, subtle though it is, 540 00:23:49,296 --> 00:23:54,476 interesting over time but notice that I'm using 32 extra bits 541 00:23:54,476 --> 00:23:56,426 to implement this particular function. 542 00:23:56,426 --> 00:23:59,006 I'm taking A and B, I'm swapping them but to make 543 00:23:59,006 --> 00:24:00,606 that happen I actually need 544 00:24:00,606 --> 00:24:02,366 in this implementation a bit more memory, 545 00:24:02,816 --> 00:24:04,056 but not such a big deal. 546 00:24:04,716 --> 00:24:06,316 But apparently it's still wrong. 547 00:24:07,016 --> 00:24:09,926 So why can you find logical fault with this. 548 00:24:09,926 --> 00:24:12,546 Or even if you're maybe among those more comfortable, 549 00:24:12,736 --> 00:24:14,236 you can express what the problem is. 550 00:24:14,236 --> 00:24:16,586 Why is swap not apparently swapping? 551 00:24:17,796 --> 00:24:17,936 Yeah. 552 00:24:17,936 --> 00:24:19,276 >> [Inaudible background question] 553 00:24:19,276 --> 00:24:20,596 >> So I'm not returning anything. 554 00:24:20,596 --> 00:24:21,836 So that's an interesting point. 555 00:24:21,836 --> 00:24:25,656 So swap is declared as having a return value of void, 556 00:24:25,956 --> 00:24:28,496 so that's interesting because it's certainly not printing 557 00:24:28,496 --> 00:24:30,456 anything and the last time we saw a function 558 00:24:30,456 --> 00:24:31,706 that didn't print anything, 559 00:24:31,976 --> 00:24:34,996 it really didn't do anything useful. 560 00:24:34,996 --> 00:24:36,316 It didn't have any side effects. 561 00:24:36,706 --> 00:24:38,606 So that could be the explanation here. 562 00:24:38,606 --> 00:24:40,286 So it's not returning anything. 563 00:24:40,286 --> 00:24:45,106 Certainly up here notice I'm not even assigning a variable. 564 00:24:45,326 --> 00:24:47,966 The return variable of swap because it's return variable -- 565 00:24:47,966 --> 00:24:49,166 it's return value is nothing. 566 00:24:49,256 --> 00:24:49,866 It's void. 567 00:24:50,216 --> 00:24:52,076 So we could fix this that way somehow. 568 00:24:52,076 --> 00:24:55,656 But what else might the problem be? 569 00:24:55,896 --> 00:24:55,976 Yeah. 570 00:24:56,426 --> 00:24:58,426 >> [Inaudible background response] 571 00:24:58,876 --> 00:25:00,936 >> Okay so that's an interesting gotcha too. 572 00:25:00,936 --> 00:25:04,686 I seem to be talking about X and Y up at the top here in main, 573 00:25:04,936 --> 00:25:07,306 but then all of a sudden I start talking about A and B, 574 00:25:07,306 --> 00:25:09,766 so clearly X and Y aren't getting swapped 575 00:25:09,766 --> 00:25:12,626 because I'm only swapping A and B. So there's a couple 576 00:25:12,626 --> 00:25:14,146 of interesting take-aways here. 577 00:25:14,406 --> 00:25:16,136 So that's true in this case 578 00:25:16,136 --> 00:25:19,986 and it's not fundamentally the explanation, 579 00:25:19,986 --> 00:25:21,166 but it's definitely related. 580 00:25:21,166 --> 00:25:24,406 So in this case, yes I have two variables in this context 581 00:25:24,406 --> 00:25:28,406 of main called X and Y. I go ahead and pass them 582 00:25:28,476 --> 00:25:31,936 in as what we'll start calling consistently arguments. 583 00:25:32,016 --> 00:25:34,686 So arguments are inputs to functions, otherwise known 584 00:25:34,686 --> 00:25:37,506 as parameters, slight difference in terminology depending 585 00:25:37,506 --> 00:25:39,136 on the context but for the most part they're synonyms. 586 00:25:39,636 --> 00:25:42,246 So a function can take arguments or inputs 587 00:25:42,526 --> 00:25:44,336 and that obviously influences behavior. 588 00:25:44,416 --> 00:25:46,886 Think back a week, printfF takes an argument. 589 00:25:46,976 --> 00:25:50,536 It takes a string and maybe multiple arguments separated 590 00:25:50,536 --> 00:25:51,966 by commas, the variables 591 00:25:51,966 --> 00:25:53,686 that then get inserted into that string. 592 00:25:53,686 --> 00:25:54,836 So we've seen this before, 593 00:25:55,106 --> 00:25:58,306 so inputs to functions are called arguments or parameters. 594 00:25:58,626 --> 00:26:01,626 Well it turns out the way these work is as follows. 595 00:26:01,896 --> 00:26:06,946 If I have a variable X here and variable Y here notice 596 00:26:06,986 --> 00:26:10,006 that in the function that I'm calling I need 597 00:26:10,006 --> 00:26:11,926 to give those inputs names. 598 00:26:12,006 --> 00:26:14,846 Now I could coincidently name them X and Y 599 00:26:15,106 --> 00:26:17,056 but that's not strictly the case right. 600 00:26:17,056 --> 00:26:20,276 Consider after all that you guys have no idea what the person 601 00:26:20,506 --> 00:26:23,336 who wrote printf called those variables. 602 00:26:23,336 --> 00:26:25,026 You've been passing in quote-unquotes 603 00:26:25,316 --> 00:26:26,516 and then a string inside of it 604 00:26:26,516 --> 00:26:30,566 and maybe comma X comma Y comma Z but hopefully that programmer 605 00:26:30,566 --> 00:26:33,436 who wrote that function twenty plus years ago doesn't have 606 00:26:33,496 --> 00:26:36,516 to know or care what your variables are called. 607 00:26:36,756 --> 00:26:39,956 He or she can use his or her own variable names. 608 00:26:39,956 --> 00:26:41,216 So that's in fact the case here. 609 00:26:41,216 --> 00:26:44,226 It is completely my prerogative to call these things A and B, 610 00:26:44,226 --> 00:26:46,536 Fu and bar, anything I want, 611 00:26:46,876 --> 00:26:49,676 because what happens you call a function in pass 612 00:26:49,676 --> 00:26:53,116 and inputs usually, a copy is made of the inputs. 613 00:26:53,446 --> 00:26:57,086 So yes I have 32 bits up here called X and 32 bits 614 00:26:57,086 --> 00:27:00,186 up here called Y. They're storing a value, 1 and 2. 615 00:27:00,366 --> 00:27:03,496 What happens when I call swap is that a copy of X 616 00:27:03,616 --> 00:27:05,546 and a copy of Y are passed in. 617 00:27:05,926 --> 00:27:08,256 And now in this scope so to speak, 618 00:27:08,256 --> 00:27:11,296 in this context we now have copies of X 619 00:27:11,296 --> 00:27:14,646 and Y. They're called A and B. There a different 32 bits for A, 620 00:27:14,646 --> 00:27:20,196 a different 32 bits for Y. Are these four lines swapping do you 621 00:27:20,196 --> 00:27:22,186 think logically, A and B? 622 00:27:22,446 --> 00:27:25,126 Like is the code at least correct? 623 00:27:25,436 --> 00:27:26,326 So yes right. 624 00:27:26,326 --> 00:27:28,356 There's no logical fault with doing this. 625 00:27:28,356 --> 00:27:29,596 I have a temporary variable. 626 00:27:29,786 --> 00:27:30,816 I keep A around. 627 00:27:30,816 --> 00:27:33,036 I clobber A with B's value. 628 00:27:33,036 --> 00:27:36,066 Then I update B's value with A's former value, 629 00:27:36,286 --> 00:27:38,786 which I tucked away and tapped so logically if you think this 630 00:27:38,786 --> 00:27:40,046 through and even kind of play 631 00:27:40,046 --> 00:27:42,106 around with a simulation on a piece of paper. 632 00:27:42,416 --> 00:27:42,976 That's correct. 633 00:27:43,406 --> 00:27:45,916 But at this point in the story the very last line 634 00:27:45,916 --> 00:27:48,726 of this program, what is A's value? 635 00:27:51,056 --> 00:27:54,226 So what is A's value where the arrow is there. 636 00:27:54,226 --> 00:27:57,716 So 2 and what is B's value, 1. 637 00:27:58,116 --> 00:27:59,896 But what are X and Y's value? 638 00:28:01,086 --> 00:28:02,236 They're still the opposite. 639 00:28:02,236 --> 00:28:03,516 They're still the original values 640 00:28:03,516 --> 00:28:05,136 because again these are copies. 641 00:28:05,416 --> 00:28:06,726 So the key take away here is 642 00:28:06,726 --> 00:28:10,096 that when you actually call a function you get a copy 643 00:28:10,096 --> 00:28:12,256 of the variables that are being passed in. 644 00:28:12,496 --> 00:28:14,706 Now we'll see in time that there's a solution to this. 645 00:28:14,966 --> 00:28:17,376 The fundamental problem here are that functions 646 00:28:17,376 --> 00:28:21,666 like swap have their own scope, their own context, 647 00:28:21,946 --> 00:28:23,656 their own dedicated memory. 648 00:28:23,866 --> 00:28:26,686 So yes you can pass inputs to a function like swap. 649 00:28:26,896 --> 00:28:29,256 And yes that function can manipulate those numbers, 650 00:28:29,256 --> 00:28:32,886 do some math on them, even print out things, all side effects 651 00:28:33,186 --> 00:28:34,976 but it does not have the capability 652 00:28:34,976 --> 00:28:39,586 to change the original variables values, at least not yet. 653 00:28:40,246 --> 00:28:43,856 So we can see this in one other context here. 654 00:28:43,856 --> 00:28:45,826 So let's take a look at this little snippet. 655 00:28:46,266 --> 00:28:49,476 So here's a function called increment as the name implies. 656 00:28:49,476 --> 00:28:50,596 It's purpose in life is just 657 00:28:50,596 --> 00:28:53,276 to implement a value called X. Well they claim 658 00:28:53,276 --> 00:28:55,656 that this identical in spirit to this same problem. 659 00:28:55,656 --> 00:28:59,106 Let me go ahead and open buggy 4 dot C. Scroll down. 660 00:28:59,446 --> 00:29:03,096 Notice that I'm even claiming a simpler program here. 661 00:29:03,136 --> 00:29:05,236 X gets 1 quote-unquote. 662 00:29:05,236 --> 00:29:08,806 X is now 1 presumably incrementing dot dot dot. 663 00:29:08,806 --> 00:29:11,756 I call increment and now I claim it's incremented 664 00:29:11,966 --> 00:29:15,136 and then okay X is now 2 hopefully. 665 00:29:15,136 --> 00:29:15,846 So let's do this. 666 00:29:15,846 --> 00:29:18,366 Make buggy 4 enter. 667 00:29:19,696 --> 00:29:21,716 Increment that's interesting. 668 00:29:22,016 --> 00:29:25,266 Error oh okay that was the bug. 669 00:29:25,326 --> 00:29:25,896 Not an error. 670 00:29:25,936 --> 00:29:26,616 That's intentional. 671 00:29:26,976 --> 00:29:27,956 So notice what happened. 672 00:29:27,956 --> 00:29:30,606 I'm going to do it more simply with GCC so GCC 673 00:29:30,856 --> 00:29:34,966 of buggy 4 dot C. In function increments error X 674 00:29:35,216 --> 00:29:36,186 is undeclared. 675 00:29:36,186 --> 00:29:37,836 But wait a minute, I'm pretty sure I had int X 676 00:29:38,446 --> 00:29:39,336 so what does this mean? 677 00:29:39,336 --> 00:29:41,486 If you haven't realized it already if you get errors 678 00:29:41,486 --> 00:29:43,696 from GCC they're cryptic a bit 679 00:29:43,936 --> 00:29:45,796 but they also have some juicy information; 680 00:29:45,796 --> 00:29:48,516 the name of the file that's got the problem colon, 681 00:29:48,596 --> 00:29:52,546 the line number that GCC at least thinks the problem is on 682 00:29:52,616 --> 00:29:56,366 or began on and then hopefully an instructive error message 683 00:29:56,366 --> 00:29:57,306 as in this case. 684 00:29:57,626 --> 00:30:00,436 And realize too especially if you're first experience 685 00:30:00,436 --> 00:30:03,456 in the science center or on your laptop with CS50 Pset is 686 00:30:03,456 --> 00:30:04,916 to get not three lines of errors 687 00:30:04,916 --> 00:30:06,606 but like this many lines of errors. 688 00:30:06,906 --> 00:30:09,306 Realize that early on a stupid 689 00:30:09,306 --> 00:30:12,016 or simple little problem just trigger a whole lot 690 00:30:12,016 --> 00:30:12,786 of confusion. 691 00:30:13,226 --> 00:30:15,436 So again if your first reaction to your problem set is, 692 00:30:15,436 --> 00:30:17,086 "Oh my God, I wrote ten lines of code 693 00:30:17,086 --> 00:30:21,206 and generated a hundred lines of errors" fix the top ones first 694 00:30:21,236 --> 00:30:23,286 because there's often a cascading effect. 695 00:30:23,286 --> 00:30:24,866 So don't pluck off the one you understand 696 00:30:24,866 --> 00:30:25,586 because it's in the middle. 697 00:30:25,896 --> 00:30:26,946 Start from the top down 698 00:30:26,946 --> 00:30:30,246 and you'll likely knock off multiple bugs at once. 699 00:30:30,246 --> 00:30:31,376 So let's look at line 40. 700 00:30:31,666 --> 00:30:35,276 So notice I'm using, well let me use nano here for a second, 701 00:30:35,276 --> 00:30:38,366 so buggy 4 dot C. Notice if I scroll down, 702 00:30:38,826 --> 00:30:41,196 notice that the line number is changing so I can see, 703 00:30:41,196 --> 00:30:43,566 "Ah hah" line forty of forty-two. 704 00:30:43,566 --> 00:30:46,346 So it's not a very long file, X plus plus. 705 00:30:46,866 --> 00:30:49,466 So hereto we see the fundamental problem of scope. 706 00:30:49,466 --> 00:30:53,526 Notice in the top of this program buggy 4 I declared X. 707 00:30:53,556 --> 00:30:55,016 It's X. That was good. 708 00:30:55,016 --> 00:30:56,926 And then I assigned it a value of 1. 709 00:30:57,156 --> 00:31:00,576 But now in increments, the increment is a function. 710 00:31:00,606 --> 00:31:04,226 Increments has its own scope, its own context, its own chunk 711 00:31:04,226 --> 00:31:07,876 of memory to play with and guess what, X was declared 712 00:31:08,026 --> 00:31:10,426 in someone else's scope, namely me. 713 00:31:10,696 --> 00:31:13,496 So you can't just start talking about X in the context 714 00:31:13,496 --> 00:31:15,716 of increment because who knows 715 00:31:16,186 --> 00:31:19,316 where increment is being stored in memory. 716 00:31:19,386 --> 00:31:23,666 Increment knows nothing about make other than what it's told 717 00:31:23,666 --> 00:31:26,696 and apparently I have told increment nothing 718 00:31:26,696 --> 00:31:28,456 because I didn't pass in any arguments. 719 00:31:28,786 --> 00:31:32,296 So this program fundamentally doesn't work because I'm trying 720 00:31:32,296 --> 00:31:33,346 to implement a variable 721 00:31:33,346 --> 00:31:35,466 that just doesn't exist in that scope. 722 00:31:36,216 --> 00:31:38,496 So what's a solution perhaps to this problem? 723 00:31:38,496 --> 00:31:40,836 How can I at least get this program to compile? 724 00:31:41,816 --> 00:31:44,536 What could I do? 725 00:31:44,766 --> 00:31:47,436 So I could declare X. So I could say something like, 726 00:31:47,436 --> 00:31:50,436 "Alright well the problem is that I didn't declare X." 727 00:31:50,606 --> 00:31:52,546 To declare a variable means to give it name 728 00:31:52,546 --> 00:31:53,276 and to give it a type. 729 00:31:53,596 --> 00:31:56,766 Well let's say this int X, alright does that fix it. 730 00:31:57,886 --> 00:31:57,986 >> [Group response] No. 731 00:31:57,986 --> 00:31:59,226 >> No obviously not. 732 00:31:59,226 --> 00:32:02,146 That does give me X but a problem in C 733 00:32:02,146 --> 00:32:05,346 in many languages is if you don't initialize a variable 734 00:32:05,626 --> 00:32:07,636 who knows what's inside of it right. 735 00:32:07,636 --> 00:32:09,636 People have been using this computer for you. 736 00:32:09,876 --> 00:32:12,086 You on your own computer have been checking email, 737 00:32:12,176 --> 00:32:13,786 sending -- checking websites. 738 00:32:13,786 --> 00:32:17,136 So a lot of your RAM is in use and who knows what 0's 739 00:32:17,186 --> 00:32:20,376 and 1's have littered your RAM at this point in the story. 740 00:32:20,616 --> 00:32:21,906 So when you just say int X, 741 00:32:22,306 --> 00:32:25,036 you're just telling GCC give me 32 bits of space. 742 00:32:25,436 --> 00:32:28,926 But if you don't tell GCC what to put there the compiler, 743 00:32:28,926 --> 00:32:29,966 the computer is pretty much going 744 00:32:29,966 --> 00:32:32,366 to hand you what is already there. 745 00:32:32,896 --> 00:32:34,196 Now that's a bit of a white lie 746 00:32:34,196 --> 00:32:36,456 because some operating systems do scrub the memory 747 00:32:36,496 --> 00:32:39,816 so if you would actually get back in some programs is all 0's 748 00:32:39,916 --> 00:32:41,756 and is better than just complete nonsense. 749 00:32:42,046 --> 00:32:43,226 But the rule of thumb for today 750 00:32:43,226 --> 00:32:46,856 and for a while realizes do not ever fail 751 00:32:46,856 --> 00:32:48,256 to initialize your variables. 752 00:32:48,316 --> 00:32:50,206 Do not assume that they will be 0 753 00:32:50,206 --> 00:32:51,886 because weird bad things will happen. 754 00:32:52,226 --> 00:32:54,896 So this does work in terms of compilation. 755 00:32:55,266 --> 00:32:56,616 Let me go ahead and save this. 756 00:32:56,696 --> 00:32:58,586 Save-modify buffer, yes, enter. 757 00:32:58,586 --> 00:32:59,846 Let me go ahead and rerun GCC. 758 00:32:59,846 --> 00:33:03,416 That made GCC happy, but it didn't make the logic 759 00:33:03,416 --> 00:33:06,136 of my program happy because I'm incrementing -- 760 00:33:06,596 --> 00:33:08,736 now I'm A dot out. 761 00:33:09,066 --> 00:33:14,196 No I'm incrementing some other variable that yes its 32 bits 762 00:33:14,196 --> 00:33:15,786 but it's a different 32 bits. 763 00:33:15,876 --> 00:33:18,966 And again I haven't really solved the problem. 764 00:33:19,216 --> 00:33:21,816 So what then is another solution to this problem, 765 00:33:21,816 --> 00:33:24,066 like how could I go about fixing this problem 766 00:33:24,386 --> 00:33:28,066 in buggy 4 dot c correctly, quote-unquote? 767 00:33:28,066 --> 00:33:29,176 >> [Inaudible background discussion] 768 00:33:29,176 --> 00:33:32,966 >> Yeah so what if the problem -- 769 00:33:32,966 --> 00:33:35,846 so the short answer is what about global variable. 770 00:33:35,846 --> 00:33:37,326 No something we've really touched on yet, 771 00:33:37,326 --> 00:33:38,406 but it's a simple idea. 772 00:33:38,696 --> 00:33:42,236 If the fundamental problem is that X does not exist 773 00:33:42,316 --> 00:33:43,596 in the scope of increment. 774 00:33:43,596 --> 00:33:45,646 It only exists in the scope of main. 775 00:33:45,926 --> 00:33:48,746 Well is there some other context, some other scope 776 00:33:48,746 --> 00:33:50,476 that all functions can see. 777 00:33:50,696 --> 00:33:53,396 I feel like intuitively I could get away 778 00:33:53,396 --> 00:33:54,606 with some trick like this. 779 00:33:54,606 --> 00:33:56,466 You know what, let me say X gets one there, 780 00:33:56,466 --> 00:33:59,646 but you know what let me try declaring X outside 781 00:33:59,646 --> 00:34:03,726 of every function because if you think of it now as having -- 782 00:34:04,116 --> 00:34:07,006 being viewable by the entire file this kind of makes sense. 783 00:34:07,006 --> 00:34:09,466 Now X is in scope for everyone 784 00:34:09,706 --> 00:34:11,606 because it wasn't declared inside 785 00:34:11,606 --> 00:34:12,886 of any particular function. 786 00:34:12,886 --> 00:34:13,926 So let's see if this works. 787 00:34:13,926 --> 00:34:14,706 Let me save this. 788 00:34:14,706 --> 00:34:20,526 Let me run GCC on buggy 4 dot c. Okay compile, so that seems 789 00:34:20,526 --> 00:34:21,486 to have been progress. 790 00:34:21,816 --> 00:34:23,476 No let me go ahead and run A dot out. 791 00:34:24,336 --> 00:34:26,936 Viola, so that did in fact fix the problem. 792 00:34:26,936 --> 00:34:30,586 So yes global variables are a possible solution 793 00:34:30,586 --> 00:34:33,866 to this problem but now what we just handed you as a solution, 794 00:34:33,866 --> 00:34:34,756 we kind of need to take back. 795 00:34:35,156 --> 00:34:37,696 In general using global variables, bad. 796 00:34:38,506 --> 00:34:40,756 That's sort of the montra within programming 797 00:34:40,756 --> 00:34:44,136 because global variables very quickly become a mess. 798 00:34:44,446 --> 00:34:46,596 It becomes unclear when you start writing programs 799 00:34:46,626 --> 00:34:48,886 that are this long, but this long and spread 800 00:34:48,886 --> 00:34:50,096 across multiple files. 801 00:34:50,096 --> 00:34:51,446 It's not clear where they were defined. 802 00:34:51,716 --> 00:34:54,456 Realize that if things get more sophisticated we'll introduce 803 00:34:54,456 --> 00:34:57,476 better ways of solving this problem for scope. 804 00:34:57,576 --> 00:35:00,376 But in this context for such a simple problem 805 00:35:00,376 --> 00:35:02,326 of incrementing a counter, which frankly seems 806 00:35:02,356 --> 00:35:06,856 to have little real world value as of it, 807 00:35:06,916 --> 00:35:09,406 this is an adequate solution to this problem. 808 00:35:09,496 --> 00:35:12,336 So for now, the tool in ones tool kit you have this ability 809 00:35:12,336 --> 00:35:13,476 to use global variables 810 00:35:13,696 --> 00:35:16,506 but clearly per the previous examples do we have the ability 811 00:35:16,506 --> 00:35:19,276 to pass inputs and get outputs 812 00:35:19,716 --> 00:35:22,296 so in general you should take the latter approach. 813 00:35:22,586 --> 00:35:25,706 So I promised to explain what this is here. 814 00:35:26,016 --> 00:35:29,336 So this was buggy -- actually let's go back to buggy 4 815 00:35:29,336 --> 00:35:30,506 since we're already on it. 816 00:35:30,876 --> 00:35:34,636 So this is buggy 4 and notice that it to at the very top 817 00:35:34,636 --> 00:35:38,266 of the page has this, which looks like a function 818 00:35:38,476 --> 00:35:41,296 but it's missing what, minimally? 819 00:35:43,206 --> 00:35:44,726 There's none of those curly braces. 820 00:35:44,726 --> 00:35:46,046 Every time we've declared a function 821 00:35:46,046 --> 00:35:48,706 so far we have open brace and then some code right. 822 00:35:48,706 --> 00:35:49,776 It's missing code. 823 00:35:50,026 --> 00:35:51,646 But there seems to be this coincident, 824 00:35:51,746 --> 00:35:54,806 a void increment open paren clothes paren semicolon. 825 00:35:55,056 --> 00:35:58,376 That looks awfully like this thing here except I got rid 826 00:35:58,376 --> 00:35:58,886 of the code. 827 00:35:58,886 --> 00:36:01,546 I got rid of the curly braces and I slapped on a semicolon 828 00:36:01,776 --> 00:36:03,886 and also got rid of the line break between void. 829 00:36:04,196 --> 00:36:06,346 Incidentally you'll see different conventions. 830 00:36:06,346 --> 00:36:07,676 This is a matter of style. 831 00:36:07,926 --> 00:36:10,076 A lot of books, a lot of people will write code 832 00:36:10,076 --> 00:36:11,186 like this on one line. 833 00:36:11,486 --> 00:36:14,456 Personally I actually tend to put the return type on one line 834 00:36:14,456 --> 00:36:15,906 and the name of the function on the other 835 00:36:16,196 --> 00:36:18,826 because with sophisticated editor, text editors, 836 00:36:18,886 --> 00:36:21,916 word processors you can actually search your code 837 00:36:22,126 --> 00:36:24,676 and say you know what, I know I want the function called 838 00:36:24,746 --> 00:36:26,416 increment but I want to find it 839 00:36:26,416 --> 00:36:28,576 when it's the left most character on the screen 840 00:36:28,576 --> 00:36:30,546 because then I can find where I implemented it, 841 00:36:30,776 --> 00:36:31,766 not where I called it. 842 00:36:32,016 --> 00:36:34,046 So there's little tricks like this you pick up over time. 843 00:36:34,116 --> 00:36:35,416 But it's just a matter of style. 844 00:36:35,746 --> 00:36:38,816 So why do I have this seemingly redundant mention 845 00:36:38,816 --> 00:36:39,936 of increments up here? 846 00:36:40,136 --> 00:36:41,646 Well my program is working, right. 847 00:36:41,646 --> 00:36:43,166 We've mixed it with the global barrier. 848 00:36:43,166 --> 00:36:45,306 Let me go ahead and delete this for now, 849 00:36:45,846 --> 00:36:47,646 the seemingly superfluous line. 850 00:36:47,916 --> 00:36:53,216 Let me go ahead and run GCC of buggy 4 dot C enter, huh? 851 00:36:53,636 --> 00:36:54,866 Let me redo that. 852 00:36:54,866 --> 00:36:56,256 You know maybe it's not my fault. 853 00:36:56,256 --> 00:36:57,236 Maybe the computer screwed up. 854 00:36:57,946 --> 00:37:01,486 Hmh, probably not the computer's fault, 855 00:37:01,596 --> 00:37:03,436 so I'm doing something wrong, what is it? 856 00:37:03,686 --> 00:37:06,506 Fortunately again GCC is being a little helpful but it looks 857 00:37:06,506 --> 00:37:11,086 like I made three mistakes, line 23, line 38, okay line 23 gain, 858 00:37:11,266 --> 00:37:12,446 so that's two mistakes. 859 00:37:12,836 --> 00:37:14,186 But what's the warning saying? 860 00:37:14,496 --> 00:37:17,396 Implicit declaration of function increment. 861 00:37:17,926 --> 00:37:22,606 So the short explanation of this is that C is kind of stupid. 862 00:37:22,606 --> 00:37:25,556 It's an old language, so it's a very powerful language 863 00:37:25,806 --> 00:37:29,656 but when efficiency was more important then user friendliness 864 00:37:30,506 --> 00:37:32,336 certain decisions were made. 865 00:37:32,336 --> 00:37:34,716 For instance the decision was made 866 00:37:34,716 --> 00:37:38,556 that you cannot call a function unless you've already declared 867 00:37:38,556 --> 00:37:41,826 it earlier in the file than your actual calling it. 868 00:37:42,356 --> 00:37:43,216 So what does that mean? 869 00:37:43,456 --> 00:37:45,776 It means if I want this line here 870 00:37:45,776 --> 00:37:47,636 to call the function called increment, 871 00:37:47,876 --> 00:37:51,676 well it had better have been defined somewhere previously 872 00:37:51,676 --> 00:37:52,986 in that actual file. 873 00:37:53,356 --> 00:37:55,866 Alright so there's clearly a fix for this. 874 00:37:56,076 --> 00:37:57,206 Let's see let me just go ahead 875 00:37:57,206 --> 00:37:59,636 and cut these lines using little text editor tricks 876 00:38:00,126 --> 00:38:01,746 that you can just copy, paste and nano 877 00:38:01,746 --> 00:38:02,756 in different ways or just retype. 878 00:38:02,756 --> 00:38:05,086 Let me just go ahead and paste it here. 879 00:38:05,356 --> 00:38:08,316 Alright so the problem is if the increment was defined, 880 00:38:08,496 --> 00:38:10,086 was implemented after main, 881 00:38:10,416 --> 00:38:12,676 well an easy fix is just define it before main. 882 00:38:12,676 --> 00:38:13,846 So let me go ahead and recompile. 883 00:38:13,936 --> 00:38:17,996 Let me go ahead and rerun GCC and that seems 884 00:38:17,996 --> 00:38:21,386 to have fixed it A dot out enter seems to have worked. 885 00:38:21,726 --> 00:38:25,516 So does this mean the simple solution is just always put your 886 00:38:25,556 --> 00:38:26,686 functions above main. 887 00:38:27,026 --> 00:38:29,136 So maybe but there's some push backs here. 888 00:38:29,136 --> 00:38:32,756 So one when you open the file the program ultimately runs 889 00:38:32,756 --> 00:38:33,436 main first. 890 00:38:33,696 --> 00:38:37,406 I as the reader prefer to see what the program does first. 891 00:38:37,406 --> 00:38:38,586 I don't want see increments. 892 00:38:38,586 --> 00:38:40,386 I don't want to see other random functions. 893 00:38:40,386 --> 00:38:42,196 I want to see the main function first. 894 00:38:42,426 --> 00:38:45,626 So just in terms of friendliness and readability it feels 895 00:38:45,626 --> 00:38:47,926 like a good practice to put main first. 896 00:38:48,226 --> 00:38:50,666 But then you clearly run into these problems. 897 00:38:50,666 --> 00:38:52,596 And if you really think this through you'll run 898 00:38:52,596 --> 00:38:56,136 into scenarios where one functions call another 899 00:38:56,136 --> 00:38:57,706 and then he might call that again. 900 00:38:58,016 --> 00:39:00,646 You run into a problem where this guy has to go here 901 00:39:00,886 --> 00:39:03,536 and this guy has to here, like you'll run 902 00:39:03,536 --> 00:39:06,136 into this endless loop where you're in this catch 22. 903 00:39:06,226 --> 00:39:08,666 Everyone can't go in front of everyone else. 904 00:39:09,126 --> 00:39:10,966 So there is a solution to this problem. 905 00:39:11,176 --> 00:39:13,446 Not only can you define a function, 906 00:39:13,766 --> 00:39:16,486 to define a function is the very precise terminology 907 00:39:16,486 --> 00:39:18,726 for implementing the function without actual code. 908 00:39:19,116 --> 00:39:23,156 You can also declare the function by simply stating 909 00:39:23,156 --> 00:39:26,366 at the very top of the file what the return type is, 910 00:39:26,696 --> 00:39:28,176 what the name of the function is, 911 00:39:28,366 --> 00:39:31,006 what its arguments if any are semicolon. 912 00:39:31,336 --> 00:39:33,516 So at the top of most any program you write 913 00:39:33,516 --> 00:39:36,626 that has multiple functions within you just have to get 914 00:39:36,626 --> 00:39:38,626 into the habit of declaring your functions 915 00:39:38,886 --> 00:39:40,346 at the top of the file right. 916 00:39:40,346 --> 00:39:41,546 It's kind of nuisance job. 917 00:39:41,546 --> 00:39:43,026 It does not have this downside. 918 00:39:43,026 --> 00:39:44,616 PHP does not have this downside. 919 00:39:44,826 --> 00:39:46,336 It's sort of an artifact of the fact 920 00:39:46,526 --> 00:39:50,236 that when this language was really first in use it took time 921 00:39:50,296 --> 00:39:53,026 for the compiler to read the whole file from top 922 00:39:53,056 --> 00:39:54,106 to bottom, left to right. 923 00:39:54,416 --> 00:39:56,746 So it's not going to waste cycles and waste human time 924 00:39:56,746 --> 00:39:58,476 by reading it once just to figure 925 00:39:58,476 --> 00:40:00,666 out what functions you wrote and then go back 926 00:40:01,346 --> 00:40:03,326 with that knowledge and start compiling. 927 00:40:03,476 --> 00:40:05,016 So it's that simple reason there. 928 00:40:05,016 --> 00:40:08,896 So it's minorly annoying these days but very simply solved. 929 00:40:08,896 --> 00:40:11,776 Well let's look at one last buggy example. 930 00:40:12,356 --> 00:40:16,566 So here we have a very similar variant. 931 00:40:16,766 --> 00:40:20,106 So what I initially tried to do, this program compiled 932 00:40:20,396 --> 00:40:23,406 and this program has a global variable. 933 00:40:23,546 --> 00:40:24,526 Look at the top of the file, 934 00:40:24,566 --> 00:40:27,126 int X. Here's my function declaration. 935 00:40:27,366 --> 00:40:28,046 Here's main. 936 00:40:28,286 --> 00:40:30,126 Here I initialize X to 1. 937 00:40:30,126 --> 00:40:31,146 What do I do down here? 938 00:40:32,276 --> 00:40:35,166 You see already why this program is not in fact going 939 00:40:35,166 --> 00:40:40,256 to increment and then print the value of 2. 940 00:40:40,496 --> 00:40:47,426 So I didn't declare it as an int in main but that's okay 941 00:40:47,426 --> 00:40:50,546 because is there an X in scope, at that point 942 00:40:50,546 --> 00:40:52,406 in the story where my pointer is. 943 00:40:53,516 --> 00:40:57,316 There is as I declared a global variable called X up here. 944 00:40:57,586 --> 00:40:59,826 So this variable is in scope everywhere 945 00:40:59,826 --> 00:41:01,706 which means it's certainly in scope here, 946 00:41:01,906 --> 00:41:02,696 which means at this point 947 00:41:02,696 --> 00:41:05,866 in the story I am successfully initializing X to 1. 948 00:41:06,466 --> 00:41:08,546 But notice what happens if I run this thing. 949 00:41:08,546 --> 00:41:12,376 So this is buggy 5 -- GCC of buggy 5 dot C. And we go ahead 950 00:41:12,376 --> 00:41:14,996 and run A dot outs here, A dot out. 951 00:41:15,936 --> 00:41:18,246 Apparently the code is broken, it didn't compile 952 00:41:18,246 --> 00:41:20,306 but the code is printing one both times, 953 00:41:20,646 --> 00:41:26,016 what is happening do you think given these concepts of today? 954 00:41:26,166 --> 00:41:26,366 Yeah. 955 00:41:26,366 --> 00:41:26,433 >> [Inaudible background discussion] 956 00:41:26,433 --> 00:41:27,556 >> Yes, I declared X twice. 957 00:41:27,846 --> 00:41:31,546 And because I declared X twice in a more recent context, 958 00:41:31,966 --> 00:41:34,486 one that's actually enclosed in these curly braces, 959 00:41:34,716 --> 00:41:37,276 this is the declaration of X in the bottom 960 00:41:37,276 --> 00:41:39,676 of my file is quote-unquote shadowing 961 00:41:39,916 --> 00:41:43,476 or shadowing the original declaration of X. 962 00:41:43,586 --> 00:41:44,896 So this is completely fine. 963 00:41:44,966 --> 00:41:47,996 You are welcome as a programmer to use the same variable name 964 00:41:47,996 --> 00:41:51,026 in multiple context but if you pick the same name, 965 00:41:51,026 --> 00:41:54,446 that's all in context, still in scope from elsewhere. 966 00:41:54,756 --> 00:41:57,576 You're going to shadow it and end up incrementing X, 967 00:41:57,576 --> 00:42:03,766 yes but what is the value of X at this line the file, 11. 968 00:42:03,986 --> 00:42:07,866 But it's this copy of X, 32 bits that was declared inside 969 00:42:07,866 --> 00:42:10,776 of increments, not the 32 bits that was declared 970 00:42:10,776 --> 00:42:13,926 at the very top of this file by way of a global variable. 971 00:42:13,926 --> 00:42:16,786 So again another subtlety but this is actually a good thing 972 00:42:16,786 --> 00:42:19,626 because it means that C is very -- it keeps things very tight. 973 00:42:19,866 --> 00:42:22,336 If you declare a variable in a function, that's the one 974 00:42:22,606 --> 00:42:24,396 that you actually have access to. 975 00:42:24,666 --> 00:42:26,576 But there's actually an interesting explanation 976 00:42:26,626 --> 00:42:27,196 for all of this. 977 00:42:27,196 --> 00:42:29,166 And this is just a teaser because we'll start 978 00:42:29,166 --> 00:42:31,056 to peel back these layers in future weeks 979 00:42:31,056 --> 00:42:32,526 as to what's really going on. 980 00:42:33,056 --> 00:42:36,146 But for today's purposes, long story short, your computer -- 981 00:42:36,196 --> 00:42:39,936 nice.fas.harbor.edu it has a bunch of ram, 982 00:42:40,216 --> 00:42:42,206 probably like a gig of ram to gigs, 983 00:42:42,286 --> 00:42:44,816 four gigs whatever it is these days, and if you think 984 00:42:44,816 --> 00:42:47,496 of your RAM, I mean physically they're these little 985 00:42:47,646 --> 00:42:49,036 silicone boards. 986 00:42:49,036 --> 00:42:51,386 They're these little chips so if you think of them literally 987 00:42:51,386 --> 00:42:54,736 as a rectangle and inside of that rectangle are a whole bunch 988 00:42:54,736 --> 00:42:57,896 of bytes, billions of bytes, you have gigabytes of RAM. 989 00:42:58,236 --> 00:42:59,096 Well, what happens 990 00:42:59,096 --> 00:43:03,516 when a program runs its main function first take some 991 00:43:03,516 --> 00:43:05,976 of the bytes from the very bottom of your bank 992 00:43:05,976 --> 00:43:07,806 of RAM, so visualized here. 993 00:43:07,806 --> 00:43:10,226 So main's parameters, any inputs to main 994 00:43:10,226 --> 00:43:11,456 that we'll start using today, 995 00:43:11,776 --> 00:43:13,446 end up at the bottom of your RAM. 996 00:43:13,446 --> 00:43:15,396 Again if you think of your RAM, you're memory 997 00:43:15,606 --> 00:43:16,836 as just some big rectangle, 998 00:43:17,156 --> 00:43:20,806 so when main is actually called main can have equal variables, 999 00:43:20,806 --> 00:43:24,186 variables like X. Variables like those we've seen in examples, 1000 00:43:24,446 --> 00:43:26,556 those yet put next into your RAM. 1001 00:43:26,786 --> 00:43:30,136 So, from the bottom on up meanwhile if main happens 1002 00:43:30,186 --> 00:43:34,406 to call a function called Fu, well if Fu takes some arguments, 1003 00:43:34,406 --> 00:43:36,216 if Fu takes some parameters like A 1004 00:43:36,266 --> 00:43:39,516 and B those parameters are stored next 1005 00:43:40,046 --> 00:43:44,046 in your computer's ram, so above conceptually main's memory. 1006 00:43:44,276 --> 00:43:46,786 Meanwhile if FU itself has some local variables, 1007 00:43:47,586 --> 00:43:51,756 TMP that 32 bit integer, those meanwhile get put next 1008 00:43:51,756 --> 00:43:53,126 into your computer's memory. 1009 00:43:53,446 --> 00:43:57,566 So the point of this story is that when we talk about scope 1010 00:43:57,566 --> 00:44:00,376 or we talk about context, what we're really talking 1011 00:44:00,376 --> 00:44:04,156 about are conceptually these different chunks of ram 1012 00:44:04,406 --> 00:44:08,946 that are being allocated from your program's start to finish 1013 00:44:09,186 --> 00:44:10,366 as it's actually running. 1014 00:44:11,486 --> 00:44:14,116 Fu has it's own layer there, it's own frame 1015 00:44:14,156 --> 00:44:15,186 as we'll start calling it. 1016 00:44:15,486 --> 00:44:16,946 That's distinct from main. 1017 00:44:17,126 --> 00:44:20,876 That means that main does not have access to Fu's chunk 1018 00:44:20,876 --> 00:44:23,776 of memory because just look at it, it's in a different place 1019 00:44:24,186 --> 00:44:25,516 in the computer's memory. 1020 00:44:25,786 --> 00:44:28,846 And things called global memory are actually stored somewhere 1021 00:44:28,846 --> 00:44:29,756 else altogether. 1022 00:44:30,016 --> 00:44:33,056 This is a more complete picture of the story we're beginning 1023 00:44:33,056 --> 00:44:35,096 to tell and will continue to tell over time. 1024 00:44:35,346 --> 00:44:38,756 Our global variables are actually stored way at the top 1025 00:44:38,756 --> 00:44:40,656 of your computer's memory along 1026 00:44:40,656 --> 00:44:42,056 with what's called the text segment. 1027 00:44:42,646 --> 00:44:44,846 So all those 0's and 1's that you see in C outputs, 1028 00:44:45,106 --> 00:44:47,016 when you run a program they've got to be loaded 1029 00:44:47,016 --> 00:44:48,706 into RAM off of your hard drive. 1030 00:44:49,006 --> 00:44:51,546 They too end up at the top of the rectangle 1031 00:44:51,896 --> 00:44:53,946 that is your chunk of memory. 1032 00:44:55,336 --> 00:44:55,926 Any questions? 1033 00:44:56,656 --> 00:44:58,376 Okay that was a lot. 1034 00:44:58,376 --> 00:45:01,626 Let's take our five minute break a bit early today. 1035 00:45:04,666 --> 00:45:06,556 [Sound effects] Alright so we are back. 1036 00:45:06,656 --> 00:45:09,726 Let's solve this problem in one way to just to equip you 1037 00:45:09,726 --> 00:45:11,106 for one possible approach. 1038 00:45:11,106 --> 00:45:14,606 It was actually proposed already here in the crowd, 1039 00:45:14,606 --> 00:45:16,196 was to use a return value. 1040 00:45:16,446 --> 00:45:18,606 So again the problem was that we're running into problems 1041 00:45:18,606 --> 00:45:21,026 of scope that somehow relates to memory management, 1042 00:45:21,026 --> 00:45:22,406 which is a topic we'll revisit. 1043 00:45:22,406 --> 00:45:26,066 Well one approach to this problem of scope would be yes, 1044 00:45:26,626 --> 00:45:30,216 don't just have a local variable whose value gets incremented 1045 00:45:30,216 --> 00:45:31,506 and then do nothing with the result. 1046 00:45:31,506 --> 00:45:33,646 Return the result of your hard work, 1047 00:45:33,646 --> 00:45:35,786 return the result of those mathematics. 1048 00:45:36,166 --> 00:45:37,796 But for this to work in the context 1049 00:45:37,796 --> 00:45:40,546 of incrementation we're going to have to make a change. 1050 00:45:40,546 --> 00:45:44,206 If I want to increment X maybe I should actually provide X 1051 00:45:44,206 --> 00:45:46,536 as an input to the function so that 1052 00:45:46,536 --> 00:45:48,056 that function has access to it. 1053 00:45:48,156 --> 00:45:50,636 And you know what don't just let that function increment X 1054 00:45:51,146 --> 00:45:53,746 or A if that's what the name is that it wants to give it, 1055 00:45:54,016 --> 00:45:55,436 go ahead and actually have 1056 00:45:55,436 --> 00:45:58,266 that function turn the result of its mathematics. 1057 00:45:58,506 --> 00:46:00,166 But then the burden is back on me, 1058 00:46:00,166 --> 00:46:02,656 main to actually keep it around. 1059 00:46:03,086 --> 00:46:06,956 So a relatively simple logical fix to that particular problem, 1060 00:46:07,246 --> 00:46:10,556 but let's see what else, how else this can expressed. 1061 00:46:10,766 --> 00:46:13,886 So this now is slightly more compelling right. 1062 00:46:13,886 --> 00:46:14,746 We already have a way 1063 00:46:14,776 --> 00:46:17,276 of incrementing values, something plus plus. 1064 00:46:17,556 --> 00:46:19,156 So that's not that interesting 1065 00:46:19,156 --> 00:46:21,806 but you already have been inclined some of you to turn 1066 00:46:21,806 --> 00:46:23,656 to the math library for rounding, 1067 00:46:23,656 --> 00:46:25,766 so there's certainly little bite-sized problems 1068 00:46:26,356 --> 00:46:28,066 that would be really nice to factor out 1069 00:46:28,226 --> 00:46:29,566 and put inside their own method -- 1070 00:46:29,786 --> 00:46:31,056 and put inside their own function. 1071 00:46:31,286 --> 00:46:33,146 Maybe another such example similar 1072 00:46:33,246 --> 00:46:35,606 to round is cube function. 1073 00:46:35,606 --> 00:46:38,156 If you want to take a number and raise it to the power 1074 00:46:38,156 --> 00:46:41,706 of 3 you know yes you could X times X times X all 1075 00:46:41,706 --> 00:46:44,826 over the place, but it feels a little cleaner, or should start 1076 00:46:44,826 --> 00:46:47,366 to feel a little cleaner to actually outsource 1077 00:46:47,396 --> 00:46:49,226 that to a dedicated function 1078 00:46:49,476 --> 00:46:52,116 like a function called Q. Define it 1079 00:46:52,116 --> 00:46:56,936 as taking one argument called X. The name I give this argument 1080 00:46:56,936 --> 00:46:58,536 does not matter and because this is 1081 00:46:58,576 --> 00:47:00,286 such a short function I'm really not going 1082 00:47:00,286 --> 00:47:02,576 to give it an interesting name, it's really not justified, 1083 00:47:02,926 --> 00:47:05,256 so return A times A times A. 1084 00:47:05,746 --> 00:47:08,896 So it's a slightly more useful function here. 1085 00:47:08,896 --> 00:47:11,776 It does in fact solve the problem because now at the top, 1086 00:47:12,056 --> 00:47:14,676 I'll pass in X as an argument. 1087 00:47:14,976 --> 00:47:18,206 It does the math and returns the result and then I keep X 1088 00:47:18,206 --> 00:47:22,036 around as a return value therefrom. 1089 00:47:22,876 --> 00:47:27,616 Alright so enough on scope for a moment; let's actually segway 1090 00:47:27,656 --> 00:47:29,956 to a topic that ultimately will be related. 1091 00:47:29,956 --> 00:47:32,136 Because it boils down to manipulation of memory, 1092 00:47:32,526 --> 00:47:35,336 but suppose I want to do something like this. 1093 00:47:35,416 --> 00:47:38,086 If I want to write a program called grades dot C 1094 00:47:38,086 --> 00:47:40,616 and this thing has to somehow manipulate grades for a course. 1095 00:47:40,616 --> 00:47:44,996 I'm going to do something like into main, into RXC, Charstar, 1096 00:47:44,996 --> 00:47:48,866 argv and now if I want to right a program 1097 00:47:48,866 --> 00:47:51,366 that manages all the grades I get in a semester. 1098 00:47:51,626 --> 00:47:53,156 Well I could do something like this 1099 00:47:53,156 --> 00:47:55,466 and for simplicity we'll say grades are numbered 1100 00:47:55,466 --> 00:47:56,876 from 0 to 100. 1101 00:47:57,146 --> 00:47:59,726 Alright, so my grade for course 1 is going 1102 00:47:59,726 --> 00:48:01,806 to be an int, course 1. 1103 00:48:02,126 --> 00:48:04,596 My grade for course 2 is going to be int. 1104 00:48:04,936 --> 00:48:08,426 Int course 3, int course 4. 1105 00:48:08,426 --> 00:48:10,056 Okay so now I need to put values in these 1106 00:48:10,056 --> 00:48:12,396 so I'm writing a program so that I sort of geek 1107 00:48:12,396 --> 00:48:15,766 in my dorm room can do things later like course 1 -- 1108 00:48:15,766 --> 00:48:17,896 I can do something like this. 1109 00:48:18,416 --> 00:48:23,336 Hello me, what did you get in course 1, 1110 00:48:23,336 --> 00:48:25,616 question mark closed parentheses. 1111 00:48:25,616 --> 00:48:28,106 And now I'm going to do something like course 1 gets, 1112 00:48:28,366 --> 00:48:29,416 what would you propose? 1113 00:48:30,736 --> 00:48:33,756 Yeah so gets int for instance from the CS50 library 1114 00:48:33,856 --> 00:48:36,386 and immediately begs the question well how do I make 1115 00:48:36,386 --> 00:48:36,926 that happen. 1116 00:48:36,926 --> 00:48:38,786 Well I need standard IO for printf. 1117 00:48:39,296 --> 00:48:44,206 I need the CS50 library for get int and then okay I got 1118 00:48:44,206 --> 00:48:46,776 that one course, now let me do a copy paste. 1119 00:48:47,326 --> 00:48:49,286 So I'm just going to past this three times. 1120 00:48:49,626 --> 00:48:51,676 Incidentally these little keyboard tricks I'm doing, 1121 00:48:51,816 --> 00:48:54,016 if you again go to the course website resources, 1122 00:48:54,296 --> 00:48:57,176 there's little instruction sheets for nano for VIM 1123 00:48:57,176 --> 00:48:59,456 and also Emax which is another one. 1124 00:48:59,746 --> 00:49:02,156 So I know how to change things around. 1125 00:49:02,156 --> 00:49:05,886 I'm just going to change this to a 4 a 3 okay, so this will work. 1126 00:49:05,886 --> 00:49:07,326 Okay so this program works. 1127 00:49:07,326 --> 00:49:08,766 So arguably it's pretty clean. 1128 00:49:08,766 --> 00:49:09,636 It's pretty simple. 1129 00:49:09,926 --> 00:49:12,406 It's hard to find a huge amount of fault with this, 1130 00:49:12,786 --> 00:49:14,716 but I only take four courses this semester. 1131 00:49:14,716 --> 00:49:16,366 Next semester I'm going to another four, 1132 00:49:16,366 --> 00:49:18,016 and after that another four and another four, 1133 00:49:18,116 --> 00:49:20,116 32 or more maybe in the end. 1134 00:49:20,536 --> 00:49:24,026 Okay I can get a little creative here, 1, 2, 3, 4, 5, 6, 7, 8, 1135 00:49:24,026 --> 00:49:25,916 9, 10, 11 lines of code. 1136 00:49:25,916 --> 00:49:28,566 Okay so I'm going to get 12 so I get the white space. 1137 00:49:28,566 --> 00:49:29,566 Okay so here's fall. 1138 00:49:29,766 --> 00:49:30,516 There's spring. 1139 00:49:30,826 --> 00:49:33,966 Sophomore year, sophomore spring, junior fall, 1140 00:49:33,966 --> 00:49:36,696 junior spring, senior fall, senior spring, good. 1141 00:49:37,066 --> 00:49:38,726 Now my program's coming along. 1142 00:49:38,726 --> 00:49:39,176 [Background sound effects] 1143 00:49:39,176 --> 00:49:42,356 At this point this should be rubbing you the wrong way, 1144 00:49:42,416 --> 00:49:46,216 just like some of your larger scratch strips, probably started 1145 00:49:46,216 --> 00:49:48,966 to get out of hand after awhile, especially if you're copying 1146 00:49:48,966 --> 00:49:51,746 or duplicating strips or nesting things a lot. 1147 00:49:52,046 --> 00:49:55,196 So even if one has not taken a course in computer science, 1148 00:49:55,456 --> 00:49:58,796 hopefully the act of programming is not as tedious or naive 1149 00:49:58,796 --> 00:50:00,826 as just copying and pasting like I've been doing 1150 00:50:00,826 --> 00:50:02,146 in Microsoft word for years. 1151 00:50:02,366 --> 00:50:03,666 So there's clearly an opportunity 1152 00:50:03,666 --> 00:50:07,026 for a construct here called a loop, right. 1153 00:50:07,026 --> 00:50:10,656 A 4 loop, a Y loop but there's kind of a problem at least 1154 00:50:10,656 --> 00:50:12,646 for those who don't have any programming background. 1155 00:50:12,986 --> 00:50:16,506 Even if I have a loop, I kind of still need 32 1156 00:50:16,506 --> 00:50:17,796 of these things, right. 1157 00:50:17,796 --> 00:50:21,366 So I need a lot of these and thus far we've not seen a way 1158 00:50:21,366 --> 00:50:23,976 where you can create a new variable inside a loop 1159 00:50:24,126 --> 00:50:25,416 but create a new copy of it. 1160 00:50:25,416 --> 00:50:28,076 We've declared variable I inside of a loop, 1161 00:50:28,076 --> 00:50:31,296 but we don't get a new copy of I for every iteration. 1162 00:50:31,526 --> 00:50:33,846 Usually we're incrementing the same copy of I. 1163 00:50:34,126 --> 00:50:35,996 So we need a solution to this problem. 1164 00:50:35,996 --> 00:50:39,446 I need a way of representing 32 ins and I'd really 1165 00:50:39,446 --> 00:50:41,756 like programming not to be as awful as having 1166 00:50:41,756 --> 00:50:44,336 to define 32 separate integers 1167 00:50:44,336 --> 00:50:47,316 and then me the human remember what the name of all 1168 00:50:47,316 --> 00:50:50,286 of those things are called and in fact, there are. 1169 00:50:50,346 --> 00:50:54,096 So one of the nice things about seeing in most languages is 1170 00:50:54,096 --> 00:50:56,376 that they provide us with this thing called an array. 1171 00:50:56,376 --> 00:50:59,716 And I use this language in week 0 when we talk about scratch, 1172 00:50:59,716 --> 00:51:02,096 because scratch has things called lists, which is sort 1173 00:51:02,096 --> 00:51:04,096 of the child friendly name that they gave 1174 00:51:04,126 --> 00:51:05,816 to the notion of the list or array. 1175 00:51:06,396 --> 00:51:09,226 These things are also called vectors in some context. 1176 00:51:10,176 --> 00:51:11,736 Linked lists will be something related. 1177 00:51:11,786 --> 00:51:13,676 They are all different in certain ways. 1178 00:51:13,676 --> 00:51:16,186 So what we define today I actually distinct 1179 00:51:16,186 --> 00:51:19,176 from scratch did and even what some other languages do. 1180 00:51:19,496 --> 00:51:22,306 But this is a little excerpt from a really nice tutorial 1181 00:51:22,306 --> 00:51:25,186 on line that we also link on the courses and resources page 1182 00:51:25,496 --> 00:51:26,856 about what an array is. 1183 00:51:27,286 --> 00:51:29,716 So we've talked about memory already today. 1184 00:51:29,966 --> 00:51:32,316 So it turns out and let me try to make use 1185 00:51:32,316 --> 00:51:33,586 of this for one second. 1186 00:51:34,186 --> 00:51:41,536 If again your computer's RAM is just a big rectangle from byte 0 1187 00:51:41,536 --> 00:51:44,546 on up to byte like 4 billion, if you got 4 gigabytes of RAM. 1188 00:51:44,866 --> 00:51:47,546 Well what I'd really like if this represents my RAM, 1189 00:51:47,926 --> 00:51:52,586 if I want to have courses -- if I want to have 32 courses I feel 1190 00:51:52,586 --> 00:51:56,326 like I want to say something like int courses 1191 00:51:56,946 --> 00:51:58,816 but now I don't want to copy-paste that. 1192 00:51:58,816 --> 00:52:02,726 I want to say give me not one int but 32 ints all together. 1193 00:52:02,946 --> 00:52:06,116 Well the notation for this is to use square brackets, 1194 00:52:06,506 --> 00:52:08,336 say the number of ints that you want, 1195 00:52:08,596 --> 00:52:10,796 closed square bracket, semicolon. 1196 00:52:10,796 --> 00:52:12,276 What that has done is to declare 1197 00:52:12,276 --> 00:52:14,106 for me a variable called courses. 1198 00:52:14,486 --> 00:52:17,126 That variable tough is an array, 1199 00:52:17,126 --> 00:52:20,156 an array of what, an array of integers. 1200 00:52:20,156 --> 00:52:23,506 Now an array is just a contiguous block 1201 00:52:23,506 --> 00:52:26,986 of memory that's been divvied up into smaller pieces all 1202 00:52:26,986 --> 00:52:29,066 of which structurally are the same. 1203 00:52:29,396 --> 00:52:32,426 Put in real English an array is just 32 ints back 1204 00:52:32,426 --> 00:52:33,986 to back to back in memory. 1205 00:52:34,016 --> 00:52:39,036 What that means is if I declare a single int called X with code 1206 00:52:39,036 --> 00:52:42,536 like that, that would carve out my computer's RAM, 32 bits 1207 00:52:42,686 --> 00:52:46,756 and call it X. But if I instead do int courses 32, 1208 00:52:47,036 --> 00:52:51,366 what I really get then is a whole bunch of int here, 1209 00:52:51,646 --> 00:52:54,896 and this obviously now a picture we're drawing here 1210 00:52:54,896 --> 00:52:56,816 so they won't fit all in a row necessarily. 1211 00:52:57,126 --> 00:52:59,056 [Background sound effects] So if I just count out 32 1212 00:52:59,056 --> 00:53:02,746 of these things, this is all going to be part of courses 1213 00:53:03,216 --> 00:53:07,376 so this whole chunk of memory will have the name courses. 1214 00:53:07,986 --> 00:53:10,486 Now how do I access an individual course, 1215 00:53:10,696 --> 00:53:11,946 well what I'm going to do is this. 1216 00:53:11,946 --> 00:53:13,086 I'm going to go ahead and clean 1217 00:53:13,086 --> 00:53:15,036 up the code I started writing a moment ago. 1218 00:53:15,036 --> 00:53:16,786 So I'm going to delete all that junk. 1219 00:53:16,786 --> 00:53:19,416 I'm going to get rid of this original approach and I'm going 1220 00:53:19,416 --> 00:53:23,676 to say something like int courses 32, now I've got them. 1221 00:53:23,926 --> 00:53:27,176 How do I know start plopping grades inside of each 1222 00:53:27,176 --> 00:53:29,506 of these cells, each of these elements. 1223 00:53:29,886 --> 00:53:32,166 Well course 1 is no more. 1224 00:53:32,266 --> 00:53:33,996 Courses is a variable. 1225 00:53:34,286 --> 00:53:37,016 How do you think I might get at the first element in an array. 1226 00:53:38,486 --> 00:53:39,236 Yeah so 0. 1227 00:53:39,236 --> 00:53:40,526 We always start counting from 0 1228 00:53:40,526 --> 00:53:43,466 so courses bracket 0 equals get int. 1229 00:53:43,466 --> 00:53:46,926 And now down here this becomes courses bracket, oops, 1230 00:53:47,396 --> 00:53:49,576 courses bracket 1 gets get ints. 1231 00:53:49,636 --> 00:53:51,626 Let me do another copy-paste just for now. 1232 00:53:51,926 --> 00:53:54,266 Courses brackets 2 gets get int 1233 00:53:54,786 --> 00:53:56,736 and you now see where this can go. 1234 00:53:57,156 --> 00:53:59,246 We already have the ability to implement loops 1235 00:53:59,276 --> 00:54:02,746 in C. We have the ability to have counter-variables in C 1236 00:54:02,746 --> 00:54:07,656 like I and J. So do you already see in your mind how we can kind 1237 00:54:07,656 --> 00:54:10,706 of rip out almost all of this code and try getting rid 1238 00:54:10,706 --> 00:54:11,796 of a hundred lines at once. 1239 00:54:12,226 --> 00:54:14,506 In fact I don't really need any of these lines either. 1240 00:54:14,786 --> 00:54:17,216 Now this is not yet a loop but couldn't I do something 1241 00:54:17,216 --> 00:54:22,266 like 4 int, I gets 0, I is less than 32, I plus plus. 1242 00:54:22,626 --> 00:54:24,186 Give me little curly braces here, 1243 00:54:24,656 --> 00:54:26,046 what did you get in course 1? 1244 00:54:26,046 --> 00:54:27,066 I'll fix that in a moment. 1245 00:54:27,066 --> 00:54:28,866 Let me close my curly braces here. 1246 00:54:29,216 --> 00:54:30,296 There's a couple of problems. 1247 00:54:30,386 --> 00:54:31,656 What's one I can fix easily? 1248 00:54:32,876 --> 00:54:34,516 Yeah so it's not courses brackets 0. 1249 00:54:34,516 --> 00:54:35,626 Stop hard-coding that. 1250 00:54:35,836 --> 00:54:39,896 Courses bracket I and then this too is a little weird. 1251 00:54:39,896 --> 00:54:41,176 I can't hard-code this. 1252 00:54:41,306 --> 00:54:44,376 Now obviously that's not the solution there to change the 1 1253 00:54:44,376 --> 00:54:46,966 to an I in that context, but what could I say, 1254 00:54:46,966 --> 00:54:53,936 course percent D question mark close quote comma I. 1255 00:54:54,296 --> 00:54:55,316 So again same building set 1256 00:54:55,316 --> 00:54:57,676 and again already viola, you know a new idea. 1257 00:54:57,716 --> 00:54:59,546 We just deployed this really quick. 1258 00:54:59,586 --> 00:55:02,176 We said you can have multiple variables all of the same type 1259 00:55:02,176 --> 00:55:05,846 but give them one name and refer to them numerically in this way. 1260 00:55:06,046 --> 00:55:08,186 Now we have the capability of expressing what 1261 00:55:08,216 --> 00:55:10,056 in the beginning was a very simple problem. 1262 00:55:10,056 --> 00:55:12,266 I want to waive inputting all of my grades. 1263 00:55:12,766 --> 00:55:15,016 Well now let's actually look at a polished version of this. 1264 00:55:15,546 --> 00:55:19,246 Well in array 1 dot C I have this program here 1265 00:55:19,476 --> 00:55:20,816 and it's a little more interesting 1266 00:55:20,816 --> 00:55:22,466 because I didn't want to just input my data, 1267 00:55:22,526 --> 00:55:24,356 I wanted to just compute my average, 1268 00:55:24,356 --> 00:55:25,646 which is slightly more compelling. 1269 00:55:25,956 --> 00:55:27,976 So let's actually see the result of this first. 1270 00:55:28,386 --> 00:55:33,246 So GCC array 1 dot C. Uh huh, 1271 00:55:33,466 --> 00:55:37,686 can fix that right -- dash L CS50 good. 1272 00:55:38,096 --> 00:55:39,726 A dot out enter. 1273 00:55:39,726 --> 00:55:40,796 What were your quiz scores? 1274 00:55:40,796 --> 00:55:43,876 So I got lets say 100 on the first one, 1275 00:55:44,146 --> 00:55:45,666 then I kind of screwed up. 1276 00:55:45,986 --> 00:55:49,226 So you're average -- you'll remember that comment now 1277 00:55:49,276 --> 00:55:51,616 if you're the student that gets a 50 on the quiz right. 1278 00:55:51,616 --> 00:55:53,536 So alright, so 50 is good. 1279 00:55:54,096 --> 00:55:55,706 So 75 is my average. 1280 00:55:55,706 --> 00:55:57,446 So you could do this on a calculator 1281 00:55:57,446 --> 00:56:00,396 but if you had 32 courses, you now it's kind of nice 1282 00:56:00,436 --> 00:56:01,506 to let a computer do that. 1283 00:56:01,506 --> 00:56:03,736 You don't want to have to do it all by hand necessarily. 1284 00:56:04,026 --> 00:56:05,166 So how does this work? 1285 00:56:05,166 --> 00:56:07,686 Well notice, I'm incrementing some variable, 1286 00:56:07,686 --> 00:56:09,556 so we already have the building blocks in there. 1287 00:56:09,756 --> 00:56:11,016 I'm doing some mathematics. 1288 00:56:11,196 --> 00:56:14,326 It feels like I'm maybe rounding too to the nearest int. 1289 00:56:14,536 --> 00:56:17,776 You would notice if we were just using casting from floats 1290 00:56:17,776 --> 00:56:19,236 to ints in this course instead 1291 00:56:19,236 --> 00:56:21,976 of actually mathematically rounding grades at terms end. 1292 00:56:22,226 --> 00:56:23,826 So let me go back into this. 1293 00:56:23,896 --> 00:56:28,416 This was array 1 dot C. Well let's see what I'm doing. 1294 00:56:28,586 --> 00:56:31,096 Well new feature; it turns out what I started to do 1295 00:56:31,096 --> 00:56:33,576 on the fly there a moment ago is kind of bad practice. 1296 00:56:33,686 --> 00:56:37,486 I hard-coded the number 32 and not only once but twice, 1297 00:56:37,826 --> 00:56:38,966 that tends to be bad practice. 1298 00:56:39,466 --> 00:56:42,816 So that's what's known as a magic number, magic in the sense 1299 00:56:42,946 --> 00:56:46,046 that where the heck did I define that value all over the place? 1300 00:56:46,396 --> 00:56:49,076 So another feature of C, and it's a little dark, 1301 00:56:49,156 --> 00:56:52,036 I apologize here on this screen is this ability 1302 00:56:52,036 --> 00:56:55,126 to have another thing called the preprocessor directive. 1303 00:56:55,126 --> 00:56:56,606 We've seen the sharp sign before. 1304 00:56:56,826 --> 00:56:59,376 That thing sharp include is a preprocessor directed. 1305 00:56:59,646 --> 00:57:00,716 There's another one that's useful. 1306 00:57:00,976 --> 00:57:04,236 Sharp define allows you to define a constant. 1307 00:57:04,506 --> 00:57:05,486 It's not a variable. 1308 00:57:05,816 --> 00:57:08,596 It's just the symbolic representation of something, 1309 00:57:08,746 --> 00:57:09,726 in this case a number. 1310 00:57:10,016 --> 00:57:13,386 So that henceforth in my code I can use capital QUIZZES anywhere 1311 00:57:13,386 --> 00:57:17,666 I want and it's going to refer back to that same value. 1312 00:57:18,066 --> 00:57:20,346 Now just intuitively what's an advantage 1313 00:57:20,346 --> 00:57:23,746 to having used a constant like this instead of typing in 32? 1314 00:57:24,006 --> 00:57:24,096 Yeah. 1315 00:57:24,386 --> 00:57:26,386 >> [Inaudible background discussion] 1316 00:57:26,676 --> 00:57:29,736 >> Yeah, exactly. 1317 00:57:29,736 --> 00:57:32,096 If I'm on -- if my class has more quizzes in the future 1318 00:57:32,096 --> 00:57:34,856 and I need to store more grades I just change it one place, 1319 00:57:34,856 --> 00:57:36,136 recompile and done. 1320 00:57:36,306 --> 00:57:39,036 And in fact that's a bit of an annoying process. 1321 00:57:39,036 --> 00:57:42,636 We'll also see ways when I run the program I could just tell my 1322 00:57:42,636 --> 00:57:44,816 program how many quizzes we had that semester. 1323 00:57:45,036 --> 00:57:46,416 We don't even have to hard-code it. 1324 00:57:46,656 --> 00:57:49,426 Now the fact that I'm capitalizing it this is this 1325 00:57:49,466 --> 00:57:51,016 matter of style, convention. 1326 00:57:51,016 --> 00:57:53,256 Almost any time you have a constant value, 1327 00:57:53,546 --> 00:57:56,966 you should capitalize it entirely so that it stands 1328 00:57:57,006 --> 00:57:59,836 to you the reader and other readers, teaching fellows 1329 00:57:59,836 --> 00:58:02,816 or other programmers that in fact, it is a constant value. 1330 00:58:03,166 --> 00:58:04,266 So what's going on here? 1331 00:58:04,576 --> 00:58:09,196 Well let's see I have a float called grades bracket quizzes. 1332 00:58:09,196 --> 00:58:11,906 Alright so we've not quite seen this context yet in examples. 1333 00:58:12,106 --> 00:58:14,656 It turns out if you want to have multiple variables all 1334 00:58:14,656 --> 00:58:16,176 of the same type, you don't need 1335 00:58:16,176 --> 00:58:17,606 to put them each on their own line. 1336 00:58:17,716 --> 00:58:19,806 You can just bring them together with commas. 1337 00:58:20,156 --> 00:58:22,916 So on that first line I'm declaring an array called 1338 00:58:22,916 --> 00:58:29,296 grades, I have how many elements in it, how many floats, 1339 00:58:29,856 --> 00:58:32,926 so two as per the constant called quizzes and then 1340 00:58:33,146 --> 00:58:35,206 after that I have another float altogether, 1341 00:58:35,206 --> 00:58:37,316 a different chunk of memory called sum. 1342 00:58:37,536 --> 00:58:39,336 And I also apparently have the foresight 1343 00:58:39,336 --> 00:58:42,536 to declare a variable called average and one called I. 1344 00:58:42,736 --> 00:58:44,086 And I start to use those here. 1345 00:58:44,666 --> 00:58:45,236 Alright print. 1346 00:58:45,306 --> 00:58:47,426 What were your quiz scores? 1347 00:58:47,676 --> 00:58:50,586 Alright for I is 0, I is less than quizzes, 1348 00:58:50,646 --> 00:58:54,116 I is plus plus quiz number percent, alright this is easy. 1349 00:58:54,116 --> 00:58:55,996 I know what this is and here's the line of code 1350 00:58:55,996 --> 00:58:58,336 that we already did on the fly albeit 1351 00:58:58,336 --> 00:59:01,326 with get int grade bracket I get flow. 1352 00:59:01,576 --> 00:59:03,896 So realize 0 indexing is important. 1353 00:59:03,896 --> 00:59:07,936 When the compiler gives you memory you get 0 1 2. 1354 00:59:07,936 --> 00:59:11,686 If you start counting at 1 you get 1, 2, 3, 1355 00:59:11,686 --> 00:59:14,686 4, 5, 6, 7 that's bad. 1356 00:59:14,686 --> 00:59:17,336 You wanted to end at 6 so to speak. 1357 00:59:17,336 --> 00:59:19,716 So what do I next with this value? 1358 00:59:19,716 --> 00:59:21,406 Alright so now I have some. 1359 00:59:21,606 --> 00:59:23,706 How do you compute a student's average? 1360 00:59:23,706 --> 00:59:25,606 Well you take all the grades, you sum them together 1361 00:59:25,606 --> 00:59:26,896 and you divide them by the total number 1362 00:59:26,926 --> 00:59:28,226 of quizzes right, old school stuff. 1363 00:59:28,226 --> 00:59:30,336 So sum is initialized as 0. 1364 00:59:30,796 --> 00:59:33,906 So I is 0 on up to quizzes to another 4 loop, 1365 00:59:34,076 --> 00:59:35,286 so this is easy alright. 1366 00:59:35,286 --> 00:59:39,186 I'm just adding to sum inside of this loop the ith value 1367 00:59:39,186 --> 00:59:40,836 of grades, which is just 2, 1368 00:59:41,106 --> 00:59:43,176 but again realize the dynamism of this program. 1369 00:59:43,176 --> 00:59:45,336 Just by changing that constant I could do this 1370 00:59:45,336 --> 00:59:48,606 for a whole semester for four years worth of college. 1371 00:59:48,946 --> 00:59:52,276 So now here's an interesting trick and I'll offer this just 1372 00:59:52,276 --> 00:59:54,316 as a little teaser for now. 1373 00:59:54,926 --> 00:59:57,656 To compute a student's average is really the result 1374 00:59:57,656 --> 01:00:00,686 of taking the sum, dividing by the number of quizzes 1375 01:00:01,146 --> 01:00:02,696 but then there's this .5 trick. 1376 01:00:02,776 --> 01:00:04,786 For those of you who want to think this through actually 1377 01:00:04,786 --> 01:00:07,096 for problem set 1, this .5 trick, 1378 01:00:07,456 --> 01:00:09,526 this is how you can implement the notion 1379 01:00:09,526 --> 01:00:10,846 of mathematical rounding. 1380 01:00:11,206 --> 01:00:13,036 And I'll leave that as a little thought exercise, 1381 01:00:13,116 --> 01:00:15,456 but if you think through what might happen to your grades 1382 01:00:15,456 --> 01:00:19,856 if you do and do not include this .5 and then proceed to cast 1383 01:00:20,256 --> 01:00:23,456 to an int, casting has the effect of truncating everything 1384 01:00:23,456 --> 01:00:24,476 after the decimal point. 1385 01:00:24,756 --> 01:00:28,676 So there's actually a very clever magic that's induced 1386 01:00:28,756 --> 01:00:31,826 by adding .5 before you throw that stuff away. 1387 01:00:32,066 --> 01:00:34,296 So the person who wrote the math round function 1388 01:00:34,296 --> 01:00:37,276 in the math library, odds are they did something very similar 1389 01:00:37,346 --> 01:00:37,536 to this. 1390 01:00:38,016 --> 01:00:39,206 Then finally what's the average, 1391 01:00:39,206 --> 01:00:40,806 well I just print out the results. 1392 01:00:41,116 --> 01:00:43,256 Well just to show that you don't need to show that little trick, 1393 01:00:43,256 --> 01:00:46,346 array 2 that you have print outs of is the exact same thing 1394 01:00:46,616 --> 01:00:50,076 but I decided to use the round function explicitly instead 1395 01:00:50,076 --> 01:00:51,746 of rolling it myself. 1396 01:00:52,826 --> 01:00:55,746 Okay so enough about math, because we've actually dwelled 1397 01:00:55,746 --> 01:00:56,746 on this for some time. 1398 01:00:57,006 --> 01:00:59,126 Let's actually start manipulating things 1399 01:00:59,166 --> 01:01:02,336 that are more genuinely useful and those are strings right, 1400 01:01:02,336 --> 01:01:05,036 because problem set 1 even though it's largely grounded 1401 01:01:05,036 --> 01:01:06,166 in numbers and arithmetic, 1402 01:01:06,386 --> 01:01:08,056 that's really just a design of necessity. 1403 01:01:08,056 --> 01:01:10,866 It's actually kind of hard to do more sophisticated things 1404 01:01:10,866 --> 01:01:15,066 when you've not even written one 4 loop or one Y loop in C. 1405 01:01:15,066 --> 01:01:16,566 So it's meant as a stepping stone. 1406 01:01:16,816 --> 01:01:19,076 Problem set 2 though is going to introduce you 1407 01:01:19,076 --> 01:01:22,776 to another real-world domain of cryptography, 1408 01:01:22,776 --> 01:01:24,676 the art of concealing information. 1409 01:01:24,866 --> 01:01:26,806 Information is often expressed in English 1410 01:01:26,806 --> 01:01:29,746 or some other textual language and so that makes the question, 1411 01:01:29,746 --> 01:01:33,296 "How do I somehow manipulate actual characters, 1412 01:01:33,296 --> 01:01:36,926 actual strings, actual paragraphs or entire letters, 1413 01:01:37,576 --> 01:01:38,616 entire letters of text?" 1414 01:01:38,616 --> 01:01:41,086 So here's a simple example that's going to get us there. 1415 01:01:41,086 --> 01:01:45,706 I turns out that what we called last time a string, 1416 01:01:45,876 --> 01:01:47,706 we already know that a string is synonymous 1417 01:01:47,706 --> 01:01:52,296 with a slightly more confusing Charstar the asterisk 1418 01:01:52,296 --> 01:01:54,206 so again we'll tease apart what that means 1419 01:01:54,256 --> 01:01:55,876 but a string is a Charstar. 1420 01:01:55,876 --> 01:01:59,956 For today's purposes what a string really is is an -- 1421 01:02:00,596 --> 01:02:06,006 it's an array not a int but an array of Chars, 1422 01:02:06,006 --> 01:02:08,506 an array of 8-bit characters. 1423 01:02:08,806 --> 01:02:10,246 So that kind of makes sense right? 1424 01:02:10,246 --> 01:02:14,286 An array of if a string is just a contiguous sequence 1425 01:02:14,286 --> 01:02:17,946 of letters, you know f o o or h e l l o you know it kind 1426 01:02:17,946 --> 01:02:20,276 of makes sense that you know what the perfect data structure 1427 01:02:20,276 --> 01:02:23,216 for strings if we're not talking about ints anymore 1428 01:02:23,636 --> 01:02:26,746 but are instead alphabetical letters why don't we call this 1429 01:02:26,796 --> 01:02:29,506 something like the string S 1430 01:02:29,506 --> 01:02:34,336 so then I can put the letter H there e-l-l-o and so forth. 1431 01:02:34,336 --> 01:02:37,826 It feels like it makes perfect sense to actually store a string 1432 01:02:37,916 --> 01:02:40,916 in an array because I actually get this built-in feature 1433 01:02:40,916 --> 01:02:43,616 of storing things back to back to back and not all 1434 01:02:43,616 --> 01:02:44,646 over the place in memory. 1435 01:02:44,976 --> 01:02:49,116 So this program here is called string 1 dot C. Notice 1436 01:02:49,186 --> 01:02:52,796 that I'm using a new library up here, string dot H 1437 01:02:53,106 --> 01:02:54,396 for reasons we'll see in a moment. 1438 01:02:54,676 --> 01:02:57,306 Now what am I doing, well I've removed the comments 1439 01:02:57,306 --> 01:03:00,656 for today's training wheel removal purposes but this line 1440 01:03:00,656 --> 01:03:03,916 of code there simply gets a string from the user, 1441 01:03:03,916 --> 01:03:06,896 not a flow per se but a generic string 1442 01:03:07,246 --> 01:03:09,096 and in fact its going to store it. 1443 01:03:09,336 --> 01:03:12,116 Get string is going to allocate a bunch of RAM like this 1444 01:03:12,116 --> 01:03:14,836 and whatever you type it's going to put the first letter here, 1445 01:03:15,086 --> 01:03:17,516 the next letter there, the next letter there and so forth 1446 01:03:17,516 --> 01:03:18,976 and then it's going to hand you back 1447 01:03:19,546 --> 01:03:21,546 that entire chunk of memory. 1448 01:03:21,916 --> 01:03:23,996 So I do need to do one sanity check 1449 01:03:24,166 --> 01:03:26,316 and this too is a teaser of sorts today. 1450 01:03:26,796 --> 01:03:30,156 It turns out that if the user is malicious or foolish 1451 01:03:30,466 --> 01:03:32,206 or there's just some problem 1452 01:03:32,206 --> 01:03:33,876 that you can't anticipate we need 1453 01:03:33,876 --> 01:03:37,096 to sometimes have return values that are not legit values 1454 01:03:37,356 --> 01:03:40,746 like the actual string, a return value that's not the actual 1455 01:03:40,746 --> 01:03:42,116 return value number you were expecting 1456 01:03:42,396 --> 01:03:44,386 but some kind of sentinel value. 1457 01:03:44,516 --> 01:03:47,706 So a special value that you the programmer can check for 1458 01:03:47,896 --> 01:03:50,516 and if you get back that sentinel value instead 1459 01:03:50,516 --> 01:03:53,236 of a legit value you know something went wrong 1460 01:03:53,236 --> 01:03:55,916 and error checking is one of the topics for Monday. 1461 01:03:56,366 --> 01:03:58,236 So it turns out, and you would only know this 1462 01:03:58,236 --> 01:04:00,666 from reading the documentation or being told it in class, 1463 01:04:01,166 --> 01:04:06,896 get string maybe can return a special value called NULL 1464 01:04:07,286 --> 01:04:08,346 in all capital letters. 1465 01:04:08,786 --> 01:04:10,466 So we'll use this throughout the semester. 1466 01:04:10,466 --> 01:04:12,026 So today we're just scratching the surface. 1467 01:04:12,226 --> 01:04:16,216 So this is just a special value NULL that if you check for it, 1468 01:04:16,216 --> 01:04:18,616 when you get it back it means that the user did something 1469 01:04:18,616 --> 01:04:21,556 like never typed anything or just hit enter immediately 1470 01:04:21,556 --> 01:04:23,126 or just tried to generally mess with you 1471 01:04:23,126 --> 01:04:24,636 or did something you weren't expecting. 1472 01:04:24,976 --> 01:04:27,246 So the very first thing I do in this program is I check. 1473 01:04:27,386 --> 01:04:31,446 If S does not equal NULL that's a good thing, let's proceed 1474 01:04:31,446 --> 01:04:32,446 to do something with it. 1475 01:04:32,956 --> 01:04:34,166 I now have 4-loop. 1476 01:04:34,356 --> 01:04:35,546 4 I gets 0. 1477 01:04:35,756 --> 01:04:37,686 I is less than, oh this is interesting. 1478 01:04:37,746 --> 01:04:41,816 Take a guess what this briefly named functions does. 1479 01:04:41,816 --> 01:04:42,706 >> [Inaudible background discussion] 1480 01:04:42,706 --> 01:04:43,426 >> String lengths. 1481 01:04:43,546 --> 01:04:46,216 Stir lengths, so this is common feature of C. 1482 01:04:46,216 --> 01:04:49,286 So why say string length when you can say stirleng. 1483 01:04:49,576 --> 01:04:51,066 Well how does that -- that's a function. 1484 01:04:51,256 --> 01:04:54,456 It exists in the string library whose hetero file is called 1485 01:04:54,456 --> 01:04:57,386 string dot H. Now even just a little tangent, 1486 01:04:57,386 --> 01:04:59,816 how would you the human implement string length. 1487 01:05:00,186 --> 01:05:03,446 Well, I'd probably start looking at my chunk of memory 1488 01:05:03,496 --> 01:05:06,126 and just start counting how many legitimate values do I 1489 01:05:06,126 --> 01:05:07,086 actually see. 1490 01:05:07,346 --> 01:05:09,696 Now it turns out there's going to be a special value 1491 01:05:10,386 --> 01:05:13,116 that will store at the end of a string. 1492 01:05:13,406 --> 01:05:16,426 Backslash 0 is literally the value 1493 01:05:17,276 --> 01:05:21,076 of [Sound effects] eight 0 bits in a row 1494 01:05:21,076 --> 01:05:23,536 so it's a special value not quote-unquote 0 1495 01:05:23,536 --> 01:05:27,126 which is an ASCII character but rather a special sentinel value 1496 01:05:27,356 --> 01:05:29,276 so stirleng as we will see eventually 1497 01:05:29,276 --> 01:05:32,286 in more detail actually iterates over an array 1498 01:05:32,286 --> 01:05:33,196 like that to get its length. 1499 01:05:33,196 --> 01:05:37,126 So what does this do, well notice is a Char 1500 01:05:37,156 --> 01:05:38,496 because I declared it up here. 1501 01:05:39,126 --> 01:05:42,566 S is a string because I got here from the user 1502 01:05:42,896 --> 01:05:46,996 and S bracket I gives me the ith character in the string 1503 01:05:47,326 --> 01:05:49,566 and then plops it in the variable called C. 1504 01:05:49,566 --> 01:05:53,246 So what's the end result, well let me go ahead and on GCC 1505 01:05:53,246 --> 01:05:58,116 on string 1 dot C, so undefined reference again dot LCS dash 50 1506 01:05:58,406 --> 01:06:02,956 okay so A dot out enter okay it's not doing anything why? 1507 01:06:04,966 --> 01:06:06,766 Right because I didn't say printf -- 1508 01:06:06,766 --> 01:06:08,206 I really cut some corners here 1509 01:06:08,396 --> 01:06:11,576 so hello there exclamation point enter. 1510 01:06:12,276 --> 01:06:17,686 Okay so h e l l o comma space t h e r e exclamation point all 1511 01:06:17,686 --> 01:06:19,626 in a separate line, why did that happen? 1512 01:06:19,846 --> 01:06:21,806 Well this was string one dot C, 1513 01:06:21,946 --> 01:06:23,626 that's precisely what should have happened 1514 01:06:23,626 --> 01:06:26,436 because in this 4 loop I printed percent C 1515 01:06:26,436 --> 01:06:29,906 which is the new format code for just a character backslash N 1516 01:06:30,206 --> 01:06:32,466 and so I got everything on a separate line. 1517 01:06:32,466 --> 01:06:36,066 So in fact had I done something maybe a little less weird 1518 01:06:36,066 --> 01:06:38,426 and just complied this and rerun this, 1519 01:06:38,886 --> 01:06:43,586 now I have a sentence here enter, now I get it back. 1520 01:06:44,086 --> 01:06:46,356 Now grant it I forgot one new line altogether 1521 01:06:46,586 --> 01:06:49,056 but all this program demonstrates is that a string 1522 01:06:49,256 --> 01:06:50,816 that you can fetch from the user by way 1523 01:06:50,816 --> 01:06:53,806 of get string hands you effectively a chunk of memory 1524 01:06:54,086 --> 01:06:56,966 that you can iterate over with this bracket notation just 1525 01:06:56,966 --> 01:07:00,286 like you could a whole bunch of grades from your course work. 1526 01:07:00,666 --> 01:07:04,076 Well string 2 has a slight variant on this 1527 01:07:04,316 --> 01:07:09,106 so in string 2 dot C I do something more intelligent 1528 01:07:09,106 --> 01:07:12,616 which is good design so this speaks to this access of design. 1529 01:07:12,966 --> 01:07:16,316 Notice this what have I done differently here 1530 01:07:16,356 --> 01:07:18,786 if you can think back or on your printouts see the difference 1531 01:07:18,786 --> 01:07:19,926 between that line and the previous version. 1532 01:07:20,516 --> 01:07:25,546 [ Silence ] 1533 01:07:26,046 --> 01:07:26,836 >> A little louder, anyone? 1534 01:07:27,111 --> 01:07:29,111 >> [Inaudible background discussion] 1535 01:07:29,386 --> 01:07:31,696 >> Yeah, so I have two initializations. 1536 01:07:31,696 --> 01:07:37,676 So notice at the left I have I get 0 comma N get stirleng S. 1537 01:07:38,536 --> 01:07:42,716 Why did I move or add this declaration of N 1538 01:07:42,846 --> 01:07:45,156 and assign it the return value of stirleng 1539 01:07:45,496 --> 01:07:48,646 in the initialization part of the loop instead of, 1540 01:07:48,886 --> 01:07:50,626 I'll scroll back if you haven't the print out, 1541 01:07:50,836 --> 01:07:55,016 instead of putting it right here in the condition 1542 01:07:55,016 --> 01:07:57,316 which was really nice and clean and sensible. 1543 01:07:57,316 --> 01:07:57,916 >> [Inaudible background discussion] 1544 01:07:57,916 --> 01:08:02,786 >> Yeah, because this approach elegant though it is, it's nice 1545 01:08:02,786 --> 01:08:05,206 and compact, right looks like a really good piece of code, 1546 01:08:05,566 --> 01:08:09,006 but remember the condition is checked every iteration 1547 01:08:09,326 --> 01:08:13,306 which means the code inside this condition is executed every 1548 01:08:13,376 --> 01:08:16,186 iteration which means you're calling the function again 1549 01:08:16,186 --> 01:08:19,786 and again and again for as many times as this thing iterates. 1550 01:08:20,186 --> 01:08:21,276 No what does that mean? 1551 01:08:21,536 --> 01:08:26,036 You're checking the length of this string by going 1,2, 3, 4, 1552 01:08:26,036 --> 01:08:28,456 5, return 5 again and again. 1553 01:08:28,456 --> 01:08:30,386 1, 2, 3, 4, 5 return 5. 1554 01:08:30,416 --> 01:08:33,366 1, 2, 3, I mean this should feel stupid 1555 01:08:33,366 --> 01:08:37,256 because this changing length in any way will know. 1556 01:08:37,336 --> 01:08:40,106 So they're to is an opportunity for optimization 1557 01:08:40,176 --> 01:08:42,316 for a little bit more clever design and thus 1558 01:08:42,316 --> 01:08:45,816 in string 2 do I actually call that function once? 1559 01:08:46,136 --> 01:08:47,446 And I wanted to be a little elegant 1560 01:08:47,446 --> 01:08:48,686 so I put it in the 4 loop. 1561 01:08:48,686 --> 01:08:53,156 I could have just done this and just got stirleng of S here 1562 01:08:53,266 --> 01:08:55,266 and I didn't need to use this common notation 1563 01:08:55,266 --> 01:08:57,116 so again there's often multiple ways 1564 01:08:57,146 --> 01:08:59,426 to implement the particular goal but realize 1565 01:08:59,556 --> 01:09:02,266 that was a fundamental improvement 1566 01:09:02,536 --> 01:09:05,596 because I'm not calling the function whose value is never 1567 01:09:05,596 --> 01:09:09,206 going to change more times than is strictly necessary. 1568 01:09:09,626 --> 01:09:11,006 So let's use this same feature 1569 01:09:11,186 --> 01:09:12,936 to do something that's more useful. 1570 01:09:13,276 --> 01:09:15,086 So it's a little weird looking code 1571 01:09:15,086 --> 01:09:16,646 but it uses all the same building blocks 1572 01:09:16,646 --> 01:09:17,776 that we been using for days. 1573 01:09:17,776 --> 01:09:21,026 So this program is set up as follows: Get a string 1574 01:09:21,026 --> 01:09:23,486 from the user that's old hat now. 1575 01:09:23,896 --> 01:09:27,736 Now what? Iterate over the string from I up to the length, 1576 01:09:27,936 --> 01:09:28,986 okay so we just did that. 1577 01:09:29,046 --> 01:09:30,086 This code is the same. 1578 01:09:30,316 --> 01:09:33,016 So the only new magic here are these four lines. 1579 01:09:33,546 --> 01:09:35,236 Now what is the saying in English? 1580 01:09:35,666 --> 01:09:40,076 If the ith character in S is greater than or equal 1581 01:09:40,076 --> 01:09:44,666 to lower case A and the ith character in S is less than 1582 01:09:44,666 --> 01:09:48,306 or equal to the value of the lower case Z, 1583 01:09:48,476 --> 01:09:52,046 what do you think the next line of code is doing? 1584 01:09:52,246 --> 01:09:53,856 It's just capitalizing it. 1585 01:09:54,326 --> 01:09:56,446 Well mathematically and I won't dwell so much 1586 01:09:56,446 --> 01:09:58,996 on the arithmetic here because it's the same stuff 1587 01:09:58,996 --> 01:10:01,856 as a week ago but notice do you capitalize something, 1588 01:10:02,046 --> 01:10:04,226 well let's just do the math quickly for one example. 1589 01:10:04,436 --> 01:10:06,026 Little A is 97. 1590 01:10:06,026 --> 01:10:08,596 Right I've promised this would be useful so little A is 97. 1591 01:10:08,656 --> 01:10:11,156 Big A is 65 so not matter what I'm doing I'm going 1592 01:10:11,156 --> 01:10:13,616 to be subtracting because I gotta take big numbers 1593 01:10:13,806 --> 01:10:16,496 and make them smaller if I'm going to go to like 97 1594 01:10:16,496 --> 01:10:18,456 and above to 65 and above. 1595 01:10:18,726 --> 01:10:22,956 So I have the value of S bracket I here then I do subtraction. 1596 01:10:23,276 --> 01:10:24,276 Well what do I subtract? 1597 01:10:24,506 --> 01:10:28,646 Well technically I just need to subtract 65 but if I put 65 1598 01:10:28,646 --> 01:10:31,026 in here, kind of a bad thing. 1599 01:10:31,256 --> 01:10:33,496 It's a magic number like Y 65. 1600 01:10:33,906 --> 01:10:35,776 Well why don't we just go ahead and do this. 1601 01:10:36,196 --> 01:10:38,946 The difference that we want to subtract off is always going 1602 01:10:38,946 --> 01:10:42,226 to be this, A minus A, little A minus big A. 1603 01:10:42,586 --> 01:10:45,406 So let's just subtract things off a little more dynamically. 1604 01:10:45,626 --> 01:10:49,206 And again the astute can say why are you computing A minus A 1605 01:10:49,206 --> 01:10:50,636 again and again and again. 1606 01:10:50,766 --> 01:10:52,916 It turns out compilers can be smart 1607 01:10:52,916 --> 01:10:55,236 and if they realize you're doing some arithmetic that's never 1608 01:10:55,236 --> 01:10:58,076 going to change, sometimes the compiler can optimize 1609 01:10:58,076 --> 01:11:00,906 that for you, but for today we offer it just for clarity. 1610 01:11:01,176 --> 01:11:04,416 So if we go ahead and compile this, capitalize dot C, 1611 01:11:04,966 --> 01:11:07,096 damn again dash L dash CS50 1612 01:11:07,096 --> 01:11:10,406 but again these errors should no longer be intimidating. 1613 01:11:10,626 --> 01:11:12,846 Let's go ahead and run A dot out and capitalize. 1614 01:11:13,246 --> 01:11:18,616 Hello there exclamation point and there I preserved my space. 1615 01:11:18,616 --> 01:11:21,446 It preserved my exclamation point 1616 01:11:21,696 --> 01:11:24,006 so I actually did get this right. 1617 01:11:24,236 --> 01:11:28,616 So let's lay the stage now for where we're going with this. 1618 01:11:28,616 --> 01:11:31,256 Two quick comments- one by far if you're feeling 1619 01:11:31,256 --> 01:11:33,696 like you're not among those with books in hand or you just 1620 01:11:33,696 --> 01:11:35,896 like learning stuff on line, these are the two places 1621 01:11:35,896 --> 01:11:38,416 to start because it covers the stuff we've been looking at. 1622 01:11:38,836 --> 01:11:41,006 But let's now pull back a layer 1623 01:11:41,006 --> 01:11:42,946 that I've been waving my hand at for a week. 1624 01:11:43,226 --> 01:11:46,746 You have for any program you've been writing used int, main, 1625 01:11:46,746 --> 01:11:49,546 and then int argC comma Charstar argv. 1626 01:11:49,906 --> 01:11:51,356 Well Charstar is strength. 1627 01:11:51,946 --> 01:11:53,316 So that's the synonym for now. 1628 01:11:53,316 --> 01:11:57,026 So it appears that it may all of this time has been allowed 1629 01:11:57,026 --> 01:12:01,466 to take in two inputs ints and also a string. 1630 01:12:02,096 --> 01:12:03,666 Alright so what does that actually mean? 1631 01:12:03,776 --> 01:12:05,966 Well it means that just like I can run GCC 1632 01:12:06,046 --> 01:12:11,576 and then Fu dot C I can apparently pass input to -- 1633 01:12:12,766 --> 01:12:13,816 you gotta say something sometimes. 1634 01:12:14,346 --> 01:12:18,396 I can apparently pass to programs like GCC 1635 01:12:18,576 --> 01:12:21,726 by just writing those arguments on the command line. 1636 01:12:21,726 --> 01:12:24,316 So it's not quite an argument in the sense of a function 1637 01:12:24,316 --> 01:12:25,726 but this is a very different context. 1638 01:12:25,726 --> 01:12:27,346 This is my so-called blinking prompt. 1639 01:12:27,736 --> 01:12:32,196 I can clearly -- been doing things like dash 0 Fu and then 1640 01:12:32,196 --> 01:12:34,866 down here I've been saying things like dash L CS50 1641 01:12:34,866 --> 01:12:38,106 so there's many different labels the world has slapped 1642 01:12:38,106 --> 01:12:41,036 on these kinds of words, switches, lags, 1643 01:12:41,216 --> 01:12:42,446 command line, arguments. 1644 01:12:42,486 --> 01:12:44,676 We'll typically call them command line arguments 1645 01:12:45,036 --> 01:12:47,476 but you've used these before any time you've typed commands. 1646 01:12:47,536 --> 01:12:49,736 Nano takes the command line argument. 1647 01:12:49,956 --> 01:12:52,316 It gives the command line argument that nano is supposed 1648 01:12:52,316 --> 01:12:54,616 to take, the name of the file. 1649 01:12:54,756 --> 01:12:57,026 So we have this ability clearly in Linux, 1650 01:12:57,026 --> 01:12:59,116 MAC OS and other context to. 1651 01:12:59,346 --> 01:13:01,076 So how do we get at that information? 1652 01:13:01,306 --> 01:13:04,906 Well in argv 1 see a very simple example. 1653 01:13:05,186 --> 01:13:08,206 Notice here that in this function I first, 1654 01:13:08,206 --> 01:13:11,736 in this main function I print a new line then I do this. 1655 01:13:12,356 --> 01:13:14,986 For I is 0 on up to argC. 1656 01:13:15,296 --> 01:13:19,116 So take a guess, what does argC maybe represent? 1657 01:13:20,426 --> 01:13:24,116 Argument count, argC it's a convention. 1658 01:13:24,116 --> 01:13:26,136 I could have called it X and Y but the world's kind 1659 01:13:26,136 --> 01:13:27,716 of standardized on these two nicknames. 1660 01:13:28,016 --> 01:13:29,486 ArgC I argument count. 1661 01:13:29,826 --> 01:13:33,026 So if I want to iterate over all of the words that the user typed 1662 01:13:33,026 --> 01:13:36,326 at the command prompt, I'm going to iterate from 0 up to argC. 1663 01:13:36,526 --> 01:13:38,166 What is inside of argv? 1664 01:13:38,436 --> 01:13:41,486 Argv apparently all the time has been an array 1665 01:13:41,616 --> 01:13:43,256 because of the bracketed notation. 1666 01:13:43,426 --> 01:13:46,616 What is inside that array are not just characters. 1667 01:13:46,836 --> 01:13:51,736 It turns out that inside an array you also put other arrays. 1668 01:13:51,856 --> 01:13:53,986 You can put strings themselves. 1669 01:13:54,546 --> 01:13:58,126 So you're really taking this -- it's like a 2-dimensional array. 1670 01:13:58,326 --> 01:14:01,966 So argv in this case is an array of not Chars 1671 01:14:01,966 --> 01:14:05,356 but of Charstars aka strings. 1672 01:14:06,056 --> 01:14:07,016 So what does that mean? 1673 01:14:07,016 --> 01:14:09,446 Well let's compile this and try to infer what's going on. 1674 01:14:09,446 --> 01:14:13,366 GCC argv 1 dot C okay good stuff happens. 1675 01:14:13,366 --> 01:14:18,066 Okay so A dot out enter okay, not such good stuff. 1676 01:14:18,066 --> 01:14:20,846 Alright so how about Fu bar bas. 1677 01:14:21,606 --> 01:14:24,266 So apparently my program has the capacity 1678 01:14:24,336 --> 01:14:26,056 to spit out anything I type. 1679 01:14:26,326 --> 01:14:29,236 Let me just get a little familiar like A dot 1680 01:14:29,236 --> 01:14:31,846 out Fu dot C. So let's just think of this as burning nano. 1681 01:14:32,576 --> 01:14:36,336 So it looks like also inside of argv is not just the words 1682 01:14:36,466 --> 01:14:38,376 after the program you ran but what else? 1683 01:14:39,536 --> 01:14:43,766 The program itself which is useful, so in short argv all 1684 01:14:43,766 --> 01:14:45,046 of the words the user has typed 1685 01:14:45,046 --> 01:14:47,876 at the command prompt including the program 1686 01:14:47,876 --> 01:14:50,506 or the program name itself from 0 on up. 1687 01:14:50,716 --> 01:14:52,536 So all this program does notice is this. 1688 01:14:52,536 --> 01:14:55,546 It iterates from 0 to argC okay pretty simple. 1689 01:14:55,546 --> 01:14:58,006 It's just integer incrementation 1690 01:14:58,276 --> 01:15:01,946 and then what am I printing well percent S is a new format code 1691 01:15:01,946 --> 01:15:05,126 to here today so this means put a string here not a decimal, 1692 01:15:05,126 --> 01:15:06,406 not a float put a string here. 1693 01:15:06,666 --> 01:15:10,356 Give me the ith word inside 1694 01:15:10,356 --> 01:15:13,116 of argv the ith argument, inside of argv. 1695 01:15:13,116 --> 01:15:15,466 We can get more creative still if I open 1696 01:15:15,466 --> 01:15:20,096 up argv 2 dot C this looks a little fancier 1697 01:15:20,096 --> 01:15:21,926 and we won't dwell so much on the syntax 1698 01:15:22,056 --> 01:15:23,876 because again we have seen things like this. 1699 01:15:24,106 --> 01:15:25,906 Here's a 4 loop iterating over I. 1700 01:15:26,196 --> 01:15:29,316 Here's another 4 loop iterating over J 1701 01:15:29,686 --> 01:15:31,226 but notice what this program does 1702 01:15:31,266 --> 01:15:32,906 if I just reveal the magic here. 1703 01:15:32,976 --> 01:15:38,436 Argv 2 dot C so RA dot out, oh interesting. 1704 01:15:38,436 --> 01:15:40,626 So I'm just getting a littler aesthetically interesting Fu 1705 01:15:40,626 --> 01:15:41,146 bar bas. 1706 01:15:41,896 --> 01:15:44,716 So here I'm iterating not only over each 1707 01:15:44,716 --> 01:15:48,146 of the words the user typed but also over each 1708 01:15:49,076 --> 01:15:51,436 of the characters within. 1709 01:15:51,696 --> 01:15:54,206 So I hinted a moment ago and we'll see this in more detail 1710 01:15:54,206 --> 01:15:57,876 over time but argv is just a 2-dimensional array. 1711 01:15:57,876 --> 01:16:00,936 An array of strings but strings are arrays of Chars 1712 01:16:00,936 --> 01:16:04,806 so that's an array of an array of Chars so how am I doing this? 1713 01:16:05,126 --> 01:16:07,576 Well I iterate over the arguments from 0 to argC 1714 01:16:07,576 --> 01:16:09,596 and then oh here's something familiar. 1715 01:16:09,596 --> 01:16:11,936 Again I've been just taking babysteps over the code today. 1716 01:16:12,156 --> 01:16:14,556 Here's J is 0, N is initialized 1717 01:16:14,556 --> 01:16:18,006 to the string length of ith argument okay. 1718 01:16:18,286 --> 01:16:21,956 Iterate from J to that length, oh this is interesting 1719 01:16:22,286 --> 01:16:24,856 if argv is in fact a 2-dimensional array, 1720 01:16:24,856 --> 01:16:27,866 an array of strings aka an array -- 1721 01:16:27,866 --> 01:16:32,126 an array of Chars it turns out you can put brackets back 1722 01:16:32,436 --> 01:16:35,426 to back if you know it's this sort of 2-dimensional array. 1723 01:16:35,716 --> 01:16:40,016 So argv bracket I right here, this gives me the ith word. 1724 01:16:40,196 --> 01:16:43,236 What does argv bracket I bracket J give me, 1725 01:16:43,796 --> 01:16:47,346 the J character in the ith word. 1726 01:16:47,606 --> 01:16:49,676 So it's actually fairly straightforward. 1727 01:16:49,676 --> 01:16:53,316 Again some new syntax but some fairly basic building blocks. 1728 01:16:53,316 --> 01:16:57,576 So here's the canonical list for now of functions you can use 1729 01:16:57,876 --> 01:17:01,116 that will be used not so much for problem set 1. 1730 01:17:01,116 --> 01:17:03,586 For the most part you've had already the basic building 1731 01:17:03,586 --> 01:17:07,946 blocks for problem set 1 as of last weekend and this Monday. 1732 01:17:08,196 --> 01:17:09,726 But for now before we start looking 1733 01:17:09,726 --> 01:17:11,446 at the code here's a quick reminder 1734 01:17:11,666 --> 01:17:13,926 of what functions are in CS50's library. 1735 01:17:14,166 --> 01:17:17,346 The last one of which is this latest one get string returns 1736 01:17:17,346 --> 01:17:17,856 a string. 1737 01:17:17,856 --> 01:17:20,776 But remember a string can be manipulated 1738 01:17:20,776 --> 01:17:23,146 with this bracket notation as we've been doing. 1739 01:17:23,396 --> 01:17:26,256 Now for the astute 2 at the top I disclaim memory leaks. 1740 01:17:26,676 --> 01:17:29,236 So CS50's library is buggy. 1741 01:17:29,686 --> 01:17:33,226 It intentionally -- we allow ourselves to indulge 1742 01:17:33,226 --> 01:17:35,976 in what's called the memory leak whereby we are actually 1743 01:17:36,106 --> 01:17:38,156 allocating a whole bunch of RAM for you 1744 01:17:38,416 --> 01:17:39,956 and we never ask you to give it back. 1745 01:17:40,446 --> 01:17:44,266 So realize that we'll have a lot of interesting opportunity 1746 01:17:44,266 --> 01:17:46,826 to discuss that but where are we going with this 1747 01:17:46,916 --> 01:17:48,376 and why introduce this at all? 1748 01:17:48,376 --> 01:17:50,576 Well clearly we got to arrays today and that's kind 1749 01:17:50,576 --> 01:17:52,756 of a juicy new feature but we can do this 1750 01:17:52,756 --> 01:17:56,726 in a more interesting context such as cryptography. 1751 01:17:56,876 --> 01:17:58,666 So we introduced Monday with Ralphie. 1752 01:17:58,906 --> 01:18:01,336 We had that expression be sure to drink your Ovaltine, 1753 01:18:01,336 --> 01:18:05,096 which I is repeated here using ROT 13, rotate 13, 1754 01:18:05,256 --> 01:18:07,296 just rotate all the letters 13 places 1755 01:18:07,296 --> 01:18:09,056 and that's the nonsense you get out 1756 01:18:09,286 --> 01:18:11,226 but it was a specific instance 1757 01:18:11,506 --> 01:18:13,936 of something we called a Caesar cipher, 1758 01:18:14,506 --> 01:18:17,226 a cipher being an algorithm or program 1759 01:18:17,226 --> 01:18:18,506 that scrambles information. 1760 01:18:18,906 --> 01:18:20,236 So this is very similar in spirit 1761 01:18:20,236 --> 01:18:21,626 to some very real world things. 1762 01:18:21,666 --> 01:18:23,186 This is the German enigma machine. 1763 01:18:23,186 --> 01:18:24,566 You can read up on this on Wikipedia. 1764 01:18:24,566 --> 01:18:27,226 It's covered in many movies and books certain over the years. 1765 01:18:27,646 --> 01:18:30,456 We've seen real-world incarnations of cryptography. 1766 01:18:30,706 --> 01:18:32,906 This is essentially a really fancy typewriter 1767 01:18:32,906 --> 01:18:36,136 with really special really secret gears and levers 1768 01:18:36,306 --> 01:18:39,566 that when you type out a message what gets spit out is something 1769 01:18:39,566 --> 01:18:41,676 that looks more in spirit like this. 1770 01:18:41,976 --> 01:18:43,256 But the Germans were smarter 1771 01:18:43,256 --> 01:18:45,286 than just rotating all the letters 1772 01:18:45,286 --> 01:18:47,086 in the German alphabet by 13 places. 1773 01:18:47,416 --> 01:18:50,296 That's why the Americans and allies were so intent 1774 01:18:50,296 --> 01:18:53,456 on stealing boxes like this so that they could figure 1775 01:18:53,456 --> 01:18:54,966 out what those messages were. 1776 01:18:54,966 --> 01:18:58,446 But what we'll look at this coming week is this notion 1777 01:18:58,446 --> 01:19:01,936 of cryptography and this sort of a fancy depiction 1778 01:19:02,226 --> 01:19:04,366 that I'll just wave my hand at just so we can look 1779 01:19:04,366 --> 01:19:07,556 at this guy here, Caesar supposedly. 1780 01:19:08,186 --> 01:19:10,916 Naive though this algorithm may seem all these years later, 1781 01:19:11,236 --> 01:19:14,246 use simple cryptography to send messages to 1782 01:19:14,246 --> 01:19:15,646 and from his generals in the field. 1783 01:19:15,966 --> 01:19:18,196 And they did so by way of writing them down on a piece 1784 01:19:18,196 --> 01:19:21,576 of paper and then rotating all of the letters as the story goes 1785 01:19:21,856 --> 01:19:26,756 by some number of places, so that A became D and B became E 1786 01:19:26,756 --> 01:19:29,526 and so forth so he and his generals had a secret 1787 01:19:29,786 --> 01:19:31,766 that they whispered together before they went off 1788 01:19:31,766 --> 01:19:36,836 out to battle and such and he said 13 or something like that. 1789 01:19:37,016 --> 01:19:40,456 Right now it's a fairly simple cipher and even I have sort 1790 01:19:40,456 --> 01:19:43,226 of trouble imaging that there weren't still even back then 1791 01:19:43,226 --> 01:19:45,766 before computers, really smart people who maybe by trial 1792 01:19:45,766 --> 01:19:46,926 and error might have figured 1793 01:19:46,926 --> 01:19:48,346 out how these messages were encrypted. 1794 01:19:48,346 --> 01:19:51,066 But for us it offers a really interesting opportunity, 1795 01:19:51,066 --> 01:19:52,346 because just as I've written -- 1796 01:19:52,346 --> 01:19:55,576 just as this figure presents these letters from left to right 1797 01:19:55,576 --> 01:19:58,236 in a grid, it suggests that just by using a little bit 1798 01:19:58,236 --> 01:20:00,856 of arithmetic can we rotate letters. 1799 01:20:00,856 --> 01:20:03,966 An in the event that we go too far off, if we try adding 13 1800 01:20:03,966 --> 01:20:07,026 to the letter Z we don't want to fall off into no-man's land. 1801 01:20:07,026 --> 01:20:08,766 We want to wrap around back to the letter A 1802 01:20:08,766 --> 01:20:12,146 and what simple building block have we seen whereby we can 1803 01:20:12,146 --> 01:20:12,776 wrap around. 1804 01:20:12,776 --> 01:20:14,276 >> [Inaudible background discussion] 1805 01:20:14,276 --> 01:20:15,986 >> Yeah so the modulo operator. 1806 01:20:15,986 --> 01:20:17,106 So that's where we're going. 1807 01:20:17,296 --> 01:20:20,246 Now all this time throughout today's lecture has Homer 1808 01:20:20,246 --> 01:20:22,976 Simpson been busy at work with his little internet company. 1809 01:20:23,246 --> 01:20:25,426 I thought we would conclude with this 20 second clip 1810 01:20:25,726 --> 01:20:27,696 of how his internet business turned out. 1811 01:20:28,176 --> 01:20:36,956 >> Oh they have the internet on computers now. 1812 01:20:36,956 --> 01:20:37,546 >> Bill Gates is here. 1813 01:20:37,546 --> 01:20:50,616 >> Bill Gates the millionaire computer nerd Bill Gates? 1814 01:20:50,736 --> 01:20:51,176 Oh my God. 1815 01:20:51,176 --> 01:20:52,496 Oh my God. 1816 01:20:52,496 --> 01:20:54,866 Get on a site Marge. 1817 01:20:54,866 --> 01:20:58,636 I don't want this to look like a 2-bit operation. 1818 01:20:58,636 --> 01:20:59,306 [Sound effects] 1819 01:20:59,306 --> 01:21:01,546 >> Mr. Simpson. 1820 01:21:01,546 --> 01:21:08,806 >> You don't look rich. 1821 01:21:08,806 --> 01:21:10,166 >> Don't let the haircut fool you. 1822 01:21:10,646 --> 01:21:14,046 I'm exceedingly wealthy. 1823 01:21:14,046 --> 01:21:16,296 >> Make small chat Marge. 1824 01:21:16,296 --> 01:21:25,006 >> Your internet ad was brought to my attention 1825 01:21:25,576 --> 01:21:30,246 but I can't figure out what 1826 01:21:30,646 --> 01:21:33,326 if anything Compu Global Hypermagenet does. 1827 01:21:33,356 --> 01:21:34,316 So rather than risk competing with you, 1828 01:21:34,346 --> 01:21:35,216 I've decided simply to buy you out. 1829 01:21:35,246 --> 01:21:36,356 >> I put my heart and soul into this business 1830 01:21:36,386 --> 01:21:37,136 and now it's finally paying off. 1831 01:21:37,166 --> 01:21:37,376 We're rich. 1832 01:21:37,406 --> 01:21:38,066 We're richer than astronauts. 1833 01:21:38,096 --> 01:21:38,486 >> Homer be quiet. 1834 01:21:38,516 --> 01:21:38,996 You'll cramp the deal. 1835 01:21:39,026 --> 01:21:40,016 >> I reluctantly accept your proposal. 1836 01:21:40,046 --> 01:21:40,676 >> Well everyone always does. 1837 01:21:40,706 --> 01:21:41,096 >> Buy him out boys. 1838 01:21:41,096 --> 01:21:41,163 [Sound effects] 1839 01:21:41,163 --> 01:21:41,786 >> Hey what the hell's going on? 1840 01:21:41,816 --> 01:21:43,016 >> Well I could get rich by writing a lot of checks. 1841 01:21:43,016 --> 01:21:43,083 [Laughter] 1842 01:21:43,083 --> 01:21:44,006 >> Have a good weekend we'll see you Monday.