>> [MUSIC PLAYING]
>> ANDI PENG: This is CS50, and welcome to week two of section.
This is actually while week two of our class-- actually,
the first week we've had section.
Show of hands, how many of you all came to super section last week?
OK, that's OK.
That's OK.
How many of you watched super section last week?
OK.
And you guys did fine on problem set one?
So I'm going to take it that we have a bunch of really intelligent kids
in this class and things will be fine.
OK.
So first--
>> First, we're just going to briefly go over the agenda for today.
We'll talk about course logistics.
A lot of you probably have questions regarding
how things run in this course.
We'll answer them.
We're going to briefly go over loops in case
any of you guys missed super section.
We'll talk about ASCII mapping.
We'll talk about arrays, functions, command line arguments.
And finally, what you guys probably all really want to be here to learn about,
is your pset2.
We'll have some nice tricks and tips for how to do that.
>> OK.
Also, secondly, we have lots of candy here.
Anyone who likes candy, just come up here.
Grab some.
I have a bunch.
We have a ton in the office.
I don't really want to eat candy all day.
You guys should eat all the candy, because I brought it here for you guys.
Just eat candy.
There'll be candy every section.
>> So first, who am I?
My name is Andi Peng.
I am the head TA of CS50 here at Yale.
And in addition to that, I will also be--
if anyone would like-- your running buddy, your shoulder to cry on.
If the night before your pset's due, you have no idea what's going on,
hit me up.
We'll commiserate together, et cetera.
Just know that I'm here as a resource for you as the front line
in helping you guys figure out how to solve problems in CS50 and beyond.
>> My email is andi.peng@yale.edu.
I'll also send out an individual email with my phone number in it
so that not everyone on the internet can see my phone number.
Feel free to call me at any time.
Seriously, my phone is usually attached to my hand.
I may not respond within a couple minutes.
I may not respond within a couple hours.
But I can guarantee you, if you e-mail me, if you call me,
text me, I will respond within 24 hours.
Oftentimes, I'll respond before that, because I understand
that everyone's got some problems, and you want your answers
to be answered very quickly.
>> However, if you call me 30 minutes before
that piece that is due Thursday night-- be like, Andi, help me,
I haven't started-- I may give you that shoulder to cry on.
But it may be time to think about how to not do that next time.
OK.
Oops.
How do I go back?
There we go.
>> So in this course, we have a ton of support for everyone.
In addition to just sections here, which you
every week get to spend 90 wonderful minutes with me,
we also have office hours.
How many of you guys have already been to office hours for help?
Great.
That's really good to see, because we have a staff of over 45 TAs and CAs
trained for this course that are here for helping you guys with your psets.
>> Mondays they're held in the TEAL classroom in 17 Hill House here
at Yale.
Tuesdays and Thursdays, they're held in commons.
8:00 to 11:00 pretty much every weekday.
Come in, come out.
Any problem, no problem-- just come on in,
and then we'll help you out with that.
>> Secondly, in addition to office hours and sections, we also have shorts.
Has anyone ever seen a couple of the shorts or walkthroughs in the videos
on the website?
OK.
So they're really, really helpful.
Our lovely Harvard production team over there on that school
up north, they have filmed and produced these incredible walkthrough videos.
>> They'll take you step by step through how to go about solving a problem.
Oftentimes, if you're confused about a simple concept, a singular concept,
take a look, because we probably have a short on it somewhere on our website.
And it's usually a great resource to look for when you're first
lost on how to handle a problem.
>> Third, we also have study50 and reference50.
So study 50 on the website is just a link.
It's, I think, study.cs50.net or something like that.
Just Google it.
We have lots of slides prepared.
We have lots of shorts and walkthroughs-- things all compiled
very nice and neat for you guys all to browse through, see any concepts
that you're confused about.
It'd probably be covered over there in study50.
>> Reference50-- similar.
We have a lot of various notes readily available for your disposal.
And lastly, we have what's called CS50 Discuss,
a forum in which, if the night before you're doing your problems, say,
you have a question.
Log in to CS50 Discuss.
Post your question.
And then one of the hundreds-- perhaps, thousands-- of peers
taking this class online will log in.
And maybe somebody can get your answer before a TA can.
It's essentially just an online forum that we can all
use to discuss our problems.
That's cool.
>> OK.
So problem sets.
We're about right in the thick of problem set two.
A lot of us have already struggled through problem set one.
We already know that perhaps waiting for the last night to do your pset
is not the smartest thing to do in CS50.
>> Tips and tips essentially for success are read the whole spec.
How many of you have already read the spec for pset 2?
OK.
That's a solid amount.
I know you guys have probably recognized by now the specs are really long.
They're really, really long.
>> It's really hard to read every line of that carefully word by word.
You want to just skim to where it tells you to do something.
I can guarantee you, do not do that.
If you do that, you're probably going to miss somewhere
where it tells you how to start a problem
or it tells you what to name your problem or it tells you,
this is probably the best way to start doing your problem.
And then you'll be lost.
>> When you try to read your spec halfway through,
a lot of the important information-- in that you're not
going to be able to really access.
So read the whole spec.
Try to read the whole spec before you come to section,
because then, when we talk about the pset,
we can all have an idea of what's going on.
>> Secondly, this is a concept that I'll like to reference
a lot throughout the course semester.
But essentially, meet us halfway, right?
Me, as your TA and Jason as course manager and Scaz as your professor
and David as your professor and all of the other lovely TAs in this course,
assistants in this course-- we will do our best
to ensure that you are set up to succeed in this course.
We will hold section.
We will hold office hours.
We will answer your phone calls, answer your e-mails,
do everything we can to make sure your questions are answered.
>> However, help us out too.
Meet us halfway.
If you come to office hours not having gone to section, not
having seen the lectures, having no idea what's going on, be like, help me.
I'm going to be like, well, I'm sorry.
It may be time to lend that shoulder to cry on,
because I'm not sure what I can do to help you
if you don't try to at least help yourself a little bit in the beginning.
Meet us halfway.
You know, please come prepared to answer and get
ready to engage in the material.
It makes things so much easier for everyone involved.
Yeah, that's pretty much it.
>> Grading.
So on the syllabus, it's pretty in-depth regarding how we grade.
But essentially, the breakdown in this is 50% of your grade
will be the psets, which is lovely, because that's
where you're spending the vast majority of your course time on.
Quizzes 40%, and your final project will be 10%.
There will be two quizzes throughout the course of the semester.
I think the times and dates are also listed on the syllabus.
>> The way that we will be grading your psets
is that we have essentially four values that we assign to each grade.
We have a value of scope, correctness, design, and style.
So as you see, that's kind of our formula for calculating psets.
Three we give the most weight to correctness,
because obviously your coach should be testing for the cases
that we want it to test for.
>> You guys all have check50 at your disposal.
It's a very useful function that you guys can all
upload your code to our servers, where we will check it for you.
And you've seen those frowny faces, smiley faces.
They're really, really helpful.
Please don't be the student that like tries
to hardcode in all the answers to them.
I've definitely seen a pset where somebody saw all the values of check50
and they just hard coded in-- if this number, print out this number.
If that number, print out that number.
Don't do that.
>> Even though technically check50 is correct,
your way of solving the problem is not correct.
So just use check50 sparingly.
But also make sure that you understand how it's functioning,
the program is functioning in addition to check50,
because we can't test every possible case of check50.
We'll be testing some of them.
>> Design is essentially, how well designed is your code?
If you are copy-pasting multiple lines of code many, many times.
Maybe you don't have quite the best design in your code.
Maybe it's time to add that loop.
Essentially, here it's just all about trying to do your code,
trying to write your code as efficiently as possible so that your program runs
as quickly as possible.
>> Lastly, we have style.
So style is kind of an arbitrary, subjective term.
I know we officially have a style 50 guide for CS50,
where it tells you, oh, you should have a space here.
You should kind of format your code this way.
You should name things this way.
>> I don't really care, per se, how you choose to style your code,
just as long as you stay consistent.
For example, if you like to leave a space after your four
before your four loop, just do that every time.
Don't kind of do it.
Don't do it other times.
If you like to keep curly braces space a certain way,
just always do it that way.
Don't kind of do it somewhere here and somewhere not there.
When we're grading, it's really difficult
if I have no idea how you're formatting your code,
things are wacky and out of place.
If you just keep it consistent, it's much easier for me as the greater
to be able to read your code.
It's much easier for you as a student to be able to look through your code
and see what's wrong with it slash why there's issues.
Style is one of the easiest things you guys can do to get full scores.
Essentially, if you just care, if you pay five minutes of attention
to your code every week, you should be getting full style points.
>> Lastly, we have what's called the multiplier of scope.
Scope-- I know it's a big word, especially in this class.
But scope, all that means is that you're attempting
the psets to the best of your ability.
If you turn in three out of your four problems
and don't even attempt the last one, you're probably
going to lose some points on scope.
>> Even if you just start the problem, do your best to walk through it.
Even if it doesn't work, turn it in, because that shows and demonstrates
to us that you're trying, that you attempted the problem set
to the best of your ability.
And then we can give you full points for scope.
>> Scope is also very important for when we talk about the lowest pset score drop.
So over the course of the semester, you guys have nine psets.
And we will actually be dropping the lowest score out of those nine,
but only if you have full scope points.
>> So if you turn in an incomplete pset, unfortunately, we cannot drop that one,
because your scope points weren't completed.
So even if you have the awful week where you're dying and you're sick
or your dog dies, your cat dies, and you're overloaded with homework,
just attempt the pset.
Do it to the best of your ability.
If it doesn't work, it doesn't matter.
Just turn it in.
At least we can give you points for effort.
>> A for effort in this class.
At least lowest score drop for effort.
So yeah.
That's pretty much it.
Does anyone have questions so far on how we grade in the class?
Or any of these?
Any of these so far-- sections, office hours?
Cool.
OK.
>> So this is a subject that no one really likes to talk about.
I don't really like to talk about it.
You guys don't really want to hear me talk about it.
But unfortunately, we all have to talk about it.
David spent 20 minutes a lecture talking about it.
And that is the subject of academic honesty.
>> So we all here have probably written an essay sometime in our Yale career.
We've probably gotten a talk where we've been told to not plagiarize our essay,
because that work is not ours.
In computer science, we have the same kind of concept.
All work that you submit, all code that you write
should be code that you have written.
Shouldn't be code that you've copied from somewhere else.
Shouldn't be code that you've googled and plopped in
and you don't really know if it works.
You kind of have a sense of what it's doing but not really.
When in doubt, essentially, it's just be reasonable.
On our syllabus, we have a whole list of things
we see as reasonable versus not reasonable.
>> For example, reasonable is you and your friend
discussing what the best way to go about logically solving a problem.
That's fairly reasonable.
What would not be reasonable is if you guys got together, typed the same code,
and turned in the same code.
That's not reasonable.
Same thing, kind of an essay.
If you want to discuss with your friend, hey,
this is what I want to write about.
These are the steps I want to take to write about it, great.
That's wonderful.
Please collaborate with each other.
If you guys start writing the same thing,
turn in the same essay, that's less OK.
So when in doubt, just don't do it.
>> Here, we in CS50, we have scripts that run to automate to test
not only the correctness of your code, but also the uniqueness of your code.
So please don't put us in the position of having to refer your case to EXCOMM.
Please just for everybody, let's just make this a wonderful experience.
Everyone learns better, we all are happier,
and we are all set up to succeed in this course.
Something that's very unique about this class
that I think everyone should really pay attention to is that we have
what's called the regret clause in our syllabus.
So essentially, within 72 hours, if you believe you've done something
you're not really sure was appropriate, please come to us.
We promise you that within 72 hours we will handle the case ourselves
without referring to a higher authority in the administration.
>> So if you come to me and say, Andi, hey, I'm really sorry,
but I think there's a couple lines in my code last night that I kind of googled,
got off of Stack Overflow, copy and pasted,
and I'm really, really sorry about that, let me know.
Please don't just let it fester and just hope that I catch it.
We will catch it.
Just come to me.
Let me know within 72 hours.
We'll figure out a solution.
And we promise that we won't refer to university authorities essentially.
So it's in your best interest to just be honest with everyone
involved in the course.
>> OK.
OK.
So now, quickly before I move on, does anyone
have questions regarding logistics on how courses we're going to run,
how sections are going to run, any of that?
OK.
Yeah.
>> AUDIENCE: [INAUDIBLE]?
ANDI PENG: Yeah.
So quizzes-- how many of you are in the Monday/Wednesday section?
How many of you are in the Tuesday/Thursday?
OK, so it's a solid split here.
So the way that we run it at Yale is that we
are going to have two separate quizzes-- one for each section-- that
will be taken during class.
I think it's October, end of October, something like that, as week one
of the quiz.
Yeah, so just come to class.
On that Monday or Wednesday, you'll take the quiz.
On that Tuesday or Thursday, you'll take a different quiz.
But the same material will be covered.
Yeah.
Good question.
Yeah.
>> AUDIENCE: Where do we go to check our grades?
>> ANDI PENG: Yeah.
So I will be sending out an e-mail whatever each week
when quizzes are graded-- or, sorry, when psets are graded.
Psets are usually turned in by noon or Friday.
So I promise that I will try to get them back to you
by the noon of the following Friday.
>> Whenever I grade a pset, I will send out a notification on the grade book
that tells you your score can be viewed online.
So right this week, after I finish grading last week's psets,
you guys will get an e-mail notification telling you,
hey this is where you go to view your grades.
And you can see every breakdown of your grades.
You can see comments.
Oh, quick thing also.
The comment section in the great book is where I'll probably
spend the majority of my time grading.
So one thing that's really important when
you guys are viewing your grades on your psets
is not only looking at the physical score
but also taking time to really read my comments.
>> Often that gives you feedback on how you're solving a problem.
If you need to do something a bit better,
constructive criticism usually is best given in those comments section.
So please, please, I'm going to spend time writing comments.
Please, I would really appreciate it if you guys would read those comments.
OK.
Cool.
>> All right.
So we are going to start talking and just reviewing quickly
some of the material from week zero just so we're
on the right page for beginning this week's problems.
So the while loop is one of the three types of loops
we discussed earlier in this class.
>> A while loop essentially is written in the syntax
where, while a certain condition, do this repeatedly, right?
Think of it in this graphic here.
You're going to begin at a certain point in your code.
You're going to enter the loop condition if-- I don't know-- x is less than one.
>> If that is true, you're going to execute the loop body.
And you're going to keep doing that again, again, again,
over and over-- which is why it's a loop-- until your condition becomes
false.
So in this way, a while loop is one of the simpler ways
to write any sort of condition that needs to repeat over and over and over.
Just be careful whenever you're writing any sort of loop
that you have an exit condition as well as an update to whatever it
is so that your loop doesn't just run over and over infinitely.
In any case, you're going to want to make sure
that you're changing some aspect of your code or at the end of the loop
just to make sure that you have a way of progressing towards the condition
that you want to meet to end.
Does that make sense to everybody?
We just don't want to be caught up in this spiral where
we go around and around and there's no way to break the loop.
And every loop essentially has a way of doing that.
OK.
>> Secondly, many of you in your Mario psets
probably had to employ this type of loop.
It is called a do while loop.
First of all, can anyone tell me what the difference between a do
while loop and a while loop is?
Yeah.
>> AUDIENCE: The do while loop runs at first [INAUDIBLE].
>> ANDI PENG: Yeah, exactly.
So a do while loop always does whatever is inside the do, inside the brackets
there and does that condition before checking for the-- sorry,
does that code before checking for the condition.
And this is especially relevant to us here in this class, because most times
we're going to want to prompt the user for some sort of input.
And then, depending on the input they give us,
then we can evaluate, oh, do we need to prompt them again?
>> So in Mario, if the user gave you a negative height, for example,
you're going to first prompt and do whatever is inside.
Then you're going to check while.
You know, is negative 1, is that a positive number?
If it's not, I'm going to go back and repeat the do and repeat
and repeat and repeat until they finally give you a number that you like,
that we can all employ in our code.
It's pretty important for essentially any user input.
I can guarantee you any time in a pset where
we ask you to input any sort of code, we're
probably going to give you a test case in which we're
going to give you something bad that's going to try to break your code.
>> If we try to ask you to input an integer, we may just give you a string
and see how you would handle that.
If we ask you to employ an age, we may give you a negative number
to see how you would handle that.
Just make sure you guys are testing for cases in which you don't get the best
input, let's just say.
And a do while loop is oftentimes the best
way to design your code so that it meets that scope.
OK.
>> OK.
So this is probably the most complex loop out of the three
that we've looked at so far.
And it seems very scary at first.
But I guarantee you, once you guys get the hang of how to use a for loop,
it's one of the most useful things, most useful tools in your arsenal
for moving forward in this class.
>> So for example, in Scratch, we had this very simple block
that just said repeat this certain phrases certain number of times.
By the way, SAJ-- that's Scaz Andi Jason.
We oftentimes sign our emails SAJ.
If we say SAJ, don't be confused.
That's just us.
>> So in Scratch, we were able to have a block that said, repeat I love SAJ!
10 times.
Very simple.
The logic behind that is a very, very simple, right?
I want to first go through the first time and see that, the second time
and see that, third time, so forth and so on, until you hit to 10.
>> And the way we would represent that in code is just through a simple for loop.
So for, you're going to declare your variable here, in this case,
with an int.
We're going to name it i.
We're going to initialize it to 0.
And the stopping edition is going to be i is less than 10.
And the update is going to be i++.
>> And within the loop, it's going to execute until eventually it
hits the end of the condition-- in which case, it's going to break the loop.
There should be stuff that you guys have all kind of seen before
and had to do for your problem set one.
Does anyone have any questions regarding for loops right now?
OK.
Great.
>> OK.
So for those of you who have read the pset spec for this week
know that we are going to have to employ something called ASCII and ASCII table.
So David in lecture went over briefly how computers essentially-- everything
is encoded in a computer in binary, in zeros and ones.
And the way that computers are able to store different values in addition
to zeros and ones are through mapping those numbers to represent other digits
or essentially other characters.
>> So in this case, an ASCII table-- all it does
is map characters, or chars, to numbers.
So the source code file in your computer-- it sees,
hey, a bunch of zeros and ones, zeros and ones, zeros and ones.
That's what stored in the actual memory of your computer.
>> But when we humans want to communicate with the computer,
we want-- say, for example, if I want the uppercase A,
I'm going to need some way of telling the computer, oh,
when I type uppercase A, I mean this representation in binary.
>> And so the way we do that is threw an arbitrary thing called an ASCII table,
where we, as humans, as programmers, some time ago,
we arbitrarily decided that we were going to assign these number
values to these characters.
>> So you guys can google this online.
I think there's a link to it in your pset-- just an ASCII map table, ASCII
table.
It just translates binary numbers into characters.
And it's going to be very useful for your problem set
whenever you want to calculate anything or if you
want to display certain characters or integers
or manipulate certain characters.
It's going to be very important that you all
know how to navigate an ASCII table.
>> So for example, the uppercase A is represented by the number 65.
And the lowercase a is represented by 97.
So it's important to know that the difference between the two values
is 32.
Oftentimes, if you need to convert from one to the other, the difference is 32.
And don't worry if you're kind of confused on this at first.
We'll go over how we would employ this in actual code.
>> OK.
So for those of you with laptops out, feel free to pull up an ASCII table,
because these will probably require you guys to reference
what the characters are.
OK.
>> So knowing that certain characters map to certain numbers,
if I were to run just the first line of that code-- the printf a,
lowercase a, minus uppercase A. Does anyone
have a guess on what would be printing out of the screen right now?
>> So first of all, what does lowercase a represent?
What number is that encoded in the ASCII table.
Sorry?
>> AUDIENCE: 97?
>> ANDI PENG: 97, great.
And what is uppercase A?
>> AUDIENCE: 65.
>> ANDI PENG: So what is 97 minus 65?
>> AUDIENCE: 32.
ANDI PENG: OK.
So what do you guys think is going to happen when I input that line of code
into my computer?
>> AUDIENCE: [INAUDIBLE].
ANDI PENG: Sorry, speak up.
No worries.
This is a very safe environment, minus the camera.
We're all going to-- no worries.
Just we're all going to pretend like it's just us chilling in this room.
No worries.
No question is too stupid.
No answer is a stupid answer.
I'm probably going to make mistakes in the course of my teaching.
Seriously, guys, just blurt it out.
Be confident in yourself, you know?
So what was that?
Who said that last answer?
OK.
Shout that nice and clear.
>> AUDIENCE: 32?
>> ANDI PENG: 32.
OK, let's run this code and see if that's what happens.
OK.
So as you guys can kind of see, the typical notation I've
set up here for how we would employ any sort of program in our computer.
We have our main function within our main function.
I'm just going to copy and paste this line of code.
Also be careful when you guys are copying and pasting code.
Sometimes certain operators don't paste over correctly.
In that case, the minus sign was actually a dash.
And so the computer didn't pick it up.
So I had to go back and physically retype that.
Just be careful when you guys are doing that.
>> OK.
We're going to run this here.
So we're going to cd into our section2.
I've called this program asciimath.
So remember, when we run any program, we want to first compile it
by running it through our make.
And then we want to actually run the program by doing dot-slash.
So we're going to ./asciimath.
>> Right, there we go.
And we see 32.
Well done.
You deserve a piece of candy.
Candy for you.
Sorry.
All right.
OK.
So we can go back to our example here.
Aah, no.
Aah.
OK.
I'm just going to keep it like that.
OK.
So as you guys can see, we can do lots of very interesting things,
a lot of very cool things, a lot of very complicated things involving
ASCII characters and numbers.
Once you get down to line five, that's a lot to follow along.
We're not going to go through the section.
Feel free to, if you can, reason it out on paper
first on what should be happening when you input such a string of values.
For example, in that last line, we have z-- which represents a certain number--
a-- which also represents a certain number-- plus 1 modulos 26
plus lowercase a.
>> If you guys keep reading through these, you
may see a pattern come up in how we're manipulating the code.
I highly, highly suggest after section all of you
guys go ahead and input those all in your computer
and see what kind of numbers are coming out
and reasoning through why those are happening, because for your psets
it'll be really important for you to understand
why certain things are happening.
All of these slides will be online.
So no worries about trying to physically copy down notes.
Everything's online.
This section itself will be online.
All my source code that I'm running will be online.
Yeah.
Did you still have a question?
AUDIENCE: What is modulos?
ANDI PENG: OK.
So modulo is an operator that's going to be pretty important to your guys's pset
here.
So the way that operators in C and in programming work
is that you have what's called the division symbol and the modulus symbol,
which is just like the percent sign.
>> So in C, when you do an integer divided by an integer with a slash,
C has a tendency to want to cut off all of the decimal points,
because an integer wants to convert to an integer.
It's not going to want to be a double with a bunch of decimals all after it.
>> So if I do 3 divided by 2, it's going to cut off the 0.5 and just give you 1.
So that's something to be very careful of when you're
doing any sort of math in programming, is that the numbers you get
may not be the numbers that you were thinking,
which is why rounding in your last pset is so important.
>> Modulo gives you the remainder.
So for example, if I did 3 modulo 2-- so 3 percent sign 2--
it would give you the remainder of that.
So 3 divided by 2 is 1.5.
It's 1 remainder of 1.
It would give you the 1, which is the remainder of that.
>> So when you guys are moving through the ASCII table,
modulo will end up being something that's very important,
and we'll discuss that later on.
>> OK.
So something that is pretty, pretty new and pretty unique
that we've discussed this week is the concept of what an array is.
So array is the first type of data structure
that we're going to encounter in this class.
All data structure is is some sort of arbitrary,
literally things like structure that we as programmers have created,
that we've put in our code that can contain other pieces of code.
>> So in this sense, an array-- think of it as a file cabinet,
where if you open different shelves of your file cabinet,
you can access different things.
In memory, an array is just doing the same thing on your computer.
You can have different blocks-- we call them indices-- of an array.
It's just like a block, a shelf of memory
that we've created within your computer that you can input
certain things in different spaces.
>> So with an array, you always have to specify--
you have to specify declaring an array in the following format.
You're going to first specify the data type
that you want to create of the array.
If I want an array of integers, I going to put int right there.
If I want an array of strings, I'm going to put strings there,
the name of your array, and then you're going to have square brackets.
And inside the square brackets, you're going to have the size of your array.
>> Something that's really important to keep in mind when creating arrays is
that, once you create an array, that size cannot change.
So if you know that right now you have an array of size 10,
you know that I'm going to have 10 cells within or 10 indices within this array,
and it's never going to expand or diminish
no matter what, and that there's currently just 10 blocks of space
allocated in your memory that can store up to 10 things of whatever you've put.
>> So in this way, an array data type, the data structure that is an array
is very different from some others we'll be covering later on in this course.
Yeah.
For example, if you wanted to create an array of size 3 that
contained variables of integer-- oh, sorry, of temperature--
and temperature, of course, is an integer.
>> So we would create int, which is the data type of what we want to store.
We're going to call this temperatures for the sake of nomenclature of naming
something that we all understand.
And we're going to have square brackets.
And we want three numbers.
So we're going to put three inside of it.
>> Something that's really important to keep in mind
is that arrays are zero indexed.
All that means is that you start with the index of 0,
and you run up through the size of the array minus 1.
So for example here, we have an array of size 3.
It's going to be able to hold three values.
But the numbers themselves-- that number, the array, that index,
the array, are 0 through 2.
So guys, be really, really careful when you're going through your problems set
and creating any sort of array, because a lot of the times it's
really, really easy to forget that.
I actually don't have the index of 3, that I currently just
have the index of 2.
And if you try to access the third index,
it's going to be what's called the null terminator.
It's not going to actually exist in the array.
And the computer is not going to like that.
So be careful whenever you're accessing things just
to ensure that you remember that arrays are zero-indexed.
>> OK.
So the first example is just one way of creating an array.
The second example I have below is just a separate way
of creating what is the same data structure that we've just inputted.
So instead of physically running through and putting in temperature of 0
equals whatever, temperature 1 equals whatever, temperature 2 equals
whatever, I could actually just directly create it all in one line
into temperature square brackets equals.
>> And notice in this case, you don't need to specify how large your array is,
because the computer's going to go through and see that there's
three elements in those curly braces.
And it's going to know, OK, I need an array of size 3.
You're not going to need to input it the following way.
>> And also, yeah, that way.
Does anyone have questions regarding how we make arrays
or how the structure of an array works?
Yeah.
>> AUDIENCE: [INAUDIBLE]?
ANDI PENG: Yeah, exactly.
So if you were to declare and initialize an array the following method,
which is the second way, you can just leave those.
And the computer automatically knows that it
needs to count how many elements are in those curly braces, separated
by commas.
>> So here they see 65, 87, 30.
So the computer knows, oh, there's three integers.
I know to create an array name temperature with three elements in it.
Good question.
Yeah.
>> AUDIENCE: Isn't it possible to create an array with different types of data
that could be entered into it?
For example, with integers [INAUDIBLE]?
ANDI PENG: For the purposes of this class, no, right now.
When you create a data structure like an array,
you're telling the computer, hey, I need you
to allocate this much memory in my hard drive,
with each cell being a certain number of bits.
>> Remember we learned in week zero that different data
types have different sizes?
So for example, a string is a different amount
of space than a char, which is a different amount of space
from an integer.
And so if you don't specify and you mix and match what kinds of variables
you have, the computer's going to be very confused.
And it's not going to know how much memory to give you.
So for purposes of right now, computers can only
recognize one type of an array.
Good question.
OK.
>> So naturally, the second question we have
is, well, now that we've created an array and we've put all of these things
in the array, how are we going to be able to access it?
So the typical structure that we always access an array is our lovely for loop.
I promise you guys that we'll be seeing a lot of this fellow here.
>> Essentially, anytime you want to input values into an array
or you want to access them, the best way to do so is a for loop,
because in a for loop, you know how many times you're
going to want to run through the array, because you have a stop edition, right?
And every time you run through, you can access
a different element of the array.
>> And also, this is a reason why typically we start our for loops at the 0 value,
because when you access arrays, you can access the zero index.
And so it parallels very nicely.
You guys may have wanted to write for int i equals 1.
i is less than or equal to 3.
>> But it wouldn't work quite as well here, because you only
have elements of 0, 1, and 2.
And so if you were to start your i at element 1, 2, and 3,
you're going to end up running out of the bounds of your array,
and bad things are going to happen.
>> So I hope you guys see the segue of why in the earlier classes
we were teaching you guys how to run and format a for loop the way that we were.
It's because, now we've transitioned into arrays,
you can see why the 0 lends itself very nicely to accessing.
>> So the way we do that is that-- I'm just printing it out here for the sake
of printing it out.
But I have my placeholder, comma.
And the actual accessing part is happening.
The name of the array was called temperature.
So it's temperature and the i-th element of the array.
>> So as the for loop runs through, it's going to start at 0.
It's going to print out the 0th index of this array.
Then it's going to print out the first element.
Then it's going to print out the second one.
And then we're going to break.
Is everyone clear on how that's happening?
Great.
>> All right.
So here we have a way of saying, if we didn't want
to hard-code in-- me as the programmer.
I didn't want to actually physically put in every individual element
of this array.
If I wanted to instead have the user input values,
what's the best way to do that?
>> Well, here I have created this lovely function,
in which I can declare an array.
So int scores-- let's just say we want to make
an array that held the grades of all 18 of the students here in this section.
I think we've got a bit more than 18 kids today.
But for example's sake, let's just assume we had 18.
I would create an array name scores with type int, because scores, of course,
are numbers.
And I'm going to have 18 in square brackets,
because that's how many students I want to be able to store scores of.
>> And the way I'd populate the array is that I'd
run it through a for loop, of course, with 0 being my 0th index.
And then with 18 being my stopping edition, because there's
18 elements in the array.
And then I'm going to do printf, Enter score for student-- yada yada yada.
>> Can anyone tell me why here I'm printing i plus 1 and not i?
It's kind of a trick question, not really.
It doesn't actually physically affect the running of the code.
Yeah.
>> AUDIENCE: [INAUDIBLE] 0?
ANDI PENG: Yeah, exactly.
It's a bit awkward to say, hey, you're the 0th student in this class.
It's a bit weird.
So us, as humans, don't really like to think of how computers think.
So even though in the computer, it's storing values
in the 0-th index, when we're humans, we don't really
like to refer to ourselves as zero.
So when I just print that, I'm going to print and add
1 just for the sake of clarity.
So when I print, I'm going to be able to print students 1 through 18.
It doesn't actually impact the running of the code
in any way, the way I print.
>> But be careful when you're actually accessing the array.
When you see the scores i, notice here I'm actually
accessing the 0th index and not the 1 plus 0-- or 1 plus 1,
in this case, index so that it's actually clear.
Is everyone kind of OK on how this is running through
and with every index I'm putting a value into the array
and creating an array with 18 numbers that the user is going to input?
OK.
Cool.
>> OK.
We now move into something that's pretty relevant for this piece set as well.
I know in lecture, David-- sorry, you had a question?
>> AUDIENCE: Can you enlarge it?
>> ANDI PENG: Yeah, so I tried.
But I don't know.
For some reason, this version of PowerPoint
is really not working well with the display.
So we're just going to keep it like this.
All these will be uploaded online.
Sorry, guys.
Yeah.
OK.
>> So we can also have strings.
So if you guys actually notice-- I know Rob went over this in that lecture
that he did-- a string is actually just an array of characters,
if you think about it, right?
A string is a name or a sentence or a word, right?
>> If I were to create a string named Andi-- just my name, A-N-D-I.
You think of that as just one variable.
But actually, it's broken down into just an array of chars.
So it's got a character of a stored in an array value.
It's got a character of n stored in the second index and so on and so forth.
>> So in such a way, we actually have this kind
of structure set in place for our strings.
So here, if I were to input the word "eat"-- so string word equals
get string.
If I were to input the word "eat," that is physically the way that my computer
is storing that string in my memory.
>> And if I wanted to run through that and print that out-- so for into i
equals zero, remember, in lecture, we covered something that's called strlen,
or the length of the string.
Because I don't actually know how large the array is
of whatever the user's inputting--
>> For example, I inputted the word "eat," and I know
that's three characters long, right?
So I could put in a three there and everything will be fine.
But if your user input something that's a different number of values,
you're going to not be able to really know that when you program your code.
>> So the way that we handle test cases like that is that we have something
called strlen, which is just a function that tells you how long a string is.
So strlen of word.
My word is eat.
n equals strlen of word.
Can somebody tell me what that value actually is right there?
What does n representing right now, in this example, if I had eat?
>> AUDIENCE: 3.
>> ANDI PENG: 3, exactly.
So we have for int i equals zero, n equals 3, essentially.
And i is going to run until it's less than 3i++.
And it's going to essentially go through and do the same thing.
It's going to print out every value and give you
E-A-T. It's just denoting a different way of writing it.
That's going to be very helpful.
Yeah.
AUDIENCE: What's the benefits of putting the n equals strlen word inside that
for loop [INAUDIBLE]?
ANDI PENG: Yeah.
So if I were to-- say, for example, if I were to do that and then
do-- oop-- that, in my code, it would actually be doing like the same thing.
However, the way that David explained it during lecture, if any of you guys
remember, was that, as humans, as programmers,
we really try to program our code so that our computer has
to work as minimal as possible, so that our code is very efficient.
>> So if I had that there, what would happen through my for loop
is that I'd first declare a variable named i is going to be 0.
I am going to check, oh, what is the strlen of word?
Oh, the strlen is 3.
So is i less than three?
Yes, it is.
I'm going to run.
>> And then the second time back around the loop, I'm going to increment i.
i is going to be one.
And i is going to check, oh, but what's the strlen of word?
Oh, it's three.
And does that seem kind of wasteful, every time you run through the loop,
to be checking the function, even though the strlen of word
never actually changes?
>> So it's extra power for a computer.
When you start talking about things that are billions and billions
and billions of places long, imagine your computer
having to physically go through and check all of that every single time.
That is why, to make just for the sake of efficiency,
we tend to just do this, because this way we're only
calling the function once in the beginning,
and that every time it goes through it's going
to store the value 3 there, that you don't
have to continually check every time.
Yeah.
>> AUDIENCE: Sorry.
Just [INAUDIBLE].
Could you do int n equals strlen and put that above the outside of the for loop
as well?
ANDI PENG: Yeah.
You could do that, absolutely.
The reason we have it in here is because the way a for loop works
is that it's called a local variable in the sense
that everything you're creating inside of the for loop
only exists inside of the for loop.
>> So the variable i only exists in those brackets.
And here the variables of n also only exists in those brackets.
So if you were to use strlen of word multiple times down below,
absolutely the best way to do that would be to declare it up the top
so you don't have to do it once.
Yeah.
AUDIENCE: Why do you have a new line after the percent
seeing if you want to put all the letters next to it separate?
>> ANDI PENG: Oh, I wanted to print them all on each line.
It doesn't matter.
Yeah, it's a formatting.
That's a good question, though.
Yeah, if I wanted to print it all on just one line,
I wouldn't have the dash in.
OK.
Everyone good?
OK.
Cool.
>> So I think I have talked enough.
You guys's turn to run through the code and tell me what is wrong here.
Where's the bug?
So as you can see, I've declared a new array of type string named class.
And I've inputted Sam, Jess, and Kim into it.
And I'm attempting to print out all the elements of the array.
Can somebody tell me why this is going to give me problems?
I'll give you guys 10 seconds to think about this.
>> OK.
Yeah?
>> AUDIENCE: Is the left center equal to 3 or [INAUDIBLE]?
ANDI PENG: Right.
So how many times is this actually going to run through this loop?
>> AUDIENCE: Four.
>> ANDI PENG: Exactly.
It's going to through four times.
It's going to run through at 0, 1, 2, and 3,
because their element is i is less than or equal to 3.
It's not going to stop when it's 2.
It's going to keep going until it hits 3.
And as we know, there's only three elements in our actual array.
>> If we try to access the fourth element or the index of 3,
you're going to hit somewhere in memory that does not exist.
It's called the null terminator.
Nothing's going to be there.
Your computer is not going to be very happy with you.
Yeah.
Does anyone have questions on why that was happening?
That's a common area to avoid.
Yeah.
>> AUDIENCE: Doesn't the first slide also have a string of 2?
ANDI PENG: No.
So essentially, when you're making an array,
that bracket right there, that number-- all
it's telling you is how many elements I have.
It's not actually telling me the indexes of anything.
So in this case, I know I want to write with three places,
with three physical places to hold whatever I want to hold.
So that's why the number three is there.
However, if I wanted to actually access it,
if I wanted to say, printf class bracket number, then
you're going to actually put the physical index there.
Yeah, good question.
>> AUDIENCE: So is the physical index supposed to be [INAUDIBLE]?
ANDI PENG: I'm sorry.
Can you speak up a bit?
>> AUDIENCE: So is the physical index [INAUDIBLE] each of the boxes?
[INAUDIBLE]?
>> ANDI PENG: Yeah.
So I'm going to go back to right here.
Think of right here.
We have an array of size 3.
There's three places, like, physical placeholders in here.
But they're named 0, 1, and 2.
So if I wanted to access them, the way that I access them
is printf of whatever I wanted in here.
You would have to print out the name of it,
because then the computer knows, oh, I need to look
in this array for the 0th index.
Yeah.
But the size of it doesn't change.
The size is 3, regardless of how you label them.
OK.
Everyone good?
AUDIENCE: So every time I [INAUDIBLE]?
ANDI PENG: OK.
So in this case, we don't really get into it right now in the course.
But know that a string-- like I said before, a string is essentially
an array of characters.
So if I create an array of strings, I kind of
have an array of array of characters, right?
>> So in this case, because I have an array of strings,
if you were to input a really long word, that still takes up only one space,
because that's the one string.
But if you were to think of the characters of that array,
then that's taking up a lot more characters than any of the other words
are.
Not really important for right now.
But that's just generally how it's working.
>> OK.
So this is something I'll let you do a lot over the course of the semester.
I need to rest my voice.
You guys need to stimulate yourselves.
A lot of you probably have to sleep right now.
I'm inputting random problems where we as a class or you
with a partner next to you are going to spend a couple minutes discussing
on how we would go about solving or creating a program such as this.
>> So right now, we want to create a program--
we're going to call it upper.c-- that converts
a lowercase word to an upper-class string-- uppercase, sorry.
Word in strings, sorry, is synonymous.
I'm going to change them to mean the same thing.
>> Yeah.
Take a couple of minutes.
It doesn't have to be written in any language.
Just in pseudocode code or logically how we would even
go about doing such a problem.
Yeah.
>> [SIDE CONVERSATION]
>> I also noticed that you guys can-- I kind of see already the program.
I guess my lack of presenter mode is a problem.
But it's OK.
>> [SIDE CONVERSATION]
Yeah, please, guys.
Come get candy.
>> AUDIENCE: Yes!
>> [SIDE CONVERSATION]
ANDI PENG: Also, yeah, I'll start throwing candy
at people who don't answer questions.
So you should all answer questions.
Or I suppose people who do answer questions.
Yeah, other way around.
>> [SIDE CONVERSATION]
AUDIENCE: [INAUDIBLE]
>> ANDI PENG: Yeah.
Yeah.
>> [SIDE CONVERSATION]
>> All right, guys.
Take, like, 10 more seconds.
>> [SIDE CONVERSATION]
>> OK, guys.
So before we start trying to actually physically write code,
a good habit to develop is that we want to first kind of reason
out logically how we would do it.
You want to make sure, before you physically
start trying to code in Mario, that you make
sure that you have your pseudocode.
That is taking you through what you need to create so
that, when you physically write your program later,
you are better able to catch errors in your code and things like that.
>> So we're going to just start by-- in English, in pseudocode,
does anyone want to give me a generic explanation
of how we would go about doing this?
Yeah.
>> AUDIENCE: [INAUDIBLE].
ANDI PENG: Sure, no worries.
AUDIENCE: Can you ask it to or ask someone to get string 1-- yeah, string,
and then--
>> ANDI PENG: Yes, so this is a good start.
I'll start typing, sorry, as you speak.
So--
>> AUDIENCE: --the lowercase numbers are higher, right?
Or the lowercase letters have high numbers?
ANDI PENG: Exactly.
AUDIENCE: So then we subtract 32 from whatever [INAUDIBLE].
>> ANDI PENG: Great.
So we kind of have a general sense of how to work about this problem.
Knowing that we've kind of learned the physical way that strings
are stored in memory, what do you already
know that you're going to probably have to write in your code
in order to move through the string?
>> AUDIENCE: For loop.
ANDI PENG: A for loop, exactly.
Great.
So we kind of have a generic pseudocode written down
that kind of is giving you notes on how you
would move about solving the problem.
Now that you have this, you can reference it for later
on when you're trying to actually write your code.
So we can actually go here.
And I've got a function called upper.c-- there it is-- of just a blank template
right now that you guys are going to help me figure out on how to write
this line of code-- not line of code.
It'll be multiple lines of code.
>> When starting any sort of blank pset, what's
the first thing I need to remember to do?
>> AUDIENCE: [INAUDIBLE].
>> ANDI PENG: Great Yep.
Include.
stdio.h.
Oftentimes this is one of the most easy mistakes people
will make when they're writing, is that they'll
forget to include an important library they need.
So if it's office hours and you're like, I don't know why my code doesn't work.
Can you tell me why it's not working?
We're going to say did you #include?
You should #include it.
OK.
>> So we've got the standard I/O here.
Is that the only library we're going to need here?
What else are we going to do?
Sorry.
Someone just scream it out?
>> AUDIENCE: [INAUDIBLE].
ANDI PENG: There you go.
OK.
And how do I go about starting any sort of function, any sort of main function
within our program?
Int main.
OK.
What do I put inside here?
What's the first thing you want to do?
If we want to get a string from the user,
what are we going to have to do on this first line?
Sorry, you guys just feel free to speak up and loud.
Just shout out whatever.
>> AUDIENCE: Ask the user?
ANDI PENG: How do we do that?
Ask-- am I going to type "ask the user"?
>> AUDIENCE: Printf.
>> ANDI PENG: OK.
Printf.
What do I want to printf?
>> AUDIENCE: Type in something.
>> ANDI PENG: Like that?
Guys, pretend like I'm a computer.
Physically tell me every step that I need to type in here?
Am I doing something wrong?
Should I have typed something?
>> AUDIENCE: You need quotes.
>> ANDI PENG: I need quotes?
OK.
>> AUDIENCE: And then new line.
Semicolon.
ANDI PENG: Semicolon?
OK.
Good.
>> AUDIENCE: And maybe specify that you want it in lowercase?
>> ANDI PENG: Great.
You guys laugh at me forgetting to put a semicolon.
I guarantee you somewhere in the course of this class,
you will forget to put a semicolon, and it
will take you three hours to figure out why your code isn't working.
It's happened to all of us.
It will probably happen to you.
Get in a good habit of using semicolons.
OK.
>> AUDIENCE: Do you want to do the backslash in?
>> ANDI PENG: Sure.
Do you want to do the backslash in?
>> AUDIENCE: Yes.
>> ANDI PENG: Great.
OK.
What do I do next?
>> AUDIENCE: Get string.
ANDI PENG: Get string.
So what do I type?
Somebody?
>> AUDIENCE: String s.
>> ANDI PENG: String s.
AUDIENCE: GetString.
ANDI PENG: Somebody, can you tell me where
this function GetString is coming from?
>> AUDIENCE: String.h.
>> ANDI PENG: String.h?
You think it's from string.h?
>> AUDIENCE: [INAUDIBLE].
>> ANDI PENG: There you go.
It's from CS50.h.
If you're always confused on where a library file is or a header file is,
google literally string.h, and I'll tell you
what are all the functions that are in string.h.
OK.
>> So now that I have created a string and I've prompt the user for it,
I've stored it in a variable named s, what do I need to do now?
>> AUDIENCE: Check if it's in lowercase.
ANDI PENG: Sorry?
AUDIENCE: Check if it's in lowercase.
ANDI PENG: All right, let's do that.
How do I do that?
Actually, for the purposes of the class right now,
we're just going to assume that everything
that we input is already in lowercase.
If you wanted to check, you would just add a conditional statement
that checked, that ran through every single array element
and checked if it's between certain values.
I forget what the number of values of lowercase are.
You can look it up on the ASCII table.
But yeah, that's a really good point.
But right now, we're just going to assume that all the strings we input
are in lowercase.
OK.
>> So how would I go about this problem next?
AUDIENCE: For loop.
ANDI PENG: For loop?
OK.
Tell me what to type.
>> AUDIENCE: For int i equals 0.
>> ANDI PENG: OK.
>> AUDIENCE: Oh, actually, then you do a comma and do n equals strlen.
>> ANDI PENG: So important thing that I think she's noticed here
is that we didn't have to say int n the second time we did this.
Just know that in a for loop when you're declaring,
you actually don't need the int of the second time you do a variable.
You can say n strlen.
>> AUDIENCE: Of s.
>> ANDI PENG: s.
OK.
>> AUDIENCE: Then a semicolon.
>> ANDI PENG: Sure.
>> AUDIENCE: And then [INAUDIBLE] n.
Then i++.
>> ANDI PENG: Great.
All right.
What do we want inside of this for loop now?
Remember, if we're going to run through a string and run through an array,
we want to check things in it.
What are we going to need?
This is kind of the tricky part now.
Anyone have a guess?
>> OK.
So first, how do we even access?
How do we even first check or access an element in the array?
How do we do that?
What's the notation we use to do that?
What's the name of this array called?
It's called s, right?
>> Remember, any string is always an array.
So s bracket i, right?
Because that is the current value or index value we're checking.
And we're going to set that equal to-- we want a lowercase value, right?
We want to turn that lower-- sorry, we want an uppercase.
We want to turn the lowercase value into an uppercase.
And so like-- I'm sorry, what is your name?
>> AUDIENCE: Heidi.
>> ANDI PENG: Sorry?
>> AUDIENCE: Heidi.
>> ANDI PENG: Heidi.
Like Heidi said initially, we're probably
going to need the-- we're probably going to have to subtract
32 from whatever charc that is, right?
Because on an ASCII table, the difference
between a lowercase letter and an uppercase letter is 32.
So when we're doing this, we're probably going to want to subtract 32, right?
>> So we're going to do s i.
Does everyone understand why I did that?
Because right now, in our array, we're checking the 0th index, right?
And in the 0th index of the string, that's the first character.
And that character we're going to assume it's lowercase.
If we want to make it uppercase, we have to subtract 32 from its value,
because in our ASCII table, that's how we get it
to the next corresponding value that makes it uppercase.
Does everyone understand that?
Yeah.
>> AUDIENCE: Could you also do lowercase a minus--
>> ANDI PENG: Yeah, so that's actually really good.
I'm going to come back to that question after we do this.
Yeah.
And then if I want to see what's going on,
I'm probably going to want to print, right?
Somebody tell me what I want to print here.
>> AUDIENCE: printf percent c backslash [INAUDIBLE].
That's the value of [INAUDIBLE] s i.
>> ANDI PENG: s i, sorry?
AUDIENCE: [INAUDIBLE].
ANDI PENG: I don't know.
What do you think?
>> AUDIENCE: Well, I wouldn't-- I guess I wouldn't bring--
I would take it out the inside, because [INAUDIBLE].
ANDI PENG: Oh, you would have done that?
AUDIENCE: Yeah.
ANDI PENG: Let's leave it the way it is, and I'll explain why later.
Remember, even when you have a place holder,
you want to put parentheses around it.
All right.
So this should be a solid function here.
Let's run it and see if it compiles.
Make upper.
Uh-oh.
That doesn't look too good.
Why is this happening?
As with any error, you want to go back and start with the first one,
because typically one error causes lots of other errors after it.
Here we see upper.c:18:25, which tells me in this program named upper.c,
on line 18, error implicitly declaring library of functions strlen with type
unassigned-- blah, I don't know what's going on after that.
>> All it's telling me right now is that something's going on the strlen.
And the computer's confused, because it's like, I don't know what strlen is?
What does that probably tell you you're missing?
>> AUDIENCE: You're missing [INAUDIBLE].
>> ANDI PENG: You're right.
Exactly.
So this is where it's important to make sure
that every function you use in your code is having the correct header
file for the library, or else you're going to get lots of errors
and your code is not going to know what's going on.
So we're going to include string.h here.
>> Now when we try to compile upper.
Compile correctly.
Let's run this program.
So type something in lowercase.
What do you guys want to type?
Shout out something.
All right, Charly just walked in.
We'll type Charly's name.
>> Charly in lowercase.
And hopefully, hopefully, this is going to shout out and spit out
Charly in uppercase.
Yay!
does everyone understand how I went about solving that?
The way that I can manipulate using integers
to solve something that I want to do in strings,
because characters and integers can be referenced in the same way because
of ASCII mapping.
>> So to go back to your point, if I wanted to put here instead of 32
uppercase A minus lowercase a just like that, that would work equally as well,
because that's simply just the difference between those two values.
If I want to make this again-- charly.
Oh no.
I think we went the wrong way, yeah?
>> AUDIENCE: [INAUDIBLE] lowercase a.
>> ANDI PENG: There you go.
Yep.
And out spits charly.
So be careful when you're subtracting values
to remember which one is greater than the other.
For example, up here I forgot that lowercase a is actually
greater than uppercase A. So when I tried to subtract them the other way
around, I got negative 32.
And my computer was like, I don't know what that is.
It's probability just some random value that is not very good.
And so make sure that you're subtracting in the correct length
to find the character you want.
OK.
Is anyone confused on how we went about writing this function?
OK.
So actually, a function already exists in the library called ctype.h.
The library's called ctype.h.
This function actually is already written for you.
It's called to upper.
>> And so for purposes of this pset, you're going
to find that you really want to use a lot of the functions contained
within the ctype library.
To upper, to lower, is upper, is lower-- those are all functions
that you will be able to use very, very quickly in your own code that
does exactly what this does.
We wrote out how to convert a string to uppercase.
But this is actually the code that somebody else has written
and that you can access via a different function.
Yeah?
>> AUDIENCE: So you just copy A's in there and do it for every single letter?
>> ANDI PENG: Yeah, because I don't care about the values of a.
I just care that the difference between them is 32, right?
The difference between lowercase b and uppercase B is also 32.
And the difference between c and uppercase C is always 32.
>> I just have to care about the difference between the two,
because all of the letters follow the same pattern, right?
If I have the difference between one of them,
I know what the difference between all of them is.
Good question.
Yeah.
Everyone good?
OK.
Oop.
>> OK.
So another concept that's going to be very useful as you
continue to develop your code is this idea
of creating functions or abstraction.
So right now, we've all taken algebra.
In algebra, you are taught that there is this wonderful thing called a function
machine, where if you input one sort of number,
it runs through this wonderful function and out pops a different output, right?
In code, the same thing happens in any sort of function.
>> So I could physically write in my body of my code
a main statement that actually does whatever the body of the code is doing.
But outside of that, I can also write many different functions
that do many different things.
For example, we've already started using some of them.
Printf-- that is a function that somebody else
has already written that we can call in our code.
To upper-- in this case, upper-- is another function
we've written that we can call in our code.
And so I guess why do we even have functions?
Why don't we just plop it all in the same line of code?
It makes it easy for everyone.
>> Well, the reasoning behind that is, first of all, organization.
It's really annoying going through somebody's code
and one function like printf.
If you guys actually knew what the function printf entailed to write,
it's, like, 1,000 lines of code.
>> If every time I wanted to printf something,
I had to write 1,000 lines of code, that would be really annoying to read,
right?
That is why we've just created this abstract function
that we have already someone else has written somewhere else.
And every time we need to use it in our code, we just have to say printf.
And that is able to use the function someone else has written in our code.
It makes it easier organizationally to read code.
>> Secondly, it's simplification.
It simplifies the steps we have to take to solve our problem.
Printf, functions like printf, functions like to upper
are all things that allow us to simplify our code down so it's easier.
And last thing is reusability.
>> So the fact that we have a function called printf
that we can call many different times and allows it to be reusable.
If I were to write printf, I only write it once.
It exists only in that one place.
If I wanted to do it again, I would have to copy and paste all of that
into my second line of code.
That's why if we create a function that exists outside of our main,
we can just call upon it and reuse it whenever
we need so that it's a lot easier for us as programs to write.
>> So the way that we would actually write a function is very similar, right?
This is kind of the first instance in which we'll
see a function take on a style different than our int main void.
In this case, if I wanted to write a function called q, right?
>> And the int value here is all telling me,
what do I want this function to return to me?
If I want to cube an integer, I'm going to want to input.
These are the parameters.
I'm going to inputs a value of type int.
And I'm going to return another type value of int.
And in here, as you can see, all I'm doing
is cubing whatever my input is as my output and returning it.
>> So all this function does is takes some sort of integer.
It multiplies it by itself twice so that it cubes in effect.
And then it returns whatever that output is.
So in this case, it's two lines of code that we had to write extra.
But if we wanted to call upon this multiple times,
it's a lot easier to type that one line right here that's in cube than
have to go through multiple times.
>> So the way that essentially any formatting for any sort of function
is going to exist is like here.
So we have the function name.
In this case, it's called cube.
And we name it cube because it's easy to remember.
You can name it square and it can actually be cubed.
Doesn't matter.
Just a name that you're assigning your function.
>> This right here, int, is the type of the parameter you need.
So what does this function need in order to operate?
Well, it needs an input.
I named it input.
You can name it whatever you want.
But I need something of type int.
It's going to actually execute whatever is inside of here,
the body of the function.
And then the return type right here, this int--
all it's telling me is that this function is
going to return to me in int.
So it's going to take in an int, and it's going to give you back an int.
Does everyone understand how the formatting of this kind works?
Cool.
>> OK.
So don't be worried if this seems a little abstract right now.
This is something that we'll talk about.
We'll dive in deeper later on in the course.
The way that any sort of higher level abstraction of these things work
is that, in memory, in your computer, everything
is stored in this type of stack, shall I say.
>> So up there at the top.
I don't know if you guys can see that well.
I'll try to zoom in.
Here at the top, we have the physical text
of what the computer is interpreting-- all the zeros and ones that
exist within our computer.
And then we have the initialized data and uninitialized data,
what we call global variables.
So variables that exist across all programs
and across your entire, essentially, code.
No worries if you guys are kind of unclear on this.
It doesn't really matter right now.
>> And then we have what's called a heap in a stack.
Think of a stack literally as just like a stack of things,
a stack of different things being pushed on top
of each other that store the various variables and syntax within your code.
And then here at the very bottom, we have the environment variables
of what you are executing within just your little line of code.
>> And we're just going to zoom in on the actual stack part.
So right here, if we were to zoom in on just this area of the stack,
this is what it looks like.
And this is actually pretty important when you guys talk about functions,
because notice that the memory of your computer
is storing the variables and the functions
and the parameters of both your function and your main variables
in different places.
>> So right now, main is the actual function
that you're executing in a code.
You have its parameters stored here and its local variables stored here.
Local variable just means any variable that exist only within that function.
And on top of it, you have this separate function called cube.
You have that's parameters as well as those locals.
>> And the reason you can see here is that what
happens when cube takes in a value from your main function
is that actually it's copying that over.
So if I wanted to cube 2 and return 8, that 2
is actually inputted as a parameter and it's
copied over so that you have it existing in two different places in memory.
And you have to be very careful on remembering which one of those
you're actually manipulating.
And an example of what happened that would be very, very bad
exists right here.
>> So right here, I have got, in theory, a program right here, a main function
that I'm declaring an int x equals 1.
I'm declaring another int y equals 2.
And then I'm running it through this function
called swap, where I assume that it's swapping the two values.
And then I'm going to print them out, right?
That's what I want this program to do.
>> So let's come down and take a look.
So if I actually write a different function, as you can see,
we have our main function here.
And then we have our second function here.
Void swap.
Void just means that it's not going to return anything.
The function name is called swap, and it's
going to intake two variables, int a and int b, from your [INAUDIBLE] function.
>> So essentially in here, we're passing x and y into this function.
And if we were to create this-- so we want
to create a temporary value, right?
We're going to assign that to a.
And then a is going to now equal b.
And b is going to be back where the temp value was,
which is a, because when you want to swap things,
you can't just like swap them.
>> You have to keep one out here so that it remembers
what that is, because once you swap one, you forget
what that original value was, right?
So in theory, this program should work, right?
If I wanted to swap the two, they should swap.
So let's run it and see if it works.
>> So as you guys can see, x was once 1 and y was once 2.
And if I print it out, x is still 1 and y is 2.
Hmm, this program doesn't seem to be working the way that I want it to work.
Does anyone want to take a shot at guessing why this was happening?
It has to do with the way that different things are
stored in different places in memory.
>> OK.
So you guys don't have to worry too much about this right now.
But know that cube's local variables were a and b,
because here in our function, we've declared a and b
as the variables that exist within cube, that function.
But as you can see, the parameters that it's taking in were being stored here.
But we weren't actually returning anything.
We weren't actually changing x and y.
We were just changing a and b.
>> We had copied x and y into something called a and b.
But we never actually manipulated x and y themselves.
Are you guys seeing how that's happening?
Is that we've simply copied it over, but we haven't actually kept
track of where they were in memory.
>> And so now the computer is looking, oh, I've
run this thing through this lovely function.
But x and y are still x and y.
Nothing's happened to them because of the fact
that those parameters and local variables
are stored in a different place in memory.
And this is a higher-level concept that we'll
start seeing later on in the course.
But just know that this is a problem that can happen
and that we'll figure out ways on how to deal with this later on in the course.
OK.
All right.
So the last concept we're really going to cover
that's going to be useful for the pset this week
are what are called command line arguments.
So Rob in his lecture went through kind of briefly on how these work.
Essentially know that when you're writing
like a main function, when we wrote functions before in the past,
we had int main void.
And the reason we had void there was because our programs didn't
need to input values to run, right?
When I ran Mario, I didn't need to actually just type in something
when I ran Mario.
I could prompt the user later on.
But that won't always be the case.
>> For example, in this week's problem sets,
you're going to be asked that, in addition to running your program,
you're going to want to input certain things in straight as you
run your program.
So the way we do that is called a command line argument, which
is like an argument you can input directly
as you're trying to run your program.
>> So the notation for this, it's pretty complex-looking,
but it's really not that difficult. So int main--
that's just your main function.
Into argc comma string argv brackets.
So all that's saying is that this int argc-- all that's telling you
is that those are the number of arguments the computer should expect.
>> So one thing that's really important to keep track of
is the physical name of your program counts as one of those arguments.
So if I wanted to have my program run plus one additional command line
argument, I'm actually going to have two.
Argc's actually going to be two.
And then string argv bracket-- all that is doing,
it's giving me an array of strings that's
going to just display and store what all of those command line arguments are.
>> So for example, if I wanted to have this program, ./copy infile outfile, right?
That's just a typical Linux command.
What actually is argc?
How many command line arguments are there actually in this?
All this does is make a copy of an infile of the name of one file
and it copies it into another file, right?
That's all this is doing.
As you can see, I'm doing it on one line without actually needing the program.
>> What is our argc in this case?
How many arguments do we have?
AUDIENCE: Three?
ANDI PENG: We have three, exactly.
So it's very intuitive to just think that it's just
the infile and the outfile.
But know that the physical name of the program you're running counts as one.
So in this case, argc is actually three.
How about, what is argv 0?
Remember, all of the command line arguments
were stored in an array of strings.
So if I try to access it, what would argv bracket 0 give me?
>> AUDIENCE: [INAUDIBLE].
ANDI PENG: Exactly.
It would give me the dot slash copy, because that's
the 0th element, the first element of my array.
What about argv 1?
>> AUDIENCE: Infile.
ANDI PENG: Infile, exactly.
What about argv 2?
AUDIENCE: Outfile.
ANDI PENG: Outfile.
This is a bit tricky now.
What do you guys think argv 3 is going to be giving me
if I tried to print that out?
>> AUDIENCE: [INAUDIBLE]?
>> ANDI PENG: Did you have a hand up?
Yeah.
>> AUDIENCE: An alternative.
>> ANDI PENG: Exactly.
So remember in lecture that we have at the end of a race something
called a null terminator?
We'll get into this also further on in the class.
But know that in notation, the way your computer
knows that it's the end of the array is that it
puts something called a null terminator or a dash zero.
>> So if you try to access argv 3, you're going to hit this dash 0.
This is even more of a trick question.
What about argv 4?
So this is past null terminator, past the bounds of our array.
What do you think is going to happen when we try to do that?
>> AUDIENCE: [INAUDIBLE]?
ANDI PENG: Sorry?
Say that a bit louder?
>> AUDIENCE: [INAUDIBLE].
ANDI PENG: Yes.
That is the name of the area you'll most likely get.
But know that argv 4-- that allows you to access
places in your computer's memory that you really
shouldn't be touching, because right now you know what argv is.
You know that it's an array of size 3 that has dot copy, infile and outfile.
>> If you try to go so far beyond that that you're past the null terminator,
your computer has probably stored something in argv 4.
And you really shouldn't be allowed access into argv 4,
because you have a file cabinet and you're only
allowed access to those three shelves.
But if you try to reach way beyond the shelf of where you are allowed,
you're going to start messing with other things in your computer's memory.
That's going to be really, really bad.
So just know that.
Be very careful that you don't just randomly start
accessing elements in an array that weren't in what you wanted to create.
Yep.
>> OK.
So we're going to do another terminal example here.
OK.
So Rob quickly in lecture the other day went
through this example, where he had int main int argc string argv
brackets, which is telling me that I'm going to have command line arguments.
>> He has, if argc equals equals 2-- remember,
equals equals means comparison.
If it equals 2, then I'm going to print hello,
whatever the first element of the array is.
Else print, hello world.
What is this going to happen essentially?
Can someone just explain in English what this program is doing?
Yeah.
>> AUDIENCE: If someone typed in a command and said their first and last name,
it won't just print the first one and say, hello,
whatever your first name is.
>> ANDI PENG: OK, let's run it.
So for example, if I were to make this file make hello to-- hello
to-- what do you want me to input?
>> AUDIENCE: First and last name.
>> ANDI PENG: First and last name?
Spaces?
>> AUDIENCE: Yeah.
>> ANDI PENG: Can somebody tell me right now, what is argc?
Just right there?
Oops, sorry.
Let me go back.
How many command line arguments are-- you guys can't really see.
But I'll try to zoom in.
I'm trying.
Yeah, there's three, right?
It's dot slash hello, it's Andi, and it's Peng.
>> So what do you guys think this program is going to be printing out?
Oh.
Oh, sorry.
Wrong name.
It's printing out, hello world.
Does anyone want to explain why that's happening?
Yeah?
>> AUDIENCE: [INAUDIBLE].
ANDI PENG: Exactly.
So here, are conditional is, if argc equals equals 2.
So only in the case that there's two command line arguments.
Then I'm going to print, hello, whatever it is.
But else, I'm going to print just, hello world.
So does someone want to give me an example
of how I would be able to actually get it to print the value of argv 1?
>> What would I have to input here?
Only one thing, right?
Because the dot hello to already counts as the first argument.
I only have one more.
So dot hello if I just do Andi.
Hello, Andi.
Does everyone understand why that's happening?
Yeah.
>> AUDIENCE: So the space between the command line-- is it [INAUDIBLE]?
ANDI PENG: No.
So the space in a command line argument, x, to tell your computer
that this is the start of a new string.
So string argv here-- it stores all of your commands line arguments
in strings.
And so the space in the command line argument-- all that does is tell you,
this is at the end of one string and it's time to move on to another.
>> Yeah.
This is pretty important for your pset, because you're
going to be testing these.
So does anyone have any questions on this?
OK, cool.
Yeah?
>> AUDIENCE: So the reason you would put integer argument calc instead
of [INAUDIBLE] would be when you want to type additional information to
[INAUDIBLE]?
>> ANDI PENG: Yeah, exactly.
Some programs will require you to input a command line argument
that it passes in and that uses that.
Yeah.
AUDIENCE: So what if [INAUDIBLE]?
>> ANDI PENG: Well, what do I have to change then in my program?
>> AUDIENCE: Just 3.
>> ANDI PENG: Why is that happening, do you think?
>> AUDIENCE: Because the [INAUDIBLE].
>> AUDIENCE: You'd have to change it to 2 [INAUDIBLE].
>> AUDIENCE: 1 and 2.
ANDI PENG: 1 and 2, exactly.
So in this case, you would probably want to have two printf statements--
one that printed argv 1 and the one that printed argv 2.
Here.
I can actually do that real quick.
There you go.
Oh.
Not quite what you wanted.
But if you guys mess around with the formatting, it will come out.
Yeah.
Cool.
I know we're a bit short on time.
But I'm just going to quickly spend a couple minutes going through helpful--
AUDIENCE: You have two minutes.
ANDI PENG: I have two minutes?
Thank you.
Some quick pset tips.
So for this pset, I highly recommend, like I said, everyone to read the spec.
You'll be writing three programs-- one called initials.c, one called caesar.c,
one called vigenere.c.
Some important tips that you guys really want
to keep in mind-- so there is this function
called atoi-- as I like to say, a trois.
Unclear if that's actually how you say it.
But all it's doing, it's inputting.
It converts a string to an int.
So a represents ASCII to i integer.
It's just converting ASCII values to integer values.
So remember argv, the array that stores all of your commands line arguments--
it stores them all as strings.
And so if you want to be able to have one of them become an integer,
you're going to have to use this value here.
>> Modulo, like we covered earlier in class today,
allows you to have the remainder of whatever you have.
So if we have really, really large numbers in Caesar,
what if we have the end of a string of Vigenere?
How are you going to be able to get that to wrap around?
If I hit z and I want it to go back to a,
how am I going to get that to wrap around?
Probably want to use modulo in there somewhere.
>> Secondly, browse around your library. ctype.h.
That's a new library we have.
You'll find a lot of really, really useful functions-- isupper, islower,
toupper, tolower, isalpha, et cetera.
All functions that will be very useful when you're trying to figure out,
is this character a lowercase letter?
How do I convert this string to an uppercase?
All these things will be very, very helpful for your pset.
OK.
>> Office hours tonight are 8:00 to 11:00 in the TEAL classroom tomorrow
as well as Wednesday and Thursday night.
Office hours are in commons.
Highly suggest you guys all get started right away,
because you don't want to be that kid who Thursday night has problems
submitting your pset and is like, I don't know what to do.
I started, I finished, I submitted.
Try to give yourself some room, because in code problems always occur.
You want to make sure that you give yourself
a lot of time to be able to complete the psets.
Cool.
I'll hang around here.
>> AUDIENCE: Is this online already?
ANDI PENG: I don't know if it's online already, but it will be.
I'll hang around here if anyone has any questions already.
Thanks for coming.