>> [MUSIC PLAYING]
DAVID MALAN: All right.
This is CS50, and this is the end of week 2.
I'm so sorry I couldn't be there with you all today,
but you're in very good hands.
Allow me to introduce CS50's own Rob Bowden.
ROB BOWDEN: And of course, then we have to make fun of the fact
that he sent us a vertical video and show this.
>> [VIDEO PLAYBACK]
>> [MUSIC PLAYING]
>> [FOOTSTEPS]
>> -This video didn't have to look this way.
It could have been prevented.
Say no to vertical videos.
>> -Vertical videos happen when you hold your camera the wrong way.
Your video will end up looking like crap.
>> -[GRUNT]
>> -There are more and more people addicted to making vertical videos every day.
It's not crack or nothing, but it's still really bad.
There are two different kinds of people who are afflicted with VVS.
The first group treats the videos they shoot like pictures.
They don't mean any harm.
They just don't understand that while you can turn a picture,
you can't really turn a video.
>> [CRASH]
>> -[MONKEY SOUNDS]
>> -The other group is people who don't give a [BLEEP].
>> -Vertical Video Syndrome is dangerous.
Motion pictures have always been horizontal.
Televisions are horizontal.
Computer screens are horizontal.
People's eyes are horizontal.
We aren't built to watch vertical videos.
>> -I love vertical videos.
>> -Nobody cares about you.
>> -If this problem's left unchecked, you too
will begin showing four videos at once just to save bandwidth.
>> -Letterboxed vertical videos would be the size of a postage stamp.
>> -And it will spread everywhere.
Movie screens have always been horizontal.
If vertical videos become accepted, movie theaters
will have to be tall and skinny.
>> -And all of the movie theaters would have to get torn down and rebuilt.
And by the time they were rebuilt, Mila Kunis would be old and ugly.
>> -Birds will crash into them and die.
>> -We'll all get stiff necks from looking up.
>> -And no one will sit in the front row ever again.
>> -George Lucas will re-release Star Wars again-- the skinny edition.
>> -I was never really able to tell the story that I wanted to tell.
This was a great chance for me to experiment with a new technology.
>> -You're a jerk.
>> -Every time a mobile device is used to record video, the temptation is there.
Just say no.
Say no to George Lucas.
Say no to old Mila Kunis.
Say no to vertical videos.
>> -And if you see someone doing it, say, "you're not shooting that right dummy!"
>> [MUSIC PLAYING]
>> [END PLAYBACK]
>> [APPLAUSE]
>> [AUDIO OUT]
ROB BOWDEN: --simple form of cryptography,
which is basically the encryption and decryption of secret messages.
So here we have a very simple toy.
And the idea is the outer ring rotates around the inner ring.
And you can see, maybe if I zoom in, that-- it's hard to see.
But, like, the number 1-- well, that moved.
>> The number 1 maps to the letter X, number 2 maps to the letter
J. Incredibly difficult not to skip forward.
Letter 2 maps to J. Number 3 maps to D. So
with this ring you can give someone a message 1, 2, 3.
For some reason you want to tell them XJD.
But you can give them some message of numbers,
and as long as they have this ring, they can decrypt what you're trying to say.
>> So you may have seen this particular example of cryptography
before if around the Christmas season you've watched A Christmas Story.
If you've never seen it before, then just
turn on TBS at literally any time on Christmas Eve,
because they just show it back to back to back to back to back the entire day.
And the relevant video is this.
[VIDEO PLAYBACK]
-Be it known to all and sundry that Ralph Parker is hereby
appointed a member of the Little Orphan Annie Secret Circle
and is entitled to all the honors and benefits occurring thereto.
Signed Little Orphan Annie.
Countersigned Pierre Andre in ink!
Honors and benefits already at the age of nine!
>> [YELLING FROM RADIO]
-Come on.
Let's get on with it.
I don't need all that jazz about smugglers and pirates.
-Listen tomorrow night with the concluding adventure
of the Black Pirate Ship.
Now, it's time for Annie's secret message
for you members of the Secret Circle.
Remember kids, only members of Annie's Secret Circle
can decode Annie's secret message.
Remember, Annie is depending on you.
Set your pins to B2.
Here is the message.
12.
11.
2.
-I am in my first secret meeting.
>> -25.
14.
11.
18.
16.
>> -Oh, Pierre is in great voice tonight.
I could tell that tonight's message was really important.
-3.
25.
That's a message from Annie herself.
Remember, don't tell anyone.
>> -Five seconds later, I'm in the only room in the house where a boy of nine
could sit in privacy and decode.
Aha!
B!
I went to the next.
E. The first word is "be."
S. It was coming easier now.
U. 25.
That's R.
-Come on, Ralphie!
I've gotta go!
-I'll be right down, Ma!
Gee whiz.
T. O. "Be sure to"-- be sure to what?
What was Little Orphan Annie trying to say?
Be sure to what?
>> -Randy has got to go!
Will you please come out?
>> -All right, mom!
I'll be right out!
I was getting closer now.
The tension was terrible.
What was it?
The fate of the planet may hang in the balance!
>> -Ralphie, Randy's got to go!
>> -I'll be right out, for crying out loud!
Almost there!
My fingers flew.
My mind was a steel trap.
Every pore vibrated.
It was almost clear!
Yes.
Be sure to drink your Ovaltine.
Ovaltine?
A crummy commercial?
Son of a bitch.
>> [END PLAYBACK]
>> ROB BOWDEN: So that's how Ovaltine relates to cryptography.
Basically CS50 just advertised Ovaltine, so we could
be a crummy commercial for Ovaltine.
All right.
So now actual computer science.
Remember on Monday we left off diving deeper into strings.
So we were dealing with the string "Zamyla."
And we were recognizing the fact that we can treat
"Zamyla" as a sequence of characters.
And remember that we learned the bracket notation.
So if this were stored in a string "s," then
if we said s bracket 0, that would indicate the letter capital Z.
And if we said s bracket 1, that would indicate the first lowercase a,
and so on up to s bracket 5, which would indicate the last a.
>> Now remember that the length of this string is 6,
but the indices into the string are 0 through 5, Z through that last a.
So this now fits into a bigger picture of your computer's memory, your RAM.
So somewhere the program that you're running your computer
needs to remember Zamyla somewhere in memory.
So can I have a volunteer?
Yes, please.
And what is your name?
>> DEAN: Dean.
>> ROB BOWDEN: Dean?
Nice to meet you, Dean.
So come over here, and we're going to have you draw
on our nice nifty layout of memory.
Now I like to think of memory as one long strip of bytes,
but just for display purposes we'll just do left to right, top to bottom.
OK?
>> So I'm going to show a program-- Getstrings.c.
And so all this program is doing is requesting four strings
from the user with GetString and then printing
whatever that first string entered was.
We're ignoring two through four.
OK.
So over here now-- when I first request s1.
So you are the computer.
And you are implementing GetString.
So you request a string from me, and I say, OK, Dean.
Give the string "Dean."
>> So somewhere in memory you need to remember "Dean."
So write it into memory somewhere.
Perfect.
OK.
So now we have s2.
And s2 is going to be a request to GetString.
So I'm going to enter a string.
I'm going to enter "Hannah."
So enter "Hannah" somewhere into memory.
Yeah.
A-H.
>> OK, so now s3.
And it's going to be another request to GetString.
And so now enter "Maria."
All right.
And then there's one last request to GetString, s4.
So, I don't know.
How about we go with antidisestablishmentarianism.
So enter that into memory.
Yeah.
So just do "Rob."
>> OK.
So now explain-- why did you leave these spaces?
Why do you have this blank space here, here, and up here?
Yes.
So notice when I go to print s1-- so if we
had "Hannah" running right up next to "Dean,"
how do we know when the string "Dean" ends?
So printing the string s1 may have just printed "DeanHannahMariaRob"
if it doesn't have any clue of when "Dean" actually ends.
>> All right.
So in memory the way we actually represent this end of a string
is with backslash zero.
So this space is exactly what we wanted.
It should be a backslash zero.
This will be a backslash zero, and this will be a backslash zero.
And you can have a fabulous prize for being a perfect volunteer.
Take a stress ball!
>> OK.
So this character backslash zero is how we indicate the end of a string.
It's how when any given program wants to print a string,
it's how-- remember we learned the strlen function last week?
String length?
It's how string length is able to determine how long a string is.
It just keeps iterating over the characters
until it finds the backslash zero character.
So the important thing to realize about the backslash zero character
is it's represented by all zeros in bits.
So notice that this is distinct from the zero character.
So the zero character, if you remember in the example that he gave at the end
of lecture where characters map to-- like capital A maps to 65.
Lowercase a maps to 97.
Lowercase b would be 98.
So the number 0 maps to-- I don't know off the top of my head.
44 or 45.
Somewhere in that region.
>> So the character 0 is an actual number.
But backslash zero maps to all zero bits.
So there's a distinction between backslash zero,
which we'll call the null terminator.
There's a distinction between backslash zero and the character zero.
>> All right.
So talking a bit more about strings.
So then we see here this is how it would be laid out in memory.
So this idea of strings as a sequence of characters-- so the official computer
sciency term for sequence is an array.
So we would call a string an array of characters.
And there are actually other data types that we can make arrays out of.
>> So to motivate this, look at an example.
We'll call it ages0.c I'll copy and paste our template.
OK.
So in this program what we want to do is grab the age
of three students in the course.
So we know the int age-- and now I'm going to say 0.
So you might want to say age1, but for purposes we'll see very shortly,
I'll say int age0 equals GetInt.
So the same call to GetInt that we've been using I
don't happen to be prompting saying, "give me the age."
But just requests it.
>> And age1 equals GetInt.
And int age2 equals GetInt.
So, again, three students, but ultimately the variable indices
are age0 through age2.
OK.
So this program will do whatever we want with age0, age1, and age2,
but this program ultimately works for three students.
>> OK.
So now what if I want four students?
Well, I'm going to have to go back into my code, change the comment,
and now we have an int age3 equals GetInt.
OK.
So who sees the problem here?
What is the problem with this sort of setup?
Yeah.
So we're creating a variable for each student.
Now that works, but ultimately what if I now
say, "I want to grab the age of eight students or 16 students
or the however many students of the hundreds of students in CS50
or the thousands of students on campus or the billions of people in the world?
So ultimately this is not sustainable.
Any time you see yourself copying and pasting code like this,
you should generally feel that there is a better way.
>> So this is where we introduce an array declaration.
So when you declare an array, this is what the general format
is going to look like.
We're going to say the type.
and then we're going to give the name of that array,
just as we define any given variable.
And then finally we are using this bracket notation again
but in a different context from how we were using it earlier.
>> So here this looks like a normal variable declaration that we've seen.
So we've seen int x semi-colon before.
Well now we might see something like int x brackets 5.
And putting this idea into the GetInt program that we have--
so we can implement this in the same way.
>> Let's say in CS we tend to use n as the number of something.
So here we're going to store four students.
And now we can say int age bracket n-- not quite get it yet--
to declare an array of four students.
So how this will look in memory will be similar to this.
Clear this.
And we're going to have somewhere in memory-- I'll put this up there.
>> So somewhere in memory.
One, two, three, four.
We have four integers in a row for this array of four integers.
So, currently, what is the size of one of these boxes?
Yeah.
It's four bytes.
It's 32 bits.
So now this is different from the array that we
saw earlier, the array of characters.
In a string each box was only one byte, because a character is only one byte.
But with an array of integers, each box has to be four bytes in order
to fit an entire integer.
So this is what an array of four ints would look like.
>> And then back to code.
Now we want to actually store integers into that array.
So now this is a very, very, very common pattern that will at some point
become muscle memory.
So int i equals 0.
i less than n.
i plus plus.
Age bracket i equals GetInt.
>> So this for loop, this format, you should get very used to.
So this is generally how we will iterate over almost any array.
Now notice this sort of explains why from the beginning
we didn't have for loops going for int i equals 1, i less than or equal to 10.
The reason being that starting from zero makes this work well with arrays.
So arrays are zero indexed.
If this array is of length 4, the indices are 0 through 3.
>> So through the first iteration of this for loop
we're going to be setting age bracket 0 equal to a call to GetInt.
So whatever I happened to enter at the keyboard.
In the second pass, we're setting age1 equal to GetInt.
Third pass, age2.
Final pass age3.
So if in the first pass of the loop I enter the number 4 at the keyboard,
then we'll insert a 4 here.
If on the second pass I enter 50, we'll put a 50 here.
On the third pass I might enter negative 1, negative 1,
and then finally if I enter 0-- and now remember that this was index three.
>> After we loop back, i is going to be incremented to 4.
i is no longer less than n, which is 4.
And we break out of the loop.
So what would be wrong with this?
[INAUDIBLE]?
AUDIENCE: [INAUDIBLE]
>> Yes.
So the array only has four places, which means as indices 0 through 3.
So if this were the case, I would take on the value 4 at some point.
age bracket 4 will be setting whatever happens to be over here
to what I have to say, enter 6.
That'll be setting this to 6.
>> But we don't know what's over here.
This is not memory that we had access to.
So if you remember from the previous lecture,
he was printing out values of Zamyla and at some point he hit this segmentation
fault. So you will probably be seeing many segmentation faults as you
implement some of the problem sets.
But this is one of the ways in which you can encounter a segmentation
fault, when you start accessing memory in ways that you shouldn't be.
So we did not have access to this location and this is a bug.
>> So this is better.
Now there is still a small issue with this code.
And that's basically that we're still stuck at four students.
Now if I want to use eight students, OK.
It's not that big a deal.
I can go in, change the comment, and change n.
Now this will work with eight students.
If I compile this and run this, it will show me--
it will request integers for eight students and it will just work.
But it's less than ideal to need to recompile the program every single time
I want to change the number of students that I want to enter the ages for.
>> So the final improvement on this, as we'll see here-- we're
going to request the number of people.
Here we have a number of people in the rooms
or any ages of people in the room.
But we're going to request the number of people in the room from the user.
So this is the exact same do-while loop that we've seen before.
It's the exact same do-while loop that you
may be implementing on the problem set.
So as long as they're entering an n less than 1,
so there's got to be at least one person in the room.
As long as they're entering an n less than 1,
then we're going to keep asking again.
Please enter the number of people in the room.
Now, once we have the number of people in the room--
so I might enter that there are 200 people in this room.
Then down here we're going to come and declare an array of size 200.
We're declaring array that's big enough to hold 200 ages.
Coming down, it's the for loop that you will get very used to.
So iterating over this array, assigning to each location
in that array an integer, and then ultimately here we're
just getting an example of iterating over that array, not to assign values,
but to access values.
>> So over here we see that we are saying, a year from now,
person %i will be %i years old, where the first %i is i plus 1.
So i is this index variable.
And the second %i is going to be the value stored in the ages array plus 1.
So this plus 1 is just because we're saying-- this plus 1, ages i plus 1.
This plus 1 is just because we're saying, a year from now the person
will be this old.
>> So why is this i plus 1?
Why we have a plus 1 there?
Yeah.
Yes.
So remember arrays are zero indexed.
So if we are printing this out for someone to just read the output,
then probably they want to see something like person one, person number one,
will be 20 years old.
Person number two will be 15 years old.
They'd rather not see person number zero is 15 years old.
>> So compiling this and just seeing what it looks like-- Create some space.
Make ages compiles.
Running ages.
We see number of people in the room.
So I'll say there are three people in the room.
Age of person number one, let's say 15, 20, 25.
And now I'll say a year from now they will be 16, 21, 26.
Let's see that this works with an n that isn't equal to 3.
So if I say number of people is 5, one, two, three, two, one, a year from now
they'll be two, three, four, three, two years old.
So I could just as easily have n be 10,000.
Now I will be sitting here for quite a while entering ages, but this works.
>> So now in memory somewhere we have an array of size 10,000,
so ultimately 40,000 bytes, because there are
four bytes for each of those integers.
So there's an array of size 10,000 where we can
store the ages of those 10,000 people.
All right.
Questions about any of this?
Yeah.
What if you gave a negative number?
Let's see what happens.
So in this particular case-- number of people in the room, negative one.
It rejected that, because up here we happen
to be handling the fact that if n is less than one we're going to ask again.
If you try to declare an array of negative size,
it generally doesn't work.
>> So let's try.
Let's ignore whatever value they input for n
and just say int ages negative one.
Let's see if it even compiles.
I am not sure.
No.
So ages is declared as an array with a negative size.
So upfront it recognizes an array cannot be of negative size and rejects it.
Now, if we didn't handle this do-while loop correctly,
if we weren't checking if n is less than 1--
let's say we just didn't have this at all
and instead we just grab an integer.
No matter what that integer is, we declare an array of that size.
>> So the compiler cannot possibly complain now.
If I compile this-- so it can't complain,
because it can't know that I'm going to enter a negative number,
which might be invalid.
For all it knows, I might enter a positive number,
which is perfectly valid.
So I imagine if I enter negative 1 people in the room, segmentation fault.
>> So, OK.
So let's add this back just keep it what it originally was.
So make ages.
Now if I want to try a negative age-- so let's
say there are five people in the room.
Age of person number one is negative 4, person three
is zero, person three-- OK.
So here, a year from now, person number one will be negative 3 years old.
So probably doesn't make sense.
But that's just because looking at the code all we're doing
is requesting GetInt.
>> Now, if we had had the GetPositiveInt function
or we had just simply done this sort of same while loop down there,
then this would work perfectly fine.
But in this particular case, we just don't
happen to be handling negative values.
Any other questions about arrays?
OK.
>> So we've now seen arrays.
And we're going to need to use this for command-line arguments.
So in problem set two-- I know many of you
might still be working on problem set one, but problem set two is coming up.
In problem set two, you're going to need to be dealing with strings, arrays,
and command-line arguments.
>> So what are command line arguments?
Now, you can see down here a little teaser for exactly what's
going to be happening.
We see int main, inc argc, string argv brackets.
So first let's try to interpret what that's trying to say.
Now, OK.
>> So at the command line you should be getting used to some of these commands
now, and you've probably run cd in the terminal before.
So if we say cd pset1, you know that that should
be changing into the pset1 directory.
>> Now notice that you've never written a program like this before.
Each program that you've written, you would run, say, dot slash Mario,
dot slash greedy, and then it might prompt you for input.
Now, that's not what change directory does.
When you run cd, it doesn't then say, which directory do you want to cd into?
Instead, you just say, cd pset1, and it just goes into the pset1 directory.
>> So similarly we have other examples.
make hello.
When you run make, it doesn't then say, which program would you like to make?
You just say, at the command line make hello.
>> Move is another example.
This one we are moving the mario.c file up one directory.
So now we know with this example we're really passing two arguments.
There's mario.c as the first argument, and dot dot is the second argument.
And then when you run make, you see that really long command line--
that really long command printed at the command line.
So that long command-- this is just a short part of it,
but now we have three command-line arguments.
Dot dash zero, hello, and hello.c.
>> So these are command-line arguments, arguments
that you're passing at the command line so that it
doesn't have to be prompted when you run the program.
It would be frustrating if when you ran clang it said, "OK,
which program-- which file are you compiling?
Hello.c.
What flags would you like to enter? dash o.
What would you like the file to be called?
hello.
No, you just run clang dash o hello hello.c.
>> So looking back at this.
Now argc-- argc is the argument counts.
It's the number of command-line arguments entered at the command line.
Well, argv-- technically the v stands for vector,
which basically means array.
But you can ignore that.
Argv-- we have string argv, so string argv brackets.
So this is another form of brackets you haven't seen before.
So we've seen bracket notation when we've said,
like, string s equals Zamyla.
s bracket 0 accesses the character Z.
>> We've also seen brackets when we said int ages bracket 5.
That declared an array of size 5.
So here is a version of the brackets we haven't seen before.
So this sort of string argv that would be completely familiar
that it would just be a string.
Now the brackets indicate that this is an array.
So string argv brackets means that argv is an array of strings.
Now technically a string is an array of characters.
So this is now an array of an array of characters.
But it's much easier to think about this as just an array of strings.
>> So why might the brackets be empty?
Like, why can't we say, bracket 5, bracket n?
Yeah.
We don't know how many inputs there are going to be.
So if we look at the clang example, we say clang dash o hello hello.c.
In this particular case, there happen to be three command-line arguments.
And so the brackets-- we'll see in a second it wouldn't be three.
It technically would be four.
But the brackets, we would say, there are three.
But now if we looked at move mario.c dot dot, the brackets
we would want to put two in them.
>> And there are a lot of commands that have a variable number of command line
arguments.
So what this version of the bracket notation indicates
is that argv is an array of strings.
But we don't know how many strings are in that array.
And how do we then know how many strings are in the array?
That's the whole point argc.
argc tells us how long argv is.
>> So the last thing to keep in mind is that, technically,
the command itself counts as one of the command-line arguments.
So cd pset1, there are two command-line arguments.
The program in itself, cd, and then the actual argument part of it, pset1.
Any program that you've written thus far has had one command-line argument-- dot
slash Mario.
That is the only command-line argument.
>> So now looking at clang dash o hello hello.c.
So what is argc?
4.
So argc is 4.
Clang, so argv bracket 0 is clang.
argv bracket 1 is dash 0.
argv bracket 2 is hello, and argv bracket 3 is hello.c.
OK, so questions on this, and then we'll look at some programmatic examples.
>> OK.
So we'll take a look at hello3.c.
So this should be familiar from one of the first c examples
we had where we would just say, hello world, but now this is more general.
So here we are saying hello %s backslash n argv bracket 1.
Notice-- so up till this point, this is what my template file has looked like.
I had int main(void), and then I would do something in the main function.
Now instead, once we start dealing with command-line arguments,
we need to state a different form of main.
>> So looking at hello3 again, the main is going
to take two arguments now-- int argc, the number of command-line arguments,
and string argv brackets, the actual strings entered at the command line.
So I'm going to change that template to reflect that fact.
Now whenever you write a program, if you don't
need to take any command-line arguments, then just use int main(void).
But now when you're writing command-line argument programs, which
you're going to be doing for problem set two-- so now that you're running
programs that need to take command-line arguments,
you need to have main of this form.
>> So here-- this is the big usage of the command-line argument.
So printing argv 1.
OK so let's compile and run this program.
Make hello3.
Compiles.
Dot slash hello3.
And let's say, "Rob."
Hello Rob.
If I say, "hello Maria," hello Maria.
Hello Maria.
Hannah still says, "hello Maria," because I'm not
doing anything with our argv 2.
Argv 2 now would be "Hannah."
Argc would be 3.
What if I did this?
So hello null.
>> He briefly touched on the fact that, technically, GetString
might return null, but we'll get a lot more into what null actually is.
But take it as a matter of fact that no is generally bad.
We did something wrong if it's printing "hello null."
And the reason we did something wrong is-- well,
when I ran dot slash hello3, argc was 1.
So that means the length of argv was 1.
If an array is of length 1, the only valid index is zero.
And so here argv 1 is outside the range of this array.
It was similar to before when I tried to store 6 outside the end of the array.
So I'm trying to access something outside of the argv counts,
and we're getting null.
>> So a better version of this, an improvement,
is explicitly checking argc.
So if argc equals 2, that means we ran something like dot slash hello3 Rob.
And it will print "hello Rob."
If argc does not equal 2, then it's just going
to ignore whatever you put at the command-line argument
as command-line arguments.
Or if you didn't put any at all, it's just going to ignore that and just say,
"hello you."
>> So compiling this.
Make hello4.
And running hello4.
Running it this way, what should be printed?
"Hello you."
Hello you.
What about hello4 Rob?
"Hello Rob."
And finally, hello Rob Maria is just "hello You" again,
because you didn't really enter something that it expected.
You entered more names than it could handle,
so it just defaulted to the hello you behavior.
So questions on this?
Or command-line arguments?
>> OK, so taking a look at a couple more examples of using
command-line arguments-- first we have argv dash 1 dot c.
So the comments give away what this program should be doing.
But notice now-- this for loop, this matches the exact pattern
I was saying before.
We just happen to be using argc instead of n.
Now argc is really the n.
It's the length of the argv array.
So are iterating over the argv array printf-ing each argv value.
>> So if I make this.
Make argv 1.
It compiles.
Dot slash argv 1.
Just running this, it prints dot slash argv 1
since that was the only command-line argument-- the program name.
There will always be at least-- argc cannot be less than one,
since there will always at least be the program name to run.
So argv 1 Rob will print argv 1 and then on the new line "Rob."
>> So in the first iteration of this loop, i is 0.
Argv 0 is the program name.
Dot slash argv 1.
And then argv 1 is my first command-line argument, which is Rob.
At this point, we are equal to argc.
We break out of the loop and we're done.
So this will work for an arbitrary number of command-line arguments.
Notice it prints argv 0, argv 1, argv 2, argv 3, argv 4.
And there is no argv 5.
argc is equal to 5.
So at argc-- at i equals 5, we break out of the loop.
OK.
So questions on that before we look at a more complex example?
>> So argv 2.
All right.
So we're still printing the command-line arguments.
But now notice we have a nested for loop.
So what is this doing?
So the first loop is doing exactly what it did before.
We're still looping over each command-line argument,
but now this second loop-- we've also seen something like this before.
When he was iterating over Zamyla printing out Z-A-M-Y-L-A.
So this second loop for int j equals 0, n equals strlen of argv bracket i.
>> So let's first think for the-- let's walk through.
Let's think what the computer would do if I ran this program as just dot
slash argv dash 2.
So if I ran this code, then argc is going to be equal to 1.
And string argv-- there's only going to be one index in argv,
and that's going to be equal to dot slash argv 2-- the program name.
>> OK, so now i equals 0, i less than 1, i plus plus for int j equals 0,
n equals strlen of argv bracket 0, so in the first iteration of this loop. argv
bracket 0 is dot slash argv 2.
So what is the length of that string?
Well, dot slash A-R-G-V dash 2.
So strlen of that will be 8.
So j equals 0, n equals 8.
As long as j is less than 8, j++.
And with that we're going to be printing a single character, which
is argv bracket i bracket j.
>> So the only i is zero.
We still only have one command-line argument.
In that first iteration of the for loop, we're
going to be printing argv bracket 0 bracket 0.
And then j is going to increment.
And we're going to printing argv bracket 0 bracket 1.
And then argv bracket 0 bracket 2.
>> So this is our first encounter of multi-dimensional arrays.
Remember earlier that I said that argv is technically
an array of arrays of characters.
So here if I said something like string s equals argv bracket i,
and then I said, s bracket j, this would be accomplishing the same thing.
Now, you've seen s bracket j before.
That's just access the j-th character of this string.
So with this, we are getting the j-th character of the i-th argv.
>> So what should this ultimately output?
Make argv 2.
It compiles.
Dot slash argv 2.
"Rob Maria Hannah," and give us some room.
So we see that this is outputting dot on its own line and slash
on its own line and a on its own line.
It's printing out each individual character
of each command-line argument.
And then in between them, because of this new line
we're printing down here, in between them it'll print a new line.
>> So this is similar to the prior argv dash 1,
which printed each command-line argument,
but now we're printing the command-line arguments
and then iterating through each character of each command-line argument
to get this output.
OK?
So questions on this?
>> One thing to note is that command-line arguments--
so they are separated by spaces as you would naturally expect them to be.
So a string can have spaces in it.
It's not super important, but if I wanted the third command-line argument
to have a space in it, then I could say something like this.
OK?
So this now still only has three command-line arguments-- well 4.
Dot slash argv dash 2, Rob, Maria, and Hannah Bloomberg.
OK.
Questions on this?
>> There's nothing special about the space character.
It just happens to be that the command-line treats the space character
as how you separate each argument.
All right.
So then Problem set two-- you're going to be
looking at secret-key cryptography.
So, similar to that example we saw from A Christmas Story,
you're going to be implementing some algorithms that, given a message,
you're going to be able to encrypt that message
that only someone with that secret key, with that decoder ring,
should be able to decrypt.
>> So that is the standard edition.
You're going to be implementing two different versions.
If you happen to take a look at the hacker edition-- now,
we're going to give you a string like this,
which represents an encrypted password.
So your goal is to figure out what the decrypted password is.
Now this is actually how passwords are stored in a lot of computers,
and it just stores this random string of characters.
You have to figure out how to get from this random string of characters
to what the original password was.
>> And finally, after this problem set, you should
be able to understand what this means.
So you will learn how to decrypt this sort of random string.
Similarly, if you remember from week 0, you might have seen this URL.
And you should be able to decrypt this eventually.
You might not be happy when you decrypt it and click on the link.
All right.
That's it for today.
So see you next week!
>> [ELECTRONIC MUSIC PLAYING]