>> Hello, everybody. Welcome to the Walkthrough One, my name is Tommy MacWilliam, and today we're going to be over our PSET 1, which is on C. So the music I hypothetically would have played before the walkthrough, so first I just want to talk a little bit about grading, so PSETs you're going to be graded along four different axis. The first is scope, and this is basically how much of the PSET did you complete? So make us a program that has three features. Did you do all three of them and did all three of them do what they're supposed to do? Next is correctness, so did you program have any bugs? Can we crash it somehow? Does it not behave the way it's supposed to? Next is design, and this is kind of the broadest. Is it well designed? Does it work efficiently? Did you make good code design decisions? This is where you're going to get the most qualitative learning experience out of the course. And finally is style, and this is how does it look? Is it commented well? Did you indent well, did you name your variables a good name and things like that. So any questions on the axis and how they'll be graded? So you'll be graded on a scale of one to five, so where five is best, one is not so great and three is right in the middle, so that would be good. So on each of your PSETs you'll get back with each of those axis and associated score and then we'll at the end of the term add up all those scores and see where you end up relative to yourself in week zero. So any questions on grading? I know you haven't gotten any grades back yet, but before you do any questions? So as I'm talking feel free to interrupt me with questions. Don't wait until I pause, it's much more fun that way. So before we get started, we're going to need a few things. The first is the CS50 appliance which we've seen in lecture. It's basically the environment in which you're going to be working with your code. It's kind of a computer within a computer. And it looks like this. So once you download it from manual.cs50.net/appliance, you're going to open up virtual box and you'll see a little thing that says CS50 appliance 2.3 and then you can click show and then it's going to fire up a little Lenox desktop. So this is just like an operating system just like Windows or Mac, and this is just so everyone is working with the same operating system and has the same tools as they're writing their code. So to get out of the appliance you'll notice once you click into it you have control of the mouse inside of the appliance and to get out of it you want to click the host key, which on Mac is the left command, and the first time you write it it'll get a little popup that says this is your host key, but that's what it's referring to, just this key you press to get your mouse pointer back. Sure. >> I started [inaudible] the virtual box in it and asked for a name and I got confused because the instructions said [inaudible] Harvard and I thought am I going into [inaudible] Harvard and give it my name or - I wasn't sure if I had to do that early or -- >> So when you're installing it asked for a name? so that's interesting, if you're just installing the actual appliance itself the name is probably just left as CS50 appliance 2.3 or something like that. It shouldn't matter what you name the actual appliance but the user inside of that is going to be J Harvard. So just like you log in onto your laptop with some user name, that's the user that's in the appliance created for you is called J. Harvard. So if you have any troubleshooting issues, we have two pages on our CS50 manual. One is the appliance in general, like how to use the appliance, what its IP is, how to do things like text editing, and the second page is just for installation troubleshooting issues for Mac and Windows. And if you have an issue that's not on either one of these pages, just feel free to post to help.cs50.net and then someone from the staff will respond and make sure that you're good to go before you start the PSET. So inside of the appliance we're going to be using a program called gedit or Gedit, whichever it is, and so this is just a text editor. It's where you're going to be writing all of your source code and save the dotc files and then you can run these programs that you write inside of the terminal. So it's both a standalone program in the appliance or the way we've set up gedit just at the bottom there there's a little mini terminal where you can do things like compile your code and run your code and do all of your testing. So you don't even need to leave you're text editor to actually run your program. and finally this week we're going to be using something called make, and this just basically makes the compilation process, taking your C-code and creating machine code that the -- your computer can run and it just makes this process really, really simple. So let's actually take a look at what this process would be. So the first thing you're asked to do in your PSET is write a little program called Hello.seek, so it's a three-step process to do this. One we're going to actually write the dotc file and have the source code that says hello world or whatever we want it to say and then we need to take that source code and translate it into object code or machine code, the zeros and ones that your computer can understand. And to do that we're going to run make hello, and this is going to be inside of our little terminal, inside of gedit and finally to run it we're going to say dot/hello. That just says that there's a program called hello inside of the folder that I'm looking at right now and I want you to run it. So let's take a look at how we will do that. So here we go, inside of the appliance I'm going to go to menu, programming, and then I have gedit right here. I also have a shortcut right there, so once I open it we need to write our first c program. So what's the first thing we need to do in our c program? >> [Inaudible] >> So a lot of different things because there are a lot of different things we can do first. The correct answer is write a comment that says what it is, what your name is, what the name of the program is, what it does. So I'm just going to say hello.c write my name, and now we need to do two things. We need to include some libraries that allow us to use functions like print out and we also need to actually write the program. So which of those is first? Do we need o pull in the libraries first or can we just get right into it and include the libraries later? >> [Inaudible] >> Right. So we first need to include the libraries, so it's going to be pound include and then inside of these angle brackets we're going to say standard io.h. So this is basically some file that some really nice person wrote for us and gave it to us for free and it contains a lot of really handy functions relating to input and output. So getting users input, so having them type something and then outputting it, so displaying something in the terminal. So now that I've included the library that I need, what do I do right next? So now we need to write our main functions, so the net point for our program, so when the machine runs it's going to look for something called main that's going to say okay, I'm going to run this thing called main. So we're going to say ent main, doesn't take any arguments which we'll learn about later, and now -- whoa, here's where I can actually write what I want to display to the user. So let's say I just want to output some message onto the terminal, how can I do that? >> Print it. >> Printout, exactly. So we're going to say print and then inside of the parenthesis we're going to tell it what to print. So we can just print off hello and now what is this forward slash end thing? A new line, exactly, so if I didn't have this new line I would basically say hello and then it would awkwardly say J. Harvard appliance after that instead of having that be on its own line like it usually is. So we include that new line so our program doesn't look awkward when it's all done, and now what else do I need on this line, one more thing, a semicolon, exactly. So we need to make sure that every statement in our C program, like a function call or a variable assignment or anything like that ends in a semicolon and if not then make is going to yell at you and you'll feel badly. So now once our program is written we're going to go to file. We can save it. I'm just going to call it hello.c, so I'll just save it in my home folder. So now it's saved and you'll notice that get it recognized, it's a c file and we now have the syntax highlighting. So different words are colored differently depending on what they mean. So now I can come down here at the bottom into my terminal. So it says J. Harvard at appliance, so this is just the user you're logged into, which I mentioned is J. Harvard, the at and then the machine name. So this is the CS50 appliance, so it's J. Harvard at appliance. So now I want to type make hello. I click enter and there we go. So it ran this GCC thing for us, which basically compiles our code into a program called hello, so now if I want to list out everything that I have inside of this folder I can just type LS and hit enter and now you can see that I have a desktop folder, I have a folder for the walkthrough, here's my hello.c and now here's this extra thing hello. And so this is the program that we just wrote. So I can say do slash hello hit enter and there we go. Question? >> How [inaudible] >> You're getting -- so what else does it say? >> It says name [inaudible] >> So if you're getting error one I'd have to look at so I know what error one is. So just make sure that your file looks exactly like this. You might be forgetting a semicolon somewhere or misspelled something but I can take a look at it right afterwards and help you out with that. Yeah. So generally the error message will include some number in it, like it will have a colon and then some number and that's usually the line number in your file, where you have your error. So other questions? Yep? >> It says [inaudible] file directory [inaudible] directory? >> So if it says no such file in directory how do you make sure you know where you are? So we also have a built-in file manager, so if you go over here to menu and then system and oh, so soon our file manager this is the equivalent of something like Windows explorer or finder on a Mac, and it's going to open up into your home directory. So over here I click on J. Harvard, and this is my home directory so you notice I have my hello and my hello.c inside of this directory. Now if I wanted to get into the walkthrough one directory from the terminal, I'm going to type in CD for a change directory and then the name of the directory I want to change to. So I can start typing walkthrough. If you hit tab it's actually going to auto complete for you so I just type wa tab and it auto completed walkthrough, and now I hit enter and you notice that now it says J. Harvard at appliance and now we're inside of the walkthrough directory. So that little tilde at the beginning that just signified the home directory, so J. Harvard's home and so now if I do an LS I can see a different set of files. So no longer -- well, I do actually have another file called hello there but it's a different file than the one that I have inside of my home directory. So if we want to get back there we can do a couple of things. We can do CD tilde, so we can go to my home directory so like my documents or my documents folder, or if I want to go up a directory I can say CD dot dot. That's going to say go to the directory that's immediately above mine. So we just went into one folder now we're going to go up out of that folder and we're back here with my hello. So you see more on that in the super section if you haven't already, but any questions on how we're going to compile our code and start running it. >> What does LS mean? >> So what is LS mean? So LS is to list all the files inside of the current directory, so now I'm inside of home, I say LS and I'm going to list them all. If I want a list of all the files inside of a different folder I and just CD to that folder and then type LS again and it's going to be a different set of files. >> Can you scroll up to the top [inaudible] in looking around definitely [inaudible] >> So this is called a comet, so when the compiler is going to actually translate your code to the object code, so the machine code that's going to be run the first thing it's' going to do is that. It's going to delete everything in a comment, but as programmers, it's really good to know, you know, what this file does, who wrote it in addition to what every line or not every line but what the main parts of our program are. So we'll see more about that shortly. But whenever you write a program it's really good to have your name, the date, what the program does at the very top of it. Yep? >> Do we have to use a [inaudible] series before [inaudible]? >> So do we have to use an asterix before every line of our comment? So this is just kind of a convention, so a multiline comment has to be enclosed between these two things. So anything inside of the slash star and the star slash, this is going to be immediately deleted as soon as we run our code. But there are kind of notes to ourselves as programmers and notes to anyone else who might read our code about what the program does. So I just think it looks pretty if you have stars in every line, but that's not strictly required no. yep? >> [Inaudible] one line? >> So how do you do a single line comment? So that's just going to be slash slash so I can say slash slash what comment. So this is a multiline comment, and this is a single-line comment. Other questions? All right. So speaking of commenting, code style means a lot to me. It's near and dear to my heart. And so really as far as you're concerned in your PSETs they're like free points. If you pay attention to your style, you have everything will intended and commented it's literally free points and it really does mean a lot to me personally. So to help you with your style, we put together the CS50 style guide. So if you just go to manual.cs50.net/style, you have this whole list of things that we would like you to do and things that we would not like you to do. So before the first PSET, this is definitely worth a read-through. But the most important thing with regard to code style is consistency so if you're consistently naming variables with underscores instead of capital letters you want to d that everywhere in your program. we don't want to see like my underscore variable and then my capital V variable in the same program, you want to be consistent. Make sure indentation is also consistent; don't use four spaces somewhere and then eight tabs somewhere else, and the same thing with your brakes placement. If you want to put them on a new line, that's cool, if you want to put them on the same line that's cool, just be condiment throughout all your programs. Yep? >> [Inaudible] should we use for [inaudible] >> So for indenting should we use four spaces or a tab? I think it's going to default to four spaces, which I think is what the style guide also recommends, but you can use something else as long as you're consistent then that's all we're looking for. Other questions? All right. So oh so we have a couple examples of code style here, so I'm going to open up two files so let me scroll down a little bit. So here is our first example, so let me just knock down the font a little bit. Okay. so this is what we're talking about when we mean good style. So you notice here at the top we have a program what's called who wrote it, and then the purpose of this program is to demonstrate good style and so that's it for the top of the harder. Next we have our includes, so now you'll notice that I have the single-line comments that describe what blocks of code do, so iterations equals get it, that doesn't really mean anything without the comment, but now if I say ask the user how many iterations that I want, now I say okay, so that's what that line of code that means. So next I have an if else, you notice the braces are on their own line, the indentation is consistent and we have a comment above this block explaining what it's there for. And finally the same thing with our loop, and then we end. So this is what I would consider good code style, we're consistent we have commencing, we named our variables good things like iterations, and now let's take a look at another program so here is style 2, let me just zoom out so you've got the full effect. >> Do we make the window bigger? >> So the appliance has a default size of 800 X 600 and the projector is going to get kind of weird if we change that. So first we have this really long comment, so this is not uncommon for people to write us like an essay at the beginning of our code that says what it does. So feel free to read that. One other little thing is a little personal tick. Some people have three comments, three slashes instead of two slashes at the beginning. It's also good to have a space after those two slashes, so both of that is also in the style guide as well. so scroll down. So immediately what jumps out as you as bad style here? anybody? So first the curly braces, so they're kind of all over the place. So this one looks good but the other is on the same line. This curly brace is indented, this one I don't know what it's doing. So it makes a big difference, right? These two programs are functionally equivalent; they do the exact same thing. But their style is very, very different and it's pretty clear to see which is better than another. So you also notice that style we call this variable that we're getting from the user P and really we don't' know why we called it P, we just decided that letter was next to the brace. And there's no comments throughout this, so we don't really know what's going on unless you really think about it and then even then we don't really understand what was going through the programmers mind when they wrote it. So rather than this big, 500 word essay at the beginning it's good to just kind of pepper your code with comments, saying this is what this block of code does. Finally speaking of blocks of code, its' really good to hit return every once in a while to separate blocks of code. So this if and else, they're kind of related because the else replies to the if, but the score loop kind of really doesn't have much to do with that if else [inaudible] so I can have a carriage return here to kind of separate the loop out of the way. So other questions on code style? Excellent, so we'll all write beautifully indented code and make me very happy. Okay. So now let's actually start talking about the three programs that you have to write. So the first is called pennies, and pennies is inspired by the lecture example we saw, which is in a better idea to take 10 million dollars right away or get one penny in the first day of the month, double that on the second day of the month, again, and the answer of course, is the pennies. So what your program essentially needs to do is take some input first I want the number of days in the month, so that's just going to be what type of variable? And integer, exactly, we can't have fractional days in the month and there doesn't need to be two of the 34 days of the month, so that's just going to be an int. we need to spit out the number of pennies that I have at the end of the month, so as a second input we're going to say how many pennies do you start off with? So some example output of this program, so we are going to run pennies in the appliance because we've already written it and compiled it, and it's going to prompt for the number of days in the month. So notice on the example in the P side it actually prompts twice. And why does it prompt you again after the first one? >> [Inaudible] >> Exactly so 32, there's no month with 32 days yet. So after that it's going to ask you how many pennies do you have on the first day, and similarly if you said something stupid here you'd want to reprompt the user. And then finally it just spits out how many pennies you'd have if you doubled it every day for that month. So we need to get some input from the user, so we need to have this days and month and then allow the user to type that in. so the way to do that is with a combination of Print F which is going to display some message and Get Int which is this function that's defined in the CS50 library, that CS50 duck H and that's just going to allow you to read from the command line some value that happens to be an integer, so you can use it like intend equals Get Int, pretty simple. So just so you can see an example of how you'd use that let's open up [ background noise ] Yep, question? >> Can you slow down a bit? >> Oh, sure. I can slow down. So we are now getting input from the user, so we're using this function called Get Int. so here at the beginning we're including two things. The first is the CS50 library, so this is where Get Int is defined for us. The next is the standard IO library and this is where Print F is defined for us. >> Does the order matter? >> So does the order matter? In this case no because these files, they're not using any -- they're kind of mutually independent, so you can include them in either order. >> [Inaudible] >> What's that? So and sometimes in more complicated programs, like the order would matter, but it in this case it doesn't. So now the first thing we're going to do is Print F enter a number. So this is the equivalent of the days and month. Notice here that I did not include that forward slash end. Because I want by the spec what I want the input to look like is this, so that 32 shouldn't be on a new line, it should be on the same line as that message I printed. So I don't want to include that forward slash n. next we're going to get the integer from the user, so just like we saw in 10 equals [inaudible] and that's going to take care of a lot of user to type something and then reading what the user types and storing it into a variable. So now I have the value that the user typed in this variable called N. question? >> What if on the [inaudible]? >> Oh, so this -- this actually doesn't need to be here, that was just habit. So eventually we'll use those but for now like we sound like we can just say it main void. So those are basically arguments you can supply to the program so that you can tell the program some values at the beginning that you can use later, but we're not going to be using those in that PSET so we can just say it main void. So other questions? Yep? >> [Inaudible] is that D? >> Oh, yeah, I'll get to that right now. Yep? >> In the [inaudible] into RFC comma char star, [inaudible]? Is it under main? >> Yeah. So -- so I had that before this endear sea [inaudible] star [inaudible] thing, so that's we'll be using that in later pieces, but right now you can just say it main void. So that was by that. So now that we have the value, we want to display it to the user. So again were going to be using Print F, however, we can't just say your number was N because this is just going to print the letter N. instead we want to have some place holder that once Print F runs it's going to say okay, I have a place holder and I'm going to substitute this place holder with some value. so whoever made this decision, decides to percent D was a really good place holder for an integer. So this says when Print F runs my number was percent B is going to say okay I have some place holder I'm going to end a value to put in there. So to supply a value we're going to have a comma after our statement in quotes, and then we're going to have the variable that we want to substitute in there. So the D is for an integer, you can use F for a float, and so what this is going to do is say your number was and it's going to output the number at that you typed in. yeah? >> [Inaudible] percent and the [inaudible]? >> So should it be percent N? So no, so the D just means integer, so D for integer, if that helps you remember. So this just says that after this statement, there's going to be a comma, and there's going to be an integer. So if I wanted to say five here, and every time it would five here and every time it would say your number was five, then I could do that, but because I want it to be what the user typed in, I stored it in a variable, so now it's in memory floating around somewhere and I can say your number was and then whatever the user typed in. Yep? >> What's the difference between like you saying that [inaudible] versus what we can't have? >> So what's the difference between int and n equals Get Int and the standup thing? So Get Int actually just uses standup, this is just kind of a nicer way of looking at it. It also does a little bit of error checking for you, like if I type in a string then Get Int is going to say you didn't give me an integer, try again. So the Get Int is literally just a wrapper for that scan off thing. Yep? >> Do you capital the g in get and the in int? is it sensitive? >> So, yes, so I capitalize the g and the I because it is case sensitive. So these are functions, just like Print F is a function that someone else wrote for us and gave to us, Get Int s a function that someone else wrote and gave to us, and they happened to write it with a capital G and a capital I and so we have to use that capital g and the capital I else we'll get an error that says it doesn't exist. Yep? >> Do we need that Print F number was section, like in the example in the problem set like in the CS50 library there's like a sample program I guess that we can run and it doesn't spit out like your number -- like in that program you just type in enter a number you type in 5 for example and then it just puts 5 on the screen. Do you know what I mean? Like the second Print F that you put up there is not in the problem, is not in the program that you made in the problem set. >> So right. So the only -- we don't have to -- we don't have to display whatever number the user typed in, right? So if this Print F weren't here then it wouldn't output anything. It would just read an input and then it would just be done and you could do something else. >> Right. Like an optional [inaudible] include that or -- >> Yeah so in the example for the PSET you don't want to. You want to actually do something with the number and then output a different number. But in this case this is different, we're just outputting the number right away. So you probably wouldn't want to do this in the actual PSET but when you do want to output some number this is how you would do it. >> Right. >> Yep. >> Does the Get Int have to be directly after the Print F sequence or a little bit after we should have known what we were talking about. >> So does this have to go immediately after Print F statement? No, it doesn't if I didn't have this Print F here, what would happen when I run the program was it would just sit there and it would wait for input because we're saying get an integer. But as the user I'm kind of confused as to why it's waiting. I'm like well, maybe it's loading or something so we just want to display a message that says you need to enter a number and then the program is going to sit there and wait for it. So no, so you can really put the Get Int app to the Print F you can do some stuff after the Print F and then have the Get Int it's just kind of so the user knows what's going on. Yep? >> [inaudible] a long data set, what would the percent be? >> Oh, sure. so along is going to be ll, so percent ll. It's going to be along ,and then d for integer, f for float and that's all you'll need so far. So you can look that up I think in so Google is actually really, really helpful. If you have a problem goggling it, we'll find you the answer very quick. There's also a reference that CS50 has that basically has a list of every C function you'll ever need and it will show you what arguments it takes and how to use them. I can show you that like afterwards. All right so yep. >> [Inaudible] apps what your number was it in concerning the variable that it shows you just the number [inaudible]? >> Yes. So let's actually run this just to see. So I already compiled it so I can just say dot slash input. I hit enter. Oh, I have a random character, one second. [ background noise ] Okay. I decided like an extra something. Okay. So I'm going to say make input and I can run input. Now it's going to ask me to enter a number. I can say 5 and then it just says your number was 5. So where that percent d was was substituted with 5 and I can say whatever I want there and it's going to spit back the number. Yep? >> Why do we need [inaudible]? >> So that's a good question. I think I just had a typo somewhere. [ background noise ] Yeah. There we go, so I just had a typo. So I just changed it back to void, saved it. I can say make input and it worked so I just typo somewhere. Okay. So let's move on to actually how we can implement this pennies program. So the first thing we want to make sure we do is to do something called input validation, to make sure what the user typed in is something we can actually use. So if I'm a grader and I decide that there are -- this is CS50 days in the month and I got negative pie pennies on the first day your program shouldn't blow up. It should let me know that ok you need to actually enter the right number of days and a number of pennies that actually make sense. So in this case you need to make sure that both of those inputs are numerical, which is something that Get Int is going to take care of for you, but what it's not going to do for you is make sure that the number of days in the month is valid. So if I say 100, your program needs to recognize that there aren't 100 days in the month and keep prompting me. And also, similarly the number of pennies has to make sense so if I say negative 100 pennies, your program shouldn't allow that to be an input. So what do you do? So let's say the I did say negative 100 pennies, how can you handle that in your coat? So we want to use some kind of loop that says keep prompting me until I give some valid input. So the first thing you want to do is to clear our variable, so it's we're going to store our input in something called n. let's just start it off at the number zero. So now I want you prompt for the input, so you're saying N equals Get Int and that's going to have the program wait for you to type something hit enter. And after you input that we're going to say okay, if it's not valid, then I want you to go back up to that n equals Get Int and do it again and I'm going to keep doing this until eventually n is valid and then I can get out of this loop. So this is a do while loop, so you'll notice this N equals Get Int is going to be executed for the first time regardless of what N is, so it's not going to check N and then ask you, it's going to ask you and then check what N is. so questions on what's going on here? Yep? >> Can you do it so that N is part of the [inaudible] >> So can I put the int N inside of the do, so no. and that's because of something called scope. So what scope is it limits where you can access a variable. So if I declare a variable inside of a loop, I can't get at that variable anymore once I leave that loop. So if I say int N equals Get Int, and then outside of the loop later, try to reference N it's going to be gone. So that's why outside of the loop we say int N equals zero, so this variable N is accessible everywhere outside of the loop, then we assign the value inside of the loop. Yep? >> So what is N stand for? >> So N just stands for number in this case just because it was shortened to fit on the slide. >> So the number where you put it in? >> Yeah. So that -- so N is the variable where your input is going to be stored. So if I said int pennies equals zero, then I would use pennies everywhere, and based on our style pennies would probably be a much better name than N but N just fits on the slide better. Yep? >> Is there a reason that you assign N when you do the loop and then you just normally sign up [inaudible] >> So is there a reason I assigned it? Not really, no. I could have just said indent semicolon. It's just once you're working with pointers and stuff, which is later, it's just good to assign things sometimes. Yep? Yep? >> Sorry. [inaudible] put the N in the loop could you put [inaudible] in [inaudible]? >> So could I put in int N equals Get Int on top of the loop? >> So I could and then say N equals Get Int inside of the loop but what that's going to do is it's going to prompt me twice. It's going to do the first Get Int then it's going to go into the loop and do another Get Int. so that's the reason that this is structured the way it is. Yep? >> So [inaudible] that every time you do the [inaudible] that you assign N? is there [inaudible] have to do that [inaudible] sometimes error to say that you assign the variable. >> So you're allowed to reassign multiple values to a variable, what you can't do is create two variables with the same name. so this int N is going to create a new variable called N. if I do int N somewhere else, it's going to yell at me because I already have a variable called N. so once it's created, you can assign to it whatever you want. But you can't --what you can't do is create two different things and try to recreate it. Or else it's going to yell at you. Okay. So now that we have these -- Yep? >> How -- are you just -- did you define in that win and it's not valid ,or -- >> No, no. so this bottom while N is not valid, so that's not actual like see, you can type. You probably want to do something with like a greater than or an equal to or something like that, but -- >> So it's just like while N is less than or equal to. >> Yeah. Exactly so you could say like while N less than zero or lesser than equal to zero. So any booming expression, that thing you can see that evaluates to either true or false, based on the values you're giving it, that's where you'd want to put inside of this while here. Yep? >> So isn't that N equals [inaudible] because it's the silent [inaudible] or why did that [inaudible] >> So the reason it's not recreating the variable is because it's missing that int thing. So that int N is going to create the variable N. so if I'm just -- once it's created I can just use it by using its name. >> So if you're not declaring a variable then you don't interpret that as being you're using the variable. >> So say that again? >> So you're not declaring the type of the variable and [inaudible] will interpret that as being using the variable for -- >> Yes. Exactly. So if you don't give it a type -- so if I don't say int N I just say N then C is going to say I'm not creating it. It must exist somewhere. If it doesn't exist somewhere I'm going to yell at you. Okay. so once we have that input, let's go over what we want to do with it. So what we need to do is we want to keep doubling our money the right number of times, and then at the end output how much money we have. So this is some pseudo code, so not actual c code, but kind of code written out in English what you need to do. So lets' take a look at an outline of how we can do this. So the first thing we want to do is get the number of days in the month and after we get that we want to validate that input to make sure it's a number that makes sense and then we want to do the same for the number of pennies on the first day. Make sure that's a number of pennies that makes sense. So now we're going to enter our loop or the block of code that's going to repeat more than one time. So for every day after the first day, we're going to double the amount of pennies we have. And so that's going to represent the number of pennies that we get on that day. After that we want to update the total number of pennies we have. So after this loop is done, that means we've gotten some value from pennies for every day of the month and we can output the total number of dollars and cents that we have after the month is over. So this is essentially an outline for what you want to be writing and see, so you can't compile and run this, unfortunately, but you want to translate this just like we were going to translate scratch to see or to translate this pseudo code or like almost code, not quite, into actual code. So questions on this outline or what we went over so far? All right. So that's pennies. Let's move onto greeting. So this is, as the PSET says, an example of what greedy is going to look like. So after you write it and compile you can say greedy, it's going to ask you how much change is owed, and it's going to output a number. And that number is how to make that amount of change using the fewest coins possible. So the user inputted 41 in this case. Whoa. So let's look at how we'd make change for 41 cents. So the first thing we're going to do is use a quarter, so after our quarter we've used one coin and there's 16 cents left if I do the math right. So after that we can't use another quarter because then that's too much, so let's say okay let's use a dime. So now we've used two coins total, and there is six cents left. So now we can't use a dime because that's too big, so let's try a nickel. So now we've used three coins total, once step left, and finally we can just use a penny, so that means we've used four coins total and there's no change left so we're done. So that's where that four came from. So the reason we're doing things in that order is because our goal is to use the fewest amount of coins possible. So the strategy we're going to employ to do that is to use the best coin at each step. So at each step we have a different amount of change to make. So we just want to say make as much change as possible, so remove as much money as possible from what we owe you using as few coins as possible. So by using the biggest coin at each step, we're actually going to get the right answer, the fewest coins possible. So this isn't true for all problems and computer science, in fact it's usually not but in this case we can say just use the biggest coin possible and by doing that at every step we're going to get the right answer, the fewest coins possible overall. So we can do two things to accomplish this. The first is loose, you can kind of see how this is going to play out, right? We're going to try a quarter, keep using a quarter, till we can't use a quarter anymore. And then we're going to try to keep using a dime until we can't use a dime anymore. So that's one way to do it. Another way to do it is a more math way. So using division and something called the modulo operator, so what this operator is is basically a fancy way of saying get the remainder when I do some division. So if I had five modulo two, that's going to be what's the remainder when I do five divided by two. Which is one. so 11 mod three is going to be 2, because 11 divided by 3 would be 3 and then you have two left over, so that's two, and then if you're doing the modulo of something that's smaller, so if I do three modulo 4, 3 is less than 4 so it's just going to be 3. So does that make sense to everyone what that percent sign thing is doing? >> I don't get why [inaudible] >> So yeah, so it's not quite divided by, so this is the remainder. So it's going to calculate 5 divided by 2, so this is going to be 2.5 right? So it's like 2 remainder 1, and so that's where that 1 came from. So same thing with 11 mod 3 and 3 mod 4. So combining this with division, we can actually calculate how much change is left after we use each coin, so if we divide our total by the value of the coin, then that's how many of that coin can be used, and then we can use the modulo to figure out how much change you need to make after using all those coins. So that's kind of the slightly fancier way of doing it, but before we can do that we need to convert our dollars and cents to just cents. So it's just easier to work with so we don't have to worry about decimals. So how do you learn to divert dollars to cents? So you just multiply by 100, right? So I have, you know, $1.25, I multiply by 100, I have 125 cents. So let's see what happens when we do that and see. So let's open up or let's write it. So let's create a new file. We're going to include our CS50.h, and we're going to include our standard IO.h, so just the two [inaudible] fails that allow us to get input from the user and pout something with Print F, so it's name void so now we want to get some value from the user, so I can say ent f equals get slope, and so just Get Int this is a function that someone else wrote for us, and it's going to get a decimal from the user. Yep? >> [Inaudible] >> Oh, yeah. Sorry. So that should be float f, not int f. So now once we have it, let's create some integer that represents the number of cents, so let's say int N equals F times 100. Makes sense, right? We're saying multiply it by 100 and then we're going to have some cents. So now let's just print out what they have. So percent d because we're printing out an integer, and now comma, the value we want to print out, which is N. so does this make sense what's going on here? we're asking the user for some float, then we're going to take that float and we're going to multiply it by 100, so we have the number of cents that represents, and then we're just going to output the number of cents. So let's save this so I called it cents so let's say make cents all went well, no errors, now let's run cents. So you notice because I don't have a Print F anywhere, it's just sitting there waiting for me to enter something. So let's enter in that 0.41, hit enter and let's add a new line so you can see that better, so added a new line, I saved it, we're going to say make cents and try that again. So I'm going to say 0.41, 40 cents. Why would that happen? [ background noise ] So basically when we store something as a float and try to convert it to an int, we're going to do -- something's going to happen called a loss of precision. So C basically stores that .41 as maybe 0.409999 or something like that. So when we convert to an integer, what she is doing is saying, okay, everything after the decimal point, just get rid of it. So if this was stored as 40.99999 and we get rid of everything after that decimal point, we're at 40, not 41. So what do we want to do instead? >> [Inaudible] around it. >> Right. We want a round. So luckily there's a function that someone else so nicely wrote for us called round. Oh, boy. So there's this function called round, and like Print F is defined in standard IO.h, this is defined in math.h. Yep? >> [inaudible] the functions are somehow like the round -- the round the [inaudible] there is a bunch of them [inaudible] >> Yes. So is there a list of some functions on line somewhere? There is so there's a good list called CPP reference.com, but if you just Google like list of math.h functions or even just like list of C standard library functions or something, you'll find something. I 'little try to find a good link right after this and show everybody. So questions on round? So to use it, we just want to say instead of int N equals F times 100, we just want to say round, F times 100. And so this round is going to return a rounded value. so just like we said int N equals Get Int, we're going to say int N equals round, the value that we take in, times 100 to make it to cents, and rounded correctly. So we don't just truncate. Now if we save this we're going to say make sense. So just Yep? >> Based on the [inaudible] >> So yes, I was about to get an error, so I also have to say include math.h. so just as a reminder, every time you change your program, you can't just run it again. So I can't just say .cents again because I was still going to get that error. So every time you make a change make sure you save it, then make sure you say make and then the name of your program without the .c so I said, make sense and I'm going to run cents again, 0.41. so now we are good to go, we're not going to be off by one. so questions on why we need round or how round works? Yep? >> Why didn't we have to like say need to [inaudible]? Its figures [inaudible]? >> So how do we say we didn't need two significant figures? >> We do need -- like here we do need two significant figures, [inaudible] understand that we do need two figures. >> So no, it doesn't. so if -- if I typed in like 0.413 we're just going to chop off that 3 at the end. Because if I just said 0.4 that's going to be interpreted as 0.40. so we can, you know, we're not going to deal with halves of cents or anything, we'll just deal with at least pennies as the lowest. Yep? >> [Inaudible] section of the [inaudible] is using this round, is using round a good idea to get -- to get the decimal place correct when you print off the money at the end? >> So yeah, so you might want to do that. I think the problem set specifies if you should round or just truncate, so whichever the problem set says, I'm not sure off the top of my head. Yep? >> So like you want to round numbers, you're like more, so the reading in some places, how would you discuss the fact that that happened? >> So if you wanted to round to -- so in this case we're dealing, we're just dealing with integers. >> Right. So let's say you have like a full and you want to like round it up to like 3 or something. >> So yes, so you definitely won't have to do that in this problem set, but usually what you'd want to do is just kind of deal with the number in as many decimal places as possible and then once you're done with it you can truncate. >> Okay. >> So there's a way in Print F to just display like two totally different decimal places. But yeah, so the best thing to do is just deal with as many decimal places as possible, and then once you're done, out the rate on the ops. >> So now let's go through the outline for greedy.c. so again we want to do user input to make sure they typed something that we can use, and I want to keep track of two things, the first being how many coins you use in total, so a counter of how many coins you've used. And then how much change is left to you in any given step. So we want to know, yeah, I used two coins, how much change do I have left? So when we're using these coins we want to try to use each coin, in descending order. So quarters first, dimes, nickels, pennies. So then we're going to do this until there's no change left, and then we get output the number of coins, so make sure you're not outputting zero every time because the zero change left, but output the number of coins that were used. And so here's the pseudo code outline. again this is just one way to do it. This doesn't use that division or modulus operator, but this is using the loops approach. So you're going to get the money you want to make change for, convert that into cents, now we're going to say wow, there is more than a quarter left, then I want to subtract a quarter from how much change we're making, and then you say I just used a coin. So we're going to do that until there's no more than a quarter left, then we can say okay. well there's more than a dime left, I'm going to take a dime off how much money I'm making change for, and then I just use another coin. So make that counter go up by one. then once that's all done, I'm going to say this is how many coins I used. And if your counter's working right then that counter should be exactly how many coins are necessary to make that much change. So questions on that outline? >> What is the [inaudible] again? >> So that increment coins used, so you want to be having some counter of how many coins have been used so far, and you just want to make that kind of go up by one because you just use one coin. Yep? >> So you set up the counter and you read it to all of them [inaudible] costs when you [inaudible]? >> Yeah. So we would probably want to use an integer for this counter, because you're never going to be using half a coin. And then just make that integer go up every time you use a coin. Yep? >> Are we assuming that we're only making change for quarters and smaller amounts? >> Yes. I think the quarter is the biggest you can use. It's in the problem set, like what the highest coin you can use is, but it should be a quarter. All right, so one program left, and that is chart. Oops, a question before that? >> Yeah. If you have [inaudible] and then an [inaudible]? >> Sure. so you know, there is more than one thing you can do here. you can use one Y loop and a bunch of [inaudible] and you can use no loops at all and still do it. There's a bunch of different ways that you could write this program. so where one is better than the other is where a design comes into play. So after you write your program you can think to yourself can I do this better? Like does this look kind of ugly or is this kind of a dumb thing I did her or can I do this better? And once the answer to that question is no, then you should probably submit your PSET, or if it's the deadline that's a good time to submit it too. So other questions on greedy? Okay. so last program is chart, and so this we're going to be implementing our bar graph library, it's going to be sweet. So we're going to ask the user for four different numbers, number of males spotting females, females spotting males, and so on. And so after we get all those values we want to somehow display a bar graph so we can visualize, you know, who what gender is spotting the other gender the most or the least. So the maximum width of this chart is going to be 80 characters. So that means that there can be at most 80 hash tags across the screen. So you'll notice that there were three males spotting females, but there aren't three hash tags. That's because this is based on proportion. So what percentage of the total spotting were male spotting females? And that is how we determine the width of that bar. So let's take a look at this example. So there were 3, 4, 1 and 2 in that order, and so that's going to add up to 10 total sites. So that means that 30 percent were males spotting females, 40 percent were females spotting males, and so on. So now you have everything as a percentage and you want to convert that percentage to those hash tags, because 30 percent doesn't mean 30 hashes either. So to do that, we're going to say okay, I have 30 percent, what's 30 percent of 80? And I'm going to get some number. So the problem set specifies if I get a number and it's a -- you know, 24.1, I'm just going to always round down. So just get rid of the decimal place, don't worry about rounding up correctly, just always round down. So I have 24 of these, 32 female spotting males, and so on, and so now you'll notice there are 8 females spotting females and so we have 8 hash tags at the bottom there. So questions on the behavior of what this is supposed to do? Yep? >> If we're always rounding down, did the -- like do the [inaudible] have to add up to 80 in the end? >> So do they have to add up to 80 in the end? No, because you know, you'll run into rounding errors. So they don't have to add up to 80, but the rule is get the percentage and get that basic, get that percentage of 80 and then round down. So they won't necessarily all add up to 80 every time I don't think. Maybe they do. Yep? >> So the [inaudible] function will let you decide whether you're going to round up or down or -- >> So that round function we looked at is going to round correctly either way. So we don't want to round correctly, we just want to round down. So that essentially means is just chop off all the decimals after the float or whatever it is that we're using. So what that is is what we actually tried the first time, we tried to convert cents to dollars, we effectively just chopped off all those decimal places which in that problem was wrong, but in this problem is right. >> Why do we need to round down? >> So why do we want to round down? Just because the PSET said so, it's slightly easier, so we don't ever run into 81 sometimes. Yep? >> Can you repeat why the -- why it chops off all the [inaudible]? >> So why it chopped off the decimal place, sure. so if I have a float and I want to convert that float to an int, the way that C is going to do that , is it's going to say, well, there's an integer at the beginning of my float, let's just use that. So that's just something that C does. C isn't smart enough to round for you, it just assumes that you want to effectively round down or get rid of all the decimal places. So if I say like int N equals get float, I'm just going to get that float rounded down. >> But in the case of the point 41, it just rounds down to the [inaudible] that's zero? >> Right. And so this is -- has to do with how floats are stored. So we're not actually storing 0.41, we're storing 0.40999999 or something, and so basically when we're going to chop off we're going to lose all those 9s. so that Y isn't so important right now, but it's more of just make sure that you use round on that part, but on this part we can just chop off. Yep? >> I don't understand the problem you're trying to solve. I thought you were trying to solve the first one you showed us, but I don't understand what I'm trying to solve here. >> So the chart itself? >> Yeah. >> So what we want to do is we want to say I saw this many people of each gender spotting some gender, and I want to output a bar graph that visualizes what those -- what those values are as a percentage of all the sittings. So there were, you know, 10 sittings total, of those 30 percent of them were males spotting females, and so this bar is 30 percent of the maximum possible width. Does that make sense? >> Do you take individual representation? >> Yes. So you're basically taking these numbers and making a bar graph in text so visualizing what those look like. >> So you're given those numbers? >> So these numbers you're going to prompt the user for them. so the user's going to type in 3 or whatever they want, and then based on what the user types in, you want to display a different graph. Yep? >> And how do you get the program to put out the number of hash marks that you want? >> So a good question. So we probably want to use some kind of loop, right? So here's this quick pseudo code English outline. we want to get those four values, make sure that they're integers, because you can have fractional people. and we want to calcite the total number, so just like we did before, we added them all up to get the total number, and now we want to convert those to percentages. And based on those percentages, you want to get a number of hash tags to display. Now based on that number, you want to print out that many hash tags, so if I got a 10, I want to have some loop that prints out 10 hash tags. So if inside of my loop we have our Print F and we don't include that forward slash N, then it's going to stay on the same line. So you're basically going to print out everything line by line. So we're going to say I'm not the male-spotting female track, so I'm going to print out all the hash tags based on how many we calculate it to be. So we calculated there to be 10 so I want 10 hash tags and then I want a new line because then I want to say female spotting male on a new line, and then another graph. Does that make sense? >> Yeah. >> Okay. so just again, so after we do all of our math we're going to get some values. We're going to say, okay, I want 24 hash for male, the first one, 32 for the second one, and so now we want to have loops that display each of those. So we know that we want 24, so we want some loop that executes 24 times that just prints a single hash. So if you have a loop that executes 24 times and each of those 24 times it prints a single hash, that means we just printed 24 in a row. >> Thank you. >> Questions on that? So that's our other shorter outline, so we just get all the input, do some calculations and then output them using a loop. So any questions on anything in this problem set before we finish up? So other questions? Yep? >> I would just like to see what a loop looks like again. >> Sure. so what a loop looks like, so let's open up our style here. so this is what a four loop looks like. So we have three parts to the four loop. And the super section we'll go over this in more detail so if you want to look at these slides of the video. But basically there's three parts. We have the first part, and that is how -- what to do before the loop starts. So before the loop starts we're creating a variable eye, and we're starting it off at zero. So this statement is going to be executing before anything happens. Now what's next is what needs to be happen -- what needs to be true before the loop will execute again. So we say that the I, our counter variable, has to be less than the total number of times we want to execute the loop. So this just happens if you ever user told us, and at the end of every loop we want to say increment this counter so that we don't go too many times. So the first on this loop iterates, we're going to have I of zero. Zero is less than 5 so we're going to print high. And now zero is going to go up, so now I is 1. So now I is less than 5, so we're going to print high, now I is 2. So this is going to keep building until eventually I is 5. Now 5 is not less than 5, so this loop is going to finish. So with a wow loop, it's pretty similar. We can just say while I is less than iterations, we need to make sure that we create I so int I is going to create a new variable I, we're going to start it off at zero, and so while I is less than the number of insertions, we can do something like print high and then we want to increment I. so just like I plus plus was all the way to the right of this force four loop, this is executing once the loop is done, so before it loops again. So now we're saying while I is less than the total number of times you want to do this, we're going to do whatever it is you want to do, you know, double pennies or something, and then we want to go on to the next iteration. So those are the two basic loops here. Yeah. >> [Inaudible] equals or you [inaudible] surround the [inaudible]? >> So in this case we're always starting I off at zero. What we're asking them for is this iterations variable. So if I is always zero and then you go into I is less than some value, we're going to execute that loop, that maximum value a number of times, so that upper bound. Yep? >> [Inaudible] change [Inaudible] >> So the difference between do while and while, so let's say that we said int I equals 5 for some reason and then iterations is 4. So let's say, you know, the user typed in a 4 and now iterations has a value of 4. Now this loop would never execute, because I is not less than iterations, 5 is not less than 4, so this will never be true. If instead we use a do while, then we would execute it once then check if it's true and never execute it again. So the difference is where the check is. with the regular while, you're going to check before you do anything and with the do while you're going to check after you do everything. Other questions? Yep? >> If you have a long, long number, does it like display -- if you have like a real long number [inaudible] .39 will it display that whatever? >> So the -- so a long, long can't have decimals afterwards, only a float can. But Yeah. So if you just printed a really big float it would try to print the whole thing. >> Okay. >> So last questions? >> [Inaudible] >> So what is the I plus plus? So this is the same as saying I equals I plus 1. >> Oh. >> Yeah. So it's just a shorthand notation to make I go up by one or increment I. >> Could you write it either way? >> Yes, so yes. So you could write this either way. You could also write this as I plus equals 1, which is shorthand for I equals I plus 1, which I plus plus is shorthand for. Yep? >> This is not [inaudible] but when you are closing on virtual box can you reopt it to say [inaudible] states [inaudible] shut down signal and how [inaudible] to do? >> Oh so when you're closing virtual box, which one should you do? >> Yeah. >> It probably doesn't matter. I usually just do power off the machine, but send the machine a shut down signal will be the same as like going to start shutdown. And power off is the same as like hitting the power button, which is faster. But not always the best. Other questions? Oh yeah? >> So you were talking about the library that uses the math.h. can we use [inaudible] library that are [inaudible] which ones we should consider? >> So if the problem so are there limitations one what libraries we can use if the problem set doesn't list any? Then yeah, you're free to use whatever standard C functions you're using. If you use something like you like download something from the Internet, first don't download the answer to the problem set, but if you download like some function from like stack overflow or somewhere, make sure you cite it. Say someone else wrote this function, I got it from here. this is the URL and you'll be good to go. But if you're using something in like math.h you don't need to document this is from math.h. Yep? >> Do you have to [inaudible] download math.h like into our directory and the Lenox with it? >> No. so math.h is kind of included for you. so just like standard IO, .h kind of comes with c and the same thing with math. >> Where are these [inaudible] math.h? >> So these are -- so where are all of these stored? At various by computer, but probably inside of like user live or something like that. If you open up [inaudible] and just like go to the root of your computer, which is just slash, which is like the equivalent to c or just like the highest level possible, then you can poke around in there to see where things are. But it's different on every installation. Any last questions? Yep? >> In the beginning [inaudible] early on someone asked about the main thing that you were supposed to type in for the applications like on the website. But that doesn't really help [inaudible] do you say like [inaudible]? >> So if afterwards you just want to show me I can give you a better answer by looking at it. So just come up here afterwards I can help you out. Yep? >> Are these are all [inaudible] and the other [inaudible]? >> Yes, so you are installing two different things. First you're installing virtual box and then you're installing the CS50 appliance into virtual box. So they're two different things, one of which we made, one of which we didn't. all right, if there are no more questions than good luck on the first PSET. Don't forget about help.cs50.net with any questions.