LEXI ROSS: All right, hi, everyone.
Welcome to the first CS50 supersection.
This will be the only week of supersections.
After this, you'll be going to regular section with your
assigned TF.
But this week, we're doing it in a little bit of a different
format, although the actual format, how it's going to go
for the next hour and a half or so, is going to be pretty
similar to what you'll be seeing for
the rest of the semester.
>> So what we're going to do is start by going over the
section part of your problem set.
So if you've read over the p set, which hopefully you have,
you'll see that there's a part called "A Section of
Questions", and these are meant to be gone
over in your sections.
So you have an advantage by being here in that you get to
go over these more conceptual-based questions.
And after we do that, we're going to go over the last two
section questions, which are more dive
into coding type questions.
So we're actually going to have you guys take a few
minutes for each question, try to solve them on your own, and
then we're going to come together using the new CS50
Spaces tool and go over the answers so you all have the
correct code to review.
>> So a couple of announcements.
Don't forget that there's office hours every night this
week in Annenberg from 8 to 11, so do take
advantage of those.
And also, don't start the p set too late into the week.
Start tonight or tomorrow night at the latest.
You will regret it if you wait too long to start
and run into issues.
So take our advice and definitely start early.
This'll be a good way to get started with
these section questions.
So I'm Lexi, by the way.
I'm a TF.
And this is Dan, who's also a TF.
DAN BRADLEY: Hey, guys.
LEXI ROSS: And he's going to get
started with the questions.
>> DAN BRADLEY: All right.
So I'm going to just introduce a couple little compiler
issues that we're going to talk about and just go through
some sort of conceptual ideas that we need to actually run
code, either in an appliance or with our sort of fake
appliances, Run CS50 and Spaces.
So let's just see if anyone in the audience has any idea
about what we're talking about.
>> So what is a library in the context of code?
Anyone have any idea?
Okay.
Sweet.
AUDIENCE: Like a stock of functions that someone else
already programmed that we can build up?
DAN BRADLEY: Exactly.
So a library is a stock of functions that somebody else
has already programmed that we don't have to
re-implement ourselves.
So we have things like the standard I/O library, the
cs50.h library, that are things that the world, the
Internet, the staff have made that makes our
lives easier as coders.
So how do you print things to a screen?
I have no idea, printf does it for me.
It's something somebody else has already made that is
awesome and works quite well.
>> Okay.
So to use libraries, on the other hand, however, you have
to actually do some extra things with your code.
So you have to use things like this #include.
What does that do?
What role does that play when you have it in a program?
AUDIENCE: It lets you make use of any functions that are
[INAUDIBLE]
library by simply using the definitions that have already
[INAUDIBLE]
library.
DAN BRADLEY: Okay.
It lets you pull library functions into your code, but
it does it in a way that isn't exactly the same as like
-lcs50, which we'll get to in a minute.
It just lets the compiler know that hey, there are these
functions that are going to come up in your code.
Be aware that if they aren't fully described in the code
that's in front of you, there's some library function
that is going to take care of it.
>> So in the case of printf, which we'll go over a couple
times today, is the standard I/O library.
So while I don't define printf anywhere in my code ever, the
standard I/O takes care of it for me, and I'll do #include
stdio.h, and it will pull in these functions that I
haven't ever defined.
So what is the difference, then, between this #include
and -lcs50?
What does -lcs50 do when I pass it to the
compiler, like Clang?
Anyone have any idea?
Okay.
Yeah?
AUDIENCE: It lets you access the CS50 library?
DAN BRADLEY: It lets you access the CS50 library in
that it tells the compiler, Clang, that hey, this is where
you actually go to get these functions.
So the #include says there are
these functions somewhere.
The -lcs50 says, go to this spot in your computer--
that's the cs50 library--
and pull up the actual code for these functions.
Does that make sense, everyone?
>> So these are all things that are to help your compiler
along with making the code actually run.
Quick question.
Do people know what a compiler is?
Good sense?
It's basically taking the written code that you have,
converting it to binary, converting into machine code,
so your computer can actually understand it.
And without further ado, we're going to go into the more
actual coding exercises that you guys will have some fun
sinking your teeth into, and Lexi will go over that.
>> LEXI ROSS: Okay.
Awesome.
So we're going to go over two questions here.
So the first one is actually, you can see, it's a little bit
small right here, but essentially it's asking you to
write a program to take some user input.
I'm not sure why there's a black square there.
It's mysterious.
It should be fine, though.
So it wants you to take a user input in Celsius, convert that
to Fahrenheit, and then tell the user what the temperature
would be in Fahrenheit.
So you can see a sample input/output sequence here.
The user inputs--after Celsius, the bold refers to
user input.
So the user inputs 100, and the program outputs
Fahrenheit, 212.0.
And an important distinction in this program is that we're
going to only have one decimal place, so we'll see how to do
that as well.
And so another thing to note is that we're using a.out
because Clang, the compiler, automatically names its binary
files a.out unless you specify it with a certain flag to give
it its own name, but for now we're just going to use the
default, a.out.
>> So let's actually take a few minutes and try to write out
some code for this yourselves.
So it's 4:15.
So at 4:20, we are going to come back and--oh, right.
Spaces.
Cool.
This is sort of like what we call skeleton code in that
it's a skeleton, but you have to fill in the meat of the
program to actually make it run.
So you can see the #include directives up there to include
cs50 and stdio.
I noted in a comment the actual formula for converting
Celsius to Fahrenheit, in case you don't remember from high
school science or whatnot.
And then this is just a skeleton for
the actual main function.
So everything you write is going to go here.
And in order to actually get to this space--
so CS50 Spaces can be accessed using this URL, which Dan is
going to write on the board, and I'll dictate as well.
DAN BRADLEY: cs50.net/spaces--
LEXI ROSS: /qWLc--
I'm sorry, y.
DAN BRADLEY: Y?.
LEXI ROSS: No, no, no, no.
That's fine.
qYLcYWfg.
So I should see everyone's names kind of popping up here.
>> Awesome.
This looks great.
This means people are joining the space.
So if you haven't gotten a chance to play around in
Spaces yet, this is actually what you're going to be using
with your TFs in section all semester.
How it works is kind of everybody writes their own
code, and nobody can see it until you press the Save
button, which I'm pointing to right now.
So I'm going to press Save, and if you all go to my name,
which you should be able to see it in People in this
Space, which is Lexis Beryl Ross.
So how it's going to look is you click on my name, and
you're going to see Revision 5, which
is the latest revision.
Copy only Revision 5 into your space by clicking on it.
>> All right.
So let's go through this problem together, and I'm
actually going to write the code up here on the screen.
You can zoom in a little bit.
And then once I'm done, you can copy my latest revision
into your space, if you want to see the final solution.
Now, Spaces has the capability for me to go in and see any of
the code that you guys have written, but just because it's
such a large section and we don't really have the same
kind of discussion capabilities as a smaller
section, we're just going to go through and I'm just going
to write the code.
But definitely stop me with questions at any point.
We're going to go through it kind of step by step to make
sure we understand each part of what's going on here.
Can everyone see this text here?
Does anyone want it bigger?
>> So what's the first thing we're going to do here?
Well essentially, since we want to get input from the
user and we want it to be kind of a good experience for the
user, we actually want to tell them what we're looking for.
So let's print out exactly what we want form the user.
So just don't bother raising your hand, just rattle it off.
What is the command to print something to the screen?
AUDIENCE: printf.
LEXI ROSS: Awesome.
Okay, so printf.
>> AUDIENCE: Hey, Lexi?
[INAUDIBLE]?
LEXI ROSS: Sure.
So printf, as we've seen frequently in lecture, is the
command to print something to the standard output stream.
So we want to ask the user to input a
temperature in Celsius.
And now, we actually want to take the input that they give
us and store it into a variable.
So we've learned a little bit about types.
Can someone remember what type we would want for a variable
with a decimal place in it?
AUDIENCE: Float.
LEXI ROSS: Float.
Yeah, awesome.
It's a float.
So.
And how are we going to--big hint--get the
float from the user?
AUDIENCE: GetFloat.
LEXI ROSS: Yeah.
Cool.
And the user could potentially be mean and try to give us a
negative number, but let's not worry about that for this
problem now.
You're going to worry about that a little bit more on your
homework on the problem set.
>> So let's assume they give us a good number, and now we
actually have to convert that float from Celsius to
Fahrenheit.
So let's consult our formula up here, and let's create a
new variable and call it Fahrenheit.
I studied the spelling.
It's kind of crazy, I know.
And let's count on the order of operations to kind of do
its job here, but we'll use parentheses just to be
completely verbose and clear.
So Celsius times 9 divided by 5.
I'm going to zoom out just a little bit so this can fit on
one line, but I'll zoom in again for those who can't see,
farther in the back.
Sorry about that.
Strange cursor issues.
Plus 32.
Okay, does anyone have any questions about how I put in
the formula, or any issues with that?
Yeah.
>> AUDIENCE: Do we need to put printf before the float
celsius, float fahrenheit script lines?
Does it matter which order they're in?
LEXI ROSS: So the question was, do we need to printf
before we actually ask for the float?
The answer is that we do.
That way, that the cursor that the user gets to input their
float will occur after the printed c, right?
And it makes sense if you think about logically, like
you're asking the user a question, and then you're
receiving an answer.
So it prints c, and then there's a cursor, then the
user can type in the value, as we'll see
later on when we demo.
Yes?
>> AUDIENCE: Why do we use a float and not a double?
LEXI ROSS: So a double, essentially, as the name
implies, gives us double the amount of
precision as a float.
But typically in C, in this course, for a number of
reasons, we're going to use floats.
And this is really all the precision we need just for
kind of basic temperature conversions.
Typically, doubles are more used in the Java language,
floats are more common in C. All right, awesome.
>> DAN BRADLEY: One more.
LEXI ROSS: One more.
Yes?
AUDIENCE: Where do you put the Space Bar?
Like, [INAUDIBLE] the Space Bar [INAUDIBLE]?
Does it matter?
LEXI ROSS: Oh, no.
So it doesn't matter.
I actually should have put another space here.
But what does matter is that you're consistent
throughout your code.
So if you decide to use one style of spacing, make sure to
keep it throughout your whole code, or you may get style
points deducted.
So consistency is key.
DAN BRADLEY: I think he was talking about that space.
LEXI ROSS: Oh, you're talking about that space?
By the actual C?
AUDIENCE: Yeah.
LEXI ROSS: Oh, so that's just for looks, right?
So that way when we run the code, if we didn't have this
space here, if it looked like this, the user would start
typing in the number and it would be squashed up right
against the colon after the C, and it would look a little bit
better if there was a space.
And you can see that a little bit better once we actually
run this code, when we get it finished.
Awesome.
Yeah, one more question.
>> AUDIENCE: Do I have to put a decimal point after one of the
[INAUDIBLE]?
LEXI ROSS: So you don't, because of the order of
operations.
So because Fahrenheit is already a float, as is
Celsius, you don't need to explicitly cast any of these
other numbers as floats, although in other
circumstances, when you're dealing with ints, you may
have to put a point to explicitly cast it as a float.
Yeah?
AUDIENCE: I was just wondering if capitalization matters in
these commands like GetFloat?
LEXI ROSS: Yeah, good question.
So the question was, does capitalization matter in
commands like GetFloat?
The answer is yes, it does.
So GetFloat is, as Dan was describing earlier, a library
function in the CS50 library, and it was written as capital
G, capital F. So if you don't write it with those capitals
there, the compiler is not going to understand what
function you're referring to.
Okay.
Yeah?
>> AUDIENCE: Is the argument for main always void?
LEXI ROSS: Yeah, that's another great question.
So the code that you copied from my space mistakenly had
some more arguments.
So the question was, is the argument to the main function
always void, which essentially means nothing?
The answer is no.
You may notice that the code you copied from my earlier
revision had what may look like nothing that really makes
sense right now, some char*, argv, argc.
Don't worry about that for now, but you'll find out later
on they do make a difference.
But if you're not using that sort of added functionality
which you'll learn about later, void
is completely fine.
>> All right.
And we have one more step, which is that we want to
actually show the user what the converted temperature is.
So again, we're going to use printf, and let's say f, just
for consistency's sake.
And now we're going to do something a little bit tricky.
So we're going to use a flag, or a special kind of notation,
to tell printf to only print one decimal place.
And does anyone happen to know what that's going to look
like, before I type it out?
Yeah?
AUDIENCE: 0.2%f?
LEXI ROSS: Yeah, yeah.
So actually, it's really close.
0.01f for .01%f, and then we have to remember to include
the other argument to printf, which is the variable itself,
which is n.
>> One last step.
Can someone remember what that is in our main function?
Yeah, up there.
AUDIENCE: Return?
LEXI ROSS: Exactly.
So since this is main returns in int, we want to
also return in int.
DAN BRADLEY: Percent needs to be on the other side.
LEXI ROSS: Okay.
Sorry, guys.
That was a mistake.
So the percent comes before 0.01.
So it's %.01f.
Those are kind of hard to remember.
Many people, like me, just look them up
whenever we use them.
Yeah, up there.
AUDIENCE: Why is it 0.01 and not 0.2 or 0.1?
LEXI ROSS: I don't know, actually.
Do you know?
DAN BRADLEY: The reason it's 0.01 as opposed to 0.02, or
0.2, or 0.1, is fairly arbitrary to the way that the
programmer originally designed it.
AUDIENCE: That's not what I meant.
I'm not asking the reason.
[INAUDIBLE] reason why.
I have it written as 0.2, and it delivers two--
DAN BRADLEY: Yeah.
So we were only wanting one.
AUDIENCE: Oh, Okay.
So you could 0.01 then.
DAN BRADLEY: 0.1 would probably also work.
0.01 works as well.
LEXI ROSS: Oh, the 0 might be to show zero, if
it's less than one.
DAN BRADLEY: Yeah, I think that is right.
So the 0.01 would show a zero if it doesn't have anything
that would go there naturally.
So if you were, say, putting in 100 for Celsius, it would
come out to 212 normally in Fahrenheit, like 212 exactly.
If you have the 0.01, it will show 212.0, as
opposed to just 212.
LEXI ROSS: That's right.
So reading it basically tells printf, print a decimal point,
and print a 0 if necessary.
If not, print the first decimal position.
DAN BRADLEY: And we'll explore some of this when we're
actually running the code.
LEXI ROSS: Yeah.
AUDIENCE: So I put 0.1.
It still prints its zero.
DAN BRADLEY: It still prints its zero?
AUDIENCE: Yeah.
DAN BRADLEY: Hm, okay.
LEXI ROSS: Cool.
>> All right.
More questions on this code before I compile and run?
Does anyone see any bugs here, first of all?
Anyone see something that should be here?
If this were your problem set, what would be here that is not
there right now?
Yeah, great.
So I didn't include comments because I'm delivering them
orally, but if I were writing this in a problem set, I would
definitely want to put comments to indicate what I'm
doing, essentially, in the program.
And you'll kind of get a feel for it, as you move through
the semester, the right amount of commenting.
You don't want to be too verbose and put lines and
lines of comments for each of your lines of code, but you
also don't want to be not verbose enough and leave the
reader to try to figure out what the heck
your code is doing.
>> Okay, cool.
Let's try running this code.
Okay, I'm going to zoom out a little bit.
Cool.
So you'll notice there's a Play button here.
Let's click Play.
So you'll notice here, just as in the spec on the problem
set, there is the ./a.out.
a.out is the name of the binary executable, which is a
long word for just a file consisting of binary code that
your computer is actually running.
So as we expected, we have C here.
So call out a number.
AUDIENCE: 23.
LEXI ROSS: 23.
DAN BRADLEY: Your program took too long to run.
Just run it again.
LEXI ROSS: Oh, my bad.
Okay.
Spaces is picky sometimes.
It wants you to think on your feet.
Okay, 23.
Awesome.
Does everyone believe this?
Cool, because it works.
>> Okay.
Let's try a couple more.
Let's try an edge case here.
In computer science, when we talk about edge cases, we mean
cases for your program to run that are sort of on the very
edges of reasonable inputs or that test certain corners of
the logic in your program, and this makes more sense when we
do more complicated programs.
Let's try 0.
Awesome.
We all know this.
Cool.
Any more questions on this before I move
on to the next program?
All right, awesome.
So now, we're going to--
do you want to just use my computer?
DAN BRADLEY: Yeah, that's easy enough.
>> All right, so going to go and talk about making percentages.
So we're essentially, in this next little exercise, going to
take two integer inputs from the user, hoping for a
non-negative numerator and a positive denominator, and
basically dividing them and outputting what comes out as a
percentage.
So we want it to only accept non-negative numerators and
accept only positive denominators, and keep asking
for new ones if it hasn't gotten what it wants.
All right?
So give that a go.
LEXI ROSS: Oh yeah, do you want to do the--
oh, maybe people should copy my code.
DAN BRADLEY: Yeah.
LEXI ROSS: [INAUDIBLE]
Spaces.
>> AUDIENCE: I have a question.
If I'm using Spaces and I want to save [INAUDIBLE] the
Fahrenheit conversion, do I save it and then make a new--
LEXI ROSS: We could just do a new Space, or you may just
want to copy and paste it to a text file or
something so you have it.
DAN BRADLEY: I'll just change my name.
LEXI ROSS: So I just saved the latest revision of
tofahrenheit.c, so if you go into AAAA Lexis Beryl
Ross, you can go in and copy that latest code, and then
start on this new one, which I think Dan is going to provide
some skeleton code for as well.
Okay.
DAN BRADLEY: The latest revision I have on mine, so
it's at the very top this time, is Revision 4, and
it'll have what you need to get started.
LEXI ROSS: All right, and I'll copy that into mine as well.
>> DAN BRADLEY: All right.
I think we're going to just start going over this, if
anyone is interested.
LEXI ROSS: We're all interested.
DAN BRADLEY: We are going to start out just by declaring
some variables. So we have a numerator, and for the sake of
now, I'm going to just start it with the value -1,
and then I'll set up a do-while loop. While--
so when do I want it to loop?
When do I want it to keep asking--
yeah?
AUDIENCE: When it's less than or equal to zero.
DAN BRADLEY: Why can't it be equal to zero?
AUDIENCE: [INAUDIBLE].
DAN BRADLEY: Yeah.
So for the numerator, it's just while the numerator
is less than zero.
Does that make sense to people?
We keep looping while the numerator is less than zero,
we keep asking for new input, and keep demanding that they
input new things.
So I'll just have a little printf in here, non-negative
num--.
And then we do the same thing as before, except we do a
GetInt, just because we wanted to do it as
percentages of ints.
So numerator = GetInt.
>> Okay, now a few things.
Do I need this -1 here?
AUDIENCE: [INAUDIBLE].
DAN BRADLEY: No.
Why not?
AUDIENCE: Because you're getting a random value from the user.
DAN BRADLEY: Exactly.
But do I want to have the declaration out here?
Yes.
Why?
AUDIENCE: You have to find the outside of the loop.
Otherwise, you're not going to have the variable for the rest
of the program.
DAN BRADLEY: Exactly.
Scope.
Sweet.
Does that make sense to everyone?
AUDIENCE: Can you say that one more time?
DAN BRADLEY: Okay.
So if I were to declare this numerator inside the loop, if
I had this instead of numerator being int numerator,
and I got rid of this entirely,
numerator would disappear.
It wouldn't exist outside of this loop, because everything
is within the scope of these curly braces.
Make sense?
Okay.
>> So for denominator, we do something fairly similar, int
denominator =--
and I don't need the equals--
do, and for the while in this case, what do I want?
What condition do I want it to still loop on?
AUDIENCE: Less than or equal to 0.
DAN BRADLEY: Less than or equal to 0.
Denominator <= 0.
Don't forget your semicolons, kids.
So we do the same thing as before, printf a positive
denominator, and we get int from here.
There we go.
GetInt.
Okay.
And then in the end, we want to print out some percentage.
So to do this, what should our string look like?
printf.
Any other ideas of what I can do here?
Yeah?
AUDIENCE: Maybe %0.2 [INAUDIBLE].
DAN BRADLEY: Yep.
Okay.
I'll start with the first bit, %.02%%,
quotes, comma.
And I'm going to throw a new line in here, just because I
like new lines when I'm doing things.
What sort of mathematical expression should I have here?
AUDIENCE: Parentheses and float.
DAN BRADLEY: Good.
Why do I want to have it in float?
AUDIENCE: [INAUDIBLE].
DAN BRADLEY: Good, good.
Float.
AUDIENCE: Numerator.
DAN BRADLEY: Numerator?
Okay, so I do float numerator.
AUDIENCE: Divided by--
DAN BRADLEY: Divided by?
AUDIENCE: Denominator.
DAN BRADLEY: Do I do just denominator, or do I do float
denominator?
AUDIENCE: Just denominator.
DAN BRADLEY: Okay.
AUDIENCE: And you can multiply that by 100.
DAN BRADLEY: And I multiply that by 100.
AUDIENCE: And then parentheses around the entire expression.
DAN BRADLEY: Yep.
>> Okay.
Look good?
Let's try it.
-50.
Good, 50.
0, and 100, 50%.
It worked out just like we wanted.
Does this code make sense to people?
I'm going to save it so you can get a copy of it.
Yeah?
AUDIENCE: Why don't you need to put [INAUDIBLE]?
DAN BRADLEY: Where?
AUDIENCE: Like, when you say denominator equals
[INAUDIBLE].
DAN BRADLEY: When I have it here?
Why would I have int before it?
AUDIENCE: Because the other one has int before it.
DAN BRADLEY: Well, so that wasn't in a loop, right?
So I declare this int denominator above the loop,
and I don't have it in the loop because I would be
declaring it a bunch of times, and it wouldn't actually carry
over to anywhere else in the program.
LEXI ROSS: Yeah, the important thing to remember is that
variables only need to be declared once.
So imagine it's like introducing a new variable
into the world by calling it int or float or whatnot, and
once it's declared, all you need to do to change it is
just call it by its name: denominator, or x, or y.
You only need to actually call it an int just once.
>> DAN BRADLEY: Yeah.
AUDIENCE: Can you explain the difference between %d
and %f and if you ever declare a
variable as a decimal?
DAN BRADLEY: So %d and %f in here?
LEXI ROSS: So d actually refers to just a digit, not
decimal, which is kind of confusing.
There's no actual data type called decimal, and when you
want to print out an int, you refer to it as %d, or
%-whatever-d. F is for float, which you'd think it
would be i, but it's actually d for int.
>> DAN BRADLEY: Yep?
AUDIENCE: So in the other program, why didn't we just do
the same thing when you're saying Fahrenheit?
Why didn't we put, like--?
DAN BRADLEY: In the bottom here?
You could have.
It would have been completely legitimate.
I like doing it this way, other people like separating
it out into different lines of code.
Either way works fine.
What's up?
>> AUDIENCE: Can you explain what the %%
[INAUDIBLE]?
DAN BRADLEY: Okay, so the percent, percent.
Right, I was going to go over this.
The first percent is called an escape character, and it just
says the thing behind this is not actually code.
So this first percent, right, the %.02f, all of those %.02f
mean something to printf as a function.
%%, I just wanted it to actually just print a
percent sign.
So I'm not saying there's anything important behind it.
I'm just saying there's a percent sign behind it.
Please print out a percent sign.
So percent, then another percent.
AUDIENCE: Can you show what it would look like if you have
float based on another line?
DAN BRADLEY: Oh yeah, sure.
So I'm being asked to show what it would look like if I
had this float numerator, all this math, on another line.
So float percent =--
and I would get rid of this and just put in percent.
And it's doing the exact same thing, I just
have another variable.
>> AUDIENCE: [INAUDIBLE].
DAN BRADLEY: Scroll down to what?
AUDIENCE: So that I can see [INAUDIBLE].
DAN BRADLEY: What can't you see?
AUDIENCE: Sorry, I was [INAUDIBLE].
DAN BRADLEY: Yeah.
AUDIENCE: So if we're replacing the escape character,
suppose we wanted to have a dollar sign instead, just for
example, a dollar sign behind percent?
DAN BRADLEY: Nope.
It's just a dollar, just one dollar sign, because a dollar
sign doesn't mean anything to printf.
The question was, do I need to have an escape character for
symbols like dollar signs?
AUDIENCE: So sort of related, are there any other cases that
we should know of when something like the percent
sign might mean something [INAUDIBLE]?
LEXI ROSS: Yes.
Not necessarily the percent sign, but the backslash, see
how we have backlash end here?
Let's say we actually wanted to print a backlash, we'd have
to escape that--
and this is going to sound confusing--
with another backslash.
So if we wanted to print \\, we would
just have to type that, or for additional
backslash, another escape.
And another one, if we want to print out a quote--so
basically, anything that kind of denotes something special
within a string, we want to escape.
So let's say we actually want to have a quote here, we would
want to do \".
AUDIENCE: So it's usually a backslash?
DAN BRADLEY: It's usually a backslash.
LEXI ROSS: Yeah.
For some reason in here, it's a percent sign.
>> AUDIENCE: [INAUDIBLE]?
DAN BRADLEY: This guy?
This is just to say, the thing that's being divided here is
going to be treated as a float for the sake of the division.
So in the case of 50 divided by 100, if I didn't have the
float there, it would just be 0.
That's all that would come out, because 50 divided by
100, if you aren't including any decimals, is 0.--
and everything after the decimal is just truncated off.
AUDIENCE: Um,
why don't we put it before denominator as well?
DAN BRADLEY: So the reason we don't put it in front of
denominator is because when you're dividing something by
an integer, it works just fine.
Having a larger number on the bottom
doesn't change anything.
You can divide by integers just fine.
It just doesn't really matter.
You could, it wouldn't change anything.
AUDIENCE: So if you wanted three decimal places, would
you put percent .03?
DAN BRADLEY: Yep.
You would put %.03 if you wanted
three decimal places.
>> AUDIENCE: And do commands, what's the difference between
that and just doing while at the top?
DAN BRADLEY: So a do-while versus a while loop are two
fairly similar commands.
The only difference is you run the code once, you run the
code inside the loop one time before
making any actual check.
So it's also the reason I can say denominator without giving
it a number.
So if I said int denominator, and then while (denominator <=
0), it might never even
run the code inside.
It'll make that check before--
so here.
So if I had this while loop here, I would have to have
denominator equals, say, -2 or some number less
than or equal to 0 so it would actually try running the code
inside first.
So generally speaking, you want to use a do-while loop
instead of a while loop when you're asking for user input
because running the code once, asking for something the user
will give you is generally your best solution, as opposed
to just trusting whatever you put in to start with.
AUDIENCE: And do is always followed by a while.
DAN BRADLEY: Do is followed by some chunk of code that you
want to execute, and then while some condition is.
LEXI ROSS: Think of it like one command sort of broken up
into two parts.
It's a do-while, not like here's a do, and then I'm
going to add a while, it's just a do-while.
>> AUDIENCE: I understand the reason why you're using the
two percent signs, but can you explain to me why we have to
have the [INAUDIBLE]?
DAN BRADLEY: Okay, so the one percentage sign is just saying
printf, here is a command that I want you to
fill with some number.
So I want you to fill it with two decimal places and a
float, and you'll find the float at the end of the
command, it's this percent.
Does that make sense?
AUDIENCE: Yep.
DAN BRADLEY: Okay.
>> AUDIENCE: Is there something about the do-while loop that
if the condition is not fulfilled, if you get a
negative number for your numerator, it'll just keep
asking and will keep asking the user?
DAN BRADLEY: Yeah.
So if the condition of while is not fulfilled at the end of
the do-while, it'll run back to the start.
It's just a loop.
So it's like a while loop that the condition hasn't been
fulfilled on.
It just runs one more time.
Yeah.
AUDIENCE: So if you're doing this, can you, for your while,
put two conditions in?
DAN BRADLEY: You can.
It depends what you're asking.
So you can put two conditions in a while loop if you have it
all within some sort of Boolean operator, which sounds
ugly, but it's basically this denominator is less than or
equal to zero, either returns true or false, right?
It's either less than or equal to zero or not.
So you can do other Boolean operators that'll say while
this is also true or while this is also false.
So it's things like and numerator is greater than
zero, right?
That's asking, this'll only loop while denominator is less
than zero and you have a positive numerator.
AUDIENCE: And you do the two ANDs, right?
DAN BRADLEY: Two ANDs.
LEXI ROSS: We'll cover that more later.
>> AUDIENCE: What if someone doesn't put in a positive
denominator?
If they put in a negative numerator, can you prompt them
to, say, put in a non-negative numerator and have
a command for that?
DAN BRADLEY: You could.
AUDIENCE: Right now it just says non-negative numerator.
What would be used for that?
DAN BRADLEY: So you'd have an if condition, so like if
numerator is less than zero.
LEXI ROSS: You'd probably just want to break it up into more
steps, right?
So the first time they put in the input, we'd just say give
us a numerator or whatnot.
And then if they do the wrong thing, this might be the case
for a while loop, rather than a do-while, because we have
the first time we have the GetInt, and then while that input
is bad, we keep asking them for further input with a
different sort of text, like that didn't work or give us a
non-negative numerator instead, that kind of thing.
>> DAN BRADLEY: Yeah?
AUDIENCE: How do you clear the stuff that [INAUDIBLE]
the terminal?
LEXI ROSS: Clear.
DAN BRADLEY: Yeah, you can't type into it.
LEXI ROSS: Actually, this is not a real terminal.
The question was how do you clear that stuff down there?
The answer is it's not a real terminal, but let's say you
are in a real terminal.
Clear.
Oh, it's a cat.
DAN BRADLEY: [INAUDIBLE]?
It's that one, right?
LEXI ROSS: No, that's all my stuff.
DAN BRADLEY: [INAUDIBLE].
LEXI ROSS: There we go.
DAN BRADLEY: Yeah?
>> AUDIENCE: Does clear work if you have--
like, say you run a program [INAUDIBLE] and it's asking
you for something, and you don't want to.
How do you--
DAN BRADLEY: Control+C?
LEXI ROSS: Yeah.
So if you're running a program and something is not going the
way you want it to and you just want to exit the whole
program, Control+C. Even if you're on a Mac, not Apple+C,
Control+C.
AUDIENCE: Did you have the correct version in the
revision in your profile?
DAN BRADLEY: I think it's saved, isn't it?
Yeah.
LEXI ROSS: This is the space.
Yeah.
I think it's Revision 11.
>> DAN BRADLEY: Yeah?
AUDIENCE: Does C make any assumptions about what a
variable is if you define a variable but don't assign
anything to it immediately?
DAN BRADLEY: So it'll only accept ints into that spot.
So if you declare it--
AUDIENCE: If I just did int denominator semicolon, does it
assume that it's equal to zero or any number--?
DAN BRADLEY: Nope.
So if you don't assign, say, int denominator to any value,
it could be anything.
It's the old memory that was in that location.
So it could be negative 2 billion, it could be positive
2 billion, and it could be anywhere in between.
So if you're actually running things, make sure that you
assign a value to the variables.
This, it doesn't matter because we're immediately
assigning the value, but generally speaking, yeah.
Any other questions?
>> LEXI ROSS: We can also just take general questions at this
point on lecture material, p set material, anything you've
been wondering about, this is a good time to ask those.
AUDIENCE: So in the walkthroughs, Zamyla
mentioned the round function.
Would you mind elaborating on how to implement that?
LEXI ROSS: Sure, how to implement that?
AUDIENCE: She said it was round.
LEXI ROSS: Oh yeah.
So just so we all have the same vocab and stuff,
implement generally means actually writing from scratch.
And so I don't think she was talking about sort of writing
the round function.
She probably just meant how to use it.
Implement means sort of like create.
So generally, a time when we want to use the round function is I
mean, sort of, for instance,
if you want to round.
Can you give me a little bit more context as to what the
problem was?
AUDIENCE: To avoid the imprecisions of decimals
by making them a float.
LEXI ROSS: Yeah.
So the issue with floats that we've kind of alluded to is
that because of the way they're stored in the machine,
they're not 100% precise in the way that you might think
your calculator would store them.
It's hard for me to show an example without knowing what
the context was from.
I don't know, can you think of something, Dan?
>> DAN BRADLEY: So if you're asking for, say, numbers
greater than 10 and you're using floats, and you're adding one
to a float every time, it will sometimes wait until you've
gotten to 12 before it'll say this is actually a number
greater than 10.
Floats have some serious issues just between the 32nd
and 33rd decimal place that'll go, so it might 9.9999999, or
it might be 10.0000001, right?
It doesn't really distinguish between the two, so if you're
waiting for things greater than or equal to 10, you might
not get there at what you think is the 10th iteration.
You might get there in the 11th.
LEXI ROSS: Essentially, yeah.
When you're looping over a lot of floats, that's when the
imprecisions happen.
AUDIENCE: I think, so basically, it's like on the p
set, it wants you to round the float value, and if you put in
4.24 for the amount of change you need back, if you just do
times 100, it'll actually change into 423--
DAN BRADLEY: Sometimes, yeah.
AUDIENCE: So I think it's just like, how do you use the round
function in the actual code to make it so it doesn't do that?
DAN BRADLEY: So if you're rounding 424 by multiplying it
by 100 and making it into an integer, it will sometimes
make it 423 because of that imprecision.
And so things you can do is just add 0.0000003.
AUDIENCE: But I think, actually, they want
us to use the round.
DAN BRADLEY: The round function?
There's probably a math function, something in the
math library.
AUDIENCE: #include math.
DAN BRADLEY: #include , and there's
probably a round function.
AUDIENCE: Just like round()--?
LEXI ROSS: Essentially, because you want to round to
the nearest cent.
DAN BRADLEY: Let's pull up the man pages.
So a handy tool, if you don't know--
you pull up the terminal--
is if you go to man math.
LEXI ROSS: Cool.
>> So you can see, so man is short for manual, not man, and
it allows you to look up more information about any given
function or library that you might be dealing with.
So I just typed in man math.
Let me zoom in.
DAN BRADLEY: We can try man round, too.
LEXI ROSS: Sure.
DAN BRADLEY: And this will list out--
man math will list everything that's in the math library,
which we'll include.
Yeah, we also have round.
LEXI ROSS: So I just did man round.
You can see here in the synopsis, it says #include
, so that's kind of like cs50.h, stdio.h.
And it looks like it's actually overloaded, meaning
it takes different types of arguments, so you can round
doubles or floats.
DAN BRADLEY: Round versus roundf.
LEXI ROSS: Roundf.
Did Zamyla mention roundf versus round?
AUDIENCE: In the walkthrough, it was just round().
DAN BRADLEY: Okay, so she probably was using the double,
then, as opposed to a float.
Okay, so you would multiply by 100 and then round it, and
you'd get the number closest to it, I think.
>> LEXI ROSS: So the round function returns the integral
value nearest to x rounding halfway cases away from 0,
regardless of the current rounding direction.
So essentially, you would want to only round once you're at
an integer value, so you multiply by
100 and then round.
Does that make more sense?
AUDIENCE: To use that function in a C file, do you need to
put round float, like if your variable is called q, do you
put round (float) q, and then a parentheses, or can
you just put round, and then q in parentheses?
DAN BRADLEY: So if you were trying to use this function
actually, you would take that q, is q a float,
or how is it declared?
AUDIENCE: Q is a float.
DAN BRADLEY: So if q is declared as a float, you would
just take round, and then give it q.
You might have to declare it as a double in this case.
LEXI ROSS: Yeah, unless you want to use roundf.
AUDIENCE: I could just use roundf, and then q in quotes?
DAN BRADLEY: No, not in quotes, just q.
AUDIENCE: Sorry, not in quotes, in the parentheses.
DAN BRADLEY: In the parentheses, yeah.
LEXI ROSS: Sorry that was a little bit confusing.
Any more questions?
>> DAN BRADLEY: Yeah?
AUDIENCE: So round only rounds to integers?
LEXI ROSS: Yes.
DAN BRADLEY: Yeah, round only rounds to integers.
AUDIENCE: So if you wanted to have something exact that
needed to be [INAUDIBLE] multiplied by 100 and then
divided by 100 again if you wanted to--?
LEXI ROSS: So if you wanted to get it into a decimal form?
AUDIENCE: Well the example, for instance, [INAUDIBLE] you
have 1.25, and then [INAUDIBLE]
1.24, if you wanted to keep that as 1.25, you would
multiply that by 100, round it, and then divide it by 100
again to get 1.25?
LEXI ROSS: Yeah.
So the question is, since rounding, as the man page
says, rounds to the nearest integral value, does that mean
that one would want to multiply by 100, do the
rounding, and divide by 100 again to, say, get the correct
dollars and cents value?
And the answer is--?
DAN BRADLEY: No.
I mean, if you're just getting a float value, if you start
with a float value of 1.25, that's how it's going to be
interpreted.
So if you multiply it by 100 and then divide it by 100,
it's still going to be interpreted as 1.25, or
possibly 1.249...
..999.
LEXI ROSS: I think the question was like, you would
multiply, then use round, then divide, so it's like--
DAN BRADLEY: Yeah.
I still don't think it changes anything, though.
I think it's still.
LEXI ROSS: Because round does-- it uses integers.
So if you try to round 1.25, you just get 1.
So I think in order to get like the additional precision,
if that's what you're talking about.
>> AUDIENCE: So in what instances do you use round, then?
DAN BRADLEY: So in this case, if you want, say, the number
of cents from $1.25, you'd multiply by 100, round, get
125, and leave it as that, as cents, as opposed to trying to
convert it back into dollars and cents.
LEXI ROSS: Oh yeah, yeah, for sure.
AUDIENCE: Why do you need to round that [INAUDIBLE]?
DAN BRADLEY: So if you multiply it by 100 and then
immediately convert it to an integer, which is the more
useful form, that's when you deal with that
imprecision, right?
So it might be 124.999999, and so if you then convert that to
an integer, it just drops everything off that's below
the decimal.
AUDIENCE: So it'll give you [INAUDIBLE].
DAN BRADLEY: Exactly, instead of 125, which is
why you want to round.
>> AUDIENCE: Sorry, could you repeat what you said you
should do as opposed to what you shouldn't do?
DAN BRADLEY: Okay.
So what you should do is if you start with 1.25, multiply
it by 100, and then round that whole number, that 125 or
124.9999, and get that to the nearest integer.
AUDIENCE: So if you round, that puts it
to the nearest integer?
DAN BRADLEY: Yeah.
AUDIENCE: Is there a way to limit, if you ask for a float
to be inputted, is there a way to limit how many decimal
places you can input a float to?
LEXI ROSS: Not using the CS50's implementation.
I mean, you can count how large the input is given by
the user, but that would require a new implementation
of GetFloat, and so you don't need to worry about that.
DAN BRADLEY: So if they, say, input 1.256, you could just
use 1.25, or you could do 1.26.
Either way, I think the implementation says you can--
AUDIENCE: You can just have it round to--
DAN BRADLEY: Whatever it wants, yeah, as long as you do
some sort of rounding.
Anything else?
Doesn't have to be about rounding.
LEXI ROSS: Yeah?
>> AUDIENCE: If we want to check to see if the style of our
code is appropriate or the best style
that it can be, is that the sort of thing we'd bring up in
section, or at office hours, or is that the sort of thing
we can post privately on the thing, on discussion, and then
ask for advice on?
LEXI ROSS: So we don't, at office hours or on CS50
Discuss, we won't actually grade your p set in advance
for style, but I think probably the most helpful
thing you could do would be to look at examples, and you
should be able to find that in section code, which should be
distributed online, as well as the lecture slides, there
should be examples of code.
But if you have individual questions about a certain
style issue, definitely feel free to ask that on CS50
Discuss, and office hours, or on your own time.
But if it's more a general look at my code, it's best to
kind of use examples and try to fit your code to that.
And I believe there is a style guide on--
DAN BRADLEY: There's the style guide.
The style guide's awesome.
It teaches you everything you need, and it covers pretty
much every example known to man, or at least known to me.
>> Anything else?
All right.
Cool, guys.
LEXI ROSS: Have a great day.
DAN BRADLEY: Thanks for coming.