>> JASON HIRSCHHORN: Welcome to CS50 section, everyone.
My name is Jason Hirschhorn.
I am a lot of yours TFs, but a lot of people will hopefully be joining us
online, extension students or students trying to look through section if they
missed their section that week or just catch up if they wanted
to review some material.
>> So as per the syllabus, which all of you have read, correct?
I see a lot of head nods.
Sections provide you with opportunities to explore the course's
material in a more intimate environment, as well as dive into
hands-on activities.
>> So that's basically what we're going to be doing every week.
We're going to go through what we have determined to be the most important
topics covered in lecture, and the ones probably also most useful and
most likely to come up on problem sets and in quizzes, and in your coding
life outside of CS50.
So we're going to go into those topics, discuss them, go over them,
answer any questions you guys have, and then we're going to do a lot of
practice problems in coding.
>> You all brought laptops, which is fantastic.
This is a computer science class.
However, a lot of the exercises we do, you won't need your laptops for.
You can do them on a sheet of paper.
That's how you're going to do them on the quizzes.
And indeed, that's a great way to really know that you how to do
something if you don't have Check 50 to rely on or Make to rely on, telling
you made any errors.
>> So you're welcome to code on the appliance or run.cs50.net, which is
another tool that allows you to code online, but I also would recommend, if
you haven't tried it, using a sheet of paper and trying these practice
problems on a sheet of paper.
Because again, that's ultimately what you're going to be
asked to do on quizzes.
>> So let's start with our agenda for today.
We're going to go over some norms for our section, then we're going to dive
into arrays, functions, and command line arguments, which is what you guys
have covered this past week in lecture and then Monday, and what you'll cover
more of tomorrow.
And then finally, we're going to talk about pset2.
If I could get a show of hands if you haven't looked at the problem set two
specification.
We'll say everyone.
Fantastic.
>> We'll talk about problem sets in a bit, but if you read the problem set
specification, that's all you need to do.
If you read that before you come to class and section on Tuesday, you will
be a great position.
We can go through the problem set, which is what we're going
to do at the end.
We're going to go through the logic, make sure
everybody's on the same page.
>> I want you to be able to leave Tuesday feeling very comfortable with what you
need to do for this week's problem set, and if you don't, I will happily
stay after and work with you.
But that is one thing that you have to do but I think would be really smart,
just to read the problem set specification by class on Tuesday,
which it sounds like a lot of you guys did.
>> I'll start with an agenda each class so you know where we're going.
This is also helpful for people watching online because each section
is color coded, so if you want to come back and just watch 10 minutes rather
than the whole 90-minute section, you can just go to the color coded section
and look at that part.
So I've tried to break them up into nice modules that will allow you to--
if you want to come back and review, or other students who are just
watching a section to review--
to find the specific thing they want to pay attention to
and learn more about.
Sweet.
>> Norms.
I'm a FOP leader on campus.
I don't know if anybody did FOP.
Did anybody do FOP?
One person did FOP.
Two people did FOP.
Awesome.
So FOP is the First year Outdoor Orientation Program.
We take freshmen into the woods and make the most magical experiences of
their lives, correct?
Yeah.
Sounds about right.
>> So I just wanted to start our very first section by discussing some norms
for the class and our relationship because I think
they're very important.
First and foremost, as those of you who are my section have received and
has been discussed in that email, I love this.
This is my favorite part of the year.
I've been so excited.
I've been here for hours today.
I haven't left this building except to go eat because I love CS50 section, I
love teaching, I love working with you guys.
>> One of my highest priorities, if not my number one priority, not only for
this class but including my other classes and extracurriculars, is to
support you guys, and help you guys, and do my best to make sure that you
guys have a wonderful, fun, productive, engaging, exciting
experience in this class.
That's what I'm committed to.
That's why I love to do this.
That is my number one thing, supporting you guys.
If you have any questions about lecture, the problem set, section,
life, even if you want to talk about life, please feel free to reach out.
>> You have my cell phone number, you have my email
address, you have my GChat.
You can contact me at any or all of those whenever, but probably won't get
a response whenever.
I try and get back within 24 hours.
Oftentimes it will be within 20 minutes or 24 minutes.
I should have said that to make it parallel.
But you will get a response pretty quickly.
>> If you text me an hour before the problem set's due, I will calm you
down and do deep breathing exercises with you, but if you haven't started,
you're probably not going to finish, and I can't really help you out there
even though I'll try my best.
So I will do everything I can to support you.
Please feel free to reach out whether it's in section again, at Office Hours
if you see me, or even outside.
I'm happy to set up a time to meet and work through problems
with or go over material.
So that is number one, support.
I will do everything I can to support you.
>> Number two, meeting in the middle.
As much as I would like to be, I'm not omnipotent or all-powerful or
all-knowing, so I don't know all the time if what I'm doing is getting
across, if the way I'm teaching the topic helps you learn it, or if you
are confused on a topic but don't raise your hand or ask me
or send me an email.
Or even if you, again, are confused about something on the problem set.
If you don't reach out to me, I can't know that for sure.
I'm going to do my best to check in on you guys, make sure everything is
going well, try to teach things in a way that will be conducive to most of
your learning styles, but if there's anything I can do, let me know.
>> Along those lines, if you want some help with the problem set, I'm more
than happy to help you with the problem set.
We're going to do a lot of that towards the second
half of this section.
But if you haven't read the problem sets back and haven't watched the walk
through and haven't watched the shorts and expecting me to teach you
everything that has been videotaped online or that you can find at CS50
study, that's not really fair to me or to your other peers.
>> So please, I'm more than happy to help you, but you guys got to meet me in
the middle.
We have tons of resources for you available on CS50.
I mentioned some of them.
Please take advantage of them.
If you're confused, I'm more than happy to help you explain them in
another way, but it's not fair if students show up at office hours,
haven't started anything, and it's Thursday night and they're expecting
the TF to walk them through every single thing for the problem set due
at noon the next day.
Make sense?
Again, please meet us in the middle.
>> And finally, high expectations.
I'm going to be grading everybody who's in my section, or scoring your
problem sets.
You haven't gone back problem set zero or one yet.
We're sorry about that.
It took us awhile to finally settle sections, and now
we can start grading.
>> I'll get them back to you as soon as possible.
Those are some of my advisees out there.
I'll get them back to you as soon as possible.
And for problem set two, we'll be on schedule and we'll start getting them
to you pretty quickly after you turn them in.
>> When you get your problem set back, you will get scores, as you know from
reading the syllabus-- correctness, design, scope, and style scores.
You'll also get comments.
The comments are more important than the scores.
I think it's safe to say that.
>> Scores are just a number from one to five.
It's hard to read into those.
It's hard to improve from those.
But the comments will point out specific things you did well or areas
you can improve on and give you some ideas for the future or alternative
ways of doing things.
>> So please, please, please read through the comments.
I put a lot of time into those comments.
Please read through them and don't just scroll down to the bottom of the
PDF, look for your score, or go online and find your score, and then ignore
the comments.
That's not how you're going to get better.
That's not how you're going to increase your scores ultimately, just
by looking at the scores.
Please look at those comments.
>> And along the line of high expectations, again, like I said, you
can be scored from one through five.
Twos, threes, and fours are the vast majority of scores
you're going to be getting.
Ones and fives are both really rare.
There's always things we can do better.
Whenever I code a program, there's always something I
could have done better.
>> So rather than saying you get a five all the time on everything and did
everything perfect, I don't think any of us really believe that we-- or
hopefully, we don't do everything perfect all of the time.
So don't be worried if you didn't get a five.
A five does not map onto an A, a four does not map onto a B, a three does
not map onto a C. That's not how the scores work.
>> Like David has said many times, at semester's end all of your scores and
input from me, your TF, come together and determine a final grade for you.
But again, it's not five, A, four, B, et cetera.
So I'm going to have high expectations for you guys.
I'm going to expect good problem sets and I'm going to give
you feedback as such.
And it's not going to mean you're going to get a worse grade.
You are graded in terms of your progress from week
zero to course's end.
>> And finally, in terms of high expectations in class, I love when
people participate.
I understand some people feel more or less comfortable participating.
Other people are participating not by raising their hand and
talking all the time.
But we in this class are going to be doing a lot of small problems, a lot
of mini questions, and then some larger coding problems.
So definitely work these exercises out when we do them on your computer or on
a sheet of paper.
>> But I'll also give you some time before anybody ever has to answer some
of the questions I ask.
So because I will give you some time to think about it, because we're going
to do things in partners sometimes, I will expect everybody
to be engaged somewhat.
And for lack of a better term, there will probably be cold calling, which
is where everybody will have a chance to think about the answer, and then
I'll call on you, and then you'll say the answer.
>> Don't worry or be scared about that.
I've done it in the past and we've found that it's much more successful,
also read educational research on it to determine whether or not it was a
good tactic.
It is very helpful, especially if you give students time to think about the
answers and don't make them just come out and say it right off the bat.
But if you don't feel comfortable answering, I'm also pretty good at
sensing that out, and we're not going to put you on the spot, especially on
the camera, even though none of you are on camera right now.
>> So those are the norms I have.
One more thing along the lines of meeting in the middle.
I'm going to give you guys a lot of feedback.
Please give me feedback as well.
There's a website, sayat.me/cs50.
So it's an honest feedback.
If you want to give it during section, I get an email whenever it happens.
We'll take a break in a bit, so I'll look through the feedback then.
If you ever have any feedback, please give it to me there.
>> If you give me feedback on the Q, that's wonderful, but that's the guide
that you do at the end.
But that will be at semester's end so I won't be able to take any of that
feedback and turn it into teaching you guys better.
So please, if you have any feedback, give it to me.
I love feedback.
Back to FOP, we get feedback all the time.
>> You can give me pluses, good things, deltas, things I can improve upon.
You can comment on my outfits.
That won't be as helpful, but you're welcome to comment on any and
everything.
Please, please give me feedback.
I value it a lot and it means a lot to me.
>> One more norm, fun.
We're going to have a lot of fun.
Hopefully in this course, you'll have a lot of fun.
We'll have candy at section.
We'll have music.
We will be hopefully getting to know one another somewhat well, so I want
this to be a fun, engaging experience.
>> I know some of you have fallen asleep in the lectures you had earlier today,
so hopefully that won't be the case now.
And if it is the case now, just don't snore because that might
be caught on camera.
Sweet.
Any questions, comments, or concerns before I move on about anything we've
talked about?
Great.
>> So we're going to go into arrays first.
This is how you make an array.
We've seen this before.
You have a data type, you have a name, and then you have a size.
Right here, I create two arrays.
The first one is an array of characters, and it has 26 spaces.
The second one is an array of scores, and it has three
spaces for three integers.
Yes?
>> STUDENT: Are all of these slides available somewhere for us?
>> JASON HIRSCHHORN: Great question.
The question was, are all of these slides available?
Yes.
All of these slides will be available, and in the future, I'll email them to
you before section.
But today, because this is the first week, we had a late start.
So usually, you'll get these slides, but they will be available online and
I'll email them to you right after class.
Any other questions?
>> So again, arrays are boxes, for lack of a better word, that hold multiple
elements of one data type, and they're next to one another in memory.
And so again, this is how you can make two types of arrays.
>> So after you've made your array with that first line-- int scores, bracket
three, close bracket-- you want to initialize it.
You want to put some values in there.
And this is how you put some values in there, just choosing the indice and
writing the number.
Let's draw this on the board.
Can everybody see?
Everybody can see.
Sweet.
>> So that's going to be my array called Scores.
As you can see, it's size three.
There are three boxes in there.
Each one can fit what type of data type?
>> STUDENT: Integer.
>> JASON HIRSCHHORN: An integer in this one.
We wrote int scores, and the first word is the data type that's going to
go in the boxes in that array.
So I can fit three.
Why do I have zero up there on the board?
Why does it say score zero equals 1?
Remind me of your name.
>> STUDENT: Kurt.
>> JASON HIRSCHHORN: Kurt.
>> STUDENT: Yes.
>> JASON HIRSCHHORN: Why?
>> STUDENT: Because the first index is zero, we start counting at zero.
>> JASON HIRSCHHORN: So we start counting at zero.
That's exactly right.
This is the confusing part and will trip you up, especially when you're
trying to iterate through arrays, which we'll do in a bit.
We start counting at zero.
So if I want to refer to the first box, it's not box one.
Unfortunately, it's box zero.
The second one is box one, and in this case, the third one is box two.
>> What about box three?
Where is box three?
Or if I want to do scores, bracket three, close bracket, what's that?
So you said the null character.
Any other thoughts?
What is score bracket three bracket?
>> STUDENT: It doesn't really exist because you only defined as scores,
bracket three.
>> JASON HIRSCHHORN: So you say it doesn't really exist.
In some cases, when we're dealing with strings, generally we'll put a null
character at the end.
We'll get to that in a second.
But in this case, there is no third box here.
So the last box in any array is size minus 1.
So if we have an array of size n, the first box is at zero, the last one is
at n minus 1.
And in this case, it doesn't exist.
There is something here in memory but it's not ours.
We don't own it.
We didn't ask for it.
So if we try to access it, which we'll do in a bit, we're probably going to
get into trouble.
>> In this case, I'm initializing my array with three variables.
We're putting a one in box zero, a two in box one, and a three in box two.
Does anybody have any questions about initializing an array?
>> So we went over this just now.
Scores, bracket three, close bracket, that doesn't exist.
We're zero indexed in computer science, so we start counting at zero.
The last box available to us, it is the third box, but it's called box
number two.
>> Down below, this is an alternative way of initializing arrays.
So instead of creating the array on one line, filling the first box, then
the second box, then the third box, you can do it all in one line.
So in that line, both arrays.
This creates the same exact array as what we created right here, but it
just does it in one line.
>> Now we have created our array.
Usually once you create an array, you want to do something with it, whether
that's fill it or get things out of it and print them out on the screen.
In this case, we're going through our scores array, which we initialized on
the first line up here in gray.
And then on the second line, we start iterating through the array.
Does anybody have any questions about the structure of a for loop?
>> So this is the for loop.
We have our initial condition, and then our final condition, and then we
iterate, i, each loop.
And in the curly braces in this for loop, what happens if I were to run
this program?
Can anybody tell me what we would get?
[? Matu? ?]
>> MATU: Print all the scores on each line.
It would print each score on a separate line.
>> JASON HIRSCHHORN: What exactly would it print?
>> STUDENT: It would print one, then two, then three.
>> JASON HIRSCHHORN: Exactly.
It would print one, then two, then three, each on separate lines, because
we're going through the array and you'll see that second, the printf
statement, scores i.
So on the first round through our for loop, i equals zero, which means we're
getting the value at score zero--
which, if we go back to our diagram, is one--
and filling in--
oh, it says c.
That should be %d.
I will fix that in a bit when I hand that out to you guys.
But we're presenting an integer so it should be %d right here, not %c.
What is %c for?
>> STUDENT: Character.
>> JASON HIRSCHHORN: Character, exactly.
What is the other one?
If you don't want to do %d for an integer, what else can you do?
>> STUDENT: i.
>> JASON HIRSCHHORN: %i, exactly.
So any questions about iterating through the array?
So what happens if we do this?
Instead of doing i is less than three, we do i is less
than or equal to three.
What happens when we run this code?
Yes, Annie?
>> STUDENT: They're going to print out weird stuff on the fourth line.
>> JASON HIRSCHHORN: So you said, is it going to print out weird stuff on the
fourth line?
Does anybody have an answer to her question?
So the odds are that when we get to the--
so on the fourth round through our for loop, just like you thought, Annie,
it'll be scores bracket three, which we've already figured out doesn't
exist, and we're going to try and access that, print it
out, use it some way.
So the computer will probably get mad at us.
You're exactly right.
>> It might quit the program altogether.
It might print out something really weird.
You can't say for sure.
Odds are it'll probably get mad at you and just quit right away.
>> But this is a common mistake you'll have where you're just off by one at
the end, and this is a bug that will plague you throughout your coding.
One way I like to do is just on a whiteboard, go through each iteration
of the loop on hand or on paper to see what's happening, and then you
generally realize, oh wait, I shouldn't have gone to less than or
equal to three.
I should have stopped at two, which is what happens if your condition is less
than three.
Any questions so far?
Sweet.
>> So you all have your computers open.
We're going to give you two to three minutes to write a program that
creates an array with the integers one through five and then prints out each
integer on a new line.
And if you have any questions, raise your hand.
I'm happy to come by.
Like I said, let's take three minutes to write this.
Is everyone OK if I go to a new window and close this?
>> I'm sorry if you haven't had a chance to finish, but we're going to do it
together right now.
So up on the screen, I have #include cs50.h, #include stdio.h.
Can somebody tell me what those two lines do?
Michael?
>> STUDENT: They include the libraries that CS50 made and that Steve has
saved for you to use to make your life easier.
>> JASON HIRSCHHORN: So you said they include the libraries, one that CS50
made, cs50.h, and one that is just standard, comes with
the C package, stdio.h.
What do you mean include the libraries, though?
Can you be a bit more specific?
>> STUDENT: It makes it so that you can use certain code words to have a
function happen without having to go and make the function c yourself.
>> JASON HIRSCHHORN: Can you give me an example?
>> STUDENT: Like printf is one you can use.
>> JASON HIRSCHHORN: Printf.
So you said printf is one of those functions where rather than having to
write printf on your own, printf is included actually in stdio.h, so you
can just use it and assume that it will work.
In stdio.h, if you opened up that file, would it include how printf
works, all of the code for printf?
Why are you shaking your head?
>> STUDENT: Because the header file just includes the function declaration.
>> JASON HIRSCHHORN: Exactly right.
The header file just includes the function declaration.
So basically, when you compile this, hit Make Count, which we'll do in a
second, the compiler knows that if you use printf, printf exists.
It's going to be defined somewhere.
It's not a problem for you using it.
And then later on, it'll include the ones and zeros that actually contain
printf's implementation and merge them with your code.
How do you make sure it includes the ones and zeros for, say, get string?
>> STUDENT: Get string is in cs50.h.
>> JASON HIRSCHHORN: Right.
So that says get string exists, the function declaration.
But then the ones and zeros, the binary file that actually includes the
implementation of get string?
How do you make sure that goes with your program?
>> STUDENT: Pass the linker in the command line argument.
>> JASON HIRSCHHORN: Pass the linker in the command line argument.
How do you do that?
>> STUDENT: It's the -l line in the clang
>> JASON HIRSCHHORN: Exactly right.
You say -l cs50, right?
That includes the ones and zeros.
>> STUDENT: But Make does that for you, right?
>> JASON HIRSCHHORN: Exactly.
Make does that for you, but eventually, you won't have Make, or
you'll have to make a Make on your own, so that's a
good thing to remember.
If you were going to use one of these other libraries, not only do you have
to write #include and then the library.
You also need to link it in.
And those two things are different. -l cs50 is different
than #include cs50.h.
>> Then we have int main void curly braces, and now we're going to code
inside main.
The first line I have here is a comment that says, create an array
with ints one through five.
What is a good word to describe what I have right now on the screen, just
comments inside main?
What do we call that?
>> STUDENT: Pseudocode.
>> JASON HIRSCHHORN: Pseudocode.
Thank you, Jeffrey.
That is exactly right.
Pseudocode.
Have you guys seen pseudocode before?
Raise your hand if you've used pseudocode before problem set one.
About half of you.
>> I would highly recommend using pseudocode to break down problems into
smaller, more easy to bite off pieces.
Also, if you do the pseudocode, you have your comments already largely
built in, so you don't have to go back and try to figure out
what you were doing.
So can somebody give me this first line to create an array with ints one
through five?
You?
>> STUDENT: Int and then you can call whatever number, and then square
bracket five.
>> JASON HIRSCHHORN: So int numbers, square bracket five.
>> STUDENT: Equals, and then curly braces.
>> JASON HIRSCHHORN: Equals, curly braces.
>> STUDENT: One, two, three, four.
>> JASON HIRSCHHORN: One, two, three, four, five.
>> STUDENT: And then curly braces and then semicolon.
>> JASON HIRSCHHORN: And curly braces and semicolon.
Excellent.
Print out each int on a new line.
Can somebody else give me that code, please?
[? Matu? ?]
Go ahead.
>> STUDENT: For (
>> JASON HIRSCHHORN: For (
>> STUDENT: Int i=0
>> JASON HIRSCHHORN: Int i=0
>> STUDENT: Semicolon i less than five.
>> JASON HIRSCHHORN: Semicolon, i less than 5.
>> STUDENT: Semicolon, i++.
>> JASON HIRSCHHORN: Semicolon, i++.
>> STUDENT: Closed parentheses, then open brace.
>> JASON HIRSCHHORN: Closed parentheses and two braces.
Inside the braces?
>> STUDENT: And then printf "%d/n"
>> JASON HIRSCHHORN: Sorry, hold on.
Printf "%d/n, closed quote.
>> STUDENT: And then closed quote, comma, count, bracket i.
>> JASON HIRSCHHORN: Comma, count, bracket i.
>> STUDENT: Sorry, numbers bracket i.
>> JASON HIRSCHHORN: Numbers, bracket i.
>> STUDENT: Sorry.
And then closed parentheses, semicolon.
>> JASON HIRSCHHORN: Closed parentheses, semicolon.
I'm going to save that.
We're going to make count.
And how do I run count?
>> STUDENT: Backslash.
>> STUDENT: Dot forward slash--
>> JASON HIRSCHHORN: ./count, exactly.
Thank you. ./count.
One, two, three, four, five on new lines.
Looks like it worked.
Yes, Noah?
>> STUDENT: You could also do less than or equal to four, like i is less than
or equal to four?
Is there any advantage to doing less than five, or just a different way of
writing it?
>> JASON HIRSCHHORN: So the question is you could have done i is less than or
equal to four instead of i is less than five.
Would that have worked the same?
>> STUDENT: It should.
>> JASON HIRSCHHORN: Yes.
It totally would have work the same.
Stylistically, it's generally less than, and you'll see in a second when
we do strings, you can use something called strlen, which you've seen
before, to get the length of the string.
And you can't do less than or equal strlen.
You could do less than or equal strlen minus one.
So it's just nicer to do less than strlen.
Yes?
>> STUDENT: And in the actual quizzes, do we have to provide comments on our
code as well?
>> JASON HIRSCHHORN: The question was in quizzes, do you
have to provide comments?
So we'll talk more about quizzes when they come up, but generally, a good
rule of thumb is whenever you're writing code, provide comments.
If you get the code wrong but the comment right, you might get some
credit both from me if I was grading you on a problem set or on a quiz.
So comments are always good to provide.
Sweet.
Count.
Excellent job.
Let's move on.
>> Strings.
Strings.
We've seen this before in lecture.
This blew my mind when I first found this out.
It probably didn't blow anybody's mind here, but that's OK.
>> Strings are arrays of characters, and they end with, as you said earlier,
the null terminator, which is this /0 character.
If we're going to draw a string on the board, we'll draw it over here.
So how long is this string?
Sorry.
How long is this string?
>> STUDENT: [INAUDIBLE].
>> JASON HIRSCHHORN: Four?
Why is it four?
>> STUDENT: Because the fifth spot is for the null character.
>> JASON HIRSCHHORN: So the fifth spot is for the null character.
So if I were to run strlen on this, it would give me four.
Correct.
And the array, though, if I were to initialize this array, how many spaces
would I have to ask for?
>> STUDENT: Five.
>> JASON HIRSCHHORN: Five.
I would have to ask for five, but I can only put four characters in here
because I need to put that null terminator on the end.
So we're going to write in "ohai" and put the null terminator in at the end.
We'll say this is string s equals this.
What is s bracket zero?
s bracket zero, Annie?
>> STUDENT: O.
>> JASON HIRSCHHORN: O, exactly. s bracket one?
s bracket five?
>> STUDENT: Doesn't exist.
>> JASON HIRSCHHORN: Doesn't exist.
Exactly right.
Cool.
Any questions about understanding strings as arrays of characters?
OK, then.
One more program for you guys.
Write a program that asks the user for a string then prints out each
character on a new line.
So let's take three minutes to write this program, and then we'll code it
as a group.
>> Sorry.
Did you need to see this?
>> STUDENT: No.
>> JASON HIRSCHHORN: OK.
Let's take one more minute to code this, or at least get started on it.
>> So you can keep working, but I'm going to start going through it.
So like before, I have on the screen #include cs50.h, #include stdio.h.
int main void.
Inside main's curly braces, I have two comments, pseudocode.
One is ask for a string from the user.
The second is print out each character on a new line.
So Anna, can you give me ask for a string from the user?
>> STUDENT: Printf.
>> JASON HIRSCHHORN: Printf?
>> STUDENT: Yes.
(
>> JASON HIRSCHHORN: (
>> STUDENT: "give me a string.
>> JASON HIRSCHHORN: "give me a str-- oops--
string.
>> STUDENT: /n") semicolon.
>> JASON HIRSCHHORN: End the string, closed parentheses, semicolon.
>> STUDENT: Yes.
And then next line, string s = get string.
>> JASON HIRSCHHORN: String s = get string.
>> STUDENT: Yes.
And then ( )
>> JASON HIRSCHHORN: ( )
>> STUDENT: Semicolon.
>> JASON HIRSCHHORN: Semicolon.
Fantastic.
So I'm going to get a string and I'm going to save it in what?
What's the variable called that you created?
>> STUDENT: String s.
>> JASON HIRSCHHORN: The variable is called s.
It's type string.
Exactly.
Next thing we want to is print out each character on a new line.
Who can give me that code?
[INAUDIBLE]?
Can you give it to me?
>> STUDENT: Sure.
>> JASON HIRSCHHORN: Let's do it.
And sorry.
I don't like to repeat what you say because you're getting it right, but I
have to repeat it so that when they transcribe this, they can get all of
the text out on the transcriptions.
So you can just keep talking.
Unfortunately, I have to say it over you.
I'm sorry, but--
>> STUDENT: That's fine.
For int i=0
>> JASON HIRSCHHORN: For int i=0
>> STUDENT: I less than--
>> JASON HIRSCHHORN: Semicolon.
>> STUDENT: Before this, can we put a variable so you get int name length?
>> JASON HIRSCHHORN: Oh, you don't want to put the semicolon?
OK.
So what do you want to do after zero?
>> STUDENT: I want to get the string length of s and
set that to a variable?
>> JASON HIRSCHHORN: OK, so how do I do that?
>> STUDENT: So you do int length =
>> JASON HIRSCHHORN: Int length =
>> STUDENT: S-T-R-L-E-N.
>> JASON HIRSCHHORN: S-T-R-L-E-N--
>> STUDENT: Of s.
Brackets.
>> JASON HIRSCHHORN: (s).
>> STUDENT: And then I think you have to put a semicolon between the two ints.
>> JASON HIRSCHHORN: A semicolon after zero?
>> STUDENT: Yeah.
Then put a semicolon after that, and you say, i less than length.
i less than length, which is the variable.
>> STUDENT: And then semicolon.
>> JASON HIRSCHHORN: Semicolon.
>> STUDENT: i++.
>> JASON HIRSCHHORN: i++.
There's our for loop.
OK, inside the for loop.
>> STUDENT: And then do printf.
>> JASON HIRSCHHORN: Printf.
>> STUDENT: Brackets.
>> JASON HIRSCHHORN: Parentheses, quotes.
>> STUDENT: Parentheses, quotes, %c/n.
>> JASON HIRSCHHORN: %c, OK.
>> STUDENT: /n.
>> JASON HIRSCHHORN: /n.
>> STUDENT: Closed quotes.
>> JASON HIRSCHHORN: Closed quotes.
>> STUDENT: Comma.
>> JASON HIRSCHHORN: Comma.
>> STUDENT: S.
>> JASON HIRSCHHORN: S.
>> STUDENT: Bracket i.
>> JASON HIRSCHHORN: Bracket i.
>> STUDENT: And then closed parentheses.
>> JASON HIRSCHHORN: Closed parentheses.
>> STUDENT: And then semicolon.
>> JASON HIRSCHHORN: Semicolon.
Fantastic.
So let's save this.
Make spell.
Oh boy.
I've got a couple of errors.
First error on line 11, it says, expected expression, and it's giving
me an error that the second int, there's the error.
There some error around there.
Does anybody know what error that is?
Kurt?
>> STUDENT: I think it's supposed to be a comma and not a semicolon.
>> JASON HIRSCHHORN: OK, a comma instead of a semicolon.
So is that all we need to fix?
>> STUDENT: No.
>> JASON HIRSCHHORN: What else do we need to fix?
>> STUDENT: We have to include string.h.
>> JASON HIRSCHHORN: Include string.h.
Where do I need to include that?
>> STUDENT: Same place you're including the other stuff, right
after include stdio.h.
>> JASON HIRSCHHORN: Let's see if this runs first because generally, I like
to fix one thing first, and then run it, and then see the errors we get.
So we're going to clear the terminal with control n, let's do make spell.
So another error on line 11.
Expected semicolon.
And I'm still getting errors on this line.
So it hasn't given me an error saying it doesn't understand what strlen is,
but it is giving me errors on this line, so what else is wrong?
Mark?
Marcus.
>> STUDENT: Do you have to remove the second int declaration?
>> JASON HIRSCHHORN: Remove the second int.
Let's try this.
Now clear the terminal.
Make spell.
Error.
Implicitly declaring library function strlen.
So now we do see that error.
What do we need to include?
>> STUDENT: String.h.
>> JASON HIRSCHHORN: Include string.h, we'll do #include string.h at the top
of the screen.
Let's save, make spell.
Now compiled.
So again, the things we needed to remember line 12 in our for loop when
we're setting it up, int i=0, length = strlen (s).
>> This is a fantastic strategy.
The style of this is great, the design of this is great.
We can go into those reasons later, but this way you only need to call
strlen once instead of every single time you run through the loop.
However, you need to use a comma there and you don't need int again.
If you're declaring both those variables together, you
don't need int again.
And then if you're going to use strlen, which is another library
function not included in stdio.h or cs50.h, make
sure you include string.h.
Kurt?
>> STUDENT: So then if you don't need to say what type length is, does that
mean it has to be an int?
You couldn't have two different types there?
>> JASON HIRSCHHORN: So you could have two different types.
Length is an int here, though.
Length is an int because it's comma and then the same thing.
>> STUDENT: So it assumes that it's going to be the same as the first one?
>> JASON HIRSCHHORN: Exactly.
We're not going to do this now, but you can include two variables on the
same line using one data type.
So int x, y would create two variables.
So int x, y, if we make that, it gives me an unused variable x and y because
we don't use them.
So putting void before is just a fancy trick if you don't use
something, to use it.
But as you see, I create x and y on the same line with a comma.
It's creating two integers right there.
So yes, totally possible to create two things on the same line
separated by a comma.
Let's get rid of those, though.
>> So there's spell.
Thank you guys for coding it for us.
Let's make it one more time, run it.
Give me a string.
[INAUDIBLE], you did most of it.
What's the string?
>> STUDENT: Hello.
>> JASON HIRSCHHORN: Hello.
H-E-L-L-O, each character on one line.
Looks great.
Let's run it again.
Always going to check for corner cases.
What's a good corner case to check?
>> STUDENT: A number?
>> JASON HIRSCHHORN: A number.
So I'll put in 1, and it prints out 1.
It's not asking for letters, so it seems like numbers work.
Good corner case to check.
What's another good corner case to check?
>> STUDENT: A blank string.
>> JASON HIRSCHHORN: Hm?
>> STUDENT: Like an empty string.
>> JASON HIRSCHHORN: Empty string.
So let's not put in anything.
And what gets printed out?
>> STUDENT: Nothing.
>> JASON HIRSCHHORN: Nothing.
Seems like it works.
You could try more corner cases, but generally numbers, a different data is
great to try, as well is nothing.
Nothing, zero, negatives are always great things to try and things you
should test, things certainly we'll test for and Check 50 will test for.
Any questions about spell.c?
>> OK, let's head back.
I have one more program for you guys to write.
It's called students.c.
This is a program that asks the user for five names, then randomly chooses
and prints out one of the names.
This program is a little bit more involved than the last two we wrote,
so why don't we take five minutes to code it individually, and then we'll
code it as a group.
>> This program also involve some things you may not have seen before, so if
you have a question about, how do I do that, raise your hand.
I'll come over and point you in the right direction.
But we're also doing it because this will be a good way to introduce some
topics that are tricky.
>> So it works, right?
>> STUDENT: Yeah.
>> JASON HIRSCHHORN: Whenever you turn in a problem set, there's many ways to do
it, but some are more elegant than others.
Let us dive in.
#include cs50.h.
#include stdio.h.
int main void, curly braces.
Inside the curly braces, I have two comments.
The first one is, ask the user for five names.
The second one is, randomly choose and print out one of the names.
So let's do the first one, ask the user for five names.
Who has asked the user for five names and can tell me how to do that?
Only two people did the first part.
Three people did the first part.
Four, five.
Tom, how do you do it?
>> STUDENT: You would declare a string and then name, bracket five.
>> JASON HIRSCHHORN: String name bracket five.
So what is this line doing, string name bracket five?
>> STUDENT: You would declare an array of strings.
>> JASON HIRSCHHORN: This is an array of strings.
How many strings are going to fit in here?
>> STUDENT: Five.
>> JASON HIRSCHHORN: Five.
Excellent.
We're going to call it Names because we're going to get multiple ones.
OK, next?
>> STUDENT: And then you have a for loop--
>> JASON HIRSCHHORN: For loop.
>> STUDENT: --for i=0.
>> JASON HIRSCHHORN: For (int i=0.
>> STUDENT: Semicolon.
>> JASON HIRSCHHORN: Semicolon.
>> STUDENT: I less than five.
>> JASON HIRSCHHORN: I less than five.
>> STUDENT: Semicolon.
>> JASON HIRSCHHORN: Semicolon.
>> STUDENT: i++.
>> JASON HIRSCHHORN: i++.
>> STUDENT: And then closed parentheses.
>> JASON HIRSCHHORN: Closed parentheses, and then some curly braces.
Inside the curly braces?
>> STUDENT: And then printf.
>> JASON HIRSCHHORN: Printf.
>> STUDENT: Open parentheses, name %d.
>> JASON HIRSCHHORN: Sorry?
>> STUDENT: Name.
>> JASON HIRSCHHORN: Name.
>> STUDENT: And then space, and then %d.
>> JASON HIRSCHHORN: Name, space, %d.
>> STUDENT: And then /n, and then closed.
>> JASON HIRSCHHORN: So name, space, %d/n, closed parentheses.
>> STUDENT: Comma, i + 1.
I'm sorry.
Closed quote, and then comma, i +1.
>> JASON HIRSCHHORN: Closed quote, comma, i+1.
>> STUDENT: Yes.
And then the next line--
>> JASON HIRSCHHORN: i + 1.
>> STUDENT: Yes.
And then the next slide is names bracket i.
>> JASON HIRSCHHORN: Next line is names bracket i.
>> STUDENT: = get string.
>> JASON HIRSCHHORN: = Get--
capital G-E-T--
string.
Oops, sorry.
>> STUDENT: Open, closed parentheses and then--
>> JASON HIRSCHHORN: Open, closed paren.
>> STUDENT: Semicolon.
>> JASON HIRSCHHORN: Semicolon.
OK, so you initialize the array of strings, names, and then go through a
for loop, and five times print out some prompt to the user and then store
five strings.
Does anybody have any adjustments to this?
Tom, I think everybody agrees with you.
Looks pretty good.
OK.
Why did you do i+1, though?
>> STUDENT: I mean, doesn't have to be, but i+1 so that it's
name one to name five.
Otherwise, it would be name zero to name four.
>> JASON HIRSCHHORN: So you did i+1 because otherwise it would have
printed out zero through four because that's how computers think, but humans
don't really read like that.
That is another thing you'll encounter.
Sometimes when you print out something to the user, you'll want to make it a
little bit prettier, and doing i+1 in this case is a
way to make it prettier.
>> I also see five twice, string name is five, i is less than five.
I know where you got the five.
You got it from me telling you five names.
But if we wanted to make the code a little bit more robust, one thing we
could do is out here, outside of int, we could do something like this,
#define, and we could do NAMES 5.
>> So basically, wherever I write capital N, capital A, capital M, capital E,
capital S, that string, it will replace that with 5 in the code.
Or let's just call it-- we'll just call it NUMBER.
So I'm defining all caps NUMBER to be 5.
>> So now wherever I have the number 5, I can replace it with all caps NUMBER.
And now, if I want to change the number of names, all I need to do is
this one line, line four, where I have #define number 5, change that to 10,
or change that to 4.
When your code gets longer, you're not going to remember all the places you
put how many names you wanted, and in order for you not to miss one, this is
a good way to do it, to find something up top and then just use that phrase
everywhere throughout your code.
Marcus?
>> STUDENT: Is that stylistically preferred than globally defining a
constant n?
>> JASON HIRSCHHORN: So is that stylistically preferred than globally
defining a constant n?
So you're saying you could do, int number = 5?
>> STUDENT: Do they have consonants in C or no?
>> JASON HIRSCHHORN: Or const-- in equal.
So you're saying just a global variable?
>> STUDENT: Yeah.
>> JASON HIRSCHHORN: So stylistically, there's no necessarily one right
answer, but both of those are better than just throwing these random
numbers throughout your code.
We call them magic numbers.
When I look at your code or when anybody's reading through it, they
don't want just these random numbers that don't make sense.
Even the number 26, which generally is the number of letters in the alphabet,
it's better to define ALPHABET up top and save the number 26 in that string,
if that makes sense.
>> Or on problem set one, rather than-- again, we didn't talk about this at
the time, but instead of just throwing in a 25 everywhere or a 10, you can
save the number 25 as QUARTER or 10 as DIME, and that makes it easier for
whoever's reading through your code to understand why you chose that number.
And also for you, if you ever want to update your code, it makes it a bit
more robust.
Yeah?
>> STUDENT: Is this code also going to be available for us after?
>> JASON HIRSCHHORN: All of these codes that we have written together are
going to be emailed to you guys and available.
Yes.
So ask the user for five names.
Randomly choose and print one of the names.
Who did this line?
This is the trickiest line here.
Sorry, Tom.
You did a really good job, but this is a bit trickier.
Marcus, go ahead.
>> STUDENT: Well first, you're going to need to include stdlib.h and time.h.
>> JASON HIRSCHHORN: So you're saying at the top, include two more libraries.
You want me to include S-T-D-L-I-B dot H, Standard Library, and what
was the other one?
>> STUDENT: Time.h.
>> JASON HIRSCHHORN: #include time.h.
So these are two more libraries.
I assume now we're going to be using functions that are contained in these
two, which is why you needed to include them?
>> STUDENT: So then, first you're going to want to write srand.
>> JASON HIRSCHHORN: Srand.
>> STUDENT: (time(0)
>> JASON HIRSCHHORN: Sorry.
Srand (time(
>> STUDENT: 0.
>> JASON HIRSCHHORN: 0.
>> STUDENT: )), semicolon.
>> JASON HIRSCHHORN: Another closed paren?
Semicolon.
I'm going to just replace your zero with NULL.
It'll do about the same thing, but generally, you'll see NULL, which
we'll talk about this line in a second.
>> STUDENT: Stylistically better?
>> JASON HIRSCHHORN: Generally, people right NULL, but same thing.
>> STUDENT: And then int name.
>> JASON HIRSCHHORN: Int name.
>> STUDENT: =
>> JASON HIRSCHHORN: =
>> STUDENT: Rand.
>> JASON HIRSCHHORN: Rand.
>> STUDENT: ()
>> JASON HIRSCHHORN: ()
>> STUDENT: Modulo number.
>> JASON HIRSCHHORN: What's modulo?
The character?
The percent character?
>> STUDENT: Percent character.
Modulo NUMBER that you defined up top.
>> JASON HIRSCHHORN: Modulo NUMBER.
>> STUDENT: And then semicolon.
>> JASON HIRSCHHORN: Semicolon.
>> STUDENT: And then new line, printf.
>> JASON HIRSCHHORN: Printf.
>> STUDENT: (
>> JASON HIRSCHHORN: (
>> STUDENT: "
>> JASON HIRSCHHORN: "
>> STUDENT: %s/n
>> JASON HIRSCHHORN: %s/n
>> STUDENT: "
>> JASON HIRSCHHORN: %s/n"
>> STUDENT: Comma, space names.
>> JASON HIRSCHHORN: Space--
>> STUDENT: Oh, I guess, what did we call it here?
>> JASON HIRSCHHORN: We called it names.
>> STUDENT: Open bracket, name.
>> JASON HIRSCHHORN: Open bracket, name.
>> STUDENT: Closed bracket.
>> JASON HIRSCHHORN: Closed bracket.
>> STUDENT: )
>> JASON HIRSCHHORN: )
>> STUDENT: Semicolon.
>> JASON HIRSCHHORN: Semicolon.
OK, let's save.
Let's make this code.
Students.
Let's run this code.
Students.
Not ./student. ./students.
Name one.
We'll say David.
Name two, RJ.
Name three, Rob.
Name four, Lucas.
Name five, Joseph.
And then it prints out Joseph again.
>> So let me run this again.
Let me do the same names.
David, RJ, Rob, Lucas, Joseph.
And it gave me David this time.
So I did the same thing, but it gave me a different thing, and that's
because, of course, we're asking for a random name.
So I think, based off of me wandering around the room, that most people
probably got the first part right, setting up that array and then getting
all the names.
I'm talking about lines 10 through 16.
But lines 18 through 21 are probably some things you haven't seen before,
so let's go through those.
>> This line, srand time NULL is essentially--
sorry, let's skip down a line, rand ().
That gives you a random number, a random integer.
And if you hit man rand, which a lot of you did--
M-A-N space R-A-N-D--
in the terminal like this, M-A-N space R-A-N-D, hit Enter.
You would come up to the manual and would be able to read that the rand
command outputs num pseudo random bytes after seeding the random number
generator once.
>> So that's a description.
That is what rand does, gives you a random number when
you call that function.
Unfortunately, it's not really random because if you just call random
without getting it some seed, some starting point, it will always choose
the same one and it uses a formula to determine the next one.
So if you call rand, it'll just give you the same thing.
It will be very predictable.
It actually won't be random.
>> So what we need to do, per Marcus's instructions to me, were seed rand
with something else, which is what the srand function does.
It seeds rand with something a bit more random than the same thing, and
he's seeding it with time NULL, which is, just by convention, the current
time in seconds.
>> So you're assuming that everybody's not going to run the code at the exact
same second, so it's a safe bet that time NULL is going to be different
every time this code is run.
Safe bet that thus, srand will seed rand with something different every
single time.
So by convention, when we do want a random number, we write this line,
srand time NULL, to give us a much more random seed, and then we run
rand, which gives us a random number.
Yeah?
>> STUDENT: Can you say what you mean by seed?
>> JASON HIRSCHHORN: Seed as in--
so rand is going to pick some number and it's going to use some
formula to get there.
So it's going to start with something and then turn it into something based
off of that formula.
If you always give it the same thing, it's always going to turn it into the
same thing.
So that first thing you give it is the seed.
So you want to give it something different to start out with, and so it
will turn it into something different.
>> And then finally, line 20, this % is called the
modulo, and that is confusing.
That's something that is a little bit different here that you don't really
have in real life math.
But essentially, when you take a giant number and you divide in C, what
happens to the remainder?
>> STUDENT: It gets truncated.
>> JASON HIRSCHHORN: It gets truncated.
So 20 divided by 3 is what?
What does that return to you, 20 divided by 3.
Sorry, 10 divided by 3.
What does that return to you?
>> STUDENT: [INAUDIBLE].
>> JASON HIRSCHHORN: 10 divided by 3 just returns 3, not 3 remainder 1.
So what 10 modulo 3 returns is just the remainder, 1.
So if you wanted to know what 10 divided by 3 actually was, you would
do 10 divided by 3, you'd get your 3, and then you'd do 10 modulo 3, and
you'd get a 1.
So you'd realize that 10 divided by 3 is 3, remainder 1.
>> And in this line, rand could be any integer, essentially.
So modulo is saying, divide it by number, which is in this case 5.
And if you ever divide by a number, the integer always has to be less than
that number.
So if you're dividing by a number 5, the remainder is always going to be
zero through four.
That's just how the math works out.
>> So a large number moduloed by a smaller number will always return
remainder and will always return some number that's less than the number you
divided by.
Does anybody have any questions about modulo, because it will come in very,
very handy, so I want to make sure everybody understands it?
Cool.
And we saw that this program worked.
OK, moving on.
>> So next, we're going to go into functions, and we're going to go
through the rest of this stuff pretty quickly because I want to get to
command line arguments.
So functions are essentially, as you've seen, black boxes.
They take things in called parameters, they do something to them, either
change them or create some side effects, and finally, they spit
something out, which is the return value.
Can anybody give me one reason why we would use functions?
Anybody?
One reason why we'd use functions?
Anyone?
I saw two hands.
What's a good reason?
Marcus?
>> STUDENT: If you have to call the same chunk of code multiple times.
>> JASON HIRSCHHORN: OK, if you need to call the same chunk of
code multiple times.
Jeff, did you have a different reason?
>> STUDENT: Just so that you can code less.
>> JASON HIRSCHHORN: You can code less.
Right.
If you want to do something a lot--
in the previous code, I could have written get string five times, but a
for loop makes it look nicer and you have to write less code.
Functions are a similar idea.
If I want to do the same thing a lot, move it to a function and call that
function each time.
That will do that thing for you.
That's a great reason to use functions.
>> So anatomy of a function.
A function will generally look like this.
It'll start with the return type, then you'll get the name.
It'll take the arguments, otherwise known as the parameters, and inside
those curly braces, you'll write your code.
>> We've seen one function a lot.
That's main.
Here is main superimposed onto that anatomy I had earlier.
What does int right here signify?
What does int mean?
When I write int main (void).
>> STUDENT: Return type?
>> JASON HIRSCHHORN: Sorry?
>> STUDENT: The return type.
>> JASON HIRSCHHORN: Int is the return type, exactly right.
Main returns an int.
What is main?
>> STUDENT: Function name.
>> JASON HIRSCHHORN: Main is the name of the function.
Exactly right.
And void?
What is void inside the parentheses?
>> STUDENT: It doesn't take any external value.
>> JASON HIRSCHHORN: So void--
right.
Inside the parentheses, we put the parameters or the arguments the
function takes, what goes inside the black box.
When you write void, you're saying this takes nothing in.
So int main void means main's not taking anything in.
>> Inside the curly braces, we have some code, and then we see at the end
return zero.
What does return zero mean?
Well, we talked about return type.
Main needs to return an int.
That's what this line does.
It returns an int, zero.
>> Of course, we haven't seen that a lot because in the programming we do with
the machines we do and the compilers we use nowadays, you
don't need that line.
Main is special enough that it knows when you reach the end of main, just
return zero if everything went well.
In fact, if something doesn't go well, main will often return a different
number signifying that something went wrong with your program.
>> So generally when you see code, you don't write return zero, but it's good
to know that it's happening behind the scenes because whenever you write
another type of function, you are going to need to
include a return line.
If you're saying I'm going to return an int, you have to
write return something.
If you say void up here at the beginning, void main void, for
example, then it's not returning anything-- void means nothing--
so you don't need that return line.
But you ever write something that's not void for the return type, you need
a line that says return and you need to put something there
that matches the type.
Any questions about that?
Sweet.
>> Scope.
This is something we've touched on as well.
Every variable, as we know, has a certain scope, and that's basically
talking about where the variable may or may not be referenced.
And I like to say what happens in curly braces stays
inside the curly braces.
So for example, if we look at this code, we're defining a global variable
outside of main, int a, up here at the top.
We're calling another int a inside main.
We have some more curly braces, and we're calling another int a.
>> We have three a's in code.
These aren't the same thing.
I've created three distinct variables.
Then inside these inner curly braces, I say a = 4.
Which a is that talking about, the first one, the second one,
or the third one?
>> STUDENT: The third one.
>> JASON HIRSCHHORN: It's talking about the third one.
All of those a's actually can be talked about in this particular
instance because their scope all goes into these innermost curly braces.
However, when you call things the same name, there's something called hiding.
So you only deal with or fix or use the most recent variable, or the one
closest to you.
So inside here, this third a is closest to that line, a = 4, so it
affects the third one.
And the last line, a = 2, which a is that talking about, the first, second,
or third a?
>> STUDENT: Second.
>> JASON HIRSCHHORN: It's talking about the second a.
Of course, the third a is closest to it in the code, but the third a
doesn't exist outside those curly braces.
Once you get to that closed curly brace, it's like it's gone,
essentially.
So the only two you have left are those first two, and of course, now
it's closest to the second one.
>> So that's a good thing to remember about scope.
For example, I'm sure you've encountered this in a do while loop.
If you initialize the variable inside the do while loop and then you try to
access it outside, you probably got some warning like, this
variable does not exist.
That's because of the scope idea, that things only exist
inside those curly braces.
Curly braces are like a giant hug that you're giving to your code, is another
way to think about it.
>> So this is another function, something for you guys to code, hellofunction.c.
We're going to skip that for now, but this is something I encourage you to
practice on your own.
Write a program in which main calls another function that prints out a
greeting to the user.
It's essentially taking hello world and abstracting that printf into
another function.
But we're not going to do that right now because we don't have as much time
left as I would have liked.
>> So function declaration.
We've seen this before as well.
If you want to write another function, you need to let the computer know and
main know that it exists.
So what we do is above main, we write the return type, the name, and the
parameters, and then down below main, down here, we actually copy that line
and then write the actual code.
So this way, if you call the function in main at the top, main knows that
hello exists, for example, if I were to call hello, and then it looks to
the implementation down below.
What happens if I don't include this line up top, void hello (void)?
>> If I don't include this line and main calls hello, I'm
going to get an error.
I'm going to get an error because the compiler doesn't know
that this thing exists.
This is similar to why we do #include cs50.h.
What essentially cs50.h is doing is putting get string up there.
It's putting get int up there.
It's putting all of those function prototypes up before main so that when
you call them in main, main knows that these things exist.
That's what those .h files do.
And so if you're going to make your own function, you've got to make sure
you put that prototype up there.
Any questions about this so far?
>> Command line arguments.
This is what we're going to finish on, and then hopefully we'll
squeeze in some pset.
I don't know if we'll be able to squeeze that in because of time, but
with you guys who are here, and if you guys want to say after for a bit, we
can go over it outside.
But we're going to finish with command line arguments
for the last 15 minutes.
We awake, Michael?
You're awake?
Good.
I don't know if you sleep with your eyes closed.
It would be tricky.
>> Command line arguments.
Those are one way to pass information into a program.
Eventually, we'll encounter others.
But essentially, we're going to stop writing int main (void) if we want to
take some command line arguments.
We're going to write this thing called int main (int argc,
string argv, brackets.
That is confusing.
That's a lot of letters that aren't in human English, so we're going to
unpack them right now.
>> Argc stands for argument count--
and again, all of these will be available online--
and that's the number of arguments.
Argv stands for argument vector, and those are the arguments themselves.
It's an array of strings.
We've actually seen an array of strings before today.
We created one in the last program we coded.
>> So here's an example.
I write ./ohai cs50 section on my command line.
Argc in this case equals 3.
Argv bracket 0 is ohai, argv bracket 1 is cs50, argv bracket 2 is section.
Again, argc is the number of arguments, and to trip you up a little
bit, we always count the name of the function as the first argument.
So in this case, there are three arguments, the name of the function
and then those two that I typed in.
>> Next, we're going to save those arguments themselves in argv.
And again, just as we saw before, we did this in the last program we coded.
You can move through the array of argv and get those strings.
That's essentially what we did at the end of our last program.
We picked a random name and then went to names bracket whatever and printed
out the string located there.
>> On top of just the one array, because strings themselves are arrays, as
talked about, you can have arrays of arrays, which is
essentially what argv is.
One way you can think about this is rows and columns.
I can draw that right now.
So argv--
that's not erasing.
Let's draw under ohai.
>> So if I wrote in, ohai cs50 section, I'm going to truncate
section for this example.
But essentially, my multi-dimensional array, ohai cs50,
we're going to do sect.
So here, I can access--
if I just give argv one number--
this is wrong.
What's wrong in this diagram?
>> STUDENT: Start at zero.
>> JASON HIRSCHHORN: It should start with zero.
I should probably also rotate it, but since I've already drawn it on, I'm
going to leave it on there for now.
But it starts at zero.
That's right.
I'm sorry if this is small.
Can you guys kind of see?
So if I do argv 0, I'll get all of ohai.
If I do argv 1, I'll get all of cs50.
If I do argv 2, I'll get all of sect.
>> But now, as we said, strings are arrays themselves.
So what I can do is I can treat this as a multi-dimensional array, and I
can do something along the lines of argv 0, 0, which gives me just the
character 0.
Argv 0, 1, which gives me just h.
Argv 1, 3, which gives me just this 0 down here in cs50.
>> So because strings themselves are arrays, we're putting
that in another array.
We now have array of arrays, and we can index into our first array and
then index into our second array.
And as you would expect, we do that by just putting those two--
we do the square brackets just right next to one another.
So in this example, argv 1 is cs50 and argv 1, 2, like we said, is just 5.
Does anybody have any questions about multi-dimensional arrays?
You guys are doing awesome.
Sweet.
>> So we have another program right here, and this is the final thing we're
going to do right now.
So I want you to write a program that takes a user's full name, two and only
two command line arguments, and finally, print out a greeting to the
user that includes their first name.
So why don't you take three or four minutes to do this, and then we'll go
over it as a group, and then we'll wrap up?
>> And again, here, when we say take a user's full name per the description
of this problem, it should be command line arguments, not using get string.
Yeah, Marcus?
>> STUDENT: So when you say two command line arguments, is that in addition to
the ./run, or--
>> JASON HIRSCHHORN: Good question.
Do, including the ./run, three command line arguments.
Or including the ./personalized, in this case, because that's the
name of this file.
So argc would be three.
But I want only two arguments given to the program.
I should specify that.
I'll make that change before I post these and send them to you guys.
>> STUDENT: What if you have a middle name?
>> JASON HIRSCHHORN: If you have a middle name, you're out of luck.
I'm sorry.
Or if you only go by Shakira, you're also out of luck.
Yeah?
>> STUDENT: So when people don't have cs50.h, do they define argv
as char star argv?
Or is it still called string argv?
>> JASON HIRSCHHORN: Yes.
So string, again, we mask over what exactly is happening there.
That's going to be unpacked maybe on Wednesday, but soon.
It's like training wheels.
Because essentially, it's the same thing.
>> So let's take another minute to program this, and then we will go over
it as a group, and then like I said, wrap up.
And I think they're still extra candy, so you guys should hopefully take
that, or please take that.
>> Sorry if you're still writing, but I want to dive in and we
can finish it together.
So I have written in, #include cs50.h, #include stdio.h.
int main (void), curly brace, curly brace, inside three lines of
pseudocode, take a user's full name, two and only two command line
arguments, print out a greeting to the user that includes their first name.
>> So the first one, take a user's full name.
Who can help me take a user's full name?
How do I do that part?
Did you guys do that first part?
Take a user's full name.
Well, for that one command line argument, what should I write?
What should I change about my function declaration?
>> STUDENT: You don't want it to be void anymore.
>> JASON HIRSCHHORN: Exactly.
I don't want it to be void.
What do I want it to be?
>> STUDENT: Int argc.
>> JASON HIRSCHHORN: Int argc.
>> STUDENT: String argv.
>> JASON HIRSCHHORN: String argv.
>> STUDENT: Bracket, bracket.
>> JASON HIRSCHHORN: Bracket, bracket.
That's exactly right, and that's actually how I'm going to get
a user's full name.
They're going to put it in there.
So that one's done.
So now I want two and only two command line arguments.
So Noah, what is one way I can check the number of command
line arguments I have?
>> STUDENT: You do an if statement.
>> JASON HIRSCHHORN: If statement, exactly.
What do I want as the condition?
>> STUDENT: If argc greater than 3.
>> JASON HIRSCHHORN: If argc is greater than 3.
If argc greater than 3, what do I do?
>> STUDENT: Return 1.
>> JASON HIRSCHHORN: Return 1.
Will that ensure I have two and only two?
>> STUDENT: No.
If argc is greater than 3 and less than 2.
>> JASON HIRSCHHORN: If argc is greater than 3 and argc is less than 2.
What's one way I can write this in just one line, or without using that
and Boolean operator?
>> STUDENT: It's not equal to 2.
>> JASON HIRSCHHORN: If argc is not equal to 2.
Exactly.
So if argc is not equal to 2, that will give me this program,
personalized, when I run it, and that will give me how many
command line arguments?
How many will I check for?
>> Sorry.
Let me reword that.
If argc = 2, how many things did the user write into the line?
>> STUDENT: One.
>> JASON HIRSCHHORN: Just one.
So before, you were right, Noah.
What do I actually need to put there?
>> STUDENT: That equals 3.
>> JASON HIRSCHHORN: Argc does not equal 3.
I want to equal 3 because I want personalized plus the other two.
So if argc does not equal 3, return 1.
Why did you say return 1?
>> STUDENT: Because that means it's wrong.
>> JASON HIRSCHHORN: That means it's wrong, and what's that going to do?
>> STUDENT: Re-prompt the user.
>> JASON HIRSCHHORN: It's going to quit the program, so it will have to run
again, but we don't have anything in here to ask for them again.
I didn't ask for that.
So that's exactly right.
Return 1 just quits the program.
Great.
>> And print out a greeting to the user that includes their first name.
How can I do that?
Anna?
>> STUDENT: Printf.
>> JASON HIRSCHHORN: Printf.
>> STUDENT: ("
>> JASON HIRSCHHORN: (
>> STUDENT: "hello
>> JASON HIRSCHHORN: "hello
>> STUDENT: %s
>> JASON HIRSCHHORN: %s
>> STUDENT: /n
>> JASON HIRSCHHORN: /n
>> STUDENT: ",
>> JASON HIRSCHHORN: ",
>> STUDENT: Argv
>> JASON HIRSCHHORN: Sorry?
>> STUDENT: Argv
>> JASON HIRSCHHORN: Argv
>> STUDENT: Square brackets 1.
>> JASON HIRSCHHORN: Square brackets?
>> STUDENT: 1.
>> JASON HIRSCHHORN: 1.
>> STUDENT: And ) semicolon.
>> JASON HIRSCHHORN: ) semicolon.
Let us run this.
Make personalized.
./personalized.
It just quit.
Why did it quit?
>> STUDENT: You didn't put any arguments.
>> JASON HIRSCHHORN: I didn't put any arguments in.
Looks great.
You guys nailed it.
So again, you went into argv and you checked at the beginning argc.
Those are two important things to remember.
So in our last two minutes before we leave, we won't get to pset two, but
like I said, if you want to stay after for 10 minutes, I would he happy to go
over with you guys the logic for these problem sets.
We had a lot to cover today so I didn't get to them.
Generally, I like to include that at the end.
So if you want to stick around, stick around for 10 minutes.
We'll do that.
>> Then, as I mentioned at the beginning, pset zero and one, we'll get that
feedback back to you as soon as possible.
The goal is to get pset one feedback, the comments part,
before pset two is due.
So if you made some mistakes or stylistically, things could be better,
you'll have that information so you won't do the same things in pset two.
And then in the future, you'll get all of your pset feedback and comments
back before the next one is due.
>> Finally, if I could put in another plug for feedback.
This is your first section.
It was my first section teaching this year.
Go there.
Write stuff, good and bad, and I will use it to make
section better next time.
And last, again, if you need anything, let me know.
I'm more than happy to help you guys.
If you just want candy, I will have extra candy.
I'm happy to bring it to you.
>> This is going to be an amazing course.
This is one of, if not my favorite course, I've taken in my entire life,
and I hope it will be that wonderful for you.
So I want to do everything in my power to make it that awesome for you.
I'm going to send out an email if you're in my section or Noah, and
going to try and schedule with each of you guys a time to check in over the
next couple of weeks so I can get a sense of what your CS background is,
and it will also help me tailor these sections to better suit your needs.
So that's what I've got lined up.
Again, I'll see you outside if you want to be there.
This was week two section.
Thank you guys so much.