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 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!”
No comments:
Post a Comment