[ Silence ]
>> Alright, welcome back.
This is CS50 and this is the start of week 1.
So just a couple of announcements, one,
so Problem Set Zero is due tomorrow by noon,
realize that there are two aspects to submitting,
one is to actually upload to the Scratch website on MIT's domain.
But also to submit the form and follow the instructions
of that URL, that URL is in the PDF itself,
just don't overlook it.
It's at the very end of the P-set that says how to submit.
If you've not yet taken advantage of such
or did this week and want to in the future as well.
Do realize that for every one
of this problem sets there will code walkthroughs led by one
of our teaching fellows Tommy MacWilliam and these are meant
to be opportunities again to get you started
on the P-sets specifically the standard edition thereof,
though if you have questions on the hacker edition,
Tommy is certainly happy to fill those.
These walkthroughs take place in Maxwell Dworkin which is
up the street at 33 Oxford Street.
It will-- it always takes time
to get everyone's ID card activated.
So if you ever find yourself locked out of the building,
just call 617-bug-CS50.
Hit the secret option 0 and that will ring multiple folks
on the teaching staff to help you solve that problem.
Help.cs50.net, so do take advantage
of this resource throughout the term.
It's an opportunity to post questions, receive answers,
and also see whether your classmates have asked similar
questions as well.
Office hours too debuted two nights ago, and tonight
and tomorrow, they'll be taking placer in law house.
Upon arrival simply pull out your laptop and go to q.cs50.net
and if and when you have a question that you'd like to ask
of the TFs and CAs,
simply follow the simple three-step process.
What are you working on, what's your question
and click raise your hand.
The cs50 greeter's iPad will then list your name in the order
in which you've raised your hand and he
or she will then touch your name and that of a staff member
as soon as someone's available
and your browser will start beeping and flashing
at you saying, it's your turn for help.
So that is q.cs50.net.
Realize too that we do hold office hours not only tonight
but again tomorrow night.
So that if you do decide to cash in one
of your four late days per the syllabus,
you still have a support structure in place
to take advantage of tomorrow evening as well,
so refer to the syllabus for the details on how
to cash in such things.
Lastly, resources available in general,
so realize that retroactively after a lecture,
we will post a canonical set of scribe notes wherein by one
of our teaching fellows Andrew Sellergren who has been
with the course for some time and is very good
at documenting what happens in class as well
as making snarky remarks in the footnotes.
So hopefully you will enjoy reading the PDFs
that will always be posted a few days after lecture
on the course's website particularly around quiz time
where they're a useful set of--
a useful guide to what actually happened in class.
Which you'll also see appearing soon alongside the videos
of lectures online is verbatim transcripts
of every word that's uttered on stage here
so that you can search for keywords, topics of interest.
If you didn't quite catch something, you can read as well
as listen to and watch it online.
And lastly, with regard to lectures,
in interest of preserving trees,
we tend not to print many handouts except
for last week during shopping period.
But realize PDFs of the slides, PDFs of source code,
programing code that we'll start writing this week will always be
available online if you wanna print it yourselves,
or bring it on a laptop to tinker with it during class.
Any questions of any sort?
Anything at all?
Alright, so, you've probably if you're an upper class--
oh in sectioning, one last FYI,
sectioning will commence this weekend but more
on that this coming Friday at class.
So indeed we have one final Friday lecture this week.
Thereafter we are back on our Monday, Wednesday schedule.
So [inaudible] if you're an upper class,
I mean you've walked past this thing for like three years
in the science center and never want to really thought--
stop to think about what it actually is.
Maybe you indulged during prefrosh
and actually read the little tourist friendly signs
describing what this thing is.
But this is one-- this is the first electro-mechanic computers
named the Mark 1 from many years ago that was actually invented
by a professor and a graduate student by the name
of Howard Aiken here in physics and it was meant
to be an automatic calculator.
I thought I'd read off a few fun facts here namely one,
it did indeed handle some nontrivial amounts
of math namely up to 23 significant digits.
So that in it of itself is impressive
and that could take a human some amount of time.
But in terms of the weight, this thing was 10,000 pounds
and what you see in the science center is only a portion of it,
most of it is actually been dismantled.
And in terms of its performance,
a multiplication took six seconds,
a division took 15.3 seconds, and a logarithm
or trigonometric function took over one minutes.
So of course contrast this with your Dell laptops,
your MacBook airs and what not which, you know,
maybe are annoying when they're 5 pounds these days to carry
around in your backpack and surely, mathematically,
they can perform these calculations instantly.
But I thought I would give you a sense
of just how exciting technology was some years ago.
I'll replete with not only black and white footage and that voice
that seems to be on the present from '30s and '40s.
This is a little look back it's about a minute long
as to the history and the utility
of that Mark 1 computer you've been ignoring
for some years here.
[ Pause ]
>> User error.
[ Background Music ]
>> A triumph of mathematical
and mechanical skill is this great new automatic calculator
at Harvard University.
Intricate problems in mathematics put
through the machine in coded form
on tape are accurately solved in a minute fraction
of the time required for human calculations.
[ Music ]
[ Background Music ]
>> Designed to expedite all forms of mathematical
and scientific research.
The giant mechanical brain will work
for the United States Navy until war's end.
[ Music ]
>> It was a magical time.
So do if nothing else, thank you.
[ Applause ]
>> Do if nothing else, perhaps take a little more interest
when walking past that machine
and cause there actually is a lot of compelling history there.
And you might have heard terms like punch cards and tape
with regard to how you actually feed input into a computer.
Back in the day you actually toggled switches
and you'd literally fed in cards, pieces of paper with hole
in them that represented zeroes and ones
and other such instructions.
We've moved beyond that certainly now
with our keyboards and such.
But there's a lot of history on this campus in particular.
So perhaps at least pause the next time you walk past.
Last week, we introduced not only the course,
and not only Scratch, but some of these fundamentals
that we're gonna now start to take for granted.
Namely how to represent information and it's not
that we're gonna dwell on things like zeroes and ones and bits
in such in this course.
We're very quickly gonna start taking them for granted
that somehow we have this ability
to store numbers and data.
But just to tie this week to last week, I thought I'd see
if we could get eight brave volunteers to join me up here
to hammer home one of last week's lessons.
So how about you three there in the center four, five, six-oh--
it's over here seven, and Rob, eight, come on up.
So you have to be comfortable as always appearing here on camera.
We have eight pieces of paper, one for each of you here.
And if you could each align yourselves on stage,
consistent with the order here.
Rob you will be let's say the-- Rob will be number 1.
Over there on the right, if you wanna to be two's place,
you'd like to be eight's place.
I'll find the four's place in a moment.
You'd like to be the four's place.
How about the-- let's see the--
come on, 16's place and three more,
32's place 64's place and 128's place.
So if you wanna go ahead in front
of the stage so we all can see.
Indeed hold you number outwardly.
What these guys each have-- and Rob, do you mind shifting
down just a little bit?
So we have now places 1 through 128 and recall
when in the takeaways last week is that this system of binary
of base 2 is not all that dissimilar
to what we've been using for years.
Namely base 10 or decimal where we have the ones place,
the tens place hundreds place and so forth.
Instead in the world of computers,
we tend to use powers of two.
The two really deriving from the binary nature of bits zeroes
and ones and so our column place holders are the ones place,
the two's place, the four's, eight, 16,
in other words instead of multiple
by 10 you multiplied by 2.
But we built up from this numeric capability last week.
To actually express numbers.
And does anyone recall what the code or the system was
that maps numbers to letters in computers typically?
So it's something called ASCII, A-S-C-I-I which is just a code,
a mapping between numbers and letters and recall
that that mapping looked a little something like this
and the chart itself is certainly not worth memorizing.
But a few key values like 65 for capital letter A,
97 for lower case A. A few of those tend
to be useful just a kind of ingrain in your mind
so that they can be handy when we're actually writing code.
Now, these guys have on the backs of each
of their sheets here, a little cheat sheet.
So round 1, round 2, round 3, and in each of these rounds,
their little cheat sheet advises them
to either raise their hand or just stand there.
If they've raised their hand, they're meant
to be representing a one, if they're just standing there,
they're means to be representing a zero.
>> We've got a three-letter word to represent
with an eight-bit bytes an ASCII character if you will.
So let's see what three-letter word we can spell
with these folks here.
So if you all as our volunteers, round 1, could raise
or not raise your hand as instructed by your cheat sheet.
What? Number first is being represented
by these eight volunteers?
[ Inaudible Remarks ]
>> Yeah, 66, right.
'Cause we've got the 64's place plus the two's place,
add them together get 66 and that letter then maps to?
>> Capital B.
>> Capital B, alright.
So we're on our way.
How about round two?
What are we representing now?
[ Inaudible Remarks ]
>> That's definitely not universal agreement.
How about again?
[ Inaudible Remarks ]
>> Okay, I heard more 79's.
So if you just stole the person next you number,
you are correct.
So the number 79 maps to what letter here?
[ Inaudible Remarks ]
>> BO, okay.
So now, the third and final round, round three, go.
What number do we have here?
>> 87.
>> Yeah, so we've 87.
And again you can get this by adding up the numbers in front
of anyone with our hand raised, 87 maps to?
[ Inaudible Remarks ]
>> A W? So W and so we have a nice word here, BOW,
often pronounced by last year's classes BOW
which didn't really make for good ending.
But if you would take your BOW, we've indeed spelled out B-O-W,
so congrats to these guys here.
[ Applause ]
>> If you want to head off in the stage to the left,
we have a little parting gift for you
in the way of a waiver form.
[ Laughter ]
>> So we now have this ability to express numbers,
to express letters and so now let's start taking this
for granted.
And indeed let's transition from last week's exploration
of Scratch which was very user friendly and graphical.
But also very limited, even if you've never programmed before,
you probably found it or are finding it
or will find it difficult to do certain things
because the program isn't necessarily as expressive
as you might like it to be.
Or that or you're just not very ac-- familiar with it yet,
so it just takes some time finding your way around.
And allow me to emphasize at this point
in the course right before study card are due,
there's quite the spectrum of students in this course.
And in the weeks zero through two,
we typically run this risk historically of why they're kind
of boring those more comfortable because they're coming
in with more background.
The goal of lectures in particular is to start filling
in any holes that you might have
from your prior experience whether formal
in classes or just self-taught.
And for those less comfortable, making sure that we equip you
with the same basic mental model and understanding of some basics
so that all of us in just a week's
or two time can start having a conversation
on exactly the same level.
Now if you're finding that even Scratch is kind of stressing you
out is taking a ridiculous amount of time, realize that all
of these things get easier and consistently every year,
even when I took it myself, the best strategy in general,
and maybe it's too late for this first P-set,
is to start these things early.
They'll typically go out the door--
they'll go out the door by Friday's each week,
which means you have the weekend if you'd
like to sort of ease into it.
The walkthroughs are on Sundays.
And really the key to programming is not to start
on say, Wednesday night and say, "I'm gonna finish this
by morning or by midnight."
'Cause it's just not fun and you can't think necessarily clearly
and there's just a huge advantage to being able
to walk away from some problem.
I've lost count over the years
of just how many bugs I've solved just by randomly thinking
about them passively through out the and I'm like, "Oh,
so that was what I was doing wrong."
And to allow yourself
that opportunity honestly makes things easier,
makes things more fun
and certainly eliminates the stress that's inevitably induced
in any course by postponing things to last minute.
So let me get off that soap box there and now start
to transition us to this programming language C.
So today we begin a more traditional
programming language.
It's an older language but it's the one the really sets the
direction of a lot of today's modern languages
which we'll then begin to visit toward the end of this semester
in the form of PHP and JavaScript
and some other things as well.
And what's nice about C as we'll soon see is
that it's relatively simple.
And it's not gonna feel that way particularly
to those less comfortable, most likely the first week or so.
But relatively speaking, you can learn this language
in just a few weeks.
Contrast that with some human language, a spoken language,
you typically don't master a new language
after just a few weeks of practice.
It can take months, it can take semesters, it can take years.
So just realize that the scope of the languages we're
about to learn is actually fairly narrow.
So here's a little program that we saw last week
when green flag clicked, say "Oh hi world" something
to that effect, so these was a script,
the purple piece there was a statement
and statements again just do something, we'll just kind
of tease you with how we're gonna start translating was very
user friendly and very sort of graphical and playful
into something that's a little more easy
for a machine to understand.
Starting TNA and beyond, if you want to write a program
that just says something silly like "oh hi world",
you're actually gonna type in a text editor like Notepad
or TextEdit, whatever or more fancy programs
that we'll introduce something like this, int, main, void,
some curly braces printf.
And we mentioned printf last time in "oh hi world".
So there's some crazy syntax in here, semi colon,
curly braces things that take a little getting used to.
But you start to see these things recurring.
So when you start to see code like this today and beyond,
just realize, ah, it's really just Scratch,
it's really just those two puzzle pieces we saw last week.
But the syntax now is a little more cryptic,
it's nearly as user friendly, but you get good at it,
I dare say relatively quickly.
So statements in general, this was a statement, oh hi world.
This would translate more directly just
to this one line of code.
So when we say things like code and source code,
programming code, any of these sorts of--
any of this sort of jargon we're talking about lines like that.
And in most programming languages
when you want the computer to do something you express it
in the form of a line of code.
And sometimes the line gets a little long, but in general,
one line means do something.
Just like one puzzle piece means, do something.
Well what about loops?
Last week we saw something like this, forever say "oh hi",
it's just gonna say this, and say this, and say this,
and say this, and say this.
The syntax this week and beyond is gonna look a little more
cryptic but we can express the same idea.
We're gonna have keyword called while.
We're gonna have kind of a stupid structure here
where we say while true.
And then in the curly braces, we're just gonna say printf
which as you can kind of infer means print this to the screen.
And what are we gonna print?
Oh hi. Now while true,
why is this equivalent functionally to forever?
Well true is always true so it's just as though you're saying,
do this again, and again,
and again 'cause there's no variable there.
There is just what we'll call a constant, the keywords true.
And it's like saying while one, while one, while one, while one,
one is always not zero which means it's always true.
So again one and true and yes are all synonyms
for our purposes thus far.
Well what about loops like this?
Repeat 10 times, say "oh, hi",
well we need is slightly different construct
because while true kind of implies an infinite loop.
But there's this syntax in C called the for loop.
And again we'll come back to this, this doesn't all have
to sink in now but you say something like for int,
and int's gonna means integer.
Int I, I is gonna be a variable.
So set I equal to zero, then do this loop
so long as I is less than 10.
And every time you do something, go ahead
and increment I with plus, plus.
So there's gonna be shorthand notation like this, plus,
plus which just means increment.
Add one to a variable and we had a puzzle piece quite
like that in Scratch.
Add one to some variable puzzle piece.
Well variables in Scratch looked indeed like this,
set counter to zero forever do this.
Say counter, change counter by one.
So this should just say 1, 2, 3, 4, 5, 6, infinitely long?
How can we implement that in C?
Well, we need a variable.
So it's gonna look like this, int counter gets zero,
or it equals zero, while true and now we kind
of steal some syntax from previous examples.
The printf, the plus plus, but the idea is the same.
So again, even-- had we shown you the text in white
in the first week it's kind of overwhelming,
it's kinda confusing, it's not all that intuitive at all,
but the ideas are exactly the same.
And this is why we start with Scratch because it gets
across the ideas without these stupid distractions
of what's ultimately some arbitrary syntax
that humans decided on that we all just need to acclimate to.
But we will and quite quickly.
So Boolean expressions, Boolean expression were what in--
how do you define them?
[ Inaudible Remarks ]
>> Yeah, so true or false?
Something that's yes or no?
One or zero?
True or false?
So they look like this in Scratch.
Is X less than Y?
Is X less than Y and Y less than Z?
Well we can do the same sort of expression in C,
we use some parenthesis, we use some angle brackets
but it looks like this.
X less than Y or is X less than Y ampersand ampersand,
and ampersand ampersand happens to represent the notion
of Boolean and, like to and in things together.
It's distinct as we'll see from a single ampersand.
So there are some oddities like that
but it's the exact same idea.
So we can do a few others with conditions.
We had something like this.
If X is less than Y, say X is less than Y. Else if,
so remember we could embed one puzzle piece in another
to have multiple forks in the road.
Not just one or two or three, we could keep doing this.
Well, we can do this in C as well.
In this case, I'm essentially saying if X is less than Y,
then say as much, else if X is greater than Y say
as much else say they're equal,
'cause that's the only possible alternative if it's neither less
than nor greater than.
So it's translates to this.
If X less than Y, curly braces, printf, else if X greater
than Y printf, else printf.
And the curly braces kind of-- just like the if conditions
and the else condition in Scratch kind
of have these embracing yellow bars similarly
to the curly braces, conjure up that same idea.
>> It's enclosing one or more statements inside of them.
Well what about arrays?
We only talked briefly about these,
maybe you've deployed them in your program.
But an array is what in English?
[ Inaudible Remarks ]
>> Yeah, it's like a list, it's some kind of data structure,
data storage, it's a container
in which you can put zero or more things.
This is in contrast with the simple variable,
'cause with a variable, you can just put one thing.
So if you wanted to store like in the game of Fruit Craft RPG
which we played briefly last week.
If you wanted to the collect the apple and the orange
and the pear and what not, you could have an apple variable,
an orange variable, a pear variable,
but your program very quickly gets bloated with all
of these darn variables, all of which are related and
yet you need to sort of independently define them.
An array, let's you say, I need multiple such fruits and I want
to keep them somehow together in sort of 1 bigger variable
but still get at individual components as we did
with the Fruit Craft RPG.
So how do we translate this to C?
It takes a couple of lines this time.
But add orange to inventory might map
to something like this.
A string inventory bracket 1, and we'll come
to this next week, but string is synonymous with word
or phrase or sentence.
It's some number of characters that a human might type.
Inventory is just the name of this variable
and the bracket 1 probably denotes what?
[ Inaudible Remarks ]
>> Yeah, the size in this case.
And we'll see this is kind
of a stupid array 'cause I'm only storing one thing
but it's meant to be a simple example.
This just means give me an array of strings of words of size one.
So again it's stupid in that,
yeah I could just use a single variable for this,
but we could just change that one to a two or 10 or a 100
and that little character on the screen could then collect two
or 10, 100 pieces of fruit.
It wouldn't necessarily be bounded by one.
And then inventory, open bracket zero equals "orange",
we'll see next week, this is just the way
of saying put the string, the word orange inside
of location zero in the inventory.
Now here we see one of the first curiosities
of computer scientist turns out computer scientists,
programmers more generally typically count starting
at zero.
Why? Well, if you think about our discussion last week a bit
and even just now, what's the smallest number you can
represent with the bit?
Zero, what the smallest number you can represent with 8 bits?
Zero, just zero, zero, zero, zero, zero,
zero so why not just start counting at zero.
Why waste this bit arbitrarily like humans do
in speech counting at one.
So in general, it's just easier and it's definitely conventional
to start counting at zero.
Thus was born problems at zero.
Thus was born Week Zero last week and so forth.
We've just preserved the same tradition.
And so, Scratch however does not do this,
realize they made pedagogical decision particularly
for the target audience
to actually start counting most things at one.
So realize this is not universal,
but it is quite common.
So here's the complete program.
So this is again, a language called C, this is a program
that I might write using a simple text editor,
how do I actually get to the point of writing this program?
How do I get to the point of translating this program
into the zeroes and ones
that the computer actually understands?
Well, first let's tease this apart.
Just to get some jargon out of the way.
Even though some of it we'll just keep taking for granted
until it more naturally fits into the conversation.
So this is a simple program, and in C, just like in Scratch,
there is this when green flag clicked block,
that kinda kick starts the whole program in C
as in a lot of languages.
Java, C plus plus and others main is sort of the equivalent
of the when green flag is clicked.
If you have a main function we'll call it,
Scratch call them scripts we'll call them functions.
But they're essentially the same for today's purposes.
So long as you're programmed, the text file you're typing
in on your computer, Mac, PC or whatever,
contains the keyword main it's probably an executable program.
Something you can run, something you can double click on.
Now you can't just type main, you have to adhere
to some annoying conventions
and among those conventions C are first, you have to say
that main return something.
And we'll com back to this.
But for now, just take for granted
that main must be prefixed with the keyword int, I-N-T.
Now as in aside, what is this all about?
Well if you'd ever used your own Mac or PC or Linux Fox
and something weird or bad has happened,
you get some error message, it freezes.
Odds are you've seen this little text box on the screen
that says something like an error has occurred,
you must reboot, or your changes were lost.
And then it will often say something like error,
negative one or error 253 or some random number that's
of no value to any of us, it's probably of some value
to the programmer who wrote that program.
Because programs or specifically functions can return numeric
values which are not often useful to people like us,
but they're useful to the programmers or other parts
of the program and generally,
zero means everything went perfectly.
Zero is-- it's a little counter intuitive
in that zero we typically say is false or no but zero is perfect
and why is it perfect?
Well so long as you choose zero to mean success,
this program works great, it quit without any problems,
you have a seemingly endless supply of error codes
that your program can return if something's indeed broken
so when you see these error messages on the screen
and you see some numeric value there are almost never the
number zero, it's the number one, or two or negative one
or negative three and that's so that Microsoft and Apple
and others have again so many possibilities
for error codes to return.
Now it's not always error codes, we'll see other uses
but for now the reason that main returns a number 'cause it's
some way for an individual program
to tell the OS it worked great or there is a problem.
So what else is there void, I'm gonna wave my hand out for now
but know that program can take input when you run them
by typing a command or by double clicking them void just means
um-um, this program takes no input.
It's so simple, it just does something,
doesn't take any input from human,
the currently braces are what keep everything together just
like the yellow structure of most Scratch blocks,
printf is what we're gonna call function, so main is a function,
printf is a function and what's nice here is
that printf a function can call another function.
Now what's a function?
And you can kind of infer it from the name,
it's like a miniature program that just does something,
it has some functionality so printf's purpose
in life is turns out to print something to the screen,
main's function in life is
to kick start a program the moment it starts running,
main is what it's suppose to do first, now functions here,
printf has this parenthesis and then another parenthesis inside
of which is a quote, unquote and just to see
if we can tie the conversations together,
what would you call perhaps the thing inside quotes here?
>> A string.
>> Yes so it's a string,
so we've used the word string before
to just generally say a word, a phrase so indeed,
printf can take as input, a string
and that string is what's printed on the screen
and that's it, all this conversations just
to say here is how you implement hello--
Oh hi world on a computer but it gets us to the point now
of being able to write and run this.
So we need the ability now to write
and run a program, so how we do this?
Well those of you who are coming at this
with prior program experience, you probably use something
like Eclipse or NetBeans, or Emacs
or there is seemingly infinite number of programs
with which you can write software, write programs.
But again I can do this on my Mac, I could open
up a stupid little program called TextEdit, it's analogous
on a PC it's a notepad.exe
and I could just start typing includes standard IO int, main,
void, printf, I'm literally just transcribing what we saw before
Oh hi, I think I did that yesterday too, "Oh hi world".
And again there's some syntax in here, I'm waving my hand
at the back slash N, the semi colon but we'll come back
to that, all I need to write software is a text editor
like this.
You can even use Microsoft Word so long as you don't save it
as a dot doc but you save it like a dot txt file or something
like that but that's not the right program to use,
really you want a program dedicated to programming
as we'll soon introduce.
So unfortunately, this is not quite zeroes and ones, right,
I could if I really wanted to create a nightmare
for myself write "Oh hi world" by expressing myself with zeroes
and ones saving that file with a special program
and then double clicking its icon, or running some command,
but this is obviously not human friendly nor is it human
friendly to feed punch cards or tape
into a machine instead we write programs
in higher level languages like C that look like this,
but what do you need especially
if you have some prior background what do you need
to do with this source code in order to convert it
into those zeroes and ones?
[ Inaudible Remark ]
>> Yeah, so there is this other utility that's out there
and it's called the compiler and it's not a text editor,
it doesn't like you write programs,
but it lets you prepare programs for running so step one
in writing a program is just write the program in your choice
of language, for now we use C,
later on the semester we use PHP, and JavaScript,
we just need to type it at our keyboard in some simple program
but step two usually at least
with the language called C we have to convert this into zeroes
and ones by passing this file as input to another program
that will essentially convert this to zeroes and ones
and then once I have those zeroes
and ones then I have a .exe on my desktop to borrow Windows B
or I have a .app on my desk top
to borrow Mac OS' speak whereby I can now run this program
so how do we do this?
Well, if you have a Mac you can go to the Mac store these days,
you can download the program called Xcode for free,
or if you have Lion, you can pay some money
if you an older version and you can then install a compiler
and again a program called Xcode and you can do all of this.
Windows, there is no Xcode, that's an Apple product
so instead on Windows, you go and buy Visual Studio
from Microsoft or NetBeans or Eclipse or some other tool
that there are freely available alternatives
but that too is kind of a pain and that it doesn't come
with the operating system.
And in a class of this size, we have all sorts
of people using all sorts of versions of Mac OS and Windows
and even Linux and other OSes
and my God it would be a nightmare all using different
computers trying to get all of our codes to work
on completely different hardware
and so thus was born the CS50 appliance.
>> And we made a mention of this last week
but the CS50 appliance is our way of saying ah, use your Mac,
use your PC, use your Fedora machine, your bunting machine
and instead install inside of a window
on your computer our operating system,
now the CS50 appliance is going to be an operating system
that runs inside of a window on your own computer
that operating system happens to be called Linux
with which you're probably familiar with by name,
it's freely available, very popular, super popular
in the world of websites and running servers primarily
because it's free and high performing,
we're using a specific flavor of Linux called Fedora,
there's also a competing alternatives Fedora in Ubuntu
and all these big names, we chose Fedora
but it doesn't really matter which one you use
but once you install this things,
we will all have no matter what OS you brought to campus
or what computer you brought to campus a similar environment
that looks a little something like this and it actually looks
like Mac OS or Windows, all of these manufacturer kind
of put the menus in different places
and the icons might be called something trash or recycle bin
but at the end of the day, they're all stealing ideas
from each other and at the end it all kind of looks the same.
So if you look at top left here, you have a home directory,
this is gonna be like My Computer or MacIntosh HD,
you have file system which is everything,
you have the trash can and the bottom left corner you'll see
that we don't have a Start menu, we have a CS50 menu and inside
of that is gonna be links to programs
and so long story short, even if you're using Mac OS or Windows,
starting this week and beyond,
we're all gonna be running the same OS inside of a window
on our own computer so how do you do this?
Well, we'll provide you with instructions
with the first problem set but no for now,
but you'll download a free program happens
to be called Virtual Box, this is a program that exists
for Windows, for Mac OS, for Linux, for Solaris,
are you unfamiliar with some of those?
They're just other operating systems and when you run
in on your computer, it looks a little something like this
on the left hand side you see a list of all the virtual machines
that you have installed on your computer and you see
on the right the virtual specifications
of those computers, of those virtual machines,
so what is a virtual machine?
It is just a computer running inside of a window being tricked
into thinking that it's actually running
on a physical hardware CPU with physical RAM
and a physical hard drive but no we've gone so sophisticated
as a society that we can actually trick Windows
and trick Linux and even with some effort trick Mac OS
to thinking that those operating systems are not running on--
that those operating systems are running on a piece of hardware,
a computer that you bought at the store or online
but in reality you're actually running it inside of a window
on a computer that you bought online or in some store
and you can really mess with your mind if you think
about running a virtual machine in a virtual machine
in a virtual machine but that tends not to work so well,
so you kind of do it once, you leave it at that,
but what's the advantage here?
Well, just as an aside and this is really just a teaser
for things to come, these days most of your laptops
if relatively new have probably one CPU but multiple cores,
you might have two cores typically, well these days
for servers you might have 16 cores, 32 cores
and that's essentially equivalent to having two or 32
or more brains inside of your computer all
of which can do something which is literally simultaneously,
now the world is not in very good at taking advantage
of 32 CPUs even two CPUs we humans,
we don't check our e-mail all
that quickly despite what you might think,
you don't need 2 gigahertz of power times 2 to check your mail
so even our computers have a lot of spare resources at least
if they're 3, 4 or years old or newer
and so what virtual machines take advantage of is all
of the spare brain power and memory and disk space
that we just don't typically use,
you don't use all your computer's resources
for Microsoft Word, instant messaging, for Facebook,
and the like so the virtual machine is going to borrow some
of your CPU cycles so to speak, some of you RAM,
some of your disk space, so what you see here
in the CS50 appliance, you will download essentially a zip file
compressed file from the courses' website
for the next P-set, when you follow the instructions,
you'll see that the virtual machine you're installing has
turns out 768 megabytes of RAM, now by modern standards,
that's actually not all that much and we could crank it
down even further cause most of your laptops have at least a gig
or two or four or eight,
these days so realize these are all configurable
but once you then double click CS50 appliance at top left,
voila, your Mac becomes Linux, or your PC becomes Linux
and if you maximize the screen,
you don't even realize what operating system you're actually
running and so now we're all on the same page, now as an aside,
I've also had this icon here, I just for fun installed Windows
in advance on my Mac inside of a window so that also
in class moving forward if we ever want to have a conversation
about what something looks like on a Mac or PC, I don't need
to have a two separate computers,
I can use just the Mac by default
but then open a Windows window
and we can see what it would look like for the half of you
or so that run Windows 7 or XP or Vista
so realize that's another advantage as well
to run multiple operating systems at once.
So, enough about what the technology is,
let's actually use it and do something interesting.
So, once I double click this icon a few seconds
or minutes later the operating system would boot
up in a window, if I maximize it here,
I now have my home directory like, My Computer
and I have the trash, I have this menu at bottom left
and I have all sorts of programs which we'll explore over time
but the one that I care about first happens to be
under programming and you'll see there's a few things
under programming, one of-- is called GEdit,
there's a few other things.
We actually installed Scratch
in the appliance even though you didn't need the appliance
for the first P-set.
We just put it there so that
in theory you can take the whole course inside of this appliance.
We'll see a program called Eclipse later in the semester
which is an even fancier tool with which to write programs.
And we'll see Terminal as well but for now I just want GEdit.
GEdit is going to be a slightly better version of TextEdit
on a Mac and Notepad on a PC and it's still pretty simple,
I mean it's mostly a white screen with very few menus
but it's in this area here in the middle
that I'm actually gonna start writing my programs and it's
at the bottom of my screen here where it says,
J. Harvard at appliance and there's this blinking prompt,
that's where I'm gonna run my programs.
So, typically in a Linux environment you don't have
to double click icons, you can actually type the name
of the name of the program at a prompt, hit Enter
and that program will run and that's where we'll start
to keep things simple.
So, let's give this a try.
I'm gonna go ahead and first increase the font size here just
so we can zoom in, but this is just
for the sake of lecture here.
So now we've got a bigger font and I'm just go ahead
and save this file like I would on any operating system,
Save As, I'm gonna go to my desktop just for kicks
and again all the this looks a little different from Mac OS
or Windows but it's the same thing once you get used to it,
hello.c, so the convention when writing programs in C is
to call them something dot C. So,
now I'm gonna save this on my desktop.
The file itself contains nothing,
let's just do a sanity check, if I minimize this window
for a moment, there it is, it's just a file, just like Mac OS
or Windows, there's nothing
in it 'cause it's just this big blank text file.
So, let's start coding, let's start programming,
include standard Io.h. We'll come back to that in a moment,
int, main, void, I remember type-- seeing that before.
Here's some curly braces and then the command was, printf,
ohhiworld!N; now I've done this sometime
so it kinda gets easier overtime but that is a transcription
of what we had before.
So, it's very underwhelming,
it even though it might be syntactically be a bit ugly
but let's actually run this thing.
So, first I'm gonna hit Save either with Control S
or I can do it sort of old school file Save
and now I've saved the file on my desktop.
But now I need to run it but not yet,
what needs to happen before you can run it?
>> Compile.
>> We need to compile it.
So, it turns out that in a Linux environment and also
in a Mac environment and also in a Windows environment
if you download the appropriate software but it's,
the instructions vary by operating system so again,
we're standardizing here.
Notice that at the bottom of this screen, inside of GEdit,
I have this again blinking prompt.
So, what the appliance does when you boot it
up is even though we could give you all unique user names,
it's just kind of a pain to create a user name and password
for a program that's running on your own computer
so instead we decided that everyone will take
on the identity by default of John Harvard
and John Harvard's user name is jharvard and so
when John Harvard is automatically logged in,
you have his blinking prompt and his home directory,
his folder of storage but when you submit,
well that's at the point we'll ask you your actual name
and so forth.
But for now only you can access this account and notice
at this prompt, I can do certain things.
In Linux, and this is what we're seeing now,
the operating system Linux is very often navigated by way
of textual commands not by clicking and pointing
and so the textual command we'll say first is LS,
LS is short hand notation for "List" and when you hit LS
and hit Enter, what you see is a list of all of the files
and folders in John Harvard's default directory or folder.
John Harvard's default directory
or folder is conventionally called his home folder.
So, in Windows this would be My Documents,
in Mac OS this would be Documents,
in Linux it's Home Directory.
So, we're in my Home Directory and you can see
that you have a desktop folder
and also 'cause I was doing a bit of prep work,
I also created a lecture folder and they're in bold just
to kinda convey to me, the human,
that these are directories.
So, I wanna actually go where to compile this program?
>> So, it's on my desktop indeed,
it's not in this directory so instead of double clicking
which we could do, again we're starting
at the simplest form here, I can do CD desktop and then hit Enter
and notice what happens, my prompt just changed.
So, in the interest of user friendliness,
we've pre-configured the appliance to always remind you
where you are by just telling you parenthetically what
directory you're in otherwise it's very quickly
to kinda get lost or forget where you CDed into.
So, now I'm in desktop so if I type LS,
I'll see the file I created.
I don't see trash, I don't see file system,
I don't see those fake icons that are just there
to be user friendly, I only actual files
and folders that I put there.
So, there's hello.c. So, now I need to compile this.
So, how do you go about compiling a program?
Well, there's actually a few ways
but they all produce the same result.
The simplest way to translate a program written in C to zeros
and ones is with a command called GCC,
GNU Compiler Collection, it's very, it's very popular,
very standard, very robust program
that simply makes you type GCC space the name
of the program you wanna compile and that's it,
it does all the conversion for you.
So, if I go back to GEdit here and then let me zoom in just
so it's nice and big and I type GCC hello.c, enter,
the fact that nothing happened is actually wonderful in fact
when you just sit down to write your first program
and something does happen that means you made a mistake
because what you'll see is a whole bunch of error messages,
you left off a semicolon or you did something wrong
where as Scratch isn't quite as pedantic and doesn't yell
at you it just doesn't work, GCC will check your code and say,
"Um-um, I can't-- I'm not converting that to zeros
and ones, there' some mistake or mistakes."
But no error messages is good if I now type LS,
you'll see two things, I have just created a program
that by convention is called, a.out this is something like,
whatever .exc in Mac OS--
in Windows or whatever .app in Mac OS.
So, a.out is just a default name for a program so to run it,
double clicking on this is not gonna have any effect,
this is just text, I can run, .
/a.out. So, a.out and then just as in the side, the dot slash is
for the following reason because this is a stupid little program
I myself wrote and it didn't come with the operating system,
I didn't run an installer because it's
in my current directory, I can't just type its name
like I typically can on a computer and it just knows
where to look, I have to tell it where to look and so in Linux,
in Mac Os and even in Windows if you use the right tools,
dot is short hand notation
for the current directory wherever I'm in,
whatever folder I opened and as aside,
anyone know what the parent directory is
if you want to go backwards?
It's gonna be dot dot, so we're gonna see conventions like this
but for now dot just means look here
and slash just means here slash, file name so enter, oh my God,
I wrote my first program, like a.out contains now zeros
and ones and they're in a nice specific pattern that's
understood not only by the operating system but also
by the CPU, Intel Inside is relevant here because the zeros
and ones that GCC outputted are structured in such a way
that these zeros and ones mean print, these and zeros
and ones mean quit, these zeros and ones mean add, these zeros
and ones mean subtract and so forth.
They're our conventions that Intel
and others have decided on.
So, unfortunately this is kind of a stupid name for a program
so I can actually improve upon it so that
when I write my programs I don't have to brag to my friends,
"I just wrote a.out" which would be nice
to give it a name like, "Hello."
So, to compile a program
with a specific name we just introduce one new idea.
Most programs that you run by typing their names
at a prompt take what are called Commands Line Arguments
or options or switches any of these jargon is relevant
and here we're saying, GCC-O, the O happens
to represent outputs and I know that just
from reading the documentation.
So, GCC-O Hello then hello.c with spaces
in between are saying, compile hello.c and output it
to a file not called a.out, the default,
output it to a file called Hello.
So, now if I go back to my prompt and zoom in,
I can recompile this with GCC-O Hello then the name
of the source code, hello.c, Enter, nothing seems
to have happened, LS though, there's Hello but, um-um,
that doesn't work, why?
>> Dot slash.
>> Right, so dot slash is important only
because again this is not a standard program that I bought
and installed or downloaded rather I wrote it,
it's in my own little directory which means I need
to be more explicit and .
/hello indeed runs that program there.
So, you know we can actually simplify even further
because you'll soon see that GCC-O Hello, one,
just gets hard to remember.
Two, it just gets tedious, my God,
just to compile your program you have to type all these commands
but as an aside realize that Linux comes
with all these tricks, right now I'm hitting the up and the
down arrow on my keyboard
and it's remembering seemingly an infinite supply
of things I've typed before so there's lot's
of little tricks like this.
If you want to do what you just did, you can typically hit up
and hit Enter and it will just do it again
but I can do better than that.
To compile a program, I can also just make it.
There's another program in Linux called, Make and Make figures
out for you typically what types of GCC commands need
to be executed in order
to compile your programs 'cause we're doing something fairly
simple right now but if I actually do make hello this will
actually make a program called Hello and figure
out what to type for me.
So, let's do this, I'm gonna go ahead and just type make hello,
notice the dichotomy here, I'm not typing make hello.c,
I'm typing just make hello and the program's gonna assume
if I've said hello, it will look for a file called hello.c,
I hit Enter, okay well that's even more friendly, it's saying,
um-um, I don't even need to compile this into zeros
and ones 'cause you just did this.
Well I really wanted to do it
so let me remove those files I created, the command in Linux
to remove files is RM, remove,
so there's a pattern here, right?
Rather than ever type really a whole word,
the folks who invented Linux and Unix
and these other operating systems tried
to make the command as compact as possible so RM means remove.
So, I can remove Hello, I'm gonna also remove a.out
for good measure, so just putting a space
and separating them on the same line will then prompt me,
remove regular file hello, whatever that means.
So, Y for yes, Enter, remove regular a.out, Y for yes, Enter.
Now, if I do LS notice we're just back to where I've started
with just hello.c. So, let me go ahead now and run make hello,
Enter and notice even more stuff appeared on the screen
and this is because we've pre-configured the compliant
to know about all sorts of useful commands that, my God,
it would be a pain in the neck to type that whole line
of code every time you want to compile a program.
So, Make is gonna automate this for you and we'll tease apart
over time with these various flags or switches mean
but for now the compelling feature is
that we have a program called Hello, I can go ahead and run it
as such and now I have Oh high world executed much
more succinctly.
Yeah?
>> Really quickly, so programs
that are actually runable are in bold?
>> Programs that are actually runable or typically
in this program written in bold as our directories
and it's just a convention, it will differ
in different programs.
So, just to tease you with one other thing,
so this is again the CS50 appliance,
if I minimize we're back at our desktop
but realize this is a full fledged modern computer,
I can click the Firefox icon and I can go to google.com inside
of the appliance and actually now do everything I could
on the internet inside of this machine.
Moreover, what's nice about the appliance, irrespective
of the internet is that especially in the dorms
and houses if you have kind of flakey wireless, you don't need
to be consonantly connected to the internet,
the appliance boots independent of the internet
so you can now work anywhere, anytime including over breaks
if that's at all appealing, without actually needing
to be perpetually on line and it's all irrespective
of what your actual operating system is.
Any question?
All right so that's a lot to absorb all
at once why don't we-- oh, yes?
>> Whys is that Make Hello not Make Hello.c?
>> Good question, why is it Make Hello and not Make Hello.c?
Honestly, just because, like the programs authors decided
that if you type Make Hello, it will assume
that you want hello.c and it will look for those files.
We'll also see that it's more motivating in future weeks
because you can, you can create nicknames so that
when you say make something?
Something can be defined by you as a doing sequence of steps
that you wanted to automate.
>> What would happen if you had another file
like that tells hello [inaudible]?
>> If you had another program called Hello but you want it
to compile hello.c, would it get confused?
No, 'cause that's what we did a moment ago,
I still had the Hello program
but so I typed Make Hello and it said, um-um.
It's up to date because it had already been compiled
and it realized that it would create the same output.
So, in short no, yeah?
>> If you are to type Make Hello.c would it have done the
same thing?
>> Good question,
so and actually I can emphasize this quite explicitly,
when in doubt honestly?
When you have a computer, when you have a keyboard in front
of you, try it, the worst you can do is screw everything up
and download a new appliance.
So, Make Hello.c, enter, nothing to be done
for hello.c. Well the last time I saw that it related to this,
so let me get rid of this.
So, now let me do Make Hello.c, it doesn't want to compile it
because what it's now looking for, it's now realizing
that this is not a file that needs to be compiled
so in short, error message though it's not explicitly clear
that this is a problem, yeah?
[ Inaudible Remark ]
>> Really good question.
Do the files we create like hello.c exist in a file
on your own hard drive?
Actually no, if you poke around your own Mac
or Windows hard drive, you will just see one huge file,
it's called a VMDK file, Virtual something or other
and that means that all of the files you create inside
of the appliance are actually hidden inside
of one Gigabyte file and that gigabyte is chopped
up into the individual files but you'll see overtime
in the specifications for P-sets and in the online documentation.
You can interact with the appliance,
with your own computer, that home directory
that John Harvard's files are stored in?
You can actually mount that on your own computer,
you can connect to it via various internet programs
so in short you will see that we can interact with this
in all sorts of ways especially
if you find this environment just unappealing or confining,
you won't risk-- strictly need to use it.
There's many ways to make use of the appliance still.
So, this is a lot to absorb all at once,
let's take a two minute break to kind of give you a,
give you a moments rest bit and we'll resume
with something more fancy.
[ Inaudible Discussions ]
[ Background Noise ]
>> All right, so we are back,
if at any point honestly you are confused, have questions,
please catch my eye, raise your hand or I'll certainly stick
around afterward, more over moving forward in addition
to PDFs, of slides and source code being available online,
realize that all of the programs we write
in class will either be posted in advance on the website
or if we create it on the fly, we'll post it right
after lecture so that you can download it to your own Mac
or PC inside of the appliance and so even
if you want during class if you find it helpful to play along
and actually type what I'm typing,
get the same error messages I'm getting,
realize you can run the appliance in Sanders here
on your own machine, the wireless is pretty spotty
in Sanders right now, they're working on bolstering
that for later in the semester
but if you do download the appliance back home
or somewhere else on campus then next time bring to class,
realize it's a good way to more actively engage rather
than just sitting there listening passively.
So, a couple of more building blocks
with which we can do more interesting things.
So again a function is a chunk of code, a chunk of lines
of programming code that do something and what do they do?
It's really up to you, main is entirely up to you
because what you put in main just like in Scratch,
what you put in one green flag clicked is what happens
when you start running the program.
Now, printf, you can control its behavior only to some extent,
printf is another function whose purpose in life again is
to print something to the screen
but the F actually denotes something, it means formatted
because even though we've been using it for very simple things,
Oh hi world, you can actually print numbers and characters
and whole words and sentences and other things.
You can format numbers as integers, as real numbers
with decimal points and all sorts of other conventions.
And so printf takes what are called arguments,
so when we saw printf before,
recall that we saw these parenthesis inside of my program
and any time you see parenthesis after the name of a function,
that's your way of modifying its behavior.
If you think back on Scratch with all those puzzle pieces,
sometimes you could type a number into a little white box
or you could drag a puzzle piece on top of another and which sort
of magically grow and then contain it?
Well, same idea here, you can modify the behavior
of puzzle pieces and functions by passing them arguments.
It's just in C, you have to be a little more deliberate
with the keyboard and so anytime you see parenthesis right
outside, right after a function name
that means it's taking input generally called arguments
or parameters but these are for our purposes now synonymous.
It takes input, why is that useful?
Well, it'll be a pretty stupid function
if all printf could do is print, oh hi world,
it be nice if you could print anything and indeed it can,
I just have to choose as the programmer what to pass
in between double quotes.
Now, any guesses as to what this backslash N
at the end has been representing?
>> New line.
>> So, new line, so computers again are pretty dumb,
they only do what you tell them to do and if you don't tell them
to move the blinking cursor
to the next line, it's not gonna do it.
So, if I actually omit this, save my file and let me zoom in
and then re-run Make Hello, Enter and then run Hello,
notice what happens at the prompt, it's kind of a mess.
It says, oh hi world!
But then the prompt reappears and that's
because I've omitted the backslash N.
And we'll see there's other ones, there's backslash R
and backslash T but backslash N is by far the most commonly used
in C and in other languages.
So, what else do we want know about?
Well, in C, you have the notion of variables which we'll see
in a moment but these variables are specific type,
there's a char which represents a single character,
the act of touching one key on the keyboard,
there's an int which is an integer.
Now, an integer as an aside is 32 bits
and we only had eight bits on stage before,
with eight bits you can represent how many
different values?
It's actually 256, how do you get there?
Well, if you have eight bits here and each of these bits
or volunteers can be a zero or a one?
There's two opportunities here, two opportunities here,
two opportunities here, zero, one, zero, one,
so if you multiply all those together, two times two,
times two, times two, that's the same thing as two
to the eight and that's 256.
So, now if you have a 32-bit number not an 8-bit number
but a 32-bit number, that's two to the 32
which is roughly, anyone know?
It's actually 4 billion, give or take.
So, two to the 32 is four billion,
now what's the takeaway?
That's means an integer in a computer can store the number
from zero to four billion or more typically
from negative two billion to positive 2 billion, give or take
but notice the key takeaway here, it's finite,
there's only a fixed number of bits
which means there's only a fixed range of values which means
at some point if you try counting up to two billion
to billion one, two billion two, two billion three,
eventually what's gonna happen?
You're actually gonna end up being back at zero
or a negative number, some crazy thing's gonna happen
where you're counting is actually gonna wrap around
and bad things then happen as a result.
So, how do you get even bigger numbers?
Well, you use bigger integers, you use 64-bit integers.
So, if you've heard that your PC or Mac these days is 64-bit
that means that all of the numbers
with which it performs math are now 64-bits
and that generally is good for speed and performance
but it also means older programs that are 10 years old
or more often will break on that computer.
So, we are using as an aside of 32 bit virtual machine
so that it works on anyone's laptop even
if it's several years old but there's other data types.
Chars are 8 bits, ints are 32 bits, float is also 32 bits
but it's what's called a floating point value,
a real number where the decimal point can actually,
it's a real number with a decimal point.
Now, there too, if you think about it, real numbers,
we were taught in grade school can be infinitely precise,
you can have 0.333333 and you put a little line over it
which means infinitely many threes,
that's really darn precise.
It means one third exactly, computer, you're gonna have
to stop writing down threes at some point,
write just conceptually even if bits are completely new
to you it stands to reason that if you only have a fixed number
of them whether it's eight or 32 or 64,
at some point you're gonna have to decide,
"Do I want to represent 0.3333333 or 0.4444444?
In other words, there's only a finite number of permutations
of zeros and ones, only a finite number of permutations
of these eight humans on stage so you've got to pick
which of those real numbers do you want to represent?
So, in short computers are in inherently imprecise
and this can actually create some significant problems.
If you think back some 10 plus years, the Y2K problem
which is one of the more recent times
when everyone thought the world was gonna end?
Well, what was the Y2K problem, what was the source
of that issue and all the drama, yeah?
[ Inaudible Remark ]
>> Yeah, exactly so in a lot of computer programs,
the date accidentally went from 1999 back to 1900
because the programmers made the judgment call,
good or bad at the time, so you know, "ugh, there's--
no one's gonna be using my software in the year 2000,
I'm just gonna use two digits
to represent the two number after the 19."
The problem though is if you're only using two digits
and you get to 1998, so the 98, 99 what do you get
when you wanna hit 2000?
You need the number 100
but if you're only using three decimal digits,
you're kinda out of luck.
So, what did these programs do in Y2K?
They wrapped around back to 00 so all
of a sudden people's dates were 1900
and just bad things happened, the world did not in fact end
and in fact a lot people made a lot
of money fixing all these problems
but using fairly arcane languages at that time
but the point was that that was just a design decision,
arguably a foolish design decision but if you can imagine
from the 10,000 pound computer, memory was expensive back
in the day and it wasn't all that large so you wanted to cut
as many corners as possible but it turns
out that people were still using software that's 20 years old,
30 years old and I know that on campus there's computers
that are still running software that old
and they have just been patched together over time.
So, what can you do when you need a bigger number than32 bits
for an int or a float?
Well, there's-- ironically you'd think it would be a long number
but it's in fact not, it's usually a long, long,
so we'll see that you call the data type, the type of number,
it's a long, long and that typically gives you 64 bits.
If you want even more precision
for that decimal point you can instead use a double
which is a 64 bit real number, it's still finite so you cannot
in fact represent all possible values despite what we learned
in grade school.
You have to choose a finite range of them so again
that means that computers are inherently imprecise.
So, how do we actually use these things?
Well, we've only seen printf as a function thus far
and so we've actually seen way more functions or puzzle pieces
in Scratch so let's round out a few more.
>> So, in C, it's actually not all that easy
to get user inputs, you kinda have to jump
through some hoops just to see what the user typed and not
to mention convert what they've typed
from ASCII characters to actually numbers.
For instance, when you type in 1.5 on a keyboard,
that's not a number, that's actually the character one,
the character period, the character five,
so you have to kind of convert that to a floating point value
and how we do that we'll ignore for now but it's suffice
to say some work has to go in to getting user input
so we have what's called the CS50 library which is a suite
of functions that you can use in your programs and indeed we do
for the first several weeks and the names imply what they do.
If you want to get a character
from the user we're gonna call Get char or get double
or get float or get in to get a long-long
or get string depending on the data type
and that's gonna allow me to write programs like this.
Let me go back to my code here, let me go ahead
and say not just, oh hi world now
but let me declare a variable
and then see you declare a variable like this, string S
and I'm gonna go ahead and say, "Get a string from the user
and get string just like printf is a function but what's neat
about get string is that it doesn't just do something
and leave it at that, it does something
and then it hands it back to me, all right?
So, it's as though this is doing my bidding, get string,
it's gonna deal with all the complexity of keyboard input,
it's gonna get that string of characters from the user
and it's gonna hand it to me in such a way
that I can store it inside a variable just like Scratch
and I'm gonna call S and the type
of that variable is what we'll call string.
If you're familiar with Java, it's not capital S,
it's lower case S string but it's just a variable and that's
because of the CS50 library.
Now again, I said that we don't always have to type,
oh hi world, we can pass in an argument whose value changes.
So, it turns out in C there is these place holders and if I do,
not just a hard coded string of characters, I do percent S,
that means put a string here.
Well, what string do you want to put there?
Well, let me go ahead and just move outside of the quotes,
comma S and what we'll see here is
that some functions can actually take not just zero, not just one
but even two arguments and the convention in programming is
to separate those arguments, those inputs,
those parameters again all synonyms by a comma,
a comma right, a comma separated list of arguments.
So, now what's gonna happen?
Well, whatever I typed is gonna get printed
and that is gonna be-- because the S gets substituted
for the percent S here.
But I need to do one more thing
and I can finally now explain what was going on here.
This includes standard io.h is what's called a header file.
It turns out that I can't just start using things like printf
and just assume that they'll work,
I have to tell the compiler,
"I wanna use a function called printf, written by someone,
some 20, 30 years ago and that function is declared, it's--
it's written in a file called, standard io.h, now I don't know
where that is, it's somewhere on the hard drive
but the compiler know where to look by default.
So, that's why I was able to use printf,
if I had omitted the highlighted first line,
I would have gotten some kind of error.
Same thing with the CS50 library,
if I wanna use the CS50 library, if I wanna use the CS50 library,
I have to say, use CS50.h and I don't know where this file is,
it's somewhere in the appliance's hard drive,
GCC will find it for me but now I have the ability
to use the Get String function as well.
So, you didn't have to do this in Scratch,
Scratch makes everything available to you at once
but now I have to be a little more deliberate and that's okay,
it keeps my programs even more explicit as to what's going on.
So, let me zoom in again, let me type,
what do I type to compile this?
Yes, it's kinda, it's kinda there,
you can always cheat there, so make Hello, Enter, okay,
no errors so that's good, run Hello, nothing seems
to be happening but I do have a blinking prompt
so I can say something like, David, Enter and ump kind
of a bug, I don't really like that output.
What can I do to fix?
[ Inaudible Remark ]
>> Yeah, backslash N there
and I can make this a little more user friendly,
I can say something like, printf, name please, colon,
space and again a semicolon turns
out this is just a convention at the end of most any statement
in C you have to put a semicolon.
So, now I have a three line program.
Let me go back here and re-run it, David, didn't fix.
Okay, good, right, so just
because I changed the source code doesn't mean the program
itself is gonna start working
so let me actually re-run Make Hello, that looks good,
now let me re-run Hello, enter name please,
that looks prettier, enter, David.
Now, you can imagine making even more sentence structure there
but now we have the ability to get input and get output.
Let's take a look at just one other format string,
it turns out I'm gonna go ahead and get a float from the user
and I'm gonna have to change this now to floatf and the name
of my variable, I'm just keeping it simple, F for float,
I only have one variable,
there's no confusing with something else.
I'm gonna put percent F here
and F here 'cause now I wanna say just give me a number please
so let's go ahead and run this program.
So let's go ahead and run make Hello.
Run Hello.
Number please?
Let's do 1.5, enter.
Interesting, that looks pretty precise.
That's a lot of zeros.
But it turns out we can reveal to ourselves at least
for educational purposes here,
I actually wanna see what number is really being stored there.
Let me go ahead and show 10 digits after the decimal point
so I'm changing the format string.
Instead of just percent F, I'm doing percent 0.1F
but that's just so I'll see more on the screen.
So let me rerun this, make hello.
Let me rerun hello, enter 1.5, interesting.
So that's actually a pretty good number.
Let's try something else.
Let's run it with 0.1, Enter.
That is not what I typed.
So why did this just happen?
I'm pretty sure I did not type 0.10000015.
Why does the computer think that's what I indeed stored
in a variable?
Yeah?
>> It doesn't have that precision
to represent 0.1 exactly.
>> Exactly.
[ Inaudible Remark ]
>> It doesn't have enough precision
to represent that number 0.1.
Simple as that number is exactly
because if you only have 32 bits you got to pick and choose
which numbers you will be allowed to represent so rounded
to the closest possible much.
Now this might seem like a silly bug or issue
but they can actually lead to very serious problems.
And indeed to help visualize this,
there's some very famous examples in history of programs
and real world things that have been implemented in software
and that's software is flawed because humans
like us literally have made mistakes as simple or as trivial
or as innocent as that.
And you'll see from this video some of the things both
with real consequences or not that tend to happen.
So let me go ahead and pull this up.
It's just a few minutes long and let's see what indeed can happen
and just how severe, how important in understanding
of these low levels primitives are
when you're actually making decisions to buy, to write,
or to actually use software like this.
[ Background Music ]
>> We now return to Engineering Disasters on Modern Marvels.
Computers, we've all come
to accept the often frustrating problems that go with them,
bugs, viruses, and software glitches are small prices
to pay for the convenience.
But in high-tech and high-speed military
and space program applications,
the smallest problem can be magnified into disaster.
On June 4th, 1996 scientists prepare
to launch an unmanned Ariane 5 rocket,
it was carrying scientific satellites designed
to establish precisely how the Earth's magnetic field interacts
with solar winds.
The rocket was built for the European Space Agency
and lifted off from its facility on the coast of French Guiana.
>> At about 37 seconds into the flight,
they first noticed something was going wrong,
that the nozzles were swiveling in a way they really shouldn't.
Around 40 seconds into the flight,
clearly the vehicle was in trouble.
And that's when they made the decision to destroy it.
The range safety officer, with tremendous guts,
pressed the button, blew up the rocket before it,
it could become a hazard to public safety.
>> This was the maiden voyage of the Ariane 5,
and its destruction took place because of a flaw embedded
in the rocket's software.
>> The problem on the Ariane was that there was a number
that required 64 bits to express and they wanted
to convert it to a 16-bit number.
They assumed that the number was never gonna be very big.
That most of those digits in the 64-bit number were zeros.
They were wrong.
>> The inability of one software program to accept the kind
of number generated by another was at the root of the failure.
Software development had become a very costly part
of new technology.
The Ariane 4 rocket had been very successful so much
of the software created for it was also used in the Ariane 5.
>> The basic problem was that the Ariane 5 was faster,
accelerated faster, and, and the software hadn't accounted
for that.
>> The destruction of the rocket was a huge financial disaster,
all due to a minute software error.
But this wasn't the first time data conversion problems had
plagued modern rocket technology.
>> In 1991, with the start of the first Gulf War,
the Patriot missile experienced a similar kind
of number-conversion problem, and as a result, 28 people,
28 American soldiers were killed
and about a hundred others wounded when the Patriot
which was supposed to protect
against incoming Scuds failed to fire a missile.
>> When Iraq invaded Kuwait and America launched Desert Storm
in early 1991, Patriot missile batteries were deployed
to protect Saudi Arabia and Israel
from Iraqi Scud missile attacks.
>> The Patriot is a U.S. medium-range surface-to-air
system manufactured by the Raytheon Company.
>> The size of the Patriot interceptor itself is,
it's about roughly 20-feet long, and it weighs about 2,000 pounds
and it carries a warhead of about,
I think it's roughly 150 pounds.
And the warhead itself is high explosive
which has fragments around them.
The casing of the warhead is designed to act like buckshot.
>> The missiles are carried four per container,
and are transported by a semitrailer.
>> The Patriot antimissile system goes back
at least 20 years now.
It was originally designed as an air defense missile
to shoot down enemy airplanes.
In the First Gulf War, when that war came along, the army wanted
to use it to shoot down Scuds, not airplanes.
The Iraqi Air Force was not so much of a problem
but the army was worried about Scuds.
And so they tried to upgrade the Patriot.
>> Intercepting an enemy missile traveling at Mach 5 was going
to be challenging enough.
But when the Patriot was rushed into service,
the army was not aware of an Iraqi modification.
That made their Scuds nearly impossible to hit.
>> What happened is the Scuds
that were coming in were unstable.
They were wobbling.
The reason for this was the Iraqis,
in order to get 600 kilometers
out of the 300-kilometer range missile took weight
out of the front warhead, they made the warhead lighter.
So now the Patriot is trying to come at the Scud and most
of the time, the overwhelming majority of the time,
it will just fly by the Scud.
>> Once the Patriot system operators realize the Patriot
missed its target.
They detonated the Patriot's warhead
to avoid possible casualties if it was allowed
to fall to the ground.
>> That was what most people saw, as big fire balls
in the sky, and misunderstood as intercepts of Scud warheads.
[ Background Noise ]
>> Although in the night sky, Patriots appeared
to be successfully destroying Scuds,
at Dhahran there could be no mistake about its performance.
There, the Patriot's radar system lost track
of an incoming Scud, and never launched due to a software flaw.
It was the Israeli's who first discovered
that the longer the system was on,
the greater the time discrepancy became due to a clock embedded
in the system's computer.
>> About two weeks before the tragedy in Dhahran,
the Israeli's reported to the Defense Department
that the system was losing time.
After about eight hours of running, they noticed
that the system is becoming noticeably less accurate.
The Defense Department responded by telling all
of the Patriot batteries
to not leave the systems on for a long time.
They never said what a long time was.
Eight hours, ten hours, a thousand hours, nobody knew.
>> The Patriot battery stationed at the barracks at Dhahran,
and its flawed internal clock, had been on over 100 hours
on the night of February 25th.
>> It tracked time, to an accuracy
of about a tenth of a second.
Now, a tenth of a second is an interesting number
because it can't be expressed in binary exactly
which means it can't be expressed exactly
in any modern digital computer.
It's hard to believe, but use this as an example.
Let's take the number one-third.
One-third cannot be expressed in decimal exactly.
One-third is 0.333 going on for infinity.
There's no way to do that with absolute accuracy in decimal.
That's exactly the kind of problem that happened
in the Patriot, the longer the system ran,
the worse the time error became.
>> After one hundred hours of operation the error
in time was only about one-third of a second, but in terms
of targeting a missile traveling at Mach 5,
it resulted in a tracking error of over 600 meters.
It would be a fatal error for the soldiers at Dhahran.
>> What happened is a Scud launch was detected
by early warning satellites, and they knew
that the Scud was coming in their general direction.
They didn't know where it was coming.
>> It was now up to the radar component
of the Patriot system defending Dhahran to locate and keep track
of the incoming enemy missile.
>> The radar was very smart.
It would actually track the position of the Scud,
and then predict where it probably would be the next time
the radar sent a pulse out.
That was called a range gate.
>> Then once the Patriot decides enough time has passed
to go back and check the next location,
for this detected object, it goes back.
So when it went back to the wrong place,
it then sees no object and it decides
that there was no object,
it was a false detection, and drops the track.
>> The incoming Scud disappeared from the radar screen,
and seconds later, it slammed into the barracks.
The Scud killed 28, and was the last one fired during the First
Gulf War.
Tragically, the updated software arrived
at Dhahran the following day.
The software flaw had been fixed, closing one chapter
in the troubled history of the Patriot missile.
The Patriot is actually an acronym
for Phased Array Tracking Radar to Intercept of Target.
>> So it suffices to say there are all sorts
of non-militaristic applications of this stuff
but it's frightening just what the implications are.
And so, over the next few weeks,
what we will do is focus not only on doing
but understanding exactly how all of this stuff works
so that you yourselves are empowered
to build almost anything you would like by terms end.
We will see you on Friday.
[ Noise ]
[ Silence ]