CS50 2020 - Lecture 0 - Scratch
CS50
This is [UNK] Harvard University ’s introduction to the intellectual enterprises of computer science and the
art of programming. David J [UNK] We wanted to evoke memories or some imagery of the campus itself..
We took what is a relatively small watercolor that this graduate student painted some 200 years ago In th
e Harvard Archives. Nearly. Two thirds of students who take [UNK] have never taken a computer science
course before. [UNK] is meant to be the measure of your own success.. The course’s capstone experienc
e, which will be your very own project., is the capstone project. The class will explore what it means to sol
ve problems in computer science.. How we might represent information boils down to simple digits on you
r hand.. We all need to agree on how we ’re going to represent these inputs and outputs.. But computers
do n’t really speak the same language as us.. They ’re in some sense, much simpler than we humans,, ev
en though they seem so complicated or so sophisticated..
What language do computers speak, if not the system that you and I use of zeros through nines or decim
al? What system do computers use, so far as you ’ve heard, whether or not you’ve taken a [UNK] class b
efore? Brian, Keith, Keith and Brian can be asked to answer this question.. The computers use the binary
system, bi implying two. We can harness electricity to harness and keep track of information with electricit
y. For instance, here is a light bulb that’s currently off, but has the ability to turn on. WE just need to plug it
in or throw on a switch to represent information.. THis really is the germ of an idea that gave Us compute
rs and with it, their use of the binary system.. How high can you count with three light bulbs? How high co
uld we count? We took into account not just how. Just how many bulbs are on or how many fingers are in
the air, but rather the pattern of on and off light bulbs that we ’ve created. We have a lot of folks thinking it’
s eight is the highest you can count. A lot of you think it’s seven, and some of you also think it might be thr
ee or two..
Computer scientists and computer programs typically start counting from zero. SO if we start counting at z
ero, that would allow us to count as high as seven. SO from zero to seven, so seven is the highest. We ca
n count with three light bulbs. SO. How do we actually now get to the zeros and ones that a computer is a
ctually using? IN the world of computers, the system they use is fundamentally the same. The only differe
nce is that computers only have access to zeros and ones, not zeros through nines. SO. If we consider no
w in the abstract, just three possible digits represented here, let ’s consider for a moment why those colu
mns are places where one, 10, and 100. There ’s 10 digits, zero through nine,, so the columns are using t
his base of 10. [UNK] If. All of those light bulbs are. Those light bulbs are off, we ’re just going to start thin
king of those things as zeros. But in our human world, the mental math you would probably do now instan
taneously after today would be 4 times 0 plus 2 times 0, plus 1 times 0.. How does a computer represent t
he number one, for instance,? Well, it ’s just. changing that rightmost bit from a zero to a one. How do I re
present two? IT ’s going to be 010 in binary.
WE started with the story with electricity. WE then moved on to numbers and representing things, either in
decimal or in binary. But we ’ve kind of painted ourselves into a corner, because if we only have at our di
sposal switches or the metaphorical light bulbs, it would seem that the only things computers can do is co
mpute. SO let ’s consider how could a computer represent not just numbers,, but letters of the alphabet. A
capital letter A is actually going to be represented by the decimal number you and I know as 65. The syst
em that humans came up with years ago that maps 65 to A, 66 to B, 67 to C is calledNK]. The American
Standard Code for Information Interchange. ANd. There is a whole mapping, too for punctuation, for lower
case letters and the like. A text message containing these patterns. Text message containing these patter
ns of numbers. 72, 73, 33. What message might you have just received? Let me pull up the abbreviated c
hart here to consider exactly what message you ’ve received. And Sumner, could we go ahead and throw
this same three-letter word on the lights? If you ’d like to see it in bitwise form, so to speak, it will appear h
ere on these light bulbs now as well..
CS50 2020 - Lecture 1 - C
,CS50
cs50 and this is week one our second week and today recall that we ’ll focus on this other programming la
nguage called c. The goal at hand is to ignore things at first glance that we don’t necessarily understand a
nd latch on to those ideas that are familiar from last week. The correctness of your code just speaks to do
es it work as intended. There ’s other aspects to writing good software and writing good programs and tha
t has to do with design. Design is more of a qualitative a more subjective measure just how well-written yo
ur code is. We ’re going to use our keyboard much more than our mouse to actually program but to do so
we’re going ahead to introduce the first of several tools this semester. cs50 is meant to be representative
of a very common popular programming environment where you have a so-called text editor or tabbed wi
ndows where you can write code and a terminal window. This is where i ’m going to do my programming
along the top of the screen and along the bottom is what we ’re in a moment going to start calling our term
inal window it ’s in this terminal window that i can actually run commands and ultimately run my actual cod
e.
We ’re in more of a traditional programming environment the environment that we ’re now in requires that
i use my keyboard a little bit more or what ’s known as a command line interface or cli. This is in contrast
with a graphical user interface or gui or gui which is what describes mac os windows ios and android. i thi
nk there ’s a disconnect between where we left off last week and where we’re currently at if i’m writing cod
e that now looks like this cindy what language do i do well? computers speak would you say binary binary
and just elaborate a little bit what do you mean by binary to recap yeah so they use ones and zeros to re
present everything. The computer ironically does not know what it is i have just typed in order for it to und
erstand what i ’ve just typed i need to actually convert it to zeros and ones. This is a tool you could downl
oad on your own mac or pc or the like and for now we ’re going to describe that tool as being quite simply
called make literally if i want to make my program i ’m going ahead and type make hello. The tool is called
make and the compiler is going to infer from this command that i actually intend to compile a file called h
ello. c. Now if i. want to actually run this program i. need to type a different command that ’s the analog of
double clicking an icon on your mac or. pc or phone’
CS50 2020 - Lecture 2 - Arrays
CS50
This week we dive into a new programming language and also revisit some concepts from last week. Last
week, we began with perhaps the most canonical of programs in c: that which says "hello world." Recall t
hat any time you run make hello, make mario, or make cash, Clang is a popular compiler that you can do
wnload onto your own Macs and PCs. However, to run it is a little different: I’m going ahead and say Clan
g and the name of the file I want to compile, hello. C is the only file that remains well temporarily. Let me t
ake away the ability to use make and let’s now use Clang directly. Last week, the way we compiled this pr
ogram was just make hello. However, this week suppose I were to instead get rid of make only because it’
s sort of automating steps for me that I now want to understand in more detail: I could compile this progra
m again with Clang dash o hello hello hello. C.
When using a library like cs50s library or others, it is sometimes not sufficient to only include the header fil
e at the top of your own code. Sometimes you additionally have to tell the computer where to find the zero
s and ones that someone has written to implement a function like getstring. So with that said, I’m going to
additionally and admittedly cryptically do dash l cs50 at the end of this command which quite simply refers
to linking in the cs50 library. When you compile your code from source code to machine code, there are a
few more steps that are ultimately involved, and when we say compiling, we actually mean these four ste
ps. It’s perhaps enlightening just to see a brief tour of what’s going on when you start with your source co
de and end up trying to produce machine code. Header files that we’ve been including for the past week e
ssentially contain all of the prototypes for all the functions that exist in the library, so that your code when
compiled knows from the top down that those functions will indeed exist. The preprocessor just saves us t
he trouble of having to copy and paste all of these prototypes if you will.
When working with cs50, an IDE, or a compiler, you will generally find a number of files. The first file is th
e "project." This project contains all of your source code and any dependencies. Once the project is creat
ed, the IDE or compiler will automatically precompile any library files needed, which will save time and en
, ergy on compilation. Additionally, the project contains a "main" file that is run when the project is compiled
.
Debugging is an important part of programming. This week, we’re going to add a tool to the list of resourc
es that help you debug your code. Debug 50 is a tool that can help you see what’s going on inside of your
code. This tool can save you hours over the course of the next many weeks.
A debugger in any language is a tool that allows you to run your code step by step and look inside of varia
bles and other pieces of memory inside of the computer while your program is running. I’m going to go ah
ead and knit up now and I don’t have to bother typing this whole command again it’s a helpful way to just
save time. I’m going through my history in CS50 using your arrow keys up and down. Debug 50 needs me
to tell the computer in advance at what line I want to break into and step through step by step. Set at leas
t one breakpoint by clicking to the left of a line number and then rerun debug 50. Notice that what debug 5
0 is doing is it’s running my program but it has paused execution on line six. Notice the yellow highlighting
doubles back that makes sense because I’m in a loop so it should be going back and forth. We’ll do one
other example where step into and step out actually are germaine but before we do that any other questio
ns about debug 50 do for us as well. Ctrl c is your new friend today too pretty much any time you lose con
trol of a program because the debugger’s running and you’ve lost interest in it.
In this paragraph, the author is discussing how they are going to implement a function called "get negative
int" and explains how it works. The author also mentions that this function is invalid in C99, which is inter
esting because everything else in the code is basically boilerplate at this point. The author also mentions t
hat they have set a break point at line 10, which is inside of main but on line 19, the debugger has jumped
to that line. The author then goes on to say that n equals get int feels pretty correct and that it is hard to
misuse get int. However, the author still sees their local variable n because they are into get negative in fu
nction.
One tool in your toolkit is printf, but debug 50 is hands down the more powerful of the two. There’s one fin
al debugging technique that we’ll introduce you today too, known as rubber duck debugging. In an ideal w
orld, we would just talk to our colleague or our partner on some project and just in hearing ourselves vocal
ize what it is our code is supposed to, often that proverbial light bulb goes off and we’re like "oh wait a min
ute, never mind I got it just because you heard yourself speaking illogically." Now we do n’t often all have
colleagues or partners or friends who want to hear about our code. Each of these data types is defined on
a typical computer system, and each type of data takes up a fixed amount of space. It depends on the co
mputer whether it’s a Mac or PC or old or new, but on CS50, the sizes of all of these types are as follows:
a bool uses just one bite, a string of text is a variable number of bytes, and a long is twice as big and that
allows you to represent an even bigger number.
CS50 2020 - Lecture 3 - Algorithms
CS50
This week, we equipped you with all the more tools by which to solve problems not only problems that we
had proposed, but problems in your own code that is to say bugs and recall that those tools involve comm
and line tools like help 50 for help with cryptic error messages that the compiler might spit out. style 50 giv
es you a bit of feedback on the stylization of your code. The aesthetics thereof. debug 50 is a little specific
to CS50 what it triggers to happen that little side window where you can see stack of functions that you m
ight have called Uh during some break point.. An array is a contiguous block of memory otherwise known
as an array.. This is a feature of many programming languages being able to store things contiguously in
a computer’s memory. this simple layout. This very simple feature of the language is. going to open up all
sorts of powerful features and in fact we can even revisit some of the problems We tried to solve.. The se
arch algorithm is a compelling feature of so many of today ’s tools that you and I use. we won’t really intro
duce that many more features of c we wo n’t introduce that much more code. We ’ll focus again on ideas j
ust taking for granted now that you have some more tools in your toolkit beyond loops and conditions..
The running time of an algorithm is roughly how fast or how slow it is, but still using some symbology like
N As a placeholder. a computer scientist would actually wave their hands at some of these mathematical
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller shaikmunavvarhameed. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $8.19. You're not tied to anything after your purchase.