>> Well this is CS15.
All right, welcome back to the end of week one,
this is our last Friday lecture,
henceforth they will be Monday and Wednesdays only.
A couple of announcements though, zero new handouts
but there is code outside if you didn't get it on Wednesday.
Office hours will be held one last time this evening for those
of you who want to hang out in a group and work
on scratch projects or just get help
from the teaching fellows and CA's.
Cavott [assumed spelling] dining Hall
and Quincy dining hall per the, that is entirely false.
[Laughter] No more office hours for PSet 0 but they will be held
for PSet 1 I'm mis-remembering our own schedule.
So office hours will generally be offered on Sunday, Monday,
Tuesday, Wednesday, Thursday evenings at different supplies
of staff members so do check the course's web site if you would
like help with PSet 1 which will be released
on the course's web site at 7:00 PM tonight.
So this coming Monday we won't have proper sections just
yet with your own intimate classrooms
with single teaching fellows
but rather we'll have a few Super Sections on Monday,
Tuesday and Wednesday.
Check the courses web site to find out the schedule.
These are meant to help you, not only with the first couple
of weeks material particularly the C and Linniks but also
to help you with PSet 1 which will,
again be released this evening but in addition to that,
starting this Sunday, led by Marta,
one of our teaching fellows, will be, hey Marta,
will be the first of several walk throughs
so once per week what we do is hold an course wide session
that's a code walk through and its really meant
to help you get started with the standard addition of the PSet.
It will answer such questions as where do I even begin,
what is this all about, what might be some strategies
for tackling the problems set, those will be Sunday nights,
check the web site for the schedule, they'll also be filmed
and placed on line, usually by Tuesday
so if you can't make it you'll still have that resource
and they are meant to eliminate questions of the form
and office hours, where do I begin?
Finally PSet 1 so, it will in fact go out tonight,
this will be your first PSet involving Linniks
which may very well may be new topics to some of you.
you'll find that the PSet walks you through a lot
of the mechanical details that we looked at on Wednesday
in great detail so you should not feel yourself
in over your head at all and you'll find
in the standard edition, which again, is meant for 90 percent
of the class, a few challenges.
So the first one is going to introduce you
to the world of ISBNs.
Not a terribly sexy field but these are the numbers
that have been stamped on the back of most any book
that you've ever purchased.
Well it turns out those aren't just random numbers,
there's some pattern to them
and we can actually detect whether something is
or is not a valid ISBN and what you'll find,
though very much a bite sized problem,
it will non the less get you acclimated, I think fairly well
with some basic C syntax and with loops
and with manipulating characters and or numbers.
So you find that it's a nice introduction to that
but then you'll dive into something that's more
about thought and more about design.
You'll be tasked with solving the change making problems
so you've probably never thought
and probably shouldn't have given it ever much thought
to what the cashiers do at say, CDS,
which is when you're due some change,
they look in their cash drawer,
they've got some 20's some 10's some one's some change
and odds are that human goes from left to right or from right
to left through their cash register in order
to hand you some amount of change
and if they're doing this intelligently
or at least a bit methodically they're probably going
to minimize the number of bills or coins that they hand you
because no one likes to get 10 singles back
when you're owed $10.00,
there are more optimal solutions to that problem.
But thinking about it, and actually expressing
that algorithm that cashiers throughout the world have been
executing for years is actually interesting in and of itself
and so we'll have you implement what's generally known
as a greedy algorithm for solving that problem
of making change and then finally will you implement a
little something reminiscent of Super Mario One,
his little pyramid which will be a quick little exercise in loops
and in printing and such and the hacker addition meanwhile
for those of you who would
like to elect it presents slightly more sophisticated
problems throughout and in particular provides you
with some sample credit card numbers
and has you validate credit cards instead of actual ISBN's
as the algorithms a bit more sophisticated.
So with that said, that will be posted
on line later this evening.
I thought I would address one thing, so we were quite,
well we were, its interesting to note
that in the surveys you guys have been submitting
for problem set 0 which still isn't due for several hours,
most of you have just regular mobile phones which is great,
because it means we don't have to spend
as much energy developing, say,
sophisticated iPhone applications
when only 10 15 percent of you actually have them
so we've actually optimized now Shuttleboy not only
for your voice but also for SMS.
Now this is a bit of a hype because this actually existed
for several years but I kind of let it die for a little while
since I didn't actually fix it, well its been fixed
so now you can text a message like, and the succinct might
like this, if you text a message
to 411 this is what's called a short messaging code
so there's people in the world
that have these very short telephone numbers and you
for final projects will actually be able to make use
of precisely the service if you send a text message
to that number and send it a message like, sboy,
doesn't matter if its capitalized or lower case
and then a and b, where a is your origin,
b is your destination like Memorial Hall or like
and followed by quad or you know,
that's actually pretty tedious to type on a phone
so you actually just use the first three letters,
cryptic though this may be, memqua will get you
to the quad right after class
so if you actually send a text message there,
what's actually happening is an interesting sequence of steps
which is, a message gets sent to this number,
this number has some fancy phone switching equipment for SMS,
they route that request, whatever message you type in,
back to one of our servers here on campus, our server then says,
oh, here's two inputs, a and b and memqua, what is that,
it parses those strings, analyzes them, figures out, oh,
obviously this is Memorial Hall and obviously this is quad
because there's no other stop starting with those letters,
we do a look up in our own data base,
much like this website here does
and then return a fairly succinct explanation
that the next shuttle is in two minutes at such and such a time,
so we're using a little bit of web programming,
we happened to use php and some Java Script here,
can you implement precisely this kind of service on demand.
So more on that toward terms end and some
of you may dive in yourselves.
But we left off here.
So we introduced C, we introduced Linniks and we talked
about some of the syntax but now we can start
to do much more interesting things with this
so int main cryptic string it first,
but for now this is sort of a copy paste job.
Whenever you start out writing a program for say, PSet 1,
odds are its going to be in a file called something.C
and then one of the first things you're going
to do is write something like int main
and then this expression in parenthesis.
Your not going to put the semi-colon at the end
because this program has no content, you're going
to actually have a curly brace, some stuff
and then a closed curly brace and that constitute your program
and how do you make all this happen?
Well, if you're on, for instance a Mac, recall that we began
by running a program called Terminal,
problems that once PDF will walk you through the process
of making that icon appear if you don't yet actually have it.
if you're on a p c what you'll do is run a program called putty
which will give you a little window like this
and again the program set will point you at a hand out on line
for just clicking the right buttons and typing
in the right words and you will then be connected to your
so called FAS account at which point,
your at the so called blinking prompt so we used a program
on Wednesday to start writing a program.
What was the recommended app that day?
It's a little program.
Nano. So nano is this very simple text editor,
it allowed me to do something like Nanofooc [sounds like]
and then I could just start typing some code
and there was some stuff at the top of most files,
I had comments thanks to the stars and the slashes,
I had some pre-processor directives,
that was the fancy way of saying sharp include, standard IO
or CS50's library so we'll see some of that same syntax again
but once I then had my program running,
say hione.c I then ran gcchione.c hit enter,
I then saw nothing happen but if I type LS I saw that oh,
an out has just been created and if I in fact run about I
in fact got the results as my program.
So that's where we left off, we can do more interesting things
than that so let's see what?
We had this first.
This ability to print.
So this is a cryptic way of saying
that print takes one argument
which is generally called the format string,
followed by some number of optional arguments
so it's a pattern like this, I actually ripped
out of the manual, the man page or this reference on line
and that's just kind of a clue to me, the programmer,
how do I use this function?
Well this function takes constant char*.
Well constant just means constant and forget
about the meaning for today's purposes, char means character
but I want to have multiple characters so in fact today
for at least the next couple of weeks,
whenever you see char* this is going to be synonymous
with a word, or more generally a string.
So a char is a single character, a char* is a string so to speak.
It's a sequence of characters, like a word, a sentence,
a paragraph, whatever.
So char* just means that I can pass in a pair
of double quotes inside of which is things like percent
or percentile and some other things we'll see today.
Now as for the ...
what do you put there?
well you plug in whatever variables whose values you want
to be substituted into the percent or into the percents
and then there's some escape sequences and escaping is going
to be something that will occur throughout programming
in general because you often need to treat special characters
in a special way and these were just some
of the ones that we saw.
So we then started with some math
and that was very uninteresting at first because I did
like 1+2 equals 3 and then I did nothing with the answer but at
that point we introduced the notion of a data type
so variables in scratch are pretty much numbers
that you can also pass in strings.
The user can type in a string and that gets stored
in that answer variable but you don't tell scratch that,
you know what?
I want to put a floating point value here.
You don't tell scratch I want this to be a 32 bit
or 64 bit variable, it does all those details by itself
and after all, what 12 year old cares about that level of detail
and that is in fact their target audience.
Well we need a bit more power, a bit more expressiveness
and you can INSEE as is the case with Java
and some other languages specify what type of data you want
to store in a variable and this is useful
because sometimes the language will protect you from yourself,
it won't allow you to perform, say arithmetic,
on a string because that just doesn't make sense
and other times it will allow the compiler,
say GCC to optimize where those things go in memory
or how their laid out.
So C has these built in data types, but char,
which is a single character.
A double, which is a 64 bit real value.
Something with a decimal point.
A float is a 32 bit floating point value with a decimal point
and then and int is just an integer.
And again, a quick sanity check from, last time,
how big was an int in say bits or bytes?
32 bits right?
so if you've ever heard this jargon in the media
that this is a 64 bit architecture
or this is a 32 bit computer well that's just a fancy way
of saying how big are the integers that that computer uses
and it actually relates for the size
of something called a register which is a little piece
of memory inside of the INTEL PTU
so if you've ever seen this advertised
and even Apple does this these days, 64 bit Zeyon Mac Book Pros
and what not, its just referring, frankly to the size
of an integer which has implications
for how much ram you can have,
how much disc space and other things.
But there's these other types of data that you can use
in C, there are longs.
Now, longs a bit of a misnomer at least these days
because a long is how many bits?
So its also 32 and we saw that because very quickly at the end
on Wednesday I used the size of operator and just printed
out the size in bytes of an int and a long
and they were the same.
So we have this stupid need to say, not long,
if you have 64 bits, but long long
and then short actually goes the other direction,
16 bits generally.
Signed and unsigned are types and its technically signed int
or unsigned int but you can abbreviate by just saying signed
or unsigned as the word implies means
that you can have negative numbers or you cannot.
So if you have a signed integer,
what's the largest positive number you can represent,
give or take?
so 2 billion because you need to go negative 2 billion
if you want negative numbers
but an unsigned int gives you literally twice
as many positive values because you can now go from 0
to 4 billion give or take and that's actually important
when you start to write programs that actually care
about large data or big chunks of memory or things like that.
So those are our data types, the format strings
that pertain to these are several.
So this is another sort of RTFM if you want to figure
out what's the right one for what
but for the most part you'll start using percent S
for strings, percent C for chars, percent D for ints
and for long longs, just FYI its percent LLD for long D
so the rest though are generally not going to be as useful,
you can have like scientific notation and other things there
that just aren't as often used, I would say.
so C has arithmetic, we saw plus we saw assignments,
not to be confused with the equal sign,
but FYI you can do subtraction, multiplication, division
and this last one, which is actually a little interesting,
so there's the arithmetic remainder operator
or there are slight distinctions, modules.
Now in this percent sign means you can do X percent Y
in a program and that is similar to doing x divided by Y
but what the X percent Y does is that it returns not the result
of dividing one into the other
but rather the remainder if you do so.
And as simple as this is,
this will prove an incredibly useful trick, through all sorts
of context when writing software so this ability
to get remainders we'll see sill actually be a very neat trick.
And this table is just meant as a reference
so it's not interesting at all to dwell on this
but C has a whole bunch of operators, right?
There's a plus, minus, division, multiplication.
We've seen the equals sign,
we'll see less than, and greater than.
So there's all these punctuation symbols
that have some mathematical or some programmatic meaning,
well the world had to decide which of those takes priority
if you've got a whole bunch of them in a row.
In other words if you have in Middle School arithmetic,
X plus Y times Z, which of those operations are you supposed
to perform first?
Y times Z right?
You can parenthesize it to be clear
but your teachers probably taught you if you have a plus
and a multiplication you do the multiplication first.
So C has the same thing, anytime you have multiple operators
involved in some kind of statement, a line of code,
someone's got to will if there's a debate as to
who should get evaluated first so realizing, we'll revisit this
over time, there's a whole set of rules as to
who gets evaluated first and you just kind of learn as you go
which ones more involved.
Which one is more powerful than the other?
So now let's start to do things slightly more interesting
aesthetically so within precision, all right,
this is a really uninteresting way of saying
that we can do something like this.
So let me go ahead and open from your printouts, which again,
will continue today, this bit of code here, this is Math 3.C
so I propose that this is a program that takes two numbers,
17 and 13, divides one by the other and then prints
out that answer and I seem
to have done a pretty good job here, right?
I'm specifying my type as a float,
I'm giving my variable a name, here is the top,
here is the bottom, divide is my operator and okay, at this point
in the story I have my answer inside the variable
called answer.
So what do I then do?
Well, hm, this is slightly new syntax,
let's ignore the part I don't understand, its not even there
so that's really just percent F/N.
so that means put floating point value here,
then put a new line character what value do you want
to substitute in?
Well you substitute in the answer.
Well .2 simply allows us
to control how many decimal points do we want to show?
.2 means give me 2 numbers after the decimal point as opposed
to say, an infinite number
which a real number fundamentally could be
but there's kind of a problem here so let me go ahead
and run GCC of math.C but you know what,
so I can tell my programs apart I'm going to use -O math 3 just
to keep things straight, enter.
Okay, all is good.
I'm going to run math 3 and then hit enter and hm,
interesting, let me go back in.
so 1.0 is the answer but 17 divided
by 13 I'm pretty sure is not 1.00.
So how did I screw up?
>> [inaudible]
>> Yeah it seems to be evaluating them as integers
or we're clearly getting the wrong number,
the wrong answer buy why might that be?
Well 17 divided by 13.
Well what is 17?
You know, frankly it looks like 17 is just an integer.
If I ever write down on a piece of paper, an integer,
I write 17, I don't write 17.0 but the moment I write .0 now
that compiler, GCC knows, oh you know what?
Yeah, it happens to be equal to 17
but it actually should be stored as a floating point value
so we have that ability to remember that bit of data
after the decimal point and you know what?
If I really want to do this right, let me change this
to be .0 there so now I'm taking the floating point,
divided by a floating point and what happens now in C,
as in most languages, is a float,
is sort of more powerful than an int.
it kind of wins this battle and so the return value of X divided
by Y here, if X or Y or both of X
and Y are floating point values, is itself going to be a float?
So if I now recompile this, let me go back to my prompt,
I'm going to go ahead and run GCC on math 3.C,
I get back my blinking prompt, I've run math 3 and there it is.
1.31. Well maybe I want a little bit more precision,
well let me go and change this, so not 2 but let's say 9.
I have to save, and now notice again,
a common mistake early on might be math 3.
Hmm. It doesn't seem to have changed, so again you need
to recompile at this point, so I've rerun GCC
and now I run my program, okay.
So now I've got 9 decimal points of precision.
Well let me get a little greedy and say something like 99.
I really need this to be a very precise answer, maybe its pi
or something like that that should allow for that amount
of precision so let me recompile this.
Enter, and now rerun math 3.
Interesting.
Now maybe that's the case
but I'm pretty sure this is a real number which should
at least in grade school, have an infinite number of numbers
after the decimal point, right?
So why are we kind of bottoming out at some point
and then getting just a whole lot of zeros?
What limitation are we hitting?
>> [inaudible]
>> Yeah, so the size of a float in this case.
So if you have a floating point value and you only have 32 bits,
don't even think of bits as meaning a digit
because you need more than one bit
to represent a decimal digit, 0 through 9
but its clearly a limit.
Where is the limit, I'm even not quite sure,
does it say that the biggest possible float is 4 billion.0
or does it say its two billion
but you can actually have more decimals, more numbers
after the decimal point, but the point is
that there's some finite limit, if you only have 32 bits,
you only have so many numbers you can express, so all right,
let's try to fix this.
If I'm running into trouble, what can I do?
All right, double,
but unfortunately this doesn't really solve the problem
fundamentally; let me try recompiling this,
GCC and now rum math 3 so it's better.
So I've got more numbers, they're going farther
to the right but I'm still bottoming
out so the take away here is that floating point values in C
and in most any language are inherently limited.
In fact, you will occasionally find that even if you're trying
to represent a relatively small number,
like 12345.67 it's not actually going
to be 12345.670000000 ad infinitum underneath the hood
there might be a little bit of rounding error there.
It might be 12345.69999999 and so
with an infinite number of 9 conceptually.
In other words you cannot assume
that there will be precision underneath the hood
which means you should never,
never if you take nothing away today
from about floating point values, never try to compare
on floating point value against the other
if you want an exact answer, because you will run
into these very subtle bugs that are the result
of simply computers being limited in this way.
Well let's take a look at math 4.C. so this is an approach
that I very simply fixed the answer by making
that denominator a floating point value
and that would solve this problem as well.
Now in Math 5, notice that I've done this.
So this is a different syntax
but it reveals another capability that we have
so we can declare a float called answer,
its equal to 17 divided by, well you know what?
The programmers know that a number like 13, though an int,
could also be viewed as a floating point value
so you can force the compiler to treat what is not a float
as a float if you're comfortable that's a legitimate conversion
to make.
So this use of the parenthetical float right here is simply our
way of saying, you know what?
Treat this not as an int but as a float and this is an instance
of what's called typecasting and this is a very simple use for it
but it will let us ultimately change characters to numbers,
numbers to characters and recall from last week
when we had a bunch of volunteers
up here raising their hands and not raising their hands,
representing numbers, we then converted those numbers to BOW,
by way of the ASCII coding system,
well we can do that in code.
And in fact for problem set 2
in the course what you will do is implement a number of ciphers
so cryptographic mechanisms
that convert essentially what is human readable text
to something that's not human readable but you're going to do
that by way of the underlying implementation
of characters as numbers.
All right, this is kind of basic, right?
addition, subtraction all of this,
well fortunately we can use other data types too
in programs, similar to what we've seen in scratch,
so C does not have a bullion data type.
So there's no notion
of a variable whose value can only be true or false.
We could simulate this.
So if I hand you the C programming language
and I tell you there's no data type whose values can only be 0
or 1, you know, how could you kind of work around that.
What might your hack be?
Your solution?
To be more specific, what data type would I use
if bool does not exist?
>> [inaudible]
>> Right, maybe just use an int and just use one
of the 31 bits, right?
just impose on yourself the constraint
that any time you use a variable that you want to be true
or false, just make sure that you only make it 0
or you only make it 1 and yes, you're wasting some bits.
Okay so let's push back.
I don't need to waste 32 bits or 31,
but the smaller data type I could use.
So I could use a char even, a char is 8 bits or 1 byte
and that's why we have 256 possible letters
in the chart we showed last week,
so 8 bits means I'm only wasting 7, that's not bad
but its still unfortunate.
But we, the cores have gone ahead and done this,
so this thing called the CS50 library among,
besides giving you functions like get string and get long
and get int, also gives you a data type called bool
and we just kind of cheat.
Underneath the hood we just implement it as an int or char
or a short and a string.
Where string is literally just a synonym in our code,
we'll see for char * and you'll find that early
on in the course it's just nice to be able to assume
that these things actually exist.
so in fact if I go into high 2.C recall
from Wednesday we had this example
and I said, here's a string.
Well that's not technically a string
because that word doesn't exist, it only exists
because I'm using this library,
well what I've really typed here, which frankly in the first
or so week of the course is unnecessarily complex, is that.
It's exactly the same.
so this is why at least
in the first weeks you occasionally see mentions
of chars and char * but sometimes you'll see strings
but that's because very intentionally
for just the first couple of weeks do we simplify some
of these details but then
when we quickly take the training wheels off
so that you're not making assumptions after this course
as to what does and does not exist.
But here's what exists for the courses say
for the next several weeks.
There's a function called Get Char.
Notice the capital G, capital C. there's a function called get
double, get float, get ints get long long and get string.
So there's actually no get long as I misspoke earlier
because its really kind of pointless
because it would essentially give you back an int.
so let's go ahead and use one of these, I'm provised
to try High 3.C. So we saw this, you recall on Wednesday,
get string is interesting because, one,
it doesn't take any input so print
up took something we'll call arguments,
something in between parenthesis,
get string doesn't do that, some more on that next week
but it does return some things, so again,
we'll call it functions, can take input like print off does
but they can also produce output like get string does
by returning some answer that you had better keep
around if you care about it as in this case here.
Well in this case here, adder,
now we've got something slightly more interesting.
I hard coded a program on Wednesday that added 1 + 2.
Big deal. Not a very useful program but what about this one?
So let me go ahead and compile before we glance
at the code, adder.C, okay?
Interesting.
Problem. What was the problem here, did we say?
>> [inaudible]
>> Yeah, so because I'm using CS50 library it's not sufficient
to just say sharp, include, brackets, CS50.H I need
to also tell GCC that, you know what?
You need to link in with -L the library called CS50,
a little bit of syntax and you get used to it,
any time you see this undefined reference just think, oh,
I'm missing the -L flag so now I can go ahead and run,
actually let me add my -O flag, let me go back to Gcc-O adder.c
and then the L-CS50 needs to go at the end.
All right, now I've got a program called adder, hit enter,
give me an integer, audience participation time.
57. 12 I heard first.
The sum of 57 and 12 is 69.
Either that's very clever, or you, yeah, clever.
You can do arithmetic faster than the teacher can right now.
Excellent, so we've written a program
that adds two random numbers together.
So how did that actually work?
well we have in this file again, the following lines of code,
so we've got one print app which is uninteresting now
because it just says give me an integer and then I have a space,
a closed quote, I don't have a back slash end
and that's how I actually implemented all
of that on one line.
Well then I have this integer called X,
I assigned it the return value of get ints
so that's pretty basic right now and then I do the same thing
for Y and then notice you can start embedding,
even arithmetic expressions inside of parenthesis
as arguments so to speak, as inputs to other functions,
the sum of percent D, place holder,
and percent D is percent D !N
and I just have the comma enumerated list
of the tree variables I want.
All right, so what can we do with this here?
So this is sort of the canonical stupid program one does
in typical computer science class because it's very basic
but none the less allows you to get started.
So here is a blank.
All right, think to yourself, little at home in seat exercise
for say, 30 seconds and no more, what is the one or two
or three lines of coke you might need to write to implement this?
In other words fill in, if you could on a piece
of paper there how you would implement this program
and I'll seat it by saying, you know what?
Go ahead and use get float to get the input.
And for those who like to use this as an opportunity just
to sit there and zone out you will be challenged to look
at the persons code next to you and point out whether
or not it is right or is wrong.
This always seems to help.
[ Music from Jeopardy ]
let's give you one more loop of this.
[ Music from Jeopardy ]
All right, so go ahead if you would and raise your hand
if the person next to you wrote nothing down.
[Laughter] Okay, that's all right, it's to be expected,
but here's a possible approach and let's just see.
Think to yourself or pat yourself on the back if you
at least have a couple of these pieces right.
all right, so this is your first C program, simple though it is,
all of this stuff up here is pretty much cookie cutter
and the comments certainly don't fundamentally matter but are
in fact a matter of good style
so let's see what I've done first.
So print F temperature in Fahrenheit:.
So I didn't specify all this kind of detail
so maybe you didn't even have this and that's fine
but I printed out some prompts.
Temperature in F: and then get some input,
this part hopefully you had at least mentally going on.
So float F gets get float, and that's it ;. All right,
well how do you do the conversion?
Well this part's relatively easy,
even if you didn't remember the formula,
frankly that's the one point of this little bit there,
so we just need to convert that expression from the slide
into mathematics and notice there's a couple
of interesting details here.
So one, you notice
that I intentionally use parenthesis here so that just
like the arithmetic expression on the board it is
in fact doing the subtraction before the multiplication
and then there's one other detail that's sort
of technically interesting and what's that here?
Yeah, so I have the .0 so the denominator there is not 9,
5 divided by 9 because if I had just 5 divided
by 9 what would the value of C always end up being?
If it in fact always be 0 right?
because if you have 2 integers, 5 divided by 9
so that's .something, .4, but then if its an ints well,
you can't fit the decimal point in an int
so you throw away the .4 so then you're left with just the 0
so that means 5 divided by 9 is integers,
in fact always gives you 0 which means multiply 0
by From minus 32, it doesn't matter, its always, in fact,
going to be 0 so we fix that by specifying
that we have a decimal point here, we can fix it
with casting, although
that would look un necessarily complicated,
but then the final answer just involves these basic building
blocks so the .1 percent .1F means what in layman's terms?
Display on decimal point, that's it.
and clearly there's a limit there somewhere
but you're usually safe with just one or two or a few points
of precision, then I say, capital F just
to show units equals and then I have another percent point 1F
Capital C for Celsius and then I plug in the values.
So simple but again, look to this especially as you dive
into problems that won, if you're kind of thinking, wow,
it looks relatively easy and trivial
in class, where do I begin?
Well the basic questions you must ask are, well one,
what framework do you need in place to write a program
and you need, we've seen stuff like this here
and even this is only necessary,
the CS50.H if you're actually using bool or string or one
of our capitalized function names.
We've certainly had examples where we don't even need
that yet, then you have your main routine,
then you have some basic lines of code,
so let's now introduce some tricks we already had last week,
conditions.
So how do you write a condition in C?
You simply say if, and then I parenthesis,
a Boolean expression.
A Boolean expression might be X less than Y, Y greater than X,
very similar in spirit to what you saw in scratch,
you usually need the curly braces just
like scratch had sort of shapes that said inside
of this construct is the following code that you need
to execute, the curly braces achieve that same effect
and now I've done this a few times now,
this slash, slash notation.
What is that probably?
So its another type of comment so whereas at the top
of this file and most we've seen thus far, I have slash, star,
and then whatever I want then,
star slash so that's a multi-line comment in C
and the world again has just adopted some aesthetically
interesting or very anal tendencies
like creating these long lines of stars or boxes like this,
its completely meaningless to the computer,
its just really a matter of style or convention
but it does mean that that comment can go
as many lines as you want.
By contrast inside of my code its very good practice
and in problem sets it will be expected
that you comment your code and by this we mean every few lines
of code, you should have a generally a single line comment
unless you really need to elaborate in more words
than a single line allows.
You would have and then a little comment that's useful
for one you, the reader, so that when you go back and look
at this code again you don't have to sift
through all the minutia of C you can just look at your own words
as to what this chunk of code did and certainly
from the courses perspective it allows the teaching fellows then
to understand, maybe not what your code does,
but what you intended it to do and so realize there's
that motivation there.
But this matter of style, choosing variables
that are intelligently named and describe what they do,
commenting your code every several lines
or actually explaining particularly complicated blocks
of code that you yourself might forget how they actually work
because you were just so darn clever,
it's again a matter of good style.
But we can stack these things so if we have
if else conditions as well.
If, parenthetical then you do some stuff inside curly braces
else you can then go and do something else altogether.
So let's actually see this here in code.
If I go ahead and open conditions 1.C we have this
code here.
So the top is again cookie cutter, this program does what?
So let's see.
This is again, conditions1.C ask user for an integer.
All right, so I've got some print F there that does
that then I'm doing some basic CS50 library stuff, get int,
now I've got an integer and now, okay, I'm using this condition.
But I told a little bit of a lie a moment ago.
What's missing from my own code here?
So those curly braces.
So again silly little syntactical detail
but a tendency people have is if you only have one line
of code related to a branch for the if
or for the outs you actually don't need the curly braces
at all.
So if you have one statement
that you want executed beneath the if
or beneath the let you can simply do it very correctly
as I've done here and therefore not clutter your code
with what ultimately boils
down to just unnecessary characters like,
okay now I can put this in here,
I mean this really hasn't added anything an
in fact its made my page wider, its sort of not quite as fast
to read, so again that's why.
People just sometimes omit those things.
if I had done something like this though,
I'll get into trouble, so if I did this,
you picked a negative number and say something like,
that was stupid!N; so you can indent all you want,
the compiler is not going to realize this.
So if I go ahead and run this program now,
I'm going to go ahead and compile conditions1 I'm going
to save it as -0conditions1 this is conditions1.C enter,
I screwed up again, -LCS50, fixes that mistake, enter.
So now I run conditions1, I'd like and integer please.
44, enter, hm, feels like a bug right?
I thought its only supposed to do
that when I pick a negative number like negative 123,
but again, the reason is that it doesn't matter
if you've indented it you need the curly brace.
So I can actually fix this now, a little curly brace there,
close curly brace there, save it, recompile,
bang G again executes the most recent command, that started
with G and now I'm going to rerun my program
with say, a positive number.
123, well that's good.
Well 456, still good.
How about negative 123
and in fact we fixed that mistake there.
Any questions thus far?
Since I know this has been a lot of little things quickly.
All right, so let's take it up a notch
so that we can actually do more interesting things still
so if you want to stack these things together if else if,
else if, else but again just as some of you might be finding
or once we provide you with some feedback
on problem set 0 we'll find that even scratch programs that are
like yay wide if you're doing a lot of left right scrolling
or a lot of top down scrolling odd are you can implement it a
little more cleanly so you know, its no accident
that I only depict an if else if else here, if you've got 20
of these things, I mean you're pushing the limits
of what constitutes good design and again, these are just rules
of thumb I'm offering right now
and they're fairly vague I realize but we'll see by way
of examples over time why code that starts to get this and this
and this long, can probably be done more intelligently
so in this case, here's the second version of this code.
So here I've actually fixed what was apparently a bug,
you know what, let me go back before we actually spoil that
and maybe you've already realized it, 123 worked,
negative 123 worked, but hm the anal mathematicians
in the room should know that --
so 0 is neither positive nor negative
so I'm actually returning,
certainly though the bug is a mistake, but notice it derives
from the simple fact that I have, let me go back
to the program, and let me roll back to the very original,
I only have two cases.
Either a number is positive or negative
but there's a corner case there and so
when we say you should be testing your own programs before
you submit, just like you're
on scratch programs should you be trying to figure
out if you can actually break them
by doing something the program doesn't intend,
this is in fact the bug but very easily fixed because I can say
if else if N equals equals 0 then I can say something
like printF you picked 0!new line else.
I could do this, else if N is greater than 0 or rather, oops,
if N is less than 0 so do I need to do this?
It will work but in this case you're really just wasting
characters because it's not necessary.
Logically there's only three possible conditions there.
All right, so now let's look
at what you can do inside those parents.
So if condition or condition, so we have this ability not only
to say is this and this true, but we can also ask is this
or this true and you can also string these things together
so this too is useful if you want to check multiple things
at a time, vertical bar,
vertical bar is the ore operator,
ampersand ampersand is the and operator and here's one approach
where we might use exactly this code.
So this is called non-switch's to be distinguished in a moment
from something called a switch and notice what I've done here,
if I actually want to have some kind of boundaries, lower bound
and upper bound here, you know,
I can arbitrarily write this program so if I type
in the number 5, what does this program spit out at me.
I picked a medium number, right?
Its fairly straight forward to read,
that okay so if N is 5 well that's greater than 1, yes,
but its not less than 3 which means this first block
of code doesn't actually execute so N is greater than 4
and N is less than or equal to 6.
That works which means this condition gets executed
but these branches, these ifs and else ifs just
as the English implies are mutually exclusive.
So in fact this is only going to execute one of these lines
of code and the moment this checked off line
of code executes, the compiler, or rather,
the computer does not even look at those remaining code.
So it turns out there's a different approach to this
and again, just as last week on Friday was about kind
of seeding you with some basic building blocks with scratch,
the same deal here before we take things up a level and try
to do something more interesting,
there's also this construct known as a switch in C and also
in other languages where if you find that you're kind
of enumerating a lot of cases again
and again its simply a matter of style or readability sometimes
to use this thing instead so this is called a switch,
you put the word switch and then
in parenthesis a Boolean expression or rather, I'm sorry,
you put in the switch you put say a variable.
So whether its X or Y or Z you put in a place holder there
and then what each of the cases so thereafter, is they check,
does that case match the expression?
Does this case match the exception and if
so do this otherwise do that otherwise if none
of the cases match, do this other thing.
So this construct here is essentially equivalent to saying
if expression equals equals I else
if expression equals equals J fill
in the blank for the last, else.
All right so there is no actual check
because that's what we mean by default so if I pull
up this version here, this is switch 1.C let's take a look
at how I can implement precisely that same idea.
Now the code is longer
so arguably I've not really done anything better here,
this is there saying not an improvement because even I had
to scroll but it's slightly more readable.
Even you at first glance with that last program might have had
to think, okay less or greater than one, less than 3,
greater than 4 and all that, this is pretty darn clear
as to what case is induced what behavior so here too,
its sort of a matter of style, matter of judgment,
is this more readable?
Frankly I think it's probably a coin toss between the two.
Its longer, its clearer
but technically you'll even see things like this,
so technically those could be on one line
so if its simply the length
of the code we're not thrilled with, you know,
that's still pretty reasonable too and I point these kinds
of things out tokay because you'll see sort of discrepancies
in the course from things I say
versus things your teaching fellows said,
versus things the c a's in the lab say
as to how you should write your code and that's
because this is again just a matter of style.
How one writes English kind of varies from person
to person, same deal with code.
What we'll do ultimately
on the courses web site is post a style guide of sorts,
sort of rules of thumb
or different approaches you can take to actually writing code
but for the most parts is sort of an element of flair.
You yourselves will ultimately add to your code.
Whoops, third times a charm.
So what about this?
There's a difference here.
this program is not numeric so it turns
out you can use characters as those expressions
so this program simply says you got an excellent grade,
you picked a fair grade you picked a poor grade and so forth
and it does that by way of enumerating these cases
and it handles the case sensitivity of the user's input
by just saying, you know what, this case applies
if its capital A or lower case a and if you now think
about how we would implement that in code using ifs
and else ifs and else, there would again be a lot
of those Boolean operators like ampersands ampersand just
as in the case a moment ago.
All right.
So now let's do things that are not just syntactically different
but at least visually more interesting.
So we had loops introduced in scratch,
we had loops even demoed on Wednesday,
here is one of the most pop, uh, common approaches to a loop
in C. you've got three things in parenthesis, or initializations,
a condition or conditions if you ampersand them together
or ore them together and then you've got updates so to speak.
So what does that mean?
It means when you write a fore loop,
if you want to do something multiple times you are going
to initialize some value like a variable to 0 or 1,
you are then going to check and make sure
that variable is non greater than some threshold and then
in every iteration of this loop you're going
to do an incrimination or something like that.
Well let's actually try something very simple.
So Foo.C and you'll find that food, bar, bas and stupid words
like these are just placeholders that computer scientists use
when you need to whip out a variable.
So char, star, arg v is the start of my program, okay?
What's that?
Okay, thank you.
Actually it would be funnier if you just let me go
for a while but thank you.
so include standard io.h I'm not sure I need SC50's library just
yet but here I have the basic set up for a program
so let me practice what I'm preaching here,
so 4 give me an integer I and I'll assign it
to a default value of 0, let's do this so long as 200 times
and then each adoration do plus plus.
Well what do I want to do?
Well why don't we just do something like print F
and I know this is kind of simple
but I'm just going to print F this.
So GCC of foo.c enter, now I'm going to run a.out.
Okay, that's a pretty damn fast program.
Let's see if I can do something a little more interesting.
Let me go back to this, more interesting
than star might actually to print out the number
so I could do this and then print out I
so let me recompile that, rerun a. out okay not very readable,
give me a quick fix, all right, n still underwhelming
but let's do that and then a.out okay.
Interesting.
But I thought I said 200.
All right, it's less than.
So again computer scientists or programmers generally count
from 0 so if you're saying 0 I is 0 up to 100
up to 200 realize, yes you're printing 200 values
but the first is 0 thus the last is 199.
Well we can make this even more interesting by screwing up.
What if I do something like this?
So I minus minus is the opposite of I plus plus a.out
so what's going to happen, will this go forever?
Well actually it might go forever, the y. its not going
to go forever in the same direction right, because think
about what's happening, if you,
even though we really haven't talked in detail
about how you represent negative numbers underneath the hood
in this course, at some point you're going
to run out of bits, right?
so what's going to happen if we keep flipping bits from 0 to 1 0
to 1 what range are we going to end up in.
its probably at some point become positive
which is mathematically wrong but we're just starting
to confuse then with the representative of a positive
with a negative number in fact this will take a while
because we're only at what?
800,000 and we've got negative 2 billion to go
so let me go back into this.
How could I fix this and make it more dramatic perhaps?
Let me say so long as it's greater
than 0 and you know what?
I don't have to do just plus plus I could do I gets itself I
times 2 so this little expentiation a.out,
whoops what's wrong?
What did I do wrong?
So times 2, this part's right, I didn't hear it loudly enough
so its initialized to 0 which means here too the loop,
the condition, the syntax is perfectly correct
but I is initialized to 0 do the following while I is greater
than 0 that never happens and so this line here never gets seen
so a quick fix, and again, this is completely nonsensical
at this point, we're trying
to demonstrate a different point altogether, what happens here?
It stopped.
But what happened?
So we kept increasing the value of I so bigger and bigger
and bigger so think back to the light bulbs from last Wednesday,
to keep incrementing numbers in binary from 1 to 2 to 3
to 4 remember we just kept changing 0's to 1's
and then the 1's kept kind of bubbling
up until we had more 1's than we did 0's well
at some point you're going to run out of bits to flip
so to speak, and you're going to get all the way back
down to 32 0's, right?
The clock changes over again and so
in fact this condition eventually does stop us.
Well let's see, if we get rid
of this condition all together can we
at least see what's going on at a.out.
Hm. What's going on here?
Multiplying 0 by 2 right?
If eventually I becomes 0 if I keep trying
to double it I get stuck at 0 so in short this is bad things.
if your code is doing things
like this then there's a problem somewhere
and very clearly there are many different ways you can screw
up code like this but what interesting hopefully is why
this is actually happening
and for today's purposes realize we've hit a couple
of thresholds, one, how large the ints are
that we can represent, and how large the floats are
that we can represent but these are things that become important
when you try to actually now use them in context.
So let's now use this, not to just print out numbers like this
but to do something that's
at least more aesthetically in the progress bar.
So this is a file called progress1.c, you've got it
in your code there and it makes use of the same idea
but now I'm kind of trying
to simulate what's a very reasonable program.
It's not just printing some stupid numbers,
its actually indicating progress like most programs do.
So I've got an int that initializes to 0,
do the following while I is less than or equal to 100 percent
and then increment and now notice this, percent d
and then what's the percent percent mean?
Print a percent, you run into these problems
if percent has special meanings
so percent percent means give me a little percent
and this is a new feature, so sleep1 apparently is going
to make this thing pause for some amount of time,
GSS progress1, let me go ahead and run a.outenter,
all right its probably not worth spending 100 seconds seeing
if this thing is, in fact, correct,
but it will in fact ultimately bottom out at 100 percent.
That's not very interesting, I don't really want to see
where I've been again and again so let me go ahead
and open progress2.c so progress2.c operates as follows.
So that's a little more interesting, right?
It's not moving and for those of you
in the awkwardly close seats here, now you can see that,
so now it's counting up.
How did I do this?
Yeah, so remember this is again, one of these sort
of random things that at first glance doesn't seem all
that useful but if you don't want to advance
to the next line, we n but instead want
to move the typewriters head so to speak sort
of all the way back to the left
and then just reprint the line, reprint the line.
Essentially implementing the idea of animation,
this is in fact one way we can do this, so you know what,
I thought we'd do this.
Let me go ahead and type in endow.c,
I'm going to do intmain intargc charstar rgv [sounds like] I got
a little bit of that, I'm going to need my standard library
so include standard io.h I want to have a 4, oops, careful,
typo there, so 4 ints and endowment gets, all right,
I just so happened to bring this,
so last year our endowment was 36.9 billion so let's initialize
and endowment to 36.9 billion so that's a lot of 0's
and actually c will not like the comma, so let me remove those,
in fact I've already made a mistake which is what?
So while endowment is greater than or equal to,
well now our endowment is 26.0 billion
so let's make that the threshold.
26.0 billions but let me get rid of the things there
and then you know what, endowment minus minus
so now let me go and do this
so print endowment:$ this isn't funny to someone
out there I'm sure, so endowment percent d n well that's going
to look a little ugly, you know, let me whip out that r
and keep returning the reading head to the start
of the line percent d but there is a bug here, endowment,
has someone caught it yet?
Well you gotta say it a little louder and clearer.
So a double or really we have long longs
so Harvard's endowment curiously enough,
is neither now nor then small enough to fit in the size
of an integer, we actually have to whip out a long long
so we get 64 bits because what?
If we had an endowment of 36.9 billion, that's larger
than the 4 billion max even in an unsigned int so let me change
that to long long endowment endowment minus minus
and then I got to change this to a long lld
and now let me go ahead and run GCC on endow.c oops, not print,
there we go, little typo, let me recompile,
let me run a.out all right, let's take a 5 minute break.
[Laughter] all right, we are back but we still have plenty
of money, I went ahead and made the font size bigger,
this is actually those of you who have ever been to Manhattan
and Union Square might know the clock that kinds of ticks
down perpetually so that's essentially what we've
implemented and it really just boils
down to something simple like r.
I'll admit when I first kind of banged
out this example it's really kind of disturbing
to see what it really means to lose dollar
after dollar after dollar.
I mean, just in the time I just spent stuttering,
do you know how much stuff we could have bought?
So anyhow, so we'll leave that running,
probably for several days before we bottom out but let's go ahead
and just wrap up this look at conditions
and let's see once we've gotten some
of these basic building blocks out of the way what you can do
with these same fundamentals.
So the revelation in the progress example,
the progress bar example was quite simply this.
So I'm going to go back real quick to nice.fas I'm going
to log back in as myself here and go into my CS50 directory
and the code in question is this progress2 so the only change
that I made really, is this slash r but you'll notice
in your printout that what I didn't do and actually got away
with it, I actually have this line of code here,
f flush of standard out and I mention this now,
this is one of the things to tuck in the back of your head,
what computers often do in the interest
of efficiency is even though you might tell them to do something
like print this, they might say, hm when I get
around to it essentially so print F has this thing,
you know, a buffer, you're all familiar with buffers
from the idea of like internet videos, buffering and all
of this kind of building up enough bites before you
and then see the movie, well same deal here with print F even
so there's this thing known as standard out put
which is essentially your screen and it has a buffer
of some number of bites or kilobytes or whatever
that the computer then uses to fill up
and only once its sufficiently full does it display what's
on the screen, well in the case of the computer I ran this
on a moment ago, I changed the number
and the change got written out immediately
but this f flush here essentially says,
flush the file, because standard out, we'll see later
in the course is essentially a file, just means spit
out what I just told you to right now
and then sleep meanwhile,
apparently takes a single argument, a single in put,
in this case the number of seconds one
and the reason I was able to use this thing is
because I also included this header fire file here.
so in fact, you'll get used to this over time too,
there's a command in Linniks called man, for manual,
and you can type in something like sleep
and sometimes you need to specify the chapter
of the manual that you want by default
in this class you almost always use what's called chapter 3
or chapter 2, let me see if I guessed right, I did in fact,
so this is the linniks [sounds like] programmer's manual,
the function in question is sleep, its in chapter three
and this is again just kind of a very concise explanation
of what this function does and more importantly
with what library its included.
It tells you what header file you need
so as the course progresses
and as you start getting a little more daring
and finding new functions that we just haven't gotten to around
to mentioning in class, you'll find that just typing man
and then the name of the function with sometimes a 2
or a 3 before it, will tell you how to use that function
and its very similar in spirit to the stuff you'll see
on the courses web site resources page if you go
to that CPP reference.com.
So there's one other approach though, that we haven't
yet done that's already in your printouts and that's this.
So we said there's 4 loops but I also said
on Wednesday there's wild loops
and there's actually another thing called the do wild loop
and these two are just kinds of different approaches
to solving the same problems so in the previous version
of the progress bar
or the endowment program I just used a 4 loop
and frankly I probably go with a 4 loop way more often then I do
with a wild loop, just because, I feel more comfortable with it.
but you can do things differently, so if I know
in advance that I need a variable,
well like we saw Wednesday I can declare that variable
and give it a value but then I can say while that value is less
than or equal to 100 do the following,
so whereas the 4 loop has the 2 semicolons
and 3 things going on, a while loop is actually much simpler.
It just has a condition
which says do the following while this condition is true,
so the effect is ultimately the same.
print this stuff out, flush standard outputs,
sleep for a second, but I kind of have
to manually update my counter for me
because the while loop just does not give you that capability.
Which one is better?
Eh? In this case it feels to me sort of being a little anal
about this, that the 4 loops a little nicer
because everything's kind of self contained all in one block
of code and this is, again,
the kinds of instincts you begin to acquire over time.
this to me feels a little messy, I got this orphaned variable
at the top, I'm doing this I plus plus arbitrarily
at the bottom, its completely correct, its not wrong
and its hard to fault it but there's something about it to me
that kind of rubs me the wrong way and so I might go
with the 4 loop approach.
But, again, there's different approaches to different problems
but there's also this construct
which actually has a fundamentally different meaning
and that's the do while construct.
Do followed by a curly brace and some stuff
and then the wild loop is at the end
and that's the key difference here.
So the do while construct
as it implies aesthetically does something,
the stuff between the curly braces,
then it checks its condition and if the condition is still true,
it does that something again.
Rechecks, does it again, rechecks.
Whereas in the previous case
of the typical while loop its condition gets checked first,
then some stuff maybe happens.
The do while loop is fundamentally different
because it definitely does what's
in between the curly braces at least once.
Now why might this be useful?
Well, let's put it into context.
So here's an example called positive 1.c
so this example here does, oh, okay,
so this is actually representative
of the typical use, frankly, for do while loop.
Any time you want to prompt a user for input
but you don't really trust them,
you think there's a chance they're going to mess
around with you or screw up their input, you want them
to do something once and then you want to check,
did they do it right because if not,
you want them to do it again.
So you want them to try something then you want
to check a condition and then you want to do it again
if they screwed up and that's precisely what's going
on here, what do I want to do?
Well, I want to print out,
I demand that you give me a positive integer,
then I get the ints but notice the order of operations here.
code is generally executed top to bottom left to right,
which means those 2 lines of code have already executed
which means now, stored in n,
is whatever the user typed while is now the condition
so while n is less than 1 what do I do?
Well, if the user just did something but n is less than 1,
that means they did the wrong thing
which means I should go do it again and if they still screw
around and give me negative 3, let's do it again.
Negative 4, do it again.
Positive 6, okay, this condition is now false and so it breaks
out of this whole construct.
So now if I go ahead and compile, positive 1.c, oh,
same problem again, I need to link in CS50's library,
lcs50 but again, recall the trick from Wednesday,
make positive 1 because of the way we've figured the system
should automate that for us, even though its going
to dump some more detail on the screen,
I can now run positive1 enter,
negative 4 negative 3, negative 2.
that's because of the do while loop keeping in.
if I say something stupid like monkey,
then its also going to catch that.
[Laughter] you didn't see that one coming, so its also catching
that because one of the things cs50's library does is
if you call the get in function, we were persistent,
we spent some good amount of time thinking through how
to implement the process of getting an int from a user,
so we, in our library are actually doing some
error checking.
We can't, we're not checking, the function is called get int,
we're not checking if it positive or negative,
because the function is called get int.
its not called get positive int or get negative int,
we're just getting an int so you have to do some
of the error checking but we can at least detect
when someone types in something random like monkey or banana
or random characters at the keyboard,
but then if you finally type 6, thanks for the 6
and it finally detects that its actually a legitimate number.
Well, let's take a look at this second version here.
So this is different how?
Well it doesn't do anything fundamentally different
but thus far we've had some fairly small programs,
I can introduce other variables if I want,
so there's this notion of a Boolean variable
which we promise existed, its value can only be true.
All lower case or false, but realize that's only
because of cs50's library.
Now other languages, c plus plus, java have true
and false built in, c is pretty old, it didn't have true
or false so we've reimplemented those
and you declare a Boolean variable with bool,
the name you want to give to the thing,
and then assign it a value, and notice what I'm doing,
I actually don't care in this program what the user is giving
me, I'm calling get int but I'm completely disregarding the
return value except to check if its greater that 0.
I'm not storing it in any value, if it is in fact, positive,
I change the value of thankful to true, notice I didn't bother
with curly braces around the int because its just one line
of code, and now my Boolean expression has just changed
to be this.
Here. But a common mistake might be this, you know what?
I'm very used to seeing equals, meaning if something is equal
to something I write one equal sign.
Now I just change the program as follows.
I change it to a single equal sign, make positive 2,
let me go ahead and run positive 2, its warning me
because I'm actually being a little loose with my code
as it is, let me give it a negative 23, hm.
So why is this all of a sudden broke in for negative values?
What's going on in this line of code?
So I do this, then what happens?
I'm assigning that variable called thankful no matter what a
value of false so then think about order of operations,
the parenthesis are outside, just like high school math,
parenthesis outside means do what's inside first
so thankful gets assigned the value of false,
so what's the value of that whole expression now?
False so inside parenthesis now is while false,
so that immediately means you don't repeat that loop
because it's false and you only repeat loops when it's true
so you immediately break and therefore say,
thanks for the positive integer even though it wasn't a
positive integer.
So here too, another sort of common mistake,
it's a little annoying that the world chose equals equals
and just equals for different meanings
and in fact its really going to annoy you when we get the php
and not only is there equal and equal,
there's also equal equal equal, but there's no equal.
There's three of them and they all have different subtle
meanings but a common bug there, so let's take a look
at one final version of this.
So positive 3.c is a little more elegant, so I got a little fancy
or a little lazy depending on how you look at it,
what's the one key difference for the bottom of this program?
Yes, so this bang sign.
So bang has a meaning at my shell,
my blinking prompt, if I do bang or !
Then G that gives me the last command that started with G
but that's at your shell the blinking prompt.
This is now c code in that context of c, the !
Or bang inverts Boolean values.
If it's true, it becomes false, if it's false it becomes true.
So this bang variable name is actually a very common syntaxic
trick just because it looks kind of nice and clean
to say while something is not true,
you literally say not variable name and this inverts its value
and because again, I've chosen my variable names cleverly
but in a straightforward way.
The fact that I called it thankful means
that even though I'm now writing c code and not pseudo code
and not scratch, while not thankful is
in fact the translation of c into what I might say in English
so this too speaks to the niceness
of choosing the variables appropriately it's a little more
clear than like, while not x like while not thankful again,
is all the more clear.
So that's all kind of interesting
and you now have these building blocks,
you'll need all those building blocks for the problems set
but you can do, and we will do, later in the semester,
even more powerful things using sort
of more familiar tools in API.
So again, this is meant as a teaser for what's ahead,
because I realize that in the first week or so
of the course you just made some pretty sexy scratch projects,
loud and interactive and what not, and now we kind of have
to take a step back and have you implement change making
and these smaller little Mario puzzles
but we'll very quickly build back up to things more familiar
so this is a program so if you're looking for something
to do on a Friday night or procrastinates
and you've never played with Google earth, download it,
its free, and this is sort of the earth version of Google maps
that Google acquired from a company and frankly,
you can take little virtual vacations with this thing.
So if I type in 1 Oxford Street 02138 this is actually going
to whisk me away, if you've never seen this before
to the science centers address, notice that we're going to zoom
and zoom and zoom, it gets pretty uninteresting quickly,
but, aha.
There we go.
It gets a little spooky at some point, if you've not played
with this stuff and you can do some of this in Google maps
but you can do things
like change the elevation you're actually looking at,
this gets kind of trippy, its even cooler frankly,
when you do it on an iPhone these days
with the accelerometer, we can do things a little fancier,
this is going somewhere, don't worry, so Paris France,
we can head over there, frankly its just cool doing
this sometimes.
All right, so now I'm zoomed in on Paris France,
and just to show you what's really cool these days with say,
graphics, let me go ahead and zoom in, if I can find it
from thousands of feet above, the Eifel Tower which is there,
I'm going to click a little radio button here and I'm going
to keep zooming in, its really rather trippy, frankly.
All right, zoom in there, yeah come on,
my Mac does not quite keep
up with the computational needs here, so it's pretty darn cool,
so what's the relevance to CS50?
So toward the end of the semester,
one of the problem sets will actually be
to implement this thing generally known as a mashup
where you actually take some data set another data set
or some kind of graphical interface and do something
with them together, whether its Google news, Google maps
or Google earth, what's really nice is that companies
like Google provide what are called again,
application program interfaces
which means their code their libraries,
similar in spirit to these .h files.
You can use in your own programs whether you're writing something
in C or php or in Java script
and so the demo I thought I'd show you just to think
through a problem, [Laughter] we still have a ways
to go, amazingly.
Just to show you how we might think through this,
I whipped up this little demo here, so let me go ahead and go
into this directory, let me just one moment, so we end right
on time, so I'm going to go ahead to CS50 going
into our course's web server, which lives elsewhere,
I'm going to go into our lectures directory,
the source code and map and all this is available on line
and what I did in advance of today, was a couple of things.
so one, even though its not due for several hours a bunch
of folks have submitted the surveys at the end
of problem set 0, and provided their hometown addresses.
So all of this has been scrubbed, so we don't know who's
from where, but we have in this CSV file,
comma separated values file, a whole bunch of hometowns
from people in this course,
you might have seen yourself scroll past really fast.
So a reasonable question to ask, and a question
that you'll be able to answer at the end of the semester is,
what can I do that's interesting with this?
Well Google maps for instance, provides you with the ability,
you probably know, to embed a map into your own personal
or corporate web site, that's pretty easy
and using a language called Java Script can you grab data
like that from this text file and actually embed it
into your own map but that begs the question, how do you go
from one to the other?
Well one of the things we'll be able to do ultimately,
is take what's in excel file, so a CSV file,
its something you would generally open in excel,
but if you've got 200, 300 names and hometowns,
odds are you don't want to be doing copying
and paste all day long, it will certainly take forever
so there's these things called,
there's these things called scripts and in fact,
and I'll leave this available on line,
I wrote this little script.
It's not many lines of code and in fact even though it's written
in php notice some familiar idea.
There's a loop in php called for each and apparently iterating
over things called hometowns and long story short,
what I did with this little script was convert the excel
file the survey.csv into what's called xml or really kml
which is a mark up language for mapping programs
and the end result is that I'm able to do this.
If I go ahead to any old browser, go to CS50.net/lectures
and pull up our source code for today and go
into the map directory therein, what we will see is this.
So frankly, it took like 10 minutes of effort
after downloading the csv file from the survey program
that we're using, and none of you apparently live in Cambridge
and have submitted yet, but if we start zooming out,
each of these little bubbles represents CS50 students
and it's really neat.
Some of you are really from pretty far away
and this is only some of the students in this course,
so with that said, that's where we're going, you got PSet 1
in your hands as of tonight, see you on Monday.
?? ?? ?? ??