{"captions":[{"content":"[Review] [Quiz 0]","startTime":0,"duration":3000,"startOfParagraph":false},{"content":"[Lexi Ross, Tommy MacWilliam, Lucas Freitas, Joseph Ong] [Harvard University]","startTime":3000,"duration":2000,"startOfParagraph":true},{"content":"[This is CS50.] [CS50.TV]","startTime":5000,"duration":3000,"startOfParagraph":true},{"content":"Hey, everyone. ","startTime":8000,"duration":2000,"startOfParagraph":true},{"content":"Welcome to the review session for Quiz 0, which is taking place this Wednesday.","startTime":10000,"duration":5000,"startOfParagraph":false},{"content":"What we're going to do tonight, I'm with 3 other TFs,","startTime":15000,"duration":4000,"startOfParagraph":false},{"content":"and together we're going to go through a review of what we've done in the course so far.","startTime":19000,"duration":5000,"startOfParagraph":false},{"content":"It's not going to be 100% comprehensive, but it should give you a better idea","startTime":24000,"duration":3000,"startOfParagraph":false},{"content":"of what you already have down and what you still need to study before Wednesday.","startTime":27000,"duration":4000,"startOfParagraph":false},{"content":"And feel free to raise your hand with questions as we're going along,","startTime":31000,"duration":3000,"startOfParagraph":false},{"content":"but keep in mind that we'll also have a little bit of time at the end—","startTime":34000,"duration":4000,"startOfParagraph":false},{"content":"if we get through with a few minutes to spare—to do general questions,","startTime":38000,"duration":3000,"startOfParagraph":false},{"content":"so keep that in mind, and so we're going to start at the beginning with Week 0.","startTime":41000,"duration":6000,"startOfParagraph":false},{"content":"[Quiz 0 Review!] [Part 0] [Lexi Ross] But before we do that let's talk about","startTime":47000,"duration":3000,"startOfParagraph":true},{"content":"the logistics of the quiz.","startTime":50000,"duration":3000,"startOfParagraph":false},{"content":"[Logistics] [Quiz takes place on Wednesday 10/10 in lieu of lecture] ","startTime":53000,"duration":2000,"startOfParagraph":true},{"content":"[(See http://cdn.cs50.net/2012/fall/quizzes/0/about0.pdf for details)] It is on Wednesday, October 10th. ","startTime":55000,"duration":2000,"startOfParagraph":true},{"content":"That's this Wednesday, and if you go to this URL here,","startTime":57000,"duration":3000,"startOfParagraph":true},{"content":"which is also accessible from CS50.net—there's a link to it—","startTime":60000,"duration":3000,"startOfParagraph":false},{"content":"you can see information about where to go based on ","startTime":63000,"duration":3000,"startOfParagraph":false},{"content":"your last name or school affiliation as well as","startTime":66000,"duration":4000,"startOfParagraph":false},{"content":"it tells about exactly what the quiz will cover and the types of questions that you're going to get.","startTime":70000,"duration":4000,"startOfParagraph":false},{"content":"Keep in mind that you'll also have a chance to review for the quiz in section,","startTime":74000,"duration":5000,"startOfParagraph":false},{"content":"so your TFs should be going over some practice problems,","startTime":79000,"duration":2000,"startOfParagraph":false},{"content":"and that's another good chance to see where you still need to study up for the quiz.","startTime":81000,"duration":8000,"startOfParagraph":false},{"content":"Let's start at the beginning with Bits 'n' Bytes.","startTime":89000,"duration":3000,"startOfParagraph":false},{"content":"Remember a bit is just a 0 or a 1, ","startTime":92000,"duration":3000,"startOfParagraph":false},{"content":"and a byte is a collection of 8 of those bits.","startTime":95000,"duration":3000,"startOfParagraph":false},{"content":"Let's look at this collection of bits right here.","startTime":98000,"duration":4000,"startOfParagraph":false},{"content":"We should be able to figure out how many bits there are.","startTime":102000,"duration":2000,"startOfParagraph":false},{"content":"Where we count there's just 8 of them, eight 0 or 1 units.","startTime":104000,"duration":4000,"startOfParagraph":false},{"content":"And since there's 8 bits, that's 1 byte,","startTime":108000,"duration":3000,"startOfParagraph":false},{"content":"and let's convert it to hexadecimal. ","startTime":111000,"duration":2000,"startOfParagraph":false},{"content":"Hexadecimal is base 16, and it's pretty easy to convert","startTime":113000,"duration":5000,"startOfParagraph":false},{"content":"a number in binary, which is what that is, to a number in hexadecimal.","startTime":118000,"duration":3000,"startOfParagraph":false},{"content":"All we do is we look at groups of 4, ","startTime":121000,"duration":3000,"startOfParagraph":false},{"content":"and we convert them to the appropriate hexadecimal digit.","startTime":124000,"duration":3000,"startOfParagraph":false},{"content":"We start with the right-most group of 4, so 0011.","startTime":127000,"duration":4000,"startOfParagraph":false},{"content":"That's going to be one 1 and one 2, so together that makes 3.","startTime":131000,"duration":5000,"startOfParagraph":false},{"content":"And then let's look at the other block of 4.","startTime":136000,"duration":3000,"startOfParagraph":false},{"content":"1101. That's going to be one 1, one 4, and one 8.","startTime":139000,"duration":5000,"startOfParagraph":false},{"content":"Together that's going to be 13, which makes D.","startTime":144000,"duration":4000,"startOfParagraph":false},{"content":"And we'll remember that in hexadecimal we don't just go 0 through 9.","startTime":148000,"duration":4000,"startOfParagraph":false},{"content":"We go 0 through F, so after 9, 10 corresponds to A,","startTime":152000,"duration":4000,"startOfParagraph":false},{"content":"11 to B, et cetera where F is 15.","startTime":156000,"duration":4000,"startOfParagraph":false},{"content":"Here 13 is a D,","startTime":160000,"duration":4000,"startOfParagraph":false},{"content":"so to convert it to decimal all we do is we actually ","startTime":164000,"duration":5000,"startOfParagraph":false},{"content":"treat each position as a power of 2.","startTime":169000,"duration":3000,"startOfParagraph":false},{"content":"That's one 1, one 2, zero 4s, zero 8s, one 16, et cetera,","startTime":172000,"duration":6000,"startOfParagraph":false},{"content":"and it's a little hard to compute in your head, but if we go to the next slide","startTime":178000,"duration":5000,"startOfParagraph":false},{"content":"we can see the answer to that. ","startTime":183000,"duration":2000,"startOfParagraph":false},{"content":"Essentially we're going across from right back to left,","startTime":185000,"duration":4000,"startOfParagraph":true},{"content":"and we're multiplying each digit by the corresponding power of 2.","startTime":189000,"duration":5000,"startOfParagraph":false},{"content":"And remember, for hexadecimal we denote these numbers with 0x at the beginning","startTime":194000,"duration":5000,"startOfParagraph":false},{"content":"so we don't confuse it with a decimal number.","startTime":199000,"duration":4000,"startOfParagraph":false},{"content":"Continuing on, this is an ASCII Table,","startTime":203000,"duration":6000,"startOfParagraph":false},{"content":"and what we use ASCII for is to map from characters to numerical values.","startTime":209000,"duration":6000,"startOfParagraph":false},{"content":"Remember in the cryptography pset we made extensive use of the ASCII Table","startTime":215000,"duration":4000,"startOfParagraph":false},{"content":"in order to use various methods of cryptography, ","startTime":219000,"duration":4000,"startOfParagraph":false},{"content":"the Caesar and the Vigenère cipher, to convert different letters","startTime":223000,"duration":4000,"startOfParagraph":false},{"content":"in a string according to the key given by the user.","startTime":227000,"duration":5000,"startOfParagraph":false},{"content":"Let's look at a little bit of ASCII math.","startTime":232000,"duration":4000,"startOfParagraph":false},{"content":"Looking at 'P' + 1, in character form that would be Q,","startTime":236000,"duration":6000,"startOfParagraph":false},{"content":"and remember that '5' ≠ 5.","startTime":242000,"duration":5000,"startOfParagraph":false},{"content":"And how exactly would we convert between those 2 forms?","startTime":247000,"duration":3000,"startOfParagraph":false},{"content":"It's not actually too hard.","startTime":250000,"duration":3000,"startOfParagraph":false},{"content":"In order to get 5 we subtract '0'","startTime":253000,"duration":3000,"startOfParagraph":false},{"content":"because there are 5 places between the '0' and the '5.'","startTime":256000,"duration":4000,"startOfParagraph":false},{"content":"In order to go the other way we just add the 0,","startTime":260000,"duration":3000,"startOfParagraph":false},{"content":"so it's sort of like regular arithmetic. ","startTime":263000,"duration":2000,"startOfParagraph":false},{"content":"Just remember that when something has quotes around it it's a character","startTime":265000,"duration":4000,"startOfParagraph":false},{"content":"and thus corresponds to a value in the ASCII table.","startTime":269000,"duration":8000,"startOfParagraph":false},{"content":"Moving into more general computer science topics.","startTime":277000,"duration":3000,"startOfParagraph":false},{"content":"We learned what an algorithm is and how we use programming","startTime":280000,"duration":3000,"startOfParagraph":false},{"content":"to implement algorithms.","startTime":283000,"duration":2000,"startOfParagraph":false},{"content":"Some examples of algorithms are something really simple like ","startTime":285000,"duration":3000,"startOfParagraph":false},{"content":"checking whether a number is even or odd.","startTime":288000,"duration":3000,"startOfParagraph":false},{"content":"For that remember we mod the number by 2 and check if the result is 0.","startTime":291000,"duration":3000,"startOfParagraph":false},{"content":"If so, it's even. If not, it's odd.","startTime":294000,"duration":3000,"startOfParagraph":false},{"content":"And that's an example of a really basic algorithm.","startTime":297000,"duration":2000,"startOfParagraph":false},{"content":"A little bit of a more involved one is binary search,","startTime":299000,"duration":3000,"startOfParagraph":true},{"content":"which we'll go over later in the review session.","startTime":302000,"duration":3000,"startOfParagraph":false},{"content":"And programming is the term we use for taking an algorithm","startTime":305000,"duration":4000,"startOfParagraph":false},{"content":"and converting it to code the computer can read.","startTime":309000,"duration":6000,"startOfParagraph":false},{"content":"2 examples of programming is Scratch,","startTime":315000,"duration":5000,"startOfParagraph":false},{"content":"which is what we did in Week 0.","startTime":320000,"duration":2000,"startOfParagraph":false},{"content":"Even though we don't actually type out the code it's a way of implementing","startTime":322000,"duration":3000,"startOfParagraph":false},{"content":"this algorithm, which is printing the numbers 1-10,","startTime":325000,"duration":4000,"startOfParagraph":false},{"content":"and here we do the same in the C programming language.","startTime":329000,"duration":3000,"startOfParagraph":false},{"content":"These are functionally equivalent, just written in different languages or syntax.","startTime":332000,"duration":9000,"startOfParagraph":false},{"content":"We then learned about boolean expressions,","startTime":341000,"duration":3000,"startOfParagraph":false},{"content":"and a boolean is a value that's either true or false,","startTime":344000,"duration":4000,"startOfParagraph":false},{"content":"and here oftentimes boolean expressions","startTime":348000,"duration":3000,"startOfParagraph":false},{"content":"go inside of conditions, so if (x ≤ 5),","startTime":351000,"duration":4000,"startOfParagraph":false},{"content":"well, we already set x = 5, so that condition is going to evaluate to true.","startTime":355000,"duration":5000,"startOfParagraph":false},{"content":"And if it's true, whatever code is beneath the condition","startTime":360000,"duration":3000,"startOfParagraph":false},{"content":"is going to be evaluated by the computer, so that string is going to be printed","startTime":363000,"duration":5000,"startOfParagraph":false},{"content":"to standard output, and the term condition","startTime":368000,"duration":4000,"startOfParagraph":false},{"content":"refers to whatever is inside the parentheses of the if statement.","startTime":372000,"duration":4000,"startOfParagraph":false},{"content":"Remember all the operators.","startTime":376000,"duration":4000,"startOfParagraph":false},{"content":"Remember it's && and | | when we're trying to combine 2 or more conditions,","startTime":380000,"duration":6000,"startOfParagraph":false},{"content":"== not = to check whether 2 things are equal.","startTime":386000,"duration":4000,"startOfParagraph":false},{"content":"Remember that = is for assignment whereas == is a boolean operator.","startTime":390000,"duration":6000,"startOfParagraph":false},{"content":"≤, ≥ and then the final 2 are self-explanatory.","startTime":396000,"duration":5000,"startOfParagraph":false},{"content":"A general review of boolean logic here. ","startTime":401000,"duration":4000,"startOfParagraph":false},{"content":"And boolean expressions are also important in loops, ","startTime":405000,"duration":3000,"startOfParagraph":false},{"content":"which we'll go over now.","startTime":408000,"duration":2000,"startOfParagraph":false},{"content":"We learned about 3 types of loops so far in CS50, for, while, and do while.","startTime":410000,"duration":6000,"startOfParagraph":false},{"content":"And it's important to know that while for most purposes","startTime":416000,"duration":3000,"startOfParagraph":false},{"content":"we can actually use any type of loop generally","startTime":419000,"duration":3000,"startOfParagraph":false},{"content":"there are certain types of purposes or common patterns","startTime":422000,"duration":4000,"startOfParagraph":false},{"content":"in programming that specifically call for one of these loops","startTime":426000,"duration":3000,"startOfParagraph":false},{"content":"that make it the most efficient or elegant to code it in that way.","startTime":429000,"duration":4000,"startOfParagraph":false},{"content":"Let's go over what each of these loops tends to be used for most often.","startTime":433000,"duration":5000,"startOfParagraph":false},{"content":"In a for loop we generally already know how many times we want to iterate.","startTime":438000,"duration":3000,"startOfParagraph":true},{"content":"That's what we put in the condition.","startTime":441000,"duration":3000,"startOfParagraph":false},{"content":"For, i = 0, i < 10, for example.","startTime":444000,"duration":4000,"startOfParagraph":false},{"content":"We already know that we want to do something 10 times.","startTime":448000,"duration":3000,"startOfParagraph":false},{"content":"Now, for a while loop, generally we don't necessarily ","startTime":451000,"duration":3000,"startOfParagraph":false},{"content":"know how many times we want the loop to run.","startTime":454000,"duration":2000,"startOfParagraph":false},{"content":"But we do know some sort of condition that we want it to","startTime":456000,"duration":3000,"startOfParagraph":false},{"content":"always be true or always be false.","startTime":459000,"duration":2000,"startOfParagraph":false},{"content":"For example, while is set.","startTime":461000,"duration":3000,"startOfParagraph":false},{"content":"Let's say that's a boolean variable.","startTime":464000,"duration":2000,"startOfParagraph":false},{"content":"While that's true we want the code to evaluate, ","startTime":466000,"duration":2000,"startOfParagraph":false},{"content":"so a little bit more extensible, a little bit more general than a for loop,","startTime":468000,"duration":4000,"startOfParagraph":false},{"content":"but any for loop can also be converted to a while loop.","startTime":472000,"duration":3000,"startOfParagraph":false},{"content":"Finally, do while loops, which may be the trickiest to comprehend right away,","startTime":475000,"duration":5000,"startOfParagraph":false},{"content":"are often used when we want to evaluate the code first","startTime":480000,"duration":4000,"startOfParagraph":false},{"content":"before the first time we check the condition.","startTime":484000,"duration":2000,"startOfParagraph":false},{"content":"A common use case for a do while loop","startTime":486000,"duration":3000,"startOfParagraph":false},{"content":"is when you want to get user input, and you know you want to ask the user","startTime":489000,"duration":3000,"startOfParagraph":false},{"content":"for input at least once, but if they don't give you good input right away","startTime":492000,"duration":3000,"startOfParagraph":false},{"content":"you want to keep asking them until they give you the good input.","startTime":495000,"duration":3000,"startOfParagraph":false},{"content":"That's the most common use of a do while loop,","startTime":498000,"duration":3000,"startOfParagraph":false},{"content":"and let's look at the actual structure of these loops.","startTime":501000,"duration":2000,"startOfParagraph":false},{"content":"They typically always tend to follow these patterns.","startTime":503000,"duration":4000,"startOfParagraph":false},{"content":"On the for loop inside you have 3 components:","startTime":507000,"duration":3000,"startOfParagraph":true},{"content":"initialization, typically something like int i = 0 where i is the counter, ","startTime":510000,"duration":5000,"startOfParagraph":false},{"content":"condition, where we want to say run this for loop as long as this condition still holds,","startTime":515000,"duration":5000,"startOfParagraph":false},{"content":"like i < 10, and then finally, update, which is how we increment ","startTime":520000,"duration":4000,"startOfParagraph":false},{"content":"the counter variable at each point in the loop.","startTime":524000,"duration":3000,"startOfParagraph":false},{"content":"A common thing to see there is just i++,","startTime":527000,"duration":3000,"startOfParagraph":false},{"content":"which means increment i by 1 every time.","startTime":530000,"duration":2000,"startOfParagraph":false},{"content":"You could also do something like i+ = 2,","startTime":532000,"duration":3000,"startOfParagraph":false},{"content":"which means add 2 to i each time you go through the loop.","startTime":535000,"duration":3000,"startOfParagraph":false},{"content":"And then the do this just refers to any code that actually runs as part of the loop.","startTime":538000,"duration":5000,"startOfParagraph":false},{"content":"And for a while loop, this time we actually have the initialization outside of the loop,","startTime":543000,"duration":6000,"startOfParagraph":false},{"content":"so for example, let's say we're trying to do the same type of loop as I just described.","startTime":549000,"duration":3000,"startOfParagraph":false},{"content":"We would say int i = 0 before the loop begins.","startTime":552000,"duration":4000,"startOfParagraph":false},{"content":"Then we could say while i < 10 do this,","startTime":556000,"duration":4000,"startOfParagraph":false},{"content":"so the same block of code as before,","startTime":560000,"duration":2000,"startOfParagraph":false},{"content":"and this time the update part of the code, for example, i++,","startTime":562000,"duration":4000,"startOfParagraph":false},{"content":"actually goes inside of the loop.","startTime":566000,"duration":3000,"startOfParagraph":false},{"content":"And finally, for a do while, it's similar to the while loop,","startTime":569000,"duration":4000,"startOfParagraph":false},{"content":"but we have to remember that the code will evaluate once","startTime":573000,"duration":3000,"startOfParagraph":false},{"content":"before the condition is checked, so it makes a lot more sense","startTime":576000,"duration":4000,"startOfParagraph":false},{"content":"if you look at it in order of top to bottom.","startTime":580000,"duration":4000,"startOfParagraph":false},{"content":"In a do while loop the code evaluates before you even look at the while condition,","startTime":584000,"duration":5000,"startOfParagraph":false},{"content":"whereas a while loop, it checks first.","startTime":589000,"duration":6000,"startOfParagraph":false},{"content":"Statements and variables.","startTime":595000,"duration":4000,"startOfParagraph":false},{"content":"When we want to create a new variable we first want to initialize it.","startTime":599000,"duration":5000,"startOfParagraph":false},{"content":"For example, int bar initializes the variable bar,","startTime":604000,"duration":3000,"startOfParagraph":true},{"content":"but it doesn't give it a value, so what is bar's value now?","startTime":607000,"duration":3000,"startOfParagraph":false},{"content":"We don't know. ","startTime":610000,"duration":2000,"startOfParagraph":false},{"content":"It could be some garbage value that was previously stored in memory there,","startTime":612000,"duration":2000,"startOfParagraph":false},{"content":"and we don't want to use that variable","startTime":614000,"duration":2000,"startOfParagraph":false},{"content":"until we actually give it a value, ","startTime":616000,"duration":3000,"startOfParagraph":false},{"content":"so we declare it here. ","startTime":619000,"duration":2000,"startOfParagraph":false},{"content":"Then we initialize it to be 42 below.","startTime":621000,"duration":3000,"startOfParagraph":false},{"content":"Now, of course, we know this can be done on one line, int bar = 42.","startTime":624000,"duration":4000,"startOfParagraph":false},{"content":"But just to be clear the multiple steps that are going on,","startTime":628000,"duration":2000,"startOfParagraph":false},{"content":"the declaration and the initialization are happening separately here.","startTime":630000,"duration":4000,"startOfParagraph":false},{"content":"It happens on one step, and the next one, int baz = bar + 1,","startTime":634000,"duration":4000,"startOfParagraph":false},{"content":"this statement below, that increments baz, so at the end of this code block","startTime":638000,"duration":6000,"startOfParagraph":false},{"content":"if we were to print the value of baz it would be 44","startTime":644000,"duration":4000,"startOfParagraph":false},{"content":"because we declare and initialize it to be 1 > bar, ","startTime":648000,"duration":4000,"startOfParagraph":false},{"content":"and then we increment it once more with the ++.","startTime":652000,"duration":6000,"startOfParagraph":false},{"content":"We went over this pretty briefly, but it's good to have a general ","startTime":658000,"duration":4000,"startOfParagraph":false},{"content":"understanding of what threads and events are.","startTime":662000,"duration":2000,"startOfParagraph":false},{"content":"We mainly did this in Scratch,","startTime":664000,"duration":2000,"startOfParagraph":false},{"content":"so you can think of threads as multiple sequences of code","startTime":666000,"duration":3000,"startOfParagraph":false},{"content":"running at the same time.","startTime":669000,"duration":2000,"startOfParagraph":false},{"content":"In actuality, it probably isn't running at the same time,","startTime":671000,"duration":3000,"startOfParagraph":false},{"content":"but sort of abstractly we can think of it in that way.","startTime":674000,"duration":3000,"startOfParagraph":false},{"content":"In Scratch, for example, we had the multiple sprites.","startTime":677000,"duration":3000,"startOfParagraph":true},{"content":"It could be executing different code at the same time.","startTime":680000,"duration":2000,"startOfParagraph":false},{"content":"One could be walking while the other is saying something ","startTime":682000,"duration":4000,"startOfParagraph":false},{"content":"in a different part of the screen.","startTime":686000,"duration":3000,"startOfParagraph":false},{"content":"Events are another way of separating out the logic","startTime":689000,"duration":5000,"startOfParagraph":false},{"content":"between different elements of your code,","startTime":694000,"duration":3000,"startOfParagraph":false},{"content":"and in Scratch we were able to simulate events using the Broadcast,","startTime":697000,"duration":3000,"startOfParagraph":false},{"content":"and that's actually When I Receive, not When I Hear,","startTime":700000,"duration":3000,"startOfParagraph":false},{"content":"but essentially it's a way to transmit information","startTime":703000,"duration":4000,"startOfParagraph":false},{"content":"from one sprite to another.","startTime":707000,"duration":2000,"startOfParagraph":false},{"content":"For example, you may want to transmit game over,","startTime":709000,"duration":3000,"startOfParagraph":false},{"content":"and when another sprite receives game over,","startTime":712000,"duration":4000,"startOfParagraph":false},{"content":"it responds in a certain way.","startTime":716000,"duration":2000,"startOfParagraph":false},{"content":"It's an important model to understand for programming.","startTime":718000,"duration":5000,"startOfParagraph":false},{"content":"Just to go over the basic Week 0, what we've gone over so far,","startTime":723000,"duration":4000,"startOfParagraph":false},{"content":"let's look at this simple C program.","startTime":727000,"duration":3000,"startOfParagraph":false},{"content":"The text may be a little bit small from here, but I'll go over it really quick.","startTime":730000,"duration":4000,"startOfParagraph":false},{"content":"We're including 2 header files at the top, cs50.h and stdio.h.","startTime":734000,"duration":6000,"startOfParagraph":false},{"content":"We're then defining a constant called limit to be 100.","startTime":740000,"duration":3000,"startOfParagraph":false},{"content":"We're then implementing our main function.","startTime":743000,"duration":3000,"startOfParagraph":false},{"content":"Since we don't use command line arguments here we need to put void","startTime":746000,"duration":3000,"startOfParagraph":false},{"content":"as the arguments for main.","startTime":749000,"duration":3000,"startOfParagraph":false},{"content":"We see int above main. That's the return type, hence return 0 at the bottom.","startTime":752000,"duration":6000,"startOfParagraph":false},{"content":"And we're using CS50 library function get int","startTime":758000,"duration":3000,"startOfParagraph":false},{"content":"to ask the user for input, and we store it in this variable x,","startTime":761000,"duration":4000,"startOfParagraph":false},{"content":"so we declare x above, and we initialize it with x = GetInt.","startTime":765000,"duration":6000,"startOfParagraph":false},{"content":"We then check to see if the user gave us good input.","startTime":771000,"duration":2000,"startOfParagraph":true},{"content":"If it's ≥ LIMIT we want to return an error code of 1 and print an error message.","startTime":773000,"duration":6000,"startOfParagraph":false},{"content":"And finally, if the user has given us good input","startTime":779000,"duration":3000,"startOfParagraph":false},{"content":"we're going to square the number and print out that result.","startTime":782000,"duration":6000,"startOfParagraph":false},{"content":"Just to make sure that those all hit home","startTime":788000,"duration":3000,"startOfParagraph":false},{"content":"you can see the labels of different parts of the code here.","startTime":791000,"duration":6000,"startOfParagraph":false},{"content":"I mentioned constant, header files.","startTime":797000,"duration":2000,"startOfParagraph":false},{"content":"Oh, int x. Make sure to remember that's a local variable.","startTime":799000,"duration":2000,"startOfParagraph":false},{"content":"That contrasts it from a global variable, which we'll talk about ","startTime":801000,"duration":3000,"startOfParagraph":false},{"content":"a little bit later in the review session,","startTime":804000,"duration":3000,"startOfParagraph":false},{"content":"and we are calling the library function printf, ","startTime":807000,"duration":3000,"startOfParagraph":false},{"content":"so if we hadn't included the stdio.h header file","startTime":810000,"duration":4000,"startOfParagraph":false},{"content":"we would not be able to call printf.","startTime":814000,"duration":3000,"startOfParagraph":false},{"content":"And I believe the arrow that got cut off here is pointing to the %d,","startTime":817000,"duration":5000,"startOfParagraph":false},{"content":"which is a formatting string in printf.","startTime":822000,"duration":3000,"startOfParagraph":false},{"content":"It says print out this variable as a number, %d.","startTime":825000,"duration":7000,"startOfParagraph":false},{"content":"And that is it for Week 0.","startTime":832000,"duration":6000,"startOfParagraph":false},{"content":"Now Lucas is going to continue.","startTime":838000,"duration":8000,"startOfParagraph":false},{"content":"Hey, guys. My name is Lucas.","startTime":846000,"duration":2000,"startOfParagraph":false},{"content":"I'm a sophomore in the best house on campus, Mather, ","startTime":848000,"duration":2000,"startOfParagraph":false},{"content":"and I'm going to talk a little bit about Week 1 and 2.1.","startTime":850000,"duration":4000,"startOfParagraph":false},{"content":"[Week 1 and 2.1!] [Lucas Freitas]","startTime":854000,"duration":2000,"startOfParagraph":false},{"content":"As Lexi was saying, when we started translating your code from Scratch to C","startTime":856000,"duration":3000,"startOfParagraph":false},{"content":"one of the things that we noticed is that you cannot just","startTime":859000,"duration":4000,"startOfParagraph":false},{"content":"write your code and run it using a green flag anymore.","startTime":863000,"duration":3000,"startOfParagraph":false},{"content":"Actually, you have to use some steps to make your C program","startTime":866000,"duration":4000,"startOfParagraph":false},{"content":"become an executable file.","startTime":870000,"duration":3000,"startOfParagraph":false},{"content":"Basically what you do when you're writing a program is that","startTime":873000,"duration":3000,"startOfParagraph":false},{"content":"you translate your idea into a language that a compiler can understand,","startTime":876000,"duration":4000,"startOfParagraph":false},{"content":"so when you're writing a program in C","startTime":880000,"duration":4000,"startOfParagraph":false},{"content":"what you're doing is actually writing something that your compiler is going to understand,","startTime":884000,"duration":3000,"startOfParagraph":false},{"content":"and then the compiler is going to translate that code","startTime":887000,"duration":3000,"startOfParagraph":false},{"content":"into something that your computer will understand.","startTime":890000,"duration":3000,"startOfParagraph":false},{"content":"And the thing is, your computer is actually very dumb.","startTime":893000,"duration":2000,"startOfParagraph":true},{"content":"Your computer can only understand 0s and 1s,","startTime":895000,"duration":2000,"startOfParagraph":false},{"content":"so actually in the first computers people usually programmed ","startTime":897000,"duration":4000,"startOfParagraph":false},{"content":"using 0s and 1s, but not anymore, thank God.","startTime":901000,"duration":3000,"startOfParagraph":false},{"content":"We don't have to memorize the sequences for 0s and 1s ","startTime":904000,"duration":3000,"startOfParagraph":false},{"content":"for a for loop or for a while loop and so on.","startTime":907000,"duration":3000,"startOfParagraph":false},{"content":"That's why we have a compiler.","startTime":910000,"duration":3000,"startOfParagraph":false},{"content":"What a compiler does is it basically translates the C code, ","startTime":913000,"duration":4000,"startOfParagraph":false},{"content":"in our case, to a language that your computer will understand,","startTime":917000,"duration":4000,"startOfParagraph":false},{"content":"which is the object code, and the compiler that we're using","startTime":921000,"duration":4000,"startOfParagraph":false},{"content":"is called clang, so this is actually the symbol for clang.","startTime":925000,"duration":5000,"startOfParagraph":false},{"content":"When you have your program, you have to do 2 things.","startTime":930000,"duration":3000,"startOfParagraph":false},{"content":"First, you have to compile your program, and then you're going to run your program.","startTime":933000,"duration":4000,"startOfParagraph":false},{"content":"To compile your program you have a lot of options to do so.","startTime":937000,"duration":4000,"startOfParagraph":false},{"content":"The first one is to do clang program.c","startTime":941000,"duration":3000,"startOfParagraph":false},{"content":"in which program is the name of your program.","startTime":944000,"duration":3000,"startOfParagraph":false},{"content":"In this case you can see they're just saying \"Hey, compile my program.\"","startTime":947000,"duration":4000,"startOfParagraph":false},{"content":"You're not saying \"I want this name for my program\" or anything.","startTime":951000,"duration":5000,"startOfParagraph":false},{"content":"The second option is giving a name to your program.","startTime":956000,"duration":2000,"startOfParagraph":true},{"content":"You can say clang -o and then the name that you want ","startTime":958000,"duration":4000,"startOfParagraph":false},{"content":"the executable file to be named as and then program.c.","startTime":962000,"duration":4000,"startOfParagraph":false},{"content":"And you can also do make program, and see how in the first 2 cases","startTime":966000,"duration":5000,"startOfParagraph":false},{"content":"I put .c, and in the third one I only have programs?","startTime":971000,"duration":4000,"startOfParagraph":false},{"content":"Yeah, you actually should not put .c when you use make.","startTime":975000,"duration":3000,"startOfParagraph":false},{"content":"Otherwise the compiler is actually going to yell at you.","startTime":978000,"duration":4000,"startOfParagraph":false},{"content":"And also, I don't know if you guys remember,","startTime":982000,"duration":2000,"startOfParagraph":false},{"content":"but a lot of times we also used -lcs50 or -lm.","startTime":984000,"duration":5000,"startOfParagraph":false},{"content":"That is called linking.","startTime":989000,"duration":2000,"startOfParagraph":false},{"content":"It just tells the compiler that you will use those libraries right there,","startTime":991000,"duration":4000,"startOfParagraph":false},{"content":"so if you want to use cs50.h you actually have to type","startTime":995000,"duration":4000,"startOfParagraph":false},{"content":"clang program.c -lcs50.","startTime":999000,"duration":4000,"startOfParagraph":false},{"content":"If you don't do that, the compiler is not going to know ","startTime":1003000,"duration":2000,"startOfParagraph":false},{"content":"that you're using those functions in cs50.h.","startTime":1005000,"duration":5000,"startOfParagraph":false},{"content":"And when you want to run your program you have 2 options.","startTime":1010000,"duration":2000,"startOfParagraph":false},{"content":"If you did clang program.c you didn't give a name to your program.","startTime":1012000,"duration":5000,"startOfParagraph":false},{"content":"You have to run it using ./a.out.","startTime":1017000,"duration":4000,"startOfParagraph":false},{"content":"A.out is a standard name that clang gives your program if you don't give it a name.","startTime":1021000,"duration":5000,"startOfParagraph":false},{"content":"Otherwise you're going to do ./program if you gave a name to your program,","startTime":1026000,"duration":5000,"startOfParagraph":false},{"content":"and also if you did make program the name that a program is going to get","startTime":1031000,"duration":4000,"startOfParagraph":false},{"content":"is already going to be programmed the same name as the c file.","startTime":1035000,"duration":8000,"startOfParagraph":false},{"content":"Then we talked about data types and data. ","startTime":1043000,"duration":3000,"startOfParagraph":false},{"content":"Basically data types are the same thing as little boxes they use","startTime":1046000,"duration":5000,"startOfParagraph":true},{"content":"to store values, so data types are actually just like Pokémons.","startTime":1051000,"duration":4000,"startOfParagraph":false},{"content":"They come in all sizes and types.","startTime":1055000,"duration":4000,"startOfParagraph":false},{"content":"I don't know if that analogy makes sense.","startTime":1059000,"duration":4000,"startOfParagraph":false},{"content":"The data size actually depends on the machine architecture.","startTime":1063000,"duration":3000,"startOfParagraph":false},{"content":"All the data sizes that I'm going to show here ","startTime":1066000,"duration":3000,"startOfParagraph":false},{"content":"are actually for a 32-bit machine, which is the case of our appliance,","startTime":1069000,"duration":4000,"startOfParagraph":false},{"content":"but if you are actually coding your Mac or in a Windows also","startTime":1073000,"duration":3000,"startOfParagraph":false},{"content":"probably you're going to have a 64-bit machine,","startTime":1076000,"duration":3000,"startOfParagraph":false},{"content":"so remember that the data sizes that I'm going to show here","startTime":1079000,"duration":4000,"startOfParagraph":false},{"content":"are for the 32-bit machine.","startTime":1083000,"duration":3000,"startOfParagraph":false},{"content":"The first one that we saw was an int, ","startTime":1086000,"duration":2000,"startOfParagraph":false},{"content":"which is pretty straightforward.","startTime":1088000,"duration":2000,"startOfParagraph":false},{"content":"You use int to store an integer.","startTime":1090000,"duration":3000,"startOfParagraph":false},{"content":"We also saw the character, the char.","startTime":1093000,"duration":3000,"startOfParagraph":false},{"content":"If you want to use a letter or a little symbol you're probably going to use a char.","startTime":1096000,"duration":4000,"startOfParagraph":false},{"content":"A char has 1 byte, which means 8 bits, like Lexi said.","startTime":1100000,"duration":6000,"startOfParagraph":false},{"content":"Basically we have an ASCII Table that has 256 ","startTime":1106000,"duration":5000,"startOfParagraph":false},{"content":"possible combinations of 0s and 1s,","startTime":1111000,"duration":3000,"startOfParagraph":false},{"content":"and then when you type a char it's going to translate","startTime":1114000,"duration":3000,"startOfParagraph":false},{"content":"the character that inputs you a number that you have in the ASCII table, like Lexi said.","startTime":1117000,"duration":7000,"startOfParagraph":false},{"content":"We also have the float, which we use to store decimal numbers.","startTime":1124000,"duration":4000,"startOfParagraph":false},{"content":"If you want to choose 3.14, for example, you're going to use a float","startTime":1128000,"duration":5000,"startOfParagraph":false},{"content":"or a double that has more precision.","startTime":1133000,"duration":2000,"startOfParagraph":false},{"content":"A float has 4 bytes.","startTime":1135000,"duration":2000,"startOfParagraph":false},{"content":"A double has 8 bytes, so the only difference is the precision.","startTime":1137000,"duration":4000,"startOfParagraph":false},{"content":"We also have a long that is used for integers,","startTime":1141000,"duration":3000,"startOfParagraph":false},{"content":"and you can see for a 32-bit machine an int and a long have the same size,","startTime":1144000,"duration":5000,"startOfParagraph":false},{"content":"so it doesn't really make sense to use a long in a 32-bit machine.","startTime":1149000,"duration":4000,"startOfParagraph":false},{"content":"But if you're using a Mac and 64-bit machine, actually a long has size 8,","startTime":1153000,"duration":4000,"startOfParagraph":true},{"content":"so it really depends on the architecture.","startTime":1157000,"duration":2000,"startOfParagraph":false},{"content":"For the 32-bit machine it doesn't make sense to use a long really.","startTime":1159000,"duration":3000,"startOfParagraph":false},{"content":"And then a long long, on the other hand, has 8 bytes,","startTime":1162000,"duration":3000,"startOfParagraph":false},{"content":"so it is very good if you want to have a longer integer.","startTime":1165000,"duration":5000,"startOfParagraph":false},{"content":"And finally, we have string, which is actually a char *, ","startTime":1170000,"duration":4000,"startOfParagraph":false},{"content":"which is a pointer to a char.","startTime":1174000,"duration":3000,"startOfParagraph":false},{"content":"It's very easy to think that the size of the string is going to be like ","startTime":1177000,"duration":3000,"startOfParagraph":false},{"content":"the number of characters that you have there,","startTime":1180000,"duration":2000,"startOfParagraph":false},{"content":"but actually the char * itself","startTime":1182000,"duration":3000,"startOfParagraph":false},{"content":"has the size of a pointer to a char, which is 4 bytes.","startTime":1185000,"duration":4000,"startOfParagraph":false},{"content":"The size of a char * is 4 bytes.","startTime":1189000,"duration":3000,"startOfParagraph":false},{"content":"It doesn't matter if you have a small word or a letter or anything.","startTime":1192000,"duration":4000,"startOfParagraph":false},{"content":"It's going to be 4 bytes.","startTime":1196000,"duration":2000,"startOfParagraph":false},{"content":"We also learned a little bit about casting,","startTime":1198000,"duration":3000,"startOfParagraph":false},{"content":"so as you can see, if you have, for example, a program that says","startTime":1201000,"duration":3000,"startOfParagraph":false},{"content":"int x = 3 and then printf(\"%d\", x/2)","startTime":1204000,"duration":4000,"startOfParagraph":false},{"content":"do you guys know what it's going to print on screen?","startTime":1208000,"duration":4000,"startOfParagraph":false},{"content":"Someone?>>[Students] 2.","startTime":1212000,"duration":2000,"startOfParagraph":true},{"content":"1.>>1, yeah.","startTime":1214000,"duration":2000,"startOfParagraph":false},{"content":"When you do 3/2 it's going to get 1.5,","startTime":1216000,"duration":4000,"startOfParagraph":false},{"content":"but since we're using an integer it's going to ignore the decimal part,","startTime":1220000,"duration":4000,"startOfParagraph":false},{"content":"and you're going to have 1.","startTime":1224000,"duration":2000,"startOfParagraph":false},{"content":"If you don't want that to happen what you can do, for example,","startTime":1226000,"duration":3000,"startOfParagraph":false},{"content":"is declare a float y = x.","startTime":1229000,"duration":4000,"startOfParagraph":false},{"content":"Then x that used to be 3 is now going to be 3.000 in y.","startTime":1233000,"duration":7000,"startOfParagraph":false},{"content":"And then you can print the y/2. ","startTime":1240000,"duration":4000,"startOfParagraph":false},{"content":"Actually, I should have a 2. over there.","startTime":1244000,"duration":6000,"startOfParagraph":false},{"content":"It's going to do 3.00/2.00,","startTime":1250000,"duration":5000,"startOfParagraph":false},{"content":"and you're going to get 1.5.","startTime":1255000,"duration":3000,"startOfParagraph":false},{"content":"And we have this .2f just to ask for 2 decimal units in the decimal part.","startTime":1258000,"duration":8000,"startOfParagraph":false},{"content":"If you have .3f it's going to have actually 1.500.","startTime":1266000,"duration":6000,"startOfParagraph":false},{"content":"If it's 2 it's going to be 1.50.","startTime":1272000,"duration":4000,"startOfParagraph":false},{"content":"We also have this case here.","startTime":1276000,"duration":2000,"startOfParagraph":false},{"content":"If you do float x = 3.14 and then you printf x","startTime":1278000,"duration":4000,"startOfParagraph":false},{"content":"you're going to get 3.14.","startTime":1282000,"duration":2000,"startOfParagraph":false},{"content":"And if you do x = the int of x,","startTime":1284000,"duration":5000,"startOfParagraph":false},{"content":"which means treat x as an int and you print x now","startTime":1289000,"duration":5000,"startOfParagraph":false},{"content":"you're going to have 3.00.","startTime":1294000,"duration":2000,"startOfParagraph":false},{"content":"Does that make sense?","startTime":1296000,"duration":2000,"startOfParagraph":false},{"content":"Because you're first treating x as an integer, so you're ignoring the decimal part,","startTime":1298000,"duration":3000,"startOfParagraph":false},{"content":"and then you're printing x.","startTime":1301000,"duration":4000,"startOfParagraph":false},{"content":"And finally, you can also do this,","startTime":1305000,"duration":2000,"startOfParagraph":false},{"content":"int x = 65, and then you declare a char c = x,","startTime":1307000,"duration":5000,"startOfParagraph":false},{"content":"and then if you print the c you're actually going to get ","startTime":1312000,"duration":4000,"startOfParagraph":false},{"content":"A, so basically what you're doing here ","startTime":1316000,"duration":3000,"startOfParagraph":false},{"content":"is translating the integer into the character,","startTime":1319000,"duration":3000,"startOfParagraph":false},{"content":"just like the ASCII Table does.","startTime":1322000,"duration":3000,"startOfParagraph":false},{"content":"We also talked about math operators.","startTime":1325000,"duration":3000,"startOfParagraph":false},{"content":"Most of them are pretty straightforward, so +, -, *, /, ","startTime":1328000,"duration":6000,"startOfParagraph":false},{"content":"and also we talked about mod, which is the remainder of a division of 2 numbers.","startTime":1334000,"duration":6000,"startOfParagraph":false},{"content":"If you have 10 % 3, for example,","startTime":1340000,"duration":3000,"startOfParagraph":false},{"content":"it means divide 10 by 3, and what is the remainder?","startTime":1343000,"duration":4000,"startOfParagraph":false},{"content":"It's going to be 1, so it's actually very useful for a lot of the programs.","startTime":1347000,"duration":3000,"startOfParagraph":false},{"content":"For Vigenère and Caesar I'm pretty sure that all of you guys used mod.","startTime":1350000,"duration":8000,"startOfParagraph":false},{"content":"About math operators, be very careful when combining * and /.","startTime":1358000,"duration":5000,"startOfParagraph":false},{"content":"For example, if you do (3/2) * 2 what are you going to get?","startTime":1363000,"duration":5000,"startOfParagraph":true},{"content":"[Students] 2.","startTime":1368000,"duration":2000,"startOfParagraph":false},{"content":"Yeah, 2, because 3/2 is going to be 1.5,","startTime":1370000,"duration":4000,"startOfParagraph":false},{"content":"but since you're doing operations between 2 integers","startTime":1374000,"duration":3000,"startOfParagraph":false},{"content":"you're actually just going to consider 1, ","startTime":1377000,"duration":2000,"startOfParagraph":false},{"content":"and then 1 * 2 is going to be 2, so be very, very careful","startTime":1379000,"duration":4000,"startOfParagraph":false},{"content":"when doing arithmetic with integers because ","startTime":1383000,"duration":4000,"startOfParagraph":false},{"content":"you might get that 2 = 3, in that case.","startTime":1387000,"duration":5000,"startOfParagraph":false},{"content":"And also be very careful about precedence.","startTime":1392000,"duration":2000,"startOfParagraph":false},{"content":"You should usually use parentheses to be sure that you know what you're doing.","startTime":1394000,"duration":7000,"startOfParagraph":false},{"content":"Some useful shortcuts, of course, one is i++ or i += 1","startTime":1401000,"duration":6000,"startOfParagraph":false},{"content":"or using +=.","startTime":1407000,"duration":3000,"startOfParagraph":false},{"content":"That is the same thing as doing i = i + 1.","startTime":1410000,"duration":4000,"startOfParagraph":false},{"content":"You can also do i-- or i -= 1,","startTime":1414000,"duration":5000,"startOfParagraph":false},{"content":"which is the same thing as i = i -1,","startTime":1419000,"duration":3000,"startOfParagraph":false},{"content":"something you guys use a lot in for loops, at least.","startTime":1422000,"duration":4000,"startOfParagraph":false},{"content":"Also, for *, if you use *= and if you do, for example, ","startTime":1426000,"duration":6000,"startOfParagraph":false},{"content":"i *= 2 is the same thing as saying i = i * 2,","startTime":1432000,"duration":5000,"startOfParagraph":false},{"content":"and the same thing for division.","startTime":1437000,"duration":2000,"startOfParagraph":false},{"content":"If you do i /= 2 it's the same thing as i = i/2.","startTime":1439000,"duration":9000,"startOfParagraph":false},{"content":"Now about functions.","startTime":1448000,"duration":2000,"startOfParagraph":true},{"content":"You guys learned that functions are a very good strategy to save code","startTime":1450000,"duration":3000,"startOfParagraph":false},{"content":"while you're programming, so if you want to perform the same task","startTime":1453000,"duration":3000,"startOfParagraph":false},{"content":"in code again and again, probably you want to use a function","startTime":1456000,"duration":4000,"startOfParagraph":false},{"content":"just so you don't have to copy and paste the code over and over again.","startTime":1460000,"duration":5000,"startOfParagraph":false},{"content":"Actually, main is a function, and when I show you the format of a function","startTime":1465000,"duration":3000,"startOfParagraph":false},{"content":"you're going to see that that is pretty obvious.","startTime":1468000,"duration":4000,"startOfParagraph":false},{"content":"We also use functions from some libraries,","startTime":1472000,"duration":3000,"startOfParagraph":false},{"content":"for example, printf, GetIn, which is from the CS50 library,","startTime":1475000,"duration":4000,"startOfParagraph":false},{"content":"and other functions like toupper.","startTime":1479000,"duration":4000,"startOfParagraph":false},{"content":"All of those functions are actually implemented in other libraries,","startTime":1483000,"duration":3000,"startOfParagraph":false},{"content":"and when you put those tether files in the beginning of your program","startTime":1486000,"duration":3000,"startOfParagraph":false},{"content":"you're saying can you please give me the code for those functions","startTime":1489000,"duration":4000,"startOfParagraph":false},{"content":"so I don't have to implement them by myself?","startTime":1493000,"duration":4000,"startOfParagraph":false},{"content":"And you can also write your own functions, so when you start programming","startTime":1497000,"duration":3000,"startOfParagraph":false},{"content":"you realize that libraries don't have all the functions that you need.","startTime":1500000,"duration":4000,"startOfParagraph":false},{"content":"For the last pset, for example, we wrote draw, scramble, and lookup,","startTime":1504000,"duration":6000,"startOfParagraph":false},{"content":"and it's very, very important to be able to write functions ","startTime":1510000,"duration":3000,"startOfParagraph":false},{"content":"because they are useful, and we use them all the time in programming,","startTime":1513000,"duration":4000,"startOfParagraph":false},{"content":"and it saves a lot of code.","startTime":1517000,"duration":2000,"startOfParagraph":false},{"content":"The format of a function is this one.","startTime":1519000,"duration":2000,"startOfParagraph":false},{"content":"We have return type in the beginning. What is the return type?","startTime":1521000,"duration":3000,"startOfParagraph":false},{"content":"It's just when your function is going to return.","startTime":1524000,"duration":3000,"startOfParagraph":false},{"content":"If you have a function, for example, factorial, ","startTime":1527000,"duration":2000,"startOfParagraph":false},{"content":"that is going to calculate a factorial of an integer,","startTime":1529000,"duration":2000,"startOfParagraph":false},{"content":"probably it's going to return an integer also.","startTime":1531000,"duration":3000,"startOfParagraph":false},{"content":"Then the return type is going to be int.","startTime":1534000,"duration":3000,"startOfParagraph":false},{"content":"Printf actually has a return type void","startTime":1537000,"duration":4000,"startOfParagraph":false},{"content":"because you're not returning anything.","startTime":1541000,"duration":2000,"startOfParagraph":false},{"content":"You're just printing things to the screen","startTime":1543000,"duration":2000,"startOfParagraph":false},{"content":"and quitting the function afterwards.","startTime":1545000,"duration":3000,"startOfParagraph":false},{"content":"Then you have the name of the function that you can choose.","startTime":1548000,"duration":3000,"startOfParagraph":false},{"content":"You should be a little reasonable, like don't choose a name like xyz","startTime":1551000,"duration":4000,"startOfParagraph":false},{"content":"or like x2f.","startTime":1555000,"duration":3000,"startOfParagraph":false},{"content":"Try to make up a name that makes sense.","startTime":1558000,"duration":4000,"startOfParagraph":false},{"content":"For example, if it's factorial, say factorial.","startTime":1562000,"duration":2000,"startOfParagraph":true},{"content":"If it's a function that is going to draw something, name it draw.","startTime":1564000,"duration":4000,"startOfParagraph":false},{"content":"And then we have the parameters, which are also called arguments,","startTime":1568000,"duration":3000,"startOfParagraph":false},{"content":"which are like the resources that your function needs","startTime":1571000,"duration":3000,"startOfParagraph":false},{"content":"from your code to perform its task.","startTime":1574000,"duration":3000,"startOfParagraph":false},{"content":"If you want to calculate the factorial of a number","startTime":1577000,"duration":3000,"startOfParagraph":false},{"content":"probably you need to have a number to calculate a factorial.","startTime":1580000,"duration":3000,"startOfParagraph":false},{"content":"One of the arguments that you're going to have is the number itself.","startTime":1583000,"duration":4000,"startOfParagraph":false},{"content":"And then it's going to do something and return the value at the end","startTime":1587000,"duration":4000,"startOfParagraph":false},{"content":"unless it's a void function.","startTime":1591000,"duration":4000,"startOfParagraph":false},{"content":"Let's see an example. ","startTime":1595000,"duration":2000,"startOfParagraph":false},{"content":"If I want to write a function that sums all the numbers in an array of integers,","startTime":1597000,"duration":3000,"startOfParagraph":false},{"content":"first of all, the return type is going to be int","startTime":1600000,"duration":3000,"startOfParagraph":false},{"content":"because I have an array of integers.","startTime":1603000,"duration":3000,"startOfParagraph":false},{"content":"And then I'm going to have the function name like sumArray,","startTime":1606000,"duration":5000,"startOfParagraph":false},{"content":"and then it's going to take the array itself, to int nums,","startTime":1611000,"duration":3000,"startOfParagraph":false},{"content":"and then the length of the array so I know how many numbers I have to sum.","startTime":1614000,"duration":4000,"startOfParagraph":false},{"content":"Then I have to initialize a variable called sum, for example, to 0,","startTime":1618000,"duration":4000,"startOfParagraph":false},{"content":"and every time I see an element in the array I should add it to sum, so I did a for loop.","startTime":1622000,"duration":6000,"startOfParagraph":false},{"content":"Just like Lexi said, you do int i = 0, i < length and i++.","startTime":1628000,"duration":7000,"startOfParagraph":false},{"content":"And for every element in the array I did sum += nums[i], ","startTime":1635000,"duration":5000,"startOfParagraph":false},{"content":"and then I returned the sum, so it's very simple, and it saves a lot of code","startTime":1640000,"duration":4000,"startOfParagraph":false},{"content":"if you're using this function a lot of times.","startTime":1644000,"duration":4000,"startOfParagraph":false},{"content":"Then we took a look at conditions.","startTime":1648000,"duration":4000,"startOfParagraph":false},{"content":"We have if, else, and else if.","startTime":1652000,"duration":6000,"startOfParagraph":false},{"content":"Let's see what is the difference between those.","startTime":1658000,"duration":4000,"startOfParagraph":false},{"content":"Take a look at these 2 codes. What is the difference between them?","startTime":1662000,"duration":3000,"startOfParagraph":false},{"content":"The first one has—basically the codes want you to tell","startTime":1665000,"duration":4000,"startOfParagraph":false},{"content":"if a number is +, -, or 0.","startTime":1669000,"duration":2000,"startOfParagraph":false},{"content":"The first one says if it's > 0 then it's positive.","startTime":1671000,"duration":4000,"startOfParagraph":false},{"content":"If it's = to 0 then it's 0, and if it's < 0 then it's negative.","startTime":1675000,"duration":5000,"startOfParagraph":false},{"content":"And the other one is doing if, else if, else.","startTime":1680000,"duration":4000,"startOfParagraph":true},{"content":"The difference between the two is that this one is actually going to","startTime":1684000,"duration":3000,"startOfParagraph":false},{"content":"check if > 0, < 0 or = 0 three times,","startTime":1687000,"duration":6000,"startOfParagraph":false},{"content":"so if you have the number 2, for example, it's going to come here and say","startTime":1693000,"duration":4000,"startOfParagraph":false},{"content":"if (x > 0), and it's going to say yes, so I print positive.","startTime":1697000,"duration":4000,"startOfParagraph":false},{"content":"But even though I know that it's > 0 and it's not going to be 0 or < 0","startTime":1701000,"duration":4000,"startOfParagraph":false},{"content":"I'm still going to do is it 0, is it < 0, ","startTime":1705000,"duration":4000,"startOfParagraph":false},{"content":"so I'm actually going inside of ifs that I didn't have to","startTime":1709000,"duration":4000,"startOfParagraph":false},{"content":"because I already know that it's not going to satisfy any of these conditions.","startTime":1713000,"duration":5000,"startOfParagraph":false},{"content":"I can use the if, else if, else statement.","startTime":1718000,"duration":3000,"startOfParagraph":false},{"content":"It basically says if x = 0 I print the positive.","startTime":1721000,"duration":4000,"startOfParagraph":false},{"content":"If it's not, I'm going to also test this.","startTime":1725000,"duration":3000,"startOfParagraph":false},{"content":"If it's 2 not I'm going to do this.","startTime":1728000,"duration":3000,"startOfParagraph":false},{"content":"Basically if I had x = 2 you would say ","startTime":1731000,"duration":3000,"startOfParagraph":false},{"content":"if (x > 0), yes, so print this.","startTime":1734000,"duration":3000,"startOfParagraph":false},{"content":"Now that I know that it's > 0 and that it satisfied the first if","startTime":1737000,"duration":3000,"startOfParagraph":false},{"content":"I'm not even going to run this code.","startTime":1740000,"duration":2000,"startOfParagraph":false},{"content":"The code runs faster, actually, 3 times faster if you use this.","startTime":1742000,"duration":7000,"startOfParagraph":false},{"content":"We also learned about and and or.","startTime":1749000,"duration":2000,"startOfParagraph":false},{"content":"I'm not going to go through this because Lexi already talked about them.","startTime":1751000,"duration":4000,"startOfParagraph":false},{"content":"It's just the && and | | operator.","startTime":1755000,"duration":2000,"startOfParagraph":false},{"content":"The only thing I'll say is be careful when you have 3 conditions.","startTime":1757000,"duration":4000,"startOfParagraph":true},{"content":"Use parentheses because it's very confusing when you have a condition","startTime":1761000,"duration":3000,"startOfParagraph":false},{"content":"and another one or another one.","startTime":1764000,"duration":3000,"startOfParagraph":false},{"content":"Use parentheses just to be sure that your conditions make sense","startTime":1767000,"duration":3000,"startOfParagraph":false},{"content":"because in that case, for example, you can imagine that","startTime":1770000,"duration":4000,"startOfParagraph":false},{"content":"it could be the first condition and one or the other","startTime":1774000,"duration":4000,"startOfParagraph":false},{"content":"or the 2 conditions combined in an and","startTime":1778000,"duration":3000,"startOfParagraph":false},{"content":"or the third one, so just be careful.","startTime":1781000,"duration":4000,"startOfParagraph":false},{"content":"And finally, we talked about switches.","startTime":1785000,"duration":3000,"startOfParagraph":false},{"content":"A switch is very useful when you have a variable.","startTime":1788000,"duration":5000,"startOfParagraph":false},{"content":"Let's say that you have a variable like n","startTime":1793000,"duration":2000,"startOfParagraph":false},{"content":"that can be 0, 1, or 2, and for each of those cases","startTime":1795000,"duration":4000,"startOfParagraph":false},{"content":"you're going to perform a task.","startTime":1799000,"duration":2000,"startOfParagraph":false},{"content":"You can say switch the variable, and it indicates that","startTime":1801000,"duration":3000,"startOfParagraph":false},{"content":"the value then is like value1 I'm going to do this,","startTime":1804000,"duration":4000,"startOfParagraph":false},{"content":"and then I break, which means I'm not going to look at any of the other cases","startTime":1808000,"duration":4000,"startOfParagraph":false},{"content":"because we already satisfied that case","startTime":1812000,"duration":3000,"startOfParagraph":false},{"content":"and then value2 and so on, and I also can have a default switch.","startTime":1815000,"duration":5000,"startOfParagraph":false},{"content":"That means if it doesn't satisfy any of the cases that I had","startTime":1820000,"duration":4000,"startOfParagraph":false},{"content":"that I'm going to do something else, but that's optional.","startTime":1824000,"duration":5000,"startOfParagraph":false},{"content":"That's all for me. Now let's have Tommy.","startTime":1829000,"duration":7000,"startOfParagraph":false},{"content":"All right, this is going to be Week 3-ish.","startTime":1836000,"duration":5000,"startOfParagraph":false},{"content":"These are some of the topics we'll be covering, crypto, scope, arrays, et cetera.","startTime":1841000,"duration":4000,"startOfParagraph":false},{"content":"Just a quick word on crypto. We're not going to hammer this home.","startTime":1845000,"duration":4000,"startOfParagraph":false},{"content":"We did this in pset 2, but for the quiz make sure you know the difference","startTime":1849000,"duration":3000,"startOfParagraph":true},{"content":"between the Caesar cipher and the Vigenère cipher,","startTime":1852000,"duration":2000,"startOfParagraph":false},{"content":"how both of those ciphers work and what it's like to encrypt","startTime":1854000,"duration":3000,"startOfParagraph":false},{"content":"and decrypt text using those 2 ciphers.","startTime":1857000,"duration":2000,"startOfParagraph":false},{"content":"Remember, the Caesar cipher simply rotates each character by the same amount,","startTime":1859000,"duration":4000,"startOfParagraph":false},{"content":"making sure you mod by the number of letters in the alphabet.","startTime":1863000,"duration":3000,"startOfParagraph":false},{"content":"And the Vigenère cipher, on the other hand, rotates each character","startTime":1866000,"duration":3000,"startOfParagraph":false},{"content":"by a different amount, so rather than saying","startTime":1869000,"duration":3000,"startOfParagraph":false},{"content":"every character rotated by 3 Vigenère will rotate each character","startTime":1872000,"duration":3000,"startOfParagraph":false},{"content":"by a different amount depending on some keyword ","startTime":1875000,"duration":2000,"startOfParagraph":false},{"content":"where each letter in the keyword represents some different amount","startTime":1877000,"duration":3000,"startOfParagraph":false},{"content":"to rotate the clear text by.","startTime":1880000,"duration":6000,"startOfParagraph":false},{"content":"Let's first talk about variable scope.","startTime":1886000,"duration":2000,"startOfParagraph":false},{"content":"There are 2 different types of variables.","startTime":1888000,"duration":2000,"startOfParagraph":false},{"content":"We have local variables, and these are going to be defined","startTime":1890000,"duration":3000,"startOfParagraph":false},{"content":"outside of main or outside any function or block,","startTime":1893000,"duration":3000,"startOfParagraph":false},{"content":"and these will be accessible anywhere in your program.","startTime":1896000,"duration":3000,"startOfParagraph":false},{"content":"If you have a function and in that function is a while loop","startTime":1899000,"duration":2000,"startOfParagraph":false},{"content":"the big global variable is accessible everywhere.","startTime":1901000,"duration":3000,"startOfParagraph":false},{"content":"A local variable, on the other hand, is scoped to the place where it is defined. ","startTime":1904000,"duration":4000,"startOfParagraph":false},{"content":"If you have a function here, for example, we have this function g, ","startTime":1908000,"duration":5000,"startOfParagraph":true},{"content":"and inside of g there is a variable here called y,","startTime":1913000,"duration":3000,"startOfParagraph":false},{"content":"and that means that this is a local variable.","startTime":1916000,"duration":2000,"startOfParagraph":false},{"content":"Even though this variable is called y","startTime":1918000,"duration":2000,"startOfParagraph":false},{"content":"and this variable is called y these 2 functions","startTime":1920000,"duration":3000,"startOfParagraph":false},{"content":"have no idea what each other's local variables are.","startTime":1923000,"duration":3000,"startOfParagraph":false},{"content":"On the other hand, up here we say int x = 5,","startTime":1926000,"duration":4000,"startOfParagraph":false},{"content":"and this is outside the scope of any function.","startTime":1930000,"duration":2000,"startOfParagraph":false},{"content":"It's outside the scope of main, so this is a global variable.","startTime":1932000,"duration":4000,"startOfParagraph":false},{"content":"That means that inside of these 2 functions when I say x-- or x++","startTime":1936000,"duration":4000,"startOfParagraph":false},{"content":"I'm accessing the same x whereby this y and this y are different variables.","startTime":1940000,"duration":6000,"startOfParagraph":false},{"content":"That's the difference between a global variable and a local variable.","startTime":1946000,"duration":4000,"startOfParagraph":false},{"content":"As far as design is concerned, sometimes it's probably a better idea","startTime":1950000,"duration":3000,"startOfParagraph":false},{"content":"to keep variables local whenever you possibly can","startTime":1953000,"duration":4000,"startOfParagraph":false},{"content":"since having a bunch of global variables can get really confusing.","startTime":1957000,"duration":2000,"startOfParagraph":false},{"content":"If you have a bunch of functions all modifying the same thing","startTime":1959000,"duration":3000,"startOfParagraph":false},{"content":"you might forget what if this function accidentally modifies this global,","startTime":1962000,"duration":3000,"startOfParagraph":false},{"content":"and this other function doesn't know about it, ","startTime":1965000,"duration":2000,"startOfParagraph":false},{"content":"and it does get pretty confusing as you get more code.","startTime":1967000,"duration":3000,"startOfParagraph":false},{"content":"Keeping variables local whenever you possibly can","startTime":1970000,"duration":3000,"startOfParagraph":false},{"content":"is just good design.","startTime":1973000,"duration":3000,"startOfParagraph":false},{"content":"Arrays, remember, are simply lists of elements of the same type.","startTime":1976000,"duration":4000,"startOfParagraph":false},{"content":"Inside of C I can't have a list like 1, 2.0, hello.","startTime":1980000,"duration":4000,"startOfParagraph":false},{"content":"We just can't do that.","startTime":1984000,"duration":2000,"startOfParagraph":false},{"content":"When we declare an array in C all of the elements have to be of the same type.","startTime":1986000,"duration":5000,"startOfParagraph":true},{"content":"Here I have an array of 3 integers.","startTime":1991000,"duration":3000,"startOfParagraph":false},{"content":"Here I have the length of the array, but if I'm just declaring it in this syntax","startTime":1994000,"duration":4000,"startOfParagraph":false},{"content":"where I specify what all of the elements are I don't technically need this 3.","startTime":1998000,"duration":3000,"startOfParagraph":false},{"content":"The compiler is smart enough to figure out how big the array should be.","startTime":2001000,"duration":4000,"startOfParagraph":false},{"content":"Now when I want to get or set the value of an array","startTime":2005000,"duration":3000,"startOfParagraph":false},{"content":"this is the syntax to do that.","startTime":2008000,"duration":2000,"startOfParagraph":false},{"content":"This will actually modify the second element of the array because, remember,","startTime":2010000,"duration":3000,"startOfParagraph":false},{"content":"numbering starts at 0, not at 1.","startTime":2013000,"duration":3000,"startOfParagraph":false},{"content":"If I want to read that value I can say something like int x = array[1].","startTime":2016000,"duration":6000,"startOfParagraph":false},{"content":"Or if I want to set that value, like I'm doing here,","startTime":2022000,"duration":2000,"startOfParagraph":false},{"content":"I can say array[1] = 4.","startTime":2024000,"duration":3000,"startOfParagraph":false},{"content":"That time accessing elements by their index","startTime":2027000,"duration":3000,"startOfParagraph":false},{"content":"or their position or where they are in the array,","startTime":2030000,"duration":2000,"startOfParagraph":false},{"content":"and that listing starts at 0.","startTime":2032000,"duration":5000,"startOfParagraph":false},{"content":"We can also have arrays of arrays,","startTime":2037000,"duration":3000,"startOfParagraph":false},{"content":"and this is called a multi-dimensional array.","startTime":2040000,"duration":3000,"startOfParagraph":false},{"content":"When we have a multi-dimensional array","startTime":2043000,"duration":2000,"startOfParagraph":false},{"content":"that means we can have something like rows and columns,","startTime":2045000,"duration":2000,"startOfParagraph":false},{"content":"and this is just one way of visualizing this or thinking about it.","startTime":2047000,"duration":4000,"startOfParagraph":false},{"content":"When I have a multi-dimensional array that means I'm going to start needing","startTime":2051000,"duration":3000,"startOfParagraph":false},{"content":"more than 1 index because if I have a grid","startTime":2054000,"duration":3000,"startOfParagraph":false},{"content":"just saying what row you're in doesn't give us a number.","startTime":2057000,"duration":2000,"startOfParagraph":false},{"content":"That's really just going to give us a list of numbers.","startTime":2059000,"duration":3000,"startOfParagraph":false},{"content":"Let's say I have this array here.","startTime":2062000,"duration":3000,"startOfParagraph":false},{"content":"I have an array called grid, and I'm saying it's 2 rows and 3 columns,","startTime":2065000,"duration":5000,"startOfParagraph":false},{"content":"and so this is one way of visualizing it.","startTime":2070000,"duration":2000,"startOfParagraph":false},{"content":"When I say I want to get the element at [1] [2]","startTime":2072000,"duration":5000,"startOfParagraph":false},{"content":"that means that because these are rows first and then columns","startTime":2077000,"duration":4000,"startOfParagraph":false},{"content":"I'm going to jump to row 1 since I said 1.","startTime":2081000,"duration":3000,"startOfParagraph":false},{"content":"Then I'm going to come over here to column 2, and I'm going to get the value 6.","startTime":2084000,"duration":5000,"startOfParagraph":true},{"content":"Make sense?","startTime":2089000,"duration":2000,"startOfParagraph":false},{"content":"Multi-dimensional arrays, remember, are technically just an array of arrays.","startTime":2091000,"duration":4000,"startOfParagraph":false},{"content":"We can have arrays of arrays of arrays.","startTime":2095000,"duration":2000,"startOfParagraph":false},{"content":"We can keep going, but really one way to think about","startTime":2097000,"duration":3000,"startOfParagraph":false},{"content":"how this is being laid out and what's going on is to visualize it ","startTime":2100000,"duration":3000,"startOfParagraph":false},{"content":"in a grid like this.","startTime":2103000,"duration":6000,"startOfParagraph":false},{"content":"When we pass arrays to functions, they're going to behave ","startTime":2109000,"duration":3000,"startOfParagraph":false},{"content":"a little bit differently than when we pass regular variables to functions","startTime":2112000,"duration":4000,"startOfParagraph":false},{"content":"like passing an int or a float.","startTime":2116000,"duration":2000,"startOfParagraph":false},{"content":"When we pass in an int or char or any of these other data types","startTime":2118000,"duration":3000,"startOfParagraph":false},{"content":"we just took a look at if the function modifies","startTime":2121000,"duration":3000,"startOfParagraph":false},{"content":"the value of that variable that change is not going to propagate up","startTime":2124000,"duration":4000,"startOfParagraph":false},{"content":"to the calling function.","startTime":2128000,"duration":4000,"startOfParagraph":false},{"content":"With an array, on the other hand, that will happen.","startTime":2132000,"duration":3000,"startOfParagraph":false},{"content":"If I pass in an array to some function and that function changes some of the elements,","startTime":2135000,"duration":4000,"startOfParagraph":false},{"content":"when I come back up to the function that called it","startTime":2139000,"duration":4000,"startOfParagraph":false},{"content":"my array is now going to be different, and the vocabulary for that","startTime":2143000,"duration":4000,"startOfParagraph":false},{"content":"is arrays are passed by reference, as we'll see later.","startTime":2147000,"duration":3000,"startOfParagraph":false},{"content":"This is related to how pointers work, where these basic data types, ","startTime":2150000,"duration":3000,"startOfParagraph":false},{"content":"on the other hand, are passed by value.","startTime":2153000,"duration":2000,"startOfParagraph":false},{"content":"We can think of that as making a copy of some variable and then passing in the copy. ","startTime":2155000,"duration":4000,"startOfParagraph":true},{"content":"It doesn't matter what we do with that variable.","startTime":2159000,"duration":2000,"startOfParagraph":false},{"content":"The calling function will not be aware that it was changed.","startTime":2161000,"duration":5000,"startOfParagraph":false},{"content":"Arrays are just a little bit different in that regard.","startTime":2166000,"duration":4000,"startOfParagraph":false},{"content":"For example, as we just saw, main is simply a function","startTime":2170000,"duration":3000,"startOfParagraph":false},{"content":"that can take in 2 arguments.","startTime":2173000,"duration":2000,"startOfParagraph":false},{"content":"The first argument to the main function is argc, or the number of arguments,","startTime":2175000,"duration":5000,"startOfParagraph":false},{"content":"and the second argument is called argv, ","startTime":2180000,"duration":3000,"startOfParagraph":false},{"content":"and those are the actual values of those arguments. ","startTime":2183000,"duration":4000,"startOfParagraph":false},{"content":"Let's say I have a program called this.c, ","startTime":2187000,"duration":3000,"startOfParagraph":false},{"content":"and I say make this, and I'm going to run this at the command line.","startTime":2190000,"duration":4000,"startOfParagraph":false},{"content":"Now to pass in some arguments to my program called this,","startTime":2194000,"duration":4000,"startOfParagraph":false},{"content":"I could say something like ./this is cs 50.","startTime":2198000,"duration":4000,"startOfParagraph":false},{"content":"This is what we imagine David to do every day at the terminal.","startTime":2202000,"duration":3000,"startOfParagraph":false},{"content":"But now the main function inside of that program","startTime":2205000,"duration":3000,"startOfParagraph":false},{"content":"has these values, so argc is 4.","startTime":2208000,"duration":4000,"startOfParagraph":false},{"content":"It might be a little confusing because really we're only passing in is cs 50.","startTime":2212000,"duration":4000,"startOfParagraph":false},{"content":"That's only 3.","startTime":2216000,"duration":2000,"startOfParagraph":false},{"content":"But remember that the first element of argv or the first argument","startTime":2218000,"duration":4000,"startOfParagraph":false},{"content":"is the name of the function itself.","startTime":2222000,"duration":3000,"startOfParagraph":false},{"content":"So that means that we have 4 things here, ","startTime":2225000,"duration":2190,"startOfParagraph":false},{"content":"and the first element is going to be ./this. ","startTime":2227190,"duration":3340,"startOfParagraph":false},{"content":"And this will be represented as a string. ","startTime":2230530,"duration":2440,"startOfParagraph":false},{"content":"Then the remaining elements are what we typed in after the name of the program. ","startTime":2232970,"duration":5620,"startOfParagraph":false},{"content":"So just as an aside, as we probably saw in pset 2, ","startTime":2238590,"duration":4130,"startOfParagraph":false},{"content":"remember that the string 50 is ≠ the integer 50. ","startTime":2242720,"duration":6060,"startOfParagraph":false},{"content":"So we can't say something like, 'int x = argv 3.' ","startTime":2248780,"duration":3740,"startOfParagraph":false},{"content":"That's just not going to make sense, because this is a string, and this is an integer. ","startTime":2252520,"duration":3950,"startOfParagraph":true},{"content":"So if you want to convert between the 2, remember, we're going to ","startTime":2256470,"duration":2040,"startOfParagraph":false},{"content":"have this magic function called atoi. ","startTime":2258510,"duration":2300,"startOfParagraph":false},{"content":"That takes a string and returns the integer represented inside of that string. ","startTime":2260810,"duration":5460,"startOfParagraph":false},{"content":"So that's an easy mistake to make on the quiz,","startTime":2266270,"duration":2090,"startOfParagraph":false},{"content":"just thinking that this will automatically be the correct type. ","startTime":2268360,"duration":3230,"startOfParagraph":false},{"content":"But just know that these will always be strings ","startTime":2271590,"duration":2270,"startOfParagraph":false},{"content":"even if the string only contains an integer or a character or a float. ","startTime":2273860,"duration":7060,"startOfParagraph":false},{"content":"So now let's talk about running time. ","startTime":2280920,"duration":2460,"startOfParagraph":false},{"content":"When we have all these algorithms that do all these crazy things, ","startTime":2283380,"duration":3320,"startOfParagraph":false},{"content":"it becomes really useful to ask the question, \"How long do they take?\"","startTime":2286700,"duration":4880,"startOfParagraph":false},{"content":"We represent that with something called asymptotic notation. ","startTime":2291580,"duration":3920,"startOfParagraph":false},{"content":"So this means that--well, let's say we give our algorithm ","startTime":2295500,"duration":2930,"startOfParagraph":false},{"content":"some really, really, really big input. ","startTime":2298430,"duration":2410,"startOfParagraph":false},{"content":"We want to ask the question, \"How long is it going to take?","startTime":2300840,"duration":3000,"startOfParagraph":false},{"content":"How many steps will it take our algorithm to run ","startTime":2303840,"duration":2530,"startOfParagraph":false},{"content":"as a function of the size of the input?\"","startTime":2306370,"duration":3610,"startOfParagraph":false},{"content":"So the first way we can describe run time is with big O. ","startTime":2309980,"duration":3100,"startOfParagraph":false},{"content":"And this is our worst-case running time. ","startTime":2313080,"duration":2300,"startOfParagraph":false},{"content":"So if we want to sort an array, and we give our algorithm an array","startTime":2315380,"duration":3210,"startOfParagraph":false},{"content":"that's in descending order when it should be in ascending order, ","startTime":2318590,"duration":2410,"startOfParagraph":false},{"content":"that's going to be the worst case. ","startTime":2321000,"duration":2130,"startOfParagraph":false},{"content":"This is our upper bound in the maximum length of time our algorithm will take. ","startTime":2323130,"duration":6670,"startOfParagraph":false},{"content":"On the other hand, this Ω is going to describe best-case running time. ","startTime":2329800,"duration":4940,"startOfParagraph":false},{"content":"So if we give an already sorted array to a sorting algorithm, ","startTime":2334740,"duration":3470,"startOfParagraph":false},{"content":"how long will it take to sort it? ","startTime":2338210,"duration":2730,"startOfParagraph":false},{"content":"And this, then, describes a lower bound on running time. ","startTime":2340940,"duration":5670,"startOfParagraph":false},{"content":"So here are just some words that describe some common running times. ","startTime":2346610,"duration":4370,"startOfParagraph":false},{"content":"These are in ascending order. ","startTime":2350980,"duration":2140,"startOfParagraph":false},{"content":"The fastest running time we have is called constant. ","startTime":2353120,"duration":2940,"startOfParagraph":false},{"content":"That means no matter how many elements we give our algorithm, ","startTime":2356060,"duration":3740,"startOfParagraph":true},{"content":"no matter how big our array is, sorting it ","startTime":2359800,"duration":2480,"startOfParagraph":false},{"content":"or doing whatever we're doing to the array will always take the same amount of time. ","startTime":2362280,"duration":4230,"startOfParagraph":false},{"content":"So we can represent that just with a 1, which is a constant. ","startTime":2366510,"duration":3760,"startOfParagraph":false},{"content":"We also looked at logarithmic run time. ","startTime":2370270,"duration":2140,"startOfParagraph":false},{"content":"So something like binary search is logarithmic, ","startTime":2372410,"duration":2390,"startOfParagraph":false},{"content":"where we cut the problem in half every time","startTime":2374800,"duration":2340,"startOfParagraph":false},{"content":"and then things just get higher from there. ","startTime":2377140,"duration":3830,"startOfParagraph":false},{"content":"And if you're ever writing an O of any factorial algorithm, ","startTime":2380970,"duration":2610,"startOfParagraph":false},{"content":"you probably shouldn't consider this as your day job. ","startTime":2383580,"duration":4270,"startOfParagraph":false},{"content":"When we compare running times it's important to keep in mind these things. ","startTime":2387850,"duration":6060,"startOfParagraph":false},{"content":"So if I have an algorithm that's O(n), and someone else ","startTime":2393910,"duration":3850,"startOfParagraph":false},{"content":"has an algorithm of O(2n) these are actually asymptotically equivalent. ","startTime":2397760,"duration":5830,"startOfParagraph":false},{"content":"So if we imagine n to be a big number like eleventy billion:","startTime":2403590,"duration":3000,"startOfParagraph":false},{"content":"so when we're comparing eleventy billion to something like eleventy billion + 3, ","startTime":2406590,"duration":6500,"startOfParagraph":false},{"content":"suddenly that +3 doesn't really make a big difference anymore. ","startTime":2413090,"duration":4550,"startOfParagraph":false},{"content":"That's why we're going to start considering these things to be equivalent. ","startTime":2417640,"duration":3340,"startOfParagraph":false},{"content":"So things like these constants here, there's 2 x this, or adding a 3, ","startTime":2420980,"duration":3240,"startOfParagraph":false},{"content":"these are just constants, and these are going to drop up. ","startTime":2424220,"duration":2960,"startOfParagraph":false},{"content":"So that's why all 3 of these run times are the same as saying they're O(n). ","startTime":2427180,"duration":5300,"startOfParagraph":false},{"content":"Similarly, if we have 2 other run times, let's say O(n³ + 2n²), we can add ","startTime":2432480,"duration":5010,"startOfParagraph":false},{"content":"+ n, + 7, and then we have another run time that's just O(n³).","startTime":2437490,"duration":4580,"startOfParagraph":false},{"content":"again, these are the same thing because these--these are not the same. ","startTime":2442070,"duration":4220,"startOfParagraph":false},{"content":"These are the same things, sorry. So these are the same because ","startTime":2446290,"duration":3550,"startOfParagraph":false},{"content":"this n³ is going to dominate this 2n². ","startTime":2449840,"duration":3250,"startOfParagraph":false},{"content":"What is not the same thing is if we have run times like O(n³) and O(n²)","startTime":2453090,"duration":6040,"startOfParagraph":true},{"content":"because this n³ is much larger than this n². ","startTime":2459130,"duration":3690,"startOfParagraph":false},{"content":"So if we have exponents, suddenly this starts to matter, ","startTime":2462820,"duration":2650,"startOfParagraph":false},{"content":"but when we're just dealing with factors as we are up here, ","startTime":2465470,"duration":2810,"startOfParagraph":false},{"content":"then it's not going to matter because they are just going to drop out. ","startTime":2468280,"duration":4530,"startOfParagraph":false},{"content":"Let's take a look at some of the algorithms we've seen so far","startTime":2472810,"duration":3950,"startOfParagraph":false},{"content":"and talk about their run time. ","startTime":2476760,"duration":2500,"startOfParagraph":false},{"content":"The first way of looking for a number in a list, that we saw, was linear search. ","startTime":2479260,"duration":4590,"startOfParagraph":false},{"content":"And the implementation of linear search is super straightforward. ","startTime":2483850,"duration":3100,"startOfParagraph":false},{"content":"We just have a list, and we're going to look at every single element in the list ","startTime":2486950,"duration":3540,"startOfParagraph":false},{"content":"until we find the number we're looking for. ","startTime":2490490,"duration":3770,"startOfParagraph":false},{"content":"So that means that in the worst case, this O(n). ","startTime":2494260,"duration":4110,"startOfParagraph":false},{"content":"And the worst case here could be if the element is ","startTime":2498370,"duration":2490,"startOfParagraph":false},{"content":"the last element, then using linear search we have to look at every single element ","startTime":2500860,"duration":4850,"startOfParagraph":false},{"content":"until we get to the last one in order to know that it was actually in the list. ","startTime":2505710,"duration":4470,"startOfParagraph":false},{"content":"We can't just give up halfway and say, \"It's probably not there.\" ","startTime":2510180,"duration":2730,"startOfParagraph":false},{"content":"With linear search we have to look at the whole thing. ","startTime":2512910,"duration":3070,"startOfParagraph":false},{"content":"The best-case running time, on the other hand, is constant","startTime":2515980,"duration":3110,"startOfParagraph":false},{"content":"because in the best case the element we're looking for is just the first one in the list. ","startTime":2519090,"duration":5110,"startOfParagraph":false},{"content":"So it's going to take us exactly 1 step, no matter how big the list is","startTime":2524200,"duration":4730,"startOfParagraph":false},{"content":"if we're looking for the first element every time. ","startTime":2528930,"duration":3210,"startOfParagraph":false},{"content":"So when you search, remember, it does not require that our list be sorted. ","startTime":2532140,"duration":3250,"startOfParagraph":true},{"content":"Because we're simply going to look over every single element, and it doesn't really matter ","startTime":2535390,"duration":4040,"startOfParagraph":false},{"content":"what order those elements are in. ","startTime":2539430,"duration":4130,"startOfParagraph":false},{"content":"A more intelligent search algorithm is something like binary search. ","startTime":2543560,"duration":4550,"startOfParagraph":false},{"content":"Remember, the implementation of binary search is when you're going to ","startTime":2548110,"duration":3390,"startOfParagraph":false},{"content":"keep looking at the middle of the list. ","startTime":2551500,"duration":2820,"startOfParagraph":false},{"content":"And because we're looking at the middle, we require that the list is sorted","startTime":2554320,"duration":3680,"startOfParagraph":false},{"content":"or else we don't know where the middle is, and we have to look over","startTime":2558000,"duration":2580,"startOfParagraph":false},{"content":"the whole list to find it, and then at that point we're just wasting time. ","startTime":2560580,"duration":3900,"startOfParagraph":false},{"content":"So if we have a sorted list and we find the middle, we're going to compare the middle","startTime":2564480,"duration":4000,"startOfParagraph":false},{"content":"to the element we're looking for. ","startTime":2568480,"duration":3110,"startOfParagraph":false},{"content":"If it's too high, then we can forget the right half","startTime":2571590,"duration":3050,"startOfParagraph":false},{"content":"because we know that if our element is already too high","startTime":2574640,"duration":3170,"startOfParagraph":false},{"content":"and everything to the right of this element is even higher,","startTime":2577810,"duration":3270,"startOfParagraph":false},{"content":"then we don't need to look there anymore. ","startTime":2581080,"duration":1680,"startOfParagraph":false},{"content":"Where on the other hand, if our element is too low, ","startTime":2582760,"duration":2670,"startOfParagraph":false},{"content":"we know everything to the left of that element is also too low, ","startTime":2585430,"duration":3270,"startOfParagraph":false},{"content":"so it doesn't really make sense to look there, either. ","startTime":2588700,"duration":2690,"startOfParagraph":false},{"content":"This way, with every step and every time we look at the midpoint of the list, ","startTime":2591390,"duration":4370,"startOfParagraph":false},{"content":"we're going to cut our problem in half because suddenly we know ","startTime":2595760,"duration":3300,"startOfParagraph":false},{"content":"a whole bunch of numbers that can't be the one we're looking for. ","startTime":2599060,"duration":3980,"startOfParagraph":false},{"content":"In pseudocode this would look something like this,","startTime":2603040,"duration":3910,"startOfParagraph":true},{"content":"and because we're cutting the list in half every single time,","startTime":2606950,"duration":4040,"startOfParagraph":false},{"content":"our worst-case run time jumps from linear to logarithmic. ","startTime":2610990,"duration":3930,"startOfParagraph":false},{"content":"So suddenly we have log-in steps in order to find an element in a list. ","startTime":2614920,"duration":4340,"startOfParagraph":false},{"content":"The best-case running time, though, is still constant ","startTime":2619260,"duration":3200,"startOfParagraph":false},{"content":"because now, let's just say that the element we're looking for is","startTime":2622460,"duration":2720,"startOfParagraph":false},{"content":"always the exact middle of the original list. ","startTime":2625180,"duration":3200,"startOfParagraph":false},{"content":"So we can grow our list as big as we want, but if the element we're looking for is at the middle, ","startTime":2628380,"duration":3700,"startOfParagraph":false},{"content":"then it's only going to take us 1 step. ","startTime":2632080,"duration":2830,"startOfParagraph":false},{"content":"So that's why we're O(log n) and Ω(1) or constant. ","startTime":2634910,"duration":6010,"startOfParagraph":false},{"content":"Let's actually run binary search on this list. ","startTime":2640920,"duration":3590,"startOfParagraph":false},{"content":"So let's say that we're looking for the element 164. ","startTime":2644510,"duration":3510,"startOfParagraph":false},{"content":"The first thing we are going to do is find the midpoint of this list. ","startTime":2648020,"duration":3630,"startOfParagraph":false},{"content":"It just so happens that the midpoint is going to fall in between these 2 numbers, ","startTime":2651650,"duration":3410,"startOfParagraph":false},{"content":"so let's just arbitrarily say, every time the midpoint falls between 2 numbers, ","startTime":2655060,"duration":3900,"startOfParagraph":false},{"content":"let's just round up. ","startTime":2658960,"duration":2190,"startOfParagraph":false},{"content":"We just need to make sure we do this every step of the way. ","startTime":2661150,"duration":3180,"startOfParagraph":false},{"content":"So we're going to round up, and we're going to say that 161 is the middle of our list. ","startTime":2664330,"duration":4710,"startOfParagraph":false},{"content":"So 161 < 164, and every element to the left of 161 ","startTime":2669040,"duration":5600,"startOfParagraph":false},{"content":"is also < 164, so we know that it's not going to help us at all ","startTime":2674640,"duration":4480,"startOfParagraph":false},{"content":"to start looking over here because the element we're looking for can't be there. ","startTime":2679120,"duration":3570,"startOfParagraph":false},{"content":"So what we can do is we can just forget about that whole left half of the list, ","startTime":2682690,"duration":4370,"startOfParagraph":false},{"content":"and now only consider from the right of the 161 onward. ","startTime":2687060,"duration":4640,"startOfParagraph":false},{"content":"So again, this is the midpoint; let's just round up. ","startTime":2691700,"duration":2350,"startOfParagraph":true},{"content":"Now 175 is too big. ","startTime":2694050,"duration":2210,"startOfParagraph":false},{"content":"So we know it's not going to help us looking here or here, ","startTime":2696260,"duration":2920,"startOfParagraph":false},{"content":"so we can just throw that away, and eventually we'll hit the 164. ","startTime":2699180,"duration":7430,"startOfParagraph":false},{"content":"Any questions on binary search? ","startTime":2706610,"duration":3950,"startOfParagraph":false},{"content":"Let's move on from searching through an already-sorted list ","startTime":2710560,"duration":3620,"startOfParagraph":false},{"content":"to actually taking a list of numbers in any order","startTime":2714180,"duration":3480,"startOfParagraph":false},{"content":"and making that list in ascending order. ","startTime":2717660,"duration":3300,"startOfParagraph":false},{"content":"The first algorithm we looked at was called bubble sort. ","startTime":2720960,"duration":3100,"startOfParagraph":false},{"content":"And this would be simpler of the algorithms we saw. ","startTime":2724060,"duration":3240,"startOfParagraph":false},{"content":"Bubble sort says that when any 2 elements inside the list are out of place, ","startTime":2727300,"duration":5670,"startOfParagraph":false},{"content":"meaning there is a higher number to the left of a lower number, ","startTime":2732970,"duration":3530,"startOfParagraph":false},{"content":"then we're going to swap them, because that means that the list will be ","startTime":2736500,"duration":3690,"startOfParagraph":false},{"content":"\"more sorted\" than it was before. ","startTime":2740190,"duration":2670,"startOfParagraph":false},{"content":"And we're just going to continue this process again and again and again","startTime":2742860,"duration":2320,"startOfParagraph":false},{"content":"until eventually the elements kind of bubble to their correct location and we have a sorted list. ","startTime":2745180,"duration":6920,"startOfParagraph":false},{"content":"The run time of this is going to be O(n²). Why?","startTime":2752100,"duration":5130,"startOfParagraph":true},{"content":"Well, because in the worst case, we're going to take every element, and ","startTime":2757230,"duration":3140,"startOfParagraph":false},{"content":"we're going to end up comparing it to every other element in the list. ","startTime":2760370,"duration":4200,"startOfParagraph":false},{"content":"But in the best case, we have an already sorted list, bubble sort's ","startTime":2764570,"duration":3460,"startOfParagraph":false},{"content":"just going to go through once, say \"Nope. I didn't make any swaps, so I'm done.\"","startTime":2768030,"duration":4200,"startOfParagraph":false},{"content":"So we have a best-case running time of Ω(n). ","startTime":2772230,"duration":5180,"startOfParagraph":false},{"content":"Let's run bubble sort on a list. ","startTime":2777410,"duration":3270,"startOfParagraph":false},{"content":"Or first, let's just look at some pseudocode really quickly. ","startTime":2780680,"duration":2880,"startOfParagraph":false},{"content":"We want to say we want to keep track of, in every iteration of the loop, ","startTime":2783560,"duration":4600,"startOfParagraph":false},{"content":"keep track of whether or not we changed any elements. ","startTime":2788160,"duration":4030,"startOfParagraph":false},{"content":"So the reason for that is, we're going to stop when we have not swapped any elements. ","startTime":2792190,"duration":5420,"startOfParagraph":false},{"content":"So at the start of our loop we haven't swapped anything, so we'll say that's false. ","startTime":2797610,"duration":4370,"startOfParagraph":false},{"content":"Now, we're going to go through the list and compare element i to element i + 1","startTime":2801980,"duration":5190,"startOfParagraph":false},{"content":"and if it is the case that there is a bigger number to the left of a smaller number, ","startTime":2807170,"duration":3140,"startOfParagraph":false},{"content":"then we're just going to swap them. ","startTime":2810310,"duration":2000,"startOfParagraph":false},{"content":"And then we're going to remember that we swapped an element. ","startTime":2812310,"duration":2180,"startOfParagraph":true},{"content":"That means that we need to go through the list at least 1 more time","startTime":2814490,"duration":4410,"startOfParagraph":false},{"content":"because the condition in which we stopped is when the whole list is already sorted, ","startTime":2818900,"duration":3260,"startOfParagraph":false},{"content":"meaning we have not made any swaps. ","startTime":2822160,"duration":2730,"startOfParagraph":false},{"content":"So that's why our condition down here is 'while some elements have been swapped.'","startTime":2824890,"duration":5070,"startOfParagraph":false},{"content":"So now let's just look at this running on a list. ","startTime":2829960,"duration":3760,"startOfParagraph":false},{"content":"I have the list 5,0,1,6,4. ","startTime":2833720,"duration":2920,"startOfParagraph":false},{"content":"Bubble sort is going to start all the way at the left, and it's going to compare ","startTime":2836640,"duration":3210,"startOfParagraph":false},{"content":"the i elements, so 0 to i + 1, which is element 1. ","startTime":2839850,"duration":4850,"startOfParagraph":false},{"content":"It's going to say, well 5 > 0, but right now 5 is to the left, ","startTime":2844700,"duration":4320,"startOfParagraph":false},{"content":"so I need to swap the 5 and the 0. ","startTime":2849020,"duration":3480,"startOfParagraph":false},{"content":"When I swap them, suddenly I get this different list. ","startTime":2852500,"duration":2970,"startOfParagraph":false},{"content":"Now 5 > 1, so we're going to swap them. ","startTime":2855470,"duration":2790,"startOfParagraph":false},{"content":"5 is not > 6, so we don't need to do anything here. ","startTime":2858260,"duration":3900,"startOfParagraph":false},{"content":"But 6 > 4, so we need to swap. ","startTime":2862160,"duration":4530,"startOfParagraph":false},{"content":"Again, we need to run through the whole list to eventually discover ","startTime":2866690,"duration":3050,"startOfParagraph":false},{"content":"that these are out of order; we swap them, ","startTime":2869740,"duration":2590,"startOfParagraph":false},{"content":"and at this point we need to run through the list 1 more time ","startTime":2872330,"duration":4790,"startOfParagraph":false},{"content":"to make sure that everything's in its order, and at this point bubble sort has finished. ","startTime":2877120,"duration":8270,"startOfParagraph":false},{"content":"A different algorithm for taking some elements and sorting them is selection sort. ","startTime":2885390,"duration":5330,"startOfParagraph":false},{"content":"The idea behind selection sort is that we're going to build up a sorted portion of the list","startTime":2890720,"duration":5020,"startOfParagraph":false},{"content":"1 element at a time. ","startTime":2895740,"duration":2410,"startOfParagraph":false},{"content":"And the way we're going to do that is by building up the left segment of the list. ","startTime":2898150,"duration":5020,"startOfParagraph":true},{"content":"And basically, every--on each step, we're going to take the smallest element we have left","startTime":2903170,"duration":4340,"startOfParagraph":false},{"content":"that hasn't been sorted yet, and we're going to move it into that sorted segment. ","startTime":2907510,"duration":4800,"startOfParagraph":false},{"content":"That means we need to continuously find the minimum unsorted element","startTime":2912310,"duration":3540,"startOfParagraph":false},{"content":"and then take that minimum element and swap it with whatever ","startTime":2915850,"duration":4870,"startOfParagraph":false},{"content":"left-most element that is not sorted. ","startTime":2920720,"duration":4370,"startOfParagraph":false},{"content":"The run time of this is going to be O(n²) because in the worst case","startTime":2925090,"duration":5800,"startOfParagraph":false},{"content":"we need to compare every single element to every other element. ","startTime":2930890,"duration":4180,"startOfParagraph":false},{"content":"Because we're saying that if we start at the left half of the list, we need ","startTime":2935070,"duration":4180,"startOfParagraph":false},{"content":"to go through the entire right segment to find the smallest element. ","startTime":2939250,"duration":3720,"startOfParagraph":false},{"content":"And then, again, we need to go over the entire right segment and","startTime":2942970,"duration":2460,"startOfParagraph":false},{"content":"keep going over that over and over and over again. ","startTime":2945430,"duration":2780,"startOfParagraph":false},{"content":"That's going to be n². We're going to need a for loop inside of another for loop ","startTime":2948210,"duration":3140,"startOfParagraph":false},{"content":"which suggests n². ","startTime":2951350,"duration":2000,"startOfParagraph":false},{"content":"In the best case thought, let's say we give it an already sorted list;","startTime":2953350,"duration":3180,"startOfParagraph":false},{"content":"we actually don't do any better than n². ","startTime":2956530,"duration":2740,"startOfParagraph":false},{"content":"Because selection sort has no way of knowing that ","startTime":2959270,"duration":2460,"startOfParagraph":false},{"content":"the minimum element is just the one I happen to be looking at. ","startTime":2961730,"duration":3810,"startOfParagraph":false},{"content":"It still needs to make sure that this is actually the minimum. ","startTime":2965540,"duration":3430,"startOfParagraph":false},{"content":"And the only way to make sure that it's the minimum, using this algorithm, ","startTime":2968970,"duration":2700,"startOfParagraph":true},{"content":"is to look at every single element again. ","startTime":2971670,"duration":2970,"startOfParagraph":false},{"content":"So really, if you give it--if you give selection sort an already sorted list, ","startTime":2974640,"duration":3780,"startOfParagraph":false},{"content":"it's not going to do any better than giving it a list that isn't sorted yet. ","startTime":2978420,"duration":4300,"startOfParagraph":false},{"content":"By the way, if it happens to be the case that something is O(something)","startTime":2982720,"duration":3600,"startOfParagraph":false},{"content":"and the omega of something, we can just say more succinctly that it's θ of something. ","startTime":2986320,"duration":4320,"startOfParagraph":false},{"content":"So if you see that come up anywhere, that's what that just means. ","startTime":2990640,"duration":2120,"startOfParagraph":false},{"content":"If something is theta of n², it is both big O(n²) and Ω(n²). ","startTime":2992760,"duration":4820,"startOfParagraph":true},{"content":"So best case and worst case, it doesn't make a difference, ","startTime":2997580,"duration":2210,"startOfParagraph":false},{"content":"the algorithm is going to do the same thing every time. ","startTime":2999790,"duration":4610,"startOfParagraph":false},{"content":"So this is what pseudocode for selection sort could look like. ","startTime":3004400,"duration":2210,"startOfParagraph":false},{"content":"We're basically going to say that I want to iterate over the list","startTime":3006610,"duration":4020,"startOfParagraph":false},{"content":"from left to right, and at each iteration of the loop, I'm going to move","startTime":3010630,"duration":4550,"startOfParagraph":false},{"content":"the minimum element into this sorted portion of the list. ","startTime":3015180,"duration":4600,"startOfParagraph":false},{"content":"And once I move something there, I never need to look at that element again. ","startTime":3019780,"duration":3480,"startOfParagraph":false},{"content":"Because as soon as I swap an element in to the left segment of the list, it's sorted","startTime":3023260,"duration":5340,"startOfParagraph":false},{"content":"because we're doing everything in ascending order by using minimums. ","startTime":3028600,"duration":4000,"startOfParagraph":false},{"content":"So we said, okay, we're at position i, and we need to look at all of the elements ","startTime":3032600,"duration":6140,"startOfParagraph":false},{"content":"to the right of i in order to find the minimum. ","startTime":3038740,"duration":3520,"startOfParagraph":false},{"content":"So that means we want to look from i + 1 to the end of the list. ","startTime":3042260,"duration":3890,"startOfParagraph":false},{"content":"And now, if the element that we're currently looking at is less than our minimum so far,","startTime":3046150,"duration":5460,"startOfParagraph":false},{"content":"which, remember, we're starting the minimum off to just be","startTime":3051610,"duration":2580,"startOfParagraph":false},{"content":"whatever element we're currently at; I'll assume that's the minimum. ","startTime":3054190,"duration":2830,"startOfParagraph":false},{"content":"If I find an element that's smaller than that, then I'm going to say, okay, ","startTime":3057020,"duration":3250,"startOfParagraph":false},{"content":"well, I have found a new minimum. ","startTime":3060270,"duration":2430,"startOfParagraph":false},{"content":"I'm going to remember where that minimum was. ","startTime":3062700,"duration":3380,"startOfParagraph":false},{"content":"So now, once I've gone through that right unsorted segment, ","startTime":3066080,"duration":3480,"startOfParagraph":true},{"content":"I can say I'm going to swap the minimum element with the element that is in position i. ","startTime":3069560,"duration":7130,"startOfParagraph":false},{"content":"That's going to build up my list, my sorted portion of the list from left to right, ","startTime":3076690,"duration":4410,"startOfParagraph":false},{"content":"and we don't ever need to look at an element again once it's in that portion. ","startTime":3081100,"duration":4090,"startOfParagraph":false},{"content":"Once we've swapped it. ","startTime":3085190,"duration":2740,"startOfParagraph":false},{"content":"So let's run selection sort on this list. ","startTime":3087930,"duration":2330,"startOfParagraph":false},{"content":"The blue element here is going to be the i, and the red element is going to be the minimum element. ","startTime":3090260,"duration":7960,"startOfParagraph":false},{"content":"So i starts all the way at the left of the list, so at 5. ","startTime":3098220,"duration":3350,"startOfParagraph":false},{"content":"Now we need to find the minimum unsorted element. ","startTime":3101570,"duration":3040,"startOfParagraph":false},{"content":"So we say 0 < 5, so 0 is my new minimum. ","startTime":3104610,"duration":4870,"startOfParagraph":false},{"content":"But I can't stop there, because even though we can recognize that 0 is the smallest, ","startTime":3109480,"duration":4340,"startOfParagraph":true},{"content":"we need to run through every other element of the list to make sure. ","startTime":3113820,"duration":5570,"startOfParagraph":false},{"content":"So 1 is bigger, 6 is bigger, 4 is bigger. ","startTime":3119390,"duration":2370,"startOfParagraph":false},{"content":"That means that after looking at all of these elements, I've determined 0 is the smallest. ","startTime":3121760,"duration":4090,"startOfParagraph":false},{"content":"So I'm going to swap the 5 and the 0. ","startTime":3125850,"duration":3950,"startOfParagraph":false},{"content":"Once I swap that, I'm going to get a new list, and I know that I never need to look at that 0 again","startTime":3129800,"duration":5680,"startOfParagraph":false},{"content":"because once I've swapped it, I've sorted it and we're done. ","startTime":3135480,"duration":3900,"startOfParagraph":false},{"content":"Now it just so happens that the blue element is again the 5, ","startTime":3139380,"duration":3350,"startOfParagraph":false},{"content":"and we need to look at the 1, the 6 and the 4 to determine that 1 ","startTime":3142730,"duration":3300,"startOfParagraph":false},{"content":"is the smallest minimum element, so we'll swap the 1 and the 5. ","startTime":3146030,"duration":5490,"startOfParagraph":false},{"content":"Again, we need to look at--compare the 5 to the 6 and the 4, ","startTime":3151520,"duration":5370,"startOfParagraph":false},{"content":"and we're going to swap the 4 and the 5, and finally, compare ","startTime":3156890,"duration":2940,"startOfParagraph":false},{"content":"those 2 numbers and swap them until we get our sorted list. ","startTime":3159830,"duration":5910,"startOfParagraph":false},{"content":"Any questions on selection sort? ","startTime":3165740,"duration":3990,"startOfParagraph":false},{"content":"Okay. Let's move to the last topic here, and that is recursion. ","startTime":3169730,"duration":6690,"startOfParagraph":false},{"content":"Recursion, remember, is this really meta thing where a function ","startTime":3176420,"duration":3390,"startOfParagraph":true},{"content":"repeatedly calls itself. ","startTime":3179810,"duration":2930,"startOfParagraph":false},{"content":"So at some point, while our fuction is repeatedly calling itself, ","startTime":3182740,"duration":2880,"startOfParagraph":false},{"content":"there needs to be some point at which we stop calling ourselves. ","startTime":3185620,"duration":4480,"startOfParagraph":false},{"content":"Because if we don't do that, then we're just going to continue to do this forever, ","startTime":3190100,"duration":3570,"startOfParagraph":false},{"content":"and our program is just not going to terminate. ","startTime":3193670,"duration":2990,"startOfParagraph":false},{"content":"We call this condition the base case. ","startTime":3196660,"duration":2540,"startOfParagraph":false},{"content":"And the base case says, rather than calling a function again, ","startTime":3199200,"duration":3370,"startOfParagraph":false},{"content":"I'm just going to return some value. ","startTime":3202570,"duration":2760,"startOfParagraph":false},{"content":"So once we've returned a value, we've stopped calling ourselves, ","startTime":3205330,"duration":2750,"startOfParagraph":false},{"content":"and the rest of the calls we've made so far can also return. ","startTime":3208080,"duration":4470,"startOfParagraph":false},{"content":"The opposite of the base case is the recursive case. ","startTime":3212550,"duration":3500,"startOfParagraph":false},{"content":"And this is when we want to make another call to the function that we're currently in. ","startTime":3216050,"duration":3000,"startOfParagraph":false},{"content":"And we probably, although not always, want to use different arguments. ","startTime":3219050,"duration":5640,"startOfParagraph":false},{"content":"So if we have a function called f, and f just called take 1 argument, ","startTime":3224690,"duration":4250,"startOfParagraph":true},{"content":"and we just keep calling f(1), f(1), f(1), and it just so happens that ","startTime":3228940,"duration":3070,"startOfParagraph":false},{"content":"the argument 1 falls into recursive case, we're still never going to stop. ","startTime":3232010,"duration":4500,"startOfParagraph":false},{"content":"Even if we have a base case, we need to make sure that eventually we're going to hit that base case. ","startTime":3236510,"duration":5110,"startOfParagraph":false},{"content":"We don't just keep staying in this recursive case. ","startTime":3241620,"duration":2630,"startOfParagraph":false},{"content":"Generally, when we call ourselves, we're probably going to have a different argument each time. ","startTime":3244250,"duration":5620,"startOfParagraph":false},{"content":"Here is a really simple recursive function. ","startTime":3249870,"duration":2830,"startOfParagraph":false},{"content":"So this will compute the factorial of a number. ","startTime":3252700,"duration":2390,"startOfParagraph":false},{"content":"Up top here we have our base case. ","startTime":3255090,"duration":2700,"startOfParagraph":false},{"content":"In the case that n ≤ 1, we're not going to call factorial again. ","startTime":3257790,"duration":4540,"startOfParagraph":false},{"content":"We're going to stop; we're just going to return some value. ","startTime":3262330,"duration":4160,"startOfParagraph":false},{"content":"If this is not true, then we're going to hit our recursive case. ","startTime":3266490,"duration":3680,"startOfParagraph":false},{"content":"Notice here that we're not just calling factorial(n), because that wouldn't be very helpful. ","startTime":3270170,"duration":3380,"startOfParagraph":false},{"content":"We're going to call factorial of something else. ","startTime":3273550,"duration":3260,"startOfParagraph":false},{"content":"And so you can see, eventually if we pass a factorial (5) or something,","startTime":3276810,"duration":4040,"startOfParagraph":true},{"content":"we're going to call factorial (4) and so on, and eventually we're going to hit this base case. ","startTime":3280850,"duration":5050,"startOfParagraph":false},{"content":"So this looks good. Let's see what happens when we actually run this. ","startTime":3285900,"duration":5830,"startOfParagraph":false},{"content":"This is the stack, and let's say that main is going to call this function with an argument (4). ","startTime":3291730,"duration":6110,"startOfParagraph":false},{"content":"So once factorial sees and = 4, factorial will call itself. ","startTime":3297840,"duration":4360,"startOfParagraph":false},{"content":"Now, suddenly, we have factorial(3). ","startTime":3302200,"duration":2810,"startOfParagraph":false},{"content":"So these functions are going to keep growing until eventually we hit our base case. ","startTime":3305010,"duration":5770,"startOfParagraph":false},{"content":"At this point, the return value of this is the return(n x the return value of this), ","startTime":3310780,"duration":7050,"startOfParagraph":false},{"content":"the return value of this is n x the return value of this. ","startTime":3317830,"duration":3460,"startOfParagraph":false},{"content":"Eventually we need to hit some number. ","startTime":3321290,"duration":2000,"startOfParagraph":false},{"content":"At the top here, we say return 1. ","startTime":3323290,"duration":3270,"startOfParagraph":false},{"content":"That means that once we return that number, we can pop this off the stack. ","startTime":3326560,"duration":4090,"startOfParagraph":false},{"content":"So this factorial(1) is done. ","startTime":3330650,"duration":5920,"startOfParagraph":false},{"content":"When 1 returns, this factorial(1) returns, this return to 1. ","startTime":3336570,"duration":4620,"startOfParagraph":false},{"content":"The return value of this, remember, was n x the return value of this. ","startTime":3341190,"duration":5720,"startOfParagraph":false},{"content":"So suddenly, this guy knows that I want to return 2. ","startTime":3346910,"duration":3810,"startOfParagraph":false},{"content":"So remember, return value of this is just n x the return value up here. ","startTime":3350720,"duration":5190,"startOfParagraph":true},{"content":"So now we can say 3 x 2, and finally, here we can say","startTime":3355910,"duration":5250,"startOfParagraph":false},{"content":"this is just going to be 4 x 3 x 2. ","startTime":3361160,"duration":2850,"startOfParagraph":false},{"content":"And once this returns, we get down to a single integer inside of main. ","startTime":3364010,"duration":5560,"startOfParagraph":false},{"content":"Any questions on recursion? ","startTime":3369570,"duration":5890,"startOfParagraph":false},{"content":"All right. So there's more time for questions at the end,","startTime":3375460,"duration":1630,"startOfParagraph":false},{"content":"but now Joseph will cover the remaining topics. ","startTime":3377090,"duration":6270,"startOfParagraph":false},{"content":"[Joseph Ong] All right. So now that we've talked about recursions, ","startTime":3383360,"duration":2230,"startOfParagraph":true},{"content":"let's talk a little bit about what merge sort is. ","startTime":3385590,"duration":2250,"startOfParagraph":false},{"content":"Merge sort is basically another way of sorting a list of numbers. ","startTime":3387840,"duration":3900,"startOfParagraph":false},{"content":"And how it works is, with merge sort you have a list, and what we do is","startTime":3391740,"duration":4690,"startOfParagraph":false},{"content":"we say, let's split this into 2 halves. ","startTime":3396430,"duration":2690,"startOfParagraph":false},{"content":"We'll first run merge sort again on the left half, ","startTime":3399120,"duration":3630,"startOfParagraph":false},{"content":"then we'll run merge sort on the right half, ","startTime":3402750,"duration":2290,"startOfParagraph":false},{"content":"and that gives us now 2 halves that are sorted, and now we're going to combine those halves together. ","startTime":3405040,"duration":5200,"startOfParagraph":false},{"content":"It's a bit hard to see without an example, so we'll go through the motions and see what happens. ","startTime":3410240,"duration":4770,"startOfParagraph":false},{"content":"So you start with this list, we split it into 2 halves. ","startTime":3415010,"duration":4580,"startOfParagraph":false},{"content":"We run merge sort on the left half first. ","startTime":3419590,"duration":2710,"startOfParagraph":false},{"content":"So that's the left half, and now we run them through this list again","startTime":3422300,"duration":4360,"startOfParagraph":false},{"content":"which gets passed into merge sort, and then we look, again, ","startTime":3426660,"duration":3140,"startOfParagraph":false},{"content":"at the left side of this list and we run merge sort on it. ","startTime":3429800,"duration":3470,"startOfParagraph":false},{"content":"Now, we get down to a list of 2 numbers, ","startTime":3433270,"duration":2610,"startOfParagraph":false},{"content":"and now the left half is only 1 element long, and we can't ","startTime":3435880,"duration":3130,"startOfParagraph":false},{"content":"split a list that's only 1 element into half, so we just say, once we have 50,","startTime":3439010,"duration":4370,"startOfParagraph":false},{"content":"which is just 1 element, it's already sorted. ","startTime":3443380,"duration":3020,"startOfParagraph":false},{"content":"Once we're done with that, we can see that we can ","startTime":3446400,"duration":3460,"startOfParagraph":true},{"content":"move on to the right half of this list, ","startTime":3449860,"duration":2370,"startOfParagraph":false},{"content":"and 3 is also sorted, and so now that both halves of this list are sorted","startTime":3452230,"duration":4250,"startOfParagraph":false},{"content":"we can join these numbers back together. ","startTime":3456480,"duration":2600,"startOfParagraph":false},{"content":"So we look at 50 and 3; 3 is smaller than 50, so it goes in first and then 50 comes in. ","startTime":3459080,"duration":6240,"startOfParagraph":false},{"content":"Now, that's done; we go back up to that list and sort it's right half. ","startTime":3465320,"duration":4020,"startOfParagraph":false},{"content":"42 is it's own number, so it's already sorted. ","startTime":3469340,"duration":3100,"startOfParagraph":false},{"content":"So now we compare these 2 and 3 is smaller than 42, so that gets put in first, ","startTime":3472440,"duration":5410,"startOfParagraph":false},{"content":"now 42 gets put in, and 50 gets put in. ","startTime":3477850,"duration":4490,"startOfParagraph":false},{"content":"Now, that's sorted, we go all the way back to the top, 1337 and 15. ","startTime":3482340,"duration":4880,"startOfParagraph":false},{"content":"Well, we now look at the left half of this list; 1337 is by itself so it's sorted and same with 15. ","startTime":3487220,"duration":7340,"startOfParagraph":false},{"content":"So now we combine these 2 numbers to sort that original list, 15 < 1337, ","startTime":3494560,"duration":4460,"startOfParagraph":false},{"content":"so it goes in first, then 1337 goes in. ","startTime":3499020,"duration":4040,"startOfParagraph":false},{"content":"And now we sorted both halves of the original list up top. ","startTime":3503060,"duration":3580,"startOfParagraph":false},{"content":"And all we have to do is combine these. ","startTime":3506640,"duration":3800,"startOfParagraph":false},{"content":"We look at the first 2 numbers of this list, 3 < 15, so it goes into the sort array first. ","startTime":3510440,"duration":6450,"startOfParagraph":false},{"content":"15 < 42, so it goes in. Now, 42 < 1337, that goes in. ","startTime":3516890,"duration":7570,"startOfParagraph":false},{"content":"50 < 1337, so it goes in. And notice that we just took 2 numbers off of this list. ","startTime":3524460,"duration":6550,"startOfParagraph":false},{"content":"So we're not just alternating between the 2 lists. ","startTime":3531010,"duration":2630,"startOfParagraph":false},{"content":"We're just looking at the beginning, and we're taking the element","startTime":3533640,"duration":2410,"startOfParagraph":false},{"content":"that's smaller and then putting it into our array. ","startTime":3536050,"duration":4220,"startOfParagraph":false},{"content":"Now we've merged all the halves and we're done. ","startTime":3540270,"duration":3810,"startOfParagraph":false},{"content":"Any questions about merge sort? Yes? ","startTime":3544080,"duration":3700,"startOfParagraph":true},{"content":"[Student] If it's splitting into different groups, why don't they just split it once","startTime":3547780,"duration":6410,"startOfParagraph":false},{"content":"and you have 3 and 2 in a group? [Rest of question unintelligible]","startTime":3554190,"duration":5780,"startOfParagraph":false},{"content":"The reason--so the question is, why can't we just merge them at that first step after we have them?","startTime":3559970,"duration":4970,"startOfParagraph":false},{"content":"The reason we can do this, start at the left-most elements of both sides,","startTime":3564940,"duration":4590,"startOfParagraph":false},{"content":"and then take the smaller one and put it in, is that we know that these ","startTime":3569530,"duration":3510,"startOfParagraph":false},{"content":"individual lists are in sorted orders. ","startTime":3573040,"duration":2250,"startOfParagraph":false},{"content":"So if I'm looking at the left-most elements of both halves, ","startTime":3575290,"duration":2000,"startOfParagraph":false},{"content":"I know they're going to be the smallest elements of those lists. ","startTime":3577290,"duration":3200,"startOfParagraph":false},{"content":"So I can put them into the smallest element spots of this large list. ","startTime":3580490,"duration":3440,"startOfParagraph":false},{"content":"On the other hand, if I look at those 2 lists in the second level over there, ","startTime":3583930,"duration":3880,"startOfParagraph":false},{"content":"50, 3, 42, 1337 and 15, those aren't sorted. ","startTime":3587810,"duration":3830,"startOfParagraph":false},{"content":"So if I look at 50 and 1337, I'm going to put 50 into my list first. ","startTime":3591640,"duration":4130,"startOfParagraph":false},{"content":"But that doesn't really make sense, because 3 is the smallest element out of all of those. ","startTime":3595770,"duration":4360,"startOfParagraph":false},{"content":"So the only reason we can do this combining step is because our lists are already sorted. ","startTime":3600130,"duration":4260,"startOfParagraph":false},{"content":"Which is why we have to get down all the way to the bottom","startTime":3604390,"duration":2620,"startOfParagraph":false},{"content":"because when we have just a single number, you know that a single number ","startTime":3607010,"duration":2790,"startOfParagraph":false},{"content":"in and of itself is already a sorted list. ","startTime":3609800,"duration":4320,"startOfParagraph":false},{"content":"Any questions? No?","startTime":3614120,"duration":5240,"startOfParagraph":true},{"content":"Complexity? Well, you can see that at each step there's end numbers, ","startTime":3619360,"duration":4900,"startOfParagraph":false},{"content":"and we can divide a list in half log n times, ","startTime":3624260,"duration":3330,"startOfParagraph":false},{"content":"which is where we get this n x log n complexity. ","startTime":3627590,"duration":4110,"startOfParagraph":false},{"content":"And you'll see the best case for merge sort is n log n, and it just so happens ","startTime":3631700,"duration":3240,"startOfParagraph":false},{"content":"that the worst case, or the Ω over there, is also n log n. ","startTime":3634940,"duration":4400,"startOfParagraph":false},{"content":"Something to keep in mind. ","startTime":3639340,"duration":3140,"startOfParagraph":false},{"content":"Moving on, let's go on to some super basic file I/O. ","startTime":3642480,"duration":3270,"startOfParagraph":false},{"content":"If you looked at Scramble, you'll notice we had some sort of system ","startTime":3645750,"duration":3080,"startOfParagraph":false},{"content":"where you could write to a log file if you read through the code. ","startTime":3648830,"duration":2440,"startOfParagraph":false},{"content":"Let's see how you might do that. ","startTime":3651270,"duration":2460,"startOfParagraph":false},{"content":"Well, we have fprintf, which you can think of as just printf, ","startTime":3653730,"duration":3720,"startOfParagraph":false},{"content":"but just printing to a file instead, and hence the f at the beginning. ","startTime":3657450,"duration":4270,"startOfParagraph":false},{"content":"This sort of code up here, what it does is, as you might have seen in Scramble, ","startTime":3661720,"duration":5850,"startOfParagraph":false},{"content":"it goes through your 2-dimensional array printing out row by row what the numbers are. ","startTime":3667570,"duration":4740,"startOfParagraph":false},{"content":"In this case, printf prints out to your terminal or what we call the standard output of section. ","startTime":3672310,"duration":5540,"startOfParagraph":false},{"content":"And now, in this case, all we have to do is replace printf with fprintf, ","startTime":3677850,"duration":4320,"startOfParagraph":true},{"content":"tell it what file you want to print to, and in this case it just prints it out to that file ","startTime":3682170,"duration":4600,"startOfParagraph":false},{"content":"instead of printing it out to your terminal. ","startTime":3686770,"duration":5460,"startOfParagraph":false},{"content":"Well, then that begs the question: Where do we get this sort of file from, right? ","startTime":3692230,"duration":4270,"startOfParagraph":false},{"content":"We passed log in to this fprintf fuction but we had no idea where it came from. ","startTime":3696500,"duration":3340,"startOfParagraph":false},{"content":"Well, early in the code, what we had was this chunk of code over here, ","startTime":3699840,"duration":4140,"startOfParagraph":false},{"content":"which basically says that open the file calls log.txt. ","startTime":3703980,"duration":4360,"startOfParagraph":false},{"content":"What we do after that is we have to make sure that the file is actually opened successfully. ","startTime":3708340,"duration":4880,"startOfParagraph":false},{"content":"So it might fail for multiple reasons; you don't have enough space on your computer, for example. ","startTime":3713220,"duration":3850,"startOfParagraph":false},{"content":"So it's always important before you do any operations with the file ","startTime":3717070,"duration":2720,"startOfParagraph":false},{"content":"that we check whether that file was opened successfully. ","startTime":3719790,"duration":3510,"startOfParagraph":false},{"content":"So what that a, that's an argument to fopen, well, we can open a file in many ways. ","startTime":3723300,"duration":6030,"startOfParagraph":false},{"content":"What we can do is, we can pass it w, which means override the file if it exits already, ","startTime":3729330,"duration":4180,"startOfParagraph":false},{"content":"We can pass an a, which they append to the end of the file instead of overriding it, ","startTime":3733510,"duration":4560,"startOfParagraph":false},{"content":"or we can specify r, which means, let's open the file as read-only. ","startTime":3738070,"duration":4660,"startOfParagraph":false},{"content":"So if the program tries to make any changes to the file, ","startTime":3742730,"duration":2160,"startOfParagraph":false},{"content":"yell at them and don't let them do it. ","startTime":3744890,"duration":5250,"startOfParagraph":false},{"content":"Finally, once we're done with the file, done doing operations on it, ","startTime":3750140,"duration":3180,"startOfParagraph":false},{"content":"we need to make sure we close the file. ","startTime":3753320,"duration":2540,"startOfParagraph":false},{"content":"And so at the end of your program, you are going to pass them again ","startTime":3755860,"duration":2970,"startOfParagraph":false},{"content":"this file that you opened, and just close it. ","startTime":3758830,"duration":3290,"startOfParagraph":false},{"content":"So this is something important that you have to make sure you do. ","startTime":3762120,"duration":2530,"startOfParagraph":false},{"content":"So remember you can open a file, then you can write to the file, ","startTime":3764650,"duration":2530,"startOfParagraph":false},{"content":"do operations in the file, but then you have to close the file at the end. ","startTime":3767180,"duration":4090,"startOfParagraph":false},{"content":"Any questions on basic file I/O? Yes?","startTime":3771270,"duration":2000,"startOfParagraph":true},{"content":"[Student question, unintelligible] ","startTime":3773270,"duration":4780,"startOfParagraph":false},{"content":"Right here. The question is, where does this log.txt file appear? ","startTime":3778050,"duration":4430,"startOfParagraph":false},{"content":"Well, if you just give it log.txt, it creates it in the same directory as the executable. ","startTime":3782480,"duration":5410,"startOfParagraph":false},{"content":"So if you're-- >>[Student question, unintelligible]","startTime":3787890,"duration":2610,"startOfParagraph":false},{"content":"Yes. In the same folder, or in the same directory, as you call it. ","startTime":3790500,"duration":8330,"startOfParagraph":false},{"content":"Now memory, stack, and heap. ","startTime":3798830,"duration":2570,"startOfParagraph":false},{"content":"So how is memory laid out in the computer? ","startTime":3801400,"duration":2000,"startOfParagraph":false},{"content":"Well, you can imagine memory as sort of this block here.","startTime":3803400,"duration":2870,"startOfParagraph":false},{"content":"And in memory we have what's called the heap stuck over there, and the stack that's down there. ","startTime":3806270,"duration":3990,"startOfParagraph":false},{"content":"And the heap grows downward and the stack grows upward. ","startTime":3810260,"duration":4220,"startOfParagraph":false},{"content":"So as Tommy mentioned--oh, well, and we have these other 4 segments which I'll get to in a second--","startTime":3814480,"duration":4140,"startOfParagraph":false},{"content":"As Tommy said earlier, you know how his functions call themselves and call each other? ","startTime":3818620,"duration":4270,"startOfParagraph":false},{"content":"They build up this sort of stack frame.","startTime":3822890,"duration":2040,"startOfParagraph":false},{"content":"Well, if main calls foo, foo gets put on the stack. ","startTime":3824930,"duration":2430,"startOfParagraph":false},{"content":"Foo calls bar, bar get's put on the stack, and that gets put on the stack after. ","startTime":3827360,"duration":5070,"startOfParagraph":false},{"content":"And as they return, they each get taken off the stack. ","startTime":3832430,"duration":4610,"startOfParagraph":false},{"content":"What do each of these locations and memory hold? ","startTime":3837040,"duration":3100,"startOfParagraph":false},{"content":"Well, the top, which is the text segment, contains the program itself. ","startTime":3840140,"duration":2970,"startOfParagraph":false},{"content":"So the machine code, that's there, once you compile your program. ","startTime":3843110,"duration":3280,"startOfParagraph":false},{"content":"Next, any initialized global variables. ","startTime":3846390,"duration":2130,"startOfParagraph":false},{"content":"So you have global variables in your program, and you say like, a = 5, ","startTime":3848520,"duration":4140,"startOfParagraph":true},{"content":"that gets put in that segment, and right under that, ","startTime":3852660,"duration":2600,"startOfParagraph":false},{"content":"you have any uninitialized global data, which is just int a, ","startTime":3855260,"duration":3730,"startOfParagraph":false},{"content":"but you don't say it's equal to anything. ","startTime":3858990,"duration":2000,"startOfParagraph":false},{"content":"Realize these are global variables, so they're outside of main. ","startTime":3860990,"duration":2880,"startOfParagraph":false},{"content":"So this means any global variables that are declared but are not initialized. ","startTime":3863870,"duration":4690,"startOfParagraph":false},{"content":"So what's in the heap? Memory allocated using malloc, which we'll get to in a little bit.","startTime":3868560,"duration":3750,"startOfParagraph":false},{"content":"And finally, with the stack you have any local variables","startTime":3872310,"duration":3680,"startOfParagraph":false},{"content":"and any functions you might call in any of their parameters. ","startTime":3875990,"duration":3960,"startOfParagraph":false},{"content":"The last thing, you don't really have to know what the environment variables do, ","startTime":3879950,"duration":3770,"startOfParagraph":false},{"content":"but whenever you run program, there is something associated, like","startTime":3883720,"duration":2980,"startOfParagraph":false},{"content":"this is the username of the person who ran the program. ","startTime":3886700,"duration":2850,"startOfParagraph":false},{"content":"And that's going to be sort of at the bottom. ","startTime":3889550,"duration":2000,"startOfParagraph":false},{"content":"In terms of memory addresses, which are hexadecimal values, ","startTime":3891550,"duration":2990,"startOfParagraph":false},{"content":"the values at the top start at 0, and they go all the way down to the bottom. ","startTime":3894540,"duration":3630,"startOfParagraph":false},{"content":"In this case, if you're on the 32-bit system, ","startTime":3898170,"duration":2270,"startOfParagraph":false},{"content":"the address at the bottom is going to be 0x, followed by af, because that's 32 bits, ","startTime":3900440,"duration":4950,"startOfParagraph":false},{"content":"which is 8 bytes, and in this case 8 bytes corresponds to 8 hexadecimal digits. ","startTime":3905390,"duration":5500,"startOfParagraph":false},{"content":"So down here you're going to have, like, 0xffffff, and up there you're going to have 0.","startTime":3910890,"duration":9220,"startOfParagraph":false},{"content":"So what are pointers? Some of you may not have covered this in section before. ","startTime":3920110,"duration":3550,"startOfParagraph":false},{"content":"but we did go over it in lecture, so a pointer is just a data type ","startTime":3923660,"duration":3000,"startOfParagraph":false},{"content":"which stores, instead of some sort of value like 50, it stores the address of some location in memory. ","startTime":3926660,"duration":7370,"startOfParagraph":false},{"content":"Like that memory [unintelligible]. ","startTime":3934030,"duration":1990,"startOfParagraph":false},{"content":"So in this case, what we have is, we have a pointer to an integer or an int *, ","startTime":3936020,"duration":5100,"startOfParagraph":false},{"content":"and it contains this hexadecimal address of 0xDEADBEEF. ","startTime":3941120,"duration":5090,"startOfParagraph":false},{"content":"So what we have is, now, this pointer points at some location in memory, ","startTime":3946210,"duration":4670,"startOfParagraph":true},{"content":"and that's just a, the value 50 is at this memory location. ","startTime":3950880,"duration":5140,"startOfParagraph":false},{"content":"On some 32-bit systems, on all 32-bit systems, pointers take up 32 bits or 4 bytes. ","startTime":3956020,"duration":5790,"startOfParagraph":false},{"content":"But, for example, on a 64-bit system, pointers are 64 bits. ","startTime":3961810,"duration":4210,"startOfParagraph":false},{"content":"So that's something you'll want to keep in mind. ","startTime":3966020,"duration":2020,"startOfParagraph":false},{"content":"So on an end-bit system, a pointer is end bits long. ","startTime":3968040,"duration":4270,"startOfParagraph":false},{"content":"Pointers are sort of hard to digest without extra things, ","startTime":3972310,"duration":5010,"startOfParagraph":false},{"content":"so let's go through an example of dynamic memory allocation. ","startTime":3977320,"duration":2980,"startOfParagraph":false},{"content":"What dynamic memory allocation does for you, or what we call malloc, ","startTime":3980300,"duration":4830,"startOfParagraph":false},{"content":"it lets you allocate some sort of data outside of the set. ","startTime":3985130,"duration":4150,"startOfParagraph":false},{"content":"So this data is sort of more permanent for the duration of the program. ","startTime":3989280,"duration":2550,"startOfParagraph":false},{"content":"Because as you know, if you declare x inside of a function, and that function returns, ","startTime":3991830,"duration":4600,"startOfParagraph":false},{"content":"you no longer have access to the data that was stored in x.","startTime":3996430,"duration":4480,"startOfParagraph":false},{"content":"What pointers let us do is they let us store memory or store values ","startTime":4000910,"duration":3510,"startOfParagraph":false},{"content":"in a different segment of memory, namely the heap. ","startTime":4004420,"duration":2420,"startOfParagraph":false},{"content":"Now once we return out of function, as long as we have a pointer ","startTime":4006840,"duration":2500,"startOfParagraph":false},{"content":"to that location in memory, then what we can do is we can just look at the values there. ","startTime":4009340,"duration":5620,"startOfParagraph":false},{"content":"Let's look at an example: This is our memory layout again. ","startTime":4014960,"duration":3060,"startOfParagraph":false},{"content":"And we have this function, main. ","startTime":4018020,"duration":2030,"startOfParagraph":false},{"content":"What it does is--okay, so simple, right?--int x = 5, that's just a variable on the stack in main. ","startTime":4020050,"duration":6820,"startOfParagraph":false},{"content":"On the other hand, now we declare a pointer which calls the function giveMeThreeInts.","startTime":4026870,"duration":5580,"startOfParagraph":true},{"content":"And so now we go into this function and we create a new stack frame for it. ","startTime":4032450,"duration":4350,"startOfParagraph":false},{"content":"However, in this stack frame, we declare int * temp,","startTime":4036800,"duration":3640,"startOfParagraph":false},{"content":"which in mallocs 3 integers for us.","startTime":4040440,"duration":2770,"startOfParagraph":false},{"content":"So size of int will give us how many bytes this int is, ","startTime":4043210,"duration":2670,"startOfParagraph":false},{"content":"and malloc gives us that many bytes of space on the heap. ","startTime":4045880,"duration":3740,"startOfParagraph":false},{"content":"So in this case, we have created enough space for 3 integers, ","startTime":4049620,"duration":3270,"startOfParagraph":false},{"content":"and the heap is way up there, which is why I've drawn it higher up. ","startTime":4052890,"duration":3940,"startOfParagraph":false},{"content":"Once we're done, we come back up here, you only need 3 ints returned, ","startTime":4056830,"duration":6070,"startOfParagraph":false},{"content":"and it returns the address, in this case over where that memory is. ","startTime":4062900,"duration":4100,"startOfParagraph":false},{"content":"And we set pointer = switch, and up there we have just another pointer. ","startTime":4067000,"duration":4250,"startOfParagraph":false},{"content":"But what that function returns is stacked here and disappears. ","startTime":4071250,"duration":3300,"startOfParagraph":false},{"content":"So temp disappears, but we still maintain the address of where ","startTime":4074550,"duration":4700,"startOfParagraph":false},{"content":"those 3 integers are inside of mains. ","startTime":4079250,"duration":2600,"startOfParagraph":false},{"content":"So in this set, the pointers are scoped locally for the stacked frame, ","startTime":4081850,"duration":4330,"startOfParagraph":false},{"content":"but the memory to which they refer is in the heap. ","startTime":4086180,"duration":3680,"startOfParagraph":false},{"content":"Does that make sense? ","startTime":4089860,"duration":2330,"startOfParagraph":true},{"content":"[Student] Could you repeat that? >>[Joseph] Yes. ","startTime":4092190,"duration":2770,"startOfParagraph":false},{"content":"So if I go back just a little bit, you see that temp allocated ","startTime":4094960,"duration":5310,"startOfParagraph":false},{"content":"some memory on the heap up there. ","startTime":4100270,"duration":3230,"startOfParagraph":false},{"content":"So when this function, giveMeThreeInts returns, this stack here is going to disappear. ","startTime":4103500,"duration":5180,"startOfParagraph":false},{"content":"And with it any of the variables, in this case, this pointer that was allocated in stacked frame. ","startTime":4108680,"duration":7139,"startOfParagraph":false},{"content":"That is going to disappear, but since we returned temp ","startTime":4115819,"duration":3830,"startOfParagraph":false},{"content":"and we set pointer = temp, pointer's now going to point the same memory of location as temp was. ","startTime":4119649,"duration":6681,"startOfParagraph":false},{"content":"So now, even though we lose temp, that local pointer, ","startTime":4126330,"duration":4040,"startOfParagraph":false},{"content":"we still retain the memory address of what it was pointing to inside of that variable pointer. ","startTime":4130370,"duration":8739,"startOfParagraph":false},{"content":"Questions? That can be kind of a confusing topic if you haven't gone over it in section. ","startTime":4139109,"duration":4631,"startOfParagraph":false},{"content":"We can, your TF will definitely go over it and of course we can answer questions ","startTime":4143740,"duration":5500,"startOfParagraph":false},{"content":"at the end of the review session for this. ","startTime":4149240,"duration":2260,"startOfParagraph":false},{"content":"But this is sort of a complex topic, and I have more examples that are going to show up","startTime":4151500,"duration":2720,"startOfParagraph":false},{"content":"that will help clarify what pointers actually are. ","startTime":4154220,"duration":4570,"startOfParagraph":false},{"content":"In this case, pointers are equivalent to arrays,","startTime":4158790,"duration":3710,"startOfParagraph":true},{"content":"so I can just use this pointer as the same thing as an int array. ","startTime":4162500,"duration":2729,"startOfParagraph":false},{"content":"So I'm indexing into 0, and changing the first integer to 1, ","startTime":4165229,"duration":4611,"startOfParagraph":false},{"content":"changing the second integer to 2, and the 3rd integer to 3. ","startTime":4169840,"duration":9849,"startOfParagraph":false},{"content":"So more on pointers. Well, recall Binky. ","startTime":4179689,"duration":4521,"startOfParagraph":false},{"content":"In this case we've allocated a pointer, or we declared a pointer, ","startTime":4184210,"duration":4109,"startOfParagraph":false},{"content":"but initially, when I just declared a pointer, it's not pointing to anywhere in memory. ","startTime":4188319,"duration":4441,"startOfParagraph":false},{"content":"It's just garbage values inside of it. ","startTime":4192760,"duration":2170,"startOfParagraph":false},{"content":"So I have no idea where this pointer is pointing to. ","startTime":4194930,"duration":1540,"startOfParagraph":false},{"content":"It has an address which is just filled with 0's and 1's where it was initially declared. ","startTime":4196470,"duration":5160,"startOfParagraph":false},{"content":"I can't do anything with this until I call malloc on it","startTime":4201630,"duration":3180,"startOfParagraph":false},{"content":"and then it gives me a little space on the heap where I can put values inside. ","startTime":4204810,"duration":3580,"startOfParagraph":false},{"content":"Then again, I don't know what's inside of this memory.","startTime":4208390,"duration":3590,"startOfParagraph":false},{"content":"So the first thing I have to do is check whether the system had enough memory ","startTime":4211980,"duration":4800,"startOfParagraph":false},{"content":"to give me back 1 integer in the first place, which is why I'm doing this check. ","startTime":4216780,"duration":4070,"startOfParagraph":false},{"content":"If pointer is null, that means that it didn't have enough space or some other error occurred, ","startTime":4220850,"duration":4170,"startOfParagraph":false},{"content":"so I should exit out of my program.","startTime":4225020,"duration":1300,"startOfParagraph":false},{"content":" But if it did succeed, now I can use that pointer","startTime":4226320,"duration":3080,"startOfParagraph":false},{"content":"and what * pointer does is it follows where the address is","startTime":4229400,"duration":5620,"startOfParagraph":false},{"content":"to where that value is, and it sets it equal to 1. ","startTime":4235020,"duration":3460,"startOfParagraph":false},{"content":"So over here, we're checking if that memory existed. ","startTime":4238480,"duration":3370,"startOfParagraph":false},{"content":"Once you know it exists, you can put into it","startTime":4241850,"duration":3530,"startOfParagraph":true},{"content":"what value you want to put into it; in this case 1. ","startTime":4245380,"duration":5080,"startOfParagraph":false},{"content":"Once we're done with it, you need to free that pointer","startTime":4250460,"duration":2600,"startOfParagraph":false},{"content":"because we need to get back to the system that memory that you asked for in the first place. ","startTime":4253060,"duration":4100,"startOfParagraph":false},{"content":"Because the computer doesn't know when we're done with it. ","startTime":4257160,"duration":2530,"startOfParagraph":false},{"content":"In this case we're explicitly telling it, okay, we're done with that memory. ","startTime":4259690,"duration":2820,"startOfParagraph":false},{"content":"If some other process needs it, some other program needs it, feel free to go ahead and take it. ","startTime":4262510,"duration":8270,"startOfParagraph":false},{"content":"What we can also do is we can just get the address of local variables on the set. ","startTime":4270780,"duration":4330,"startOfParagraph":false},{"content":"So int x is inside the stacked frame of main. ","startTime":4275110,"duration":3970,"startOfParagraph":false},{"content":"And when we use this ampersand, this and operator, what it does is ","startTime":4279080,"duration":3980,"startOfParagraph":false},{"content":"it takes x, and x is just some data in memory, but it has an address. ","startTime":4283060,"duration":4250,"startOfParagraph":false},{"content":"It's located somewhere. So by calling & x, what this does is it gives us the address of x. ","startTime":4287310,"duration":6480,"startOfParagraph":false},{"content":"By doing this, we're making pointer point to where x is in memory. ","startTime":4293790,"duration":4640,"startOfParagraph":false},{"content":"Now we just do something like *x, we're going to get 5 back. ","startTime":4298430,"duration":3280,"startOfParagraph":false},{"content":"The star is called dereferencing it. ","startTime":4301710,"duration":2110,"startOfParagraph":false},{"content":"You follow the address and you get the value of it stored there. ","startTime":4303820,"duration":2820,"startOfParagraph":false},{"content":"Any questions? Yes? ","startTime":4311000,"duration":2310,"startOfParagraph":true},{"content":"[Student] If you don't do the 3-pointed thing, does it still compile? ","startTime":4313310,"duration":3190,"startOfParagraph":false},{"content":"Yes. If you don't do the 3-pointer thing, it's still going to compile,","startTime":4316500,"duration":2990,"startOfParagraph":false},{"content":"but I'll show you what happens in a second, and without doing that, ","startTime":4319490,"duration":3230,"startOfParagraph":false},{"content":"that's what we call a memory leak. You're not giving the system ","startTime":4322720,"duration":2140,"startOfParagraph":false},{"content":"back its memory, so after a while the program is going to accumulate ","startTime":4324860,"duration":2990,"startOfParagraph":false},{"content":"memory that it's not using, and nothing else can use it. ","startTime":4327850,"duration":3090,"startOfParagraph":false},{"content":"If you've ever seen Firefox with 1.5 million kilobytes on your computer, ","startTime":4330940,"duration":4810,"startOfParagraph":false},{"content":"in the task manager, that's what's going on. ","startTime":4335750,"duration":2090,"startOfParagraph":false},{"content":"You have a memory leak in the program that they're not handling. ","startTime":4337840,"duration":2920,"startOfParagraph":false},{"content":"So how does pointer arithmetic work? ","startTime":4343080,"duration":3160,"startOfParagraph":false},{"content":"Well, pointer arithmetic is sort of like indexing into an array. ","startTime":4346240,"duration":3240,"startOfParagraph":false},{"content":"In this case, I have a pointer, and what I do is I make pointer point to the first element ","startTime":4349480,"duration":6890,"startOfParagraph":false},{"content":"of this array of 3 integers that I've allocated. ","startTime":4356370,"duration":5730,"startOfParagraph":false},{"content":"So now what I do, star pointer just changes the first element in the list. ","startTime":4362100,"duration":4570,"startOfParagraph":false},{"content":"Star pointer +1 points over here. ","startTime":4366670,"duration":2470,"startOfParagraph":false},{"content":"So pointer is over here, pointer +1 is over here, pointer +2 is over here. ","startTime":4369140,"duration":4000,"startOfParagraph":false},{"content":"So just adding 1 is the same thing as moving along this array. ","startTime":4373140,"duration":3470,"startOfParagraph":true},{"content":"What we do is, when we do pointer +1 you get the address over here, ","startTime":4376610,"duration":3270,"startOfParagraph":false},{"content":"and in order to get the value in here, you put a star in from the entire expression","startTime":4379880,"duration":4300,"startOfParagraph":false},{"content":"to dereference it.","startTime":4384180,"duration":1810,"startOfParagraph":false},{"content":"So, in this case, I'm setting the first location in this array to 1,","startTime":4385990,"duration":3950,"startOfParagraph":false},{"content":"second location to 2, and third location to 3.","startTime":4389940,"duration":4030,"startOfParagraph":false},{"content":"Then what I'm doing over here is I'm printing our pointer +1,","startTime":4393970,"duration":4210,"startOfParagraph":false},{"content":"which just gives me 2.","startTime":4398180,"duration":1790,"startOfParagraph":false},{"content":"Now I'm incrementing pointer, so pointer equals pointer +1,","startTime":4399970,"duration":3680,"startOfParagraph":false},{"content":"which moves it forward.","startTime":4403650,"duration":3130,"startOfParagraph":false},{"content":"And so now if I print out pointer +1, pointer +1 is now 3,","startTime":4406780,"duration":4030,"startOfParagraph":false},{"content":"which in this case prints out 3.","startTime":4410810,"duration":3180,"startOfParagraph":false},{"content":"And in order to free something, the pointer that I give it","startTime":4413990,"duration":2570,"startOfParagraph":false},{"content":"must be pointing at the beginning of the array which I got back from malloc.","startTime":4416560,"duration":3980,"startOfParagraph":false},{"content":"So, in this case, if I were to call 3 right here, this wouldn't be right,","startTime":4420540,"duration":2890,"startOfParagraph":false},{"content":"because it's in the middle of the array.","startTime":4423430,"duration":1640,"startOfParagraph":false},{"content":"I have to subtract to get to the original location","startTime":4425070,"duration":3750,"startOfParagraph":false},{"content":"the initial first spot before I can free it.","startTime":4428820,"duration":1600,"startOfParagraph":false},{"content":"So, here's a more involved example.","startTime":4436300,"duration":2150,"startOfParagraph":false},{"content":"In this case, we're allocating 7 characters in a character array.","startTime":4438450,"duration":4910,"startOfParagraph":false},{"content":"And in this case what we're doing is we're looping over the first 6 of them,","startTime":4443360,"duration":3120,"startOfParagraph":true},{"content":"and we're setting them to Z.","startTime":4446480,"duration":3420,"startOfParagraph":false},{"content":"So, for int i = 0, i > 6, i ++, ","startTime":4449900,"duration":3450,"startOfParagraph":false},{"content":"So, pointer +i will just give us, in this case, ","startTime":4453350,"duration":2870,"startOfParagraph":false},{"content":"pointer, pointer +1, pointer +2, pointer +3, and so on and so forth in the loop.","startTime":4456220,"duration":4640,"startOfParagraph":false},{"content":"What it's going to do is it gets that address, dereferences it to get the value,","startTime":4460860,"duration":3180,"startOfParagraph":false},{"content":"and changes that value to a Z.","startTime":4464040,"duration":3400,"startOfParagraph":false},{"content":"Then at the end remember this is a string, right?","startTime":4467440,"duration":2910,"startOfParagraph":false},{"content":"All strings have to end with the null terminating character.","startTime":4470350,"duration":3210,"startOfParagraph":false},{"content":"So, what I do is in pointer 6 I put the null terminator character in.","startTime":4473560,"duration":5060,"startOfParagraph":false},{"content":"And now what I'm basically doing over here is implementing printf for a string, right?","startTime":4478620,"duration":5360,"startOfParagraph":false},{"content":"So, when does printf now when it's reached the end of a string?","startTime":4483980,"duration":2210,"startOfParagraph":true},{"content":"When it hits the null terminating character.","startTime":4486190,"duration":2040,"startOfParagraph":false},{"content":"So, in this case, my original pointer points to the beginning of this array.","startTime":4488230,"duration":3800,"startOfParagraph":false},{"content":"I print the first character out. I move it over one.","startTime":4492030,"duration":4380,"startOfParagraph":false},{"content":"I print that character out. I move it over.","startTime":4496410,"duration":2010,"startOfParagraph":false},{"content":"And I keep doing this until I reach the end.","startTime":4498420,"duration":3760,"startOfParagraph":false},{"content":"And now the end *pointer will dereference this and get the null terminating character back.","startTime":4502180,"duration":5570,"startOfParagraph":false},{"content":"And so my while loop runs only when that value is not the null terminating character.","startTime":4507750,"duration":4030,"startOfParagraph":false},{"content":"So, now I exit out of this loop.","startTime":4511780,"duration":1990,"startOfParagraph":false},{"content":"And so if I subtract 6 from this pointer,","startTime":4518780,"duration":2400,"startOfParagraph":false},{"content":"I go back all the way to the beginning.","startTime":4521180,"duration":1680,"startOfParagraph":false},{"content":"Remember, I'm doing this because I have to go to the beginning in order to free it.","startTime":4522860,"duration":5020,"startOfParagraph":false},{"content":"So, I know that was a lot. Are there any questions?","startTime":4527880,"duration":2390,"startOfParagraph":true},{"content":"Please, yes?","startTime":4530270,"duration":1600,"startOfParagraph":false},{"content":"[Student question unintelligible]","startTime":4531870,"duration":4740,"startOfParagraph":false},{"content":"Can you say that louder? Sorry.","startTime":4536610,"duration":1580,"startOfParagraph":false},{"content":"[Student] On the last slide right before you freed the pointer,","startTime":4538190,"duration":5950,"startOfParagraph":false},{"content":"where were you actually changing the value of the pointer?","startTime":4544140,"duration":3160,"startOfParagraph":false},{"content":"[Joseph] So, right here. >>[Student] Oh, okay.","startTime":4547300,"duration":3070,"startOfParagraph":false},{"content":"[Joseph] So, I have a pointer minus minus, right,","startTime":4550370,"duration":1520,"startOfParagraph":false},{"content":"which moves the thing back one, and then I free it,","startTime":4551890,"duration":2250,"startOfParagraph":false},{"content":"because this pointer has to be pointed to the beginning of the array.","startTime":4554140,"duration":2860,"startOfParagraph":false},{"content":"[Student] But that wouldn't be needed had you stopped after that line.","startTime":4557000,"duration":3420,"startOfParagraph":false},{"content":"[Joseph] So, if I had stopped after this, this would be considered a memory leak,","startTime":4560420,"duration":2710,"startOfParagraph":false},{"content":"because I didn't run the free.","startTime":4563130,"duration":1680,"startOfParagraph":false},{"content":"[Student] I [unintelligible] after the first three lines where you had pointer +1 [unintelligible].","startTime":4564810,"duration":6480,"startOfParagraph":false},{"content":"[Joseph] Uh-huh. So, what's the question there?","startTime":4571290,"duration":1850,"startOfParagraph":false},{"content":"Sorry. No, no. Go, go, please.","startTime":4573140,"duration":1640,"startOfParagraph":false},{"content":"[Student] So, you're not changing the value of pointers.","startTime":4574780,"duration":2090,"startOfParagraph":false},{"content":"You wouldn't have had to do pointer minus minus.","startTime":4576870,"duration":2260,"startOfParagraph":false},{"content":"[Joseph] Yes, exactly.","startTime":4579130,"duration":600,"startOfParagraph":false},{"content":"So, when I do pointer +1 and pointer +2, ","startTime":4579730,"duration":2160,"startOfParagraph":false},{"content":"I'm not doing pointer equals pointer +1.","startTime":4581890,"duration":2520,"startOfParagraph":false},{"content":"So, the pointer just stays pointing at the beginning of the array.","startTime":4584410,"duration":2850,"startOfParagraph":false},{"content":"It's only when I do plus plus that it sets the value back inside the pointer,","startTime":4587260,"duration":4200,"startOfParagraph":false},{"content":"that it actually moves this along.","startTime":4591460,"duration":2090,"startOfParagraph":false},{"content":"All right.","startTime":4596860,"duration":920,"startOfParagraph":false},{"content":"More questions?","startTime":4600550,"duration":1480,"startOfParagraph":false},{"content":"Again, if this is sort of overwhelming, this will be covered in session.","startTime":4604680,"duration":3110,"startOfParagraph":true},{"content":"Ask your teaching fellow about it, and we can answer questions at the end.","startTime":4607790,"duration":2920,"startOfParagraph":false},{"content":"And usually we don't like to do this minus thing.","startTime":4613510,"duration":3090,"startOfParagraph":false},{"content":"This has to require me keeping track of how much I've offset in the array.","startTime":4616600,"duration":3160,"startOfParagraph":false},{"content":"So, in general, this is just to explain how pointer arithmetic works.","startTime":4619760,"duration":4760,"startOfParagraph":false},{"content":"But what we usually like to do is we like to create a copy of the pointer,","startTime":4624520,"duration":3450,"startOfParagraph":false},{"content":"and then we'll use that copy when we're moving around in the string.","startTime":4627970,"duration":3670,"startOfParagraph":false},{"content":"So, in these case you use the copy to print the entire string,","startTime":4631640,"duration":3020,"startOfParagraph":false},{"content":"but we don't have to do like pointer minus 6 or keep track of how much we moved in this,","startTime":4634660,"duration":4380,"startOfParagraph":false},{"content":"just because we know that our original point is still pointed to the beginning of the list","startTime":4639040,"duration":3660,"startOfParagraph":false},{"content":"and all that we altered was this copy.","startTime":4642700,"duration":2640,"startOfParagraph":false},{"content":"So, in general, alter copies of your original pointer.","startTime":4645340,"duration":2910,"startOfParagraph":false},{"content":"Don't try to sort of like--don't alter original copies.","startTime":4648250,"duration":4100,"startOfParagraph":false},{"content":"Trying to alter only copies of your original.","startTime":4652350,"duration":2940,"startOfParagraph":false},{"content":"So, you notice when we pass the string into printf","startTime":4661540,"duration":3330,"startOfParagraph":false},{"content":"you don't have to put a star in front of it like we did with all the other dereferences, right?","startTime":4664870,"duration":4120,"startOfParagraph":false},{"content":"So, if you print out the entire string %s expects is an address,","startTime":4668990,"duration":5190,"startOfParagraph":false},{"content":"and in this case a pointer or in this case like an array of characters.","startTime":4674180,"duration":3430,"startOfParagraph":false},{"content":"Characters, char*s, and arrays are the same thing.","startTime":4677610,"duration":2720,"startOfParagraph":true},{"content":"Pointer is to characters, and character arrays are the same thing.","startTime":4680330,"duration":3360,"startOfParagraph":false},{"content":"And so, all we have to do is pass in pointer.","startTime":4683690,"duration":2030,"startOfParagraph":false},{"content":"We don't have to pass in like *pointer or anything like that.","startTime":4685720,"duration":2430,"startOfParagraph":false},{"content":"So, arrays and pointers are the same thing.","startTime":4693110,"duration":1820,"startOfParagraph":false},{"content":"When you're doing something like x [ y ] over here for an array,","startTime":4694930,"duration":4230,"startOfParagraph":false},{"content":"what it's doing under the hood is it's saying, okay, it's a character array,","startTime":4699160,"duration":2800,"startOfParagraph":false},{"content":"so it's a pointer.","startTime":4701960,"duration":1730,"startOfParagraph":false},{"content":"And so x are the same thing,","startTime":4703690,"duration":2820,"startOfParagraph":false},{"content":"and so what it does is it adds y to x, ","startTime":4706510,"duration":2140,"startOfParagraph":false},{"content":"which is the same thing as moving forward in memory that much.","startTime":4708650,"duration":3170,"startOfParagraph":false},{"content":"And now x + y gives us some sort of address,","startTime":4711820,"duration":3110,"startOfParagraph":false},{"content":"and we dereference the address or follow the arrow","startTime":4714930,"duration":2640,"startOfParagraph":false},{"content":"to where that location in memory is and we get the value out of that location in memory.","startTime":4717570,"duration":4070,"startOfParagraph":false},{"content":"So, so these two are exactly the same thing.","startTime":4721640,"duration":2080,"startOfParagraph":false},{"content":"It's just a syntactic sugar.","startTime":4723720,"duration":2120,"startOfParagraph":false},{"content":"They do the same thing. They're just different syntactics for each other.","startTime":4725840,"duration":2250,"startOfParagraph":false},{"content":"So, what can go wrong with pointers? Like, a lot. Okay. So, bad things.","startTime":4731500,"duration":6090,"startOfParagraph":true},{"content":"Some bad things you can do are not checking if your malloc call returns null, right?","startTime":4737590,"duration":4820,"startOfParagraph":false},{"content":"In this case, I'm asking the system to give me--what is that number?","startTime":4742410,"duration":4150,"startOfParagraph":false},{"content":"Like 2 billion times 4, because the size of an integer is 4 bytes.","startTime":4746560,"duration":4640,"startOfParagraph":false},{"content":"I'm asking it for like 8 billion bytes.","startTime":4751200,"duration":2610,"startOfParagraph":false},{"content":"Of course my computer is not going to be able to give me that much memory back.","startTime":4753810,"duration":3460,"startOfParagraph":false},{"content":"And we didn't check if this is null, so when we try to dereference it over there--","startTime":4757270,"duration":3690,"startOfParagraph":false},{"content":"follow the arrow to where it's going to--we don't have that memory.","startTime":4760960,"duration":3310,"startOfParagraph":false},{"content":"This is what we call dereferencing a null pointer.","startTime":4764270,"duration":2880,"startOfParagraph":false},{"content":"And this essentially causes you to segfault. ","startTime":4767150,"duration":2560,"startOfParagraph":false},{"content":"This is one of the ways you can segfault.","startTime":4769710,"duration":2080,"startOfParagraph":false},{"content":"Other bad things you can do--oh well. ","startTime":4774090,"duration":4000,"startOfParagraph":false},{"content":"That was dereferencing a null pointer. Okay.","startTime":4778090,"duration":2560,"startOfParagraph":false},{"content":"Other bad things--well, to fix that you just put a check in there","startTime":4780650,"duration":4510,"startOfParagraph":false},{"content":"that checks whether the pointer is null","startTime":4785160,"duration":1820,"startOfParagraph":false},{"content":"and exit out of the program if it happens that malloc returns a null pointer.","startTime":4786980,"duration":4020,"startOfParagraph":false},{"content":"That's the xkcd comic. People understand it now. Sort of.","startTime":4795110,"duration":4740,"startOfParagraph":false},{"content":"So, memory. And I went over this.","startTime":4806120,"duration":3230,"startOfParagraph":true},{"content":"We're calling malloc in a loop, but every time we call malloc","startTime":4809350,"duration":2650,"startOfParagraph":false},{"content":"we're losing track of where this pointer is pointing to,","startTime":4812000,"duration":2370,"startOfParagraph":false},{"content":"because we're clobbering it.","startTime":4814370,"duration":1380,"startOfParagraph":false},{"content":"So, the initial call to malloc gives me memory over here.","startTime":4815750,"duration":2660,"startOfParagraph":false},{"content":"My pointer pointers to this.","startTime":4818410,"duration":1580,"startOfParagraph":false},{"content":"Now, I don't free it, so now I call malloc again.","startTime":4819990,"duration":3030,"startOfParagraph":false},{"content":"Now it points over here. Now my memory is pointing over here.","startTime":4823020,"duration":3050,"startOfParagraph":false},{"content":"Pointing over here. Pointing over here.","startTime":4826070,"duration":1570,"startOfParagraph":false},{"content":"But I've lost track of the addresses of all the memory over here that I allocated.","startTime":4827640,"duration":4180,"startOfParagraph":false},{"content":"And so now I don't have any reference to them anymore.","startTime":4831820,"duration":3280,"startOfParagraph":false},{"content":"So, I can't free them outside of this loop.","startTime":4835100,"duration":2130,"startOfParagraph":false},{"content":"And so in order to fix something like this,","startTime":4837230,"duration":2160,"startOfParagraph":false},{"content":"if you forget to free memory and you get this memory leak,","startTime":4839390,"duration":2860,"startOfParagraph":false},{"content":"You have to free the memory inside of this loop once you're done with it.","startTime":4842250,"duration":3560,"startOfParagraph":false},{"content":"Well, this is what happens. I know lots of you hate this.","startTime":4845810,"duration":5590,"startOfParagraph":false},{"content":"But now--yay! You get like 44,000 kilobytes.","startTime":4851400,"duration":3870,"startOfParagraph":false},{"content":"So, you free it at the end of the loop,","startTime":4855270,"duration":1840,"startOfParagraph":false},{"content":"and that's going to just free the memory every time.","startTime":4857110,"duration":2660,"startOfParagraph":false},{"content":"Essentially, your program doesn't have a memory leak anymore.","startTime":4859770,"duration":3850,"startOfParagraph":false},{"content":"And now something else you can do is free some memory that you've asked for twice.","startTime":4863620,"duration":4530,"startOfParagraph":true},{"content":"In this case, you malloc something, you change its value.","startTime":4868150,"duration":2910,"startOfParagraph":false},{"content":"You free it once because you said you were done with it.","startTime":4871060,"duration":2080,"startOfParagraph":false},{"content":"But then we freed it again.","startTime":4873140,"duration":1800,"startOfParagraph":false},{"content":"This is something that's pretty bad.","startTime":4874940,"duration":1790,"startOfParagraph":false},{"content":"It's not going to initially segfault,","startTime":4876730,"duration":2090,"startOfParagraph":false},{"content":"but after a while what this does is double freeing this corrupts your heap structure,","startTime":4878820,"duration":4530,"startOfParagraph":false},{"content":"and you'll learn a little bit more about this if you choose to take a class like CS61.","startTime":4883350,"duration":3850,"startOfParagraph":false},{"content":"But essentially after a while your computer is going to get confused","startTime":4887200,"duration":2800,"startOfParagraph":false},{"content":"about what memory locations are where and where it's stored--","startTime":4890000,"duration":3010,"startOfParagraph":false},{"content":"where data is stored in memory.","startTime":4893010,"duration":1790,"startOfParagraph":false},{"content":"And so freeing a pointer twice is a bad thing that you don't want to do.","startTime":4894800,"duration":3280,"startOfParagraph":false},{"content":"Other things that can go wrong is not using sizeof.","startTime":4898080,"duration":3520,"startOfParagraph":true},{"content":"So, in this case you malloc 8 bytes,","startTime":4901600,"duration":2860,"startOfParagraph":false},{"content":"and that's the same thing as two integers, right?","startTime":4904460,"duration":2240,"startOfParagraph":false},{"content":"So, that's perfectly safe, but is it?","startTime":4906700,"duration":2880,"startOfParagraph":false},{"content":"Well, as Lucas talked about on different architectures,","startTime":4909580,"duration":2580,"startOfParagraph":false},{"content":"integers are of different lengths.","startTime":4912160,"duration":2060,"startOfParagraph":false},{"content":"So, on the appliance that you're using, integers are about 4 bytes,","startTime":4914220,"duration":3750,"startOfParagraph":false},{"content":"but on some other system they might be 8 bytes or they might be 16 bytes.","startTime":4917970,"duration":4400,"startOfParagraph":false},{"content":"So, if I just use this number over here,","startTime":4922370,"duration":3310,"startOfParagraph":false},{"content":"this program might work on the appliance, ","startTime":4925680,"duration":1630,"startOfParagraph":false},{"content":"but it's not going to allocate enough memory on some other system.","startTime":4927310,"duration":3050,"startOfParagraph":false},{"content":"In this case, this is what the sizeof operator is used for.","startTime":4930360,"duration":3660,"startOfParagraph":false},{"content":"When we call sizeof(int), what this does is","startTime":4934020,"duration":2860,"startOfParagraph":false},{"content":" it gives us the size of an integer on the system that the program is running.","startTime":4936880,"duration":5030,"startOfParagraph":false},{"content":"So, in this case, sizeof(int) will return 4 on something like the appliance,","startTime":4941910,"duration":3580,"startOfParagraph":false},{"content":"and now this will 4 * 2, which is 8,","startTime":4945490,"duration":4490,"startOfParagraph":false},{"content":"which is just the amount of space necessary for two integers.","startTime":4949980,"duration":2350,"startOfParagraph":false},{"content":"On a different system, if an int is like 16 bytes or 8 bytes,","startTime":4952330,"duration":4380,"startOfParagraph":false},{"content":"it's just going to return enough bytes to store that amount.","startTime":4956710,"duration":2670,"startOfParagraph":false},{"content":"And finally, structs.","startTime":4961830,"duration":3480,"startOfParagraph":true},{"content":"So, if you wanted to store a sudoku board in memory, how might we do this?","startTime":4965310,"duration":3030,"startOfParagraph":false},{"content":"You might think of like a variable for the first thing,","startTime":4968340,"duration":3230,"startOfParagraph":false},{"content":"a variable for the second thing, a variable for the third thing,","startTime":4971570,"duration":2250,"startOfParagraph":false},{"content":"a variable for the fourth thing--bad, right?","startTime":4973820,"duration":2600,"startOfParagraph":false},{"content":"So, one improvement you can make on top of this is to make a 9 x 9 array.","startTime":4976420,"duration":4330,"startOfParagraph":false},{"content":"That's fine, but what if you wanted to associate other things with the sudoku board","startTime":4980750,"duration":3730,"startOfParagraph":false},{"content":"like what the difficulty of the board is,","startTime":4984480,"duration":2010,"startOfParagraph":false},{"content":"or, for example, what your score is, or how much time it's taken you to solve this board?","startTime":4986490,"duration":5250,"startOfParagraph":false},{"content":"Well, what you can do is you can create a struct.","startTime":4991740,"duration":3230,"startOfParagraph":false},{"content":"What I'm basically saying is I'm defining this structure over here,","startTime":4994970,"duration":3940,"startOfParagraph":false},{"content":"and I'm defining a sudoku board which consists of a board that is 9 x 9.","startTime":4998910,"duration":4320,"startOfParagraph":false},{"content":"And what it has it has pointers to the name of the level.","startTime":5003230,"duration":3420,"startOfParagraph":true},{"content":"It also has x and y, which are the coordinates of where I am right now.","startTime":5006650,"duration":4080,"startOfParagraph":false},{"content":"It also has time spent [unintelligible], and it has the total number of moves I've inputted so far.","startTime":5010730,"duration":5250,"startOfParagraph":false},{"content":"And so in this case, I can group a whole bunch of data into just one structure","startTime":5015980,"duration":4030,"startOfParagraph":false},{"content":"instead of having it like flying around in like different variables ","startTime":5020010,"duration":2780,"startOfParagraph":false},{"content":"that I can't really keep track of.","startTime":5022790,"duration":1750,"startOfParagraph":false},{"content":"And this lets us have just nice syntax for sort of referencing different things inside of this struct.","startTime":5024540,"duration":5180,"startOfParagraph":false},{"content":"I can just do board.board, and I get the sudoku board back.","startTime":5029720,"duration":3710,"startOfParagraph":false},{"content":"Board.level, I get how tough it is.","startTime":5033430,"duration":2890,"startOfParagraph":false},{"content":"Board.x and board.y give me the coordinates of where I might be in the board.","startTime":5036320,"duration":4220,"startOfParagraph":false},{"content":"And so I'm accessing what we call fields in the struct.","startTime":5040540,"duration":4190,"startOfParagraph":false},{"content":"This defines sudokuBoard, which is a type that I have.","startTime":5044730,"duration":4110,"startOfParagraph":false},{"content":"And now we're here. I have a variable called \"board\" of type sudokuBoard.","startTime":5048840,"duration":5960,"startOfParagraph":false},{"content":"And so now I can access all the fields that make up this structure over here.","startTime":5054800,"duration":4020,"startOfParagraph":false},{"content":"Any questions about structs? Yes?","startTime":5060830,"duration":1620,"startOfParagraph":true},{"content":"[Student] For int x, y, you declared both on one line? >>[Joseph] Uh-huh.","startTime":5062450,"duration":3440,"startOfParagraph":false},{"content":"[Student] So, could you just do that with all of them?","startTime":5065890,"duration":1510,"startOfParagraph":false},{"content":"Like in x, y comma times that total?","startTime":5067400,"duration":3800,"startOfParagraph":false},{"content":"[Joseph] Yes, you could definitely do that, but the reason I put x and y on the same line--","startTime":5071200,"duration":3260,"startOfParagraph":false},{"content":"and the question is why can we just do this on the same line?","startTime":5074460,"duration":1870,"startOfParagraph":false},{"content":"Why don't we just put all of these on the same line is","startTime":5076330,"duration":2270,"startOfParagraph":false},{"content":"x and y are related to each other,","startTime":5078600,"duration":3490,"startOfParagraph":false},{"content":"and this is just stylistically more correct, in a sense,","startTime":5082090,"duration":2690,"startOfParagraph":false},{"content":"because it's grouping two things on the same line","startTime":5084780,"duration":1820,"startOfParagraph":false},{"content":"that like sort of relate to the same thing.","startTime":5086600,"duration":2740,"startOfParagraph":false},{"content":"And I just split these apart. It's just a style thing.","startTime":5089340,"duration":2100,"startOfParagraph":false},{"content":"It functionally makes no difference whatsoever.","startTime":5091440,"duration":2280,"startOfParagraph":false},{"content":"Any other questions on structs?","startTime":5098150,"duration":1120,"startOfParagraph":false},{"content":"You can define a Pokédex with a struct.","startTime":5103030,"duration":3590,"startOfParagraph":false},{"content":"A Pokémon has a number and it has a letter, an owner, a type.","startTime":5106620,"duration":5100,"startOfParagraph":false},{"content":"And then if you have an array of Pokémon , you can make up a Pokédex, right?","startTime":5111720,"duration":5270,"startOfParagraph":false},{"content":"Okay, cool. So, questions on structs. Those are related to structs.","startTime":5116990,"duration":3820,"startOfParagraph":false},{"content":"Finally, GDB. What does GDB let you do? It lets you debug your program.","startTime":5120810,"duration":4460,"startOfParagraph":true},{"content":"And if you haven't used GDB, I would recommended watching the short","startTime":5125270,"duration":2380,"startOfParagraph":false},{"content":"and just going over what GDB is, how you work with it, how you might use it, ","startTime":5127650,"duration":3600,"startOfParagraph":false},{"content":"and test it on a program.","startTime":5131250,"duration":1650,"startOfParagraph":false},{"content":"And so what GDB lets you do is it lets pause the [unintelligible] up your program","startTime":5132900,"duration":4500,"startOfParagraph":false},{"content":"and a practical line.","startTime":5137400,"duration":1520,"startOfParagraph":false},{"content":"For example, I want to pause execution at like line 3 of my program,","startTime":5138920,"duration":3680,"startOfParagraph":false},{"content":"and while I'm at line 3 I can print out all the values that are there.","startTime":5142600,"duration":3410,"startOfParagraph":false},{"content":"And so what we call like pausing in a line ","startTime":5146010,"duration":3700,"startOfParagraph":false},{"content":"is we call this putting a breakpoint at that line","startTime":5149710,"duration":2640,"startOfParagraph":false},{"content":"and then we can print out the variables at the state of the program at that time.","startTime":5152350,"duration":3570,"startOfParagraph":false},{"content":"We can then from there step through the program line-by-line.","startTime":5155920,"duration":3070,"startOfParagraph":true},{"content":"And then we can look at the state of the stack at the time.","startTime":5158990,"duration":4210,"startOfParagraph":false},{"content":"And so in order to use GDB, what we do is we call clang on the C file,","startTime":5163200,"duration":5400,"startOfParagraph":false},{"content":"but we have to pass it the -ggdb flag.","startTime":5168600,"duration":2690,"startOfParagraph":false},{"content":"And once we're done with that we just run gdb on the resulting output file.","startTime":5171290,"duration":4560,"startOfParagraph":false},{"content":"And so you get some like mass of text like this, ","startTime":5175850,"duration":2960,"startOfParagraph":false},{"content":"but really all you have to do is type in commands at the beginning.","startTime":5178810,"duration":3180,"startOfParagraph":false},{"content":"Break main puts a breakpoint at main.","startTime":5181990,"duration":2260,"startOfParagraph":false},{"content":"List 400 lists the lines of code around line 400.","startTime":5184250,"duration":4220,"startOfParagraph":false},{"content":"And so in this case you can just look around and say, oh,","startTime":5188470,"duration":2940,"startOfParagraph":false},{"content":"I want to set a breakpoint at line 397, which is this line,","startTime":5191410,"duration":2950,"startOfParagraph":false},{"content":"and then your program runs into that step and it's going to break.","startTime":5194360,"duration":2810,"startOfParagraph":false},{"content":"It's going to pause there, and you can print out, for example, value of low or high.","startTime":5197170,"duration":3950,"startOfParagraph":false},{"content":"And so there are a bunch of commands you need to know,","startTime":5201120,"duration":5290,"startOfParagraph":false},{"content":"and this slideshow will go up on the website,","startTime":5206410,"duration":2250,"startOfParagraph":false},{"content":"so if you just want to reference these or like put them on your cheat sheets, feel free.","startTime":5208660,"duration":5340,"startOfParagraph":false},{"content":"Cool. That was Quiz Review 0, and we'll stick around if you have any questions.","startTime":5214000,"duration":6650,"startOfParagraph":true},{"content":"All right. ","startTime":5220650,"duration":3200,"startOfParagraph":false},{"content":" [applause]","startTime":5223850,"duration":5180,"startOfParagraph":true},{"content":"[CS50.TV]","startTime":5229030,"duration":3970,"startOfParagraph":true}]}