Reference / Paper · 1982
Problem-Solving on the TRS-80 Pocket Computer
A Wiley self-teaching guide to programming the TRS-80 Pocket Computer.
Reference / Paper · 1982
A Wiley self-teaching guide to programming the TRS-80 Pocket Computer.
BS POP be OSL De Se oe 6 eh olat 3 5 e tt bee por pare 1 Ps Se eee Y § a © eet ete Oe orate ee ee a sShaek: ::: fees Peelers we ON tea Pad eras Ch BC by Se ees ee. 4 eae a LN UL De teen Sel eters . Oe te x . Oar ate ke wrest tieratetssel® © OE vg Sata en oti een eete ¢ ae 7 pe a Sciwlelet, Peeper el elebere 0 Peewee 2 ere lets Cru tate herebene pie) orate tats oreo etstrase Te tgveE® tees ~ eteee © 0 eer, eee *. Sees . te. ene careaeetisdeheiernads Bienes . Sites tot areteiee SU wretei tone re te ame Or etelts See a leans®. 0 ere latghtes erates. tee Seen oan teeiotedarencys . SNSAINRS Sit Sas an . PLS 0 a eleke 0 elarek, “PO Oe oe ow og O20 ie eke, . thetgts barbed Sees eee ee . 100 0 Se se ee * SO tig as ar e-. - 5 00 A me al OMe he ts ‘ad FE es Os oe ay 0, . webs, fe. “ oe *e “—* ee een rn - - ~ -“& - & 6! he oe yee ‘Un Me yl te he Petototoias Biter tet etos Rie a teh Re Be oe o ae te ie be ty thee a (oft ee eT SP OSS IE os. ote i er Teeny aren — 75 tra an PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER® More than a million people have learned to program, use, and enjoy microcomputers with Wiley paperback guides. Look for them all at your favorite bookshop or computer store: BASIC, 2nd ed., Albrecht, Finkel, & Brown BASIC for Home Computers, Albrecht, Finkel, & Brown TRS-80 BASIC, Albrecht, Inman, & Zamora More TRS-80 BASIC, Inman, Zamora, & Albrecht ATARI BASIC, Albrecht, Finkel, & Brown Data File Programming in BASIC, Finkel & Brown Data File Programming for the Apple Computer, Finkel & Brown ATARI Sound & Graphics, Moore, Lower, & Albrecht Using CP/M, Fernandez & Ashley Introduction to 8080/8085 Assembly Language Programming, Fernandez & Ashley 8080/Z80 Assembly Language, Miller Personal Computing, McGlynn Why Do You Need a Personal Computer? Leventhal & Stafford Problem-Solving on the TRS-80 Pocket Computer, Inman & Conlan Using Programmable Calculators for Business, Hohenstein How to Buy the Right Small Business Computer System, Smolin The TRS-80 Means Business, Lewis ANS COBOL, 2nd ed., Ashley Structured COBOL, Ashley FORTRAN IV, 2nd ed., Friedmann, Greenberg, & Hoffberg Job Control Language, Ashley & Fernandez Background Math for a Computer World, 2nd ed., Ashley Flowcharting, Stern Introduction to Data Professing, 2nd ed., Harris PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER® New York « DON INMAN JIM CONLAN Dymax Corporation Menlo Park, California EAR weve 1807 11982 a oC ORTism John Wiley & Sons, Inc. Chichester « Brisbane * Toronto ¢ Singapore Copyright © 1982, by John Wiley & Sons, Inc. All rights reserved. Published simultaneously in Canada. Reproduction or translation of any part of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Requests for permission or further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. Library of Congress Number 81-10358 ISBN: 0 471 09270~3 ISBN: O 471 86808-6 RS Printed in the United States of America 82 83 109 8 76543 2 «1 Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Appendix A Appendix B Appendix C Appendix D Appendix E Index Contents Pocket View of the Pocket Computer Applications, Memory Use, and Definable Mode Error Codes, Editing, and Cassette Use Data Files Trigonometric Functions Operation Time, Logic Functions, and Binary Bins Feedback and Systems Random Walk Computing Interest Storing, Sorting, and Searching (Or How To Make Sure You Know Where It’s At) Chaining Programs from Cassette The TRS-80 Pocket Computer Printer BASIC Statements and Commands Special BASIC Functions Acceptable Abbreviations for BASIC Statements, Commands, and Special Functions Error Codes Printer Terms 25 52 75 96 128 145 161 183 199 216 224 243 246 247 249 251 253 rol on irae ee TO THE READER Microcomputers continue to appear in more and more places. As they become smaller, they become more portable and, hence, useful in more places. The Radio Shack TRS- 80 Pocket Computer is the ultimate in portability. It can be tucked away in your pocket and used wherever you go. Due to its portability, new uses will be continually discovered for this versatile tool. This book shows a wide variety of problems that are easily solved with the Pocket Computer. Although some problems may seem to be presented with tongue in cheek, _don’t let the light-hearted approach fool you. All demonstration programs are practi- cal in some way and, hopefully, will coax you to further applications. The problems cover a wide range of difficulty, but hints are provided that may be either used or ig- nored as your needs demand. We assume that you have some familiarity with computing and with BASIC lan- guage as used on other computers. If you have never encountered BASIC language, we suggest TRS-80 BASIC; Albrecht, Inman, and Zamora; John Wiley & Sons, Inc., 1980. (It is also sold at Radio Shack stores under the title TRS-80 Level II BASIC.) Our book has been organized to meet two main objectives: 1. Teaching the Tool — The TRS-80 Pocket Computer BASIC language and the use of the Pocket Computer’s keyboard dominate the early chap- ters in the book. Problems are introduced as demonstrations to facilitate this objective. 2. Teaching Problem Solving Techniques — The techniques of problem solv- ing dominate the discussions in the later chapters. Here the computer is regarded as a precision tool that is used to solve problems. After covering the first five chapters, you will be able to make full use of the Pocket Computer. You will then be ready to solve the numerous problems that follow. Many different types of problems demonstrating a wide variety of applications are given throughout the book. Hints that are provided for the solutions to the problems are often quite extensive. You may choose not to use them, but they are there if you need them. Often, more than one solution is given for a problem. This is true to life. There are many ways to solve most problems, and we do not all solve our problems in the Same way. The TRS-80 Pocket Computer is the tool used in this book. It is a small, but so- phisticated, tool. You will find that the more that you use it, the more useful it is to you. This book is designed to help you explore the Pocket Computer in depth so that you can use it successfully in solving problems that you encounter outside of this book. CHAPTER ONE Pocket View of the Pocket Computer The TRS-80 Pocket Computer is a unique mixture of a programmable calculator and a general-purpose computer. Although this book’s main purpose is to illustrate proce- dures and techniques for solving problems with the Pocket Computer, some space must be allotted to a description of this unique problem-solving tool. Like any artisan, the problem solver is dependent on the tools that are available, and the ability to make use of fine tools is dependent on one’s understanding of them. This chapter is, therefore, devoted to the introduction of the capabilities of the TRS-80 Pocket Computer. In this chapter you will: become acquainted with the general characteristics of the Pocket Computer; learn how characters appear on the display; discover what kind of variables can be used in a program; learn a few BASIC commands and statements that are unique to the Pocket Computer or are used in unique ways; discover four different operating modes; learn how to use some keys in two different ways; learn something about error codes and how to clear them; discover PAUSE, a new form of the PRINT statement; learn all about the NEW statement; learn how to DEBUG programs; learn how to check for the amount of unused memory; and learn how to continue an interrupted program. The TRS-80 Pocket Computer can be used as a calculator, as described in the TRS-80 Pocket Computer Software Manual (Catalog Number 26-3501) that comes with the computer. There are many wondrous features to be exploited in the calculator mode — you can even find the solution to an algebraic equation. For example, if you have previously entered a value for the variables A, B, and C, the Pocket Computer is capable of solving the square root of the following expression. ( Vt Br B+tetae Cz ) 2 PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER Each of the symbols shown in the example is entered from the keyboard of the Pocket Computer. When you press the ENTER key, the result is displayed. Description of the Pocket Computer Imagine carrying around the power of a BASIC-speaking computer in your pocket. No longer are you tied to a computer room or office desk — or even an electric company. Just tuck the computer into your shirt pocket and take it with you wherever a problem exists. Note: We didn’t say tuck the computer into your back pants pocket. The liquid crystal display (LCD) is made of glass. If you sit down with it in your back pocket, you may crunch the LCD. As long as the Liquid Crystal Display has been mentioned, we might as well talk about it first. If you’re used to a 64-character, 16-line display of the TRS-80 Model I or Model III (or something similar), the Pocket Computer’s 24-character, 1-line dis- play is going to take a little adjusting to. However, within a few minutes, you’ll find yourself writing shorter instructions. You may even find out that your programs be- come easier to read and understand because of the limit of the display size. ‘e 10 PRINT ''SHORT INSTRU 7 The cursor is shown at the 24th position. What happens when the next character is typed? POCKET VIEW OF THE POCKET COMPUTER 3 Although only 24 characters are displayed, a BASIC line may contain a maxi- mum of 80 characters. Computer buffs would say, “The size of the input buffer is 80 characters.” Let's not get so technical. What they mean is that you can only input 80 characters on one program line. The computer will hold all of them even though you can’t see them all at the same time on the display. If you continue to type the previous line, you will see the letters move to the left on the display and off the other end. C PRINT ''SHORT INSTRUCT_ C 0 PRINT ''SHORT INSTRUCT_ Cursor indicates position of next character to be entered. ( PRINT ''SHORT INSTRUCTI_ ( PRINT ''SHORT INSTRUCTIO_ ( RINT ''SHORT INSTRUCTION— INT ''SHORT INSTRUCTIONS_ Lat ky) There! The instruction’s complete, ready for you C NT ''SHORT INSTRUCTIONS! '_ to press ENTER. > Gua Yes, the TRS-80 Pocket Computer has an ENTER key that works just like the ENTER key on other TRS-80 computers (or like the RETURN key on some other computers). The Pocket Computer ENTER key looks like this: ENTER The keys of the Pocket Computer are small and closely spaced, but they have a good feel and make clean contact. Numeric and arithmetic operation keys are in a sep- arate area on the right side of the keyboard. Your entries may not go as fast as if you were touch-typing, but you’ll soon get up to a respectable speed. The computer can hold a maximum of 1,424 program steps. There are 26 fixed memories (used for variables) and 178 flexible memories (shared between program steps and variables). You'll learn all about the use of memory in Chapter 2. Calcula- tions are carried out to 10-digit accuracy. 4 PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER The computer also has editing functions, which include shifting the cursor from left or right, inserting characters, deleting characters, and shifting up or down one line at a time. We'll discuss these functions in Chapter 3. Calculating capabilities include four arithmetic operations, power calculations, trigonometric and inverse trigonometric functions, logarithms and exponential func- tions, angular conversions, square roots, sign functions, absolute values, and integer and logic functions. Power is supplied by mercury batteries. Memory content is not lost when the computer is turned off. Memory is protected (or saved) by the batteries when the rest of the computer is turned off. As interface is available for a cassette recorder to save programs and data on tape and to retrieve them at a later time. There is also a 16-column printer that Radio Shack sells, which includes an interface for both a recorder and the printer. The computer itself is in a small package, 67 inches long, 234 inches wide, and 19/32 inches thick. Its weight is just over one-third of a pound. A carrying case, two keyboard templates (more about them later), and a User’s Manual are all included with the computer. TRS-80 Pocket Computer BASIC The BASIC language used by the Pocket Computer can recognize both numeric and string variables. A data memory is called a numeric variable when it stores a numeric value. Numeric variables use labels, or names, such as A, B, C, A(1), A(28), etc. A data memory is called a string variable when it contains a string of characters (limited to seven letters, numbers, or special symbols). String variables are distinguished by the $ symbol and are labeled as A$, B$, C$, A$(1), etc. CAUTION: There is a limit of 26 fixed memories used to hold variables. A (a numeric variable) and A$ (a string variable) both assign data to the same fixed memory. This must not be done at the same time. Only one value may be held in a given memory at a given time. The Pocket Computer’s memory is used in much the same way as that of a pro- grammable calculator. Here is how the fixed and flexible memories may be assigned if no arrays are used. The use of memory for arrays is discussed in Chapter 2. POCKET VIEW OF THE POCKET COMPUTER 5 \\ Program ARIK 1424 program steps, or Free Memory 178 data memories, or a combination of the two \ Zi Zor 2S Y or YS X or X$ 26 fixed memories H : for data only Cor C$ Bor BS Aor AS Any memory not used for program steps can be used for data memory, in addi- tion to the 26 fixed memories. This may be done when using arrays. The number of data memories beyond the 26 fixed memories therefore depends upon how many steps are used in the program. The free memory in the previous diagram is used for addi- tional program steps or data memories. In general, you may find out how much mem- ory is not being used by typing in the MEM command in any of the Pocket Computer’s operating modes. The display will then show how much data memory and how many program steps are unused. This is more thoroughly discussed in Chapter 2. Basic Functions, Statements, Commands, and Tape Control Statements Now let’s take a look at the functions, statements, and commands available in Pocket Computer BASIC. BASIC Functions | / 5 ABS Absolute value ACS Arc cos ASN Arc sin ATN Arc tan Cos Cosine DEG Degree/minute/second to decimal DMS Decimal to degree/minute/second EXP Exponential function INT Integer LN Natural logarithm LOG Common logarithm SGN Sign (positive or negative) SIN Sine TAN Tangent Vv Square root Tr Pi 1 6 PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER AREAD BEEP CLEAR DEGREE END FOR GOSUB GOTO GRAD IF INPUT LET NEXT PAUSE PRINT RADIAN REM RETURN STEP STOP THEN USING CONT DEBUG LIST MEM NEW RUN CHAIN CLOAD CLOAD? CSAVE INPUT # PRINT # aay BASIC Statements | ~~ Contents displayed at start of definable program are read into the specified variable Beep sound is generated as many times as specified Data memory clear Degree is designated as angle measure Indicates the end of a program Beginning of FOR-NEXT loop Execution is shifted to specified line or label where a subroutine begins Specified line or label is executed Grad is designated as angle measure Based on the specified condition, a branch is either taken or not taken Allows data to be input Assigns a value to a variable Ends a FOR-NEXT loop. Increments the step Displays an output for approximately .85 seconds before going on Displays the specified contents and waits for ENTER key to be pressed Radian is designated as angle measure Designates a non-executable statement Returns from subroutine Increments a FOR-NEXT loop Stops the execution of a program Used only with IF statement as a jump instruction Format designation for display BASIC Commands |< ) Restart an interrupted program Used to debug a program line by line For listing a program in memory Display amount of unused memory Clear memory for a new program Execute a program Tape Control Statements Program recorded on tape is read and executed Transfer a program from tape to computer ( bA/)t Check contents of program with those placed on tape Record a program on tape Transfer data from tape to memory of computer Record data memory contents on tape POCKET VIEW OF THE POCKET COMPUTER 7 In writing this book, we assume that you have some knowledge of BASIC lan- guage. If you do not, we suggest you read TRS-80 BASIC; Albrecht, Inman, and Zamora; Wiley & Sons, Inc., 1980. As you look through the Pocket Computer’s BASIC commands and statements, you will recognize many of them. We will not dwell on those statements and commands that are common to other computers. How- ever, the Pocket Computer has some new ones, as well as some familiar ones that are used in different ways, which we will explain. Modes of Operation Before you are introduced to any more BASIC commands or statements, you should take a look at the four available operating modes of the Pocket Computer. 1) Program mode (PRO) 2) Run mode (RUN) 3) Definable mode (DEF) 4) Reserve program mode (RESERVE) The mode currently being used is displayed at the top of the display. C reserve ) A mode key, MODE, changes the operating mode from left to right. The mode moves clockwise, as indicated in the following diagram, each time the MODE key is pressed. — DEF — RUN — PRO — RESERVE a — The operations carried on in each mode are briefly summarized as follows: 1) PRO-The program mode is used to write, enter, and edit programs. 2) RUN-—The run mode is used to execute programs or for direct calculations. 3) DEF-The definable mode is used to execute programs that have been defined by a label. 4) RESERVE - The reserve program mode is used to write, enter, and edit programs or functions for reserved keys that will be frequently used. 8 PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER The use of BASIC commands and statements depends upon the operating mode selected. In this chapter, you’ll find out how to use the PRO and RUN modes. In ac- tual usage, the operating modes interact with each other and should not be considered isolated from each other. Let’s investigate the PRO mode first. The Program Mode Access the program mode by pressing the MODE key a number of times (depending on the mode that you are presently in) until you see the PRO prompt on the top of the display. When you turn the Pocket Computer on, it is always in the mode in which it was last used. Example: Suppose you last used the RESERVE mode, and you want the PRO mode now. C RESERVE +) Press the MODE key once, and you see: omit er Press the MODE key again, and you see: C RUN +) Press the MODE key once more, and you see: is ee, You are now in the mode normally used to enter programs into the computer’s memory. Lines of a program are numbered using integers ranging from | through 999. When a line is complete, it is entered into memory by pressing the ENTER key. One line may contain one or more statements. If more than one statement is used within a line, the statements must be separated by a colon. Example: One statement per line: _ 19 INPUT A 1) First line POCKET VIEW OF THE POCKET COMPUTER 9 ‘@ 29 B=A+1_ ) Second line Or two statements per line: ° 19 =f rae i colon cursor You may have trouble discovering how to display the colon. Notice that some of the top two rows of keys have symbols above them. (OFF) eae AmAAanmea amo moOmMmmwee The colon is the symbol above the I key. na To enter the colon, you must first press the SHIFT key and then the I key. This selects the colon for entry. A shift designator appears on the display when the shift key is de- pressed: It tells the user that the next key stroke will be shifted. iad / 2 SHFT E Ist then next When the shift key is pressed, this appears on the display. If you should ever press the SHIFT key by mistake, press it a second time. This will cancel the affect of the first shift. 10 PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER When the ENTER key has been pressed following the completion of a program line, the line is entered into memory, and the line is displayed once again with a colon following the line number. We will not always remind you to press the ENTER key. Just remember that it must be pressed at the end of each program line. ‘@ 19: INPUT A:B=A+1 i. colon follows colon separating two line number statements on one line If you should ever forget whether you have pressed the ENTER key or not, look at the display. If the cursor shows at the end of the line and a colon does not show after the line number, (. 19 INPUT A:B=A+1_ ) n PA no colon cursor the ENTER key has not been pressed. If the cursor does not show and there is a colon following the line number, a 19:INPUT A:B=A+1 +) A Pa colon no cursor the ENTER key has been pressed. Suppose that you have pressed the ENTER key after the first statement at line 10. The computer waits for you to type in the next line of the program. It doesn’t give you any visual indication, but it is waiting for you to go on. Type in the next line. For example, type ( 29 PRINT A,B ) A i t The comma is displayed by pressing SHIFT, then the letter O. Do not put a colon here, the computer will do it after you press the ENTER key. POCKET VIEW OF THE POCKET COMPUTER 11 If you put a colon of your own after the line number, the computer will add another. You’ll then get an error mes- sage when you run the program. ‘< a eee ) To enter line 20, the sequence of keys pressed should be: 20 SPC P RIN T SPC A SHIFT O B ENTER The display would then show C 29: PRINT A,B +) Now finish off the program by adding an END statement. Type 30 END, and (of course) press ENTER. The display: The END statement is optional If it is not there, the computer will stop after the last executable line. However, sometimes the END statement must be used to separate the end of the program from data or other information that might follow. We know that you are just itching to run the program that you just entered, so... The RUN Mode Suppose that you tried to run the program while you were in the PRO (Program) mode. If you did, this is what you probably saw: As you know, this is an error code. To clear an error code: press the red CL key. 12 PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER You can’t RUN a program while you are in the PRO mode. Press the MODE key three times. C RESERVE ) First time C DEF ) Second time a RUN 2 Third time Now, you can type RUN. If you remembered to press the ENTER key after typing RUN, you saw this: we! i. The question mark means ready for an INPUT. However, if you forgot to press the ENTER key after typing the word RUN, the computer is still waiting at this point. RUN— eee } Press the enter key to see the question mark prompt. Try 25 for an input. Don’t forget to press the ENTER key following the input. This is what you will see: C 25. 26. Z There’s A; There’s B, your input (B=A+1) When the Pocket Computer encounters a PRINT state- ment, it PRINTS the value specified and waits and waits and waits. It will not do anything else until you press the ENTER key. When you press the ENTER key, the next statement: 39:END is executed, and the ready prompt shows at the beginning of an otherwise empty line. POCKET VIEW OF THE POCKET COMPUTER 13 The ready prompt indicates that the Pocket Computer is ready and waiting for the op- erator to input a command for it to do something. = ) a the ready prompt Most computers go on after a PRINT statement, but the Pocket Computer can display only one line at a time. If it didn’t stop and another PRINT statement were ex- ecuted, the first results would disappear from the display before you had a chance to read them. Let’s change the program to print each variable on a separate line. 19 INPUT A: B=A+1 29 PRINT A change these lines 39 PRINT B + oo ee 49 END Before you enter the changes, go to the PRO mode. Since you were last in the RUN mode, you should only have to press the MODE key once. Then enter the new lines (20, 30, and 40) and LIST the new program to make sure that it is correct. The LIST command works only in the PRO mode. If you try it in any other mode, you’ll get an error code. Cr) (ene en wae -) When you are in the PRO mode, you can successfully type LIST, and press the ENTER key. ( 19: INPUT A:B=A+1 » Press the down arrow key [e] once. This scrolls the program down to the next line. ( 29:PRINT A ) Press FF] again. 39:PRINT B a 14. PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER Press fs] again. ‘@ 49: END ) If you press [3] again, nothing else will happen. That’s the end of the program. Now, press the MODE key until the computer is in the RUN mode (three times should do it). Type RUN and press ENTER. The program begins execution and the input prompt appears. Type 25 (your input). C = The value for A is printed by line 20. Press the ENTER key to go on. ( = The value for B is printed by line 30. Press the ENTER key to END the program. Cy ») The ready prompt appears. Now you know how the PRINT statement works. You might not want the com- puter to stop after it has printed something. Wouldn’t it be nice if it would just flash the answer on the display for a brief time and go right on to the next statement? Well, go back to the PRO mode once more and make these changes to your last program. 29 PAUSE A 39 PAUSE B POCKET VIEW OF THE POCKET COMPUTER 15 Once you have entered the changes, go back to the RUN mode. Run the pro- gram. After you’ve typed in your input (25) and pressed ENTER, watch the right end of the display closely. C 25. ) ON, then OFF a 26. ) ON, then OFF C > ) The program’s over. Did you see the computer display the 25 for almost a second, blink off, display the 26 for about a second, blink off, and then display the prompt (>)? PAUSE works the same way as PRINT, except execu- tion of the program is halted for 0.85 seconds. The com- puter then continues. So, if your eyes are quick enough, you can flash results on the display without stopping the computer. It merely makes a PAUSE before going on to the next statement. What would happen if you said PAUSE in line 20 and PRINT in line 30? Try it by changing these lines: 29 PAUSE A 39 PRINT B Once you’ve discovered what happens, write a program that will count from 1 through 10. Each time, have the computer flash the counting number, then display 2TT times the counting number, then stop to wait for you to press the ENTER key be- fore going on. Your Program 10 20 30 40 50 60 16 PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER First type NEW. In the DEF, RUN, OR PRO modes, NEW clears all pro- gram and data memories, but not reserve memories. In the RESERVE mode, it clears all reserve memories, but not program and data memories. Then enter your program. Go to the RUN mode to execute it. This is what the display shows: RUN ) ‘a As 2 Flashes C 6.283185397_ ) Stops Press ENTER. C 2 > Flashes 7 12.56637961 ) Stops Press ENTER. C 3. e Flashes cy 12.24955592 ) Stops Press ENTER. a 1g. ) Flashes C 62.02185397 ) Stops POCKET VIEW OF THE POCKET COMPUTER 17 Press ENTER. C > ) End of program Here’s how we wrote our program: 19 FOR A=1 TO 19 29 B=2*TIka 39 PAUSE A 49 PRINT B 59 NEXT A 69 END Your program may look entirely different; but if it does the job, it is OK. No two programmers do the same thing in the same way. If the program works, that’s all that’s necessary. Notice line 20 in our program. The Pocket Computer has a built-in value of 10-digit ac- curacy. The! key is in the top row of keys and must be “shifted.” " To enter, press SHIFT and then fl. Other than the key, this program looks just like a BASIC program on most other computers. Notice the accuracy displayed in the run. The TRS-80 Pocket Computer displays numbers to 10 digits. Of course, you knew that you were finding the circumference of a circle (B) with the given radius (A) in the last program. c=2Tr 18 PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER Most BASIC languages require the multiplication symbol (*) to be entered be- tween each of the values being multiplied. Do you think the previous program would work if you changed line 20 to: 20 B=2TIA without the multiplication operator sign? Change the line and try it. The Pocket Computer will do this as long as there’s no chance for misunderstanding. If you want to multiply two constants, such as 3 times 5, 35 will not work — it has to be 3*5; but 2A will work as well as 2*A since one value is a constant and one a variable. Let’s change the program slightly again. This time make both PRINT state- ments PAUSE. Also extend the upper limit of the FOR-NEXT loop to 100. 19 FOR A=1 TO 199 29 B=2* Thea 39 PAUSE A 49 PAUSE B 59 NEXT A 69 END Suppose that you want to see the program execute each step. Maybe you're not sure it is operating as it should. The Pocket Computer has a DEBUG command that al- lows you to watch the program execute line by line. The DEBUG command is only effective in the DEF and RUN modes. 1) Enter the program in the PRO mode. 2) Access the RUN mode. 3) Now DEBUG using the following steps: POCKET VIEW OF THE POCKET COMPUTER 19 You type DEBUG press | » press | press [+] press | , press | . press | » press | 5 Display Nv a) ‘ate w —] 6. 283185307 cs a) Nu 2 v w 2 12.56637961 > 2 AD Oa Oe etc. Stops Stops Flashes Stops Flashes Stops Stops Stops Flashes Stops Flashes Stops 20 PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER If the program had not been operating properly, you could have seen where the error was occurring. The line number is displayed for each step. The # key is pressed to go on to a new step in the program. Notice how the FOR-NEXT loop worked as the program went back to line 10 after line 50 had been executed. Do you wonder how much memory the program has used? You can find out. Remember, the Pocket Computer has memory for 1,424 program steps or data memo- ries. If you are familiar with Radio Shack’s Level II BASIC, you have probably used the MEM command. You can also use it with the Pocket Computer. The MEM command functions in all modes. It will dis- play the number of program steps and flexible memories not being used. So, regardless of the mode that you are in, type MEM and press the ENTER key. C 1386STEPS 173MEMORIES ) There were 1,424 program steps or 178 data memories available before the pro- gram was entered. The display now shows that there are 1,386 program steps or 173 data memories unused. Therefore, you have used 38 program steps or 5 flexible memo- ries, whichever way you want to look at it. You have also used two fixed memories (A and B). There is lots of room for much longer programs. Let’s get back to the last program. RUN the program this time instead of using DEBUG. Suppose the phone should ring or the coffee pot starts to boil over when you have it running. You can press the ON key ON . Press it only once, and the program will “break.” A message appears on the display to let you know where the program se- quence was broken, such as: ‘4 BREAK AT 29 ) De The program was stopped at line 20. Now you can go answer the phone or turn off the coffee pot. When you come back: a) If you’ve been gone less than seven minutes, type CONT, and the pro- gram will continue from the point where the break occurred. b) If you’ve been gone longer than seven minutes, the computer automati- cally turns off to save its batteries. When you press ON, the data memo- ries are cleared, but the program is still there. You'll have to RUN the program from the beginning again, however. POCKET VIEW OF THE POCKET COMPUTER 21 Summing Up Chapter One You now have some first-hand knowledge of the TRS-80 Pocket Computer. The fol- lowing table shows commands and statements used in this chapter. COMMANDS Command Accepted Remarks Used in Abbreviation These Modes CONT Cc. Restarts a program DEF or co. from the point at RUN CON. which interrupted. DEBUG D. Starts execution of DEF or DE. a program line by RUN DEB. line. Execute each DEBU. line by pressing. /¥ | LIST L Lists programs line PRO LI by line. Press LIS. to see next line. [2] to see previous line. MEM M. Shows remaining Any mode ME. unused memory. NEW Clears program and DEF, RUN, data memories. or PRO Clears reserve RESERVE memories. RUN R. Begins execution DEF or RU. of a program. RUN Note: Although it has not been mentioned, some commands and statements may be abbreviated when entered. They will be displayed here in their unabbreviated form, but accepted abbreviations, shown above, can be used. PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER STATEMENTS Statement Accepted Remarks Introduced Abbreviation END E. Indicates end of program. EN. FOR F. Starts a FOR-NEXT loop. FO. INPUT L Stops a program for data input. IN. 2 is displayed. INP. INPU. NEXT N. End of FOR-NEXT loop. NE. Increments the loop variable. NEX. PAUSE PA. Specified value is displayed for PAU. 0.85 seconds. PAUS. PRINT PB; Specified value is displayed. PR. Program execution is halted until PRI. the ENTER key is pressed. PRIN. Note: Periods in abbreviations must be included. Many facts were presented to you in this chapter. Here is a list of the most im- portant ones. * Twenty-four characters may be displayed at one time. ¢ Numeric variables are displayed with ten digits. * Memory consists of: 26 fixed memories, and 178 flexible memories, or 1,424 program steps. ¢ Numeric or string variables (up to seven characters) may be used. POCKET VIEW OF THE POCKET COMPUTER 23 « BASIC statements and commands are similar to BASICs of larger systems. ¢ Four modes of operation offer flexible use: (1) PROgram mode — for entering normal programs. (2) RUN mode - for executing programs. (3) DEF mode —- to execute programs defined by a label. (4) RESERVE mode - for writing functions for reserved keys. « Aspecial key, MODE is used to change the operation mode. * More than one statement may be placed on one program line. (oc= 234%) « Some keys have two functions. The SHIFT key is used to access the second func- tion. ¢ The ENTER key must be pressed at the end of each program line. ¢ Error codes are given for incorrect operations. ¢ The clear key CL clears an error message from the display. * The Pocket Computer stops operation each time a PRINT statement is executed. * Programs may be LISTed, one line at a time in the PRO mode. « A PAUSE statement works like a PRINT statement, except the program pauses for about a second to display the result and then goes on. ¢ The NEW command clears all program and data memories when used in the DEF, RUN, or PRO mode. It clears all reserve memories when used in the RESERVE mode. * A built-in 7 key is available with 10-digit accuracy. ¢ A DEBUG command can be used in the DEF and RUN modes to display the exe- cution of a program, one line at a time. ¢ The MEM command can be used in any mode to display the amount of unused memory. Chapter One Self-Test I: You used a program that calculated the circumference of a circle in this chapter. Expand that program so that it will also compute the area of the circle. HINT: A =11r? Modify your answer to Problem | so that the results will be displayed as in this example: a RADIUS = 3 5 Flash and go on C CIRCUMF. = 18.84955592 ) Print and stop AREA = 28.27433388 ) Print and stop HINT: Strings can be printed by enclosing characters to be printed in quotation marks (PRINT ''RADIUS ="). Now change the program so that you can also input the height and radius of a cyl- inder. Compute and print the total surface area of the cylinder along with pre- vious information. 24 PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER HINT: (S —+— Top of cylinder t -Height ~— Side of cylinder (flattened out) Circumference of top and bottom } ~+—— Bottom of cylinder Answers To Chapter One Self-Test These solutions are merely one way to solve the problem. Yours may differ. As long as yours produce the desired results, they may be considered correct. 1. From the program on page 17. 19 POR R =,1 TO 19 29 B = 2*T eR 39 PAUSE R 49 PRINT B 59 A =TT*ReR : or A= *R 2 6— PRINT A 7@ NEXT R 89 END Optional 2. We modified lines 30, 40, and 60. 39 PAUSE ''RADIUS ='';R 49 PRINT ''CIRCUMF. ='';B 69 PRINT ''AREA ='';A 3. Added lines have been inserted as needed. 19 FOR N= 1 TO 19 ~«——-—________. This could be any range. 13 INPUT ''R=?'';R 16 INPUT ''H=?'';H 29 B= 2*tT*R 39 PAUSE ''RADIUS ='';R 49 PRINT ''CIRCUMF. ='';B 5f A =TY*R*R 69 PRINT ''AREA ='';A 63 S = (2*A)+(B*H) 66 PRINT ''SURF. AREA ='';S 79 NEXT N 89 END pe) (ea) CHAPTER TWO Applications, Memory Use, and Definable Mode There have been many questions asked about the personal use of computers. One of the most frequently posed questions is, “What do you do with a computer after you get tired of playing computer games?” Well, computers have many practical functions too, ranging from keeping a shopping list for your family to running an inventory pro- gram for a business. Computers serve three functions: recording information, processing information, and transmitting information. The TRS-80 Pocket Computer adds new dimensions to these functions. In the past, the size of computers confined their use to a limited num- ber of places. A source of power was needed, and even the desk-top computer was heavy and cumbersome to carry around. But because the TRS-80 is portable, it is pos- sible to record information at any place and at any time, and to have any previously recorded information transmitted to you by the computer for immediate use. Like larger computers, the Pocket Computer has a memory that is nonvolatile, which means it can store information permanently. (Information from the Pocket Computer can also be stored a second way, on cassettes, which we will discuss in a later chapter.) The portability of the Pocket Computer also enables you to process information imme- diately, for instant review. In this chapter you will: ¢ learn about possible uses of the Pocket Computer; * learn more about how memory is used; * learn how to use the Pocket Computer in the DEFinable mode; and * apply some problem solving techniques to practical applications. 26 PROBLEM-SOLVING ON THE TRS-80 POCKET COMPUTER Application Examples Let your imagination run loose as you think of ways to use your Pocket Computer. We'll plant a few seeds to start your “garden of ideas.” Up-to-the-second stock market analysis Time payment schedules Horse-race handicapping Compound interest Shopping list Amortization Shopping price Annuities Letter reader and writer New games Short story reader 100 most misspelled words Speed-reading machine , : Poker game Cueforspeeches - Dice games Memo pad ESP games Record job time for accounts Record people contacted during Record medical data, such as aay temperature throughout day, Record reference tables foods eaten, heart rate, blood pressure, and activity level Record class activity for later dump to cassette tape (or printer) Compute food allotment given present weight, to aid in diet Arithmetic practice for weight gain or loss There are many others which will apply to your own needs. Pocket Computer Memory One of the things that any self-respecting computer can do is to store information. The TRS-80 Pocket Computer stores an amazing amount of information for its tiny size. And when