Subtitles section Play video Print subtitles SPEAKER 1: This is CS50, and this is week 1. And by the end of the day, you will know how to create programs that look like this. So this, of course, is binary. This is the only language that machines ultimately understand. But thankfully, per last week, there's so many abstractions and there are so many humans that have come before us that we don't actually have to write anything at this level. We can abstract way above it like we did with Scratch already and like we will starting today with C. But does anyone nonetheless want to take a guess at what that program, when fed to your Mac or PC, actually does? Anyone recognize? Anyone want to hazard a guess? It's perhaps the simplest program you could write. Indeed, it does, when fed to the brain of your computer, the so-called CPU simply prints that. So how do we actually get to that point? Well, recall where we started this conversation last time talking about computer science more generally and problem solving-- we proposed could be distilled really is this. You've got some inputs. You want some outputs. And somewhere in the middle, you need to do something with those inputs. And to get to that point, though, we had to represent those inputs and outputs. We just had to decide as humans, how are we going to represent all of the inputs to our problem when it comes time to have a computer actually process them. And at the end of the day, all of the phones and the computers that we're all using only at the end of the day plug into the wall to get their physical resource, electricity, and they might store that temporarily in a battery. But that really is our only input. It's either plugged in or it's not. It's either a 1 or a 0, true or false. So the world really reduces to those two states, so to speak. And so you can think of those states then as just being like a light bulb, on or off. I pulled up my cell phone last time to turn the flashlight on or off, 1 or 0, true or false. Now of course, if you only have one light bulb, you can only count from 0 to 1. But if you start to have a bunch of them back to back to back to back, you can permute them like I did my finger-- 0, 1, 2, 3, and so forth. And so we started talking about binary more generally. And so here for instance were three sequences of 0's and 1's and each of those represented something, but we don't need to think about the world at that level. We can abstract on top of that. All of us are so much more familiar with decimal of course, and indeed recall that this was just 72, 73, and 33, which if anyone recalls, when you use ASCII-- which is this global standard for mapping numbers to letters-- we got what message? Yeah, it was just high, capital H capital I exclamation point. And so that's an abstraction on top of those otherwise binary numbers. But we don't have to model just text using numbers. At the end of the day, our only resource is still that electricity, and the only way we think about it digitally is still zeros and ones. But if we take the same value-- 72, 73, 33-- and treat them in the context of Photoshop or a photo program or a graphics program, we can instead interpret them as like some amount of red, some amount of green, some amount of blue, which gave us last time, recall, this yellowish color. So now we had another abstraction on top of binary colors, and this is just one pixel. What can you do once you have more than one pixel? What can you represent next? Yeah, right, images. So we're continuing the conversation up and up and up, and we could represents something like a graphical emoji on the screen, which has more than just one yellow dot. It's got a whole bunch of yellow dots and other colors as well. And recall that, if we want to animate things, whether it's through silly things like animojis on a phone or just more proper videos and movies, well, those are just sequences of images flying past your human eyes really quite quickly. So that's where we kind of left off last time starting at the base level and abstracting away so that we could stipulate thereafter we can represent inputs, and we can represent outputs, whatever those happen to be. And here on out, we don't need to think at that level. We can just assume we all know how to do this. And even if it eventually becomes kind of a distant memory, we know that someone can indeed do this. And that's the value of abstraction. But inside of this black box are so-called algorithms, the secret sauce-- this is where the problems are actually solved. And we not only talked about what these algorithms are, but for instance, how efficient they were. So recall that this red line represented a very simple algorithm just turning the phone book page by page one at a time. And the reason that it's a straight line is because there's a one to one correspondence between how many pages there are in the book and how many page turns there are-- one page, one more page, one more turn, and so forth. If I fly through it at twice the speed-- 2, 4, 6, 8-- I can do better. And so that yellow line now, recall, was lower on the graph. If you just look at any two points, yellow and red, yellow is below red, saying it takes less time. But it was not quite correct. There was one bug when I was looking for Mike two pages at a time. What was that issue? Yeah, I might miss him. He might accidentally get sandwiched in between two pages-- not a huge deal because I could fix it, but I have to fix it. I have to apply that additional logic and double back at least a page if I go too fast. But of course the final algorithm-- and frankly all of our initial intuition probably-- was the dividing and conquer, open it roughly to the middle, look down, and then go left, and go right, and just repeat that process as the problem gets this big to this big to this big to this big to just one page left. So that was all about efficiency. But to get to that point we needed to express ourselves more precisely. And so we introduced pseudo code. There's no formal definition. It can be English, English like. It's just meant to be succinct and get the point across. And recall that, along the way, we introduced a whole bunch of concepts, many of which you probably experimented with Scratch, like loops and conditions, Boolean expressions, variables, and so forth. And those were building blocks that came out of this kind of demonstration here. But honestly, even in this demonstration, in this pseudo code, there were a whole bunch of assumptions. If you read these instructions one at a time and you're holding the phone book yourself, odds are you can execute this pseudocode, this algorithm. But what does it really mean to, say, open to the middle of the phone book? All of us have an intuitive understanding of what that means. But honestly, if you were explaining that to a kid or someone who's learning English or whatever language for the first time, open to the middle of the phone book, you should probably sets forth some assumptions. OK, this thing in front of you has 1,000 pages, pieces of paper. Turn to the 500th page, and let's call that the middle. This would very quickly get tedious if all of us humans are talking at that level of detail. And so we abstract away with more sweeping statements like open to the middle of the phone book, but that's an abstraction. And it's not quite as precise as is probably ideal, especially feeding this algorithm to a newbie or to a robot or a computer. But it's useful because we can then make a 12 step program instead of a 20 step program by elaborating too much. And for instance, throughout here too we had our loops and conditions and so forth, but even call Mike. What does that mean? Well, if you imagine that the human knows how to use the phone, then it goes without saying. But if he or she also needs to be programmed to use the phone, you've got to explain-- pick it up, hit this button, type this sequence of buttons, and so forth. So call Mike is also an abstraction. So these abstractions are useful, but they can sometimes get in the way, especially if you're not precise enough to program the computer correctly. And to paint this picture, I thought we could begin a little heartedly here. I brought some breakfast, if you didn't quite make it next door or beyond. Just need a couple of volunteers if you're comfortable appearing on stage and on the internet here. Let me kind of-- there's a lot of lights here. How about over there on the left and over here in the front? Yeah, right there. I think your hand was up.