1 00:00:38,256 --> 00:00:39,296 >> Well this is CS15. 2 00:00:43,836 --> 00:00:46,276 All right, welcome back to the end of week one, 3 00:00:46,276 --> 00:00:47,956 this is our last Friday lecture, 4 00:00:47,956 --> 00:00:50,356 henceforth they will be Monday and Wednesdays only. 5 00:00:50,546 --> 00:00:52,616 A couple of announcements though, zero new handouts 6 00:00:52,616 --> 00:00:54,956 but there is code outside if you didn't get it on Wednesday. 7 00:00:55,226 --> 00:00:57,916 Office hours will be held one last time this evening for those 8 00:00:57,916 --> 00:00:59,946 of you who want to hang out in a group and work 9 00:00:59,946 --> 00:01:01,746 on scratch projects or just get help 10 00:01:01,806 --> 00:01:03,236 from the teaching fellows and CA's. 11 00:01:03,546 --> 00:01:04,366 Cavott [assumed spelling] dining Hall 12 00:01:04,366 --> 00:01:08,906 and Quincy dining hall per the, that is entirely false. 13 00:01:09,486 --> 00:01:12,866 [Laughter] No more office hours for PSet 0 but they will be held 14 00:01:12,866 --> 00:01:16,176 for PSet 1 I'm mis-remembering our own schedule. 15 00:01:16,176 --> 00:01:18,836 So office hours will generally be offered on Sunday, Monday, 16 00:01:18,836 --> 00:01:22,456 Tuesday, Wednesday, Thursday evenings at different supplies 17 00:01:22,456 --> 00:01:25,836 of staff members so do check the course's web site if you would 18 00:01:25,836 --> 00:01:28,756 like help with PSet 1 which will be released 19 00:01:28,756 --> 00:01:31,176 on the course's web site at 7:00 PM tonight. 20 00:01:31,176 --> 00:01:34,366 So this coming Monday we won't have proper sections just 21 00:01:34,366 --> 00:01:37,206 yet with your own intimate classrooms 22 00:01:37,206 --> 00:01:38,486 with single teaching fellows 23 00:01:38,486 --> 00:01:40,836 but rather we'll have a few Super Sections on Monday, 24 00:01:41,106 --> 00:01:42,386 Tuesday and Wednesday. 25 00:01:42,386 --> 00:01:44,616 Check the courses web site to find out the schedule. 26 00:01:45,136 --> 00:01:47,586 These are meant to help you, not only with the first couple 27 00:01:47,586 --> 00:01:50,826 of weeks material particularly the C and Linniks but also 28 00:01:50,826 --> 00:01:52,936 to help you with PSet 1 which will, 29 00:01:53,516 --> 00:01:55,756 again be released this evening but in addition to that, 30 00:01:56,116 --> 00:01:57,936 starting this Sunday, led by Marta, 31 00:01:57,936 --> 00:02:00,666 one of our teaching fellows, will be, hey Marta, 32 00:02:00,886 --> 00:02:03,886 will be the first of several walk throughs 33 00:02:03,886 --> 00:02:07,566 so once per week what we do is hold an course wide session 34 00:02:07,566 --> 00:02:10,006 that's a code walk through and its really meant 35 00:02:10,006 --> 00:02:12,466 to help you get started with the standard addition of the PSet. 36 00:02:12,556 --> 00:02:16,016 It will answer such questions as where do I even begin, 37 00:02:16,016 --> 00:02:18,366 what is this all about, what might be some strategies 38 00:02:18,366 --> 00:02:20,696 for tackling the problems set, those will be Sunday nights, 39 00:02:20,936 --> 00:02:23,116 check the web site for the schedule, they'll also be filmed 40 00:02:23,116 --> 00:02:24,736 and placed on line, usually by Tuesday 41 00:02:24,736 --> 00:02:26,596 so if you can't make it you'll still have that resource 42 00:02:26,866 --> 00:02:29,266 and they are meant to eliminate questions of the form 43 00:02:29,266 --> 00:02:31,096 and office hours, where do I begin? 44 00:02:31,376 --> 00:02:34,626 Finally PSet 1 so, it will in fact go out tonight, 45 00:02:35,126 --> 00:02:38,466 this will be your first PSet involving Linniks 46 00:02:38,466 --> 00:02:40,706 which may very well may be new topics to some of you. 47 00:02:40,706 --> 00:02:43,436 you'll find that the PSet walks you through a lot 48 00:02:43,436 --> 00:02:46,056 of the mechanical details that we looked at on Wednesday 49 00:02:46,246 --> 00:02:48,646 in great detail so you should not feel yourself 50 00:02:48,646 --> 00:02:50,536 in over your head at all and you'll find 51 00:02:50,536 --> 00:02:53,566 in the standard edition, which again, is meant for 90 percent 52 00:02:53,566 --> 00:02:55,676 of the class, a few challenges. 53 00:02:55,896 --> 00:02:58,056 So the first one is going to introduce you 54 00:02:58,056 --> 00:02:59,656 to the world of ISBNs. 55 00:03:00,076 --> 00:03:02,416 Not a terribly sexy field but these are the numbers 56 00:03:02,416 --> 00:03:04,556 that have been stamped on the back of most any book 57 00:03:04,806 --> 00:03:05,766 that you've ever purchased. 58 00:03:05,766 --> 00:03:07,606 Well it turns out those aren't just random numbers, 59 00:03:07,846 --> 00:03:08,966 there's some pattern to them 60 00:03:08,966 --> 00:03:11,106 and we can actually detect whether something is 61 00:03:11,106 --> 00:03:13,986 or is not a valid ISBN and what you'll find, 62 00:03:13,986 --> 00:03:15,986 though very much a bite sized problem, 63 00:03:16,216 --> 00:03:19,036 it will non the less get you acclimated, I think fairly well 64 00:03:19,226 --> 00:03:21,856 with some basic C syntax and with loops 65 00:03:21,936 --> 00:03:24,526 and with manipulating characters and or numbers. 66 00:03:24,526 --> 00:03:27,156 So you find that it's a nice introduction to that 67 00:03:27,156 --> 00:03:28,706 but then you'll dive into something that's more 68 00:03:28,706 --> 00:03:30,926 about thought and more about design. 69 00:03:30,926 --> 00:03:34,556 You'll be tasked with solving the change making problems 70 00:03:34,556 --> 00:03:35,986 so you've probably never thought 71 00:03:35,986 --> 00:03:38,196 and probably shouldn't have given it ever much thought 72 00:03:38,456 --> 00:03:40,346 to what the cashiers do at say, CDS, 73 00:03:40,626 --> 00:03:42,146 which is when you're due some change, 74 00:03:42,396 --> 00:03:43,536 they look in their cash drawer, 75 00:03:43,536 --> 00:03:46,066 they've got some 20's some 10's some one's some change 76 00:03:46,316 --> 00:03:49,036 and odds are that human goes from left to right or from right 77 00:03:49,036 --> 00:03:52,636 to left through their cash register in order 78 00:03:52,636 --> 00:03:54,506 to hand you some amount of change 79 00:03:54,556 --> 00:03:56,446 and if they're doing this intelligently 80 00:03:56,446 --> 00:03:58,676 or at least a bit methodically they're probably going 81 00:03:58,676 --> 00:04:02,206 to minimize the number of bills or coins that they hand you 82 00:04:02,486 --> 00:04:04,796 because no one likes to get 10 singles back 83 00:04:05,196 --> 00:04:06,326 when you're owed $10.00, 84 00:04:06,326 --> 00:04:08,396 there are more optimal solutions to that problem. 85 00:04:08,626 --> 00:04:10,866 But thinking about it, and actually expressing 86 00:04:10,866 --> 00:04:13,446 that algorithm that cashiers throughout the world have been 87 00:04:13,446 --> 00:04:17,426 executing for years is actually interesting in and of itself 88 00:04:17,426 --> 00:04:19,856 and so we'll have you implement what's generally known 89 00:04:19,856 --> 00:04:22,416 as a greedy algorithm for solving that problem 90 00:04:22,596 --> 00:04:25,576 of making change and then finally will you implement a 91 00:04:25,576 --> 00:04:28,016 little something reminiscent of Super Mario One, 92 00:04:28,276 --> 00:04:33,066 his little pyramid which will be a quick little exercise in loops 93 00:04:33,166 --> 00:04:36,026 and in printing and such and the hacker addition meanwhile 94 00:04:36,246 --> 00:04:37,216 for those of you who would 95 00:04:37,216 --> 00:04:40,136 like to elect it presents slightly more sophisticated 96 00:04:40,136 --> 00:04:42,846 problems throughout and in particular provides you 97 00:04:42,846 --> 00:04:44,566 with some sample credit card numbers 98 00:04:44,566 --> 00:04:48,116 and has you validate credit cards instead of actual ISBN's 99 00:04:48,116 --> 00:04:50,126 as the algorithms a bit more sophisticated. 100 00:04:50,506 --> 00:04:53,056 So with that said, that will be posted 101 00:04:53,056 --> 00:04:54,936 on line later this evening. 102 00:04:54,936 --> 00:04:59,716 I thought I would address one thing, so we were quite, 103 00:04:59,946 --> 00:05:03,676 well we were, its interesting to note 104 00:05:03,676 --> 00:05:05,216 that in the surveys you guys have been submitting 105 00:05:05,216 --> 00:05:07,386 for problem set 0 which still isn't due for several hours, 106 00:05:07,616 --> 00:05:10,466 most of you have just regular mobile phones which is great, 107 00:05:10,556 --> 00:05:11,846 because it means we don't have to spend 108 00:05:11,846 --> 00:05:13,776 as much energy developing, say, 109 00:05:13,776 --> 00:05:15,436 sophisticated iPhone applications 110 00:05:15,436 --> 00:05:17,556 when only 10 15 percent of you actually have them 111 00:05:17,826 --> 00:05:20,546 so we've actually optimized now Shuttleboy not only 112 00:05:20,546 --> 00:05:22,636 for your voice but also for SMS. 113 00:05:22,936 --> 00:05:25,786 Now this is a bit of a hype because this actually existed 114 00:05:25,786 --> 00:05:28,266 for several years but I kind of let it die for a little while 115 00:05:28,266 --> 00:05:30,886 since I didn't actually fix it, well its been fixed 116 00:05:30,886 --> 00:05:37,926 so now you can text a message like, and the succinct might 117 00:05:38,306 --> 00:05:40,026 like this, if you text a message 118 00:05:40,256 --> 00:05:44,396 to 411 this is what's called a short messaging code 119 00:05:44,396 --> 00:05:45,546 so there's people in the world 120 00:05:45,546 --> 00:05:47,536 that have these very short telephone numbers and you 121 00:05:47,536 --> 00:05:49,736 for final projects will actually be able to make use 122 00:05:49,736 --> 00:05:52,196 of precisely the service if you send a text message 123 00:05:52,196 --> 00:05:55,206 to that number and send it a message like, sboy, 124 00:05:55,546 --> 00:05:57,796 doesn't matter if its capitalized or lower case 125 00:05:57,846 --> 00:06:00,486 and then a and b, where a is your origin, 126 00:06:00,486 --> 00:06:03,636 b is your destination like Memorial Hall or like 127 00:06:03,636 --> 00:06:05,726 and followed by quad or you know, 128 00:06:05,806 --> 00:06:08,276 that's actually pretty tedious to type on a phone 129 00:06:08,276 --> 00:06:10,456 so you actually just use the first three letters, 130 00:06:10,786 --> 00:06:13,356 cryptic though this may be, memqua will get you 131 00:06:13,356 --> 00:06:15,046 to the quad right after class 132 00:06:15,206 --> 00:06:16,956 so if you actually send a text message there, 133 00:06:17,216 --> 00:06:20,516 what's actually happening is an interesting sequence of steps 134 00:06:20,516 --> 00:06:22,456 which is, a message gets sent to this number, 135 00:06:22,686 --> 00:06:25,896 this number has some fancy phone switching equipment for SMS, 136 00:06:26,246 --> 00:06:29,166 they route that request, whatever message you type in, 137 00:06:29,166 --> 00:06:32,556 back to one of our servers here on campus, our server then says, 138 00:06:32,556 --> 00:06:35,776 oh, here's two inputs, a and b and memqua, what is that, 139 00:06:36,216 --> 00:06:39,266 it parses those strings, analyzes them, figures out, oh, 140 00:06:39,266 --> 00:06:42,586 obviously this is Memorial Hall and obviously this is quad 141 00:06:42,586 --> 00:06:44,906 because there's no other stop starting with those letters, 142 00:06:45,166 --> 00:06:46,856 we do a look up in our own data base, 143 00:06:46,896 --> 00:06:48,376 much like this website here does 144 00:06:48,376 --> 00:06:51,066 and then return a fairly succinct explanation 145 00:06:51,066 --> 00:06:54,396 that the next shuttle is in two minutes at such and such a time, 146 00:06:54,396 --> 00:06:56,586 so we're using a little bit of web programming, 147 00:06:56,586 --> 00:06:59,556 we happened to use php and some Java Script here, 148 00:06:59,556 --> 00:07:02,896 can you implement precisely this kind of service on demand. 149 00:07:02,926 --> 00:07:05,316 So more on that toward terms end and some 150 00:07:05,316 --> 00:07:07,266 of you may dive in yourselves. 151 00:07:07,266 --> 00:07:08,286 But we left off here. 152 00:07:08,626 --> 00:07:12,136 So we introduced C, we introduced Linniks and we talked 153 00:07:12,136 --> 00:07:14,076 about some of the syntax but now we can start 154 00:07:14,076 --> 00:07:16,136 to do much more interesting things with this 155 00:07:16,136 --> 00:07:18,806 so int main cryptic string it first, 156 00:07:18,986 --> 00:07:21,106 but for now this is sort of a copy paste job. 157 00:07:21,106 --> 00:07:24,316 Whenever you start out writing a program for say, PSet 1, 158 00:07:24,676 --> 00:07:27,536 odds are its going to be in a file called something.C 159 00:07:27,796 --> 00:07:29,176 and then one of the first things you're going 160 00:07:29,176 --> 00:07:31,546 to do is write something like int main 161 00:07:31,726 --> 00:07:33,326 and then this expression in parenthesis. 162 00:07:33,326 --> 00:07:35,606 Your not going to put the semi-colon at the end 163 00:07:35,606 --> 00:07:38,116 because this program has no content, you're going 164 00:07:38,116 --> 00:07:40,116 to actually have a curly brace, some stuff 165 00:07:40,216 --> 00:07:43,156 and then a closed curly brace and that constitute your program 166 00:07:43,456 --> 00:07:45,046 and how do you make all this happen? 167 00:07:45,326 --> 00:07:48,246 Well, if you're on, for instance a Mac, recall that we began 168 00:07:48,246 --> 00:07:49,766 by running a program called Terminal, 169 00:07:49,916 --> 00:07:52,396 problems that once PDF will walk you through the process 170 00:07:52,396 --> 00:07:55,196 of making that icon appear if you don't yet actually have it. 171 00:07:55,486 --> 00:07:58,616 if you're on a p c what you'll do is run a program called putty 172 00:07:58,926 --> 00:08:00,426 which will give you a little window like this 173 00:08:00,426 --> 00:08:03,306 and again the program set will point you at a hand out on line 174 00:08:03,496 --> 00:08:05,196 for just clicking the right buttons and typing 175 00:08:05,196 --> 00:08:07,596 in the right words and you will then be connected to your 176 00:08:07,596 --> 00:08:09,866 so called FAS account at which point, 177 00:08:10,196 --> 00:08:13,976 your at the so called blinking prompt so we used a program 178 00:08:13,976 --> 00:08:15,866 on Wednesday to start writing a program. 179 00:08:15,866 --> 00:08:18,866 What was the recommended app that day? 180 00:08:19,416 --> 00:08:20,236 It's a little program. 181 00:08:20,996 --> 00:08:23,996 Nano. So nano is this very simple text editor, 182 00:08:24,166 --> 00:08:27,016 it allowed me to do something like Nanofooc [sounds like] 183 00:08:27,016 --> 00:08:29,316 and then I could just start typing some code 184 00:08:29,316 --> 00:08:31,346 and there was some stuff at the top of most files, 185 00:08:31,346 --> 00:08:34,196 I had comments thanks to the stars and the slashes, 186 00:08:34,446 --> 00:08:37,246 I had some pre-processor directives, 187 00:08:37,246 --> 00:08:40,516 that was the fancy way of saying sharp include, standard IO 188 00:08:40,516 --> 00:08:43,926 or CS50's library so we'll see some of that same syntax again 189 00:08:43,926 --> 00:08:45,716 but once I then had my program running, 190 00:08:46,046 --> 00:08:50,656 say hione.c I then ran gcchione.c hit enter, 191 00:08:50,876 --> 00:08:55,016 I then saw nothing happen but if I type LS I saw that oh, 192 00:08:55,016 --> 00:08:59,036 an out has just been created and if I in fact run about I 193 00:08:59,036 --> 00:09:00,876 in fact got the results as my program. 194 00:09:00,876 --> 00:09:03,726 So that's where we left off, we can do more interesting things 195 00:09:03,726 --> 00:09:05,776 than that so let's see what? 196 00:09:05,956 --> 00:09:07,856 We had this first. 197 00:09:08,116 --> 00:09:09,176 This ability to print. 198 00:09:09,546 --> 00:09:11,486 So this is a cryptic way of saying 199 00:09:11,656 --> 00:09:14,056 that print takes one argument 200 00:09:14,396 --> 00:09:16,496 which is generally called the format string, 201 00:09:16,896 --> 00:09:19,136 followed by some number of optional arguments 202 00:09:19,136 --> 00:09:22,186 so it's a pattern like this, I actually ripped 203 00:09:22,186 --> 00:09:25,206 out of the manual, the man page or this reference on line 204 00:09:25,436 --> 00:09:27,136 and that's just kind of a clue to me, the programmer, 205 00:09:27,136 --> 00:09:28,416 how do I use this function? 206 00:09:28,416 --> 00:09:30,796 Well this function takes constant char*. 207 00:09:30,996 --> 00:09:33,956 Well constant just means constant and forget 208 00:09:33,956 --> 00:09:37,736 about the meaning for today's purposes, char means character 209 00:09:37,976 --> 00:09:40,916 but I want to have multiple characters so in fact today 210 00:09:40,916 --> 00:09:42,286 for at least the next couple of weeks, 211 00:09:42,286 --> 00:09:45,226 whenever you see char* this is going to be synonymous 212 00:09:45,226 --> 00:09:47,306 with a word, or more generally a string. 213 00:09:47,676 --> 00:09:52,446 So a char is a single character, a char* is a string so to speak. 214 00:09:52,446 --> 00:09:54,886 It's a sequence of characters, like a word, a sentence, 215 00:09:54,886 --> 00:09:55,996 a paragraph, whatever. 216 00:09:55,996 --> 00:09:58,436 So char* just means that I can pass in a pair 217 00:09:58,436 --> 00:10:01,626 of double quotes inside of which is things like percent 218 00:10:01,626 --> 00:10:04,776 or percentile and some other things we'll see today. 219 00:10:05,286 --> 00:10:06,446 Now as for the ... 220 00:10:06,716 --> 00:10:07,406 what do you put there? 221 00:10:07,406 --> 00:10:10,196 well you plug in whatever variables whose values you want 222 00:10:10,196 --> 00:10:13,746 to be substituted into the percent or into the percents 223 00:10:14,176 --> 00:10:16,666 and then there's some escape sequences and escaping is going 224 00:10:16,666 --> 00:10:18,606 to be something that will occur throughout programming 225 00:10:18,606 --> 00:10:21,576 in general because you often need to treat special characters 226 00:10:21,576 --> 00:10:23,726 in a special way and these were just some 227 00:10:23,726 --> 00:10:25,226 of the ones that we saw. 228 00:10:25,636 --> 00:10:27,636 So we then started with some math 229 00:10:27,986 --> 00:10:30,986 and that was very uninteresting at first because I did 230 00:10:30,986 --> 00:10:35,036 like 1+2 equals 3 and then I did nothing with the answer but at 231 00:10:35,036 --> 00:10:37,906 that point we introduced the notion of a data type 232 00:10:38,306 --> 00:10:41,466 so variables in scratch are pretty much numbers 233 00:10:41,466 --> 00:10:43,616 that you can also pass in strings. 234 00:10:43,786 --> 00:10:45,766 The user can type in a string and that gets stored 235 00:10:45,766 --> 00:10:48,916 in that answer variable but you don't tell scratch that, 236 00:10:49,106 --> 00:10:49,416 you know what? 237 00:10:49,416 --> 00:10:51,336 I want to put a floating point value here. 238 00:10:51,336 --> 00:10:53,786 You don't tell scratch I want this to be a 32 bit 239 00:10:53,946 --> 00:10:57,936 or 64 bit variable, it does all those details by itself 240 00:10:57,936 --> 00:11:00,886 and after all, what 12 year old cares about that level of detail 241 00:11:00,886 --> 00:11:02,916 and that is in fact their target audience. 242 00:11:02,916 --> 00:11:06,026 Well we need a bit more power, a bit more expressiveness 243 00:11:06,026 --> 00:11:08,626 and you can INSEE as is the case with Java 244 00:11:08,626 --> 00:11:11,586 and some other languages specify what type of data you want 245 00:11:11,586 --> 00:11:13,756 to store in a variable and this is useful 246 00:11:13,756 --> 00:11:17,106 because sometimes the language will protect you from yourself, 247 00:11:17,266 --> 00:11:19,656 it won't allow you to perform, say arithmetic, 248 00:11:19,656 --> 00:11:21,346 on a string because that just doesn't make sense 249 00:11:21,346 --> 00:11:24,366 and other times it will allow the compiler, 250 00:11:24,456 --> 00:11:28,746 say GCC to optimize where those things go in memory 251 00:11:28,746 --> 00:11:29,926 or how their laid out. 252 00:11:30,146 --> 00:11:33,446 So C has these built in data types, but char, 253 00:11:33,446 --> 00:11:34,586 which is a single character. 254 00:11:34,586 --> 00:11:37,926 A double, which is a 64 bit real value. 255 00:11:37,926 --> 00:11:39,226 Something with a decimal point. 256 00:11:39,226 --> 00:11:43,436 A float is a 32 bit floating point value with a decimal point 257 00:11:43,436 --> 00:11:45,316 and then and int is just an integer. 258 00:11:45,566 --> 00:11:48,516 And again, a quick sanity check from, last time, 259 00:11:48,786 --> 00:11:52,536 how big was an int in say bits or bytes? 260 00:11:53,296 --> 00:11:54,916 32 bits right? 261 00:11:54,916 --> 00:11:56,926 so if you've ever heard this jargon in the media 262 00:11:56,926 --> 00:11:59,406 that this is a 64 bit architecture 263 00:11:59,406 --> 00:12:02,946 or this is a 32 bit computer well that's just a fancy way 264 00:12:02,946 --> 00:12:06,226 of saying how big are the integers that that computer uses 265 00:12:06,226 --> 00:12:08,036 and it actually relates for the size 266 00:12:08,036 --> 00:12:10,196 of something called a register which is a little piece 267 00:12:10,196 --> 00:12:12,316 of memory inside of the INTEL PTU 268 00:12:12,406 --> 00:12:15,116 so if you've ever seen this advertised 269 00:12:15,116 --> 00:12:18,846 and even Apple does this these days, 64 bit Zeyon Mac Book Pros 270 00:12:18,846 --> 00:12:21,616 and what not, its just referring, frankly to the size 271 00:12:21,616 --> 00:12:24,346 of an integer which has implications 272 00:12:24,346 --> 00:12:25,666 for how much ram you can have, 273 00:12:25,666 --> 00:12:27,606 how much disc space and other things. 274 00:12:27,906 --> 00:12:31,616 But there's these other types of data that you can use 275 00:12:31,616 --> 00:12:33,226 in C, there are longs. 276 00:12:33,466 --> 00:12:36,076 Now, longs a bit of a misnomer at least these days 277 00:12:36,076 --> 00:12:38,116 because a long is how many bits? 278 00:12:38,776 --> 00:12:42,956 So its also 32 and we saw that because very quickly at the end 279 00:12:42,956 --> 00:12:45,946 on Wednesday I used the size of operator and just printed 280 00:12:45,946 --> 00:12:48,386 out the size in bytes of an int and a long 281 00:12:48,466 --> 00:12:49,456 and they were the same. 282 00:12:49,456 --> 00:12:51,776 So we have this stupid need to say, not long, 283 00:12:51,776 --> 00:12:55,186 if you have 64 bits, but long long 284 00:12:55,186 --> 00:12:57,396 and then short actually goes the other direction, 285 00:12:57,476 --> 00:12:58,786 16 bits generally. 286 00:12:59,246 --> 00:13:04,506 Signed and unsigned are types and its technically signed int 287 00:13:04,506 --> 00:13:07,196 or unsigned int but you can abbreviate by just saying signed 288 00:13:07,196 --> 00:13:09,206 or unsigned as the word implies means 289 00:13:09,206 --> 00:13:11,406 that you can have negative numbers or you cannot. 290 00:13:11,826 --> 00:13:13,356 So if you have a signed integer, 291 00:13:13,356 --> 00:13:15,706 what's the largest positive number you can represent, 292 00:13:15,906 --> 00:13:16,376 give or take? 293 00:13:17,506 --> 00:13:20,126 so 2 billion because you need to go negative 2 billion 294 00:13:20,126 --> 00:13:21,236 if you want negative numbers 295 00:13:21,266 --> 00:13:23,686 but an unsigned int gives you literally twice 296 00:13:23,686 --> 00:13:26,756 as many positive values because you can now go from 0 297 00:13:26,756 --> 00:13:30,306 to 4 billion give or take and that's actually important 298 00:13:30,306 --> 00:13:32,276 when you start to write programs that actually care 299 00:13:32,276 --> 00:13:35,686 about large data or big chunks of memory or things like that. 300 00:13:36,026 --> 00:13:38,276 So those are our data types, the format strings 301 00:13:38,276 --> 00:13:39,786 that pertain to these are several. 302 00:13:39,936 --> 00:13:42,846 So this is another sort of RTFM if you want to figure 303 00:13:42,846 --> 00:13:44,236 out what's the right one for what 304 00:13:44,236 --> 00:13:46,466 but for the most part you'll start using percent S 305 00:13:46,466 --> 00:13:50,646 for strings, percent C for chars, percent D for ints 306 00:13:50,906 --> 00:13:56,206 and for long longs, just FYI its percent LLD for long D 307 00:13:56,606 --> 00:13:59,956 so the rest though are generally not going to be as useful, 308 00:13:59,956 --> 00:14:02,496 you can have like scientific notation and other things there 309 00:14:02,546 --> 00:14:04,746 that just aren't as often used, I would say. 310 00:14:05,016 --> 00:14:08,676 so C has arithmetic, we saw plus we saw assignments, 311 00:14:08,676 --> 00:14:10,096 not to be confused with the equal sign, 312 00:14:10,096 --> 00:14:14,206 but FYI you can do subtraction, multiplication, division 313 00:14:14,256 --> 00:14:17,846 and this last one, which is actually a little interesting, 314 00:14:17,846 --> 00:14:20,996 so there's the arithmetic remainder operator 315 00:14:20,996 --> 00:14:23,256 or there are slight distinctions, modules. 316 00:14:23,656 --> 00:14:27,226 Now in this percent sign means you can do X percent Y 317 00:14:27,636 --> 00:14:31,646 in a program and that is similar to doing x divided by Y 318 00:14:31,956 --> 00:14:36,886 but what the X percent Y does is that it returns not the result 319 00:14:36,886 --> 00:14:38,166 of dividing one into the other 320 00:14:38,166 --> 00:14:40,786 but rather the remainder if you do so. 321 00:14:41,026 --> 00:14:42,346 And as simple as this is, 322 00:14:42,346 --> 00:14:45,826 this will prove an incredibly useful trick, through all sorts 323 00:14:45,826 --> 00:14:48,546 of context when writing software so this ability 324 00:14:48,546 --> 00:14:51,926 to get remainders we'll see sill actually be a very neat trick. 325 00:14:52,346 --> 00:14:53,926 And this table is just meant as a reference 326 00:14:54,026 --> 00:14:56,146 so it's not interesting at all to dwell on this 327 00:14:56,426 --> 00:14:58,726 but C has a whole bunch of operators, right? 328 00:14:58,726 --> 00:15:01,306 There's a plus, minus, division, multiplication. 329 00:15:01,366 --> 00:15:05,116 We've seen the equals sign, 330 00:15:05,156 --> 00:15:07,566 we'll see less than, and greater than. 331 00:15:07,806 --> 00:15:09,496 So there's all these punctuation symbols 332 00:15:09,496 --> 00:15:12,306 that have some mathematical or some programmatic meaning, 333 00:15:12,596 --> 00:15:15,746 well the world had to decide which of those takes priority 334 00:15:15,746 --> 00:15:17,666 if you've got a whole bunch of them in a row. 335 00:15:17,926 --> 00:15:22,506 In other words if you have in Middle School arithmetic, 336 00:15:22,906 --> 00:15:27,956 X plus Y times Z, which of those operations are you supposed 337 00:15:27,956 --> 00:15:28,906 to perform first? 338 00:15:30,266 --> 00:15:31,976 Y times Z right? 339 00:15:31,976 --> 00:15:33,616 You can parenthesize it to be clear 340 00:15:33,616 --> 00:15:35,926 but your teachers probably taught you if you have a plus 341 00:15:36,066 --> 00:15:38,896 and a multiplication you do the multiplication first. 342 00:15:38,896 --> 00:15:42,276 So C has the same thing, anytime you have multiple operators 343 00:15:42,276 --> 00:15:44,576 involved in some kind of statement, a line of code, 344 00:15:44,996 --> 00:15:47,586 someone's got to will if there's a debate as to 345 00:15:47,586 --> 00:15:52,436 who should get evaluated first so realizing, we'll revisit this 346 00:15:52,436 --> 00:15:54,636 over time, there's a whole set of rules as to 347 00:15:54,636 --> 00:15:57,276 who gets evaluated first and you just kind of learn as you go 348 00:15:57,276 --> 00:15:58,356 which ones more involved. 349 00:15:58,356 --> 00:16:00,446 Which one is more powerful than the other? 350 00:16:00,776 --> 00:16:03,086 So now let's start to do things slightly more interesting 351 00:16:03,266 --> 00:16:06,166 aesthetically so within precision, all right, 352 00:16:06,206 --> 00:16:08,236 this is a really uninteresting way of saying 353 00:16:08,406 --> 00:16:09,846 that we can do something like this. 354 00:16:10,206 --> 00:16:13,806 So let me go ahead and open from your printouts, which again, 355 00:16:13,806 --> 00:16:18,256 will continue today, this bit of code here, this is Math 3.C 356 00:16:18,596 --> 00:16:21,846 so I propose that this is a program that takes two numbers, 357 00:16:22,086 --> 00:16:25,646 17 and 13, divides one by the other and then prints 358 00:16:25,646 --> 00:16:27,036 out that answer and I seem 359 00:16:27,036 --> 00:16:28,616 to have done a pretty good job here, right? 360 00:16:28,786 --> 00:16:30,696 I'm specifying my type as a float, 361 00:16:30,816 --> 00:16:33,666 I'm giving my variable a name, here is the top, 362 00:16:33,666 --> 00:16:37,636 here is the bottom, divide is my operator and okay, at this point 363 00:16:37,636 --> 00:16:40,336 in the story I have my answer inside the variable 364 00:16:40,336 --> 00:16:41,556 called answer. 365 00:16:41,926 --> 00:16:43,036 So what do I then do? 366 00:16:43,036 --> 00:16:45,636 Well, hm, this is slightly new syntax, 367 00:16:45,636 --> 00:16:48,416 let's ignore the part I don't understand, its not even there 368 00:16:48,626 --> 00:16:49,916 so that's really just percent F/N. 369 00:16:51,496 --> 00:16:53,306 so that means put floating point value here, 370 00:16:53,306 --> 00:16:55,876 then put a new line character what value do you want 371 00:16:55,876 --> 00:16:56,696 to substitute in? 372 00:16:56,896 --> 00:16:58,746 Well you substitute in the answer. 373 00:16:59,086 --> 00:17:01,606 Well .2 simply allows us 374 00:17:01,636 --> 00:17:05,106 to control how many decimal points do we want to show? 375 00:17:05,196 --> 00:17:09,666 .2 means give me 2 numbers after the decimal point as opposed 376 00:17:09,666 --> 00:17:10,936 to say, an infinite number 377 00:17:10,936 --> 00:17:13,356 which a real number fundamentally could be 378 00:17:13,516 --> 00:17:15,796 but there's kind of a problem here so let me go ahead 379 00:17:15,796 --> 00:17:19,036 and run GCC of math.C but you know what, 380 00:17:19,036 --> 00:17:23,086 so I can tell my programs apart I'm going to use -O math 3 just 381 00:17:23,086 --> 00:17:24,416 to keep things straight, enter. 382 00:17:24,806 --> 00:17:25,766 Okay, all is good. 383 00:17:25,766 --> 00:17:29,676 I'm going to run math 3 and then hit enter and hm, 384 00:17:30,966 --> 00:17:32,376 interesting, let me go back in. 385 00:17:32,376 --> 00:17:37,136 so 1.0 is the answer but 17 divided 386 00:17:37,136 --> 00:17:40,886 by 13 I'm pretty sure is not 1.00. 387 00:17:41,896 --> 00:17:43,396 So how did I screw up? 388 00:17:43,446 --> 00:17:45,446 >> [inaudible] 389 00:17:45,496 --> 00:17:49,826 >> Yeah it seems to be evaluating them as integers 390 00:17:49,826 --> 00:17:51,746 or we're clearly getting the wrong number, 391 00:17:51,746 --> 00:17:53,676 the wrong answer buy why might that be? 392 00:17:53,676 --> 00:17:55,196 Well 17 divided by 13. 393 00:17:55,196 --> 00:17:56,016 Well what is 17? 394 00:17:56,016 --> 00:17:59,156 You know, frankly it looks like 17 is just an integer. 395 00:17:59,156 --> 00:18:01,306 If I ever write down on a piece of paper, an integer, 396 00:18:01,446 --> 00:18:06,716 I write 17, I don't write 17.0 but the moment I write .0 now 397 00:18:06,716 --> 00:18:08,836 that compiler, GCC knows, oh you know what? 398 00:18:09,206 --> 00:18:11,706 Yeah, it happens to be equal to 17 399 00:18:12,016 --> 00:18:15,796 but it actually should be stored as a floating point value 400 00:18:15,796 --> 00:18:19,086 so we have that ability to remember that bit of data 401 00:18:19,386 --> 00:18:20,696 after the decimal point and you know what? 402 00:18:20,876 --> 00:18:22,836 If I really want to do this right, let me change this 403 00:18:22,836 --> 00:18:25,946 to be .0 there so now I'm taking the floating point, 404 00:18:25,946 --> 00:18:29,386 divided by a floating point and what happens now in C, 405 00:18:29,386 --> 00:18:31,636 as in most languages, is a float, 406 00:18:31,746 --> 00:18:33,656 is sort of more powerful than an int. 407 00:18:33,656 --> 00:18:37,956 it kind of wins this battle and so the return value of X divided 408 00:18:37,956 --> 00:18:42,396 by Y here, if X or Y or both of X 409 00:18:42,396 --> 00:18:46,166 and Y are floating point values, is itself going to be a float? 410 00:18:46,476 --> 00:18:49,246 So if I now recompile this, let me go back to my prompt, 411 00:18:49,366 --> 00:18:53,726 I'm going to go ahead and run GCC on math 3.C, 412 00:18:54,026 --> 00:18:57,906 I get back my blinking prompt, I've run math 3 and there it is. 413 00:18:57,906 --> 00:19:01,336 1.31. Well maybe I want a little bit more precision, 414 00:19:01,336 --> 00:19:05,436 well let me go and change this, so not 2 but let's say 9. 415 00:19:05,786 --> 00:19:07,936 I have to save, and now notice again, 416 00:19:07,936 --> 00:19:10,096 a common mistake early on might be math 3. 417 00:19:10,736 --> 00:19:13,406 Hmm. It doesn't seem to have changed, so again you need 418 00:19:13,406 --> 00:19:15,636 to recompile at this point, so I've rerun GCC 419 00:19:15,706 --> 00:19:18,066 and now I run my program, okay. 420 00:19:18,146 --> 00:19:20,236 So now I've got 9 decimal points of precision. 421 00:19:20,366 --> 00:19:23,176 Well let me get a little greedy and say something like 99. 422 00:19:23,176 --> 00:19:26,286 I really need this to be a very precise answer, maybe its pi 423 00:19:26,286 --> 00:19:29,076 or something like that that should allow for that amount 424 00:19:29,076 --> 00:19:30,786 of precision so let me recompile this. 425 00:19:31,546 --> 00:19:34,156 Enter, and now rerun math 3. 426 00:19:34,156 --> 00:19:35,486 Interesting. 427 00:19:36,476 --> 00:19:37,756 Now maybe that's the case 428 00:19:37,756 --> 00:19:40,516 but I'm pretty sure this is a real number which should 429 00:19:40,516 --> 00:19:43,616 at least in grade school, have an infinite number of numbers 430 00:19:43,616 --> 00:19:45,186 after the decimal point, right? 431 00:19:45,746 --> 00:19:48,176 So why are we kind of bottoming out at some point 432 00:19:48,176 --> 00:19:49,836 and then getting just a whole lot of zeros? 433 00:19:50,246 --> 00:19:51,486 What limitation are we hitting? 434 00:19:51,486 --> 00:19:53,046 >> [inaudible] 435 00:19:53,046 --> 00:19:55,006 >> Yeah, so the size of a float in this case. 436 00:19:55,006 --> 00:19:58,126 So if you have a floating point value and you only have 32 bits, 437 00:19:58,496 --> 00:20:00,656 don't even think of bits as meaning a digit 438 00:20:00,656 --> 00:20:02,416 because you need more than one bit 439 00:20:02,416 --> 00:20:04,786 to represent a decimal digit, 0 through 9 440 00:20:05,136 --> 00:20:06,426 but its clearly a limit. 441 00:20:06,606 --> 00:20:09,136 Where is the limit, I'm even not quite sure, 442 00:20:09,136 --> 00:20:14,866 does it say that the biggest possible float is 4 billion.0 443 00:20:14,996 --> 00:20:16,556 or does it say its two billion 444 00:20:16,556 --> 00:20:19,836 but you can actually have more decimals, more numbers 445 00:20:19,836 --> 00:20:21,666 after the decimal point, but the point is 446 00:20:21,666 --> 00:20:24,496 that there's some finite limit, if you only have 32 bits, 447 00:20:24,826 --> 00:20:27,886 you only have so many numbers you can express, so all right, 448 00:20:27,886 --> 00:20:28,786 let's try to fix this. 449 00:20:28,786 --> 00:20:31,516 If I'm running into trouble, what can I do? 450 00:20:31,516 --> 00:20:33,516 All right, double, 451 00:20:33,516 --> 00:20:36,576 but unfortunately this doesn't really solve the problem 452 00:20:36,576 --> 00:20:38,806 fundamentally; let me try recompiling this, 453 00:20:39,306 --> 00:20:43,936 GCC and now rum math 3 so it's better. 454 00:20:44,296 --> 00:20:46,406 So I've got more numbers, they're going farther 455 00:20:46,406 --> 00:20:48,086 to the right but I'm still bottoming 456 00:20:48,086 --> 00:20:51,276 out so the take away here is that floating point values in C 457 00:20:51,276 --> 00:20:54,416 and in most any language are inherently limited. 458 00:20:54,586 --> 00:20:57,536 In fact, you will occasionally find that even if you're trying 459 00:20:57,536 --> 00:20:59,646 to represent a relatively small number, 460 00:20:59,646 --> 00:21:04,826 like 12345.67 it's not actually going 461 00:21:04,826 --> 00:21:12,356 to be 12345.670000000 ad infinitum underneath the hood 462 00:21:12,356 --> 00:21:14,766 there might be a little bit of rounding error there. 463 00:21:14,766 --> 00:21:27,086 It might be 12345.69999999 and so 464 00:21:27,086 --> 00:21:28,886 with an infinite number of 9 conceptually. 465 00:21:28,886 --> 00:21:30,216 In other words you cannot assume 466 00:21:30,216 --> 00:21:32,176 that there will be precision underneath the hood 467 00:21:32,326 --> 00:21:33,416 which means you should never, 468 00:21:33,796 --> 00:21:35,326 never if you take nothing away today 469 00:21:35,326 --> 00:21:37,946 from about floating point values, never try to compare 470 00:21:37,996 --> 00:21:39,986 on floating point value against the other 471 00:21:40,136 --> 00:21:42,556 if you want an exact answer, because you will run 472 00:21:42,556 --> 00:21:44,666 into these very subtle bugs that are the result 473 00:21:44,936 --> 00:21:47,026 of simply computers being limited in this way. 474 00:21:47,356 --> 00:21:51,706 Well let's take a look at math 4.C. so this is an approach 475 00:21:51,706 --> 00:21:53,916 that I very simply fixed the answer by making 476 00:21:53,916 --> 00:21:56,246 that denominator a floating point value 477 00:21:56,246 --> 00:21:58,066 and that would solve this problem as well. 478 00:21:58,066 --> 00:22:00,496 Now in Math 5, notice that I've done this. 479 00:22:01,486 --> 00:22:03,486 So this is a different syntax 480 00:22:03,486 --> 00:22:05,436 but it reveals another capability that we have 481 00:22:05,706 --> 00:22:08,086 so we can declare a float called answer, 482 00:22:08,286 --> 00:22:10,656 its equal to 17 divided by, well you know what? 483 00:22:10,976 --> 00:22:15,266 The programmers know that a number like 13, though an int, 484 00:22:15,476 --> 00:22:18,116 could also be viewed as a floating point value 485 00:22:18,386 --> 00:22:22,626 so you can force the compiler to treat what is not a float 486 00:22:22,756 --> 00:22:26,066 as a float if you're comfortable that's a legitimate conversion 487 00:22:26,066 --> 00:22:26,486 to make. 488 00:22:26,796 --> 00:22:31,326 So this use of the parenthetical float right here is simply our 489 00:22:31,326 --> 00:22:32,376 way of saying, you know what? 490 00:22:32,376 --> 00:22:35,426 Treat this not as an int but as a float and this is an instance 491 00:22:35,426 --> 00:22:40,576 of what's called typecasting and this is a very simple use for it 492 00:22:40,576 --> 00:22:44,096 but it will let us ultimately change characters to numbers, 493 00:22:44,096 --> 00:22:46,716 numbers to characters and recall from last week 494 00:22:46,716 --> 00:22:48,046 when we had a bunch of volunteers 495 00:22:48,046 --> 00:22:50,606 up here raising their hands and not raising their hands, 496 00:22:50,606 --> 00:22:55,156 representing numbers, we then converted those numbers to BOW, 497 00:22:55,266 --> 00:22:56,936 by way of the ASCII coding system, 498 00:22:57,106 --> 00:22:58,556 well we can do that in code. 499 00:22:58,556 --> 00:23:00,016 And in fact for problem set 2 500 00:23:00,016 --> 00:23:03,146 in the course what you will do is implement a number of ciphers 501 00:23:03,146 --> 00:23:04,866 so cryptographic mechanisms 502 00:23:05,136 --> 00:23:08,396 that convert essentially what is human readable text 503 00:23:08,726 --> 00:23:11,186 to something that's not human readable but you're going to do 504 00:23:11,186 --> 00:23:13,396 that by way of the underlying implementation 505 00:23:13,396 --> 00:23:15,606 of characters as numbers. 506 00:23:16,316 --> 00:23:20,136 All right, this is kind of basic, right? 507 00:23:20,286 --> 00:23:21,836 addition, subtraction all of this, 508 00:23:22,066 --> 00:23:24,636 well fortunately we can use other data types too 509 00:23:24,636 --> 00:23:27,126 in programs, similar to what we've seen in scratch, 510 00:23:27,376 --> 00:23:30,266 so C does not have a bullion data type. 511 00:23:30,626 --> 00:23:31,646 So there's no notion 512 00:23:31,646 --> 00:23:34,706 of a variable whose value can only be true or false. 513 00:23:35,096 --> 00:23:36,266 We could simulate this. 514 00:23:36,646 --> 00:23:38,666 So if I hand you the C programming language 515 00:23:38,666 --> 00:23:42,346 and I tell you there's no data type whose values can only be 0 516 00:23:42,346 --> 00:23:45,556 or 1, you know, how could you kind of work around that. 517 00:23:45,556 --> 00:23:46,796 What might your hack be? 518 00:23:46,796 --> 00:23:47,566 Your solution? 519 00:23:49,006 --> 00:23:51,506 To be more specific, what data type would I use 520 00:23:51,506 --> 00:23:52,426 if bool does not exist? 521 00:23:52,426 --> 00:23:53,966 >> [inaudible] 522 00:23:53,966 --> 00:23:56,706 >> Right, maybe just use an int and just use one 523 00:23:56,706 --> 00:23:58,136 of the 31 bits, right? 524 00:23:58,136 --> 00:24:00,006 just impose on yourself the constraint 525 00:24:00,206 --> 00:24:03,226 that any time you use a variable that you want to be true 526 00:24:03,226 --> 00:24:06,266 or false, just make sure that you only make it 0 527 00:24:06,496 --> 00:24:09,756 or you only make it 1 and yes, you're wasting some bits. 528 00:24:09,926 --> 00:24:10,826 Okay so let's push back. 529 00:24:11,136 --> 00:24:13,686 I don't need to waste 32 bits or 31, 530 00:24:13,866 --> 00:24:15,526 but the smaller data type I could use. 531 00:24:16,536 --> 00:24:20,186 So I could use a char even, a char is 8 bits or 1 byte 532 00:24:20,186 --> 00:24:23,506 and that's why we have 256 possible letters 533 00:24:23,506 --> 00:24:24,716 in the chart we showed last week, 534 00:24:24,996 --> 00:24:27,896 so 8 bits means I'm only wasting 7, that's not bad 535 00:24:28,196 --> 00:24:29,646 but its still unfortunate. 536 00:24:29,646 --> 00:24:31,606 But we, the cores have gone ahead and done this, 537 00:24:31,716 --> 00:24:34,866 so this thing called the CS50 library among, 538 00:24:34,906 --> 00:24:37,866 besides giving you functions like get string and get long 539 00:24:37,866 --> 00:24:40,856 and get int, also gives you a data type called bool 540 00:24:40,856 --> 00:24:42,206 and we just kind of cheat. 541 00:24:42,206 --> 00:24:45,296 Underneath the hood we just implement it as an int or char 542 00:24:45,296 --> 00:24:47,276 or a short and a string. 543 00:24:47,576 --> 00:24:50,216 Where string is literally just a synonym in our code, 544 00:24:50,216 --> 00:24:53,026 we'll see for char * and you'll find that early 545 00:24:53,026 --> 00:24:55,606 on in the course it's just nice to be able to assume 546 00:24:55,816 --> 00:24:57,306 that these things actually exist. 547 00:24:57,306 --> 00:25:00,176 so in fact if I go into high 2.C recall 548 00:25:00,176 --> 00:25:01,586 from Wednesday we had this example 549 00:25:01,586 --> 00:25:02,716 and I said, here's a string. 550 00:25:03,026 --> 00:25:05,176 Well that's not technically a string 551 00:25:05,176 --> 00:25:07,806 because that word doesn't exist, it only exists 552 00:25:07,896 --> 00:25:10,556 because I'm using this library, 553 00:25:10,876 --> 00:25:13,696 well what I've really typed here, which frankly in the first 554 00:25:13,696 --> 00:25:16,976 or so week of the course is unnecessarily complex, is that. 555 00:25:17,386 --> 00:25:18,606 It's exactly the same. 556 00:25:18,856 --> 00:25:20,106 so this is why at least 557 00:25:20,106 --> 00:25:22,296 in the first weeks you occasionally see mentions 558 00:25:22,296 --> 00:25:25,506 of chars and char * but sometimes you'll see strings 559 00:25:25,676 --> 00:25:27,116 but that's because very intentionally 560 00:25:27,116 --> 00:25:29,626 for just the first couple of weeks do we simplify some 561 00:25:29,626 --> 00:25:30,946 of these details but then 562 00:25:30,946 --> 00:25:32,916 when we quickly take the training wheels off 563 00:25:32,916 --> 00:25:35,356 so that you're not making assumptions after this course 564 00:25:35,356 --> 00:25:37,156 as to what does and does not exist. 565 00:25:37,636 --> 00:25:39,306 But here's what exists for the courses say 566 00:25:39,306 --> 00:25:40,266 for the next several weeks. 567 00:25:40,306 --> 00:25:41,966 There's a function called Get Char. 568 00:25:42,206 --> 00:25:44,816 Notice the capital G, capital C. there's a function called get 569 00:25:44,816 --> 00:25:48,766 double, get float, get ints get long long and get string. 570 00:25:49,036 --> 00:25:51,796 So there's actually no get long as I misspoke earlier 571 00:25:51,946 --> 00:25:53,006 because its really kind of pointless 572 00:25:53,006 --> 00:25:54,986 because it would essentially give you back an int. 573 00:25:55,416 --> 00:25:58,306 so let's go ahead and use one of these, I'm provised 574 00:25:58,306 --> 00:26:02,396 to try High 3.C. So we saw this, you recall on Wednesday, 575 00:26:02,726 --> 00:26:05,826 get string is interesting because, one, 576 00:26:06,596 --> 00:26:12,096 it doesn't take any input so print 577 00:26:12,096 --> 00:26:13,966 up took something we'll call arguments, 578 00:26:14,436 --> 00:26:15,746 something in between parenthesis, 579 00:26:15,746 --> 00:26:17,886 get string doesn't do that, some more on that next week 580 00:26:18,116 --> 00:26:20,066 but it does return some things, so again, 581 00:26:20,066 --> 00:26:23,526 we'll call it functions, can take input like print off does 582 00:26:23,946 --> 00:26:26,726 but they can also produce output like get string does 583 00:26:26,966 --> 00:26:29,176 by returning some answer that you had better keep 584 00:26:29,176 --> 00:26:31,886 around if you care about it as in this case here. 585 00:26:32,266 --> 00:26:34,126 Well in this case here, adder, 586 00:26:34,376 --> 00:26:36,696 now we've got something slightly more interesting. 587 00:26:36,726 --> 00:26:39,716 I hard coded a program on Wednesday that added 1 + 2. 588 00:26:40,116 --> 00:26:43,216 Big deal. Not a very useful program but what about this one? 589 00:26:43,466 --> 00:26:45,386 So let me go ahead and compile before we glance 590 00:26:45,386 --> 00:26:48,936 at the code, adder.C, okay? 591 00:26:48,936 --> 00:26:49,606 Interesting. 592 00:26:50,376 --> 00:26:52,646 Problem. What was the problem here, did we say? 593 00:26:52,646 --> 00:26:54,496 >> [inaudible] 594 00:26:54,496 --> 00:26:57,616 >> Yeah, so because I'm using CS50 library it's not sufficient 595 00:26:57,616 --> 00:27:01,276 to just say sharp, include, brackets, CS50.H I need 596 00:27:01,276 --> 00:27:03,556 to also tell GCC that, you know what? 597 00:27:03,756 --> 00:27:07,146 You need to link in with -L the library called CS50, 598 00:27:07,146 --> 00:27:09,436 a little bit of syntax and you get used to it, 599 00:27:09,436 --> 00:27:12,196 any time you see this undefined reference just think, oh, 600 00:27:12,196 --> 00:27:16,276 I'm missing the -L flag so now I can go ahead and run, 601 00:27:16,516 --> 00:27:24,316 actually let me add my -O flag, let me go back to Gcc-O adder.c 602 00:27:24,316 --> 00:27:26,746 and then the L-CS50 needs to go at the end. 603 00:27:26,926 --> 00:27:30,046 All right, now I've got a program called adder, hit enter, 604 00:27:30,146 --> 00:27:32,466 give me an integer, audience participation time. 605 00:27:32,776 --> 00:27:37,186 57. 12 I heard first. 606 00:27:37,406 --> 00:27:41,016 The sum of 57 and 12 is 69. 607 00:27:42,226 --> 00:27:45,066 Either that's very clever, or you, yeah, clever. 608 00:27:45,066 --> 00:27:48,826 You can do arithmetic faster than the teacher can right now. 609 00:27:49,196 --> 00:27:50,886 Excellent, so we've written a program 610 00:27:50,886 --> 00:27:53,406 that adds two random numbers together. 611 00:27:53,756 --> 00:27:55,336 So how did that actually work? 612 00:27:55,336 --> 00:27:58,816 well we have in this file again, the following lines of code, 613 00:27:58,816 --> 00:28:01,516 so we've got one print app which is uninteresting now 614 00:28:01,516 --> 00:28:04,246 because it just says give me an integer and then I have a space, 615 00:28:04,246 --> 00:28:06,206 a closed quote, I don't have a back slash end 616 00:28:06,206 --> 00:28:07,826 and that's how I actually implemented all 617 00:28:07,826 --> 00:28:08,806 of that on one line. 618 00:28:09,116 --> 00:28:11,266 Well then I have this integer called X, 619 00:28:11,706 --> 00:28:14,606 I assigned it the return value of get ints 620 00:28:14,806 --> 00:28:17,416 so that's pretty basic right now and then I do the same thing 621 00:28:17,416 --> 00:28:20,096 for Y and then notice you can start embedding, 622 00:28:20,096 --> 00:28:23,286 even arithmetic expressions inside of parenthesis 623 00:28:23,536 --> 00:28:26,706 as arguments so to speak, as inputs to other functions, 624 00:28:26,986 --> 00:28:29,176 the sum of percent D, place holder, 625 00:28:29,326 --> 00:28:32,036 and percent D is percent D !N 626 00:28:32,036 --> 00:28:35,926 and I just have the comma enumerated list 627 00:28:36,306 --> 00:28:37,606 of the tree variables I want. 628 00:28:37,636 --> 00:28:43,606 All right, so what can we do with this here? 629 00:28:43,916 --> 00:28:47,286 So this is sort of the canonical stupid program one does 630 00:28:47,286 --> 00:28:49,826 in typical computer science class because it's very basic 631 00:28:50,126 --> 00:28:52,336 but none the less allows you to get started. 632 00:28:52,406 --> 00:28:53,486 So here is a blank. 633 00:28:53,486 --> 00:28:58,206 All right, think to yourself, little at home in seat exercise 634 00:28:58,206 --> 00:29:02,646 for say, 30 seconds and no more, what is the one or two 635 00:29:02,646 --> 00:29:06,126 or three lines of coke you might need to write to implement this? 636 00:29:06,536 --> 00:29:08,656 In other words fill in, if you could on a piece 637 00:29:08,656 --> 00:29:12,196 of paper there how you would implement this program 638 00:29:12,196 --> 00:29:13,926 and I'll seat it by saying, you know what? 639 00:29:14,006 --> 00:29:19,186 Go ahead and use get float to get the input. 640 00:29:24,126 --> 00:29:26,636 And for those who like to use this as an opportunity just 641 00:29:26,636 --> 00:29:29,296 to sit there and zone out you will be challenged to look 642 00:29:29,296 --> 00:29:32,116 at the persons code next to you and point out whether 643 00:29:32,116 --> 00:29:33,866 or not it is right or is wrong. 644 00:29:49,826 --> 00:29:52,186 This always seems to help. 645 00:29:53,516 --> 00:30:22,746 [ Music from Jeopardy ] 646 00:30:23,246 --> 00:30:34,306 let's give you one more loop of this. 647 00:30:35,516 --> 00:30:55,906 [ Music from Jeopardy ] 648 00:30:56,406 --> 00:30:58,586 All right, so go ahead if you would and raise your hand 649 00:30:58,586 --> 00:31:01,306 if the person next to you wrote nothing down. 650 00:31:02,676 --> 00:31:06,176 [Laughter] Okay, that's all right, it's to be expected, 651 00:31:06,396 --> 00:31:08,596 but here's a possible approach and let's just see. 652 00:31:08,596 --> 00:31:10,606 Think to yourself or pat yourself on the back if you 653 00:31:10,606 --> 00:31:12,646 at least have a couple of these pieces right. 654 00:31:12,646 --> 00:31:15,376 all right, so this is your first C program, simple though it is, 655 00:31:15,486 --> 00:31:17,976 all of this stuff up here is pretty much cookie cutter 656 00:31:17,976 --> 00:31:20,446 and the comments certainly don't fundamentally matter but are 657 00:31:20,446 --> 00:31:22,006 in fact a matter of good style 658 00:31:22,316 --> 00:31:24,086 so let's see what I've done first. 659 00:31:24,086 --> 00:31:26,126 So print F temperature in Fahrenheit:. 660 00:31:26,126 --> 00:31:28,866 So I didn't specify all this kind of detail 661 00:31:28,866 --> 00:31:30,846 so maybe you didn't even have this and that's fine 662 00:31:31,116 --> 00:31:32,976 but I printed out some prompts. 663 00:31:33,106 --> 00:31:35,676 Temperature in F: and then get some input, 664 00:31:35,826 --> 00:31:39,056 this part hopefully you had at least mentally going on. 665 00:31:39,056 --> 00:31:42,956 So float F gets get float, and that's it ;. All right, 666 00:31:42,956 --> 00:31:44,116 well how do you do the conversion? 667 00:31:44,296 --> 00:31:46,256 Well this part's relatively easy, 668 00:31:46,256 --> 00:31:47,566 even if you didn't remember the formula, 669 00:31:47,806 --> 00:31:49,726 frankly that's the one point of this little bit there, 670 00:31:50,036 --> 00:31:53,216 so we just need to convert that expression from the slide 671 00:31:53,216 --> 00:31:55,136 into mathematics and notice there's a couple 672 00:31:55,136 --> 00:31:56,656 of interesting details here. 673 00:31:56,916 --> 00:31:58,036 So one, you notice 674 00:31:58,086 --> 00:32:01,756 that I intentionally use parenthesis here so that just 675 00:32:01,756 --> 00:32:03,766 like the arithmetic expression on the board it is 676 00:32:03,766 --> 00:32:06,956 in fact doing the subtraction before the multiplication 677 00:32:07,246 --> 00:32:09,876 and then there's one other detail that's sort 678 00:32:09,876 --> 00:32:13,436 of technically interesting and what's that here? 679 00:32:13,796 --> 00:32:17,816 Yeah, so I have the .0 so the denominator there is not 9, 680 00:32:17,916 --> 00:32:20,916 5 divided by 9 because if I had just 5 divided 681 00:32:20,916 --> 00:32:24,746 by 9 what would the value of C always end up being? 682 00:32:24,856 --> 00:32:29,246 If it in fact always be 0 right? 683 00:32:29,246 --> 00:32:31,916 because if you have 2 integers, 5 divided by 9 684 00:32:32,176 --> 00:32:37,046 so that's .something, .4, but then if its an ints well, 685 00:32:37,046 --> 00:32:39,066 you can't fit the decimal point in an int 686 00:32:39,066 --> 00:32:42,896 so you throw away the .4 so then you're left with just the 0 687 00:32:43,176 --> 00:32:45,896 so that means 5 divided by 9 is integers, 688 00:32:45,896 --> 00:32:49,166 in fact always gives you 0 which means multiply 0 689 00:32:49,166 --> 00:32:52,136 by From minus 32, it doesn't matter, its always, in fact, 690 00:32:52,296 --> 00:32:55,146 going to be 0 so we fix that by specifying 691 00:32:55,146 --> 00:32:56,996 that we have a decimal point here, we can fix it 692 00:32:56,996 --> 00:32:58,196 with casting, although 693 00:32:58,196 --> 00:32:59,936 that would look un necessarily complicated, 694 00:33:00,126 --> 00:33:02,496 but then the final answer just involves these basic building 695 00:33:02,496 --> 00:33:06,596 blocks so the .1 percent .1F means what in layman's terms? 696 00:33:08,216 --> 00:33:09,716 Display on decimal point, that's it. 697 00:33:09,716 --> 00:33:11,336 and clearly there's a limit there somewhere 698 00:33:11,336 --> 00:33:14,056 but you're usually safe with just one or two or a few points 699 00:33:14,056 --> 00:33:16,406 of precision, then I say, capital F just 700 00:33:16,406 --> 00:33:20,126 to show units equals and then I have another percent point 1F 701 00:33:20,386 --> 00:33:23,026 Capital C for Celsius and then I plug in the values. 702 00:33:23,346 --> 00:33:26,256 So simple but again, look to this especially as you dive 703 00:33:26,256 --> 00:33:28,306 into problems that won, if you're kind of thinking, wow, 704 00:33:28,306 --> 00:33:30,896 it looks relatively easy and trivial 705 00:33:30,896 --> 00:33:32,196 in class, where do I begin? 706 00:33:32,446 --> 00:33:34,896 Well the basic questions you must ask are, well one, 707 00:33:34,986 --> 00:33:37,226 what framework do you need in place to write a program 708 00:33:37,486 --> 00:33:40,456 and you need, we've seen stuff like this here 709 00:33:40,596 --> 00:33:42,826 and even this is only necessary, 710 00:33:42,866 --> 00:33:46,786 the CS50.H if you're actually using bool or string or one 711 00:33:46,786 --> 00:33:48,436 of our capitalized function names. 712 00:33:48,536 --> 00:33:50,526 We've certainly had examples where we don't even need 713 00:33:50,526 --> 00:33:52,566 that yet, then you have your main routine, 714 00:33:52,816 --> 00:33:54,706 then you have some basic lines of code, 715 00:33:54,996 --> 00:33:58,686 so let's now introduce some tricks we already had last week, 716 00:33:58,806 --> 00:33:59,416 conditions. 717 00:33:59,686 --> 00:34:02,046 So how do you write a condition in C? 718 00:34:02,306 --> 00:34:04,506 You simply say if, and then I parenthesis, 719 00:34:04,566 --> 00:34:05,766 a Boolean expression. 720 00:34:06,096 --> 00:34:09,896 A Boolean expression might be X less than Y, Y greater than X, 721 00:34:09,896 --> 00:34:12,006 very similar in spirit to what you saw in scratch, 722 00:34:12,336 --> 00:34:14,926 you usually need the curly braces just 723 00:34:14,926 --> 00:34:18,226 like scratch had sort of shapes that said inside 724 00:34:18,226 --> 00:34:20,716 of this construct is the following code that you need 725 00:34:20,716 --> 00:34:23,206 to execute, the curly braces achieve that same effect 726 00:34:23,376 --> 00:34:25,196 and now I've done this a few times now, 727 00:34:25,196 --> 00:34:27,026 this slash, slash notation. 728 00:34:27,406 --> 00:34:28,806 What is that probably? 729 00:34:30,026 --> 00:34:33,166 So its another type of comment so whereas at the top 730 00:34:33,166 --> 00:34:36,846 of this file and most we've seen thus far, I have slash, star, 731 00:34:37,126 --> 00:34:38,826 and then whatever I want then, 732 00:34:38,826 --> 00:34:42,716 star slash so that's a multi-line comment in C 733 00:34:42,716 --> 00:34:45,356 and the world again has just adopted some aesthetically 734 00:34:45,356 --> 00:34:47,586 interesting or very anal tendencies 735 00:34:47,806 --> 00:34:51,036 like creating these long lines of stars or boxes like this, 736 00:34:51,226 --> 00:34:53,096 its completely meaningless to the computer, 737 00:34:53,096 --> 00:34:55,326 its just really a matter of style or convention 738 00:34:55,586 --> 00:34:57,316 but it does mean that that comment can go 739 00:34:57,316 --> 00:34:58,806 as many lines as you want. 740 00:34:59,036 --> 00:35:02,936 By contrast inside of my code its very good practice 741 00:35:02,936 --> 00:35:04,486 and in problem sets it will be expected 742 00:35:04,486 --> 00:35:08,256 that you comment your code and by this we mean every few lines 743 00:35:08,256 --> 00:35:11,766 of code, you should have a generally a single line comment 744 00:35:11,996 --> 00:35:14,486 unless you really need to elaborate in more words 745 00:35:14,486 --> 00:35:15,776 than a single line allows. 746 00:35:16,006 --> 00:35:18,736 You would have and then a little comment that's useful 747 00:35:18,736 --> 00:35:21,666 for one you, the reader, so that when you go back and look 748 00:35:21,666 --> 00:35:23,416 at this code again you don't have to sift 749 00:35:23,416 --> 00:35:26,576 through all the minutia of C you can just look at your own words 750 00:35:26,576 --> 00:35:28,666 as to what this chunk of code did and certainly 751 00:35:28,666 --> 00:35:31,166 from the courses perspective it allows the teaching fellows then 752 00:35:31,166 --> 00:35:33,986 to understand, maybe not what your code does, 753 00:35:33,986 --> 00:35:36,676 but what you intended it to do and so realize there's 754 00:35:36,676 --> 00:35:37,786 that motivation there. 755 00:35:37,846 --> 00:35:40,556 But this matter of style, choosing variables 756 00:35:40,556 --> 00:35:43,196 that are intelligently named and describe what they do, 757 00:35:43,446 --> 00:35:46,076 commenting your code every several lines 758 00:35:46,076 --> 00:35:49,166 or actually explaining particularly complicated blocks 759 00:35:49,166 --> 00:35:52,966 of code that you yourself might forget how they actually work 760 00:35:52,966 --> 00:35:54,456 because you were just so darn clever, 761 00:35:54,686 --> 00:35:56,516 it's again a matter of good style. 762 00:35:56,716 --> 00:35:58,706 But we can stack these things so if we have 763 00:35:58,706 --> 00:36:00,026 if else conditions as well. 764 00:36:00,076 --> 00:36:03,576 If, parenthetical then you do some stuff inside curly braces 765 00:36:03,676 --> 00:36:06,636 else you can then go and do something else altogether. 766 00:36:06,876 --> 00:36:09,016 So let's actually see this here in code. 767 00:36:09,016 --> 00:36:14,306 If I go ahead and open conditions 1.C we have this 768 00:36:14,336 --> 00:36:14,976 code here. 769 00:36:15,596 --> 00:36:19,406 So the top is again cookie cutter, this program does what? 770 00:36:19,626 --> 00:36:20,436 So let's see. 771 00:36:20,436 --> 00:36:24,046 This is again, conditions1.C ask user for an integer. 772 00:36:24,046 --> 00:36:26,246 All right, so I've got some print F there that does 773 00:36:26,296 --> 00:36:29,556 that then I'm doing some basic CS50 library stuff, get int, 774 00:36:29,666 --> 00:36:32,706 now I've got an integer and now, okay, I'm using this condition. 775 00:36:33,366 --> 00:36:35,866 But I told a little bit of a lie a moment ago. 776 00:36:35,866 --> 00:36:37,846 What's missing from my own code here? 777 00:36:39,006 --> 00:36:40,186 So those curly braces. 778 00:36:40,186 --> 00:36:42,216 So again silly little syntactical detail 779 00:36:42,216 --> 00:36:45,796 but a tendency people have is if you only have one line 780 00:36:45,796 --> 00:36:48,856 of code related to a branch for the if 781 00:36:49,046 --> 00:36:52,756 or for the outs you actually don't need the curly braces 782 00:36:52,756 --> 00:36:53,076 at all. 783 00:36:53,076 --> 00:36:54,576 So if you have one statement 784 00:36:54,796 --> 00:36:56,796 that you want executed beneath the if 785 00:36:56,796 --> 00:36:59,746 or beneath the let you can simply do it very correctly 786 00:36:59,746 --> 00:37:02,776 as I've done here and therefore not clutter your code 787 00:37:02,996 --> 00:37:04,486 with what ultimately boils 788 00:37:04,486 --> 00:37:06,886 down to just unnecessary characters like, 789 00:37:06,886 --> 00:37:09,826 okay now I can put this in here, 790 00:37:09,826 --> 00:37:12,256 I mean this really hasn't added anything an 791 00:37:12,256 --> 00:37:16,206 in fact its made my page wider, its sort of not quite as fast 792 00:37:16,206 --> 00:37:17,746 to read, so again that's why. 793 00:37:17,746 --> 00:37:19,446 People just sometimes omit those things. 794 00:37:19,726 --> 00:37:21,586 if I had done something like this though, 795 00:37:21,586 --> 00:37:23,726 I'll get into trouble, so if I did this, 796 00:37:23,726 --> 00:37:27,246 you picked a negative number and say something like, 797 00:37:27,246 --> 00:37:31,386 that was stupid!N; so you can indent all you want, 798 00:37:31,846 --> 00:37:37,406 the compiler is not going to realize this. 799 00:37:37,496 --> 00:37:39,406 So if I go ahead and run this program now, 800 00:37:39,406 --> 00:37:42,486 I'm going to go ahead and compile conditions1 I'm going 801 00:37:42,486 --> 00:37:50,986 to save it as -0conditions1 this is conditions1.C enter, 802 00:37:51,356 --> 00:37:54,596 I screwed up again, -LCS50, fixes that mistake, enter. 803 00:37:55,026 --> 00:37:58,926 So now I run conditions1, I'd like and integer please. 804 00:37:59,196 --> 00:38:03,476 44, enter, hm, feels like a bug right? 805 00:38:03,476 --> 00:38:04,746 I thought its only supposed to do 806 00:38:04,746 --> 00:38:08,036 that when I pick a negative number like negative 123, 807 00:38:08,036 --> 00:38:10,546 but again, the reason is that it doesn't matter 808 00:38:10,546 --> 00:38:12,636 if you've indented it you need the curly brace. 809 00:38:12,776 --> 00:38:16,436 So I can actually fix this now, a little curly brace there, 810 00:38:16,756 --> 00:38:20,866 close curly brace there, save it, recompile, 811 00:38:21,106 --> 00:38:24,646 bang G again executes the most recent command, that started 812 00:38:24,646 --> 00:38:26,956 with G and now I'm going to rerun my program 813 00:38:26,956 --> 00:38:28,746 with say, a positive number. 814 00:38:28,746 --> 00:38:30,746 123, well that's good. 815 00:38:30,746 --> 00:38:32,576 Well 456, still good. 816 00:38:32,576 --> 00:38:34,156 How about negative 123 817 00:38:34,406 --> 00:38:36,466 and in fact we fixed that mistake there. 818 00:38:37,046 --> 00:38:38,816 Any questions thus far? 819 00:38:39,406 --> 00:38:41,626 Since I know this has been a lot of little things quickly. 820 00:38:42,306 --> 00:38:45,546 All right, so let's take it up a notch 821 00:38:45,546 --> 00:38:47,746 so that we can actually do more interesting things still 822 00:38:47,996 --> 00:38:51,316 so if you want to stack these things together if else if, 823 00:38:51,316 --> 00:38:56,596 else if, else but again just as some of you might be finding 824 00:38:56,596 --> 00:38:58,296 or once we provide you with some feedback 825 00:38:58,296 --> 00:39:01,636 on problem set 0 we'll find that even scratch programs that are 826 00:39:01,636 --> 00:39:04,466 like yay wide if you're doing a lot of left right scrolling 827 00:39:04,466 --> 00:39:07,656 or a lot of top down scrolling odd are you can implement it a 828 00:39:07,656 --> 00:39:10,566 little more cleanly so you know, its no accident 829 00:39:10,566 --> 00:39:14,326 that I only depict an if else if else here, if you've got 20 830 00:39:14,326 --> 00:39:16,616 of these things, I mean you're pushing the limits 831 00:39:16,616 --> 00:39:19,776 of what constitutes good design and again, these are just rules 832 00:39:19,776 --> 00:39:21,266 of thumb I'm offering right now 833 00:39:21,526 --> 00:39:23,976 and they're fairly vague I realize but we'll see by way 834 00:39:23,976 --> 00:39:28,136 of examples over time why code that starts to get this and this 835 00:39:28,136 --> 00:39:31,206 and this long, can probably be done more intelligently 836 00:39:31,206 --> 00:39:33,376 so in this case, here's the second version of this code. 837 00:39:33,816 --> 00:39:37,416 So here I've actually fixed what was apparently a bug, 838 00:39:37,416 --> 00:39:40,826 you know what, let me go back before we actually spoil that 839 00:39:40,826 --> 00:39:43,156 and maybe you've already realized it, 123 worked, 840 00:39:43,156 --> 00:39:49,826 negative 123 worked, but hm the anal mathematicians 841 00:39:49,826 --> 00:39:52,026 in the room should know that -- 842 00:39:52,956 --> 00:39:55,936 so 0 is neither positive nor negative 843 00:39:55,936 --> 00:39:57,146 so I'm actually returning, 844 00:39:57,146 --> 00:40:00,816 certainly though the bug is a mistake, but notice it derives 845 00:40:00,816 --> 00:40:03,316 from the simple fact that I have, let me go back 846 00:40:03,316 --> 00:40:06,146 to the program, and let me roll back to the very original, 847 00:40:06,336 --> 00:40:07,526 I only have two cases. 848 00:40:07,666 --> 00:40:09,236 Either a number is positive or negative 849 00:40:09,296 --> 00:40:11,266 but there's a corner case there and so 850 00:40:11,266 --> 00:40:14,116 when we say you should be testing your own programs before 851 00:40:14,116 --> 00:40:15,276 you submit, just like you're 852 00:40:15,276 --> 00:40:17,286 on scratch programs should you be trying to figure 853 00:40:17,286 --> 00:40:18,836 out if you can actually break them 854 00:40:19,056 --> 00:40:20,886 by doing something the program doesn't intend, 855 00:40:21,156 --> 00:40:24,566 this is in fact the bug but very easily fixed because I can say 856 00:40:24,566 --> 00:40:29,096 if else if N equals equals 0 then I can say something 857 00:40:29,096 --> 00:40:34,506 like printF you picked 0!new line else. 858 00:40:34,666 --> 00:40:39,596 I could do this, else if N is greater than 0 or rather, oops, 859 00:40:39,686 --> 00:40:43,546 if N is less than 0 so do I need to do this? 860 00:40:44,986 --> 00:40:49,346 It will work but in this case you're really just wasting 861 00:40:49,346 --> 00:40:50,846 characters because it's not necessary. 862 00:40:50,846 --> 00:40:53,426 Logically there's only three possible conditions there. 863 00:40:53,806 --> 00:40:56,186 All right, so now let's look 864 00:40:56,186 --> 00:40:57,976 at what you can do inside those parents. 865 00:40:58,096 --> 00:41:02,046 So if condition or condition, so we have this ability not only 866 00:41:02,136 --> 00:41:06,576 to say is this and this true, but we can also ask is this 867 00:41:06,576 --> 00:41:09,556 or this true and you can also string these things together 868 00:41:09,826 --> 00:41:12,376 so this too is useful if you want to check multiple things 869 00:41:12,376 --> 00:41:14,316 at a time, vertical bar, 870 00:41:14,316 --> 00:41:16,176 vertical bar is the ore operator, 871 00:41:16,176 --> 00:41:19,696 ampersand ampersand is the and operator and here's one approach 872 00:41:19,696 --> 00:41:21,476 where we might use exactly this code. 873 00:41:21,806 --> 00:41:25,336 So this is called non-switch's to be distinguished in a moment 874 00:41:25,336 --> 00:41:28,436 from something called a switch and notice what I've done here, 875 00:41:28,806 --> 00:41:32,366 if I actually want to have some kind of boundaries, lower bound 876 00:41:32,366 --> 00:41:33,986 and upper bound here, you know, 877 00:41:33,986 --> 00:41:36,936 I can arbitrarily write this program so if I type 878 00:41:36,936 --> 00:41:39,826 in the number 5, what does this program spit out at me. 879 00:41:40,076 --> 00:41:42,896 I picked a medium number, right? 880 00:41:42,896 --> 00:41:44,566 Its fairly straight forward to read, 881 00:41:44,566 --> 00:41:48,456 that okay so if N is 5 well that's greater than 1, yes, 882 00:41:48,636 --> 00:41:51,636 but its not less than 3 which means this first block 883 00:41:51,636 --> 00:41:54,516 of code doesn't actually execute so N is greater than 4 884 00:41:54,516 --> 00:41:56,186 and N is less than or equal to 6. 885 00:41:56,186 --> 00:41:59,286 That works which means this condition gets executed 886 00:41:59,496 --> 00:42:02,746 but these branches, these ifs and else ifs just 887 00:42:02,746 --> 00:42:05,646 as the English implies are mutually exclusive. 888 00:42:06,106 --> 00:42:09,906 So in fact this is only going to execute one of these lines 889 00:42:09,906 --> 00:42:12,156 of code and the moment this checked off line 890 00:42:12,156 --> 00:42:14,376 of code executes, the compiler, or rather, 891 00:42:14,566 --> 00:42:17,766 the computer does not even look at those remaining code. 892 00:42:19,416 --> 00:42:21,756 So it turns out there's a different approach to this 893 00:42:21,966 --> 00:42:24,356 and again, just as last week on Friday was about kind 894 00:42:24,356 --> 00:42:26,766 of seeding you with some basic building blocks with scratch, 895 00:42:26,946 --> 00:42:29,296 the same deal here before we take things up a level and try 896 00:42:29,296 --> 00:42:30,556 to do something more interesting, 897 00:42:30,756 --> 00:42:34,196 there's also this construct known as a switch in C and also 898 00:42:34,196 --> 00:42:36,826 in other languages where if you find that you're kind 899 00:42:36,826 --> 00:42:39,166 of enumerating a lot of cases again 900 00:42:39,166 --> 00:42:44,006 and again its simply a matter of style or readability sometimes 901 00:42:44,256 --> 00:42:47,306 to use this thing instead so this is called a switch, 902 00:42:47,566 --> 00:42:49,326 you put the word switch and then 903 00:42:49,326 --> 00:42:52,956 in parenthesis a Boolean expression or rather, I'm sorry, 904 00:42:53,486 --> 00:42:56,616 you put in the switch you put say a variable. 905 00:42:57,026 --> 00:43:01,036 So whether its X or Y or Z you put in a place holder there 906 00:43:01,036 --> 00:43:04,866 and then what each of the cases so thereafter, is they check, 907 00:43:05,356 --> 00:43:07,696 does that case match the expression? 908 00:43:07,696 --> 00:43:09,846 Does this case match the exception and if 909 00:43:09,846 --> 00:43:13,406 so do this otherwise do that otherwise if none 910 00:43:13,406 --> 00:43:15,666 of the cases match, do this other thing. 911 00:43:15,946 --> 00:43:20,046 So this construct here is essentially equivalent to saying 912 00:43:20,046 --> 00:43:23,656 if expression equals equals I else 913 00:43:23,766 --> 00:43:26,416 if expression equals equals J fill 914 00:43:26,416 --> 00:43:29,076 in the blank for the last, else. 915 00:43:29,076 --> 00:43:31,146 All right so there is no actual check 916 00:43:31,176 --> 00:43:33,706 because that's what we mean by default so if I pull 917 00:43:33,706 --> 00:43:37,736 up this version here, this is switch 1.C let's take a look 918 00:43:37,736 --> 00:43:39,956 at how I can implement precisely that same idea. 919 00:43:40,226 --> 00:43:41,426 Now the code is longer 920 00:43:41,656 --> 00:43:44,306 so arguably I've not really done anything better here, 921 00:43:44,306 --> 00:43:46,536 this is there saying not an improvement because even I had 922 00:43:46,536 --> 00:43:49,646 to scroll but it's slightly more readable. 923 00:43:49,926 --> 00:43:52,686 Even you at first glance with that last program might have had 924 00:43:52,686 --> 00:43:55,496 to think, okay less or greater than one, less than 3, 925 00:43:55,496 --> 00:43:58,616 greater than 4 and all that, this is pretty darn clear 926 00:43:58,746 --> 00:44:02,336 as to what case is induced what behavior so here too, 927 00:44:02,336 --> 00:44:04,916 its sort of a matter of style, matter of judgment, 928 00:44:04,916 --> 00:44:05,906 is this more readable? 929 00:44:06,196 --> 00:44:08,476 Frankly I think it's probably a coin toss between the two. 930 00:44:08,536 --> 00:44:09,846 Its longer, its clearer 931 00:44:10,126 --> 00:44:12,236 but technically you'll even see things like this, 932 00:44:12,776 --> 00:44:14,816 so technically those could be on one line 933 00:44:14,816 --> 00:44:15,926 so if its simply the length 934 00:44:15,926 --> 00:44:17,746 of the code we're not thrilled with, you know, 935 00:44:17,746 --> 00:44:21,036 that's still pretty reasonable too and I point these kinds 936 00:44:21,036 --> 00:44:23,806 of things out tokay because you'll see sort of discrepancies 937 00:44:23,806 --> 00:44:25,696 in the course from things I say 938 00:44:25,746 --> 00:44:27,146 versus things your teaching fellows said, 939 00:44:27,146 --> 00:44:29,796 versus things the c a's in the lab say 940 00:44:29,796 --> 00:44:31,906 as to how you should write your code and that's 941 00:44:31,936 --> 00:44:34,236 because this is again just a matter of style. 942 00:44:34,236 --> 00:44:36,486 How one writes English kind of varies from person 943 00:44:36,486 --> 00:44:38,346 to person, same deal with code. 944 00:44:38,456 --> 00:44:39,606 What we'll do ultimately 945 00:44:39,606 --> 00:44:42,496 on the courses web site is post a style guide of sorts, 946 00:44:42,496 --> 00:44:43,656 sort of rules of thumb 947 00:44:43,966 --> 00:44:46,756 or different approaches you can take to actually writing code 948 00:44:46,756 --> 00:44:50,386 but for the most parts is sort of an element of flair. 949 00:44:50,386 --> 00:44:52,916 You yourselves will ultimately add to your code. 950 00:44:53,096 --> 00:44:55,296 Whoops, third times a charm. 951 00:44:55,596 --> 00:44:56,196 So what about this? 952 00:44:56,986 --> 00:44:57,936 There's a difference here. 953 00:44:58,076 --> 00:45:00,416 this program is not numeric so it turns 954 00:45:00,416 --> 00:45:02,856 out you can use characters as those expressions 955 00:45:02,856 --> 00:45:07,596 so this program simply says you got an excellent grade, 956 00:45:07,596 --> 00:45:10,456 you picked a fair grade you picked a poor grade and so forth 957 00:45:10,816 --> 00:45:12,966 and it does that by way of enumerating these cases 958 00:45:12,966 --> 00:45:17,286 and it handles the case sensitivity of the user's input 959 00:45:17,496 --> 00:45:19,416 by just saying, you know what, this case applies 960 00:45:19,416 --> 00:45:22,936 if its capital A or lower case a and if you now think 961 00:45:22,936 --> 00:45:25,516 about how we would implement that in code using ifs 962 00:45:25,516 --> 00:45:27,626 and else ifs and else, there would again be a lot 963 00:45:27,626 --> 00:45:31,126 of those Boolean operators like ampersands ampersand just 964 00:45:31,126 --> 00:45:32,976 as in the case a moment ago. 965 00:45:32,976 --> 00:45:34,476 All right. 966 00:45:34,476 --> 00:45:38,246 So now let's do things that are not just syntactically different 967 00:45:38,466 --> 00:45:40,226 but at least visually more interesting. 968 00:45:40,226 --> 00:45:42,796 So we had loops introduced in scratch, 969 00:45:42,796 --> 00:45:44,526 we had loops even demoed on Wednesday, 970 00:45:44,786 --> 00:45:48,566 here is one of the most pop, uh, common approaches to a loop 971 00:45:48,566 --> 00:45:53,026 in C. you've got three things in parenthesis, or initializations, 972 00:45:53,026 --> 00:45:57,166 a condition or conditions if you ampersand them together 973 00:45:57,166 --> 00:46:00,186 or ore them together and then you've got updates so to speak. 974 00:46:00,566 --> 00:46:01,476 So what does that mean? 975 00:46:01,476 --> 00:46:03,216 It means when you write a fore loop, 976 00:46:03,216 --> 00:46:06,186 if you want to do something multiple times you are going 977 00:46:06,186 --> 00:46:09,816 to initialize some value like a variable to 0 or 1, 978 00:46:10,106 --> 00:46:11,536 you are then going to check and make sure 979 00:46:11,536 --> 00:46:14,736 that variable is non greater than some threshold and then 980 00:46:14,736 --> 00:46:16,586 in every iteration of this loop you're going 981 00:46:16,586 --> 00:46:18,676 to do an incrimination or something like that. 982 00:46:19,126 --> 00:46:21,826 Well let's actually try something very simple. 983 00:46:21,826 --> 00:46:25,796 So Foo.C and you'll find that food, bar, bas and stupid words 984 00:46:25,796 --> 00:46:28,866 like these are just placeholders that computer scientists use 985 00:46:28,866 --> 00:46:30,556 when you need to whip out a variable. 986 00:46:30,916 --> 00:46:34,776 So char, star, arg v is the start of my program, okay? 987 00:46:36,186 --> 00:46:36,936 What's that? 988 00:46:36,936 --> 00:46:37,566 Okay, thank you. 989 00:46:38,126 --> 00:46:41,246 Actually it would be funnier if you just let me go 990 00:46:41,246 --> 00:46:42,436 for a while but thank you. 991 00:46:43,236 --> 00:46:47,906 so include standard io.h I'm not sure I need SC50's library just 992 00:46:47,906 --> 00:46:50,066 yet but here I have the basic set up for a program 993 00:46:50,336 --> 00:46:52,206 so let me practice what I'm preaching here, 994 00:46:52,206 --> 00:46:56,366 so 4 give me an integer I and I'll assign it 995 00:46:56,496 --> 00:47:03,166 to a default value of 0, let's do this so long as 200 times 996 00:47:03,376 --> 00:47:05,186 and then each adoration do plus plus. 997 00:47:05,186 --> 00:47:06,156 Well what do I want to do? 998 00:47:06,156 --> 00:47:08,596 Well why don't we just do something like print F 999 00:47:08,596 --> 00:47:10,696 and I know this is kind of simple 1000 00:47:10,696 --> 00:47:14,146 but I'm just going to print F this. 1001 00:47:14,146 --> 00:47:17,356 So GCC of foo.c enter, now I'm going to run a.out. 1002 00:47:18,286 --> 00:47:20,286 Okay, that's a pretty damn fast program. 1003 00:47:20,286 --> 00:47:23,156 Let's see if I can do something a little more interesting. 1004 00:47:23,156 --> 00:47:25,266 Let me go back to this, more interesting 1005 00:47:25,266 --> 00:47:27,786 than star might actually to print out the number 1006 00:47:27,786 --> 00:47:29,976 so I could do this and then print out I 1007 00:47:30,446 --> 00:47:35,906 so let me recompile that, rerun a. out okay not very readable, 1008 00:47:35,906 --> 00:47:40,206 give me a quick fix, all right, n still underwhelming 1009 00:47:40,206 --> 00:47:44,416 but let's do that and then a.out okay. 1010 00:47:44,416 --> 00:47:45,116 Interesting. 1011 00:47:45,556 --> 00:47:47,986 But I thought I said 200. 1012 00:47:48,146 --> 00:47:50,086 All right, it's less than. 1013 00:47:50,216 --> 00:47:52,676 So again computer scientists or programmers generally count 1014 00:47:52,766 --> 00:47:57,266 from 0 so if you're saying 0 I is 0 up to 100 1015 00:47:57,386 --> 00:48:00,676 up to 200 realize, yes you're printing 200 values 1016 00:48:00,676 --> 00:48:03,576 but the first is 0 thus the last is 199. 1017 00:48:03,776 --> 00:48:06,756 Well we can make this even more interesting by screwing up. 1018 00:48:06,756 --> 00:48:08,506 What if I do something like this? 1019 00:48:09,896 --> 00:48:13,506 So I minus minus is the opposite of I plus plus a.out 1020 00:48:13,776 --> 00:48:20,646 so what's going to happen, will this go forever? 1021 00:48:20,646 --> 00:48:27,926 Well actually it might go forever, the y. its not going 1022 00:48:27,926 --> 00:48:30,936 to go forever in the same direction right, because think 1023 00:48:30,936 --> 00:48:32,106 about what's happening, if you, 1024 00:48:32,106 --> 00:48:33,696 even though we really haven't talked in detail 1025 00:48:33,696 --> 00:48:36,076 about how you represent negative numbers underneath the hood 1026 00:48:36,076 --> 00:48:38,236 in this course, at some point you're going 1027 00:48:38,236 --> 00:48:39,706 to run out of bits, right? 1028 00:48:40,006 --> 00:48:47,666 so what's going to happen if we keep flipping bits from 0 to 1 0 1029 00:48:47,666 --> 00:48:49,896 to 1 what range are we going to end up in. 1030 00:48:50,306 --> 00:48:53,536 its probably at some point become positive 1031 00:48:53,536 --> 00:48:56,246 which is mathematically wrong but we're just starting 1032 00:48:56,246 --> 00:48:59,476 to confuse then with the representative of a positive 1033 00:48:59,476 --> 00:49:01,666 with a negative number in fact this will take a while 1034 00:49:01,666 --> 00:49:02,526 because we're only at what? 1035 00:49:02,526 --> 00:49:06,036 800,000 and we've got negative 2 billion to go 1036 00:49:06,366 --> 00:49:07,836 so let me go back into this. 1037 00:49:07,836 --> 00:49:10,856 How could I fix this and make it more dramatic perhaps? 1038 00:49:11,886 --> 00:49:13,986 Let me say so long as it's greater 1039 00:49:13,986 --> 00:49:15,346 than 0 and you know what? 1040 00:49:15,346 --> 00:49:19,926 I don't have to do just plus plus I could do I gets itself I 1041 00:49:20,536 --> 00:49:25,466 times 2 so this little expentiation a.out, 1042 00:49:26,446 --> 00:49:27,096 whoops what's wrong? 1043 00:49:27,096 --> 00:49:30,776 What did I do wrong? 1044 00:49:30,776 --> 00:49:36,236 So times 2, this part's right, I didn't hear it loudly enough 1045 00:49:36,656 --> 00:49:40,656 so its initialized to 0 which means here too the loop, 1046 00:49:40,656 --> 00:49:43,446 the condition, the syntax is perfectly correct 1047 00:49:43,446 --> 00:49:47,656 but I is initialized to 0 do the following while I is greater 1048 00:49:47,656 --> 00:49:51,896 than 0 that never happens and so this line here never gets seen 1049 00:49:51,896 --> 00:49:54,296 so a quick fix, and again, this is completely nonsensical 1050 00:49:54,296 --> 00:49:54,966 at this point, we're trying 1051 00:49:54,966 --> 00:49:58,656 to demonstrate a different point altogether, what happens here? 1052 00:49:59,296 --> 00:50:03,066 It stopped. 1053 00:50:04,336 --> 00:50:08,086 But what happened? 1054 00:50:09,536 --> 00:50:14,366 So we kept increasing the value of I so bigger and bigger 1055 00:50:14,366 --> 00:50:17,416 and bigger so think back to the light bulbs from last Wednesday, 1056 00:50:17,416 --> 00:50:20,866 to keep incrementing numbers in binary from 1 to 2 to 3 1057 00:50:20,866 --> 00:50:23,306 to 4 remember we just kept changing 0's to 1's 1058 00:50:23,556 --> 00:50:24,936 and then the 1's kept kind of bubbling 1059 00:50:24,936 --> 00:50:27,686 up until we had more 1's than we did 0's well 1060 00:50:27,686 --> 00:50:30,246 at some point you're going to run out of bits to flip 1061 00:50:30,306 --> 00:50:32,316 so to speak, and you're going to get all the way back 1062 00:50:32,316 --> 00:50:35,736 down to 32 0's, right? 1063 00:50:35,776 --> 00:50:37,986 The clock changes over again and so 1064 00:50:37,986 --> 00:50:41,206 in fact this condition eventually does stop us. 1065 00:50:41,486 --> 00:50:42,506 Well let's see, if we get rid 1066 00:50:42,506 --> 00:50:44,466 of this condition all together can we 1067 00:50:44,466 --> 00:50:46,506 at least see what's going on at a.out. 1068 00:50:47,756 --> 00:50:53,136 Hm. What's going on here? 1069 00:50:53,346 --> 00:50:55,106 Multiplying 0 by 2 right? 1070 00:50:55,106 --> 00:50:57,586 If eventually I becomes 0 if I keep trying 1071 00:50:57,586 --> 00:51:00,706 to double it I get stuck at 0 so in short this is bad things. 1072 00:51:00,706 --> 00:51:01,696 if your code is doing things 1073 00:51:01,696 --> 00:51:03,376 like this then there's a problem somewhere 1074 00:51:03,376 --> 00:51:06,326 and very clearly there are many different ways you can screw 1075 00:51:06,326 --> 00:51:09,146 up code like this but what interesting hopefully is why 1076 00:51:09,146 --> 00:51:10,276 this is actually happening 1077 00:51:10,516 --> 00:51:12,696 and for today's purposes realize we've hit a couple 1078 00:51:12,696 --> 00:51:15,666 of thresholds, one, how large the ints are 1079 00:51:15,666 --> 00:51:17,476 that we can represent, and how large the floats are 1080 00:51:17,476 --> 00:51:20,386 that we can represent but these are things that become important 1081 00:51:20,386 --> 00:51:23,496 when you try to actually now use them in context. 1082 00:51:23,496 --> 00:51:26,366 So let's now use this, not to just print out numbers like this 1083 00:51:26,516 --> 00:51:27,406 but to do something that's 1084 00:51:27,406 --> 00:51:30,286 at least more aesthetically in the progress bar. 1085 00:51:30,526 --> 00:51:33,646 So this is a file called progress1.c, you've got it 1086 00:51:33,646 --> 00:51:37,516 in your code there and it makes use of the same idea 1087 00:51:37,856 --> 00:51:38,736 but now I'm kind of trying 1088 00:51:38,736 --> 00:51:41,106 to simulate what's a very reasonable program. 1089 00:51:41,106 --> 00:51:42,676 It's not just printing some stupid numbers, 1090 00:51:42,676 --> 00:51:45,236 its actually indicating progress like most programs do. 1091 00:51:45,546 --> 00:51:47,576 So I've got an int that initializes to 0, 1092 00:51:47,816 --> 00:51:51,286 do the following while I is less than or equal to 100 percent 1093 00:51:51,286 --> 00:51:54,676 and then increment and now notice this, percent d 1094 00:51:54,676 --> 00:51:57,126 and then what's the percent percent mean? 1095 00:51:57,936 --> 00:52:00,486 Print a percent, you run into these problems 1096 00:52:00,486 --> 00:52:01,946 if percent has special meanings 1097 00:52:01,946 --> 00:52:04,136 so percent percent means give me a little percent 1098 00:52:04,136 --> 00:52:07,766 and this is a new feature, so sleep1 apparently is going 1099 00:52:07,766 --> 00:52:09,836 to make this thing pause for some amount of time, 1100 00:52:10,116 --> 00:52:14,096 GSS progress1, let me go ahead and run a.outenter, 1101 00:52:14,096 --> 00:52:19,506 all right its probably not worth spending 100 seconds seeing 1102 00:52:19,506 --> 00:52:21,046 if this thing is, in fact, correct, 1103 00:52:21,716 --> 00:52:25,116 but it will in fact ultimately bottom out at 100 percent. 1104 00:52:25,606 --> 00:52:27,696 That's not very interesting, I don't really want to see 1105 00:52:27,696 --> 00:52:29,876 where I've been again and again so let me go ahead 1106 00:52:29,876 --> 00:52:35,186 and open progress2.c so progress2.c operates as follows. 1107 00:52:35,946 --> 00:52:39,586 So that's a little more interesting, right? 1108 00:52:39,586 --> 00:52:41,236 It's not moving and for those of you 1109 00:52:41,236 --> 00:52:45,146 in the awkwardly close seats here, now you can see that, 1110 00:52:45,436 --> 00:52:46,406 so now it's counting up. 1111 00:52:46,676 --> 00:52:49,996 How did I do this? 1112 00:52:50,206 --> 00:52:52,136 Yeah, so remember this is again, one of these sort 1113 00:52:52,136 --> 00:52:54,686 of random things that at first glance doesn't seem all 1114 00:52:54,686 --> 00:52:56,576 that useful but if you don't want to advance 1115 00:52:56,616 --> 00:52:58,866 to the next line, we n but instead want 1116 00:52:58,866 --> 00:53:00,746 to move the typewriters head so to speak sort 1117 00:53:00,746 --> 00:53:01,806 of all the way back to the left 1118 00:53:01,806 --> 00:53:03,846 and then just reprint the line, reprint the line. 1119 00:53:04,056 --> 00:53:08,276 Essentially implementing the idea of animation, 1120 00:53:08,526 --> 00:53:11,096 this is in fact one way we can do this, so you know what, 1121 00:53:11,096 --> 00:53:12,496 I thought we'd do this. 1122 00:53:12,696 --> 00:53:16,736 Let me go ahead and type in endow.c, 1123 00:53:17,406 --> 00:53:22,716 I'm going to do intmain intargc charstar rgv [sounds like] I got 1124 00:53:22,716 --> 00:53:26,256 a little bit of that, I'm going to need my standard library 1125 00:53:26,386 --> 00:53:34,056 so include standard io.h I want to have a 4, oops, careful, 1126 00:53:34,056 --> 00:53:41,236 typo there, so 4 ints and endowment gets, all right, 1127 00:53:41,336 --> 00:53:42,656 I just so happened to bring this, 1128 00:53:42,956 --> 00:53:48,936 so last year our endowment was 36.9 billion so let's initialize 1129 00:53:48,936 --> 00:53:55,686 and endowment to 36.9 billion so that's a lot of 0's 1130 00:53:55,686 --> 00:53:58,886 and actually c will not like the comma, so let me remove those, 1131 00:53:59,136 --> 00:54:02,966 in fact I've already made a mistake which is what? 1132 00:54:02,966 --> 00:54:06,946 So while endowment is greater than or equal to, 1133 00:54:06,946 --> 00:54:09,586 well now our endowment is 26.0 billion 1134 00:54:09,866 --> 00:54:10,956 so let's make that the threshold. 1135 00:54:10,956 --> 00:54:17,356 26.0 billions but let me get rid of the things there 1136 00:54:17,356 --> 00:54:20,166 and then you know what, endowment minus minus 1137 00:54:21,226 --> 00:54:22,756 so now let me go and do this 1138 00:54:22,856 --> 00:54:27,776 so print endowment:$ this isn't funny to someone 1139 00:54:27,776 --> 00:54:34,376 out there I'm sure, so endowment percent d n well that's going 1140 00:54:34,376 --> 00:54:37,856 to look a little ugly, you know, let me whip out that r 1141 00:54:37,856 --> 00:54:40,976 and keep returning the reading head to the start 1142 00:54:40,976 --> 00:54:44,276 of the line percent d but there is a bug here, endowment, 1143 00:54:45,056 --> 00:54:46,966 has someone caught it yet? 1144 00:54:47,976 --> 00:54:49,956 Well you gotta say it a little louder and clearer. 1145 00:54:51,496 --> 00:54:54,166 So a double or really we have long longs 1146 00:54:54,166 --> 00:54:56,116 so Harvard's endowment curiously enough, 1147 00:54:56,376 --> 00:55:01,316 is neither now nor then small enough to fit in the size 1148 00:55:01,316 --> 00:55:03,956 of an integer, we actually have to whip out a long long 1149 00:55:03,956 --> 00:55:07,796 so we get 64 bits because what? 1150 00:55:07,796 --> 00:55:11,266 If we had an endowment of 36.9 billion, that's larger 1151 00:55:11,266 --> 00:55:14,836 than the 4 billion max even in an unsigned int so let me change 1152 00:55:14,836 --> 00:55:18,326 that to long long endowment endowment minus minus 1153 00:55:18,326 --> 00:55:20,886 and then I got to change this to a long lld 1154 00:55:20,886 --> 00:55:29,566 and now let me go ahead and run GCC on endow.c oops, not print, 1155 00:55:29,696 --> 00:55:32,546 there we go, little typo, let me recompile, 1156 00:55:32,966 --> 00:55:37,726 let me run a.out all right, let's take a 5 minute break. 1157 00:55:38,846 --> 00:55:49,826 [Laughter] all right, we are back but we still have plenty 1158 00:55:49,826 --> 00:55:52,366 of money, I went ahead and made the font size bigger, 1159 00:55:52,366 --> 00:55:54,836 this is actually those of you who have ever been to Manhattan 1160 00:55:54,836 --> 00:55:57,056 and Union Square might know the clock that kinds of ticks 1161 00:55:57,216 --> 00:55:59,116 down perpetually so that's essentially what we've 1162 00:55:59,116 --> 00:56:00,856 implemented and it really just boils 1163 00:56:00,856 --> 00:56:03,766 down to something simple like r. 1164 00:56:03,766 --> 00:56:05,796 I'll admit when I first kind of banged 1165 00:56:05,796 --> 00:56:08,056 out this example it's really kind of disturbing 1166 00:56:08,056 --> 00:56:10,426 to see what it really means to lose dollar 1167 00:56:10,426 --> 00:56:12,036 after dollar after dollar. 1168 00:56:12,036 --> 00:56:15,176 I mean, just in the time I just spent stuttering, 1169 00:56:15,176 --> 00:56:16,646 do you know how much stuff we could have bought? 1170 00:56:17,286 --> 00:56:21,666 So anyhow, so we'll leave that running, 1171 00:56:21,766 --> 00:56:25,976 probably for several days before we bottom out but let's go ahead 1172 00:56:25,976 --> 00:56:27,866 and just wrap up this look at conditions 1173 00:56:27,866 --> 00:56:29,806 and let's see once we've gotten some 1174 00:56:29,806 --> 00:56:32,076 of these basic building blocks out of the way what you can do 1175 00:56:32,076 --> 00:56:33,966 with these same fundamentals. 1176 00:56:33,966 --> 00:56:36,636 So the revelation in the progress example, 1177 00:56:36,636 --> 00:56:39,546 the progress bar example was quite simply this. 1178 00:56:39,546 --> 00:56:43,366 So I'm going to go back real quick to nice.fas I'm going 1179 00:56:43,366 --> 00:56:48,106 to log back in as myself here and go into my CS50 directory 1180 00:56:48,106 --> 00:56:52,456 and the code in question is this progress2 so the only change 1181 00:56:52,456 --> 00:56:56,086 that I made really, is this slash r but you'll notice 1182 00:56:56,086 --> 00:56:59,476 in your printout that what I didn't do and actually got away 1183 00:56:59,476 --> 00:57:01,716 with it, I actually have this line of code here, 1184 00:57:01,716 --> 00:57:04,386 f flush of standard out and I mention this now, 1185 00:57:04,386 --> 00:57:06,886 this is one of the things to tuck in the back of your head, 1186 00:57:06,976 --> 00:57:09,426 what computers often do in the interest 1187 00:57:09,426 --> 00:57:12,576 of efficiency is even though you might tell them to do something 1188 00:57:12,636 --> 00:57:15,326 like print this, they might say, hm when I get 1189 00:57:15,326 --> 00:57:18,606 around to it essentially so print F has this thing, 1190 00:57:18,606 --> 00:57:20,866 you know, a buffer, you're all familiar with buffers 1191 00:57:20,866 --> 00:57:23,156 from the idea of like internet videos, buffering and all 1192 00:57:23,156 --> 00:57:25,296 of this kind of building up enough bites before you 1193 00:57:25,296 --> 00:57:28,446 and then see the movie, well same deal here with print F even 1194 00:57:28,446 --> 00:57:31,326 so there's this thing known as standard out put 1195 00:57:31,476 --> 00:57:34,066 which is essentially your screen and it has a buffer 1196 00:57:34,066 --> 00:57:36,356 of some number of bites or kilobytes or whatever 1197 00:57:36,646 --> 00:57:39,206 that the computer then uses to fill up 1198 00:57:39,206 --> 00:57:42,346 and only once its sufficiently full does it display what's 1199 00:57:42,346 --> 00:57:45,236 on the screen, well in the case of the computer I ran this 1200 00:57:45,236 --> 00:57:47,196 on a moment ago, I changed the number 1201 00:57:47,196 --> 00:57:49,006 and the change got written out immediately 1202 00:57:49,216 --> 00:57:51,666 but this f flush here essentially says, 1203 00:57:51,836 --> 00:57:54,576 flush the file, because standard out, we'll see later 1204 00:57:54,576 --> 00:57:57,206 in the course is essentially a file, just means spit 1205 00:57:57,206 --> 00:58:00,206 out what I just told you to right now 1206 00:58:00,456 --> 00:58:01,736 and then sleep meanwhile, 1207 00:58:01,736 --> 00:58:04,026 apparently takes a single argument, a single in put, 1208 00:58:04,246 --> 00:58:05,956 in this case the number of seconds one 1209 00:58:06,136 --> 00:58:08,906 and the reason I was able to use this thing is 1210 00:58:08,906 --> 00:58:12,106 because I also included this header fire file here. 1211 00:58:12,436 --> 00:58:15,106 so in fact, you'll get used to this over time too, 1212 00:58:15,106 --> 00:58:17,716 there's a command in Linniks called man, for manual, 1213 00:58:17,986 --> 00:58:20,026 and you can type in something like sleep 1214 00:58:20,166 --> 00:58:22,336 and sometimes you need to specify the chapter 1215 00:58:22,336 --> 00:58:24,136 of the manual that you want by default 1216 00:58:24,136 --> 00:58:26,676 in this class you almost always use what's called chapter 3 1217 00:58:26,946 --> 00:58:29,536 or chapter 2, let me see if I guessed right, I did in fact, 1218 00:58:29,796 --> 00:58:32,236 so this is the linniks [sounds like] programmer's manual, 1219 00:58:32,516 --> 00:58:35,326 the function in question is sleep, its in chapter three 1220 00:58:35,326 --> 00:58:38,366 and this is again just kind of a very concise explanation 1221 00:58:38,556 --> 00:58:41,346 of what this function does and more importantly 1222 00:58:41,716 --> 00:58:43,876 with what library its included. 1223 00:58:43,876 --> 00:58:45,936 It tells you what header file you need 1224 00:58:46,196 --> 00:58:47,626 so as the course progresses 1225 00:58:47,626 --> 00:58:49,526 and as you start getting a little more daring 1226 00:58:49,526 --> 00:58:51,906 and finding new functions that we just haven't gotten to around 1227 00:58:51,906 --> 00:58:54,716 to mentioning in class, you'll find that just typing man 1228 00:58:54,716 --> 00:58:56,916 and then the name of the function with sometimes a 2 1229 00:58:56,916 --> 00:58:59,846 or a 3 before it, will tell you how to use that function 1230 00:58:59,846 --> 00:59:01,736 and its very similar in spirit to the stuff you'll see 1231 00:59:01,736 --> 00:59:04,606 on the courses web site resources page if you go 1232 00:59:04,606 --> 00:59:06,756 to that CPP reference.com. 1233 00:59:07,266 --> 00:59:10,316 So there's one other approach though, that we haven't 1234 00:59:10,316 --> 00:59:13,196 yet done that's already in your printouts and that's this. 1235 00:59:13,196 --> 00:59:15,066 So we said there's 4 loops but I also said 1236 00:59:15,066 --> 00:59:16,406 on Wednesday there's wild loops 1237 00:59:16,406 --> 00:59:18,636 and there's actually another thing called the do wild loop 1238 00:59:18,856 --> 00:59:21,626 and these two are just kinds of different approaches 1239 00:59:21,626 --> 00:59:24,676 to solving the same problems so in the previous version 1240 00:59:24,676 --> 00:59:25,736 of the progress bar 1241 00:59:25,736 --> 00:59:28,466 or the endowment program I just used a 4 loop 1242 00:59:28,466 --> 00:59:32,596 and frankly I probably go with a 4 loop way more often then I do 1243 00:59:32,596 --> 00:59:35,546 with a wild loop, just because, I feel more comfortable with it. 1244 00:59:35,856 --> 00:59:37,886 but you can do things differently, so if I know 1245 00:59:37,886 --> 00:59:39,146 in advance that I need a variable, 1246 00:59:39,146 --> 00:59:41,776 well like we saw Wednesday I can declare that variable 1247 00:59:41,776 --> 00:59:45,676 and give it a value but then I can say while that value is less 1248 00:59:45,736 --> 00:59:48,116 than or equal to 100 do the following, 1249 00:59:48,116 --> 00:59:50,816 so whereas the 4 loop has the 2 semicolons 1250 00:59:50,816 --> 00:59:54,036 and 3 things going on, a while loop is actually much simpler. 1251 00:59:54,036 --> 00:59:55,246 It just has a condition 1252 00:59:55,576 --> 00:59:58,696 which says do the following while this condition is true, 1253 00:59:58,966 --> 01:00:00,756 so the effect is ultimately the same. 1254 01:00:00,756 --> 01:00:03,236 print this stuff out, flush standard outputs, 1255 01:00:03,236 --> 01:00:05,586 sleep for a second, but I kind of have 1256 01:00:05,676 --> 01:00:08,496 to manually update my counter for me 1257 01:00:08,496 --> 01:00:10,846 because the while loop just does not give you that capability. 1258 01:00:11,126 --> 01:00:12,066 Which one is better? 1259 01:00:12,066 --> 01:00:15,556 Eh? In this case it feels to me sort of being a little anal 1260 01:00:15,556 --> 01:00:17,556 about this, that the 4 loops a little nicer 1261 01:00:17,556 --> 01:00:20,606 because everything's kind of self contained all in one block 1262 01:00:20,606 --> 01:00:21,986 of code and this is, again, 1263 01:00:21,986 --> 01:00:25,296 the kinds of instincts you begin to acquire over time. 1264 01:00:25,586 --> 01:00:28,436 this to me feels a little messy, I got this orphaned variable 1265 01:00:28,436 --> 01:00:30,556 at the top, I'm doing this I plus plus arbitrarily 1266 01:00:30,556 --> 01:00:32,946 at the bottom, its completely correct, its not wrong 1267 01:00:32,946 --> 01:00:36,446 and its hard to fault it but there's something about it to me 1268 01:00:36,446 --> 01:00:38,466 that kind of rubs me the wrong way and so I might go 1269 01:00:38,466 --> 01:00:39,636 with the 4 loop approach. 1270 01:00:39,636 --> 01:00:42,166 But, again, there's different approaches to different problems 1271 01:00:42,496 --> 01:00:43,926 but there's also this construct 1272 01:00:43,926 --> 01:00:46,696 which actually has a fundamentally different meaning 1273 01:00:46,886 --> 01:00:49,316 and that's the do while construct. 1274 01:00:49,546 --> 01:00:52,256 Do followed by a curly brace and some stuff 1275 01:00:52,256 --> 01:00:53,806 and then the wild loop is at the end 1276 01:00:53,806 --> 01:00:55,396 and that's the key difference here. 1277 01:00:55,396 --> 01:00:56,776 So the do while construct 1278 01:00:56,836 --> 01:01:00,006 as it implies aesthetically does something, 1279 01:01:00,486 --> 01:01:01,946 the stuff between the curly braces, 1280 01:01:02,096 --> 01:01:04,996 then it checks its condition and if the condition is still true, 1281 01:01:04,996 --> 01:01:06,696 it does that something again. 1282 01:01:07,126 --> 01:01:09,196 Rechecks, does it again, rechecks. 1283 01:01:09,406 --> 01:01:10,776 Whereas in the previous case 1284 01:01:10,776 --> 01:01:15,536 of the typical while loop its condition gets checked first, 1285 01:01:15,896 --> 01:01:17,516 then some stuff maybe happens. 1286 01:01:17,566 --> 01:01:19,466 The do while loop is fundamentally different 1287 01:01:19,606 --> 01:01:21,106 because it definitely does what's 1288 01:01:21,106 --> 01:01:22,926 in between the curly braces at least once. 1289 01:01:23,436 --> 01:01:24,986 Now why might this be useful? 1290 01:01:24,986 --> 01:01:26,836 Well, let's put it into context. 1291 01:01:26,946 --> 01:01:29,746 So here's an example called positive 1.c 1292 01:01:30,276 --> 01:01:33,556 so this example here does, oh, okay, 1293 01:01:33,866 --> 01:01:35,536 so this is actually representative 1294 01:01:35,536 --> 01:01:37,876 of the typical use, frankly, for do while loop. 1295 01:01:38,066 --> 01:01:40,766 Any time you want to prompt a user for input 1296 01:01:41,156 --> 01:01:42,326 but you don't really trust them, 1297 01:01:42,326 --> 01:01:43,746 you think there's a chance they're going to mess 1298 01:01:43,746 --> 01:01:46,526 around with you or screw up their input, you want them 1299 01:01:46,526 --> 01:01:48,976 to do something once and then you want to check, 1300 01:01:49,056 --> 01:01:50,546 did they do it right because if not, 1301 01:01:50,546 --> 01:01:52,476 you want them to do it again. 1302 01:01:52,826 --> 01:01:54,806 So you want them to try something then you want 1303 01:01:54,806 --> 01:01:56,726 to check a condition and then you want to do it again 1304 01:01:56,726 --> 01:01:58,786 if they screwed up and that's precisely what's going 1305 01:01:58,786 --> 01:02:00,386 on here, what do I want to do? 1306 01:02:00,636 --> 01:02:01,906 Well, I want to print out, 1307 01:02:01,906 --> 01:02:03,866 I demand that you give me a positive integer, 1308 01:02:04,186 --> 01:02:07,786 then I get the ints but notice the order of operations here. 1309 01:02:07,856 --> 01:02:10,686 code is generally executed top to bottom left to right, 1310 01:02:10,886 --> 01:02:13,316 which means those 2 lines of code have already executed 1311 01:02:13,316 --> 01:02:14,906 which means now, stored in n, 1312 01:02:15,126 --> 01:02:18,806 is whatever the user typed while is now the condition 1313 01:02:18,806 --> 01:02:21,826 so while n is less than 1 what do I do? 1314 01:02:22,246 --> 01:02:26,256 Well, if the user just did something but n is less than 1, 1315 01:02:26,256 --> 01:02:27,696 that means they did the wrong thing 1316 01:02:27,696 --> 01:02:30,806 which means I should go do it again and if they still screw 1317 01:02:30,806 --> 01:02:33,416 around and give me negative 3, let's do it again. 1318 01:02:33,416 --> 01:02:34,526 Negative 4, do it again. 1319 01:02:34,606 --> 01:02:38,816 Positive 6, okay, this condition is now false and so it breaks 1320 01:02:38,816 --> 01:02:40,136 out of this whole construct. 1321 01:02:40,136 --> 01:02:44,076 So now if I go ahead and compile, positive 1.c, oh, 1322 01:02:44,376 --> 01:02:47,716 same problem again, I need to link in CS50's library, 1323 01:02:47,836 --> 01:02:50,296 lcs50 but again, recall the trick from Wednesday, 1324 01:02:50,296 --> 01:02:53,596 make positive 1 because of the way we've figured the system 1325 01:02:53,596 --> 01:02:55,756 should automate that for us, even though its going 1326 01:02:55,756 --> 01:02:57,386 to dump some more detail on the screen, 1327 01:02:57,656 --> 01:03:00,686 I can now run positive1 enter, 1328 01:03:01,376 --> 01:03:04,476 negative 4 negative 3, negative 2. 1329 01:03:04,476 --> 01:03:06,316 that's because of the do while loop keeping in. 1330 01:03:06,536 --> 01:03:08,406 if I say something stupid like monkey, 1331 01:03:08,996 --> 01:03:10,876 then its also going to catch that. 1332 01:03:10,876 --> 01:03:13,146 [Laughter] you didn't see that one coming, so its also catching 1333 01:03:13,556 --> 01:03:16,366 that because one of the things cs50's library does is 1334 01:03:16,366 --> 01:03:19,596 if you call the get in function, we were persistent, 1335 01:03:19,596 --> 01:03:21,856 we spent some good amount of time thinking through how 1336 01:03:21,856 --> 01:03:24,256 to implement the process of getting an int from a user, 1337 01:03:24,546 --> 01:03:27,646 so we, in our library are actually doing some 1338 01:03:27,696 --> 01:03:28,386 error checking. 1339 01:03:28,636 --> 01:03:31,506 We can't, we're not checking, the function is called get int, 1340 01:03:31,506 --> 01:03:33,296 we're not checking if it positive or negative, 1341 01:03:33,296 --> 01:03:34,846 because the function is called get int. 1342 01:03:34,846 --> 01:03:37,346 its not called get positive int or get negative int, 1343 01:03:37,536 --> 01:03:39,336 we're just getting an int so you have to do some 1344 01:03:39,336 --> 01:03:41,606 of the error checking but we can at least detect 1345 01:03:42,086 --> 01:03:45,346 when someone types in something random like monkey or banana 1346 01:03:45,776 --> 01:03:47,736 or random characters at the keyboard, 1347 01:03:47,736 --> 01:03:50,606 but then if you finally type 6, thanks for the 6 1348 01:03:50,606 --> 01:03:53,436 and it finally detects that its actually a legitimate number. 1349 01:03:53,636 --> 01:03:55,716 Well, let's take a look at this second version here. 1350 01:03:56,206 --> 01:03:59,816 So this is different how? 1351 01:04:00,866 --> 01:04:02,916 Well it doesn't do anything fundamentally different 1352 01:04:02,916 --> 01:04:05,856 but thus far we've had some fairly small programs, 1353 01:04:05,856 --> 01:04:08,146 I can introduce other variables if I want, 1354 01:04:08,376 --> 01:04:10,296 so there's this notion of a Boolean variable 1355 01:04:10,296 --> 01:04:13,436 which we promise existed, its value can only be true. 1356 01:04:13,576 --> 01:04:18,866 All lower case or false, but realize that's only 1357 01:04:18,866 --> 01:04:20,736 because of cs50's library. 1358 01:04:20,736 --> 01:04:23,536 Now other languages, c plus plus, java have true 1359 01:04:23,536 --> 01:04:26,166 and false built in, c is pretty old, it didn't have true 1360 01:04:26,166 --> 01:04:27,866 or false so we've reimplemented those 1361 01:04:28,056 --> 01:04:30,896 and you declare a Boolean variable with bool, 1362 01:04:30,996 --> 01:04:32,096 the name you want to give to the thing, 1363 01:04:32,356 --> 01:04:35,126 and then assign it a value, and notice what I'm doing, 1364 01:04:35,126 --> 01:04:38,746 I actually don't care in this program what the user is giving 1365 01:04:38,746 --> 01:04:41,686 me, I'm calling get int but I'm completely disregarding the 1366 01:04:41,686 --> 01:04:45,566 return value except to check if its greater that 0. 1367 01:04:45,626 --> 01:04:49,196 I'm not storing it in any value, if it is in fact, positive, 1368 01:04:49,196 --> 01:04:52,876 I change the value of thankful to true, notice I didn't bother 1369 01:04:52,876 --> 01:04:55,146 with curly braces around the int because its just one line 1370 01:04:55,146 --> 01:04:58,416 of code, and now my Boolean expression has just changed 1371 01:04:58,416 --> 01:04:59,436 to be this. 1372 01:05:00,416 --> 01:05:03,716 Here. But a common mistake might be this, you know what? 1373 01:05:03,716 --> 01:05:06,836 I'm very used to seeing equals, meaning if something is equal 1374 01:05:06,836 --> 01:05:08,356 to something I write one equal sign. 1375 01:05:08,766 --> 01:05:10,956 Now I just change the program as follows. 1376 01:05:11,146 --> 01:05:14,386 I change it to a single equal sign, make positive 2, 1377 01:05:14,386 --> 01:05:17,776 let me go ahead and run positive 2, its warning me 1378 01:05:17,776 --> 01:05:19,636 because I'm actually being a little loose with my code 1379 01:05:19,636 --> 01:05:23,686 as it is, let me give it a negative 23, hm. 1380 01:05:24,666 --> 01:05:29,536 So why is this all of a sudden broke in for negative values? 1381 01:05:30,126 --> 01:05:34,716 What's going on in this line of code? 1382 01:05:35,026 --> 01:05:37,686 So I do this, then what happens? 1383 01:05:40,836 --> 01:05:44,676 I'm assigning that variable called thankful no matter what a 1384 01:05:44,676 --> 01:05:48,136 value of false so then think about order of operations, 1385 01:05:48,136 --> 01:05:50,186 the parenthesis are outside, just like high school math, 1386 01:05:50,266 --> 01:05:52,286 parenthesis outside means do what's inside first 1387 01:05:52,486 --> 01:05:54,866 so thankful gets assigned the value of false, 1388 01:05:55,106 --> 01:05:57,376 so what's the value of that whole expression now? 1389 01:05:58,226 --> 01:06:02,266 False so inside parenthesis now is while false, 1390 01:06:02,676 --> 01:06:05,626 so that immediately means you don't repeat that loop 1391 01:06:05,626 --> 01:06:08,636 because it's false and you only repeat loops when it's true 1392 01:06:08,806 --> 01:06:11,356 so you immediately break and therefore say, 1393 01:06:11,356 --> 01:06:13,936 thanks for the positive integer even though it wasn't a 1394 01:06:13,936 --> 01:06:15,366 positive integer. 1395 01:06:15,556 --> 01:06:18,016 So here too, another sort of common mistake, 1396 01:06:18,246 --> 01:06:20,966 it's a little annoying that the world chose equals equals 1397 01:06:21,086 --> 01:06:22,656 and just equals for different meanings 1398 01:06:22,656 --> 01:06:25,196 and in fact its really going to annoy you when we get the php 1399 01:06:25,196 --> 01:06:27,446 and not only is there equal and equal, 1400 01:06:27,446 --> 01:06:30,676 there's also equal equal equal, but there's no equal. 1401 01:06:31,376 --> 01:06:33,276 There's three of them and they all have different subtle 1402 01:06:33,276 --> 01:06:35,576 meanings but a common bug there, so let's take a look 1403 01:06:35,576 --> 01:06:37,326 at one final version of this. 1404 01:06:37,526 --> 01:06:44,276 So positive 3.c is a little more elegant, so I got a little fancy 1405 01:06:44,316 --> 01:06:46,376 or a little lazy depending on how you look at it, 1406 01:06:46,666 --> 01:06:49,136 what's the one key difference for the bottom of this program? 1407 01:06:49,766 --> 01:06:52,826 Yes, so this bang sign. 1408 01:06:52,826 --> 01:06:54,696 So bang has a meaning at my shell, 1409 01:06:54,736 --> 01:06:56,716 my blinking prompt, if I do bang or ! 1410 01:06:56,836 --> 01:07:00,506 Then G that gives me the last command that started with G 1411 01:07:00,666 --> 01:07:02,216 but that's at your shell the blinking prompt. 1412 01:07:02,216 --> 01:07:05,486 This is now c code in that context of c, the ! 1413 01:07:05,746 --> 01:07:08,456 Or bang inverts Boolean values. 1414 01:07:08,456 --> 01:07:11,206 If it's true, it becomes false, if it's false it becomes true. 1415 01:07:11,506 --> 01:07:15,116 So this bang variable name is actually a very common syntaxic 1416 01:07:15,116 --> 01:07:17,186 trick just because it looks kind of nice and clean 1417 01:07:17,466 --> 01:07:19,866 to say while something is not true, 1418 01:07:20,056 --> 01:07:23,716 you literally say not variable name and this inverts its value 1419 01:07:23,926 --> 01:07:27,726 and because again, I've chosen my variable names cleverly 1420 01:07:27,816 --> 01:07:29,046 but in a straightforward way. 1421 01:07:29,046 --> 01:07:31,306 The fact that I called it thankful means 1422 01:07:31,306 --> 01:07:34,046 that even though I'm now writing c code and not pseudo code 1423 01:07:34,046 --> 01:07:37,416 and not scratch, while not thankful is 1424 01:07:37,416 --> 01:07:41,266 in fact the translation of c into what I might say in English 1425 01:07:41,496 --> 01:07:43,796 so this too speaks to the niceness 1426 01:07:43,796 --> 01:07:46,116 of choosing the variables appropriately it's a little more 1427 01:07:46,116 --> 01:07:49,446 clear than like, while not x like while not thankful again, 1428 01:07:49,836 --> 01:07:50,926 is all the more clear. 1429 01:07:51,556 --> 01:07:53,276 So that's all kind of interesting 1430 01:07:53,276 --> 01:07:54,786 and you now have these building blocks, 1431 01:07:55,096 --> 01:07:57,376 you'll need all those building blocks for the problems set 1432 01:07:57,376 --> 01:08:00,676 but you can do, and we will do, later in the semester, 1433 01:08:01,016 --> 01:08:04,036 even more powerful things using sort 1434 01:08:04,036 --> 01:08:05,896 of more familiar tools in API. 1435 01:08:06,346 --> 01:08:08,796 So again, this is meant as a teaser for what's ahead, 1436 01:08:08,796 --> 01:08:10,646 because I realize that in the first week or so 1437 01:08:10,646 --> 01:08:13,556 of the course you just made some pretty sexy scratch projects, 1438 01:08:13,596 --> 01:08:16,336 loud and interactive and what not, and now we kind of have 1439 01:08:16,376 --> 01:08:19,466 to take a step back and have you implement change making 1440 01:08:19,466 --> 01:08:21,866 and these smaller little Mario puzzles 1441 01:08:22,226 --> 01:08:25,066 but we'll very quickly build back up to things more familiar 1442 01:08:25,066 --> 01:08:26,886 so this is a program so if you're looking for something 1443 01:08:26,886 --> 01:08:29,466 to do on a Friday night or procrastinates 1444 01:08:29,466 --> 01:08:33,086 and you've never played with Google earth, download it, 1445 01:08:33,086 --> 01:08:36,956 its free, and this is sort of the earth version of Google maps 1446 01:08:37,086 --> 01:08:39,516 that Google acquired from a company and frankly, 1447 01:08:39,516 --> 01:08:41,866 you can take little virtual vacations with this thing. 1448 01:08:41,866 --> 01:08:45,466 So if I type in 1 Oxford Street 02138 this is actually going 1449 01:08:45,466 --> 01:08:47,346 to whisk me away, if you've never seen this before 1450 01:08:47,346 --> 01:08:51,676 to the science centers address, notice that we're going to zoom 1451 01:08:51,676 --> 01:08:54,346 and zoom and zoom, it gets pretty uninteresting quickly, 1452 01:08:54,346 --> 01:08:55,376 but, aha. 1453 01:08:55,726 --> 01:08:56,576 There we go. 1454 01:08:57,056 --> 01:08:59,156 It gets a little spooky at some point, if you've not played 1455 01:08:59,156 --> 01:09:01,306 with this stuff and you can do some of this in Google maps 1456 01:09:01,376 --> 01:09:02,236 but you can do things 1457 01:09:02,236 --> 01:09:05,696 like change the elevation you're actually looking at, 1458 01:09:06,206 --> 01:09:07,696 this gets kind of trippy, its even cooler frankly, 1459 01:09:07,696 --> 01:09:09,496 when you do it on an iPhone these days 1460 01:09:09,496 --> 01:09:12,306 with the accelerometer, we can do things a little fancier, 1461 01:09:12,306 --> 01:09:15,346 this is going somewhere, don't worry, so Paris France, 1462 01:09:15,546 --> 01:09:18,236 we can head over there, frankly its just cool doing 1463 01:09:18,606 --> 01:09:19,296 this sometimes. 1464 01:09:20,286 --> 01:09:24,746 All right, so now I'm zoomed in on Paris France, 1465 01:09:25,856 --> 01:09:29,976 and just to show you what's really cool these days with say, 1466 01:09:31,156 --> 01:09:34,176 graphics, let me go ahead and zoom in, if I can find it 1467 01:09:34,246 --> 01:09:38,296 from thousands of feet above, the Eifel Tower which is there, 1468 01:09:38,296 --> 01:09:41,776 I'm going to click a little radio button here and I'm going 1469 01:09:41,776 --> 01:09:45,526 to keep zooming in, its really rather trippy, frankly. 1470 01:09:46,346 --> 01:09:49,686 All right, zoom in there, yeah come on, 1471 01:09:50,886 --> 01:09:52,336 my Mac does not quite keep 1472 01:09:52,336 --> 01:09:54,776 up with the computational needs here, so it's pretty darn cool, 1473 01:09:54,776 --> 01:09:56,566 so what's the relevance to CS50? 1474 01:09:56,856 --> 01:09:58,286 So toward the end of the semester, 1475 01:09:58,556 --> 01:10:00,346 one of the problem sets will actually be 1476 01:10:00,346 --> 01:10:02,806 to implement this thing generally known as a mashup 1477 01:10:02,806 --> 01:10:05,006 where you actually take some data set another data set 1478 01:10:05,006 --> 01:10:06,996 or some kind of graphical interface and do something 1479 01:10:06,996 --> 01:10:10,116 with them together, whether its Google news, Google maps 1480 01:10:10,116 --> 01:10:12,476 or Google earth, what's really nice is that companies 1481 01:10:12,476 --> 01:10:14,596 like Google provide what are called again, 1482 01:10:14,596 --> 01:10:16,876 application program interfaces 1483 01:10:17,186 --> 01:10:19,626 which means their code their libraries, 1484 01:10:19,626 --> 01:10:21,376 similar in spirit to these .h files. 1485 01:10:21,596 --> 01:10:24,616 You can use in your own programs whether you're writing something 1486 01:10:24,726 --> 01:10:26,896 in C or php or in Java script 1487 01:10:27,186 --> 01:10:29,816 and so the demo I thought I'd show you just to think 1488 01:10:29,856 --> 01:10:32,976 through a problem, [Laughter] we still have a ways 1489 01:10:32,976 --> 01:10:34,006 to go, amazingly. 1490 01:10:34,636 --> 01:10:37,196 Just to show you how we might think through this, 1491 01:10:37,336 --> 01:10:41,526 I whipped up this little demo here, so let me go ahead and go 1492 01:10:41,526 --> 01:10:48,336 into this directory, let me just one moment, so we end right 1493 01:10:48,336 --> 01:10:52,016 on time, so I'm going to go ahead to CS50 going 1494 01:10:52,406 --> 01:10:55,186 into our course's web server, which lives elsewhere, 1495 01:10:55,186 --> 01:10:58,786 I'm going to go into our lectures directory, 1496 01:10:58,786 --> 01:11:01,726 the source code and map and all this is available on line 1497 01:11:01,886 --> 01:11:05,006 and what I did in advance of today, was a couple of things. 1498 01:11:05,096 --> 01:11:07,636 so one, even though its not due for several hours a bunch 1499 01:11:07,636 --> 01:11:10,006 of folks have submitted the surveys at the end 1500 01:11:10,006 --> 01:11:13,916 of problem set 0, and provided their hometown addresses. 1501 01:11:13,946 --> 01:11:15,756 So all of this has been scrubbed, so we don't know who's 1502 01:11:15,756 --> 01:11:18,916 from where, but we have in this CSV file, 1503 01:11:18,916 --> 01:11:22,996 comma separated values file, a whole bunch of hometowns 1504 01:11:22,996 --> 01:11:23,946 from people in this course, 1505 01:11:24,216 --> 01:11:26,576 you might have seen yourself scroll past really fast. 1506 01:11:26,716 --> 01:11:29,436 So a reasonable question to ask, and a question 1507 01:11:29,436 --> 01:11:31,926 that you'll be able to answer at the end of the semester is, 1508 01:11:32,216 --> 01:11:34,226 what can I do that's interesting with this? 1509 01:11:34,226 --> 01:11:36,676 Well Google maps for instance, provides you with the ability, 1510 01:11:36,706 --> 01:11:39,936 you probably know, to embed a map into your own personal 1511 01:11:39,936 --> 01:11:41,786 or corporate web site, that's pretty easy 1512 01:11:41,926 --> 01:11:44,446 and using a language called Java Script can you grab data 1513 01:11:44,446 --> 01:11:47,736 like that from this text file and actually embed it 1514 01:11:47,736 --> 01:11:50,876 into your own map but that begs the question, how do you go 1515 01:11:50,876 --> 01:11:52,086 from one to the other? 1516 01:11:52,086 --> 01:11:54,366 Well one of the things we'll be able to do ultimately, 1517 01:11:54,416 --> 01:11:57,066 is take what's in excel file, so a CSV file, 1518 01:11:57,266 --> 01:11:59,116 its something you would generally open in excel, 1519 01:11:59,276 --> 01:12:02,316 but if you've got 200, 300 names and hometowns, 1520 01:12:02,316 --> 01:12:03,946 odds are you don't want to be doing copying 1521 01:12:03,946 --> 01:12:06,246 and paste all day long, it will certainly take forever 1522 01:12:06,666 --> 01:12:08,666 so there's these things called, 1523 01:12:08,666 --> 01:12:11,386 there's these things called scripts and in fact, 1524 01:12:11,386 --> 01:12:12,836 and I'll leave this available on line, 1525 01:12:12,836 --> 01:12:13,786 I wrote this little script. 1526 01:12:13,946 --> 01:12:16,676 It's not many lines of code and in fact even though it's written 1527 01:12:16,676 --> 01:12:18,806 in php notice some familiar idea. 1528 01:12:18,926 --> 01:12:22,766 There's a loop in php called for each and apparently iterating 1529 01:12:22,766 --> 01:12:25,696 over things called hometowns and long story short, 1530 01:12:25,696 --> 01:12:28,736 what I did with this little script was convert the excel 1531 01:12:28,736 --> 01:12:33,506 file the survey.csv into what's called xml or really kml 1532 01:12:33,506 --> 01:12:36,476 which is a mark up language for mapping programs 1533 01:12:36,816 --> 01:12:39,986 and the end result is that I'm able to do this. 1534 01:12:40,116 --> 01:12:45,126 If I go ahead to any old browser, go to CS50.net/lectures 1535 01:12:46,026 --> 01:12:49,696 and pull up our source code for today and go 1536 01:12:49,696 --> 01:12:56,436 into the map directory therein, what we will see is this. 1537 01:12:57,146 --> 01:12:59,126 So frankly, it took like 10 minutes of effort 1538 01:12:59,126 --> 01:13:02,876 after downloading the csv file from the survey program 1539 01:13:02,876 --> 01:13:06,146 that we're using, and none of you apparently live in Cambridge 1540 01:13:06,146 --> 01:13:08,926 and have submitted yet, but if we start zooming out, 1541 01:13:09,006 --> 01:13:13,126 each of these little bubbles represents CS50 students 1542 01:13:13,126 --> 01:13:14,296 and it's really neat. 1543 01:13:14,296 --> 01:13:16,056 Some of you are really from pretty far away 1544 01:13:16,056 --> 01:13:18,256 and this is only some of the students in this course, 1545 01:13:18,606 --> 01:13:21,316 so with that said, that's where we're going, you got PSet 1 1546 01:13:21,316 --> 01:13:24,936 in your hands as of tonight, see you on Monday. 1547 01:13:27,516 --> 01:13:34,310 ?? ?? ?? ??