Tuesday, November 26, 2013

Simpleton Geek Teaches Computer Programming Book 2 Ch 9D


9.4 The first significant program
“Well,” the teacher said. “It's good that you have it. Too bad that you didn't get it early enough to get points for it. What took you so long, anyway?”
Peter was in the teacher's office, reviewing the day's event as usual. Though he expected some other students to show up, nobody else showed up.
“Well,” Peter said carefully, “I guess it's the fact that parsing is such a difficult subject that I gave up immediately. It's not until I see that there really is no parsing involved that I understood the difficulty of the problem.”
“Which is?”
“Pulling one character off a string at a time, until the string becomes empty.”
“Is that a hard problem?”
“Not really.” Peter said, “I mean, it's not that easy. I'm still grasping the concept of string manipulation. But once I realize that you took a detour in explaining string operation, I knew that the knowledge is necessary to solve this problem, and I did some trial and error programming to try to understand it.”
“Ah, that's why it took you a long time. Had you done your homework properly and did the trial and error at home, you would have made it.”
“It's possible that it's true.” Peter acknowledged the possibility.
“You see, when you are faced with insurmountable problem, the first thing you did was to seek guidance. In this case, me, your teacher.”
“There's nothing wrong with that. Isn't that what school is all about?” asked Peter.
“Academia, maybe, but remember, I'm a professional. What would a professional do in such instances? Give up? Go back to school?”
“I guess not.”
“Then what would you do if you're a professional?”
Peter thought about it for awhile. “I guess, I'll be doing what I just did today.”
“Which was?”
“Just do my best in understanding the problem. Try as many solution as possible and see if I can solve the problem.”
“In fact, that is what professionals would do. And what everybody should do. The thing is, this is a school. Therefore, you know that you CAN solve the problem. The only question is how.
“Unfortunately, in the real world, that's not necessarily true. There are many problems in the real world that has no guarantee of having a solution at all. It may be one of those impossible problem. Do you understand?”
“I guess so.” said Peter. “I guess in that instance, it's okay for you not to solve the problem.”
“Well, there is a standard that we all have to adhere to. It's not okay to say that the problem is unsolvable. You have to provide PROOF that the problem is unsolvable. Otherwise, everybody can say that they worked on the problem. No standard.
“The most important thing,” the teacher continued, “is not that you actually solve the problem. But that you do enough work to see how close you can get to solve the problem. In the end, either you prove that the problem is an impossible problem, or you solve the problem.”
Peter thought for a while. “I guess next time I ran into a impossible problem, I have to show that it is impossible.”
“That's all I ask.” said the teacher.

Simpleton Geek Teaches Computer Programming Book 2 Ch 9C


9.3 Strings
“Alright, everybody! Show me what you have!”. With that opening statement, the class began. The teacher have them filled this table:
L0 D C D C
L1 D D C C
A A A=AopB A A=AopB
B B=(B*10)+C B C B
C D 0 D 0
OP OP op OP op
What that table means is that L0 and L1 denote the type of commands that is entered. D for digits and C for aritmetic operation. L0 is the last keypress, while L1 is the previous keypress (what was previously L0). A,B,C stands for variables, where A and B are variables for calculation and C is the digit being entered. OP is the variable holding the aritmetic operator, where 'op' lowercase is the actual operator.
“Now, that you have learned string operation, L0 and L1 are a piece of cake!” said the teacher. “Here is the modified main program. You see how simple this can be.”
REM CALCULATOR
CLS:CLEAR
DIM OP$[5]
@INIT
A=0:B=0:C=0:OP=0
OP$[0]=”NOP”:OP$[1]=”ADD”
OP$[2]=”SUB”:OP$[3]=”MUL”
OP$[4]=”DIV”
L0=0:L1=0

@MAIN
VSYNC 1:CLS
?”A= “;A:?”B= “;B:?”C= “;C
?”OP= “;OP$[OP]
?”L0= “;L0”?”L1= “;L1
@MAIN1
CH$=INKEY$:IF CH$==”” GOTO @MAIN1
IF CH$==”C” GOTO @INIT
IF CH$==”E” THEN B=0:GOTO @MAIN
L1=L0:L0=0:'0=D,1=C
IF INSTR(“0123456789”,CH$)<0 THEN L0=1
ON (L1*2+L0) GOTO @ADD,@ADC,@ACD,@ACC
@ADD
GOTO @MAIN
@ADC
GOTO @MAIN
@ACD
GOTO @MAIN
@ACC
GOTO @MAIN
“And there you go. All you have to do is fill in the blanks. Now that you have all the table set up, it's easy! Of course, if you fill in the table wrong, then you will have trouble doing it. Do you understand now why I'm not grading your homework? I'd rather see you suffer all the bugs you yourself created!
“And then I will see you try to overcome the bugs. The way you approach your debugging will reveal your character. If you blame yourself, then you'll be alright. If you blame the tool, then you will never make it! Gya ha ha ha!”
@ADD
C=INSTR(“0123456789”,CH$)
B=(B*10)+C
GOTO @MAIN
@ADC
IF OP==0 THEN A=B
IF OP==1 THEN A=A+B
IF OP==2 THEN A=A-B
IF OP==3 THEN A=A*B
IF OP==4 THEN A=A/B
C=0
IF INSTR(“=+-*/”,CH$)>0 THEN OP=INSTR(“=+-*/”,CH$)
GOTO @MAIN
@ACD
C=INSTR(“0123456789”,CH$)
B=C
GOTO @MAIN
@ACC
GOTO @ADC
GOTO @MAIN
Filling the routines then becomes a simple exercise. The students need to be careful regarding some order or execution, but otherwise, it's not a problem. The problem, of course, is in design, and with bad design, comes bad program. Peter, the smart one in the class, has actually programmed in the routines just to be sure what he has actually works. In fact, that's the best way to do it. The other students, being beginners, never thought to do it that way. They still think that coding remains the most difficult phase of computer programming.
The whole process of filling in data takes less than 10 minutes. Testing, of course, takes longer. One by one, however, the students finally managed to get their program going. There are two special commands involved: All Clear (AC) and Clear Entry (CE). These corresponded with character 'C' and 'E' respectively. If you see the code, they are very simple. 'C' will simply shuffle the program execution to @INIT function, whereas 'E' will simply reset B to 0.
“Alright.” the teacher said. “Now that everybody got it, the next step is to create a graphical calculator. Now, don't worry about it because it doesn't involve anything you haven't done. In fact, if you remember your piano playing program, then it's the same exact thing. The only thing different is the key or button arrangement. So, what I want you to do now is to draw some character text on screen and shape that as calculators. So, get out your journal and start drawing!
“Remember, program design starts on paper! Those of you who tries to shortcut the step will find out that the whole process will take 2 to 3 times longer. If you don't believe me, just try it! Gya ha ha ha!”
The students dutifully drew the design on their journal. A 4 by 4 design emerged. In the end, there was no place to put the equal sign command, and the function is placed on the display instead. The final code is listed in Appendix G.
And so it goes. The students finally managed to draw a rough draft of the calculator, and it did start to look like a real calculator. Certainly there are some features that are missing, and some of the calculation behavior isn't quite right, but these are just little nits. The fact is, even beginning students are perfectly capable of doing a calculator program that is not dependent upon INPUT commands.
“Right, guys. Now for homework, I want you to think very carefully about how strings work because I will take the character patterns away from you. You will have to use character arrays for your on-screen keyboard input.”
The students all moaned their displeasure.
The teacher banged on their desk. “What's this? You're all complaining on such a little task? Well, well, why don't I add this to your homework, then: Memory button. Remember, you have 4 different operations on it. Memory Add, Memory Clear, Memory Reset, and Memory Substract.”
The students regretted their outburst already, but the teacher isn't finished yet. “Come to think of it, that's still too easy. So, not only you will have to come up with 8 memory banks, but also a way to read a script so you can do the operation automatically.”
“Teacher, what do you mean a 'script'?” asked a student.
“What I mean is that you have automatic input. Coming from a string. You need to parse the string and automatically give the answer.”
Peter raised his hand. “Parse the string? Isn't parsing a difficult thing to do? As in Graduate Student level?”
The teacher waved his hand, dismissing Peter's concern. “Nah. I learned it in College Senior level. So, should be fine.”
All the students looked at one another. College Level? Yikes! What kind of trouble have their teacher got them into? They couldn't believe it!
“Will this be graded?” asked Peter. There is hope yet at salvation.
“Of course. If you managed to do this, you will be amply rewarded.” The teacher nodded his head. “Of course, if you fail miserably, then you will be severely punished, considering that it will have the highest scoring criteria. So, good luck on working on that problem. Gya ha ha ha!” The bell rang, and there's nothing more to say on the subject.
On the next day, the students compared their notes. The first criteria turns out to be easy. Instead of using PRINT and CHKCHR, the solution is simply use a string array instead of PRINT for the row, and use MID$ for the column. That's it. It's very trivial to do.
The second solution is a bit harder. However, the AC/CE button provided a hint on the solution. It's a simple matter to just add more command to it. The most important function is READ/WRITE command equivalent. What about this 8 bank memory? Another array solves the problem. Simply by setting aside 8 characters for each bank, the memory bank problem is trivially solved. What remains are the task of Memory Add, Sub, Read, and Clear. An extension of AC/CE buttons solved that one also.
Of course, the students only had so much time in a day, and they were unable to solve the last problem. Parsing! A whole book can, and have been, written on the subject. In the end, they gave up and on the next day, they entered the class with fear, just waiting for the hammer to fall.
There was nothing they could do. How can mere high school students compete with University Level students? How can they, as beginner, do what 4 year college students are expected to do? Impossible!
Of course, this doesn't sit well with the teacher. “What do you mean you have no idea what to do? Well, well, it's lucky that I have my favorite stamp with me, today.” The students coiled in terror!
“So everybody got the first problem? Of course, it only scored 5 points. That's so easy. All you have to do is create a string array, and then check against it. Like this:”
DIM SC$[25]
@DIS
CLS:PNLTYPE “OFF”
SC$[0]=”====================”
SC$[1]=SC$[0]:SC$[2]=SC$[0]
SC$[3]=”111112222233333+++++”
SC$[4]=SC$[3]:SC$[5]=SC$[3]:SC$[6]=3:SC$[7]=3
SC$[8]=”444445555566666-----”
and so on...
'Checking
CH$=MID$(SC$[TCHY/8],TCHX/8,1)
For memory bank problem, simply set aside a memory array. Then set some keys to select them. “C” and “E” are taken, but “a”-”h” are not. As for Memory key operations, “M”-”P” are available. This simply lends itself to this implementation:
DIM MM[8]:'MEMORY BANK STORAGE
MI=0:'MEMORY BANK INDEX

IF INSTR('abcdefgh',CH$)>=0 THEN MI=INSTR('abcdefgh',CH$):GOTO MAIN
IF CH$==”M” THEN MM[MI]=MM[MI]+B:GOTO @MAIN
IF CH$==”N” THEN MM[MI]=MM[MI]-B:GOTO @MAIN
IF CH$==”M” THEN MM[MI]=0:GOTO @MAIN
IF CH$==”M” THEN B=MM[MI]:GOTO @MAIN
IF CH$==”C” GOTO @INIT
“Does everybody got that? They are all so easy, if you cannot do it, you should be ashamed of yourself! Well, now that you've got 15 points out of 100, ready to get the other 85 points?” the teacher asked. Of course, this is where none of the students have any solution whatsoever. The teacher gleefully put 15 points for everybody. “What a bunch of losers! Gya ha ha ha!”
All the students are just so frustrated. They simply do not know what to do. Instead of resuming teaching, however, the teacher simply went back to his desk, put his feet up, and went to sleep. Snoozing. At first, the student were patiently waiting for the teacher to wake up. After a while, it's clear that the students are left to fend for themselves.
None of the students know what to do. Some started doodling. Others decided to take a snooze as well. Only Peter was alert. He knew that the teacher did this for a reason. What could that be?
What was the assignment? To have automated entry? That's parsing. Parsing is difficult. But since the teacher said that it isn't impossible, that means it's possible, and within the limits of the lectures so far. So, how can that be?
It was not after Peter stared at the problem for 20 minutes that he looked at this line one more time:
CH$=MID$(SC$[TCHY/8],TCHX/8,1)
The key isn't in TCHX/TCHY. The key is in CH$! That's one single character. What's a single character? LEFT$(S$,1). If he can find a way to pull a single character each time...
IS$=”1+2+3+4+5=”
@LOOP
IF LEN(I$) THEN CH$=LEFT$(IS$,1) ELSE END
IS$=RIGHT$(IS$,LEN(IS$)-1)
?CH$:GOTO @LOOP
Peter confidently raised his hand. “Teacher! I got it!” All the rest of the students couldn't believe it!

Simpleton Geek Teaches Computer Programming Book 2 Ch 9B


9.2 Arithmetic: Add, sub, mul,div,mod
The students were all sitting in their seats. The teacher was standing in front of the blackboard. “Today, we are going to teach the computer how to calculate! That means addition, substraction, multiplication, division, and maybe even some modular arithmetic.”
The students were confused. “Didn't we learn all that before?”
“Hey, what did I tell you about more than one way of doing things? Have you forgotten that already?” The students kept silent.
“Alright, take a look of this program. You see three variables, A, B and C. Furthermore, there's another variable. This one denotes the arithmetic operator. That's add, substract, multiply, and divide.”
REM CALCULATOR
CLS:CLEAR
DIM OP$[5]
@INIT
A=0:B=0:C=0
OP=0
OP$[0]=”NOP”:OP$[1]=”ADD”
OP$[2]=”SUB”:OP$[3]=”MUL”
OP$[4]=”DIV”

@MAIN
CLS
?”A= “;A:?”B= “;B:?”C= “;C
?”OP= “;OP$[OP]:?
?”MAIN MENU”
?”1. INPUT A”
?”2. INPUT B”
?”3. SET OP TO ADD”
?”4. SET OP TO SUB”
?”5. SET OP TO MULTIPLY”
?”6. SET OP TO DIVIDE”
?”7. CALCULATE”
?”WHICH?”
LINPUT CH$:CH=(VAL(CH$))%8
ON CH GOTO @NOP,@STA,@STB,@ADD,@SUB,@MUL,@DIV,@CAL
@NOP
GOTO @MAINEND
@STA
GOTO @MAINEND
@STB
GOTO @MAINEND
@ADD
GOTO @MAINEND
@SUB
GOTO @MAINEND
@MUL
GOTO @MAINEND
@DIV
GOTO @MAINEND
@CAL
GOTO @MAINEND
@MAINEND
GOTO @MAIN
“There's the main structure of the program. All you have to do is fill in the blanks. You have 10 minutes! Go do it!”
The students went all panicky, but quickly entered the program. 10 minutes later, a few struggling students got their heads whacked by the teacher. Most, however, managed to finish the program. It's all very easy:
@NOP: OP=0
@STA: INPUT A
@STB: INPUT B
@ADD: OP=1
@SUB: OP=2
@MUL: OP=3
@DIV: OP=4
@CAL:IF OP==1 THEN C=A+B
IF OP==2 THEN C=A-B
IF OP==3 THEN C=A*B
IF OP==4 THEN C=A/B
The students tried out their program. All managed to have the program running correctly. It's all very easy. It's just a question of how quickly you can get the program running. The teacher then compared their program with the existing one provided as a sample demo (SAMPLE2) program.
“Now, count the number of lines, which one is shorter? How about convenience? Would you like to enter the numbers repeatedly or would you like to just enter the numbers once? You already know how long it takes you to do this. How long do you think it takes them to do this? One more thing. I want you to draw the tables of operations in your journal. Not that I'll be grading this since this is so easy! Gya ha ha ha!”
“As you notice, this is where most people stop working. Once you have the functionalities of a calculator, they simply stop programming and move on to another program. Well, that's not good enough! We want to have a program that is not only works as expected, but also easy and convenient to use! Therefore, you are going to learn how to do a calculator, like a real calculator. Notice that there is no 'INPUT A' or 'INPUT B' on the calculator. So, we're going to do the same. We will simply concentrate on the buttons, and see if we can duplicate the interface. That means INKEY$.
“Now, if you first begin using the calculator, everything is zero. When you press the operator button, just assign the operator. Simple enough. If you press the equal sign button, that means calculate. Also simple. But what happens when you press the number button? There are different responses.
1. Add the number to the display
2. Clear the display and show the number.
“That's right. 2 different actions, depending upon whether or not you are simply adding digits to the number or you are inputting a new number. So, can anyone tell me the difference?”
The student were listening intently to the lecture, and expected the teacher to tell them the difference. They simply didn't expect to solve the problem by themselves.
All was quite. What would be the difference between adding a digit and inputting new number? Finally, Peter raised his hand. “You add a new number after an operator.” The teacher smiled.
“You got it! It's so simple, isn't it? Take a look at this: 23*45+67. You reset the display when the last key was an operator. What does that mean in our program? We set a flag to clear the display when the last key was an operator. That's it! Now, modify the program so that instead of INPUT, we use INKEY$. Also see if you can do automatic calculation whenever we press any of the four arithmetical operators!”
@INIT
L=0:'0 NORMAL, 1-CLEAR DISP

@MAIN
VSYNC 1:CLS
?”A= “;A:?”B= “;B:?”C= “;C
?”L= “;L:?”OP= “;OP$[OP]:?
@MAIN1
C=0:CH$=INKEY$:IF CH$=”” GOTO @MAIN1
IF CH$==”+” THEN L=1:GOTO @ADD
IF CH$==”-” THEN L=1:GOTO @SUB
IF CH$==”*” THEN L=1:GOTO @MUL
IF CH$==”/” THEN L=1:GOTO @DIV
IF CH$==”=” THEN L=1:GOTO @CAL
C=VAL(CH$)
IF L==1 THEN A=B:B=C
IF L==0 THEN B=B*10+C
L=0
GOTO @MAIN

@CAL:IF OP==1 THEN B=A+B
IF OP==2 THEN B=A-B
IF OP==3 THEN B=A*B
IF OP==4 THEN B=A/B
GOTO @MAIN
“You see we do not do anything different in algorithmic sense. Everything is still the same. Later on, I'll show you a bit more advance knowledge. Now get to it! you have 15 minutes!”
After all the students finished the program and tested it to their satisfaction, the teacher pointed out that their program is actually a poorly designed one.
“You can see, after all, that you need to press equal sign everytime you want to calculate. If you look at the regular calculator, then you see that it calculates automatically after every calculation. So, in that sense, your program works as designed. No one can say that your program is buggy, because it's not. It performs as designed. The program isn't bad, it's the design that is bad!
“So, for your next project, you want to duplicate the function of regular calculator as close as possible. I'll tell you right now that your program must be thrown away and you need to build the new program from scratch!”
All the students moaned in disappointment. “Teacher, why must we rebuild the whole thing? Can't we just modify the existing program?”
“Anything is possible.” said the teacher. “It's just a question of time and money. Sometimes the core (the main program flow) is so different, that it's easier and faster to just do the whole program from scratch. The thing is, sometimes you have gone down the wrong path so far, that you need to redo the whole thing.”
All the students aren't convinced at all. That's just double the work! All that work for nothing!
“Here's the key: You cannot just throw existing program willy-nilly. You must first understand what the problem is, what your solution is, and what your plan is going to be. Once you have those, then you can make the decision whether starting from scratch is the right thing to do. Since I have done the work already, I KNEW that starting from scratch is the right thing to do. So, do your homework!”
“But before that, I want to teach you something really quick: STRING. That is string variables and what you can do with it. Now, you already know what string is. You've been using it with LINPUT, so you have the basic understanding of what it is already. That's why I'm confident that the 15 minutes of class time that we have left is more than sufficient to teach you everything you need to know about strings.”
The teacher then smiled, “Of course, if it turns out that you haven't been doing your homework, and that your understanding of string variables is faulty, then you will be clueless and will not be able to do your homework properly. But in that case, it's your fault! Gya ha ha ha!”
The students are all scared and confused. Learning a whole new concept in 15 minutes? Is that even possible? But since there's no time left on the clock, the students don't have any choice but to go along with the lesson.
“Now, tell me what you know about string.” the teacher started.
“Strings are variables that has value between two quotes.”
“Great. Now let's say there's a string with the value: 'HELLO AND GOODBYE'. Now I want you to tell me the following:
1. The length of the string.
2. The first 5 characters of the string
3. The last 3 characters of the string
4. The string from 7 to 9 position
5. Finding the position of substring “GOOD”
6. Finding the position of substring “END”
“That's should be enough for now. In fact, it's so easy, I don't have to say anything other than it's all described on Help page #32. So get to it! I'll be scoring your work this time around. After all, there's nothing better to make the students fail the quiz than giving precious little time to do it. Gya ha ha ha!”
All the students moaned and get to work immediately. In fact, the answers are all obvious. It's just straight implementation of the commands in help page.
1. LEN(“HELLO AND GOODBYE”)
2. LEFT$(“HELLO AND GOODBYE”,5)
3. RIGHT$(“HELLO AND GOODBYE”,3)
4. MID$(“HELLO AND GOODBYE”,7,3)
5. INSTR(“HELLO AND GOODBYE”,“GOOD”)
6. INSTR(“HELLO AND GOODBYE”,“END”)
The students barely managed to type those into the computer, just to make sure that they have it correct. Of course, smart students would put C$=”HELLO AND GOODBYE”, and use C$ as the variable inside the commands. Doing it that way certainly saved quite a lot of time.
Some of the students actually managed to miss the instruction. Typing 'AND' instead of 'END' for example. Others still didn't follow the instructions too closely and switch MID$, LEFT$, and RIGHT$ in various orders. With predictable bad scores on the quiz.
Just because there's basic understanding on how string works, does not mean the students can just ignore the tasks presented in front of them. They still have to pay attention to details.
“Time's up! Let's see those papers! For homework, I want you to fill in this table. Make sure to do this properly. It is of the utmost importance that you do it right!”
“The grading is that important?” asked a student.
“Oh, I'm not grading it. If you do it wrong, you will certainly suffer greatly tomorrow. Gya ha ha ha!”

Simpleton Geek Teaches Computer Programming Book 2 Ch 9


9. Arithmetic
9.1 Simple calculator
“Does anybody know what a computer does?” asked the teacher.
“It plays game!” “It makes music!” “It does web and e-mail!” answered the students.
“He computes.” said the teacher.
“HE computes?” the students were confused.
“Sure. We're talking about the original computer. Computer as a profession, instead of a machine. So, this is all before Charles Babbage and his Differential Engine.” Of course, the explanation went over the head of the students. They're still in high school, after all.
“And what about modern computers. You say it plays games?” asked the teacher.
“Yes.”
“Well, underneath, it's still the same: shuffling numbers. Loading and saving data, but shuffling numbers, mostly. There is an exception to that, however. In the future, we may not be dealing with bits and bytes.” He surveyed the class. “We may be dealing with qubits.”
“Qubits? What are they?”
“It's like a bit, but it can be 0,1 or both.”
“I'm sorry, teacher, but I don't understand.”
“That's fine. Neither do I, to be honest. As the venerable Richard Feynman said, 'if you think you understand quantum physic, it means you don't.' So, don't be too hard on yourself.”
The students are all waiting patiently for the other shoe to drop. They are now used to long periods of peace, punctuated by moments of sheer terror.
“Of course, that doesn't mean that you can slack off and not understand basic skills necessary to the functioning of every day life! With that comment, I think we should explore STRING functions. I hope you understand that once you learned strings, you will find that it is a better solution to your data storage as to make RESTORE-DATA keyword obsolete!”
“What? You mean we can't use them anymore?” The students are having trouble accepting the fact. “But teacher! We spent so much time learning them, and we're really good at it! Why can't we use them anymore?” The students were disappointed. Does that mean they're spending a lot of time learning obsolete concept? Why even bother?
“Of course, you can still use it. It's just that I don't think you want to do it when the alternative is much better than that. Why bother learning how to crawl, when you will be walking eventually? Because you need to! It's too much to try to learn everything all at once, so you need to be eased into the concept. Remember when I talked to you about growing?” The student remembered. Broken branches and all that. “There are many ways to do something, and the problem is everybody is different. So, the only way to PROPERLY learn computer programming, is to learn as many different techniques as possible, and pick the best one for your use.”
“Which one is the best one?” asked Nancy.
The teacher stamped her head immediately. “Didn't I just tell you to pick the best one? Do you even think about what you're saying?” Of course, with Nancy, that doesn't happen too often.
“Anyway, in the course of developing your computer programming skill, you will learn so many different languages, each with its own strength and weakness. No one can tell you what's the best language to learn. If he does, he's wrong!” 

Monday, November 25, 2013

Simpleton Geek Teaches Computer Programming Book 2 Ch 8D


8.4 Learning vs. Practice
“That was quite a fun session.” Peter admitted. He was in the teacher's office after class, per usual.
“Sure is. The songs you guys wrote are rather stilted, but it can't be helped. Playing it by ear is difficult enough, but without proper music theory, the tempo is rather robotic.”
“But the programming demands that the tempo is robotic.”
“Of course, not! The way I showed you is that tempo is a ratio of one note over another. However, it can easily be the number of jiffies.”
Peter did a face palm. “That's true. I didn't think of it!”
“Ha, it's best if you try to think of three different solutions to a problem. Each and every time.”
“So, each new problem will demand 3 different solutions?”
“Nope. Each problem. Even if you have encountered the problem before, try to come up with 3 different solutions.” said the teacher.
“But what good is it? I'll just come up with the same 3 solutions. So the time is wasted.”
“Not quite true. As your computer programming skills grow, you will come up with better solutions. The number of answers far outnumber the number of problems. Trust me in this that you will think of many new and novel solutions to existing problem.”
“Can you give an example?”
“Take the piano program. Do you see the text on top screen?”
“Sure, you had us print those characters in the shape of the keys.”
“That's a new solution. Traditionally, you would have computed the regions, and do a lot of calculations. Alternatively, you would have stored the data in arrays. Furthermore, you wouldn't be using letters to represents the notes. You would have been using numbers.”
“But your solution uses musical letters and you print them up on top screen.” said Peter.
“Exactly. That's new. Something I came up with. The reason is because you're just learning arrays, and frankly, without proper loops programming, it's just a pain. Loops make array programming an easy task, indeed.”
“So why not learn loops now?”
“Because, believe it or not, BEEP is a whole lot easier to learn than loops.”
“Well, I've learned some loops before, and it's not too bad.”
“In fact, they're rather easy. The reason is that they are conforming to structure. Well-structured programs are rather easy to do. However, compared to BEEP command, BEEP command is hands down way more simpler than loops.”
“Hmmm. It's true that BEEP is simpler than loops.”
“Also, the reason that I'm using musical letters is to facilitate your eventual lessons in music using MML.”
“MML? There's something about that in the Help menu. It looks extremely difficult.”
“It's rather confusing. However, at the heart, it's very simple. Notes are given by letters, tempo by numbers. There's more to it. Some commands for octave shifting, for example. But in simple terms, that's about it.”
“And this is not taught in school? Only you would do it this way?”
“That's correct. That's the difference between me and academia. You see sound and music are being put last. That's academia. I simply concentrate on what's doable and FUN.”

Simpleton Geek Teaches Computer Programming Book2 Ch 8C


8.3 CLS,PRINT,LOCATE,CSRX,CSRY+On screen keyboard
“Does everybody have their design on the journal?” asked the teacher. He asked the student to draw some lines and squares on the 32x24 graph paper.
“Yes, teacher.”
“Good. Now, I'm going to show you this program, and you're going to program it. This here is a good test to see if you can convert from English to Computerese. Or, as any professional will tell you, from Pseudocode to Smile Basic (Petit Computer).”
ASCII ART PAINTER
1. Clear top and bottom screen
2. Set array for text graphic characters
3. Main Loop
1. left/right changes character.
2. Print character on top at (0,0)
3. if touch screen then
1. Print character on X,Y.
“And that's it! Pretty simple, eh? Any question?” the teacher asked.
“What kind of array for text graphic characters?” asked a student.
“You look at the design, then find corresponding characters on the character sheet. Note the number, and store that into an array. Don't forget DIM, and choose your own variable name.”
“What do you mean left/right changes character?”
“You need to have a variable for array index. The value of the variable will vary between 0-max array. Remember that the value of max array is one less than the DIM value. You may use modulus arithmetic to do it. I'll show it to you later.”
“Where do we print the character if touch screen?”
“The touch screen condition is activated when the bottom screen is touched with a stylus. You want to print the character that is indicated on the array, as shown on the top screen, on the stylus location. One more thing: Make sure to include the character 0, so you can erase your drawing.”
As there are no more question, the students all started their programming. When the students reached the button reading code, the teacher showed a little piece of code to cycle the array index.
REM ASCII ART PAINTER
CLS:PNLTYPE “OFF”
CLEAR:DIM T[14]
T[0]=0:T[1]=144
T[2]=145:T[3]=146
T[4]=147:T[5]=148
T[6]=149:T[7]=150
T[8]=151:T[9]=152
T[10]=153:T[11]=154
T[12]=155:T[13]=156
U=0:'U=0-13
@LOOP
VSYNC 1:BT=BTRIG()
TX=TCHX:TY=TCHY:TS=TCHST
IF BT AND 4 THEN U=U-1
IF BT AND 8 THEN U=U+1
U=(U+14)%14:'KEEPING IT IN 0-13
LOCATE 0,0:?CHR$(T[U])
IF TS THEN PNLSTR TX/8,TY/8,CHR$(T[U])
GOTO @LOOP
Ten minutes later, most of the students have finished their programming, and started drawing their design on the computer. Some students, who have opted to get standard size, instead of XL version were struggling. They were not amused when their teacher showed a picture of magnifying glass holder. The teacher just laughed.
“Oh, by the way, in case you're having trouble with the small screen size,” the teacher showed his darkside, “you can easily modify the program to use the DPad for a cursor for the bottom screen. A button to print the character. B button to erase the character. X and Y button to cycle through the character, respectively. In fact, that'll be a good homework for you. Not that I'll be grading it! Gya ha ha ha!”
“Don't we need to show cursor for that?” asked Peter.
“Not really. Just erase the character if you draw it wrong! Although, if you really want to, you can use either BG command or Sprite command to show a cursor.”
“BG command?”
“Short for background. I supposed I should teach you about it one of these days, but not anytime soon! Gya ha ha ha!”
The students were feeling rather unappreciated. All these ungraded homework. Yet, they were resigned to do all of them, since failure to do the homework will result in low test scores. Mind you, their quiz scores were rather dismal even after doing all the homeworks, they dared not to imagine what their scores will be if they skipped doing their homework.
“Everybody got it?” asked the teacher.
“Yes, teacher. How can we save it?”
“You can't. There's no way to read the bottom screen. You need to save it in a separate array for it. I suggest a double dimension array to do it, although you certainly can do it using single dimensional array.”
“Double dimension array? What's that means?”
“Are you kidding me? You don't know what 'double dimension' means?” The teacher was looking around the class. Clueless faces abound. “Okay, you know that when you want to have more of the same variables, you use array? DIM and all that? Well, so far, we've been using single dimensional array. So, imagine that all variables are all in a line.”
The teacher drew a line, and put some numbers on it. Just like an axis.
“Now, a double dimensional array, will have another line.” He drew another axis. In fact, it looks suspiciously like the 32x24 grid representing the character screen.
“So, instead of having a single array index, we're using two array indexes. See this thing here? PNLSTR X,Y,CHR$()? If you have a two dimensional array representing a character screen, you can have it like this:
DIM SCR[32,24]
SCR[TX/8,TY/8]=T[U]
“You need to insert them in the appropriate places, but I'm sure you can do that easy!” Of course, the student were still ignorant of the issue, but with some not so gentle guidance, they were able to finally figure out what to do, and be able to modify the program so that the program will output a series of numbers so that they can copy the numbers into DATA commands for future drawing.
“This is actually the hard way to do things. It would be much simpler once you've learned loops.” said the teacher. “But I want you to suffer some more still! Gya ha ha ha!”
“So, how good are you at reading help file? We're going to skip to the end real quick and make some music! Not REAL music. We're going to do with using BEEP. That's right. Old computer 8-bit music. Now, I want you to write this program displaying the set of characters on top screen, and an ASCII picture of the piano keyboard on the bottom screen.” (Appendix E)
“For the main loop,” the teacher continued, “I want you to check whether or not the user touched the bottom screen, and if so, shows the corresponding character on the top screen. Let me know when you're done.”
Some of the students were having a little difficulty in implementing the program. The teacher encourage student discussions and help. “The best way to learn things is to teach things, so why don't you help your fellow students? By the way, here's the pseudocode I'm looking for:”
@LOOP
1. READ TCHX,TCHY,TCHTIME
2. CHKCHR(CURSOR LOCATION)
3. PNLSTR 0,15,CHR$
4. GOTO @LOOP
After some trial and error, all the students finally got it. “Great! Now I want to to open up the help menu and see the help page number 43, specifically the BEEP command. First, you notice that there is waveform number (0-69) and pitch. Scroll down to the bottom of the page and there is an instruction on how to do the pitch. Specifically, how P=4096/12. Also notice that there is a table called 'Changes to Musical Interval'. I think we all agree that it doesn't take a genius to realize that the letters are pretty much self explanatory. Notice that I put in the letter H at the end. That's equivalent to P*12, by the way. So, now, what we need to do is to create some look up table.
C=0 c=1 D=2 d=3
E=4 F=5 f=6 G=7
g=8 A=9 a=10 B=11
H=12
“Fortunately, when we do CLEAR command, all the variables are set to zero. So, when we do DIM notes later on, all the variables are still zero and all we need to do is implement the values above. I suggest that you create an array with 256 elements, and for each corresponding value in the table above, set the values in the character code entry. You know how to determine the character code for a character, right? Use ASC command! Here's the pseudocode that I want you to use:”
DIM N[256]
SET N[] ENTRIES PER ABOVE * P
@LOOP
1. READ TCHX,TCHY,TCHTIME,BTRIG
2. IF TCHTIME==1 THEN
1. A=CHKCHR(CURSOR LOCATION)
2. IF A!=0 THEN BEEP WAVEFORM,N[A]
3. SET WAVEFORM ACCORDING TO L/R DPAD
4. GOTO @LOOP
“Of course, I do not expect you to start from scratch! I expect you to modify the existing program! Now get to it! First find out what the character code values are using ASC command, then fill in the details.”
The students are busy typing '?ASC(“C”)' and other characters. After finishing the table, then they began to modify their program. All in all, it only took them about 10 minutes to do. Soon, the class is filled with electronic music!
“Now, does anybody know how to do karaoke?” asked the teacher. Of course, no one does. The students all stared blankly at the teacher. “Come on, now, show and tell time! What does a karaoke system entails?”
“Well, there's music.” said a student.
“Which is note and tempo. What else?”
“Words?”
“Correct, so we have note, tempo and words. Would this covers it?”
1. Note
2. Wait time (tempo)
3. Text
“So, what I want you to do next is to create DATA statements, consisting of those 3 different elements. We can just use the same program and simply write it after the original data. Press 'A' for automatic music! How's that?”
The students are all getting excited! Soon, they were getting busy. It's rather interesting how little it takes to write a worth while program. Animated text! Who would have thought of it?
IF BT AND 4 THEN DELAY=DELAY-1
IF BT AND 8 THEN DELAY=DELAY+1
DELAY=(DELAY+60)%60

@AUTOPLAY
READ NT$,WT,XT$
IF XT$==”END” GOTO @MAIN
BEEP W,N[ASC(NT$)]
PNLSTR 0,17,XT$
VSYNC DELAY*WT
GOTO @AUTOPLAY
Of course, there is more than one way to do it. This particular implementation, Appendix F, relies on the fact that notes are given in string. However, it can easily be done that the notes are given in multiple P blocks.
The data is set in @MUSIC DATA segment, and it does take a long time to do, even with copy and paste. The tempo is rather rough because it's very difficult, unless one is reading musical notation from song book. The text is currently limited to just one line, but there is no reason why the data cannot include text cursor positioning as well.
It does illustrate the point that computer programming, coding the program, doesn't take a lot of time. Once the design is decided, there is nothing to stop the computer programmer to just code the program quickly and easily. It is much more difficult and time consuming to write the data than it is writing the program itself.
The students were really having fun. In fact, some students decided to form an impromptu band. Some playing the beep, others playing drums. Some cat's meow can be heard as well.
The teacher simply leaned back on his chair. He enjoyed the situation. Of course, it would have been better with ear phone, but there's nothing inherently bad about making music with the computer, especially using the computer program the students wrote themselves. It is very encouraging to have the students write the tools as opposed to just give them the completed tools, a mysterious black box if you will, that is all-powerful but with all the features hidden.
“By the way, guys, you know that the sound can have up to 7 sounds? You can have polyphonic. Simply add an extra note, and READ statement, and there you have it!”

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!”