1 00:00:09,176 --> 00:00:12,926 >> Welcome to CS50 this is the start of week two 2 00:00:12,926 --> 00:00:15,256 and I actually got an email just this weekend from one 3 00:00:15,256 --> 00:00:18,016 of our own TFs who had been here at last week's lectures 4 00:00:18,016 --> 00:00:20,696 when we talked about some of these more arcane topics, 5 00:00:20,696 --> 00:00:22,706 namely one called typecasting which we'll look 6 00:00:22,706 --> 00:00:24,206 at in more technical detail today, 7 00:00:24,386 --> 00:00:26,086 but his email started as follows. 8 00:00:26,086 --> 00:00:28,566 Hi David, you introduced the concept of typecast, 9 00:00:29,056 --> 00:00:31,586 recall call this as converting a char to an int or an int 10 00:00:31,586 --> 00:00:34,076 to a char, last week, and exemplified the importance 11 00:00:34,076 --> 00:00:36,636 of understanding the types of operators. 12 00:00:36,916 --> 00:00:39,366 In case you want to insist on this point one day, 13 00:00:40,056 --> 00:00:42,196 here's a video of an event that changed my life. 14 00:00:42,196 --> 00:00:45,566 I was 14 years old watching this on live TV so what you're 15 00:00:45,566 --> 00:00:49,826 about to see is a video of ARIAN 5, Flight 501. 16 00:00:49,826 --> 00:00:51,616 This was a European rocket, there were no humans 17 00:00:51,616 --> 00:00:55,496 on board this rocket but at the risk of pulling up YouTube 18 00:00:55,496 --> 00:00:57,186 in class again, let me go ahead 19 00:00:57,186 --> 00:00:59,976 and show you exactly what this teaching fellow is referring to. 20 00:01:00,516 --> 00:01:26,556 [ background noise ] 21 00:01:27,056 --> 00:01:31,446 So there weren't, to be clear, any humans on board 22 00:01:31,496 --> 00:01:34,596 but the explanation for this explosion, which wasn't all 23 00:01:34,596 --> 00:01:36,476 that many years ago, was this. 24 00:01:36,476 --> 00:01:40,076 Because of a different flight path, a data conversion 25 00:01:40,286 --> 00:01:42,376 from a 64 bit floating point value 26 00:01:42,616 --> 00:01:46,396 to a 16 bit signed integer value caused a hardware exception. 27 00:01:46,396 --> 00:01:48,536 More specifically an arithmetic overflow 28 00:01:48,796 --> 00:01:51,386 as the floating point number had a value too large 29 00:01:51,386 --> 00:01:54,926 to be represented by a 16 bit signed integer. 30 00:01:55,326 --> 00:01:57,216 As it was an unmanned flight there were no victims 31 00:01:57,246 --> 00:01:59,136 but the breakup caused the loss 32 00:01:59,136 --> 00:02:02,356 of four cluster mission space craft, resulting in a loss 33 00:02:02,356 --> 00:02:08,656 of more than US $370 million because of one line of code. 34 00:02:08,816 --> 00:02:11,966 So if you find yourselves getting hung up on some 35 00:02:11,966 --> 00:02:13,636 of the esoteric nature of some 36 00:02:13,636 --> 00:02:15,176 of the lower level syntax realize 37 00:02:15,176 --> 00:02:16,416 that these are just building blocks. 38 00:02:16,416 --> 00:02:18,426 They're doing far more interesting things and certainly 39 00:02:18,426 --> 00:02:22,346 when you're actually coding real applications far more important 40 00:02:22,386 --> 00:02:23,766 applications do arise. 41 00:02:24,036 --> 00:02:26,256 So I asked this teaching fellow if he could elaborate on, 42 00:02:26,436 --> 00:02:28,626 you know, why was this actually changed your life? 43 00:02:28,626 --> 00:02:32,056 And he responded, I think that at first I was fascinated 44 00:02:32,056 --> 00:02:34,946 by space and I wanted to engineer rockets and satellites. 45 00:02:35,116 --> 00:02:37,876 To me computers and programs were amazing but a toy. 46 00:02:37,876 --> 00:02:40,386 While the conquest of space was serious stuff. 47 00:02:40,386 --> 00:02:42,876 Progressively however as I was studying engineering 48 00:02:42,876 --> 00:02:45,256 and sciences in high school and then college the memory 49 00:02:45,256 --> 00:02:47,796 of this event helped me realize that software engineering 50 00:02:47,796 --> 00:02:50,046 and computer science were much more noble then I 51 00:02:50,046 --> 00:02:50,886 initially thought. 52 00:02:51,076 --> 00:02:52,396 I think the reasons were three. 53 00:02:52,766 --> 00:02:56,376 One, software is useful enough so that some of it was 54 00:02:56,376 --> 00:02:58,456 in this rocket sent into space. 55 00:02:58,936 --> 00:03:01,586 Two, it is critical enough that a malfunction result 56 00:03:01,586 --> 00:03:04,846 in the rocket exploding and three, 57 00:03:04,846 --> 00:03:06,966 writing software is actually such a craft 58 00:03:07,446 --> 00:03:09,736 that even these rocket engineers from all 59 00:03:09,736 --> 00:03:11,306 around Europe could get it wrong. 60 00:03:11,306 --> 00:03:12,576 It made software engineering 61 00:03:12,576 --> 00:03:16,246 and computer science a whole lot more valuable to my eyes. 62 00:03:16,246 --> 00:03:19,706 He wasn't the only one so we've linked to the Wikipedia article 63 00:03:19,706 --> 00:03:22,566 that discusses the real in depth history of this particular story 64 00:03:22,746 --> 00:03:25,036 but one or part of the article concludes as follows. 65 00:03:25,036 --> 00:03:28,716 Flight 501's high profile disaster brought the high risk 66 00:03:28,716 --> 00:03:30,846 associated with complex computing systems 67 00:03:31,096 --> 00:03:33,146 to the attention of the general public, politicians 68 00:03:33,146 --> 00:03:36,036 and executives resulting in an increased support for research 69 00:03:36,036 --> 00:03:39,606 in ensuring the reliability of safety critical systems 70 00:03:39,606 --> 00:03:42,366 so we appreciate, if nothing else, 71 00:03:42,366 --> 00:03:44,326 as you dive into your own problem sets. 72 00:03:44,326 --> 00:03:47,296 One this week and into future problem sets 73 00:03:47,576 --> 00:03:50,526 that even these small bugs absolutely can recur 74 00:03:50,526 --> 00:03:51,926 in the real world and if you think about some 75 00:03:51,926 --> 00:03:53,476 of the P-Set problems you're working on now, 76 00:03:53,796 --> 00:03:56,606 your code might be this long or this long, a few dozen lines, 77 00:03:56,606 --> 00:03:58,446 maybe 100 at some point very soon, 78 00:03:58,776 --> 00:04:01,426 but if you're finding yourself having a bug with this line 79 00:04:01,556 --> 00:04:03,566 of code, imagine what happens 80 00:04:03,566 --> 00:04:06,356 when you literally have their implement rocket ships 81 00:04:06,646 --> 00:04:09,496 and operating system which are millions of lines of code long. 82 00:04:09,726 --> 00:04:13,306 So thankfully there are tools and there are other languages. 83 00:04:13,486 --> 00:04:15,636 And one of the goals of this course is to introduce you 84 00:04:15,636 --> 00:04:19,106 to progressively better and better approaches to design, 85 00:04:19,106 --> 00:04:20,696 to style, to correctness and so 86 00:04:20,696 --> 00:04:22,776 by semesters end it won't necessarily feel 87 00:04:22,776 --> 00:04:24,946 like you're struggling with these if conditions and loops 88 00:04:24,946 --> 00:04:27,186 so much, you'll actually be thinking the much bigger picture 89 00:04:27,186 --> 00:04:28,816 and that will happen quite soon. 90 00:04:28,816 --> 00:04:30,636 What some of you might have seen as you came 91 00:04:30,636 --> 00:04:35,766 in today is a little something called ASCIImation. 92 00:04:35,856 --> 00:04:38,006 This is free time to the extreme. 93 00:04:38,556 --> 00:04:40,386 This is someone having taken this idea 94 00:04:40,386 --> 00:04:44,276 of ASCII using ASCII characters to actually represent, 95 00:04:44,276 --> 00:04:46,246 not just pictures but an actual movie 96 00:04:46,296 --> 00:04:50,016 so this is his interpretation of the original Star Wars movie. 97 00:04:50,016 --> 00:04:55,126 I bring this up only because in problems that won for problems 98 00:04:55,126 --> 00:04:57,186 that won, well you dabble a little bit in this idea 99 00:04:57,256 --> 00:04:59,756 of ASCII art but realize that as the semester progresses we'll 100 00:04:59,846 --> 00:05:03,766 very soon have more expressive graphical capabilities. 101 00:05:03,846 --> 00:05:07,746 I thought, and speaking of, what you'll find if you haven't poked 102 00:05:07,746 --> 00:05:10,016 around recently on the course's website is you. 103 00:05:10,326 --> 00:05:13,246 So we took all of your answers to Problem Set 0 where one 104 00:05:13,246 --> 00:05:15,826 of the demographic questions, where is your home town, 105 00:05:16,056 --> 00:05:18,166 recall we didn't ask for your home address, just your city, 106 00:05:18,166 --> 00:05:20,256 state and country And what we did was, 107 00:05:20,256 --> 00:05:22,356 we used an off-the-shelf API. 108 00:05:22,706 --> 00:05:25,176 Application Programming interfaces, it's called. 109 00:05:25,416 --> 00:05:27,666 The students last year in fact used this for one 110 00:05:27,666 --> 00:05:29,316 of the tail end problem sets in the course 111 00:05:29,566 --> 00:05:32,006 and we embedded a Google map in the courses website 112 00:05:32,156 --> 00:05:34,126 and then we proceeded to represent each and one 113 00:05:34,126 --> 00:05:35,626 of you with a little marker. 114 00:05:35,626 --> 00:05:37,576 The projector in here is making this a little darker 115 00:05:37,576 --> 00:05:40,606 than it actually is on a real computer but if we zoom 116 00:05:40,606 --> 00:05:45,526 in down here we might see that Dapo is from Lagos, Nigeria. 117 00:05:45,806 --> 00:05:48,316 And if we click on any number of our other folks here, 118 00:05:48,456 --> 00:05:52,706 is he here now, you will see where each and every one 119 00:05:52,706 --> 00:05:54,816 of the 500 of you are from. 120 00:05:54,876 --> 00:05:58,776 So with that said, where are we going? 121 00:05:58,776 --> 00:06:00,106 So we're going to lunch this Friday. 122 00:06:00,106 --> 00:06:02,296 Not all of us but one of the traditions we've started 123 00:06:02,296 --> 00:06:05,596 in this course because its fairly large, is, 124 00:06:05,596 --> 00:06:08,996 on a semi-weekly basis to get to know some 125 00:06:09,036 --> 00:06:10,606 of the teaching fellows, the course assistance, 126 00:06:10,606 --> 00:06:12,456 myself and so we have CS50 lunches. 127 00:06:12,456 --> 00:06:14,596 Either in one of the dining halls or somewhere in the square 128 00:06:14,596 --> 00:06:17,566 and we generally do this by RSVP first come first serve 129 00:06:17,636 --> 00:06:20,586 and its really just an opportunity for 20,30 40 of us 130 00:06:20,586 --> 00:06:22,446 to head somewhere and grab a bite together 131 00:06:22,446 --> 00:06:24,876 so that the form is not on line yet but starting tonight 132 00:06:24,876 --> 00:06:29,686 if you are free this Friday at 1:15ish p.m. which is otherwise 133 00:06:29,686 --> 00:06:31,826 when lecture would be if we still had it on Fridays. 134 00:06:32,116 --> 00:06:36,046 Go to cs50.net/rsvp and think of this as just an opportunity 135 00:06:36,046 --> 00:06:38,656 to chat us up, ask about the course, talk about whatever, 136 00:06:38,656 --> 00:06:41,496 and its sort of very low key and edible office hours. 137 00:06:41,496 --> 00:06:43,836 So we'll do that on occasion if you are interested. 138 00:06:44,146 --> 00:06:45,456 So problems Set 1 is now out. 139 00:06:45,606 --> 00:06:48,236 if you haven't dived in yet that's totally fine. 140 00:06:48,236 --> 00:06:52,626 I take great thought, I have great fondness for those 141 00:06:52,626 --> 00:06:56,316 who do dive in at 7:01 PM on Friday nights much 142 00:06:56,316 --> 00:06:57,666 like myself back in the day. 143 00:06:57,886 --> 00:06:59,636 But just a couple of comments on this. 144 00:06:59,736 --> 00:07:02,376 So last week got very arc and quickly. 145 00:07:02,376 --> 00:07:04,956 For those of you without prior programming backgrounds all 146 00:07:04,956 --> 00:07:06,756 of a sudden we had this blinking prompt and a black 147 00:07:06,756 --> 00:07:10,136 and white terminal and I was banging out code fairly rapidly 148 00:07:10,346 --> 00:07:12,656 and was fairly out of context most likely for all of you. 149 00:07:12,656 --> 00:07:15,106 so realize, if you walked out of last week thinking, wow, 150 00:07:15,436 --> 00:07:18,346 scratch just got harder, well realize once you actually sit 151 00:07:18,346 --> 00:07:20,546 down with this week's pdf and walk yourself 152 00:07:20,546 --> 00:07:22,096 through the check boxes, well you find 153 00:07:22,276 --> 00:07:25,156 that it actually all very quite accessible and you get familiar 154 00:07:25,156 --> 00:07:27,546 with the new interface soon, we'll see different interfaces 155 00:07:27,546 --> 00:07:30,516 over time and so let me make and emphatic point here too. 156 00:07:30,516 --> 00:07:32,426 Especially as you do this P-Set, 157 00:07:32,726 --> 00:07:34,666 if you are finding yourself a bit overwhelmed 158 00:07:34,666 --> 00:07:36,716 or if you're just feeling like there's a disconnect 159 00:07:36,716 --> 00:07:37,876 between what you heard in lecture 160 00:07:37,876 --> 00:07:39,856 and what your expected to do, talk to us. 161 00:07:39,856 --> 00:07:41,956 Talk to me, drop me a not, attend office hours, 162 00:07:41,956 --> 00:07:43,826 there are any number of resources and at the end 163 00:07:43,826 --> 00:07:46,806 of the day, please, if you're on that point thinking, damn, 164 00:07:46,806 --> 00:07:49,316 I really should not have signed that study card with this course 165 00:07:49,316 --> 00:07:51,096 on it, at least have a conversation with me. 166 00:07:51,096 --> 00:07:53,016 I would love to be your barrier 167 00:07:53,016 --> 00:07:55,796 between exiting the course altogether and just kind 168 00:07:56,096 --> 00:07:57,976 of sneaking away thinking this isn't for you. 169 00:07:57,976 --> 00:08:00,066 we can make it work for you. 170 00:08:00,326 --> 00:08:01,856 so what is this P-Set all about? 171 00:08:02,406 --> 00:08:05,436 well there's essentially three noteworthy parts in each 172 00:08:05,436 --> 00:08:07,416 of the addition and in the standard addition you are 173 00:08:07,416 --> 00:08:10,726 introduced to the candy machine that is free in the lounge 174 00:08:10,896 --> 00:08:13,296 in Maxwell Dworkin which you are welcome to start frequenting. 175 00:08:13,296 --> 00:08:16,166 We'll occasionally hold some office hours there and hang out 176 00:08:16,166 --> 00:08:19,316 but the challenge here is really just to get your hands dirty 177 00:08:19,396 --> 00:08:22,606 with Linux and with C. And what you'll find in, 178 00:08:22,606 --> 00:08:25,956 even though the problem is relatively simple in concept, 179 00:08:26,216 --> 00:08:29,226 we challenge you to implement a program that allows you, 180 00:08:29,226 --> 00:08:33,806 the human, to guess how many Skittles are in a candy machine. 181 00:08:34,086 --> 00:08:37,636 You simulate this candy machine using some pseudo-random numbers 182 00:08:37,636 --> 00:08:38,806 and we walk you through the process 183 00:08:38,806 --> 00:08:41,716 of generating pseudo-random numbers in a computers memory 184 00:08:42,006 --> 00:08:43,726 and then ultimately you, the human, gets to interact 185 00:08:43,956 --> 00:08:46,176 with your own program and for those of you 186 00:08:46,176 --> 00:08:48,516 who have prior backgrounds you'll realize this is really 187 00:08:48,516 --> 00:08:50,216 just a warm up exercise if nothing else 188 00:08:50,216 --> 00:08:53,006 to get you acquainted with the new environment, the cloud, 189 00:08:53,006 --> 00:08:55,786 with the new language, C. but then do the problems 190 00:08:55,786 --> 00:08:58,746 to become a little more sophisticated. 191 00:08:58,746 --> 00:09:01,396 so for instance, the next question asks you to take 192 00:09:01,396 --> 00:09:02,956 on the role of a cashier. 193 00:09:03,276 --> 00:09:05,916 So it turns out that in the United States and in Europe 194 00:09:05,916 --> 00:09:09,316 and in other countries the currency systems are designed 195 00:09:09,316 --> 00:09:14,086 in such a way that very naively can a cashier at a store, 196 00:09:14,286 --> 00:09:16,556 if they have 20s and 10s and 5s 197 00:09:16,556 --> 00:09:19,866 or the similarly labeled quarters, nickels and dimes, 198 00:09:19,866 --> 00:09:21,436 if they're all in sorted order 199 00:09:21,746 --> 00:09:25,146 that cashier can very mindlessly hand people change 200 00:09:25,326 --> 00:09:27,236 by just starting with the biggest denomination 201 00:09:27,236 --> 00:09:30,436 in the drawer and working his or her way on down, 202 00:09:30,436 --> 00:09:32,966 handing the person the amount of change they're due 203 00:09:33,266 --> 00:09:34,836 and this is an algorithms that's just generally thought 204 00:09:34,836 --> 00:09:35,736 of as greedy. 205 00:09:35,736 --> 00:09:38,416 In other words if you literally take the biggest bill possible, 206 00:09:38,586 --> 00:09:41,556 a 20 but if you owe them say 30 buck you obviously don't want 207 00:09:41,556 --> 00:09:42,326 to take another 20. 208 00:09:42,376 --> 00:09:44,946 you then take a slightly smaller bite but a big one, a ten. 209 00:09:45,366 --> 00:09:48,956 Do you ultimately hand that human being the fewest number 210 00:09:48,956 --> 00:09:50,846 of bills or the fewest number of coins. 211 00:09:50,846 --> 00:09:52,926 So what's nice is when greedy algorithms 212 00:09:52,926 --> 00:09:56,316 which are generally easy conceptually and to implement 213 00:09:56,316 --> 00:09:58,246 and to execute what's really nicest is 214 00:09:58,246 --> 00:09:59,866 when they're also optimal. 215 00:09:59,896 --> 00:10:01,946 But as you'll see in a moment for the hacker version 216 00:10:01,946 --> 00:10:04,486 of this problem set we throw a wrench into the works 217 00:10:04,486 --> 00:10:06,296 and you don't necessarily have all of the bills 218 00:10:06,296 --> 00:10:07,216 and coins that you need. 219 00:10:07,216 --> 00:10:09,736 So in this program here you are asked, 220 00:10:10,066 --> 00:10:12,306 you implement its program, that has to figure 221 00:10:12,306 --> 00:10:14,876 out how many quarters, how many dimes, how many nickels 222 00:10:14,876 --> 00:10:17,476 and how many pennies to hand someone given a finite amount 223 00:10:17,476 --> 00:10:18,606 of change and its going to reduce some 224 00:10:18,606 --> 00:10:20,306 of those basic building blocks from scratch. 225 00:10:20,306 --> 00:10:22,826 So some if conditions, some subtraction, 226 00:10:22,826 --> 00:10:25,296 maybe some variables but this too will get you familiar 227 00:10:25,296 --> 00:10:27,496 with this new environment and this new language 228 00:10:27,496 --> 00:10:30,096 and then we thought we'd try to get trendy and so we pulled up, 229 00:10:30,096 --> 00:10:33,816 isawyouharvard.com, the final project from CS50 last year, 230 00:10:34,016 --> 00:10:35,246 and we thought, wow, this is perfect. 231 00:10:35,246 --> 00:10:38,726 This is a graph, this happens to represent who is spotting whom 232 00:10:38,726 --> 00:10:40,876 on campus based on the data she's collected, 233 00:10:41,066 --> 00:10:43,686 and its really nice and really simple bar graph 234 00:10:43,686 --> 00:10:45,116 that we can plot so we're not going 235 00:10:45,116 --> 00:10:48,606 to actually use the same API that Tej is using, which happens 236 00:10:48,606 --> 00:10:50,146 to come from Google, it too is free, 237 00:10:50,146 --> 00:10:51,856 just like the maps we looked at a moment ago 238 00:10:52,136 --> 00:10:54,366 but we can simulate this using ASCII art. 239 00:10:54,366 --> 00:10:58,406 Using characters on the keyboard to give a decent approximation 240 00:10:58,686 --> 00:11:01,286 of what a bar graph is and so here will you be asked 241 00:11:01,616 --> 00:11:04,906 to ask the user for the number of people spotting whom, 242 00:11:04,906 --> 00:11:08,206 based on gender and then you'll plot this graph horizontally 243 00:11:08,426 --> 00:11:10,146 and this is clearly going to involve some kind 244 00:11:10,146 --> 00:11:13,106 of looping structure, some conditions, maybe a bit of math 245 00:11:13,226 --> 00:11:15,836 because we specified too how big the users screen is going to be 246 00:11:15,836 --> 00:11:16,826 so you're going to have to figure 247 00:11:16,826 --> 00:11:18,386 out the proportions accordingly. 248 00:11:18,666 --> 00:11:21,196 Now for those of you who decide to tackle the hacker addition 249 00:11:21,196 --> 00:11:23,756 of this P-Set and realized this is generally the minority 250 00:11:23,756 --> 00:11:24,686 of the students in the class. 251 00:11:24,776 --> 00:11:27,916 Anyone is comfortable, less comfortable more comfortable 252 00:11:27,916 --> 00:11:28,856 like is welcome to do this. 253 00:11:28,856 --> 00:11:30,456 It doesn't matter what you declared yourself as, 254 00:11:30,796 --> 00:11:31,746 you can decide week to week. 255 00:11:31,986 --> 00:11:34,276 We have the same warm up exercise involving some candy 256 00:11:34,276 --> 00:11:36,966 for you but then we hand you credit card numbers. 257 00:11:37,296 --> 00:11:38,456 So in the second portion 258 00:11:38,456 --> 00:11:40,696 of the hacker addition are you handed a definition 259 00:11:40,696 --> 00:11:42,346 of what it means to be a credit card number. 260 00:11:42,626 --> 00:11:44,576 It turns out these things have patterns 261 00:11:44,736 --> 00:11:46,026 and there are algorithms, 262 00:11:46,156 --> 00:11:47,926 procedures with which you can verify 263 00:11:47,926 --> 00:11:50,276 if credit card number is legit or not. 264 00:11:50,516 --> 00:11:51,156 And so in this part 265 00:11:51,156 --> 00:11:54,376 of the problem set you implement a validity checker 266 00:11:54,446 --> 00:11:57,236 for credit cards to actually ascertain yes or no, 267 00:11:57,516 --> 00:12:00,446 this is a credit card or this is just some random sequence 268 00:12:00,446 --> 00:12:02,406 of numbers that someone is trying to use to pay 269 00:12:02,406 --> 00:12:03,736 for something on the phone. 270 00:12:03,986 --> 00:12:05,346 Toward the tail end 271 00:12:05,346 --> 00:12:07,556 of the hacker addition do you implement the same idea 272 00:12:07,556 --> 00:12:09,746 of graphing with some ASCII art this chart 273 00:12:10,046 --> 00:12:14,076 but this chart we compel you to rotate it 90 degrees. 274 00:12:14,076 --> 00:12:16,336 You actually have to make a vertical bar chart which, 275 00:12:16,336 --> 00:12:18,916 if you think about how you would implement this in screen, 276 00:12:18,916 --> 00:12:20,696 thus far we've only seen PrinTif. 277 00:12:20,846 --> 00:12:23,196 Of printing to the screen and PrinTif's gotcha is 278 00:12:23,196 --> 00:12:25,456 that it pretty much prints line by line by line 279 00:12:25,666 --> 00:12:27,356 which lends itself to horizontal bars. 280 00:12:27,356 --> 00:12:28,846 Print a row, print a row. 281 00:12:28,846 --> 00:12:32,026 But if now these bars are stacked up high you're going 282 00:12:32,026 --> 00:12:33,886 to have to be a little more clever when it comes 283 00:12:33,916 --> 00:12:36,766 to figuring out, when do you draw this bar versus this bar 284 00:12:36,816 --> 00:12:39,976 and that will be the hacker addition this week. 285 00:12:39,976 --> 00:12:42,796 So those pdfs are on line and just FYI, if you were one 286 00:12:42,796 --> 00:12:45,176 of those like me who downloaded the pdf for hacker 287 00:12:45,306 --> 00:12:48,596 at 7:01 PM do redown load it because I screwed 288 00:12:48,596 --> 00:12:49,596 up in the middle of the night 289 00:12:49,596 --> 00:12:52,106 and I made the bars the wrong shape in the sample output 290 00:12:52,106 --> 00:12:53,236 so grab the latest pdf. 291 00:12:53,746 --> 00:12:55,756 So what exists in the way of support? 292 00:12:55,756 --> 00:12:57,136 So we've echoed this multiple times. 293 00:12:57,136 --> 00:12:59,476 Helpatcs50.net is manned by the CS 50 staff 294 00:12:59,476 --> 00:13:03,546 and our response time tend to be remarkably fast. 295 00:13:03,546 --> 00:13:08,206 But in the interest of economies of scale, 296 00:13:08,236 --> 00:13:11,216 what we also released this weekend is the courses 297 00:13:11,216 --> 00:13:13,386 discussion forms or bulletin boards. 298 00:13:13,556 --> 00:13:16,376 There is certainly redundancy in the types of questions we get 299 00:13:16,376 --> 00:13:18,756 because there are absolutely some frequently asked questions 300 00:13:19,026 --> 00:13:20,656 so on the course's home page now 301 00:13:20,656 --> 00:13:22,246 on the left hand side will you find the link 302 00:13:22,526 --> 00:13:26,866 to discussion aka help.cs50.net any time thenceforth you have a 303 00:13:26,866 --> 00:13:29,756 question that you think probably someone else might have 304 00:13:29,756 --> 00:13:31,846 and its not something that's particularly personal 305 00:13:32,016 --> 00:13:35,486 or particularly unique to your own code, absolutely turn 306 00:13:35,486 --> 00:13:37,216 to the bulletin board first for two reasons. 307 00:13:37,216 --> 00:13:40,056 One, to check if someone else has asked the same question, 308 00:13:40,056 --> 00:13:41,576 its nicely browzeable and searchable, 309 00:13:41,776 --> 00:13:44,156 you can save yourself sometime, get an immediate response or, 310 00:13:44,156 --> 00:13:47,346 two, post the question if you think it might benefit others 311 00:13:47,346 --> 00:13:50,056 and we spell out in the main page 312 00:13:50,056 --> 00:13:52,866 of the discussion forms exactly where that line is 313 00:13:52,866 --> 00:13:55,886 between oversharing and asking the right kind of questions. 314 00:13:55,886 --> 00:13:58,986 Posting a few lines of your code saying here I'm having trouble 315 00:13:58,986 --> 00:14:01,016 figuring this out, that's probably fine, 316 00:14:01,016 --> 00:14:04,116 saying here's my program, doesn't work, help, too much. 317 00:14:04,186 --> 00:14:07,176 The idea is to actually engage us more privately 318 00:14:07,176 --> 00:14:09,046 with such things hence the email address. 319 00:14:09,396 --> 00:14:12,736 So let me for just one moment put on my scary voice now 320 00:14:13,406 --> 00:14:14,846 that we're at the start of the semester. 321 00:14:15,256 --> 00:14:17,146 So the course takes very seriously issues 322 00:14:17,146 --> 00:14:18,276 of academic honesty. 323 00:14:18,276 --> 00:14:20,846 It is very clearly spelled out in the courses syllabus 324 00:14:20,846 --> 00:14:23,046 and for your convenience we include it on the second page 325 00:14:23,046 --> 00:14:24,986 of every single problems set throughout the semester. 326 00:14:24,986 --> 00:14:28,446 It explains as best we can where that line is 327 00:14:28,446 --> 00:14:31,266 between collaborating and working and learning and working 328 00:14:31,266 --> 00:14:33,026 with and learning from your classmates 329 00:14:33,026 --> 00:14:35,716 which is absolutely encouraged and where that line is 330 00:14:35,766 --> 00:14:38,696 between actually taking their ideas, taking their code 331 00:14:38,696 --> 00:14:40,406 and then presenting it as your own. 332 00:14:40,406 --> 00:14:42,496 as you'll see in the syllabus and do read this paragraph 333 00:14:42,496 --> 00:14:45,206 if you haven't already, the line essentially boils 334 00:14:45,206 --> 00:14:47,806 down to its absolutely okay to talk with your friends, 335 00:14:47,836 --> 00:14:48,936 talk with people in your section, 336 00:14:48,936 --> 00:14:50,256 stand up at a white board together, 337 00:14:50,256 --> 00:14:52,216 sort of brainstorming ideas, writing things 338 00:14:52,216 --> 00:14:55,256 down in pseudo code and tackling the problems conceptually 339 00:14:55,256 --> 00:14:57,816 together but where we draw the line in the syllabus is, 340 00:14:58,306 --> 00:15:01,376 don't go to the point of talking in exact C code. 341 00:15:01,376 --> 00:15:02,996 Don't look at the person's screen 342 00:15:02,996 --> 00:15:05,736 and certainly don't say hey do you mind sending me your code? 343 00:15:06,076 --> 00:15:08,006 To be honest, almost every time there are instances 344 00:15:08,006 --> 00:15:10,046 of academic dishonesty in this course its not 345 00:15:10,046 --> 00:15:13,006 because it was this fine line between oversharing, 346 00:15:13,006 --> 00:15:16,466 talking a little too much, it was 2 a.m. and someone decided 347 00:15:16,606 --> 00:15:18,626 to copy the code from their friends computer, 348 00:15:18,806 --> 00:15:21,556 their friend made the mistake of actually sending them their code 349 00:15:21,556 --> 00:15:24,086 in poor judgment and its those kinds of issues, 350 00:15:24,086 --> 00:15:26,176 so realize its absolutely the type of course 351 00:15:26,176 --> 00:15:28,746 where it's a community, you're certainly encouraged to learn 352 00:15:28,746 --> 00:15:31,706 from and work with each other but do mind that line 353 00:15:31,706 --> 00:15:33,436 and in conclusion in scary voice, 354 00:15:33,436 --> 00:15:33,976 I made a little animation. 355 00:15:34,516 --> 00:15:43,936 [ background noise ] 356 00:15:44,436 --> 00:15:48,186 So that is the number of people this course has add boarded 357 00:15:48,326 --> 00:15:49,866 over the past several years. 358 00:15:50,106 --> 00:15:53,446 recent years so this is perhaps more effective I can say 359 00:15:53,446 --> 00:15:55,986 verbally so realize we are computer scientists. 360 00:15:55,986 --> 00:15:57,216 Realize we retain the archives 361 00:15:57,216 --> 00:15:58,916 of all past problem sets that are submitted. 362 00:15:58,916 --> 00:16:00,126 Realize we have the capabilities 363 00:16:00,126 --> 00:16:01,696 to cross check every problem set submitted 364 00:16:01,696 --> 00:16:04,316 against every problem set submitted and we're very good 365 00:16:04,316 --> 00:16:07,296 at what we do and it gives us no pleasure to escalate things 366 00:16:07,296 --> 00:16:09,626 to the administrative board but we do it in fairness 367 00:16:09,626 --> 00:16:11,926 to your classmates who are really giving it their all, 368 00:16:11,986 --> 00:16:14,566 so late days exist for reason. 369 00:16:14,566 --> 00:16:16,226 When you're at that point at 2 a.m. 370 00:16:16,226 --> 00:16:18,056 when your feeling yourself stressed and you've got 371 00:16:18,056 --> 00:16:20,936 so much going on in your life, honestly that's when you cash 372 00:16:20,936 --> 00:16:23,396 in the late day, go to bed or worst case reach out to me 373 00:16:23,396 --> 00:16:26,156 or Jan Sorukki and we'll work something out if you're feeling 374 00:16:26,156 --> 00:16:30,226 that temptation that frequently happens with stress 375 00:16:30,226 --> 00:16:32,876 and we have these mechanisms in place so you can avoid it. 376 00:16:33,236 --> 00:16:36,986 so hello world was the first program that we began with, 377 00:16:37,076 --> 00:16:38,466 I have to make this awkward transition now 378 00:16:38,966 --> 00:16:42,936 to happy time again, so we began looking in quite detail 379 00:16:43,316 --> 00:16:45,606 at first scratch and then see and it turns 380 00:16:45,606 --> 00:16:47,966 out you can do a bunch of things in all sorts of languages 381 00:16:47,966 --> 00:16:50,296 and just so that you've realized that the ideas we're talking 382 00:16:50,296 --> 00:16:52,956 about now are absolutely applicable to other languages 383 00:16:52,956 --> 00:16:54,576 and event the tail end of the same course, 384 00:16:54,896 --> 00:16:58,056 what I thought I would do is just grab a couple of snippets 385 00:16:58,056 --> 00:17:01,066 of code, and actually let me not take for granted this work flow. 386 00:17:01,066 --> 00:17:01,726 So I'm on a Mac. 387 00:17:02,256 --> 00:17:05,766 I decide its time to connect to the cloud.cs50.net so I open 388 00:17:05,766 --> 00:17:07,346 on my Mac a program called terminal, 389 00:17:07,636 --> 00:17:09,686 I can zoom in on my font size here, 390 00:17:09,996 --> 00:17:13,166 I'm going to type the command as the problem set 1 walks you 391 00:17:13,166 --> 00:17:17,786 through cloud.cs50.net, I'm going to hit enter, 392 00:17:18,086 --> 00:17:19,696 type in my password and bam. 393 00:17:19,696 --> 00:17:23,586 Now I'm connected from my Mac to the remote server. 394 00:17:23,586 --> 00:17:25,986 For those of you with pc's realize the problems 395 00:17:25,986 --> 00:17:28,776 that actually walks you through the same process, you will see 396 00:17:28,776 --> 00:17:31,286 as the P-Set directs there are some how-tos which is sort 397 00:17:31,286 --> 00:17:34,106 of geek speak for tutorials, user manuals or what not 398 00:17:34,426 --> 00:17:36,276 for both terminal which I just used 399 00:17:36,276 --> 00:17:38,976 and If your finding this a little too fast realize we took 400 00:17:38,976 --> 00:17:41,596 nice screenshots and walked you through the exact same process 401 00:17:41,646 --> 00:17:44,526 and sequence of steps here and just so you've seen it before, 402 00:17:44,526 --> 00:17:47,256 the half of some of you with PCs realize 403 00:17:47,286 --> 00:17:49,266 that putty is a free program 404 00:17:49,266 --> 00:17:52,876 that you can download off the internet, it is an SSH client, 405 00:17:53,146 --> 00:17:54,186 it looks a little different, 406 00:17:54,186 --> 00:17:57,426 it doe have a more interesting guy graphical user interface, 407 00:17:57,676 --> 00:17:59,566 but at the end of the day once you follow this sequence 408 00:17:59,566 --> 00:18:01,676 of steps you end up seeing the same thing. 409 00:18:01,676 --> 00:18:02,986 Font might look a little different 410 00:18:03,216 --> 00:18:04,766 but the result is actually the same. 411 00:18:04,766 --> 00:18:07,356 and those of you running Linux probably know how to do this 412 00:18:07,356 --> 00:18:10,966 with your own computers anyway but let us know if not. 413 00:18:11,486 --> 00:18:11,646 Yeah? 414 00:18:11,646 --> 00:18:12,386 >> [inaudible] 415 00:18:12,386 --> 00:18:18,426 >> Why did I tell them that? 416 00:18:18,426 --> 00:18:20,556 Oh so why did I tell the Mac people 417 00:18:20,556 --> 00:18:22,076 to change your font colors and background? 418 00:18:22,266 --> 00:18:25,096 So when you read through this tutorial, we strongly suggest 419 00:18:25,096 --> 00:18:28,336 that you configure terminal or configure PuTTy in a certain way 420 00:18:28,606 --> 00:18:31,076 so they graphically things look the same in terms 421 00:18:31,076 --> 00:18:32,906 of color coding, syntax highlighting, 422 00:18:33,216 --> 00:18:34,446 so things look a little prettier. 423 00:18:34,766 --> 00:18:38,206 You do that on your Mac but those setting then sort 424 00:18:38,206 --> 00:18:40,116 of put a lens in front of the server 425 00:18:40,116 --> 00:18:42,076 through which you look at your code. 426 00:18:42,076 --> 00:18:44,896 So we just change the colors locally but the effect is 427 00:18:44,896 --> 00:18:47,236 when you look at remote things, they too are different colors. 428 00:18:47,786 --> 00:18:48,066 That's all. 429 00:18:48,496 --> 00:18:50,306 Is it necessary? 430 00:18:50,306 --> 00:18:53,376 No, if you prefer just looking at the more generic white screen 431 00:18:53,376 --> 00:18:55,836 that certainly fine and even I, in lecture today am switching 432 00:18:55,836 --> 00:18:58,596 to white for readability sake and its really to each his 433 00:18:58,596 --> 00:19:01,776 or her own, its up to you, but the structure is there. 434 00:19:01,776 --> 00:19:05,746 so I am on the cloud, I'm going to go ahead into source, 435 00:19:05,746 --> 00:19:09,106 my source directory which you have print outs of today 436 00:19:09,386 --> 00:19:11,856 and what I thought I'd do is open up, for instance this, 437 00:19:11,856 --> 00:19:13,556 which might make some of you feel more at home. 438 00:19:13,836 --> 00:19:16,306 So this is hi.java. 439 00:19:16,566 --> 00:19:18,996 so stupid name for a program but this is a canonical program 440 00:19:18,996 --> 00:19:21,196 in Java, some of you already know what this looks like, 441 00:19:21,476 --> 00:19:24,006 if you've ever wondered what Java's source code looks like, 442 00:19:24,396 --> 00:19:25,406 it looks like this. 443 00:19:25,626 --> 00:19:28,416 There's a few different key, oh sorry, I didn't print this one 444 00:19:28,416 --> 00:19:31,246 out because its so useless for moving forward. 445 00:19:31,566 --> 00:19:33,166 No, not as a language, just as an example. 446 00:19:34,896 --> 00:19:37,506 You'll see that the syntax kind of similar in spirit, 447 00:19:37,506 --> 00:19:39,426 there's a mention of main, there's parenthesis, 448 00:19:39,426 --> 00:19:40,936 there's semi colons, there's closed quotes 449 00:19:41,126 --> 00:19:42,346 but there's some other jargon there. 450 00:19:42,346 --> 00:19:45,616 there's the word class, there's some additional bracket syntax 451 00:19:45,866 --> 00:19:47,626 here so just realize at the end 452 00:19:47,626 --> 00:19:51,096 of the day this program too get compiled in a similar way. 453 00:19:51,336 --> 00:19:54,656 Instead of running gcc, someone writing this program would run 454 00:19:54,656 --> 00:19:58,656 Javachi.java and then what you would do is run Java 455 00:19:58,656 --> 00:20:02,686 on this program hi and there is in fact the same program. 456 00:20:02,686 --> 00:20:07,276 I think I have another one here in, let's say, hi.php, nope, 457 00:20:07,276 --> 00:20:15,456 that's not it, okay so here's perhaps the simplest php program 458 00:20:15,626 --> 00:20:18,476 you can write and we'll be using php toward the town 459 00:20:18,476 --> 00:20:20,886 of the semester to actually implement things 460 00:20:20,886 --> 00:20:25,986 like isawyouharvard.com or any web site based application you'd 461 00:20:25,986 --> 00:20:28,526 like to make you can kind of infer how this thing works. 462 00:20:28,526 --> 00:20:30,566 Echo, sounds like its pretty similar to print, 463 00:20:30,636 --> 00:20:33,506 the n are familiar, the, are the same. 464 00:20:33,736 --> 00:20:34,936 There's some new syntax there 465 00:20:34,936 --> 00:20:36,236 with the open bracket and then the ? 466 00:20:36,236 --> 00:20:39,816 but again the point of showing you these little teasers 467 00:20:39,816 --> 00:20:43,126 of other languages is that once you have a solid foundation 468 00:20:43,126 --> 00:20:45,736 in one language, maybe two languages or different types 469 00:20:45,736 --> 00:20:48,326 of languages as might be covered in this course and follow 470 00:20:48,326 --> 00:20:50,996 on courses, you can very easily bootstrap yourself 471 00:20:50,996 --> 00:20:51,926 to other environment. 472 00:20:51,926 --> 00:20:54,426 To be honest, I learned in 50, the language C, 473 00:20:54,646 --> 00:20:56,666 I took CS51 the next semester 474 00:20:56,666 --> 00:21:00,066 and learned the language called lisp and C++ at the time 475 00:21:00,276 --> 00:21:01,886 and that was it for my formal introduction 476 00:21:01,886 --> 00:21:02,806 to programming languages. 477 00:21:02,806 --> 00:21:04,326 Every other language I've learned 478 00:21:04,376 --> 00:21:07,246 since has been completely on my own and from friends 479 00:21:07,246 --> 00:21:08,406 and from projects and the likes 480 00:21:08,406 --> 00:21:10,926 so realize its very empowering just getting your foot 481 00:21:10,926 --> 00:21:11,746 in that door. 482 00:21:11,866 --> 00:21:13,906 Well what about this issue of imprecision? 483 00:21:14,236 --> 00:21:19,466 So let me go ahead and whip up a program like, let me take a look 484 00:21:19,466 --> 00:21:22,766 at this for one sec, let's do, yeah. 485 00:21:22,886 --> 00:21:25,276 So I'm going to go ahead and create with nano, 486 00:21:25,416 --> 00:21:28,326 imprecision.c this is not among your print outs 487 00:21:28,326 --> 00:21:30,296 because we're doing it in real time, first thing I'm going 488 00:21:30,296 --> 00:21:33,466 to start off with is include standardio.h 489 00:21:33,466 --> 00:21:34,206 and what's the point of that? 490 00:21:34,386 --> 00:21:35,096 Quick sanity check? 491 00:21:35,636 --> 00:21:39,656 It will give me PrinTif so I can actually interact 492 00:21:39,746 --> 00:21:43,256 with standard input the keyboard and output the screen such as 493 00:21:43,316 --> 00:21:44,996 through printf, I'm going to say int 494 00:21:45,146 --> 00:21:47,916 and we'll explain eventually why you say int, main, 495 00:21:47,916 --> 00:21:49,626 I'm going to say void because main 496 00:21:49,626 --> 00:21:51,196 at the moment does not take any arguments 497 00:21:51,196 --> 00:21:53,076 but we'll soon peel back that layer and I'm going 498 00:21:53,076 --> 00:21:54,906 to get myself ready to start coding 499 00:21:55,186 --> 00:21:57,456 by putting the curly braces there in advance. 500 00:21:57,676 --> 00:21:59,976 So now I'm going to do a couple of things. 501 00:21:59,976 --> 00:22:03,586 I'm going to say float, to get a floating point variable, 502 00:22:03,586 --> 00:22:07,926 x gets let's say .088 which just so happens 503 00:22:07,926 --> 00:22:09,006 to be a floating point value one 504 00:22:09,006 --> 00:22:11,446 of your classmates mentioned before class 505 00:22:11,446 --> 00:22:13,146 when we were chatting so it's a perfect example, 506 00:22:13,146 --> 00:22:13,916 we'll go with that one. 507 00:22:14,166 --> 00:22:20,546 Let's say float y= and I have a .01 and now float z is going 508 00:22:20,546 --> 00:22:24,556 to equal X plus y. so now to the astute, the mathematicians 509 00:22:24,556 --> 00:22:28,636 in the room, this value of z is now .89, at least it should be. 510 00:22:28,876 --> 00:22:31,156 Now let's make this more relatable to the actual piece 511 00:22:31,156 --> 00:22:33,386 at hand, this is like change so one of the problems 512 00:22:33,386 --> 00:22:34,986 in that problem set is again change making 513 00:22:34,986 --> 00:22:37,216 where you have quarters which might be .25, 514 00:22:37,526 --> 00:22:41,196 dimes which might be .10, pennies which might be .01 515 00:22:41,436 --> 00:22:43,336 so surely there's an application already 516 00:22:43,336 --> 00:22:45,586 for floating point values so suppose I'm trying 517 00:22:45,586 --> 00:22:49,346 to add 88 cents and 1 cent here to get 89 cents 518 00:22:49,346 --> 00:22:50,646 and then display this value. 519 00:22:50,646 --> 00:22:51,586 Well let's do that. 520 00:22:51,586 --> 00:22:58,186 Let's do PrinTif % f for the floating point format string 521 00:22:58,186 --> 00:23:00,656 if you don't remember these as an aside one 522 00:23:00,656 --> 00:23:03,736 of the best resources early on if we don't mention things 523 00:23:03,736 --> 00:23:07,086 in the problems set at hand if you go under C 524 00:23:07,456 --> 00:23:10,506 in the resources page of the courses web site one 525 00:23:10,506 --> 00:23:12,766 of the best references here is this thing called c reference 526 00:23:13,056 --> 00:23:15,096 and all you have on the left hand side are links 527 00:23:15,166 --> 00:23:17,956 to the categories of functions that come built into C 528 00:23:18,236 --> 00:23:19,426 so for instance if I'm dealing 529 00:23:19,426 --> 00:23:22,356 with PrinTif you should probably know intuitively now it has 530 00:23:22,396 --> 00:23:23,776 something to do with IO right? 531 00:23:23,776 --> 00:23:26,946 because it standard IO so if I follow this link and then scroll 532 00:23:26,946 --> 00:23:29,416 down I'll see a whole bunch of functions we've not looked at 533 00:23:29,416 --> 00:23:31,436 yet and we're not going to bother looking at all of these, 534 00:23:31,436 --> 00:23:33,876 you pick them up as you go but there in fact is printf. 535 00:23:34,226 --> 00:23:36,056 So here is some nice documentation 536 00:23:36,326 --> 00:23:38,006 with all the format codes for printf, 537 00:23:38,366 --> 00:23:40,716 there's some example code on this website which is why we 538 00:23:40,716 --> 00:23:44,446 like it so this should help get you going as well if you feel 539 00:23:44,446 --> 00:23:46,656 that you didn't quite see the right approach 540 00:23:46,976 --> 00:23:47,796 in say, some class. 541 00:23:48,066 --> 00:23:50,386 So here we go percent F, let me do a new line F 542 00:23:50,386 --> 00:23:52,216 so that things don't blend onto the multiple, 543 00:23:52,876 --> 00:23:56,546 don't stay on the same line, ; let me go ahead and quit 544 00:23:56,546 --> 00:24:00,476 with control X, Y for yes, enter for save and now 545 00:24:00,476 --> 00:24:02,076 to compile this I've got a few options, 546 00:24:02,076 --> 00:24:03,636 I'm going to start getting more efficient 547 00:24:03,636 --> 00:24:05,056 and just say let make do it for me. 548 00:24:05,056 --> 00:24:07,766 figure out all the flags, figure out the -o and all 549 00:24:07,766 --> 00:24:09,126 that mess and hit enter. 550 00:24:09,666 --> 00:24:11,456 And nothing bad seems to have happened, 551 00:24:11,456 --> 00:24:13,296 which presumably means something good happened, 552 00:24:13,566 --> 00:24:16,316 so now let's go ahead and run imprecision with . 553 00:24:16,896 --> 00:24:17,466 /imprecision. 554 00:24:18,056 --> 00:24:19,826 Okay. so that actually looks pretty good. 555 00:24:19,826 --> 00:24:22,546 Answer looks right, I seem to have gotten some free zeros 556 00:24:22,546 --> 00:24:24,826 at the end of it but remember from last time 557 00:24:24,826 --> 00:24:28,186 that we can actually exercise a bit more discretion 558 00:24:28,186 --> 00:24:31,766 when it comes to seeing the places after that decimal point 559 00:24:31,766 --> 00:24:35,246 and I can do that by saying .and let me see 10 decimal places. 560 00:24:35,246 --> 00:24:37,556 That was like 6 or 7 there, let me go a little farther 561 00:24:37,556 --> 00:24:40,656 to the right and see what's really inside this variable Z, 562 00:24:40,656 --> 00:24:44,366 I'm going to go ahead and hit control X Y and enter. 563 00:24:44,366 --> 00:24:46,366 I'm back at the prompt and I'm going to rerun it and 564 00:24:46,366 --> 00:24:47,876 yet I see the same thing. 565 00:24:48,426 --> 00:24:48,896 Sanity check? 566 00:24:48,896 --> 00:24:49,776 >> [inaudible] 567 00:24:49,776 --> 00:24:51,456 >> You have to recompile, right. 568 00:24:51,456 --> 00:24:53,116 Again, it's sort of a neophyte mistake, 569 00:24:53,276 --> 00:24:54,626 you get used to it very soon. 570 00:24:54,686 --> 00:24:56,856 Let's remake this with imprecision, enter. 571 00:24:57,176 --> 00:24:59,626 I've recompiled my code, now if I rerun it 572 00:24:59,756 --> 00:25:02,016 and we move the cursor to the top, enter. 573 00:25:02,766 --> 00:25:03,436 Interesting. 574 00:25:03,856 --> 00:25:06,836 So that was kind of a lie, so when we've said before 575 00:25:06,836 --> 00:25:09,086 that computers only have a finite amount of memory 576 00:25:09,086 --> 00:25:12,216 on this a finite amount of precision which is another way 577 00:25:12,216 --> 00:25:14,256 of saying if you've only got a fixed number of bits, 578 00:25:14,686 --> 00:25:17,326 surely you cannot represent with a fixed number of bits, 579 00:25:17,586 --> 00:25:21,896 32 or 64 the infinite number of real or rational numbers 580 00:25:21,896 --> 00:25:24,126 that exist in the world so there's got 581 00:25:24,126 --> 00:25:26,406 to be some approximation going on and there you have it. 582 00:25:26,406 --> 00:25:28,966 at 88 cents plus one cent you do not 583 00:25:28,966 --> 00:25:32,856 in fact get 89 cents you get just shy of 89 cents 584 00:25:32,856 --> 00:25:34,916 so again those of you who are fans of Office Space 585 00:25:34,916 --> 00:25:37,076 or Superman 3 this is the kind of stuff 586 00:25:37,076 --> 00:25:39,066 that they were literally referring to in that movie. 587 00:25:39,066 --> 00:25:41,326 These rounding errors, you start to add all that up 588 00:25:41,326 --> 00:25:42,766 after billions of transactions 589 00:25:42,766 --> 00:25:45,136 and this is how these guys start to make some money. 590 00:25:45,386 --> 00:25:46,766 So why is this relevant? 591 00:25:46,766 --> 00:25:50,256 Well suppose I add to this program a little condition. 592 00:25:50,666 --> 00:25:56,136 Again, just to hammer this home, if Z is equal to 0.89 593 00:25:56,136 --> 00:25:57,506 because that's a reasonable check to make, 594 00:25:57,506 --> 00:25:59,906 let me say printf and just say equal. 595 00:25:59,936 --> 00:26:02,406 I'm just doing a little bit of diagnostic work here. 596 00:26:02,656 --> 00:26:07,976 now I'm going to say, let's say else if Z is greater than 0.89, 597 00:26:08,036 --> 00:26:15,876 let me go ahead and printf greater than .089 print greater 598 00:26:15,876 --> 00:26:19,136 than backslash N else let's go ahead 599 00:26:19,236 --> 00:26:22,666 and printf it should be obviously less. 600 00:26:23,046 --> 00:26:26,226 I could say else if Z is less than but its redundant, 601 00:26:26,416 --> 00:26:28,426 I can just say less because there's only three possible 602 00:26:28,426 --> 00:26:32,626 scenarios here but I could, to be clear, say Z less than 0.89 603 00:26:33,066 --> 00:26:34,056 and we'll get the same result. 604 00:26:34,056 --> 00:26:35,616 So let's see what's actually going to print. 605 00:26:35,616 --> 00:26:40,766 Let's save, quit, remake it, rerun it and in fact it's less. 606 00:26:41,256 --> 00:26:42,966 so you'll see how to deal with this. 607 00:26:42,966 --> 00:26:44,986 Well in the problems that we walk you through the process 608 00:26:44,986 --> 00:26:46,956 of suggesting, well you know, if you've got floats, 609 00:26:46,956 --> 00:26:50,016 and the problem is floats well why not work with cents, 610 00:26:50,176 --> 00:26:52,676 why work with ints so we propose very simply 611 00:26:52,676 --> 00:26:55,446 and this does actually work out to solve this particular problem 612 00:26:55,446 --> 00:26:59,296 in this case, multiply those dollars, cents by 100 613 00:26:59,296 --> 00:27:00,186 and what do you get back? 614 00:27:00,186 --> 00:27:04,946 Well $1.99 becomes 199 pennies and if you round appropriately 615 00:27:05,336 --> 00:27:08,596 as you'll see in the spec than it becomes 89 616 00:27:08,736 --> 00:27:09,596 which is an actual int. 617 00:27:09,886 --> 00:27:12,416 but therein lies the reason and this isn't quite the same bug, 618 00:27:12,686 --> 00:27:15,706 certainly that resulted in the explosion of that rocket 619 00:27:16,006 --> 00:27:17,306 but its this kind of idea, 620 00:27:17,536 --> 00:27:21,006 not understanding what's really going on under the hood 621 00:27:21,006 --> 00:27:22,966 and not understanding what the machine is doing for you 622 00:27:23,126 --> 00:27:25,126 and writing one stupid line of code 623 00:27:25,126 --> 00:27:28,346 and $300 million later, you've got a problem. 624 00:27:28,346 --> 00:27:30,726 Now just to push back, in case you're thinking, 625 00:27:30,986 --> 00:27:32,526 well this is ridiculous, I should come back 626 00:27:32,526 --> 00:27:35,916 in 20 years once mankind's gotten better at providing me 627 00:27:35,916 --> 00:27:38,476 with tools to avoid these problems and we have. 628 00:27:38,476 --> 00:27:40,696 There are other languages, we get to them toward the tail end 629 00:27:40,696 --> 00:27:42,156 of the semester, but again, 630 00:27:42,156 --> 00:27:45,726 too many people don't understand these underlying implementation 631 00:27:45,726 --> 00:27:47,796 issues and that's why we continue to have bugs 632 00:27:47,886 --> 00:27:51,056 and security holes and more on that in just a bit. 633 00:27:51,416 --> 00:27:53,076 So where did the first bug come from? 634 00:27:53,446 --> 00:27:58,216 It was, as legend have it, a bug inside of a very old machine, 635 00:27:58,216 --> 00:28:00,956 one of which you can actually see in the science center, 636 00:28:00,996 --> 00:28:04,036 not the bug but the machine, that big Mach 1 computer, 637 00:28:04,546 --> 00:28:05,586 take a look if you've never, 638 00:28:05,586 --> 00:28:08,036 at the little touristy plaques that are on it. 639 00:28:08,036 --> 00:28:10,396 its quite a part of this schools and computers history 640 00:28:10,626 --> 00:28:14,446 but this is supposedly the first bug that was taped, 641 00:28:14,446 --> 00:28:15,686 with some cellophane tape 642 00:28:15,986 --> 00:28:19,316 by a computer scientist named Grace Hopper to a piece of paper 643 00:28:19,316 --> 00:28:22,936 in her lab notes documenting why the computer had malfunctioned 644 00:28:22,936 --> 00:28:23,456 that day. 645 00:28:23,456 --> 00:28:26,716 These days bugs clearly has a different meaning but derived 646 00:28:26,776 --> 00:28:28,166 from that spirit so let's see 647 00:28:28,166 --> 00:28:30,546 if we can take a look at a bug ourselves. 648 00:28:30,546 --> 00:28:34,436 I'm going to open up buggy1.c. This is the first 649 00:28:34,436 --> 00:28:36,196 of the programs you do have a print out of, 650 00:28:36,196 --> 00:28:38,786 so if you'd like to follow along at home there, go ahead 651 00:28:38,786 --> 00:28:42,696 and take a look at this, take say, 30 seconds, 60 seconds 652 00:28:42,696 --> 00:28:45,816 and decide for yourself and confirm with a partner what 653 00:28:45,816 --> 00:28:49,496 in fact the bug is with this thing here. 654 00:28:50,816 --> 00:28:55,106 Don't worry we won't do this every time you have a question. 655 00:28:56,516 --> 00:29:02,566 [ Music ] 656 00:29:03,066 --> 00:29:08,666 All right, so short program, if you didn't catch this it's okay. 657 00:29:08,666 --> 00:29:10,766 You'll very quickly get better at spotting these things and one 658 00:29:10,766 --> 00:29:13,906 of our roles at office hours is to glance at code like this is, 659 00:29:13,906 --> 00:29:19,166 bam, unstick you and move you along your way, what's the bug? 660 00:29:19,166 --> 00:29:19,233 >> [inaudible] 661 00:29:19,233 --> 00:29:19,646 >> Where is it? 662 00:29:20,616 --> 00:29:24,746 Hopefully alphabetically in the handouts. 663 00:29:26,466 --> 00:29:30,016 Is it not there? 664 00:29:30,016 --> 00:29:30,736 It should be. 665 00:29:30,736 --> 00:29:32,196 Buggy1.c. Okay what's the bug? 666 00:29:32,196 --> 00:29:34,436 Its supposed to print 10 stars but it does not 667 00:29:34,436 --> 00:29:37,916 because it actually prints all right, it prints 11. 668 00:29:37,916 --> 00:29:39,586 So this is again one of these newbie mistakes. 669 00:29:39,586 --> 00:29:41,816 You get used to it too and I point this out again, 670 00:29:41,906 --> 00:29:44,406 never to poke fun but because these are the kinds 671 00:29:44,406 --> 00:29:46,616 of stupid little bugs you trip over at first, 672 00:29:46,616 --> 00:29:48,076 you sink too much time into. 673 00:29:48,326 --> 00:29:49,666 So once you get past some 674 00:29:49,666 --> 00:29:52,746 of these frequently encountered bugs can you really start 675 00:29:52,746 --> 00:29:54,706 focusing on the juicier problems. 676 00:29:54,706 --> 00:29:57,686 So the problem here is that I started counting at zero 677 00:29:57,686 --> 00:30:00,866 like a good computer scientist or a computer but I then went 678 00:30:00,866 --> 00:30:02,676 up to and equal to 10. 679 00:30:02,906 --> 00:30:05,116 So if you actually think through the iterations there, 680 00:30:05,306 --> 00:30:08,616 because recall that in a 4 loop the initializations happen first 681 00:30:08,616 --> 00:30:12,126 on the left, the condition gets checked, then something happens 682 00:30:12,416 --> 00:30:15,496 and then the incrementation or the update happens that will, 683 00:30:15,496 --> 00:30:18,466 in this case actually print 11 stars. 684 00:30:18,466 --> 00:30:20,316 And so the fix could be a couple of things. 685 00:30:20,356 --> 00:30:23,976 The quickest fix and perhaps the stylistically common fix is 686 00:30:24,006 --> 00:30:25,616 to just do that. 687 00:30:25,616 --> 00:30:28,056 When you're iterating from something to something you start 688 00:30:28,056 --> 00:30:30,946 at zero and you go up to but less than that other value. 689 00:30:31,156 --> 00:30:33,686 If you really want to be more consistent with sort 690 00:30:33,686 --> 00:30:37,096 of normal human beings, we could absolutely count from 1 691 00:30:37,146 --> 00:30:39,416 to 10 literally, but just realize 692 00:30:39,546 --> 00:30:41,846 that probably shouldn't start this habit 693 00:30:42,056 --> 00:30:44,166 because there are some lower, there's some other features 694 00:30:44,166 --> 00:30:46,346 of programming languages, arrays being one of them 695 00:30:46,346 --> 00:30:48,866 which we'll talk about this week but your just going to get 696 00:30:48,866 --> 00:30:50,586 in trouble if you insist on counting 697 00:30:50,586 --> 00:30:51,526 like normal human beings. 698 00:30:51,886 --> 00:30:53,056 Start counting in this way. 699 00:30:53,526 --> 00:30:55,866 So let's take a look at a second little example here. 700 00:30:55,866 --> 00:30:57,466 Let's go ahead and look at buggy2 701 00:30:57,466 --> 00:30:58,546 which should follow that one. 702 00:30:58,926 --> 00:31:01,976 This one too is supposed to print 10 stars 703 00:31:02,376 --> 00:31:04,696 but this time one per line because I whipped 704 00:31:04,696 --> 00:31:06,696 out a backslash N but it does not. 705 00:31:06,696 --> 00:31:07,656 So a little sanity check? 706 00:31:08,236 --> 00:31:09,956 What's the bug here? 707 00:31:09,956 --> 00:31:10,586 >> [inaudible] 708 00:31:10,586 --> 00:31:15,256 >> Right. so another frequently encountered issue. 709 00:31:15,316 --> 00:31:17,826 Even though you typed this code out so prettily, 710 00:31:17,826 --> 00:31:19,726 you hit tab a couple times or the space bar 711 00:31:19,726 --> 00:31:20,626 to nicely indent things. 712 00:31:20,626 --> 00:31:22,336 It's very readable, seems consistent 713 00:31:22,336 --> 00:31:25,146 with the CS50 style guide which is discussed in problem set 1. 714 00:31:25,496 --> 00:31:30,736 Unfortunately the compiler only knows to execute one line 715 00:31:30,736 --> 00:31:34,536 of code after a 4 loop unless you have encapsulated it 716 00:31:34,766 --> 00:31:38,326 with curly braces like this and then one down here. 717 00:31:38,616 --> 00:31:41,396 So this would be correct if I want to print a star 718 00:31:41,676 --> 00:31:44,116 and then print a new line, print star new line, star new line. 719 00:31:44,326 --> 00:31:46,886 But let's take a look at what actually happens if I go 720 00:31:46,886 --> 00:31:49,186 with the original version, which is just this one. 721 00:31:49,186 --> 00:31:52,926 So let me go ahead and make buggy2 and then let me go ahead 722 00:31:52,926 --> 00:31:56,186 and run buggy 2 and in fact they're all on the same line 723 00:31:56,266 --> 00:31:59,576 but if I go back into nano, scroll down 724 00:31:59,806 --> 00:32:01,956 and obviously I could just fix it like this, 725 00:32:02,146 --> 00:32:03,866 but demonstrating a little point here. 726 00:32:04,286 --> 00:32:06,866 So I could just go in here, put that curly brace, 727 00:32:07,366 --> 00:32:11,356 put this curly brace, save, quit, recompile, 728 00:32:11,756 --> 00:32:13,476 rerun and there we go. 729 00:32:13,516 --> 00:32:17,436 Stupid program one star though is now per line and let me point 730 00:32:17,436 --> 00:32:20,276 out one thing too, because when we get questions a lot 731 00:32:20,276 --> 00:32:21,016 of the time on help 732 00:32:21,146 --> 00:32:25,666 at cs50.netinthelight very commonly we see code 733 00:32:25,666 --> 00:32:27,766 that clearly won't compile because you've got a ; 734 00:32:27,766 --> 00:32:30,916 over here, a parenthesis over here, and that's normal 735 00:32:30,916 --> 00:32:33,036 because it's a lot of new syntax for a lot of folks 736 00:32:33,246 --> 00:32:36,336 but I can't emphasize enough, before diving in 737 00:32:36,466 --> 00:32:40,536 or before wondering wherein lies this problem, look back to some 738 00:32:40,536 --> 00:32:42,216 of the examples from last week or even this week. 739 00:32:42,426 --> 00:32:44,166 They're very short very deliberately 740 00:32:44,166 --> 00:32:45,406 so that you can actually look at them, 741 00:32:45,406 --> 00:32:47,726 get a sense of what correctly structured code is like, 742 00:32:48,006 --> 00:32:50,646 look at this one and say, okay it starts with int, 743 00:32:50,716 --> 00:32:53,586 main then open curly brace and just make sure you're mimicking 744 00:32:53,586 --> 00:32:55,556 that kind of syntax and then you'll realize 745 00:32:55,556 --> 00:32:58,856 that there is some logical sense to this kind of structure. 746 00:32:59,146 --> 00:33:00,116 All right, so moving 747 00:33:00,116 --> 00:33:02,626 on to something more interesting involving type casting 748 00:33:02,626 --> 00:33:05,926 as that teaching fellow alluded to in his email to me. 749 00:33:05,926 --> 00:33:08,756 so typecasting is just this process of taking one data type 750 00:33:08,756 --> 00:33:12,396 like an int and casting it, converting it to a char 751 00:33:12,656 --> 00:33:15,886 or vice versa, you can do this with a few different data types, 752 00:33:15,886 --> 00:33:21,316 we've talked about char int like a letter A going to 65 753 00:33:21,366 --> 00:33:23,796 or 65 going to the letter A. this will be useful 754 00:33:23,796 --> 00:33:25,736 in applications of cryptography and other things 755 00:33:26,066 --> 00:33:28,836 but you can even cast from a float to an int. 756 00:33:29,016 --> 00:33:32,806 you can take 1.99 put parenthesis int in front of that 757 00:33:32,806 --> 00:33:35,216 and that casts it to an actual int but what's the answer? 758 00:33:35,896 --> 00:33:36,596 So it's just 1. 759 00:33:36,596 --> 00:33:38,396 you throw away everything after the decimal point 760 00:33:38,396 --> 00:33:41,206 which may be what you want, in fact as you'll see 761 00:33:41,206 --> 00:33:42,876 in the standard edition of the problem set 762 00:33:43,416 --> 00:33:46,106 for this week we tell you that if you ever do have 763 00:33:46,146 --> 00:33:48,276 to round these bars when figuring out, sorry, 764 00:33:48,276 --> 00:33:49,966 their width, go ahead and round 765 00:33:49,966 --> 00:33:53,036 down so its not always 100 percent of the length, 766 00:33:53,036 --> 00:33:55,026 we tell you to round down and if you need to round 767 00:33:55,026 --> 00:33:57,276 down casting can do the trick but we're going 768 00:33:57,276 --> 00:33:59,016 to see other data types before too long. 769 00:33:59,016 --> 00:34:00,586 Certainly toward the tail end of the semester 770 00:34:00,586 --> 00:34:03,246 when we're implementing more sophisticated programs we might 771 00:34:03,246 --> 00:34:05,996 want to implement the notion of a course, right? 772 00:34:05,996 --> 00:34:08,376 The Harvard courses web site of the CS50 app, 773 00:34:08,606 --> 00:34:11,856 we clearly have inside our server's memory the notion 774 00:34:11,856 --> 00:34:12,446 of a course. 775 00:34:12,446 --> 00:34:13,686 The notion of an instructor. 776 00:34:13,896 --> 00:34:15,236 The notion of a Q guide rating. 777 00:34:15,236 --> 00:34:17,646 There's all these conceptual entities and yes, 778 00:34:17,646 --> 00:34:20,156 we could represent all of those things with just numbers 779 00:34:20,156 --> 00:34:22,746 and strings and the like but as we'll very soon see, 780 00:34:22,746 --> 00:34:25,216 you can make your own data structures. 781 00:34:25,216 --> 00:34:27,736 You can make your own data types so if you want 782 00:34:27,736 --> 00:34:30,376 to actually represent the notion of a course 783 00:34:30,636 --> 00:34:33,386 and a course has a name, a catalogue number and ... 784 00:34:33,386 --> 00:34:34,506 some other stuff. 785 00:34:34,786 --> 00:34:36,776 Well you can essentially join a whole bunch 786 00:34:36,776 --> 00:34:39,596 of variables together, call them something new. 787 00:34:39,856 --> 00:34:43,356 The course data type and then start manipulating that 788 00:34:43,356 --> 00:34:45,896 but if you choose to do that, 789 00:34:46,066 --> 00:34:48,506 typecasting doesn't mean you can take a course and cast it 790 00:34:48,506 --> 00:34:52,576 to faculty member or vice versa, it has to actually make sense 791 00:34:52,576 --> 00:34:54,616 so in this context of primitives where at the end 792 00:34:54,616 --> 00:34:57,256 of the day its just bits, doesn't make sense some time 793 00:34:57,316 --> 00:34:58,666 to cast one thing to another. 794 00:34:58,666 --> 00:35:04,126 So let's take a look at ASCII1.c This too is among your printouts 795 00:35:04,126 --> 00:35:08,596 toward the beginning, let me go ahead and scroll down here 796 00:35:09,906 --> 00:35:11,926 and I've left the comments into this one, 797 00:35:11,926 --> 00:35:13,366 just because it's a few more lines of code, 798 00:35:13,366 --> 00:35:14,996 feel free to mark it up if you like, 799 00:35:15,336 --> 00:35:16,866 but what does this actually do? 800 00:35:16,866 --> 00:35:19,726 Well this is just to hammer home the point from week zero 801 00:35:19,726 --> 00:35:22,966 and if you want to represent letters and sentences and words 802 00:35:22,966 --> 00:35:24,226 on the screen, well it turns 803 00:35:24,226 --> 00:35:26,996 out you can do this using just the underlying integer 804 00:35:26,996 --> 00:35:28,996 representations but if you cast things 805 00:35:28,996 --> 00:35:32,766 to a char you can very explicitly see that 65 is 806 00:35:32,766 --> 00:35:36,826 in fact capital A. 66 is in fact capital B and so forth 807 00:35:37,126 --> 00:35:38,476 and so what is this really going to do? 808 00:35:38,476 --> 00:35:39,686 Well here we have a 4 loop, 809 00:35:39,986 --> 00:35:42,346 certainly by the time you've dived into problems that one, 810 00:35:42,346 --> 00:35:44,136 this kind of syntax will look more familiar, 811 00:35:44,356 --> 00:35:46,536 even though we've started counting at zero before, 812 00:35:46,816 --> 00:35:50,006 it's totally legit if I want to start at a fixed value like this 813 00:35:50,376 --> 00:35:54,246 and iterate from 65 up to 65 +26. 814 00:35:54,546 --> 00:35:56,736 Now arguably this is a bit of a cheat, I just didn't want 815 00:35:56,736 --> 00:35:58,056 to do the math that late at night 816 00:35:58,056 --> 00:36:00,156 so I just did the addition here because the computer can do it 817 00:36:00,406 --> 00:36:01,786 but certainly we could add those together 818 00:36:01,786 --> 00:36:04,556 in advance an probably save ourselves some cpu cycles. 819 00:36:04,886 --> 00:36:05,806 What's going to happen here? 820 00:36:06,226 --> 00:36:09,766 Well this line of printf prints a character followed 821 00:36:09,766 --> 00:36:12,576 by A semicolon and a space and then one or more digits. 822 00:36:12,976 --> 00:36:13,966 What am I putting in there? 823 00:36:14,236 --> 00:36:16,186 Well I'm putting I both times. 824 00:36:16,186 --> 00:36:18,536 I'm plugging I into both of those placeholders 825 00:36:18,756 --> 00:36:20,156 but I'm first telling the compiler, 826 00:36:20,156 --> 00:36:22,456 you know what, treat I as a char. 827 00:36:22,456 --> 00:36:26,906 Treat it as a character so that what's printed is not 65; 828 00:36:27,086 --> 00:36:31,376 5 but hopefully A;65 and as an aside, 829 00:36:31,536 --> 00:36:34,676 it turns out its not strictly necessary to explicitly cast 830 00:36:35,206 --> 00:36:38,116 or explicitly say, open (char) here. 831 00:36:38,116 --> 00:36:40,696 I'm doing it really just to be deliberate and clear 832 00:36:41,006 --> 00:36:44,886 but the compiler or printf is really smart enough to know 833 00:36:44,886 --> 00:36:46,636 that its this thing over here 834 00:36:46,636 --> 00:36:48,536 that dictates how those bits are rendered 835 00:36:48,616 --> 00:36:51,026 and not necessarily this so realize that. 836 00:36:51,226 --> 00:36:55,146 As for the bottom half here 97 happens to represent little a, 837 00:36:55,146 --> 00:36:58,016 98 little b and so forth so there are charts 838 00:36:58,076 --> 00:36:59,416 that show you what these mappings are 839 00:36:59,416 --> 00:37:03,686 but now knowing 65 is big A 97 is little a we've got 52 840 00:37:03,686 --> 00:37:05,706 letters, representations that we can figure out 841 00:37:05,796 --> 00:37:07,746 and every other character on the keyboard has its own. 842 00:37:08,086 --> 00:37:08,936 so let's run this. 843 00:37:08,936 --> 00:37:12,526 MakeASCII1 seems to have compiled okay, let's go ahead 844 00:37:12,526 --> 00:37:13,656 and run it and what do I get? 845 00:37:14,106 --> 00:37:16,886 Well just get a big long chart showing me the 846 00:37:16,886 --> 00:37:19,906 so called ASCII table so we've pretty much scratched the 847 00:37:19,906 --> 00:37:25,626 surface of this verbally only but A is 65, B is 66, T is 84 848 00:37:25,626 --> 00:37:29,836 and then sure enough, little a down here is 97. 849 00:37:29,996 --> 00:37:32,846 all together this is a pretty unwieldy program. 850 00:37:32,846 --> 00:37:35,466 If you spit out output like this its already annoying to me 851 00:37:35,466 --> 00:37:37,976 that I have to scroll up just other see what the program told 852 00:37:37,976 --> 00:37:39,846 me, so let's see if we can do a little better. 853 00:37:39,846 --> 00:37:42,726 Let me go ahead and open the second version of this, 854 00:37:43,616 --> 00:37:45,446 nanoASCII2 and scroll down 855 00:37:46,036 --> 00:37:48,446 and notice I've done something a little differently here. 856 00:37:48,886 --> 00:37:51,146 again just a little trick especially for something 857 00:37:51,146 --> 00:37:53,966 like problem set 1 or future P-Sets where you want to start 858 00:37:53,966 --> 00:37:56,176 to control the aesthetics of your program a little more, 859 00:37:56,176 --> 00:37:58,646 not just dump stuff to the screen like printf makes 860 00:37:58,646 --> 00:38:01,086 so easy, well here, same kind of iteration. 861 00:38:01,086 --> 00:38:02,166 But what am I doing? 862 00:38:02,166 --> 00:38:05,126 Well I'm printing out character, some white space digits, 863 00:38:05,616 --> 00:38:08,876 some more white space and then the 3 D. What does this 864 00:38:09,386 --> 00:38:10,656 denote here? 865 00:38:10,876 --> 00:38:14,526 So 3 spaces so I figured this out, frankly by trial and error 866 00:38:14,526 --> 00:38:17,216 or if I were more astute I'd sort of anticipated this 867 00:38:17,356 --> 00:38:20,166 but once I get to the lower case letters, it starts at 97 868 00:38:20,166 --> 00:38:23,476 and then 98, 99 it eventually becomes three digits so just 869 00:38:23,476 --> 00:38:25,946 to be anal and aesthetically pretty I wanted to make sure 870 00:38:25,946 --> 00:38:28,956 that all those numbers take up three columns of text 871 00:38:29,026 --> 00:38:32,766 so that everything aligns nicely so I eventually went back in 872 00:38:32,766 --> 00:38:35,366 and said, all right, make this 3 D and not just D 873 00:38:35,366 --> 00:38:37,216 for reasons we'll see literally in a moment 874 00:38:37,216 --> 00:38:39,986 and then we have another character, so what's over here? 875 00:38:39,986 --> 00:38:42,446 well let me scroll over to the right, 876 00:38:42,446 --> 00:38:44,636 since the lines a little long actually, 877 00:38:44,636 --> 00:38:47,366 let me just scroll back, shrink the font a little bit, 878 00:38:47,546 --> 00:38:48,626 so what do we have here? 879 00:38:48,746 --> 00:38:51,276 the first place holder is going to get the character equivalent 880 00:38:51,276 --> 00:38:52,636 of I, that's copy paste 881 00:38:52,636 --> 00:38:55,516 from last time then literally I, copy paste. 882 00:38:55,766 --> 00:38:57,286 Well here I just did some trickery. 883 00:38:57,286 --> 00:38:58,966 I realized, you know what, 884 00:38:58,966 --> 00:39:02,206 big A and little a they're always the same distance apart, 885 00:39:02,206 --> 00:39:03,946 they're always 32 characters apart, 886 00:39:04,206 --> 00:39:08,266 65 to 97 so let me just do this all at once, the current value 887 00:39:08,266 --> 00:39:11,276 of I plus 32 will take me from the upper case 888 00:39:11,316 --> 00:39:14,526 to the lower case range and lastly over here I'm casting 889 00:39:14,526 --> 00:39:18,256 to a char the result of doing that same calculation, 890 00:39:18,306 --> 00:39:21,746 I plus 32 so in the end, what do we have here? 891 00:39:22,136 --> 00:39:27,496 ASCII 2, let's go ahead and run ASCII 2 so its not perfect 892 00:39:27,496 --> 00:39:28,856 but my screen, my fonts. 893 00:39:28,856 --> 00:39:30,706 My screen's a little small, my font's a little big 894 00:39:30,916 --> 00:39:32,326 but now I feel like, okay 895 00:39:32,326 --> 00:39:35,186 at least I'm now controlling this machine a bit more. 896 00:39:35,426 --> 00:39:38,686 so maybe a little underwhelming in the context of today's guise 897 00:39:38,686 --> 00:39:41,816 and what not but again the take away for now is control 898 00:39:41,816 --> 00:39:46,556 and specifying to the machine what you want it to do for you. 899 00:39:46,866 --> 00:39:47,476 any questions? 900 00:39:47,856 --> 00:39:50,086 On this little ASCII exercise? 901 00:39:50,816 --> 00:39:50,916 Yeah? 902 00:39:50,916 --> 00:39:51,616 >> [inaudible] 903 00:39:51,616 --> 00:39:54,456 >> Oh, good question. 904 00:39:54,946 --> 00:39:58,356 And when in doubt we'll always try so let's scroll down here 905 00:39:58,356 --> 00:40:02,476 and let me get rid of what I thought was a fairly clever 906 00:40:02,476 --> 00:40:06,706 trick, let me save, quit, recompile, and now rerun 907 00:40:06,996 --> 00:40:09,136 and so you know, its not that bad, 908 00:40:09,136 --> 00:40:10,576 I'm being a little uptight here 909 00:40:10,576 --> 00:40:12,536 but that's the problem I was trying to solve. 910 00:40:12,536 --> 00:40:14,256 I wanted to make sure each of those numbers took 911 00:40:14,256 --> 00:40:17,116 up three places and not just two potentially. 912 00:40:18,066 --> 00:40:20,076 All right so one last variant of ASCII. 913 00:40:20,186 --> 00:40:22,186 Let's scroll on down and realize 914 00:40:22,186 --> 00:40:24,846 if you really have free time you can transform these very basic 915 00:40:24,846 --> 00:40:26,836 primitives into the entirety of movies. 916 00:40:27,236 --> 00:40:28,686 Well now notice two here, 917 00:40:28,686 --> 00:40:31,026 this isn't necessarily the way you should do things, 918 00:40:31,246 --> 00:40:33,326 but just to hammer home this point that at the end 919 00:40:33,326 --> 00:40:35,276 of the day all we're doing is manipulating bits. 920 00:40:35,466 --> 00:40:38,676 Zeros and ones and we happen to group those bits as bytes 921 00:40:38,896 --> 00:40:40,706 like a char is one byte, recall, 922 00:40:40,706 --> 00:40:42,856 an int is 4 bytes recall generally. 923 00:40:43,146 --> 00:40:46,376 Turns out we can even iterate over characters just 924 00:40:46,376 --> 00:40:49,656 to make all the more clear what, that there is this equivalent. 925 00:40:49,896 --> 00:40:53,376 I can iterate from the character c starting at capital A, 926 00:40:53,376 --> 00:40:57,216 I can do this as long as that's less than or equal to capital C 927 00:40:57,406 --> 00:40:59,006 and then I can do this instrumentation, 928 00:40:59,006 --> 00:41:00,836 again here I'm being very deliberate with all 929 00:41:00,836 --> 00:41:04,156 of my parenthesisation the compiler does not need all 930 00:41:04,156 --> 00:41:07,076 of these hints necessarily but notice let's start on the inside 931 00:41:07,076 --> 00:41:09,686 of the parenthesis just like you would in grade school math, 932 00:41:09,816 --> 00:41:12,136 focus on the deepest ones first. 933 00:41:12,466 --> 00:41:15,296 So here's c, I cast it to a number and then I add 1, 934 00:41:15,296 --> 00:41:18,156 because again the goal is to just start incrementing 935 00:41:18,156 --> 00:41:20,776 on each iteration, then I don't want to print a number I want 936 00:41:20,776 --> 00:41:23,936 to print a char so I cast c back to a char and then this line 937 00:41:23,936 --> 00:41:26,366 of code is the same and at the end of the day, sure you want 938 00:41:26,366 --> 00:41:28,446 to iterate and call it a character, that's fine, 939 00:41:28,656 --> 00:41:31,536 its still just a byte, its still just a number underneath 940 00:41:31,576 --> 00:41:31,876 the hood. 941 00:41:32,326 --> 00:41:33,066 Well you might recall 942 00:41:33,066 --> 00:41:35,456 from childhood a game called Battleship. 943 00:41:35,686 --> 00:41:38,006 Now I can't do justice to this game 944 00:41:38,006 --> 00:41:40,596 with my little command line programs yet but I can print 945 00:41:40,596 --> 00:41:42,116 out the board thereof. 946 00:41:42,296 --> 00:41:45,686 So when you start using interfaces where the user has 947 00:41:45,716 --> 00:41:50,016 to now see data and isn't really tolerant 948 00:41:50,046 --> 00:41:52,436 of just a blinking cursor and line after line of text. 949 00:41:52,666 --> 00:41:54,396 We can absolutely begin 950 00:41:54,396 --> 00:41:57,376 to implement what we would call graphical interfaces, 951 00:41:57,376 --> 00:41:59,906 albeit in a command line environment so if you don't, 952 00:42:00,026 --> 00:42:00,966 if you've never played this, 953 00:42:00,966 --> 00:42:03,216 this is essentially a plastic board you get as a kid, 954 00:42:03,426 --> 00:42:06,006 you've got numbers on the top, letters on the side 955 00:42:06,006 --> 00:42:09,566 and you put these little ships inside these holes that I chose 956 00:42:09,566 --> 00:42:11,536 to represent with the lower case zero there. 957 00:42:11,736 --> 00:42:14,236 what's interesting about this particular exercise is just how 958 00:42:14,236 --> 00:42:18,056 nicely formatted everything is and also there's some kind 959 00:42:18,056 --> 00:42:21,496 of looping going on here and that certainly might be a trick 960 00:42:21,496 --> 00:42:24,296 that proves handy in problem set 1, let's take a look 961 00:42:24,296 --> 00:42:27,746 for a moment at battleship.c. it turns out to print all 962 00:42:27,746 --> 00:42:30,256 that out there's not all that much going on but again, 963 00:42:30,256 --> 00:42:33,316 there's some decisions I need to make at each point so I claim 964 00:42:33,316 --> 00:42:35,186 with this comment up here that I'm first going 965 00:42:35,186 --> 00:42:36,866 to print the top row of numbers. 966 00:42:37,166 --> 00:42:39,706 Well first I print some white space so new line, 967 00:42:39,706 --> 00:42:42,586 just because I want to move it down a line just 968 00:42:42,586 --> 00:42:45,446 because it looked prettier to me, then indent three spaces. 969 00:42:45,446 --> 00:42:46,816 Again, just to keep things pretty, 970 00:42:46,876 --> 00:42:47,776 but then what am I doing? 971 00:42:47,776 --> 00:42:51,436 It looks like I'm just printing 1, 2, 3, 4, 5, 6, 7, 8, 9, 972 00:42:51,436 --> 00:42:53,956 10 on top of the little battleship board like we saw. 973 00:42:54,236 --> 00:42:55,636 Well then what do I do after that? 974 00:42:55,636 --> 00:42:59,436 Just a new line, so this white, the problem I've taken 975 00:42:59,436 --> 00:43:01,956 out first, it just prints the row of numbers 976 00:43:01,956 --> 00:43:04,016 and so this is again to echo the point from last week, 977 00:43:04,016 --> 00:43:05,806 especially when you're diving into P-Set 1, 978 00:43:06,126 --> 00:43:08,026 do not try to implement the entirely 979 00:43:08,026 --> 00:43:09,646 of the Skittles problem, the entirety 980 00:43:09,646 --> 00:43:11,426 of the change making problem, the entirety 981 00:43:11,426 --> 00:43:14,256 of the I saw you program because inevitably you'll have one 982 00:43:14,256 --> 00:43:16,316 or two or three bugs and you'll have to chase them 983 00:43:16,316 --> 00:43:20,246 down altogether it just adds complexity and wastes time, 984 00:43:20,526 --> 00:43:24,136 start by just implementing the numbers, save, compile, run, 985 00:43:24,136 --> 00:43:25,796 looks good and now move on to the next part. 986 00:43:26,076 --> 00:43:26,886 That next part is this. 987 00:43:27,346 --> 00:43:30,526 Print the rows of holes represented by lower case o 988 00:43:30,636 --> 00:43:32,756 and the letters in the left most column. 989 00:43:33,246 --> 00:43:34,506 So I had to do some thinking here. 990 00:43:34,766 --> 00:43:37,766 now I don't want to print one row, I've got to print one row 991 00:43:37,766 --> 00:43:39,366 than another and then another and another 992 00:43:39,486 --> 00:43:42,386 but in each row I kind of have to iterate this way so I have 993 00:43:42,386 --> 00:43:46,006 to go this way and then this way so that's just looping 994 00:43:46,006 --> 00:43:48,566 in two different directions so you can absolutely as you could 995 00:43:48,566 --> 00:43:52,016 in scratch, loop using nested loops. 996 00:43:52,016 --> 00:43:55,066 Using wild, do wild with 4 loops, I decided to go 997 00:43:55,066 --> 00:43:56,896 with 4 here and as follows. 998 00:43:57,106 --> 00:43:59,576 So 4 int I gets 0 up to 10 999 00:43:59,806 --> 00:44:04,236 so this loop here is this controlling the cursors top 1000 00:44:04,236 --> 00:44:06,536 down movement or left right movement would you say? 1001 00:44:07,246 --> 00:44:09,696 so it is in fact top down. 1002 00:44:10,256 --> 00:44:11,016 Now why is that? 1003 00:44:11,336 --> 00:44:12,636 Well on each iteration 1004 00:44:12,636 --> 00:44:15,196 of the outer loop just conceptually you're probably 1005 00:44:15,196 --> 00:44:17,326 going to print as we've seen printf behave before, 1006 00:44:17,476 --> 00:44:20,996 one line at a time so to print this and this 1007 00:44:20,996 --> 00:44:22,796 and this that's got to happen on the inside 1008 00:44:22,796 --> 00:44:24,706 because the only one I've printed this and this 1009 00:44:24,706 --> 00:44:27,166 and this am I allowed to wrap around to the new line? 1010 00:44:27,356 --> 00:44:29,366 and that's the roll being played by this outer loop 1011 00:44:29,366 --> 00:44:31,126 so indeed this outer loop, I, 1012 00:44:31,406 --> 00:44:34,666 represents the rows not the columns per se. 1013 00:44:34,946 --> 00:44:35,876 now what am I doing here? 1014 00:44:35,876 --> 00:44:38,136 I'm printing out a character place holder followed 1015 00:44:38,136 --> 00:44:42,206 by literally a plus I so here I started to understand 1016 00:44:42,266 --> 00:44:44,946 that I know that this cast can happen and I don't have to be 1017 00:44:44,946 --> 00:44:47,616 so explicit any more and so this is going to print A 1018 00:44:47,616 --> 00:44:52,516 when I is zero, B when I is 1, C when I is 2 1019 00:44:52,516 --> 00:44:54,706 because you're just adding 65 plus that value. 1020 00:44:55,106 --> 00:44:56,116 Then what do I print next? 1021 00:44:56,346 --> 00:44:58,776 Well I just need to print 10 of these holes in the board 1022 00:44:59,066 --> 00:45:02,376 so I print a lower case o space, space to line things up 1023 00:45:02,526 --> 00:45:04,026 and then a new line eventually, 1024 00:45:04,026 --> 00:45:06,076 notice that its not inside the 4 loop its 1025 00:45:06,076 --> 00:45:08,386 at the end nor is this thing inside the 4 loop that's 1026 00:45:08,386 --> 00:45:10,736 at the end and the end result is to print this, this, 1027 00:45:10,736 --> 00:45:12,536 this and this but before you advance 1028 00:45:12,536 --> 00:45:15,626 to the next line you're going this, DDDD this DDDDDD 1029 00:45:15,626 --> 00:45:17,696 and so you get this looping structure 1030 00:45:17,696 --> 00:45:21,196 and this is very similar in spirit to a little something 1031 00:45:21,196 --> 00:45:26,426 that you might do for instance in say, a picture like this. 1032 00:45:26,696 --> 00:45:28,286 Why don't we go ahead and take a five minute break. 1033 00:45:28,586 --> 00:45:29,026 All right. 1034 00:45:30,056 --> 00:45:36,976 so we are back. 1035 00:45:36,976 --> 00:45:41,886 Let me verbally answer two frequently just asked questions. 1036 00:45:41,886 --> 00:45:43,816 So some of you noticed that all 1037 00:45:43,816 --> 00:45:46,886 of a sudden I started making main look a lot more complicated 1038 00:45:46,886 --> 00:45:49,066 than it used to be by including int, part, 1039 00:45:49,066 --> 00:45:51,626 C char, star, art B, bracket. 1040 00:45:51,916 --> 00:45:53,666 So the reason for this is that this week 1041 00:45:53,666 --> 00:45:57,756 when we start action teasing apart what a string actually is. 1042 00:45:57,756 --> 00:46:00,266 So far we've talked about a sting as being a word, hello, 1043 00:46:00,266 --> 00:46:03,076 a sentence or what not but it ultimately it's a sequence 1044 00:46:03,076 --> 00:46:07,056 of chars so it turns out that program like, that you write 1045 00:46:07,446 --> 00:46:11,646 that have main functions can take strings, can take words 1046 00:46:11,786 --> 00:46:14,906 as arguments themselves so this is a foreshadowing 1047 00:46:14,906 --> 00:46:18,456 of exactly the syntax that we're going to start using this week 1048 00:46:18,526 --> 00:46:21,126 and beyond, it's a little more sophisticated then just saying 1049 00:46:21,126 --> 00:46:23,806 void but it useful when you actually want to take input 1050 00:46:23,806 --> 00:46:25,856 from the user that moment they run the command. 1051 00:46:25,856 --> 00:46:28,526 Case in point, you've been running GCC or soon will, 1052 00:46:28,526 --> 00:46:30,376 you've been using nano or soon will, 1053 00:46:30,636 --> 00:46:33,416 those programs take what we've been calling command 1054 00:46:33,416 --> 00:46:34,156 line arguments. 1055 00:46:34,156 --> 00:46:37,406 Nanohello.c make hello. 1056 00:46:37,606 --> 00:46:41,076 So that word has to somehow be handed into your program 1057 00:46:41,286 --> 00:46:43,336 and the means by which you're going to empower you 1058 00:46:43,336 --> 00:46:46,236 to hand inputs, words, strings and the like. 1059 00:46:46,236 --> 00:46:49,216 Add the command line to your programs is by way 1060 00:46:49,216 --> 00:46:51,216 of these inputs to main. 1061 00:46:51,216 --> 00:46:52,466 The thing inside the parenthesis 1062 00:46:52,466 --> 00:46:54,606 but we'll come back to that before long. 1063 00:46:54,836 --> 00:46:57,476 As another FAQ I suddenly seem 1064 00:46:57,476 --> 00:46:58,936 to have stopped using double quotes 1065 00:46:58,936 --> 00:47:02,156 and instead using single quotes, the reason for this is, again, 1066 00:47:02,156 --> 00:47:03,636 it reduces to an issue of types. 1067 00:47:04,056 --> 00:47:07,266 When I'm using double quotes as an argument as an input 1068 00:47:07,266 --> 00:47:10,446 to PrinTif so that is a string, what we've been calling it. 1069 00:47:10,446 --> 00:47:13,466 a string, recall from last week is just a sequence of chars, 1070 00:47:13,466 --> 00:47:14,566 a sequence of characters. 1071 00:47:14,916 --> 00:47:18,286 When you have just one character and you want to be sure 1072 00:47:18,286 --> 00:47:21,896 that that one character is in fact being represented 1073 00:47:21,896 --> 00:47:23,996 with just 8 bits, a single byte, 1074 00:47:24,416 --> 00:47:27,056 must you use single quotes around it. 1075 00:47:27,106 --> 00:47:30,386 if you don't as we'll see next week and perhaps this Wednesday, 1076 00:47:30,626 --> 00:47:32,106 if you don't that's A, 1077 00:47:32,106 --> 00:47:35,966 if you use double quotes it will actually be treated as a string 1078 00:47:36,206 --> 00:47:39,116 or really a one character word, a one character sentence 1079 00:47:39,356 --> 00:47:41,926 and that has implications for how much memory is being used 1080 00:47:42,156 --> 00:47:44,416 and also for potentially the security 1081 00:47:44,496 --> 00:47:46,896 of the information being stored there. 1082 00:47:46,896 --> 00:47:47,566 >> [inaudible] 1083 00:47:47,566 --> 00:47:51,276 >> Ah, good question. 1084 00:47:52,016 --> 00:47:56,566 When you are printing backslash N can you use single quotes? 1085 00:47:56,666 --> 00:47:58,876 No this needs to go inside of double quotes 1086 00:47:58,876 --> 00:48:01,716 so I have been very particular in using my double quotes 1087 00:48:01,716 --> 00:48:04,006 and single quotes and pretty much you should, 1088 00:48:04,346 --> 00:48:05,436 just a safe generalization? 1089 00:48:05,826 --> 00:48:08,776 Almost always use double quotes unless you know what you want 1090 00:48:08,776 --> 00:48:11,326 to print is and individual char and you've hard coded it 1091 00:48:11,416 --> 00:48:13,266 into that program as such. 1092 00:48:13,736 --> 00:48:14,136 All right. 1093 00:48:14,196 --> 00:48:18,466 so a little bit of a familiar if annoying song. 1094 00:48:18,646 --> 00:48:21,076 So I needed something that had a lot of simplicity in it, 1095 00:48:21,076 --> 00:48:23,636 just repeats ad nauseum and something ideally 1096 00:48:23,636 --> 00:48:25,246 that starts counting downwards 1097 00:48:25,246 --> 00:48:28,226 so you have this song the kids used to sing on the school bus 1098 00:48:28,226 --> 00:48:29,806 for instance and what's interesting 1099 00:48:29,806 --> 00:48:32,246 that there's a couple of changes that happen over time 1100 00:48:32,246 --> 00:48:36,396 with each annoying verse of this song, the 99 becomes 98, 1101 00:48:36,396 --> 00:48:39,796 the 98 becomes 97 and you count down to one or zero or whatever. 1102 00:48:40,196 --> 00:48:45,676 But notice too inside of each verse there is a change 1103 00:48:45,756 --> 00:48:49,056 in let's call it I itself and it doesn't happen on every loop, 1104 00:48:49,056 --> 00:48:51,926 it actually happens inside the verse itself and then 1105 00:48:51,926 --> 00:48:55,716 if I really want to be anal, this last line when I say, 1106 00:48:56,036 --> 00:48:59,336 something bottles of beer on the wall, when I get down to one, 1107 00:48:59,556 --> 00:49:01,736 I probably want to fix the English 1108 00:49:01,736 --> 00:49:05,316 so that it just says one bottle of beer on the wall so it seems 1109 00:49:05,316 --> 00:49:07,506 like there's an opportunity for a condition 1110 00:49:07,506 --> 00:49:09,856 and if else there even though its only useful 1111 00:49:09,856 --> 00:49:11,716 at the very tail end of that program. 1112 00:49:11,996 --> 00:49:13,206 So let's go ahead and open 1113 00:49:13,206 --> 00:49:18,556 up a little program called beer1.c first incarnation 1114 00:49:18,556 --> 00:49:18,806 of this. 1115 00:49:20,256 --> 00:49:24,886 I'm again using cs50's library and what's going 1116 00:49:24,886 --> 00:49:26,166 on here in this program? 1117 00:49:26,166 --> 00:49:27,486 So the first thing I've decided 1118 00:49:27,486 --> 00:49:29,076 to do is ask the user for a number. 1119 00:49:29,076 --> 00:49:29,986 All right, how do I do that? 1120 00:49:30,306 --> 00:49:32,726 Well I literally use printf an ask them for a number 1121 00:49:32,876 --> 00:49:35,726 and then I use getin from cs50's library and recall 1122 00:49:35,726 --> 00:49:38,596 that get int is a function and a function is kind 1123 00:49:38,596 --> 00:49:41,206 of this black box that might take input, it doesn't have to 1124 00:49:41,206 --> 00:49:45,256 but might take input and might return output and if it does 1125 00:49:45,256 --> 00:49:47,046 in fact return output that you care 1126 00:49:47,046 --> 00:49:48,886 about you better do something with it 1127 00:49:48,886 --> 00:49:51,486 and in this case you better store that return value, 1128 00:49:51,486 --> 00:49:54,266 that output in a variable end so we can use it later. 1129 00:49:54,646 --> 00:49:57,276 Now here's what we'll start calling error checking 1130 00:49:57,276 --> 00:49:58,626 or rigorous error checking 1131 00:49:58,886 --> 00:50:01,906 and they won't necessarily see it specified pedantically 1132 00:50:01,906 --> 00:50:04,066 in every problem set, check for this, check for that, 1133 00:50:04,066 --> 00:50:05,656 make sure the user can't do this. 1134 00:50:06,006 --> 00:50:07,916 This should become natural before long. 1135 00:50:07,916 --> 00:50:10,036 If you're writing a program that takes and form 1136 00:50:10,036 --> 00:50:13,466 of user input it is ultimately and should be up to you 1137 00:50:13,696 --> 00:50:16,266 to make sure that that users input cannot 1138 00:50:16,266 --> 00:50:18,246 in any way break you're program. 1139 00:50:18,476 --> 00:50:19,886 Its not an excuse to say, oh, 1140 00:50:19,886 --> 00:50:21,786 well they shouldn't have inputted in a negative number 1141 00:50:21,786 --> 00:50:24,726 and a case in point, not to over emphasize that explosion 1142 00:50:24,726 --> 00:50:26,546 of that rocket ship you make assumptions 1143 00:50:26,546 --> 00:50:28,846 and bad things eventually happen, especially when lots 1144 00:50:28,846 --> 00:50:30,886 of people and lots of users become involved. 1145 00:50:31,156 --> 00:50:32,636 So I'm doing a sanity check here. 1146 00:50:32,636 --> 00:50:36,056 if the user has typed in an int, which is allowed by get int, 1147 00:50:36,326 --> 00:50:39,606 but that int is 0 or negative, I don't want to deal 1148 00:50:39,606 --> 00:50:41,506 with this user so I'm simply going to say, 1149 00:50:41,506 --> 00:50:44,706 sorry that makes no sense and then I'm going to return 1. 1150 00:50:45,026 --> 00:50:46,586 So this is a piece of new syntax. 1151 00:50:46,586 --> 00:50:49,466 It turns out that when you are writing a function which we are, 1152 00:50:49,496 --> 00:50:53,856 this thing called main, function tab can have return values, 1153 00:50:54,026 --> 00:50:56,986 this is that point I was just making, the return value 1154 00:50:56,986 --> 00:50:59,486 of get int is what data type presumably? 1155 00:51:00,066 --> 00:51:02,386 So its an int and you know, 1156 00:51:02,556 --> 00:51:04,216 I kind of have been waving my hands at it, 1157 00:51:04,216 --> 00:51:05,596 what's the return type of main? 1158 00:51:06,386 --> 00:51:10,136 Its been always int, even though I've not used this explicitly 1159 00:51:10,356 --> 00:51:12,896 so it turns out that even the function called main 1160 00:51:12,896 --> 00:51:15,146 which is the one you have to start your programs in c, 1161 00:51:15,216 --> 00:51:16,636 even it has a return value 1162 00:51:16,896 --> 00:51:20,676 and thus far every program I've written has had a return value, 1163 00:51:20,676 --> 00:51:22,176 every implementation of main 1164 00:51:22,176 --> 00:51:24,896 that I've written actually has a return to value 1165 00:51:25,186 --> 00:51:27,696 but it returned implicitly a value of zero. 1166 00:51:28,166 --> 00:51:31,266 So if you don't specify a return value at least in main, 1167 00:51:31,466 --> 00:51:34,136 the value of zero is just assumed so you don't have 1168 00:51:34,136 --> 00:51:37,486 to waste time and effort at the very bottom of your function. 1169 00:51:37,726 --> 00:51:38,766 Now why is this useful? 1170 00:51:38,956 --> 00:51:40,846 Well there's a lot of things that can go wrong 1171 00:51:40,846 --> 00:51:43,756 when you write a program, we've seen visually an example 1172 00:51:43,756 --> 00:51:45,956 of that, we've certainly seen a couple of buggy examples today 1173 00:51:45,956 --> 00:51:48,376 of that, so the idea that a program 1174 00:51:48,376 --> 00:51:50,756 or function can return a value is useful 1175 00:51:50,756 --> 00:51:53,906 because its only mechanism for informing the programmer 1176 00:51:53,906 --> 00:51:57,406 or the human, the user, that or whether 1177 00:51:57,406 --> 00:51:58,956 or that something went wrong. 1178 00:51:59,216 --> 00:52:01,006 So zero is good. 1179 00:52:01,006 --> 00:52:05,066 Zero means nothing went wrong, I'm not going to tell you 1180 00:52:05,066 --> 00:52:08,576 that nothing went wrong but unfortunately with programming 1181 00:52:08,576 --> 00:52:10,886 and with computers can we make a heck of a lot of mistakes. 1182 00:52:10,886 --> 00:52:14,936 There's as many as four billion mistakes exist when it comes 1183 00:52:14,936 --> 00:52:17,526 to programming, even that is technically an 1184 00:52:17,526 --> 00:52:18,436 over simplification. 1185 00:52:18,636 --> 00:52:21,946 But this is good because zero means good and I can assume 1186 00:52:21,946 --> 00:52:23,896 that zero always means success. 1187 00:52:23,896 --> 00:52:25,666 You return zero, good stuff happens, 1188 00:52:25,976 --> 00:52:28,936 but in case you made any number of the billion possible mistakes 1189 00:52:28,936 --> 00:52:31,786 that someone can make, you got a billion other integers you can 1190 00:52:31,786 --> 00:52:34,866 return so that you can signify those errors. 1191 00:52:34,866 --> 00:52:38,306 Now I'm exaggerating, hopefully your not hand error checking 1192 00:52:38,306 --> 00:52:39,916 for a billion different things in a program. 1193 00:52:39,916 --> 00:52:42,346 Usually you check for one or two that can actually happen 1194 00:52:42,626 --> 00:52:43,696 but the point here is this. 1195 00:52:43,696 --> 00:52:46,046 I've decided arbitrarily by human convention 1196 00:52:46,366 --> 00:52:48,786 if this thing goes wrong I'm going to return this 1197 00:52:48,786 --> 00:52:51,896 so called sentinel value, just a special arbitrary number, 1198 00:52:52,136 --> 00:52:54,956 the number 1 why, because its different from zero 1199 00:52:55,076 --> 00:52:56,496 and that's really all it is. 1200 00:52:56,496 --> 00:52:59,626 So if you've ever been using your Mac or your pc and you all 1201 00:52:59,626 --> 00:53:01,896 of a sudden get some popup that says an error has occurred 1202 00:53:01,896 --> 00:53:03,516 or it freezes, very often 1203 00:53:03,516 --> 00:53:05,346 in these little dialog windows do you see a number. 1204 00:53:05,346 --> 00:53:06,886 Maybe it positive, maybe it's negative, 1205 00:53:07,056 --> 00:53:09,306 it generally means nothing to you but guess 1206 00:53:09,306 --> 00:53:10,446 who it does mean something to? 1207 00:53:11,026 --> 00:53:12,856 Right the programmer. 1208 00:53:12,856 --> 00:53:14,876 The person at Microsoft, Apple or whoever who wrote it, 1209 00:53:14,876 --> 00:53:18,446 so not all that useful for you unless you call tech support 1210 00:53:18,446 --> 00:53:20,066 and tell them what you're seeing on the screen 1211 00:53:20,256 --> 00:53:21,316 but these error codes. 1212 00:53:21,316 --> 00:53:24,286 These return codes are generally used by the developer, 1213 00:53:24,286 --> 00:53:27,126 the programmer to actually understand what's going wrong. 1214 00:53:27,126 --> 00:53:29,016 So here by convention I'm returning 1. 1215 00:53:29,286 --> 00:53:32,336 Really this program is so simple it doesn't matter what I return 1216 00:53:32,476 --> 00:53:34,836 but again, convention, return a non-zero value. 1217 00:53:35,126 --> 00:53:37,396 All right so let's assume that the user is not being difficult 1218 00:53:37,396 --> 00:53:39,926 and I'm proceeding to sing this annoying song so I'm going 1219 00:53:39,926 --> 00:53:42,106 to print a new line first, just for aesthetic reasons 1220 00:53:42,316 --> 00:53:44,666 and it turns out this program is not that hard to implement. 1221 00:53:44,666 --> 00:53:46,656 Let me go ahead and iterate from I equals, 1222 00:53:46,656 --> 00:53:49,256 oh this is interesting, so even though thus far, 1223 00:53:49,256 --> 00:53:51,736 just by habit I've been incrementing from zero on up 1224 00:53:51,736 --> 00:53:54,836 or one on up, there is nothing stopping you from doing it 1225 00:53:54,836 --> 00:53:56,876 in the reverse direction, in fact here, 1226 00:53:56,876 --> 00:53:59,726 because conceptually I'm starting at 99 and going 1227 00:53:59,726 --> 00:54:03,176 on down, I don't want to confuse myself by starting the loop 1228 00:54:03,176 --> 00:54:05,326 at zero by printing the opposite, 99, 1229 00:54:05,326 --> 00:54:07,086 let's just start at 99. 1230 00:54:07,156 --> 00:54:10,346 so int I gets 99 or whatever the value of n is, 1231 00:54:10,616 --> 00:54:14,296 I'm going to sing this song so long as I is greater than zero, 1232 00:54:14,626 --> 00:54:16,446 maybe the song is supposed to end at zero or one, 1233 00:54:16,446 --> 00:54:18,636 I wasn't sure so I said keep going until its zero, 1234 00:54:18,636 --> 00:54:20,436 we definitely don't want to go negative and then 1235 00:54:20,436 --> 00:54:22,626 on each iteration do I minus minus 1236 00:54:23,036 --> 00:54:24,846 and this recalls a shorthand quotation. 1237 00:54:24,846 --> 00:54:26,686 We saw plus plus even as far back 1238 00:54:26,686 --> 00:54:28,226 as our sock changing example, 1239 00:54:28,536 --> 00:54:30,206 but really this is shorthand notation 1240 00:54:30,206 --> 00:54:34,476 for I gets the assignment operator, I minus 1, right? 1241 00:54:34,476 --> 00:54:39,336 or if you do the plus plus that is equivalent to I plus one 1242 00:54:39,786 --> 00:54:42,196 or there's yet other shorthand notation you might see 1243 00:54:42,196 --> 00:54:44,806 in on line resources or the TF's code or in books, 1244 00:54:45,096 --> 00:54:50,366 you can do I minus equals 1 to decrement or I plus equals one 1245 00:54:50,366 --> 00:54:53,166 to increment, frankly this is why most people just do I plus 1246 00:54:53,166 --> 00:54:55,696 plus or minus minus because its much more straight forward. 1247 00:54:55,776 --> 00:54:59,996 As an aside there is also a minus minus operator 1248 00:54:59,996 --> 00:55:01,876 and a plus plus operator. 1249 00:55:02,186 --> 00:55:04,046 In many context they do the same thing, 1250 00:55:04,336 --> 00:55:06,776 in some context they don't, we'll see over time when 1251 00:55:06,776 --> 00:55:09,496 and if you should actually care but generally put it after 1252 00:55:09,496 --> 00:55:10,796 and you'll be pretty safe. 1253 00:55:11,026 --> 00:55:13,556 So let's take a look at this song, its got four lines, 1254 00:55:13,886 --> 00:55:16,416 prints some number of bottles and notice here, 1255 00:55:16,416 --> 00:55:18,986 I'm already kind of taking a shortcut, I didn't want to have 1256 00:55:18,986 --> 00:55:19,916 to write too much code. 1257 00:55:20,086 --> 00:55:21,086 What's my cheat here? 1258 00:55:21,086 --> 00:55:22,376 >> [inaudible] 1259 00:55:22,376 --> 00:55:24,706 >> Yeah bottles, like cut this corner, right? 1260 00:55:24,706 --> 00:55:27,416 it's not wrong grammatically its just the parenthesis are only 1261 00:55:27,416 --> 00:55:30,076 useful eventually so we'll get there right? 1262 00:55:30,076 --> 00:55:30,676 baby steps. 1263 00:55:30,676 --> 00:55:33,376 so the first line is print some number of bottles of beer 1264 00:55:33,376 --> 00:55:36,206 on the wall and I'm inserting to that format string, 1265 00:55:36,206 --> 00:55:40,936 that placeholder I, some number of bottles of beer, same thing, 1266 00:55:41,146 --> 00:55:43,496 , I, take one down and pass it around, that's just text 1267 00:55:43,496 --> 00:55:46,166 so notice printf again, it doesn't have to take any commas, 1268 00:55:46,166 --> 00:55:47,796 or anything after it, now I have 1269 00:55:47,826 --> 00:55:49,396 to do a little bit of math but not much. 1270 00:55:49,746 --> 00:55:52,716 Some number of bottles of beer on the wall but now I want 1271 00:55:52,716 --> 00:55:53,766 to do the decrematation. 1272 00:55:54,546 --> 00:55:57,636 So now is there a risk that I'm actually going 1273 00:55:57,816 --> 00:56:04,256 to say negative one at any point? 1274 00:56:04,256 --> 00:56:04,436 >> [inaudible] 1275 00:56:04,436 --> 00:56:06,306 >> Disagreement so push back. 1276 00:56:06,306 --> 00:56:07,556 What's, how is this happening? 1277 00:56:07,556 --> 00:56:08,206 Well let's see. 1278 00:56:08,436 --> 00:56:09,886 There's a loop here, we're starting at n 1279 00:56:09,886 --> 00:56:13,356 so we're definitely starting big but I'm checking this condition 1280 00:56:13,356 --> 00:56:18,316 on every iteration is I greater than zero so its only going 1281 00:56:18,316 --> 00:56:21,226 to be the case, even at the very last iteration that I has 1282 00:56:21,226 --> 00:56:23,626 to be greater than zero, so on my very last pass 1283 00:56:24,026 --> 00:56:25,876 through this loop I is going to be minimally. 1284 00:56:26,476 --> 00:56:30,766 So 1, so if I is one I think I'm safe, so 1 gets printed here, 1285 00:56:30,876 --> 00:56:32,946 because its I, 1 gets printed here because its I, 1286 00:56:32,946 --> 00:56:36,456 1 gets printed here, or rather, no, zero gets printed here 1287 00:56:36,456 --> 00:56:38,906 because its I minus one but recall what happens 1288 00:56:38,906 --> 00:56:41,536 with a 4 loop after each iteration, you don't go back 1289 00:56:41,776 --> 00:56:45,896 to the left of this thing, its the update that happens, 1290 00:56:46,216 --> 00:56:48,416 I becomes 0, the condition is checked 1291 00:56:48,416 --> 00:56:50,136 and so in fact we are safe. 1292 00:56:50,426 --> 00:56:53,156 So its close but we are in fact safe because I'm stopping 1293 00:56:53,156 --> 00:56:56,546 so long as I is greater than or equal to 0, the error would be 1294 00:56:56,966 --> 00:56:58,156 if I did something like this 1295 00:56:58,506 --> 00:57:00,776 or any other number of logical quirks. 1296 00:57:01,226 --> 00:57:03,686 So now what's one difference toward the end of this program? 1297 00:57:03,686 --> 00:57:05,846 Well at the end I decide to exit and no 1298 00:57:05,846 --> 00:57:09,086 because I've used a non-zero return value technically I don't 1299 00:57:09,086 --> 00:57:11,866 need this return 0 because 0 will be assumed but as a matter 1300 00:57:11,866 --> 00:57:14,386 of good style, if I'm going to start taking the trouble 1301 00:57:14,386 --> 00:57:17,116 to return values explicitly let's be consistent 1302 00:57:17,116 --> 00:57:18,616 and actually return 0 at the end. 1303 00:57:18,896 --> 00:57:21,106 For all intents and purposes, certainly in this course, 1304 00:57:21,316 --> 00:57:23,086 its not generally going to matter 1305 00:57:23,196 --> 00:57:25,326 but when your programs do get larger 1306 00:57:25,326 --> 00:57:27,856 and more sophisticated is this a very useful habit 1307 00:57:28,026 --> 00:57:28,926 to already be in. 1308 00:57:29,246 --> 00:57:31,706 but let's see if we can, well let's actually sing the song, 1309 00:57:31,706 --> 00:57:33,596 so let's, well that would be awkward, 1310 00:57:33,596 --> 00:57:35,416 but let's just do the program version. 1311 00:57:35,986 --> 00:57:38,236 Make beer, let's run this first. 1312 00:57:38,236 --> 00:57:40,836 So make beer one, let's go ahead and run beer one. 1313 00:57:41,106 --> 00:57:42,326 How many bottles would there be? 1314 00:57:42,426 --> 00:57:44,386 Well its not going to fit on the screen but let's try it. 1315 00:57:44,856 --> 00:57:48,636 99 and sure enough, if I scroll back up in time it's a whole lot 1316 00:57:48,636 --> 00:57:51,356 of iteration going on, seems to check out at the top, 1317 00:57:51,356 --> 00:57:55,976 99 goes down to 98, 97 and at the very bottom 0 bottles 1318 00:57:55,976 --> 00:57:57,816 of beer on the wall so not a great program, 1319 00:57:57,816 --> 00:57:59,216 because I'm totally cutting corners 1320 00:57:59,216 --> 00:58:07,016 with the parenthesization of the s, but its not bad. 1321 00:58:07,766 --> 00:58:08,956 Yeah? Oh , good question. 1322 00:58:08,956 --> 00:58:12,276 So when in doubt let's just answer it definitively. 1323 00:58:12,276 --> 00:58:14,706 So let's go ahead and do what's pretty reasonable, 1324 00:58:14,706 --> 00:58:16,906 maybe I'm not even quite sure early on of the logic 1325 00:58:16,906 --> 00:58:20,396 so let's resave that, go down, sorry, that's not correct, 1326 00:58:20,756 --> 00:58:23,576 greater than or equal to, so the syntax follows the English, 1327 00:58:23,606 --> 00:58:26,626 greater than or equal to 0 so I argue this is going 1328 00:58:26,626 --> 00:58:27,916 to take us one step too far. 1329 00:58:27,986 --> 00:58:31,296 It will compile, so this is not a compilation error, 1330 00:58:31,296 --> 00:58:32,806 this is s logical error. 1331 00:58:32,806 --> 00:58:36,286 This is a programming conceptual mistake, not a syntax error, 1332 00:58:36,566 --> 00:58:38,446 so let me go ahead and no rerun beer one, 1333 00:58:38,446 --> 00:58:41,216 doesn't matter what it is, I could do 199, its still going 1334 00:58:41,216 --> 00:58:44,836 to run fast and in fact now I see this aesthetic quirk. 1335 00:58:45,296 --> 00:58:48,306 So I see in fact a negative 1 because percent d just means one 1336 00:58:48,306 --> 00:58:55,756 or more digits whether positive or negative. 1337 00:58:55,906 --> 00:58:56,226 Yeah? 1338 00:58:56,226 --> 00:58:56,336 >> [inaudible] 1339 00:58:56,336 --> 00:58:58,976 >> Oh, I'm sorry, okay I'm sorry, I misunderstood, 1340 00:58:59,206 --> 00:59:03,176 so let's do that too, so let me go back and let's say, 1341 00:59:03,176 --> 00:59:04,596 let's just rerun it with this 1342 00:59:04,636 --> 00:59:07,626 so negative 99 beers, I should catch that. 1343 00:59:08,006 --> 00:59:11,226 Because of my if condition, my error checking 1344 00:59:11,546 --> 00:59:14,906 at the top of the function. 1345 00:59:14,906 --> 00:59:15,236 >> [inaudible] 1346 00:59:15,236 --> 00:59:16,476 >> Oh, okay. 1347 00:59:16,476 --> 00:59:18,046 so this is where I say for all intents 1348 00:59:18,046 --> 00:59:20,346 and purposes it doesn't matter in this course 1349 00:59:20,346 --> 00:59:22,576 because you don't see the return values but in a week 1350 00:59:22,576 --> 00:59:25,056 or two's time you're going to find that PrinTif 1351 00:59:25,186 --> 00:59:28,186 as a debugging tool, a tool with which to find mistakes 1352 00:59:28,186 --> 00:59:29,996 in programs is only so useful. 1353 00:59:29,996 --> 00:59:33,326 When your programs get, start taking input from users 1354 00:59:33,326 --> 00:59:35,576 and you might have a lot of variables in mind, 1355 00:59:35,576 --> 00:59:37,666 you can't just rely on PrinTif as a useful tool so we're going 1356 00:59:37,666 --> 00:59:39,226 to start using what's called a debugger. 1357 00:59:39,486 --> 00:59:40,086 Something called GGDB. 1358 00:59:40,086 --> 00:59:44,036 You may recall from last week I just mentioned terribly briefly 1359 00:59:44,256 --> 00:59:46,506 that there's this other flag GGDB 1360 00:59:46,676 --> 00:59:48,126 that includes some special stuff 1361 00:59:48,276 --> 00:59:50,366 in your program when you compile it. 1362 00:59:50,546 --> 00:59:52,236 well, long story short, we're going to get 1363 00:59:52,236 --> 00:59:54,386 to the point very soon of not only being able 1364 00:59:54,386 --> 00:59:56,866 to run programs you write but be able to run them 1365 00:59:56,866 --> 00:59:59,306 in a simulation mode whereby you can step 1366 00:59:59,446 --> 01:00:02,166 through your own code line by line by line. 1367 01:00:02,436 --> 01:00:03,946 Interact with it, change it and in 1368 01:00:03,946 --> 01:00:07,636 that context will be more useful to know what the return value is 1369 01:00:07,636 --> 01:00:09,426 because you'll be able to see it in that simulator. 1370 01:00:09,426 --> 01:00:09,696 >> [inaudible] 1371 01:00:09,696 --> 01:00:14,316 >> So it's a good question. 1372 01:00:14,366 --> 01:00:16,426 In the first one, it's not really necessary. 1373 01:00:16,426 --> 01:00:19,056 Your welcome too so long as your consistent with these lessons, 1374 01:00:19,336 --> 01:00:21,006 return 0 for instance by default 1375 01:00:21,366 --> 01:00:24,126 but at some point soon will you need to get to that point, 1376 01:00:24,126 --> 01:00:26,126 and if you are, in fact already in problem set 1, 1377 01:00:26,326 --> 01:00:29,206 doing any error checking which you might very well be, 1378 01:00:29,206 --> 01:00:30,946 check the number of Skittles that are in the machine 1379 01:00:30,946 --> 01:00:32,486 and so forth, then absolutely. 1380 01:00:32,686 --> 01:00:35,166 Start returning 1 then 2 then 3 1381 01:00:35,386 --> 01:00:37,826 to represent arbitrarily your various return values. 1382 01:00:38,926 --> 01:00:40,746 But again, and we'll talk more about some of these details 1383 01:00:40,746 --> 01:00:43,936 in section but let's introduce a pretty bad bug here. 1384 01:00:43,936 --> 01:00:46,796 so let me go back into beer 1.c and let me scroll 1385 01:00:46,796 --> 01:00:48,136 on down, and you know what? 1386 01:00:48,136 --> 01:00:52,596 Let's actually omit this error check at the very top 1387 01:00:53,006 --> 01:00:53,946 so let me save that, 1388 01:00:53,946 --> 01:00:55,676 and actually let me fix the other mistake, 1389 01:00:56,336 --> 01:00:58,356 so we're not changing too many things at once, 1390 01:00:58,386 --> 01:01:00,226 I'm going to go back to the original but notice, 1391 01:01:00,226 --> 01:01:05,296 I've not done my original check, save, recompile beer 1, 1392 01:01:05,836 --> 01:01:11,666 rerun beer 1, whoops, rerun beer 1, 99, seems to work, 1393 01:01:11,666 --> 01:01:13,856 let's try negative 99. 1394 01:01:15,226 --> 01:01:21,386 Hm. So okay, so its not quite right, right? 1395 01:01:22,006 --> 01:01:25,116 so in fact let's make one other mistake just 1396 01:01:25,116 --> 01:01:27,946 to see how well we can really break the cloud here. 1397 01:01:28,286 --> 01:01:31,356 so let's actually suppose that I goof and I do, 1398 01:01:31,356 --> 01:01:33,636 while I is less than 0. 1399 01:01:34,366 --> 01:01:38,926 so let's recompile, and again this bang trick I'm doing, 1400 01:01:38,926 --> 01:01:41,956 again, exclamation point XYZ will actually get the last 1401 01:01:42,016 --> 01:01:45,196 command that started with XYZ so let me recompile this, oh, 1402 01:01:45,196 --> 01:01:47,236 and this is another message, if you get this and you, 1403 01:01:47,276 --> 01:01:49,086 I just want to recompile my program, 1404 01:01:49,086 --> 01:01:51,366 well you already did is what that message is telling you. 1405 01:01:52,206 --> 01:01:52,526 all right. 1406 01:01:52,926 --> 01:01:54,056 that ones come up before too. 1407 01:01:54,256 --> 01:01:57,056 So let's rerun beer1, 99 bottles. 1408 01:01:57,706 --> 01:02:03,486 Okay I've really broken this now let's be difficult, negative 99. 1409 01:02:03,486 --> 01:02:06,876 so what's going to happen. 1410 01:02:06,876 --> 01:02:12,686 Can we reason through what's going to happen? 1411 01:02:12,686 --> 01:02:14,036 Say that's a lot of ideas. 1412 01:02:14,366 --> 01:02:16,716 So a couple things could happen. 1413 01:02:16,716 --> 01:02:19,906 One, the assumption you have to make is this is an int, 1414 01:02:19,906 --> 01:02:22,246 even though I'm messing around with the input its still and int 1415 01:02:22,436 --> 01:02:25,676 and we know and int, like any data type in a computer is going 1416 01:02:25,676 --> 01:02:29,236 to be a finites size, probably 32 bits, maybe 64 bits 1417 01:02:29,236 --> 01:02:30,356 but in this case, 32 bits, 1418 01:02:30,646 --> 01:02:34,296 the biggest negative number we can represent with an int is 1419 01:02:34,296 --> 01:02:37,686 so negative 2 billion so frankly we might be here a while 1420 01:02:37,686 --> 01:02:39,106 if we keep waiting for this thing to go 1421 01:02:39,306 --> 01:02:41,456 but eventually you're going to go to negative 2 billion 1422 01:02:41,456 --> 01:02:43,116 and then you're going to keep going a little too much 1423 01:02:43,116 --> 01:02:44,406 and what's going to happen? 1424 01:02:44,406 --> 01:02:48,176 You're not going to overflow to another int in memory, 1425 01:02:48,246 --> 01:02:50,816 what's going to happen if you only have 32 bits and now you're 1426 01:02:50,816 --> 01:02:52,126 at the point of representing those, 1427 01:02:52,326 --> 01:02:53,806 the biggest negative one possible. 1428 01:02:54,906 --> 01:02:56,416 Its probably, something's going to change, right? 1429 01:02:56,416 --> 01:02:59,066 if you're just flipping 0's and 1's as zero becomes a 1 1430 01:02:59,066 --> 01:03:01,446 and 1 becomes a 0, just like the board exercise we did 1431 01:03:01,446 --> 01:03:02,926 and the hands being raised the other day. 1432 01:03:03,156 --> 01:03:05,836 If you only have so many premutatons of hands in the air 1433 01:03:05,836 --> 01:03:08,306 or 0's and 1's on the board, well eventually 1434 01:03:08,306 --> 01:03:11,136 if you keep adding 1 your probably going to wrap around. 1435 01:03:11,136 --> 01:03:13,456 Its wrong, but you're going to wrap around from what appears 1436 01:03:13,456 --> 01:03:14,196 to be a negative number 1437 01:03:14,196 --> 01:03:18,216 to what's an arbitrary positive number probably positive 2 1438 01:03:18,216 --> 01:03:19,556 billion and you'll get in this loop. 1439 01:03:19,706 --> 01:03:21,656 Now this really will take a while so let's see 1440 01:03:21,656 --> 01:03:24,746 if we can't speed up this bug, why don't we, 1441 01:03:24,846 --> 01:03:28,466 instead of doing this, we could do minus equals how 1442 01:03:28,566 --> 01:03:30,166 about 1000 at a time. 1443 01:03:30,336 --> 01:03:33,106 this program is 1000 times faster now, 1444 01:03:33,606 --> 01:03:37,406 negative 99 let's see if that's fast enough. 1445 01:03:37,406 --> 01:03:41,266 No that's still going to take a while. 1446 01:03:42,346 --> 01:03:51,416 Let's do, that's pretty fast, let's recompile, rerun, 1447 01:03:51,926 --> 01:03:55,596 be difficult again, okay. 1448 01:03:55,726 --> 01:03:58,306 I fixed it right? 1449 01:03:58,306 --> 01:03:59,216 it stopped. 1450 01:03:59,356 --> 01:04:02,576 So it didn't go positive it seems, I lied right? 1451 01:04:02,726 --> 01:04:07,356 so it did become positive, right? 1452 01:04:07,466 --> 01:04:11,006 the only condition in which this song ends and says, wow, 1453 01:04:11,006 --> 01:04:13,626 that's annoying, is when that condition is no longer true 1454 01:04:13,816 --> 01:04:17,096 but the condition I just changed was to be sure 1455 01:04:17,096 --> 01:04:19,906 that I is always less than 0 so logically 1456 01:04:19,936 --> 01:04:21,876 if this program stopped and I didn't kill it 1457 01:04:21,876 --> 01:04:23,636 like with control c, it just stopped on its own, 1458 01:04:23,636 --> 01:04:24,606 it said wow that's annoying, 1459 01:04:24,836 --> 01:04:28,746 it must be that I became 0 and in fact it did. 1460 01:04:28,746 --> 01:04:31,286 It simply wrapped around back to a positive value, 1461 01:04:31,406 --> 01:04:35,166 but condition exited so it kind of fixed the programs execution 1462 01:04:35,166 --> 01:04:37,786 but it didn't fix the logical error at hand. 1463 01:04:37,786 --> 01:04:39,836 So you have the correct version, I have made a complete mess 1464 01:04:39,926 --> 01:04:42,596 of this particular version, but the take away here is again, 1465 01:04:42,786 --> 01:04:45,596 not only do computers have a finite amount of precision, 1466 01:04:45,876 --> 01:04:51,206 they also have a finite amount of capabilities when it comes 1467 01:04:51,206 --> 01:04:54,376 to storing a large or potentially large number. 1468 01:04:54,456 --> 01:04:57,896 Let me point out one little teaser, if I may. 1469 01:04:58,026 --> 01:04:59,566 I pulled up this web site in advance, 1470 01:04:59,926 --> 01:05:02,986 some of you might have a ps3 or Wii or Xbox. 1471 01:05:03,426 --> 01:05:06,536 If you have a Wii you can crack these things, there's this 1472 01:05:06,536 --> 01:05:09,616 so called homebrew club that allows you to do fun things 1473 01:05:09,616 --> 01:05:12,976 with your Wii and I thought I would read this and it related 1474 01:05:12,976 --> 01:05:14,876 to this idea of computers only having 1475 01:05:14,956 --> 01:05:18,066 so much finite memory space and we'll see more on this 1476 01:05:18,066 --> 01:05:18,916 when we discus a raise. 1477 01:05:19,506 --> 01:05:21,756 How the Zelda TP Exploit works. 1478 01:05:21,916 --> 01:05:23,126 Stack, buffer over flow. 1479 01:05:23,126 --> 01:05:26,196 During a stack buffer over flow a program writes outside 1480 01:05:26,196 --> 01:05:28,926 of the intended program data structure in the memory address, 1481 01:05:28,926 --> 01:05:31,926 so not the same problem we just induced, but definitely related 1482 01:05:31,926 --> 01:05:34,606 to this idea of finite storage and maybe not checking 1483 01:05:34,606 --> 01:05:36,456 if we're using memory that doesn't belong to us. 1484 01:05:36,786 --> 01:05:38,396 This is used in the twilight house 1485 01:05:38,396 --> 01:05:41,636 so this is the twilight Zelda game so for the familiar, 1486 01:05:41,886 --> 01:05:45,546 when you're in a game, a lengthy name of Opona the horse is used 1487 01:05:45,546 --> 01:05:46,936 to generate a stack overflow, 1488 01:05:46,936 --> 01:05:48,876 this occurs during a conversation with a man 1489 01:05:49,116 --> 01:05:52,626 when you load your save, when he loads the name and in order, 1490 01:05:52,716 --> 01:05:54,976 when he loads the name to use in the dialog box 1491 01:05:54,976 --> 01:05:56,256 or when you're entering the next zone 1492 01:05:56,486 --> 01:05:57,696 where you have to ride a horse. 1493 01:05:57,696 --> 01:06:00,546 This is to say, we will start Wednesday's lecture not 1494 01:06:00,546 --> 01:06:03,346 with an explosion but with this particular gaming error. 1495 01:06:03,346 --> 01:06:06,926 See you then. 1496 01:06:07,236 --> 01:06:07,303 I ?? ?? ?? 1497 01:06:07,803 --> 01:06:10,690 ??