Code/Notes |
Sp |
# |
Utterance |
Hail.prb
|
T |
9 |
alright let's do an example. |
|
T |
10 |
If we start at 6 what comes next? |
|
S |
11 |
3 |
|
T |
12 |
good. next? |
|
S |
13 |
10 |
|
T |
14 |
after that? |
|
S |
15 |
5 |
|
S |
16 |
16 |
|
S |
17 |
4 |
|
S |
18 |
8 |
|
T |
19 |
good. Any need to keep going? |
|
S |
20 |
8 not 4 |
|
T |
21 |
no sweat. got it. |
|
S |
22 |
4 after 8 |
|
S |
23 |
that's it |
|
T |
24 |
why is it ok to stop there? |
|
S |
25 |
because 4 is one of the numbers in the ground state |
|
T |
26 |
right on. Using the counting rule you read aobut how many items were in that sequence? |
|
S |
27 |
7 |
|
T |
28 |
good. And what was the largest element? |
|
S |
29 |
16 |
|
T |
30 |
ok I think you've got it. I may refer back to this example later. |
|
T |
31 |
Let's start writing the program. What is the first thing it should do? |
|
S |
32 |
have the user pick a positive integer |
|
T |
33 |
(tile created) ask the user for a positive integer |
|
S |
35 |
(tile placed) |
|
T |
36 |
great. what next? |
|
S |
37 |
call a procedure with the steps to figure out the hailstone sequence |
|
T |
38 |
yeah we could do that but for the pseudo-code let's just focus on the steps we need to do. |
|
S |
39 |
find out if the number is odd or even |
|
T |
40 |
good. |
|
T |
41 |
we'll do that for sure but to be complete can you suggest a variable name for this user input? |
|
S |
42 |
InitialNumber |
|
T |
43 |
sounds good... actually I'm going to cut off "Initial" for you because we'll use it for more than just the first one. |
|
T |
44 |
(tile created) read user input into Number |
|
S |
45 |
ok |
|
S |
47 |
(tile placed) |
|
T |
48 |
now we'll check to see if it is odd or even. Which one would you like to handle first? |
|
S |
49 |
odd |
|
T |
50 |
(tile created) if Number is odd then |
|
S |
52 |
(tile placed) |
|
T |
53 |
what should we do to number if that turns out true? |
|
T |
54 |
(tutor comment) i will make a lot of connections implicit in the tiles this time. |
|
S |
55 |
Number * 3 plus 1 |
|
T |
56 |
good. We'll just reuse Number for the destination. |
|
T |
57 |
(tile created) Number is assigned Number * 3 plus 1 |
|
S |
59 |
(tile placed) |
|
T |
60 |
remember to indent the body of an if. |
|
S |
62 |
(tile placed) |
|
T |
63 |
nice. |
|
T |
64 |
ok if Number isn't odd it must be what? |
|
S |
65 |
even |
|
T |
66 |
(tile created) else |
|
S |
68 |
(tile placed) |
|
T |
69 |
and then for that case? |
|
S |
70 |
Number div 2 |
|
T |
71 |
(tile created) Number is assigned Number div 2 |
|
S |
73 |
(tile placed) |
|
T |
74 |
looking good so far. |
|
T |
75 |
what needs to be done next? |
|
S |
76 |
we need a loop |
|
T |
77 |
good idea. Let's work on the condition of the loop first. |
|
T |
78 |
we need to loop while what is true? |
|
S |
79 |
while Number <>4 2 or 1 |
|
T |
80 |
good! In words we can say while it is not in the ground state. |
|
T |
81 |
(tile created) while Number is not in the ground state do |
|
S |
83 |
(tile placed) |
|
T |
84 |
(HINT in Pascal a set will be very useful here) |
|
T |
85 |
ok... now let me ask about where you put that tile. |
|
T |
86 |
Do you think you'll want to check Number to see if it is odd or even again? |
|
S |
87 |
yes |
|
T |
89 |
good. Sequences can be pretty long. |
|
S |
90 |
(tile placed) |
|
T |
91 |
So... you might as well reuse the tiles that do that check. |
|
T |
92 |
Which means you'll want to move that while tile up so that the if-then-else is part of the loop body. |
|
S |
94 |
(tile placed) |
|
T |
114 |
actually... want all of those indented to show they are in the while. Here I'll select them you drag them over. |
|
S |
116 |
oh |
|
S |
118 |
(tile placed) |
|
T |
122 |
perfect. |
|
T |
123 |
click in an open area to deselect everything. |
|
T |
126 |
sorry. open on the drawing area. |
|
T |
127 |
ok usually with a loop we need to mark the end. so here's that tile. |
|
T |
128 |
(tile created) end while |
|
S |
130 |
(tile placed) |
|
T |
131 |
very good. |
|
S |
132 |
can you just put end instead of end while |
|
T |
133 |
sure! Pseudo-code is up to the person writing it. The only reason I put "while" there is to make it obvious. |
|
S |
134 |
ok |
|
T |
135 |
sometimes there will be lots of ends floating around. |
|
T |
136 |
now we'll pause and take a snapshot. This program will generate an entire series. Not too bad eh? |
|
T |
137 |
could you type that into the description box and click on the snapshot button? |
|
S |
138 |
that cool |
|
S |
139 |
|
|
T |
143 |
looks great! |
|
T |
144 |
now we'll keep working... what should we tackle next? |
|
S |
145 |
when we reach the ground state we need to count how many numbers are in the sequence |
|
T |
146 |
good idea. So we'll need a variable to keep the count. |
|
T |
147 |
what would you like to call it? |
|
S |
148 |
totalnumbers |
|
T |
149 |
total sort of implies a sum. anything else come to mind? |
|
S |
150 |
Items? |
|
T |
151 |
not bad... how about itemCount? |
|
S |
152 |
sure |
|
T |
153 |
can you tell me how we can use itemCount to count up the items in a sequence? |
|
S |
154 |
I'm not sure |
|
S |
155 |
make a procedure to do it |
|
T |
156 |
so tell me when you said there were 7 items in our example earlier how did you know? |
|
S |
157 |
I just counted them by hand |
|
T |
158 |
unfortunately our program cannot do it that way at least not in any easy way. |
|
T |
159 |
so can you think of a different way to count? A way that doesn't require you to look back over your work? |
|
T |
160 |
It's ok... let's go back to the example. |
|
T |
162 |
If we start at 6 how many items so far? |
|
S |
163 |
1 |
|
T |
164 |
good. you said 3 is next. How many items now? |
|
S |
165 |
2 |
|
T |
166 |
after that is 10. takes us up to 3. |
|
T |
167 |
and so on. |
|
T |
168 |
so rather than count 'em up when we're done we keep track of it as we go along. |
|
S |
169 |
ohhh! |
|
S |
170 |
do we need to make another variable then? |
|
T |
171 |
we can just use itemCount. What should we initialize it to? |
|
S |
172 |
Number |
|
T |
173 |
whoa... so Number in our example was 6. Do you want to start off saying there are 6 items? |
|
S |
174 |
No I mean if the user enters 6 then ItemCount will be 1 |
|
T |
175 |
got it. Good. |
|
T |
176 |
(tile created) initialize itemCount to 1 |
|
T |
178 |
you'll need to move some tiles around. |
|
S |
179 |
(tile placed) |
|
T |
215 |
what do we do next? |
|
S |
216 |
what does initialize mean? |
|
S |
217 |
in Pascal |
|
S |
218 |
is that a reserved word |
|
T |
219 |
it just means to assign a value to a variable. no it's just a word. |
|
T |
220 |
in Pascal (I'll cave this once for you) you'd just just say itemCount = 1; |
|
S |
221 |
got it |
|
T |
223 |
can you tell me what we need to do to itemCount? |
|
S |
224 |
after we have ItemCount we need to find the largest number in the sequence |
|
T |
225 |
eventually yes. But we've only intialized itemCount. There is more work to be done. |
|
T |
226 |
how is it ever going to go up to 2? 3? 4? etc. as we build the sequence? |
|
S |
227 |
we have to assign it a new value every time |
|
T |
228 |
good. what new value? |
|
T |
229 |
How many new elements do we see each time through the loop? |
|
S |
230 |
1 |
|
T |
231 |
so what should we do to itemCount? |
|
S |
232 |
add 1 evry time |
|
T |
233 |
absolutely! |
|
T |
234 |
In programming the word that means "add one" is increment. |
|
T |
235 |
(tile created) increment itemCount |
|
S |
237 |
(tile placed) |
|
T |
238 |
think hard about where you want that tile. |
|
T |
239 |
remember "each time through". |
|
S |
241 |
(tile placed) |
|
T |
251 |
that looks almost perfect. Right now it sort of looks like the increment is part of the else. |
|
T |
252 |
Could you move it over one line to the left? |
|
S |
254 |
(tile placed) |
|
T |
255 |
good. then the end tile should line up with the while. |
|
S |
257 |
(tile placed) |
|
S |
258 |
sorry |
|
T |
259 |
not at all... you're doing a great job. |
|
T |
260 |
ok we've initialized the count added 1 each time through and so what do we need to do for the user after the loop is over? |
|
S |
261 |
print out ItemCount |
|
T |
262 |
(tile created) print out ItemCount |
|
S |
264 |
(tile placed) |
|
T |
265 |
looking good. |
|
T |
266 |
ok let's pause to take a snapshot. |
|
T |
267 |
We just added a little code to handle counting. |
|
T |
272 |
In the description box could you type something about counting and do the snapshot? |
|
S |
273 |
how about "find Item Count"? |
|
S |
274 |
"find total items in sequence |
|
T |
275 |
pretty good... even more general would be good too like "count the items in a sequence". |
|
T |
279 |
I need to take a quick peek.... hold on. |
|
T |
284 |
great. |
|
T |
285 |
Ok you mentioned it earlier. What is left to do? |
|
S |
286 |
find largest number in sequence |
|
T |
287 |
yep. So like the count we just can't scan all the items when we're done and magically pick out the largest. |
|
T |
288 |
we'll need to figure it out. |
|
T |
289 |
any ideas here? |
|
S |
290 |
we need another variable |
|
T |
291 |
absolutely. any good names come to mind? |
|
S |
292 |
Maxvalue |
|
T |
293 |
cool. What should we initialize that to? |
|
S |
294 |
Number |
|
T |
295 |
awesome. In a way we'll assume the first value is the largest and then go from there. |
|
T |
296 |
(tile created) initialize MaxValue to Number |
|
T |
298 |
you'll need to move some tiles around to fit it in. |
|
S |
299 |
(tile placed) |
|
T |
336 |
looks very good to me. |
|
T |
337 |
now when will MaxValue change? |
|
T |
338 |
if at all? |
|
S |
339 |
when Number is greater than the first value entered |
|
T |
340 |
well... perhaps true sometimes. |
|
T |
341 |
MaxValue will hold the largest we've seen so far. |
|
S |
342 |
that |
|
T |
343 |
that starts out as the starting number. |
|
S |
344 |
that's what I mean |
|
S |
345 |
if we have 6 Maxvalue won't change until "Number" becomes greater than 6 |
|
T |
346 |
right. So let's do that example. 6 is first it is also the max. |
|
T |
347 |
what's after 6? |
|
S |
348 |
3 |
|
T |
349 |
so 6 stays as the largest. |
|
T |
350 |
after 3? |
|
S |
351 |
10 |
|
T |
352 |
yep. is 6 still the largest? |
|
S |
353 |
no |
|
T |
354 |
good. we update the largest to be 10. |
|
T |
355 |
ok after 10? |
|
S |
356 |
5 |
|
T |
357 |
yep. 10 holds on. |
|
T |
358 |
next? |
|
S |
359 |
16 |
|
T |
360 |
ok new largest? |
|
S |
361 |
that is the max |
|
T |
362 |
yep. Now did you compare 16 with 10? or 16 with 6? to know that? |
|
S |
363 |
compare 16 with 10 |
|
T |
364 |
absolutely. |
|
T |
365 |
So inside our loop we'll need to compare Number with MaxValue. but how? |
|
S |
366 |
relational operators |
|
T |
367 |
which one? and how? |
|
S |
368 |
if Number > MaxValue then MaxValue = Number |
|
T |
369 |
good!!! |
|
T |
370 |
(tile created) if Number > MaxValue then |
|
S |
372 |
(tile placed) |
|
T |
398 |
hold on... more work to do in there. |
|
S |
401 |
(tile placed) |
|
T |
403 |
(tile created) MaxValue = Number |
|
S |
405 |
(tile placed) |
|
S |
418 |
if Number < MaxValue then MaxValue = MaxValue |
|
T |
419 |
I see where you're coming from but since MaxValue will already hold the largest there is no need. |
|
S |
420 |
oh |
|
T |
421 |
Let me arrange your tiles just a bit. We want to make it clear that the max comparison is unrelated to the even/odd part. |
|
T |
423 |
(tile placed) |
|
T |
434 |
look ok? |
|
S |
435 |
looks good |
|
T |
436 |
one last thing to do then. We had to do it for count as well. Do you see it? |
|
S |
437 |
we need to print out MaxValue |
|
T |
438 |
(tile created) print out MaxValue |
|
S |
440 |
(tile placed) |
|
S |
442 |
don't we need to have something if the User enters one of the numbers in the ground state as the first number |
|
T |
443 |
Well suppose they do. What if they type 4 into Number at the top. |
|
T |
444 |
What happens in the program? |
|
S |
445 |
it skips the loop |
|
T |
446 |
good. |
|
T |
447 |
printing out 1 as the count and 4 as the max. |
|
T |
448 |
which is correct. |
|
S |
449 |
ok |
|
T |
450 |
that's it! This code now finds the maximum in a sequence. Go ahead and do the snapshot |