So far everything that is stored in the computer's memory Is exactly what you typed. Often, however, we would like to tell the computer how to do a problem before we tell it what numbers to use. This is something like the riddle where you are asked to think of a number, then add, subtract, multiply, and divide various quantities without disclosing the number. At the end you are supposed to be surprised that the result can be told you, or that the original number can be deduced from the result. We want to give such riddles to the computer, then maybe tell it what number to think of. This chapter tells you how to do it.
First you need to understand how memory is organized in your computer. TINY BASIC has two places to store things. One of these you already know about, and every time you type in a line with a line number, it goes into that storage area of memory. Internally, it is kind of like a file drawer, where each program line is inserted into its respective place in the file. As more program lines are added, you just move the stop at the back of the drawer back some more to accomodate them. When you take lines out, you crimp the backstop up, so that the rest of the lines in the file will not fall over. Of course it is possible to try to put too much into the file. After a while you simply cannot push the stop back any farther, and TINY BASIC refuses to take the next line. When it does this, it will tell you with error message number 8.
Right now, however, we are interested in the other storage area in memory. It is divided into 26 little boxes like a post office. Each box holds exactly one number. Each box has a name, which is one of the letters of the alphabet. Any time we choose to do so, we can take the number out and look at it, br maybe put another number back into the box. We tell TINY to put a new number into a box with the LET command. For example:
LET A=123tells your computer to put the number 123 Into the box named "A". You can look at the number in the box named "A" with our friend, the PRINT command:
PRINT AAnd because the box contains a number, me can do anything with it that we might want to do with a number. For example, we can ask the computer to take whatever number is in A, add 5 to it, then print half of the sum:
PRINT (A+5)/2If you do that now, after typing in the other lines above, the computer will compute (123+5)/2 which is 64. Suppose, however, you type in
LET A=27The computer immediately forgets that the box named "A" ever had 123 in it, and remembers instead that it has 27 in it. The answer is now 16.
PRINT (A+5)/2
It is important to realize that the LET command is not an equation like those you studied In algebra. It may be true that A does equal 27 immediately after the command is obeyed (we say "executed"), but what does it mean to tell the computer
LET A=A+2Obviously A never equals A+2 in any mathematics you and I are familiar with. But TINY BASIC understands what that means. Type it in, then PRINT A again. You see, what happened is that TINY saw the line start with "LET A=", and sort of mumbled to itself, "oh, yes, I have to put some now number in A." Then it sees the "A+2" while thinking, "gotta have a number, gotta have a number,..." Of course A+2 is a number: it is the sum of the number in A (which, you recall, vm had just stored 27 into) plus two, which is 29. Therefore, TINY stores the number 29, which it just computed, into the box named "A". Now it matters not a whit that there used to be 27 In A; all that matters is that A has 29 in it now. That is why we call these boxes variables; the value of the number in them can vary from time to time.
Now ve can begin to write interesting programs. The idea is to write a program in which we tell the computer what to do with the numbers in certan variables, then when we run the program me tell it what numbers are in those variables.
Let us suppose that you want to order another memory board from Netronics and you cannot find your calculator to compute the sales tax and handling charges. Oh, let's be efficient, and set it up so you can also order books from BYTE and parts from your favorite surplus store.
Variable P will have the price in cents (no decimal point, remember?), R will have the tax rate in percent, and H will have the handling fees, also in cents. Remembering that the largest number TINY can think about is 32767, we observe that our order must not exceed $327.67. No problem! Netronics boards don't cost very much. We will start with a price of $89.95, which means P will be 8995.
The first thing the program needs to do is calculate the tax on the order. The program will put this number into variable T. If we just multiply the price times the tax rate, we might got a very large number (consider $100 x 596, which is computed as 10000*5 or 50000, which is too large). So we will be tricky. Besides, it is more fun if you can trick the computer into doing something more than it ought to. We will divide the price by 100, which gives a price in dollars, then multiply that times the tax rate to get the tax in cents on the dollar part of the price:
100 LET T=P/100*RBut we must not forget the 95 cents part of the price, which is likely to have another nickel or so of tax. Remember that when you divided the price by 100, the cents part was thrown amay. We can depend on that fact, so if we divide the price by 100 then multiply it by 100 again, we will got the dollar part of the price, measured in cents. This can be subtracted from the original price, leaving the cents part of the price, which we tell the program to put in the variable C. I know It sounds confusing, but it's really not too bad -- think it through one step at a time: divide 8995 by 100 giving 89 (throw away the remainder 95); multiply 89 times 100, which is 8900. Subtract that from the original 8995 (which is still in P) leaving 95, which is the cents part.
110 LET C=P-(P/100)*100Actually the parentheses are not necessary, because TINY does multiplications and divisions in left-to-right order. Now we still have to calculate the tax on this amount. It will be C*R/100 (remember the tax rate is in percent). But since this throws away the fraction, we need to do the rounding before we divide by 100. Rounding is normally done by adding .5 to the result, then throwing away the fractional part. This is mathematically the same as adding 50 before dividing by 100, so the tax on the cents part is (C*R+50)/100. The parentheses are necessary here. At add this to the tax on the dollars part, and put the sum back into T.
120 LET T = T+(C*R+50)/100Notice that this will add 50 to the product of CxR, divide the result by 100, (throw away the fraction), then add the result to T. We know this because of the natural order for doing addition and division, and because of the parentheses.
Now that we have instructed the computer on how to compute the sales tax, the rest is easy. The check should be made out to the total (in cents) of T+P+H. Let's tell the computer to put that into variable M:
130 LET M=P+T+HNow we could just tell the computer to print the result, but an amount in cents looks awfully big, so let's print it out in dollars and cents, with a dollar sign and a decimal point. Yes, I know I said that TINY BASIC cannot compute decimal fractions but that does not mean you cannot print them! You know how to get the dollar part of M; it is M/100. And you know how to got the cents part alone; it is M-M/100*100. We will print the result in two stages:
150 PRINT "$";M/100;".";Notice that on line 150 we are printing three different things, each followed by a semicolon (";"). The semicolon tells TINY that we wint to print something else on the same line. So first it prints a dollar sign, then it prints the dollars part of M, then it prints a decimal point, then it notices that there is still more to print, but that you have not yet told it what. When it gets to line 170 it now knows to print the cents part that we put into C (on line 160) on that same line, and since there is not another semicolon, the line is finished.
160 LET C=M-M/100*100
170 PRINT C
We need one more thing: an END statement. I think it is nice also to tell yourself what the number means, so why dont you print out a line explaining the result?
200 ENDOf course you must try the program out. Say the memory board costs $89.95 with $3 postage and handling. Assume the tax rate is 5%. To run the program, type
140 PRINT "WRITE CHECK FOR"
LET P=8995Did your computer give you the answer of $97.45?
LET H=300
LET R=5
RUN
Now it is a big nuisance to have to remember to type in LET commands for P, R, and H. Wouldn't it be nice if the computer could simply ask for price, handling, and tax rate? Well, it can. This takes a new command which inputs data from you as the program is running. It is the INPUT command. Try it out just now; type:
INPUT XNotice that you got a question mark instead of a colon prompt. TINY is now waiting for you to type a number, which it will then put into the variable X. Type some number (say, 456) and a RETURN. Now that you have your colon prompt back, ask TINY to print out X. Repeat the experiment, but this time with some other number. Do you think you understand the INPUT command?
Let's put it into the program. First we will print out what the computer is expecting, then we will request it as input:
10 PRINT "PRICE";Notice that each PRINT command ends with a semicolon. This tells TINY that more is to come on this line. What is to come is the question mark of the INPUT command, so each request for input comes with a question mark automatically supplied by TINY. Now RUN the program (without any LETs). See how it asks you for a price? Say you want to buy a $2.95 Lexicon from an ad in BYTE; type in 295 (remember, no decimal point or dollar sign!) and RETURN. Now it asks for Handling. $.75 per book, so type 75. There is no tax on books from New Hampshire, so type in 0. The check is to be for $3.70. Try a few others (type RUN again).
20 INPUT P
30 PRINT "HANDLING";
40 INPUT H
50 PRINT "TAX RATE";
60 INPUT R
You should not assume because you got the right answer the first two
times you run your program, that it is necessarily correct. Here for example,
is a very subtle problem in this program. Perhaps you will notice it as
you experiment with various numbers. Or you can hurry on to the next chapter,
which also tells you how to fix the problem.
Back to Top
Continue with: 4 -- If an Elf Could Decide...