CS50-Intro To Programming
CS50-Intro To Programming
This is CS50.
to the final days and gain that practical skill set to take with you to
engineering sciences, applied math, natural sciences, or really any
discipline.
As you make your way through the semester, try to keep the following
snippet in mind:
what ultimately matters in this course is not so much where you end up
relative to your classmates but where you, in Week 11, end up relative to
yourself in Week 0
Know that you are not at a disadvantage at all for being one of those
Putting on Socks
1.
bend down
2.
Here we have a bit of ambiguity. Which sock? Before long, well need
to express ourselves more precisely so the computer knows exactly what
were trying to accomplish.
1.
bend down
2.
3.
pick it up...
6.
7.
take it
8.
convention: indentation. When you construct a loop, the chunk of code that
gets executed with each iteration of the loop is usually indented. Likewise
with the code that meets a certain condition (e.g.
take it
in the example
above).
1.
bend down
2.
3.
pick it up...
6.
7.
take it
8.
9.
10.
11.
touch toes
12.
put on sock
Now that weve taken care of one foot, we need to make a design
decision to take care of the second foot. Should we write another loop?
Perhaps. Since we only have two cases to handle, we could simply copy
and paste the steps above. However, anytime you find yourself copying and
pasting code, you should probably consider using a loop instead.
Our program is buggy! What happens if theres no matching sock?
What if there is only one sock? What if David already has socks on? We
havent handled these so-called corner cases, which could cause problems
going forward. Generally, when you make assumptions while writing
programs (e.g. of course there will be enough disk space!), you risk
introducing bugs.
#include <stdio.h>
int main(void)
printf("hello, world!");
The syntax is fairly cryptic, but you can probably guess what this
program does: it writes hello, world! to the screen.
Whether you have a Mac or a PC, you can use software applications
to write programs like this. On a Mac, you can use a program called
Terminal to execute programs like this. The Terminal interface is
reminiscent of computers from the past which didnt have graphical user
interfaces (GUIs).
After we open up a text editor, we insert the lines of code above and
save the file as hello.c. This file now represents our source code. As weve
already learned, computers cant understand anything but binary, so we
need to translate this source code into binary. This same code in binary is
called object code. To do this translation, we run the following from the
command line:
clang hello.c
world!
jharvard.
world!.
We can do this by
#include <stdio.h>
int main(void)
printf("hello, world!\n");
a.out
xxd -b a.out
Back in the day (when David was like 34 or so), a programmer would
have had to physically punch holes in cards to represent his or her
program. Luckily, we now have CPUs that can interpret the 0s and 1s
directly.
Scratch
Once you install Scratch and open it, take note of the following layout:
At bottom right are sprites, or characters that will carry out your
instructions.
At top right is the stage, where the program will be carried out.
To the left of the stage is the scripts area, where puzzle pieces
must be dragged and strung together.
If we open up Dancing
cookies.sb,
and in the scripts area, there are two long chains of puzzle pieces joined
together, each with a when green flag clicked piece at the top. Recall that
to start the animation, we clicked a green flag, which suggests that both of
these scripts start running as soon as we do so.
We can recreate our very simple C program in Scratch using the say
block. Hello1.sb is equivalent to hello.c, only a little more colorful.
Hello2.sb
for 1 second, wait 1 second, say it again for 1 second, wait 1 second, and
say it again for 1 second. This seems a little wasteful and, in fact, we could
implement this more cleanly using the repeat block.
Hello4.sb
expressions. In the first, the condition 1 < 2 always evaluates to true, so the
cat meows every time we click the green flag. In the second, however, the
condition says, pick a random number between 1 and 10 and if that
number is less than 6, have the cat meow. Thus, the cat will meow
approximately half the time we click the green flag.
keyword)
else
so that the cat will meow indefinitely, but will roar if we touch it with the
mouse pointer.
Hello9.sb
cookies.sb
did). In
muted
is 0,
muted:
to 1 if
Scratch Revolution.sb.sb)
examples of what you can do with Scratch! Realize that they didnt
implement these complex games all in one sitting. Rather, they broke them
down into bitesize pieces. In the case of Scratch Scratch Revolution,
maybe the student first implemented support for the left arrow key only.
Then maybe the student implemented the green arrow that flies from the
The objective of Problem Set 0 is for you to have fun playing around
with Scratch (or BYOB in the case of the Hacker Edition). Make something
interesting, interactive, artistic, fun. Dont feel like you need to implement
Scratch Scratch Revolution, but hopefully youll be proud of what you make,
enough to show it off to your friends and family once youve uploaded it to
MITs website.
Men.sb.