Keynotes
Loading
Summary
Loading
Transcripts
[00:02]: YULIIA ZHUKOVETS: This is CS50 Week 1,
section on C. So if this is the video
[00:06]: you were expecting to hear, you
are definitely in the right place.
[00:09]: My name is Yuliia.
[00:11]: I'm a preceptor here at Harvard.
[00:12]: Here's my email address.
[00:14]: If you have any questions or
would like to get in touch,
[00:17]: I was actually some years ago a TA
at Yale where we also teach CS50,
[00:22]: and now I joined the
team on the Harvard side
[00:24]: and helping them out with the Harvard
College class here on campus, and also,
[00:28]: supporting all of our students online.
[00:30]: So as I mentioned just now,
this is week 1 of CS50,
[00:35]: and we're going to talk about C. So
this is really where everything begins.
[00:40]: And we're going to
dive into some building
[00:42]: blocks that are really going to help
us throughout the whole semester.
[00:44]: And some things on the agenda for
today are variables and operators,
[00:49]: so the most basic, basic, basic
things that we need to get started.
[00:54]: We're going to talk
about functions, what
[00:56]: are functions, what parameters
functions use, how to define functions,
[01:02]: how to use them.
[01:03]: We're going to chat about
conditionals and loops,
[01:05]: so something that will be
really handy in our problem
[01:07]: set, and again, just one
of those stepping stones
[01:10]: to get us through the next weeks.
[01:12]: And we'll get a little
taste of problem set 1.
[01:15]: We're going to talk about
Mario, how to build those bricks
[01:18]: and stack a little, nice pyramid.
[01:21]: So without further
ado, let's get started,
[01:23]: and let's talk about variables,
types, input, and printing.
[01:28]: So you might recall that during
lecture, we talked about a phone book.
[01:34]: Professor Malan showed an
example where, if we're
[01:36]: looking for some contact or a person,
we might flip through the pages
[01:42]: or might go to the middle and
use a more efficient algorithm
[01:45]: of finding that person.
[01:47]: Well, today we're taking
sort of a step down
[01:49]: from the idea of searching to how
are those phone books comprise.
[01:54]: What are the components of those?
[01:56]: So you might imagine that one of the
variables in a phone book is called
[02:00]: "calls."
[02:01]: So maybe it is the number of calls
that one of your friend called you.
[02:07]: And maybe they called you 4 times.
[02:10]: So you can imagine having this
square or a little box that
[02:13]: stores some value, in this case, 4.
[02:16]: And it is called--
[02:17]: it's called "calls."
[02:18]: So what actually is going
on underneath the hood?
[02:22]: What is the code that enables us to have
this little square with the number 4?
[02:27]: Well, it can sound--
[02:29]: it can look a little cryptic for
now, but we will parse it together.
[02:32]: So in order to get this
box with number 4 in it,
[02:35]: the line of code we would
use is "int calls equals 4."
[02:40]: And as such, there are
4 components to it.
[02:43]: The first one is name,
variable name, and it
[02:46]: can be whatever sort of word
or phrase you would prefer,
[02:50]: sort of like calls or name, age, address
you're going to see a little later.
[02:55]: Next one is type.
[02:57]: This is very important.
[02:58]: We need to tell C exactly
what type of value it
[03:02]: should expect to receive on its end.
[03:04]: So in this case, it
is integer, to specify
[03:09]: just a number that doesn't
have any floating point, just
[03:12]: one number, integer.
[03:14]: Then, the value itself, if we created
this box that is called "calls"
[03:19]: and we're supposed to put an integer
into it, we're going to put 4 into it.
[03:22]: That is our value.
[03:23]: And lastly, sort of what
brings everything together
[03:26]: is the assignment
operator, the equals sign.
[03:29]: And you might also read it like this.
[03:33]: "Create an integer named calls
that gets the value--" oops--
[03:40]: "that gets the value 4," combining all
of our 4 components together to create
[03:48]: this variable.
[03:53]: All right, so let's
practice one more time.
[03:56]: Maybe someone in the chat could type in
a similar definition that we just saw.
[04:02]: We have int x equals sign and 50.
[04:06]: How could we combine it in one sentence
in a way that we saw earlier here?
[04:16]: Someone in the chat if you
guys want to type it out?
[04:24]: So we have integer x equals 50.
[04:27]: How can we bring it all
together into one sentence?
[04:37]: All right, seeing none, but
this is how you would say it.
[04:40]: "Create an integer named
x that gets the value 50."
[04:45]: Just reading left to right--
[04:49]: yeah, exactly.
[04:50]: "Create an integer named
x that gets the value 50."
[04:53]: So creating these sentences in your
head and relating code to plain English
[04:59]: or whatever language
you are studying CS in
[05:02]: can really help you get
through these first few steps
[05:05]: of getting used to how to write
code and how to phrase it properly.
[05:09]: Well, let's take it a step further.
[05:12]: So what if we--
[05:13]: what if my friend
called me another time?
[05:16]: They just gave me a call.
[05:17]: And now, the value is, of
course, it should be five.
[05:20]: So how can I change that?
[05:21]: Well, we can use a simple reassignment.
[05:25]: So in the beginning, if we created int
calls equals 4, that was our initial box
[05:30]: we create.
[05:30]: And then, we want to
assign it to five, what
[05:35]: we can do is just have
a second line of code
[05:39]: where we specify, well, now the value
of calls should be five and not 4.
[05:45]: And therefore, the value inside
of our box will change as well.
[05:51]: And again, translating
it to plain English,
[05:54]: the way it would look like
is calls, the variable name,
[05:59]: gets five, combining the name, the
assignment operator, and the value
[06:04]: together.
[06:06]: Well, what if it's not
just a plain number?
[06:09]: What if we want to make
some changes, for example,
[06:12]: incrementing by one every time my
friend or my mom gives me a call?
[06:16]: Well, we can also use different
operations as we're reassigning.
[06:21]: So, for example, if we want to add plus
one, we can do that on the second line.
[06:27]: So calls equals calls plus one.
[06:31]: Or if we want to subtract one, it would
be very similar but calls minus one.
[06:35]: But it gets a little confusing when
we have calls equals calls minus one.
[06:42]: It's just, how can I take calls
and then assign it back to calls?
[06:47]: This is, again, where it
gets a little confusing.
[06:49]: But actually, what's
going on, we're first
[06:51]: taking a look at the right-hand side.
[06:53]: We're grabbing our value 4 that
was in our little box before.
[06:58]: And so we're doing the operation 4
for minus one, which will be three,
[07:02]: and then assigning it
back to our variable calls
[07:07]: in such a way creating the new
box that contains value three.
[07:14]: And you can also use
multiplication and division,
[07:19]: sort of the same idea of grabbing
the initial value that we had
[07:24]: and changing it and reassigning it.
[07:29]: We could also do-- yeah, I see someone
in the chat saying we can also do calls
[07:33]: ++.
[07:33]: If we just want to increment by one, ++
is a very useful operator we will see
[07:39]: shortly when we're
talking about for loops.
[07:41]: But let me pause here
for a second and ask
[07:43]: what questions do we have about
variables, reassignment, operators?
[07:49]: This was sort of our first
chunk of information.
[07:52]: So if you have any questions, feel
free to type them in the chat.
[08:04]: OK, well, let's give
it a few more seconds.
[08:10]: Yeah, and feel free to put any questions
you have in the chat throughout,
[08:13]: and then I'll try to take a little
peek and then address them as we go.
[08:18]: All right, seeing none so let's move on.
[08:21]: So say I didn't have that number of
calls stored in the phone book just yet.
[08:27]: I wanted to get it from the user.
[08:29]: So how could we do that?
[08:30]: How can we get input from the user?
[08:33]: Well, there are a few ways we can do it.
[08:35]: In this case, if we're trying to get
an input that is a number, an integer,
[08:41]: we might want to use a
function called get_int.
[08:43]: And again, the way that
it's structured, we
[08:45]: can break it down in several components.
[08:48]: We are starting with our function.
[08:52]: So this is the tool that will allow
us to grab this input from the user
[08:57]: and then store it in our proverbial box.
[08:59]: So if we have function,
get_int, what it takes in--
[09:06]: I guess coming back a little bit,
the first part is the function name.
[09:10]: Get_int is something that was defined
before, is actually defined by CS50,
[09:15]: and this is in the library
CS50.H that you can also
[09:19]: read a little bit more about.
[09:20]: But someone once upon a
time wrote this function
[09:23]: that we now can use
to grab these values.
[09:27]: Inside of the function
is function input.
[09:30]: In this case, it is calls.
[09:32]: So we're prompting the user to input
the number of calls in the terminal
[09:39]: that we can then later use.
[09:43]: And then, our next
component is talking about--
[09:50]: OK, so we've looked at the code for this
function, but how does it actually work?
[09:57]: A lot of moving pieces.
[09:59]: So we can imagine this box that
represents the get_int function.
[10:04]: So something will go into the box, and
something will come out of the box.
[10:07]: So what goes into the box?
[10:11]: What goes in the box is prompt.
[10:13]: So in this case, it is calls.
[10:15]: And what we get out from it
is some kind of number that
[10:18]: is stored in the variable, calls.
[10:20]: Or if we give a specific example,
that could be, quote, unquote,
[10:26]: "calls colon space" to
give space for our user.
[10:29]: And then, we get back 4 that we
will then store in our variable.
[10:37]: And this is, again, where
we come back to assignment.
[10:40]: So we got this number 4 back.
[10:43]: So what is actually going on?
[10:45]: We're going to take this value.
[10:46]: And then, we're going to store
it in the calls, calls variable.
[10:53]: So zooming out again,
we created an integer
[10:57]: named calls that gets the value 4.
[10:59]: So before this four was
even on the picture,
[11:03]: we had this whole get_int function
where we prompted the user.
[11:07]: We got the number back.
[11:08]: And now, we're using this return value
to store it in the variable, calls--
[11:14]: so again, coming back to our idea
of having this little box that
[11:17]: stores some kind of number.
[11:21]: OK, I see a question.
[11:24]: I have a question about format codes.
[11:25]: What's the difference between
percent I and percent D?
[11:29]: That's a great question.
[11:30]: So %i and %d are interchangeable.
[11:33]: They're both used for integers
if we want to print them out,
[11:35]: which is a perfect segue
because we're going to talk
[11:38]: about printing values in just a second.
[11:40]: But %d is a little deprecated,
and we've been using %i since.
[11:45]: So definitely, default to that.
[11:48]: But as someone in the chat,
how to print out values, right?
[11:53]: So for example, we had
our value int calls 4,
[11:57]: and now we want to print it out.
[11:59]: Well, for that, we can
use function, printf.
[12:01]: And so we can have our string.
[12:06]: So just printing out the number
doesn't really mean anything.
[12:09]: It is nice to give some kind of context.
[12:11]: So in quotes, we can put "calls equals,"
and then %i would be the placeholder,
[12:17]: the placeholder for our variable because
if we did something like calls equals
[12:21]: calls, well, that's just text.
[12:24]: It wouldn't really print
out "calls equals 4."
[12:26]: It's just going to print
out "calls equals calls."
[12:29]: That's not what we want.
[12:30]: So to avoid that, we're going to
have our placeholder or format code.
[12:36]: So %i is one of the options
that's used for integers.
[12:40]: And then, the second
component is having the value,
[12:44]: the variable that stores the
value that we want to print out--
[12:49]: so putting these pieces together.
[12:52]: And there are other
types and format codes.
[12:54]: So for example, for
integers, sorry, for numbers,
[12:57]: we can also use floats for that.
[12:59]: In that case, it would be %f.
[13:01]: For text, we'll be looking into
chars and strings in just a second.
[13:06]: And then, true or false, there
are Boolean, type of Boolean.
[13:10]: We're using %i as well.
[13:14]: So next we're actually going
to write our own program.
[13:19]: So if you want to pull up cs50.dev
with me right now or just follow along,
[13:23]: feel free to do that.
[13:24]: But I'm going to pause again and take
questions, any questions that folks
[13:28]: have right now.
[13:30]: Questions on printing,
types, functions, variables,
[13:37]: anything that we just touched upon?
[13:47]: OK, seeing none but definitely
feel free to pop them into the chat
[13:50]: as we're chatting along.
[13:52]: But let's move over to
cs50.dev now, which is
[13:56]: our environment where we write codes.
[13:59]: Where can I write programs
is a question in the chat.
[14:01]: That's a great question.
[14:03]: So for that, we use cs50.dev, which
is the Visual Studio Code for CS50.
[14:08]: It's this virtual IDE where students
and teachers, and really, anyone
[14:15]: could use to get started with
programming, write their own programs.
[14:19]: It is simplified version of
the desktop Visual Code that
[14:22]: might be familiar to someone and should
be really easy to use and get started.
[14:27]: Well, let's go back to the
problem we're trying to solve.
[14:31]: We're going to write a program
that prints out "Hello, world,"
[14:35]: which is also the first
step of problem set 0.
[14:37]: So we can start checking off some boxes
on our list of programs to complete.
[14:44]: So to start, let's create this program,
and I'm going to call it Hello.
[14:49]: So I'm going to create
a file called hello.c.
[14:52]: And again, there are several components
that go into writing a program.
[14:56]: The first one is
including the libraries.
[14:58]: So I'm going to include stdio.h.
[15:04]: this will allow me to run my program.
[15:06]: And then, I'm going to create
a function, a main function,
[15:11]: like int main(void).
[15:12]: So in every program in this
course and probably in the future,
[15:16]: there's going to be a main function,
the main component of your program
[15:19]: that's going to be
responsible for things.
[15:21]: And inside of it, let's
use the printf function
[15:24]: that we just saw earlier in the slides.
[15:26]: And what we're going to do, we're
going to print out "Hello, world."
[15:33]: Semicolon, and those
are a little tricky,
[15:37]: and odds are you'll
probably forget it not once,
[15:40]: not twice, probably many more times.
[15:43]: But it is just getting
into the habit of it.
[15:47]: So let's go back to the terminal
and now run make hello to compile.
[15:54]: OK, compiles.
[15:55]: And now, we're going to run hello.
[15:58]: And.
[16:00]: it's not pretty.
[16:01]: It's on the same line
with my dollar sign.
[16:04]: So how could I fix that?
[16:06]: How can I separate it into
a new line right here?
[16:10]: Exactly, so I can use backslash
n, which will give me a new line.
[16:16]: And I'm going to run.
[16:17]: Hello again.
[16:19]: Not working.
[16:21]: OK, what step am I missing here?
[16:26]: I changed.
[16:26]: I added my escape character now.
[16:28]: I added the new line, but I
keep running hello, and it's--
[16:32]: exactly, I need to make hello again.
[16:35]: So I-- every time you
make any changes, make
[16:39]: sure you make the program again so
that all the changes are now reflected.
[16:45]: And boom, we have "hello, world" right
here in our terminal on its own line.
[16:53]: Any questions about this?
[16:57]: This is sort of one of those--
[17:03]: one of the first programs that everyone
writes, the "Hello, world" one.
[17:06]: And I think even though
it's a little simple,
[17:09]: there's still mistakes you can make.
[17:12]: There's the new line
character you might forget
[17:14]: or the semicolon or even
including the library.
[17:18]: So you can feel a little proud of
yourself even for getting that far.
[17:24]: Question in the chat, there's
another type like backslash n?
[17:27]: Yeah, so this is sort
of the escape character
[17:30]: that we were just talking about.
[17:31]: So for certain things,
you will need that.
[17:33]: For example, if I wanted to
use quotes around "Hello,"
[17:38]: I can't just put double quotes by
themselves because the program is going
[17:45]: to read it as if I'm just trying
to create a separate string.
[17:48]: What I could do is use
an escape character
[17:51]: and then put hello in
the quotes in such a way.
[17:58]: So let's show it.
[18:00]: Make Hello.
[18:02]: Hello.
[18:03]: So now, I have my quotes around hello.
[18:08]: Let me get rid of this so that we
can go back to what we saw before.
[18:12]: I'm going to make hello again, run it.
[18:17]: OK, hello, world.
[18:19]: Now, let's go back to
our slides and see.
[18:22]: The next thing we're going to accomplish
is let's write a program, "Hello, me"
[18:26]: or "Hello, your name" to complete
the second set of problem set 0.
[18:30]: Going back to cs50.dev now, and let's
keep editing just in this file, hello.c.
[18:35]: So my name is Yuliia.
[18:38]: And I could just do, "Hello, Yuliia."
[18:42]: I'm going to make hello.
[18:45]: I'm going to run hello.
[18:47]: Hello, Yuliia.
[18:48]: So this does a trick.
[18:51]: This is it.
[18:51]: The program is done.
[18:52]: We can now move on to
our next challenge.
[18:55]: What could we change here to actually
make it say, "Hello, name, Hello,
[19:04]: Daniel."
[19:08]: Exactly, we can get user input.
[19:10]: So instead of just hard coding
Yuliia, baking it inside of my code,
[19:14]: I can do something different.
[19:15]: I can, again, get input from the user.
[19:18]: So for that, I'm going to
create a variable called name,
[19:22]: and it's going to have type string,
which is what we use for words.
[19:26]: I'm going to use another
function called get_string.
[19:29]: Very similar to get_int, it's just
meant to getting strings from users,
[19:33]: not integers.
[19:34]: And then, I'm going to
ask, "what is your name?"
[19:38]: Leaving space, not forgetting
about the semicolon.
[19:41]: And then here, I'm going
to say, "Hello, name."
[19:46]: OK, make hello.
[19:49]: Oh, OK, here's our first error, right?
[19:53]: They're super cryptic.
[19:55]: This is a lot--
[19:57]: use of undeclared identifier,
Did you mean stdin?
[20:05]: Very cryptic.
[20:06]: Nica is saying, "include cs50."
[20:08]: Exactly, great thinking.
[20:11]: So what I didn't mention earlier
when we were talking about get_int
[20:16]: is that they're coming
from the CS50 library.
[20:19]: So for that, I'm going
to include cs50.h.
[20:27]: And so this will allow
us to use now string
[20:31]: and get_string to get user's input.
[20:34]: So let's make hello again.
[20:37]: Let's run hello.
[20:39]: What is your name, Yuliia?
[20:41]: Uh-oh, hello, name.
[20:44]: Isn't supposed to
print, "Hello, Yuliia?"
[20:48]: What is going on here?
[20:51]: OK, Nica's saying &as, but
where does exactly that %as go?
[21:02]: Uh-huh, OK, after me,
exactly, instead of name.
[21:05]: Perfect, yeah.
[21:06]: You guys are so good at it.
[21:07]: So instead of just baking in just string
name inside of my input of printf,
[21:14]: I actually want to
make it dynamic, right?
[21:17]: I want to be able to change it.
[21:18]: So for that, I'm going to go back to our
placeholders or these percent operators.
[21:26]: And after the comma,
I'm going to put name.
[21:29]: So what I'm saying is the text itself
is going to be "Hello, comma, space",
[21:34]: and then I'm saving a placeholder for
a value stored in variable name that is
[21:39]: of type string.
[21:40]: So you see how these pieces are
connecting between each other, string
[21:45]: name, variable names of type string?
[21:47]: So we use a %as operator.
[21:50]: All these pieces are connected together.
[21:52]: So now, let's make hello again.
[21:55]: Let's run it.
[21:56]: What is your name?
[21:57]: Yuliia.
[21:58]: Yay, OK, this is going well.
[22:00]: Let's try it again.
[22:01]: Hello, what is your
name, for example, David?
[22:04]: Hello, David.
[22:06]: All right, so let me scroll.
[22:08]: I see some questions in the
chat so let me scroll and see.
[22:12]: Gigi's saying, can you
explain what %as does?
[22:14]: Yeah, totally.
[22:15]: So remember how just now when
we were talking about integers,
[22:20]: we had our placeholder for i so that
we can print out variables calls.
[22:25]: So we can't really just
bake in calls equals calls.
[22:30]: We have to create some
kind of placeholder
[22:32]: to have this dynamic interaction between
the variable and grabbing the value.
[22:38]: So a similar thing is going on here.
[22:41]: We want to make sure that we have
the placeholder for our variable name
[22:44]: so that whatever user types in,
like David Daniel, Andrew, Bob,
[22:48]: doesn't matter, we can put
it in inside of our string
[22:53]: that is ultimately in printf.
[22:57]: OK, let's see what
other questions we have.
[23:05]: All right.
[23:08]: OK, any other questions about this
program, hello, me or hello, world.
[23:16]: OK, all right, seeing none.
[23:20]: So let's move on to our next exercise.
[23:24]: So next, what we're going
to do is create a program
[23:28]: that stores and prints
out some information,
[23:31]: like name, age, phone number, can be
address or hometown of your friends,
[23:39]: so adding some more layers
to our hello, me problem.
[23:46]: So for that, let's create a
new file called friends.c.
[23:51]: All right, it opened up
here in the terminal.
[23:54]: Let's create sort of the same
steps that we've done before.
[23:57]: So we want to include stdio.h library.
[24:03]: We want to have int
main(void), curly braces.
[24:10]: OK, so what we're going
to do next is a few steps.
[24:15]: So let's try to think what
attributes my friends can have.
[24:21]: So the first one was name.
[24:24]: That's easy.
[24:25]: That's something that
we just talked about.
[24:27]: What other things can we
ask our friends to input?
[24:33]: Age, yeah, so maybe age.
[24:38]: Height, yeah, that's a good one.
[24:41]: Let's maybe do hometown.
[24:48]: And maybe, let's do phone number.
[24:57]: All right, so as you might
see, these are commented out.
[25:02]: These are not lines of code.
[25:03]: These are not doing anything.
[25:05]: These are comments.
[25:05]: And the way you can do them is
by doing two slashes in a row.
[25:09]: So for get name, we're going
to do something very similar
[25:13]: that we just did in hello.c, string
name, get_String, what is your name.
[25:17]: So let's do just that.
[25:19]: So I'm going to create a variable
called string name get_String.
[25:26]: I'm going to ask, what
is your name, semicolon.
[25:32]: For age now, I'm going
to use an integer.
[25:39]: So oops, I'm going to do int age
equals get_int, what is your age?
[25:54]: OK, semicolon.
[25:55]: Same for hometown, I'm going to
say string hometown, get_string,
[26:04]: What is your hometown, semicolon.
[26:12]: And then, for phone number, I'm going
to do int number equals get_String--
[26:19]: sorry, get_int, what
is your phone number?
[26:29]: And I think I forgot to
include CS50 library again.
[26:36]: So let's not make the same mistake.
[26:38]: Let's come back and put it here on top.
[26:42]: OK, let's start with this.
[26:44]: Let's make sure we can
grab our variables.
[26:47]: Thank you, Ana.
[26:48]: Yep, good eyes.
[26:50]: Forgot to include the library.
[26:51]: So let's make this first and then
see if we're getting any errors.
[26:56]: Fingers crossed.
[26:56]: None, that's awesome.
[26:58]: And then, let's run it to see if we're
getting all the right inputs that we
[27:02]: want.
[27:03]: So what is your name?
[27:04]: Yuliia.
[27:05]: What is your age?
[27:06]: 22.
[27:07]: What is your hometown?
[27:08]: Cambridge.
[27:10]: And then, what is your phone number?
[27:12]: I'm going to say 111-111-1111.
[27:20]: So it didn't like it.
[27:23]: Let me do this again.
[27:25]: So it's prompting me again.
[27:30]: What's going on here?
[27:32]: Why can I not put in my phone number?
[27:34]: Oh, wait, it's a fake phone number.
[27:36]: But-- Yeah, exactly.
[27:39]: So parentheses doesn't
count as an infinite string.
[27:42]: So notice how in line
16 right here, I defined
[27:45]: number, the variable to get to call--
[27:49]: excuse me.
[27:50]: I defined number, the variable that's
going to store my phone number,
[27:54]: as an int.
[27:55]: But there are a lot of different
formats in which you can write it.
[27:58]: Surely, you could have
done like 1111111111.
[28:03]: This works.
[28:04]: But what if I want to include
the parentheses and the dashes?
[28:06]: So since we're not going to do any
mathematical operations with the number,
[28:11]: it is totally fine to
just store it in a string
[28:13]: and allow for those
different formats to come in.
[28:18]: So let's make this again.
[28:20]: Let's make the terminal
slightly bigger, make friends.
[28:26]: Let's run it.
[28:30]: OK.
[28:31]: Yuliia, 22, Cambridge.
[28:35]: Now let's try this format again.
[28:40]: Yay, now it works.
[28:42]: OK, awesome.
[28:43]: So changing it to a
string did the trick.
[28:45]: Now, we can put whatever
formats we want.
[28:49]: Let me pause here for a second.
[28:51]: Any questions about how we
just got all these user inputs?
[29:04]: Yeah, so Paula said, why did we
define the output for main as int?
[29:08]: That is a great question.
[29:09]: And actually, Professor Malan is going
to address that in the coming lectures.
[29:12]: But this is really a conventional
way to write main functions.
[29:16]: So it doesn't necessarily
take anything as an input.
[29:19]: So that's why we leave it as void.
[29:21]: But we do expect to
get a number from it.
[29:25]: And you can notice that, here, we're
not actually returning anything.
[29:28]: Even the printf, the printf action
is not actually returning a value.
[29:35]: It's more of a side effect
that we have in the terminal.
[29:39]: But good question.
[29:43]: Daniel said, will casting
work in this context?
[29:47]: In the context of putting in the
user's input, probably no because we
[29:52]: can't even cast something
that we haven't received yet.
[29:55]: So we want to make sure we are defining
the correct variables right away.
[30:02]: Tatia said, when we use two
backslashes, sorry, two slashes, that's
[30:07]: when we want to create comments, right?
[30:10]: So right here, when I'm just leaving
little hints for myself to remember what
[30:16]: I was typing.
[30:18]: Let's move along and now print it out.
[30:21]: So we have all these users
input that we just got.
[30:25]: Now let's put it all together.
[30:28]: So let's create sort of a lengthy
sentence where we're going to say,
[30:34]: "My new friend's name is %s."
[30:43]: And then I'm going to
say, "comma %i" for age.
[30:51]: "They are from %s" again
because I'm using hometown now.
[30:57]: And "Their phone number is %s" again.
[31:05]: So now, we just need to
match it back together.
[31:09]: So the first %s was referring to a name.
[31:13]: The next %s was referring to an age.
[31:17]: The third one was referring to hometown.
[31:22]: And the second one was
referring to a number.
[31:26]: So you can, again, see how all
these things come back together.
[31:31]: So we started off by creating users
input, by grabbing users input,
[31:37]: creating variables, name, age, hometown,
number, storing them in the variables
[31:42]: property types.
[31:43]: And then we're printing out all
of these variables in a sentence
[31:47]: by using these placeholders.
[31:50]: So let's make this.
[31:52]: So "make friends."
[31:54]: Oops, I forgot a semicolon.
[31:57]: See, I told you it was going
to happen at some point.
[31:59]: And it happened to me.
[32:00]: So let's do it again.
[32:01]: So "make friends."
[32:04]: OK, compiles friends.
[32:08]: Let's run it.
[32:09]: What is your name?
[32:10]: Yuliia.
[32:11]: What is your age?
[32:12]: 22.
[32:13]: What is your hometown?
[32:14]: Cambridge.
[32:16]: What is your phone number?
[32:17]: 1111111111.
[32:20]: OK, "My new friend's name is
Yuliia, 22, they are from Cambridge,
[32:23]: and their phone number
is 111--" lots of ones.
[32:27]: And I think I'm forgetting a new line
because I see that my ones are now
[32:32]: getting on the next one.
[32:33]: So I'm just going to create that here.
[32:36]: Let me compile it again.
[32:39]: What's your name?
[32:40]: Yuliia, 22, Cambridge, 1111111111.
[32:46]: OK, so still going a
little bit over the line.
[32:49]: But that is the program
that we just wrote.
[32:56]: OK, let me bring the
terminal down a little bit.
[33:01]: Ann said, what's the
difference between %s and %i?
[33:05]: That's a great question.
[33:06]: So you notice how here
we define name as string
[33:10]: and then hometown as string and
then number as string as well.
[33:13]: These are sort of text or words.
[33:16]: And this is exactly
why we want to create
[33:17]: these different placeholders to
distinguish between the different types
[33:21]: that we have.
[33:21]: So placeholder s is
for strings or words,
[33:25]: and placeholder i is for
integers, which is just numbers.
[33:33]: Gigi said, what does make file name do?
[33:36]: So when we do something like make
friends or make hello, what it does,
[33:43]: it takes the machine
code and compiles it--
[33:46]: sorry.
[33:47]: It takes the source code and
compiles it into machine code
[33:49]: so that the computer
can actually read it.
[33:52]: And we will see different
kind of variations of this.
[33:55]: For example, Python is
interpreted language.
[33:57]: We don't need to compile
it before we run it.
[34:00]: The computer does that for us.
[34:02]: But C is a little bit more
like lower controlled language.
[34:05]: So we want to make sure we are
making or compiling the program
[34:08]: first before we actually run it.
[34:11]: But great questions.
[34:14]: OK, all right, let's move on.
[34:17]: So we've talked about
return types and printing.
[34:22]: Now, let's go into our next
part of loops and conditionals.
[34:28]: So let's talk about conditionals first.
[34:31]: So you might see something like this--
[34:34]: if calls less than 1,
printf "Call more often!"
[34:38]: But what is actually going on here?
[34:42]: Well, again, several components
to it, it's just like LEGOs.
[34:45]: It's like building blocks.
[34:46]: So the first one is Boolean expression.
[34:48]: We're checking some kind of condition.
[34:50]: We're taking that
variable, calls, where we
[34:53]: are storing four or
five or any other value,
[34:55]: and we're checking if it is
less than one in this case.
[35:00]: If the expression is true, we're
then going inside of our conditional,
[35:08]: these curly braces that are
hugging our printf statement.
[35:11]: If the Boolean expression
is actually true,
[35:13]: we're going to execute
this conditional code.
[35:15]: So if calls is less than one, if
it's zero or maybe even negative,
[35:20]: which is probably not possible,
but we're going to printf,
[35:23]: "Call more often!"
[35:28]: If we have multiple situations
that we would like to address,
[35:31]: this is where else statements come in.
[35:34]: So the first one can still be "if
calls is less than one," right?
[35:38]: This is our first condition
that we want to check.
[35:40]: And then, else takes care
of all the other options.
[35:44]: So if calls is not less than one, we
want to printf, "Thanks for calling!"
[35:50]: So it means that calls
is either one or more.
[35:53]: So the Boolean expression
was indeed false,
[35:56]: and we jumped to our next part of code.
[36:00]: And these are mutually exclusive.
[36:02]: So either one or the other
can happen, and they're
[36:07]: separated by this else in the middle.
[36:12]: All right.
[36:14]: Chahab asked, referring to the
previous exercise, in sake of style,
[36:18]: is it better to print out every
information separate line?
[36:20]: That's a great question.
[36:21]: Yeah, you could have noticed that my
line was getting a little long, right?
[36:26]: It was going off screen.
[36:29]: It depends on the situation.
[36:30]: Sometimes, you want to print out
everything in one go and then
[36:34]: in the terminal jump between the lines.
[36:36]: But sometimes, you want to print it
out in different printf statements.
[36:41]: And that's totally fine as well.
[36:43]: All right.
[36:45]: Let's move into loops.
[36:47]: So next, we're going to talk
about while loops and for loops.
[36:50]: And let's start with while loops.
[36:52]: Again, we're going to break
down some of the components.
[36:55]: And the first one that we have going
top to bottom is initialization.
[36:59]: So we're creating this variable
i, and we're setting it to zero.
[37:04]: And that's going to be our count, right?
[37:06]: It's going to be the
variable that helps us
[37:08]: make sure we're doing the right
number of repetitions or loops.
[37:12]: So the next part is Boolean
expression, something that we just saw.
[37:16]: The comparison is going on.
[37:18]: We're going to get either true or false.
[37:20]: And if it is true, we're going
to go into our next chunk
[37:24]: in the code that is hugged
by these curly braces.
[37:27]: So if the Boolean expression
is, indeed, correct,
[37:33]: we're going to go inside
of our while conditional,
[37:40]: and I'm missing a step here.
[37:42]: We are first going to
go into printf, right?
[37:46]: We're still going top to bottom.
[37:48]: So we're going to do printf.
[37:50]: We're going to print out i.
[37:52]: And the first iteration
is going to be zero.
[37:54]: And then, we're going to
increment, bumping an i by one,
[37:57]: and then going into the
Boolean expression again.
[38:01]: So let's visualize it.
[38:03]: So again, let's go back to our boxes.
[38:06]: Let's try to picture
these things in our heads.
[38:09]: So again, we're starting
with int i equals zero.
[38:11]: We're creating this box.
[38:12]: We're storing value zero.
[38:14]: Second part is, is i less than one?
[38:18]: And it's sort of this message
or idea that is going on.
[38:21]: Nothing gets printed out.
[38:22]: Nothing gets returned
or changed in this step.
[38:25]: But it's still a step
that we need to make.
[38:27]: So we're asking ourselves,
is i less than two?
[38:30]: And i is less than two.
[38:31]: It's zero.
[38:32]: So then, we're going
inside the curly braces.
[38:34]: We're going to printf, print out
the value that i currently has,
[38:39]: which is zero.
[38:40]: And next step, we're going
to increment i by one.
[38:43]: And as someone said before, another
option, another way to do it is i++.
[38:48]: Both formats are correct.
[38:51]: Next, we're going to go back
to our Boolean expression.
[38:54]: Note that we're not going
back to int i equals zero.
[38:58]: We've done that step.
[38:59]: We're past it.
[39:01]: We're now coming back to
the Boolean expression.
[39:03]: We ask ourselves, is i less than two?
[39:06]: Well, i is one, so yes, it is true.
[39:09]: We're going back inside.
[39:11]: We're printing out i, which
is, in this case, one.
[39:15]: And now we're incrementing i by one
again and now repeating the process
[39:22]: again and again.
[39:23]: So is i less than two?
[39:24]: Well, i is not less than 2.
[39:26]: i is 2.
[39:27]: So our Boolean expression is false.
[39:31]: And now, we will finish the program.
[39:35]: We're going to be passed this while loop
and just left with what's on the right.
[39:44]: OK, so what are some other
ways that we could do things?
[39:49]: So let's consider this block
inside, the printf and the i++.
[39:54]: Let's try translating it into for
loops like one of our other tools
[39:57]: that we have.
[39:58]: So notice how we're still
grabbing the printf portion.
[40:02]: That is the part of code that's
going to go with us to the for loops.
[40:06]: And going to the next
slide, we see exactly that.
[40:09]: So we kept the printf part.
[40:11]: And now, the first line
looks very cryptic.
[40:14]: What is going on?
[40:15]: I see i++.
[40:15]: It looks similar.
[40:16]: i less than two but completely
different format, right?
[40:20]: It's actually all the same things that
we just saw on the previous slide.
[40:23]: So just like with the while loops,
we're defining int i equals zero.
[40:28]: Just as with while loops, we're
setting up our Boolean expression,
[40:32]: which is i less than 2.
[40:33]: And just as while loops,
we're incrementing i++.
[40:36]: So we can think about
it in the same ways
[40:38]: that we did with while loops, the
initialization, the Boolean expression,
[40:43]: and the increment.
[40:44]: And then ultimately, we will
printf our i in this case.
[40:52]: So still going through the same
steps of initializing i to zero,
[40:57]: leaving it aside, and then going
into the for loop, printing out
[41:02]: i one by one until we reach false
with our Boolean expression.
[41:08]: OK, before we jump to the next part,
which is actually talking about Mario,
[41:13]: one of the problems on the
problem set, what questions
[41:16]: do we have about conditionals,
for loops or while loops?
[41:23]: I'm going to stop here
to look at the chat.
[41:40]: What is the do loop syntax?
[41:42]: I think you're referring
to the do while loop.
[41:44]: And we'll actually see
it in just a second
[41:46]: because we're going to talk
about it as we're writing Mario.
[41:50]: Good question.
[41:55]: Any other questions about for
loop, conditionals, while loops?
[42:06]: Yeah so Remy says, what
to use what in these?
[42:08]: So it really depends on what
you're trying to accomplish.
[42:11]: And as you saw, they're
pretty interchangeable.
[42:13]: We can use either while
loops or for loops.
[42:16]: Either or is fine.
[42:17]: But the way that I
like to think about it,
[42:20]: you want to use for loops when
you know the exact number of steps
[42:23]: you want to take.
[42:24]: You know that you're
going to take two steps.
[42:26]: You can set the Boolean
expression correctly and easily.
[42:30]: And the while loops are
sort of a more fluid.
[42:33]: In this case, it is very similar.
[42:35]: We can translate while loop
into the for loop easily.
[42:38]: But sometimes we don't know the
exact like i is less than two.
[42:43]: Maybe it's some other condition.
[42:44]: Maybe it's, is it empty?
[42:46]: Is some variable empty, or have
we reached the end of something?
[42:50]: So with while, you have more freedom
to play around with different Boolean
[42:54]: expressions that you want to set versus
for loops are more strict in that sense.
[42:59]: You have to have two, three, four count.
[43:03]: All right, so let's move on, and
let's talk about Mario, which I think
[43:07]: is everyone's favorite problem set.
[43:08]: And it's a lot of fun.
[43:10]: But the problem at hand that we have
is that if you are not familiar,
[43:13]: Mario is a video game
where a character called
[43:18]: Mario hops around this
imaginary world that
[43:21]: is built of bricks and other things.
[43:23]: So one of the images from
the game is this pyramid
[43:27]: that goes dun, dun, dun, step by
step, which is exactly what we
[43:32]: will try to build today.
[43:33]: So we will not build such a
fancy image as we see here.
[43:38]: But we would want to
build something like this,
[43:41]: like a right-sided pyramid
that goes up in a similar way
[43:47]: as it does on the image.
[43:49]: But let's start with a
left-aligned pyramid first.
[43:52]: This will be a little easier to tackle.
[43:55]: And for that, let's go
back to our VS Code.
[43:58]: I'm going to clear the terminal,
and I'm going to create--
[44:03]: oops.
[44:04]: I'm going to create a
new file called Mario.
[44:09]: All right, I'm going
to close these friends.
[44:13]: I'm going to clear the terminal.
[44:14]: So, again, building
blocks, we have to make
[44:18]: sure we're including all the necessary
things for our program to run.
[44:21]: And let's start with our headers.
[44:23]: So we're going to start with stdio.h.
[44:27]: I already know that I will
need the CS50 library,
[44:31]: so I'm going to code that as well.
[44:33]: I'm going to create int main
void, our main function.
[44:40]: And then, inside this
main function, I'm going
[44:43]: to leave some comments for myself just
to help me get through this process.
[44:46]: So the first one will be
prompt user for input.
[44:50]: So as we saw here, the pyramid
can be different sizes.
[44:55]: So in this case, the
pyramid is of height six,
[44:58]: but it can be of height 5, 4, 3, 2.
[45:01]: We will leave this up to the user.
[45:05]: Then, the second step's print a pyramid.
[45:11]: Print a pyramid of that height.
[45:16]: So many typos.
[45:17]: All right, well, this is simple, right?
[45:19]: Two steps.
[45:20]: Why is everyone's making
such a big deal out of it?
[45:24]: It's just two steps.
[45:25]: But it takes time, right?
[45:27]: It takes, again, building blocks
and components to make this work.
[45:30]: So the first one, we've tackled before.
[45:32]: So we can deal with this right away.
[45:34]: So let's call it int height,
and I'm going to print.
[45:40]: I'm going to prompt my
user for an integer.
[45:45]: And I'm going to say, what is the height
of the pyramid, question mark, leave
[45:51]: space, semicolon.
[45:52]: OK, next we want to print a
pyramid of that height, right?
[45:58]: And that's where things
get a little bit tricky.
[46:01]: How can we go from 0 to 100?
[46:05]: How can we go from knowing
just how to print maybe one
[46:08]: hash at a time to printing multiple?
[46:12]: And I think you just saw me giving
away the next slide a little bit.
[46:16]: But let's work on this together.
[46:20]: All right, so again, building blocks.
[46:24]: Let's start with the idea
of printing a row, right?
[46:27]: So I want to print one
row at a time, and this
[46:31]: is what I'm going to start with.
[46:32]: And for that, I'm going to create
a separate helper function.
[46:35]: So I'm going to call it print_row.
[46:38]: And it has return type void and the
input void because we're not taking--
[46:44]: sorry.
[46:44]: We're going to return type
void, but we're actually
[46:46]: going to take in something.
[46:47]: We want to make sure that we're
printing different length.
[46:52]: Our rows have to be different.
[46:54]: So we're going to put in the
variable called int bricks.
[46:58]: Tell me how many bricks I
need to print on each row.
[47:02]: And here, what I'm going to
do is printf, printf hashtag.
[47:15]: And then, in here, what
I'm going to do, I'm
[47:17]: just going to say print_row 4 semicolon.
[47:22]: So we've created a
function, print_row, that
[47:24]: will take in the variable, int bricks.
[47:28]: We'll print some hashes.
[47:30]: And the way I know how many bricks to
print is by passing in this value here.
[47:34]: So let me make Mario.
[47:37]: And I am running into the first issue.
[47:39]: So call to undeclared
function to print_row.
[47:45]: How do I tackle this one?
[47:50]: Oh, beyond forgetting this guy, yes.
[47:53]: But how do I tackle the error?
[48:02]: Exactly, I also need to make sure
I prototype it ahead of main.
[48:07]: So what I'm going to do,
and as [INAUDIBLE] said,
[48:10]: this is the only acceptable time
that you can copy-paste in CS50.
[48:12]: But I'm going to copy this
guy and put it on the very top
[48:18]: before my main event starts.
[48:20]: And the reason I'm going
to do that is because I'm
[48:23]: reading my program top to bottom.
[48:25]: So I'm going inside of main,
and I'm going through line nine.
[48:28]: I'm getting input from the user.
[48:30]: And I'm getting line 12.
[48:31]: And without this prototype, main
does not know what print_row is.
[48:36]: It can't look ahead and jump below it.
[48:39]: It just sees what it is, what it has
right now, and what it's seen before.
[48:43]: So in order to avoid these errors, we
have to prototype this function before.
[48:48]: Let's try to make it again.
[48:52]: So make Mario.
[48:54]: OK, compiles, that's a good sign.
[48:57]: And now, we're going to run it.
[48:58]: What is the height of the pyramid?
[49:00]: For example, five.
[49:03]: OK, I'm getting just one hash sign.
[49:08]: This is not at all what I wanted to do.
[49:10]: OK, I'm seeing some
suggestions in the chat.
[49:15]: This was solving our scope issue.
[49:17]: OK, someone seems to put the entire
solution in the chat already,
[49:22]: but let's take it step by step.
[49:24]: So right now, I'm just sort
of printing our hash once.
[49:28]: What I really want to do is have some
kind of for loop, do it multiple times.
[49:34]: And for that, I'm going to
do just this, int i equals 0.
[49:41]: This is how we initialize it.
[49:42]: We're going to print out
some number of bricks.
[49:46]: And bricks is exactly the variable
that gives us that information.
[49:51]: So I'm going to set i less than bricks.
[49:54]: And I'm going to
increment i at every step.
[50:00]: I'm going to move printf hashtag
inside of the for loop, indent it.
[50:08]: Let's make Mario again.
[50:10]: OK, height of the pyramid?
[50:14]: Let's say four.
[50:16]: OK, it's printing out four hashes.
[50:18]: So that's a good--
[50:20]: that's a good sign.
[50:21]: But what if I want to make five hashes?
[50:25]: Still printing out four.
[50:27]: Where is my mistake here?
[50:31]: Why does it keep printing out four
hashes even if I put five as a user?
[50:43]: Any suggestions?
[50:48]: So I can see that--
[50:50]: yeah, exactly.
[50:51]: I have print_row four.
[50:53]: So I don't want to hard
code the number, right?
[50:56]: I want to make it dynamic.
[50:57]: So I'm going to change it to height.
[50:59]: And this way, whatever
user input puts in,
[51:02]: I will be able to
dynamically change that.
[51:08]: So let's make Mario
again, and let's run it.
[51:12]: What is the height of the pyramid?
[51:13]: Let's say six.
[51:14]: And I'm seeing six hashes.
[51:15]: OK, that's a good sign.
[51:17]: But again, I'm missing the new line.
[51:20]: That's just so annoying.
[51:21]: So I'm going to go in here
and create a new line.
[51:25]: And so I'm going to make Mario, Mario.
[51:27]: What's the height of the pyramid?
[51:28]: Five.
[51:30]: Ugh, this is not what I wanted again.
[51:32]: It's printing out in the column.
[51:33]: I still want it in the row.
[51:35]: What can I do different?
[51:38]: How can I change this?
[51:45]: Create a new line at the
end of the loop, exactly.
[51:48]: So instead of creating
a new line every time
[51:51]: I go through my for loop,
what I'm going to do
[51:53]: is just printf a new line right here.
[51:59]: And let's make Mario--
[52:01]: oops-- semicolon, make Mario.
[52:08]: Run Mario.
[52:08]: What is the height of the pyramid?
[52:09]: Five.
[52:10]: OK, we have a row.
[52:13]: We have a row, people.
[52:14]: This is awesome.
[52:14]: So this is one of the first steps in
our mission of creating a Mario pyramid.
[52:22]: OK, so what we did so far is created
a separate function called print_row.
[52:29]: We created a for loop in
which we print out hashes.
[52:32]: We also print out a new line every
time we finish that for loop.
[52:35]: And then, we've already preset
up some of those building blocks
[52:38]: inside of the main function.
[52:42]: I'm going to pause
right here for a second
[52:44]: and ask what questions do we
have about these steps so far.
[52:54]: What questions do we have about this
code, the way we used our various tools?
[53:07]: Any questions about this so far?
[53:16]: All right, seeing none so
let's keep moving along.
[53:19]: So let me just run this one more time
to illustrate what we're tackling here.
[53:25]: So right now, I'm just
printing out one row at a time.
[53:28]: But I want to have this
stepping stone construction.
[53:34]: I want to have multiple rows.
[53:35]: So how can I do that?
[53:36]: Well, we're going to make
use of for loops again.
[53:40]: So instead of just calling
print_row function one time,
[53:43]: we can call it multiple times,
again, using our for loops.
[53:46]: So in here, I'm going to
create "for int i equals 0.
[53:52]: i is less than height.
[53:56]: i++."
[53:58]: And then inside of the for loop,
I'm going to move this line of code
[54:09]: right here.
[54:11]: OK, let's make it.
[54:15]: Make Mario, Mario.
[54:20]: What's the height of the pyramid?
[54:21]: Four.
[54:22]: OK, so this is resembling what
we need to do a little better,
[54:26]: so just having 4x4 grids,
like a little square.
[54:32]: OK, let's see.
[54:34]: I see a question in the chat.
[54:35]: In the print_row int bricks, and
declare any type of variable,
[54:38]: int is if we can manipulate by
using inputs, width and height.
[54:44]: Yeah, so there are
different functions which
[54:48]: have different variables that they use.
[54:50]: So even though we put
in height right here,
[54:56]: we're referencing bricks
inside of the print_row
[54:59]: because that is the variable--
that is the variable that is
[55:03]: defined within the scope of print_row.
[55:06]: And therefore, if my
height in bricks just
[55:09]: match where I want them to
match on the print_row input,
[55:13]: I can just be using bricks
inside of my function
[55:16]: because this is what I defined earlier.
[55:20]: So here's the issue.
[55:24]: And let me propose that we can solve it.
[55:26]: But we have a 4x4 grid.
[55:31]: And it just seems to be doing the
same thing over and over again, right?
[55:34]: It prints four hashes four times.
[55:37]: It's very static, right?
[55:40]: It's sort of almost
constant, except for the fact
[55:43]: that we can get a different
input from the user.
[55:45]: How can we make it more dynamic?
[55:48]: What is the variable
inside our main function
[55:51]: that will give us this dynamic option?
[55:54]: What is the variable inside
our main function that
[55:57]: keeps changing with every iteration?
[56:00]: Remy said, replace height with i.
[56:02]: Exactly, so you can notice that
inside of our main function,
[56:07]: we have two variables, height and i.
[56:11]: And in fact, height, after the user
inputted that number, that is it, right?
[56:16]: That is where it stays.
[56:17]: It does not change.
[56:19]: But i, on the other hand, changes
with every iteration of the for loop.
[56:23]: It increments by one every time.
[56:25]: So let's do just that.
[56:27]: Let's make Mario.
[56:29]: Let's run Mario.
[56:31]: What is the height of the pyramid?
[56:32]: Four.
[56:34]: OK, this is looking a
little better, but I would
[56:37]: say that I'm still missing something.
[56:41]: What am I missing here?
[56:48]: The height of the pyramid
is supposed to be four.
[56:50]: So I should see 1, 2, 3, 4.
[56:52]: But I'm just seeing 1, 2, 3.
[56:55]: I'm missing one row.
[56:56]: Yeah, exactly, but how do I fix that?
[56:58]: How do I make it four
rows where I need it?
[57:09]: Any suggestions of how
we can change this code?
[57:16]: OK, so seeing some options.
[57:19]: i is less than height plus one, OK.
[57:20]: But that will just stop it.
[57:23]: But, I need to--
[57:25]: I'm missing something on
the very first row, right?
[57:27]: I'm not printing out
anything on the first row.
[57:34]: So maybe we can notice that in the
beginning, we initialize i to zero.
[57:39]: This is where we initialize it for
the first time. i is equal to zero.
[57:43]: So when I do print_row for the first
time, I'm actually doing print_row zero.
[57:47]: So naturally, it's going to go
inside of the print_row function.
[57:50]: And it's going to be
i is less than zero.
[57:52]: But wait, int i equals zero.
[57:55]: i is less than zero already.
[57:57]: This will not work.
[57:59]: So on the first iteration,
print_row function
[58:01]: is actually not going to print anything.
[58:02]: So we need to make some changes.
[58:04]: And for that, to solve
that, we can do i plus one.
[58:08]: So instead of starting at i equals
zero, we can start at i one.
[58:14]: And let's make Mario.
[58:16]: Let's run it.
[58:20]: What is the height of the pyramid?
[58:22]: Four.
[58:22]: And voila, I have my
left-handed pyramid that
[58:26]: goes 1, 2, 3, 4 exactly as we intended.
[58:34]: Your task now is to make
it go the other way.
[58:37]: Flip it.
[58:40]: Make it a right-sided pyramid.
[58:43]: But that you will tackle
in your problem set.
[58:46]: And let me propose just one more thing.
[58:48]: So, for example, what happens if I do
./mario, and then I say negative two?
[58:57]: The pyramid is not printing.
[59:01]: Well, you can't really print
out a pyramid of negative 2.
[59:03]: But I also don't want the program
to kick me out completely.
[59:08]: I want it to give me
give me another chance.
[59:11]: Maybe the next integer
that I was going to put in
[59:13]: was actually going to be a good
integer, a positive integer.
[59:16]: How can I fix that?
[59:18]: What can I do differently?
[59:23]: Yeah, Chahab is saying
do while loop, exactly.
[59:24]: And this is where I promised
that I'm going to show you
[59:27]: how the do while loop works.
[59:28]: So we can do just that.
[59:30]: In the very beginning, we're going
to create a variable, int height.
[59:33]: And then later, we're going
to use our do while loop.
[59:37]: And the way that it is set up is a
little quirky, a little different
[59:43]: from what we saw.
[59:44]: So first, you're going to define the do
action, something that has to happen.
[59:49]: And then the second part is the
condition, your Boolean expression.
[59:52]: While something is true, do x.
[59:55]: So we're going to say
height is less than one.
[01:00:00]: So this is sort of the contrary of
what we're trying to accomplish.
[01:00:04]: So as long as height is less than
one, 0, negative 2, negative 5,
[01:00:08]: keep prompting the user
for the right input.
[01:00:11]: Do not stop until they
give you the right input.
[01:00:14]: So I'm going to move this guy
inside of the do while loop.
[01:00:24]: I'm going to delete
int in front of height
[01:00:26]: because we've already
done that on line 9.
[01:00:29]: And I'm going to make it.
[01:00:32]: So make Mario.
[01:00:35]: Oops, I forgot a semicolon.
[01:00:40]: I'm going to run Mario.
[01:00:42]: I'm going to say negative
2, negative 5, 0, 4.
[01:00:47]: OK, so this is now working as intended.
[01:00:50]: You see that it did not stop
until I gave it the right input.
[01:00:55]: It kept reprompting me until
I gave it the right thing.
[01:01:04]: Any questions about
Mario, the do while loops,
[01:01:08]: for loops, the logic of how
we arrived to this solution?
[01:01:20]: Any questions about this part?
[01:01:33]: Yes, I can go to the bottom.
[01:01:40]: Any questions at all?
[01:01:48]: All right, seeing none.
[01:01:49]: So let's go back and actually do a
quick recap of the print_row function.
[01:01:55]: So we just wrote it, and
we already talked about it.
[01:01:59]: Let's briefly consider, again, what
are the components that go into it.
[01:02:04]: So we had our setup with
print_row int bricks.
[01:02:07]: We're printing out one
row of bricks at a time.
[01:02:10]: And again, just a good habit of
breaking down things as you go.
[01:02:13]: So the first part is return type.
[01:02:16]: As we said, print_row
does not return anything.
[01:02:19]: It just returns-- it just has a
side effect of printing something.
[01:02:22]: Then the function name,
print_row, which, again,
[01:02:24]: can be whatever you choose it to be.
[01:02:26]: Then, the next is input.
[01:02:28]: Int bricks is what we pass
into our print_row function.
[01:02:32]: And then, lastly is some kind of
action that we're going to take,
[01:02:36]: which is print row of bricks.
[01:02:37]: And again, coming back to
our visualization, our boxes,
[01:02:41]: so if print_row is a box, and
something is happening here,
[01:02:46]: so what we pass into this box is bricks.
[01:02:50]: So it is the variable that
we grabbed from the user
[01:02:54]: or that we defined earlier in main.
[01:02:57]: We pass it into print_row.
[01:02:58]: And when we get out of it is a row of
bricks, depending on the size of it.
[01:03:04]: So more specifically,
if we had numbers--
[01:03:08]: if bricks equaled three,
three bricks, we get out
[01:03:12]: is these three hashes in the
end of using print_row function.
[01:03:19]: All right, this was a lot.
[01:03:21]: So I'm going to pause
again and ask if folks
[01:03:23]: have any questions about return types,
printing, placeholders, for loops,
[01:03:29]: conditionals, Mario.
[01:03:30]: We've done so many things today.
[01:03:31]: So if you have any questions at
all, please feel free to type
[01:03:34]: them in the chat in
the next minute or so?
[01:03:51]: All right, not seeing any questions.
[01:03:53]: So this is where we're going to wrap up.
[01:03:56]: So this was CS50 Week 1 section.
[01:04:00]: Thank you so much for tuning in.
[01:04:02]: We're going to be here
every week at the same time.
[01:04:05]: Feel free to log into Zoom
again in the coming weeks.
[01:04:09]: It was so nice to see all of you.
[01:04:11]: My name is Yuliia.
[01:04:11]: And hopefully, I'll see you next week.
[01:04:13]: Bye, everyone.
Flashcards
1 / 3
Loading
Click to reveal answer
Loading
💡 Explanation
Loading
Quizzes
JavaScript Fundamentals Quiz
Test your knowledge of JavaScript basics
180s
Question 2
Which method is used to add an element to the end of an array?
A. push()
B. add()
C. append()
D. insert()
Unlock Interactive Quizzes
Create quizzes from your content in one clickUsers get instant feedback. You get clear accuracy, time, strength & weakness insights and VEGA's next-step recommendations.
🎯 Sign Up for Free to Create Quizzes✓ $10 free AI credit on signup✓ No credit card required
Keynotes
Loading
Summary
Loading
Transcripts
[00:02]: YULIIA ZHUKOVETS: This is CS50 Week 1,
section on C. So if this is the video
[00:06]: you were expecting to hear, you
are definitely in the right place.
[00:09]: My name is Yuliia.
[00:11]: I'm a preceptor here at Harvard.
[00:12]: Here's my email address.
[00:14]: If you have any questions or
would like to get in touch,
[00:17]: I was actually some years ago a TA
at Yale where we also teach CS50,
[00:22]: and now I joined the
team on the Harvard side
[00:24]: and helping them out with the Harvard
College class here on campus, and also,
[00:28]: supporting all of our students online.
[00:30]: So as I mentioned just now,
this is week 1 of CS50,
[00:35]: and we're going to talk about C. So
this is really where everything begins.
[00:40]: And we're going to
dive into some building
[00:42]: blocks that are really going to help
us throughout the whole semester.
[00:44]: And some things on the agenda for
today are variables and operators,
[00:49]: so the most basic, basic, basic
things that we need to get started.
[00:54]: We're going to talk
about functions, what
[00:56]: are functions, what parameters
functions use, how to define functions,
[01:02]: how to use them.
[01:03]: We're going to chat about
conditionals and loops,
[01:05]: so something that will be
really handy in our problem
[01:07]: set, and again, just one
of those stepping stones
[01:10]: to get us through the next weeks.
[01:12]: And we'll get a little
taste of problem set 1.
[01:15]: We're going to talk about
Mario, how to build those bricks
[01:18]: and stack a little, nice pyramid.
[01:21]: So without further
ado, let's get started,
[01:23]: and let's talk about variables,
types, input, and printing.
[01:28]: So you might recall that during
lecture, we talked about a phone book.
[01:34]: Professor Malan showed an
example where, if we're
[01:36]: looking for some contact or a person,
we might flip through the pages
[01:42]: or might go to the middle and
use a more efficient algorithm
[01:45]: of finding that person.
[01:47]: Well, today we're taking
sort of a step down
[01:49]: from the idea of searching to how
are those phone books comprise.
[01:54]: What are the components of those?
[01:56]: So you might imagine that one of the
variables in a phone book is called
[02:00]: "calls."
[02:01]: So maybe it is the number of calls
that one of your friend called you.
[02:07]: And maybe they called you 4 times.
[02:10]: So you can imagine having this
square or a little box that
[02:13]: stores some value, in this case, 4.
[02:16]: And it is called--
[02:17]: it's called "calls."
[02:18]: So what actually is going
on underneath the hood?
[02:22]: What is the code that enables us to have
this little square with the number 4?
[02:27]: Well, it can sound--
[02:29]: it can look a little cryptic for
now, but we will parse it together.
[02:32]: So in order to get this
box with number 4 in it,
[02:35]: the line of code we would
use is "int calls equals 4."
[02:40]: And as such, there are
4 components to it.
[02:43]: The first one is name,
variable name, and it
[02:46]: can be whatever sort of word
or phrase you would prefer,
[02:50]: sort of like calls or name, age, address
you're going to see a little later.
[02:55]: Next one is type.
[02:57]: This is very important.
[02:58]: We need to tell C exactly
what type of value it
[03:02]: should expect to receive on its end.
[03:04]: So in this case, it
is integer, to specify
[03:09]: just a number that doesn't
have any floating point, just
[03:12]: one number, integer.
[03:14]: Then, the value itself, if we created
this box that is called "calls"
[03:19]: and we're supposed to put an integer
into it, we're going to put 4 into it.
[03:22]: That is our value.
[03:23]: And lastly, sort of what
brings everything together
[03:26]: is the assignment
operator, the equals sign.
[03:29]: And you might also read it like this.
[03:33]: "Create an integer named calls
that gets the value--" oops--
[03:40]: "that gets the value 4," combining all
of our 4 components together to create
[03:48]: this variable.
[03:53]: All right, so let's
practice one more time.
[03:56]: Maybe someone in the chat could type in
a similar definition that we just saw.
[04:02]: We have int x equals sign and 50.
[04:06]: How could we combine it in one sentence
in a way that we saw earlier here?
[04:16]: Someone in the chat if you
guys want to type it out?
[04:24]: So we have integer x equals 50.
[04:27]: How can we bring it all
together into one sentence?
[04:37]: All right, seeing none, but
this is how you would say it.
[04:40]: "Create an integer named
x that gets the value 50."
[04:45]: Just reading left to right--
[04:49]: yeah, exactly.
[04:50]: "Create an integer named
x that gets the value 50."
[04:53]: So creating these sentences in your
head and relating code to plain English
[04:59]: or whatever language
you are studying CS in
[05:02]: can really help you get
through these first few steps
[05:05]: of getting used to how to write
code and how to phrase it properly.
[05:09]: Well, let's take it a step further.
[05:12]: So what if we--
[05:13]: what if my friend
called me another time?
[05:16]: They just gave me a call.
[05:17]: And now, the value is, of
course, it should be five.
[05:20]: So how can I change that?
[05:21]: Well, we can use a simple reassignment.
[05:25]: So in the beginning, if we created int
calls equals 4, that was our initial box
[05:30]: we create.
[05:30]: And then, we want to
assign it to five, what
[05:35]: we can do is just have
a second line of code
[05:39]: where we specify, well, now the value
of calls should be five and not 4.
[05:45]: And therefore, the value inside
of our box will change as well.
[05:51]: And again, translating
it to plain English,
[05:54]: the way it would look like
is calls, the variable name,
[05:59]: gets five, combining the name, the
assignment operator, and the value
[06:04]: together.
[06:06]: Well, what if it's not
just a plain number?
[06:09]: What if we want to make
some changes, for example,
[06:12]: incrementing by one every time my
friend or my mom gives me a call?
[06:16]: Well, we can also use different
operations as we're reassigning.
[06:21]: So, for example, if we want to add plus
one, we can do that on the second line.
[06:27]: So calls equals calls plus one.
[06:31]: Or if we want to subtract one, it would
be very similar but calls minus one.
[06:35]: But it gets a little confusing when
we have calls equals calls minus one.
[06:42]: It's just, how can I take calls
and then assign it back to calls?
[06:47]: This is, again, where it
gets a little confusing.
[06:49]: But actually, what's
going on, we're first
[06:51]: taking a look at the right-hand side.
[06:53]: We're grabbing our value 4 that
was in our little box before.
[06:58]: And so we're doing the operation 4
for minus one, which will be three,
[07:02]: and then assigning it
back to our variable calls
[07:07]: in such a way creating the new
box that contains value three.
[07:14]: And you can also use
multiplication and division,
[07:19]: sort of the same idea of grabbing
the initial value that we had
[07:24]: and changing it and reassigning it.
[07:29]: We could also do-- yeah, I see someone
in the chat saying we can also do calls
[07:33]: ++.
[07:33]: If we just want to increment by one, ++
is a very useful operator we will see
[07:39]: shortly when we're
talking about for loops.
[07:41]: But let me pause here
for a second and ask
[07:43]: what questions do we have about
variables, reassignment, operators?
[07:49]: This was sort of our first
chunk of information.
[07:52]: So if you have any questions, feel
free to type them in the chat.
[08:04]: OK, well, let's give
it a few more seconds.
[08:10]: Yeah, and feel free to put any questions
you have in the chat throughout,
[08:13]: and then I'll try to take a little
peek and then address them as we go.
[08:18]: All right, seeing none so let's move on.
[08:21]: So say I didn't have that number of
calls stored in the phone book just yet.
[08:27]: I wanted to get it from the user.
[08:29]: So how could we do that?
[08:30]: How can we get input from the user?
[08:33]: Well, there are a few ways we can do it.
[08:35]: In this case, if we're trying to get
an input that is a number, an integer,
[08:41]: we might want to use a
function called get_int.
[08:43]: And again, the way that
it's structured, we
[08:45]: can break it down in several components.
[08:48]: We are starting with our function.
[08:52]: So this is the tool that will allow
us to grab this input from the user
[08:57]: and then store it in our proverbial box.
[08:59]: So if we have function,
get_int, what it takes in--
[09:06]: I guess coming back a little bit,
the first part is the function name.
[09:10]: Get_int is something that was defined
before, is actually defined by CS50,
[09:15]: and this is in the library
CS50.H that you can also
[09:19]: read a little bit more about.
[09:20]: But someone once upon a
time wrote this function
[09:23]: that we now can use
to grab these values.
[09:27]: Inside of the function
is function input.
[09:30]: In this case, it is calls.
[09:32]: So we're prompting the user to input
the number of calls in the terminal
[09:39]: that we can then later use.
[09:43]: And then, our next
component is talking about--
[09:50]: OK, so we've looked at the code for this
function, but how does it actually work?
[09:57]: A lot of moving pieces.
[09:59]: So we can imagine this box that
represents the get_int function.
[10:04]: So something will go into the box, and
something will come out of the box.
[10:07]: So what goes into the box?
[10:11]: What goes in the box is prompt.
[10:13]: So in this case, it is calls.
[10:15]: And what we get out from it
is some kind of number that
[10:18]: is stored in the variable, calls.
[10:20]: Or if we give a specific example,
that could be, quote, unquote,
[10:26]: "calls colon space" to
give space for our user.
[10:29]: And then, we get back 4 that we
will then store in our variable.
[10:37]: And this is, again, where
we come back to assignment.
[10:40]: So we got this number 4 back.
[10:43]: So what is actually going on?
[10:45]: We're going to take this value.
[10:46]: And then, we're going to store
it in the calls, calls variable.
[10:53]: So zooming out again,
we created an integer
[10:57]: named calls that gets the value 4.
[10:59]: So before this four was
even on the picture,
[11:03]: we had this whole get_int function
where we prompted the user.
[11:07]: We got the number back.
[11:08]: And now, we're using this return value
to store it in the variable, calls--
[11:14]: so again, coming back to our idea
of having this little box that
[11:17]: stores some kind of number.
[11:21]: OK, I see a question.
[11:24]: I have a question about format codes.
[11:25]: What's the difference between
percent I and percent D?
[11:29]: That's a great question.
[11:30]: So %i and %d are interchangeable.
[11:33]: They're both used for integers
if we want to print them out,
[11:35]: which is a perfect segue
because we're going to talk
[11:38]: about printing values in just a second.
[11:40]: But %d is a little deprecated,
and we've been using %i since.
[11:45]: So definitely, default to that.
[11:48]: But as someone in the chat,
how to print out values, right?
[11:53]: So for example, we had
our value int calls 4,
[11:57]: and now we want to print it out.
[11:59]: Well, for that, we can
use function, printf.
[12:01]: And so we can have our string.
[12:06]: So just printing out the number
doesn't really mean anything.
[12:09]: It is nice to give some kind of context.
[12:11]: So in quotes, we can put "calls equals,"
and then %i would be the placeholder,
[12:17]: the placeholder for our variable because
if we did something like calls equals
[12:21]: calls, well, that's just text.
[12:24]: It wouldn't really print
out "calls equals 4."
[12:26]: It's just going to print
out "calls equals calls."
[12:29]: That's not what we want.
[12:30]: So to avoid that, we're going to
have our placeholder or format code.
[12:36]: So %i is one of the options
that's used for integers.
[12:40]: And then, the second
component is having the value,
[12:44]: the variable that stores the
value that we want to print out--
[12:49]: so putting these pieces together.
[12:52]: And there are other
types and format codes.
[12:54]: So for example, for
integers, sorry, for numbers,
[12:57]: we can also use floats for that.
[12:59]: In that case, it would be %f.
[13:01]: For text, we'll be looking into
chars and strings in just a second.
[13:06]: And then, true or false, there
are Boolean, type of Boolean.
[13:10]: We're using %i as well.
[13:14]: So next we're actually going
to write our own program.
[13:19]: So if you want to pull up cs50.dev
with me right now or just follow along,
[13:23]: feel free to do that.
[13:24]: But I'm going to pause again and take
questions, any questions that folks
[13:28]: have right now.
[13:30]: Questions on printing,
types, functions, variables,
[13:37]: anything that we just touched upon?
[13:47]: OK, seeing none but definitely
feel free to pop them into the chat
[13:50]: as we're chatting along.
[13:52]: But let's move over to
cs50.dev now, which is
[13:56]: our environment where we write codes.
[13:59]: Where can I write programs
is a question in the chat.
[14:01]: That's a great question.
[14:03]: So for that, we use cs50.dev, which
is the Visual Studio Code for CS50.
[14:08]: It's this virtual IDE where students
and teachers, and really, anyone
[14:15]: could use to get started with
programming, write their own programs.
[14:19]: It is simplified version of
the desktop Visual Code that
[14:22]: might be familiar to someone and should
be really easy to use and get started.
[14:27]: Well, let's go back to the
problem we're trying to solve.
[14:31]: We're going to write a program
that prints out "Hello, world,"
[14:35]: which is also the first
step of problem set 0.
[14:37]: So we can start checking off some boxes
on our list of programs to complete.
[14:44]: So to start, let's create this program,
and I'm going to call it Hello.
[14:49]: So I'm going to create
a file called hello.c.
[14:52]: And again, there are several components
that go into writing a program.
[14:56]: The first one is
including the libraries.
[14:58]: So I'm going to include stdio.h.
[15:04]: this will allow me to run my program.
[15:06]: And then, I'm going to create
a function, a main function,
[15:11]: like int main(void).
[15:12]: So in every program in this
course and probably in the future,
[15:16]: there's going to be a main function,
the main component of your program
[15:19]: that's going to be
responsible for things.
[15:21]: And inside of it, let's
use the printf function
[15:24]: that we just saw earlier in the slides.
[15:26]: And what we're going to do, we're
going to print out "Hello, world."
[15:33]: Semicolon, and those
are a little tricky,
[15:37]: and odds are you'll
probably forget it not once,
[15:40]: not twice, probably many more times.
[15:43]: But it is just getting
into the habit of it.
[15:47]: So let's go back to the terminal
and now run make hello to compile.
[15:54]: OK, compiles.
[15:55]: And now, we're going to run hello.
[15:58]: And.
[16:00]: it's not pretty.
[16:01]: It's on the same line
with my dollar sign.
[16:04]: So how could I fix that?
[16:06]: How can I separate it into
a new line right here?
[16:10]: Exactly, so I can use backslash
n, which will give me a new line.
[16:16]: And I'm going to run.
[16:17]: Hello again.
[16:19]: Not working.
[16:21]: OK, what step am I missing here?
[16:26]: I changed.
[16:26]: I added my escape character now.
[16:28]: I added the new line, but I
keep running hello, and it's--
[16:32]: exactly, I need to make hello again.
[16:35]: So I-- every time you
make any changes, make
[16:39]: sure you make the program again so
that all the changes are now reflected.
[16:45]: And boom, we have "hello, world" right
here in our terminal on its own line.
[16:53]: Any questions about this?
[16:57]: This is sort of one of those--
[17:03]: one of the first programs that everyone
writes, the "Hello, world" one.
[17:06]: And I think even though
it's a little simple,
[17:09]: there's still mistakes you can make.
[17:12]: There's the new line
character you might forget
[17:14]: or the semicolon or even
including the library.
[17:18]: So you can feel a little proud of
yourself even for getting that far.
[17:24]: Question in the chat, there's
another type like backslash n?
[17:27]: Yeah, so this is sort
of the escape character
[17:30]: that we were just talking about.
[17:31]: So for certain things,
you will need that.
[17:33]: For example, if I wanted to
use quotes around "Hello,"
[17:38]: I can't just put double quotes by
themselves because the program is going
[17:45]: to read it as if I'm just trying
to create a separate string.
[17:48]: What I could do is use
an escape character
[17:51]: and then put hello in
the quotes in such a way.
[17:58]: So let's show it.
[18:00]: Make Hello.
[18:02]: Hello.
[18:03]: So now, I have my quotes around hello.
[18:08]: Let me get rid of this so that we
can go back to what we saw before.
[18:12]: I'm going to make hello again, run it.
[18:17]: OK, hello, world.
[18:19]: Now, let's go back to
our slides and see.
[18:22]: The next thing we're going to accomplish
is let's write a program, "Hello, me"
[18:26]: or "Hello, your name" to complete
the second set of problem set 0.
[18:30]: Going back to cs50.dev now, and let's
keep editing just in this file, hello.c.
[18:35]: So my name is Yuliia.
[18:38]: And I could just do, "Hello, Yuliia."
[18:42]: I'm going to make hello.
[18:45]: I'm going to run hello.
[18:47]: Hello, Yuliia.
[18:48]: So this does a trick.
[18:51]: This is it.
[18:51]: The program is done.
[18:52]: We can now move on to
our next challenge.
[18:55]: What could we change here to actually
make it say, "Hello, name, Hello,
[19:04]: Daniel."
[19:08]: Exactly, we can get user input.
[19:10]: So instead of just hard coding
Yuliia, baking it inside of my code,
[19:14]: I can do something different.
[19:15]: I can, again, get input from the user.
[19:18]: So for that, I'm going to
create a variable called name,
[19:22]: and it's going to have type string,
which is what we use for words.
[19:26]: I'm going to use another
function called get_string.
[19:29]: Very similar to get_int, it's just
meant to getting strings from users,
[19:33]: not integers.
[19:34]: And then, I'm going to
ask, "what is your name?"
[19:38]: Leaving space, not forgetting
about the semicolon.
[19:41]: And then here, I'm going
to say, "Hello, name."
[19:46]: OK, make hello.
[19:49]: Oh, OK, here's our first error, right?
[19:53]: They're super cryptic.
[19:55]: This is a lot--
[19:57]: use of undeclared identifier,
Did you mean stdin?
[20:05]: Very cryptic.
[20:06]: Nica is saying, "include cs50."
[20:08]: Exactly, great thinking.
[20:11]: So what I didn't mention earlier
when we were talking about get_int
[20:16]: is that they're coming
from the CS50 library.
[20:19]: So for that, I'm going
to include cs50.h.
[20:27]: And so this will allow
us to use now string
[20:31]: and get_string to get user's input.
[20:34]: So let's make hello again.
[20:37]: Let's run hello.
[20:39]: What is your name, Yuliia?
[20:41]: Uh-oh, hello, name.
[20:44]: Isn't supposed to
print, "Hello, Yuliia?"
[20:48]: What is going on here?
[20:51]: OK, Nica's saying &as, but
where does exactly that %as go?
[21:02]: Uh-huh, OK, after me,
exactly, instead of name.
[21:05]: Perfect, yeah.
[21:06]: You guys are so good at it.
[21:07]: So instead of just baking in just string
name inside of my input of printf,
[21:14]: I actually want to
make it dynamic, right?
[21:17]: I want to be able to change it.
[21:18]: So for that, I'm going to go back to our
placeholders or these percent operators.
[21:26]: And after the comma,
I'm going to put name.
[21:29]: So what I'm saying is the text itself
is going to be "Hello, comma, space",
[21:34]: and then I'm saving a placeholder for
a value stored in variable name that is
[21:39]: of type string.
[21:40]: So you see how these pieces are
connecting between each other, string
[21:45]: name, variable names of type string?
[21:47]: So we use a %as operator.
[21:50]: All these pieces are connected together.
[21:52]: So now, let's make hello again.
[21:55]: Let's run it.
[21:56]: What is your name?
[21:57]: Yuliia.
[21:58]: Yay, OK, this is going well.
[22:00]: Let's try it again.
[22:01]: Hello, what is your
name, for example, David?
[22:04]: Hello, David.
[22:06]: All right, so let me scroll.
[22:08]: I see some questions in the
chat so let me scroll and see.
[22:12]: Gigi's saying, can you
explain what %as does?
[22:14]: Yeah, totally.
[22:15]: So remember how just now when
we were talking about integers,
[22:20]: we had our placeholder for i so that
we can print out variables calls.
[22:25]: So we can't really just
bake in calls equals calls.
[22:30]: We have to create some
kind of placeholder
[22:32]: to have this dynamic interaction between
the variable and grabbing the value.
[22:38]: So a similar thing is going on here.
[22:41]: We want to make sure that we have
the placeholder for our variable name
[22:44]: so that whatever user types in,
like David Daniel, Andrew, Bob,
[22:48]: doesn't matter, we can put
it in inside of our string
[22:53]: that is ultimately in printf.
[22:57]: OK, let's see what
other questions we have.
[23:05]: All right.
[23:08]: OK, any other questions about this
program, hello, me or hello, world.
[23:16]: OK, all right, seeing none.
[23:20]: So let's move on to our next exercise.
[23:24]: So next, what we're going
to do is create a program
[23:28]: that stores and prints
out some information,
[23:31]: like name, age, phone number, can be
address or hometown of your friends,
[23:39]: so adding some more layers
to our hello, me problem.
[23:46]: So for that, let's create a
new file called friends.c.
[23:51]: All right, it opened up
here in the terminal.
[23:54]: Let's create sort of the same
steps that we've done before.
[23:57]: So we want to include stdio.h library.
[24:03]: We want to have int
main(void), curly braces.
[24:10]: OK, so what we're going
to do next is a few steps.
[24:15]: So let's try to think what
attributes my friends can have.
[24:21]: So the first one was name.
[24:24]: That's easy.
[24:25]: That's something that
we just talked about.
[24:27]: What other things can we
ask our friends to input?
[24:33]: Age, yeah, so maybe age.
[24:38]: Height, yeah, that's a good one.
[24:41]: Let's maybe do hometown.
[24:48]: And maybe, let's do phone number.
[24:57]: All right, so as you might
see, these are commented out.
[25:02]: These are not lines of code.
[25:03]: These are not doing anything.
[25:05]: These are comments.
[25:05]: And the way you can do them is
by doing two slashes in a row.
[25:09]: So for get name, we're going
to do something very similar
[25:13]: that we just did in hello.c, string
name, get_String, what is your name.
[25:17]: So let's do just that.
[25:19]: So I'm going to create a variable
called string name get_String.
[25:26]: I'm going to ask, what
is your name, semicolon.
[25:32]: For age now, I'm going
to use an integer.
[25:39]: So oops, I'm going to do int age
equals get_int, what is your age?
[25:54]: OK, semicolon.
[25:55]: Same for hometown, I'm going to
say string hometown, get_string,
[26:04]: What is your hometown, semicolon.
[26:12]: And then, for phone number, I'm going
to do int number equals get_String--
[26:19]: sorry, get_int, what
is your phone number?
[26:29]: And I think I forgot to
include CS50 library again.
[26:36]: So let's not make the same mistake.
[26:38]: Let's come back and put it here on top.
[26:42]: OK, let's start with this.
[26:44]: Let's make sure we can
grab our variables.
[26:47]: Thank you, Ana.
[26:48]: Yep, good eyes.
[26:50]: Forgot to include the library.
[26:51]: So let's make this first and then
see if we're getting any errors.
[26:56]: Fingers crossed.
[26:56]: None, that's awesome.
[26:58]: And then, let's run it to see if we're
getting all the right inputs that we
[27:02]: want.
[27:03]: So what is your name?
[27:04]: Yuliia.
[27:05]: What is your age?
[27:06]: 22.
[27:07]: What is your hometown?
[27:08]: Cambridge.
[27:10]: And then, what is your phone number?
[27:12]: I'm going to say 111-111-1111.
[27:20]: So it didn't like it.
[27:23]: Let me do this again.
[27:25]: So it's prompting me again.
[27:30]: What's going on here?
[27:32]: Why can I not put in my phone number?
[27:34]: Oh, wait, it's a fake phone number.
[27:36]: But-- Yeah, exactly.
[27:39]: So parentheses doesn't
count as an infinite string.
[27:42]: So notice how in line
16 right here, I defined
[27:45]: number, the variable to get to call--
[27:49]: excuse me.
[27:50]: I defined number, the variable that's
going to store my phone number,
[27:54]: as an int.
[27:55]: But there are a lot of different
formats in which you can write it.
[27:58]: Surely, you could have
done like 1111111111.
[28:03]: This works.
[28:04]: But what if I want to include
the parentheses and the dashes?
[28:06]: So since we're not going to do any
mathematical operations with the number,
[28:11]: it is totally fine to
just store it in a string
[28:13]: and allow for those
different formats to come in.
[28:18]: So let's make this again.
[28:20]: Let's make the terminal
slightly bigger, make friends.
[28:26]: Let's run it.
[28:30]: OK.
[28:31]: Yuliia, 22, Cambridge.
[28:35]: Now let's try this format again.
[28:40]: Yay, now it works.
[28:42]: OK, awesome.
[28:43]: So changing it to a
string did the trick.
[28:45]: Now, we can put whatever
formats we want.
[28:49]: Let me pause here for a second.
[28:51]: Any questions about how we
just got all these user inputs?
[29:04]: Yeah, so Paula said, why did we
define the output for main as int?
[29:08]: That is a great question.
[29:09]: And actually, Professor Malan is going
to address that in the coming lectures.
[29:12]: But this is really a conventional
way to write main functions.
[29:16]: So it doesn't necessarily
take anything as an input.
[29:19]: So that's why we leave it as void.
[29:21]: But we do expect to
get a number from it.
[29:25]: And you can notice that, here, we're
not actually returning anything.
[29:28]: Even the printf, the printf action
is not actually returning a value.
[29:35]: It's more of a side effect
that we have in the terminal.
[29:39]: But good question.
[29:43]: Daniel said, will casting
work in this context?
[29:47]: In the context of putting in the
user's input, probably no because we
[29:52]: can't even cast something
that we haven't received yet.
[29:55]: So we want to make sure we are defining
the correct variables right away.
[30:02]: Tatia said, when we use two
backslashes, sorry, two slashes, that's
[30:07]: when we want to create comments, right?
[30:10]: So right here, when I'm just leaving
little hints for myself to remember what
[30:16]: I was typing.
[30:18]: Let's move along and now print it out.
[30:21]: So we have all these users
input that we just got.
[30:25]: Now let's put it all together.
[30:28]: So let's create sort of a lengthy
sentence where we're going to say,
[30:34]: "My new friend's name is %s."
[30:43]: And then I'm going to
say, "comma %i" for age.
[30:51]: "They are from %s" again
because I'm using hometown now.
[30:57]: And "Their phone number is %s" again.
[31:05]: So now, we just need to
match it back together.
[31:09]: So the first %s was referring to a name.
[31:13]: The next %s was referring to an age.
[31:17]: The third one was referring to hometown.
[31:22]: And the second one was
referring to a number.
[31:26]: So you can, again, see how all
these things come back together.
[31:31]: So we started off by creating users
input, by grabbing users input,
[31:37]: creating variables, name, age, hometown,
number, storing them in the variables
[31:42]: property types.
[31:43]: And then we're printing out all
of these variables in a sentence
[31:47]: by using these placeholders.
[31:50]: So let's make this.
[31:52]: So "make friends."
[31:54]: Oops, I forgot a semicolon.
[31:57]: See, I told you it was going
to happen at some point.
[31:59]: And it happened to me.
[32:00]: So let's do it again.
[32:01]: So "make friends."
[32:04]: OK, compiles friends.
[32:08]: Let's run it.
[32:09]: What is your name?
[32:10]: Yuliia.
[32:11]: What is your age?
[32:12]: 22.
[32:13]: What is your hometown?
[32:14]: Cambridge.
[32:16]: What is your phone number?
[32:17]: 1111111111.
[32:20]: OK, "My new friend's name is
Yuliia, 22, they are from Cambridge,
[32:23]: and their phone number
is 111--" lots of ones.
[32:27]: And I think I'm forgetting a new line
because I see that my ones are now
[32:32]: getting on the next one.
[32:33]: So I'm just going to create that here.
[32:36]: Let me compile it again.
[32:39]: What's your name?
[32:40]: Yuliia, 22, Cambridge, 1111111111.
[32:46]: OK, so still going a
little bit over the line.
[32:49]: But that is the program
that we just wrote.
[32:56]: OK, let me bring the
terminal down a little bit.
[33:01]: Ann said, what's the
difference between %s and %i?
[33:05]: That's a great question.
[33:06]: So you notice how here
we define name as string
[33:10]: and then hometown as string and
then number as string as well.
[33:13]: These are sort of text or words.
[33:16]: And this is exactly
why we want to create
[33:17]: these different placeholders to
distinguish between the different types
[33:21]: that we have.
[33:21]: So placeholder s is
for strings or words,
[33:25]: and placeholder i is for
integers, which is just numbers.
[33:33]: Gigi said, what does make file name do?
[33:36]: So when we do something like make
friends or make hello, what it does,
[33:43]: it takes the machine
code and compiles it--
[33:46]: sorry.
[33:47]: It takes the source code and
compiles it into machine code
[33:49]: so that the computer
can actually read it.
[33:52]: And we will see different
kind of variations of this.
[33:55]: For example, Python is
interpreted language.
[33:57]: We don't need to compile
it before we run it.
[34:00]: The computer does that for us.
[34:02]: But C is a little bit more
like lower controlled language.
[34:05]: So we want to make sure we are
making or compiling the program
[34:08]: first before we actually run it.
[34:11]: But great questions.
[34:14]: OK, all right, let's move on.
[34:17]: So we've talked about
return types and printing.
[34:22]: Now, let's go into our next
part of loops and conditionals.
[34:28]: So let's talk about conditionals first.
[34:31]: So you might see something like this--
[34:34]: if calls less than 1,
printf "Call more often!"
[34:38]: But what is actually going on here?
[34:42]: Well, again, several components
to it, it's just like LEGOs.
[34:45]: It's like building blocks.
[34:46]: So the first one is Boolean expression.
[34:48]: We're checking some kind of condition.
[34:50]: We're taking that
variable, calls, where we
[34:53]: are storing four or
five or any other value,
[34:55]: and we're checking if it is
less than one in this case.
[35:00]: If the expression is true, we're
then going inside of our conditional,
[35:08]: these curly braces that are
hugging our printf statement.
[35:11]: If the Boolean expression
is actually true,
[35:13]: we're going to execute
this conditional code.
[35:15]: So if calls is less than one, if
it's zero or maybe even negative,
[35:20]: which is probably not possible,
but we're going to printf,
[35:23]: "Call more often!"
[35:28]: If we have multiple situations
that we would like to address,
[35:31]: this is where else statements come in.
[35:34]: So the first one can still be "if
calls is less than one," right?
[35:38]: This is our first condition
that we want to check.
[35:40]: And then, else takes care
of all the other options.
[35:44]: So if calls is not less than one, we
want to printf, "Thanks for calling!"
[35:50]: So it means that calls
is either one or more.
[35:53]: So the Boolean expression
was indeed false,
[35:56]: and we jumped to our next part of code.
[36:00]: And these are mutually exclusive.
[36:02]: So either one or the other
can happen, and they're
[36:07]: separated by this else in the middle.
[36:12]: All right.
[36:14]: Chahab asked, referring to the
previous exercise, in sake of style,
[36:18]: is it better to print out every
information separate line?
[36:20]: That's a great question.
[36:21]: Yeah, you could have noticed that my
line was getting a little long, right?
[36:26]: It was going off screen.
[36:29]: It depends on the situation.
[36:30]: Sometimes, you want to print out
everything in one go and then
[36:34]: in the terminal jump between the lines.
[36:36]: But sometimes, you want to print it
out in different printf statements.
[36:41]: And that's totally fine as well.
[36:43]: All right.
[36:45]: Let's move into loops.
[36:47]: So next, we're going to talk
about while loops and for loops.
[36:50]: And let's start with while loops.
[36:52]: Again, we're going to break
down some of the components.
[36:55]: And the first one that we have going
top to bottom is initialization.
[36:59]: So we're creating this variable
i, and we're setting it to zero.
[37:04]: And that's going to be our count, right?
[37:06]: It's going to be the
variable that helps us
[37:08]: make sure we're doing the right
number of repetitions or loops.
[37:12]: So the next part is Boolean
expression, something that we just saw.
[37:16]: The comparison is going on.
[37:18]: We're going to get either true or false.
[37:20]: And if it is true, we're going
to go into our next chunk
[37:24]: in the code that is hugged
by these curly braces.
[37:27]: So if the Boolean expression
is, indeed, correct,
[37:33]: we're going to go inside
of our while conditional,
[37:40]: and I'm missing a step here.
[37:42]: We are first going to
go into printf, right?
[37:46]: We're still going top to bottom.
[37:48]: So we're going to do printf.
[37:50]: We're going to print out i.
[37:52]: And the first iteration
is going to be zero.
[37:54]: And then, we're going to
increment, bumping an i by one,
[37:57]: and then going into the
Boolean expression again.
[38:01]: So let's visualize it.
[38:03]: So again, let's go back to our boxes.
[38:06]: Let's try to picture
these things in our heads.
[38:09]: So again, we're starting
with int i equals zero.
[38:11]: We're creating this box.
[38:12]: We're storing value zero.
[38:14]: Second part is, is i less than one?
[38:18]: And it's sort of this message
or idea that is going on.
[38:21]: Nothing gets printed out.
[38:22]: Nothing gets returned
or changed in this step.
[38:25]: But it's still a step
that we need to make.
[38:27]: So we're asking ourselves,
is i less than two?
[38:30]: And i is less than two.
[38:31]: It's zero.
[38:32]: So then, we're going
inside the curly braces.
[38:34]: We're going to printf, print out
the value that i currently has,
[38:39]: which is zero.
[38:40]: And next step, we're going
to increment i by one.
[38:43]: And as someone said before, another
option, another way to do it is i++.
[38:48]: Both formats are correct.
[38:51]: Next, we're going to go back
to our Boolean expression.
[38:54]: Note that we're not going
back to int i equals zero.
[38:58]: We've done that step.
[38:59]: We're past it.
[39:01]: We're now coming back to
the Boolean expression.
[39:03]: We ask ourselves, is i less than two?
[39:06]: Well, i is one, so yes, it is true.
[39:09]: We're going back inside.
[39:11]: We're printing out i, which
is, in this case, one.
[39:15]: And now we're incrementing i by one
again and now repeating the process
[39:22]: again and again.
[39:23]: So is i less than two?
[39:24]: Well, i is not less than 2.
[39:26]: i is 2.
[39:27]: So our Boolean expression is false.
[39:31]: And now, we will finish the program.
[39:35]: We're going to be passed this while loop
and just left with what's on the right.
[39:44]: OK, so what are some other
ways that we could do things?
[39:49]: So let's consider this block
inside, the printf and the i++.
[39:54]: Let's try translating it into for
loops like one of our other tools
[39:57]: that we have.
[39:58]: So notice how we're still
grabbing the printf portion.
[40:02]: That is the part of code that's
going to go with us to the for loops.
[40:06]: And going to the next
slide, we see exactly that.
[40:09]: So we kept the printf part.
[40:11]: And now, the first line
looks very cryptic.
[40:14]: What is going on?
[40:15]: I see i++.
[40:15]: It looks similar.
[40:16]: i less than two but completely
different format, right?
[40:20]: It's actually all the same things that
we just saw on the previous slide.
[40:23]: So just like with the while loops,
we're defining int i equals zero.
[40:28]: Just as with while loops, we're
setting up our Boolean expression,
[40:32]: which is i less than 2.
[40:33]: And just as while loops,
we're incrementing i++.
[40:36]: So we can think about
it in the same ways
[40:38]: that we did with while loops, the
initialization, the Boolean expression,
[40:43]: and the increment.
[40:44]: And then ultimately, we will
printf our i in this case.
[40:52]: So still going through the same
steps of initializing i to zero,
[40:57]: leaving it aside, and then going
into the for loop, printing out
[41:02]: i one by one until we reach false
with our Boolean expression.
[41:08]: OK, before we jump to the next part,
which is actually talking about Mario,
[41:13]: one of the problems on the
problem set, what questions
[41:16]: do we have about conditionals,
for loops or while loops?
[41:23]: I'm going to stop here
to look at the chat.
[41:40]: What is the do loop syntax?
[41:42]: I think you're referring
to the do while loop.
[41:44]: And we'll actually see
it in just a second
[41:46]: because we're going to talk
about it as we're writing Mario.
[41:50]: Good question.
[41:55]: Any other questions about for
loop, conditionals, while loops?
[42:06]: Yeah so Remy says, what
to use what in these?
[42:08]: So it really depends on what
you're trying to accomplish.
[42:11]: And as you saw, they're
pretty interchangeable.
[42:13]: We can use either while
loops or for loops.
[42:16]: Either or is fine.
[42:17]: But the way that I
like to think about it,
[42:20]: you want to use for loops when
you know the exact number of steps
[42:23]: you want to take.
[42:24]: You know that you're
going to take two steps.
[42:26]: You can set the Boolean
expression correctly and easily.
[42:30]: And the while loops are
sort of a more fluid.
[42:33]: In this case, it is very similar.
[42:35]: We can translate while loop
into the for loop easily.
[42:38]: But sometimes we don't know the
exact like i is less than two.
[42:43]: Maybe it's some other condition.
[42:44]: Maybe it's, is it empty?
[42:46]: Is some variable empty, or have
we reached the end of something?
[42:50]: So with while, you have more freedom
to play around with different Boolean
[42:54]: expressions that you want to set versus
for loops are more strict in that sense.
[42:59]: You have to have two, three, four count.
[43:03]: All right, so let's move on, and
let's talk about Mario, which I think
[43:07]: is everyone's favorite problem set.
[43:08]: And it's a lot of fun.
[43:10]: But the problem at hand that we have
is that if you are not familiar,
[43:13]: Mario is a video game
where a character called
[43:18]: Mario hops around this
imaginary world that
[43:21]: is built of bricks and other things.
[43:23]: So one of the images from
the game is this pyramid
[43:27]: that goes dun, dun, dun, step by
step, which is exactly what we
[43:32]: will try to build today.
[43:33]: So we will not build such a
fancy image as we see here.
[43:38]: But we would want to
build something like this,
[43:41]: like a right-sided pyramid
that goes up in a similar way
[43:47]: as it does on the image.
[43:49]: But let's start with a
left-aligned pyramid first.
[43:52]: This will be a little easier to tackle.
[43:55]: And for that, let's go
back to our VS Code.
[43:58]: I'm going to clear the terminal,
and I'm going to create--
[44:03]: oops.
[44:04]: I'm going to create a
new file called Mario.
[44:09]: All right, I'm going
to close these friends.
[44:13]: I'm going to clear the terminal.
[44:14]: So, again, building
blocks, we have to make
[44:18]: sure we're including all the necessary
things for our program to run.
[44:21]: And let's start with our headers.
[44:23]: So we're going to start with stdio.h.
[44:27]: I already know that I will
need the CS50 library,
[44:31]: so I'm going to code that as well.
[44:33]: I'm going to create int main
void, our main function.
[44:40]: And then, inside this
main function, I'm going
[44:43]: to leave some comments for myself just
to help me get through this process.
[44:46]: So the first one will be
prompt user for input.
[44:50]: So as we saw here, the pyramid
can be different sizes.
[44:55]: So in this case, the
pyramid is of height six,
[44:58]: but it can be of height 5, 4, 3, 2.
[45:01]: We will leave this up to the user.
[45:05]: Then, the second step's print a pyramid.
[45:11]: Print a pyramid of that height.
[45:16]: So many typos.
[45:17]: All right, well, this is simple, right?
[45:19]: Two steps.
[45:20]: Why is everyone's making
such a big deal out of it?
[45:24]: It's just two steps.
[45:25]: But it takes time, right?
[45:27]: It takes, again, building blocks
and components to make this work.
[45:30]: So the first one, we've tackled before.
[45:32]: So we can deal with this right away.
[45:34]: So let's call it int height,
and I'm going to print.
[45:40]: I'm going to prompt my
user for an integer.
[45:45]: And I'm going to say, what is the height
of the pyramid, question mark, leave
[45:51]: space, semicolon.
[45:52]: OK, next we want to print a
pyramid of that height, right?
[45:58]: And that's where things
get a little bit tricky.
[46:01]: How can we go from 0 to 100?
[46:05]: How can we go from knowing
just how to print maybe one
[46:08]: hash at a time to printing multiple?
[46:12]: And I think you just saw me giving
away the next slide a little bit.
[46:16]: But let's work on this together.
[46:20]: All right, so again, building blocks.
[46:24]: Let's start with the idea
of printing a row, right?
[46:27]: So I want to print one
row at a time, and this
[46:31]: is what I'm going to start with.
[46:32]: And for that, I'm going to create
a separate helper function.
[46:35]: So I'm going to call it print_row.
[46:38]: And it has return type void and the
input void because we're not taking--
[46:44]: sorry.
[46:44]: We're going to return type
void, but we're actually
[46:46]: going to take in something.
[46:47]: We want to make sure that we're
printing different length.
[46:52]: Our rows have to be different.
[46:54]: So we're going to put in the
variable called int bricks.
[46:58]: Tell me how many bricks I
need to print on each row.
[47:02]: And here, what I'm going to
do is printf, printf hashtag.
[47:15]: And then, in here, what
I'm going to do, I'm
[47:17]: just going to say print_row 4 semicolon.
[47:22]: So we've created a
function, print_row, that
[47:24]: will take in the variable, int bricks.
[47:28]: We'll print some hashes.
[47:30]: And the way I know how many bricks to
print is by passing in this value here.
[47:34]: So let me make Mario.
[47:37]: And I am running into the first issue.
[47:39]: So call to undeclared
function to print_row.
[47:45]: How do I tackle this one?
[47:50]: Oh, beyond forgetting this guy, yes.
[47:53]: But how do I tackle the error?
[48:02]: Exactly, I also need to make sure
I prototype it ahead of main.
[48:07]: So what I'm going to do,
and as [INAUDIBLE] said,
[48:10]: this is the only acceptable time
that you can copy-paste in CS50.
[48:12]: But I'm going to copy this
guy and put it on the very top
[48:18]: before my main event starts.
[48:20]: And the reason I'm going
to do that is because I'm
[48:23]: reading my program top to bottom.
[48:25]: So I'm going inside of main,
and I'm going through line nine.
[48:28]: I'm getting input from the user.
[48:30]: And I'm getting line 12.
[48:31]: And without this prototype, main
does not know what print_row is.
[48:36]: It can't look ahead and jump below it.
[48:39]: It just sees what it is, what it has
right now, and what it's seen before.
[48:43]: So in order to avoid these errors, we
have to prototype this function before.
[48:48]: Let's try to make it again.
[48:52]: So make Mario.
[48:54]: OK, compiles, that's a good sign.
[48:57]: And now, we're going to run it.
[48:58]: What is the height of the pyramid?
[49:00]: For example, five.
[49:03]: OK, I'm getting just one hash sign.
[49:08]: This is not at all what I wanted to do.
[49:10]: OK, I'm seeing some
suggestions in the chat.
[49:15]: This was solving our scope issue.
[49:17]: OK, someone seems to put the entire
solution in the chat already,
[49:22]: but let's take it step by step.
[49:24]: So right now, I'm just sort
of printing our hash once.
[49:28]: What I really want to do is have some
kind of for loop, do it multiple times.
[49:34]: And for that, I'm going to
do just this, int i equals 0.
[49:41]: This is how we initialize it.
[49:42]: We're going to print out
some number of bricks.
[49:46]: And bricks is exactly the variable
that gives us that information.
[49:51]: So I'm going to set i less than bricks.
[49:54]: And I'm going to
increment i at every step.
[50:00]: I'm going to move printf hashtag
inside of the for loop, indent it.
[50:08]: Let's make Mario again.
[50:10]: OK, height of the pyramid?
[50:14]: Let's say four.
[50:16]: OK, it's printing out four hashes.
[50:18]: So that's a good--
[50:20]: that's a good sign.
[50:21]: But what if I want to make five hashes?
[50:25]: Still printing out four.
[50:27]: Where is my mistake here?
[50:31]: Why does it keep printing out four
hashes even if I put five as a user?
[50:43]: Any suggestions?
[50:48]: So I can see that--
[50:50]: yeah, exactly.
[50:51]: I have print_row four.
[50:53]: So I don't want to hard
code the number, right?
[50:56]: I want to make it dynamic.
[50:57]: So I'm going to change it to height.
[50:59]: And this way, whatever
user input puts in,
[51:02]: I will be able to
dynamically change that.
[51:08]: So let's make Mario
again, and let's run it.
[51:12]: What is the height of the pyramid?
[51:13]: Let's say six.
[51:14]: And I'm seeing six hashes.
[51:15]: OK, that's a good sign.
[51:17]: But again, I'm missing the new line.
[51:20]: That's just so annoying.
[51:21]: So I'm going to go in here
and create a new line.
[51:25]: And so I'm going to make Mario, Mario.
[51:27]: What's the height of the pyramid?
[51:28]: Five.
[51:30]: Ugh, this is not what I wanted again.
[51:32]: It's printing out in the column.
[51:33]: I still want it in the row.
[51:35]: What can I do different?
[51:38]: How can I change this?
[51:45]: Create a new line at the
end of the loop, exactly.
[51:48]: So instead of creating
a new line every time
[51:51]: I go through my for loop,
what I'm going to do
[51:53]: is just printf a new line right here.
[51:59]: And let's make Mario--
[52:01]: oops-- semicolon, make Mario.
[52:08]: Run Mario.
[52:08]: What is the height of the pyramid?
[52:09]: Five.
[52:10]: OK, we have a row.
[52:13]: We have a row, people.
[52:14]: This is awesome.
[52:14]: So this is one of the first steps in
our mission of creating a Mario pyramid.
[52:22]: OK, so what we did so far is created
a separate function called print_row.
[52:29]: We created a for loop in
which we print out hashes.
[52:32]: We also print out a new line every
time we finish that for loop.
[52:35]: And then, we've already preset
up some of those building blocks
[52:38]: inside of the main function.
[52:42]: I'm going to pause
right here for a second
[52:44]: and ask what questions do we
have about these steps so far.
[52:54]: What questions do we have about this
code, the way we used our various tools?
[53:07]: Any questions about this so far?
[53:16]: All right, seeing none so
let's keep moving along.
[53:19]: So let me just run this one more time
to illustrate what we're tackling here.
[53:25]: So right now, I'm just
printing out one row at a time.
[53:28]: But I want to have this
stepping stone construction.
[53:34]: I want to have multiple rows.
[53:35]: So how can I do that?
[53:36]: Well, we're going to make
use of for loops again.
[53:40]: So instead of just calling
print_row function one time,
[53:43]: we can call it multiple times,
again, using our for loops.
[53:46]: So in here, I'm going to
create "for int i equals 0.
[53:52]: i is less than height.
[53:56]: i++."
[53:58]: And then inside of the for loop,
I'm going to move this line of code
[54:09]: right here.
[54:11]: OK, let's make it.
[54:15]: Make Mario, Mario.
[54:20]: What's the height of the pyramid?
[54:21]: Four.
[54:22]: OK, so this is resembling what
we need to do a little better,
[54:26]: so just having 4x4 grids,
like a little square.
[54:32]: OK, let's see.
[54:34]: I see a question in the chat.
[54:35]: In the print_row int bricks, and
declare any type of variable,
[54:38]: int is if we can manipulate by
using inputs, width and height.
[54:44]: Yeah, so there are
different functions which
[54:48]: have different variables that they use.
[54:50]: So even though we put
in height right here,
[54:56]: we're referencing bricks
inside of the print_row
[54:59]: because that is the variable--
that is the variable that is
[55:03]: defined within the scope of print_row.
[55:06]: And therefore, if my
height in bricks just
[55:09]: match where I want them to
match on the print_row input,
[55:13]: I can just be using bricks
inside of my function
[55:16]: because this is what I defined earlier.
[55:20]: So here's the issue.
[55:24]: And let me propose that we can solve it.
[55:26]: But we have a 4x4 grid.
[55:31]: And it just seems to be doing the
same thing over and over again, right?
[55:34]: It prints four hashes four times.
[55:37]: It's very static, right?
[55:40]: It's sort of almost
constant, except for the fact
[55:43]: that we can get a different
input from the user.
[55:45]: How can we make it more dynamic?
[55:48]: What is the variable
inside our main function
[55:51]: that will give us this dynamic option?
[55:54]: What is the variable inside
our main function that
[55:57]: keeps changing with every iteration?
[56:00]: Remy said, replace height with i.
[56:02]: Exactly, so you can notice that
inside of our main function,
[56:07]: we have two variables, height and i.
[56:11]: And in fact, height, after the user
inputted that number, that is it, right?
[56:16]: That is where it stays.
[56:17]: It does not change.
[56:19]: But i, on the other hand, changes
with every iteration of the for loop.
[56:23]: It increments by one every time.
[56:25]: So let's do just that.
[56:27]: Let's make Mario.
[56:29]: Let's run Mario.
[56:31]: What is the height of the pyramid?
[56:32]: Four.
[56:34]: OK, this is looking a
little better, but I would
[56:37]: say that I'm still missing something.
[56:41]: What am I missing here?
[56:48]: The height of the pyramid
is supposed to be four.
[56:50]: So I should see 1, 2, 3, 4.
[56:52]: But I'm just seeing 1, 2, 3.
[56:55]: I'm missing one row.
[56:56]: Yeah, exactly, but how do I fix that?
[56:58]: How do I make it four
rows where I need it?
[57:09]: Any suggestions of how
we can change this code?
[57:16]: OK, so seeing some options.
[57:19]: i is less than height plus one, OK.
[57:20]: But that will just stop it.
[57:23]: But, I need to--
[57:25]: I'm missing something on
the very first row, right?
[57:27]: I'm not printing out
anything on the first row.
[57:34]: So maybe we can notice that in the
beginning, we initialize i to zero.
[57:39]: This is where we initialize it for
the first time. i is equal to zero.
[57:43]: So when I do print_row for the first
time, I'm actually doing print_row zero.
[57:47]: So naturally, it's going to go
inside of the print_row function.
[57:50]: And it's going to be
i is less than zero.
[57:52]: But wait, int i equals zero.
[57:55]: i is less than zero already.
[57:57]: This will not work.
[57:59]: So on the first iteration,
print_row function
[58:01]: is actually not going to print anything.
[58:02]: So we need to make some changes.
[58:04]: And for that, to solve
that, we can do i plus one.
[58:08]: So instead of starting at i equals
zero, we can start at i one.
[58:14]: And let's make Mario.
[58:16]: Let's run it.
[58:20]: What is the height of the pyramid?
[58:22]: Four.
[58:22]: And voila, I have my
left-handed pyramid that
[58:26]: goes 1, 2, 3, 4 exactly as we intended.
[58:34]: Your task now is to make
it go the other way.
[58:37]: Flip it.
[58:40]: Make it a right-sided pyramid.
[58:43]: But that you will tackle
in your problem set.
[58:46]: And let me propose just one more thing.
[58:48]: So, for example, what happens if I do
./mario, and then I say negative two?
[58:57]: The pyramid is not printing.
[59:01]: Well, you can't really print
out a pyramid of negative 2.
[59:03]: But I also don't want the program
to kick me out completely.
[59:08]: I want it to give me
give me another chance.
[59:11]: Maybe the next integer
that I was going to put in
[59:13]: was actually going to be a good
integer, a positive integer.
[59:16]: How can I fix that?
[59:18]: What can I do differently?
[59:23]: Yeah, Chahab is saying
do while loop, exactly.
[59:24]: And this is where I promised
that I'm going to show you
[59:27]: how the do while loop works.
[59:28]: So we can do just that.
[59:30]: In the very beginning, we're going
to create a variable, int height.
[59:33]: And then later, we're going
to use our do while loop.
[59:37]: And the way that it is set up is a
little quirky, a little different
[59:43]: from what we saw.
[59:44]: So first, you're going to define the do
action, something that has to happen.
[59:49]: And then the second part is the
condition, your Boolean expression.
[59:52]: While something is true, do x.
[59:55]: So we're going to say
height is less than one.
[01:00:00]: So this is sort of the contrary of
what we're trying to accomplish.
[01:00:04]: So as long as height is less than
one, 0, negative 2, negative 5,
[01:00:08]: keep prompting the user
for the right input.
[01:00:11]: Do not stop until they
give you the right input.
[01:00:14]: So I'm going to move this guy
inside of the do while loop.
[01:00:24]: I'm going to delete
int in front of height
[01:00:26]: because we've already
done that on line 9.
[01:00:29]: And I'm going to make it.
[01:00:32]: So make Mario.
[01:00:35]: Oops, I forgot a semicolon.
[01:00:40]: I'm going to run Mario.
[01:00:42]: I'm going to say negative
2, negative 5, 0, 4.
[01:00:47]: OK, so this is now working as intended.
[01:00:50]: You see that it did not stop
until I gave it the right input.
[01:00:55]: It kept reprompting me until
I gave it the right thing.
[01:01:04]: Any questions about
Mario, the do while loops,
[01:01:08]: for loops, the logic of how
we arrived to this solution?
[01:01:20]: Any questions about this part?
[01:01:33]: Yes, I can go to the bottom.
[01:01:40]: Any questions at all?
[01:01:48]: All right, seeing none.
[01:01:49]: So let's go back and actually do a
quick recap of the print_row function.
[01:01:55]: So we just wrote it, and
we already talked about it.
[01:01:59]: Let's briefly consider, again, what
are the components that go into it.
[01:02:04]: So we had our setup with
print_row int bricks.
[01:02:07]: We're printing out one
row of bricks at a time.
[01:02:10]: And again, just a good habit of
breaking down things as you go.
[01:02:13]: So the first part is return type.
[01:02:16]: As we said, print_row
does not return anything.
[01:02:19]: It just returns-- it just has a
side effect of printing something.
[01:02:22]: Then the function name,
print_row, which, again,
[01:02:24]: can be whatever you choose it to be.
[01:02:26]: Then, the next is input.
[01:02:28]: Int bricks is what we pass
into our print_row function.
[01:02:32]: And then, lastly is some kind of
action that we're going to take,
[01:02:36]: which is print row of bricks.
[01:02:37]: And again, coming back to
our visualization, our boxes,
[01:02:41]: so if print_row is a box, and
something is happening here,
[01:02:46]: so what we pass into this box is bricks.
[01:02:50]: So it is the variable that
we grabbed from the user
[01:02:54]: or that we defined earlier in main.
[01:02:57]: We pass it into print_row.
[01:02:58]: And when we get out of it is a row of
bricks, depending on the size of it.
[01:03:04]: So more specifically,
if we had numbers--
[01:03:08]: if bricks equaled three,
three bricks, we get out
[01:03:12]: is these three hashes in the
end of using print_row function.
[01:03:19]: All right, this was a lot.
[01:03:21]: So I'm going to pause
again and ask if folks
[01:03:23]: have any questions about return types,
printing, placeholders, for loops,
[01:03:29]: conditionals, Mario.
[01:03:30]: We've done so many things today.
[01:03:31]: So if you have any questions at
all, please feel free to type
[01:03:34]: them in the chat in
the next minute or so?
[01:03:51]: All right, not seeing any questions.
[01:03:53]: So this is where we're going to wrap up.
[01:03:56]: So this was CS50 Week 1 section.
[01:04:00]: Thank you so much for tuning in.
[01:04:02]: We're going to be here
every week at the same time.
[01:04:05]: Feel free to log into Zoom
again in the coming weeks.
[01:04:09]: It was so nice to see all of you.
[01:04:11]: My name is Yuliia.
[01:04:11]: And hopefully, I'll see you next week.
[01:04:13]: Bye, everyone.