CS50-raw / a8Fyf3gwvfM.json
cutehusky's picture
Upload folder using huggingface_hub
0791e78 verified
[
{
"text": "[MUSIC PLAYING]",
"start": 0.0,
"duration": 0.0
},
{
"text": ">> DAVID J. MALAN: All right, this is CS50.",
"start": 13.95,
"duration": 2.29
},
{
"text": "And this is week one.",
"start": 16.24,
"duration": 1.77
},
{
"text": "So recall that last time in week zero,\nwe focused on computational thinking.",
"start": 18.01,
"duration": 4.04
},
{
"text": "And we transitioned from that to\nScratch, a graphical programming",
"start": 22.05,
"duration": 3.39
},
{
"text": "language from our friends\nat MIT's Media Lab.",
"start": 25.44,
"duration": 1.92
},
{
"text": ">> And with Scratch, did we explore\nideas like functions, and conditions,",
"start": 27.36,
"duration": 4.37
},
{
"text": "and loops, and variables, and even\nevents, and threads, and more.",
"start": 31.73,
"duration": 3.48
},
{
"text": "And today, we're going to\ncontinue using those ideas,",
"start": 35.21,
"duration": 2.67
},
{
"text": "and really taking them for\ngranted, but translate them",
"start": 37.88,
"duration": 2.75
},
{
"text": "to another language known as C. Now,\nC is a more traditional language.",
"start": 40.63,
"duration": 3.59
},
{
"text": "It's a lower level\nlanguage, if you will.",
"start": 44.22,
"duration": 1.8
},
{
"text": ">> It's purely textual.",
"start": 46.02,
"duration": 1.28
},
{
"text": "And so at first glance, it's\nall going to look rather cryptic",
"start": 47.3,
"duration": 2.61
},
{
"text": "if you've never programmed before.",
"start": 49.91,
"duration": 1.52
},
{
"text": "We're going to have\nsemi-colons, and parentheses,",
"start": 51.43,
"duration": 2.1
},
{
"text": "and curly braces, and more.",
"start": 53.53,
"duration": 1.62
},
{
"text": "But realize that even\nthough the syntax is",
"start": 55.15,
"duration": 2.09
},
{
"text": "about to look a little unfamiliar\nto most of you, see past that.",
"start": 57.24,
"duration": 3.36
},
{
"text": "And try to see the ideas\nthat are, indeed, familiar,",
"start": 60.6,
"duration": 2.62
},
{
"text": "because here in week one what\nwe'll begin to do is to compare,",
"start": 63.22,
"duration": 3.53
},
{
"text": "initially, Scratch versus C.",
"start": 66.75,
"duration": 2.23
},
{
"text": ">> So, for instance, recall that when we\nimplemented the first of our programs",
"start": 68.98,
"duration": 3.37
},
{
"text": "last time, we had a block that looked\na little something like this-- when",
"start": 72.35,
"duration": 3.87
},
{
"text": "green flag clicked, and then we had\none or more puzzle pieces below it,",
"start": 76.22,
"duration": 3.77
},
{
"text": "in this case, say, hello world.",
"start": 79.99,
"duration": 2.16
},
{
"text": "So, indeed, in Scratch,\nwhen I click that green flag",
"start": 82.15,
"duration": 2.72
},
{
"text": "to run my program, so\nto speak, these are",
"start": 84.87,
"duration": 2.52
},
{
"text": "the blocks that get executed, or run.",
"start": 87.39,
"duration": 2.13
},
{
"text": "And, specifically, Scratch\nsaid, hello, world.",
"start": 89.52,
"duration": 2.71
},
{
"text": ">> Now, I could have specified\ndifferent words here.",
"start": 92.23,
"duration": 3.147
},
{
"text": "But we'll see that, indeed, many\nof these blocks-- and indeed,",
"start": 95.377,
"duration": 2.583
},
{
"text": "in C many functions-- can be\nparametrized or customized",
"start": 97.96,
"duration": 3.92
},
{
"text": "to do different things.",
"start": 101.88,
"duration": 1.27
},
{
"text": "In fact, in C if we\nwant to convert, now,",
"start": 103.15,
"duration": 2.37
},
{
"text": "this Scratch program\nto this other language,",
"start": 105.52,
"duration": 2.047
},
{
"text": "we're going to write a\nlittle something like this.",
"start": 107.567,
"duration": 2.083
},
{
"text": ">> Granted, there is some unfamiliar\nsyntax there most likely, int,",
"start": 109.65,
"duration": 2.89
},
{
"text": "and parentheses, and void.",
"start": 112.54,
"duration": 1.84
},
{
"text": "But printf-- even though you would\nthink it would just be print.",
"start": 114.38,
"duration": 3.36
},
{
"text": "But print means print\nformatted, as we'll soon see.",
"start": 117.74,
"duration": 2.38
},
{
"text": "This literally will print\nto the screen whatever",
"start": 120.12,
"duration": 2.02
},
{
"text": "is inside of those parentheses, which\nof course in this case is, hello world.",
"start": 122.14,
"duration": 3.85
},
{
"text": ">> But you'll notice some other\nsyntax, some double quotes,",
"start": 125.99,
"duration": 3.3
},
{
"text": "that the parentheses at the end,\nthe semi-colon and the like.",
"start": 129.29,
"duration": 2.6
},
{
"text": "So there's a bit of overhead,\nso to speak, both cognitively",
"start": 131.89,
"duration": 3.137
},
{
"text": "and syntactically, that we're going\nto have to remember before long.",
"start": 135.027,
"duration": 2.833
},
{
"text": "But realize that with practice,\nthis will start to jump out at you.",
"start": 137.86,
"duration": 2.86
},
{
"text": ">> In fact, let's focus on that one\nfunction specifically-- in this case,",
"start": 140.72,
"duration": 4.2
},
{
"text": "say hello world.",
"start": 144.92,
"duration": 1.37
},
{
"text": "So say is the function.",
"start": 146.29,
"duration": 1.27
},
{
"text": "Hello world is its parameter,\nor argument, its customisation.",
"start": 147.56,
"duration": 3.76
},
{
"text": ">> And the equivalence in C is just\ngoing to be this one line here,",
"start": 151.32,
"duration": 3.0
},
{
"text": "where printf is equivalent to, say,\nthe double quoted string, hello",
"start": 154.32,
"duration": 4.39
},
{
"text": "world is equivalent, of course,\nto what's in the white box there.",
"start": 158.71,
"duration": 2.76
},
{
"text": "And the backslash n, though a little\nstrange and absent from Scratch,",
"start": 161.47,
"duration": 4.21
},
{
"text": "simply is going to have the effect we'll\nsee in a computer, like my Mac or a PC,",
"start": 165.68,
"duration": 3.7
},
{
"text": "of just moving the\ncursor to the next line.",
"start": 169.38,
"duration": 2.28
},
{
"text": "It's like hitting\nEnter on your keyboard.",
"start": 171.66,
"duration": 2.31
},
{
"text": ">> So we'll see that again before long.",
"start": 173.97,
"duration": 1.61
},
{
"text": "But first, let's take a look at this\nother example in the case of loops.",
"start": 175.58,
"duration": 3.06
},
{
"text": "We had this forever loop last time,\nwhich was a series of puzzle pieces",
"start": 178.64,
"duration": 4.19
},
{
"text": "that did something literally\nforever-- in this case,",
"start": 182.83,
"duration": 2.66
},
{
"text": "say, hello world, hello world,\nhello world, hello world.",
"start": 185.49,
"duration": 2.87
},
{
"text": "So it's an infinite loop by design.",
"start": 188.36,
"duration": 1.99
},
{
"text": ">> In C, if we want to implement this\nsame idea, we might simply do this.",
"start": 190.35,
"duration": 4.23
},
{
"text": "While true, printf hello world-- now\nwhile, just semantically, kind of",
"start": 194.58,
"duration": 4.99
},
{
"text": "conjures up the idea of doing\nsomething again, and again, and again,",
"start": 199.57,
"duration": 3.52
},
{
"text": "and for how long?",
"start": 203.09,
"duration": 0.89
},
{
"text": "Well, true-- recall that\ntrue is just on or one.",
"start": 203.98,
"duration": 4.01
},
{
"text": ">> And true is, of course, always true.",
"start": 207.99,
"duration": 2.67
},
{
"text": "So it's kind of a meaningless\nstatement just to say true.",
"start": 210.66,
"duration": 2.4
},
{
"text": "But indeed, this is deliberate,\nbecause if true is just always true,",
"start": 213.06,
"duration": 3.83
},
{
"text": "than while true just implies,\nif a little indirectly,",
"start": 216.89,
"duration": 3.96
},
{
"text": "that the following lines of code\nin between those curly braces",
"start": 220.85,
"duration": 3.22
},
{
"text": "should just execute again, and again,\nand again, and never actually stop.",
"start": 224.07,
"duration": 4.25
},
{
"text": ">> But if you do want your\nloop to stop, as we",
"start": 228.32,
"duration": 1.91
},
{
"text": "did last time with something like\nthis, repeat the following 50 times,",
"start": 230.23,
"duration": 4.27
},
{
"text": "in C we can do the same with what's\ncalled a for loop-- the keyword",
"start": 234.5,
"duration": 3.2
},
{
"text": "not being while, but for.",
"start": 237.7,
"duration": 1.63
},
{
"text": "And then we have some new syntax here,\nwith int i equals 0, i less than 50,",
"start": 239.33,
"duration": 3.96
},
{
"text": "i++.",
"start": 243.29,
"duration": 0.59
},
{
"text": "And we'll come back to that.",
"start": 243.88,
"duration": 1.55
},
{
"text": "But this is simply how we would\ntranslate the set of Scratch blocks",
"start": 245.43,
"duration": 4.23
},
{
"text": "to a set of C lines of code.",
"start": 249.66,
"duration": 3.419
},
{
"text": ">> Meanwhile, consider variables.",
"start": 253.079,
"duration": 1.371
},
{
"text": "And, in fact, we just\nsaw one a moment ago.",
"start": 254.45,
"duration": 2.09
},
{
"text": "And in the case of Scratch, if we\nwanted to declare a variable called i",
"start": 256.54,
"duration": 4.68
},
{
"text": "for i being integer, just a number,\nand we want to set it to some value,",
"start": 261.22,
"duration": 3.37
},
{
"text": "we would use this orange\nblock here-- set i to 0.",
"start": 264.59,
"duration": 3.82
},
{
"text": ">> And we'll see today and\nbeyond, just like last week,",
"start": 268.41,
"duration": 2.39
},
{
"text": "programmers do almost always\nstart counting from zero, really",
"start": 270.8,
"duration": 3.05
},
{
"text": "by convention.",
"start": 273.85,
"duration": 1.1
},
{
"text": "But also because recall from\nour discussion of binary,",
"start": 274.95,
"duration": 2.3
},
{
"text": "the smallest number you can\nrepresent with any number of bits",
"start": 277.25,
"duration": 2.74
},
{
"text": "is just going to be 0 itself.",
"start": 279.99,
"duration": 1.65
},
{
"text": "And so we'll generally start\ninitializing even our variables to 0.",
"start": 281.64,
"duration": 3.55
},
{
"text": ">> And in C to do the same,\nwe're going to say int",
"start": 285.19,
"duration": 2.52
},
{
"text": "for integer, i just by convention.",
"start": 287.71,
"duration": 2.4
},
{
"text": "I could have called this variable\nanything I want, just like in Scratch.",
"start": 290.11,
"duration": 3.28
},
{
"text": "And then equals 0 just assigns\nthe value 0 from the right",
"start": 293.39,
"duration": 4.38
},
{
"text": "and puts it into the variable, or the\nstorage container there, on the left.",
"start": 297.77,
"duration": 3.549
},
{
"text": "And the semi-colon as we'll see-- and\nwe've seen a few of these already--",
"start": 301.319,
"duration": 3.041
},
{
"text": "just means end of thought.",
"start": 304.36,
"duration": 2.17
},
{
"text": "Proceed to do something else\non the lines that follow.",
"start": 306.53,
"duration": 2.9
},
{
"text": ">> Now, what about Boolean expressions?",
"start": 309.43,
"duration": 1.9
},
{
"text": "Recall that in Scratch,\nthese were expressions",
"start": 311.33,
"duration": 2.99
},
{
"text": "that are either true\nor false-- questions,",
"start": 314.32,
"duration": 2.42
},
{
"text": "really, that are either true or false.",
"start": 316.74,
"duration": 2.17
},
{
"text": "So in the case of Scratch, we might\nask a simple question like this,",
"start": 318.91,
"duration": 3.05
},
{
"text": "is i less than 50?",
"start": 321.96,
"duration": 2.626
},
{
"text": "So i, again, is an integer.",
"start": 324.586,
"duration": 1.124
},
{
"text": "Maybe we're using it\nin a Scratch program",
"start": 325.71,
"duration": 1.5
},
{
"text": "to keep track of a score\nor something like that.",
"start": 327.21,
"duration": 2.1
},
{
"text": "So this syntax here in Scratch\njust means, is i less than 50?",
"start": 329.31,
"duration": 4.5
},
{
"text": "Well, thankfully, something is\nsimple in C. And to translate,",
"start": 333.81,
"duration": 3.52
},
{
"text": "this we would simply say i less\nthan 50, using the familiar key",
"start": 337.33,
"duration": 4.45
},
{
"text": "on your keyboard.",
"start": 341.78,
"duration": 1.07
},
{
"text": ">> Meanwhile, if you wanted to\nsay something more general,",
"start": 342.85,
"duration": 2.291
},
{
"text": "like, well, is x less than y where each\nof x and y are themselves variables?",
"start": 345.141,
"duration": 4.749
},
{
"text": "We can do the same thing\nin C, so long as we've",
"start": 349.89,
"duration": 2.39
},
{
"text": "created these variables already.",
"start": 352.28,
"duration": 1.662
},
{
"text": "And we'll see how to\ndo that before long.",
"start": 353.942,
"duration": 1.708
},
{
"text": "We would simply say x less than y.",
"start": 355.65,
"duration": 2.94
},
{
"text": ">> So you're starting to\nsee some similarities.",
"start": 358.59,
"duration": 1.94
},
{
"text": "And those folks who made\nScratch were certainly",
"start": 360.53,
"duration": 2.96
},
{
"text": "inspired by some of these basic ideas.",
"start": 363.49,
"duration": 1.76
},
{
"text": "And you'll see this kind of\nsyntax in many languages--",
"start": 365.25,
"duration": 5.1
},
{
"text": "not just Scratch, not\njust C, but Python,",
"start": 370.35,
"duration": 1.81
},
{
"text": "and JavaScript, and\nother languages still.",
"start": 372.16,
"duration": 2.63
},
{
"text": ">> Let's consider another construct\nfrom C, the notion of a condition,",
"start": 374.79,
"duration": 3.48
},
{
"text": "doing something conditionally.",
"start": 378.27,
"duration": 2.1
},
{
"text": "If something is true, do this.",
"start": 380.37,
"duration": 2.35
},
{
"text": "If something else is true, do that.",
"start": 382.72,
"duration": 1.737
},
{
"text": "It's sort of the programming\nequivalent of a fork in the road.",
"start": 384.457,
"duration": 2.583
},
{
"text": "Maybe it's a two-way fork,\na three-way fork, or more.",
"start": 387.04,
"duration": 2.69
},
{
"text": "And in Scratch, we might have\nseen something like this.",
"start": 389.73,
"duration": 3.07
},
{
"text": ">> So this one's a big one.",
"start": 392.8,
"duration": 1.21
},
{
"text": "But consider the relative\nsimplicity of the logic.",
"start": 394.01,
"duration": 2.74
},
{
"text": "If x is less than y, then say x is less\nthan y, else if x is greater than y,",
"start": 396.75,
"duration": 7.26
},
{
"text": "then say x is greater than y.",
"start": 404.01,
"duration": 2.22
},
{
"text": "And then, logically, if\nyou think back to Scratch",
"start": 406.23,
"duration": 2.07
},
{
"text": "or just your own human intuition,\nwell, if x is not greater than y, and x",
"start": 408.3,
"duration": 4.31
},
{
"text": "is not less than y, then of course\nx is going to be equal to y.",
"start": 412.61,
"duration": 4.39
},
{
"text": "So in this case, by nesting\nthose Scratch blocks,",
"start": 417.0,
"duration": 2.69
},
{
"text": "can we achieve a three\nway fork in the road?",
"start": 419.69,
"duration": 2.89
},
{
"text": ">> Meanwhile, if we want to\ndo that in C, it arguably",
"start": 422.58,
"duration": 2.4
},
{
"text": "looks a little simpler-- at least\nonce you get familiar with the syntax.",
"start": 424.98,
"duration": 3.44
},
{
"text": "If x is less than y,\nprintf x is less than y.",
"start": 428.42,
"duration": 3.63
},
{
"text": "Else if x is greater than y,\nprintf x is greater than y.",
"start": 432.05,
"duration": 4.09
},
{
"text": "Else printf x is equal to y-- and,\nagain, with those backslash ends just",
"start": 436.14,
"duration": 5.07
},
{
"text": "for those new lines so that if you\nactually ran this kind of program",
"start": 441.21,
"duration": 2.95
},
{
"text": "it would just move\nyour cursor ultimately",
"start": 444.16,
"duration": 1.78
},
{
"text": "to the next line of the screen.",
"start": 445.94,
"duration": 2.16
},
{
"text": ">> Now, meanwhile Scratch had other\nmore sophisticated features, only",
"start": 448.1,
"duration": 3.17
},
{
"text": "some of which we're going to\ninitially move over to the world of C.",
"start": 451.27,
"duration": 3.05
},
{
"text": "And one of them was\ncalled a list in Scratch.",
"start": 454.32,
"duration": 2.69
},
{
"text": "And this was a special\ntype of variable that",
"start": 457.01,
"duration": 2.09
},
{
"text": "allowed you to store multiple things\nin it back, to back, to back, to back.",
"start": 459.1,
"duration": 3.74
},
{
"text": ">> In C, it doesn't have\nlists, per se, but something",
"start": 462.84,
"duration": 2.7
},
{
"text": "that are more generally\ncalled arrays, although we'll",
"start": 465.54,
"duration": 2.55
},
{
"text": "come back later this semester\nto looking at something",
"start": 468.09,
"duration": 2.5
},
{
"text": "called a list, or really a linked list.",
"start": 470.59,
"duration": 2.19
},
{
"text": "But for now, the closest\nequivalent in C for us",
"start": 472.78,
"duration": 2.73
},
{
"text": "is going to be something\ncalled an array.",
"start": 475.51,
"duration": 1.835
},
{
"text": "And an array is simply a\nspecial type of variable",
"start": 477.345,
"duration": 2.395
},
{
"text": "that allows you to store data\nback, to back, to back, to back.",
"start": 479.74,
"duration": 3.42
},
{
"text": ">> And, indeed, in Scratch,\nif we wanted to access",
"start": 483.16,
"duration": 2.68
},
{
"text": "the first element of an array or\na list-- and I'm going to call it,",
"start": 485.84,
"duration": 3.19
},
{
"text": "by convention, argv, argument\nvector, but more on that before long.",
"start": 489.03,
"duration": 4.57
},
{
"text": "If I want to get at the first element\nof argv, in the world of Scratch",
"start": 493.6,
"duration": 3.49
},
{
"text": "you actually do typically\nstart counting from 1.",
"start": 497.09,
"duration": 3.84
},
{
"text": ">> And so I might get item 1 of argv.",
"start": 500.93,
"duration": 1.92
},
{
"text": "That's just how MIT implemented\nthe notion of lists.",
"start": 502.85,
"duration": 3.46
},
{
"text": "But in C, I'm going to\nmore simply just say, argv,",
"start": 506.31,
"duration": 3.55
},
{
"text": "which again is the name of my\nlist-- or to be clear, an array.",
"start": 509.86,
"duration": 2.898
},
{
"text": "And if I want the first\nelements, I'm going",
"start": 512.758,
"duration": 1.791
},
{
"text": "to use square brackets, which you\nmight not often used under a keyboard.",
"start": 514.549,
"duration": 3.341
},
{
"text": ">> But 0 just means, get me the first.",
"start": 517.89,
"duration": 2.26
},
{
"text": "So on occasion and as\ntime passes, we're going",
"start": 520.15,
"duration": 2.01
},
{
"text": "to start to see these dichotomies\nbetween Scratch and C,",
"start": 522.16,
"duration": 2.41
},
{
"text": "whereby Scratch uses one.",
"start": 524.57,
"duration": 1.5
},
{
"text": "We in C use 0 here.",
"start": 526.07,
"duration": 1.6
},
{
"text": "But you'll quickly see\nonce you understand",
"start": 527.67,
"duration": 1.75
},
{
"text": "the foundations of each language, that\nthese things start to get all the more",
"start": 529.42,
"duration": 3.5
},
{
"text": "familiar through practice and practice.",
"start": 532.92,
"duration": 3.94
},
{
"text": ">> So let's actually look now at a program.",
"start": 536.86,
"duration": 2.84
},
{
"text": "Here shall be the first of our C\nsource code for complete programs.",
"start": 539.7,
"duration": 4.331
},
{
"text": "And the program we're going\nto offer for consideration",
"start": 544.031,
"duration": 2.249
},
{
"text": "is the one that's equivalent\nto that earlier Scratch piece.",
"start": 546.28,
"duration": 3.06
},
{
"text": ">> So in here, we have what's\narguably the simplest C program",
"start": 549.34,
"duration": 3.87
},
{
"text": "you can write that\nactually does something.",
"start": 553.21,
"duration": 2.2
},
{
"text": "Now, we'll look past,\nfor now, has include,",
"start": 555.41,
"duration": 2.84
},
{
"text": "standard io.h, and these angle\nbrackets, and int, and void,",
"start": 558.25,
"duration": 2.94
},
{
"text": "and the curly braces, and the like.",
"start": 561.19,
"duration": 1.65
},
{
"text": ">> And let's just focus on\nwhat, at least intuitively,",
"start": 562.84,
"duration": 2.55
},
{
"text": "might jump out at you already.",
"start": 565.39,
"duration": 1.47
},
{
"text": "In fact, main, I don't\nnecessarily know what this is,",
"start": 566.86,
"duration": 3.44
},
{
"text": "but much like Scratch had that when\ngreen flag clicked puzzle piece,",
"start": 570.3,
"duration": 4.28
},
{
"text": "so does C as a programming language\nhave a main piece of code that",
"start": 574.58,
"duration": 4.49
},
{
"text": "gets executed by default. And, indeed,\nit's literally going to be called main.",
"start": 579.07,
"duration": 4.31
},
{
"text": ">> So main is a function.",
"start": 583.38,
"duration": 1.34
},
{
"text": "And it's a special function that exists\nin C that when you run a program,",
"start": 584.72,
"duration": 4.0
},
{
"text": "it is main that gets run by\ndefault. In the world of Scratch,",
"start": 588.72,
"duration": 4.0
},
{
"text": "it was usually when green flag\nclicked that got run by default.",
"start": 592.72,
"duration": 4.25
},
{
"text": ">> Meanwhile, we've seen this before,\nprintf or print formatted, that's",
"start": 596.97,
"duration": 4.16
},
{
"text": "going to be a function that comes with\nC, along with a whole bunch of others,",
"start": 601.13,
"duration": 4.49
},
{
"text": "that will from time and time\nagain, in order to do exactly",
"start": 605.62,
"duration": 4.52
},
{
"text": "as its name suggests, print something.",
"start": 610.14,
"duration": 2.31
},
{
"text": "What do we want to print?",
"start": 612.45,
"duration": 1.05
},
{
"text": "Well, we'll see that\nby enclosing characters",
"start": 613.5,
"duration": 2.27
},
{
"text": "like these-- hello world,\nbackslash n in double quotes,",
"start": 615.77,
"duration": 2.91
},
{
"text": "we can tell printf exactly\nwhat to print on the screen.",
"start": 618.68,
"duration": 4.36
},
{
"text": ">> But in order to do\nthat, we unfortunately",
"start": 623.04,
"duration": 3.39
},
{
"text": "need to take something that is\nalready cryptic to us humans,",
"start": 626.43,
"duration": 3.58
},
{
"text": "but at least it's somewhat readable--\nsharp include, standard io.h, int,",
"start": 630.01,
"duration": 4.5
},
{
"text": "main, void, printf, all of the magical\nincantations we just saw on the screen.",
"start": 634.51,
"duration": 4.83
},
{
"text": "But we actually have to\ngo more arcane still.",
"start": 639.34,
"duration": 3.13
},
{
"text": "We first need to translate the code\nthat we write into machine code.",
"start": 642.47,
"duration": 4.67
},
{
"text": "And recall from last week that machines,\nat least the ones we know here,",
"start": 647.14,
"duration": 4.23
},
{
"text": "at the end of the day only\nunderstand zeros and ones.",
"start": 651.37,
"duration": 3.08
},
{
"text": ">> And my God, if we had to write these\nzeros and ones to actually program,",
"start": 654.45,
"duration": 3.65
},
{
"text": "it would very, very quickly\ntake the fun out of anything.",
"start": 658.1,
"duration": 3.16
},
{
"text": "But it turns out, per last week,\nthat these patterns of zeros and ones",
"start": 661.26,
"duration": 3.89
},
{
"text": "just have special meaning.",
"start": 665.15,
"duration": 1.25
},
{
"text": "In certain contexts,\nthey might mean numbers.",
"start": 666.4,
"duration": 2.1
},
{
"text": ">> In some contexts, they might mean\nletters, or colors, or any number",
"start": 668.5,
"duration": 3.34
},
{
"text": "of other abstractions there upon.",
"start": 671.84,
"duration": 2.87
},
{
"text": "But just as your computer has\na CPU, Central Processing Unit,",
"start": 674.71,
"duration": 3.74
},
{
"text": "or the brains inside of your computer.",
"start": 678.45,
"duration": 1.94
},
{
"text": "It's usually Intel\ninside, because that's",
"start": 680.39,
"duration": 1.85
},
{
"text": "one of the biggest companies\nthat makes CPUs for computers.",
"start": 682.24,
"duration": 2.66
},
{
"text": ">> Well, Intel CPUs and others\nsimply have decided in advance",
"start": 684.9,
"duration": 4.01
},
{
"text": "that certain patterns of zeros and\nones shall mean specific things.",
"start": 688.91,
"duration": 5.06
},
{
"text": "Certain patterns of zeros and ones\nwill mean, print this to the screen,",
"start": 693.97,
"duration": 3.07
},
{
"text": "or add these two numbers, or\nsubtract these two numbers,",
"start": 697.04,
"duration": 2.67
},
{
"text": "or move this piece of data from\nmy computer's memory over here,",
"start": 699.71,
"duration": 3.6
},
{
"text": "or any number of other very low level,\nbut ultimately useful, operations.",
"start": 703.31,
"duration": 4.56
},
{
"text": "But, thankfully, we humans are not going\nto need to know this level of detail.",
"start": 707.87,
"duration": 5.152
},
{
"text": "Indeed, just like last time, where we\nabstracted again, and again, and again,",
"start": 713.022,
"duration": 3.208
},
{
"text": "building from very low level\nprimitives like zeros and ones",
"start": 716.23,
"duration": 2.7
},
{
"text": "to higher level concepts\nlike numbers, and letters,",
"start": 718.93,
"duration": 2.23
},
{
"text": "and colors, and more,\nso can we as programmers",
"start": 721.16,
"duration": 3.17
},
{
"text": "stand on the shoulders of\nothers who have come before us",
"start": 724.33,
"duration": 2.75
},
{
"text": "and use software that other\npeople have written before us--",
"start": 727.08,
"duration": 4.18
},
{
"text": "namely programs called compilers.",
"start": 731.26,
"duration": 3.08
},
{
"text": ">> C is a language that\nis usually compiled,",
"start": 734.34,
"duration": 3.43
},
{
"text": "which means converted from\nsource code to machine code.",
"start": 737.77,
"duration": 4.36
},
{
"text": "In particular, what this means\nis that if you've got your source",
"start": 742.13,
"duration": 3.1
},
{
"text": "code that you yourself write, as we soon\nwill in just a moment on the screen,",
"start": 745.23,
"duration": 4.3
},
{
"text": "and you want to convert it\nultimately to machine code--",
"start": 749.53,
"duration": 3.61
},
{
"text": "those zeros and ones that\nonly your Mac or your PC",
"start": 753.14,
"duration": 3.96
},
{
"text": "understands-- you've got a first\nfeed that source code in as",
"start": 757.1,
"duration": 4.13
},
{
"text": "input to a special\nprogram called a compiler,",
"start": 761.23,
"duration": 5.11
},
{
"text": "the output of which we\nshall see is machine code.",
"start": 766.34,
"duration": 2.634
},
{
"text": "And, indeed, last time we talked\nabout, really, at the end of the day,",
"start": 768.974,
"duration": 2.916
},
{
"text": "problem solving.",
"start": 771.89,
"duration": 0.72
},
{
"text": "You've got inputs.",
"start": 772.61,
"duration": 0.75
},
{
"text": "And you've got outputs.",
"start": 773.36,
"duration": 0.958
},
{
"text": "And you've got some kind\nof algorithm in the middle.",
"start": 774.318,
"duration": 2.242
},
{
"text": ">> Algorithms can surely be\nimplemented in software,",
"start": 776.56,
"duration": 3.27
},
{
"text": "as we saw with pseudocode last week\nand as we'll see with actual code",
"start": 779.83,
"duration": 3.07
},
{
"text": "this week.",
"start": 782.9,
"duration": 0.59
},
{
"text": "And so a compiler really just\nhas a set of algorithms inside",
"start": 783.49,
"duration": 2.94
},
{
"text": "of it that know how to\nconvert the special keywords,",
"start": 786.43,
"duration": 3.63
},
{
"text": "like main, and printf,\nand others that we just",
"start": 790.06,
"duration": 2.12
},
{
"text": "saw into the patterns of zeros and\nones that Intel inside and other CPUs",
"start": 792.18,
"duration": 5.44
},
{
"text": "actually understands.",
"start": 797.62,
"duration": 2.4
},
{
"text": "So how do we do this?",
"start": 800.02,
"duration": 2.44
},
{
"text": "Where do we get a compiler?",
"start": 802.46,
"duration": 2.01
},
{
"text": ">> Most of us here have a Mac or a PC.",
"start": 804.47,
"duration": 1.93
},
{
"text": "And you're running Mac OS, or\nWindows, or Linux, or Solaris,",
"start": 806.4,
"duration": 2.752
},
{
"text": "or any number of other\noperating systems.",
"start": 809.152,
"duration": 1.708
},
{
"text": "And, indeed, we could\ngo out onto the web",
"start": 810.86,
"duration": 1.708
},
{
"text": "and download a compiler\nfor your Mac or your PC",
"start": 812.568,
"duration": 3.142
},
{
"text": "for your particular operating system.",
"start": 815.71,
"duration": 1.65
},
{
"text": "But we would all be on\ndifferent pages, so to speak.",
"start": 817.36,
"duration": 2.257
},
{
"text": "We'd have slightly\ndifferent configurations.",
"start": 819.617,
"duration": 1.833
},
{
"text": "And things wouldn't work all the same.",
"start": 821.45,
"duration": 1.76
},
{
"text": "And, indeed, these days\nmany of us don't use",
"start": 823.21,
"duration": 2.07
},
{
"text": "software that runs only on our laptops.",
"start": 825.28,
"duration": 2.236
},
{
"text": "Instead, we use something\nlike a browser that",
"start": 827.516,
"duration": 1.874
},
{
"text": "allows us to access web-based\napplications in the cloud.",
"start": 829.39,
"duration": 3.54
},
{
"text": "And later this semester,\nwe will do exactly that.",
"start": 832.93,
"duration": 2.7
},
{
"text": "We will write applications or\nsoftware using code-- not C,",
"start": 835.63,
"duration": 4.03
},
{
"text": "but other languages like Python and\nJavaScript-- that run in the cloud.",
"start": 839.66,
"duration": 3.2
},
{
"text": ">> And to do that, we ourselves\nduring the semester",
"start": 842.86,
"duration": 3.0
},
{
"text": "will actually use a cloud-based\nenvironment known as CS50 IDE.",
"start": 845.86,
"duration": 6.03
},
{
"text": "This is a web-based programming\nenvironment, or integrated development",
"start": 851.89,
"duration": 4.14
},
{
"text": "environment, IDe, that's built atop some\nopen source software called Cloud 9.",
"start": 856.03,
"duration": 4.58
},
{
"text": "And we've made some pedagogical\nsimplifications to it",
"start": 860.61,
"duration": 2.356
},
{
"text": "so as to hide certain features in\nthe first weeks that we don't need,",
"start": 862.966,
"duration": 2.874
},
{
"text": "after which you can\nreveal them and do most",
"start": 865.84,
"duration": 1.93
},
{
"text": "anything you want with the environment.",
"start": 867.77,
"duration": 1.63
},
{
"text": ">> And it allows us, too, to\npre-install certain software.",
"start": 869.4,
"duration": 3.07
},
{
"text": "Things like a so-called CS50\nlibrary, which we'll soon see",
"start": 872.47,
"duration": 2.86
},
{
"text": "provides us in C with some\nadditional functionality.",
"start": 875.33,
"duration": 3.88
},
{
"text": "So if you go to, ultimately, CS50.io,\nyou'll be prompted to log in,",
"start": 879.21,
"duration": 5.182
},
{
"text": "and once you do and create\nan account for free,",
"start": 884.392,
"duration": 1.958
},
{
"text": "you will be able to access an\nenvironment that looks quite like this.",
"start": 886.35,
"duration": 5.8
},
{
"text": ">> Now, this is in the default mode.",
"start": 892.15,
"duration": 1.61
},
{
"text": "Everything is nice and\nbright on the screen.",
"start": 893.76,
"duration": 1.89
},
{
"text": "Many of us have a habit of\nworking on CS50 piece that's",
"start": 895.65,
"duration": 2.291
},
{
"text": "quite late into the night.",
"start": 897.941,
"duration": 1.209
},
{
"text": "And so some of you might prefer to\nturn it into night mode, so to speak.",
"start": 899.15,
"duration": 3.25
},
{
"text": ">> But, ultimately, what you're\ngoing to see within CS50 IDE",
"start": 902.4,
"duration": 3.15
},
{
"text": "is three distinct areas--\nan area on the left where",
"start": 905.55,
"duration": 2.79
},
{
"text": "your files are going to be in the\ncloud, an area on the top right",
"start": 908.34,
"duration": 4.264
},
{
"text": "where your code is going to be editable.",
"start": 912.604,
"duration": 1.666
},
{
"text": "You'll be able to open\nindividual tabs for any program",
"start": 914.27,
"duration": 2.38
},
{
"text": "that you write this semester inside\nof that top right hand corner.",
"start": 916.65,
"duration": 3.02
},
{
"text": "And then most arcanely,\nand yet powerfully,",
"start": 919.67,
"duration": 3.4
},
{
"text": "is going to be this thing at the\nbottom known as a terminal window.",
"start": 923.07,
"duration": 3.54
},
{
"text": ">> This is an old school\nCommand Line Interface,",
"start": 926.61,
"duration": 2.84
},
{
"text": "or CLI, that allows\nyou to execute commands",
"start": 929.45,
"duration": 2.79
},
{
"text": "on the computer-- in this case,\nthe computer in the cloud--",
"start": 932.24,
"duration": 3.02
},
{
"text": "to do things like compile your code\nfrom source code to machine code,",
"start": 935.26,
"duration": 3.83
},
{
"text": "to run your programs, or to start your\nweb server, or to access your database,",
"start": 939.09,
"duration": 4.51
},
{
"text": "and any number of other techniques\nthat we'll start to use before long.",
"start": 943.6,
"duration": 3.854
},
{
"text": "But to get there, we're\ngoing to actually have",
"start": 947.454,
"duration": 1.916
},
{
"text": "to go online and start playing.",
"start": 949.37,
"duration": 1.87
},
{
"text": "And to do that, let's first\nstart tinkering with main,",
"start": 951.24,
"duration": 3.159
},
{
"text": "and write the main part of a program.",
"start": 954.399,
"duration": 1.541
},
{
"text": "And let's use that function\nprintf, which we used earlier,",
"start": 955.94,
"duration": 3.23
},
{
"text": "simply to say something.",
"start": 959.17,
"duration": 1.88
},
{
"text": ">> So here I am already inside of CS50 IDE.",
"start": 961.05,
"duration": 3.86
},
{
"text": "I've logged in advance.",
"start": 964.91,
"duration": 1.02
},
{
"text": "And I full screened the window.",
"start": 965.93,
"duration": 1.43
},
{
"text": "And so, ultimately, you\ntoo in coming problems",
"start": 967.36,
"duration": 2.31
},
{
"text": "will follow similar steps that\nwill provide online documentation.",
"start": 969.67,
"duration": 3.29
},
{
"text": "So you don't need to worry about\nabsorbing every little technical step",
"start": 972.96,
"duration": 3.4
},
{
"text": "that I do here today.",
"start": 976.36,
"duration": 1.37
},
{
"text": ">> But you'll get a screen like this.",
"start": 977.73,
"duration": 1.492
},
{
"text": "I happen to be in night mode.",
"start": 979.222,
"duration": 1.208
},
{
"text": "And you can brighten everything\nup by disabling night mode.",
"start": 980.43,
"duration": 2.514
},
{
"text": "And at the end of the\nday, you're going to see",
"start": 982.944,
"duration": 1.916
},
{
"text": "these three main areas-- the file\nbrowser at left, the code tabs up top,",
"start": 984.86,
"duration": 5.23
},
{
"text": "and the terminal window at the bottom.",
"start": 990.09,
"duration": 2.34
},
{
"text": ">> Let me go ahead and\nwrite my first program.",
"start": 992.43,
"duration": 2.46
},
{
"text": "I'm going to preemptively go to File,\nSave, and save my file as hello.c.",
"start": 994.89,
"duration": 7.41
},
{
"text": "Indeed, by convention, any program we\nwrite that's written in the C language",
"start": 1002.3,
"duration": 4.55
},
{
"text": "should be named something\ndot c, by convention.",
"start": 1006.85,
"duration": 2.889
},
{
"text": "So I'm going to name it hello.c, because\nI just want to say hello to the world.",
"start": 1009.739,
"duration": 3.291
},
{
"text": "Now I'm going to zoom\nout and click Save.",
"start": 1013.03,
"duration": 1.79
},
{
"text": "And all I have here now is a tab\nin which I can start writing code.",
"start": 1014.82,
"duration": 3.36
},
{
"text": ">> This is not going to compile.",
"start": 1018.18,
"duration": 1.31
},
{
"text": "This means nothing.",
"start": 1019.49,
"duration": 0.81
},
{
"text": "And so even if I converted\nthis to zeros and ones,",
"start": 1020.3,
"duration": 2.45
},
{
"text": "the CPU is going to have no\nidea what's going around.",
"start": 1022.75,
"duration": 2.64
},
{
"text": "But if I write lines that do match\nup with C's conventions-- C being,",
"start": 1025.39,
"duration": 8.78
},
{
"text": "again, this language-- with syntax like\nthis, printf hello world-- and I've",
"start": 1034.17,
"duration": 5.98
},
{
"text": "gotten comfortable with\ndoing this over time.",
"start": 1040.15,
"duration": 2.06
},
{
"text": "So I don't think I made\nany typographical errors.",
"start": 1042.21,
"duration": 2.3
},
{
"text": ">> But, invariably, the very first\ntime you do this, you will.",
"start": 1044.51,
"duration": 3.4
},
{
"text": "And what I am about to do might very\nwell not work for you the first time.",
"start": 1047.91,
"duration": 3.18
},
{
"text": "And that's perfectly OK,\nbecause right now you",
"start": 1051.09,
"duration": 2.52
},
{
"text": "might just see a whole lot of newness,\nbut over time once you get familiar",
"start": 1053.61,
"duration": 4.052
},
{
"text": "with this environment, and\nthis language, and others,",
"start": 1057.662,
"duration": 2.208
},
{
"text": "you'll start to see things that\nare either correct or incorrect.",
"start": 1059.87,
"duration": 2.5
},
{
"text": ">> And this is what the\nteaching fellows and course",
"start": 1062.37,
"duration": 1.999
},
{
"text": "assistants get so good at over time, is\nspotting mistakes or bugs in your code.",
"start": 1064.369,
"duration": 4.411
},
{
"text": "But I claim that there\nare no bugs in this code.",
"start": 1068.78,
"duration": 3.33
},
{
"text": "So I now want to run this program.",
"start": 1072.11,
"duration": 1.88
},
{
"text": ">> Now on my own Mac or PC, I'm in\nthe habit of double clicking icons",
"start": 1073.99,
"duration": 3.45
},
{
"text": "when I want to run some program.",
"start": 1077.44,
"duration": 1.91
},
{
"text": "But that's not the model here.",
"start": 1079.35,
"duration": 1.73
},
{
"text": "In this environment, which is CS50 IDE.",
"start": 1081.08,
"duration": 3.49
},
{
"text": "We are using an operating\nsystem called Linux.",
"start": 1084.57,
"duration": 2.622
},
{
"text": "Linux is reminiscent of another\noperating system, generally known",
"start": 1087.192,
"duration": 2.708
},
{
"text": "as Unix.",
"start": 1089.9,
"duration": 0.95
},
{
"text": "And Linux is particularly known for\nhaving a Command Line Environment, CLI.",
"start": 1090.85,
"duration": 5.49
},
{
"text": "Now, we're using a specific\nflavor of Linux called Ubuntu.",
"start": 1096.34,
"duration": 3.73
},
{
"text": "And Ubuntu is simply a\ncertain version of Linux.",
"start": 1100.07,
"duration": 2.7
},
{
"text": ">> But these Linux's these days do actually\ncome with graphical user interfaces.",
"start": 1102.77,
"duration": 5.13
},
{
"text": "And the one we happen to\nbe using here is web-based.",
"start": 1107.9,
"duration": 2.46
},
{
"text": "So this might look even a\nlittle different from something",
"start": 1110.36,
"duration": 2.375
},
{
"text": "you yourself might have\nseen or run in the past.",
"start": 1112.735,
"duration": 2.575
},
{
"text": ">> So I'm going to go ahead\nnow and do the following.",
"start": 1115.31,
"duration": 2.6
},
{
"text": "I've saved this file as hello.c.",
"start": 1117.91,
"duration": 3.04
},
{
"text": "I'm going to go ahead and\ntype clanghello.c So Clang",
"start": 1120.95,
"duration": 6.4
},
{
"text": "for the C language is a compiler.",
"start": 1127.35,
"duration": 2.5
},
{
"text": "It's pre-installed in CS50 IDE.",
"start": 1129.85,
"duration": 2.102
},
{
"text": "And you can absolutely download and\ninstall this on your own Mac or PC.",
"start": 1131.952,
"duration": 2.958
},
{
"text": ">> But, again, you wouldn't have all of\nthe pre-configuration done for you.",
"start": 1134.91,
"duration": 3.0
},
{
"text": "So for now, I'm just\ngoing to run clanghello.c.",
"start": 1137.91,
"duration": 3.03
},
{
"text": "And now notice this syntax\nhere will eventually",
"start": 1140.94,
"duration": 2.3
},
{
"text": "realize just means that I'm in a\nfolder or directory called Workspace.",
"start": 1143.24,
"duration": 3.69
},
{
"text": "This dollar sign is just convention\nfor meaning, type your commands here.",
"start": 1146.93,
"duration": 4.1
},
{
"text": ">> It's what's called a prompt, just\nby convention is dollar sign.",
"start": 1151.03,
"duration": 3.53
},
{
"text": "And if I go ahead now and click\nEnter, nothing seems to have happened.",
"start": 1154.56,
"duration": 4.57
},
{
"text": "But that's actually a good thing.",
"start": 1159.13,
"duration": 1.8
},
{
"text": "The less that happens on\nyour screen, the more likely",
"start": 1160.93,
"duration": 2.72
},
{
"text": "your code is to be correct,\nat least syntactically.",
"start": 1163.65,
"duration": 3.06
},
{
"text": ">> So if I want to run this\nprogram, what do I do?",
"start": 1166.71,
"duration": 2.41
},
{
"text": "Well, it turns out that the\ndefault name by convention",
"start": 1169.12,
"duration": 4.65
},
{
"text": "for programs when you don't specify a\nname for your program is just a.out.",
"start": 1173.77,
"duration": 5.084
},
{
"text": "And this syntax too, you'll\nget familiar with before long.",
"start": 1178.854,
"duration": 2.416
},
{
"text": ">> Dot slash just means, hey, CS50\nIDE, run a program called a.out",
"start": 1181.27,
"duration": 6.23
},
{
"text": "that's inside my current directory.",
"start": 1187.5,
"duration": 1.9
},
{
"text": "That dot means the current directory.",
"start": 1189.4,
"duration": 2.12
},
{
"text": "And we'll see what other such sequences\nof characters means before long.",
"start": 1191.52,
"duration": 3.52
},
{
"text": ">> So here we go, Enter, hello world.",
"start": 1195.04,
"duration": 3.39
},
{
"text": "And you'll notice, that what happened?",
"start": 1198.43,
"duration": 1.65
},
{
"text": "Not only did it print hello world.",
"start": 1200.08,
"duration": 1.5
},
{
"text": "It also moved the\ncursor to the next line.",
"start": 1201.58,
"duration": 4.41
},
{
"text": ">> And why was that?",
"start": 1205.99,
"duration": 1.17
},
{
"text": "What was the code that we wrote before\nthat ensured that the cursor would",
"start": 1207.16,
"duration": 5.24
},
{
"text": "go on the next line?",
"start": 1212.4,
"duration": 2.482
},
{
"text": "Funny thing about a\ncomputer is it's only going",
"start": 1214.882,
"duration": 1.958
},
{
"text": "to do literally what you tell it to do.",
"start": 1216.84,
"duration": 1.73
},
{
"text": ">> So if you tell it to printf hello,\ncomma, space, world, close quote,",
"start": 1218.57,
"duration": 7.48
},
{
"text": "it's literally only going\nto print those characters.",
"start": 1226.05,
"duration": 3.04
},
{
"text": "But I had this special character\nat the end, recall, backslash n.",
"start": 1229.09,
"duration": 2.89
},
{
"text": "And that's what ensured\nthat the character went",
"start": 1231.98,
"duration": 2.25
},
{
"text": "to the next line of the screen.",
"start": 1234.23,
"duration": 2.34
},
{
"text": ">> In fact, let me go and do this.",
"start": 1236.57,
"duration": 1.527
},
{
"text": "Let me go ahead and delete this.",
"start": 1238.097,
"duration": 1.333
},
{
"text": "Now, notice that the\ntop of my screen there's",
"start": 1239.43,
"duration": 1.75
},
{
"text": "a little red light in\nthe tab indicating,",
"start": 1241.18,
"duration": 1.71
},
{
"text": "hey, you've not saved your file.",
"start": 1242.89,
"duration": 2.157
},
{
"text": "So I'm going to go ahead with control\nS or command S, save the file.",
"start": 1245.047,
"duration": 2.833
},
{
"text": "Now it goes-- went for a moment-- green.",
"start": 1247.88,
"duration": 3.25
},
{
"text": "And now it's back to\njust being a close icon.",
"start": 1251.13,
"duration": 2.63
},
{
"text": ">> If I now run clanghello.c again,\nEnter, dot slash, a.out, Enter,",
"start": 1253.76,
"duration": 8.1
},
{
"text": "you'll see that it still worked.",
"start": 1261.86,
"duration": 2.25
},
{
"text": "But it's arguably a little buggy.",
"start": 1264.11,
"duration": 1.91
},
{
"text": "Right now, my prompt-- workspace,\nand then that dollar sign,",
"start": 1266.02,
"duration": 2.694
},
{
"text": "and then my actual prompt--\nis all on the same line.",
"start": 1268.714,
"duration": 2.166
},
{
"text": "So this certainly an aesthetic bug,\neven if it's not really a logical bug.",
"start": 1270.88,
"duration": 3.66
},
{
"text": ">> So I'm going to undo what I just did.",
"start": 1274.54,
"duration": 1.71
},
{
"text": "I'm going to rerun a.out.",
"start": 1276.25,
"duration": 2.31
},
{
"text": "Notice I've added the\nnewline character back.",
"start": 1278.56,
"duration": 4.15
},
{
"text": "I've saved the file.",
"start": 1282.71,
"duration": 1.57
},
{
"text": ">> So I'm going to rerun a.out, and--\ndammit, a bug, a bug meaning mistake.",
"start": 1284.28,
"duration": 7.35
},
{
"text": "So the bug is that even though\nI added the backslash n there,",
"start": 1291.63,
"duration": 3.39
},
{
"text": "re-saved, re-ran the program,\nthe behavior was the same.",
"start": 1295.02,
"duration": 6.16
},
{
"text": "Why would that be?",
"start": 1301.18,
"duration": 1.46
},
{
"text": ">> I'm missing a step, right?",
"start": 1302.64,
"duration": 1.27
},
{
"text": "That key step earlier was that you have\nto-- when you change your source code,",
"start": 1303.91,
"duration": 3.71
},
{
"text": "it turns out also run\nit through the compiler",
"start": 1307.62,
"duration": 1.99
},
{
"text": "again so you get new machine code.",
"start": 1309.61,
"duration": 1.492
},
{
"text": "And the machine code,\nthe zeros and ones,",
"start": 1311.102,
"duration": 1.708
},
{
"text": "are going to be almost identical, but\nnot perfectly so, because we need,",
"start": 1312.81,
"duration": 3.45
},
{
"text": "of course, that new line.",
"start": 1316.26,
"duration": 1.25
},
{
"text": ">> So to fix this, I'm going to need\nto rerun clanghello.c, enter, dot",
"start": 1317.51,
"duration": 5.13
},
{
"text": "slash, a.out.",
"start": 1322.64,
"duration": 1.16
},
{
"text": "And now, hello world is back\nto where I expect it to be.",
"start": 1323.8,
"duration": 4.602
},
{
"text": "So this is all fine and good.",
"start": 1328.402,
"duration": 1.208
},
{
"text": "But a.out is a pretty stupid name for a\nprogram, even though it happens to be,",
"start": 1329.61,
"duration": 3.54
},
{
"text": "for historical reasons, the\ndefault-- meaning assembly outputs.",
"start": 1333.15,
"duration": 3.38
},
{
"text": ">> But let me go ahead here\nand do this differently.",
"start": 1336.53,
"duration": 4.25
},
{
"text": "I want my hello world program\nto actually be called hello.",
"start": 1340.78,
"duration": 3.98
},
{
"text": "So if it were an icon on my\ndesktop, it wouldn't be a.out.",
"start": 1344.76,
"duration": 3.56
},
{
"text": "It would be called hello.",
"start": 1348.32,
"duration": 1.41
},
{
"text": ">> So to do this, it turns out\nthat Clang, like many programs,",
"start": 1349.73,
"duration": 3.93
},
{
"text": "supports command line arguments,\nor flags, or switches,",
"start": 1353.66,
"duration": 4.32
},
{
"text": "which simply influence its behavior.",
"start": 1357.98,
"duration": 1.62
},
{
"text": "Specifically, Clang supports a dash o\nflag, which then takes a second word.",
"start": 1359.6,
"duration": 5.56
},
{
"text": "In this case, I'll arbitrarily,\nbut reasonably, call it hello.",
"start": 1365.16,
"duration": 3.03
},
{
"text": "But I could call it anything\nI want, except a.out, which",
"start": 1368.19,
"duration": 2.52
},
{
"text": "would be rather besides the point.",
"start": 1370.71,
"duration": 1.68
},
{
"text": ">> And then just specify the name\nof the file I do want to compile.",
"start": 1372.39,
"duration": 3.25
},
{
"text": "So now even though at the beginning\nof the command I still have Clang,",
"start": 1375.64,
"duration": 3.55
},
{
"text": "at the end of the command\nI still have the filename,",
"start": 1379.19,
"duration": 2.22
},
{
"text": "I now have these command line\narguments, these flags that are saying,",
"start": 1381.41,
"duration": 4.11
},
{
"text": "oh, by the way, output-o, a file\ncalled hello, not the default a.out.",
"start": 1385.52,
"duration": 5.66
},
{
"text": ">> So if I hit Enter now, nothing\nseems to have happened.",
"start": 1391.18,
"duration": 2.63
},
{
"text": "And, yet, now I can do dot slash hello.",
"start": 1393.81,
"duration": 4.09
},
{
"text": "So it's the same program.",
"start": 1397.9,
"duration": 1.189
},
{
"text": "The zeros and ones are\nidentical at the end of the day.",
"start": 1399.089,
"duration": 2.291
},
{
"text": ">> But they're in two\ndifferent files-- a.out,",
"start": 1401.38,
"duration": 2.83
},
{
"text": "which is the first version\nand just foolishly named,",
"start": 1404.21,
"duration": 2.28
},
{
"text": "and now hello, which is a much\nmore compelling name for a program.",
"start": 1406.49,
"duration": 3.76
},
{
"text": "But, honestly, I am never\ngoing to remember this again,",
"start": 1410.25,
"duration": 2.945
},
{
"text": "and again, and again.",
"start": 1413.195,
"duration": 0.875
},
{
"text": "And, actually, as we write\nmore complicated programs,",
"start": 1414.07,
"duration": 2.341
},
{
"text": "the commands you're\ngoing to have to write",
"start": 1416.411,
"duration": 1.749
},
{
"text": "are going to get even\nmore complicated still.",
"start": 1418.16,
"duration": 2.76
},
{
"text": ">> And so not to worry.",
"start": 1420.92,
"duration": 1.02
},
{
"text": "It turns out that humans before\nus have realized they too",
"start": 1421.94,
"duration": 4.28
},
{
"text": "had this exact same problem.",
"start": 1426.22,
"duration": 1.31
},
{
"text": "They too did not enjoy having to\ntype fairly long, arcane commands,",
"start": 1427.53,
"duration": 3.37
},
{
"text": "let alone remember them.",
"start": 1430.9,
"duration": 1.3
},
{
"text": "And so humans before us have made\nother programs that make it easier",
"start": 1432.2,
"duration": 3.87
},
{
"text": "to compile your software.",
"start": 1436.07,
"duration": 1.6
},
{
"text": ">> And, indeed, one such\nprogram is called Make.",
"start": 1437.67,
"duration": 3.939
},
{
"text": "So I'm going to go ahead and do this.",
"start": 1441.609,
"duration": 1.541
},
{
"text": "I'm going to undo everything I\njust did in the following way.",
"start": 1443.15,
"duration": 2.541
},
{
"text": "Let me type LS.",
"start": 1445.691,
"duration": 1.999
},
{
"text": "And you'll notice three things--\na.out, and a star, hello",
"start": 1447.69,
"duration": 3.29
},
{
"text": "and a star, and hello.c.",
"start": 1450.98,
"duration": 1.83
},
{
"text": "Hopefully, this should\nbe a little intuitive,",
"start": 1452.81,
"duration": 1.92
},
{
"text": "insofar as earlier there was\nnothing in this workspace.",
"start": 1454.73,
"duration": 3.49
},
{
"text": "There was nothing that I had\ncreated until we started class.",
"start": 1458.22,
"duration": 3.02
},
{
"text": ">> And I created hello.c.",
"start": 1461.24,
"duration": 1.6
},
{
"text": "I then compiled it, and called it a.out.",
"start": 1462.84,
"duration": 1.704
},
{
"text": "And then I compiled it again slightly\ndifferently and called it hello.",
"start": 1464.544,
"duration": 2.916
},
{
"text": "So I have three files in this directory,\nin this folder called Workspace.",
"start": 1467.46,
"duration": 5.37
},
{
"text": "Now, I can see that as well\nif I zoom out actually.",
"start": 1472.83,
"duration": 2.175
},
{
"text": ">> If I zoom out here and\nlook at that top right hand",
"start": 1475.005,
"duration": 2.525
},
{
"text": "corner, as promised the left\nhand side of your screen",
"start": 1477.53,
"duration": 2.41
},
{
"text": "is always going to show you\nwhat's in your account, what's",
"start": 1479.94,
"duration": 3.05
},
{
"text": "inside of CS50 IDE.",
"start": 1482.99,
"duration": 1.8
},
{
"text": "And there is three files there.",
"start": 1484.79,
"duration": 1.89
},
{
"text": ">> So I want to get rid of a.out and hello.",
"start": 1486.68,
"duration": 2.39
},
{
"text": "And as you might\nimagine intuitively, you",
"start": 1489.07,
"duration": 2.205
},
{
"text": "could sort of control click\nor right click on this.",
"start": 1491.275,
"duration": 2.125
},
{
"text": "And this little menu pops up.",
"start": 1493.4,
"duration": 1.19
},
{
"text": "You can download the file, run\nit, preview it, refresh, rename,",
"start": 1494.59,
"duration": 2.58
},
{
"text": "or what not.",
"start": 1497.17,
"duration": 0.53
},
{
"text": ">> And I could just delete,\nand it would go away.",
"start": 1497.7,
"duration": 2.56
},
{
"text": "But let's do things with a command\nline for now, so as to get comfortable",
"start": 1500.26,
"duration": 5.0
},
{
"text": "with this, and do the following.",
"start": 1505.26,
"duration": 1.75
},
{
"text": "I'm going to go ahead and remove\na.out by typing literally rma.out.",
"start": 1507.01,
"duration": 5.335
},
{
"text": "It turns out, the command for\nremoving or deleting something,",
"start": 1512.345,
"duration": 2.545
},
{
"text": "is not remove or delete.",
"start": 1514.89,
"duration": 1.39
},
{
"text": ">> It's more succinctly RM, just to save\nyou some keystrokes, and hit Enter.",
"start": 1516.28,
"duration": 4.98
},
{
"text": "Now we're going to be somewhat\ncryptically remove regular file a.out.",
"start": 1521.26,
"duration": 3.447
},
{
"text": "I don't really know what an\nirregular file would be yet.",
"start": 1524.707,
"duration": 2.333
},
{
"text": "But I do want to remove it.",
"start": 1527.04,
"duration": 1.62
},
{
"text": ">> So I'm going to type y for yes.",
"start": 1528.66,
"duration": 1.49
},
{
"text": "Or I could type it out, and hit Enter.",
"start": 1530.15,
"duration": 1.79
},
{
"text": "And, again, nothing seems to happen.",
"start": 1531.94,
"duration": 1.5
},
{
"text": "But that is, generally, a good thing.",
"start": 1533.44,
"duration": 2.4
},
{
"text": ">> If I type LS this time,\nwhat should I see?",
"start": 1535.84,
"duration": 4.65
},
{
"text": "Hopefully, just hello and hello.c.",
"start": 1540.49,
"duration": 4.44
},
{
"text": "Now, as an aside, you'll\nnotice this star, asterisk,",
"start": 1544.93,
"duration": 2.356
},
{
"text": "that's at the end of my programs.",
"start": 1547.286,
"duration": 1.374
},
{
"text": "And they're also showing up in green.",
"start": 1548.66,
"duration": 1.541
},
{
"text": "That is just CS50 IDE's way\nof cluing you into the fact",
"start": 1550.201,
"duration": 3.769
},
{
"text": "that that's not source code.",
"start": 1553.97,
"duration": 1.31
},
{
"text": "That's an executable, a runnable\nprogram that you can actually run",
"start": 1555.28,
"duration": 3.6
},
{
"text": "by doing dot slash, and then it's name.",
"start": 1558.88,
"duration": 2.14
},
{
"text": ">> Now, let me go ahead and remove\nthis, rm hello, Enter, remove regular",
"start": 1561.02,
"duration": 4.84
},
{
"text": "file hello, yes.",
"start": 1565.86,
"duration": 2.15
},
{
"text": "And now if I type LS,\nwe're back to hello.c.",
"start": 1568.01,
"duration": 3.17
},
{
"text": "Try not to delete your\nactual source code.",
"start": 1571.18,
"duration": 2.737
},
{
"text": "Even though there are features\nbuilt into CS50 IDE where",
"start": 1573.917,
"duration": 2.333
},
{
"text": "you can go through your revision history\nand rewind in time if you accidentally",
"start": 1576.25,
"duration": 3.62
},
{
"text": "delete something, do be mindful\nas per these prompts yes or no,",
"start": 1579.87,
"duration": 3.79
},
{
"text": "of what you actually want to do.",
"start": 1583.66,
"duration": 1.721
},
{
"text": "And if I go up to the top\nleft hand corner here,",
"start": 1585.381,
"duration": 1.999
},
{
"text": "all that remains is hello.c.",
"start": 1587.38,
"duration": 3.316
},
{
"text": "So there's bunches of\nother commands that you",
"start": 1590.696,
"duration": 1.874
},
{
"text": "can execute in the world of Linux,\none of which is, again, Make.",
"start": 1592.57,
"duration": 4.98
},
{
"text": "And we're going to Make\nmy program now as follows.",
"start": 1597.55,
"duration": 2.63
},
{
"text": ">> Instead of doing clang,\ninstead of doing clang-o,",
"start": 1600.18,
"duration": 3.09
},
{
"text": "I'm going to simply\nliterally type, make hello.",
"start": 1603.27,
"duration": 2.59
},
{
"text": "And now notice, I am\nnot typing make hello.c.",
"start": 1605.86,
"duration": 3.77
},
{
"text": "I am typing make hello.",
"start": 1609.63,
"duration": 1.28
},
{
"text": ">> And this program Make that\ncomes with CS50 IDE, and more",
"start": 1610.91,
"duration": 3.93
},
{
"text": "generally with Linux,\nis a program that's",
"start": 1614.84,
"duration": 2.25
},
{
"text": "going to make a program called Hello.",
"start": 1617.09,
"duration": 2.03
},
{
"text": "And it's going to assume, by convention,\nthat if this program can be made,",
"start": 1619.12,
"duration": 4.56
},
{
"text": "it's going to be made from a source\ncode file ending in dot c, hello.c.",
"start": 1623.68,
"duration": 5.35
},
{
"text": ">> So if I hit Enter now, notice that\nthe command that gets executed",
"start": 1629.03,
"duration": 3.18
},
{
"text": "is actually even longer\nbefore than before.",
"start": 1632.21,
"duration": 2.13
},
{
"text": "And that's because we've\npreconfigured CS50 IDE to have",
"start": 1634.34,
"duration": 2.33
},
{
"text": "some additional features built in that\nwe don't need just yet, but soon will.",
"start": 1636.67,
"duration": 3.208
},
{
"text": "But the key thing to realize\nis now I have a Hello program.",
"start": 1639.878,
"duration": 3.592
},
{
"text": ">> If I type LS again, I\nhave a hello program.",
"start": 1643.47,
"duration": 3.61
},
{
"text": "And I can run it with\ndot slash a.out, no,",
"start": 1647.08,
"duration": 4.99
},
{
"text": "because the whole point of this\nexercise was dot slash hello.",
"start": 1652.07,
"duration": 3.52
},
{
"text": "And now I have my hello world program.",
"start": 1655.59,
"duration": 2.499
},
{
"text": "So moving forward,\nwe're almost always just",
"start": 1658.089,
"duration": 1.791
},
{
"text": "going to compile our programs\nusing the command Make.",
"start": 1659.88,
"duration": 2.208
},
{
"text": "And then we're going to run them by\ndot slash, and the program's name.",
"start": 1662.088,
"duration": 3.212
},
{
"text": "But realize what Make is doing for\nyou, is it is itself not a compiler.",
"start": 1665.3,
"duration": 4.31
},
{
"text": "It's just a convenience program\nthat knows how to trigger a compiler",
"start": 1669.61,
"duration": 3.7
},
{
"text": "to run so that you yourself can use it.",
"start": 1673.31,
"duration": 3.16
},
{
"text": ">> What other commands exist in\nLinux, and in turn the CS50 IDE?",
"start": 1676.47,
"duration": 3.75
},
{
"text": "We'll soon see that there's a\nCD command, Change Directory.",
"start": 1680.22,
"duration": 2.887
},
{
"text": "This allows you within\nyour command line interface",
"start": 1683.107,
"duration": 2.083
},
{
"text": "to move forward, and back,\nand open up different folders",
"start": 1685.19,
"duration": 2.42
},
{
"text": "without using your mouse.",
"start": 1687.61,
"duration": 1.25
},
{
"text": ">> LS we saw, which stands for list\nthe files in the current directory.",
"start": 1688.86,
"duration": 3.61
},
{
"text": "Make Dir, you can\nprobably start to infer",
"start": 1692.47,
"duration": 2.18
},
{
"text": "what these mean now-- make directory,\nif you want to create a folder.",
"start": 1694.65,
"duration": 3.5
},
{
"text": "RM for remove, RM Dir for\nremove directory-- and these,",
"start": 1698.15,
"duration": 3.12
},
{
"text": "again, are the command line\nequivalents of what you",
"start": 1701.27,
"duration": 2.89
},
{
"text": "could do in CS50 IDE with your mouse.",
"start": 1704.16,
"duration": 2.785
},
{
"text": "But you'll soon find\nthat sometimes it's just",
"start": 1706.945,
"duration": 1.875
},
{
"text": "a lot faster to do\nthings with a keyboard,",
"start": 1708.82,
"duration": 1.79
},
{
"text": "and ultimately a lot more powerful.",
"start": 1710.61,
"duration": 3.08
},
{
"text": ">> But it's hard to argue that\nanything we've been doing so far",
"start": 1713.69,
"duration": 2.75
},
{
"text": "is all that powerful, when all\nwe've been saying is, hello world.",
"start": 1716.44,
"duration": 3.55
},
{
"text": "And, in fact, I hardcoded the\nwords hello world into my program.",
"start": 1719.99,
"duration": 3.75
},
{
"text": "There is no dynamism yet.",
"start": 1723.74,
"duration": 1.79
},
{
"text": "Scratch was an order of magnitude\nmore interesting last week.",
"start": 1725.53,
"duration": 3.79
},
{
"text": ">> And so let's get there.",
"start": 1729.32,
"duration": 1.9
},
{
"text": "Let's take a step toward that by\nway of some of these functions.",
"start": 1731.22,
"duration": 4.09
},
{
"text": "So not only does C come with printf,\nand bunches of other functions",
"start": 1735.31,
"duration": 4.16
},
{
"text": "some of which we'll see\nover time, it doesn't",
"start": 1739.47,
"duration": 2.38
},
{
"text": "make it all that easy right out\nof the gate in getting user input.",
"start": 1741.85,
"duration": 3.91
},
{
"text": ">> In fact, one of the weaknesses\nof languages like C,",
"start": 1745.76,
"duration": 2.38
},
{
"text": "and even Java and yet\nothers, is that it doesn't",
"start": 1748.14,
"duration": 2.0
},
{
"text": "make it easy to just get things like\nintegers from users, or strings, words,",
"start": 1750.14,
"duration": 5.72
},
{
"text": "and phrases, let alone things like\nfloating point values, or real numbers",
"start": 1755.86,
"duration": 4.11
},
{
"text": "with decimal points, and really\nlong numbers, as we'll soon see.",
"start": 1759.97,
"duration": 3.27
},
{
"text": "So this list of functions here, these\nare like other Scratch puzzle pieces",
"start": 1763.24,
"duration": 3.76
},
{
"text": "that we have pre-installed in CS50\nIDE that we'll use for a few weeks",
"start": 1767.0,
"duration": 4.09
},
{
"text": "as training wheels of sorts, and\neventually take them off, and look",
"start": 1771.09,
"duration": 2.92
},
{
"text": "underneath the hood, perhaps, at\nhow these things are implemented.",
"start": 1774.01,
"duration": 3.2
},
{
"text": ">> But to do this, let's\nactually write a program.",
"start": 1777.21,
"duration": 3.25
},
{
"text": "Let me go ahead now.",
"start": 1780.46,
"duration": 1.31
},
{
"text": "And I'm going to create a new\nfile by clicking this little plus,",
"start": 1781.77,
"duration": 2.98
},
{
"text": "and clicking New File.",
"start": 1784.75,
"duration": 1.22
},
{
"text": ">> I'm going to save this next\none as, let's say, string.c,",
"start": 1785.97,
"duration": 3.28
},
{
"text": "because I want to play with strings.",
"start": 1789.25,
"duration": 1.5
},
{
"text": "And string in C is just\na sequence of characters.",
"start": 1790.75,
"duration": 3.24
},
{
"text": "So now let's go ahead\nand do the following.",
"start": 1793.99,
"duration": 2.1
},
{
"text": ">> Include standard IO.h-- and\nit turns out standard IO,",
"start": 1796.09,
"duration": 5.114
},
{
"text": "IO just means input and output.",
"start": 1801.204,
"duration": 2.156
},
{
"text": "So it turns out that\nthis line here is what",
"start": 1803.36,
"duration": 2.56
},
{
"text": "is the neighboring us to use printf.",
"start": 1805.92,
"duration": 2.22
},
{
"text": "Printf, of course, produces output.",
"start": 1808.14,
"duration": 2.27
},
{
"text": "So in order to use printf, it turns\nout you have to have this line of code",
"start": 1810.41,
"duration": 4.59
},
{
"text": "at the top of your file.",
"start": 1815.0,
"duration": 1.04
},
{
"text": ">> And we'll come back to what\nthat really means before long.",
"start": 1816.04,
"duration": 2.416
},
{
"text": "It turns out that in\nany C program I write,",
"start": 1818.456,
"duration": 1.944
},
{
"text": "I've got to start it with\ncode that looks like this.",
"start": 1820.4,
"duration": 3.24
},
{
"text": "And you'll notice CS50 IDE, and\nother integrated development",
"start": 1823.64,
"duration": 3.22
},
{
"text": "environments like it,\nare going to try as best",
"start": 1826.86,
"duration": 3.19
},
{
"text": "they can to finish your thought.",
"start": 1830.05,
"duration": 1.73
},
{
"text": "In fact, a moment ago if I undo\nwhat I just did, I hit Enter.",
"start": 1831.78,
"duration": 4.15
},
{
"text": ">> I then hit open curly\nbrace, hit Enter again.",
"start": 1835.93,
"duration": 3.23
},
{
"text": "And it finished my thought.",
"start": 1839.16,
"duration": 1.27
},
{
"text": "It gave me a new line, indented no less\nfor nice stylistic reasons we'll see.",
"start": 1840.43,
"duration": 4.71
},
{
"text": "And then it automatically gave me\nthat curly brace to finish my thought.",
"start": 1845.14,
"duration": 3.419
},
{
"text": "Now, it doesn't always\nguess what you want to do.",
"start": 1848.559,
"duration": 2.041
},
{
"text": "But in large part, it does\nsave you some keystrokes.",
"start": 1850.6,
"duration": 3.02
},
{
"text": "So a moment ago, we ran this program--\nhello, world, and then compiled it,",
"start": 1853.62,
"duration": 5.94
},
{
"text": "and then ran it.",
"start": 1859.56,
"duration": 0.9
},
{
"text": "But there's no dynamism here.",
"start": 1860.46,
"duration": 1.407
},
{
"text": "What if we wanted to\ndo something different?",
"start": 1861.867,
"duration": 1.833
},
{
"text": "Well, what if I wanted to actually\nget a string from the user?",
"start": 1863.7,
"duration": 3.93
},
{
"text": "I'm going to use a puzzle piece\ncalled exactly that-- get string.",
"start": 1867.63,
"duration": 3.62
},
{
"text": ">> Turns out in C that when you don't want\nto provide input to a puzzle piece,",
"start": 1871.25,
"duration": 4.61
},
{
"text": "or more properly to a function, you\nliterally just do open parenthesis,",
"start": 1875.86,
"duration": 3.5
},
{
"text": "close parenthesis.",
"start": 1879.36,
"duration": 1.07
},
{
"text": "So it's as though there's\nno white box to type into.",
"start": 1880.43,
"duration": 5.11
},
{
"text": "The say block before\nhad a little white box.",
"start": 1885.54,
"duration": 2.18
},
{
"text": "We don't have that white box now.",
"start": 1887.72,
"duration": 1.94
},
{
"text": ">> But when I call get string, I\nwant to put the result somewhere.",
"start": 1889.66,
"duration": 3.65
},
{
"text": "So a very common paradigm in C is to\ncall a function, like get string here,",
"start": 1893.31,
"duration": 4.37
},
{
"text": "and then store its return value.",
"start": 1897.68,
"duration": 3.39
},
{
"text": "It's the result of its\neffort in something.",
"start": 1901.07,
"duration": 3.38
},
{
"text": ">> And what is the\nconstruct in programming,",
"start": 1904.45,
"duration": 3.18
},
{
"text": "whether in Scratch or now C, that we\ncan use to actually store something?",
"start": 1907.63,
"duration": 5.82
},
{
"text": "Called it a variable, right?",
"start": 1913.45,
"duration": 2.54
},
{
"text": "And in Scratch, we don't really\ncare what was going in variables.",
"start": 1915.99,
"duration": 4.33
},
{
"text": ">> But in this case, we actually do.",
"start": 1920.32,
"duration": 1.85
},
{
"text": "I'm going to say string.",
"start": 1922.17,
"duration": 1.549
},
{
"text": "And then I could call\nthis anything I want.",
"start": 1923.719,
"duration": 1.791
},
{
"text": "I'm going to call it\nname, gets get string.",
"start": 1925.51,
"duration": 2.83
},
{
"text": ">> And now even if you're\na little new to this,",
"start": 1928.34,
"duration": 1.91
},
{
"text": "notice that I'm lacking some detail.",
"start": 1930.25,
"duration": 1.734
},
{
"text": "I'm forgetting a semi-colon.",
"start": 1931.984,
"duration": 1.166
},
{
"text": "I need to finish this thought.",
"start": 1933.15,
"duration": 1.25
},
{
"text": "So I'm going to move my cursor,\nand hit semi-colon there.",
"start": 1934.4,
"duration": 3.08
},
{
"text": "And what have I just done?",
"start": 1937.48,
"duration": 1.65
},
{
"text": "In this line of code,\nnumber 5 at the moment,",
"start": 1939.13,
"duration": 2.31
},
{
"text": "I'm calling get string with no inputs.",
"start": 1941.44,
"duration": 2.359
},
{
"text": "So there's no little white\nbox like the Save block has.",
"start": 1943.799,
"duration": 2.291
},
{
"text": ">> I'm just saying, hey,\ncomputer, get me a string.",
"start": 1946.09,
"duration": 2.5
},
{
"text": "The equal sign is not really\nan equal sign, per se.",
"start": 1948.59,
"duration": 2.8
},
{
"text": "It's the assignment\noperator, which means,",
"start": 1951.39,
"duration": 2.4
},
{
"text": "hey, computer, move the value\nfrom the right over to the left.",
"start": 1953.79,
"duration": 4.07
},
{
"text": "And in the left, I have the following.",
"start": 1957.86,
"duration": 2.62
},
{
"text": ">> Hey, computer, give me a string--\na sequence of characters.",
"start": 1960.48,
"duration": 3.1
},
{
"text": "And call that string Name.",
"start": 1963.58,
"duration": 2.057
},
{
"text": "And I don't even have to call it Name.",
"start": 1965.637,
"duration": 1.583
},
{
"text": ">> I could call it, conventionally,\nsomething like S,",
"start": 1967.22,
"duration": 2.75
},
{
"text": "much like we used i to\ncall the variable i.",
"start": 1969.97,
"duration": 2.93
},
{
"text": "But now I need to do something with it.",
"start": 1972.9,
"duration": 1.929
},
{
"text": "It would be pretty stupid to\ntry compiling this code, running",
"start": 1974.829,
"duration": 2.541
},
{
"text": "this program, even though\nI'm getting a string,",
"start": 1977.37,
"duration": 2.04
},
{
"text": "because it's still just\ngoing to say hello world.",
"start": 1979.41,
"duration": 2.17
},
{
"text": ">> But what if I do want to change this.",
"start": 1981.58,
"duration": 4.56
},
{
"text": "Why don't I do this?",
"start": 1986.14,
"duration": 1.8
},
{
"text": "Percent s, comma s.",
"start": 1987.94,
"duration": 3.692
},
{
"text": "And this is a little cryptic still.",
"start": 1991.632,
"duration": 1.458
},
{
"text": ">> So let me make my variables more clear.",
"start": 1993.09,
"duration": 2.47
},
{
"text": "Let me name this variable Name.",
"start": 1995.56,
"duration": 1.95
},
{
"text": "And let's see if we can't tease\napart what's happening here.",
"start": 1997.51,
"duration": 2.72
},
{
"text": ">> So on line five, I'm getting a string.",
"start": 2000.23,
"duration": 2.54
},
{
"text": "And I'm storing that string,\nwhatever the user has typed in",
"start": 2002.77,
"duration": 2.85
},
{
"text": "at his or her keyboard,\nin a variable called Name.",
"start": 2005.62,
"duration": 2.81
},
{
"text": "And it turns out that\nprintf doesn't just",
"start": 2008.43,
"duration": 2.16
},
{
"text": "take one argument in double\nquotes, one input in double quotes.",
"start": 2010.59,
"duration": 3.63
},
{
"text": ">> It can take two, or three, or more, such\nthat the second, or third, or fourth,",
"start": 2014.22,
"duration": 4.88
},
{
"text": "are all the names of variables,\nor specifically values,",
"start": 2019.1,
"duration": 3.22
},
{
"text": "that you want to plug into,\ndynamically, that string in quotes.",
"start": 2022.32,
"duration": 6.29
},
{
"text": "In other words, what\nwould be wrong with this?",
"start": 2028.61,
"duration": 3.5
},
{
"text": "If I just said hello name, backslash\nn, saved my file, compiled my code,",
"start": 2032.11,
"duration": 5.81
},
{
"text": "and ran this, what would happen?",
"start": 2037.92,
"duration": 3.74
},
{
"text": ">> It's just going to say, hello\nname, literally N-A-M-E,",
"start": 2041.66,
"duration": 3.479
},
{
"text": "which is kind of stupid because\nit's no different from world.",
"start": 2045.139,
"duration": 2.761
},
{
"text": "So anything in quotes is\nwhat literally gets printed.",
"start": 2047.9,
"duration": 2.5
},
{
"text": "So if I want to have\na placeholder there,",
"start": 2050.4,
"duration": 2.12
},
{
"text": "I actually need to use\nsome special syntax.",
"start": 2052.52,
"duration": 1.902
},
{
"text": "And it turns out if you read the\ndocumentation for the printf function,",
"start": 2054.422,
"duration": 2.958
},
{
"text": "it will tell you that\nif you use percent s,",
"start": 2057.38,
"duration": 3.94
},
{
"text": "you can substitute a value as follows.",
"start": 2061.32,
"duration": 2.6
},
{
"text": ">> After a comma after that\ndouble quote, you simply",
"start": 2063.92,
"duration": 3.27
},
{
"text": "write the name of the\nvariable that you want",
"start": 2067.19,
"duration": 1.989
},
{
"text": "to plug in into that format\ncode, or format specifier,",
"start": 2069.179,
"duration": 4.611
},
{
"text": "percent s for strings.",
"start": 2073.79,
"duration": 1.679
},
{
"text": "And now if I've saved my file,\nI go back down to my terminal.",
"start": 2075.469,
"duration": 3.721
},
{
"text": "And I type Make String,\nbecause, again, the name of this",
"start": 2079.19,
"duration": 3.68
},
{
"text": "file that I chose before is string.c.",
"start": 2082.87,
"duration": 2.64
},
{
"text": ">> So I'm going to say Make String, enter.",
"start": 2085.51,
"duration": 3.0
},
{
"text": "Oh my goodness, look at all of\nthe mistakes we've made already.",
"start": 2088.51,
"duration": 3.04
},
{
"text": "And this is-- what, this is really\nlike a six, seven line program?",
"start": 2091.55,
"duration": 3.99
},
{
"text": "So this is where it can very\nquickly get overwhelming.",
"start": 2095.54,
"duration": 2.25
},
{
"text": ">> This terminal window has\nnow just regurgitated",
"start": 2097.79,
"duration": 3.1
},
{
"text": "a huge number of error messages.",
"start": 2100.89,
"duration": 2.34
},
{
"text": "Surely, I don't have more error\nmessages than I have lines of code.",
"start": 2103.23,
"duration": 4.33
},
{
"text": "So what is going on?",
"start": 2107.56,
"duration": 1.12
},
{
"text": ">> Well, the best strategy\nto do anytime you",
"start": 2108.68,
"duration": 2.24
},
{
"text": "do encounter an overwhelming\nlist of errors like that,",
"start": 2110.92,
"duration": 2.79
},
{
"text": "is scroll back, look for the command\nyou just ran, which in my case",
"start": 2113.71,
"duration": 2.98
},
{
"text": "is make string.",
"start": 2116.69,
"duration": 1.33
},
{
"text": "Look at what make did, and that's that\nlong Clang command, no big deal there.",
"start": 2118.02,
"duration": 3.61
},
{
"text": ">> But the red is bad.",
"start": 2121.63,
"duration": 1.32
},
{
"text": "Green is trying to be\ngentle and helpful.",
"start": 2122.95,
"duration": 1.8
},
{
"text": "But it's still bad, in this case.",
"start": 2124.75,
"duration": 1.39
},
{
"text": "But where is it bad?",
"start": 2126.14,
"duration": 1.37
},
{
"text": ">> String.c, line five, character five.",
"start": 2127.51,
"duration": 3.94
},
{
"text": "So this is just common convention.",
"start": 2131.45,
"duration": 1.48
},
{
"text": "Something colon something means\nline number and character number.",
"start": 2132.93,
"duration": 3.13
},
{
"text": "Error, use of undeclared\nidentifier string.",
"start": 2136.06,
"duration": 5.02
},
{
"text": "Did you mean standard in?",
"start": 2141.08,
"duration": 1.82
},
{
"text": ">> So, unfortunately, Clang\nis trying to be helpful.",
"start": 2142.9,
"duration": 2.63
},
{
"text": "But it's wrong, in this case.",
"start": 2145.53,
"duration": 1.32
},
{
"text": "No, Clang, I did not mean standard IO.",
"start": 2146.85,
"duration": 2.5
},
{
"text": "I meant that on line one, yes.",
"start": 2149.35,
"duration": 1.72
},
{
"text": ">> But line five is this one here.",
"start": 2151.07,
"duration": 2.35
},
{
"text": "And Clang does not\nunderstand S-T-R-I-N-G.",
"start": 2153.42,
"duration": 3.62
},
{
"text": "It's an undeclared identifier, a\nword it just has never seen before.",
"start": 2157.04,
"duration": 4.45
},
{
"text": "And that's because C, the language\nwe're writing code in right now,",
"start": 2161.49,
"duration": 4.24
},
{
"text": "does not have variables called strings.",
"start": 2165.73,
"duration": 2.34
},
{
"text": ">> It doesn't, by default, support\nsomething called a string.",
"start": 2168.07,
"duration": 3.31
},
{
"text": "That's a CS50 piece of\njargon, but very conventional.",
"start": 2171.38,
"duration": 5.37
},
{
"text": "But I can fix this as follows.",
"start": 2176.75,
"duration": 1.85
},
{
"text": ">> If I add one line of code\nto the top of this program,",
"start": 2178.6,
"duration": 3.49
},
{
"text": "include CS50.h, which is another file\nsomewhere inside of CS50 IDE, somewhere",
"start": 2182.09,
"duration": 5.8
},
{
"text": "on the hard drive, so to speak,\nof the Ubuntu operating system",
"start": 2187.89,
"duration": 2.93
},
{
"text": "that I'm running, that\nis the file that's",
"start": 2190.82,
"duration": 2.77
},
{
"text": "going to teach the operating\nsystem what a string is, just",
"start": 2193.59,
"duration": 5.15
},
{
"text": "like standard io.h is the file\nin the operating system that's",
"start": 2198.74,
"duration": 3.19
},
{
"text": "going to teach it what printf is.",
"start": 2201.93,
"duration": 2.5
},
{
"text": ">> Indeed, we would have gotten\na very similar message",
"start": 2204.43,
"duration": 2.38
},
{
"text": "if IO had admitted standard\nIO.h and tried to use printf.",
"start": 2206.81,
"duration": 3.79
},
{
"text": "So I'm going to go ahead and just\ntake Control L to clear my screen.",
"start": 2210.6,
"duration": 3.032
},
{
"text": "Or you can type clear and it will\njust clear the terminal window.",
"start": 2213.632,
"duration": 2.708
},
{
"text": "But you can still scroll back in time.",
"start": 2216.34,
"duration": 1.68
},
{
"text": ">> And I'm going to rerun Make String.",
"start": 2218.02,
"duration": 3.08
},
{
"text": "Cross my fingers this time, Enter.",
"start": 2221.1,
"duration": 2.56
},
{
"text": "Oh my God, it worked.",
"start": 2223.66,
"duration": 1.72
},
{
"text": "it shows me a long cryptic command\nthat is what Make generated via Clang,",
"start": 2225.38,
"duration": 3.9
},
{
"text": "but no error messages.",
"start": 2229.28,
"duration": 1.18
},
{
"text": "So realize, even though\nyou might get completely",
"start": 2230.46,
"duration": 2.0
},
{
"text": "overwhelmed with the\nnumber of error messages,",
"start": 2232.46,
"duration": 2.02
},
{
"text": "it just might be this annoying cascading\neffect, where Clang doesn't understand",
"start": 2234.48,
"duration": 3.06
},
{
"text": "one thing, which means it then\ndoesn't understand the next word,",
"start": 2237.54,
"duration": 2.08
},
{
"text": "or the next line.",
"start": 2239.62,
"duration": 0.94
},
{
"text": "And so it just chokes on your code.",
"start": 2240.56,
"duration": 2.29
},
{
"text": "But the fix might be simple.",
"start": 2242.85,
"duration": 1.59
},
{
"text": "And so always focus on the\nvery first line of output.",
"start": 2244.44,
"duration": 3.382
},
{
"text": "And if you don't\nunderstand it, just look",
"start": 2247.822,
"duration": 1.708
},
{
"text": "for keywords that might be\nclues, and the line number,",
"start": 2249.53,
"duration": 2.95
},
{
"text": "and the character, where\nthat mistake might be.",
"start": 2252.48,
"duration": 2.17
},
{
"text": ">> Now let me go ahead and type\ndot slash, string, enter.",
"start": 2254.65,
"duration": 5.678
},
{
"text": "Hm, it's not saying hello anything.",
"start": 2260.328,
"duration": 4.012
},
{
"text": "Why?",
"start": 2264.34,
"duration": 1.87
},
{
"text": "Well, recall, where is it running?",
"start": 2266.21,
"duration": 1.96
},
{
"text": ">> It's probably stuck at the moment\nin a loop, if you will, on line six,",
"start": 2268.17,
"duration": 5.56
},
{
"text": "because Get String by design,\nwritten by CS50 staff,",
"start": 2273.73,
"duration": 3.22
},
{
"text": "is literally meant to just sit\nthere waiting, and waiting,",
"start": 2276.95,
"duration": 3.4
},
{
"text": "and waiting for a string.",
"start": 2280.35,
"duration": 1.5
},
{
"text": "All we mean by string is human input.",
"start": 2281.85,
"duration": 1.942
},
{
"text": "So you know what?",
"start": 2283.792,
"duration": 0.708
},
{
"text": "Let me go ahead.",
"start": 2284.5,
"duration": 0.666
},
{
"text": "And just on a whim, let me\ntype my name, David, enter.",
"start": 2285.166,
"duration": 3.538
},
{
"text": "Now I have a more dynamic program.",
"start": 2288.704,
"duration": 1.416
},
{
"text": "It said, hello David.",
"start": 2290.12,
"duration": 1.12
},
{
"text": ">> If I go ahead and run this again,\nlet me try say Zamila name, enter.",
"start": 2291.24,
"duration": 5.04
},
{
"text": "And now we have a dynamic program.",
"start": 2296.28,
"duration": 1.66
},
{
"text": "I haven't hard coded world.",
"start": 2297.94,
"duration": 1.44
},
{
"text": "I haven't hard coded\nname, or David, or Zamila.",
"start": 2299.38,
"duration": 2.38
},
{
"text": ">> Now it's much more like the programs\nwe know, where if it take input,",
"start": 2301.76,
"duration": 3.59
},
{
"text": "it produces slightly different output.",
"start": 2305.35,
"duration": 2.52
},
{
"text": "Now, this is not the best\nuser experience, or UX.",
"start": 2307.87,
"duration": 3.15
},
{
"text": "I run the program.",
"start": 2311.02,
"duration": 1.98
},
{
"text": ">> I don't know what I'm supposed\nto do, unless I actually look at",
"start": 2313.0,
"duration": 2.83
},
{
"text": "or remember the source code.",
"start": 2315.83,
"duration": 1.46
},
{
"text": "So let's make the user\nexperience a little better",
"start": 2317.29,
"duration": 2.35
},
{
"text": "with the simplest of things.",
"start": 2319.64,
"duration": 1.6
},
{
"text": "Let me go back into this\nprogram, and simply say printf.",
"start": 2321.24,
"duration": 3.542
},
{
"text": ">> And let me go ahead and say name, colon,\nand a space, and then a semi-colon.",
"start": 2324.782,
"duration": 4.088
},
{
"text": "And just for kicks, no backlash n.",
"start": 2328.87,
"duration": 2.3
},
{
"text": "And that's deliberate,\nbecause I don't want",
"start": 2331.17,
"duration": 1.81
},
{
"text": "the prompt to move to the next line.",
"start": 2332.98,
"duration": 1.61
},
{
"text": ">> I want to, instead, do this, make string\nto recompile my code into new machine",
"start": 2334.59,
"duration": 4.21
},
{
"text": "code dot slash string.",
"start": 2338.8,
"duration": 2.18
},
{
"text": "Ah, this is much prettier.",
"start": 2340.98,
"duration": 1.48
},
{
"text": "Now I actually know what the computer\nwants me to do, give it a name.",
"start": 2342.46,
"duration": 3.32
},
{
"text": ">> So I'm going to go ahead and type\nin Rob, enter, and hello, Rob.",
"start": 2345.78,
"duration": 4.24
},
{
"text": "So, realize, this is still, at the end\nof the day, only a nine line program.",
"start": 2350.02,
"duration": 3.62
},
{
"text": "But we've taken these baby steps.",
"start": 2353.64,
"duration": 1.45
},
{
"text": ">> We wrote one line with which we\nwere familiar, printf, hello world.",
"start": 2355.09,
"duration": 3.29
},
{
"text": "Then we undid a little bit of that.",
"start": 2358.38,
"duration": 1.6
},
{
"text": "And we actually used get string.",
"start": 2359.98,
"duration": 1.58
},
{
"text": "And we tossed that value in a variable.",
"start": 2361.56,
"duration": 1.802
},
{
"text": "And then we went ahead and improved\nit further with a third line.",
"start": 2363.362,
"duration": 2.708
},
{
"text": "And this iterative process of\nwriting software is truly key.",
"start": 2366.07,
"duration": 3.15
},
{
"text": "In CS50, and in life in general,\nyou should generally not sit down,",
"start": 2369.22,
"duration": 4.2
},
{
"text": "have a program in mind, and try writing\nthe whole damn thing all at once.",
"start": 2373.42,
"duration": 3.38
},
{
"text": ">> It will, inevitably, result in way\nmore errors than we ourselves saw here.",
"start": 2376.8,
"duration": 4.01
},
{
"text": "Even I, to this day, constantly\nmake other stupid mistakes,",
"start": 2380.81,
"duration": 3.26
},
{
"text": "are actually harder mistakes\nthat are harder to figure out.",
"start": 2384.07,
"duration": 3.41
},
{
"text": "But you will make more mistakes the more\nlines of code you write all at once.",
"start": 2387.48,
"duration": 4.615
},
{
"text": "And so this practice of,\nwrite a little bit of code",
"start": 2392.095,
"duration": 2.125
},
{
"text": "that you're comfortable with, compile\nit, run it, test it more generally,",
"start": 2394.22,
"duration": 3.71
},
{
"text": "then move on-- so just like we kept\nlayering and layering last week,",
"start": 2397.93,
"duration": 3.44
},
{
"text": "building from something very\nsimple to something more complex,",
"start": 2401.37,
"duration": 2.82
},
{
"text": "do the same here.",
"start": 2404.19,
"duration": 1.01
},
{
"text": "Don't sit down, and try to\nwrite an entire problem.",
"start": 2405.2,
"duration": 3.3
},
{
"text": "Actually take these baby steps.",
"start": 2408.5,
"duration": 2.28
},
{
"text": ">> Now, strings aren't all\nthat useful unto themselves.",
"start": 2410.78,
"duration": 4.32
},
{
"text": "We'd actually, ideally, like to\nhave something else in our toolkit.",
"start": 2415.1,
"duration": 3.11
},
{
"text": "So let's actually do exactly that.",
"start": 2418.21,
"duration": 2.78
},
{
"text": ">> Let me go ahead now and whip up\na slightly different program.",
"start": 2420.99,
"duration": 3.91
},
{
"text": "And we'll call this int.c, for integer.",
"start": 2424.9,
"duration": 3.42
},
{
"text": "I'm going to, similarly,\ninclude CS550.h.",
"start": 2428.32,
"duration": 2.55
},
{
"text": "I'm going to include standard IO.",
"start": 2430.87,
"duration": 2.19
},
{
"text": "And that's going to be pretty common\nin these first few days of the class.",
"start": 2433.06,
"duration": 3.57
},
{
"text": ">> And I'm going to ready\nmyself with a main function.",
"start": 2436.63,
"duration": 2.42
},
{
"text": "And now instead of getting a string,\nlet's go ahead and get an int.",
"start": 2439.05,
"duration": 4.32
},
{
"text": "Let's call it i, and call it get\nint, close parens, semi-colon.",
"start": 2443.37,
"duration": 5.915
},
{
"text": "And now let's do\nsomething with it, printf.",
"start": 2449.285,
"duration": 2.125
},
{
"text": ">> Let's say something like\nhello, backslash n, comma i.",
"start": 2451.41,
"duration": 4.78
},
{
"text": "So I'm pretty much mimicking\nwhat I did just a moment ago.",
"start": 2456.19,
"duration": 3.82
},
{
"text": "I have a placeholder here.",
"start": 2460.01,
"duration": 1.65
},
{
"text": "I have comma i here, because I want\nto plug i into that placeholder.",
"start": 2461.66,
"duration": 3.49
},
{
"text": ">> So let's go ahead and try\ncompiling this program.",
"start": 2465.15,
"duration": 2.1
},
{
"text": "The file is called int.c.",
"start": 2467.25,
"duration": 2.81
},
{
"text": "So I'm going to say, make int, enter.",
"start": 2470.06,
"duration": 2.86
},
{
"text": "Oh my God, but no big deal, right?",
"start": 2472.92,
"duration": 3.5
},
{
"text": "There's a mistake.",
"start": 2476.42,
"duration": 0.81
},
{
"text": ">> There's a syntactic mistake\nhere such that the program can't",
"start": 2477.23,
"duration": 2.58
},
{
"text": "be compiled inside int.c, line\nseven, character 27, error format",
"start": 2479.81,
"duration": 5.65
},
{
"text": "specifies type char\nstar, whatever that is.",
"start": 2485.46,
"duration": 2.94
},
{
"text": "But the argument type is int.",
"start": 2488.4,
"duration": 1.62
},
{
"text": ">> So here, too, we're not going to--\neven though today is a lot of material,",
"start": 2490.02,
"duration": 3.09
},
{
"text": "we're going to overwhelm you with\nabsolutely every feature of C,",
"start": 2493.11,
"duration": 2.6
},
{
"text": "and programming more generally,\nin just these first few weeks.",
"start": 2495.71,
"duration": 2.36
},
{
"text": "So there's often going to be jargon\nwith which you're not familiar.",
"start": 2498.07,
"duration": 2.33
},
{
"text": "And, in fact, char star is something\nwe're going to come back to",
"start": 2500.4,
"duration": 2.95
},
{
"text": "in a week or two's time.",
"start": 2503.35,
"duration": 1.48
},
{
"text": ">> But for now, let's see if we can\nparse words that are familiar.",
"start": 2504.83,
"duration": 2.7
},
{
"text": "Formats-- so we heard format\nspecifier, format code before.",
"start": 2507.53,
"duration": 3.22
},
{
"text": "That's familiar.",
"start": 2510.75,
"duration": 1.09
},
{
"text": "Type-- but the argument has type int.",
"start": 2511.84,
"duration": 2.0
},
{
"text": "Wait a minute, i is an int.",
"start": 2513.84,
"duration": 2.14
},
{
"text": ">> Maybe percent s actually\nhas some defined meaning.",
"start": 2515.98,
"duration": 3.25
},
{
"text": "And, indeed, it does.",
"start": 2519.23,
"duration": 1.0
},
{
"text": "An integer, if you want\nprintf to substitute it,",
"start": 2520.23,
"duration": 2.871
},
{
"text": "you actually have to use a\ndifferent format specifier.",
"start": 2523.101,
"duration": 2.249
},
{
"text": "And you wouldn't know this\nunless someone told you,",
"start": 2525.35,
"duration": 1.54
},
{
"text": "or you had done it before.",
"start": 2526.89,
"duration": 1.083
},
{
"text": "But percent i is what\ncan be commonly used",
"start": 2527.973,
"duration": 2.517
},
{
"text": "in printf for plugging in an integer.",
"start": 2530.49,
"duration": 1.75
},
{
"text": "You can also use percent\nd for a decimal integer.",
"start": 2532.24,
"duration": 2.68
},
{
"text": "But i is nice and simple here.",
"start": 2534.92,
"duration": 1.57
},
{
"text": "So we'll go with that.",
"start": 2536.49,
"duration": 1.1
},
{
"text": ">> Now let me go ahead and\nrerun make int, Enter.",
"start": 2537.59,
"duration": 3.57
},
{
"text": "That's good, no errors.",
"start": 2541.16,
"duration": 2.168
},
{
"text": "Dot slash int-- OK, bad user experience,\nbecause I haven't told myself",
"start": 2543.328,
"duration": 3.932
},
{
"text": "what to do.",
"start": 2547.26,
"duration": 0.5
},
{
"text": "But that's fine.",
"start": 2547.76,
"duration": 0.666
},
{
"text": "I'm catching on quickly.",
"start": 2548.426,
"duration": 1.054
},
{
"text": ">> And now let me go ahead and\ntype in David, OK, Zamila, Rob.",
"start": 2549.48,
"duration": 6.78
},
{
"text": "OK, so this is a good thing.",
"start": 2556.26,
"duration": 1.56
},
{
"text": "This time, I'm using a function,\na puzzle piece, called get int.",
"start": 2557.82,
"duration": 3.89
},
{
"text": "And it turns out-- and we'll\nsee this later in the term--",
"start": 2561.71,
"duration": 2.52
},
{
"text": "the CS50 staff has implemented\nget string in such a way",
"start": 2564.23,
"duration": 3.5
},
{
"text": "that it will only physically\nget a string for you.",
"start": 2567.73,
"duration": 2.62
},
{
"text": ">> It has implemented get int in\nsuch a way that it will only",
"start": 2570.35,
"duration": 3.99
},
{
"text": "get an integer for you.",
"start": 2574.34,
"duration": 1.25
},
{
"text": "And if you, the human,\ndon't cooperate, it's",
"start": 2575.59,
"duration": 2.24
},
{
"text": "literally just going to\nsay retry, retry, retry,",
"start": 2577.83,
"duration": 2.76
},
{
"text": "literally sitting there looping, until\nyou oblige with some magical number,",
"start": 2580.59,
"duration": 4.61
},
{
"text": "like 50, and hello 50.",
"start": 2585.2,
"duration": 2.47
},
{
"text": ">> Or if we run this again\nand type in 42, hello 42.",
"start": 2587.67,
"duration": 3.77
},
{
"text": "And so the get int function\ninside of that puzzle piece",
"start": 2591.44,
"duration": 4.31
},
{
"text": "is enough logic, enough thought,\nto figure out, what is a word?",
"start": 2595.75,
"duration": 3.3
},
{
"text": "And what is a number?",
"start": 2599.05,
"duration": 1.28
},
{
"text": "Only accepting, ultimately, numbers.",
"start": 2600.33,
"duration": 2.835
},
{
"text": ">> So it turns out that this\nisn't all that expressive.",
"start": 2605.69,
"duration": 4.54
},
{
"text": "so far.",
"start": 2610.23,
"duration": 0.68
},
{
"text": "So, yay, last time we\nwent pretty quickly",
"start": 2610.91,
"duration": 2.78
},
{
"text": "into implementing games, and animation,\nand artistic works in Scratch.",
"start": 2613.69,
"duration": 4.63
},
{
"text": "And here, we are being content\nwith hello world, and hello 50.",
"start": 2618.32,
"duration": 3.94
},
{
"text": ">> It's not all that inspiring.",
"start": 2622.26,
"duration": 1.436
},
{
"text": "And, indeed, these first few\nexamples will take some time",
"start": 2623.696,
"duration": 2.374
},
{
"text": "to ramp up in excitement.",
"start": 2626.07,
"duration": 1.44
},
{
"text": "But we have so much more\ncontrol now, in fact.",
"start": 2627.51,
"duration": 2.344
},
{
"text": "And we're going to very\nquickly start layering",
"start": 2629.854,
"duration": 1.916
},
{
"text": "on top of these basic primitives.",
"start": 2631.77,
"duration": 2.1
},
{
"text": ">> But first, let's understand\nwhat the limitations are.",
"start": 2633.87,
"duration": 2.5
},
{
"text": "In fact, one of the things\nScratch doesn't easily",
"start": 2636.37,
"duration": 2.25
},
{
"text": "let us do is really look\nunderneath the hood,",
"start": 2638.62,
"duration": 2.37
},
{
"text": "and understand what a\ncomputer is, what it can do,",
"start": 2640.99,
"duration": 2.75
},
{
"text": "and what its limitations are.",
"start": 2643.74,
"duration": 1.51
},
{
"text": "And, indeed, that lack of\nunderstanding, potentially, long-term",
"start": 2645.25,
"duration": 3.33
},
{
"text": "can lead to our own mistakes-- writing\nbugs, writing insecure software that",
"start": 2648.58,
"duration": 3.94
},
{
"text": "gets hacked in some way.",
"start": 2652.52,
"duration": 1.36
},
{
"text": ">> So let's take some steps toward\nunderstanding this a little better by",
"start": 2653.88,
"duration": 3.25
},
{
"text": "way of, say, the following example.",
"start": 2657.13,
"duration": 2.58
},
{
"text": "I'm going to go ahead and implement\nreal quick a program called Adder.",
"start": 2659.71,
"duration": 3.84
},
{
"text": "Like, let's add some numbers together.",
"start": 2663.55,
"duration": 1.584
},
{
"text": "And I'm going to code some corners\nhere, and just copy and paste",
"start": 2665.134,
"duration": 2.666
},
{
"text": "where I was before, just\nso we can get going sooner.",
"start": 2667.8,
"duration": 2.47
},
{
"text": "So now I've got the basic beginnings\nof a program called Adder.",
"start": 2670.27,
"duration": 2.82
},
{
"text": ">> And let's go ahead and do this.",
"start": 2673.09,
"duration": 1.58
},
{
"text": "I'm going to go ahead and\nsay, intx gets get int.",
"start": 2674.67,
"duration": 4.01
},
{
"text": "And you know what?",
"start": 2678.68,
"duration": 0.75
},
{
"text": "Let's make a better user experience.",
"start": 2679.43,
"duration": 1.56
},
{
"text": ">> So let's just say x is, and effectively\nprompt the user to give us x.",
"start": 2680.99,
"duration": 4.75
},
{
"text": "And then let me go ahead and say, printf\nhow about y is, this time expecting",
"start": 2685.74,
"duration": 4.86
},
{
"text": "two values from the user.",
"start": 2690.6,
"duration": 2.54
},
{
"text": "And then let's just go ahead and\nsay, printf, the sum of x and y is.",
"start": 2693.14,
"duration": 6.619
},
{
"text": "And now I don't want to do percent s.",
"start": 2699.759,
"duration": 1.541
},
{
"text": "I want to do percent i, backslash\nn, and then plug in sum value.",
"start": 2701.3,
"duration": 7.78
},
{
"text": ">> So how can I go about doing this?",
"start": 2709.08,
"duration": 1.54
},
{
"text": "You know what?",
"start": 2710.62,
"duration": 0.65
},
{
"text": "I know how to use variables.",
"start": 2711.27,
"duration": 1.57
},
{
"text": "Let me just declare a new one, int z.",
"start": 2712.84,
"duration": 2.3
},
{
"text": ">> And I'm going to take a guess here.",
"start": 2715.14,
"duration": 1.63
},
{
"text": "If there are equal signs in this\nlanguage, maybe I can just do x plus y,",
"start": 2716.77,
"duration": 4.7
},
{
"text": "so long as I end my\nthought with a semi-colon?",
"start": 2721.47,
"duration": 2.19
},
{
"text": "Now I can go back down here, plug in z,\nfinish this thought with a semi-colon.",
"start": 2723.66,
"duration": 4.51
},
{
"text": "And let's see now, if these\nsequences of lines-- x is get int.",
"start": 2728.17,
"duration": 4.99
},
{
"text": "Y is get int.",
"start": 2733.16,
"duration": 1.61
},
{
"text": ">> Add x and y, store the value in z--\nso, again, remember the equal sign",
"start": 2734.77,
"duration": 3.21
},
{
"text": "is not equal.",
"start": 2737.98,
"duration": 0.58
},
{
"text": "It's assignment from right to left.",
"start": 2738.56,
"duration": 2.54
},
{
"text": "And let's print out that the sum\nof x and y is not literally z,",
"start": 2741.1,
"duration": 4.08
},
{
"text": "but what's inside of z.",
"start": 2745.18,
"duration": 1.65
},
{
"text": "So let's make Adder --\nnice, no mistakes this time.",
"start": 2746.83,
"duration": 3.26
},
{
"text": "Dot slash Adder, enter,\nx is going to be 1.",
"start": 2750.09,
"duration": 2.94
},
{
"text": ">> Y is going to be 2.",
"start": 2753.03,
"duration": 2.35
},
{
"text": "And the sum of x and y is 3.",
"start": 2755.38,
"duration": 3.584
},
{
"text": "So that's all fine and good.",
"start": 2758.964,
"duration": 1.166
},
{
"text": ">> So you would imagine that math\nshould work in a program like this.",
"start": 2760.13,
"duration": 3.13
},
{
"text": "But you know what?",
"start": 2763.26,
"duration": 0.78
},
{
"text": "Is this variable, line\n12, even necessary?",
"start": 2764.04,
"duration": 2.864
},
{
"text": "You don't need to get in the habit\nof just storing things in variables",
"start": 2766.904,
"duration": 2.916
},
{
"text": "just because you can.",
"start": 2769.82,
"duration": 1.16
},
{
"text": "And, in fact, it's generally\nconsidered bad design",
"start": 2770.98,
"duration": 2.57
},
{
"text": "if you are creating a variable, called\nz in this case, storing something in it,",
"start": 2773.55,
"duration": 4.55
},
{
"text": "and then immediately\nusing it, but never again.",
"start": 2778.1,
"duration": 3.29
},
{
"text": "Why give something a name\nlike z if you're literally",
"start": 2781.39,
"duration": 3.31
},
{
"text": "going to use that\nthing only once, and so",
"start": 2784.7,
"duration": 2.07
},
{
"text": "proximal to where you created\nit in the first place,",
"start": 2786.77,
"duration": 2.61
},
{
"text": "so close in terms of lines of code?",
"start": 2789.38,
"duration": 1.672
},
{
"text": "So you know what?",
"start": 2791.052,
"duration": 0.708
},
{
"text": "It turns out that C is pretty flexible.",
"start": 2791.76,
"duration": 2.72
},
{
"text": "If I actually want to\nplug-in values here,",
"start": 2794.48,
"duration": 2.106
},
{
"text": "I don't need to declare a new variable.",
"start": 2796.586,
"duration": 1.624
},
{
"text": "I could just plug-in x plus\ny, because C understands",
"start": 2798.21,
"duration": 3.47
},
{
"text": "arithmetic, and mathematical operators.",
"start": 2801.68,
"duration": 1.71
},
{
"text": ">> So I can simply say, do this math,\nx plus y, whatever those values are,",
"start": 2803.39,
"duration": 3.75
},
{
"text": "plug the resulting\ninteger into that string.",
"start": 2807.14,
"duration": 3.64
},
{
"text": "So this might be, though\nonly one line shorter,",
"start": 2810.78,
"duration": 2.95
},
{
"text": "a better design, a better program,\nbecause there's less code, therefore",
"start": 2813.73,
"duration": 4.75
},
{
"text": "less for me to understand.",
"start": 2818.48,
"duration": 1.441
},
{
"text": "And it's also just cleaner,\ninsofar as we're not",
"start": 2819.921,
"duration": 1.999
},
{
"text": "introducing new words,\nnew symbols, like z,",
"start": 2821.92,
"duration": 2.7
},
{
"text": "even though they don't really\nserve much of a purpose.",
"start": 2824.62,
"duration": 2.89
},
{
"text": ">> Unfortunately, math isn't\nall that reliable sometimes.",
"start": 2827.51,
"duration": 5.38
},
{
"text": "Let's go ahead and do this.",
"start": 2832.89,
"duration": 2.38
},
{
"text": "I'm going to go ahead\nnow and do the following.",
"start": 2835.27,
"duration": 2.93
},
{
"text": ">> Let's do printf, percent i, plus percent\ni, shall be percent i, backslash n.",
"start": 2838.2,
"duration": 9.45
},
{
"text": "And I'm going to do this-- xyx plus y.",
"start": 2847.65,
"duration": 4.59
},
{
"text": "So I'm just going to rewrite\nthis slightly differently here.",
"start": 2852.24,
"duration": 2.581
},
{
"text": "Let me just do a quick sanity check.",
"start": 2854.821,
"duration": 1.499
},
{
"text": "Again, let's not get ahead of ourselves.",
"start": 2856.32,
"duration": 1.666
},
{
"text": "Make adder, dot slash adder.",
"start": 2857.986,
"duration": 3.434
},
{
"text": "x is 1, y is 2, 1 plus 2 is 3.",
"start": 2861.42,
"duration": 3.53
},
{
"text": "So that's good.",
"start": 2864.95,
"duration": 0.92
},
{
"text": "But let's complicate this now\na bit, and create a new file.",
"start": 2865.87,
"duration": 3.19
},
{
"text": ">> I'm going to call this one,\nsay, ints, plural for integers.",
"start": 2869.06,
"duration": 4.29
},
{
"text": "Let me start where I was a moment ago.",
"start": 2873.35,
"duration": 2.63
},
{
"text": "But now let's do a few other lines.",
"start": 2875.98,
"duration": 1.79
},
{
"text": "Let me go ahead and do the following,\nprintf, percent i, minus percent i,",
"start": 2877.77,
"duration": 5.66
},
{
"text": "is percent i, comma x, comma yx minus y.",
"start": 2883.43,
"duration": 5.529
},
{
"text": "So I'm doing slightly\ndifferent math there.",
"start": 2888.959,
"duration": 1.791
},
{
"text": "Let's do another one.",
"start": 2890.75,
"duration": 0.874
},
{
"text": "So percent i times percent\ni is percent i, backslash n.",
"start": 2891.624,
"duration": 4.986
},
{
"text": "Let's plug-in x, and y, and x times y.",
"start": 2896.61,
"duration": 4.82
},
{
"text": "We'll use the asterisk on\nyour computer for times.",
"start": 2901.43,
"duration": 3.1
},
{
"text": ">> You don't use x. x is\na variable name here.",
"start": 2904.53,
"duration": 1.86
},
{
"text": "You use the star for multiplication.",
"start": 2906.39,
"duration": 1.88
},
{
"text": "Let's do one more.",
"start": 2908.27,
"duration": 0.75
},
{
"text": "Printf percent I, divided\nby percent i, is percent i,",
"start": 2909.02,
"duration": 5.56
},
{
"text": "backslash n. xy divided by y--\nso you use the forward slash in C",
"start": 2914.58,
"duration": 5.88
},
{
"text": "to do division.",
"start": 2920.46,
"duration": 1.042
},
{
"text": "And let's do one other.",
"start": 2921.502,
"duration": 0.958
},
{
"text": "Remainder of percent i, divided\nby percent i, is percent i.",
"start": 2927.92,
"duration": 7.32
},
{
"text": "xy-- and now remainder\nis what's left over.",
"start": 2935.24,
"duration": 4.31
},
{
"text": "When you try dividing a\ndenominator into a numerator,",
"start": 2939.55,
"duration": 3.43
},
{
"text": "how much is left over that\nyou couldn't divide out?",
"start": 2942.98,
"duration": 2.59
},
{
"text": ">> So there isn't really,\nnecessarily, a symbol",
"start": 2945.57,
"duration": 2.34
},
{
"text": "we've used in grade school for this.",
"start": 2947.91,
"duration": 1.56
},
{
"text": "But there in C. You can\nsay x modulo y, where",
"start": 2949.47,
"duration": 4.36
},
{
"text": "this percent sign in this context--\nconfusingly when you're inside",
"start": 2953.83,
"duration": 4.17
},
{
"text": "of the double quotes,\ninside of printf, percent",
"start": 2958.0,
"duration": 2.17
},
{
"text": "is used as the format specifier.",
"start": 2960.17,
"duration": 1.66
},
{
"text": ">> When you use percent outside of\nthat in a mathematical expression,",
"start": 2961.83,
"duration": 3.59
},
{
"text": "it's the modulo operator for modular\narithmetic-- for our purposes",
"start": 2965.42,
"duration": 4.49
},
{
"text": "here, just means, what is the\nremainder of x divided by y?",
"start": 2969.91,
"duration": 3.74
},
{
"text": "So x divided by y is x slash y.",
"start": 2973.65,
"duration": 2.48
},
{
"text": "What's the remainder of x divided by y?",
"start": 2976.13,
"duration": 2.09
},
{
"text": "It's x mod y, as a programmer would say.",
"start": 2978.22,
"duration": 3.56
},
{
"text": ">> So if I made no mistakes here, let me\ngo ahead and make ints, plural, nice,",
"start": 2981.78,
"duration": 6.52
},
{
"text": "and dot slash ints.",
"start": 2988.3,
"duration": 1.71
},
{
"text": "And let's go ahead and\ndo, let's say, 1, 10.",
"start": 2990.01,
"duration": 5.26
},
{
"text": "All right, 1 plus 10 is 11, check.",
"start": 2995.27,
"duration": 3.12
},
{
"text": "1 minus 10 is negative 9, check.",
"start": 2998.39,
"duration": 2.85
},
{
"text": ">> 1 times 10 is 10, check.",
"start": 3001.24,
"duration": 2.18
},
{
"text": "1 divided by 10 is--\nOK, we'll skip that one.",
"start": 3003.42,
"duration": 3.67
},
{
"text": "Remainder of 1 divided by 10 is 1.",
"start": 3007.09,
"duration": 2.39
},
{
"text": "That's correct.",
"start": 3009.48,
"duration": 1.2
},
{
"text": "But there's a bug in here.",
"start": 3010.68,
"duration": 1.95
},
{
"text": ">> So the one I put my\nhand over, not correct.",
"start": 3012.63,
"duration": 2.76
},
{
"text": "I mean, it's close to 0.",
"start": 3015.39,
"duration": 1.28
},
{
"text": "1 divided by 10, you know, if we're\ncutting some corners, sure, it's zero.",
"start": 3016.67,
"duration": 4.0
},
{
"text": "But it should really be 1/10,\n0.1, or 0.10, 0.1000, or so forth.",
"start": 3020.67,
"duration": 7.38
},
{
"text": ">> It should not really be zero.",
"start": 3028.05,
"duration": 2.55
},
{
"text": "Well, it turns out that the computer is\ndoing literally what we told it to do.",
"start": 3030.6,
"duration": 5.39
},
{
"text": "We are doing math like x divided by y.",
"start": 3035.99,
"duration": 3.47
},
{
"text": "And both x and y, per the lines\nof code earlier, are integers.",
"start": 3039.46,
"duration": 5.22
},
{
"text": ">> Moreover, on line 15, we are\ntelling printf, hey, printf plug-in",
"start": 3044.68,
"duration": 5.76
},
{
"text": "an integer, plug-in an integer,\nplug-in an integer-- specifically",
"start": 3050.44,
"duration": 3.79
},
{
"text": "x, and then y, and then x\ndivided by y. x and y are ints.",
"start": 3054.23,
"duration": 3.35
},
{
"text": "We're good there.",
"start": 3057.58,
"duration": 1.48
},
{
"text": ">> But what is x divided by x?",
"start": 3059.06,
"duration": 2.19
},
{
"text": "x divided by y should be,\nmathematically, 1/10, or 0.1,",
"start": 3061.25,
"duration": 5.54
},
{
"text": "which is a real number, a real number\nhaving, potentially, a decimal point.",
"start": 3066.79,
"duration": 4.81
},
{
"text": "It's not an integer.",
"start": 3071.6,
"duration": 1.63
},
{
"text": ">> But what is the closest\ninteger to 1/10, or 0.1?",
"start": 3073.23,
"duration": 5.06
},
{
"text": "Yeah, it kind of is zero.",
"start": 3078.29,
"duration": 2.824
},
{
"text": "0.1 is like this much.",
"start": 3081.114,
"duration": 0.916
},
{
"text": "And 1 is this much.",
"start": 3082.03,
"duration": 0.86
},
{
"text": "So 1/10 is closer to\n0 than it is to one.",
"start": 3082.89,
"duration": 2.98
},
{
"text": ">> And so what C is doing for us--\nkind of because we told it to--",
"start": 3085.87,
"duration": 4.93
},
{
"text": "is truncating that integer.",
"start": 3090.8,
"duration": 1.8
},
{
"text": "It's taking the value, which again is\nsupposed to be something like 0.1000,",
"start": 3092.6,
"duration": 7.94
},
{
"text": "0 and so forth.",
"start": 3100.54,
"duration": 1.26
},
{
"text": "And it's truncating everything\nafter the decimal point",
"start": 3101.8,
"duration": 3.52
},
{
"text": "so that all of this\nstuff, because it doesn't",
"start": 3105.32,
"duration": 2.19
},
{
"text": "fit in the notion of an integer, which\nis just a number like negative 1, 0, 1,",
"start": 3107.51,
"duration": 4.4
},
{
"text": "up and down, it throws away everything\nafter the decimal point because you",
"start": 3111.91,
"duration": 3.92
},
{
"text": "can't fit a decimal point\nin an integer by definition.",
"start": 3115.83,
"duration": 3.19
},
{
"text": ">> So the answer here is zero.",
"start": 3119.02,
"duration": 2.27
},
{
"text": "So how do we fix this?",
"start": 3121.29,
"duration": 1.31
},
{
"text": "We need another solution all together.",
"start": 3122.6,
"duration": 1.8
},
{
"text": "And we can do this, as follows.",
"start": 3124.4,
"duration": 2.48
},
{
"text": ">> Let me go ahead and create a new\nfile, this one called floats.c.",
"start": 3126.88,
"duration": 5.94
},
{
"text": "And save it here in the\nsame directory, float.c.",
"start": 3132.82,
"duration": 3.68
},
{
"text": "And let me go ahead and copy\nsome of that code from earlier.",
"start": 3139.36,
"duration": 3.9
},
{
"text": ">> But instead of getting\nan int, let's do this.",
"start": 3143.26,
"duration": 4.43
},
{
"text": "Give me a floating point value\ncalled x. where a floating point",
"start": 3147.69,
"duration": 3.347
},
{
"text": "value is just literally\nsomething with a floating point.",
"start": 3151.037,
"duration": 2.333
},
{
"text": "It can move to the left, to the right.",
"start": 3153.37,
"duration": 1.04
},
{
"text": "It's a real number.",
"start": 3154.41,
"duration": 1.12
},
{
"text": ">> And let me call not\nget int, but get float,",
"start": 3155.53,
"duration": 2.52
},
{
"text": "which also was among the menu\nof options in the C250 library.",
"start": 3158.05,
"duration": 3.37
},
{
"text": "Let's change y to a float.",
"start": 3161.42,
"duration": 1.8
},
{
"text": "So this becomes get float.",
"start": 3163.22,
"duration": 1.78
},
{
"text": ">> And now, we don't want to plug in ints.",
"start": 3165.0,
"duration": 2.62
},
{
"text": "It turns out we have to use percent\nf for float, percent f for float,",
"start": 3167.62,
"duration": 5.51
},
{
"text": "and now save it.",
"start": 3173.13,
"duration": 1.43
},
{
"text": "And now, fingers crossed, make\nfloats, nice, dot slash floats.",
"start": 3174.56,
"duration": 6.66
},
{
"text": "x is going to be one 1. y\nIs going to be 10 again.",
"start": 3181.22,
"duration": 3.06
},
{
"text": ">> And, nice, OK my addition is correct.",
"start": 3184.28,
"duration": 3.96
},
{
"text": "I was hoping for more,\nbut I forgot to write it.",
"start": 3188.24,
"duration": 2.0
},
{
"text": "So let's go and fix this logical error.",
"start": 3190.24,
"duration": 3.01
},
{
"text": ">> Let's go ahead and grab the following.",
"start": 3193.25,
"duration": 3.03
},
{
"text": "We'll just do a little copy and paste.",
"start": 3196.28,
"duration": 1.8
},
{
"text": "And I'm going to say minus.",
"start": 3198.08,
"duration": 2.0
},
{
"text": ">> And I'm going to say times.",
"start": 3200.08,
"duration": 1.81
},
{
"text": "And I'm going to say divided.",
"start": 3201.89,
"duration": 2.17
},
{
"text": "And I'm not going to do modulo,\nwhich is not as germane here,",
"start": 3204.06,
"duration": 4.18
},
{
"text": "divided by f, and times plus--\nOK, let's do this again.",
"start": 3208.24,
"duration": 5.45
},
{
"text": ">> Make floats, dot slash floats,\nand 1, 10, and-- nice, no, OK.",
"start": 3213.69,
"duration": 10.52
},
{
"text": "So I'm an idiot.",
"start": 3224.21,
"duration": 1.04
},
{
"text": "So this is very common\nin computer science",
"start": 3225.25,
"duration": 1.75
},
{
"text": "to make stupid mistakes like this.",
"start": 3227.0,
"duration": 2.78
},
{
"text": ">> For pedagogical purposes,\nwhat I really wanted to do",
"start": 3229.78,
"duration": 3.32
},
{
"text": "was change the science here\nto plus, to minus, to times,",
"start": 3233.1,
"duration": 4.31
},
{
"text": "and to divide, as you hopefully\nnoticed during this exercise.",
"start": 3237.41,
"duration": 3.73
},
{
"text": "So now let's re-compile this\nprogram, do dot slash floats.",
"start": 3241.14,
"duration": 3.56
},
{
"text": ">> And for the third time, let's\nsee if it meets my expectations.",
"start": 3244.7,
"duration": 3.25
},
{
"text": "1, 10, enter, yes, OK, 1.000,\ndivided by 10.000, is 0.100000.",
"start": 3247.95,
"duration": 13.53
},
{
"text": "And it turns out we can control how many\nnumbers are after those decimal points.",
"start": 3261.48,
"duration": 3.472
},
{
"text": "We actually will.",
"start": 3264.952,
"duration": 0.708
},
{
"text": "We'll come back to that.",
"start": 3265.66,
"duration": 1.13
},
{
"text": ">> But now, in fact, the math is correct.",
"start": 3266.79,
"duration": 1.65
},
{
"text": "So, again, what's the takeaway here?",
"start": 3268.44,
"duration": 1.65
},
{
"text": "It turns out that in C, there are\nnot only just strings-- and, in fact,",
"start": 3270.09,
"duration": 2.96
},
{
"text": "there aren't really, because we\nadd those with the CS50 library.",
"start": 3273.05,
"duration": 3.07
},
{
"text": "But there aren't just ints.",
"start": 3276.12,
"duration": 1.59
},
{
"text": ">> There are also floats.",
"start": 3277.71,
"duration": 1.28
},
{
"text": "And it turns out a bunch of other data\ntypes too, that we'll use before long.",
"start": 3278.99,
"duration": 3.82
},
{
"text": "Turns out if you want a single\ncharacter, not a string of characters,",
"start": 3282.81,
"duration": 3.46
},
{
"text": "you can use just a char.",
"start": 3286.27,
"duration": 1.34
},
{
"text": ">> Turns out that if you want a bool,\na Boolean value, true or false only,",
"start": 3287.61,
"duration": 4.74
},
{
"text": "thanks to the CS50 library, we've\nadded to C the bool data type as well.",
"start": 3292.35,
"duration": 4.49
},
{
"text": "But it's also present in\nmany other languages as well.",
"start": 3296.84,
"duration": 2.34
},
{
"text": "And it turns out that sometimes you\nneed bigger numbers then come by default",
"start": 3299.18,
"duration": 4.95
},
{
"text": "with ints and floats.",
"start": 3304.13,
"duration": 1.08
},
{
"text": ">> And, in fact, a double is a number\nthat uses not 32 bits, but 64 bits.",
"start": 3305.21,
"duration": 5.38
},
{
"text": "And a long long is a number that\nuses not 32, bits but 64 bits,",
"start": 3310.59,
"duration": 4.4
},
{
"text": "respectively, for floating point\nvalues and integers, respectively.",
"start": 3314.99,
"duration": 4.2
},
{
"text": "So let's actually now\nsee this in action.",
"start": 3319.19,
"duration": 3.59
},
{
"text": ">> I'm going to go ahead here\nand whip up one other program.",
"start": 3322.78,
"duration": 3.37
},
{
"text": "Here, I'm going to go ahead\nand do include CS50.h.",
"start": 3326.15,
"duration": 5.87
},
{
"text": "And let me go, include standard IO.h.",
"start": 3332.02,
"duration": 2.89
},
{
"text": ">> And you'll notice something\nfunky is happening here.",
"start": 3334.91,
"duration": 2.41
},
{
"text": "It's not color coding things in\nthe same way as it did before.",
"start": 3337.32,
"duration": 3.272
},
{
"text": "And it turns out, that's because I\nhaven't given the thing a file name.",
"start": 3340.592,
"duration": 2.958
},
{
"text": ">> I'm going to call this one\nsizeof.c, and hit Save.",
"start": 3343.55,
"duration": 3.72
},
{
"text": "And notice what happens to my very\nwhite code against that black backdrop.",
"start": 3347.27,
"duration": 3.769
},
{
"text": "Now, at least there's\nsome purple in there.",
"start": 3351.039,
"duration": 1.791
},
{
"text": "And it is syntax highlighted.",
"start": 3352.83,
"duration": 1.66
},
{
"text": ">> That's because, quite simply, I've\ntold the IDE what type of file",
"start": 3354.49,
"duration": 3.21
},
{
"text": "it is by giving it a name, and\nspecifically a file extension.",
"start": 3357.7,
"duration": 3.36
},
{
"text": "Now, let's go ahead and do this.",
"start": 3361.06,
"duration": 2.56
},
{
"text": "I'm going to go ahead and very\nsimply print out the following-- bool",
"start": 3363.62,
"duration": 5.29
},
{
"text": "is percent LU.",
"start": 3368.91,
"duration": 2.17
},
{
"text": ">> We'll come back to\nthat in just a moment.",
"start": 3371.08,
"duration": 1.87
},
{
"text": "And then I'm going to\nprint size of bool.",
"start": 3372.95,
"duration": 2.89
},
{
"text": "And now, just to save\nmyself some time, I'm",
"start": 3375.84,
"duration": 2.33
},
{
"text": "going to do a whole\nbunch of these at once.",
"start": 3378.17,
"duration": 2.11
},
{
"text": "And, specifically, I'm going to\nchange this to a char and char.",
"start": 3380.28,
"duration": 4.34
},
{
"text": "This one, I'm going to change\nto a double and a double.",
"start": 3384.62,
"duration": 3.14
},
{
"text": ">> This one, I'm going to change\nto a float and a float.",
"start": 3387.76,
"duration": 3.68
},
{
"text": "This one, I'm going to\nchange to an int and an int.",
"start": 3391.44,
"duration": 4.23
},
{
"text": "And this one, I'm going\nto change to a long long.",
"start": 3395.67,
"duration": 2.99
},
{
"text": "And it's still taking\na long time, long long.",
"start": 3398.66,
"duration": 2.18
},
{
"text": ">> And then, lastly, I gave\nmyself one too many, string.",
"start": 3400.84,
"duration": 3.732
},
{
"text": "It turns out that in C, there's\nthe special operator called",
"start": 3404.572,
"duration": 2.458
},
{
"text": "size of that's literally\ngoing to, when run,",
"start": 3407.03,
"duration": 3.23
},
{
"text": "tell us the size of\neach of these variables.",
"start": 3410.26,
"duration": 1.839
},
{
"text": "And this is a way, now,\nwe can connect back",
"start": 3412.099,
"duration": 1.791
},
{
"text": "to last week's discussion\nof data and representation.",
"start": 3413.89,
"duration": 3.25
},
{
"text": ">> Let me go ahead and compile\nsize of dot slash size of.",
"start": 3417.14,
"duration": 3.19
},
{
"text": "And let's see.",
"start": 3420.33,
"duration": 0.88
},
{
"text": "It turns out that in C,\nspecifically on CS50 IDE,",
"start": 3421.21,
"duration": 4.0
},
{
"text": "specifically on the\noperating system Ubuntu,",
"start": 3425.21,
"duration": 2.96
},
{
"text": "which is a 64-bit operating\nsystem in this case,",
"start": 3428.17,
"duration": 2.93
},
{
"text": "a bool is going to\nuse one byte of space.",
"start": 3431.1,
"duration": 3.089
},
{
"text": "That's how size is measured,\nnot in bits, but in bytes.",
"start": 3434.189,
"duration": 2.291
},
{
"text": "And recall that one byte is eight bits.",
"start": 3436.48,
"duration": 2.21
},
{
"text": "So a bool, even though you\ntechnically only need a 0 or 1,",
"start": 3438.69,
"duration": 3.34
},
{
"text": "it's a little wasteful\nhow we've implemented it.",
"start": 3442.03,
"duration": 2.062
},
{
"text": "It's actually going to use a whole\nbyte-- so all zeros, are maybe",
"start": 3444.092,
"duration": 2.708
},
{
"text": "all ones, or something like that,\nor just one 1 among eight bits.",
"start": 3446.8,
"duration": 4.25
},
{
"text": ">> A char, meanwhile, used for a character\nlike an Ascii character per last week,",
"start": 3451.05,
"duration": 3.912
},
{
"text": "is going to be one character.",
"start": 3454.962,
"duration": 1.208
},
{
"text": "And that synchs up with our notion of\nit being no more than 256 bits-- rather,",
"start": 3456.17,
"duration": 6.17
},
{
"text": "synchs up with it being no\nlonger than 8 bits, which",
"start": 3462.34,
"duration": 3.02
},
{
"text": "gives us as many as 256 values.",
"start": 3465.36,
"duration": 2.09
},
{
"text": "A double is going to\nbe 8 bytes or 64 bits.",
"start": 3467.45,
"duration": 2.23
},
{
"text": ">> A float is 4.",
"start": 3469.68,
"duration": 0.83
},
{
"text": "An int is 4.",
"start": 3470.51,
"duration": 1.18
},
{
"text": "A long, long is 8.",
"start": 3471.69,
"duration": 1.29
},
{
"text": "And a string is 8.",
"start": 3472.98,
"duration": 1.736
},
{
"text": "But don't worry about that.",
"start": 3474.716,
"duration": 1.124
},
{
"text": "We're going to peel back that layer.",
"start": 3475.84,
"duration": 1.5
},
{
"text": "It turns out, strings can\nbe longer than 8 bytes.",
"start": 3477.34,
"duration": 2.6
},
{
"text": ">> And, indeed, we've written\nstrings already, hello world,",
"start": 3479.94,
"duration": 2.37
},
{
"text": "longer than 8 bytes.",
"start": 3482.31,
"duration": 1.39
},
{
"text": "But we'll come back to\nthat in just a moment.",
"start": 3483.7,
"duration": 2.57
},
{
"text": "But the take away here is the following.",
"start": 3486.27,
"duration": 3.42
},
{
"text": ">> Any computer only has a finite\namount of memory and space.",
"start": 3489.69,
"duration": 5.63
},
{
"text": "You can only store so many\nfiles on your Mac or PC.",
"start": 3495.32,
"duration": 2.54
},
{
"text": "You can only store so many programs in\nRAM running at once, necessarily, even",
"start": 3497.86,
"duration": 5.17
},
{
"text": "with virtual memory, because\nyou have a finite amount of RAM.",
"start": 3503.03,
"duration": 3.33
},
{
"text": ">> And just to picture-- if\nyou've never opened up a laptop",
"start": 3506.36,
"duration": 2.63
},
{
"text": "or ordered extra memory\nfor a computer, you",
"start": 3508.99,
"duration": 2.31
},
{
"text": "might not know that\ninside of your computer",
"start": 3511.3,
"duration": 2.37
},
{
"text": "is something that looks\na little like this.",
"start": 3513.67,
"duration": 2.92
},
{
"text": "So this is just a common company named\nCrucial that makes RAM for computers.",
"start": 3516.59,
"duration": 3.95
},
{
"text": "And RAM is where programs\nlive while they're running.",
"start": 3520.54,
"duration": 3.08
},
{
"text": ">> So on every Mac or PC, when you double\nclick a program, and it opens up,",
"start": 3523.62,
"duration": 3.01
},
{
"text": "and it opens some Word document\nor something like that,",
"start": 3526.63,
"duration": 2.291
},
{
"text": "it stores it temporarily in\nRAM, because RAM is faster",
"start": 3528.921,
"duration": 2.843
},
{
"text": "than your hard disk, or\nyour solid state disk.",
"start": 3531.764,
"duration": 1.916
},
{
"text": "So it's just where programs go\nto live when they're running,",
"start": 3533.68,
"duration": 2.92
},
{
"text": "or when files are being used.",
"start": 3536.6,
"duration": 1.46
},
{
"text": ">> So you have things that look\nlike this inside of your laptop,",
"start": 3538.06,
"duration": 2.83
},
{
"text": "or slightly bigger things\ninside of your desktop.",
"start": 3540.89,
"duration": 2.43
},
{
"text": "But the key is you only have a\nfinite number of these things.",
"start": 3543.32,
"duration": 4.12
},
{
"text": "And there's only a finite amount of\nhardware sitting on this desk right",
"start": 3547.44,
"duration": 3.79
},
{
"text": "here.",
"start": 3551.23,
"duration": 0.5
},
{
"text": ">> So, surely, we can't store\ninfinitely long numbers.",
"start": 3551.73,
"duration": 4.19
},
{
"text": "And, yet, if you think back to\ngrade school, how many digits can",
"start": 3555.92,
"duration": 3.11
},
{
"text": "you have to the right\nof a decimal point?",
"start": 3559.03,
"duration": 2.37
},
{
"text": "For that matter, how many digits can\nyou have to the left of a decimal point?",
"start": 3561.4,
"duration": 3.28
},
{
"text": "Really, infinitely many.",
"start": 3564.68,
"duration": 1.62
},
{
"text": ">> Now, we humans might only\nknow how to pronounce million,",
"start": 3566.3,
"duration": 4.54
},
{
"text": "and billion, trillion, and\nquadrillion, and quintillion.",
"start": 3570.84,
"duration": 4.15
},
{
"text": "And I'm pushing the limits of my\nunderstanding-- or my-- I understand",
"start": 3574.99,
"duration": 4.38
},
{
"text": "numbers, but my\npronunciation of numbers.",
"start": 3579.37,
"duration": 1.74
},
{
"text": "But they can get infinitely large with\ninfinitely many digits to the left",
"start": 3581.11,
"duration": 3.61
},
{
"text": "or to the right of a decimal point.",
"start": 3584.72,
"duration": 2.33
},
{
"text": ">> But computers only have a\nfinite amount of memory,",
"start": 3587.05,
"duration": 2.99
},
{
"text": "a finite number of transistors, a\nfinite number of light bulbs inside.",
"start": 3590.04,
"duration": 3.47
},
{
"text": "So what happens when\nyou run out of space?",
"start": 3593.51,
"duration": 3.84
},
{
"text": "In other words, if you\nthink back to last week",
"start": 3597.35,
"duration": 2.27
},
{
"text": "when we talked about numbers\nthemselves being represented in binary,",
"start": 3599.62,
"duration": 3.54
},
{
"text": "suppose that we've got\nthis 8-bit value here.",
"start": 3603.16,
"duration": 2.32
},
{
"text": ">> And we have seven 1's and one 0.",
"start": 3605.48,
"duration": 2.81
},
{
"text": "And suppose that we want\nto add 1 to this value.",
"start": 3608.29,
"duration": 2.537
},
{
"text": "This is a really big number right now.",
"start": 3610.827,
"duration": 1.583
},
{
"text": ">> This is 254, if I remember\nthe math from last week right.",
"start": 3612.41,
"duration": 4.2
},
{
"text": "But what if I change\nthat rightmost 0 to a 1?",
"start": 3616.61,
"duration": 2.87
},
{
"text": "The whole number, of\ncourse, becomes eight 1's.",
"start": 3619.48,
"duration": 3.32
},
{
"text": "So we're still good.",
"start": 3622.8,
"duration": 1.25
},
{
"text": ">> And that probably represents\n255, though depending on context",
"start": 3624.05,
"duration": 3.154
},
{
"text": "it could actually represent\na negative number.",
"start": 3627.204,
"duration": 1.916
},
{
"text": "But more on that another time.",
"start": 3629.12,
"duration": 2.12
},
{
"text": "This feels like it's about\nas high as I can count.",
"start": 3631.24,
"duration": 2.98
},
{
"text": ">> Now, it's only 8 bits.",
"start": 3634.22,
"duration": 1.07
},
{
"text": "And my Mac, surely, has way\nmore than 8 bits of memory.",
"start": 3635.29,
"duration": 2.88
},
{
"text": "But it does have finite.",
"start": 3638.17,
"duration": 1.0
},
{
"text": "So the same argument applies, even if we\nhave more of these ones on the screen.",
"start": 3639.17,
"duration": 4.06
},
{
"text": ">> But what happens if you're\nstoring this number, 255,",
"start": 3643.23,
"duration": 3.79
},
{
"text": "and you want to count 1 bit higher?",
"start": 3647.02,
"duration": 2.27
},
{
"text": "You want to go from 255 to 256.",
"start": 3649.29,
"duration": 2.31
},
{
"text": "The problem, of course, is that if you\nstart counting at zero like last week,",
"start": 3651.6,
"duration": 4.2
},
{
"text": "you can't count as high\nas 256, let alone 257,",
"start": 3655.8,
"duration": 3.87
},
{
"text": "let alone 258,m because what\nhappens when you add a 1?",
"start": 3659.67,
"duration": 2.914
},
{
"text": "If you do the old grade school\napproach, you put a 1 here,",
"start": 3662.584,
"duration": 2.416
},
{
"text": "and then 1 plus 1 is 2, but that's\nreally a zero, you carry the 1,",
"start": 3665.0,
"duration": 3.15
},
{
"text": "carry the 1, carry the 1.",
"start": 3668.15,
"duration": 1.545
},
{
"text": "All of these things,\nthese 1's, go to zero.",
"start": 3669.695,
"duration": 2.925
},
{
"text": "And you wind up, yes, as someone\npointed out, a 1 on the left hand side.",
"start": 3672.62,
"duration": 5.2
},
{
"text": "But everything you can\nactually see and fit in memory",
"start": 3677.82,
"duration": 4.72
},
{
"text": "is just eight 0's, which is to say\nat some point if you, a computer,",
"start": 3682.54,
"duration": 5.42
},
{
"text": "tried counting high enough up, you're\ngoing to wrap around, it would seem,",
"start": 3687.96,
"duration": 4.53
},
{
"text": "to zero, or maybe even negative\nnumbers, which are even lower than zero.",
"start": 3692.49,
"duration": 3.36
},
{
"text": ">> And we can kind of see this.",
"start": 3695.85,
"duration": 1.41
},
{
"text": "Let me go ahead and write\na real quick program here.",
"start": 3697.26,
"duration": 2.64
},
{
"text": "Let me go ahead and write\na program called Overflow.",
"start": 3699.9,
"duration": 3.79
},
{
"text": "Include CS50.h, include\nstandard IO.h-- oh,",
"start": 3703.69,
"duration": 6.29
},
{
"text": "I really missed my syntax highlighting.",
"start": 3709.98,
"duration": 1.75
},
{
"text": "So let's save this as overflow.c.",
"start": 3711.73,
"duration": 2.71
},
{
"text": ">> And now int main void--\nand before long, we'll",
"start": 3714.44,
"duration": 2.644
},
{
"text": "come back to explaining why\nwe keep writing int main void.",
"start": 3717.084,
"duration": 2.416
},
{
"text": "But for now, let's just do\nit, taking it for granted.",
"start": 3719.5,
"duration": 2.58
},
{
"text": "Let's give myself an int,\nand initialize it to 0.",
"start": 3722.08,
"duration": 4.12
},
{
"text": ">> Let's then do for int i get zero--\nactually, let's do an infinite loop",
"start": 3726.2,
"duration": 5.516
},
{
"text": "and see what happens.",
"start": 3731.716,
"duration": 0.874
},
{
"text": "While true, then let's print out n\nis percent i, backslash n, plug-in n.",
"start": 3732.59,
"duration": 9.85
},
{
"text": "But, now, let's do n gets n plus 1.",
"start": 3742.44,
"duration": 4.76
},
{
"text": ">> So in other words, on each\niteration of this infinite loop,",
"start": 3747.2,
"duration": 2.46
},
{
"text": "let's take n's value,\nand add 1 to it, and then",
"start": 3749.66,
"duration": 2.89
},
{
"text": "store the result back in n on the left.",
"start": 3752.55,
"duration": 1.8
},
{
"text": "And, in fact, we've seen syntax\nslightly like this, briefly.",
"start": 3754.35,
"duration": 2.8
},
{
"text": "A cool trick is instead\nof writing all this out,",
"start": 3757.15,
"duration": 2.58
},
{
"text": "you can actually say an n plus equals 1.",
"start": 3759.73,
"duration": 3.04
},
{
"text": ">> Or if you really want to be fancy,\nyou can say n plus plus semi-colon.",
"start": 3762.77,
"duration": 4.71
},
{
"text": "But these latter two are just\nwhat we'd call syntactic sugar",
"start": 3767.48,
"duration": 2.65
},
{
"text": "for the first thing.",
"start": 3770.13,
"duration": 0.66
},
{
"text": ">> The first thing is more explicit,\ntotally fine, totally correct.",
"start": 3770.79,
"duration": 2.666
},
{
"text": "But this is more common, I'll say.",
"start": 3773.456,
"duration": 2.014
},
{
"text": "So we'll do this for just a moment.",
"start": 3775.47,
"duration": 1.74
},
{
"text": ">> Let's now make overflow, which sounds\nrather ominous, dot slash overflow.",
"start": 3777.21,
"duration": 4.475
},
{
"text": "Let's see, n's getting pretty big.",
"start": 3784.38,
"duration": 5.472
},
{
"text": "But let's think, how big can n get?",
"start": 3789.852,
"duration": 1.458
},
{
"text": ">> n is an int.",
"start": 3791.31,
"duration": 1.56
},
{
"text": "We saw a moment ago with the size of\nexample that an int is four bytes.",
"start": 3792.87,
"duration": 3.53
},
{
"text": "We know from last week, four bytes is\n32 bits, because 8 times 4, that's 32.",
"start": 3796.4,
"duration": 5.67
},
{
"text": "That's going to be 4 billion.",
"start": 3802.07,
"duration": 1.39
},
{
"text": ">> And we are up to 800,000.",
"start": 3803.46,
"duration": 2.342
},
{
"text": "This is going to take forever to\ncount as high as I possibly can.",
"start": 3805.802,
"duration": 2.708
},
{
"text": "So I'm going to go ahead,\nas you might before long,",
"start": 3808.51,
"duration": 2.125
},
{
"text": "and hit Control C-- frankly, Control\nC, a lot, where Control C generally",
"start": 3810.635,
"duration": 4.275
},
{
"text": "means cancel.",
"start": 3814.91,
"duration": 1.124
},
{
"text": "Unfortunately, because this\nis running in the cloud,",
"start": 3816.034,
"duration": 2.166
},
{
"text": "sometimes the cloud is\nspitting out so much stuff,",
"start": 3818.2,
"duration": 2.99
},
{
"text": "so much output, it's going to\ntake a little while for my input",
"start": 3821.19,
"duration": 2.99
},
{
"text": "to get to the cloud.",
"start": 3824.18,
"duration": 1.45
},
{
"text": "So even though I hit\nControl C a few seconds ago,",
"start": 3825.63,
"duration": 3.61
},
{
"text": "this is definitely the side\neffect of an infinite loop.",
"start": 3829.24,
"duration": 3.87
},
{
"text": ">> And so in such cases, we're\ngoing to leave that be.",
"start": 3833.11,
"duration": 2.96
},
{
"text": "And we're going to add another\nterminal window over here",
"start": 3836.07,
"duration": 2.98
},
{
"text": "with the plus, which of course doesn't\nlike that, since it's still thinking.",
"start": 3839.05,
"duration": 4.136
},
{
"text": "And let's go ahead and be\na little more reasonable.",
"start": 3843.186,
"duration": 2.124
},
{
"text": ">> I'm going to go ahead and do\nthis only finitely many times.",
"start": 3845.31,
"duration": 2.458
},
{
"text": "Let's use a for loop,\nwhich I alluded to earlier.",
"start": 3847.768,
"duration": 2.279
},
{
"text": "Let's do this.",
"start": 3850.047,
"duration": 0.583
},
{
"text": "Give me another variable int i gets 0.",
"start": 3850.63,
"duration": 2.8
},
{
"text": "i is less than, let's say, 64 i++.",
"start": 3853.43,
"duration": 4.0
},
{
"text": "And now let me go ahead and print\nout n is percent i, comma n.",
"start": 3857.43,
"duration": 6.58
},
{
"text": "And then n-- this is still\ngoing to take forever.",
"start": 3864.01,
"duration": 3.537
},
{
"text": "Let's do this.",
"start": 3867.547,
"duration": 0.583
},
{
"text": ">> n gets n times 2.",
"start": 3868.13,
"duration": 2.49
},
{
"text": "Or we could be fancy\nand do times equals 2.",
"start": 3870.62,
"duration": 3.52
},
{
"text": "But let's just say n\nequals itself, times 2.",
"start": 3874.14,
"duration": 2.98
},
{
"text": "In other words, in this\nnew version of the program,",
"start": 3877.12,
"duration": 2.201
},
{
"text": "I don't want to wait forever\nfrom like 800,000 to 4 billion.",
"start": 3879.321,
"duration": 2.499
},
{
"text": "Let's just get this over with.",
"start": 3881.82,
"duration": 1.25
},
{
"text": ">> Let's actually double n each time.",
"start": 3883.07,
"duration": 1.85
},
{
"text": "Which, recall, doubling is the\nopposite of having, of course.",
"start": 3884.92,
"duration": 2.74
},
{
"text": "And whereas last week we have\nsomething again, and again,",
"start": 3887.66,
"duration": 2.375
},
{
"text": "and again, super fast,\ndoubling will surely",
"start": 3890.035,
"duration": 2.165
},
{
"text": "get us from 1 to the biggest possible\nvalue that we can count to with an int.",
"start": 3892.2,
"duration": 5.88
},
{
"text": ">> So let's do exactly this.",
"start": 3898.08,
"duration": 1.67
},
{
"text": "And we'll come back to this before long.",
"start": 3899.75,
"duration": 1.97
},
{
"text": "But this, again, is just like\nthe repeat block in Scratch.",
"start": 3901.72,
"duration": 2.46
},
{
"text": "And you'll use this before long.",
"start": 3904.18,
"duration": 1.42
},
{
"text": ">> This just means count from zero\nup to, but not equal, to 64.",
"start": 3905.6,
"duration": 4.57
},
{
"text": "And on each iteration of this\nloop, just keep incrementing i.",
"start": 3910.17,
"duration": 4.115
},
{
"text": "So i++-- and this general construct\non line 7 is just a super common way",
"start": 3914.285,
"duration": 4.705
},
{
"text": "of repeating some lines of\ncode, some number of times.",
"start": 3918.99,
"duration": 3.3
},
{
"text": "Which lines of code?",
"start": 3922.29,
"duration": 1.072
},
{
"text": "These curly braces, as you\nmay have gleaned from now,",
"start": 3923.362,
"duration": 2.208
},
{
"text": "means, do the following.",
"start": 3925.57,
"duration": 1.21
},
{
"text": ">> It's in like Scratch, when\nit has the yellow blocks",
"start": 3926.78,
"duration": 2.73
},
{
"text": "and other colors that kind of\nembrace or hug other blocks.",
"start": 3929.51,
"duration": 3.17
},
{
"text": "That's what those curly\nbraces are doing here.",
"start": 3932.68,
"duration": 2.07
},
{
"text": "So if I got my syntax right-- you\ncan see the carrot symbol in C means",
"start": 3934.75,
"duration": 5.45
},
{
"text": "that's how many times I was\ntrying to solve this problem.",
"start": 3940.2,
"duration": 2.506
},
{
"text": "So let's get rid of that one\naltogether, and close that window.",
"start": 3942.706,
"duration": 2.624
},
{
"text": "And we'll use the new one.",
"start": 3945.33,
"duration": 1.19
},
{
"text": "Make overflow, dot slash\noverflow, Enter, all right,",
"start": 3946.52,
"duration": 5.46
},
{
"text": "it looks bad at first.",
"start": 3951.98,
"duration": 1.11
},
{
"text": "But let's scroll back in time,\nbecause I did this 64 times.",
"start": 3953.09,
"duration": 3.11
},
{
"text": ">> And notice the first time, n is 1.",
"start": 3956.2,
"duration": 2.5
},
{
"text": "Second time, n is 2,\nthen 4, then 8, then 16.",
"start": 3958.7,
"duration": 4.41
},
{
"text": "And it seems that as soon as\nI get to roughly 1 billion,",
"start": 3963.11,
"duration": 6.34
},
{
"text": "if I double it again, that\nshould give me 2 billion.",
"start": 3969.45,
"duration": 3.35
},
{
"text": "But it turns out, it's\nright on the cusp.",
"start": 3972.8,
"duration": 2.18
},
{
"text": ">> And so it actually overflows\nan int from 1 billion",
"start": 3974.98,
"duration": 3.95
},
{
"text": "to roughly negative 2\nbillion, because an integer,",
"start": 3978.93,
"duration": 4.584
},
{
"text": "unlike the numbers we\nwere assuming last week,",
"start": 3983.514,
"duration": 1.916
},
{
"text": "can be both positive and negative\nin reality and in a computer.",
"start": 3985.43,
"duration": 2.967
},
{
"text": "And so at least one of those\nbits is effectively stolen.",
"start": 3988.397,
"duration": 2.333
},
{
"text": "So we really only have 31 bits,\nor 2 billion possible values.",
"start": 3990.73,
"duration": 3.46
},
{
"text": ">> But for now, the takeaway is quite\nsimply, whatever these numbers are",
"start": 3994.19,
"duration": 4.03
},
{
"text": "and whatever the math is,\nsomething bad happens eventually,",
"start": 3998.22,
"duration": 4.06
},
{
"text": "because eventually you are trying to\npermute the bits one too many times.",
"start": 4002.28,
"duration": 4.7
},
{
"text": "And you effectively go from all\n1's to maybe all 0's, or maybe",
"start": 4006.98,
"duration": 4.08
},
{
"text": "just some other pattern that it\nclearly, depending on context,",
"start": 4011.06,
"duration": 3.2
},
{
"text": "can be interpreted as a negative number.",
"start": 4014.26,
"duration": 2.082
},
{
"text": "And so it would seem the highest I\ncan count in this particular program",
"start": 4016.342,
"duration": 2.958
},
{
"text": "is only roughly 1 billion.",
"start": 4019.3,
"duration": 1.91
},
{
"text": "But there's a partial solution here.",
"start": 4021.21,
"duration": 1.55
},
{
"text": "You know what?",
"start": 4022.76,
"duration": 0.72
},
{
"text": ">> Let me change from an\nint to a long long.",
"start": 4023.48,
"duration": 4.12
},
{
"text": "And let me go ahead here\nand say-- I'm going to have",
"start": 4027.6,
"duration": 3.033
},
{
"text": "to change this to an unsigned long.",
"start": 4030.633,
"duration": 1.657
},
{
"text": "Or, let's see, I never remember myself.",
"start": 4032.29,
"duration": 4.57
},
{
"text": ">> Let's go ahead and make overflow.",
"start": 4036.86,
"duration": 3.06
},
{
"text": "No, that's not it, LLD, thank you.",
"start": 4039.92,
"duration": 1.94
},
{
"text": "So sometimes Clang can be helpful.",
"start": 4041.86,
"duration": 1.57
},
{
"text": "I did not remember what the format\nspecifier was for a long long.",
"start": 4043.43,
"duration": 4.12
},
{
"text": ">> But, indeed, Clang told me.",
"start": 4047.55,
"duration": 1.4
},
{
"text": "Green is some kind of good,\nstill means you made a mistake.",
"start": 4048.95,
"duration": 2.62
},
{
"text": "It's guessing that I meant LLD.",
"start": 4051.57,
"duration": 1.62
},
{
"text": ">> So let me take it's advice, a long\nlong decimal number, save that.",
"start": 4053.19,
"duration": 5.56
},
{
"text": "And let me rerun it, dot\nslash overflow, Enter.",
"start": 4058.75,
"duration": 4.44
},
{
"text": "And now what's cool is this.",
"start": 4063.19,
"duration": 1.83
},
{
"text": ">> If I scroll back in time, we still start\ncounting at the same place-- 1, 2, 4,",
"start": 4065.02,
"duration": 4.12
},
{
"text": "8, 16.",
"start": 4069.14,
"duration": 1.08
},
{
"text": "Notice, we get all the\nway up to 1 billion.",
"start": 4070.22,
"duration": 4.64
},
{
"text": "But then we safely get to 2 billion.",
"start": 4074.86,
"duration": 2.21
},
{
"text": ">> Then we get to 4 billion,\nthen 8 billion, 17 billion.",
"start": 4077.07,
"duration": 4.23
},
{
"text": "And we go higher, and\nhigher, and higher.",
"start": 4081.3,
"duration": 2.04
},
{
"text": "Eventually, this, too, breaks.",
"start": 4083.34,
"duration": 2.4
},
{
"text": ">> Eventually, with a long long,\nwhich is the 64-bit value, not",
"start": 4085.74,
"duration": 3.61
},
{
"text": "a 32-bit value, if you count\ntoo high, you wrap around 0.",
"start": 4089.35,
"duration": 4.31
},
{
"text": "And in this case, we happen to\nend up with a negative number.",
"start": 4093.66,
"duration": 2.75
},
{
"text": ">> So this is a problem.",
"start": 4096.41,
"duration": 1.14
},
{
"text": "And it turns out that this\nproblem is not all that arcane.",
"start": 4097.55,
"duration": 2.889
},
{
"text": "Even though I've deliberately\ninduced it with these mistakes,",
"start": 4100.439,
"duration": 2.621
},
{
"text": "it turns out we see it kind of all\naround us, or at least some of us do.",
"start": 4103.06,
"duration": 3.089
},
{
"text": ">> So in Lego Star Wars, if\nyou've ever played the game,",
"start": 4106.149,
"duration": 2.79
},
{
"text": "it turns out you can go around\nbreaking things up in LEGO world,",
"start": 4108.939,
"duration": 4.891
},
{
"text": "and collecting coins, essentially.",
"start": 4113.83,
"duration": 2.81
},
{
"text": "And if you've ever played\nthis game way too much time,",
"start": 4116.64,
"duration": 2.56
},
{
"text": "as this unnamed individual\nhere did, the total number",
"start": 4119.2,
"duration": 3.43
},
{
"text": "of coins that you can collect\nis, it would seem, 4 billion.",
"start": 4122.63,
"duration": 4.07
},
{
"text": ">> Now, with it's actually rounded.",
"start": 4126.7,
"duration": 1.54
},
{
"text": "So LEGO was trying to\nkeep things user friendly.",
"start": 4128.24,
"duration": 1.999
},
{
"text": "They didn't do it exactly 2 to\nthe 32 power, per last week.",
"start": 4130.239,
"duration": 3.54
},
{
"text": "But 4 billion is a reason.",
"start": 4133.779,
"duration": 1.531
},
{
"text": "It seems, based on this information,\nthat LEGO, and the company that",
"start": 4135.31,
"duration": 3.669
},
{
"text": "made this actual software, decided\nthat the maximum number of coins",
"start": 4138.979,
"duration": 3.645
},
{
"text": "the user can accumulate\nis, indeed, 4 billion,",
"start": 4142.624,
"duration": 1.916
},
{
"text": "because they chose in their code\nto use not a long long, apparently,",
"start": 4144.54,
"duration": 7.529
},
{
"text": "but just an integer, an unsigned\ninteger, only a positive integer, whose",
"start": 4152.069,
"duration": 4.071
},
{
"text": "max value is roughly that.",
"start": 4156.14,
"duration": 1.949
},
{
"text": "Well, here's another funny one.",
"start": 4158.089,
"duration": 1.291
},
{
"text": "So in the game Civilization, which\nsome of you might be familiar, with",
"start": 4159.38,
"duration": 4.12
},
{
"text": "it turns out that years ago there\nwas a bug in this game whereby",
"start": 4163.5,
"duration": 3.16
},
{
"text": "if you played the role\nof Gandhi in the game,",
"start": 4166.66,
"duration": 2.09
},
{
"text": "instead of him being very pacifist,\ninstead was incredibly, incredibly",
"start": 4168.75,
"duration": 5.27
},
{
"text": "aggressive, in some circumstances.",
"start": 4174.02,
"duration": 2.379
},
{
"text": "In particular, the way that Civilization\nworks is that if you, the player,",
"start": 4176.399,
"duration": 4.13
},
{
"text": "adopt democracy, your\naggressiveness score gets",
"start": 4180.529,
"duration": 4.151
},
{
"text": "decremented by two, so minus\nminus, and then minus minus.",
"start": 4184.68,
"duration": 3.45
},
{
"text": ">> So you subtract 2 from\nyour actual iterating.",
"start": 4188.13,
"duration": 2.439
},
{
"text": "Unfortunately, if your iterating is\ninitially 1, and you subtract 2 from it",
"start": 4190.569,
"duration": 6.081
},
{
"text": "after adopting democracy\nas Gandhi here might",
"start": 4196.65,
"duration": 2.4
},
{
"text": "have done, because he was very passive--\n1 on the scale of aggressiveness.",
"start": 4199.05,
"duration": 3.15
},
{
"text": "But if he adopts democracy, then\nhe goes from 1 to negative 1.",
"start": 4202.2,
"duration": 2.63
},
{
"text": ">> Unfortunately, they were\nusing unsigned numbers,",
"start": 4204.83,
"duration": 6.64
},
{
"text": "which means they treated even negative\nnumbers as though they were positive.",
"start": 4211.47,
"duration": 3.93
},
{
"text": "And it turns out that the\npositive equivalent of negative 1,",
"start": 4215.4,
"duration": 4.38
},
{
"text": "in typical computer programs, is 255.",
"start": 4219.78,
"duration": 3.7
},
{
"text": "So if Gandhi adopts\ndemocracy, and therefore has",
"start": 4223.48,
"duration": 3.77
},
{
"text": "his aggressiveness score decreased,\nit actually rolls around to 255",
"start": 4227.25,
"duration": 5.22
},
{
"text": "and makes him the most\naggressive character in the game.",
"start": 4232.47,
"duration": 3.0
},
{
"text": "So you can Google up on this.",
"start": 4235.47,
"duration": 1.46
},
{
"text": "And it was, indeed, an\naccidental programming bug,",
"start": 4236.93,
"duration": 2.45
},
{
"text": "but that's entered quite\nthe lore ever since.",
"start": 4239.38,
"duration": 3.63
},
{
"text": ">> That's all fun and cute.",
"start": 4243.01,
"duration": 1.35
},
{
"text": "More frightening is when actual\nreal world devices, and not games,",
"start": 4244.36,
"duration": 3.4
},
{
"text": "have these same bugs.",
"start": 4247.76,
"duration": 1.06
},
{
"text": "In fact, just a year ago an article came\nout about the Boeing 787 Dreamliner.",
"start": 4248.82,
"duration": 5.68
},
{
"text": ">> And the article at first\nglance reads a little arcane.",
"start": 4254.5,
"duration": 2.35
},
{
"text": "But it said this, a software\nvulnerability in Boeing's",
"start": 4256.85,
"duration": 4.63
},
{
"text": "new 787 Dreamliner jet has\nthe potential to cause pilots",
"start": 4261.48,
"duration": 3.31
},
{
"text": "to lose control of\nthe aircraft, possibly",
"start": 4264.79,
"duration": 2.43
},
{
"text": "in mid-flight, the FAA officials\nwarned airlines recently.",
"start": 4267.22,
"duration": 4.53
},
{
"text": "It was the determination\nthat a model 787",
"start": 4271.75,
"duration": 2.77
},
{
"text": "airplane that has been powered\ncontinuously for 248 days",
"start": 4274.52,
"duration": 5.25
},
{
"text": "can lose all alternating current, AC,\nelectrical power due to the generator",
"start": 4279.77,
"duration": 5.11
},
{
"text": "control units, GCUs, simultaneously\ngoing into fail safe mode.",
"start": 4284.88,
"duration": 4.012
},
{
"text": "It's kind of losing me.",
"start": 4288.892,
"duration": 0.958
},
{
"text": "But the memo stated, OK, now I got that,\nthe condition was caused by a software",
"start": 4289.85,
"duration": 5.54
},
{
"text": "counter internal to\nthe generator control",
"start": 4295.39,
"duration": 3.2
},
{
"text": "units that will overflow after\n248 days of continuous power.",
"start": 4298.59,
"duration": 6.27
},
{
"text": "We are issuing this\nnotice to prevent loss",
"start": 4304.86,
"duration": 2.21
},
{
"text": "of all AC electrical\npower, which could result",
"start": 4307.07,
"duration": 2.23
},
{
"text": "in loss of control of the airplane.",
"start": 4309.3,
"duration": 1.68
},
{
"text": ">> So, literally, there is some integer,\nor some equivalent data type,",
"start": 4310.98,
"duration": 4.4
},
{
"text": "being used in software\nin an actual airplane",
"start": 4315.38,
"duration": 2.58
},
{
"text": "that if you keep your airplane\non long enough, which apparently",
"start": 4317.96,
"duration": 2.796
},
{
"text": "can be the case if you're just running\nthem constantly and never unplugging",
"start": 4320.756,
"duration": 3.124
},
{
"text": "your airplane, it seems, or\nletting its batteries die,",
"start": 4323.88,
"duration": 2.93
},
{
"text": "will eventually count up, and up,\nand up, and up, and up, and up.",
"start": 4326.81,
"duration": 3.03
},
{
"text": ">> And, by nature, a\nfinite amount of memory",
"start": 4329.84,
"duration": 2.31
},
{
"text": "will overflow, rolling back to\nzero or some negative value,",
"start": 4332.15,
"duration": 3.73
},
{
"text": "a side effect of which is the\nfrighteningly real reality",
"start": 4335.88,
"duration": 4.04
},
{
"text": "that the plane might need\nto be rebooted, effectively,",
"start": 4339.92,
"duration": 4.05
},
{
"text": "or might fall, worse, as it flies.",
"start": 4343.97,
"duration": 3.32
},
{
"text": "So these kinds of issues\nare still with us,",
"start": 4347.29,
"duration": 1.94
},
{
"text": "even-- this was a 2015 article,\nall the more frightening",
"start": 4349.23,
"duration": 3.9
},
{
"text": "when you don't necessarily\nunderstand, appreciate, or anticipate",
"start": 4353.13,
"duration": 2.97
},
{
"text": "those kinds of errors.",
"start": 4356.1,
"duration": 2.54
},
{
"text": ">> So it turns out there's one other\nbad thing about data representation.",
"start": 4358.64,
"duration": 3.39
},
{
"text": "It turns out that even floats are\nkind of flawed, because floats, too,",
"start": 4362.03,
"duration": 5.05
},
{
"text": "I proposed are 32 bits, or\nmaybe 64 if you use a double.",
"start": 4367.08,
"duration": 4.36
},
{
"text": "But that's still finite.",
"start": 4371.44,
"duration": 1.63
},
{
"text": ">> And the catch is that if you can\nput an infinite number of numbers",
"start": 4373.07,
"duration": 4.0
},
{
"text": "after the decimal point,\nthere is no way you",
"start": 4377.07,
"duration": 2.39
},
{
"text": "can represent all the possible\nnumbers that we were taught",
"start": 4379.46,
"duration": 3.23
},
{
"text": "in grade school can exist in the world.",
"start": 4382.69,
"duration": 2.3
},
{
"text": "A computer, essentially, has to\nchoose a subset of those numbers",
"start": 4384.99,
"duration": 3.88
},
{
"text": "to represent accurately.",
"start": 4388.87,
"duration": 1.33
},
{
"text": ">> Now, the computer can\nround maybe a little bit,",
"start": 4390.2,
"duration": 2.25
},
{
"text": "and can allow you to roughly store\nany number you might possibly want.",
"start": 4392.45,
"duration": 5.45
},
{
"text": "But just intuitively, if you\nhave a finite number of bits,",
"start": 4397.9,
"duration": 3.04
},
{
"text": "you can only permute them\nin so many finite ways.",
"start": 4400.94,
"duration": 3.62
},
{
"text": "So you can't possibly\nuse a finite number",
"start": 4404.56,
"duration": 2.01
},
{
"text": "of permutation of bits,\npatterns of zeros and ones,",
"start": 4406.57,
"duration": 3.31
},
{
"text": "to represent an infinite\nnumber of numbers,",
"start": 4409.88,
"duration": 3.06
},
{
"text": "which suggests that computers might\nvery well be lying to us sometimes.",
"start": 4412.94,
"duration": 4.43
},
{
"text": ">> In fact, let's do this.",
"start": 4417.37,
"duration": 1.4
},
{
"text": "Let me go back into CS50 IDE.",
"start": 4418.77,
"duration": 2.469
},
{
"text": "Let me go ahead and\ncreate a little program",
"start": 4421.239,
"duration": 1.791
},
{
"text": "called Imprecision, to show that\ncomputers are, indeed, imprecise.",
"start": 4423.03,
"duration": 4.91
},
{
"text": ">> And let me go ahead and start with\nsome of that code from before,",
"start": 4427.94,
"duration": 3.97
},
{
"text": "and now just do the following.",
"start": 4431.91,
"duration": 1.92
},
{
"text": "Let me go ahead and do printf, percent\nf, backslash n, 1 divided by 10.",
"start": 4433.83,
"duration": 9.81
},
{
"text": "In other words, let's dive in deeper\nto 1/10, like 1 and divided by 10.",
"start": 4443.64,
"duration": 3.79
},
{
"text": "Surely, a computer can represent 1/10.",
"start": 4447.43,
"duration": 2.33
},
{
"text": ">> So let's go ahead and make imprecision.",
"start": 4449.76,
"duration": 3.86
},
{
"text": "Let's see.",
"start": 4453.62,
"duration": 0.77
},
{
"text": "Format specifies type double.",
"start": 4454.39,
"duration": 1.82
},
{
"text": "But the argument has type int.",
"start": 4456.21,
"duration": 1.95
},
{
"text": "What's going on?",
"start": 4458.16,
"duration": 0.88
},
{
"text": ">> Oh, interesting, so it's a\nlesson learned from before.",
"start": 4459.04,
"duration": 2.93
},
{
"text": "I'm saying, hey, computer show\nme a float with percent f.",
"start": 4461.97,
"duration": 4.08
},
{
"text": "But I'm giving it 2 ints.",
"start": 4466.05,
"duration": 2.15
},
{
"text": "So it turns out, I can fix\nthis in a couple of ways.",
"start": 4468.2,
"duration": 2.92
},
{
"text": ">> I could just turn one into 1.0, and\n10 into 10.0, which would, indeed,",
"start": 4471.12,
"duration": 7.31
},
{
"text": "have the effect of converting\nthem into floats-- still hopefully",
"start": 4478.43,
"duration": 3.96
},
{
"text": "the same number.",
"start": 4482.39,
"duration": 0.79
},
{
"text": "Or it turns out there's something\nwe'll see again before long.",
"start": 4483.18,
"duration": 2.7
},
{
"text": "You could cast the numbers.",
"start": 4485.88,
"duration": 1.29
},
{
"text": ">> You can, using this parenthetical\nexpression, you can say,",
"start": 4487.17,
"duration": 2.71
},
{
"text": "hey, computer, take this\n10, which I know is an int.",
"start": 4489.88,
"duration": 2.68
},
{
"text": "But treat it, please,\nas though it's a float.",
"start": 4492.56,
"duration": 2.1
},
{
"text": "But this feels unnecessarily complex.",
"start": 4494.66,
"duration": 2.02
},
{
"text": ">> For our purposes today,\nlet's just literally",
"start": 4496.68,
"duration": 2.36
},
{
"text": "make them floating point values\nwith a decimal point, like this.",
"start": 4499.04,
"duration": 3.66
},
{
"text": "Let me go ahead and rerun, make\nimprecision, good, dot slash",
"start": 4502.7,
"duration": 4.36
},
{
"text": "imprecision, enter.",
"start": 4507.06,
"duration": 1.81
},
{
"text": "OK, we're looking good.",
"start": 4508.87,
"duration": 2.12
},
{
"text": ">> 1 divided by 10, according to my\nMac here, is, indeed, 0.100000.",
"start": 4510.99,
"duration": 7.204
},
{
"text": "Now, I was taught in grade school there\nshould be an infinite number of 0's.",
"start": 4518.194,
"duration": 3.166
},
{
"text": "So let's at least try\nto see some of those.",
"start": 4521.36,
"duration": 1.791
},
{
"text": "It turns out that printf is a little\nfancier still than we've been using.",
"start": 4523.151,
"duration": 3.619
},
{
"text": "It turns out you don't have to specify\njust percent f, or just percent i.",
"start": 4526.77,
"duration": 4.12
},
{
"text": "You can actually specify\nsome control options here.",
"start": 4530.89,
"duration": 2.94
},
{
"text": ">> Specifically, I'm going\nto say, hey, printf,",
"start": 4533.83,
"duration": 2.64
},
{
"text": "actually show me 10 decimal points.",
"start": 4536.47,
"duration": 3.19
},
{
"text": "So it looks a little weird.",
"start": 4539.66,
"duration": 1.16
},
{
"text": "But you say percent,\ndot, how many numbers",
"start": 4540.82,
"duration": 2.025
},
{
"text": "you want to see after the\ndecimal point, and then f",
"start": 4542.845,
"duration": 2.125
},
{
"text": "for flat, just because that's\nwhat the documentation says.",
"start": 4544.97,
"duration": 3.37
},
{
"text": "Let me go ahead and save that.",
"start": 4548.34,
"duration": 1.74
},
{
"text": ">> And notice too, I'm getting\ntired of retyping things.",
"start": 4550.08,
"duration": 2.38
},
{
"text": "So I'm just setting the up and\ndown arrow on my keys here.",
"start": 4552.46,
"duration": 3.44
},
{
"text": "And if I keep hitting up, you\ncan see all of the commands",
"start": 4555.9,
"duration": 2.81
},
{
"text": "that I made, or incorrectly made.",
"start": 4558.71,
"duration": 2.38
},
{
"text": ">> And I'm going to go ahead now and\nnot actually use that, apparently.",
"start": 4561.09,
"duration": 3.54
},
{
"text": "Make imprecision, dot\nslash imprecision-- so",
"start": 4564.63,
"duration": 6.786
},
{
"text": "what I was taught in\ngrade school checks out.",
"start": 4571.416,
"duration": 1.874
},
{
"text": "Even if I print it to 10 decimal\nplaces it, indeed, is 0.10000.",
"start": 4573.29,
"duration": 5.72
},
{
"text": "But you know what?",
"start": 4579.01,
"duration": 0.83
},
{
"text": ">> Let's get a little greedy.",
"start": 4579.84,
"duration": 1.31
},
{
"text": "Let's say, like, show me 55\npoints after the decimal.",
"start": 4581.15,
"duration": 2.84
},
{
"text": "Let's really take this\nprogram out for a spin.",
"start": 4583.99,
"duration": 2.17
},
{
"text": "Let me remake it with make\nimprecision, dot slash, imprecision.",
"start": 4586.16,
"duration": 5.01
},
{
"text": ">> And here we go.",
"start": 4591.17,
"duration": 1.22
},
{
"text": "Your childhood was a lie.",
"start": 4592.39,
"duration": 2.03
},
{
"text": "Apparently, 1 divided by 10 is indeed\n0.100000000000000005551115123--",
"start": 4594.42,
"duration": 13.99
},
{
"text": ">> What is going on?",
"start": 4608.41,
"duration": 1.33
},
{
"text": "Well, it turns out, if you kind of\nlook far enough out in the underlying",
"start": 4609.74,
"duration": 3.62
},
{
"text": "representation of this\nnumber, it actually",
"start": 4613.36,
"duration": 2.59
},
{
"text": "is not exactly 1/10, or 0.1 and\nan infinite number of zeros.",
"start": 4615.95,
"duration": 4.45
},
{
"text": "Now, why is that?",
"start": 4620.4,
"duration": 1.23
},
{
"text": ">> Well, even though this is a simple\nnumber to us humans, 1 divided by 10,",
"start": 4621.63,
"duration": 4.62
},
{
"text": "it's still one of infinitely many\nnumbers that we could think up.",
"start": 4626.25,
"duration": 4.66
},
{
"text": "But a computer can only represent\nfinitely many so numbers.",
"start": 4630.91,
"duration": 3.58
},
{
"text": "And so, effectively, what the\ncomputer is showing us is its closest",
"start": 4634.49,
"duration": 4.22
},
{
"text": "approximation to the number\nwe want to believe is 1/10,",
"start": 4638.71,
"duration": 4.23
},
{
"text": "or really 0.10000 ad infinitum.",
"start": 4642.94,
"duration": 4.82
},
{
"text": ">> Rather, though, this is\nas close as it can get.",
"start": 4647.76,
"duration": 2.665
},
{
"text": "And, indeed, if you look\nunderneath the hood,",
"start": 4650.425,
"duration": 1.875
},
{
"text": "as we are here by looking\n55 digits after the decimal,",
"start": 4652.3,
"duration": 4.75
},
{
"text": "we actually see that reality.",
"start": 4657.05,
"duration": 2.94
},
{
"text": "Now as an aside, if you've\never seen the movie--",
"start": 4659.99,
"duration": 2.62
},
{
"text": "most of you probably haven't--\nbut Superman 3 some years ago,",
"start": 4662.61,
"duration": 3.17
},
{
"text": "Richard Pryor essentially leveraged this\nreality in his company to steal a lot",
"start": 4665.78,
"duration": 3.72
},
{
"text": "of fractions and fractions of pennies,\nbecause the company-- as I recall,",
"start": 4669.5,
"duration": 4.0
},
{
"text": "it's been a while-- was essentially\nthrowing away anything that didn't fit",
"start": 4673.5,
"duration": 3.71
},
{
"text": "into the notion of cents.",
"start": 4677.21,
"duration": 1.58
},
{
"text": ">> But if you add up all these\ntiny, tiny, tiny numbers again,",
"start": 4678.79,
"duration": 2.69
},
{
"text": "and again, and again, you can, as in\nhis case, make a good amount of money.",
"start": 4681.48,
"duration": 3.48
},
{
"text": ">> That same idea was ripped off by\na more recent, but still now older",
"start": 4684.96,
"duration": 3.05
},
{
"text": "movie, called Office Space,\nwhere the guys in that movie,",
"start": 4688.01,
"duration": 2.49
},
{
"text": "did the same thing, screwed it up\ncompletely, ended up with way too much",
"start": 4690.5,
"duration": 3.001
},
{
"text": "money in their bank account.",
"start": 4693.501,
"duration": 1.165
},
{
"text": "It was all very suspicious.",
"start": 4694.666,
"duration": 1.134
},
{
"text": "But at the end of the day,\nimprecision is all around us.",
"start": 4695.8,
"duration": 3.49
},
{
"text": ">> And that, too, can be\nfrighteningly the case.",
"start": 4699.29,
"duration": 2.95
},
{
"text": "It turns out that Superman 3\nand Office Space aside, there",
"start": 4702.24,
"duration": 3.35
},
{
"text": "can be some very real\nworld ramifications",
"start": 4705.59,
"duration": 2.87
},
{
"text": "of the realities of imprecise\nrepresentation of data",
"start": 4708.46,
"duration": 3.83
},
{
"text": "that even we humans to\nthis day don't necessarily",
"start": 4712.29,
"duration": 2.48
},
{
"text": "understand as well as we should,\nor remember as often as we should.",
"start": 4714.77,
"duration": 3.46
},
{
"text": "And, indeed, the following clip is\nfrom a look at some very real world",
"start": 4718.23,
"duration": 4.72
},
{
"text": "ramifications of what happens if you\ndon't appreciate the imprecision that",
"start": 4722.95,
"duration": 4.78
},
{
"text": "can happen in numbers representation.",
"start": 4727.73,
"duration": 2.335
},
{
"text": ">> [VIDEO PLAYBACK]",
"start": 4730.065,
"duration": 1.235
},
{
"text": ">> -Computers, we've all come to accept\nthe often frustrating problems that",
"start": 4731.3,
"duration": 4.32
},
{
"text": "go with them-- bugs, viruses,\nand software glitches,",
"start": 4735.62,
"duration": 4.69
},
{
"text": "for small prices to pay\nfor the convenience.",
"start": 4740.31,
"duration": 2.82
},
{
"text": "But in high tech and high speed\nmilitary and space program applications,",
"start": 4743.13,
"duration": 4.67
},
{
"text": "the smallest problem can\nbe magnified into disaster.",
"start": 4747.8,
"duration": 5.0
},
{
"text": ">> On June 4th, 1996, scientists prepared\nto launch an unmanned Ariane 5 rocket.",
"start": 4752.8,
"duration": 6.1
},
{
"text": "It was carrying scientific\nsatellites designed",
"start": 4758.9,
"duration": 2.32
},
{
"text": "to establish precisely how the\nearth's magnetic field interacts",
"start": 4761.22,
"duration": 3.38
},
{
"text": "with solar winds.",
"start": 4764.6,
"duration": 2.81
},
{
"text": "The rocket was built for\nthe European Space Agency,",
"start": 4767.41,
"duration": 3.39
},
{
"text": "and lifted off from its facility\non the coast of French Guiana.",
"start": 4770.8,
"duration": 3.57
},
{
"text": ">> -At about 37 seconds into\nthe flight, they first",
"start": 4774.37,
"duration": 3.17
},
{
"text": "noticed something was going wrong.",
"start": 4777.54,
"duration": 1.73
},
{
"text": "The nozzles were swiveling in\na way they really shouldn't.",
"start": 4779.27,
"duration": 2.98
},
{
"text": "Around 40 seconds into the flight,\nclearly, the vehicle was in trouble.",
"start": 4782.25,
"duration": 4.33
},
{
"text": ">> And that's when they made\na decision to destroy it.",
"start": 4786.58,
"duration": 2.27
},
{
"text": "The range safety officer, with\ntremendous guts, pressed the button,",
"start": 4788.85,
"duration": 3.93
},
{
"text": "blew up the rocket, before it could\nbecome a hazard to the public safety.",
"start": 4792.78,
"duration": 5.37
},
{
"text": ">> -This was the maiden\nvoyage of the Ariane 5.",
"start": 4798.15,
"duration": 2.91
},
{
"text": "And its destruction took\nplace because of a flaw",
"start": 4801.06,
"duration": 2.9
},
{
"text": "embedded in the rocket's software.",
"start": 4803.96,
"duration": 1.862
},
{
"text": "-The problem on the Ariane was\nthat there was a number that",
"start": 4805.822,
"duration": 2.458
},
{
"text": "required 64 bits to express.",
"start": 4808.28,
"duration": 2.32
},
{
"text": "And they wanted to convert\nit to a 16-bit number.",
"start": 4810.6,
"duration": 2.99
},
{
"text": "They assumed that the\nnumber was never going",
"start": 4813.59,
"duration": 2.02
},
{
"text": "to be very big, that most of those\ndigits in a 64-bit number were zeroes.",
"start": 4815.61,
"duration": 5.37
},
{
"text": "They were wrong.",
"start": 4820.98,
"duration": 1.46
},
{
"text": ">> -The inability of one\nsoftware program to accept",
"start": 4822.44,
"duration": 2.62
},
{
"text": "the kind of number generated by\nanother was at the root of the failure.",
"start": 4825.06,
"duration": 4.45
},
{
"text": "Software development had become a\nvery costly part of new technology.",
"start": 4829.51,
"duration": 4.84
},
{
"text": "The Ariane rocket have been very\nsuccessful, so much of the software",
"start": 4834.35,
"duration": 3.79
},
{
"text": "created for it was also\nused in the Ariane 5.",
"start": 4838.14,
"duration": 3.41
},
{
"text": ">> -The basic problem was that the Ariane\n5 was faster, accelerated faster.",
"start": 4841.55,
"duration": 6.39
},
{
"text": "And the software hadn't\naccounted for that.",
"start": 4847.94,
"duration": 3.51
},
{
"text": ">> -The destruction of the rocket\nwas a huge financial disaster,",
"start": 4851.45,
"duration": 3.61
},
{
"text": "all due to a minute software error.",
"start": 4855.06,
"duration": 3.73
},
{
"text": "But this wasn't the first\ntime data conversion problems",
"start": 4858.79,
"duration": 2.42
},
{
"text": "had plagued modern rocket technology.",
"start": 4861.21,
"duration": 3.61
},
{
"text": ">> -In 1991, with the start\nof the first Gulf War,",
"start": 4864.82,
"duration": 3.23
},
{
"text": "the Patriot Missile\nexperienced a similar kind",
"start": 4868.05,
"duration": 2.52
},
{
"text": "of number conversion problem.",
"start": 4870.57,
"duration": 2.23
},
{
"text": "And as a result, 28 people,\n28 American soldiers,",
"start": 4872.8,
"duration": 3.29
},
{
"text": "were killed, and about\n100 others wounded,",
"start": 4876.09,
"duration": 2.99
},
{
"text": "when the Patriot, which was supposed\nto protect against incoming scuds,",
"start": 4879.08,
"duration": 3.7
},
{
"text": "failed to fire a missile.",
"start": 4882.78,
"duration": 3.05
},
{
"text": ">> -When Iraq invaded Kuwait, and America\nlaunched Desert Storm in early 1991,",
"start": 4885.83,
"duration": 5.84
},
{
"text": "Patriot Missile batteries were deployed\nto protect Saudi Arabia and Israel",
"start": 4891.67,
"duration": 4.11
},
{
"text": "from Iraqi Scud missile attacks.",
"start": 4895.78,
"duration": 3.45
},
{
"text": "The Patriot is a US medium-range\nsurface to air system, manufactured",
"start": 4899.23,
"duration": 4.58
},
{
"text": "by the Raytheon company.",
"start": 4903.81,
"duration": 1.96
},
{
"text": ">> -The size of the Patriot interceptor\nitself is about roughly 20 feet long.",
"start": 4905.77,
"duration": 6.57
},
{
"text": "And it weighs about 2,000 pounds.",
"start": 4912.34,
"duration": 2.89
},
{
"text": "And it carries a warhead of about,\nI think it's roughly 150 pounds.",
"start": 4915.23,
"duration": 4.09
},
{
"text": "And the warhead itself is\na high explosive, which",
"start": 4919.32,
"duration": 4.61
},
{
"text": "has fragments around it.",
"start": 4923.93,
"duration": 3.4
},
{
"text": "The casing of the warhead is\ndesigned to act like buckshot.",
"start": 4927.33,
"duration": 4.35
},
{
"text": ">> -The missiles are carried\nfour per container,",
"start": 4931.68,
"duration": 2.43
},
{
"text": "and are transported by a semi trailer.",
"start": 4934.11,
"duration": 3.02
},
{
"text": ">> -The Patriot anti-missile system\ngoes back at least 20 years now.",
"start": 4937.13,
"duration": 7.8
},
{
"text": "It was originally designed\nas an air defense missile",
"start": 4944.93,
"duration": 3.49
},
{
"text": "to shoot down enemy airplanes.",
"start": 4948.42,
"duration": 2.3
},
{
"text": "In the first Gulf War,\nwhen that war came along,",
"start": 4950.72,
"duration": 3.78
},
{
"text": "the Army wanted to use it to\nshoot down scuds, not airplanes.",
"start": 4954.5,
"duration": 5.245
},
{
"text": ">> The Iraqi Air Force was\nnot so much of a problem.",
"start": 4959.745,
"duration": 3.875
},
{
"text": "But the Army was worried about scuds.",
"start": 4963.62,
"duration": 3.05
},
{
"text": "And so they tried to\nupgrade the Patriot.",
"start": 4966.67,
"duration": 3.5
},
{
"text": ">> -Intercepting an enemy\nmissile traveling at mach 5",
"start": 4970.17,
"duration": 2.63
},
{
"text": "was going to be challenging enough.",
"start": 4972.8,
"duration": 3.03
},
{
"text": "But when the Patriot\nwas rushed into service,",
"start": 4975.83,
"duration": 2.66
},
{
"text": "the Army was not aware of an\nIraqi modification that made",
"start": 4978.49,
"duration": 4.37
},
{
"text": "their scuds nearly impossible to hit.",
"start": 4982.86,
"duration": 3.07
},
{
"text": ">> -What happened is the scuds that\nwere coming in were unstable.",
"start": 4985.93,
"duration": 4.81
},
{
"text": "They were wobbling.",
"start": 4990.74,
"duration": 0.952
},
{
"text": "The reason for this was\nthe Iraqis, in order",
"start": 4991.692,
"duration": 3.218
},
{
"text": "to get 600 kilometers\nout of a 300 kilometer",
"start": 4994.91,
"duration": 3.37
},
{
"text": "range missile, took weight\nout of the front warhead.",
"start": 4998.28,
"duration": 3.42
},
{
"text": "They made the warhead lighter.",
"start": 5001.7,
"duration": 1.69
},
{
"text": ">> So now the Patriot is\ntrying to come at the Scud.",
"start": 5003.39,
"duration": 3.94
},
{
"text": "And most of the time, the\noverwhelming majority of the time,",
"start": 5007.33,
"duration": 2.9
},
{
"text": "it would just fly by the Scud.",
"start": 5010.23,
"duration": 2.71
},
{
"text": "Once the Patriot system operators\nrealized the Patriot missed its target,",
"start": 5012.94,
"duration": 4.32
},
{
"text": "they detonated the Patriot's warhead\nto avoid possible casualties if it",
"start": 5017.26,
"duration": 4.43
},
{
"text": "was allowed to fall to the ground.",
"start": 5021.69,
"duration": 2.88
},
{
"text": ">> -That was what most people saw,\nthose big fireballs in the sky,",
"start": 5024.57,
"duration": 4.22
},
{
"text": "and misunderstood as\nintercepts of Scud warheads.",
"start": 5028.79,
"duration": 5.76
},
{
"text": ">> -Although in the night\nskies, Patriots appeared",
"start": 5034.55,
"duration": 2.08
},
{
"text": "to be successfully\ndestroying Scuds, at Dhahran,",
"start": 5036.63,
"duration": 3.74
},
{
"text": "there could be no mistake\nabout its performance.",
"start": 5040.37,
"duration": 2.99
},
{
"text": "There, the Patriot's radar system\nlost track of an incoming Scud,",
"start": 5043.36,
"duration": 4.61
},
{
"text": "and never launched due\nto a software flaw.",
"start": 5047.97,
"duration": 2.751
},
{
"text": "It was the Israelis who first discovered\nthat the longer the system was on,",
"start": 5054.09,
"duration": 4.85
},
{
"text": "the greater the time discrepancy\nbecame, due to a clock embedded",
"start": 5058.94,
"duration": 3.75
},
{
"text": "in the system's computer.",
"start": 5062.69,
"duration": 2.12
},
{
"text": ">> -About two weeks before\nthe tragedy in Dhahran,",
"start": 5064.81,
"duration": 3.4
},
{
"text": "the Israelis reported to\nthe Defense Department",
"start": 5068.21,
"duration": 2.56
},
{
"text": "that the system was losing time.",
"start": 5070.77,
"duration": 1.82
},
{
"text": "After about eight hours or running,\nthey noticed that the system",
"start": 5072.59,
"duration": 2.77
},
{
"text": "was becoming noticeably less accurate.",
"start": 5075.36,
"duration": 2.36
},
{
"text": "The Defense Department responded by\ntelling all of the Patriot batteries",
"start": 5077.72,
"duration": 4.18
},
{
"text": "to not leave the systems\non for a long time.",
"start": 5081.9,
"duration": 3.05
},
{
"text": "They never said what a long time was--\neight hours, 10 hours, 1,000 hours.",
"start": 5084.95,
"duration": 4.21
},
{
"text": "Nobody knew.",
"start": 5089.16,
"duration": 2.2
},
{
"text": ">> -The Patriot battery\nstationed at the barracks",
"start": 5091.36,
"duration": 2.02
},
{
"text": "at Dhahran and its flawed internal\nclock had been on over 100 hours",
"start": 5093.38,
"duration": 4.97
},
{
"text": "on the night of February 25th.",
"start": 5098.35,
"duration": 3.32
},
{
"text": ">> -It tracked time to an accuracy\nof about a tenth of a second.",
"start": 5101.67,
"duration": 4.247
},
{
"text": "Now, a tenth of a second\nis an interesting number,",
"start": 5105.917,
"duration": 2.083
},
{
"text": "because it can't be expressed\nin binary exactly, which",
"start": 5108.0,
"duration": 3.92
},
{
"text": "means it can't be expressed exactly\nin any modern digital computer.",
"start": 5111.92,
"duration": 4.9
},
{
"text": "It's hard to believe.",
"start": 5116.82,
"duration": 1.72
},
{
"text": ">> But use this as an example.",
"start": 5118.54,
"duration": 2.67
},
{
"text": "Let's take the number one third.",
"start": 5121.21,
"duration": 2.33
},
{
"text": "One third cannot be\nexpressed in decimal exactly.",
"start": 5123.54,
"duration": 3.81
},
{
"text": "One third is 0.333\ngoing on for infinity.",
"start": 5127.35,
"duration": 4.73
},
{
"text": ">> There is no way to do that with\nabsolute accuracy in decimal.",
"start": 5132.08,
"duration": 4.4
},
{
"text": "That's exactly the kind of problem\nthat happened in the Patriot.",
"start": 5136.48,
"duration": 3.08
},
{
"text": "The longer the system ran, the\nworse the time error became.",
"start": 5139.56,
"duration": 4.54
},
{
"text": ">> -After 100 hours of operation, the\nerror in time was only about one third",
"start": 5144.1,
"duration": 4.79
},
{
"text": "of a second.",
"start": 5148.89,
"duration": 1.71
},
{
"text": "But in terms of targeting a\nmissile traveling at mach 5,",
"start": 5150.6,
"duration": 3.61
},
{
"text": "it resulted in a tracking\nerror of over 600 meters.",
"start": 5154.21,
"duration": 4.5
},
{
"text": "It would be a fatal error\nfor the soldiers on what",
"start": 5158.71,
"duration": 3.41
},
{
"text": "happened is a Scud launch was\ndetected by early Warning satellites",
"start": 5162.12,
"duration": 6.82
},
{
"text": "and they knew that the Scud was\ncoming in their general direction.",
"start": 5168.94,
"duration": 3.92
},
{
"text": "They didn't know where it was coming.",
"start": 5172.86,
"duration": 2.46
},
{
"text": ">> -It was now up to the radar\ncomponent of the Patriot system",
"start": 5175.32,
"duration": 2.93
},
{
"text": "defending Dhahran to locate and keep\ntrack of the incoming enemy missile.",
"start": 5178.25,
"duration": 4.94
},
{
"text": ">> -The radar was very smart.",
"start": 5183.19,
"duration": 1.419
},
{
"text": "It would actually track\nthe position of the Scud,",
"start": 5184.609,
"duration": 2.041
},
{
"text": "and then predict where it probably\nwould be the next time the radar sent",
"start": 5186.65,
"duration": 3.7
},
{
"text": "a pulse out.",
"start": 5190.35,
"duration": 1.07
},
{
"text": "That was called a range gate.",
"start": 5191.42,
"duration": 1.69
},
{
"text": ">> -Then, once the Patriot\ndecides enough time has",
"start": 5193.11,
"duration": 4.55
},
{
"text": "passed to go back and check the next\nlocation for this detected object,",
"start": 5197.66,
"duration": 4.79
},
{
"text": "it goes back.",
"start": 5202.45,
"duration": 1.15
},
{
"text": "So when it went back to the wrong\nplace, it then sees no object.",
"start": 5203.6,
"duration": 5.05
},
{
"text": "And it decides that there was no\nobject, it was a false detection,",
"start": 5208.65,
"duration": 3.51
},
{
"text": "and drops the track.",
"start": 5212.16,
"duration": 1.77
},
{
"text": ">> -The incoming Scud disappeared\nfrom the radar screen.",
"start": 5213.93,
"duration": 3.1
},
{
"text": "And seconds later, it\nslammed into the barracks.",
"start": 5217.03,
"duration": 3.23
},
{
"text": "The Scud killed 28, and was the last\none fired during the first Gulf War.",
"start": 5220.26,
"duration": 5.89
},
{
"text": ">> Tragically, the updated software\narrived at Dhahran the following day.",
"start": 5226.15,
"duration": 5.81
},
{
"text": "The software flaw had\nbeen fixed, closing",
"start": 5231.96,
"duration": 2.97
},
{
"text": "one chapter in the troubled\nhistory of the Patriot missile.",
"start": 5234.93,
"duration": 4.876
},
{
"text": ">> [VIDEO PLAYBACK]",
"start": 5239.806,
"duration": 0.923
},
{
"text": "DAVID J. MALAN: So this is all to\nsay that these issues of overflow",
"start": 5240.729,
"duration": 2.791
},
{
"text": "and imprecision are all too real.",
"start": 5243.52,
"duration": 2.34
},
{
"text": "So how did we get here?",
"start": 5245.86,
"duration": 1.06
},
{
"text": "We began with just talking about printf.",
"start": 5246.92,
"duration": 1.975
},
{
"text": "Again, this function that\nprints something to the screen,",
"start": 5248.895,
"duration": 2.375
},
{
"text": "and we introduced thereafter\na few other functions",
"start": 5251.27,
"duration": 2.18
},
{
"text": "from the so-called CS50's library.",
"start": 5253.45,
"duration": 1.495
},
{
"text": "And we'll continue to\nsee these in due time.",
"start": 5254.945,
"duration": 1.965
},
{
"text": "And we, particularly, used get string,\nand get int, and now also get float,",
"start": 5256.91,
"duration": 3.85
},
{
"text": "and yet others still will we encounter\nand use ourselves before long.",
"start": 5260.76,
"duration": 3.65
},
{
"text": ">> But on occasion, have\nwe already seen a need",
"start": 5264.41,
"duration": 2.81
},
{
"text": "to store what those functions hand back?",
"start": 5267.22,
"duration": 3.3
},
{
"text": "They hand us back a string,\nor an int, or a float.",
"start": 5270.52,
"duration": 2.4
},
{
"text": "And sometimes we need to put that\nstring, or int, or float, somewhere.",
"start": 5272.92,
"duration": 3.15
},
{
"text": ">> And to store those things, recall just\nlike in Scratch, we have variables.",
"start": 5276.07,
"duration": 4.03
},
{
"text": "But unlike in Scratch,\nin C we have actual types",
"start": 5280.1,
"duration": 3.16
},
{
"text": "of variables-- data\ntypes, more generally--",
"start": 5283.26,
"duration": 2.27
},
{
"text": "among them, a string, an int, a\nfloat, and these others still.",
"start": 5285.53,
"duration": 3.11
},
{
"text": ">> And so when we declare variables in C,\nwe'll have to declare our data types.",
"start": 5288.64,
"duration": 3.681
},
{
"text": "This is not something we'll\nhave to do later in the semester",
"start": 5292.321,
"duration": 2.499
},
{
"text": "as we transition to other languages.",
"start": 5294.82,
"duration": 1.99
},
{
"text": "But for now, we do need\nto a priori in advance,",
"start": 5296.81,
"duration": 2.8
},
{
"text": "explain to the computer what type\nof variable we want it to give us.",
"start": 5299.61,
"duration": 4.76
},
{
"text": ">> Now, meanwhile, to print\nthose kinds of data types,",
"start": 5304.37,
"duration": 2.92
},
{
"text": "we have to tell printf what to expect.",
"start": 5307.29,
"duration": 2.28
},
{
"text": "And we saw percent s for strings,\nand percent i for integers,",
"start": 5309.57,
"duration": 2.88
},
{
"text": "and a few others already.",
"start": 5312.45,
"duration": 1.34
},
{
"text": "And those are simply requirements\nfor the visual presentation",
"start": 5313.79,
"duration": 3.447
},
{
"text": "of that information.",
"start": 5317.237,
"duration": 0.833
},
{
"text": ">> And each of these can actually be\nparametrized or tweaked in some way,",
"start": 5318.07,
"duration": 4.01
},
{
"text": "if you want to further control\nthe type of output that you get.",
"start": 5322.08,
"duration": 3.29
},
{
"text": "And, in fact, it turns out that not only\nis there backslash n for a new line.",
"start": 5325.37,
"duration": 4.234
},
{
"text": "There's something else called backslash\nr for a carriage return, which",
"start": 5329.604,
"duration": 2.916
},
{
"text": "is more akin to an\nold school typewriter,",
"start": 5332.52,
"duration": 1.84
},
{
"text": "and also Windows used for many years.",
"start": 5334.36,
"duration": 3.33
},
{
"text": ">> There's backslash t for tabs.",
"start": 5337.69,
"duration": 2.0
},
{
"text": "Turns out, that if you want to\ndouble quote inside of a string,",
"start": 5339.69,
"duration": 3.48
},
{
"text": "recall that we've used\ndouble quote double",
"start": 5343.17,
"duration": 1.83
},
{
"text": "quote on the left and the right\nends of our strings thus far.",
"start": 5345.0,
"duration": 2.9
},
{
"text": "That would seem to confuse things.",
"start": 5347.9,
"duration": 1.52
},
{
"text": ">> If you want to put a double quote in\nthe middle of a string-- and, indeed,",
"start": 5349.42,
"duration": 3.083
},
{
"text": "it is confusing to see.",
"start": 5352.503,
"duration": 1.167
},
{
"text": "And so you have to escape, so to\nspeak, a double quote with something",
"start": 5353.67,
"duration": 3.45
},
{
"text": "like, literally, backslash double quote.",
"start": 5357.12,
"duration": 1.74
},
{
"text": "And there's a few other still.",
"start": 5358.86,
"duration": 1.37
},
{
"text": "And we'll see more of those\nin actual use before long.",
"start": 5360.23,
"duration": 4.31
},
{
"text": ">> So let's now transition from\ndata, and representation,",
"start": 5364.54,
"duration": 3.39
},
{
"text": "and arithmetic operators, all\nof which gave us some building",
"start": 5367.93,
"duration": 2.89
},
{
"text": "blocks with which to play.",
"start": 5370.82,
"duration": 1.25
},
{
"text": "But now let's actually give\nus the rest of the vocabulary",
"start": 5372.07,
"duration": 2.411
},
{
"text": "that we already had\nlast week with Scratch",
"start": 5374.481,
"duration": 1.749
},
{
"text": "by taking a look at some other\nconstructs in C-- not all of them.",
"start": 5376.23,
"duration": 3.12
},
{
"text": "But the ideas we're\nabout to see really just",
"start": 5379.35,
"duration": 2.33
},
{
"text": "to emphasize the translation from\none language, Scratch, to another, C.",
"start": 5381.68,
"duration": 3.93
},
{
"text": ">> And over time, we'll pick up\nmore tools for our toolkit,",
"start": 5385.61,
"duration": 2.86
},
{
"text": "so to speak, syntactically.",
"start": 5388.47,
"duration": 1.35
},
{
"text": "And, indeed, you'll see that the ideas\nare now rather familiar from last week.",
"start": 5389.82,
"duration": 4.37
},
{
"text": "So let's do this.",
"start": 5394.19,
"duration": 1.01
},
{
"text": ">> Let's go ahead and whip up a program\nthat actually uses some expressions,",
"start": 5395.2,
"duration": 3.67
},
{
"text": "a Boolean expression.",
"start": 5398.87,
"duration": 1.85
},
{
"text": "Let me go ahead here\nand create a new file.",
"start": 5400.72,
"duration": 2.09
},
{
"text": "I'll call this condition.c.",
"start": 5402.81,
"duration": 3.28
},
{
"text": ">> Let me go ahead and\ninclude the CS50 library.",
"start": 5406.09,
"duration": 3.26
},
{
"text": "And let me go ahead and include\nstandard IO.h for our functions,",
"start": 5409.35,
"duration": 3.29
},
{
"text": "and printf, and more respectively.",
"start": 5412.64,
"duration": 2.05
},
{
"text": "Let me give myself that boilerplate of\nint main void, whose explanation we'll",
"start": 5414.69,
"duration": 4.21
},
{
"text": "come back to in the future.",
"start": 5418.9,
"duration": 1.46
},
{
"text": ">> Now let me go ahead and give\nmyself an int via get int.",
"start": 5420.36,
"duration": 3.46
},
{
"text": "Then let me go ahead and do this.",
"start": 5423.82,
"duration": 2.15
},
{
"text": "I want to say if i is less-- let's\ndistinguish between positive, negative,",
"start": 5425.97,
"duration": 4.18
},
{
"text": "or zero values.",
"start": 5430.15,
"duration": 1.11
},
{
"text": ">> So if i is less than zero, let me\njust have this program simply say,",
"start": 5431.26,
"duration": 5.37
},
{
"text": "negative, backslash n, else\nif i is greater than zero.",
"start": 5436.63,
"duration": 5.74
},
{
"text": "Now I'm, of course, going to say\nprintf positive, backslash n.",
"start": 5442.37,
"duration": 4.66
},
{
"text": "And then else if-- I could do this.",
"start": 5447.03,
"duration": 3.66
},
{
"text": ">> I could do if i equals 0.",
"start": 5450.69,
"duration": 2.72
},
{
"text": "But I'd be making at\nleast one mistake already.",
"start": 5453.41,
"duration": 2.43
},
{
"text": "Recall that the equal sign is\nnot equal, as we humans know it.",
"start": 5455.84,
"duration": 3.64
},
{
"text": ">> But it's the assignment operator.",
"start": 5459.48,
"duration": 1.53
},
{
"text": "And we don't want to take 0 on the\nright and put it in i on the left.",
"start": 5461.01,
"duration": 4.63
},
{
"text": "So to avoid this confusion, or\nperhaps misuse of the equals sign,",
"start": 5465.64,
"duration": 6.17
},
{
"text": "humans decided some years ago\nthat in many programming languages",
"start": 5471.81,
"duration": 2.93
},
{
"text": "when you want to check for equality\nbetween the left and the right,",
"start": 5474.74,
"duration": 3.26
},
{
"text": "you actually use equals equals.",
"start": 5478.0,
"duration": 1.635
},
{
"text": "So you hit the equals sign twice.",
"start": 5479.635,
"duration": 1.375
},
{
"text": "When you want to assign from right\nto left, you use a single equal sign.",
"start": 5481.01,
"duration": 4.59
},
{
"text": "So we could do this-- else\nif i equals equals zero.",
"start": 5485.6,
"duration": 3.76
},
{
"text": ">> I could then go and\nopen my curly braces,",
"start": 5489.36,
"duration": 2.35
},
{
"text": "and say, printf 0, backslash n, done.",
"start": 5491.71,
"duration": 4.377
},
{
"text": "But remember how these\nforks in the road can work.",
"start": 5496.087,
"duration": 2.083
},
{
"text": "And, really, just think about the logic.",
"start": 5498.17,
"duration": 1.666
},
{
"text": "i is a number.",
"start": 5499.836,
"duration": 1.674
},
{
"text": "It's an integer, specifically.",
"start": 5501.51,
"duration": 1.81
},
{
"text": "And that means it's going to be less\nthan 0, or greater than 0, or 0.",
"start": 5503.32,
"duration": 5.28
},
{
"text": "So there is kind of this\nimplied default case.",
"start": 5508.6,
"duration": 3.0
},
{
"text": ">> And so we could, just like\nScratch, dispense with the else if,",
"start": 5511.6,
"duration": 3.32
},
{
"text": "and just say else.",
"start": 5514.92,
"duration": 0.827
},
{
"text": "Logically, if you the\nprogrammer know there's only",
"start": 5515.747,
"duration": 2.083
},
{
"text": "three buckets into which a\nscenario can fall-- the first,",
"start": 5517.83,
"duration": 3.805
},
{
"text": "the second, or the third\nin this case-- don't",
"start": 5521.635,
"duration": 1.875
},
{
"text": "bother adding the additional precision\nand the additional logic there.",
"start": 5523.51,
"duration": 3.59
},
{
"text": "Just go ahead with the\ndefault case here of else.",
"start": 5527.1,
"duration": 2.59
},
{
"text": ">> Now, let's go ahead\nafter saving this, make",
"start": 5529.69,
"duration": 2.26
},
{
"text": "conditions dot slash conditions--\nnot a great user interface,",
"start": 5531.95,
"duration": 3.81
},
{
"text": "because I'm not prompting the\nuser, as I mentioned earlier.",
"start": 5535.76,
"duration": 3.154
},
{
"text": "But that's fine.",
"start": 5538.914,
"duration": 0.666
},
{
"text": "We'll keep it simple.",
"start": 5539.58,
"duration": 0.874
},
{
"text": "Let's try the number 42.",
"start": 5540.454,
"duration": 1.436
},
{
"text": "And that's positive.",
"start": 5541.89,
"duration": 1.35
},
{
"text": "Let's try the number\nnegative 42, negative.",
"start": 5543.24,
"duration": 2.88
},
{
"text": ">> Let's try the value 0.",
"start": 5546.12,
"duration": 2.124
},
{
"text": "And, indeed, it works.",
"start": 5548.244,
"duration": 0.916
},
{
"text": "Now, you'll see with problems before\nlong, testing things three times,",
"start": 5549.16,
"duration": 4.74
},
{
"text": "probably not sufficient.",
"start": 5553.9,
"duration": 1.08
},
{
"text": "You probably want to test some\nbigger numbers, some smaller",
"start": 5554.98,
"duration": 2.458
},
{
"text": "numbers, some corner cases, as\nwe'll come to describe them.",
"start": 5557.438,
"duration": 3.082
},
{
"text": ">> But for now, this is a\npretty simple program.",
"start": 5560.52,
"duration": 1.98
},
{
"text": "And I'm pretty sure, logically,\nthat it falls into three cases.",
"start": 5562.5,
"duration": 2.66
},
{
"text": "And, indeed, even though we just\nfocused on the potential downsides",
"start": 5565.16,
"duration": 4.2
},
{
"text": "of imprecision and overflow, in\nreality where many of CS50's problems,",
"start": 5569.36,
"duration": 4.12
},
{
"text": "we are not going to worry\nabout, all the time,",
"start": 5573.48,
"duration": 2.52
},
{
"text": "those issues of overflow and\nimprecision, because, in fact, in C,",
"start": 5576.0,
"duration": 3.05
},
{
"text": "it's actually not all that\neasy to avoid those things.",
"start": 5579.05,
"duration": 2.839
},
{
"text": "If you want to count up\nbigger, and bigger, and bigger,",
"start": 5581.889,
"duration": 2.291
},
{
"text": "it turns out there are techniques you\ncan use, often involving things called",
"start": 5584.18,
"duration": 3.33
},
{
"text": "libraries, collections of code, that\nother people wrote that you can use,",
"start": 5587.51,
"duration": 3.73
},
{
"text": "and other languages like\nJava and others, actually",
"start": 5591.24,
"duration": 2.67
},
{
"text": "make it a lot easier\nto count even higher.",
"start": 5593.91,
"duration": 1.89
},
{
"text": "So it really is some of these dangers\na function of the language you use.",
"start": 5595.8,
"duration": 4.01
},
{
"text": "And in the coming weeks, we'll\nsee how dangerous C really",
"start": 5599.81,
"duration": 2.9
},
{
"text": "can be if you don't use it properly.",
"start": 5602.71,
"duration": 2.24
},
{
"text": "But from there, and with\nPython, and JavaScript, will",
"start": 5604.95,
"duration": 2.66
},
{
"text": "we layer on some additional protections,\nand run fewer of those risks.",
"start": 5607.61,
"duration": 5.01
},
{
"text": ">> So let's make a little more\ninteresting logic in our program.",
"start": 5612.62,
"duration": 3.2
},
{
"text": "So let me go ahead and create\na program called Logical",
"start": 5615.82,
"duration": 3.29
},
{
"text": "just so I can play with some\nactual logic, logical.c.",
"start": 5619.11,
"duration": 4.694
},
{
"text": "I'll just copy and paste some\ncode from earlier so I get back",
"start": 5623.804,
"duration": 3.066
},
{
"text": "to this nice starting point.",
"start": 5626.87,
"duration": 3.08
},
{
"text": ">> Let me this time do char C. I'm\ngoing to give it a name of C",
"start": 5629.95,
"duration": 4.03
},
{
"text": "just because it's conventional,\nget a character from the user.",
"start": 5633.98,
"duration": 4.53
},
{
"text": "And let's pretend like\nI'm implementing part",
"start": 5638.51,
"duration": 2.22
},
{
"text": "of that Rm program, the remove\nprogram before that prompted the user",
"start": 5640.73,
"duration": 3.4
},
{
"text": "to remove a file.",
"start": 5644.13,
"duration": 1.27
},
{
"text": "How could we do this?",
"start": 5645.4,
"duration": 1.35
},
{
"text": ">> I want to say, if C equals\nequals, quote unquote,",
"start": 5646.75,
"duration": 4.34
},
{
"text": "y, then I'm going to assume\nthat the user has chosen yes.",
"start": 5651.09,
"duration": 5.214
},
{
"text": "I'm just going to print yes.",
"start": 5656.304,
"duration": 1.166
},
{
"text": "If it were actually writing\nthe removal program,",
"start": 5657.47,
"duration": 1.97
},
{
"text": "we could remove the file\nwith more lines of code.",
"start": 5659.44,
"duration": 1.98
},
{
"text": "But we'll keep it simple.",
"start": 5661.42,
"duration": 1.041
},
{
"text": ">> Else if c equals equals n--\nand now here, I'm going to say,",
"start": 5665.95,
"duration": 5.3
},
{
"text": "the user must have meant no.",
"start": 5671.25,
"duration": 1.73
},
{
"text": "And then else, you know what?",
"start": 5672.98,
"duration": 1.38
},
{
"text": "I don't know what else\nthe user is going to type.",
"start": 5674.36,
"duration": 1.84
},
{
"text": "So I'm just going to say that\nthat is an error, whatever",
"start": 5676.2,
"duration": 2.333
},
{
"text": "he or she actually typed.",
"start": 5678.533,
"duration": 1.537
},
{
"text": ">> So what's going on here?",
"start": 5680.07,
"duration": 1.11
},
{
"text": "There is a fundamental difference\nversus what I've done in the past.",
"start": 5681.18,
"duration": 3.35
},
{
"text": "Double quotes, double quotes, double\nquotes, and, yet, single quotes,",
"start": 5684.53,
"duration": 4.77
},
{
"text": "single quotes.",
"start": 5689.3,
"duration": 0.87
},
{
"text": "It turns out in C, that when\nyou want to write a string,",
"start": 5690.17,
"duration": 2.69
},
{
"text": "you do use double quotes, just as we've\nbeen using all this time with printf.",
"start": 5692.86,
"duration": 3.82
},
{
"text": ">> But if you want to deal with just a\nsingle character, a so-called char,",
"start": 5696.68,
"duration": 5.35
},
{
"text": "then you actually use single quotes.",
"start": 5702.03,
"duration": 1.75
},
{
"text": "Those of you who've programmed\nbefore, you might not have",
"start": 5703.78,
"duration": 1.67
},
{
"text": "had to worry about this\ndistinction in certain languages.",
"start": 5705.45,
"duration": 2.4
},
{
"text": "In C, it does matter.",
"start": 5707.85,
"duration": 1.6
},
{
"text": "And so when I get a char and I want\nto compare that char using equals",
"start": 5709.45,
"duration": 3.11
},
{
"text": "equals to some letter like y or n, I do,\nindeed, need to have the single quotes.",
"start": 5712.56,
"duration": 5.79
},
{
"text": ">> Now, let's go ahead and do this.",
"start": 5718.35,
"duration": 1.42
},
{
"text": "Let's go ahead and do make\nlogical dot slash logical.",
"start": 5719.77,
"duration": 6.41
},
{
"text": "And now I'm being prompted.",
"start": 5726.18,
"duration": 1.125
},
{
"text": "So, presumably, a better user experience\nwould actually tell me what to do here.",
"start": 5727.305,
"duration": 3.333
},
{
"text": "But I'm going to just blindly\nsay y for yes, OK, nice.",
"start": 5730.638,
"duration": 2.392
},
{
"text": ">> Let's run it again, n for no, nice.",
"start": 5733.03,
"duration": 2.75
},
{
"text": "Suppose like certain people I know,\nmy caps lock key is on all too often.",
"start": 5735.78,
"duration": 3.83
},
{
"text": "So I do capital Y, enter, error.",
"start": 5739.61,
"duration": 4.13
},
{
"text": "OK, it's not exactly what I'm expecting.",
"start": 5743.74,
"duration": 2.39
},
{
"text": "Indeed, the computer\nis doing literally what",
"start": 5746.13,
"duration": 2.04
},
{
"text": "I told it to do-- check for\nlowercase y and lowercase n.",
"start": 5748.17,
"duration": 3.624
},
{
"text": "This doesn't feel like good\nuser experience, though.",
"start": 5751.794,
"duration": 2.166
},
{
"text": "Let me ask for and accept\neither lower case or upper case.",
"start": 5753.96,
"duration": 5.05
},
{
"text": "So it turns out, you might want\nto say something like in Scratch,",
"start": 5759.01,
"duration": 3.08
},
{
"text": "like literally or C equals\nequals capital single quoted y.",
"start": 5762.09,
"duration": 6.06
},
{
"text": "Turns out, C does not have\nthis literal keyword or.",
"start": 5768.15,
"duration": 3.25
},
{
"text": ">> But it does have two vertical bars.",
"start": 5771.4,
"duration": 1.48
},
{
"text": "You have to hold Shift usually,\nif you're using a US keyboard,",
"start": 5772.88,
"duration": 2.583
},
{
"text": "and hit the vertical bar\nkey above your return key.",
"start": 5775.463,
"duration": 3.447
},
{
"text": "But this vertical bar\nvertical bar means or.",
"start": 5778.91,
"duration": 3.5
},
{
"text": ">> If, by contrast, we wanted\nto say and, like in Scratch,",
"start": 5782.41,
"duration": 3.81
},
{
"text": "we could do ampersand ampersand.",
"start": 5786.22,
"duration": 1.96
},
{
"text": "That makes no logical sense here,\nbecause a human could not possibly",
"start": 5788.18,
"duration": 3.15
},
{
"text": "have typed both y and lowercase y\nand capital Y as the same character.",
"start": 5791.33,
"duration": 5.78
},
{
"text": "So or is what we intend here.",
"start": 5797.11,
"duration": 2.36
},
{
"text": ">> So if I do this in both places, or c\nequals equals capital N, now rerun,",
"start": 5799.47,
"duration": 6.81
},
{
"text": "make logical, rerun logical.",
"start": 5806.28,
"duration": 3.11
},
{
"text": "Now, I can type y.",
"start": 5809.39,
"duration": 1.81
},
{
"text": "And I can do it again with\ncapital Y, or capital N.",
"start": 5811.2,
"duration": 2.72
},
{
"text": "And I could add in additional\ncombinations still.",
"start": 5813.92,
"duration": 2.71
},
{
"text": ">> So this is a logical\nprogram insofar as now",
"start": 5816.63,
"duration": 2.18
},
{
"text": "I'm checking logically for\nthis value or this value.",
"start": 5818.81,
"duration": 3.13
},
{
"text": "And I don't have to, necessarily,\ncome up with two more ifs or else ifs.",
"start": 5821.94,
"duration": 4.48
},
{
"text": "I can actually combine some of the\nrelated logic together in this way.",
"start": 5826.42,
"duration": 3.54
},
{
"text": "So this would be better\ndesigned than simply",
"start": 5829.96,
"duration": 1.99
},
{
"text": "saying, if C equals lower case y,\nprint yes, else if c equals capital Y,",
"start": 5831.95,
"duration": 5.54
},
{
"text": "print yes, else if c equals\nlower-- in other words,",
"start": 5837.49,
"duration": 2.584
},
{
"text": "you don't have to have\nmore and more branches.",
"start": 5840.074,
"duration": 1.916
},
{
"text": "You can combine some of the equivalent\nbranches logically, as in this way.",
"start": 5841.99,
"duration": 6.85
},
{
"text": ">> So let's take a look at just one\nfinal ingredient, one final construct,",
"start": 5848.84,
"duration": 5.31
},
{
"text": "that C allows.",
"start": 5854.15,
"duration": 0.697
},
{
"text": "And we'll come back in the\nfuture to others still.",
"start": 5854.847,
"duration": 2.083
},
{
"text": "And then we'll conclude by looking\nat not the correctness of code--",
"start": 5856.93,
"duration": 4.47
},
{
"text": "getting code to work-- but the design\nof code, and plant those seeds early on.",
"start": 5861.4,
"duration": 4.67
},
{
"text": ">> So let me go ahead and\nopen up a new file here.",
"start": 5866.07,
"duration": 5.267
},
{
"text": "You know what?",
"start": 5871.337,
"duration": 0.583
},
{
"text": "I'm going to re-implement\nthat same program,",
"start": 5871.92,
"duration": 2.53
},
{
"text": "but using a different construct.",
"start": 5874.45,
"duration": 1.49
},
{
"text": ">> So let me quickly give myself\naccess to include CS50.h",
"start": 5875.94,
"duration": 4.17
},
{
"text": "for the CS50 library,\nstandard Io.h for printf.",
"start": 5880.11,
"duration": 4.04
},
{
"text": "Give me my int main void.",
"start": 5884.15,
"duration": 2.36
},
{
"text": "And then over here, let\nme go ahead and do this.",
"start": 5886.51,
"duration": 2.8
},
{
"text": ">> Char c gets get char, just like before.",
"start": 5889.31,
"duration": 2.7
},
{
"text": "And I'm going to use a new construct\nnow-- switch, on what character?",
"start": 5892.01,
"duration": 4.76
},
{
"text": "So switch is kind of like\nswitching a train tracks.",
"start": 5896.77,
"duration": 3.05
},
{
"text": "Or, really, it is kind of\nan if else, if else if,",
"start": 5899.82,
"duration": 2.25
},
{
"text": "but written somewhat differently.",
"start": 5902.07,
"duration": 1.91
},
{
"text": ">> A switch looks like this.",
"start": 5903.98,
"duration": 1.51
},
{
"text": "You have switch, and then what\ncharacter or number you want to look at,",
"start": 5905.49,
"duration": 3.57
},
{
"text": "then some curly braces like in\nScratch, just say do this stuff.",
"start": 5909.06,
"duration": 2.94
},
{
"text": "And then you have different cases.",
"start": 5912.0,
"duration": 1.48
},
{
"text": ">> You don't use if and else.",
"start": 5913.48,
"duration": 1.35
},
{
"text": "You literally use the word case.",
"start": 5914.83,
"duration": 2.22
},
{
"text": "And you would say something like this.",
"start": 5917.05,
"duration": 1.74
},
{
"text": ">> So in the case of a lowercase y,\nor in the case of a capital Y,",
"start": 5918.79,
"duration": 5.03
},
{
"text": "go ahead and print out yes.",
"start": 5923.82,
"duration": 3.53
},
{
"text": "And then break out of the switch.",
"start": 5927.35,
"duration": 1.67
},
{
"text": "That's it.",
"start": 5929.02,
"duration": 0.56
},
{
"text": "We're done.",
"start": 5929.58,
"duration": 1.3
},
{
"text": ">> Else if, so to speak,\nlower case n, or capital N,",
"start": 5930.88,
"duration": 6.39
},
{
"text": "then go ahead and print\nout no, and then break.",
"start": 5937.27,
"duration": 5.29
},
{
"text": "Else-- and this kind of is the\ndefault case indeed-- printf error--",
"start": 5942.56,
"duration": 5.462
},
{
"text": "and just for good measure, though\nlogically this break is not necessary",
"start": 5948.022,
"duration": 2.958
},
{
"text": "because we're at the end\nof the switch anyway,",
"start": 5950.98,
"duration": 1.916
},
{
"text": "I'm now breaking out of the switch.",
"start": 5952.896,
"duration": 1.624
},
{
"text": "So this looks a little different.",
"start": 5954.52,
"duration": 1.76
},
{
"text": ">> But, logically, it's\nactually equivalent.",
"start": 5956.28,
"duration": 1.992
},
{
"text": "And why would you use\none over the other?",
"start": 5958.272,
"duration": 1.708
},
{
"text": "Sometimes, just personal preference,\nsometimes the aesthetics,",
"start": 5959.98,
"duration": 3.24
},
{
"text": "if I glance at this\nnow, there's something",
"start": 5963.22,
"duration": 2.2
},
{
"text": "to be said for the\nreadability of this code.",
"start": 5965.42,
"duration": 2.09
},
{
"text": "I mean, never mind the fact that this\ncode is new to many of us in the room.",
"start": 5967.51,
"duration": 3.18
},
{
"text": ">> But it just kind of is pretty.",
"start": 5970.69,
"duration": 2.825
},
{
"text": "You see lowercase y, capital Y,\nlower case n, capital N default,",
"start": 5973.515,
"duration": 4.245
},
{
"text": "it just kind of jumps\nout at you in a way",
"start": 5977.76,
"duration": 2.39
},
{
"text": "that, arguably, maybe\nthe previous example",
"start": 5980.15,
"duration": 2.05
},
{
"text": "with the ifs, and the vertical bars,\nand the else ifs, might not have.",
"start": 5982.2,
"duration": 3.58
},
{
"text": "So this is really a matter of personal\nchoice, really, or readability,",
"start": 5985.78,
"duration": 5.82
},
{
"text": "of the code.",
"start": 5991.6,
"duration": 0.76
},
{
"text": ">> But in terms of functionality, let me\ngo ahead and make a switch, dot slash",
"start": 5992.36,
"duration": 5.87
},
{
"text": "switch, and now type in lowercase y,\ncapital Y, lowercase n, capital N,",
"start": 5998.23,
"duration": 7.6
},
{
"text": "David, retry because that's\nnot a single character.",
"start": 6005.83,
"duration": 3.42
},
{
"text": "Let's do x, error, as expected.",
"start": 6009.25,
"duration": 2.8
},
{
"text": "And, logically-- and this is something\nI would encourage in general-- even",
"start": 6012.05,
"duration": 3.59
},
{
"text": "though we're only scratching the\nsurface of some of these features.",
"start": 6015.64,
"duration": 2.15
},
{
"text": ">> And it might not be obvious when you\nyourself sit down at the keyboard,",
"start": 6017.79,
"duration": 2.77
},
{
"text": "how does this work?",
"start": 6020.56,
"duration": 0.81
},
{
"text": "What would this do?",
"start": 6021.37,
"duration": 0.87
},
{
"text": "The beautiful thing about having\na laptop, or desktop, or access",
"start": 6022.24,
"duration": 3.39
},
{
"text": "to a computer with a compiler,\nand with a code editor like this,",
"start": 6025.63,
"duration": 3.66
},
{
"text": "is you can almost always answer these\nquestions for yourself just by trying.",
"start": 6029.29,
"duration": 3.7
},
{
"text": ">> For instance, if the rhetorical\nquestion at hand were,",
"start": 6032.99,
"duration": 3.58
},
{
"text": "what happens if you forget\nyour break statements?",
"start": 6036.57,
"duration": 2.97
},
{
"text": "Which is actually a\nvery common thing to do,",
"start": 6039.54,
"duration": 1.86
},
{
"text": "because it doesn't look\nlike you really need them.",
"start": 6041.4,
"duration": 2.14
},
{
"text": "They don't really complete your\nthought like a parenthesis or a curly",
"start": 6043.54,
"duration": 3.25
},
{
"text": "brace does.",
"start": 6046.79,
"duration": 0.924
},
{
"text": "Let's go ahead and\nrecompile the code and see.",
"start": 6047.714,
"duration": 1.916
},
{
"text": "So make switch, dot slash switch.",
"start": 6049.63,
"duration": 4.06
},
{
"text": "Let's type in lower case\ny, the top case, Enter.",
"start": 6053.69,
"duration": 2.745
},
{
"text": "So I typed y.",
"start": 6059.39,
"duration": 1.31
},
{
"text": ">> The program said yes, no, error,\nas though it was changing its mind.",
"start": 6060.7,
"duration": 3.72
},
{
"text": "But it kind of was, because what happens\nwith a switch is the first case that",
"start": 6064.42,
"duration": 4.86
},
{
"text": "match essentially means, hey computer,\nexecute all of the code beneath it.",
"start": 6069.28,
"duration": 4.619
},
{
"text": "And if you don't say break, or\ndon't say break, or don't say break,",
"start": 6073.899,
"duration": 2.791
},
{
"text": "the computer is going to blow\nthrough all of those lines",
"start": 6076.69,
"duration": 2.85
},
{
"text": "and execute all of them until\nit gets to that curly brace.",
"start": 6079.54,
"duration": 3.239
},
{
"text": "So the brakes are, indeed, necessary.",
"start": 6082.779,
"duration": 1.541
},
{
"text": "But a takeaway here is, when\nin doubt, try something.",
"start": 6084.32,
"duration": 2.8
},
{
"text": "Maybe save your code first,\nor save it in an extra file",
"start": 6087.12,
"duration": 2.39
},
{
"text": "if you're really worried about\nmessing up and having to recover",
"start": 6089.51,
"duration": 3.42
},
{
"text": "the work that you know is working.",
"start": 6092.93,
"duration": 1.5
},
{
"text": ">> But try things.",
"start": 6094.43,
"duration": 0.98
},
{
"text": "And don't be as afraid, perhaps,\nof what the computer might do,",
"start": 6095.41,
"duration": 2.664
},
{
"text": "or that you might break something.",
"start": 6098.074,
"duration": 1.416
},
{
"text": "You can always revert back\nto some earlier version.",
"start": 6099.49,
"duration": 3.3
},
{
"text": ">> So let's end by looking\nat the design of code.",
"start": 6102.79,
"duration": 2.85
},
{
"text": "We have this ability now to write\nconditions, and write loops,",
"start": 6105.64,
"duration": 3.38
},
{
"text": "and variables, and call functions.",
"start": 6109.02,
"duration": 1.83
},
{
"text": "So, frankly, we're kind of back at\nwhere we were a week ago with Scratch,",
"start": 6110.85,
"duration": 3.74
},
{
"text": "albeit with a less compelling textual\nenvironment than Scratch allows.",
"start": 6114.59,
"duration": 5.53
},
{
"text": ">> But notice how quickly we've acquired\nthat vocabulary, even if it's",
"start": 6120.12,
"duration": 3.87
},
{
"text": "going to take a little while to sink in,\nso that we can now use this vocabulary",
"start": 6123.99,
"duration": 3.58
},
{
"text": "to write more interesting programs.",
"start": 6127.57,
"duration": 2.75
},
{
"text": "And let's take a baby step\ntoward that, as follows.",
"start": 6130.32,
"duration": 2.62
},
{
"text": "Let me go ahead and\ncreate a new file here.",
"start": 6132.94,
"duration": 1.95
},
{
"text": ">> I'm going to call this\nprototype.c, and introduce",
"start": 6134.89,
"duration": 2.86
},
{
"text": "for the first time, the ability\nto make your own functions.",
"start": 6137.75,
"duration": 3.204
},
{
"text": "Some of you might have\ndone this with Scratch,",
"start": 6140.954,
"duration": 1.916
},
{
"text": "whereby you can create your\nown custom blocks in Scratch,",
"start": 6142.87,
"duration": 2.56
},
{
"text": "and then drag them into place\nwherever you'd like in C.",
"start": 6145.43,
"duration": 2.462
},
{
"text": "And in most programming\nlanguages, you can do exactly",
"start": 6147.892,
"duration": 2.208
},
{
"text": "that-- make your own functions,\nif they don't already exist.",
"start": 6150.1,
"duration": 3.48
},
{
"text": ">> So, for instance, let me go ahead\nand include CS50.h, and include",
"start": 6153.58,
"duration": 5.08
},
{
"text": "standard IO.h, int main void.",
"start": 6158.66,
"duration": 4.45
},
{
"text": "And now we have a\nplaceholder ready to go.",
"start": 6163.11,
"duration": 2.91
},
{
"text": "I keep printing things\nlike people's names today.",
"start": 6166.02,
"duration": 2.53
},
{
"text": "And that feels like--\nwouldn't be nice if there",
"start": 6168.55,
"duration": 3.36
},
{
"text": "were a function called print name?",
"start": 6171.91,
"duration": 2.026
},
{
"text": "I don't have to use printf.",
"start": 6173.936,
"duration": 1.124
},
{
"text": "I don't have to remember\nall the format codes.",
"start": 6175.06,
"duration": 1.916
},
{
"text": "Why don't I, or why\ndidn't someone before me,",
"start": 6176.976,
"duration": 3.074
},
{
"text": "create a function called print\nname, that given some name,",
"start": 6180.05,
"duration": 2.93
},
{
"text": "simply prints it out?",
"start": 6182.98,
"duration": 1.0
},
{
"text": ">> In other words, if I say, hey,\ncomputer, give me a string",
"start": 6183.98,
"duration": 4.72
},
{
"text": "by asking the user for such,\nvia CS50's get string function.",
"start": 6188.7,
"duration": 3.17
},
{
"text": "Hey, computer, put that string in\nthe variable in the left hand side,",
"start": 6191.87,
"duration": 3.22
},
{
"text": "and call it s.",
"start": 6195.09,
"duration": 1.06
},
{
"text": "And then, hey computer, go ahead\nand print that person's name, done.",
"start": 6196.15,
"duration": 6.0
},
{
"text": ">> Now, it would be nice, because\nthis program, aptly named,",
"start": 6202.15,
"duration": 4.09
},
{
"text": "tells me what it's supposed to do\nby way of those function's names.",
"start": 6206.24,
"duration": 2.93
},
{
"text": "Let me go and make prototype, Enter.",
"start": 6209.17,
"duration": 3.76
},
{
"text": "And, unfortunately,\nthis isn't going to fly.",
"start": 6212.93,
"duration": 2.0
},
{
"text": ">> Prototype.c, line 7, character\n5, error, implicit declaration",
"start": 6214.93,
"duration": 4.5
},
{
"text": "of function print name\nis invalid in C99, C99",
"start": 6219.43,
"duration": 3.53
},
{
"text": "meaning a version of C\nthat came out in 1999.",
"start": 6222.96,
"duration": 2.17
},
{
"text": "That's all.",
"start": 6225.13,
"duration": 0.6
},
{
"text": ">> So I don't know what\nall of this means yet.",
"start": 6225.73,
"duration": 3.05
},
{
"text": "But I do recognize error in red.",
"start": 6228.78,
"duration": 2.03
},
{
"text": "That's pretty obvious.",
"start": 6230.81,
"duration": 0.96
},
{
"text": ">> And it seems that with\nthe green character here,",
"start": 6231.77,
"duration": 1.999
},
{
"text": "the issue is with print name, open\nparen s, close paren, semi-colon.",
"start": 6233.769,
"duration": 3.751
},
{
"text": "But implicit declaration of\nfunction we did see briefly earlier.",
"start": 6237.52,
"duration": 4.28
},
{
"text": "This means, simply, that Clang\ndoes not know what I mean.",
"start": 6241.8,
"duration": 3.08
},
{
"text": ">> I've used a vocabulary word that it's\nnever seen or been taught before.",
"start": 6244.88,
"duration": 4.12
},
{
"text": "And so I need to teach it\nwhat this function means.",
"start": 6249.0,
"duration": 2.95
},
{
"text": "So I'm going to go ahead and do that.",
"start": 6251.95,
"duration": 1.64
},
{
"text": ">> I'm going to go ahead and implement\nmy own function called Print Name.",
"start": 6253.59,
"duration": 4.38
},
{
"text": "And I'm going to say, as follows, that\nit does this, printf, hello, percent",
"start": 6257.97,
"duration": 6.75
},
{
"text": "s, backslash n, name, semi-colon.",
"start": 6264.72,
"duration": 3.04
},
{
"text": "So what did I just do?",
"start": 6267.76,
"duration": 1.49
},
{
"text": ">> So it turns out, to\nimplement your own function,",
"start": 6269.25,
"duration": 2.075
},
{
"text": "we kind of borrow some of\nthe same structure as main",
"start": 6271.325,
"duration": 2.52
},
{
"text": "that we've just been\ntaken for granted, and I",
"start": 6273.845,
"duration": 1.875
},
{
"text": "know just copying and\npasting pretty much what",
"start": 6275.72,
"duration": 2.01
},
{
"text": "I've been writing in the past.",
"start": 6277.73,
"duration": 1.44
},
{
"text": "But notice the pattern here.",
"start": 6279.17,
"duration": 1.4
},
{
"text": "Int, Main, Void, we'll tease apart\nbefore long what that actually means.",
"start": 6280.57,
"duration": 3.18
},
{
"text": ">> But for today, just\nnotice the parallelism.",
"start": 6283.75,
"duration": 2.41
},
{
"text": "Void, print name,\nstring name, so there's",
"start": 6286.16,
"duration": 2.05
},
{
"text": "a purple keyword, which\nwe're going to start",
"start": 6288.21,
"duration": 2.1
},
{
"text": "calling a return type, the name of\nthe function, and then the input.",
"start": 6290.31,
"duration": 3.757
},
{
"text": "So, actually, we can distill\nthis kind of like last week",
"start": 6294.067,
"duration": 2.333
},
{
"text": "as, this is the name or the\nalgorithm of the code we're",
"start": 6296.4,
"duration": 2.63
},
{
"text": "going to write-- the\nalgorithm underlying",
"start": 6299.03,
"duration": 1.731
},
{
"text": "the code we're going to write.",
"start": 6300.761,
"duration": 1.249
},
{
"text": ">> This is its input.",
"start": 6302.01,
"duration": 1.17
},
{
"text": "This is its output.",
"start": 6303.18,
"duration": 1.49
},
{
"text": "This function, print name, is\ndesigned to take a string called name,",
"start": 6304.67,
"duration": 4.06
},
{
"text": "or whatever, as input, and then void.",
"start": 6308.73,
"duration": 2.62
},
{
"text": "It doesn't return anything,\nlike get string or get int does.",
"start": 6311.35,
"duration": 2.554
},
{
"text": "So it's going to hand me something back.",
"start": 6313.904,
"duration": 1.666
},
{
"text": "It's just going to have a\nside effect, so to speak,",
"start": 6315.57,
"duration": 2.39
},
{
"text": "of printing a person's name.",
"start": 6317.96,
"duration": 1.61
},
{
"text": "So notice, line 7, I\ncan call print name.",
"start": 6319.57,
"duration": 2.69
},
{
"text": "Line 10, I can define\nor implement print name.",
"start": 6322.26,
"duration": 3.66
},
{
"text": "But, unfortunately, that's not enough.",
"start": 6325.92,
"duration": 2.53
},
{
"text": ">> Let me go ahead and\nrecompile this after saving.",
"start": 6328.45,
"duration": 2.78
},
{
"text": "Whoa, now, I've made it\nworse, it would seem.",
"start": 6331.23,
"duration": 2.68
},
{
"text": "So implicit declaration of\nfunction print name is invalid.",
"start": 6333.91,
"duration": 3.117
},
{
"text": "And, again, there's more errors.",
"start": 6337.027,
"duration": 1.333
},
{
"text": "But as I cautioned earlier, even\nif you get overwhelmed with,",
"start": 6338.36,
"duration": 3.07
},
{
"text": "or a little sad to see so many\nerrors, focus only on the first",
"start": 6341.43,
"duration": 3.42
},
{
"text": "initially, because it might just\nhave had a cascading effect.",
"start": 6344.85,
"duration": 2.65
},
{
"text": "So C, or Clang more specifically,\nstill does not recognize print name.",
"start": 6347.5,
"duration": 4.47
},
{
"text": ">> And that's because Clang,\nby design, is kind of dumb.",
"start": 6351.97,
"duration": 2.61
},
{
"text": "It only does what you tell it to do.",
"start": 6354.58,
"duration": 1.7
},
{
"text": "And it only does so in the order\nin which you tell it to do.",
"start": 6356.28,
"duration": 4.67
},
{
"text": ">> So I have defined main on line four,\nlike we've been doing pretty often.",
"start": 6360.95,
"duration": 4.32
},
{
"text": "I've defined print name on line 10.",
"start": 6365.27,
"duration": 2.71
},
{
"text": "But I'm trying to use\nprint name on line seven.",
"start": 6367.98,
"duration": 3.813
},
{
"text": ">> It's too soon, doesn't exist yet.",
"start": 6371.793,
"duration": 1.877
},
{
"text": "So I could be clever, and be like,\nOK, so let's just play along,",
"start": 6373.67,
"duration": 5.48
},
{
"text": "and move print name up\nhere, and re-compile.",
"start": 6379.15,
"duration": 4.53
},
{
"text": "Oh my God.",
"start": 6383.68,
"duration": 0.87
},
{
"text": "It worked.",
"start": 6384.55,
"duration": 0.71
},
{
"text": "It was as simple as that.",
"start": 6385.26,
"duration": 1.41
},
{
"text": ">> But the logic is exactly that.",
"start": 6386.67,
"duration": 1.45
},
{
"text": "You have to teach Clang what it\nis by defining the function first.",
"start": 6388.12,
"duration": 2.75
},
{
"text": "Then you can use it.",
"start": 6390.87,
"duration": 1.05
},
{
"text": "But, frankly, this feels\nlike a slippery slope.",
"start": 6391.92,
"duration": 2.02
},
{
"text": ">> So every time I run\ninto a problem, I'm just",
"start": 6393.94,
"duration": 1.833
},
{
"text": "going to highlight and copy the code\nI wrote, cut it and paste it up here.",
"start": 6395.773,
"duration": 3.677
},
{
"text": "And, surely, we could\ncontrive some scenarios",
"start": 6399.45,
"duration": 1.92
},
{
"text": "where one function might\nneed to call another.",
"start": 6401.37,
"duration": 1.916
},
{
"text": "And you just can't put every\nfunction above every other.",
"start": 6403.286,
"duration": 2.744
},
{
"text": ">> So it turns out there's\na better solution.",
"start": 6406.03,
"duration": 1.9
},
{
"text": "We can leave this be.",
"start": 6407.93,
"duration": 2.17
},
{
"text": "And, frankly, it's generally nice,\nand convenient, and good design",
"start": 6410.1,
"duration": 3.577
},
{
"text": "to put main first, because, again,\nmain just like when green flag clicked,",
"start": 6413.677,
"duration": 3.083
},
{
"text": "that is the function that\ngets executed by default.",
"start": 6416.76,
"duration": 2.267
},
{
"text": "So you might as well put\nit at the top of the file",
"start": 6419.027,
"duration": 2.083
},
{
"text": "so that when you or any\nother human looks at the file",
"start": 6421.11,
"duration": 2.45
},
{
"text": "you know what's going on\njust by reading main first.",
"start": 6423.56,
"duration": 2.8
},
{
"text": "So it turns out, we can tell Clang\nproactively, hey, Clang, on line four,",
"start": 6426.36,
"duration": 9.0
},
{
"text": "I promise to implement\na function called Print",
"start": 6435.36,
"duration": 2.58
},
{
"text": "Name that takes a string called name\nas input, and returns nothing, void.",
"start": 6437.94,
"duration": 4.66
},
{
"text": "And I'll get around to\nimplementing it later.",
"start": 6442.6,
"duration": 2.17
},
{
"text": ">> Here comes Main.",
"start": 6444.77,
"duration": 0.91
},
{
"text": "Main now on line 9 can use\nPrint Name because Clang",
"start": 6445.68,
"duration": 3.45
},
{
"text": "is trusting that, eventually,\nit will encounter the definition",
"start": 6449.13,
"duration": 3.47
},
{
"text": "of the implementation of Print Name.",
"start": 6452.6,
"duration": 2.28
},
{
"text": "So after saving my file, let\nme go ahead and make prototype,",
"start": 6454.88,
"duration": 2.51
},
{
"text": "looks good this time.",
"start": 6457.39,
"duration": 1.108
},
{
"text": "Dot slash, prototype, let me\ngo ahead and type in a name.",
"start": 6458.498,
"duration": 4.972
},
{
"text": "David, hello David, Zamila, hello\nZamila, and, indeed, now it works.",
"start": 6463.47,
"duration": 4.97
},
{
"text": ">> So the ingredient here is that we've\nmade a custom function, like a custom",
"start": 6468.44,
"duration": 3.76
},
{
"text": "Scratch block we're calling it.",
"start": 6472.2,
"duration": 2.019
},
{
"text": "But unlike Scratch where you can\njust create it and start using it,",
"start": 6474.219,
"duration": 2.791
},
{
"text": "now we have to be a\nlittle more pedantic,",
"start": 6477.01,
"duration": 2.32
},
{
"text": "and actually train Clang\nto use, or to expect it.",
"start": 6479.33,
"duration": 4.08
},
{
"text": "Now, as an aside, why all this time have\nwe been just blindly on faith including",
"start": 6483.41,
"duration": 5.73
},
{
"text": "CS50.h, and including standard IO.h?",
"start": 6489.14,
"duration": 3.03
},
{
"text": ">> Well, it turns out,\namong a few other things,",
"start": 6492.17,
"duration": 3.02
},
{
"text": "all that's in those dot h\nfiles, which happen to be files.",
"start": 6495.19,
"duration": 3.36
},
{
"text": "They're header files, so to speak.",
"start": 6498.55,
"duration": 1.91
},
{
"text": "They're still written in C. But\nthey're a different type of file.",
"start": 6500.46,
"duration": 2.81
},
{
"text": ">> For now, you can pretty much assume\nthat all that is inside of CS50.h",
"start": 6503.27,
"duration": 5.42
},
{
"text": "is some one-liners like this, not\nfor functions called Print Name,",
"start": 6508.69,
"duration": 4.67
},
{
"text": "but for Get String, Get\nFloat, and a few others.",
"start": 6513.36,
"duration": 3.48
},
{
"text": "And there are similar prototypes,\none liners, inside of standard IO.h",
"start": 6516.84,
"duration": 4.67
},
{
"text": "for printf, which is now in\nmy own Print Name function.",
"start": 6521.51,
"duration": 4.731
},
{
"text": "So in other words, this whole time we've\njust been blindly copying and pasting",
"start": 6526.241,
"duration": 3.249
},
{
"text": "include this, include\nthat, what's going on?",
"start": 6529.49,
"duration": 2.29
},
{
"text": "Those are just kind of clues\nto Clang as to what functions",
"start": 6531.78,
"duration": 3.53
},
{
"text": "are, indeed, implemented, just\nelsewhere in different files",
"start": 6535.31,
"duration": 4.86
},
{
"text": "elsewhere on the system.",
"start": 6540.17,
"duration": 2.27
},
{
"text": ">> So we've implemented print name.",
"start": 6542.44,
"duration": 2.72
},
{
"text": "It does have this side effect of\nprinting something on the screen.",
"start": 6545.16,
"duration": 2.75
},
{
"text": "But it doesn't actually\nhand me something back.",
"start": 6547.91,
"duration": 2.26
},
{
"text": "How do we go about\nimplementing a program that",
"start": 6550.17,
"duration": 2.03
},
{
"text": "does hand me something back?",
"start": 6552.2,
"duration": 2.31
},
{
"text": ">> Well, let's try this.",
"start": 6554.51,
"duration": 1.07
},
{
"text": "Let me go ahead and implement\na file called return.c",
"start": 6555.58,
"duration": 5.78
},
{
"text": "so we can demonstrate how something\nlike Get String, or Get Int,",
"start": 6561.36,
"duration": 3.17
},
{
"text": "is actually returning\nsomething back to the user.",
"start": 6564.53,
"duration": 2.81
},
{
"text": "Let's go ahead and define int main void.",
"start": 6567.34,
"duration": 2.5
},
{
"text": ">> And, again, in the future, we'll\nexplain what that int and that void",
"start": 6569.84,
"duration": 3.39
},
{
"text": "is actually doing.",
"start": 6573.23,
"duration": 0.86
},
{
"text": "But for today, we'll\ntake it for granted.",
"start": 6574.09,
"duration": 1.75
},
{
"text": "I'm going to go ahead and printf,\nfor a good user experience, x is.",
"start": 6575.84,
"duration": 4.13
},
{
"text": "And then I'm going to wait for the\nuser to give me x with get int.",
"start": 6579.97,
"duration": 4.39
},
{
"text": ">> And then I'm going to go ahead\nand print out x to the square.",
"start": 6584.36,
"duration": 4.099
},
{
"text": "So when you only have a\nkeyboard, people commonly",
"start": 6588.459,
"duration": 2.041
},
{
"text": "use the little carrot\nsymbol on the keyboard",
"start": 6590.5,
"duration": 2.1
},
{
"text": "to represent to the power\nof, or the exponent of.",
"start": 6592.6,
"duration": 2.73
},
{
"text": "So x squared is present i.",
"start": 6595.33,
"duration": 3.63
},
{
"text": ">> And now I'm going to do this.",
"start": 6598.96,
"duration": 1.7
},
{
"text": "I could just do-- what's x\nsquared? x squared is x times x.",
"start": 6600.66,
"duration": 3.28
},
{
"text": ">> And we did this some\ntime ago already today.",
"start": 6603.94,
"duration": 2.75
},
{
"text": "This doesn't feel like\nall that much progress.",
"start": 6606.69,
"duration": 2.04
},
{
"text": "You know what?",
"start": 6608.73,
"duration": 0.84
},
{
"text": "Let's leverage some of that idea\nfrom last time of abstraction.",
"start": 6609.57,
"duration": 3.53
},
{
"text": ">> Wouldn't it be nice if\nthere's a function called",
"start": 6613.1,
"duration": 2.98
},
{
"text": "square that does exactly that?",
"start": 6616.08,
"duration": 2.38
},
{
"text": "It still, at the end of the\nday, does the same math.",
"start": 6618.46,
"duration": 2.18
},
{
"text": "But let's abstract\naway the idea of taking",
"start": 6620.64,
"duration": 1.77
},
{
"text": "one number multiplied by\nanother, and just give it a name,",
"start": 6622.41,
"duration": 2.87
},
{
"text": "like square this value.",
"start": 6625.28,
"duration": 2.08
},
{
"text": ">> And, in other words, in\nC, let's create a function",
"start": 6627.36,
"duration": 2.2
},
{
"text": "called square that does exactly that.",
"start": 6629.56,
"duration": 3.1
},
{
"text": "It's going to be called square.",
"start": 6632.66,
"duration": 1.94
},
{
"text": "It's going to take an int.",
"start": 6634.6,
"duration": 1.19
},
{
"text": "And we'll will just\ncall it n, by default.",
"start": 6635.79,
"duration": 2.03
},
{
"text": ">> But we could call it anything we want.",
"start": 6637.82,
"duration": 1.583
},
{
"text": "And all that it's going to\ndo, literally, is return",
"start": 6639.403,
"duration": 3.497
},
{
"text": "the result of n times n.",
"start": 6642.9,
"duration": 2.91
},
{
"text": "But because it is\nreturning something, which",
"start": 6645.81,
"duration": 3.17
},
{
"text": "is the keyword in purple we've\nnever seen before, I, on line 11,",
"start": 6648.98,
"duration": 4.71
},
{
"text": "can't just say void this time.",
"start": 6653.69,
"duration": 1.72
},
{
"text": ">> Void, in the example we just saw\nrather of print name, just means,",
"start": 6655.41,
"duration": 5.91
},
{
"text": "do something.",
"start": 6661.32,
"duration": 0.87
},
{
"text": "But don't hand me something back.",
"start": 6662.19,
"duration": 1.98
},
{
"text": "In this case, I do want\nto return n times n,",
"start": 6664.17,
"duration": 2.62
},
{
"text": "or whatever that is, that number.",
"start": 6666.79,
"duration": 1.67
},
{
"text": ">> So I can't say, hey, computer,\nI return nothing, void.",
"start": 6668.46,
"duration": 4.0
},
{
"text": "It's going to return, by nature, an int.",
"start": 6672.46,
"duration": 3.706
},
{
"text": "And so that's all that's going on here.",
"start": 6676.166,
"duration": 1.624
},
{
"text": ">> The input to square\nis going to be an int.",
"start": 6677.79,
"duration": 2.28
},
{
"text": "And so that we can use it, it has to\nhave a name, N. It's going to output",
"start": 6680.07,
"duration": 4.69
},
{
"text": "an int that doesn't need a name.",
"start": 6684.76,
"duration": 1.48
},
{
"text": "We can leave it to main, or whoever is\nusing me to remember this value if we",
"start": 6686.24,
"duration": 3.35
},
{
"text": "want with its own variable.",
"start": 6689.59,
"duration": 1.53
},
{
"text": ">> And, again, the only new\nkeyword here is Return.",
"start": 6691.12,
"duration": 2.11
},
{
"text": "And I'm just doing some math.",
"start": 6693.23,
"duration": 1.25
},
{
"text": "If I really wanted to be unnecessary,\nI could say int product gets n times n.",
"start": 6694.48,
"duration": 7.345
},
{
"text": ">> And then I could say, return product.",
"start": 6701.825,
"duration": 2.345
},
{
"text": "But, again, to my point earlier of\nthis just not being good design--",
"start": 6704.17,
"duration": 3.19
},
{
"text": "like, why introduce a name,\na symbol, like product,",
"start": 6707.36,
"duration": 2.7
},
{
"text": "just to immediately return it?",
"start": 6710.06,
"duration": 1.51
},
{
"text": "It's a little cleaner,\na little tighter, so",
"start": 6711.57,
"duration": 2.1
},
{
"text": "to speak, just to say return n times\nn, get rid of this line altogether.",
"start": 6713.67,
"duration": 5.71
},
{
"text": ">> And it's just less code to read,\nless opportunity for mistakes.",
"start": 6719.38,
"duration": 3.48
},
{
"text": "And let's see if this\nactually now works.",
"start": 6722.86,
"duration": 2.32
},
{
"text": "Now, I'm going to go\nahead and make return.",
"start": 6725.18,
"duration": 4.2
},
{
"text": ">> Uh-oh, implicit declaration of function.",
"start": 6729.38,
"duration": 2.08
},
{
"text": "I made this mistake before, no big deal.",
"start": 6731.46,
"duration": 2.62
},
{
"text": "Let me just type, or highlight and\ncopy, the exact same function prototype,",
"start": 6734.08,
"duration": 4.87
},
{
"text": "or signature, of the function up here.",
"start": 6738.95,
"duration": 2.392
},
{
"text": "Or I could move the whole function.",
"start": 6741.342,
"duration": 1.458
},
{
"text": ">> But that's a little lazy.",
"start": 6742.8,
"duration": 1.041
},
{
"text": "So we won't do that.",
"start": 6743.841,
"duration": 1.029
},
{
"text": "Now, let me make return\nagain, dot slash return.",
"start": 6744.87,
"duration": 3.09
},
{
"text": ">> x is 2. x squared is 4.\nx is 3. x squared is 9.",
"start": 6747.96,
"duration": 4.83
},
{
"text": "And the function seems\nnow to be working.",
"start": 6752.79,
"duration": 2.51
},
{
"text": "So what's the difference here?",
"start": 6755.3,
"duration": 1.25
},
{
"text": "I have a function that's called square,\nin this case, which I put in an input.",
"start": 6756.55,
"duration": 5.97
},
{
"text": "And I get back an output.",
"start": 6762.52,
"duration": 1.31
},
{
"text": "And yet, previously, if\nI open the other example",
"start": 6763.83,
"duration": 2.38
},
{
"text": "from earlier, which\nwas called prototype.c,",
"start": 6766.21,
"duration": 5.43
},
{
"text": "I had print name, which\nreturned void, so to speak,",
"start": 6771.64,
"duration": 3.13
},
{
"text": "Or it returned nothing, and\nsimply had a side effect.",
"start": 6774.77,
"duration": 3.96
},
{
"text": ">> So what's going on here?",
"start": 6778.73,
"duration": 1.5
},
{
"text": "Well, consider the function\nget string for just a moment.",
"start": 6780.23,
"duration": 3.29
},
{
"text": "We've been using the function\nget string in the following way.",
"start": 6783.52,
"duration": 3.05
},
{
"text": ">> We've had a function get\nstring, like include CS50.h,",
"start": 6786.57,
"duration": 3.894
},
{
"text": "include standard IO.h, int, main, void.",
"start": 6790.464,
"duration": 6.16
},
{
"text": "And then every time I've\ncalled get string thus far,",
"start": 6796.624,
"duration": 2.166
},
{
"text": "I've said something like, string s\ngets get string, because get string--",
"start": 6798.79,
"duration": 4.47
},
{
"text": "let's call this get.c-- get string\nitself returns a string that I can then",
"start": 6803.26,
"duration": 4.62
},
{
"text": "use, and say, hello, comma,\npercent s, backslash n, s.",
"start": 6807.88,
"duration": 4.17
},
{
"text": ">> So this is the same example,\nreally, that we had earlier.",
"start": 6812.05,
"duration": 3.61
},
{
"text": "So get string returns a value.",
"start": 6815.66,
"duration": 2.26
},
{
"text": "But a moment ago, print string\ndoes not return a value.",
"start": 6817.92,
"duration": 3.34
},
{
"text": "It simply has a side effect.",
"start": 6821.26,
"duration": 1.461
},
{
"text": "So this is a fundamental difference.",
"start": 6822.721,
"duration": 1.499
},
{
"text": "We've seen different\ntypes of functions now,",
"start": 6824.22,
"duration": 2.49
},
{
"text": "some of which have returned\nvalues, some of which don't.",
"start": 6826.71,
"duration": 2.78
},
{
"text": "So maybe it's string, or int, or float.",
"start": 6829.49,
"duration": 2.4
},
{
"text": "Or maybe it's just void.",
"start": 6831.89,
"duration": 1.59
},
{
"text": ">> And the difference is\nthat these functions that",
"start": 6833.48,
"duration": 2.23
},
{
"text": "get data and return a value are actually\nbringing something back to the table,",
"start": 6835.71,
"duration": 4.23
},
{
"text": "so to speak.",
"start": 6839.94,
"duration": 1.17
},
{
"text": "So let's go ahead and\nlook at one final set",
"start": 6841.11,
"duration": 2.6
},
{
"text": "of examples that gives a sense, now, of\nhow we might, indeed, abstract better,",
"start": 6843.71,
"duration": 5.419
},
{
"text": "and better, and better, or more,\nand more, and more, in order",
"start": 6849.129,
"duration": 2.541
},
{
"text": "to write, ultimately, better code.",
"start": 6851.67,
"duration": 2.14
},
{
"text": "Let's go ahead, and in the spirit\nof Scratch, do the following.",
"start": 6853.81,
"duration": 3.05
},
{
"text": ">> Let me go ahead and include\nCS50.h and standard IO.h.",
"start": 6856.86,
"duration": 4.84
},
{
"text": "Let me go ahead and give\nmyself an int, main, void.",
"start": 6861.7,
"duration": 2.31
},
{
"text": "And let me go ahead, call this cough.c.",
"start": 6864.01,
"duration": 3.37
},
{
"text": ">> And let me go ahead and just\nlike Scratch, print out cough/n.",
"start": 6867.38,
"duration": 8.13
},
{
"text": "And I want to do this three times.",
"start": 6875.51,
"duration": 1.66
},
{
"text": "So I'm, of course, just going\nto copy and paste three times.",
"start": 6877.17,
"duration": 2.5
},
{
"text": "I'm now going to make\ncough dot slash cough.",
"start": 6879.67,
"duration": 6.77
},
{
"text": "Let's give myself a little more room\nhere, Enter, cough, cough, cough.",
"start": 6886.44,
"duration": 3.68
},
{
"text": ">> There's, obviously, already an\nopportunity for improvement.",
"start": 6890.12,
"duration": 3.85
},
{
"text": "I've copied and pasted\na few times today.",
"start": 6893.97,
"duration": 1.709
},
{
"text": "But that was only so I didn't\nhave to type as many characters.",
"start": 6895.679,
"duration": 2.582
},
{
"text": "I still changed what\nthose lines of code are.",
"start": 6898.261,
"duration": 1.989
},
{
"text": ">> These three lines are identical,\nwhich feels lazy and indeed is,",
"start": 6900.25,
"duration": 3.99
},
{
"text": "and is probably not the right approach.",
"start": 6904.24,
"duration": 2.87
},
{
"text": "So with what ingredient\ncould we improve this code?",
"start": 6907.11,
"duration": 3.919
},
{
"text": "We don't have to copy and paste code.",
"start": 6911.029,
"duration": 1.541
},
{
"text": ">> And, indeed, any time you feel\nyourself copying and pasting,",
"start": 6912.57,
"duration": 2.5
},
{
"text": "and not even changing code,\nodds are there's a better way.",
"start": 6915.07,
"duration": 2.63
},
{
"text": "And, indeed, there is.",
"start": 6917.7,
"duration": 1.77
},
{
"text": "Let me go ahead and do a for loop,\neven though the syntax might not",
"start": 6919.47,
"duration": 3.04
},
{
"text": "come naturally yet.",
"start": 6922.51,
"duration": 2.06
},
{
"text": ">> Do this three times, simply\nby doing the following--",
"start": 6924.57,
"duration": 4.924
},
{
"text": "and I happen to know this from practice.",
"start": 6929.494,
"duration": 1.666
},
{
"text": "But we have a number of examples now.",
"start": 6931.16,
"duration": 1.65
},
{
"text": "And you'll see online\nmore references still.",
"start": 6932.81,
"duration": 2.14
},
{
"text": ">> This is the syntax on line 6, that\nmuch like Scratch that repeats",
"start": 6934.95,
"duration": 2.84
},
{
"text": "block, repeat the following three times.",
"start": 6937.79,
"duration": 2.3
},
{
"text": "It's a little magical for now.",
"start": 6940.09,
"duration": 1.25
},
{
"text": "But this will get more,\nand more familiar.",
"start": 6941.34,
"duration": 1.71
},
{
"text": ">> And it's going to repeat\nline eight three times,",
"start": 6943.05,
"duration": 2.0
},
{
"text": "so that if I re-compile make cough,\ndot slash cough, cough, cough, cough.",
"start": 6945.05,
"duration": 7.34
},
{
"text": "It still works the same way.",
"start": 6952.39,
"duration": 1.64
},
{
"text": "So that's all fine and good.",
"start": 6954.03,
"duration": 1.52
},
{
"text": "But that's not very abstracted.",
"start": 6955.55,
"duration": 2.65
},
{
"text": ">> It's perfectly correct.",
"start": 6958.2,
"duration": 1.171
},
{
"text": "But it feels like there\ncould be an opportunity,",
"start": 6959.371,
"duration": 1.999
},
{
"text": "as in the world of\nScratch, to kind of start",
"start": 6961.37,
"duration": 2.38
},
{
"text": "to add some semantics here so that\nI don't just have some for loop,",
"start": 6963.75,
"duration": 3.78
},
{
"text": "and a function that says\ncough, or does cough.",
"start": 6967.53,
"duration": 2.337
},
{
"text": "You know what?",
"start": 6969.867,
"duration": 0.583
},
{
"text": "Let me try to be a\nlittle cooler than that,",
"start": 6970.45,
"duration": 2.17
},
{
"text": "and actually write a function that\nhas some side effects, call it cough.",
"start": 6972.62,
"duration": 3.47
},
{
"text": ">> And it takes no input, and\nreturns no value as output.",
"start": 6976.09,
"duration": 4.74
},
{
"text": "But you know what it does?",
"start": 6980.83,
"duration": 1.85
},
{
"text": "It does this-- printf,\nquote unquote, cough.",
"start": 6982.68,
"duration": 6.69
},
{
"text": ">> And now up here, I'm going\nto go ahead and for int,",
"start": 6989.37,
"duration": 3.01
},
{
"text": "i gets zero, i less than 3, i plus plus.",
"start": 6992.38,
"duration": 3.69
},
{
"text": "I'm going to not do printf, which is\narguably a low level implementation",
"start": 6996.07,
"duration": 3.7
},
{
"text": "detail.",
"start": 6999.77,
"duration": 0.5
},
{
"text": "I don't care how to cough.",
"start": 7000.27,
"duration": 1.083
},
{
"text": "I just want to use the cough function.",
"start": 7001.353,
"duration": 1.887
},
{
"text": "And I'm just going to call cough.",
"start": 7003.24,
"duration": 1.6
},
{
"text": ">> Now, notice the dichotomy.",
"start": 7004.84,
"duration": 1.364
},
{
"text": "When you call a function, if you don't\nwant to give it inputs, totally fine.",
"start": 7006.204,
"duration": 3.166
},
{
"text": "Just do open paren, close\nparen, and you're done.",
"start": 7009.37,
"duration": 2.41
},
{
"text": ">> When you define a function, or\ndeclare a function's prototype,",
"start": 7011.78,
"duration": 4.491
},
{
"text": "if you know in advance it's not\ngoing to take any arguments,",
"start": 7016.271,
"duration": 2.499
},
{
"text": "say void in those parentheses there.",
"start": 7018.77,
"duration": 2.4
},
{
"text": "And that makes certain that you\nwon't accidentally misuse it.",
"start": 7021.17,
"duration": 4.49
},
{
"text": "Let me go ahead and make cough.",
"start": 7025.66,
"duration": 1.36
},
{
"text": "And, of course, I've made a mistake.",
"start": 7027.02,
"duration": 1.52
},
{
"text": ">> Dammit, there's that\nimplicit declaration.",
"start": 7028.54,
"duration": 1.87
},
{
"text": "But that's fine.",
"start": 7030.41,
"duration": 0.915
},
{
"text": "It's an easy fix.",
"start": 7031.325,
"duration": 1.265
},
{
"text": "I just need the prototype higher up\nin my file than I'm actually using it.",
"start": 7032.59,
"duration": 5.65
},
{
"text": ">> So now let me make cough again, nice.",
"start": 7038.24,
"duration": 1.83
},
{
"text": "Now, it works.",
"start": 7040.07,
"duration": 0.72
},
{
"text": "Make cough, cough, cough, cough.",
"start": 7040.79,
"duration": 2.14
},
{
"text": "So you might think that we're really\njust over engineering this problem.",
"start": 7042.93,
"duration": 3.0
},
{
"text": "And, indeed, we are.",
"start": 7045.93,
"duration": 0.833
},
{
"text": "This is not a good\ncandidate of a program",
"start": 7046.763,
"duration": 2.107
},
{
"text": "at the moment for\nrefactoring, and doing what's",
"start": 7048.87,
"duration": 3.06
},
{
"text": "called hierarchical decomposition,\nwhere you take some code, and then",
"start": 7051.93,
"duration": 3.715
},
{
"text": "you kind of factor things out, so as\nto ascribe more semantics to them,",
"start": 7055.645,
"duration": 3.145
},
{
"text": "and reuse it ultimately longer term.",
"start": 7058.79,
"duration": 2.14
},
{
"text": "But it's a building block toward\nmore sophisticated programs",
"start": 7060.93,
"duration": 2.56
},
{
"text": "that we will start\nwriting before long that",
"start": 7063.49,
"duration": 2.11
},
{
"text": "allows us to have the vocabulary\nwith which to write better code.",
"start": 7065.6,
"duration": 4.49
},
{
"text": "And, indeed, let's see if we\ncan't generalize this further.",
"start": 7070.09,
"duration": 2.83
},
{
"text": ">> It seems a little lame that I, main,\nneed to worry about this darn for loop,",
"start": 7072.92,
"duration": 5.064
},
{
"text": "and calling cough again and again.",
"start": 7077.984,
"duration": 1.416
},
{
"text": "Why can't I just tell cough,\nplease cough three times?",
"start": 7079.4,
"duration": 3.65
},
{
"text": "In other words, why can't I just\ngive input to cough and do this?",
"start": 7083.05,
"duration": 5.12
},
{
"text": ">> Why can't I just say, in\nmain cough three times.",
"start": 7088.17,
"duration": 3.1
},
{
"text": "And now, this is kind of magical.",
"start": 7091.27,
"duration": 1.88
},
{
"text": "It's very iterative here.",
"start": 7093.15,
"duration": 1.39
},
{
"text": "And it's, indeed, a baby step.",
"start": 7094.54,
"duration": 1.4
},
{
"text": ">> But just the ability to say on\nline eight, cough three times,",
"start": 7095.94,
"duration": 3.31
},
{
"text": "it's just so much more readable.",
"start": 7099.25,
"duration": 1.48
},
{
"text": "And, plus, I don't have to know\nor care how cough is implemented.",
"start": 7100.73,
"duration": 3.48
},
{
"text": "And, indeed, later in the\nterm and for final projects,",
"start": 7104.21,
"duration": 2.25
},
{
"text": "if you tackle a project with\na classmate or two classmates,",
"start": 7106.46,
"duration": 2.69
},
{
"text": "you'll realize that you're going to\nhave to, or want to, divide the work.",
"start": 7109.15,
"duration": 3.22
},
{
"text": ">> And you're going to want to decide\nin advance, who's going to do what,",
"start": 7112.37,
"duration": 2.28
},
{
"text": "and in which pieces?",
"start": 7114.65,
"duration": 0.833
},
{
"text": "And wouldn't it be nice\nif you, for instance,",
"start": 7115.483,
"duration": 2.037
},
{
"text": "take charge of writing main, done.",
"start": 7117.52,
"duration": 2.58
},
{
"text": "And your roommate, or your\npartner more generally,",
"start": 7120.1,
"duration": 3.37
},
{
"text": "takes care of implementing cough.",
"start": 7123.47,
"duration": 1.76
},
{
"text": ">> And this division, these\nwalls of abstraction,",
"start": 7125.23,
"duration": 4.31
},
{
"text": "or layers of abstraction if\nyou will, are super powerful,",
"start": 7129.54,
"duration": 2.77
},
{
"text": "because especially for larger,\nmore complex programs and systems,",
"start": 7132.31,
"duration": 3.17
},
{
"text": "it allows multiple people to build\nthings together, and ultimately",
"start": 7135.48,
"duration": 4.59
},
{
"text": "stitch their work together in this way.",
"start": 7140.07,
"duration": 2.61
},
{
"text": "But, of course, we\nneed to now fix cough.",
"start": 7142.68,
"duration": 2.652
},
{
"text": "We need to tell cough\nthat, hey, you know what?",
"start": 7145.332,
"duration": 1.958
},
{
"text": "You're going to need to take an\ninput-- so not void, but int and now.",
"start": 7147.29,
"duration": 3.94
},
{
"text": "Let's go ahead and put into\ncough the int. i gets zero.",
"start": 7151.23,
"duration": 3.94
},
{
"text": ">> i is less than how many times.",
"start": 7155.17,
"duration": 1.72
},
{
"text": "I said three before.",
"start": 7156.89,
"duration": 1.66
},
{
"text": "But that's not what I want.",
"start": 7158.55,
"duration": 1.87
},
{
"text": "I want cough to be generalized to\nsupport any number of iterations.",
"start": 7160.42,
"duration": 5.1
},
{
"text": ">> So, indeed, it's n that I want,\nwhatever the user tells me.",
"start": 7165.52,
"duration": 3.28
},
{
"text": "Now, I can go ahead and say print cough.",
"start": 7168.8,
"duration": 2.82
},
{
"text": "And no matter what number\nthe user passes in,",
"start": 7171.62,
"duration": 3.13
},
{
"text": "I will iterate that many times.",
"start": 7174.75,
"duration": 2.14
},
{
"text": ">> So at the end of the day,\nprogram is identical.",
"start": 7176.89,
"duration": 2.27
},
{
"text": "But notice all of this stuff\ncould even be in another file.",
"start": 7179.16,
"duration": 3.66
},
{
"text": "Indeed, I don't know at the\nmoment how printf is implemented.",
"start": 7182.82,
"duration": 2.8
},
{
"text": ">> I don't know at the moment how get\nstring, or get int, or get float",
"start": 7185.62,
"duration": 2.36
},
{
"text": "are implemented.",
"start": 7187.98,
"duration": 0.666
},
{
"text": "And I don't want to\nsee them on my screen.",
"start": 7188.646,
"duration": 2.284
},
{
"text": "As it is, I'm starting to focus on\nmy program, not those functions.",
"start": 7190.93,
"duration": 4.39
},
{
"text": ">> And so, indeed, as soon as you\nstart factoring code like this out,",
"start": 7195.32,
"duration": 3.75
},
{
"text": "could we even move cough\nto a separate file?",
"start": 7199.07,
"duration": 2.327
},
{
"text": "Someone else could implement it.",
"start": 7201.397,
"duration": 1.333
},
{
"text": "And you and your program become the\nvery beautiful, and very readable,",
"start": 7202.73,
"duration": 4.08
},
{
"text": "arguably, really four\nline program right there.",
"start": 7206.81,
"duration": 4.02
},
{
"text": ">> So let's go ahead now\nand make one more change.",
"start": 7210.83,
"duration": 2.68
},
{
"text": "Notice that my prototype\nhas to change up top.",
"start": 7213.51,
"duration": 2.67
},
{
"text": "So let me fix that so\nI don't get yelled at.",
"start": 7216.18,
"duration": 2.21
},
{
"text": ">> Make cough, let me run cough once\nmore, still doing the same thing.",
"start": 7218.39,
"duration": 4.19
},
{
"text": "But now, notice we have an\ningredient for one final version.",
"start": 7222.58,
"duration": 3.43
},
{
"text": "You know what?",
"start": 7226.01,
"duration": 0.93
},
{
"text": "I don't want to just cough, necessarily.",
"start": 7226.94,
"duration": 2.1
},
{
"text": "I want to have something more general.",
"start": 7229.04,
"duration": 1.762
},
{
"text": "So you know what?",
"start": 7230.802,
"duration": 0.708
},
{
"text": "I want to do this.",
"start": 7231.51,
"duration": 0.94
},
{
"text": "I want to have, much like Scratch\ndoes, a say block, but not just",
"start": 7232.45,
"duration": 4.69
},
{
"text": "say something some number of times.",
"start": 7237.14,
"duration": 1.54
},
{
"text": "I want it to say a very specific string.",
"start": 7238.68,
"duration": 2.83
},
{
"text": "And, therefore, I don't\nwant it to just say cough.",
"start": 7241.51,
"duration": 2.34
},
{
"text": "I want it to say whatever\nstring is passed in.",
"start": 7243.85,
"duration": 3.81
},
{
"text": ">> So notice, I've generalized\nthis so that now",
"start": 7247.66,
"duration": 2.3
},
{
"text": "say feels like a good name\nfor this, like Scratch,",
"start": 7249.96,
"duration": 3.15
},
{
"text": "takes two arguments, unlike Scratch.",
"start": 7253.11,
"duration": 2.42
},
{
"text": "One is a string.",
"start": 7255.53,
"duration": 1.04
},
{
"text": "One is an int.",
"start": 7256.57,
"duration": 0.73
},
{
"text": ">> And I could switch them.",
"start": 7257.3,
"duration": 0.83
},
{
"text": "I just kind of like the idea of\nsay the string first, and then",
"start": 7258.13,
"duration": 2.583
},
{
"text": "how many times later.",
"start": 7260.713,
"duration": 1.227
},
{
"text": "Void means it still\ndoesn't return anything.",
"start": 7261.94,
"duration": 2.03
},
{
"text": "These are just visual side\neffects, like with [? Jordan, ?]",
"start": 7263.97,
"duration": 2.458
},
{
"text": "a verbal side effect of yelling.",
"start": 7266.428,
"duration": 1.812
},
{
"text": "It still does something n times,\n0 up to, but not equal to n.",
"start": 7268.24,
"duration": 4.39
},
{
"text": "This means n total times.",
"start": 7272.63,
"duration": 1.91
},
{
"text": "And then just print out\nwhatever that string is.",
"start": 7274.54,
"duration": 2.0
},
{
"text": "So I've really generalized\nthis line of code.",
"start": 7276.54,
"duration": 2.52
},
{
"text": "So now, how do I implement\nthe cough function?",
"start": 7279.06,
"duration": 3.4
},
{
"text": ">> I can do void cough.",
"start": 7282.46,
"duration": 3.06
},
{
"text": "And I can still take in how\nmany times you want to cough.",
"start": 7285.52,
"duration": 2.981
},
{
"text": "But you know what?",
"start": 7288.501,
"duration": 0.749
},
{
"text": "I can now punt to say.",
"start": 7289.25,
"duration": 1.99
},
{
"text": ">> I can call say with the\nword cough, passing in n.",
"start": 7291.24,
"duration": 5.3
},
{
"text": "And if I want to also implement,\njust for fun, a sneeze function,",
"start": 7296.54,
"duration": 3.87
},
{
"text": "I can sneeze some number of times.",
"start": 7300.41,
"duration": 1.88
},
{
"text": "And I can keep reusing n, because\nnotice that m in this context or scope",
"start": 7302.29,
"duration": 5.01
},
{
"text": "only exists within this function.",
"start": 7307.3,
"duration": 2.17
},
{
"text": ">> And n in this context only\nexists within this function here.",
"start": 7309.47,
"duration": 3.297
},
{
"text": "So we'll come back to\nthese issues of scope.",
"start": 7312.767,
"duration": 1.833
},
{
"text": "And here, I'm just going to say,\nachoo, and then n times, semi-colon.",
"start": 7314.6,
"duration": 6.56
},
{
"text": ">> And now, I just need to borrow\nthese function signatures up here.",
"start": 7321.16,
"duration": 3.18
},
{
"text": "So cough is correct.",
"start": 7324.34,
"duration": 1.95
},
{
"text": "Void sneeze is correct now.",
"start": 7326.29,
"duration": 3.8
},
{
"text": ">> And I still just need say.",
"start": 7330.09,
"duration": 2.3
},
{
"text": "So I'm going to say, say\nstring s, int n, semi-colon.",
"start": 7332.39,
"duration": 6.6
},
{
"text": "So I've over-engineered the\nheck out of this program.",
"start": 7338.99,
"duration": 3.02
},
{
"text": ">> And this doesn't\nnecessarily mean this is",
"start": 7342.01,
"duration": 1.75
},
{
"text": "what you should do when writing\neven the simplest of programs.",
"start": 7343.76,
"duration": 2.583
},
{
"text": "Take something that's obviously\nreally simple, really short,",
"start": 7346.343,
"duration": 2.937
},
{
"text": "and re-implement it\nusing way too much code.",
"start": 7349.28,
"duration": 2.52
},
{
"text": "But you'll actually see, and in\ntime look back on these examples,",
"start": 7351.8,
"duration": 2.76
},
{
"text": "and realize, oh, those are the steps\nwe took to actually generalize,",
"start": 7354.56,
"duration": 4.05
},
{
"text": "to factor something out,\nuntil at the end of the day",
"start": 7358.61,
"duration": 2.187
},
{
"text": "my code is actually pretty reasonable.",
"start": 7360.797,
"duration": 1.583
},
{
"text": "Because if I want to cough three\ntimes then sneeze three times,",
"start": 7362.38,
"duration": 3.58
},
{
"text": "I'm simply going to rerun this,\nprogram make cough, and run cough.",
"start": 7365.96,
"duration": 4.46
},
{
"text": "And I have three coughs\nand three sneezes.",
"start": 7370.42,
"duration": 3.2
},
{
"text": ">> And so this is a basic\nparadigm, if you will,",
"start": 7373.62,
"duration": 2.37
},
{
"text": "for how we might go about\nactually implementing a program.",
"start": 7375.99,
"duration": 4.12
},
{
"text": "But let's just see now what it is\nwe've been doing all of this time,",
"start": 7380.11,
"duration": 3.11
},
{
"text": "and what some of the final pieces\nare behind this simple command.",
"start": 7383.22,
"duration": 3.72
},
{
"text": "At the end of the day, we've\nbeen using Clang as our compiler.",
"start": 7386.94,
"duration": 2.68
},
{
"text": "We've been writing source\ncode, converting it",
"start": 7389.62,
"duration": 1.874
},
{
"text": "via Clang into machine code.",
"start": 7391.494,
"duration": 1.326
},
{
"text": ">> And we've been using Make just\nto facilitate our keystrokes so",
"start": 7392.82,
"duration": 2.72
},
{
"text": "that we don't have to remember\nthose incantations of Clang itself.",
"start": 7395.54,
"duration": 5.2
},
{
"text": "But what is Make actually doing?",
"start": 7400.74,
"duration": 1.9
},
{
"text": "And, in turn, what is\nClang actually doing?",
"start": 7402.64,
"duration": 2.11
},
{
"text": ">> It turns out, though we have simplified\ntoday's discussion by saying,",
"start": 7404.75,
"duration": 4.04
},
{
"text": "you take source code, pass it as\ninput to a compiler, which gives you",
"start": 7408.79,
"duration": 4.3
},
{
"text": "output of machine\ncode, turns out there's",
"start": 7413.09,
"duration": 2.66
},
{
"text": "a few different steps inside there.",
"start": 7415.75,
"duration": 1.67
},
{
"text": "And compiling happens to be the umbrella\nterm for a whole bunch of steps.",
"start": 7417.42,
"duration": 4.52
},
{
"text": "But let's just tease\nthis out really quickly.",
"start": 7421.94,
"duration": 2.03
},
{
"text": ">> It turns out that we've been doing\nmore things every time I run a program,",
"start": 7423.97,
"duration": 4.1
},
{
"text": "or every time I compile a program today.",
"start": 7428.07,
"duration": 2.92
},
{
"text": "So preprocessing refers to\nthis-- anything in a C program,",
"start": 7430.99,
"duration": 4.03
},
{
"text": "as we'll see again and again,\nthat starts with this hash symbol,",
"start": 7435.02,
"duration": 3.7
},
{
"text": "or the hashtag symbol here, means\nit's a preprocessor directive.",
"start": 7438.72,
"duration": 4.6
},
{
"text": "That means, in this case, hey\ncomputer, do something with this file",
"start": 7443.32,
"duration": 4.01
},
{
"text": "before you actually compile my own code.",
"start": 7447.33,
"duration": 2.1
},
{
"text": ">> In this case, hash include is,\nessentially, C's way of saying,",
"start": 7449.43,
"duration": 5.79
},
{
"text": "hey computer, go get the contents\nof CS50.h and paste them here.",
"start": 7455.22,
"duration": 4.105
},
{
"text": "Hey computer, go get the\ncontents of standard IO.h,",
"start": 7459.325,
"duration": 2.845
},
{
"text": "wherever that is on the\nhard drive, paste it here.",
"start": 7462.17,
"duration": 2.52
},
{
"text": "So those things happen\nfirst during preprocessing.",
"start": 7464.69,
"duration": 2.7
},
{
"text": ">> And Clang does all of this for us.",
"start": 7467.39,
"duration": 1.49
},
{
"text": "And it does it so darn\nfast, you don't even",
"start": 7468.88,
"duration": 1.63
},
{
"text": "see four distinct things happening.",
"start": 7470.51,
"duration": 1.49
},
{
"text": "But that's the first such step.",
"start": 7472.0,
"duration": 2.1
},
{
"text": ">> What actually happens next?",
"start": 7474.1,
"duration": 1.46
},
{
"text": "Well, the next official\nstep is compiling.",
"start": 7475.56,
"duration": 2.76
},
{
"text": "And it turns out that\ncompiling a program",
"start": 7478.32,
"duration": 2.065
},
{
"text": "technically means going from\nsource code, the stuff we've",
"start": 7480.385,
"duration": 3.675
},
{
"text": "been writing today, to something\ncalled assembly code, something",
"start": 7484.06,
"duration": 3.83
},
{
"text": "that looks a little different.",
"start": 7487.89,
"duration": 1.37
},
{
"text": ">> And, in fact, we can see this real fast.",
"start": 7489.26,
"duration": 1.79
},
{
"text": "Let me actually go into my IDE.",
"start": 7491.05,
"duration": 2.84
},
{
"text": "Let me go ahead and open hello.c, which\nis the very first program with which we",
"start": 7493.89,
"duration": 4.16
},
{
"text": "began today.",
"start": 7498.05,
"duration": 1.07
},
{
"text": "And let me go ahead and run Clang a\nlittle differently, Clang-s, hello.c,",
"start": 7499.12,
"duration": 5.01
},
{
"text": "which is actually going to\ngive me another file hello.s.",
"start": 7504.13,
"duration": 3.59
},
{
"text": ">> And we will probably never\nagain see this kind of code.",
"start": 7507.72,
"duration": 2.61
},
{
"text": "If you take a lower level\nsystems class like CS61,",
"start": 7510.33,
"duration": 2.7
},
{
"text": "you will see a lot more\nof this kind of code.",
"start": 7513.03,
"duration": 1.89
},
{
"text": "But this is assembly language.",
"start": 7514.92,
"duration": 2.1
},
{
"text": "This is X86 assembly language\nthat the CPU that is underlying",
"start": 7517.02,
"duration": 5.03
},
{
"text": "CS50 IDE actually understands.",
"start": 7522.05,
"duration": 2.41
},
{
"text": ">> And cryptic as it does\nlook, it is something",
"start": 7524.46,
"duration": 2.6
},
{
"text": "the computer understands pretty well.",
"start": 7527.06,
"duration": 2.12
},
{
"text": "Sub q, this is a subtract.",
"start": 7529.18,
"duration": 1.61
},
{
"text": "There's movements.",
"start": 7530.79,
"duration": 0.87
},
{
"text": ">> There's calling of functions here,\nx oring, a movement, an add, a pop,",
"start": 7531.66,
"duration": 4.07
},
{
"text": "a return.",
"start": 7535.73,
"duration": 0.7
},
{
"text": "So there's some very\nlow level instructions",
"start": 7536.43,
"duration": 2.42
},
{
"text": "that CPUs understand that\nI alluded to earlier.",
"start": 7538.85,
"duration": 2.43
},
{
"text": "That is what Intel Inside.",
"start": 7541.28,
"duration": 1.82
},
{
"text": ">> There are patterns of\nzeros and ones that",
"start": 7543.1,
"duration": 1.93
},
{
"text": "map to these arcanely worded, but\nsomewhat well-named, instructions,",
"start": 7545.03,
"duration": 6.77
},
{
"text": "so to speak.",
"start": 7551.8,
"duration": 0.98
},
{
"text": "That is what happens when\nyou compile your code.",
"start": 7552.78,
"duration": 2.0
},
{
"text": "You get assembly\nlanguage out of it, which",
"start": 7554.78,
"duration": 3.78
},
{
"text": "means the third step is to assemble\nthat assembly code into, ultimately,",
"start": 7558.56,
"duration": 6.12
},
{
"text": "machine code-- zeros and ones, not the\ntext that we just saw a moment ago.",
"start": 7564.68,
"duration": 4.4
},
{
"text": ">> So pre-processing does that find\nand replace, and a few other things.",
"start": 7569.08,
"duration": 4.29
},
{
"text": "Compiling takes your source\ncode from C, source code",
"start": 7573.37,
"duration": 3.06
},
{
"text": "that we wrote, to assembly\ncode that we just glanced at.",
"start": 7576.43,
"duration": 2.55
},
{
"text": "Assembling takes that assembly\ncode to zeroes and ones",
"start": 7578.98,
"duration": 3.19
},
{
"text": "that the CPU really will\nunderstand at the end of the day.",
"start": 7582.17,
"duration": 2.51
},
{
"text": "And linking is the last step\nthat happens for us-- again,",
"start": 7584.68,
"duration": 2.95
},
{
"text": "so fast we don't even\nnotice-- that says,",
"start": 7587.63,
"duration": 2.2
},
{
"text": "hey computer, take all of\nthe zeros and ones that",
"start": 7589.83,
"duration": 2.63
},
{
"text": "resulted from compiling David's code,\nand his main function in this case.",
"start": 7592.46,
"duration": 4.29
},
{
"text": ">> And hey computer, go get\nall of the zeros and ones",
"start": 7596.75,
"duration": 2.41
},
{
"text": "that the CS50 staff wrote\ninside the CS50 library.",
"start": 7599.16,
"duration": 3.02
},
{
"text": "Mix those in with David's.",
"start": 7602.18,
"duration": 1.26
},
{
"text": "And hey computer, go get all the zeros\nand ones that someone else wrote years",
"start": 7603.44,
"duration": 3.208
},
{
"text": "ago for printf.",
"start": 7606.648,
"duration": 0.822
},
{
"text": "And add those into the\nwhole thing, so that we've",
"start": 7607.47,
"duration": 2.41
},
{
"text": "got my zeros and ones, the\nCS50 staff's zeros and ones,",
"start": 7609.88,
"duration": 2.99
},
{
"text": "the printf zeros and ones,\nand anything else we're using.",
"start": 7612.87,
"duration": 2.5
},
{
"text": ">> They all get combined together into one\nprogram called, in this case, hello.",
"start": 7615.37,
"duration": 5.04
},
{
"text": "So henceforth, we will just\nuse the word compiling.",
"start": 7620.41,
"duration": 2.731
},
{
"text": "And we will take for granted that when\nwe say, compile your program, it means,",
"start": 7623.141,
"duration": 3.249
},
{
"text": "hey do the pre-processing,\nassembling, and linking.",
"start": 7626.39,
"duration": 2.459
},
{
"text": "But there's actually some juicy stuff\ngoing on there underneath the hood.",
"start": 7628.849,
"duration": 3.041
},
{
"text": "And especially if you\nget curious some time,",
"start": 7631.89,
"duration": 1.833
},
{
"text": "you can start poking\naround at this lower level.",
"start": 7633.723,
"duration": 2.177
},
{
"text": "But for now, realize that\namong the takeaways for today",
"start": 7635.9,
"duration": 3.76
},
{
"text": "are quite simply the\nbeginning of a process,",
"start": 7639.66,
"duration": 3.76
},
{
"text": "of getting comfortable with\nsomething like hello world.",
"start": 7643.42,
"duration": 3.28
},
{
"text": "Indeed, most of what we did today\ncertainly won't sink in super fast.",
"start": 7646.7,
"duration": 2.875
},
{
"text": "And it will take some\ntime, and some practice.",
"start": 7649.575,
"duration": 1.916
},
{
"text": "And odds are, you will sort\nof want to hit your keyboard",
"start": 7651.491,
"duration": 2.373
},
{
"text": "or yell at the screen.",
"start": 7653.864,
"duration": 0.916
},
{
"text": "And all of that's OK.",
"start": 7654.78,
"duration": 1.1
},
{
"text": "Though, perhaps try not to\ndo it in the library so much.",
"start": 7655.88,
"duration": 2.44
},
{
"text": ">> And ultimately, you'll\nbe able though, to start",
"start": 7658.32,
"duration": 2.5
},
{
"text": "seeing patterns, both in good code\nthat you've written and in mistakes",
"start": 7660.82,
"duration": 3.76
},
{
"text": "that you've made.",
"start": 7664.58,
"duration": 0.79
},
{
"text": "And much like the process of\nbecoming a TF or a CA is like,",
"start": 7665.37,
"duration": 3.595
},
{
"text": "you'll start to get better and\nbetter at seeing those patterns,",
"start": 7668.965,
"duration": 2.625
},
{
"text": "and just solving your\nown problems ultimately.",
"start": 7671.59,
"duration": 2.184
},
{
"text": "In the meantime, there will be plenty\nof us to lend you support, and get you",
"start": 7673.774,
"duration": 3.166
},
{
"text": "through this.",
"start": 7676.94,
"duration": 0.541
},
{
"text": "And in the write-ups\nfor all of the problems",
"start": 7677.481,
"duration": 1.969
},
{
"text": "will you be guided through\nall of the commands",
"start": 7679.45,
"duration": 1.916
},
{
"text": "that I certainly know from\na lot of practice by now,",
"start": 7681.366,
"duration": 3.964
},
{
"text": "but might have flown\nover one's head for now.",
"start": 7685.33,
"duration": 2.05
},
{
"text": "And that's totally fine.",
"start": 7687.38,
"duration": 1.2
},
{
"text": ">> But, ultimately, you're going\nto start to see patterns emerge.",
"start": 7688.58,
"duration": 2.65
},
{
"text": "And once you get past all of the\nstupid details, like parentheses,",
"start": 7691.23,
"duration": 3.03
},
{
"text": "and curly braces, and semi-colons,\nand the stuff, frankly,",
"start": 7694.26,
"duration": 2.45
},
{
"text": "that is not at all\nintellectually interesting.",
"start": 7696.71,
"duration": 2.65
},
{
"text": "And it is not the objective of\ntaking any introductory class.",
"start": 7699.36,
"duration": 3.33
},
{
"text": "It's the ideas that are going to matter.",
"start": 7702.69,
"duration": 1.72
},
{
"text": ">> It's the loops, and the\nconditions, and the functions,",
"start": 7704.41,
"duration": 2.249
},
{
"text": "and more powerfully the abstraction,\nand the factoring of code,",
"start": 7706.659,
"duration": 3.893
},
{
"text": "and the good design, and the good\nstyle, and ultimately the correctness",
"start": 7710.552,
"duration": 2.958
},
{
"text": "of your code, that's ultimately\ngoing to matter the most.",
"start": 7713.51,
"duration": 3.82
},
{
"text": "So next week, we will take these\nideas that we first saw in Scratch",
"start": 7717.33,
"duration": 3.595
},
{
"text": "and have now translated\nto C. And we'll start",
"start": 7720.925,
"duration": 1.875
},
{
"text": "to introduce the first of the\ncourse's real world domains.",
"start": 7722.8,
"duration": 2.94
},
{
"text": ">> We'll focus on the world of security,\nand more specifically cryptography,",
"start": 7725.74,
"duration": 4.4
},
{
"text": "the art of scrambling information.",
"start": 7730.14,
"duration": 1.84
},
{
"text": "And among the first\nproblems you yourself",
"start": 7731.98,
"duration": 2.02
},
{
"text": "will get to write beyond\nplaying with some of the syntax",
"start": 7734.0,
"duration": 2.84
},
{
"text": "and solving some logical\nproblems, ultimately before long,",
"start": 7736.84,
"duration": 3.04
},
{
"text": "is to actually scramble, or encrypt,\nand ultimately decrypt information.",
"start": 7739.88,
"duration": 4.08
},
{
"text": "And everything we've done\ntoday, will fairly low",
"start": 7743.96,
"duration": 2.51
},
{
"text": "level, is just going to allow\nus to take one, and one,",
"start": 7746.47,
"duration": 2.72
},
{
"text": "and one more step above toward\nwriting the most interesting code yet.",
"start": 7749.19,
"duration": 4.36
},
{
"text": ">> So more on that next week.",
"start": 7753.55,
"duration": 1.5
},
{
"text": ">> [VIDEO PLAYBACK]",
"start": 7757.834,
"duration": 0.928
},
{
"text": ">> -What can you tell me about\nthe last time you saw him?",
"start": 7759.69,
"duration": 2.316
},
{
"text": "-What can I say, really?",
"start": 7766.041,
"duration": 0.999
},
{
"text": "I mean, it was like any other\npre-production rehearsal,",
"start": 7770.5,
"duration": 4.84
},
{
"text": "except there was something he said\nat the very end that stuck with me.",
"start": 7775.34,
"duration": 5.17
},
{
"text": ">> -This was CS50.",
"start": 7784.81,
"duration": 1.83
},
{
"text": ">> -That's a cut everyone,\ngreat job on rehearsal.",
"start": 7789.44,
"duration": 2.75
},
{
"text": ">> -That's lunch?",
"start": 7792.19,
"duration": 0.88
},
{
"text": ">> -Yeah, you and I can\ngrab a sandwich in a bit.",
"start": 7793.07,
"duration": 1.916
},
{
"text": "Let me just debrief with\nDavid really quickly.",
"start": 7794.986,
"duration": 3.394
},
{
"text": "David?",
"start": 7798.38,
"duration": 0.78
},
{
"text": "David?",
"start": 7799.16,
"duration": 2.1
},
{
"text": ">> [END PLAYBACK]",
"start": 7801.26,
"duration": 1.85
}
]