[♪Buzzing♪] So to introduce you to what I've got up on the screen; it's one of the tools we developed over the summer; it's called CS50 Spaces, and it's our collaborative coding application that we're going to use in sections. It's pretty cool. Have any of you seen CS50 run yet? Let me pull that up in another tab. Run.cs50.net So if you go to run.cs50.net, you can see this is an in your browser way to actually code--write C code, and then run it, so it both compiles it, runs it, then returns the output to you, so this is kind of a cool way to experiment with some of the code that David goes over in lectures and we give to on you on problem sets without actually having to fire up the appliance. So what Spaces does that's different from that if I switch back to this tab, Spaces is CS50 run, like you see right here, but if we scroll over and look at the other side of the window, you see the right I'm the only person in this space, but what I'm going to do is ask you guys to join this Space with me and we'll all get to code in the same little window, share code with each other, and talk about the differences between our code. So it's kind of a cool thing-- so to join, if you would visit this link right here at the top that I've highlighted, you can just type in apps.cs50.net/spaces/vqcspSnO. All right, so to start off, I'm going to give you a little bit of a tour of this. We're gonna do some silly little coding with it at first, just so that everybody kinda gets a feel for what it's like, but what I've done-- or what I'm about to do right here is-- I'm going to take the code that I've written in here, which as you see right now is empty; it doesn't do anything. It's just boiler plate code, we'll call it. With a little to-do comment. I'm going to save it by hitting this Save button. And what that does is it highlighted it up here briefly--that revision save green box, and now, if you search for me in the Space, you click my name, you can see that I've got this revision right here, and if you click on that revision, it'll load that code into your Space, too. So has everybody loaded that code into their Space? All right, if you haven't yet, go ahead and do that, and then what I want you guys to do in this little to-do section is write a little bit using printf about yourself, so for example, I would write printf("My name is Nate"); and then I'd write "I graduated in 2009 from Stanford" and I would love to hear about what year you are in school, whether you're at the college or the business school or another Harvard school, what you're studying if it's known yet; if it's not known, that's totally cool. I didn't decide until I was late in my sophomore year. and then I would also love to know anything crazy you want to tell me or something about the class or just anything else. When you're done writing that with your little printf, if you'll save the revision or rather first make sure that you can run it and that it prints out exactly what you want it to print out, and then if you go ahead and save that revision using the Revision Save button, this first little guy over here, and we'll kinda go through and do a little introduction that way. So you should just have to hit the Play button, which is up here on the top left, so this Play button on the top left will send your code off to our server where it gets compiled and then run. So what's kinda crazy about this is this code actually isn't running on your computer; it's actually being sent across the Internet. How's it going, Charlotte? >>I--okay. I'm--I think I'm having difficulties saving it on the server such that it can--I can run the program. And then when I try to run it, it says that there's an error. Declared as a virus, which is not in there anymore. Oh, let's see; can you hit Save again? I see it on the screen. >>Right, okay, so that's what I have. And I don't know why it doesn't let me run it. Oh, okay, sorry. Can you explain why--what was wrong--what wasn't working with it? >>Yeah, because I didn't see that there was a last line where there's still a variable that I had not declared, which I did not intend for it being there. Yeah, perfect! >>Now if I save it, I think it should work. Now that you've saved it, it works; awesome. So yeah, as we go through-- so what was going on, I'm just going to repeat this for the camera. What was happening was that this variable s was left in after--I assume you deleted some other stuff where you declared as, and so this was throwing things off and wasn't letting you run it. So this is exactly what we want to do in section is have people try writing different pieces of code, compiling it, see what works, see what doesn't, and then kinda talk about these things, because these are tripping points that I still encounter, I still--I do lots of silly stuff all the time, and I don't know why it doesn't work, so let's pull up Charlotte's revision 3, and we'll run it. So when I run, now that I've got her code in here, I can zoom in, hit the Run button, uploads it, builds it, runs it, says it ran successfully, and if I scroll down, see Charlotte is a junior at Adams House, psychology, secondary in economics; awesome. Great to meet you! Awesome. Okay, moving on, let's go to Daniel, go to revision 3, compile, run; why don't you tell us a little about yourself, Daniel? >>Call me Dan. I'm going to business school across from you guys, and I was in the Marines before here, and this is all new to me, so yeah. Don't feel bad; I'm 30 and I don't know any of this stuff. Awesome; thanks, Dan! All right, we got another Daniel. You go by Daniel--will you introduce yourself, Daniel? >>Sure, I'm Daniel. I'm a sophomore at Mather right now. I'm concentrating on government, but I wanted to take CS50 because I've heard great reviews about the class and I've never tried CS before. Scroll down here and show this--cool. Moving on, Jared. How's it going? >>Good, how are you? As you can see, I'm a junior philosophy concentrator in Eliot, originally from southern California, and I'm interested in taking CS50 because I like the logic of it, and it seems really cool to do things on the computer, so. Awesome; where in southern California are you from? >>About an hour from [inaudible], a small city [inaudible]. Oh, yeah, my dad's from Redlands. Cool stuff. All right, Jimmy; how's it going, Jimmy? >>Good, how are you? Doing pretty well! Tell us about yourself. >>I'm a freshman, and I'm not entirely sure what I'm majoring in but I'm kinda leaning towards economics. Like Daniel before me, I've never done any CS, but it sounds kinda cool. I'm originally from Chicago. Awesome. So check out his printout here. I wanted to ask, did this print out as you expected? >>No, I forgot to print a new line. I actually forgot that when I was writing my thing for the first time, too. It's kind of a pain to remember to put those in manually. It'd be nice if it just know that's what you wanted. Okay, Kelly. Wow, we have 2 Kellys; 2 Daniels, 2 Kellys. So Kelly--do you go by Kelly Ren? Okay, awesome. Or Kelly, I mean. >>So yeah, so I'm Kelly. I'm a sophomore in Pfoho. I got an e-mail today that I still have 7 weeks to declare my concentration, so I'm trying not to think about it, and I like baking, running, and Craisins. I don't have experience with most of CS; I taught myself to build Web sites when I was really young, but I think I probably taught myself wrong a lot of times. Hopefully I'll learn a little bit more and--yeah. Awesome; well, thank you. Let's go to the other Kelly; I don't have anything--did you say something in here? >> [inaudible] There it is! Sweet! >>All right, so I'm Kelly. I'm a first-year graduate student. I'm majoring in Mind, Brain, and Education. I graduated in Franklin & Marshall College in 2008 with majors in psychology and sociology and a minor in French. I'm taking CS50 because I'm interested in getting a PhD in Neuroscience and I feel like this will help me understand the technologies being used. Awesome. Moving on, Mikaela. Is that correct pronunciation? Okay, awesome. >>I'm Mikhaila. I'm a sophomore in Kirkland House, and I'm concentrating in Mechanical Engineering. I really like cycling, built a bike this summer. I'm taking CS, actually, as a concentration requirement, but you know, I think it's still cool sort of engineering class. Awesome--you built a bike this summer? >>Yeah. Wow, like the frame and all that? >> Uh-hunh (affirmative). Wow, that's really cool. >>[inaudible off-camera chat] [laughter] Yeah, it's been so long. You never know, right? Well, cool; well thanks, Mikhaila. All right, Missy. >>My name's Missy; I'm a freshman, so that means I don't know anything! I want to concentrate in astrophysics. In CS50, I feel like will definitely help with that. An odd fact about me is I'm a nationally certified firefighter. Wow! How did that happen? >>I attended an academy and went through hell for a lot of weeks and got my certification. Wow, congrats! That is no small feat, for sure. Okay, so now we'll do me. I'm the next one in the row. I'm--run it. So my name is Nate. I was class of 2009 at Stanford. I was a Symbolic Systems major, which is similar to the Mind, Brain, Behavior concentration here, but it's kind of a mix of computer science, linguistics, psychology, and philosophy. Kind of--yeah, a lot of weird stuff, but it was great, and then I did my master's in Computer Science there, as well. So I'm new to Harvard this year. I really wanted to get into teaching. I spent the last 4 years at Stanford TA-ing Computer Science classes and so David let me come on, and I'm gonna be helping out with CS50, so I do this full time; I'm not in grad school. It's been fun. Hopefully it'll continue to be fun; I'm sure it will. All right, Sam. >>My name is Sam; I'm a sophomore at the college, in law specifically. My major will be history of science, probably, and VES secondary, and my favorite color is yellow. No need to pass judgment or anything, but it's a good color! And Stella. >>Hi, I'm Stella; I'm a freshman in Grays. I have no clue what I've concentrated in yet, so CS50 was just kind of random, which is interesting. Fun fact, Jimmy was the first Harvard friend of mine, because we're both from Chicago. Cool, and Grace--that's supposed to be the really nice one, right? >>I thought [inaudible]. Oh, yeah. [inaudible/off-camera chat] Okay; I'm still learning, still learning. So we've got something else that's kind of interesting here. Yeah, I'm just wondering, do you have an idea that's going on here? You know how to interpret this? >>I was supposed to be put the semicolon after every single line. That's kind of a pain. Sometimes those semicolons can be a real pain to track down. So it turns out that the special program we're using this year to compile our C code is called Clang, and the reason we have started to use Clang this year-- because we haven't used that program in the past; we've used another one called GCC that's really popular-- Clang actually gives you error messages that make sense. This one says "expected ';' after expression", and it even tells you the expression after which you needed a semicolon. So are you fixing it? >>Yeah, right now. Awesome; let me know and we'll pull it up, and we'll run it. But yeah, putting these semicolons in is kind of a pain. >>I fixed it, so I think it should work. Awesome--revision 3. That looks like it. All right, so now if we run it, there it goes. Cool stuff. All right. So thanks for doing that. It's great to meet you guys. What we're gonna do now is spend the next little bit kinda diving into the section problems that come from the problem set from this week and we'll do that for most of the sections for the rest of the term, and then we'll save the last 15, 20 minutes or so to talk about the problem set itself and then just any other questions you have in general about the class. That said, what we really wanna do is have everybody participating using the Spaces stuff, throwing in code, throwing in suggestions, and talking about when things like this break or don't work, just because these are things that I know I came across when I started coding, and were really kinda frustrating at times because if it's like 2 am before your problem set is due, so--just for disclosure, I didn't start programming until I was a sophomore in college. It was kind of this weird, unknown thing that I heard was kinda cool and I also heard it could be a real pain in the butt if you miss a semicolon and that's the only error keeping your program from working and keeping you from going to sleep, so definitely, if you have questions like that, if you have concerns like that, let me know, because I want to make this a good experience for you guys. So have you guys all downloaded the problem set yet? Kinda know where it is? Okay. So we'll go to cs50.net, because I don't have it on this account, pull up the problem sets, pull up problem set 2, and we'll look at the standard edition today. We'll pull up the specification here. Scroll down. So we're gonna start looking at this section of questions. Have you guys watched these shorts yet? Okay. So there are shorts on loops, which is done by Tommy, one of the head teaching fellows. There's one on scope, I believe done by another teaching fellow named David and then one on Caesar Cipher also done by David. They're pretty cool. Have you guys been watching their shorts so far? How've they been? Helpful? Okay, good to know. If they're not, you can say so. But before you do, have you guys looked at these questions at all? Let's talk about this a little bit. So we've introduced over the last couple of weeks in lecture a number of different loops. There's the for loop, the while loop, and then the do-while loop, and in lecture, we've been talking a lot about how those loops are all fairly similar in a lot of ways, and you can do much of the same things with a while loop that you can with a for loop. You just have to kind of use the right combinations of variables and such. and so really the real distinction between all of the loops is that sometimes they're more useful to use them at one time than at another time. So for example, the while loop differing from a do-while loop. Does everybody feel like they have a good handle on when a do-while loop is more useful or what the difference is? >>I think the difference is a do-while loop runs--I don't know quite what the terminology is, but the series of functions within the loop. They automatically--it does its runs at least before it checks the condition, or the while loop will only run it when it continuously runs--whether or not the condition is being satisfied. Yeah, does that make sense? Let's take a look at a little bit of code. So just to repeat the question, or the answer, rather; the answer was that a while loop will check to see if the condition that you specify is met before it runs the body of the loop for the first time, whereas the do-while loop will automatically run the body of the loop, is what we call it--it's weird terminology, but if I ever say anything that doesn't make sense, let me know-- the loop body is that stuff that you put in between the curly braces. And in a do-while loop, that body get run at least once, and then the condition gets checked before it runs each successive time. So actually, Sam, do you mind writing a simple while loop in Spaces? >>How do you sort of like make a new...? I just delete whatever you have in there. Yeah, just clear it out. >>Well, I believe I'm having this-- which one do you want, a while, or do you want...? Yeah, just do a while true, print something out. And let's see. Alexander, I realized we never introduced you. >>Oh, that's fine. Do you--can you code up a do-while loop for us? >>Yeah, I'll try. All right, we'll just pull these up on the screen, so that you kind of have a handle on what the differences are. So here's what Sam's done. He's got an integer called i that he's initialized to 17. Then he says while (int i = 17) printf("My name is Sam."); So what happens when we run this code? There's an error; what does the error say? Let's pull it up and talk about it. An error occurred, it says. Let's scroll down. So it gave us this error, and it said, "Hey, I expected an expression." That's kinda cryptic. Yeah, so exactly--and again, there will be another little bug that we will catch in just a second. But this is another one of those kind of weird terminology things, so this int i = 17 isn't an expression. What it looks like it's doing is it's declaring an integer called i and setting it to equal to 17 or, rather, putting 17 into that int called i. And that doesn't make sense in the context of this while loop. It does make sense, as we'll see, in the context of a for loop, because for loop declarations do start off with that for int i equals something and goes on. So did you-- fix something, all right. Here it goes; awesome. So now things look a little different. A couple of things have changed. Can you tell us what things have changed? >>I took out the int, so there's no declaration [inaudible] instead of an assignment. The one equal sign, I put 2 equal signs, which means an equals. Awesome. Yeah, so what Sam did was he took out the int that was right here earlier, so if I just look at this--this is what it was earlier, it said int, which was declaring it--so we took out the type. There's int, char, string, short, those are called types. So you took out the type and then previously, he had just the single equals sign and what he said was problematic about this was when you have 1 equals sign, that's an assignment operator. That's not testing whether or not 2 things are equal. That's actually setting something equal to something. We're putting the number 17 into the variable i. So to do that kind of comparison, you have to use the double equals, which is kind of a weird thing but is a fairly common error. So one other thing I'll point out, if you don't mind, is that in our function here in int main, you see that the body of our function is offset from the left side by 4 spaces or a tab. The reason we do this is just for readability. As you can see in Sam's while loop here, he didn't indent at all, and that doesn't affect the functionality of the code. It will run exactly the same. So for example, if we come up to the top here and press our Play button, it will build it, it'll run it, and what's going to happen? Why does it still say running? >>Because it'll keep repeating hi. Exactly, and why does it keep repeating hi? >>Because the integer never changes, so it's always 17. Yeah, exactly, so let's see, after--remind me of your name again? Jimmy. So Jimmy said that the reason this keeps repeating hi over and over again is because this condition is always true. We never change the value of i. We never change the contents of what's stored in that variable. So at this point we got this error that your program generated too much output, and if we scroll down and kind of look at what's going on here, you can see that it is hi, hi, hi, hi, hi, hi, hi all the way through. Awesome. So kind of back to the original point that having the indentation here in the body of the while loop didn't affect anything. The program still ran just fine. Typically for good style-- and this is something that we look for in your problem sets-- we ask you to indent this by 4 spaces, which you can do by just pressing the tab key or by manually putting in 4 spaces yourself. Cool, okay so let's go back and look at Alexander's do-while. >>Just kinda having a little trouble. Okay, well let's work through it. Is that cool? >>Yeah. Okay, so here we go. This is the code that Alexander wrote. Do you go by Alex--okay. So here's the code that Alex has written. So, we've got our declaration of main right here, and then we've got this integer variable called i being declared right here. We've got a do statement and then the body again with these braces, so this is the body of the do-while-- let me zoom in on this so it's more readable-- and in the body, we're trying to print "this is the body", i++, so increment the contents of i and do this until i gets to be 3, and then our program ends. So what happens when we run this program? >>I expected an identifier. Okay, so build, run, error occurs. What happens? We scroll down here, we get this somewhat cryptic looking thing, expected semicolon after top level declaration. That's kinda weird. Does anybody have an idea of what might be going on? Kinda weird, yeah. >>[inaudible] be declared outside of the parentheses? So does that not apply then when you use parentheses? So he declared this guy outside of which parentheses? Oh, these guys--the curly braces? >>Yeah. Yeah, exactly. So it turns out that you can't actually declare a variable right here like this. What you might have been trying to do was-- cut it out of there-- you can either put it within the curly braces right here or another alternative is to put it completely outside. And so these--depending on where you actually put the declaration of that integer variable, you can get 2 very different kinds of variables; when it's outside of main, it's what we call a global variable. It's outside the scope of any of the functions-- it's not within a function-- whereas if we put it inside of main, like right here, then it's called a local variable, because it's local to the scope of main. And we'll talk a little bit more about what the differences are between the 2 types and why you use different kinds of variables, but for now, you can just keep all the variables local, because we don't have any need for global variables. Okay, so that's one thing right there. Let's try running this again, see what happens. Run, get another error. Zoom in a bit, what do we get? We get this expected parenthesis after do/while. Any ideas what's going on? Dan? >>Is the while then parenthesis and then-- I don't know if you need to redeclare int, but the do while (i < 3). Exactly, yeah. So it turns out that Dan's exactly right. We don't need to put this type here again. We can instead kill the type and just do that. All right. I'll zoom back out, try it one more time. Look at that; it runs successful. If we scroll down, "this is the body" printed out 3 times. That's pretty sweet. Well done, well done, guys. Well done. How's this going so far? Decent pace? Okay, cool. So, as we kind of back to the original question, we--and now if we see the difference, you see that in this do-while loop, the condition comes last. So as the computer reads the program since C programs are executed kind of in this sequential order, where they literally start with the first line of main, and then execute line 6, line 7, line 8, line 9, line 10, line 11, all this stuff at the top right here is going to execute once before we ever test this condition to see whether or not i--or the contents of i, rather-- are less than the number 3. And only if once we get to line 12, the contents of i are less than 3 do we actually jump back up to line 7 and then repeat the process, and then we test the condition again and again and again. So we can contrast this with--zooming back out, scroll down to Sam's version, we're gonna look at this, slightly changed so that it's no longer an infinite loop-- where if we zoom in, we see that as this program executes, it's going to first declare int i, set the contents of i be to 0, and then it hits the condition of the while loop first, so in this case, this body, this printf("hi") and i++ won't even execute unless i is actually less than or equal to 2. So if i is 3, for example, if we set this right here equal to 4--or I'll do 3, because I said 3-- and then run it, it'll build, it'll run totally fine, but it won't actually print out anything, because it failed that test for the first case. So in class, we kind of talked about why these do-while loops are useful sometimes and why we would use it as opposed to a while loop. Are there--do you guys have any suggestions for why a do-while loop might be more useful? >>Well, I think do-while loops is if you want user input, because that way you can let them put something in once; if it's correct, we can move on to the rest of the program, but if it isn't correct, then you keep on prompting the user until you put the right input. Yeah, exactly--exactly, so a do-while loop is useful because when prompting for user input because you need to get the user's input before you can actually check it. So in pset 1, in pset 2, where you're prompting for user input a lot, that's a handy thing to use as opposed to just a simple while loop. If you use a while loop, you can do the same thing-- you can do the same thing with a while loop as you do with a do-while loop. You just have to kind of have this duplicate process where you get the input first and then start your while loop. Whereas a do-while loop, you just have to write it once and it is a lot cleaner. All right. Any questions? Make sense? >>The return 0--that's just kind of a stall or is it--? Yeah, so we can-- well, we'll cover this a little bit more later but it turns out that this return 0 that we put at the end of main all the time-- you can kinda just take it as something we do for the time being. What we've started to talk about with functions kind of at the end of last week and then again this week is that a function takes inputs which are these things within the parentheses; these are 2 inputs, our arguments or parameters, what they're also called to our main function, and then a function produces an output which we call a return value. And the return value in the case of main is this int, this integer, and the way you tell a function, "Hey, this is what your output is going to be," is with this word "return", that keyword, rather, and so main returning 0 or main outputting 0 is standard in C programs because returning 0 from main means that "Hey, everything executed with my program just fine." If we were to put something other than 0, like 1, for example, that's used to tell whoever ran this program that something went wrong. So as we get more involved with stuff over the semester, if you're ever needing to say, "Oops, you know, something went wrong with this program," rather than having my program crash, I want to notify the runner of my program that something went wrong, you can use return value , as we call it, that's not 0. So we'll cover that a little bit more once we get into functions. That sort of makes sense? Okay. Functions are kind of bizarre-- I think for people who have lots of math background, it comes a little more easily, especially if you have a little bit more abstract math background and you're used to talking about functions and all that kind of stuff. But we'll try and cover them and go nice and slow as they come up. Likewise, this whole int argc, char* argv[], for right now, you can also take that as boiler plate. We've started to cover those a little bit in lecture when we talk about arguments that get passed into programs on the command line, but in your problem sets and your programs, it's totally fine just to write void and keep it simple. And what this void means is that "Hey, main is not going to accept any inputs; this is just a program we're gonna just run. We're not gonna give it any extra information every time we start it up running." So for now, you can do this; you can also even omit the return 0 at the end of your programs and they'll run just fine, but I recommend just keeping that in. In a couple of weeks, we'll start to kind of pull back the cover a little bit and understand more about all those different little moving parts. All right, now let's talk about our second question here. So what does "undeclared identifier" usually indicate if outputted by Clang? I think we might have seen that already today. Can somebody give a recap of what that means? Let's see, Kelly--one of the Kellys. >>So-- Yeah, what does-- >>I think when you, like, don't declare the variables and you try to use it. Yeah, exactly, exactly. So if you ever see this output when you try and compile your program, just like Kelly said, it means that you used a variable that you didn't declare, so if I go back to our Space, and I do something like printf( "hello %s") and try and fill in that %s with a variable s that I haven't declared anywhere and I try to run it, you'll see what I get down at this bottom right here: "use of undeclared identifier 's'"; that just means you didn't declare your variable. Same thing happens if you have x and you try and store 7 in it and I didn't declare X. I get that same error: "use of undeclared identifier 'x'". All right, so the Caesar Cipher, not being very secure, I'll let you guys watch this short, kind of understand why, and hopefully after doing this week's problem set where you actually write a Caesar Cipher, you'll understand a little bit more about what it means to be a secure and insecure-- or not secure, I guess--insecure is slightly different. The other thing was we have these other questions here about functions and what a function is, and we kind of talked about that briefly when we talked about return values; for somebody who's been to lecture, can you recap who's been to lecture today? All right. Let's see; let's have--can somebody tell us what a function is? Kelly? >>Basically something you draw upon something that's written in the library that's more complicated over there, and then you just write a short form of it, so like you're referring back to that library, using all that long code and [inaudible]. Yeah, exactly, and then you wanna add something to that? >>Yeah, I think about it as similar to the concept of a formula in math where you just write a formula and then you can plug variables in and that's what a function is [inaudible]. Yeah, so we've got a couple of answers here that are both really good and right on. There's referring to code that's been written in the library somewhere that is more complicated and then you can draw upon that code and actually use it by just writing the short form of it-- or, rather, its name-- and then there is also this comparison to a formula in math or physics, all that kind of stuff where you have a formula and you can then plug in the variables to that formula to then get the result of the formula, so you think about it as like the quadratic equation. Imagine if rather than having to learn about what the quadratic formula is in math class, you just had a little calculator program that did it for you. Did anybody use calculator programs in math classes in high school? Yeah, physics classes? Where you can just say, "Hey, quadratic formula. These are the variables that I want to input. Now give me the answer." That's kind of what a function is. It's a way for us to kind of write like a small little sub program within our larger program and use it over and over again so that we don't have to copy and paste code, we can give it different inputs and get different outputs, it's super handy when you're trying to write different programs at different times. So for example, a function that we've been using over and over and over again throughout the course is printf. Printf is itself a function and it turns out that it's actually a pretty long and complicated, nasty looking, function to write. So as simple as it seems to us to print stuff out to the screen, because we all just did that at the beginning of section today, it turns out that it's actually, you know, a good couple of hundred lines of code. It's not something that I would ever want to write, for sure, but we get to use it every day; that's kind of cool, so we get to share it with each other, with different programs, so we'll be diving more into functions; for the time being, we're not-- the only function we're going to be writing is main. Main itself is also a function, and you can see it's got its output, its inputs like we talked about, and then its name, which is main. Main is kind of a--is a special function there. In your C program, there has to be a main function, and so for now, we've kind of omitted writing the prototype, or the declaration of, main. We've given that to you, and we've just had you fill in kind of the blanks. >>So for the second problem set, we don't need to write it in the second function? I don't believe so. I believe it's all in here. Yeah, so Caesar Cipher, you don't have to, it doesn't look like, and Vigenere, you don't either, so--and it turns out you don't ever actually have to write a function, right? You call always put all of your code within main, and many programmers do that. As we move on, we start introducing functions as a way to clean up your code, to make it more elegant, in case you have tasks--kind of like when we talked about loops, how sometimes there are things that you want to do again and again and again, and you have kind of the naive way-- or what we often call the "straw man" solution, is to just copy and paste over and over again-- you can do the same thing with functions where you just copy and paste the same code over and over again, but that bloats your code-- it ends up making your code really long-- and it also gets a little confusing, and then it means that you aren't able to like-- if you ever want to use that code again in another program, you're also stuck copying and pasting it again, so one of the things we'll talk about is kind of design and style of your programs, and so when it makes sense to kind of package something up and put it as a separate function, we encourage you to do that, and that is something that, again, there's no hard and fast rule. We rarely say that you must write a function called this, but it's more of one of those things you just develop over time as you write more and more programs. Any more questions? Okay, so let's now go back up to this guy right here, so I'm still in this section of questions. We've talked about all these different ones, what's a function, why to write functions, when you can just copy and paste? Hopefully we've convinced you that writing functions is a good thing. So now let's move on to this MySpace program, and we'll spend a little bit of time doing this. So, let's see, can I have somebody read this for me? Let's have Mikhaila--will you read this for us? >>You mean back when MySpace-- Back when MySpace was cool. >>"Back when MySpace was cool, it was all the rage to TyPe LiKe ThIs. Maybe it still is? I'm not really sure. In any case, using the CS50 Appliance, CS50 Run, or CS50 Spaces, write a program that prompts the user for a message and then outputs the message with its first letter capitalized, with all the other letters in alternating case, as per the sample output below, wherein boldfaced text represents some user's input. For simplicity, you may assume that the user will only input lowercase letters and spaces. Consider this problem an opportunity to practice; you won't be asked to submit this program." Awesome, thank you. So as Mikhaila said, we are going to write a program that MySpaces your code. So did any of you guys use MySpace? Any of you have MySpace accounts? Oh, yeah, yeah! It was pretty cool. [laughter] I know. Dating myself. So what we're trying to do is write a program that goes through and capitalizes the first letter and then every letter thereafter-- or every other letter thereafter, I'm sorry. So--and you can see in this example, this is how we run the program, and so this is just saying, "We're running a program called a.out. The input that we're going to giving it is going to be prompted. We're not gonna supply the input on the command line." If you've been following all the lectures and hearing about command line arguments, that's kind of where those--that funky syntax at main comes in with the int argc, all that stuff, so here we're not using that; we're not dealing with any of that. We're just gonna get this typed in by the user, and then this second line with the funky capitalization is what we want our program to output. So rather than using the Appliance or Run, we'll use Spaces, because we've got Spaces, and so if you guys would get your code to look something like this, so I'm going to save this revision just so you've got the boiler plate right here that looks like this, int main (void), nothing in it. Actually, what we'll do is we'll just delete everything all together. We want a blank slate, because we're not gonna start writing code; rather, we're gonna try writing some pseudocode which really just means we're going to write something that's not code that kind of describes what we want to do, kinda like David did on the first day of lecture, or I guess that was the second day of lecture when he did the whole socks demo. You guys remember that--seeing the socks? Joseph was up there kind of typing out in English? So we're gonna do the same thing. So when I start writing pseudocode, I like to start by looking at what the input is going to be. And I give myself a sample case, so like "thanks for the add!" And then I also write what I want the output to be, which in this case is something like this. And that kind of gives me a good, like, starting place to think about, "Okay, I want to take line 1 and I want to transform it into line 2," and that kind of helps guide my thinking. This is a good place to start. Another way to think about it, too, as we kinda move on and as you do this in your own programs, is to think about kind of like what are the edge cases to this? What are kind of the weird scenarios? What are the weird inputs that we might get? And what should those outputs look like? For example, one weird input would be this one, creating absolutely nothing, like what should we output in that case? Something we want to think about, too. So just as a question, what might we want to output? If we don't get any input at all, what should our program be outputting? >>Nothing. Nothing? Sounds great. So we want to make sure that we're not writing anything out if we don't get anything in. Cool. Okay. So I'm going to call on a couple of people as we go through and just figure out kind of like what the general algorithm is and we'll kind start working through this together. So, let's see. Missy. How do you think we'd start? What would be a good place to start here if we want to transform, say, line 1 into line 2? Can you kind of just explain to me, in English, what you're thinking? Don't even worry about code. >>First thing I would do would be to kind of direct the user to tell them what you want. Okay, so, say, "Hey, user. I want a string"--awesome. So let's write this down. Prompt the user for a string. That sounds like a good start. All right. Let's ask Kelly Ren this time. What would we then want to do next? What's kind of our next--? >>Collect their input and store it as a variable. Great. So collect the string, store it in a variable. So is there anything special we want to do here? Do we want to check their input before--? >>Yeah. What might we want to check for? >>Well, you said we don't have to check if it's a number, right? Because it'll assume that it's lowercase? Yeah. So it turns out that--yeah. So getString is guaranteed to return us a string. It's not going to return a number to us or anything like that. There might be digit characters within that string, though. And those, we can ignore as we did the capitalization. But an empty string is totally fine, so the case right here on line 4 and 5, right, where there's no input, and we've put nothing as our output. We don't have to worry about making sure that there's something in the string or that the string is at least 4 characters long; anything is fine. So this is contrasted, for example, with like the pennies programs that you wrote where you had to make sure you were getting non-negative numbers; you had to make sure that you were getting the correct number of days in a month. And how are you guys doing that? How would you do that test? What did that require you to use? >>Do-while loops. Do-while loops; other kinds of loops, too, yeah. So it turns out we don't have to use a loop in this case, when we're prompting for a string, which is kind of nice. All right, moving on. So, Stella. Can you tell us a little bit about what we should do next? So now we've got the string, we've got it stored in a variable; what do we need to do with the string? >>We need to-- identify the 2-- Yeah, just say it in English, that's awesome. >>Identify the [inaudible] capitals--or, uppercase. Yeah, so you'd identify which ones are uppercase and lowercase. We can't uppercase or lowercase them until we've identified which ones. So we'll put that in here. So, just thinking about this process, how do we do this identification? Do you know, Dan? Kind of what we--what tool we'd have to use out of our toolbox? >>I'd put it in somehow in like an array or each line is its own little box, and then uppercase it from there or not. Okay, so one tool we can use is an array. What do we have to look at in the string, though, to figure out--we had to look at the characters. >>So you've got to look at the length of the string? Yeah, so part of that is getting the length of the string. Just out of curiosity, what do we use to get the length of a string? >>strlen? Yeah, strlen; we call this "strlen" All of these string functions, so strlen is a function, and it's super handy, because we can use it to just say, Hey, strlen: Here's a string, tell me how long it is. In the lecture today, David talked a lot about how strlen is implemented, what's actually going on behind the scenes. But this is one of the very super handy ones. There are other ones that are also in the string library, such as strcmp, which is spelled "strcmp", which is used to compare strings to each other. Other ones you will use like strstr, which is good for finding sub strings and so on. For the most part, they all start with str and then go on to be something else. The reason they don't actually spell them like string and so on is because back in like the early days of C, when people were first writing and all that, they had limitations on the number of characters that you could use for function names and variable names, so everything is like super condensed. But anytime you see something that begins with str, that typically means this has something to do with a string. Okay, so you've got to get the length of the string, and then, what do we do with the characters? Do you know, Sam? >>I have no idea; I was trying to figure it out. I really don't know--I wish there were some way to-- I mean, I would think maybe we could do something with odds and evens, but I don't know of any function like that. Yeah. >>But that--it wouldn't necessarily work, because then you would get the spaces to be-- you would have to have the space to get tips on how-- Okay. >>I don't know--I'm kind of lost at this point. Yeah--no, no, no, these are good points. So what we need to do is, like Sam said, we need to track which letters are the odd letters, and which ones are the even letters. But we want to make sure we're not just looking at the odd and even indices in the string, but rather looking at the odd and even letters in the string, so we need to kinda have this distinction between letters--like what's actually A, B, C, D, or so on, and what things are not letters but still in our string. For example here, like this exclamation point is not a letter, but it's still part of our string; it's still a char. Likewise, all of these spaces-- it doesn't make sense to capitalize a space, right? So we want to make sure that we somehow skip those while still tracking odds and evens. So kind of in our programming toolbox so far, if you will, we have a number of different tools that we've talked about. So one, for example, are variables, like we've talked about here. Another tool we've talked about are functions. So we've talked about variables in our pseudocode up here as what we're going to use to store our string. We've talked about functions to get the length of a string, like strlen. We've been talking about something else, and we've talked about it for a lot earlier in section today: loops. What do you guys think about using a loop in this program? Jared, what do you think? Would we use a loop in this program? >>Probably not, no, because you don't have to go through it more than once. Okay, you don't have to go through what more than once? >>The entire string. Right, so you don't have to go through the string entire once. Sorry that made absolutely no sense! You don't have to go through the entire string more than once. Okay. What do you think? >>I think that it's better to use them so we can do like capitalize while the location of it is [inaudible] number. Great, and so capitalize what? Okay, so we're saying that we should use a loop because we want to capitalize a letter while it's not a number. So--sorry, did you want to add? >>I was going to say that the way you treat the string and it [inaudible] separate the [inaudible] on the characters? Yeah. >>And then we can assign the numbers to-- or based on the location of the letters in the string? Yeah. >>And identify whether it's odd or even and to capitalize the letter itself while the location of the letter is [inaudible]--sorry. Great. So just a recap. Can you remind me of your name again--I'm sorry. >>Charlotte. Charlotte, okay. So what Charlotte is saying, which is totally right--gosh, this likes to auto indent on me-- is that if we look at a string such as this one at the top-- "ThAnKs FoR tHe AdD"-- as a sequence of characters where the is at position 0, the h is at position 1. A is at 2, then we got 3, 4, 5; the space is at position 6. Whereas--what's the position of f? 7! O? 8. R? 9. And then we've got 10, and then I can't really write it without it kinda getting messy on the screen. But this is actually how we can talk about strings in memory. Is it a string is a sequence of characters, and when we refer to an individual character in a string, we often do so by referring to that character's position within the string, probably the same way that we talk about September being the ninth month of the year or Monday being the first day of the week. So this provides us a way to figure out which ones are odd versus even because you can see here, if we match it up with this T, you can see that all of the capitalized ones are at even positions, at least for the first word, and then things get thrown off once we hit that space, because we don't count this space as a letter, so it's as if we started and went 0, 1, 2, 3, 4, 5, space, 6, 7, 8, space, 9, and then 10, 11, 12, and so on. Cool, so we definitely will want to use some sort of loop to go over each of the characters in turn. So at this point, we'll come back to our old pseudocode here. As part of this identification of which ones are uppercase and which ones are lowercase, we'll want to do this in some kind of loop. Loop, colon. What kind of loop, out of the types of loops that we have, seems to be a good one to use in this case? >>I was thinking the for loop. And why are you thinking of for loop? >>Because it starts at zero, and then when we reach the length of the string, and then each iteration you can [inaudible], too? Yeah, exactly. So, out of the different choices of loops, we have for loops, while loops, and do-while loops. Where a for loop is particularly useful is when you want to know kind of where you're at in the loop, how many times the loop has been executed so far. Which is exactly what we want to know in this situation, like Kelly said, based off of Charlotte's earlier comment that we want to know the position of the character within the string, and the for loop will give us just that, because if we start kind of looking back at our string, if we start our for loop at 0, and we go up through the length of the entire string, then we can associate the letter T with the 0 in our for loop, letter H with the 1 in our for loop, and so on, going all the way through the end of the string. So let's start coding this just a little bit just so you guys get a feel for what this looks like. I'm going to save this, so we've got our pseudo code in here, and I'm going to delete everything. So I have a blank slate. I'm gonna start by writing int main (void) { I'm gonna put the return 0 in and then the }. So usually when I start to program, this is what I start with. I'll save it so that you guys have it. So one of the first things we talked about was getting input from the user and storing it in a string. How do we get input from a user? >>GetString. GetString, all right. So if I type GetString() and a semicolon. Let's try running this; will this run, first of all? And why won't this run? >>You have to declare the variable. I have to declare a variable. Okay. So how do I do that? Do you mind writing something, sharing it? And then we can pull it up. Let's see, Dan, right, okay. Or Daniel. Copy that in, string input = GetString(). Awesome. Will this run? Let's try it. Hit run, uploading source code, building, an error occurred. Okay. So whats going on here? Use of undeclared identifier 'string'. >>We have to include the libraries? Yeah, so Dan's saying we have to include the libraries. Is that what you were going to say, Stella? Awesome. That's awesome, so Stella, can you copy this code out of Daniel's Space? If you click on Daniel Roland Ki, copy it, and then will you put the include line in, and then save it out? Let me know when you're ready. So this include syntax is also kind of funky; there's like that hash tag, and there's the include, and then there are these angle brackets. Do you remember how this works, or should I put it up on this code right here? >>I'm good. Okay. >>I printed it--I wasn't sure which library, though. Oh, okay--yeah, so it's the CS50 library. >>Oh, I put stdio library. Oh, got it--yeah, no, that's-- so it turns out that string is actually something that is special to CS50. There actually isn't a true string type because right here, string is in the type position,- we're declaring a variable called input of type string-- and we're putting inside of input the value that gets returned to us: the output of this GetString function call. You can tell this is a function call because it's got the parentheses, just like printf does, just like strlen does. But string is actually not a real type in the C language. We've made it into a type, and we'll take about more what it actually is in C, because there is a way to work with strings; we've just done something to make it look a little nicer. So here we go. Here's Stella's code. It's got the #include . Other people will call this like a hash include sometimes or a sharp include. Now we can start it, run it, and we still get an error. Let's see what's happening. "Unused variable 'input'." Okay, so that's cool, that's cool. I mean, it's not really cool, because we're getting an error and it's not running, but what this is saying is that we haven't used our variable, which makes sense. We haven't tried to do anything with it. This is a way that Clang is trying to help us make sure that we're not just using variables and then inadvertently calling them something else later in our code, helping us avoid errors that way. All right, so we've got input from user, we stored it in a variable. The next step was to then start looking at all of the characters in the string and identifying which ones are to be uppercase and which ones are to be lowercase. And like Kelly and Charlotte brought up, we do this using a for loop. So does anybody remember the syntax of a for loop and would like to take a stab at just writing the first line of the for loop, just the for bit? Kelly, you wanna give it a try? >>Yeah, so you have to initialize first, so I put i = 0, and then I did i < strlen(). Is it in your Space--can I pull it up? >>Yeah, I screwed up a little bit, but yeah. Okay, let's pull it up. Awesome. >>But then I didn't know if you had that-- Yeah, okay. So here's what Kelly's got: let's look at it quickly. So she's included the cs50 library at the top. She's included the standard IO library at the top, and the standard IO has functions like printf, so if you ever want to use printf, you need that line. And then she included the string library, which we will need, because the string library has strlen function, which we'll get to in just a bit. So if we look at what Kelly's done here, can you walk us through what you did? >>Yeah, so--I mean, I got errors, so-- Yeah, no, it's-- >>So I wanted to call the string that I got just "string". I wanted to print out the string and then I-- I wanted to like set the string as like an array of each character being different [inaudible], right? And then I wanted to start at position 0 and then every 2 spaces, I was going to capitalize, so I just described it as 23, because I was looking at the ASCII table. So that's what I was thinking--I know it's not right. Got it, so this is a great start. I think a lot of the framework is in place here. Let's go through kind of line by line and see what's going on. So the way I like to do this is just start by compiling the code and letting the compiler tell me like what's wrong with my code, because the compiler is really good at telling you what is wrong with your code. So if we play it, building, and it doesn't build. It says there was an error, so if we scroll down and look at the first error, it said-- we scroll back up to the top, we can see there are 5 errors generated down here at the bottom. Oops, that's really cut off. I guess that's just not going to get any better. Here we go. So there are 5 total errors generated. Here I can only see the first 2, so I can move this up a little bit and then kind of scroll up. This is the command that was run to compile the code, and then the first error was in file.c right here: "expected identify or '('", and it was on this line where we said string = GetString(). You can see right here that it was on line 8 of file.c, and then I believe this is character 12--that's the error. So what's going wrong on line 8--Kelly? >>You just need to call it, like, a letter or something, like string s or something? Yeah, so it turns out that these special type names, so string, int, char, short, they're all special keywords, and you can't actually use them as names of your variables, because the compiler looks at that and says, "Oh, I know that's a type," so you can't actually have a string variable called string, like I'm trying to do here; this won't work. But instead, we can call it something like s or st or str. We also couldn't call it int. We can't have a string called int. We can't have a string called char; none of those will work. So we'll just call it s for the time being. Awesome. So what next is going on in this line? String--this line looks pretty good? Let's give it another try; let's run it. Okay, so we still get errors. This time, we're down to 4 errors. So we got one of them. Let's move up. "Unexpected type name 'string': expected expression," yes? >>Yeah, that's because I tried to name the string. Yeah, so it looks like we now also have to go fix all of these references to string. You can see there's a reference to string here, one here, one here, so we can move all these to s. Great. Okay, run it again. Building--another error occurred. Okay, now we're down to one error, so int i = s and then the square brackets. So can you tell us a little bit about what you were doing here, Kelly? >>Yes. It was on line 10. >>I don't know if we needed it all, but I think I was trying to-- I'll save it so you can follow along. >>I think I was trying to tell it to start at position 0 at the beginning of my for loop, so I think maybe that saying was unnecessary. So just to recap, you were saying that this--you were trying to start this at position 0 and get this--yeah, so I think earlier, too, when you were walking through the code, you were talking about how you wanted to treat the string as an array so we can access each character individually. I think Dan mentioned arrays, too, trying to convert strings into arrays. It turns out that kind of under the hood, a string is actually stored as an array already, and you can access it like an array; there's no conversion that you have to do. A string isn't--so-- a string isn't really an array, but you can access it as such, because all of the characters are stored one right next to each other in memory, so if you want to get the first character of the string, it's the same as getting the first integer in an integer array. So we often--we'll refer to the 2 like oh, as being a string, as being an array, even though it really--they're very different things, but you can treat them very much the same. So it turns out that this line actually-- we don't need to do any conversion, and in fact, this actually isn't a valid expression, which is what our compiler is telling us. It's saying, "Hey, it expected an expression, and what I've got here is something that's not an expression." To declare an array, all you need to do is something like this with a semicolon, and then you tell the array how long you want it to be, such as 5, so this line 10 will declare an array called i with 5 integers in it. This is an aside; this isn't necessary for the code, but this is--this is kind of what the declaration looks like. >>How would you not use char i[strlen(s)]? Char i[strlen(s)]? >>char i, and then inside the brackets, strlen(s). Yeah, so we could-- we could do this, and this would declare--what would this do? >>I think it would declare an array of, like--of-- oh, you don't need the i--oh, yeah, you definitely don't need the i. You declare an array of characters of length s. And so what Sam's saying is that this line 10--let me put some space in between it and the rest so we can isolate it-- this line 10 would declare an array of chars, strlen(s) worth of them, but we actually do need the i, and the reason we need the i is because we need to give the array a name; otherwise, we wouldn't have a way to refer to the array later in the code. So this right here is the array name and inside is the length. This will give us an array, but this is--again, this is a declaration. It's not an assignment; it's not an initialization. If we declare this array like this, there's actually nothing in the contents of the array. It's just junk--whatever is in there. And like we said, we actually don't even need to do this conversion, because we can already treat string s as an array. I know we're running a bit long on time, but I want to just really quickly get to this for loop and show you how to access the individual characters in the array, because that's key for our assignment. And then we'll let you go from there. So here, we've got a for loop that looks almost good. What's not quite almost there? What's--what's not quite right about this for loop? >>No curly braces? No curly braces, for one. Wanna make sure that we have the body of the loop with curly braces. Oops, not the square brace! And what is the next thing that we need to do with this loop? If I try and run it, whats this error that I get? I get all these errors--I have to initialize i. Great. So this will be a loop--this isn't quite there, but it's getting really close. This code we can now run, build, an error still occurs. Why is it still complaining, saying the expression is unused, and that's because this line doesn't do anything. We didn't store it anywhere; we didn't save the result. Where should we be saving this result? >>s or i? Exactly, and this funky syntax, this s with the square brackets means access the i-th character of s. That's going to be something that's really important for the assignment. Unfortunately, we're going to have to stop here, so that Sebastian section can come in, but I'm happy to stay after outside and will take any questions. So thank you guys for coming. This should now upload, build, run. We can enter a string like "hello" and it prints "QOX". [laughter] So not quite there, but moving, getting towards those loops! Thanks for coming, and I'll be outside.