ProgrammingMethodology-Lecture18.pdf

(62 KB) Pobierz
Programming Methodology-Lecture18
Instructor (Mehran Sahami): So welcome back to yet another fun filled exciting day of
CS106A. I'm feeling a little bit more relaxed. Are you feeling a little bit more relaxed? A
few people are nodding. A few people are like, "Yeah, Ron, we took the midterm in your
class, but we have like six other classes to deal with. You can feel relaxed, but we don't."
But don't worry, things in this class are just – it's all downhill after this. It's just a good
time.
So a few quick announcements before we jump into things. One of which is that there's
four handouts, and if you didn't get the handouts – if they weren't there when you came
in, they're all in the back now. You can pick them up after class. So you don't need to
pick them up right now. You can pick them up after class.
The midterms will also be back after class. I'll show the performance of the whole class. I
was very pleased. It was just – it was a heartwarming kind of thing to just see so many
people doing so well. But you'll get them back after class. So you'll get them today.
You – one of the handouts also solutions for the midterm, and so you can – if you got
anything wrong you can compare your answers with the solutions. One thing to also keep
in mind is that the actual solutions we were looking for are shorter than the solutions that
I give you. The solutions that I give you have comments. For one of the problems I
actually gave you two different ways of doing it just so you see different approaches. But
you weren't expected to actually write that much code. All we wanted was sort of the
code without the comments, which is actually pretty slim if you consider how much code
there is there without comments. But you'll get those back after class.
And then as you hopefully know, assignment number four is due today, and assignment
number five, you're next handout – one of your handouts is going out. For assignment
number five you will do yet another game because it's just so much fun to do games. And
this time we're gonna be testing your abilities with – we're not testing your abilities.
That's an awful way of saying it. We're going to be watching your abilities in the use of
arrays flower in terms of designing another game. Okay.
So with that said, it's time to see how much pain we actually caused on you assignment
number four. Hopefully not too much pain, although, I did hear there was quite a few
people on [inaudible] on the wee hours of the night last night.
So anyone in the zero to two hour category? Was that actually someone back there? No.
Just joking – yeah, kind of funny. Two to four hours? All right. A few folks. Good to see.
Four to six hours? Oh, nice. Six to eight? Pretty large contingent there in the old six to
eight category. Eight to ten? Wow, also pretty large contingent in the eight to ten
category. Ten to 12? All right. We begin to see the drop off at ten to 12. Twelve to 14?
Few folks at 12 to 14. Fourteen to 16? Sixteen to 18? Eighteen plus? Taking a late day?
That's – yeah. Alrighty.
Once again – it happens every time. I don't make this up, right? You saw people raising
their hand. The world is just – it's amazingly normal. It really is. There are two things the
world is – it's linear, and it's normal, not necessarily at the same time. So again, ten hours,
and less life is good. Even if you're in the ten to 12, you're still in pretty good shape. I
didn't see actually too many people up in this range, which is good to see.
If you're in the 12 to 14, it's still not very bad, right? It's just some – there might have a
bug or some particular thing that happened that caused a little extra time. If there was
some conceptual thing that did not get cleared up as a result of actually doing the
assignment, that's the whole point, right? Some times you might run into a roadblock, but
the point is once you get through that roadblock, that concept is cleared up for you. That's
the real point of doing assignments as opposed to just reading the book.
Reading the book's easy, and a matter of fact, it's surprisingly easy to read code, and think
that you could write it, right? That's what most people do. They look at code, and they're
like, "Oh, I could've written that." Right? It's like Jackson Pollock painting. You look at
that, and you're like, "I could've painted that." Right? But there's a difference, right? One,
he painted it first, and that's why it's worth like $20 million, and two, trying to actually do
the same thing later on requires a different creativity on your part than just looking at
what someone else did. So it's important to not just read code, and understand it, but to be
able to write code, and understand it, which is the whole point of the assignments.
Alrighty.
So with that said, time for some midterms statistics. So here is the stats on the midterm,
and that's just a beautiful thing to see. Right? This little hump over here – I should've just
tacked on to this, right? Then we would've actually had like a little inverse Poisson
distribution that you would've said, "But, Marron, it's not normal." And in fact, you can
approximate a Poisson to the normal, so it is.
But the important thing is that in fact the class did very well. There were six perfect
scores on the exam, which is why I broke out the 90's as a separate category, which it just
warms the cockles of my heart. What the cockles are, I'm not sure, but it warms them.
And a lot of people who did very well, right? A lot of people in the 70 or higher category.
Nice to see a very large contingent in the class in the 80 plus category.
And so the stats – this is out of 90, right. So this in not percentages, but this is out of 90
points. The mean was a 73.2. Median was a 77, which percentage wise translates into
almost 85 percent. And then the standard deviation was 13.6 points. So if you're kind of
over in this range over here, good times, happy days, life is good.
If you're kind of in this range over here, you want to make sure conceptually things get
clarified. You want to look at your exam, and figure out whatever problems there were,
look at the solutions, get them clarified, get the issues clarified in your mind. If they're
not clear, talk to your section leader this week, talk to me, talk to the TA to get any
concepts cleared up.
If you're kind of in this range or lower, it's – to be honest, it's time to worry a little bit,
and you need to really –think of the midterm not as just, "Oh, my God, my grade is
ruined," because the point of having midterms, at least as far as I'm concerned, is not just
grades. It's great if everyone does well, and we just give more A's, and life is good. But
really it's a diagnostic for you. It's a chance for you to get a realistic calibration of how
you're doing relative to the rest of the class, relative to what we expect you to know.
And so if you find yourself from that calibration falling into a range that worries you a
little bit, that you shouldn't think of as, "Oh, it's just time to pull your hair out, and panic."
All that really is is an indication that you need to come clarify some concepts. And
maybe they're clear for you, and something bad just happened during the exam, like an
earthquake. Or maybe it's some conceptual issue that needs to be cleared up, and this is a
chance while we're still well before the final exam, and well before several other
assignments in the class to get that clarified for you. So think of it as a diagnostic. Okay.
So any questions about anything related to the midterm before we sort of push off into
our next great topic in computer science? But I was just very pleased. It just made me
happy. It made me think like you had the easy button with you, and you just saw the
exam, and you were like, [Easy Button] That was easy. Yeah.
It's just like – I got this thing – well, I got – I'll tell you the story. I bought this thing like a
year ago, and I was like, "Oh, this will be great for class." And then it sat on a counter for
a year. So now I'm happy that I can actually use it. And it is not particularly branded
because I cut that off just in case someone wants to sue. All right.
So with that said it's time to actually get into our next great topic in computer science. So
I'll just leave this up here if you want to write down these numbers for whatever reason.
But to wrap up a little bit about one of our last great topics, which was arrays, and
specifically multi-dimensional arrays. And I'll just call them multi-dim arrays. And
you've seen a couple examples of these right now, but I want to – already, right, in the
last couple classes you saw some examples of multidimensional arrays. But I want to
show you yet another example to show in a very nuts, and bolts kind of way how funky
things get when you have arrays of arrays, and you pass them around as parameters in a
program – just so you see yet another example to drive the point home.
So let's say I was gonna write a program to, oh, keep track of midterm, and final scores,
for example. And let's say the class was smaller than this. So I had at most 100 students
because I was teaching some class like basket weaving or something, and it's just small.
It's cuddly. We all hold hands. So I have some two-dimensional array that I want to have
because I want to have midterm scores, and final scores, but I want to keep them
separate. So I'm gonna have one array of midterm scores, and another array of final exam
scores.
But in some sense what I really have is a two-dimensional array that's two by the number
of scores that I have as a maximum. So let's say 100 is the maximum number of scores
I'm gonna have. So I'm gonna a two by 100 array, and I'll call this scores, and I'm just
gonna create this by basically saying new int. And you've seen the syntax before. Two –
and this is 100. And these would probably be constants that I would have in a program,
but just to save space, and save time, I'm not – you actually declare public static final int.,
and saw max size 100. So I'm just gonna put 100 in there.
And as we talked about a little bit before, some particular entry of this grid, right –
because this is just a two-dimensional grid – like score zero comma zero is just – this is
just a single int. basically. But then we talked about this funky thing where we said,
"Hey, you know what? A multi-dimensional array is actually an array of arrays." So a
two-dimensional array's an array of arrays. A three-dimensional array is an array of
arrays of arrays, right?
So we could actually think about, "Well, what happens if I only specify one of the
dimensions of the array?" So what do I get if I say, for example, scores sub zero, right?
That's not one entry, so it's not an integer. That's in fact a whole array, right? The way to
thing about this is I have two arrays that have size 100. All right. So we have a little
break in there. And so score sub zero is this array, and score sub one is this array, and
scores is kind of this array of arrays, right? It's really an array that has two elements. So
this is element No. 1 over here in the heavy chalk line of the array or element No. 0 – it's
the first element in here is element number one. Okay.
So this thing – the type of this thing is really an integer array. And that's the funky thing
to kind of think about. Okay. So score sub zero behaves in your program just like if you
had declared a one-dimensional array, which means you can pass score sub zero to a
function that expects a one-dimensional array, and that's perfectly fine. What it's doing is
passing this element of the scores array of arrays, which happens to be a whole array of
integers in its self. Okay. So any questions conceptually about that? All right.
So why don't we look at a little code that actually drives that point home. All right. So
we'll put this away. Bye-bye midterm scores. Don't save. And we'll come to our friend the
Eclipse, and we'll say, "Hey, Eclipse –" oh, we'll get to Roulette in just a moment. Don't
worry. It's all about gambling in this class. All right. So it's not really gambling. All right.
So I'll tell you we have a program that wants to keep track of test scores. So here what
I'm gonna do is I'm gonna have a program test scores, again, we're just gonna set the font
to be large. It's not a big deal. And I'm gonna ask the user for the number of scores. So
I'm gonna ask the user for number of scores, and whatever the user gives me is num.
scores. I'm going to create my scores array to be a new array of integers that's two by
however many scores you have.
And you should see this right now, and you should go, "But, Marron, you're missing the
declaration of scores, right? This creates a new array of arrays, but where's the
declaration of scores?" Ah, instance variable. So I want to show you an example of where
we have an instance variable that's an array of arrays. So here I declare the instance
variable. This just tells me that score's is going to be a two-dimensional array.
It is not yet created the two-dimensional array. It is just set – I'm gonna have this thing
called scores that gonna be a two-dimensional array, I still need to create it. So when my
program runs up here, I ask the user for the number of scores, and I actually create the
two-dimensional array. I don't need to declare scores because scores was declared as an
instance variable or an ivar, so it's already declared. I need to create all the space for the
all the elements of that array. Any questions about that? Hopefully that's clear. All right.
So first thing I'm gonna do is I'm gonna initialize my scores, and here – and its scores. All
this is gonna do is have two for loops that are gonna go through, and set all the scores in
the grid to be zero to begin with. Now, I told you before, when you declare an array –
when you create an array, the values of the array get initialized to be the default value for
that type. And the default value for integers is zero. So you might say, "But, Marron,
they're already zeros anyway, so why are you doing this?" I'm just doing this to kind of
exemplify the point. I could put in a one here, and just say, "Hey, everyone starts with a
one on the exam." Right? But we'll just say it's zero.
How do I actually think about this two-dimensional array? Well, first of all, I want to
think about the rows of the array, and then for every row it's got a certain number of
entries or essentially the number of columns in the grid. So how many rows are there in
the array? If I look at the two-dimensional grid scores, and I say scores dot length, and
scores is an array of arrays, it tells me what is the size of the first dimension, right? So if
scores is a two by 100 array, what's the size of the first dimension? It's two.
So scores dot length – the way you want to think about it is this is scores. How big is
scores? Scores actually is of size two. Each of the elements of scores of size 100, but if
each of the individual entries of scores – score sub zero, and score sub one – is an array.
So there's only two arrays inside scores. So scores dot length, back over here in my
program, will in this case have the value too. Okay. So that's gonna be my outer loop. My
outer loop's gonna say, "I want you to loop over however many rows you have in your
grid, " is the way to think about it.
For every row, how many entries do you have in that row? What I'm gonna do is I'm
gonna say, "Hey, all the rows have the same size, so I'm just gonna arbitrarily pick the
first row because I know there's always gonna be one row. If I knew there was always
going to be ten rows, I could say, "Score sub nine." But for programmer, they're just like,
"Why are you doing score sub nine?" So the general idiom – the way we using do it, is
just score sub zero because you always know that there is a first – always a first entry in
an array – or in an array of arrays.
So score sub zero, what is that? Score sub zero I just told you is a whole array of integers,
right? It's this thing right here. So its length is 100 because there's 100 integers in it. So in
this for loop we loop through 100 elements, and so all we're gonna do inside here is we're
gonna set scores I sub J. I's gonna very from zero to one because it's actually gonna count
from one to two, but not include two. And J's gonna vary from zero to 99. And so it's –
we're gonna set all the elements of the grid to be zero. Any questions about that?
Zgłoś jeśli naruszono regulamin