Monday, November 25, 2013

Simpleton Geek Teaches Computer Programming Book 2 Ch 8B


8.2 Debugging, TCHX,TCHY
“Alright, class, listen up!” The teacher was dumping a huge stack of paper on the desk. “Due to your extremely low performance last time, I decided to lower my expectation, yet again.” He was scanning the class angrily. “You know what that means, don't you?”
The whole class trembled with fear. There can be only one meaning: Terminator on the prowl. Their foreheads shall be stamped with 'FAIL'. It's not a question of if. It's a question of how many.
“So, I'm going to set my expectation to the lowest possible level. Failures to do this will mean being whacked on the head with this stamp!” He holds up a stamped marked with the words 'FAIL' on it. Except this one happened to be twice the usual size. Apparently the teacher has upgraded his stamper over the weekend. Fear descended upon the classroom.
“So, distribute this program. What I want you to do is simple: Copy the program exactly! Then tell me what's wrong with the program. The first one to give me the right answer will get 100 points. The rest will get zero. That's normal.” said the teacher. “However, I must tell you that you have to copy the program EXACTLY!” His eagle stare was really scary. “The ones who give me the wrong answer will get stamped on the forehead!”
The teacher smiled. “So get going! Remember, only one of you will get 100 points. The rest of you will get zero!”
All the students are busy copying the program. There are 3 pages of long, dense code. Definitely very hard to copy correctly. Written on the top of the page, is the task the the students must do: “There is exactly one thing wrong with this program. What is it?”
All the student furiously copied the program, trying to be the first to figure out what's wrong with the code. Somewhere by the bottom half of the first page, the students were shocked to see commands not covered in class. On the top of the second page, the students are surprised even more that some lines did not look like a normal structure. But when they got to the third page, they're in the shock of their life. So many nested parenthesis and digits! They must really, really be vigilant there! One missed digit, and it's head whacking time!
“Teacher! I got it! I found the mistake!” said Nancy.
“Great. What is it?” said the teacher.
“There's a syntax error on line 23.”
“Wrong!” said the teacher as he stamped 'FAIL' on her forehead.
“But teacher! It's true! See here? It says there 'Syntax Error'!” said Nancy.
The teacher stamped her forehead the second time. “Obviously, you need to check the code well, because that's not the mistake I'm looking for!” said the teacher. “Check your code.”
Sure enough, Nancy mistyped the command. Once pointed out by the teacher, it was easy enough fix.
“The real error lies on line 27!” said Nancy.
That earned her another stamp on the forehead.
“You need to fix that one as well. 3 stamps in one minute? Gya ha ha ha!” No doubt about it: Terminator on the prowl. “Yes, you found the mistake?” he asked another student who was raising his hand.
“Uh, let me check real quick. Nope. False error. I mistyped the code.” said the student.
“Good thing you caught that. Would have been bad if you didn't.” The student gulped.
“Teacher, I found it! Error on line 35!” said a student.
The teacher bounded quickly to the student. “Show me.” he said.
The student proudly showed the teacher the error message. That earned him a stamp on the forehead. “Show me the code next.” The student coiled in fear. Sure enough once the code is shown, the teacher pointed out the student's mistake. “You forgot to put enclosing quotation mark here.” The student almost cried in shame and embarassment.
“Teacher, I found the mistake! Syntax error on line 157!”
Another stamped forehead. “How many times do I have to tell you? Comparison takes 2 equal signs! Assignment only takes one. Whenever you do comparison, you need to put down 2 equal signs, not just one.”
“Teacher, I found the mistake!” said a student. The teacher was homing in with glaring eyes. “But, I better double check before I show it to you.” the student amended hastily.
“Sure.” the teacher wasn't pleased at all. “Just don't bother me until you're ready. Got it?”
“Yes, teacher. Sorry.”
The students are checking and double checking the code. Finally, a student raised his hand. “Teacher, I have it here!” said a student. The teacher, who by this time was snoozing, with his feet on his desk, woke up and walked to the student in question.
“So? What's the mistake?” asked the teacher.
“The program prints out garbage!” said the student. That earned him a stamped forehead. The teacher then went back to sleep at his desk.
“The program actually runs?” asked another student.
“Sure. You don't think I'll be giving you a program with a syntax error, do you? That would have been too easy! Gya ha ha ha!” said the teacher. All the students wailed in frustration! “Tell you what. Just work on it, and when the class ends, just show me what you got.”
It was close to the end of the class. “Teacher, will you check this out?” asked Peter.
“Sure.” the teacher went to Peter to see his program running. The display of shows the screen slowly filled with letters. It stops running when the screen showing “HELLO” in big letters. “So, what's the question?” asked the teacher.
“I can't find the mistake.” said Peter. All the student paused in their work. Of course, there is a mistake! That's the assignment!
“So, what you're saying then is, the program has no mistake?” asked the teacher.
Peter gulped in fear. He thought for a while. For a long while. Finally, he said, “Yes. I guess so.” he resigned to be stamped on the forehead. All the other students held their breath, waiting for it.
“Well, then I guess I need to give you 100 points, and the rest of the class got zero.” The teacher smiled.
“Huh?” said Peter. “Doesn't it say here there is a mistake?”
“Sure does.”
“But if the mistake isn't in the code...” Peter was thinking about it.
“Where could it be?” prodded the teacher.
“It can only be in the statement that 'there is a mistake' because there really isn't.” said Peter. The teacher just laughed. All the rest of the students threw up their hands in frustration.
“Teacher! What is the point of having us do this if there's no mistake?” asked Nancy, the most-stamped girl.
“The point is, you guys are all careless! You need to pay attention to details! You cannot skip a variable, a letter, or even a space! All the symbols must be accurate! Do you know what happens when you missed a mathematical sign?” asked the teacher.
“The program gives reverse answer?” said a student.
“In case of NASA, the rocket flipped over and there goes hundreds of millions of dollars in a flash!” said the teacher.
The students just don't know what to say.
“In another instance, the airplane in question was flipped upside-down as it crossed the equator!” The students were all silent. “Can you imagine what happens when such program is running a nuclear reactor?” The student shuddered. They don't need to be told the grave consequences that may arise from such scenarios.
“But teacher! How can we do it?” asked the students. “It's too difficult!”
“So, Peter, how do you do it? I assume you have a secret technique for counting those parenthesis.” asked the teacher.
“I counted the parenthesis. Add one for left parenthesis, and subtract one for right parenthesis. If I end the line, and it's not zero, I know I made a mistake somewhere.” said Peter.
“See? Easy as counting.” said the teacher. “How about the digits?” Copying numerous digits accurately is very difficult.
“I look for the most common digits in the sequence, then I counted the number of digits in-between that digit. Finally, I memorize the sequence 5 digits at a time. If I arrive at the digit separator and still have fingers left, I know I skipped some digit. If I ran out, then I know I double counted some.”
“And there you go. That's how you do it. By the way, there is a way to memorize long sequence of digits. Using mnemonic.”
“Mnemonic?” The students were unfamiliar with the technique.
“Do you know the song 'This old man?' That's a sample of mnemonic. 1-wand. 2-shoe. 3-knee. 4-floor. So for a sequence 33241, you can create a sentence 'Double knees knocking the a shoe off to the floor, to be fetch with a wand.' When you say the sentence, you imagine the action. Then, when you repeat the sentence, you'll write '33' for 'double knees', followed by '2' for 'shoe', followed by '4' for 'floor', followed by '1' for 'wand'. Very effective technique.
“There are many different techniques. Benjamin Franklin designed one, where consonants stands for digits. All that's left is to choose words with the right consonants to memorize the numbers. With these kind of techniques, it's easy to memorize long sequences of digits.
“Of course, you need to memorize the key, and then you need to practice so that you can come up with the words effortlessly. But once you do, it's easy to memorize numbers.”
“So, teacher, that's how you memorize numbers?” asked a student.
“Not really. I developed a system where I use arithmetic operations. For that example 33241, I would memorize 'Two 3s, which is 2. 2 when squared is 4. 4-3, which is doubled in the beginning is 1. Something like that.”
“That's really a long way to memorize digits.”
“Sure, but you know, with practice, it'll take only 2-3 seconds. And I can memorize up to 9 digits, easy.”
“Really? Can you really do that?” asked the student.
The teacher shrugged. “That's the number of digits I have to memorize in Big Brain Academy. Sure.”
“Did I mention I scored rather high on Big Brain Academy?” the teacher asked. I forget exactly how high, but definitely over 2200 points. Of course, that's over a period of 10 weeks. You should try it sometimes. Big improvement for the first 6 weeks, and trickling improvement thereafter.”
The students were speechless. They have nothing to say.
“Anyway, we still have a few minutes, so let's write a program real quick. You remember TCHX and TCHY? Write this program:”
CLS:PNLTYPE “OFF”
?
?”A”*8;”B”*8;”C*8”;”D”*8
@LOOP
VSYNC 1
TX=TCHX:TY=TCHY:TT=TCHTIME
TX=FLOOR(TX/8):TY=FLOOR(TY/8)
IF TT==0 THEN P=0
IF TT==1 THEN P=CHKCHR(TX,TY)
LOCATE TX,TY:?CHR$(P);
GOTO @LOOP
“Now, tell me what you see.” asked the teacher.
“The bottom screen went blank and when we put the stylus on the position where the letters are, we get to see the letters repeated.”
“Correct. PNLTYPE “OFF” means to turn off the keyboard. Now, we're going to put some characters on the bottom screen. Add this line just after the line LOCATE:PRINT”
PNLSTR TX,TY,CHR$(P)
“Teacher! Now there's text on the bottom screen, just like the one on top!”
“That's because we echo print the line on the bottom screen. However, you can tell that these two lines are identical, except one goes to the top screen, and the other goes to the bottom screen. That's how you print on the bottom screen.”
LOCATE TX,TY:?CHR$(P);
PNLSTR TX,TY,CHR$(P)
“How do you read the character on the bottom screen?” asked Peter.
“There's no way to do that. It's not supported by the language. You need to keep track of that yourself. We'll do more of these things tomorrow. For now, though, you have a rough draft for a simple ASCII painter program.”
The students played with it some more. They tried to understand the program but there's nothing difficult about it. There are only two different commands: PNLTYPE and PNLSTR.
“Homework will be to figure out how to clear the bottom screen. Knowing that there isn't PNLCLS command available to clear the bottom screen. That's right. You have to do it yourself!” said the teacher.
“More homework. You want to copy these until you get 100% accuracy. I won't be scoring these, by the way.” The teacher began to distribute papers filled with program listings. There are various commands written down on paper, and they look random.
“What kind of program is this?” asked a student.
“It's not a program. Just some random lines. It won't run.”
“Then why are we doing this?”
“You need to familiarize yourself with the different commands. Practice these to avoid syntax error! You certainly don't want to forget how to spell certain commands. Also, it's good to introduce you to commands you may otherwise will miss. Remember, practice makes perfect. Gya ha ha ha!”

No comments:

Post a Comment