FOR THE ATARI COMPUTER TOM RUGG, PHIL FELDMAN, TIMOTHY BARRY ATARI USERS TAKE NOTE . If you like 32 BASIC Programs for the ATARI Computer, you will appreciate having the pro¬ grams on a disk or cassette which is ready to run on your ATARI 600XL, 800XL, 1200XL, 400 or 800 computer. The software has a “forever guarantee” (any problems, simply return the disk or cassette with $5 and we will send you a new one). Not only will it save your typing time, the software will save you time fretting about errors that are so easy to make. Interested? □ You bet I’m interested! Please send me the 5'/4" disk for my ATARI 600XL, 800XL, 400, or 800. □ You bet I’m interested! Please send me the cassette for my ATARI 600XLX, 800XL, 400, or 800. □ I’ve enclosed my check in the amount of $19.95. Please rush my ATARI software to the address below. □ Please charge my VISA_M/C_and send to the address below. Acct # _ Exp. Date _ Signature _ Name _ Address _ City, State, Zip _ (To expedite your order, call 1-800-547-1842 and charge your VISA or M/C) □ Please send me your catalog entitled Brain Food™. 32 BASIC Programs for the ATARI® Computer 32 BASIC Programs for the ATARI® Computer Tom Rugg, Phil Feldman, and Tim Barry 03 dilithium Press Beaverton, Oregon ®1984 by dilithium Press. All rights reserved. No part of this book may be reproduced in any form or by any means, electronic or mechanical, including photocopying, recording or by any information storage and retrieval system without permission in writing from the publisher, with the following exceptions: any mate¬ rial may be copied or transcribed for the nonprofit use of the pur¬ chaser, and material (not to exceed 300 words and one figure) may be quoted in published reviews of this book. Where necessary, permission is granted by the copyright owner for libraries and others registered with the Copyright Clearance Center (CCC) to photocopy any material herein for a base fee of $1.00 and an additional fee of $0.20 per page. Payments should be sent directly to the Copyright Clearance Center, 21 Congress Street, Salem, Mas¬ sachusetts 01970. 10 987654321 Library of Congress Cataloging in Publication Data Rugg, Tom. 32 BASIC programs for the ATARI computer. Bibliography: p. Includes index. 1. Atari 400 (Computer) —Programming. 2. Atari 800 (Com¬ puter-Programming. 3. Atari 1200XL (Computer) —Program¬ ming. 4. Basic (Computer program language) I. Feldman, Phil. II. Barry, Tim. III. Title IV. Title: Thirty-two BASIC programs for the ATARI computer. QA76.8.A8R84 1984 001.64'2 83-23164 ISBN 0-88056-084-3 Cover: Bruce Fleming Printed in the United States of America dilithium Press 8285 S.W. Nimbus Suite 151 Beaverton, Oregon 97005 Acknowledgements Our thanks to the following for their help and encouragement: Our wives and families, John Craig, Merl and Patti Miller, Nancy Mor- rice, and Sally Bowman. Special thanks to Ray Alen. vl Basic Programs for the Atari AN IMPORTANT NOTE The publisher and authors have made every effort to assure that the computer programs and programming information in this publication are accurate and complete. However, this publication is prepared for general readership, and neither the publisher nor the authors have any knowledge about or ability to control any third party’s use of the programs and programming information. There is no warranty or representation by either the publisher or the authors that the pro¬ grams or programming information in this book will enable the reader or user to achieve any particular result. Preface You have bought yourself an ATARI computer (or maybe you just have access to one at school or work). You will soon find that the most frequent question you are asked goes something like this: “Oh, you got a computer, eh? Uh... what are you going to do with it?” Your answer, of course, depends on your own particular situation. Maybe you got it for mathematical work, or for your business, or for home usage, or to enable you to learn more about computers. Maybe you got it for a teaching/leaming tool or for playing games. Even if you got the computer specifically for only one of these reasons, you should not neglect the others. The computer is such a powerful tool that it can be used in many different ways. If it is not being used for its “intended” function right now, why not make use of it in some other way? An ATARI computer is so small and portable that you can, say, take it home from work over the weekend and let the kids play educational games. They will have fun and learn a lot. After they go to bed, you can use it to help plan your personal finances. Or, you can let your guests at a party try to outsmart the ATARI (or each other) at some fascinating games. The possibilities go on and on. All these things can be done with an ATARI computer, but it cannot do any of them without the key ingredient-a computer pro¬ gram. People with little or no exposure to computers may be in for a surprise when they learn this. A computer without a program is like a car without a driver. It just sits there. viii Basic Programs for the Atari So you ask, “Where can I get some programs to do the things I want my computer to do?” Glad you asked. There are several alternatives. 1. Hire a computer programmer. If you have a big budget, this is the way to go. Good programmers are expensive and hard to find (and you will not know for sure if they’re really good until after the job is finished). Writing a couple of programs that are moderately complex will probably cost you more than you paid for the ATARI itself. 2. Learn to program yourself. This is a nice alternative, but it takes time. There are lots of programming books available - some are good, some not so good. You can take courses at local colleges. If you can afford the time and you have a fair amount of common sense and inner drive, this is a good solution. 3. Buy the programs you want. This is cheaper than hiring your own programmer because all the buyers share the cost of writing the programs. You still will not find it very cheap, especially if you want to accumulate several dozen programs. Each program might cost anywhere from a few dollars to several hundred dollars. The main problem is that you cannot be sure how good the programs are, and, since they are generalized for all possi¬ ble buyers, you may not be able to easily modify them to do exactly what you want. Also, they have to be written in a computer language that your computer understands. Even if you find a program written in the BASIC language, you will soon learn that the ATARI’S BASIC is not the same as other versions. Variations between versions of the same language typically re¬ sult in the program not working. This book gives you the chance to take the third alternative at the lowest possible cost. If you divide the cost of the book by the number of programs in it (use your computer if you like), you will find that the cost per program is amazingly low. Even if there are only a few programs in the book that will be useful to you, the cost is pretty hard to beat. Just as important is the fact that these programs are written specifi¬ cally for your ATARI. If you type them in exactly as shown, they will work! No changes are needed. In addition, we show you exactly what to change in order to make some simple modifications that may suit your taste or needs. Plus, if you have learned a little about BASIC, you can go even further and follow the suggestions about more extensive changes that can be made. This approach was used to try to Preface ix make every program useful to you, whether you are a total beginner or an old hand with computers. But enough of the sales pitch. Our main point is that we feel a computer is an incredibly flexible machine, and it is a shame to put it to only one or two limited uses and let it sit idle the rest of the time. We are giving you a pretty wide range of things to do with your ATARI, and we are really only scratching the surface. So open your eyes and your mind. Play a mental game against the computer (WARI, JOT). Evaluate your next financial decision (LOAN, DECIDE). Expand your vocabulary or improve your read¬ ing speed (VOCAB, TACHIST). Solve mathematical equations (DI- FFEQN, SIMEQN). But please, don’t leave your ATARI asleep in the comer too much. Give it some exercise. How to Use This Book Each chapter of this book presents a computer program that runs on an ATARI 400, 800, 600XL, 800XL, or 1200XL computer with ATARI BASIC. See Appendix 1 for details of the amount of memory required. Each chapter is made up of eight sections that serve the following functions: 1. Purpose: Explains what the program does and why you might want to use it. 2. How To Use It: Gives the details of what happens when you run the program. Explains your options and the meanings of any responses you might give. Provides details of any limitations of the program or errors that might occur. 3. Sample Run: Shows you what you will see on your screen when you run the program. 4. Program Listing: Provides a “listing” (or “print-out”) of the BASIC program. These are the instructions to the computer that you must provide so it will know what to do. You must type them in extremely carefully for correct results. 5. Easy Changes: Shows you some very simple changes you can make to the program to cause it to work differently, if you wish. You do not have to understand how to program to make these changes. 6. Main Routines: Explains the general logic of the program, in case you want to figure out how it works. Gives the BASIC line numbers and a brief explanation of what each major portion of the program accomplishes. 7. Main Variables: Explains what each of the key variables in the program is used for, in case you want to figure out how it works. xii Basic Programs for the Atari 8. Suggested Projects: Provides a few ideas for major changes you might want to make to the program. To try any of these, you will need to understand BASIC and use the information pro¬ vided in the previous two sections (Main Routines and Main Variables). To use any of these programs on your ATARI computer, you need only use the first four sections. The last four sections are there to give you supplementary information if you want to tinker with the program. RECOMMENDED PROCEDURE Here is our recommendation of how to try any of the programs in this book: 1. Read through the documentation that came with your ATARI computer to learn the fundamentals of communication with the computer. This will teach you how to turn the computer on, get into BASIC, enter a program, correct mistakes, run a pro¬ gram, etc. 2. Pick a chapter and read Section 1 (“Purpose”) to see if the program sounds interesting or useful to you. If not, move on to the next chapter until you find one that is. If you are a beginner you might want to try one of the short “Miscellaneous Pro¬ grams” first. 3. Read Sections 2 and 3 of the chapter (“How To Use It” and “Sample Run”) to learn the details of what the program does. 4. Enter the NEW command to eliminate any existing program that might already be in your ATARTs memory. Using Section 4 of the chapter (“Program Listing”), carefully enter the pro¬ gram into the ATARI. Be particularly careful to get all the punctuation characters right (i.e., commas, semicolons, colons, quotation marks, etc.). Certain typing errors will give you an immediate ERROR response. When this happens, re¬ enter the line with corrections. 5 .After the entire program is entered into the ATARI’S memory, use the LIST command to display what you have entered so you can double check for typographical errors, omitted lines, etc. Don’t mistake a semicolon for a colon, or an alphabetic I or O for a numeric 1 or 0 (zero). Take a minute to note the dif¬ ferences in these characters before you begin. 6. Before trying to RUN the program, use the CSAVE or SAVE command to save the program temporarily on cassette or disk. How to Use This Book xiii This could prevent a lot of wasted elfort in case something goes wrong (power failure, computer malfunction, severe typing errors, etc.). 7. Now RUN the program. Is the same thing happening that is shown in the Sample Run? If so, accept our congratulations and go on to step 9. If not, stay cool and go to step 8. 8. If you got an ERROR in a line, LIST that line and look at it closely. Something is not right. Referring to the error code in the error message, consult the ATARI documentation for an explanation. Keep in mind that the error might not be in the line that is pointed to by the error message. It is not unusual for the mistake to be in a line immediately preceding the error message line. Another pos¬ sibility is that one or more lines were omitted entirely. Usually the error will be in another line that deals with the same variable names that are used in the line with the error. In any event, fix the problem and go back to step 7. If there are no error messages, but the program is not doing the same thing as the Sample Run, there are two possibilities. First, maybe the program isn’t supposed to do exactly the same thing. Some of the programs are designed to do unpredictable things to avoid repetition (primarily the game programs and graphic displays). They should be doing the same types of things as the Sample Run, however. The second possibility is that you made a typing error that did not cause an error message to be displayed, but simply changed the meaning of one or more lines in the program. These are a little tricky to find, but you can usually narrow it down to the general area of the problem by noting the point at which the error takes place. Is the first thing displayed correct? If so, the error is probably after the PRINT statement that caused the first thing to be displayed. Look for the same types of things mentioned before. Make the corrections and go back to step 7. 9. Continue running the program, trying to duplicate the Sample Run. If you find a variation that cannot be accounted for in the “How To Use It” section of the chapter, go to step 8. Otherwise, if it seems to be running properly, CSAVE or SAVE the pro¬ gram on cassette or disk. 10. Read Section 5 of the chapter (“Easy Changes”). Try any of the changes that look interesting. If you think the changed version is better, CSAVE or SAVE it on cassette or disk, too. You will xiv Basic Programs for the Atari probably want to give it a slightly different title in the first REM statement to avoid future confusion. A NOTE ON THE PROGRAM LISTINGS A line on the screen of an ATARI computer is 38 characters wide, unless reset by a program to 40. The printer that was used to create the Program Listing section of each chapter prints lines up to 80 characters long. For best reproduction in this book, it was preferable that each published line be no longer than 48 characters. This com¬ bination of facts might cause you a little confusion when you are entering the programs into your ATARI. Here’s the way it works. Wherever there is a line in a program that is longer than 48 charac¬ ters, it has been divided into two or more lines that are each no more than 48 characters. You can recognize this easily because the second part has no line number at the left-hand side. This division is only for the purpose of printing the book. You should think of a divided line like this as one long line and enter it into your ATARI as a single line. Don’t be fooled by the fact that the cursor on your ATARI jumps down to the next line after you enter the 38th or 40th character — it’s just one long line until you press RETURN. IMPORTANT NOTE You can avoid all the problems of entering and debugging the programs in this book. How? All programs are available ready to run on both cassette and disk. You simply load the program and you’re ready to go. The order card in the front of the book has complete information on how to order these timesaving pre-entered programs. Contents How to Use This Book xi Section 1— -APPLICATIONS PROGRAMS Practical uses at home or work. BIORHYTH Can you predict your good and bad days? 3 CHCKBOOK Balance your checkbook. 11 DECIDE Choose the best decision from your list of alternatives. 21 LOAN Calculate payments and interest for mortgages, car loans, etc. 35 MILEAGE Analyze your car’s gasoline usage. 43 STOP Use your ATARI as a sophisticated stopwatch. 53 Section 2— -EDUCATIONAL PROGRAMS To help yourself or others to learn. MATH Math drills for school-aged children. 59 FLASH Create your own flashcards, then practice. 67 METRIC Learn the metric system. 73 NUMBERS Help pre-school children to learn numbers. 81 TACHIST Increase your reading speed with this tachistoscope. 87 VOCAB Expand your vocabulary. 95 xvl Basic Programs for the Atari Section 3- DECODE GROAN JOT OBSTACLE ROADRACE WARI Section 4- KALEIDO SPARKLE SQUARES WALLOONS Section 5- CURVE DIFFEQN GRAPH INTEGRAL SIMEON STATS GAME PROGRAMS Match wits with the computer or a friend. Figure out the computer’s secret code. 105 This dice game will make you do just that. 113 Challenge the computer to this word and logic game. 125 Play a friend at this arcade-like game. 135 Try to keep the car on the road. 143 Attempt to beat the computer at this ancient African skill game. 153 GRAPHICS DISPLAY PROGRAMS Dazzling visual diversions. A kaleidoscope with eight point symmetry. 165 Hypnotic sparkling patterns. 171 Overlaying concentric squares. 175 A classic from the ATARI Arena. 179 MATHEMATICS PROGRAMS For math, engineering, and statistical uses. Perform least-squares curve fitting. Solve ordinary differential 191 equations. Display a simple graph of a 203 function. Determine the area under 213 a curve. Solve simultaneous linear 223 equations. Perform standard “statistics of 231 population" calculations. 237 Contents xvii Section 6—MISCELLANEOUS PROGRAMS Short programs that do interesting things. BIRTHDAY What are the odds of identical birthdays in a group of people? 251 PI Calculate an approximation of pi. 255 POWERS Calculate powers of integers—up to 250 digits long. 261 PYTHAG Generate Pythagorean triplets (a 2 + = C 2). 267 Appendix 1 —MEMORY USAGE 271 Appendix 2—MICROSOFT BASIC FOR THE ATARI 272 Bibliography 273 Loading Instructions 276 xviii Basic Programs (or the Atari Section 1 Applications Programs INTRODUCTION TO APPLICATIONS PROGRAMS Good practical applications are certainly a prime use of personal computers. There are a myriad of ways an ATARI computer can help us to do useful work. Here are six programs for use around the home or business. Financial considerations are always important. LOAN will calcu¬ late interest, payment schedules, etc. for mortgages, car loans, or any such business loan. Do you ever have trouble balancing your checkbook(s)? CHCKBOOK will enable you to rectify your monthly statements and help you find the cause of any errors. Fuel usage is a constant concern for those of us who drive. MILEAGE will determine and keep track of a motor vehicle’s general operating efficiency. The ATARI is not limited to numerical applications. STOP turns your computer into a sophisticated stopwatch with a variety of uses. Often we are faced with difficult decisions. DECIDE transforms the ATARI into a trusty advisor. Help will be at hand for any decision involving the selection of one alternative from several choices. Before anything else, you might want to consult BIORHYTH each day. Some major airlines, and other industries, are placing credence on biorhythm theory. If you agree, or “just in case,” simply turn on your ATARI and run this program. 2 Basic Programs for the Atari BIORHYTH PURPOSE Did you ever have one of those days when nothing seemed to go right? All of us seem to have days when we are clumsy, feel de¬ pressed, or just cannot seem to force ourselves to concentrate as well as usual. Sometimes we know why this occurs. It may result from the onset of a cold or because of an argument with a relative. Sometimes, however, we find no such reason. Why can’t we perform up to par on some of those days when nothing is known to be wrong? Biorhythm theory says that all of us have cycles, beginning with the moment of birth, that influence our physical, emotional, and intellectual states. We will not go into a lot of detail about how biorhythm theory was developed (your local library probably has some books about this if you want to find out more), but we will summarize how it supposedly affects you. The physical cycle is twenty-three days long. For the first 11 Vi days, you are in the positive half of the cycle. This means you should have a feeling of physical well-being, strength, and endurance. Dur¬ ing the second 11 Vi days, you are in the negative half of the cycle. This results in less endurance and a tendency toward a general feeling of fatigue. The emotional cycle lasts for twenty-eight days. During the positive half (the first fourteen days), you should feel more cheerful, optimistic, and cooperative. During the negative half, you will tend to be more moody, pessimistic, and irritable. 4 Basic Programs for the Atari The third cycle is the intellectual cycle, which lasts for thirty-three days. The first half is a period in which you should have greater success in learning new material and pursuing creative, intellectual activities. During the second half, you are supposedly better off reviewing old material rather than attempting to learn difficult new concepts. The ups and downs of these cycles are relative to each individual. For example, if you are a very self-controlled, unemotional person to begin with, your emotional highs and lows may not be very notice¬ able. Similarly, your physical and intellectual fluctuations depend upon your physical condition and intellectual capacity. The day that any of these three cycles changes from the plus side to the minus side (or vice versa) is called a “critical day.” Biorhythm theory says that you are more accident-prone on critical days in your physical or emotional cycles. Critical days in the intellectual cycle aren’t considered as dangerous, but if they coincide with a critical day in one of the other cycles, the potential problem can increase. As you might expect, a triple critical day is one on which you are recommended to be especially careful. Please note that there is quite a bit of controversy about bio¬ rhythms. Most scientists feel that there is not nearly enough evidence to conclude that biorhythms can tell you anything meaningful. Others believe that biorhythm cycles exist, but that they are not as simple and inflexible as the 23, 28, and 33 day cycles mentioned here. Whether biorhythms are good, bad, true, false, or anything else is not our concern here. We are just presenting the idea to you as an interesting theory that you can investigate with the help of your ATARI computer. HOWTO USE IT The program first asks for the birth day of the person whose biorhythm cycles are to be charted. You provide the month and day as you might expect. For the year, you only need to enter the last two digits if it is between 1900 and 1999. Otherwise, enter all four digits. Next the program asks you for the start date for the biorhythm chart. Enter it in the same way. Of course, this date cannot be earlier than the birth date. After a delay of a few seconds, the program clears the screen and begins plotting the biorhythm chart, one day at a time. The left side of the screen displays the date, while the right side displays the chart. BIORHYTH 5 The left half of the chart is the “down” (negative) side of each cycle. The right half is the “up” (positive) side. The center line shows the critical days when you are at a zero point (neither positive nor negative). Each of the three curves is plotted with an identifying letter—P for physical, E for emotional, and I for intellectual. When the curves cross, an asterisk is displayed instead of either of the two (or three) letters. Eighteen days of the chart are displayed on one screen, and then the program waits for you to press a key. If you press the E key, the program ends. If you press the SPACE key (or almost any other key except BREAK or SHIFT), the program clears the screen and dis¬ plays the next eighteen days of the chart. The program will allow you to enter dates from the year 100 A.D. and on. We make no guarantees about any extreme future dates, however, such as entering a year greater than 3000. SAMPLE RUN ENTER 5 TART DATE FOR CHART ''EAR ’34 i ? 3 4 A 5 5 U ME D MONTH Cl TO 12) 7 3 DAY Cl TO 31) 7 2 lB The operator enters his or her birth date and the date for the beginning of the chart. 6 Basic Programs for the Atari The program responds with the first 18 days of the operator’s biorhythm chart, then waits for a key to be pressed. PROGRAM LISTING 10 REM BIOEHYTH 15 REM COPYRIGHT 1984 DILITHIUM PRESS 20 POKE 82,0 30 DIM U*(40) , X (12) , C* (40) , L.*<40) , R* (40) , A* (40) , X1 $(40) ,X2*(40),X3$< 40),W*(40) 40 FOR J=1 TO 12:READ A:X=A:NEXT J 50 DATA 31,28,31,30,31,30,31,31,30,31,30,31 60 L=0: Z=0. 99999: T= 14: F'=3. 14159265 70 FOR J=1 TO 29:U*(J,J)=CHR*<18):W*(J,J)=CHR*(3 2):NEXT J BO U$(15,15)=CHR$(23):W»<15,15)=CHR*(124) 90 GRAPHICS 0:PRINT " BIORHYTHM":PRINT ■.PRINT 100 PRINT " ENTER BIRTHDATE" 110 GOSUB 240:,JB=JD 120 PRINT :PRINT " ENTER START DATE FOR CHART" 130 GOSUB 240:JC=JD:IF JC>=JB THEN 150 140 PRINT " CHART DATE CAN’T BE EARLIER THAN BI RTHDATE - TRY AGAIN" 145 GOTO 100 150 POKE 752,1:FOR J=1 TO 1000:NEXT J 160 GOSUB 330 BIORHYTH 7 170 N=J C—J B:V=23:GOSUB 350:V=28:GOSUB 350:V=33:G QSUB 350 1 £30 GOSUB 490:L=L+1:POKE 752,1: PR I NT C*: POSITION 10,L+3:PRINT l_*:JC=JC+1 190 IF L< 18 THEN 170 200 POKE 764,255:PRINT :PRI NT “ PRESS ’E’ TO EN D OR SPACE TO CONTINUE"; 210 A=PEEK(764):IF A=255 THEN 210 220 IF A”42 THEN POKE 764,255:POKE 752,0:POKE 82 ,2:END 230 L=0:GOTO 160 240 X(2)=28:PRINT :PRINT " YEAR INPUT Y:Y=IN T (Y) 245 IF Y<0 THEN GOSUB 540:GOTO 240 250 IF (Y/100)-(INT(Y/100))< >0 THEN 270 260 IF (Y/400) — 0 THEN 280 270 IF (Y/4) — (INT X(M) THEN GOSUB 540:GOTO 290 300 W=INT( :B=367*(M-2-W#12)/12 310 IF B<0 THEN B=B+Z 320 B=INT 23 THEN 390 370 L*=W* 380 IF V=23 THEN C*="P" 390 IF 0=28 THEN C*="E" 400 IF V=33 THEN C*="I" 410 W=R/V:W=W*2*P:W=T*SIN(W>:W=W+T+1.5:W=INT(W): A*=L* 420 IF A*="P" OR A*="E” OR A*="#" THEN C*="*” 430 IF W=1 THEN 470 440 IF W=T+T +1 THEN 480 450 X 1*=L$ (1, W-l) : X2*=L* (LEN(L*>- (T+T-W) , LEN (L.*> >:C=LEN:X1=LEN:X2=LEN(X2*> 460 L* C1, X 1) =X 1 *: L.* (X1 +1, X1+C) =C*: L* (X 1+C+1 , X H-C +X2)= X2*:RETURN 470 X1*=L*(LEN-):C=LEN(C*):XI =L.EN (XI*): L* (1,0 =C*: L* :X1=LEN(XI*):C=LEN(C*>:L*(1,XI) =X1*:L*(X1+1,X1+C)=C*:RETURN 8 Basic Programs for the Atari 490 W=JC+68569:R=INT<4*W/ 146097) :W=W-INT< <1.46097 *R+3)/4):Y=INT(4000*< W+1)/1461001)!W=W~INT <1461 * Y/4)+31 500 M=INT <80*W/2447):D=W-INT<2447*M/80):W=INT(M/ 11):M=M+2~12*W:Y=100*(R-49)+Y+W 510 X1*=STR$:X1=LEN(X 1*) : X2=L.EN(X2$) :X3=LEN(X3*> 520 C*<1, Xl>=Xl*iC* 30 GRAPHICS 0 40 PRINT "CHECKBOOK ANALYZER" 50 PRINT :PRINT 60 MC—20:MD=10:MS-10:MR=10 70 L*="NO MORE ROOM" 80 DIM C(MC),D(MD),S(MS>„ R (MR) 90 TOO: TD=TC: TS=TC: TR=TC: ND=TC: NC=TC: NS=TC: NR=T C 1O0 E*="ERROR. RE-ENTER, PLEASE." 110 PRINT "FIRST, COMPARE THE BANK STATEMENT" 120 PRINT "WITH YOUR CHECKBOOK.":PRINT 130 PRINT "MAKE SURE THE STATEMENT AND THE " 140 PRINT "CHECKBOOK SHOW THE SAME FIGURES" 150 PRINT "FOR EACH CHECK AND DEPOSIT." 160 PRINT :PRINT "MAKE A MARK IN THE CHECKBOOK N EXT TO-" 170 PRINT "EACH CHECK AND DEPOSIT LISTED" 180 PRINT "ON THE STATEMENT." 190 PRINT :PRINT "WHAT’S THE ENDING BALANCE SHOW N" 16 Basic Programs for the Atari 200 PRINT "ON THE STATEMENT? INPUT SB 210 PRINT :PRINT "NOW FIND THE MOST RECENT CHECK THAT" 220 PRINT "IS SHOWN ON THE BANK STATEMENT." 230 PRINT 240 PRINT "WHAT IS THE CHECK NUMBER OF" 250 PRINT "THIS CHECK? "-.INPUT L.C 260 IF LC=INT(LC) THEN 290 270 PRINT "NO, NOT THE AMOUNT OF THE CHECK." 2630 GOTO 210 290 PRINT 300 PRINT "WHAT BALANCE DOES YOUR CHECKBOOK" 310 PRINT "SHOW AFTER CHECK NO. ”;LC 320 INPUT CB 330 PRINT :PRINT 340 PRINT "ENTER THE AMOUNT OF EACH DEPOSIT" 350 PRINT "THAT IS SHOWN IN YOUR CHECKBOOK" 360 PRINT "PRIOR TO CHECK NO. ";LC 370 PRINT "BUT IS NOT ON THE STATEMENT." 3630 A*="WHEN NO MORE, PRESS THE RETURN KEY": PR IN T :PRINT A* 390 INPUT RT 400 IF L.EN CRT) =0 THEN 460 410 IF VAL(RT)>0 THEN 430 420 PRINT :PRINT ETsGOTO 380 430 ND=ND+1s D < ND > =VAL < R* > s TD=TD+D(ND > 440 IF ND >0 THEN 580 560 PRINT -.PRINT ET 570 GOTO 520 5630 NC=NC+1:C(NC)=VAL(R») :TC=TC+C 590 IF NC0 THEN 680 670 PRINT sPRINT ETsGOTO 630 680 NS=NS+1 s 5 (NS)-VALCRT)sTS=TS+S(NS> CHCKBOOK 17 £.90 IF NSC MS THEN 640 700 PRINT : PRINT L$ 710 PRINT :PRINT "TOTAL = ";TS:PRINT 720 60SUB 1210 730 W=SB+TD+TS-CB—TC-TR:W=ABS(W ) 740 IF WC1.0E-03 THEN W=0 750 IF WOO THEN 780 760 PRINT "CONGRATULATIONS! IT BALANCES." 770 GOTO 790 780 PRINT :PRINT "SORRY, IT’S OUT OF BALANCE. 790 PRINT BOO 810 BIS 820 B30 835 B40 850 PRINT "STATEMENT BALANCE + " PRINT "DEPOSITS OUTSTANDING +" PRINT "SERVICE CHARGES = ”;SB+TD+TS PRINT PRINT "CHECKBOOK BALANCE +" PRINT "CHECKS OUTSTANDING + " PRINT "RECENT DEPOSITS = ";CB+TC+TR PRINT B60 PRINT "DIFFERENCE = ";W 870 PRINT :PRINT "PRESS ANY KEY TO CONTINUE": BE #1:OPEN #1,4,0,"K:" 8B0 GET # 1, R B90 PRINT II CLO 900 PRINT "NEXT ACTION:" 910 PRINT "1 - LIST CHECKS OUTSTANDING" 920 PRINT "2 -- LIST DEPOSITS OUTSTANDING" 930 PRINT "3 - LIST SERVICE CHARGES" 940 PRINT "4 - START OVER" 950 PRINT "5 - END PROGRAM" 960 PRINT "6 - DISPLAY BALANCING INFO" 970 PRINT "7 - LIST DEPOSITS AFTER LAST CHECK" 980 GET #1,R:IF R<49 OR R>56 THEN 980 990 R=VAL> 1000 IF R<1 OR R>7 THEN 1020 1010 ON R GOTO 1030,1070,1110,1150,1160,790,1170 1020 PRINT :PRINT E$:GOTO 890 1030 PRINT -.PRINT "CHECKS OUTSTANDING" 1040 FOR J=1 TO NC 1050 PRINT C 1270 IF LEN0 THEN 1300 1290 PRINT :PRINT ESiGOTO 1250 1300 NR-NR+1: R (NR) =VAL. (R *) : TR=TR+R (NR) 1310 IF NR"; 330 INPUT R* 335 IF R*="" THEN 200 340 IF R*<>"1" AND R*<>"2" AND R*< >"3" THEN ? :? "** ILLEGAL ENTRY **":? sGOTO 320 345 T=VAL(R*) 350 GOSUB 2000 400 ON T GOTO 410,440,470 410 PRINT "WHAT TYPE OF ITEM MUST YOU DECIDE IJF'O N " 420 INPUT T*sIF T*="" THEN 410 430 GOTO 500 440 T*="COURSE OF ACTION":GOTO 500 470 T$="’YES’ OR ’NO’" 4830 NI=2sL*(l, 12) ="DECIDING YES" s L* (51,61) ="DECI DING NO":LX <1)=12:LX(2) = 11 490 GOTO 750 500 GOSUB 2000sN1=0 510 PRINT " I NEED TO HAVE A LIST OF EACH” 520 PRINT T$;" UNDER CONSIDERAT ION.":PRINT 530 PRINT " INPUT THEM ONE AT A TIME" 540 PRINT "IN RESPONSE TO EACH QUESTION MARK.":P RINT 550 PRINT " THE ORDER IN WHICH YOU INPUT THEM" 28 Basic Programs for the Atari 560 PRINT "HAS NO PARTICULAR SIGNIFICANCE.":PRIN T 570 PRINT " TYPE THE WORD ’";E*;"' TO INDICATE II 580 PRINT "THAT THE WHOLE LIST HAS BEEN ENTERED. PRINT 590 NI=NI+1:INPUT X*:IF X*=E* THEN 610 592 IF X*="" THEN NI=NI-1: PRINT : PRINT "** ILLEIS AL ENTRY **":PRINT -.GOTO 590 594 IF LEN35 THEN PRINT "* YOUR ITEM IS TOO LONG. RE-ENTER IT. *“:FOR K=1 TO 1000:NEXT K:NI =N I — 1:GOTO 590 595 L*< *50+LEN >=X*:LX =2 THEN 650 630 PRINT :PRINT "# YOU MUST HAVE AT LEAST 2 CHO ICES t ":PRINT 640 PRINT "** TRY AGAIN **":GOSUB 2100:GOTO 500 650 GOSUB 2000:PRINT "HERE'S THE LIST YOU'VE GIV EIM ME": PRINT 660 FOR J=1 TO NI:PRINT J;". ";L*<*50+1, 685 IF R*="" THEN PRINT :PRINT "** ILLEGAL ENTRY **":PRINT : GOTO 6(30 690 IF R*<1,1>="Y" THEN 750 695 IF R$(1,1)< >"N" THEN 670 700 PRINT "** THE LIST MUST BE RE-ENTERED **" 710 GOSUB 2100:GOTO 500 750 GOSUB 2000 760 PRINT " NOW, THINK OF THE DIFFERENT FACTOR S " 770 IF T<3 THEN PRINT "THAT ARE IMPORTANT TO YOU IN CHOOSING" 780 IF T<3 THEN PRINT "THE BEST ";T*;"." 790 IF T—3 THEN PRINT "THAT ARE IMPORTANT TO YOU IN DECIDING"-.PRINT "YES OR NO" 800 PRINT :PRINT " INPUT THEM ONE AT A TIME IN RESPONSE" 810 PRINT "TO EACH QUESTION MARK.":PRINT (320 PRINT " TYPE THE WORD '";E*;"’ TO TERMINAT E" 830 PRINT "THE LIST.":PRINT : NF=0 (340 NF=NF+1 : INPUT X$: IF X*=E$ THEN S6C B42 IF X*="" THEN PRINT "** ILLEGAL ENTRY **":NF =NF-1:GOTO 840 845 IF LEN(X*>>35 THEN PRINT :PRINT "* YOUR ITEM IS TOO LONG. RE-ENTER IT.":PRINT :FOR K=1 TO 10 00:NEXT K:NF=NF-1 DECIDE 29 850 F*< = LEN(X$) 855 GOTO 840 860 NF=NF-1:PRINT 870 IF NF<1 THEN PRINT "YOU MUST HAVE AT LEAST 0 NE! - REDO IT" 880 IF NF<1 THEN GOSUB 2100:GOTO 750 890 GOSUB 2000:PRINT "HERE’S THE LIST OF FACTORS YOU GAVE ME:":PRINT 900 FOR J=1 TO NF:PRINT J;". ";F*(*50+1, NF THEN 890 1000 GOSUB 2000:IF NF=1 THEN 1200 1010 PRINT " NOW LET’S SUPPOSE WE HAVE A SCALE OF" 1020 PRINT "IMPORTANCE RANGING FROM 0-10.":PRINT 1030 PRINT " WE’LL GIVE ”;F$ <(A—1> *50+1, (A-l)* 50+FX(A))s" A" 1040 PRINT "VALUE OF 10 SINCE ":F$((A-l>*50+1,(A -1)*50+FX(A)) 1050 PRINT "WAS RATED THE MOST IMPORTANT.":PR I NT 1060 PRINT " ON THIS SCALE, WHAT VALUE OF" 1070 PRINT "IMPORTANCE WOULD THE OTHER FACTORS H AVE?" 1080 FOR J = 1 TO NF:Q=A:IF J=Q THEN 1110 1090 PRINT :PR I NT F*((J-l)*50+1, (J-l)*50+FX(J)) : INPUT X X : V (J) = X X 1100 IF V(J)<0 OR V(J>>10 THEN PRINT :PRINT "** IMPOSSIBLE VALUE - TRY AGAIN **":PRINT :GOTO 109 0 1110 NEXT J 1200 V (A) =10: Q=0: FOR J = 1 TO NF: Q=Q+V (J ) : NEXT J:F OR J=1 TO NF 1210 V(J)=V(J)/Q:NEXT J:GOSUB 2000 1220 IF T<>3 THEN PRINT " EACH " ; T* 1230 IF T=3 THEN PRINT " DECIDING YES OR NO" 1240 PRINT "MUST NOW BE COMPARED WITH RESPECT TO II 1250 PRINT "EACH IMPORTANCE FACTOR." 1260 PRINT " WE’LL CONSIDER EACH FACTOR" 1270 PRINT "SEPARATELY AND THEN RATE" 1280 IF T< >3 THEN PRINT "EACH ";T$;" IN TERMS” 1290 IF T=3 THEN PRINT "DECIDING YES OR DECIDING NO IN TERMS" 30 Basic Programs (or the Atari 1300 PRINT "OF THAT FACTOR ONLY." 1310 PRINT " LET’S GIVE ";L$(1,LX(1)) 1320 PRINT "A VALUE OF 10 ON EVERY SCALE.PRINT 1330 IF T< >3 THEN PRINT " THEN EVERY OTHER ";T * 1340 IF T=3 THEN PRINT " THEN DECIDING NO" 1350 PRINT "WILL BE ASSIGNED A VALUE HIGHER OR" 1360 PRINT "LOWER THAN 10. THIS VALUE DEPENDS 0 N” 1370 PRINT "HOW MUCH YOU THINK IT IS BETTER OR" 1380 PRINT "WORSE THAN ";L*(1,LX(1PRINT 1390 FOR 3 = 1 TO .NF 1400 GOSIJB 2050: PR I NT 1410 PRINT " CONSIDERING ONLY ":F*((J-l)#50+1, (J-l)*50+FX(J));" AND" 1420 PRINT "ASSIGNING 10 TO ";L$(1,LX(1));" 1430 PRINT "WHAT VALUE WOULD YOU ASSIGN TO" 1440 PRINT :FOR K=2 TO NI 1450 PRINT L*((K-1)*50+1,(K-1)450+LX;:INPUT X X:C(K,J)= X X:PRINT :IF C(K,J>>=0 THEN 1470 1460 PRINT " ••• NEGATIVE VALUES NOT LEGAL": GOTO 1 450 1470 NEXT K:PRINT :C(1,J)=10:NEXT J 1500 FDR J=1 TO NF:0=0:FOR K=1 TO NI 1510 Q=Q+C (K, J) : NEXT K: FOR K=1 TO NI 1520 C(K,J)=C(K,J)/Q:NEXT K:NEXT J 1530 FOR K=1 TO NI:DMX THEN MX=D(K> 1570 NEXT K:FOR K=1 TO NI:D=D* 100/MX:NEXT K 1600 FOR K=1 TO NI:Z=K:NEXT K:NM=NI-1 1610 FOR K-l TO NI:FOR J = 1 TO NM:N1=Z(J>:N2=Z(J+ 1 ) : IF D D :J2=Z(2):DF=D(J1>-D(J2 ):GOSUB 2000 1700 PRINT L4((31 — 1)#50+1, (31—1)S50+LX<3i>/; 1710 PRINT " COMES OUT BEST" 1720 IF DF<5 THEN PRINT "BUT IT’S VERY CLOSE.":G OTO 1800 1730 IF DF<10 THEN FRINT "BUT IT’S FAIRLY CLOSE. " : GOTO 1.800 1740 IF DF<20 THEN PRINT "BY A FAIR AMOUNT.":GOT 0 1800 1750 PRINT "QUITE DECISIVELY." 1BOO PRINT :PRINT "HERE IS THE FINAL LIST IN ORD ER.":PRINT 1810 PRINT L*<(31-1)*50+1,(31-1)*50+LX(31));" HA S BEEN" 16320 PRINT "GIVEN A VALUE OF 100 AND THE OTHERS" DECIDE 31 1830 PRINT "RATEiD ACCORDINGLY.":PRINT 1E340 GOSUB 2050: PRINT 1845 X$~" "s REM 37 BLANK SPACES 18350 FOR J = 1 TO NI: Q=Z (J ): PRINT D (0) POSITION 1 6,PEEK(84) 1 £355 PRINT L* ( (Q-l) *50+1, . 6 0 ~ r - " 7 =4 65 3. 7 I 46 22 7 JL 7 7 HI 7 65 3 5 0 5 2 3 1 3 i 7 0 5 =- — 65 3 2 3 5 3 4 1 -~t -* I 5 6 5 5 6 5 3, 0 7 6 6 0 0 1-5 7 7 4 3 5 6 5 3 3 5 7 2 5 4 51 7 4 1 Z 2 5 5 5 . 6 3 ’?! ■ •5 4 7 1 5 . 3 5 6 5 3 , 4 0 3 5 ’6 M 4 1^7 13 6 5 3 13 3 2 34 7 7 l 7 ]_ 6 5 7 3 5 3 3 3 2 1^ 145 i 3 6 5 7 “? " 10 5 4 3 5 ? TOTAL: F 0 9 HE « OR ■ screen! The program responds with information about the first 16 months of the loan, then waits. 567500 FOR 360 MONTHS AT 11.75/. CALCULATING TOTAL5... LAST PAYMENT = $ 654.66 TOTAL PAYHENT 5 = $ 245262.90 MONTHLY PAYMENT = $ 681.36 PRESS ANY KEY TO CONTINUE The operator presses “T”, and after a few seconds the program displays totalling information about the loan. LOAN 39 PROGRAM LISTING 10 REM LOAN 15 REM COPYRIGHT 1904 DILITHIUM PRESS 20 OPEN #1,4,0,"K:":POKE 82,0 30 DIM BL*'<12> ,R*(12) ,B*(12> ,T*(12> ,TT*<12> , S* (1 2),D$(12>,X*(12> 40 GRAPHICS 0 45 BL*=" ":REM 12 SPACES 50 PRINT BL*(1,8);"LOAN CALCULATOR" 60 PRINT :PRINT :PRINT 70 PRINT " LOAN AMOUNT INPUT A BO GOSUB 750:IF A=0 THEN 70 90 PRINT SPRINT " INTEREST RATE INPUT R 1O0 PRINT :F'RINT " LENGTH OF LOAN (MONTHS) "; : IN PUT N 110 R=ABS(R):N=INT(N> sM=R/1200:PRINT 120 GOSUB 630 130 W=<1+M)"N 140 P=A*M*W/(W-l) 150 P=INT(P*100+0.99):P=P/100 160 W=P*100:GOSUB 900:PRINT " MONTHLY PAYMENT IS : S* 170 R*=... sPRINT 180 PRINT " NEXT ACTION:" 190 PRINT 200 PRINT " 1 - SHOW MONTHLY ANALYSIS" 210 PRINT " 2 - OVERRIDE MONTHLY PAYMENT" 220 PRINT " 3 - START OVER" 230 PRINT " 4 - END" 240 PRINT :INPUT C 250 IF C< 1 OR CM THEN 270 260 ON C GOTO 320,300,40,290 270 PRINT " CHOICES ARE 1, 2, 3, AND 4" 2810 GOTO 180 290 POKE 82,2:END 300 PRINT sPRINT " MONTHLY PAYMENT INPUT F' 310 GOTO 170 320 GOSUB 330:GOTO 370 330 GOSUB 630:IF R$=”T" THEN RETURN 340 POSITION 7,PEEK(84)sPRINT "REMAINING"; 345 POSITION 21,PEEK(84)sPRINT "-INTEREST- , „„ II 350 PRINT "MONTH BALANCE"; 355 POSITION 22,PEEK(84)sPRINT "MONTH TQ-DAT E" 360 RETURN 370 B=A*100:TT=0:TP=TT: L=TT:P=P*100:R*="" 380 FOR J=1 TO N 390 T=M*B 400 T=INT(T+0.5) 410 IF J=N THEN P=B+T 40 Basic Programs for the Atari 420 TP=TP+P: B==B-P+T: TT=TT+T 430 IF B<0 THEN GOSUB 790 440 IF T" THEN 560 450 W=B:GOSUB 670:B*=S* 460 W=T:GOSUB 670:T*=S* 470 W=TT:GOSUB 670:TT*=S* 4S0 PRINT " J; : POSITION 6,PEEK(04) 482 PRINT B*; :POSITION 17,PEEK(84) 404 PRINT Tit; : POSITION 29, PEEK (84) 486 PRINT TTS 490 IF B=0 THEN J=N:GOTO 510 500 L=L+1:IF L<16 THEN 560 510 PRINT :PR INT " PRESS ’T’ FOR TOTALS, OR" 520 PRINT " ANY OTHER KEY FOR NEXT SCREEN"; 530 GET #1,XX 540 IF XX=84 THEN R$="T" 550 L-OsGOSUB 330:IF Rt="T" THEN PRINT :PRINT " CALCULATING TOTALS..." 560 NEXT ,J 570 W=P:GOSUB 900:PRINT :PRINT :PRINT " LAST PAY MENT •= $ " ; ST 580 W=TP:GOSUB 900:PRINT :PRINT " TOTAL PAYMENTS = it" ; Sit 590 W=FP*100:GOSUB 900:PRINT :PRINT " MONTHLY FA YMENT <= it" ; Sit: PR I NT 600 PRINT :PRINT " PRESS ANY KEY TO CONTINUE" 610 GET #1,ZX 620 P=FP:GOTO 170 630 PRINT CHR't (125) 640 W=R* 100: GOSUB 900:PRINT " fsA;" FOR ";N;" M ONTHS AT " ; Sit; "7.” 650 PRINT 660 RETURN 670 W=INT(W> 6£30 X*==S*:S*="":S*=STR*(W/100> : IF < INT (W/100) ) * 1 00=W THEN Sit (L.EN (S$) +1) * " . 00 " : GOTO 690 685 IF (INT(W/10)>*10=W THEN S*(LEN(S$)+1)-“O" 690 K=LEN (Sit) : IF K>9 THEN RETURN 700 X*=S*:S*= M ":S*(1,10-K)=BL*(1,10-K):S«(LEN(S* >+l, 10) =Xit: RETURN 710 D*(l, D-" . ":D*(2,3)=S*(K-2,K) 720 Xit-=sit: Sit ( 1, LEN (Xit (1, K-2) ) ) =X$ (1, K-2) : S't (LEN ( X*+l,LEN >+LEN)=D* 730 Xit:=Sit: Sit (1, lO)=BL$ (1, 10) : Sit (10,9+LEN (Xit) ) =Xit 740 RETURN 750 A=ABS(A):A=INT(A) 760 IF A<1000000 THEN RETURN 770 PRINT "**** TDD LARGE ****" 780 A=0:RETURN 790 P=P <-B: TP =TP+B: B=0 BOO RETURN LOAN 41 900 W= INT *10=W THEN £5* (LEN 0 THEN PRINT “MONTHLY PAYMENT = ”;FP MAIN ROUTINES 10-100 Defines arrays. Displays title. Gets loan information. 110-160 Calculates and displays monthly payment. 170-280 Asks for next action. Goes to corresponding routine. 300-310 Gets override for monthly payment. 320-620 Calculates and displays monthly analysis. 630-660 Subroutine to clear screen and display data about the loan at the top. 670-740 Subroutine to convert integer amount to fixed-length string with aligned decimal point. 750-780 Edits loan amount (size and whole dollar). 790-800 Subroutine to handle early payoff of loan. 900-930 Subroutine to convert integer amount to a string with two decimal places. MAIN VARIABLES A Amount of loan. R Interest rate (percentage). N Length of loan (number of months). M Monthly interest rate (not percentage). W Work variable. P Monthly payment (times 100). FP First monthly payment. C Choice of next action. 42 Basic Programs (or the Atari B Remaining balance of loan (times 100). TT Total interest to date (times 100). TP Total payments to date. L Number of lines of data on screen. R$ Reply from operator at keyboard. J Work variable for loops. T Monthly interest. B$ String format of B. T$ String format of T. TT$ String format of TT. S$,X Work strings. D$, BL$ Work strings. K, XX, ZX Work variables. SUGGESTED PROJECTS 1. Display a more comprehensive analysis of the loan along with the final totals. Show the ratio of total payments to the amount of the loan (TP divided by A), for example. 2. Modify the program to show an analysis of resulting monthly payments for a range of interest rates and/or loan lengths near those provided by the operator. For example, if an interest rate of 9.5 percent was entered, display the monthly payments for 8.5, 9.0, 9.5, 10.0, and 10.5 percent. MILEAGE PURPOSE For many of us, automobile operating efficiency is a continual concern. This program can help by keeping track of gasoline con¬ sumption, miles driven, and fuel mileage for a motor vehicle. DATA statements are used to hold the vehicle’s “data file.” Thus a master file can be retained and updated by merely resaving the program after adding new information. The program computes mileage (miles per gallon or MPG) obtained after each gasoline fill-up. A running log of all information is maintained, allowing trends in vehicle operating efficiency to be easily checked. HOW TO USE IT Before running the program, you must enter a chronological his¬ tory of your vehicle’s gasoline consumption. This is accomplished by the use of DATA statements beginning at line 1000. For each gas¬ oline fill-up, a record of the date, odometer reading, and number of gallons purchased is needed. The form of each DATA statement should be: line number DATA date, odometer value, number of gallons. Some comments are in order here: the line number of each state¬ ment should increase as the information becomes more recent. We recommend starting with line number 1000 and incrementing each new line by five or ten. This allows later insertion to correct mistakes or to add previously missing data. The word DATA must be typed exactly as is. 44 Basic Programs for the Atari The remainder of each DATA line contains the three pieces of information needed by the program. They must be separated by commas. The first item is the date of the gasoline fill-up. It can be comprised of any keyboard characters but should not contain com¬ mas, colons, or quotation marks. Only the leftmost eight characters will be used if more than eight are entered. We recommend that you use the general form typified by 12/25/83. However, you might want to use other notations, such as JAN 23, or WEEK 5 or something else. A comma must be typed after the date. The odometer reading and number of gallons purchased are then entered as numeric values separated by a comma. (See the Sample Run for an example of typical data entry.) If you do not know part of the information for a particular DATA line you can do one of two things: make your best guess for the unknown item(s) or leave the entry for the unknown item(s) null. You can leave a null entry as follows: for an unknown date, place a comma as the first nonblank character after DATA; for an unknown odometer reading, or gallons value, simply enter zero. The program will recognize these special input forms. However, in all cases, each DATA statement that you enter must contain precisely two commas. Once your data is entered, you can retain it by saving the program on cassette tape or disk. Then, as new data becomes available, you can load the old program, add the new data to it, and save the program again to preserve the entire data file. Having entered the appropriate data, you are ready to run the program. It operates from a central command mode. The operator branches to one of three available subroutines. When a subroutine completes execution, control returns to the command mode for addi¬ tional requests. A brief description of each subroutine now follows. Verify DATA Statements This scans the DATA statements to look for possible problems with the data. It will test to see if any odometer values are too big or too small (they are presumed to be between 0 and 999999), or if any gallons values are too big or too small (they are presumed to be between 0 and 9999). It will look to see that the odometer values increase with each successive entry. Also, it will make sure that you have entered some data. If any of these problems are found, an appropriate error message will be displayed. If a bad data record is found (usually more or less than three items on a DATA line, or perhaps a string value for one of the numeric quantities), the program MILEAGE 45 will display an error message and terminate. If this happens, check all your DATA statements to be sure they are in the correct format. If all data is in the correct form the subroutine will display the begin¬ ning and ending dates for the data and the total number of data records found. It will then ask that you hit any key to re-enter the command mode. Display Mileage information This subroutine computes mileage (miles per gallon) from the available data. It formats all information and displays it in tabular form. Numbers are rounded to the nearest tenth so that four columns of information can be displayed on one line. When data fills the screen, the user is prompted to hit any key to continue the listing. When all data is displayed, pressing any key will re-enter command mode. An error message will be printed and the program will termi¬ nate if a fatal error is found in the DATA records. Terminate Program This subroutine ends program execution and returns the computer to direct BASIC. SAMPLE RUN TO 9/28/83,51051. TO 10/6/83,51299. TO 10/17/83,51553 TO 10/29/83,51798 1040 DOTO 11/5/83,52041.9,13.3 RUMS The operator enters DATA information about his or her car. and then types RUN to start the program. MIL E A 0 E : OMMAND 5 1 - 'JERIFV DATA 5 T 0 T E ME XT5 I - DISPLAY MILEAGE INFORMATION Z - TERMINATE PROGRAM ENTER COMMAND BY NUMBER "’ll The program’s menu is displayed and the operator chooses option 1. This requests the program to scan the DATA statements for possible errors. All is okay with the data. The dates of the first and last DATA statements are shown along with the total number entered. MILEAGE 47 Later, the operator chooses option 2. This displays the data along with the fuel MPG obtained. The program will re-enter the command menu when a key is hit. PROGRAM LISTING 10 REM MILEAGE 15 REM COPYRIGHT 1984 DILITHIUM PRESS 110 DIM A* (20), cm 1) 200 GRAPHICS O:POLL 82,2:TRAP 500 210 PRINT :P08ITION 16,PEEK(84):PRINT "MILEAGE" 220 PRINT :PRINT "COMMANDS" 230 PRINT " 1 - VERIFY DATA STATEMENTS" 240 PRINT " 2 - DISPLAY MILEAGE INFORMATION" 250 PRINT " 3 - TERMINATE PROGRAM" 260 PRINT :PRINT "ENTER COMMAND BY NUMBER "::INP UT C* 270 IF C*<>"1" AND C*<>"2" AND C*<>"3" THEN 200 280 OVAL (C) : ON C GOTO 300,700,290 290 POKE 764,255:POKE 752,0:END 300 GRAPHICS O:RESTORE 310 PRINT "DATA FILE DISPOSIT ION " : F'R I NT 320 N==0: DD= ~ 1.0E-03 330 TRAP 500 340 READ A$,D,G 350 N=N+1 360 IF N=1 THEN PRINT "DATE OF FIRST DATA RECORD : " ; A$ 48 Basic Programs for the Atari 370 IF D>=0 AND D<=999999 THEN 400 380 PRINT "—BAD ODOMETER VALUE OF ";D 390 PRINT " AT DATE: ";A* 400 IF D>DD THEN 440 410 PRINT " — INCONSISTENT ODOMETER VALUES" 420 PRINT " ODO READS ";D;“ AT DATE: " ; A* 430 PRINT " YET READS ";DD;" AT PREVIOUS DATE" 440 IF G>=0 AND 009999 THEN 470 450 PRINT "—BAD BALLONS VALUE OF ";G 460 PRINT " DETECTED AT DATE: ";A$ 470 DD=D:GOTO 330 500 LN=PEEK(187)*256+PEEK(1S6) 510 EC=PEEK(195) 520 IF EC=3 THEN 560 530 IF EC=6 THEN 590 540 PRINT :PRINT "—ERROR # ";EC;" HAS BEEN DETE CTED" 550 PRINT " AT LINE ";LN 555 PRINT :GOTO 580 560 PRINT :PRINT "—BAD DATA RECORD DETECTED" 570 PRINT " AT OR BEFORE LINE ";L.N 5630 PRINT " PROGRAM ABORTED": GOTO 290 590 IF N>0 THEN 620 600 PRINT :PRINT "—NO DATA FOUND" 610 GOTO 580 620 IF C=2 THEN 650 630 PRINT "DATE OF LAST DATA RECORD: ";A't 640 PRINT :PRINT N;" DATA RECORDS FOUND" 650 PRINT :PRINT "ALL DATA PROCESSED" 660 PRINT "HIT ANY KEY TO RESUME COMMAND MODE”;: POKE 764,255 665 IF PEEK(764)=255 THEN 665 670 POKE 764,255:TRAP 500:GOTO 200 700 GOSUB 820:RESTORE :DD=-1:N=0 710 TRAP 500 720 READ A*.D,G:N=N+1:IF LEN(A*>>8 THEN A*=A*(1, 8) 730 R-D:GOSUB 860:D=R:LD=18-LsIF INT(D)=D THEN L D=LD—2 740 R=G:GOSUB 910:G=R:LG=27-L:IF INT(G)=G THEN L G=LG-2 750 IF DD<0 OR G=0 THEN M=0:G0T0 770 7 60 M= (D-DD) /' G 770 R=M:GOSUB 960:M=R:LM=38-L:IF INT(M)=M THEN L M=LM—2 7630 DD=D: G063UB 790: GOTO 710 790 PK=PEEK(84) ‘.PRINT Ait; : POSITION LD,PK:PRINT D ;;POSITION LG,PK 795 PRINT G;:POSITION LM,PK:PRINT M BOO K=K+1:IF K<20 THEN RETURN 810 PRINT -.PRINT "HIT ANY KEY TO CONTINUE";: POKE 764,255 MILEAGE 49 B15 IF PEEK(764)=255 THEN 815 820 POKE 764,0:GRAPHICS 0 B30 PRINT "DATE ODOMETER GALLONS MPG" 840 K~0 B50 RETURN B60 IF R>=0 AND R<=999999 THEN 890 870 PRINT :PRINT "ERROR IN GALLONS DATA AT DATE: ";A$ 880 GOTO 290 B90 R=R *10+0.5:R=INT(R)/10 900 L=I_EN =0 AND R<=9999 THEN 940 920 PRINT -.PRINT “ERROR IN GALLONS DATA AT DATE: A* 930 GOTO 290 940 R=R*10+0.5:R=INT 9999 THEN R=0 970 R=R * 10+0.5: B -INT < R)/10 980 L=LEN(STR*(R)):RETURN EASY CHANGES 1. If you would like to give a name to the data file and have that name print out with the command mode, change lines 110 and 210 and add line 205 as follows: 110 DIM A$(20), B$(20) ,C$( 1) 205 LET B$ = “VOLVO 1983” 210 PRINT “MILEAGE FOR:”;B$ Just set B$ in line 205 to whatever file name you wish to use. 2. This program uses ATARI BASIC TRAP statements to detect certain expected errors. Should an unexpected one occur, the pro¬ gram may abort after printing out a message like: - - ERROR #8 HAS BEEN DETECTED AT LINE 340 These error numbers and their meanings can be found in your ATARI manual with the explanation of the TRAP statement. Should you get one and not understand what has happened, re-run the program after making the following changes in order to get normal BASIC error messages: 200 GRAPHICS 0:POKE 82,2 330 REM 710 REM so Basic Programs for the Atari MAIN ROUTINES 110 200- 280 290 300- 470 500- 670 700- 780 790- 850 860- 900 910- 950 960- 980 1000 - Dimension arrays. Command mode. Displays available subroutines and branches to the operator’s choice. Subroutine to terminate execution. Subroutine to verify DATA statements. Processes errors in reading DATA statements. Subroutine to display mileage information. Subroutine to print results. Subroutine to round odometer values. Subroutine to round gallons values. Subroutine to round mileage values. User-created DATA statements. MAIN VARIABLES C Command flag (1= verify DATA, 2= display mileage, 3= terminate execution). N Number of data records read. A$ Date of current data record. C$ User reply string. DD Previous odometer value D Current odometer value. G Current gallons value. M Current MPG value. PK Current print row. LD Print position for odometer value. LG Print position for gallons value. LM Print position for mileage value. L Length of string. R Pre-rounded numbers. K Number of lines printed since screen cleared EC Error code. LN Line number of detected error. SUGGESTED PROJECTS 1. Calculate and print the average MPG over the whole data file. This will be the total miles driven divided by the total gallons pur¬ chased. The total miles driven is the difference between the odometer values of the last and first DATA statements. The total MILEAGE 51 gallons used is the sum of all the gallons values from the second DATA statement to the last DATA statement. 2. Add an option to do statistical calculations over a given subset of the data. The operator inputs a beginning and ending date. He is then shown things like average MPG, total miles driven, total gallons purchased, etc., all computed over the range requested. 3. Write a subroutine to graphically display MPG. A bar graph might work well. 4. Add a new parameter in each data record—the cost of each fill-up. Then compute things like the total cost of gasoline, miles/dollar, etc. STOP PURPOSE AND DISCUSSION If you are only using your ATARI for making calculations or other “normal” work, you are missing out on something. The ATARI has a very accurate internal timer, which can be very useful. This program uses it in a very obvious way-as a stopwatch. Using a computer as a stopwatch gives you the advantage of leaving the last timing on the screen for reference while you are making the next timing. Of course, the computer is “smart” enough to allow you to get “lap” times as well as the final time. HOW TO USE IT The opening messages from the program show you your three options. Pressing the S key causes the stopwatch to start (or restart, if you already started it). Pressing the F key causes the stopwatch program to show you the time since the S key was pressed. Pressing the F key a second time causes the program to show you both the time since the start and the time since the last F was pressed. This lets you see interim or “lap” times. Pressing the Q key terminates the program. For example, suppose you want to time a one-mile race that is run as four laps around a quarter-mile track. Before the race begins, start the program running with the RUN command. When the starting gun is fired to start the race, press the S key. At the end of the first lap around the track, press the F key. This causes the program to show 54 Basic Programs for the Atari the time since the race started. When the second lap is completed, press the F key again. The program will show the time since the start and the time of the second lap. Press the F key again when the third and fourth laps are finished to get the time since start and lap times. Of course, the time since start at the end of the fourth lap is the final time of the race, even though the stopwatch keeps running. At that time, you can either press S to restart the stopwatch (when the next race begins) or press the BREAK or Q key to stop the program. The internal timer of the ATARI is accurate to one sixtieth (1/60) of a second. The program displays the time as though it is accurate to 0.01 second. As a result, the second decimal place is not precise. The actual time is within plus or minus 0.02 second of the time that is shown. Also, because of the computation time required to display each timing, wait at least 0.5 second between consecutive F suppressions. SAMPLE RUN First the operator presses “S” to start the stopwatch. Then, after 1 minute, 13.80 seconds, he or she presses “F”. STOP 55 PROGRAM LISTING 10 REM STOP 15 REM COPYRIGHT 1984 DILITHIUM PRESS 100 REM A STOPWATCH PROGRAM 110 GRAPHICS 0 120 POKE 752,1 130 POSITION 12,2;PRINT "STOPWATCH":PRINT 140 PRINT “S = START OR RESTART" 150 PRINT "F = LAP OR STOP" 160 PRINT "Q = QUIT" 170 PRINT ISO POSITION 2,9:PRINT "? ";:POKE 764,255 190 K=PEEK<764):IF K=255 THEN 190 200 A=PEEK(20)s B=PEEK<19 ) :C=PEEK <18) 210 IF K< >62 THEN 260 220 S1=0:POKE 18,SI:POKE 19,SI:POKE 20,SI:PRINT "S" 230 POSITION 16,10:PRINT "00:00:00" 240 POSITION 2,11:PRINT " :REM 22 SPACES 250 GOTO 180 260 IF K=56 THEN PRINT "F":GOSUB 290:GOTO 180 270 IF K=47 THEN POSITION 2,12SPOKE 764,255:POKE 752,0:END 280 GOTO 180 290 S=A+ 300 POSITION 2,10:PRINT "SINCE START = "; 510 T=S:GOSUB 370 320 IF S1=0 THEN 350 330 POSITION 2,11:PRINT "SINCE LAST = 340 T*S-S1:GOSUB 370 350 S1=S 360 RETURN 370 M=INT 380 N= INT(T/60)~60*M 390 P=INT -100# ; 450 PRINT K;:RETURN EASY CHANGES I. To allow any key to act like an F (except S or Q), change line 260 to read 56 Basic Programs for the Atari 260 IF K< >47 THENGOSUB 290:GOTO 180 2. To display the time to the nearest tenth of a second (instead of the nearest hundredth), change lines 390 and 420 to: 390 P = INT (T/6) —10 * (N + 60 * M) 420 PRINT P; MAIN ROUTINES 10-170 Displays the tide and three options. 180-190 Displays question mark and waits for a key to be pressed. 200-210 Saves time of key suppression. Checks which key was pressed. 220-280 Saves starting time and displays start clock. 290-360 Displays total and lap times. 370-450 Subroutines to convert system ticks into minutes, seconds, and hundredths and display them. MAIN VARIABLES SI Time that last S or F was pressed. S Current time value. K Key pressed by operator; work variable. A, B, C System real time clock counter readings. T Time mark variable. M, N, P Minutes, seconds, and hundredths work variables. SUGGESTED PROJECTS 1. Instead of displaying only the last lap time, display the last two or three lap times. Or, display all lap times since the start by saving each time in an array. Allow for at least twenty entries. Section 2 Educational Programs INTRODUCTION TO EDUCATIONAL PROGRAMS Education is one area where computers are certain to have more and more impact. Though a computer cannot completely replace a human teacher, the machine does have certain advantages. It is ready anytime you are, allows you to go at your own pace, handles rote drill effortlessly, and is devoid of any personality conflicts. With a good software library, the ATARI can be a valuable learn¬ ing center in the school or at home. Here are six programs to get you started. Mathematics is certainly a “natural” subject for computers. NUMBERS is designed for pre-school children. While familiarizing youngsters with computers, it provides an entertaining way for them to learn numbers and elementary counting. MATH is aimed at older, grade school students. It provides drill in various kinds of math problems. The child can adjust the difficulty factors, allowing the program to be useful for several years. The ATARI is by no means restricted to mathematical disciplines. We include two programs designed to improve your word skills. VOCAB will help you expand your vocabulary. TACHIST turns the ATARI into a reading clinic, helping you to improve your reading speed. Do you have trouble familiarizing yourself with the increasingly prevalent metric system? METRIC is the answer. Need help learning a certain subject? FLASH allows you to create your own “computer flashcards.” Then you can drill yourself until you get it right. 58 Basic Programs for the Atari MATH PURPOSE MATH provides mathematics drills for grade school children. The student can request problems in addition, subtraction, or multiplica¬ tion from the program. Also, he or she may ask that the problems be easy, medium, or hard. The program should be useful to a child over an extended period of time. He can progress naturally to a harder category of problems when he begins to regularly perform well at one level. The difficulty and types of problems encompass those normally encountered by school children between the ages of six and ten. The problems are constructed randomly within the constraints imposed by the degree of difficulty selected. This gives the student fresh practice each time the program is used. After entering answers, he is told whether he was right or wrong. The correct answers are also displayed. HOWTO USE IT To begin, the student must indicate what type of problem he wishes to do. The program requests an input of 1, 2, or 3 to indicate addition, subtraction, or multiplication, respectively. It then asks whether easy, medium, or hard problems are desired. Again an input of 1, 2, or 3 is required. Now the screen will clear and five problems of the desired type will be displayed. The user now begins to enter his answers to each problem. 60 Basic Programs for the Atari A question mark is used to prompt the user for each digit of the answer, one digit at a time. This is done moving right to left, the way arithmetic problems are naturally solved. To start each problem, the question mark will appear in the spot for the rightmost (or units column) digit of the answer. When the key for a digit from 0-9 is pressed, that digit will replace the question mark on the screen. The question mark moves to the immediate left waiting for a digit for the “tens” column. Digits are entered in this right-to-left manner until the complete answer has been input. Then the RETURN must be pressed. This will end the answer to the current problem and move the question mark to begin the answer for the next question. If the RETURN key is pressed to begin a problem, an answer of zero is assumed intended. No problems created by this program have answers of more than three digits. If a four-digit answer is given, the program will accept the answer, but then go immediately to the next problem. Answers to the problems are never negative. The program will display the correct answers to the five problems on the screen after the student has entered his five answers. The message “RIGHT!” or “WRONG!” will also be displayed below each problem. Then the message “PRESS ANY KEY TO CONTINUE” will be displayed. After the key is pressed, a new set of five problems of the same type will be presented. This continues until twenty problems have been worked. Before ending, the program shows what the student’s performance has been. This is expressed as the number of problems solved correctly and also as the percentage of problems solved correctly. MATH 61 The operator chooses to do hard addition problems. The initial set of five problems is presented. With a question mark, the program prompts the operator for the answer to the first problem. The operator has entered his or her five answers. The program displays the correct answers and indicates whether or not each problem was solved correctly. The program waits for the operator to press any key in order to continue with the next set of five problems. PROGRAM LISTING 10 REM MATH 15 REM COPYRIGHT 1984 DILITHIUM PRESS 20 GRAPHICS 0;POKE 752,1:OPEN #1,4,0,"K:" 30 ND=0 40 DIM A(5),B<5>,C<5>,G<5),R*(10),P*<10> 50 NP-=20 60 GOSUE 540 70 PRINT :PRINT :PRINT "WHAT TYPE OF PROBLEM SHA LL WE DO?"-.PRINT BO PRINT ” 1 - ADDITION" 90 PRINT ” 2 - SUBTRACTION" 1O0 PRINT " 3 - MULTIPLICATION" 110 PRINT :PRINT "WHICH TYPE (1, 2, OR 3>";:INPU T 120 IF R*="l” OR R*="2" OR R*=”3" THEN 130 125 GOTO 110 130 T=VAL 210 ON D GOTO 220,230,250 220 GOSUB 570:GOSUB 550:GOSUB 560:GOTO 260 230 GOSUB 570:GOSUB 560:IF T=3 THEN GOSUB 590:GQ BUB 550:GOTO 260 240 IF T<>3 THEN GOSUB 580:GOSUB 550:GOTO 260 250 GOSUB 580:GOSUB 550:GOSUB 560:IF T=3 THEN GO BUB 570:GOSUB 560 260 IF T< >2 THEN 300 270 FOR J=1 TO 5 280 IF B C : C < J > =B (J > : B (,D =R 290 NEXT J 300 GOSUB 600:GOSUB 540 310 FOR J=1 TO 5:X =(J—1)#8+2:Y-6:GOSUB 640:NEXT 3 320 FOR K=1 TO 5:X=*B+5:Y=10:GOSUB 440:G(K) =N:NEXT K 330 X=17:Y=15:GOSUB 720:PRINT " ANSWERS" 340 FOR J=1 TO 5:X=(J—1)*8+2:Y=18:GOSUB 760:NEXT a 350 Y=20:FOR J=1 TO 5:X=(0-1)*8+2:GOSUB 720 360 IF A(J > < >G(J) THEN PRINT "WRONG!":GOTO 380 370 PRINT "RIGHT!":NR=NR+1 380 NEXT J 390 PRINT :PRINT “PRESS ANY KEY TO CONTINUE"::CL OSE #1:OPEN #1,4,O,"K:":GET #1,RX 400 ND=ND+5 410 IF ND57 THEN 460 500 V=A-48:P*=CHR»(A):GOSUB 530:N=N+M*V:M=M*10 510 IF M>1000 THEN RETURN 520 X=X—1:GOTO 450 530 POSITION X,Y:PRINT P*;:POSITION ZZ,Y:RETURN 540 PRINT CHR*(125):POSITION 10,PEEK(84):PRINT " ARITHMETI C":RETURN 550 FOR K—1 TO 5:C(K)=INT(RND(1)*(H-L+1))+L:NEXT K: RETURN 560 FOR K=1 TO 5:B(K)=INT(RND(1)*(H-L+l)>+L:NEXT K:RETURN 64 Basic Programs for the Atari 570 H=9:L=0:RETURN 580 H=99:L-0:RETURN 590 H=25:L=1:RETURN 600 ON T GOTO 610,620,630 610 FOR J = 1 TO 5:A #B(J>:NEXT J:RETURN 640 GOSUB 720:Y=Y+1:PR INT CHR* £ 32);CHR*(32); 650 IF 0(0X10 THEN PRINT CHR*(32); 660 PRINT C(J):GOSUB 720:Y=Y+1:IF T=1 THEN PRINT 670 IF T=2 THEN PRINT 6(30 IF T=3 THEN PRINT "X"; 690 PRINT CHR*(32);:IF BOX<10 THEN PRINT CHR*(3 2 ) ; 700 PRINT B < J) : GOSUB 72O:F0R K=i TQ 4:F'RINT CHR* (1(3) ; 710 NEXT K:RETURN 720 POSITION X,Y 730 RETURN 760 GOSUB 720:IF A(JX10 THEN PRINT CHR*(32);CHR $ (32);CHR*(32)::GOTO 790 770 IF A (OX 100 THEN PRINT CHR* (32) ; CHR* (32) ; : GO TO 790 780 IF A (OX 1000 THEN PRINT CHR* (32); 790 PRINT A(J):RETURN BOO GOSUB 540:PRINT :PRINT (310 PRINT "YOU GOT "; NR;" RIGHT" B20 PRINT "OUT OF “;NP;" PROBLEMS" 830 P--NR/NP* 100 B40 PRINT :PRINT "THAT’S ";P;" PERCENT CORRECT": RETURN EASY CHANGES 1. The program currently does twenty problems per session. You can change this number by altering the variable NP in line 50. For example, 50 NP= 10 will cause the program to do only ten problems per session. The value of NP should be kept a positive multiple of five. 2. Zero is currently allowed as a possible problem operand. If you do not wish to allow this, change lines 570 and 580 to read as follows: 570H=9:L=1: RETURN 580 H = 99:L = 1:RETURN MATH 65 MAIN ROUTINES 20- 60 70-195 200-300 310-430 440-520 530 540 550-560 570-590 600-630 640-710 720-730 760-790 800-840 Initializes constants, displays header. Asks operator for type of problems desired. Sets A, B, C arrays, clears screen. Mainline routine—displays problems, gets operator’s an¬ swers, displays correct answers and user’s performance. Subroutine to get and display user’s answers. Character-printing subroutine. Subroutine to clear screen and display title. Subroutine to set B, C arrays. Subroutine to set L, H. Subroutine to calculate A array from B, C arrays. Subroutine to display problems. Subroutine to move cursor to screen position X, Y. Subroutine to display the correct answers. Subroutine to display operator’s performance. MAIN VARIABLES NP Number of problems to do in the session. ND Number of problems done. NR Number of correct answers given. C, B, A Arrays of top operand, bottom operand, and correct an¬ swer to each problem. N Operator’s answer to current problem. G Array of operator’s answers. T Type of problems requested (1 = addition, 2=subtraction, 3 ^ multiplication). D Kind of problem requested (l=easy, 2 = medium, 3= hard). H, L Highest, lowest integers to allow as problem operands. M Answer column being worked on. R$ Operator’s input character. V Value of A. A Input character. X, Y Horizontal, vertical screen position of cursor. J, K Loop indices and work variables. P Percentage of correct answers. P$ Character to be printed. 66 Basic Programs for the Atari SUGGESTED PROJECTS 1. Keep track of problems missed and repeat them quickly for addi¬ tional practice. 2. No negative operands or answers are currently allowed. Rewrite the program generation routines and the operator’s answer routines to allow the possibility of negative answers. 3. The answers are now restricted to three-digit numbers. However, the program would work fine for four-digit numbers if the oper¬ ands of the problems were allowed to be large enough. Dig into the routines at lines 200-300 and 570-590. See how they work and then modify them to allow possible four-digit answers. 4. The operator cannot currently correct any mistakes he makes while typing in his answers. Modify the program to allow him to do so. 5. Modify the program to allow problems in division. FLASH PURPOSE There are certain things that the human mind is capable of learning only through repetition. Not many people can remember the multi¬ plication tables after their first exposure, for example. The same applies to learning the vocabulary of a foreign language, the capital cities of the fifty states, or famous dates in history. The best way to learn them is to simply review them over and over until you have them memorized. A common technique for doing this involves the use of flashcards. You write one half of the two related pieces of information on one side of a card, and the other half on the other side. After creating a set of these cards, you can drill yourself on them over and over until you always remember what’s on the other side of each card. But why waste precious natural resources by using cards? Use your computer instead. This program lets you create flashcards, drill using them, and save them on cassette tape or disk for later review. HOWTO USE IT As currently written, the program immediately begins drilling you on Spanish vocabulary words. After explaining how to use the pro¬ gram with these words, we’ll show you how to enter your own flashcards (see Easy Changes). The program flashes one side of one card on the screen for you. Both are chosen at random-the side and the card. Your job is to 68 Basic Programs for the Atari respond with the other side. If you enter it correctly, the program says “RIGHT!” If not, it tells you the correct answer. In either event, the program continues by picking another side and card at random. This goes on until you finally respond by simply pressing the RETURN key, which tells the program that you do not want to drill any more. It then tells you how many you got right out of the number attempted, as well as the percentage, and gives you the option of drilling more or ending the program. During the drill, the program will not repeat a card that was used in the previous four questions. SAMPLE RUN FLASHCARD PROGRAM 30 FLASHCARDS n TELL ME WHAT IS OH THE OTHER SIDE OF EACH CARD AS I SHOW IT. PRESS RETURN TO SEE PROGRESS REPORT ■’TO KNOW RIGHT 1 THE STORE J In its present form, the program begins by offering 30 flashcards. The operator enters a response and pushes RETURN. This continues until the operator requests a progress report. FLASH 69 Later the operator ends the drill and the program shows the number and percentage of correctly answered questions. The options for continuing are displayed and the program waits for the operator’s choice. PROGRAM LISTING 10 REM FLASH 15 REM COPYRIGHT 1984 DILITHIUM PRESS 20 GRAPHICS 0:POKE 82,2 30 L=50:M=5 40 DIM Ft <40> ,B$ (40) ,F'(M-1 ) , X (L) , C* (40) , P* (40) , R * (40) 50 FOR J=0 TO M-l: P < J) =0: NEXT JsF'OSITION 8,2:PRI NT "FLASHCARD PROGRAM" 60 PRINT :PR I NT 70 K-= 1: D=0:W-0 BO IF K>L THEN 120 90 READ Y:IF Y--1 THEN 130 1O0 X < K > =Y 110 K-K+l:GOTO 80 120 PRINT "#* FLASHCARD ARRAY FULL **":PRINT 130 K=K—1 140 IF K>+l 220 FOR ,1=0 TO M-2 230 IF P (J) =R THEN 210 240 NEXT J 250 RESTORE X(R):READ F$:READ B* 260 J--RND<1) : IF J >0.5 THEN 290 270 PRINT F*:C*=B* 2(30 GOTO 300 290 PRINT B*:C*=F* 300 PRINT :INPUT R$ 310 IF LEN(R*>=0 THEN 440 320 PRINT 330 IF R*=C* THEN 370 340 PRINT "NO, THE CORRECT RESPONSE IS:" 350 PRINT C* 360 W=W+1:GOTO 390 370 PRINT "RIGHT!" 380 C=C+1 390 FOR 3=1 TO M-2 400 P(J-l)=P(J):NEXT J 410 P(M-2)=R 420 PRINT 430 GOTO 210 440 PRINT 450 IF C+W=() THEN 500 460 PRINT C;" RIGHT OUT OF ";C+W 470 PRINT 480 PRINT C*100/(C+W);" PERCENT" 490 PRINT 500 PRINT "NEXT ACTION:" 510 PRINT " 1 - DRILL MORE" 520 PRINT " 2 - END PROGRAM" 530 PRINT 540 INPUT R* 550 IF R*="2" THEN END 560 IF R*="l" THEN 160 570 PRINT "** ENTER 1 OR 2 PLEASE **" 580 PRINT :GOTO 500 590 DATA 610,620,630,640,650,660,670,680,690,700 ,710,720,730,740,750,760,770,780,790,800,B10,820 ,830,840 600 DATA 850,860,870,880,890,900,-1 610 DATA THE PEN, L.A PLUMA 620 DATA THE DOOR, LA F'UERTA 630 DATA THE SCHOOL,LA ESCUELA 640 DATA THE FLOOR,EL SUELO 650 DATA THE STORE,LA TIENDA 660 DATA THE HAND,LA MANO 670 DATA THE HOUSE,LA CASA 680 DATA THE FRIEND,EL AMIGO 690 DATA THE DINNER,LA COMIDA 700 DATA THE CHAIR,LA SILLA FLASH 71 710 720 730 740 750 760 770 780 790 BOO 810 B20 830 B40 850 B60 870 B80 890 900 DATA TO ARRIVE,LLEGAR DATA TO ASK,PREGUNTAR DATA TO BUY,CQMPRAR DATA TO BRINS,LLEVAR DATA TO COME,VENIR DATA TO EAT,COMER DATA TO FIND,HALLAR DATA TO SO,ANDAR DATA TO HAVE,TENER DATA TO KNOW,SABER DATA BLUE,AZUL DATA GREEN,VERDE DATA RED,ROJO DATA WHITE,BLANCO DATA YELLOW,AMARILLO DATA ENOUGH,BASTANTE DATA FAR,L.EJOS DATA FEW,POCOS DATA MANY,MUCHOS DATA NEAR,CERCA EASY CHANGES 1. Replace the DATA statements with your own flashcards. A comma is used to separate the two sides of each card. Don’t use commas, colons, or quotation marks as part of your cards. The DATA statements are to be entered as lines 610 through 900 (or higher, if necessary). The earlier DATA statements (590 and 600) must contain a list of all the later DATA statements, with a value of negative one as the last number. If necessary, more than these two lines can be used, but be sure the line numbers used are all in the range of 590 to 609. 2. Change the limits of the number of flashcards that can be entered by altering line 30. L is the upper limit and M is the minimum. The current upper limit of 50 will fit in an ATARI with 4000 bytes free (see Appendix 1) if each side of each flashcard averages no more than about twelve to fifteen characters in length. If you have 16,000 bytes free, you can make L as large as about four hundred for flashcards this size. Do not make M much larger than about ten or so, or you will slow down the program and use more memory than you might want. But, it increases the number of cards drilled upon before repeating. Make sure each side of each flashcard is no more than 40 characters long. 3. To cause the program to always display side one of the flashcards (and ask you to respond with side two), change this line: 260 REM 72 Basic Programs for the Atari To cause it to always display side two, change it this way: 260 GOTO 290 MAIN ROUTINES 10- 70 Initializes variables. Creates arrays. Displays title. 80-150 Reads flashcards from DATA statements. 160-490 Drills operator on flashcards in memory. 500-580 Displays options and analyzes response. Branches to ap¬ propriate routine. 590-600 DATA statements defining DATA statements containing flashcards. 610-900 DATA statements of the flashcards. MAIN VARIABLES L Upper limit of number of flashcards that can be entered. M Minimum number of flashcards that can be entered. R Subscript of random flashcard chosen during drill. K Number of flashcards entered. W Number of wrong responses. C Number of correct responses. X Array containing line number of flashcards. F$ Array containing front side of flashcard (side 1). B$ Array containing back side of flashcard (side 2). P Array containing subscripts of M — 1 previous flashcards during drill. J Loop and subscript variable. C$ The correct response during drill. R$ Response from operator. Also temporary string variable. Y Flashcard input index. SUGGESTED PROJECTS 1. Modify the program for use in a classroom environment. Require the operator to drill a fixed number of times (maybe 20 or 50). Don’t allow a null response to end the drill. For example, you could make these changes: 310 REM 385 IF C=20 THEN 440 495 END This will cause the program to continue until 20 correct answers are given, and then end. METRIC PURPOSE In case you don’t realize it, we live in a metric world. The United States is one of the last holdouts, but that is changing rapidly. So if you’re still inching along or watching those pounds, it’s time to convert. METRIC is an instructional program designed to familiarize you with the metric system. It operates in a quiz format; the program randomly forms questions from its data resources. You are then asked to compare two quantities—one in our old English units and one in the corresponding metric units. When you are wrong, the exact conversion and the rule governing it are given. The two quantities to compare are usually within 50% of each other. Thus, you are constantly comparing an “English” quantity and a metric one which are in the same ball park. This has the effect of providing you with some insight by sheer familiarity with the questions. HOWTO USE IT The first thing the program does is ask you how many questions you would like to do for the session. Any value of one or higher is acceptable. The sample run shows how each question is formulated. A quan¬ tity in English units is compared with one in metric units. Either one may appear first in the question. Each quantity will have an integral value. The relating word (“longer,” “hotter,” “heavier,” etc.) indicates what type of quantities are being compared. 74 Basic Programs for the Atari There are three possible replies to each question. Pressing Y or N means that you think the answer is yes or no, respectively. Pressing any other key indicates that you have no idea as to the correct answer. If you answer the question correctly, you will be duly congratu¬ lated and the program will proceed to the next question. A wrong answer or a response of “no idea,” however, will generate some diagnostic information. The first value used in the question will be shown converted to its exact equivalent in the corresponding units. Also, the rule governing the situation will be displayed. At the end of any question, the program will request that you hit any key to pro¬ ceed to the next question. The program will continue generating the requested number of questions. Before ending, it will show you how many correct answers you gave and your percentage correct. SAMPLE RUN The operator requests a three question quiz. METRIC E : i ION i OF O ME T R I 0 ''ur. FEET LONOEP THAN ME r E P 5 " U i £ y ' Y E 5 1 ONE V 0 U ’ PE R I 0 H T OEPY OOOD ONY KEY TO CONTINUE The first question is correctly answered “yes”. The program waits for key to be hit before continuing the quiz. The second question is incorrectly answered “no”. The correct conver¬ sion and governing rule are then displayed. 76 Basic Programs for the Atari When all three questions have been answered, the program shows the number and percentage of correctly answered questions. PROGRAM LISTING 10 REM METRIC 15 REM COPYRIGHT 1984 DILITHIUM PRESS 130 GRAPHICS 0:POKE 82,2 150 DIM ES*(30),MS*(30),R*<30),EP*(30),MP*<30>,X (30) , B$ (10) 160 B*=CHR*(32> 165 TRAP 880 170 CLOSE HI:OPEN #1,4,0,"K:" 200 GOSUB 400:GQSUB 450 21.0 PRINT "HOW MANY QUESTIONS SHALL WE DO";:INPU T NQ:NGN INT(NQ):IF NQ<1 THEN 210 220 FOR J=1 TO NQ:GOSUB 600:GOSUB 900:NEXT J 230 GOSUB 450:PRINT "YOU GOT ";NR;" RIGHT OUT OF ";NQ;" QUESTIONS":PRINT 240 P~(INT(10000*NR/NQ>>/100:PRINT "PERCENTAGE C ORRECT = ";P 250 END 400 RESTORE : ND=0 410 ND=ND+1:READ ZZ 420 IF HZ=-1 THEN ND=ND-1:RETURN 430 X(ND)=ZZ:GOTO 410 450 PRINT CHR*(125):POSITION 10,PEEK(84):PRINT " A METRIC QUIZ" 460 PRINT -.PRINT -.RETURN METRIC 77 600 N=INT(ND*RND<1)> + l 605 RESTORE X(N):READ E5»,MS*,R*,0,EP*,MR* 610 F-0:IF RND(1)>0.5 THEN F=1 620 V1=INT(RND(1)*99)+2:V3=V1*C:IF F=1 THEN V3=V 1/C 630 IF N=1 THEN V3= >57 THEN 140 130 IF VAL=R+1 THEN 160 140 PRINT "NO, THAT’S NOT IT. TRY AGAIN." 150 GOTO 90 160 PRINT "THAT’S RIGHT!" 170 FUR X=1 TO 1000:NEXT X 180 F-R:PRINT CHR*(125) 190 C1 = INT(16*RND <0)>:C2=INT(16*RND(0)) 200 C3=INT(16*RND(O)):IF C1=C2 THEN C2=INT((C2+4 ) /4 ) 84 Basic Programs for the Atari 205 IF C1-C3 THEN C3=INT</2> 207 IF C2=C3 THEN C2=INT(/3> 210 GRAPHICS 5+16 220 SETCOLOR 0,C1,10 230 SETCOLOR 1,C2,10 240 SETCOLOR 2,03,10 2.45 E=INT < 10#RND <0) ) +5 250 COLOR 1:PLOT 1,1sDRAWTO E,1:DRAWTO E,E:DRAWT 0 1,E:DRAWTO 1,1 255 E=INT(10*RND(0))+5 260 COLOR 2s FOR 0 = 1 TO E 270 Y=TY+Js X=TX+Js PLOT X.YsNEXT J 200 FOR 0 = 1 TO Es Y=TY+0s X=TX—0+2;PLOT X,YsNEXT 0 290 Y=TY+E+1s FOR X=TX-E+1 TO TX+E+lsPLOT X,YsNEX T X 300 COLOR 3 305 E=INT(8*RND(0))+5 310 FOR 0=1 TO E 320 X=A+0:Y=B+0 s PLOT X,Y 330 Y=B- Js PLOT X,Y 340 Y=Bs PLOT X,Y 350 X=As PLOT X,Y 360 Y=B+0 s PLOT X,Y 370 Y=B—0 s PLOT X,Y 380 X=A-0s PLOT X,Y 390 Y=B:PLOT X,Y 400 Y=B+0: F'LOT X,Y 410 NEXT 0 420 FOR 0 = 1 TO 1500s NEXT 0 430 GRAPHICS SsSETCOLOR 1,12,6sSETCOLOR 2,0,0 440 GOTO 80 EASY CHANGES 1. Change the range of numbers that the program asks by altering the value of M in line 40. For a beginner, use a value of 3 for M instead of 9. Later, increase the value of M to 5, and then 8. 2. Alter the delay after “THAT’S RIGHT!” is displayed by altering the value of 1000 in statement 170. Double it to double the time delay, etc. The same can be done with the 1500 in line 420 to alter the delay after the figures are drawn. 3. To avoid randomness in the size of the figures that are drawn, replace line 245 with 245 E=ll Instead of 11, you can use any integer from 3 to 12. Also change lines 255 and 305 to 255 REM 305 REM NUMBERS 85 MAIN ROUTINES 10- 70 Initializes variables. Clears screen. 80 Picks random integer from 1 to M. 90-160 Asks question. Gets answer. Determines if right or wrong. 170 Delays about 3 seconds. 180-250 Determines colors and draws a square. 255-290 Draws a triangle. 300-410 Draws an asterisk. 420 Delays about 5 seconds. 430- 440 Clears screen. Goes back to ask next question. MAIN VARIABLES Cl, C2, C3 M E R P R$ X, Y TX, TY A, B J Color control variables. Maximum number that will be asked. Edge length of geometric figures. Random integer in range from 1 to M. Previous number that was asked. Reply given by operator. Coordinates in CRT display; also work variables. Triangle’s reference locations. X,Y coordinate values. Loop index. SUGGESTED PROJECTS 1. Modify the program to ask the next letter of the alphabet. Use the ASC and CHR$ functions in picking a random letter from A to Y, and to check whether the response is correct or not. 2. Ask each number from 1 to M once (in a random sequence). At the end of the sequence, repeat those that were missed. 3. Add different shapes to the graphics display that is done after a correct answer. Try an octagon, a diamond, and a rectangle. Or, combine this program with one of the graphics display programs. TACHIST PURPOSE This program turns your computer into a tachistoscope (tah-KISS- tah-scope). A tachistoscope is used in reading classes to improve reading habits and, as a result, improve reading speed. The program displays a word or phrase on the screen for a fraction of a second, then asks you what it was. With a little practice, you will find that you can read phrases that are displayed for shorter and shorter time periods. HOWTO USE IT The program starts off by displaying a brief introduction and waiting for you to press any key (except the BREAK key or shift keys, of course). If you press the ESCAPE key, the program ends. After you press a key, the screen is blanked out except for two horizontal dash lines in the upper left-hand comer. After approx¬ imately two seconds, a phrase is flashed on the screen between the two lines. Then the screen is blanked again, and you are asked what the phrase was. If you respond correctly, the next phrase is displayed for a shorter time period (half as long). If you respond incorrectly, the program shows you the correct phrase, and the next phrase is displayed for a longer period of time (twice as long). The fastest the computer can display a phrase and erase it is about 0.02 second (one-fiftieth). See if you can reach the top speed and still continue to read the phrases correctly. 88 Basic Programs for the Atari A great deal of research has been done to determine how people read and what they should do to read both faster and with better comprehension. We will not try to explain it all (see the bibliogra¬ phy), but a couple of things are worth mentioning. To read fast, you should not read one word at a time. Instead, you should learn to quickly read an entire phrase at once. By looking at a point in the center of the phrase (and slightly above it), your eyes can see the whole phrase without the necessity of scanning it from left to right, word by word. Because the tachistoscope flashes an entire phrase on the screen at once, it forces you to look at a single point and absorb the whole phrase, rather than scanning left to right, word by word. If you can incorporate this technique into your reading and in¬ crease the width of the phrases you absorb, your reading speed can increase dramatically. SAMPLE RUN The program displays an introduction, then waits. TACHIST 89 The program clears the screen and displays two parallel lines in the upper left of the screen for a couple of seconds. The program flashes a short phrase (chosen at random) between the two lines for a fraction of a second, then clears the screen. 90 Basic Programs for the Atari The program asks what the phrase was. The operator responds correctly. The program acknowledges the correct response, and indicates that the next phrase will be shown for half as long. PROGRAM LISTING 10 REM TACHIST 15 REM COPYRIGHT 1984 DILITHIUM PRESS 130 T=256:CLOSE #ltOPEN #1,4,0,"K:" 140 L=50 150 DIM T$(40) , XL THEN PRINT "#* TOO MANY DATA ST ATEMENTS **":END 200 X (C)=R 210 GOTO 170 250 GRAPHICS O 260 PRINT 270 PRINT "***# TACHISTOSCOF'E ****" 280 PRINT 290 PRINT "THIS PROGRAM IS DESIGNED TO" 300 PRINT "IMPROVE YOUR READING SPEED." 310 PRINT 320 PRINT "I’LL BRIEFLY DISPLAY A SHORT PHRASE," 330 PRINT "AND YOU TRY TO READ IT." 340 PRINT 350 PRINT "TYPE WHAT YOU SEE, AND I’LL TELL YOU" 360 PRINT "IF YOU WERE RIGHT." TACHIST 91 370 PRINT 410 PRINT "PRESS ANY KEY WHEN YOU’RE READY,” 415 PRINT "OR THE ESC KEY TO STOP." 420 GET #1, R:IF R=27 THEN END 430 R=INT(C*RND<1>>+l 440 IF R=P1 OR R=P2 OR R=P3 OR R=P4 OR R=P5 THEN 430 460 GOSUB 840:FOR K=1 TO 300:NEXT K 470 GOSUB 750:POSITION 2,2:PRINT T* 480 FOR J=1 TO T:NEXT J 500 PRINT CHR*(125):FOR K-l TO 200:NEXT K 510 PRINT : PRINT -.PRINT : PRINT 520 PRINT "WHAT WAS IT?":POKE 752,0 530 PRINT :INPUT R* 540 IF RtOTt THEN 700 550 PRINT :PR INT "THAT’S RIGHT!" 560 T=T/2 590 R*="FOR HALF AS LONG." 600 P1=P2:P2=P3:P3=P4:P4=P5:P5=R 610 PRINT 620 IF T<8 THEN T=8:R*="AT MAXIMUM SPEED." 630 PRINT "THE NEXT TIME IT WILL BE DISPLAYED":P RINT R* 640 PRINT :GOTO 410 700 PRINT "NO, THAT’S NOT IT. IT WAS" 710 PRINT :PRINT CHR*(39) ; T*;CHR*(39) 720 T=T*2 730 IF T>1024 THEN T=1024:R*="AT THE SAME SPEED. ":GOTO 600 740 R*="FOR TWICE AS LONG.":GOTO 600 750 RESTORE X 4 THEN 1140 1130 PRINT sPRINT "THAT’S NOT ENOUGH. LET’S DO 5 ":FOR L=1 TO 1000:NEXT L:L=5 1140 IF EOO THEN 1200 1150 PRINT 1200 PRINT CHR*<125)sRETURN 2000 IF EOO THEN 2200 2010 C=5 2020 D=26 2030 DIM D*(40),E* <40),P=0 3020 NEXT J 3030 FOR J=1 TO C 3040 P=INT(D*RND(1)>+l 3045 IF P=P1 OR P=P2 OR P=P3 THEN 3040 VOCAB 99 3050 FOR K=1 TO J 3060 IF P(K)=P THEN 3040 3070 NEXT K 3080 P(J)=P 3090 NEXT J 3110 A=INT>+l 3200 RETURN 4000 PRINT 4010 M=RND<1) 4020 RESTORE X(P(A>>:READ D*sREAD E® 4025 IF M>0.5 THEN 4100 4030 PRINT Q;" WHAT WORD MEANS" 4035 PRINT “ " ;E®; 4040 FOR J—l TO C 4045 RESTORE X =1 AND R<=C THEN 5050 5020 PRINT "I NEED A NUMBER FROM 1 TO ";C 5030 GOTO 5000 5050 IF R=A THEN 5100 5060 PRINT "NO, THE ANSWER IS NUMBER "j A 5070 GOTO 5210 5100 PRINT "RIGHT!" 5110 01=01+1 5210 IF 0=1 THEN 5300 5220 PRINT "YOU HAVE " 5 01 5 " RIGHT OUT OF ";Q;“ 0 UESTIONS" 5300 P3=P2 5310 P2=P1 5320 P1=P 0 THEN 6070 6050 PRINT "WELL, THAT’S A ’PERFECT’ SCORE..." 6060 GOTO 6200 6070 PRINT "THAT’S ";0;" PERCENT.” 6080 IF 0>25 THEN 6110 6090 PRINT "CONGRATULATIONS ON AVOIDING A SHUTOIJ T" 6100 GOTO 6200 100 Basic Programs for the Atari 6110 IF Q>50 THEN 6140 6120 PRINT "YOU CAN USE SOME MORE PRACTICE" 6130 GOTO 6200 6140 IF Q>75 THEN 6170 6150 PRINT "NOT BAD, BUT ROOM FOR IMPROVEMENT" 6160 GOTO 6200 6170 PRINT "VERY GOOD!" 6180 IF Q>95 THEN PRINT "YOU’RE ALMOST AS SMART AS I AM!» 6200 PRINT 6210 PRINT "WANT TO TRY AGAIN INPUT R* 6215 IF LEN(R$)=0 THEN 6210 6220 IF Rt(l,l)<>"N" THEN 6240 6230 PRINT :PRINT "CHECK YOU LATER":END 6240 IF RS> (1, 1) < >"Y" THEN 6200 6250 RETURN 6500 IF PEEK<195)=8 THEN PRINT sPRINT "** ILLE6A L ENTRY **"sPRINT :TRAP 6500 s ON ZX GOTO 5000,111 0 6510 END 7000 DATA 7020,7030,7040,7050,7060,7070,7080.709 0,7100,7110,7120,7130,7140,7150,-1 7020 DATA ANONYMOUS,OF UNKNOWN OR HIDDEN ORIGIN 7030 DATA OMINOUS,THREATENING OR MENACING 7040 DATA AFFLUENT,WEALTHY 7050 DATA LACONIC,TERSE 7060 DATA INTREPID,FEARLESS OR COURAGEOUS 7070 DATA GREGARIOUS,SOCIAL OR COMPANY-LOVING 7080 DATA ENERVATED,WEAK OR EXHAUSTED 7090 DATA VENERABLE, WORTHY OF RESPECT OR REVE.REN CE 7100 DATA DISPARATE,DIFFERENT AND DISTINCT 7110 DATA VIVACIOUS,LIVELY OR SPIRITED 7120 DATA ASTUTE,KEEN IN JUDGMENT 7130 DATA URSINE,BEARLIKE 7140 DATA PARSIMONIOUS,STINGY OR FRUGAL 7150 DATA OMNISCIENT,ALL-KNOWING EASY CHANGES 1. Add more DATA statements after line 7150, or replace the DATA statements between lines 7020 and 7150 with your own. Be careful not to use two or more words with very similar definitions; the program might select more than one of them as possible answers to the same question. Note that each DATA statement first has the vocabulary word, then a comma, and then the definition or syn¬ onym. Be sure there are no commas or colons in the definition (unless you enclose the definition in quotes). If you add more DATA statements, you have to increase the value of D in line 2020 VOCAB 101 to be at least one greater than the number of words. The number of DATA statements you can have depends on how long each one is and how much user memory your computer has. Using DATA statements that average the same length as these, you can probably have about 40 of them if you have 4000 bytes free (see Appendix 1), or over 200 with 10,000 bytes free. Be sure to add the line numbers of added DATA statements to the list in line 7000. This list is terminated with -1 to signal the end of the data. 2. To get something other than five choices for each question, change the value of C in line 2010. You might want only three or four choices per question. 3. If you do not want to be given a choice of how many questions are going to be asked, remove lines 1110 through 1130 and insert the following lines: 1110 PRINT “WE’LL DO TEN QUESTIONS.” 1120 L=10 This will always cause ten questions to be asked. Of course, you can use some number other than ten if you want. MAIN ROUTINES 200 300- 910 1000-1200 2000-2300 3000-3200 4000-4210 5000-5330 6000-6250 6500 7000-7150 Sets error trap. Mainline routine. Calls major subroutines. Displays introduction. Determines number of questions to be asked. Reads line numbers of vocabulary words and defini¬ tions into arrays. Performs housekeeping. Selects choices for answers and determines which will be the correct one. Determines in which format the question will be asked. Asks it. Accepts answer from operator. Determines if right or wrong. Keeps score. Saves subscripts of last three cor¬ rect answers. Gives final score. Asks about doing it again. Error trap for illegal inputs. Displays error message and tries again. Variable statements with vocabulary words and definitions. 102 Basic Programs for the Atari MAIN VARIABLES E Set to 1 to avoid repeating introduction after the first round. L Limit of number of questions to ask. R Operator’s reply to each question. C Number of choices of answers given for each question. D At least one greater than number of DATA statements. Used to DIM arrays. D$ Selected word. E$ Selected definition. P Array for numbers of possible answers to each question. J Work variable (subscript for FOR-NEXT loops). Q Number of questions asked so far (later used to calculate percent correct). Q1 Number of questions correct so far. P Work variable. P1, P2, Last three correct answers. P3 T$ Temporary string. K, ZX Working variables. A Subscript of correct answer in P array. M Work variable to decide which way to ask question. R$ \fcs or no reply about doing another round. X Array of DATA line numbers. SUGGESTED PROJECTS 1. Modify lines 6030 through 6200 to display the final evaluation messages based on a finer breakdown of the percent correct. For example, show one message if 100 percent, another if 95 to 99, another if 90 to 94, etc. 2. Ask the operator’s name in the introduction routine, and person¬ alize some of the messages with his/her name. 3. Instead of just checking about the last three questions, be sure that the next question has not been asked in the last eight or ten ques¬ tions. (Check lines 3045 and 5300 through 5320.) 4. Keep track of which questions the operator misses. Then, after going through the number of questions he/she requested, repeat those that were missed. Section 3 Game Programs INTRODUCTION TO GAME PROGRAMS Almost everyone likes to play games. Computer games are a fun and entertaining use of your ATARI computer. Besides providing relaxation and recreation, they have some built-in practical bonuses. They often force you to think strategically, plan ahead, or at least be orderly in your thought processes. They are also a good way to help some friends over their possible “computer phobia.” We present a collection of games to fit any game-playing mood. Maybe you desire a challenging all-skill game? Like chess or checkers, WARI involves no luck and considerable thinking. The computer will be your opponent, and a formidable one indeed. Perhaps you’re in the mood for a game with quick action and mounting excitement. GROAN is a fast-paced dice game involving mostly luck with a dash of skill (or intuition) thrown in. The com¬ puter is ready for your challenge any time. JOT is a word game. You and the ATARI each take secret words and then try to home in on each other’s selection. Do you like solving puzzles? If so, try DECODE. The computer will choose a secret code and then challenge you to break it. Graphic electronic arcade games have been a prevalent landmark of the past few years. We include two such games. ROADRACE puts you behind the wheel of a high-speed race car. You must steer accu¬ rately to stay on course. OBSTACLE lets you and a friend compete in a game of cut and thrust. Each of you must avoid crossing the path laid by the other, and by yourself! DECODE PURPOSE Decode is really more of a puzzle than a game, although you can still compete with your friends to see who can solve the puzzles the fastest. Each time you play, you are presented with a new puzzle to solve. The object is to figure out the computer’s secret code in as few guesses as possible. The program gives you information about the accuracy of each of your guesses. By carefully selecting your guesses to make use of the information you have, you can determine what the secret code must be in a surprisingly small number of guesses. Five or six is usually enough. The first few times you try, you will probably require quite a few more guesses than that, but with practice, you’ll discover that you can learn a lot more from each guess than you originally thought. HOWTO USE IT The program starts off by displaying a brief introduction. Here are some more details. The program selects a secret code for you to figure out. The code is a four-digit number that uses only the digits 1 through 6. For exam¬ ple, your ATARI might pick 6153 or 2242 as a secret code. 106 Basic Programs for the Atari Your object is to guess the code in the fewest possible guesses. After each of your guesses, the program tells you a “black” and a “white” number. The black number indicates the number of digits in your guess that were correct—the digit was correct and in the correct position. So, if the secret code is 6153 and your guess is 4143, you will be told that black is 2 (because the 1 and the 3 are correct). Of course, you aren’t told which digits are correct. That is for you to figure out by making use of the information that you get from other guesses. Each of the white numbers indicates a digit in your guess that is correct, but which is in the wrong position. For example, if the secret code is 6153 and your guess is 1434, you will be told that white is 2. The 1 and 3 are correct, but in wrong positions. The white number is determined by ignoring any digits that ac¬ counted for a black number. Also, a single position in the secret code or guess can only account for one black or white number. These facts become significant when the secret code and/or your guess have duplicate digits. For example, if the code is 1234 and your guess is 4444, there is only one black, and no whites. If the code is 2244 and your guess is 4122, there are no blacks and three whites. This may sound a little tricky, but you will quickly get the hang of it. At any time during the game, you can ask for a “SUMMARY” by entering an S instead of a guess. This causes the program to clear the screen and display each guess (with the corresponding result) that has occurred so far. Also, if you get tired of trying and want to give up, you can enter a Q (for “quit”) to end your misery and find out the answer. Otherwise, you continue guessing until you get the code right (four black, zero white), or until you have used up the maximum of 12 guesses. DECODE 107 The program displays an introduction, chooses its secret code, and asks for the operator’s first guess. After the operator makes a guess, the program responds with a “black” and a “white” number, and asks for the second guess. Later in the same game, the operator asks for a summary, then makes the guess that turns out to be correct. The program acknowledges that the guess is correct and asks about trying another game. 108 Basic Programs for the Atari PROGRAM LISTING 10 REM DECODE 15 REM COPYRIGHT 1984 DILITHIUM PRESS 30 D=6:P=4:L=12 40 DIM GG,6,C,B*(40>,A*(40) , X (L) 50 GOSUB 650 60 GOSIJB 220: GOSUB 240 65 TRAP 1000 70 PRINT "GUESS NUMBER ";G; H INPUT A* 100 IF A*="S" THEN 300 110 IF A*="Q" THEN 380 120 IF LEN) 150 NEXT J 160 TRAP 40000 170 GOSUB 470 ISO GOSUB 610 190 IF BCG)=P THEN 800 200 G«G+i:IF G>L THEN 940 210 GOTO 65 220 G=1:FOR 0=1 TO P:C(0>=0:NEXT 0 230 RETURN 240 FOR 0 = 1 TO P 250 R=INT; 340 PRINT J;" 345 FOR K=1 TO P:PRINT GG 500 X(J):=CCJ) 510 IF X(J)=G< J) THEN B=B+1:G=0 520 NEXT J 530 FOR J = 1 TO P 540 H=0:FOR K=i TO P 550 IF X(J)=0 THEN 580 560 IF C(J)<>G(K) THEN 580 570 H=1: 6 (K>=0:X(J)=0 580 NEXT K:W=W+H 590 NEXT J 600 RETURN 610 B:POKE 82,2 660 PRINT " **** DECODE *#**" 670 PRINT :PRINT 680 PRINT "FIGURE OUT A ";P;" POSITION CODE" 700 PRINT "USING THE DIGITS 1 THRU ";D 710 PRINT -.PRINT 720 PRINT "’BLACK’ INDICATES A CORRECT DIGIT" 730 PRINT "IN THE RIGHT POSITION." 740 PRINT 750 PRINT 760 PRINT "’WHITE’ INDICATES SOME OTHER CORRECT" 770 PRINT "DIGIT, BUT IN THE WRONG POSITION." 780 PRINT .-PRINT 790 RETURN BOO PRINT "YOU GOT IT IN "jG;" GUESSES" 810 IF G<5 THEN B$ : = "OUTSTANDING! " B20 IF G=5 OR G=6 THEN B*="PRETTY GOOD" 830 IF G=7 THEN B$~"NUT BAD" B40 IF G =8 THEN B*="NOT TOO GREAT" 850 IF G >8 THEN B*«"PRETTY BAD" B60 PRINT :PRINT "...THAT’S ";B* 870 PRINT B80 PRINT "WANT TO TRY AGAIN " 5 :INPUT A* 890 IF LEN(A*)=0 THEN 880 900 IF A*a,l)="Y“ THEN 50 910 IF A*(1,1X>"N" THEN 880 920 PRINT -.PRINT "COWARD. ": PRINT 930 END 940 PRINT 950 PRINT "THAT’S YOUR LIMIT OF GUESSES" 960 PRINT 970 PRINT "MY CODE WAS "s:FOR J=1 TO P:PRINT C , Q* (40) , Pit (40) 120 OPEN #1,4,0, "K: " 150 A=0:H=A:T—A:B*=CHR*(160) 160 C*="*":BZ=253 200 GRAPHICS 5:SETC0L0R 2,0,0: POKE 752,1: POKE 82 ,2:PRINT CHR*(125):GOSUB 2080:C0L0R C 210 XS=6:YS=2 220 GOSUB 2200:GOSUB 2100 230 PRINT CHR*(125>:PRINT "HOW MUCH NEEDED TO WI N":PRINT 235 TRAP 6000 240 PRINT "(BETWEEN 50-500 IS BEST)";:INPUT W 250 W--INT (W) : IF WOO THEN 230 260 GOSUB 2080:GOSUB 2500 270 IF Q>0.5 THEN 500 300 T=0 310 GOSUB 2700:T=T+R1+R2:IF F>0 THEN T=0 320 IF F=2 THEN H»0 330 GRAPHICS 5:SETC0L0R 2,0,0:POKE 752,1:PRINT C HR*(125) 335 PRINT :PRINT " YOU’RE ROLLING" 340 P*="YOU":GOSUB 3200 350 IF F>0 THEN P*="ME":GOSUB 1560:DL=400:GOSUB 2050:GOTO 500 360 GOSUB 1300:IF Q*="R" THEN 310 370 H=H+T:IF H>=W THEN 900 500 T=0 510 GRAPHICS 5:SETCOLOR 2,0,0:POKE 752,1:PRINT C HR*(125):GOSUB 2700 520 T=T+R1+R2:IF F>0 THEN T=0 530 IF F=2 THEN A*0 540 PRINT :PRINT " I’M ROLLING" 550 P*~" I " ; GOSUB 3200 560 IF F>0 THEN P*="YOU":GOSUB 1560:DL=400:GOSUB 2050:GOTO 300 570 POSITION XS,YS:GOSUB 5000 580 IF AD-l THEN PRINT "I’LL ROLL AGAIN":DL-250: 60SUB 2050:GOTO 510 590 PRINT "I’LL STOP WITH THIS":A«A+T 600 IF A>=W THEN DL=350:GOSUB 2050:GOTO 900 610 YS-YS+2:P*="YQU":GOSUB 1560:DL«350:GOSUB 205 0:GOTO 300 900 XS=2:YS=2:PRINT CHR*(125):P*="WE":T=0:GOSUB 1010 910 POSITION 3,16:IF A>=W THEN PRINT "I WIN — S KILL TRIUMPHS AGAIN" 920 IF H>=W THEN PRINT "YOU WIN — IT WAS SHEER LUCK" 118 Basic Programs for the Atari 930 IF A>=W THEN PRINT CHR*<253) 940 IF H>=W THEN BL=250: GOStJB 2000 945 POSITION 3,18:PRINT "HOW ABOUT ANOTHER GAME (Y OR N)";:INPUT Q*:IF Q$="Y" THEN 150 950 GRAPHICS OsEND 1000 GRAPHICS OrSETCOLOR 2,0,0:F'0KE 752,1:PRINT CHR*(125):XS=2:YS=9 1010 POSITION XS,YS 1020 FOR J=1 TO 11:PRINT B*;:NEXT J 1030 PRINT " SCOREBOARD 1040 FOR J=1 TO 11:PRINT B$;:NEXT J:PRINT 1050 FOR J=1 TO 9:PRINT B*;:POSITION 35,PEEK<84> :PRINT B$:NEXT J 1060 FOR J=1 TO 34:PRINT B*;:NEXT J 1080 POSITION XS+ 6 ,YB+2 1090 PRINT W:" POINTS NEEDED TO WIN"; 1100 POSITION XS+1, YS+3: FOR J==l TO 32 1110 PRINT ;:NEXT J 1120 POSITION XS+2.YS+4 1130 PRINT "POINTS SCORED";: GNPEEK(84):POSITION 22,0 1135 PRINT "YOU";:POSITION 30,0:PRINT "ME" 1140 POSITION XS+3,YS+5 1150 PRINT "BEFORE THIS";:Q=PEEK(84):POSITION 22 ,13 1155 PRINT CHR*<18);CHR*<18);CHR*<18);:POSITION 30,0:PRINT CHR*(18>;CHR*<18) 1160 POSITION XS+5,YS +6 1170 PRINT "SERIES";:POSITION XS+20,YS+ 6 :PRINT H 1180 POSITION XS+28,YS+6:PRINT A; 1190 POSITION XS+1,YS+7:FOR J~l TO 32 1200 PRINT ;:NEXT 3:P0SITI0N XS+2,YS+8 1210 PRINT F'$; " HAVE " 5 T 5 " POINTS THIS SERIES"; 1220 RETURN 1300 POSITION XB-2,YS 1310 PRINT " (P=F'ASS DICE - R=ROLL AGAIN)" 1320 POSITION XS-2.YS+2 1330 PRINT "YOUR DECISION "; 1340 GET #1,0:Q$-CHR$(Q) 1350 IF Q*="R" OR Q$="F" THEN RETURN 1370 GOTO 1340 1400 FOR Y=YS-2 TO Y8+2 STEP 4 1410 FOR X=XS-2 TO XS+2:GOSUB 1500:NEXT X 1420 FOR X=XS+7 TO XS+11:GOSUB 1500:NEXT X:NEXT Y 1430 FOR X=XS—2 TO XS+2 STEP 4 1440 FOR Y=YS--2 TO YS+2: GOSUB 1500: NEXT Y: NEXT X 1460 FOR X=XS+7 TO XS+U STEP 4: FOR Y=YS-2 TO YS +2 1470 GOSUB 1500:NEXT Y:NEXT X:RETURN 1500 POSITION X,Y:PRINT B$:RETURN GROAN 119 1520 POSITION X,Y:PRINT "G R 0 A N";:RETURN 1540 POSITION X,Y:PRINT "DESPAIR !";:RETUR N 1560 POSITION XS,YS:PRINT "DICE PASS TO ";P*:RET URN 1600 POSITION XS,YS:PRINT C*;:RETURN 1610 POSITION XS -1, YS—1: PRINT CU>; 1620 POSITION XS+l.YS+l:PRINT C*; :RETURN 1630 SOSUB 1600:GOSUB 1610 s RETURN 1640 POSITION XS+1,YS-1SPRINT C*; 1650 POSITION XS-1,YS+1:PRINT C*;:GQSUB 1610s RET URN 1660 GOSUB 1600:GOSUB 1640:RETURN 1670 POSITION XS-1,YSs PRINT C*;:POSITION XS+1,YS 1680 PRINT C*;:GOSUB 1640:RETURN 1700 POSITION XS-1,YS—1:PRINT CHR*(9);CHR*(32);C HR*(15); 1710 POSITION XS.YS:PRINT CHR*<96); 1720 POSITION XS-1,YS+1 1730 PRINT CHR*(17);CHR*(18);CHR*(5);s RETURN 1 BOO LX=XS-L: RX=XS+L: UY=YS—Ls LY=YS+L 1810 PLOT LX, UY: DR A WTO RX,UY:PLOT LX, l.Y: DRAWTO R X, LY 1820 PLOT LX,UY:DRAWTO LX,LY:PLOT RX,UY:DRAWTO R X,LY:RETURN 1900 PLOT XS,YS:RETURN 1910 PLOT XS—4,YS—5 s PLOT XS+4,Y5+5:RETURN 1920 GOSUB 1900:GOSUB 1910s RETURN 1930 PLOT XS—4,YS+SsPLOT XS+4,YS-5:GOSUB 1910:RE TURN 1940 GOSUB 1900:GOSUB 1930:RETURN 1950 PLOT XS—4,YS:PLOT XS+4,YS:GOSUB 1930:RETURN 1960 GOSUB 1940:FOR 0=1 TO 3 1970 PLOT XS-J-l,YS+J+2:PL0T XS+J+1,YS+J+2 1980 NEXT J:PLOT XS-1,YS+3:PLOT XS,YS+3:PLOT XS+ 1, YS+3 1990 RETURN 2000 SOUND 0,230,14,14:FOR 0=1 TO BL:NEXT OsSOUN D 0,0,0,0:RETURN 2050 FOR J=1 TO DL:NEXT 0:RETURN 2080 0=2:RETURN 2100 BL=300:GOSUB 2000 2110 RETURN 2150 BL=200:GOSUB 2000:RETURN 2200 PLOT XS,YS:DRAWTO XS+3,YS:PLOT XB,YS:DRAWTO XS,YS+4 2210 PLOT XS,YS+4:DRAWTO XS+3,YS+4:PLOT XS+3,YS+ 2:DRAWTO XS+3,YS+4 2220 PLOT XS+2,YS+2:PLOT XS+6,YS:DRAWTO XS+6,YS+ 4 2230 PLOT XS+6,YS:DRAWTO XS+9.YS:PL0T XS+6,YS+2: DRAWTO XS+9,YS+2 120 Basic Programs for the Atari 2240 PLOT XS+9,YS+1:PLOT XS+8,YS+3 2250 PLOT XS+9,YS+4:PLOT XS+12,YSsDRAWTO XS+15,Y B 2260 PLOT XS+12,YS+4:DRAWTO XS+15,YS+4:PLOT XS+1 2,YS:DRAWTO XS+12,YS+4 2270 PLOT XS+15,YS:DRAWTO XS+15,YS+4:PLOT XS+18, YS:DRAWTO XS+18,YS+4 2.280 PLOT XS+21.YS: DRAWTO XS+21, YS+4: PLOT XS+18, YS:DRAWTO XS+21,YS 2290 PLOT XS+18,YS+2:DRAWTO XS+21,YS+2:PLOT XS+2 4,YS:DRAWTO XS+24,YS+4 2300 PLOT XS+27.YS:DRAWTO XS+27,YS+4:PLOT XS+25, YS:DRAWTO XS+25,YS+2 2310 PLOT XS+26,YS+2:DRAWTO XS+26,YS+4:RETURN 2500 DL--20: X=20: Y--33 2510 PRINT CHR*<125>;"NOW A COIN TOSS FOR THE FI RST ROLL" 2520 PRINT :PRINT "THE COIN IS IN THE AIR AND... H • 2530 Y=Y-2:COLOR C:GOSUB 2650:80SUB 2050 2540 COLOR 0:G0SUB 2650:COLOR C:GOSUB 2660:BOSUB 2050 2550 COLOR 0:80SUB 2660:IF Y>9 THEN 2530 2560 Y=Y+2:COLOR C:GOSUB 2650:G0SUB 2050 2570 COLOR 0:G0SUB 2650:COLOR C 2580 COLOR 0:G0SUB 2660:IF Y<33 THEN 2560 2590 COLOR C:Y=33:GOSUB 2650:BL=50:GOSUB 2000 2600 0=RND(1):Q$="YOU":IF Q>0.5 THEN Q*="I" 2610 PRINT CHR*<125):PRINT 2620 PRINT CHR$ (32) ; CHR'i (32) ; Qtt; " GET THE FIRST ROLL" 2630 DL=300:GOSUB 2050:RETURN 2650 PLOT X-2,Y:DRAWTO X+2,Y:RETURN 2660 PLOT X,Y-2:DRAWTO X,Y+2:RETURN 2700 Rl-INT(RND(1)*6)+1:R2=INT(RND(1)*6)+l 2710 F=0:IF Rl=l THEN F=1 2720 IF R2=l THEN F=F+1 2730 RETURN 2800 FOR J*0 TO 14:L=J/2 2.810 XS=9:YS=8+J: COLOR C: GOSUB 1800 2820 XS-30:GOSUB 1800:COLOR 0:XS=9:GOSUB 1800 2830 XS=30:GOSUB 1800:NEXT J 2840 COLOR C:XS=9:GOSUB 1800 2850 XS=30:GOSUB 1800:RETURN 3000 XS-14:YS-5:GOSUB 1400:Q-R1:GOSUB 3100 3010 XS=23:0=R2:GOSUB 3100 3020 IF Rl-1 THEN X=2:Y=YS:GOSUB 1520:GOSUB 2150 :DL=100:GOSUB 2050 3030 IF R2=l THEN X=27: Y=YS: GOSUB 1520-.GOSUB 215 0 3050 IF F<2 THEN RETURN 3060 DL=200:GOSUB 2050 3070 X=12:Y=1:GOSUB 1540:BL=300 GROAN 121 3080 SOUND 0,230,12,14:FOR J=1 TO BL:NEXT J:SOUN B 0,0, 0,0: RETURN 3100 ON Q SOSUB 1700,1610,1630,1640,1660,1670:RE TURN 3200 GOSUB 2080:GOSUB 2800: XS=9: YS=22: CJ=R1: GOSUB 3250 3210 XS=30:Q=R2:GOSUB 3250:GOSUB 2080:COLOR C:XS —6 3220 YS=2:IF F>0 THEN GOSUB 2200:GOSUB 2150:GOTO 3240 3230 DL=400:GOSUB 2050 3240 GOSUB 1000:GOSUB 3000:XS==6:YS=21:RETURN 3250 ON <2 GOSUB 1960,1910,1920,1930,1940,1950:RE TURN 5000 V=A+T:IF V>=W THEN 5100 5010 IF W-HC10 THEN 5110 5020 IF A>=H THEN CT=T/25:GOTO 5050 5030 IF VCT THEN 5110 5100 AD=0:RETURN 5110 AD~1:RETURN 6000 IF PEEK<195)=8 THEN PRINT "ILLEGAL ENTRY":T RAP 6000:GOTO 240 EASY CHANGES 1. If you wish to set the program for a fixed value of the winning score, it can be done by changing line 230 and deleting line 240. Simply set W to the winning score desired. For example: 230 W=100: PRINT CHR$(125) would make the winning score 100. (Don’t forget to delete line 240.) 2. The rolling dice graphics display before each roll can be elimi¬ nated by deleting lines 2810, 2820, 2830, 2840 and changing lines 2800 and 2850 as follows: 2800 YS=22:L=7:COLOR C 2850 XS=9:GOSUB 1800:XS=30: GOSUB 1800: RETURN This has the effect of speeding up the game by showing each dice roll immediately. 3. After you play the game a few times, you may wish to change the “pacing” of the game; i.e., the time delays between various mes¬ sages, etc. To speed up the game try: 122 Basic Programs for the Atari 2050 DL=DL/2:FOR J= 1 TO DLiNEXT J 2055 DL=2 * DL: RETURN To slow down the pacing, try: 2050 DL=2*DL:FORJ=l TODLrNEXTJ 2055 DL=DL/2: RETURN 4. The color of the graphic displays can be altered by modifying line 2080. Try: 2080 C = 1: RETURN to see the effect. 5. As currently written, the program should fit in a 16K ATARI 400 (see Appendix 1). If it doesn’t, however, it can be made to squeeze into this system configuration by: implementing Easy Change #2 above; deleting lines 2210 through 2310; deleting lines 2540 through 2590; and adding 2200 RETURN 2500 REM 2530 DL=2000:GOSUB 2050 These changes remove some of the program’s fancy effects but do not materially change the performance of the game. MAIN ROUTINES 110- 160 Initializes constants. 200- 270 Initial displays. Gets winning score. 300- 370 Human rolls. 500- 610 ATARI rolls. 900- 950 Ending messages. 1000-1220 Displays scoreboard 1300-1370 Asks user for re-roll decision. 1400-1470 Draws scoreboard dice outline. 1500-1560 Subroutines to print various messages. 1600-1730 Draws scoreboard dice faces. 1800-1820 Draws graphics dice outline. 1900-1990 Draws graphics dice faces. 2000-2150 Delay loops and buzzer tone control loops. 2200-2310 Draws graphics “groan.” 2500-2660 Performs coin toss. 2700-2730 Determines dice roll. 2800-2850 Controls graphics dice rolling. GROAN 123 3000-3100 Controls scoreboard display. 3200-3250 Subroutine to control scoreboard and dice faces. 5000-5110 ATARI’S strategy. Sets AD=0 to stop rolling or AD=1 to continue rolling. 6000 Input error trap. Display error message and try again. MAIN VARIABLES A H T B$ C$ c XS, YS W Q Rl, R2 F DL BL P$ Q$ AD J,K X,Y L LX, RX UY, LY V CT Previous score of ATARI. Previous score of human. Score of current series of rolls. String of one blank character. String of one asterisk. Graphics color. Horizontal, vertical reference print position. Amount needed to win. Work variable. Outcome of roll for die 1, die 2 Result of roll (0=no frown, l=one frown, 2=double frown). Delay length. Buzzer length (length of tone generation). String name of current roller. Work string variable. ATARI strategy flag (0=stop rolling, 1 =roll again). Loop indices. Horizontal, vertical print position. Half length of a die side. Left, right position of a die face. Upper, lower position of a die face. Score ATARI would have if it passed the dice. Cutoff threshold used in ATARI’S built-in strategy. SUGGESTED PROJECTS 1. The computer’s built-in strategy is contained from line 5000 on. Remember, after a no-frown roll, the ATARI must decide whether or not to continue rolling. See if you can improve on the current strategy. You may use, but not modify, the variables A, T, H, and W. The variable AD must be set before returning. Set AD=0 to mean the ATARI passes the dice or AD=1 to mean the ATARI will roll again. 124 Basic Programs for the Atari 2. Ask the operator for his/her name. Then personalize the messages and scoreboard more. 3. Dig into the workings of the graphics routines connected with the dice rolling. Then modify them to produce new, perhaps more realistic, effects. JOT PURPOSE JOT is a two-player word game involving considerable mental deduction. The ATARI will play against you. But be careful! You will find your computer quite a formidable opponent. The rules of JOT are fairly simple. The game is played entirely with three-letter words. All letters of each word must be distinct—no repeats. (See the section on Easy Changes for further criteria used in defining legal words.) To begin the game, each player chooses a secret word. The re¬ mainder of the game involves trying to be the first player to deduce the other’s secret word. The players take turns making guesses at their opponent’s word. After each guess, the asker is told how many letters (or hits) his guess had in common with his opponent’s secret word. The position of the letters in the word does not matter. For example, if the secret word was “own,” a guess of “who” would have two hits. The winner is the first person to correctly guess his opponent’s secret word. HOWTO USE IT The program begins with some introductory messages while ask¬ ing you to think of your secret word. It then asks whether or not you wish to make the first guess. This is followed by you and the ATARI alternating guesses at each other’s secret word. 126 Basic Programs for the Atari After the computer guesses, it will immediately ask you how it did. Possible replies are 0, 1, 2, 3, or R. The response of R (for right) means the ATARI has just guessed your word correctly-a truly humbling experience. The numerical replies indicate that the word guessed by the ATARI had that number of hits in your secret word. A response of 3 means that all the letters were correct, but they need to be rearranged to form the actual secret word (e.g., a guess of “EAT” with the secret word being “TEA”). After learning how it did, the computer will take some time to process its new information. If this time is not trivial, the ATARI will display the message: “I’M THINKING” so you do not suspect it of idle daydreaming. If it finds an inconsistency in its information, it will ask you for your secret word and then analyze what went wrong. When it is your turn to guess, there are two special replies you can make. These are the single letters S and Q. The S, for summary, will display a table of all previous guesses and corresponding hits. This is useful as a concise look at all available information. It will then prompt you again for your next guess. The Q, for quit, will simply terminate the game. When not making one of these special replies, you will input a guess at the computer’s secret word. This will be, of course, a three- letter word. If the word used is not legal, the computer will so inform you. After a legal guess, you will be told how many hits your guess had. If you correctly guess the computer’s word, you will be duly congratulated. The ATARI will then ask you for your secret word and verify that all is on the “up and up.” JOT 127 J 0 T MOMENT PLEASE ’HANKS. NOW LET'S EACH THINK OF OUR SECRET WORD (THIS TAKES ME A WHILE . . , 3 I ' U E ALMOST GOT IT ... 0 K , DO YOU WANT TO GO FIRST ? N The player and the computer each select their secret words. The com¬ puter is given the first guess. (THIS TAKES ME A WHILE I 1 0 E ALMOST GOT IT ... OK, DO YOU WANT TO GO FIRST ?N MY GUESS IS -- FLY HOW DID I DO (0-3 OR R3 ?I I'M THINKING ... YOUR GUESS (OR 5 OR 03 3 OF HITS IS 1 MY GUESS IS — LEG HOW DID I DO (0-3 OR R3?0 YOUR G U E 35 (OR 5 OR 03 « OF HITS IS 1 MY GUESS IS — ICY HOW DID I DO (0-3 OR R3 ?0 YOUR GUESS (OR 5 OR 03 The computer and player exchange the first few guesses and their results with each other. 128 Basic Programs for the Atari OUR 3UE55 COP 5 OR 0 3 "TIE rX OF HITS I 5 1 Later in the same game, the player requests a summary before making a guess. The computer, however, guesses correctly to win the game. After revealing its secret word, the computer offers another game but the player has had enough. PROGRAM LISTING 10 REM JOT 15 REM COPYRIGHT 1984 DILITHIUM PRESS 140 M==25:N=406 150 DIM A*(4*,G1*<4*M>,G2*(5*M>,H1,H2,M2*(1),M3*(1>,P*(5 ) ' 200 G1=0:62=0 210 L“N 250 PRINT CHR*(125):POSITION 14,PEEK(84):PRINT " J 0 T":PRINT 2AO PRINT "JUST A MOMENT PLEASE ."sGOSUB 300 0: Q -1 NT(RND(1)*N)+1:PR INT 270 PRINT "THANKS, NOW LET’S EACH THINK":PRINT " OF OtJR SECRET WORD" 280 PRINT :PR INT "(THIS TAKES ME A WHILE ...)" 290 GOSUB 2200:M*=A*(0*3,0*3+2):PRINT :PRINT "OK 300 PRINT "DO YOU WANT TO GO FIRST INPUT Q* 305 IF 0*=”" THEN 330 31.0 IF Q* (1, 1 > =”N" THEN 600 320 IF 0*(1,1)="Y" THEN 500 330 PRINT :PRINT ”** YES OR NO PLEASE **":PRINT :GOTO 300 500 PRINT :PRINT "YOUR GUESS (OR S OR Q) ";:INPU T P*: IF F’*'="S" THEN GOSUB 1000: GOTO 500 JOT 129 510 IF P*= M Q" THEN 3210 520 IF P*=M* THEN G1=G1+1:Gl*(Gl*3,Gl*3+2)=P*:H1 (Gl)=9:GOTO 3400 530 GOSUB 1800:IF F=0 THEN PRINT "* THAT’S NOT A LEGAL WORD - RETRY * 11 '.GOTO 500 540 Q*=M*:GOSUB 2600:Q*=P*:GOSUB 1500 550 PRINT "# OF HITS IS ";Q 560 G1=G1+1:G1*=Q 570 IF G1=M THEN 3600 600 Q*=A*» G2=G2+1:G2*100 THEN PRINT SPRINT "I'M TH INKING ..." 670 H2:J=0:GOSUB 2600: L=L-1:IF L<1 THEN 900 BIO J=J+1:IF J >L THEN 1500 S20 Q*=A*:GOSUB 1500 B30 IF Q=H THEN 810 B40 A=J:B=L:GOSUB 2400:L=L-1 B50 IF L<1 THEN 900 860 IF L>=J THEN 820 B70 RETURN 900 PRINT :PRINT "** SOMETHING’S WRONG **" 910 PRINT SPRINT "** WHAT’S YOUR SECRET WORD";:I NPUT P*:GOSUB 1800 920 IF F=0 THEN PRINT SPRINT "* ILLEGAL WORD — I HAD NO CHANCE *":GQTO 1200 930 PRINT -.PRINT "** YOU GAVE A BAD ANSWER SOMEW HERE **" 940 PRINT "** CHECK THE SUMMARY **":GOSUB 1000 950 GOTO 1200 1000 PRINT :Q=G1:IF G2>G1 THEN «=G2 1010 IF 0=0 THEN PRINT "** NO GUESSES YET **":RE TURN 1020 FOR J = 1 TO 36:PRINT "-";:NEXT J:PRINT 1030 PK—PEEK(84)sPRINT "YOUR GUESSES"; 1040 POSITION 17,PKsPRINT "SUMMARY"; 1050 POSITION 27,PKsPRINT "MY GUESSES"sPK=PEEK<8 4) 1060 PRINT "WORD";:POSITION 9,PK:PRINT "HITS";:P OSITION 27,PK:PRINT "WORD HITS" 1070 FOR 0 = 1 TO Qs PK=PEEK <84):K=1sIF J>9 THEN K= O 1080 IF J>G1 THEN POSITION 19+K,PK:PRINT J;:POSI TION 28,PK 130 Basic Programs for the Atari 1085 IF J >61 THEN PRINT G2*POSITION 34,PK:PR INT H2(J):GOTO 1110 1090 IF J >62 THEN PRINT CHR*(32);61*(J*3,J#3+2); :POSITION 10,PK 1095 IF J >62 THEN PRINT H1(J>;:POSITION 19+K.PK: PRINT J:60T0 1110 1100 PRINT CHR*<32>;61*POSITION 10, PK SPRINT H1(J >;:POSITION 19+K,PK 1105 PRINT J ;s POSITI ON 28,PK:PRINT G2* s Q=0: 60SUB 1600 1510 P*==Q‘4> (2,2) s GOSUB 1600 1520 P*=Q* < 3,3)s 60SUB 1600:RETURN 1600 IF P*=M1* OR P*~M2!6 OR P*=M3* THEN Q==0+1 1610 RETURN 1800 F~0 1810 FOR J=1 TO N 1820 IF A* =A* (A*3, A*3+2 >:A*(A*3,A*3+2)=Q*sRETURN 2600 M1*—Q$ 8 THEN AD=AD-B 220 AR=R:AX=X:AY=Y 230 X=BX:Y=BY:D=BD:GOSUB 1000 235 IF BD>8 THEN BD=BD-8 240 BR=R:BX=X: BY=Y 245 IF AR=1 OR BR=1 THEN 400 250 GOSUB 900 260 C=PEEK :POKE F,S 270 IF C=255 OR 070 THEN 210 2£io j,i=a 290 IF JJ=0 THEN 210 300 IF J J=17 THEN BD=BD+8:G0T0 210 310 IF J,J = 18 THEN AD=AD+8:GOTO 210 320 IF ,1 J<9 THEN AD=JJ:GOTO 210 330 IF J J>8 THEN BD=JJ-8:G0T0 210 400 GOSUB 700:X=AX:Y=AY 410 IF AR=1 THEN 420 415 X=BX:Y=BY 420 FOR J=1 TO 100 430 COLOR Z:PLOT X,Y 440 FOR K=1 TO 10:NEXT K 450 COLOR 0:PLOT X,Y 460 FOR K=1 TO 10:NEXT K 470 NEXT J 490 GOTO 125 600 GRAPHICS 0:PRINT “OBSTACLE":PRINT 610 PRINT "NAME OF PLAYER ON LEFT INPUT AN* 620 PRINT 630 PRINT "NAME OF PLAYER ON RIGHT INPUT BN* 650 RETURN 700 PRINT 710 SOUND 0,100,10,10:F0R J=1 TO 200:NEXT J:SOUN D 0,0,0,0 720 IF AR=0 OR BR=0 THEN 740 730 PRINT "YOU BOTH LOSE!!!!!":RETURN 740 R*=AN*:IF AR=1 THEN R*=BN* 750 IF R*=AN* THEN L=L+1 760 IF R*=BN* THEN M=M+1 770 PRINT R*;" WINS!!!!" 780 PRINT AN*;" HAS WON ":L;" GAMES":PRINT BN*;" HAS WON ";M;" GAMES" 790 RETURN 900 COLOR A:PLOT AX,AY 910 COLOR B:PLOT BX,BY 915 FOR ,J = 1 TO 100: NEXT J 920 RETURN 940 GRAPHICS 5 950 COLOR E 140 Basic Programs for the Atari 960 PLOT 0,0s DRAWTO 79,0:DRAWTO 79,39:DRAWT0 0,3 9s DR AWTO 0,1s DRAWTO 7S,1 980 DRAWTO 78,38sDRAWTO 1,38:DRAWTO 1,1 990 RETURN 1000 ON D GOTO 1010,1020,1030,1040,1050,1060,107 0,1080,1090,1100,1110,1120,1130,1140,1150,1160 1010 Y=Y-1s GOTO 1200 1020 Y=Y+1:GOTO 1200 1030 X*X-1:GOTO 1200 1040 X=X+1:GOTO 1200 1050 Y=Y-1s X=X-1: GOTO 1200 1060 Y=Y-1:X=X+1s GOTO 1200 1070 Y=Y+1s X=X+1:GOTO 1200 1080 Y=Y+1:X=X-1:GOTO 1200 1090 Y=Y—2:GOTO 1200 1100 Y=Y+2:GOTO 1200 1110 X=X-2: GOTO 1200 1120 X=X+2:GOTO 1200 1130 Y=Y—2: X=X-2s GOTO 1200 1140 Y=Y-2:X*X+2s GOTO 1200 1150 Y=Y+2:X = X+2s GOTO 1200 1160 Y=Y+2sX=X-2:G0T0 1200 1200 LOCATE X,Y,W 1210 R-OsIF WOO THEN R-l 1220 RETURN 1300 FOR J=0 TO 70s A 160 N*=CHR*(32):B*=CHR*(32):B*(40)=B*:B*(2)=B* 170 T=0:D=0:GOSUB 670 180 A*=B* 190 Y=RND(0) 200 IF YRC THEN X=X + 1 220 IF XRM—W THEN X=RM-W 240 A*(X)=CHR*(96):A*(X+W)=CHR*(96) 250 POSITION 0,0: PRINT CHR* (157) :A* 260 POSITION P,V+1:PRINT N*;N* 270 S=PEEK(764):IF S=255 THEN 310 280 IF S=ML THEN P=P-1:G0T0 300 290 IF S=MR THEN P=P+1 300 POKE 764,255 310 LOCATE P,V,A:LOCATE P+1,V,Z 320 IF A< >32 OR Z THEN 360 330 POSITION P,V:PRINT K* 340 M=M+1:T=T+1 350 GOTO 180 360 D=D+1:POSITION P,V:PRINT CHR*(7);CHR*(6) 370 POSITION P,V+1:PRINT CHR*(6);CHR*(7) 380 FOR J=-14 TO 0 STEP 2 390 SOUND 0,200,4,14-ABS(J) 400 SOUND 1,255,4,14-ABS(J> 410 SOUND 2,255,4,14-ABS(J) 420 SOUND 3,150,4,14-ABS(J> 430 FOR K=1 TO 15:NEXT K 440 NEXT J 450 FOR J=0 TO 14 460 SOUND 0,200,4,14-J 470 SOUND 1,255,4,14-J 480 SOUND 2,255,4,14-J 490 SOUND 3,150,4,14-J 500 FOR K=1 TO 50:NEXT K 510 NEXT J 520 FOR J=0 TO 3:SOUND J,0,0,0:NEXT J 530 POSITION 0,22:PRINT B*:PRINT B*:PRINT B*:POK E 764,255 540 R*=" DAYS":IF D=1 THEN R*=" DAY" 148 Basic Programs for the Atari 550 PRINT N*;N*;"YOU WENT";N*;M;N$;"MILES FOR A TOTAL OF";N* 555 PRINT N*;N*;T;N$;"MILES IN";N$;D:R*;N*;"AND AN AVERAGE" 560 PRINT N*; NS;"OF";NS;INT <(T/D)* 100)/100;NS;"M ILES/DAY" 570 PRINT BS:PRINT NS;NS;NS;"OPTIONS" 580 PRINT " C - CONTINUE" 590 PRINT " R - RE-START" 600 PRINT " Q - QUIT" 610 POKE 764,255 620 S=PEEK<764):IF S=255 THEN 620 630 POKE 764,255:IF S=47 THEN 660 640 IF S=40 THEN M=0:T=0:D=0:GOSUB 770:GOTO 180 645 IF 8018 THEN 610 650 PR INT CHR*(125):M=0:GDSUB 940:GOTO 180 660 GRAPHICS 0:P0KE 82,2:END 670 COLOR 1 680 X=28:Y=1 690 FOR J=1 TO 6:Y=Y+1:PLOT X,Y:PLOT X+W*2,Y:NEX T J 700 FOR J=1 TO 6:X=X+2:Y=Y+1:PLOT X,Y:PLOT X+W*2 ,Y:NEXT J 710 FOR J=1 TO 12:X=X-2:Y=Y+1:PLOT X,Y:PLOT X+W* 2,Y:NEXT J 720 FOR ,1=1 TO 6: X = X+2: Y=Y+1: PLOT X,Y:PLOT X+W*2 ,Y:NEXT J 730 FOR J=1 TO 6:Y=Y+1:PLOT X,Y:PLOT X+W*2,Y:NEX T J 740 X=X+W:PLOT X,Y:Y=Y+1:PLOT X-1,Y:PL0T X,Y:PLO T X+l,Y:PLOT X,Y+1 750 PRINT :FOR J=1 TO 15:PRINT N*;:NEXT J:RESTOR E :FOR J=1 TO 8:READ K:PRINT CHR$(K);:NEXT J 755 DATA 210,207,193,196,210,193,195,197 760 FOR J=1 TO 1000:NEXT J 770 GRAPHICS 0:SETC0L0R 2,1,12:SETCOLOR 1,0,0:PQ KE 82,2:POKE 752,1 780 PRINT :PRINT :PRINT 790 PRINT N$;N$;"R 0 A D R A C E" BOO PRINT 810 PRINT "ROAD WIDTH (4 - 12) ":INPUT W:W=INT(W ) 820 IF W<4 OR W>12 THEN PRINT :PRINT ”** ILLEGAL ENTRY *t"i GOTO 800 830 PRINT :PRINT :PRINT "VISIBILITY CONDITIONS" 840 PRINT 850 PRINT " 1 - TERRIBLE" 860 PRINT " 2 - BAD" 870 PRINT " 3 - FAIR" ROADRACE 149 SBC PRINT " 4 - GOOD" 890 PRINT 900 PRINT "VISIBILITY 910 IF V<1 OR V>4 THEN PRINT SPRINT "** ILLEGAL ENTRY *#":GOTO 890 920 V=V*4+2 930 POKE 82,0 940 X =14:P=X+W/2-2 950 A*=B$:A*(X)=CHR*<96>:A$(X+W)=CHR*(96) 960 PRINT CHR$ (125) : FOR J=1 TO 23-.PRINT A$: NEXT J 970 POSITION F',V: PRINT K* 980 POSITION 3,5:PR INT CHR*(14);CHR*(14);CHR$<14 ) 990 POSITION 3, 6 :PRINT CHR*(22);N$;CHR*(2) 1000 POSITION 3,7:PRINT CHR*(22);N$;CHR*(2) 1010 POSITION 3,8:PRINT CHR*(22) 5 N*;CHR*(2) 1020 POSITION 3,9:PRINT CHR*(22);N$;CHR*(2) 1030 POSITION 3,10:PRINT CHR«(22);N*;CHR*(2) 1040 POSITION 3,11:PRINT CHR$(13);CHR*(13) 5 CHR$( 13) 1050 FOR K=1 TO 5 1060 FOR J=1 TO 200:NEXT J 1070 POSITION 4,5+K:PRINT CHR*(20) 1080 NEXT K 1090 RETURN EASYCHANGES 1. The amount of windiness in the road can be adjusted by changing the value of LC in line 125. Maximum windiness is achieved with a value of 0.5 for LC. To get a straighter road, make LC smaller. A value of 0 will produce a completely straight road. LC should lie between 0 and 0.5. To get a somewhat more winding road, you might change line 125 to read 125 LC=0.45 2. Experiment with a different color background by changing the value in line 670 and the SETCOLOR argument in line 770. Try 670 COLOR 2 770 GRAPHICS 0:SETCOLOR 2,6,12:SETCOLOR 1,0,0:POKE 82,2:POKE 752,1 150 Basic Programs for the Atari 3. The keys which cause the car to move left and right can be easily changed. You may wish to do this if you are left handed or find that two widely separated keys would be more convenient. The changes are to be made in line 140. The constants that are assigned to the variables ML and MR control which keys will cause the car to move left and right respectively. To find the correct constants, first type in this short program: 10 Q=PEEK (764):IFQ=255 THEN 10 20 POKE 764,255: PRINT Q Run the program, then press the key you wish to use to make the car move left. A number will appear on the screen. This number will be assigned to the variable ML. Run the program again and hit the key you want to cause the car to move right. Assign the number that now appears to the variable MR. If, for example, you want 1 to cause a left move and 9 to cause a right move, line 140 will become 140 ML=31:MR=48 4. Instead of the keyboard, a joystick can be used to control the car’s movements. We’ll assume stick 0 will be used. Now make the following changes to the main program: 140ML=11:MR=7 270 S = STICK(0) 280 IF S = ML THEN P=P -1 290 IF S=MR THEN P=P +1 The stick will now control the car. When the stick is neutral the car will continue straight up the road. If the stick is moved to the left, the car will continue to move left. If the stick is moved to the right, the car will continue to move right. MAIN ROUTINES 110- 170 180- 260 270- 350 360- 660 670- 790 800- 930 940- 970 980-1090 Variable initialization. Draws next road segment. Updates the car position. Processes end of race day. Routine to do initial graphics display. Gets road conditions from user. Initializes road. Routine to display starting light. ROADRACE 151 MAIN VARIABLES P W V M D T ML, MR LC, RC LM.RM R$ X, Y J, K A$,B$ A, Z S K$ N$ Current horizontal position of car. Road width. Visibility, vertical position of car. Miles driven on current day. Number of days of the race. Total miles driven for whole race. Peek constants to move road left, right. Random value cutoff to move road left, right. Leftmost, rightmost allowable road position. Work string. Horizontal, vertical position of road. Loop indices and work variables. Screen lines. Numeric values of LOCATE graphic characters. Peek argument. String representation of car. String of one blank character. SUGGESTED PROJECTS 1. Write a routine to evaluate a player’s performance after each colli¬ sion. Display a message rating him anywhere from “expert” to “back-seat driver.” This should involve comparing his actual miles achieved against an expected (or average) number of miles for the given road width and visibility. For starters, you might use Expected miles=5W 3 +50V —100 This formula is crude, at best. The coding can be done between lines 560 and 570. 2. Incorporate provisions for two players racing one at a time. Keep cumulative totals separately. After each collision, display the cur¬ rent leader and how far he is ahead. 3. Add physical obstacles or other hazards onto the road in order to increase the challenge. This can be done with appropriate state¬ ments after line 240. The program will recognize a collision if the car moves into any non-blank square. WARI PURPOSE Wari is an old game with roots that are much older. Its origins go back thousands of years to a variety of other similar games, all classified as being members of the Mancala family. Other variations are Awari, Oware, Pallanguli, Kalah, and countless other offshoots. The program matches you against the computer. You are probably going to lose a few games before you win one-the computer plays a pretty good game. This may hurt your ego a little bit, since Wari is purely a ski ll game (like chess or checkers). There is no element of luck involved, as would be the case with backgammon, for example. When you lose, it’s because you were outplayed. HOWTO USE IT When you start the program, the first thing it does is display the Wari board and ask you if you want to go first. The board is made up of twelve “squares” in two rows of six. Your side is the bottom side, numbered one through six from left to right. The computer’s side is on the top, numbered seven through twelve from right to left. At the start of the game, each square has four “stones” in it. There is no way to differentiate between your stones and the computer’s. They all look alike and will move from one side to the other during the course of play. The first player “picks up” all the stones in one of the squares on his side of the board and drops them, one to a square, starting with the next highest numbered square. The stones continue to be dropped 154 Basic Programs for the Atari consecutively in each square, continuing over onto the opponent’s side if necessary (after square number 12 comes square number 1 again). If the last stone is dropped onto the opponent’s side and leaves a total of either two or three stones in that square, these stones are captured by the player who moved, and removed from the board. Also, if the next-to-last square in which a stone was dropped meets the same conditions (on the opponent’s side and now with two or three stones), its stones are also captured. This continues backwards until the string of consecutive squares of two or three on the opponent’s side is broken. Regardless of whether any captures are made, play alternates back and forth between the two players. The object of the game is to be the first player to capture 24 or more stones. That’s half of the 48 stones that are on the board at the beginning of the game. There are a few special rules to cover some situations that can come up in the game. It is not legal to capture all the stones on the opponent’s side of the board, since this would leave the opponent with no moves on his next turn. By the same token, when your opponent has no stones on his side (because he had to move his last one to your side on his turn), you have to make a move that gives him at least one stone to move on his next turn, if possible. If you cannot make such a move, the game is over and counted as a draw. During the course of the game, it’s possible for a square to accumu¬ late 12 or more stones in it. Moving from such a square causes stones to be distributed all the way around the board. When this happens, the square from which the move was made is skipped over. So, the square moved from is always left empty. It takes the computer anywhere from fifteen seconds to about forty-five seconds to make a move, depending on the complexity of the board position. The word THINKING is displayed during this time, and a period is added to it as each possible move is evaluated in sequence (seven through twelve). Entering the word “END” instead of a square number ends the game. WARI 155 SAMPLE RUN The program starts off by drawing the playing “board” and asking who should move first. The operator decides to go first. W 0 R I COMPUTER 11 10 •3 7 4 4 4 5 5 5 4 4 4 4 0 5 YOU YOUR MO UE ? 5 THINKING.,.| The program asks for the operator’s move. He or she moves square number 5. The program alters the board accordingly and begins “think¬ ing” about what move to make. 156 Basic Programs for the Atari Later in the same game, the computer is about to move square 9, which will capture 2 or more stones and win the game. PROGRAM LISTING 10 REM WARI 15 REM COPYRIGHT 1984 DILITHIUM PRESS 120 J=1:K=1:Q=14:P=13:F=50:D=12 130 DIM T(Q),Y(Q),W(Q),V<6),E(6),B(Q) 135 DIM R$ <10),C$(24),D$(40) 140 ZB=RND<1):ZB=ZB/Q:ZA=0.25+ZB:ZB=0.25-ZB:1=1: GOSUB 750 150 FOR 0 = 1 TO D:B(3)=4:NEXT 0:B(P)=0:B(Q)=0:MN= 0:GOSUB 1200:GOSUB 900 160 GOSUB 990:PRINT "WANT TO GO FIRST"INPUT R* 165 IF LEN =T(0):NEXT 0:GOSUB 900 WARI 157 230 IF B < Q) <24 THEN 250 240 POSITION 3.22:PRINT "** I WIN **":GOTO 810 250 GOSUB 990:PRINT D*;D*:GOSUB 990:PRINT "YOUR MOVE INPUT R* 255 IF R$="END" THEN 040 260 R*=R*(1,1):IF R$<"1" OR R*>"6" THEN 330 265 M=VAL6 THEN R$="C":GOTO 380 370 FOR J=1 TO Q:Y:Y(0)=T

:M=M-6 400 C=M:N=Y(C) : FOR .3=1 TO N:C=C+1 410 IF C=P THEN C=1 420 IF C=M THEN C=C+1:G0T0 410 430 Y(C)=Y (0+1: NEXT J:Y(M>=0:L=C 440 IF L<7 OR Y(L)>3 OR Y(L)<2 THEN 460 450 Y(P)=Y:Y=0:L=L—1:GOTO 440 460 S=0:FOR J=7 TO D:S=S+Y(J):NEXT J 470 IF S=0 THEN M=-2:RETURN 480 IF R*="H" THEN FOR J=1 TO Q:T(J>=Y(0):NEXT J :RETURN 490 FOR 0=1 TO 6:T(0)=Y:NEXT J 500 T(Q)=Y(P):T(P)=Y(0):RETURN 510 FOR A=1 TO 6:M=A+6:IF B(M)=0 THEN E(A)=-F:GO TO 690 530 FOR J = 1 TO Q:T(J)=B <0):NEXT JiGOSUB 350 540 IF M<0 THEN E(A)=-F:GOTO 690 550 IF T23 THEN M=A+6:RETURN 560 FOR 0=1 TO Q:W>0 THEN FA=FA+1 620 IF T(J)<3 THEN FC=FC+1 630 IF T(J)>FD THEN FD=TFA THEN FA=E =0 THEN 840 850 IF R*(1,1)="Y" THEN 140 B60 IF R*<1,1><>"N" THEN 840 870 PRINT "SEE YOU LATER" B80 PRINT :END 900 POSITION 2,8 920 FOR J=0 TO 5s POSITION 4*J+3,8:PRINT B(12-J); sIF B(12-J)=0 THEN GOSUB 1100 930 NEXT JsPOSITION 27,8s PRINT "COMPUTER ";B(Q) 940 FOR J=0 TO 5 950 POSITION 4*J+3, llsF'RINT B<0 + l);sIF B(J + 1)=0 THEN GOSIJB 1100 960 NEXT Os POSITION 32,llsPRINT "YOU ";B(P> 970 RETURN 990 POSITION 3,19s RETURN 1050 POSITION 3,21sRETURN 1100 PRINT CHR*(32);:RETURN 1200 GRAPHICS OsPOSITION 16,lsPRINT "W A R I" 1210 PRINT 1220 POSITION 9,3s PRINT "COMPUTER"sPRINT 1230 FOR 0=0 TO SsPOSITION 4*0+3,5:PRINT 12-J;sN EXT 0 1240 POSITION 29,5s PRINT "CAPTURED"sPRINT CHR*<3 2);C*ssPOSITION 29,6:PRINT C*(1,B> 1250 PRINT sPRINT SPRINT sPRINT sPRINT SPRINT CH R*<32> s C$s PRINT 1260 FOR 0=0 TO SsPOSITION 4*0+3, 14; F'RI NT 0 + 1; :N EXT 0 1270 POSITION 12,16sPRINT "YOU" 1280 PRINT sRETURN 2000 PRINT "NO LEGAL MOVES." 2010 PRINT "GAME IS A DRAW." 2020 GOTO 840 EASY CHANGES 1. Want a faster-moving game against an opponent who isn’t quite such a good player? Insert the following two lines: WAR I 159 555 GOTO 600 665 E(A)=V(K):GOTO 690 In the standard version of the game, the computer looks at each of its possible moves and each of your possible replies when evaluat¬ ing which move to make. This change causes the computer to only look at each of its moves, without bothering to look at any of your possible replies. As a result, the computer does not play as well, but it takes only a few seconds to make each move. 2. If you are curious about what the computer thinks are the relative merits of each of its possible moves, you can make this change to find out. Change line 690 so it looks like this: 690 PRINT E(A)NEXT A:M=0:FA= -F: FOR J=1T0 6 This will cause the program to display its evaluation number for each of its moves in turn (starting with square seven). It will select the largest number of the six. A negative value means that it will lose stones if that move is made, assuming that you make the best reply you can. A value of negative 50 indicates an illegal move. A positive value greater than one means that a capture can be made by the computer, and it will come out ahead after your best reply. MAIN ROUTINES 120- 150 Initializes variables. Displays board. 160- 180 Asks who goes first. Evaluates answer. 190- 220 Determines computer’s move. Displays new board position. 230- 240 Determines if computer’s move resulted in a win. Dis¬ plays a message if so. 250- 300 Gets operator’s move. Checks for legality. Displays new board position. 310- 320 Determines if operator’s move resulted in a win. 330 Displays message if illegal move attempted. 350- 500 Subroutine to make move M in T array. 360- 390 Copies T array into Y array (inverts if computer is making the move). 400- 430 Makes move in Y array. 440- 450 Checks for captures. Removes stones. Checks previous square. 160 Basic Programs for the Atari 460- 470 480- 500 510- 710 750- 800 810- 880 900- 970 990 1050 1100 1200-1280 2000-2020 Sees if opponent is left with a legal move. Copies Y array back into T array. Subroutine to determine computer’s move. Subroutine to create graphics strings for board display. Displays ending message. Asks about playing again. Subroutine to display stones on board and captured. Subroutine to move cursor to “YOUR MOVE” position on screen. Subroutine to move cursor to “MY MOVE” position on screen. Subroutine to display one blank character. Subroutine to display Wari board (without stones). Displays message when computer has no legal move. MAIN VARIABLES Q, P, F, D Constant values of 14, 13,50 and 12, respectively. T, Y, W Arrays with temporary copies of the Wari board. V Array with evaluation values of operator’s six possible replies to computer’s move being considered. E Array with evaluation values of computer’s six possible moves. B Array containing Wari board. Thirteenth element has stones captured by operator. Fourteenth has computer’s. ZA, ZB Weighting factors for evaluation function. MN Move number. R$ Operator’s reply. Also used as switch to indicate whose move it is (C for computer, H for human). M Move being made (1-6 for operator, 7-12 for computer). Set negative if illegal. C Subscript used in dropping stones around board. L Last square in which a stone was dropped. S Stones on opponent’s side of the board after a move. A Subscript used to indicate which of the six possible com¬ puter moves is currently being evaluated. J, K Loop indices. N, XX Work variables. FA First evaluation factor used in determining favorability of board position after a move (indicates computer’s number of occupied squares). FB Second evaluation factor (total stones on computer’s side of the board). WARI 161 FC Third evaluation factor (number of squares with two or less stones). FD Fourth evaluation factor (number of stones in most popu¬ lous squares on computer’s side). FE Total stones on board. C$ String of graphics characters used to display the Wari board. D$ String of 32 blanks. SUGGESTED PROJECTS 1. Modify the program to declare the game a draw if neither player has made a capture in the past 30 moves. Line 300 adds one to the counter of the number of moves made. To make the change, keep track of the move number of the last capture, and compare the difference between it and the current move number with 30. 2. Modify the evaluation function used by the computer strategy to see if you can improve the quality of its play. Lines 600 through 660 examine the position of the board after the move that is being considered. Experiment with the factors and/or the weighting val¬ ues, or add a new factor of your own. 3. Change the program so it can allow two people to play against each other, instead of just a person against the computer. 162 Basic Programs for the Atari Section 4 Graphics Display Programs INTRODUCTION TO GRAPHICS DISPLAY PROGRAMS The ATARI computers are amazing machines. They have very useful graphics capabilities in addition to their other capacities. Pro¬ grams in the other sections of this book take advantage of these graphics to facilitate and “spice up” their various output. Here we explore the use of the ATARI'S graphic capabilities for sheer fun, amusement, and diversion. Ever look through a kaleidoscope and enjoy the symmetrical changing patterns produced? KALEIDO will create such effects with full eight-point symmetry. Two other programs produce ever-changing patterns but with much different effects. SPARKLE will fascinate you with a changing shim¬ mering collage. SQUARES uses geometric shapes to obtain its pleas¬ ing displays. WALLOONS demonstrates a totally different aspect of the ATARI. This program will keep you entertained with an example of computer animation. PURPOSE If you have ever played with a kaleidoscope, you were probably fascinated by the endless symmetrical patterns you saw displayed. This program creates a series of kaleidoscope-like designs, with each one overlaying the previous one. HOWTO USE IT There is not much to say about how to use this one. Just type RUN, then sit back and watch. Turning down the lights and playing a little music is a good way to add to the effect. Have a few friends bring their ATARIs over (all your friends do have ATARIs, don’t they?), and get them all going with KALEIDO at once. Let us know if you think you have set a new world’s record. Please note that we will not be responsible for any hypnotic trances induced this way. 166 Basic Programs for the Atari SAMPLE RUN One of the patterns generated by the KALEIDO program. PROGRAM LISTING lO REM KALEIDO 15 REM COPYRIGHT 1984 DILITHIUM PRESS 20 GOTO 100 50 COLOR R(0):PLOT X*2,Y*4:DRAWTO X*2,Y*4+3:PLOT X*2+l,Y*4+3:DRAWTQ X*2+1,Y*4 52 IF J=1 THEN RETURN 55 GOTO 900 fcO COLOR R (N) : PLOT X2#2, Y2*4: DRAWTO X2*2,Y2*4+3: PLOT X2*2+l,Y2*4+3:DRAWTO X2*2+1,Y2*4 65 RETURN 100 DIM R(16),ZY(B) 105 FOR J = 1 TO 8:ZY *8>+l 164 ZA=0 165 FOR T=1 TO 8s IF T=J THEN 167 166 IF R(.J)=R(T) THEN ZA=1 167 NEXT T 168 IF ZA=1 THEN 160 170 NEXT J 1630 D=-Ds K=1: L=19: IF D>0 THEN 200 190 K=19:L=1 200 FOR J=K TO L STEP D 210 X=A+J: Y=Bs GOSUB 50 220 X=A-J:GOSUB 50 230 X=A;Y=B+J:GOSUB 50 240 Y=B—J:GOSUB 50 250 X=A+JsY=B+JsGOSUB 50 260 X=A-J:Y=B-0:GOSUB 50 270 Y-B+J:GOSUB 50 280 X=A+J:Y=B-J:GOSUB 50 700 NEXT J 750 FOR J = 1 TO 500s NEXT J 760 FOR J=1 TO 8s R =A THEN 980 975 X2=X+NsGOSUB 60s GOTO 990 980 X2=X-NsGOSUB 60 990 X2=XsIF Y>=B THEN 1000 995 Y2=Y+NsGOSUB 60s GOTO 1010 1000 Y2=Y-NsGOSUB 60 1010 NEXT Ns RETURN 2000 COLOR R(N)s PLOT X2,Y2 2010 RETURN 3000 ZX—Os FOR (3=705 TO 712 3001 ZX=ZX+1 3005 Z=INT(RND(1)*15)+1 3009 7.A=0 3010 FOR J=1 TO 8:IF Z=ZY(J) THEN ZA=ls0=8 3011 NEXT J 3012 IF ZA=1 THEN 3005 3015 POKE Q,Z#16+ZX+6s ZY(ZX)=Z 3020 NEXT QsRETURN 168 Basic Programs for the Atari EASY CHANGES 1. To clear the screen before the next pattern about 20% of the time (chosen at random), insert this: 175 IF RND(l) <=.2 THEN GRAPHICS 10:POKE 704,0 For 50%, use .5 instead of .2, etc. 2. To cause only the outward patterns to be displayed, insert line 178 to say 178 D = —1 To cause only inward patterns, change it to say 178 D = 1 3. To alter the number of graphics colors used in the patterns, alter the value of M in line 130. Be sure it is an integer from 2 to 15. 4. To lengthen the delay after each pattern is drawn, change this line: 750 FOR J = 1 TO 1500: NEXT J Or, eliminate line 750 to eliminate the delay. Note: These changes add a lot to the appeal of the designs. Experi¬ ment! Each change can be done by itself or in combination with other changes. MAIN ROUTINES 50- 65 100- 130 150- 170 180- 190 200- 700 750 760 800 900- 1010 2000-2010 3000-3020 Subroutines to do color plotting. Housekeeping. Initializes variables. Picks 8 random graphics colors. Reverses direction of display (inward-outward). Displays a full screen of the pattern. Delay loop. Zeroes out R array. Goes back to create next pattern. Plots points on axes of design. Subroutine to plot points between axes. Initialization subroutine. KALEIDO 169 MAIN VARIABLES P Distance from center to edge of design. A, B Pointer to center of design. D Direction in which design is drawn (l=outward, —1 = inward). M Multiplier used to determine the range of random graphics colors. R Array for the random graphics colors. T, J, K, L Subscript variables. X, Y Coordinates of point to be plotted on axes. X2, Y2 Coordinates of point to be plotted between axes. N, W, Work variables. Q,Z, ZA, ZX SPARKLE PURPOSE This graphics display program provides a continuous series of hypnotic patterns, some of which seem to sparkle at you while they are created. T\vo types of patterns are used. The first is a set of concentric diamond shapes in the center of the screen. Although the pattern is regular, the sequence in which it is created is random, which results in the “sparkle” effect. The second type of pattern starts about two seconds after the first has finished. It is a series of “sweeps” across the screen—left to right and top to bottom. Each sweep uses a random graphics color that is spaced equally across the screen. The spacing distance is chosen at random for each sweep. Also, the number of sweeps to be made is chosen at random each time in the range from 10 to 30. After the second type of pattern is complete, the program goes back to the first type, which begins to overlay the center of the second type. HOWTO USE IT Confused by what you just read? Never mind. You have to see it to appreciate it. Just enter the program into your ATARI then sit back and watch the results of your labor. ill ifj 172 Basic Programs for the Atari One of the patterns generated by the SPARKLE program. PROGRAM LISTING 10 REM SPARKLE 13 REM COPYRIGHT 1904 DILITHIUM PRESS 20 GOTO 100 O H=XX#2:Z=YY*4 5 PLOT H, Z:DRAWTO H,Z+3:PL0T H+1,Z+3:DRAWTO H+l , Z 60 RETURN 100 GRAPHICS 10 1 10 S=19 120 DIM A(S),B(S):X=S:Y=S 130 GOSUB 1000 140 T=INT <8#RND <1)+1) 150 FOR J=0 TO S:A(J)=J:B(J)=J:NEXT J 160 FOR J=0 TO S:R=INT( ) +1: W=INT (li*RND (1) ) 430 COLOR W 450 FOR XX=Y-S TO Y+S STEP INT ; CHR* (102);: POS IT I ON AX,J + l:PRINT CHR*(101);CHR*(104);:RETURN 300 POSITION AX,J:PRINT CHR*(109);CHR*<111>;:POS ITION AX,J+l:PRINT CHR*(110);CHR*(112);:RETURN 400 POSITION AX,J:PRINT CHR*(107);CHR*(105);:POS ITI ON AX,J +1:PR INT CHR*(1OB);CHR*(106);:RETURN 500 POSITION AX,J:PRINT CHR*(113);CHR*(115);:POS ITION AX,J+l:PRINT CHR*(114);CHR*(116);:RETURN BOO IF AX=21 THEN 900 BIO AX=21:POSITION 6,1B:PRINT B*;B*;:POSITION 6, 19:PR INT CHR*(100);CHR*(102); B15 POSITION 6,20:PRINT CHR*(101);CHR*(104); B20 POSITION 6,19:PR INT B*;B*;:POSITION 6.20:PR I 182 Basic Programs for the Atari NT CHR* <100);CHR*(102) ; 825 POSITION 21,20:PRINT CHR*(100);CHR*<102);:P0 SITION 6,21 830 PRINT CHR*(101);CHR*(104);B*;: FOR K=1 TO 12: PRINT B*;:NEXT K:PRINT CHR*(101);CHR*<104); 835 POSITION 6,22 840 POR K—1 TO 7:PRINT CHR*(0);:NEXT K:PRINT CHR *<16) ;CHR*(11) ;CHR*(17) ; : FOR K=1 TO 7: PRINT CHR* (O);:NEXT K 850 POSITION 21,19:PRINT CHR*(100);CHR*(102);:PO SITION 6,20 855 PRINT B*;B*;:POSITION 21,20:PRINT CHR*(101); CHR*(104); 860 POSITION 6,21:PRINT CHR*(100);CHR*(102);:FOR K= 1 TO 7-.PRINT B*; : NEXT K: FOR K=7 TO 0 STEP -1 862 PRINT CHR*(K);:NEXT K 865 POSITION 6,22:PRINT CHR*(101);CHR*(104);:FOR l<=6 TO 2 STEP -1:PRINT CHR*;:NEXT KsPRINT CH R*(14);CHR*(11); 870 PRINT CHR*(13);:FOR K=1 TO 7:PRINT B*;:NEXT K 880 POSITION 21,19:PRINT B*;B*;:POSITION 21,20:P RINT B*;B*; 890 RETURN 900 AX=6:POSITION 21,18:PRINT B*;B*;:POSITION 21 ,19:PRINT CHR*(100);CHR*(102);:POSITION 21,20:PR I NT CHR*(101); 905 PRINT CHR*(104); 910 POSITION 21,19:PRINT B*;B*;:POSITION 6,20:PR INT CHR*(100);CHR*(102); 915 POSITION 21,20:PRINT CHR*(100);CHR*(102);:PO SITION 6,21 920 PRINT CHR*(101);CHR*(104);B*;:FOR K=1 TO 12: PRINT B*;:NEXT K:PRINT CHR*(101>;CHR*(104); 925 POSITION 6,22 930 FOR K=1 TO 7:PRINT CHR*(0);:NEXT K:PRINT CHR *(16);CHR*(11);CHR*(17);:FOR K=1 TO 7:PRINT CHR* (0);:NEXT K 940 POSITION 6,19:PRINT CHR*(100);CHR*(102);:POS ITION 21,20:PRINT B*; B*;:POSITION 6,20:PRINT CHR *(101);CHR*(104); 950 POSITION 6,21:FOR K=0 TO 7:PRINT CHR*(K);:NE XT K 955 FOR K=1 TO 8: PRINT B*;-.NEXT K: POSITION 21,22 :PRINT CHR*(101);CHR*(104); 960 POSITION 21,21:PRINT CHR*(100);CHR*(102);:PO SITION 6,22:FOR K=1 TO 7:PRINT B*;:NEXT K 965 PRINT CHR* (12) ; CHR* (11); CHR* (i.5) ; : FOR K=2 TO 6:PRINT CHR*(K);:NEXT K 980 POSITION 6,19: PRINT B*; B*; : POSITION 6,20:F'RI NT B*;B*; 990 RETURN WALLOONS 183 1000 POKE 752,1:PR INT CHR*<125>:FOR 1 = 1 TO 38:P0 SITIQN 1,23:PRINT CHR*(0)::NEXT I 1010 FOR 1=3 TO 22:POSITION 38,I:PRINT CHR*<9>;: NEXT I 1020 FOR 1=7 TO 22:P0SITI0N 33,I:PRINT CHR*< 8 );: NEXT I 1030 FOR 1=7 TO 22:POSITION 34,1:PRINT CHR*(0);C HR*(0);CHR*(0);CHR*(0) 5 :NEXT I 1040 POSITION 33,7:PRINT CHR*(10); 1050 FOR 1=32 TO 26 STEP -1:POSIT ION 1,7:PRINT C HR*(0);:NEXT I:POSITION 25,7:PRINT CHR*(103); 1060 FOR 1=0 TO 7:POSITION 22-1,21:PRINT CHR* (I) ;:NEXT I 1070 FOR 1 = 1 TO 7: F'OSI T ION 14-1.22: PRINT CHR* (I) ;:NEXT I 1080 POSITION 13,22:PRINT CHR*(14);CHR*<11>;CHR* (13) ; 1090 GOSUB 2100 1100 POSITION 24, 4: PRINT CHR* < 100) ; CHR* < 102) 5 : F'O SITION 24,5:PRINT CHR*(101);CHR*(104); 1105 FOR 1=1 TO 100:NEXT I 1110 POSITION 23,3:PR INT CHR*(107);CHR*<105); :PO SITION 23,4:PRINT CHR*(108);CHR*<106);B* 1115 POSITION 24,5:PRINT B*;B*:GOSUB 2900 1120 POSITION 22,3:PRINT CHR*(107):CHR*(105);B*; : POSIT I ON 22,4: PRINT CHR* (1013) ; CHR* (106) ; B* 1125 GOSUB 2900 1130 POSITION 21,4:PRINT CHR*(113) 5 CHR*<115)?B*; :POSITION 21,5:PRINT CHR*(114);CHR*(116);B* 1135 POSITION 22,3:PRINT B*;B*;:GOSUB 2900 1140 AX=21:Z=4:FOR 1=4 TO 15 1150 FOR J = I TO 18: GOSUB 150: Z = Z+1: IF Z=5 THEN Z = 1 1160 IF Z=2 AND 18-J<4 THEN Z=1 1170 ON Z GOSUB 200,300,400,500 1180 GOSUB 2900 1200 NEXT J 1210 GOSUB 800 1220 FOR J=18 TO I STEP -1:GOSUB 160:Z=Z+1:IF Z= 5 THEN Z=1 1230 ON Z GOSUB 200,300,400,500 1240 GOSUB 2900 1250 NEXT J 1255 J=J+1:GOSUB 150 1260 NEXT I 1270 FOR J = 16 TO 21: GOSUB 150:7. = Z + 1:IF Z=5 THEN Z = 1 1280 ON Z GOSUB 200,300,400,500 1290 NEXT J 1300 FOR 1=1 TO 500:NEXT I:GRAPHICS O:END 2000 RAM=PEEK(106):POKE 106,RAM-4:GRAPHICS 0:POK E 752,1:GOSUB 2500:CH=RAM-4 184 Basic Programs for the Atari 2010 AD=CH<256; FOR X=1 TO 500:POKE AD+X.PEEK(573 44+X):NEXT X:GOSUB 2600 2020 FOR X=501 TO 1023:POKE AD+X,PEEK(57344+X):N EXT X 2030 RESTORE 3000:FOR 1=1 TO 46:READ M:XX=AD+M 2040 FOR J=0 TO 7:READ N:POKE XX+J,N:NEXT J-.NEXT I 2050 GRAPHICS 0:F'0KE 756, CH+2: POKE 752,1: GOTO 10 00 2100 XX=20:D=1:FOR 1=1 TO 100:NEXT I 2110 FOR 1 = 1 TO 6 : POSIT ION I-1,22:PRINT B*-,-.POS'l TION 1-1,21: PRINT B*;:POSITION I, 21: PRINT CHR* < 1 8 ) ; 2120 D=-D:XX=XX-D:POSITION I,22:PRINT CHR*;: FOR J=1 TO 50:NEXT J:NEXT I 2130 GOSUB 2900: POSITION 6 ,21-.PRINT CHR*<100);CH Rit (102) ;: POSITION 6 , 22: PRINT CHR* (101) ; CHR* (104) 2140 GOSUB 2700 2150 FOR 1=21 TO 5 STEP -1:GOSUB 2950:POSITION 3 5,1+2:PRINT CHR$(O);CHR*(0) 5 :GOSUB 2900:NEXT I 2160 X X=22:D=1:A=19:FOR 1=35 TO 25 STEP -1:GOSUB 2450:GOSUB 2400:GOSUB 2900:NEXT I 2170 POSITION 25,6:PRINT CHR*<22);:FOR 1=1 TO 17 5:NEXT I 2180 X X=20:D=1:A=18:FOR 1=25 TO 35:GOSUB 2450:GO SUB 2350:GOSUB 2900:NEXT I:FOR 1=1 TO 100:NEXT I 2190 POSITION 35,5:PRINT CHR*<19);:POSITION 35,6 :PRINT CHR*(22);:FOR 1=1 TO 200:NEXT I 2200 XX=22:D=1:A=19:FOR 1=35 TO 25 STEP -1:GOSUB 2450:GOSUB 2400:GOSUB 2900:NEXT I 2210 FOR 1=1 TO 100:NEXT I:POSITION 25,5:PRINT B POSITION 25,6:PRINT CHR*(24);CHR*(25); 222.0 FOR 1 = 1 TO 200: NEXT I-.POSITION 25,5: PRINT C HR* (19):: F'OS ITI ON 25, 6 : F'R I NT CHR* (22 > ; B*; 2230 FOR 1=1 TO 100:NEXT I:POSITION 25,5:PRINT C HR* (18) ; : F'OS ITI ON 25,6: PRINT CHR* (20) ; 2240 XX=20:D=1:A=18:FOR 1=25 TO 35:GOSUB 2450:GO SUB 2350: GOSUB 2900: NEXT I-.FOR 1 = 1 TO 100: NEXT I 2250 POSITION 35,5:PRINT CHR*<19);:POSITION 35,6 :PRINT CHR*(22) 5 :FOR 1=1 TO 200:NEXT I 2260 XX=22:D=1:A=19:FOR 1=35 TO 25 STEP -1:GOSUB 2450:GOSUB 2400:GOSUB 2900:NEXT I 2270 FOR 1=1 TO 3:POSITION 25,4:PRINT CHR*<19);: POSITION 25,5:PRINT CHR*(22);:POSITION 25,6:PRIN T B*; 2280 FOR K=1 TO 1GO:NEXT K:POSITION 25,4:PRINT B *;:POSITION 25,5:PRINT CHR*<19);:POSITION 25,6 2290 FRINT CHR*<22>;:FOR K=1 TO 100:NEXT K:NEXT I:POSITION 25,4:PRINT CHR*(19);:POSITION 25,5:PR INT CHR*(22); WALLOONS 185 2300 POSITION 25,6s PRINT B*; :RETURN 2350 POSITION I-1,5:PRINT B*;:POSITION I-1,6:PRI NT B*; :RETURN 2400 POSITION 1+1,5:PRINT B*; :POSITION I+1,6:PRI NT B*;:RETURN 2450 D=-D: XX = XX-D:POSIT I ON I,5:F'RINT CHR*(A);:F'0 SIT ION 1,6:PRINT CHR* 2. The title placard is currently bordered by asterisks. To get another character, change the asterisk character strings in lines 2530, 2540, and 2550 to another character. 3. You might want to personalize the title placard and make yourself the presenter of the Walloons. This can be done by altering the string literal, “ATARI ARENA”, in line 2500 to something else. However, you cannot use a string with a length of more than 22 characters or it will be clipped by the end of the placard. To say, for example, that Mr. Simon Q. Fenster presents the Walloons, change line 2500 to read: 2500 PRINT CHR$( 125): POSITION 11,9: PRINT“MR. SIMON Q. FENSTER”; MAIN ROUTINES 150-1290 1300 2000-2050 2100-2450 2500-2560 2600-2630 2700-2950 3000-3180 Subroutines to draw (and erase) Walloons and their performing apparatus. Time-delay subroutine. Initializes memory locations. Drives text and graphics displays. Subroutine to display placard. The performers are announced. The Walloons make their entrance and perform. Graphics data. MAIN VARIABLES X, Y Current X,Y location of Walloon; also “bit” table data values and locations. XX, AX Reference X, Y locations for Walloons. I, J,K Loop indices. 188 Basic Programs for the Atari Z Work variable. RAM Highest available memory page. CH, AD Reference memory pages and locations. M, N, D Work variables used in reading data. SUGGESTED PROJECTS 1. Add some alternate tricks or endings to the act; try randomizing if and when they will be done. Thus, the Walloons’ performance will be different each time the program is run. At least their ending may be variable. 2. Scour the world yourself for other acts to include in the ATARI Arena. Maybe someday we will have a complete software library of performing artists. Section 5 Mathematics Programs INTRODUCTION TO MATHEMATICS PROGRAMS Since their invention, computers have been used to solve mathe¬ matical problems. Their great speed and reliability render solvable many otherwise difficult (or impossible) calculations. Several differ¬ ent numerical techniques lend themselves naturally to computer solu¬ tion. The following programs explore some of them. They will be of interest mainly to engineers, students, mathematicians, statisticians, and others who encounter such problems in their work. GRAPH takes advantage of the ATARI’S graphic powers to draw the graph of a function Y = f(X). The function is supplied by you. INTEGRAL calculates the integral, or “area under the curve,” for any such function. Experimental scientific work frequently results in data at discrete values of X and Y. CURVE finds a polynomial algebraic expression to express this data with a formula. Theoretical scientists (and algebra students) often must find the solution to a set of simultaneous linear algebraic equations. SIMEQN does the trick. Much modem engineering work requires the solution of differen¬ tial equations. DIFFEQN will solve any first-order ordinary differen¬ tial equation that you provide. STATS will take a list of data and derive standard statistical infor¬ mation describing it. In addition, it will sort the data list into ranking numerical order. CURVE PURPOSE AND DISCUSSION CURVE fits a polynomial function to a set of data. The data must be in the form of pairs of X-Y points. This type of data occurs frequently as the result of some experiment, or perhaps from sam¬ pling tabular data in a reference book. There are many reasons why you might want an analytic formula to express the functional relationship inherent in the data. Often you will have experimental errors in the Y values. A good formula expression tends to smooth out these fluctuations. Perhaps you want to know the value of Y at some X not obtained exactly in the experi¬ ment. This may be a point between known X values (interpolation) or one outside the experimental range (extrapolation). If you wish to use the data in a computer program, a good formula is a convenient and efficient way to do it. This program fits a curve of the form Y=C 0 +C,X 1 +C 2 X 2 + ... +CdX d to your data. You may select D, the degree (or power) of the highest term, to be as large as 7. The constant coefficients, C 0 — Cd, are the main output of the program. Also calculated is the goodness of fit, a guide to the accuracy of the fit. You may fit different degree poly¬ nomials to the same data and also ask to have Y calculated for specific values of X. The numerical technique involved in the computation is known as least squares curve fitting. It minimizes the sum of the squares of the errors. The least squares method reduces the problem to a set of simultaneous algebraic equations. Thus these equations could be 192 Basic Programs for the Atari solved by the algorithm used in SIMEQN. In fact, once the proper equations are set up, CURVE uses the identical subroutine found in SIMEQN to solve the equations. For more information, the bibli¬ ography contains references to descriptions of the numerical technique. HOW TO USE IT The first thing you must do, of course, is enter the data into the program. This consists of typing in pairs of numbers. Each pair represents an X value and its corresponding Y value. The two num¬ bers (of each pair) are separated by a comma. A question mark will prompt you for each data pair. After you have entered them all, type 999,999 to signal the end of the data. When you do this, the program will respond by indicating how many data pairs have been entered. A maximum of 75 data pairs is allowed. Next, you must input the degree of the polynomial to be fitted. This can be any non-negative integer subject to certain constraints. The maximum allowed is 7. Also, D must be less than the number of data pairs. A few notes regarding the selection of D may be of interest. If D=0, the program will output the mean value of Y as the coefficient C„- If D=l, the program will be calculating the best straight line through the data. This special case is known as “linear regression.” If D is one less than the number of data pairs, the program will find an exact fit to the data (barring round-off and other numerical errors). This is a solution which passes exactly through each data point. Once you have entered the desired degree, the program will begin calculating the results. There will be a pause while this calculation is performed. The time involved depends on the number of data pairs and the degree selected. For 25 data pairs and a third degree fit, the pause will be about a minute. Fifty data pairs and a fifth degree fit will take over three minutes. The results are displayed in a table. It gives the values of the coefficients for each power of X from 0 to D. That is, the values of C 0 ~Cd are output. Also shown is the percent goodness of fit. This is a measure of how accurately the program was able to fit the given case. A value of 100 percent means perfect fit, lesser values indicate correspondingly poorer fits. It is hard to say what value denotes CURVE 193 satisfactory fit since much depends on the accuracy of data and the purpose at hahd. But as a rule of thumb, anything in the high nineties is quite good. For those interested, the formula to calculate the percent goodness of fit is P.G.F. = 100* E(Yi-Yj) 2 E(Yi-Y) 2 where Yi are the actual Y data values, Y, are the calculated Y values (through the polynomial expression), and Y is the mean value of Y. Next, you are presented with three options for continuing the run. These are 1) determining specific points, 2) fitting another degree, 3) ending the program. Simply type 1 , 2 , or 3 to make your selec¬ tion. A description of each choice now follows. Option 1 allows you to see the value of Y that the current fit will produce for a given value of X. In this mode you are continually prompted to supply any value of X. The program then shows what the polynomial expression produces as the value for Y. Input 999 for an X value to leave this mode. Option 2 allows you to fit another degree polynomial to the same data. Frequently, you will want to try successively higher values of D to improve the goodness of fit. Unless round-off errors occur, this will cause the percent of goodness of fit to increase. Option 3 simply terminates the program and with that we will terminate this explanation of how to use CURVE. SAMPLE PROBLEM AND RUN Problem: An art investor is considering the purchase of Primo’s masterpiece, “Frosted Fantasy.” Since 1940, the painting has been for sale at auction seven times. Here is the painting’s sales record from these auctions. Year Price 1940 $ 8000. 1948 $13000. 1951 $16000. 1956 $20000. 1962 $28000. 1968 $39000. 1975 $53000. 194 Basic Programs for the Atari The painting is going to be sold at auction in 1984. What price should the investor expect to have to pay to purchase the painting? If he resold it in 1988, how much profit should he expect to make? Solution: The investor will try to get a polynomial function that expresses the value of the painting as a function of the year. This is suitable for CURVE. The year will be represented by the variable X, and the price is shown by the variable Y. To keep the magnitude of the numbers small, the years will be expressed as elapsed years since 1900, and the price will be in units of $1000. (Thus a year of 40 represents 1940, a price of 8 represents $8000.) Initially, a first degree fit was tried and a goodness of fit of about 97.5% was obtained. The investor wanted to do better, so he tried a second degree fit next. This had a very high goodness of fit. He then asked for the extrapolation of his data to the years 1984 and 1988. He found that he should expect to pay about $75000 to buy the painting in 1984. Around a $11000 profit could be expected upon resale in 1988. Of course, the investor did not make his decision solely on the basis of this program. He used it only as one guide to his decision. There is never any guarantee that financial data will perform in the future as it has done in the past. Though CURVE is probably as good a way as any, extrapolation of data can never be a totally reliable process. CURVE The operator enters the painting’s previous auction sales record. The operator selects a first degree fit to the data. -HR- NE«T '2 : OF PQLfNOhXilL r'O BE FITTED '2 ■ -o«ER :oefficient 15 5763735 - i . 3 I 7 11 3 0 I 8 8 2 7 0 5 3 3 3 0 7 j - : e n t :oo d mE 55 of fit- 33.3435742 - Z 3 N T I X Li P 7 IQ N Q PI 10*5 1 - DETERMINE 5 PE 0 IFIC POINT 5 2 - - I" ANOTHER DEGREE TO - A«E Di T i 7 - END PP0G H A M A second degree fit is attempted for the same data. The operator asks to see the predicted sale prices for 1984 and 1988. and then exits from the program. CURVE 197 PROGRAM LISTING 1C REM CURVE 15 REM COPYRIGHT 1984 DILITHIUM PRESS 130 GRAPHICS O:PRINT CHR*<125) 150 MX=75 160 EF=999 170 MD=7 200 DIM X(MX),Y(MX) 210 Q=MD+1:DIM A(Q,Q),R(Q>,V(Q> 220 Q=MD#2:DIM P(Q) 300 PRINT " - LEAST SQUARES CURVE FITTING -":PR I NT 310 PRINT "ENTER A DATA PAIR IN RESPONSE TO" 320 PRINT “EACH QUESTION MARK. EACH PAIR IS" 330 PRINT "AN X VALUE AND A Y VALUE SEPARATED":P RINT "BY A COMMA." 340 PRINT : PRINT "AFTER ALL DATA IS ENTERED, TYF' E" 350 PRINT CHR*<32>;EF;”,";EF 360 PRINT "IN RESPONSE TO THE LAST QUESTION":PRI NT "MARK." 370 PRINT :PRINT “THE PROGRAM IS CURRENTLY SET T 0 " 300 PRINT "ACCEPT A MAXIMUM OF ";MX;" DATA PAIRS II 400 PRINT : J=0 405 TRAF 3000 41.0 XZ = 1:J=J+1: PRINT " X , Y= INPUT X , Y: X (J ) =X s Y < J) =Y 420 IF X<3)=EF AND Y(3)=EF THEN 3 = 3--l:G0TQ 450 430 IF 3=MX THEN PRINT :PRINT "NO MORE DATA ALLO WED":GOTO 450 440 GOTO 410 450 NP=3:PRINT 460 IF NF-0 THEN GOSUB 1600:PRINT "NO DATA ENTER ED":END 470 PRINT NP;" DATA PAIRS ENTERED":PRINT 500 XZ=2:PRINT :PRINT "DEGREE OF POLYNOMIAL TO B E FITTED";:INPUT D:PRINT 510 IF D<0 THEN GOSUB 1500:PRINT "DEGREE MUST BE >= 0":GOTO 500 520 D=INT(D):IF DMD THEN GOSUB 1500:PRINT "DEGREE TDD HIGH":GOTO 500 550 N=D+1 600 FOR 3=1 TO D2:P : NEXT K: NEXT J 660 FOR J=1 TO N:FOR K=1 TO N:A(J,K)=P ;CHR*(32) ; CHR* (32) ; 720 FOR J = 1 TO 11: PRINT CHR* <18);: NEXT J-.PRINT 730 FOR J=1 TO N:PRINT J-1,V(J):NEXT JsPRINT :PR INT 740 0=0:FOR J=1 TO NP:Q=Q+Y1 THEN SCO 900 PRINT :PRINT :PRINT "ENTER ";EF;" TO LEAVE T HIS MODE" 910 XZ=4:PRINT :PRINT "X=";:INPUT XV:IF XV=EF TH EN 800 920 YV=0:FOR K=1 TO N 930 YV=YV+V (K) *XV'~ (K-l): NEXT K: PRINT "Y= " ; YV 940 GOTO 910 1500 PRINT "** ERROR! ** — RETURN 1600 PRINT "** FATAL ERROR! ** — RETURN 2000 IF N=1 THEN V <1)=R <1)/A(1,1):RETURN 2010 FOR K=1 TO N-l 2020 I=K+1 2030 L=K 2040 IF ABS(A ABS < A < L,K)) THEN L=I 2050 IF I =R(N> /A : FOR I=N-1 TO 1 STEP -1 2160 Q=0: FOR ,1=1 + 1 TO N:Q=Q+A (I, J) *V (J) 2170 V (I) = (R (I) -Q) /A < I, I > : NEXT v'UNEXT I 2ISO RETURN 3000 IF PEEK (195) 08 THEN 3050 3005 PRINT "ILLEGAL ENTRY" 3010 TRAP 3000:IF XZ=1 THEN J=J-1:G0T0 410 3020 IF XZ=2 THEN 500 3030 IF X7=3 THEN 840 3040 IF X2=4 THEN 910 3050 END EASY CHANGES 1. The program uses 999 as the flag number to terminate various input modes. This may cause a problem if your data include 999. You can easily change the flag number by modifying the value of EF in line 160 to any value not needed in your data. To use 10101, for example, make this change: 160 EF= 10101 2. Currently a maximum value of 75 data pairs is allowed. If you need more, change the value of MX in line 150 to the number required. For example, to allow up to 200 data pairs, use 150 MX=200 3. To allow fits of higher degrees than seven, set MD in line 170 to the maximum degree desired. To achieve up to tenth degree fits, set the value of MD appropriately. 170 MD=10 However, it must be stressed that it can be unreliable to attempt high degree fits. Unless your data is well behaved (X and Y values close to 1), the program will often not produce accurate results if D is greater than five or so. This is because sums of powers of X and Y are calculated up to powers of 2*D. These various sums are several orders of magnitude different from each other. Errors re¬ sult because of the numerous truncation and round-off operations involved in doing arithmetic with them. A practical limit for MD is seven. 200 Basic Programs for the Atari 4. The demand on available RAM memory is increased if you raise the values of MX or MD as described in the above two Easy Changes. Should MX or MD be set too large for your available memory, an out-of-memory or illegal-quantity error will result after execution begins. If this occurs, decrease MX and/or MD to enable the program to run. MAIN ROUTINES 130- 170 200 - 220 300- 380 400- 470 500- 550 600- 670 700- 780 800- 860 900- 940 1500-1600 2000-2180 3000-3050 Initializes constants. Dimensions arrays. Displays introductory messages. Gets X-Y input data from the user. Gets degree of polynomial from the user, determines if it is acceptable. Sets up equations for the simultaneous equation solver and calls it. Calculates percent goodness of fit, displays all results. Gets user’s continuation option and branches to it. Determines Y value corresponding to any X value. Subroutines to print error messages. Subroutine to solve simultaneous linear algebraic equations. Trap to catch illegal entries. MAIN VARIABLES MX MD EF X, Y NP D D2 N A, R, V P I, J, K, L Q, G, XZ M T XV YV Maximum number of data pairs allowed. Maximum degree allowed to fit. Ending flag value for data input and X point mode. Arrays of X and Y data points. Number of data pairs entered. Degree of polynomial to fit. 2*D, the maximum power sum to compute. D+1, number of simultaneous equations to solve. Arrays for simultaneous linear equation solver. Array for holding sums of various powers of X. Loop indices. Work variables. Mean value of Y Percent goodness of fit. Specific X point for which to calculate Y. Y value corresponding to XV. CURVE 201 SUGGESTED PROJECTS 1. No provision for modifying the data is incorporated into the pro¬ gram. Often it would be nice to add, subtract, or modify parts of the data after some results are seen. Build in a capability to do this. 2. You may desire other forms of output. A useful table for many applications might include the actual X values, calculated Y val¬ ues, and/or percentage errors in Y. 3. Sometimes certain points (or certain regions of points) are known to be more accurate than others. Then you would like to weight these points as being more important than others to be fit correctly. The least squares method can be modified to include such a weight¬ ing parameter with each data pair. Research this technique and incorporate it into the program. (Note: you can achieve some weighting with the current program by entering important points two or more times. There is a certain danger to this, however. You must only ask for a solution with D less than the number of unique data points. A division by zero error may result otherwise.) 4. Often you wish to try successively higher degree polynomials until a certain minimum percent goodness of fit is obtained. Modify the program to accept a minimally satisfactory percent goodness of fit from the user. Then have the program automatically try various polynomial fits until it finds the lowest degree fit, if any, with a satisfactory goodness of fit. DIFFEQN PURPOSE Differential equations express functions by giving the rate of change of one variable with respect to another. This type of relation occurs regularly in almost all the physical sciences. The solution of these equations is necessary in many practical engineering problems. For many such equations, a closed form (or exact analytical ex¬ pression) solution can be obtained. However, for just as many, no such “simple” solution exists. The equation must then be solved numerically, usually by a computer program such as this. There are many types and classes of differential equations. This program solves those of a simple type; namely, first order, ordinary differential equations. This means that the equation to be solved can be written in the form ^=(any function of X, Y) dX Here, X is the independent variable and Y is the dependent variable. The equation expresses the derivative (or rate of change) of Y with respect to X. The right-hand side is an expression which may involve X and/or Y. To use the program, you must supply it with the differential equa¬ tion to be solved. The procedure used to do this is explained in the “How To Use It” section. A technique known as the “fourth-order, Runge-Kutta” method is used to solve the equation. Space limitations prevent any detailed explanation of it here. However, it is discussed well in the numerical analysis books referenced in the bibliography. 204 Basic Programs for the Atari The program allows two forms of output. You can have the an¬ swers tabulated in columns or plotted graphically. HOW TO USE IT The first thing you must do is enter the differential equation into the program. This must be done at line 200. Currently this line contains a GOTO statement. This GOTO will cause an error message to be displayed if the program is run before you have changed line 200. The form of line 200 should be: 200 D = (your function of X, Y) D represents dY/dX. GOSUBs are made to line 200 with X and Y set to their current values. Thus, when each RETURN is made, D will be set to the appropriate value of dY/dX for that given X and Y. If necessary, you may use the lines between 200 and 899 to complete the definition of D. Line 899 already contains a RETURN statement so you do not need to add another one. The program begins by warning you that you should have already entered the equation at line 200. You acknowledge that this has been done by hitting the C key to continue. Now the various initial conditions are input. You are prompted for them one at a time. They consist of: the initial values of X and Y, the stepsize interval in X at which to display the output, and the final value of X. You now have a choice between two types of output. Enter a T for tabular output or a G for graphical output. The tabular form is simply a two-column display of the corresponding values of X and Y. The graphical output plots the values of Y along a horizontal axis as each corresponding X value is displayed on successive lines of the screen. This graphical display requires you to input the minimum and maximum values of Y that will be used on the Y axis. You will be prompted for them if this output form is chosen. An asterisk (*) is used to plot the value of Y. If, however, the value of Y is “off-scale,” an inverse asterisk is plotted at the appropriate edge of the graph. With the input phase completed, the program initializes things to begin the output. A question mark will be displayed in the lower left of the screen, telling you the program is waiting for you to hit any key to begin the output. The output is displayed at each interval of the stepsize until the final value of X is reached. Output may temporarily be halted at any time by simply hitting any key. This will stop the display until you hit DIFFEQN 205 any key to resume the output. The output may be started and stopped as often as desired, thus enabling you to leisurely view intermediate results before they scroll off the screen. It is applicable to both the tabular and graphical forms of output. SAMPLE PROBLEM AND RUN Problem: A body, originally at rest, is subjected to a force of 2000 dynes. Its initial mass is 200 grams. However, while it moves, it loses mass at the rate of 1 gram/sec. There is also an air resistance equal to twice its velocity retarding its movement. The differential equation expressing this motion is: dY _ (2000—2Y) where Y=velocity (cm. /sec.) dX (200—X) X=time (sec.) Find the velocity of the body every ten seconds up through two and one-half minutes. Also, plot this velocity as a function of time. Solution and Sample Run: The solution and sample run are illus¬ trated in the accompanying photographs. The operator hits a key to exit from the program. Then he enters the differential equation into line 200. He types RUN to restart the program. ♦ -• ♦ ..AO IN ~ 150 GOTO 1200 200 GOTO 3000s REM REDEFINE THIS LINE TO BE D=(YO UR FUNCTION OF X,Y> B99 RETURN 900 REM ft*************************** 910 REM DEFINE THE DIFFERENTIAL 920 REM EQUATION BETWEEN LINES 930 REM 200 AND S99 940 REM 950 REM LINE 200 MUST BE 960 REM OVERWRITTEN, MAKING IT 970 REM THE FIRST LINE OF THE 980 REM EQUATION 990 REM **************************** 1000 IF F$(1,1)="T" THEN PRINT XX,YY:GOSUB 1160s RETURN 1010 F= 1020 C=42:IF YYYH THEN V=37:C=170 1040 PRINT XX;s POSITION 16„23sPRINT CHR$(124);:P OSITION V,23 1050 PRINT CHR*(C);:POSITION 38,23:PRINT CHR*<12 4) 1060 GOSUB 1160:RETURN 1100 PRINT CHR$(32);:FOR Q=1 TO 3B:PRINT "*"::NE XT QSPRINT :RETURN 1110 PRINT CHR*(32);sPOSITION 38,PEEK(84):PR I NT RETURN 1120 POSITION 38,PEEK<84>:PRINT RETURN 1160 Q=PEEK(764):IF Q=255 THEN RETURN 1170 POKE 764,255 1180 Q=PEEK (764) : IF Q==255 THEN 1180 1190 POKE 764,255:RETURN 1200 POKE 82,0:PRINT CHR*<125) 1210 PRINT "FIRST ORDER DIFFERENTIAL EQUATION SO LVER" 1220 GOSUB HOO: GOSUB 1110 1230 PRINT " * THE DIFFERENTIAL EQUATION MUST BE GOSUB 1120 1240 PRINT " * DEFINED AT LINE 200. THE FORM IS GOSUB 1120 1250 GOSUB 1110:PRINT " * 200 D = "T“ AND F* (1, 1 > < >"G" THEN 1450 1470 IF F$(1,1)="T" THEN 1600 1480 XZ=5:PRINT :PRINT " MINIMUM Y FOR THE GRAPH AXIS" 5 :INPUT YL 1490 XZ= 6 :PRINT :PRINT " MAXIMUM Y FOR THE GRAPH AXIS";:INPUT YH 1500 IF YH>YL THEN 1600 1510 PRINT 1520 PRINT " ** ERROR — MAX Y MUST BE > MIN Y" 1530 PRINT CHR$(253):GOTO 1480 1600 PRINT :GOSUB 1100:PRINT 1610 PRINT " THE FOLLOWING OUTPUT CAN BE HALTED" 1620 PRINT " BY HITTING ANY KEY. IT CAN THEN BE" 1630 PRINT " RESUMED BY HITTING ANY KEY. THIS MA Y" 1640 PRINT " BE DONE AS OFTEN AS DESIRED.":PRINT 1650 PRINT " WHEN THE QUESTION MARK (?) APPEARS" 1660 PRINT " HIT ANY KEY TO BEGIN THE OUTPUT." 1670 PRINT :GOSUB 1100:PRINT :POKE 764,255 1700 IF F*(1,1)="T" THEN PRINT "X","Y":GOTO 1800 1710 POSITION 17,23:PRINT "YMIN = ";YL 1720 POSITION 17,23:PRINT "YMAX = ";YH 1730 PRINT :PRINT " X";:POSITION 17,23:PRINT "YM IN";:POSITION 34,23:PRINT "YMAX" 1740 POSITION 16,23 1750 PRINT CHR$(124>;"+-+-+-+-+";CHR $(124); 210 Basic Programs for the Atari 1800 PRINT :PRINT "?": 1810 IP PEEK(764)=255 THEN 1810 1815 POKE 764,255 1820 PRINT CHR*(30>;CHR*<32);CHR$(30>5 1830 GOSUB 1000 1900 Q=XX+DX:IF QXF—l.OE—05 THEN 1910 MAIN ROUTINES 120- 150 200- 899 1000-1060 1100-1120 1160-1190 1200-1330 1400-1530 Initializes, begins execution. User-supplied subroutine to define D. Displays output. Subroutines to format messages. Subroutine to stop and start output. Displays initial messages. Gets user’s inputs. DIFFEQN 211 1600-1670 Displays additional messages. 1700-1750 Initializes output display. 1800-1830 Waits for user to hit a key to start the output. 1900-1960 Computes each step. 3000-3020 Error message. 4000-4100 Error trap for illegal conditions. MAIN VARIABLES D Value of dY/dX. X, Y Values of X, Y on current step. XX, YY Values of X, Y on last step. DX StepsizeinX. XF Final value of X. F$ Output flag string (T=table, G=graph). YL, YH Minimum, maximum values of Y plot axis. F Fractional distance of graphical point along Y axis. V Tab position for graphical output. C CHR$ argument for graphical output. K0, K1, Runge-Kutta coefficients. K2,K3 Q, XZ, R Work variables. J Loop index. SUGGESTED PROJECTS 1. Modify the program to display the tabular output followed by the graphical output. During the tabular phase, the minimum and maximum values of Y can be saved and automatically used as the plot limits for the graphical output. 2. The value of dY/dX as a function of X is often a useful quantity to know. Modify the program to add it to the columnar display and/or the graphical display. 3. The inherent error in the calculation depends on the stepsize chosen. Most cases should be run with different stepsizes to insure that the errors are not large. If the answers do not change much, you can be reasonably certain that your solutions are accurate. Better yet, techniques exist to vary the stepsize during the calcula¬ tion to insure that the error is sufficiently small during each step. Research these methods and incorporate them into the program. 4. The program can be easily broadened to solve a set of coupled, first order, differential equations simultaneously. This would 212 Basic Programs for the Atari greatly increase the types of problems that could be solved. Re¬ search this procedure and expand the program to handle it. GRAPH PURPOSE Is a picture worth a thousand words? In the case of mathematical functions, the answer is often “yes.” A picture, i.e., a graph, enables you to see the important behavior of a function quickly and accu¬ rately. Trends, minima, maxima, etc., become easy and convenient to determine. GRAPH produces a two-dimensional plot of a function that you supply. The function must be in the form Y=(any function of X). The independent variable X will be plotted along the abscissa (horizontal axis). The dependent variable Y will be plotted along the ordinate (vertical axis). You have complete control over the scaling that is used on the X and Y axes. HOWTO USE IT Before running the program, you must enter into it the function to be plotted. This is done as a subroutine beginning at line 200. It must define Y as a function of X. The subroutine will be called with X set to various values. It must then set the variable Y to the correct corresponding value. The subroutine may be as simple or complex as necessary to define the function. It can take one line or several hundred lines. Line 999 is already set as a RETURN statement, so you need not add another one. Having entered this subroutine, you are ready to run the program. The program begins by warning you that it assumes the function has already been entered at line 200. It will then ask you for the domain of X, i.e., the lowest and highest values of X that you wish to have 214 Basic Programs for the Atari plotted. Values can be positive or negative as long as two conditions are met: the highest value must be algebraically larger than the lowest value and the number of characters the ATARI uses to express each value must be no more than eight. Now you must choose the scale for Y. To do this intelligently, you probably need to know the minimum and maximum values of Y over the domain of X selected. The program finds these values and dis¬ plays them for you. You must then choose the minimum and max¬ imum values you wish to have on the Y scale. Again, any two values are acceptable as long as they satisfy the two conditions given above. The program will now request that you hit any key to display the plot of your function. Each axis is 20 tick-marks long, with the origin defined as the minimum scale values of both X and Y. The minimum, middle, and maximum values on each scale are displayed appropri¬ ately. (Note: in certain cases, the program will not display the middle scaling value for one or both axes. However, the highest and lowest scaling values for each axis will always be present.) The actual plot is drawn with eight times the resolution shown by the axes’ tick-marks. That is, 160 values of X and Y are plotted. This is accomplished by taking advantage of the ATARI’S high-resolution graphics capabilities. If a value for Y should be off-scale, a special “enlarged line” will be displayed at the appropriate value of X. If the actual value of Y is too large, it will be plotted at the maximum Y value. Similarly, it will be drawn at the minimum Y value if it is too low or if its value is exactly the minimum Y value. After the plot is drawn, you can exit the program and return to text mode by hitting any key. GRAPH 215 SAMPLE RUN After loading the program, the operator enters line 200 to request the graph Y = SIN(X). RUN is typed to begin the program. The program initiates the input dialog. ««»*«** 216 Basic Programs for the Atari ' i: S FUNCTION OF * ■* :*E3 T viLUE OF x' 0 I j h E : T 0 w L U E OF y ’6,23 - £Q THIS RANGE OF M : V - 0,333333673 “7 NXHyH v = -0,3 33337147 I« 3 H U 0 5 t THE 3 CiL E FOP ¥ XnIhuh y 5 GALE MOLUE’-i y 5 GALE UALUE’l -IT an 1 ' KEY T0 5 E E THE GRAPH , THEN I" AMY KEY TO RETURN TO TEXT MODE The input dialog transpires. The operator asks that the domain of X be 0-6.28. The program responds by showing the maximum and minimum values of Y over this domain. The operator chooses an appropriate scale for the Y axis. The graph is displayed as requested. The program waits for the operator to hit any key to return to text mode. GRAPH 217 PROGRAM LISTING lO REM GRAPH 15 REM COPYRIGHT 1984 DILITHIUM PRESS 110 POKE 82,0 120 DIM XL*(10),XU*U0>,XM*(10),YL*<10),YU*<10), YM*(10),D*(10),A* (10) 150 GOTO 1200 170 REM 180 REM ** ENTER SUBROUTINE AT 200 ** 190 REM 200 REM *** Y=F < X > GOES HERE *** 999 RETURN 1200 GRAPH ICS 0:PR INT CHR*(125):TRAP 6000 1210 POSITION 13,1:PRINT "E R A P H":PRINT 1220 PRINT " ************ WARNING! He********##*" 1230 SOSUB 1290 1240 PRINT " * THE SUBROUTINE AT LINES";:60S UB 1295:SOSUB 1290 1250 PRINT » * 200-999 IS ASSUMED TO DEFINE";:G OBUB 1295:SOSUB 1290 1260 PRINT " * Y AS A FUNCTION OF X";sGOSUB 1295:SOSUB 1290 1270 PRINT " **********************************" 1280 PRINT :SOTO 1300 1290 PRINT CHR*(32);"*";:PDSITION 34,PEEK(84):PR INT RETURN 1295 POSITION 34,PEEK(84):PR INT ”*":RETURN 1300 XZ=1:PRINT :PRINT " LOWEST VALUE OF X";:INP UT XL 1310 XL*=STR*(XL):L=LEN(XL*> 1320 IF L>8 THEN PRINT :PRINT " *** TOO MANY DIG ITS - PLEASE REDUCE":GOTO 1300 1330 XZ=2:PRINT :PRINT " HIGHEST VALUE OF X";:IN PUT XU 1340 IF XU<=XL THEN PRINT :PRINT " *** BAD X RAN SE ***":SOTO 1300 1350 XU*=STR*(XU>:L=LEN(XU*> 1360 IF L >8 THEN PRINT :PR I NT " *** TOO MANY DIG ITS - PLEASE REDUCE:GOTO 1330" 1370 XM=(XL+XU)/2:XM*~STR*(XM) 1380 IF LEN(XM*)>9 THEN XM*=CHR*<32> 1400 DX=(XU—XL)/160:X=XL:SOSUB 200:MN=Y:MX-Y 1410 FOR J=1 TO 160:X=XL+J*DX:GOSUB 200 1420 IF Y>MX THEN MX=Y 1430 IF Y8 THEN PRINT ” *** TOO MANY DIGITS - P LEASE REDUCE":PR INT :GOTO 1490 1520 XZ=4:PRINT " MAXIMUM Y SCALE VALUE";:INPUT YU:PRINT 1530 IF YU<=YL THEN PRINT " *** BAD Y RANGE #**" 1 GOTO 1450 1540 YU*=STR*8 THEN PRINT " *** TOO MANY DIGITS - P LEASE REDUCE": PR I NT -.GOTO 1520 1560 YM=(YU+YL)/2:YM*=STR*(YM> 1570 IF LEN(YM*>>9 THEN YM*=CHR*(32> 1600 PRINT " HIT ANY KEY TO SEE THE GRAPH, TH EN" 1610 PRINT " HIT ANY KEY TO RETURN TO TEXT MODE. II 1620 POKE 764,255:PRINT 1630 IF PEEK(764)=255 THEN 1630 1640 POKE 764,255 2000 GRAPHICS 8+16: SETCOLOR 2,0,0: SETCDLOR 1,15, 14:COLOR 1 2100 HP= 107: VF'= 172 2110 PLOT HP,VP-160:DRAWTO HP,VP:DRAWTO HP+160,V P 23.20 FOR J-VP-160 TO VP STEP 8 2130 PLOT HP-4,J:DRAWTO HP,J:NEXT J 2140 FOR J-VP-160 TO VP STEP 80 2150 PLOT HP-8,J:DRAWTO HP,J:NEXT J 2 3.60 FOR J—HP TO HP+160 STEP 8 2170 PLOT J,VP:DRAWTO J,VP+4:NEXT J 21.80 FOR J-HP TO HP+160 STEP 80 2190 PLOT J,VP:DRAWTO J,VP+8:NEXT J 2200 PLOT HP+164, VP-4: DRAWTO HP+172,VF'+4 2210 PLOT HP+164,VP+4:DRAWTO HP+172,VP-4 2220 PLOT HP-4, VP-172:DRAWTO HP,VP-168 2230 DRAWTO HP+4,VP~172 2240 PLOT HP, VP-168: DRAWTO HP, VP-1.64 2300 H-HP-20:V-VP-163:A*=YU*:GQSUB 5000 2310 H-HP-20:V-VP-83:A*=YM*:GOSUB 5000 2320 H-HP-20: V-VP-3: A*=YL*: GOStJB 5000 2340 H=HP:V-VP+12:A*=XL*:GOSUB 5000 2350 H-HP+BO:V-VP+12:A*=XM*:GOSUB 5000 2360 H-HP+160:V-VP+12:A*=XU*:GOSUB 5000 2400 DX-(XU-XL)/160:DY=(YU-YL)/160 2410 AF=0:X=XL:GOSUB 200 2420 IF Y>YU THEN Y=YU:AF=1 2430 IF YYU THEN Y=YU:AF=1 2520 IF Y<=YL THEN Y=YL:AF=1 2530 YY= (Y-YL > /DY: H=HP+J : V=VP~YY 2540 DRAWTO H,V 2550 IF AF= 1 THEN GOSUB 3500 2560 NEXT J 2565 IF PEEK<764)=255 THEN 2565 2570 GRAPHICS OsPOKE 764,255:P0KE 82,2:END 3000 PLOT H,0:DRAWTO H4,V:DRAWTO H4,V6 3010 DRAWTO H,V6:DRAWTO H,V 3020 RETURN 3100 PLOT H4,V:DRAWTO H,V:DRAWTO H,V6 3110 DRAWTO H4,V6:PLOT H,03:DRAWTO H4.V3 3120 RETURN 3200 PLOT H,V3:DRAWTO H4,03:PLOT H+2,V+l:DRAWTO H+2,V+5 3210 RETURN 3300 PLOT H,V3:DRAWTO H4,V3:RETURN 3400 PLOT H+1,0+4:DRAWTO H+3.0+4 3410 DRAWTO H+3,V6:DRAWTO H+1,V6 3420 DRAWTO H+l,V+4:RETURN 3500 DRAWTO H-l,V-l:DRAWTO H-1,V+1 3510 DRAWTO H+l,0+1:DRAWTO H+l,V-l 3520 DRAWTO H-l,V-l:DRAWTO H,V 3530 AF=0:RETURN 4100 PLOT H+2,V:DRAWTO H+2,V6 4110 RETURN 4200 PLOT H,V:DRAWTO H4,V 4210 DRAWTO H4,V3:DRAWTO H,V3 4220 DRAWTO H,V6:DRAWTO H4,V6:RETURN 4300 PLOT H,V:DRAWTO H4,V 4310 DRAWTO H4,V6:DRAWTO H,V6 4320 PLOT H,V3:DRAWTO H4,V3:RETURN 4400 PLOT H,V:DRAWTO H,V3:DRAWTO H4,V3 4410 PLOT H+3,V:DRAWTO H+3,V6:RETURN 4500 PLOT H4,V:DRAWTO H,V 4510 DRAWTO H,V3:DRAWTO H4,V3 4520 DRAWTO H4,V6:DRAWTO H,V6:RETURN 4600 PLOT H,V:DRAWTO H,V6:DRAWTO H4,V6 4610 DRAWTO H4,V3:DRAWTO H,V3:RETURN 4700 PLOT H,V:DRAWTO H4,V:DRAWTO H4,V+2 4710 DRAWTO H,V6:RETURN 4800 PLOT H,V:DRAWTO H4,V:DRAWTO H4,V6 4810 DRAWTO H,V6:DRAWTO H,V 4820 PLOT H,V3:DRAWTO H4,V3:RETURN 4900 FLOT H4,V6:DRAWTO H4,V:DRAWTO H,V 4910 DRAWTO H,V3:DRAWTO H4,V3:RETURN 5000 FOR J=LEN"9" THEN 5040 5015 IF D$<"0" THEN 5040 5016 D=VAL 5020 IF D=0 THEN 5040 5030 ON D GOSUB 4100,4200,4300,4400,4500,4600,47 00,4800,4900:GOTO 5090 5040 IF D*="0" THEN GOSUB 3000:GOTO 5090 5050 IF D$~ : “E" THEN GOSUB 3100: GOTO 5090 5060 IF D*="+" THEN GOSUB 3200:GOTO 5090 5070 IF D‘t="-" THEN GOSIJB 3300: GOTO 5090 5080 IF 0$="." THEN GOSUB 3400 5090 H-H-S:NEXT J:RETURN 6000 IF PEEK (195) OS THEN 6050 6010 TRAP 6000-.PRINT -.PRINT " ** ILLEGAL ENTRY * *":PRINT 6020 ON X 7. GOTO 1300,1330,1490,1520 6050 END EASY CHANGES 1. You may want the program to self-scale the Y axis for you. That is, you want it to use the minimum and maximum Y values that it finds as the limits on the Y axis. This can be accomplished by adding the following lines: 1443 IF LEN(STR$(MX)) > 8 OR LEN(STR$(MN)) > 8 THEN 1450 1444 IF MX< =MN THEN 1450 1445 YU = MX:YL = MN:PRINT 1447 YU$ = STR$(YU):YL$ = STR$(YL):GOTO 1560 (Note: on rare occasions, the program will not be able to perform the desired self-scaling. In these cases, the program will revert to requesting the Y scaling from you.) 2. Do you sometimes forget to enter the subroutine at line 200 despite the introductory warning? As is, the program will plot the straight line Y=0 if you do this. If you want a more drastic reaction to prevent this, change line 200 to read: 200 PRINT:PRINT “DEFINE SUBROUTINE AT LINE 200”: END GRAPH 221 Now, if you don’t enter the actual subroutine desired, the program will stop after printing the warning message to define the sub¬ routine. MAIN ROUTINES 110- 150 200- 999 1200-1295 1300-1380 1400-1570 1600-1640 2000-2240 2300-2360 2400-2570 3000-3420 3500-3530 4100-4910 5000-5090 6000-6050 Initializes variables, dimensions arrays, begins execution. User-supplied subroutine to evaluate Y as a function of X. Displays introductory warning. Gets X scaling from user. Determines the minimum, maximum Y values; gets Y scale from user. Waits for user to hit any key. Draws plot axes. Labels each axis. Draws plot and terminates program. Subroutine to draw characters O, E, +, — in high resolution. Subroutine to draw off-axis line at H,V. Subroutines to draw digits 1-9 in high resolution. Subroutine to draw scaling values. Error trap for program errors. MAIN VARIABLES XL, XM, Lower, middle, upper scale values of X. XU YL, YM, Lower, middle, upper scale values of Y. YU DX, DY Scale increments of X, Y. X, Y Current values of X, Y. XL$, String representation of XL, XM, XU. XM$, XU$ YL$, String representation of YL, YM, YU. YM$, YUS 222 Basic Programs for the Atari D$, A$ Temporary string variables. D Numeric value of D$. H, V Horizontal, vertical position in hi-res units. J Loop index. MN, MX Minimum, maximum values of Y. XZ, L Work variables. HP, VP Horizontal, vertical position of axes origin. YY Y direction offset in hi-res units. AF Y position flag (0=in bounds, 1 = out of bounds). H4,V3, H,V offsets. V6 SUGGESTED PROJECTS 1. Determine and display the values of X at which the minimum and maximum values of Y occur. 2. After the graph is plotted, allow the user to obtain the exact value of Y for any given X. INTEGRAL PURPOSE AND DEFINITION The need to evaluate integrals occurs frequently in much scientific and mathematical work. This program will numerically integrate a function that you supply using a technique known as Simpson’s rule. It will continue to grind out successive approximations of the integral until you are satisfied with the accuracy of the solution. Mathematical integration will probably be a familiar term to those who have studied some higher mathematics. It is a fundamental subject of second-year calculus. The integral of a function between the limits x=£ (lower limit) and x=u (upper limit) represents the area under its curve; i.e., the shaded area in Figure 1. We may approximate the integral by first dividing up the area into rectangular strips or segments. We can get a good estimate of the total integral by summing the areas of these segments by using a parabolic fit across the top. For those who understand some mathe¬ matical theory, Simpson’s rule may be expressed as j f(x)dxsyjf(f)+f (u) N/2 (N—2)/2 +4 £ f[£+A(2j —1)]+2 £ f[f+2Aj] j=i j=i J Here N is the number of segments into which the total interval is divided. N is 4 in the diagram. 224 Basic Programs for the Atari y For a good discussion of the numerical evaluation of integrals see: McCracken, Dorn, Numerical Methods and FORTRAN Program¬ ming, New York, Wiley, 1964, p. 160. Don’t let the word “FOR¬ TRAN” scare you away. The discussions in the book are independent of programming language with only some program examples written in FORTRAN. HOW TO USE IT The program begins with a warning! This is remind you that you should have already entered the subroutine to evaluate Y as a function of X. This subroutine must start at line 200. More about it shortly. You will then be asked to provide the lower and upper limits of the integration domain. Any numerical values are acceptable. It is not even necessary that the lower limit of X be smaller than the upper one. INTEGRAL 225 The program will now begin displaying its numerical evaluations of the integral. The number of segments used in the calculation continually doubles. This causes the accuracy of the integral to in¬ crease at the expense of additional computation time. For most func¬ tions, you should see the value of the integral converging quickly to a constant (or near constant) value. This, of course, will be the best numerical evaluation of the integral at hand. When you are satisfied with the accuracy of the solution, you must hit BREAK to terminate the program. If not, the program will run forever (assuming you can pay the electric bills). The amount of computation is approximately doubled each step. This means it will take the computer about the same amount of time to compute the next step that it took to compute all the previous steps. Thus, it will soon be taking the ATARI hours, days, and weeks to compute steps. Eventually, round-off errors begin degrading the results, causing a nice, constant, converged solution to change. The function to be integrated can be as simple or as complicated as you desire. It may take one line or a few hundred lines of code. In any case, the subroutine to express it must start at line 200. This sub¬ routine will be continually called with the variable X set. When it returns, it should have set the variable Y to the corresponding value of the function for the given X. The subroutine must be able to evaluate the function at any value of X between the lower and upper bounds of the integration domain. If your function consists of experimental data at discrete values of X, you must do something to enable the subroutine to evaluate the function at intermediate values of X. We recommend one of two approaches. First, you could write the subroutine to linearly interpo¬ late the value of Y between the appropriate values of X. This will involve searching your data table for the pair of experimental X values that bound the value of X where the function is to be evalu¬ ated. Secondly, the program CURVE presented elsewhere in this section can produce an approximate polynomial expression to fit your experimental data. This expression can then be easily entered as the subroutine at line 200. By the way, Simpson’s rule is exact for any polynomial of degree three or less. This means that if the function can be written in the form Y=A*XA3 + B*XA2+C*X+D where A, B, C, and D are constants, the program will calculate the integral exactly even with only two segments. 226 Basic Programs for the Atari SAMPLE RUN The sample run illustrates the following integration: r 1 4 dx x=0 1+x 2 This integral has the theoretical value of 7r (pi) as the correct answer! Pi, as you may know, has the value 3.1415926535.... Before the run is started, the above function is entered at the line 200. Note how the function converges and then diverges as errors build up in the calculations. The operator enters the integrand function at line 200 and types RUN to start the program. INTEGRAL 227 The upper and lower bounds of the integration are entered as requested. The results are computed up to 1024 segments. Then BREAK is pressed to terminate the calculation. 228 Basic Programs for the Atari PROGRAM LISTING 10 REM INTEGRAL 15 REM COPYRIGHT 1984 DILITHIUM PRESS 130 GRAPHICS 0:PRINT CHR$(125> 150 N=2:GOTO 1200 170 REM ISO REM ** ENTER SUBROUTINE AT 200 ** 190 REM 200 REM *#** Y=F:GOSUB 200:2=Z+Y 1480 NEXT 0:T=T+4*Z 1500 M=M-1:IF M=0 THEN 1600 1510 2=0:FOR 0=1 TO M 1520 X=L+DX*2*0:GOSUB 200:Z=Z+Y 1530 NEXT J:T=T+2*Z 1600 A=DX*T/3 1610 PRINT N,A 1620 N=N*2 1630 GOTO 1400 2000 IF PEEK <195) 08 THEN 2050 INTEGRAL 229 2010 TRAP 2000:PRINT :PRINT "** ILLEGAL ENTRY *# II 2020 IF X7. = l THEN 1300 2030 IF XZ=2 THEN 1310 2050 END EASY CHANGES 1. You might want the program to stop calculation after the integral has been evaluated for a given number of segments. Adding the following line will cause the program to stop after the integral is evaluated for a number of segments greater than or equal to 100. 1615 IF N >= 100 THEN END Of course, you may use any value you wish instead of 100. 2. Perhaps you would like to see the number of segments change at a different rate during the course of the calculation. This can be done by modifying line 1620. To increase the rate of change, try 1620 N=N*4 To change it at a constant (and slower) rate, try 1620 N = N + 50 Be sure, however, that the value of N is always even. MAIN ROUTINES 130- 150 200- 999 1200-1295 1300-1310 1360-1380 1400-1420 1450-1480 1500-1530 1600-1630 2000-2050 Initializes variables and goes to mainline routine. User-supplied subroutine to evaluate f(X). Displays introductory messages and warning. Gets integration limits from operator. Displays column headings. Computes integral contribution from end points. Adds contribution from one summation. Adds contribution from other summation. Completes integral calculation and displays it. In¬ creases number of segments and restarts calculation. Error trap for input errors. 230 Basic Programs for the Atari MAIN VARIABLES N Number of segments. J Loop index. L, U Lower, upper integration limit of x. DX Width of one segment. T Partial result of integral. M Number of summations. Z Subtotal of summations. A Value of integral. X Current value of x. Y Current value of the function y=f(x). XZ Error trap flag. SUGGESTED PROJECTS 1. Research other similar techniques for numerical integration such as the simpler trapezoid rule. Then add a column of output com¬ puting the integral with this new method. Compare how the two methods converge toward the (hopefully) correct answer. 2. Modify the program to compute answers to “double precision” or greater; i.e., at least fifteen significant digits. Try the function used in the Sample Run to see if you can calculate pi to this high degree of precision. SIMEQN PURPOSE This program solves a set of simultaneous linear algebraic equa¬ tions. This type of problem often arises in scientific and numerical work. Algebra students encounter them regularly—many “word” problems can be solved by constructing the proper set of simul¬ taneous equations. The program can handle up to 20 equations in 20 unknowns. This should prove more than sufficient for any practical application. The equations to be solved can be written mathematically as follows: AjjX,+Ai 2 X 2 + ... +AjnXn = Ri A2lXj +A 22 X 2 + ... +A2nXn = R2 AniX, +AN2X2+ . .. +AnnXn=Rn N is the number of equations and thus the number of unknowns also. The unknowns are denoted X ; through X n . Each equation contains a coefficient multiplier for each unknown and a right-hand-side term. These coefficients (the A matrix) and the right-hand-sides (R! through Rn) must be constants—positive, nega¬ tive, or zero. The A matrix is denoted with double subscripts. The first subscript is the equation number and the second one is the unknown that the coefficient multiplies. 232 Basic Programs (or the Atari HOWTO USE IT The program will prompt you for all necessary inputs. First, it asks how many equations (and thus how many unknowns) comprise your set. This number must be at least 1 and no more than 20. Next, you must enter the coefficients and right-hand-sides for each equation. The program will request these one at a time, continually indicating which term it is expecting next. Once it has all your inputs, the program begins calculating the solution. This may take a little while if the value of N is high. The program ends by displaying the answers. These, of course, are the values of each of the unknowns, X, through Xn. If you are interested, the numerical technique used to solve the equations is known as Gaussian elimination. Row interchange to achieve pivotal condensation is employed. (This keeps maximum significance in the numbers.) Then back substitution is used to arrive at the final results. This technique is much simpler than it sounds and is described well in the numerical analysis books referenced in the bibiliography. SAMPLE PROBLEM AND RUN Problem: A painter has a large supply of three different colors of paint: dark green, light green, and pure blue. The dark green is 30% blue pigment, 20% yellow pigment, and the rest base. The light green is 10% blue pigment, 35% yellow pigment, and the rest base. The pure blue is 90% blue pigment, no yellow pigment, and the rest base. The painter, however, needs a medium green to be composed of 25% blue pigment, 25% yellow pigment, and the rest base. In what percentages should he mix his paints to achieve this mixture? Solution: Let X, = percent of dark green to use, X 2 =percent of light green to use, X 3 =percent of pure blue to use. The problem leads to these three simultaneous equations to solve: 0.3 X,+0.1 Xj+0.9 X 3 =0.25 0.2 X,+0.35 X 2 =0.25 Xj + X 2 + X 3 = 1.0 The first equation expresses the amount of blue pigment in the mix¬ ture. The second equation is for the yellow pigment. The third equa¬ tion states that the mixture is composed entirely of the three given paints. (Note that all the percentages are expressed as numbers from 0-1.) The problem leads to the following use of SIMEQN. SIMEQN 233 - 51 l t 3NE i jU 5 LINEUP EQUATION SOLuER -Eh MANY EQUATIONS IN THE 3 E T ' 3 'HP 3 UNKNOWNS WILL 8E DENOTED -i- 1 H r SOUGH H 3 iNTER Q ?ppi THE PARAMETERS FOR EQUATION i lCIENT OF HI 7 , 3 [ C IE N T OF M2' 7 . i [ CIE N T OF M3 7 ,? PIG HI HAND 5 ID E 7 , 2 5 ENTER THE PARAMETERS FOR EQUATION 2 i [CIENT OF Ml 7 ! J The operator chooses to solve a set of three simultaneous equations and then enters the coefficients for the first equations. 1 ^ : " : p -i~e M r “i:iENT0Fi3'0 1 - ” h A N D 3 I D E ' 2 5 1 f ** ■": ER The PARAMETERS FOR EQUATION 3 : FICIE N T OF Ml'l :E rP :-::ENT of m 2 ’ i :efficient of h z 7 1 < t HAND 5 I D E ' 1 * 1 SOLUTIONIS = 0 5 5 = 0 4 = 0 0 5 j The coefficients for the remaining two equations are entered and the computer provides the solution. The painter should use a mixture of 55 % dark green, 40% light green, and 5 % pure blue. 234 Basic Programs for the Atari PROGRAM LISTING 10 REM SIMEON 15 REM COPYRIGHT 1984 DILITHIUM PRESS 130 GRAPHICS 0 150 M=2C) 160 TRAP 3000 200 PRINT CHR$ <125) 210 PRINT "A SIMULTANEOUS LINEAR EQUATION SOLVER It 220 PRINT SPRINT 300 XZ=1SPRINT "HOW MANY EQUATIONS IN THE SET";: INPUT N 310 PRINT s N=INT 0 AND N<=M THEN 330 320 PRINT " ** ERROR ** IT MUST BE BETWEEN 1-";M SPRINT :GOTO 300 330 DIM A(N,N>,R(N),V= XX 420 GOSUB 900:NEXT J 430 GOSUB 2000 500 PRINT "THE SOLUTION IS"SPRINT 510 FOR J = 1 TO N 320 PRINT " X " ; J ; " = ";V(J> 530 NEXT J:END 900 PRINT :FOR L=1 TO 36s PRINT CHR*<18);sNEXT L 910 PRINT SPRINT sRETURN 2.000 IF N=1 THEN V < 1 > =R < 1)/A < 1, 1) s RETURN 2010 FOR K=1 TO N-l 2020 I=K+1 2030 L=K 2040 IF ABS(A ABS(A THEN L=I 2050 IF I =R :FOR I=N-1 TO 1 STEP -1 2160 Q=0:FOR J=I+1 TO NiQ=Q+A(I,J)*V(J) 2170 V(I) = (R(I)—Q)/A ,N$(40),R*(4G> 150 B*="*BACK":E*="*END" 160 MX=100 170 DIM D*(MX*40>,V(MX>,Z 1B0 1 (0) =0: N$=CHR$ (32) : N$(40)=N$:N*(2)=N$ 200 GRAPHICS 0:P0KE 82,2:PRINT CHR*(125) 210 POSITION 14,2:PRINT ”S T A T 8":PRINT 220 PRINT " THIS PROGRAM DOES A STATISTICAL" 230 PRINT "ANALYSIS ON A LIST OF DATA VALUES." 240 PRINT "IT CAN SORT THE LIST AND FIND SEVERAL II 250 PRINT "STATISTICAL QUANTITIES DESCRIBING" 260 PRINT "THE DATA. "-.PRINT 2.70 PRINT " THE DATA MAY BE ENTERED IN EITHER" 2B0 PRINT "OF TWO FORMS:":PRINT 290 PRINT " 1) AS A SIMPLE LIST OF VALUES, OR": PRINT 300 PRINT " 2) WITH AN IDENTIFIER ACCOMPANYING" 310 PRINT " EACH VALUE.":PRINT 320 PRINT " WOULD YOU LIKE TO USE IDENTIFIERS" 330 PRINT "WITH YOUR INPUT (Y OR N) ?"; 335 POKE 764,255 340 R=PEEK(764):IF R=255 THEN 340 350 IF R=43 THEN PRINT "YES":F=1:GOTO 400 360 IF R=35 THEN PRINT "NO":F=0:GOTO 400 370 GOTO 340 400 POKE 764,255:GOSUB 2100:PRINT 410 PRINT " THE DATA MUST NOW BE ENTERED." 420 PRINT :IF F=1 THEN 460 430 PRINT " ENTER EACH VALUE SEPARATELY IN" 440 PRINT "RESPONSE TO THE QUESTION MARK." 450 GOSIJB 2000-.GOTO 500 460 PRINT " FOR EACH DATA ITEM, ENTER ITS" 470 PRINT "IDENTIFIER (ABBREVIATED I.D.) AND ITS II 480 PRINT "VALUE IN RESPONSE TO THE SEPARATE" 490 PRINT "QUESTION MARKS.":GOSUB 2000 500 GOSUB 2100:POKE 764,255:N=1 510 IF N<1 THEN N=1 520 PRINT :PRINT "DATA ITEM #";N 530 IF F=0 THEN D$((N-l)#40+1,(N-l)#40+10)=N$:GO TO 570 540 PRINT "I.D.";:INPUT R*:IF R$=E* THEN 700 545 IF R*="" THEN 540 550 IF R*=B* THEN N=N-1:GQT0 510 552 IF LEN(R*)>=10 THEN 560 555 R*(LEN(R$)+1,10)=N*(1,10-LEN(R$>) 244 Basic Programs for the Atari 560 D!t < (N~l) *40+1, (N—l) *40+10) =R$ <1 , 10) 570 PRINT "VALUE";:INPUT R*: IF R$=E$ THEN 700 580 IF R$=B$ AND F=1 THEN 520 590 IF R$=B$ THEN N=N-1:G0T0 510 592 TRAP 595:V(N)=VAL< R$) :GOTO 600 595 PRINT " ** ILLEGAL ENTRY * *";TRAP 40000:GOTO 570 600 TRAP 40000 610 IF N=MX THEN PRINT :PRINT “ ** NO MORE DATA ALLOWED! * *":N-N+1:GOTO 700 620 N=N+1:GOTO 510 700 N—N-•1:11- N~0 THEN PRINT 710 IF N=0 THEN PRINT " ** NO DATA ENTERED — RU N ABORTED **":END 720 GOSUB 2100 730 PRINT :PRINT “ — CONTINUATION OPTIONS —": P PINT 740 PRINT " 750 PRINT " 760 PRINT " 770 PRINT " 1) LIST DATA IN ORIGINAL ORDER" 2) LIST DATA IN RANKING ORDER" 3) DISPLAY STATISTICS" 4) END PROGRAM" 775 TRAP 3000 7BO PRINT :PRINT " WHAT NEXT (1, 2, 3, OR 4) :INPUT R 790 R=INT(R):IF R<1 OR R>4 THEN 730 BOO IF R=4 THEN END BIO ON R GOSUB 1000,1200,1500 820 GOTO 720 1000 GOSUB 2100:PRINT 1010 PRINT " THE ORIGINAL DATA ORDER":PRINT 1020 PRINT N;" TOTAL ENTRIES":GOSUB 2300 1030 PRINT :PRINT POSITION 5,PEEK(84):PRINT "VALUE"; 1040 IF F==0 THEN PRINT 1050 IF F~1 THEN POSITION 22,PEEK(84):PRINT "I.D 1060 FOR J=1 TO N 1070 PRINT DEPOSITION 5, PEEK (84) : PRINT V>; : POSITI DIM 22, PEEK (84) 1325 PRINT D$((Z(J)-1>*40+1,(Z (J > -1)*40+10) 1330 GQQUB 2500 1340 NEXT JsGOSUB 2900:RETURN 1500 GQSUB 2100SPRINT : TRAP 40000 1510 PRINT M STATISTICAL ANALYSIS":PRINT 1520 PRINT " YOUR LIST HAS ”;N;” VALUES" 1530 NP=0:NN=NP:NZ=NP:SQ=NP:W=NP 1540 FOR J=1 TO NsW=W+V(.J> :SQ=Sa+V(J)*V 1550 IF V(J>>0 THEN NP=NP+1 1560 IF V(JKO THEN NN=NN-t l 1570 IF V < J) =0 THEN NZ=NZ + 1 1590 NEXT J :M=W/NsVA=OsIF N-l THEN 1610 1600 VA=(SQ-N*M*M)/(N-l) 1610 SD=SQR(VA) 1620 PRINT CHR$(32);NP;" POSITIVE; ";NN;" NEGATI VE; " ;NZ;" ZERO"sPRINT 1630 GOSUB 2700-.PRINT " MINIMUM VALUE = ";V(Z(N> ) 1640 PRINT " MAXIMUM VALUE = ";V(Z(1>> 1650 PRINT " RANGE OF VALUES = ";V(Z(1>)-V ) ) /2 sPRINT " STD. DEVIATION - ";SD " VARIANCE = ";VA 2900:RETURN 2000 PRINT E" 2010 PRINT 2020 PRINT , TYPE" 2030 PRINT 2100 PRINT 2110 PRINT 2300 PRINT NG, YOU" 2310 PRINT sPRINT " IF YOU MAKE A MISTAKE, TYP B*;" TO RE-ENTER THE LAST DATUM." sPRINT " WHEN THE LIST IS COMPLETED E*;" TO TERMINATE THE LIST."sRETURN sFOR J = 1 TO 313s PRINT ; s NEXT J :RETURN sPRINT " WHILE THE LIST IS DISPLAY I "CAN HIT ANY KEY TO CAUSE A TEMPORARY 2320 PRINT "HALT. THE DISPLAY WILL RESUME WHEN" 2330 PRINT "YOU HIT ANOTHER KEY." 2340 PRINT sPRINT " HIT ANY KEY TO START THE DI SPLAY. ” 2345 POKE 764,255 2350 IF PEEK(764)=255 THEN 2350 2360 POKE 764,255s RETURN 2500 IF PEEK(764)=255 THEN RETURN 246 Basic Programs for the Atari 2510 POKE 764,255 2520 IF PEEK(764)=255 THEN 2520 2530 POKE 764,255:RETURN 2700 IF Z(0)=1 THEN RETURN 2710 FOR 0=1 TO N:Z(0)=J:NEXT J:IF N=1 THEN RETU RN 2.720 NM=N—1: FOR K=1 TO N: FOR 0=1 TO NM:N1 = Z(0) 2730 N2=Z(0+1):IF V(N1)>V(N2) THEN 2750 2740 7. (0 +1) =N 1: Z (0) =N2 2750 NEXT J:NEXT K:Z(0)=1:RETURN 2900 PRINT :PRINT " HIT ANY KEY TO CONTINUE" 2905 POKE 764,255 2910 IF PEEK(764)=255 THEN 2910 2920 POKE 764,255:RETURN 3000 IF PEEK (195) 08 THEN 3050 3010 GOTO 775 3050 END EASY CHANGES 1. The program arrays are currently dimensioned to allow a max¬ imum of 100 data items. The total storage required for the pro¬ gram depends upon the maximum dimension parameter, MX. Should your application require more than 100 data values, you will have to increase the value of MX in line 160 accordingly. To accommodate up to 300 data items, make this change 160 MX = 300 Of course, you will have to have enough RAM memory to enable this. 2. Because of possible conflicts with identifiers in your list, you may wish to change the special strings that signal termination of data input and/or the backing up of data input. These are controlled by the variables E$ and B$, respectively. They are set in line 150. Each string can be up to eight characters long. If you wish to terminate the data with /DONE/ and to back up with /LAST/, for example, line 150 should be: 150 B $=“ /LAST /”:E$ = “/DONE/” 3. You may wish to see your lists sorted from smallest value to largest value instead of the other way around, as done now. This can be accomplished by changing the “greater than” sign (>) in line 2730 to a “less than” sign (<). Thus: 2730 N2 = Z(J + 1):IF V(N1) < V(N2) THEN 2750 STATS 247 This will, however, cause a few funny things to happen to the statistics. The real minimum value will be displayed under the heading “maximum” and vice versa. Also, the range will have its correct magnitude but with an erroneous minus sign in front. To cure these afflictions, make these changes also: 1630 GOSUB 2700: PRINT “MINIMUM VALUE=” ;V(Z(1)) 1640 PRINT “MAXIMUM VALUE=” ;V(Z(N)) 1650 PRINT “RANGE = ”;V(Z(N)) — V(Z(1)) MAIN ROUTINES 140- 180 200- 370 400- 620 700- 710 720- 820 1000-1090 1200-1340 1500-1740 2000-2360 2500-2530 2700-2750 2900-2920 3000-3050 Initializes constants and dimensioning. Displays messages, determines if identifiers will be used. Gets data from the user. Checks that input contains at least one value. Command mode—gets user’s next option and does a GOSUB to it. Subroutine to list data in the original order. Subroutine to list data in ranking order. Subroutine to calculate and display statistics. Subroutines to display various messages. Subroutine to allow user to temporarily start and stop display listing. Subroutine to sort the list in ranking order. Subroutine to detect if user has hit a key to continue. Trap routine for program errors. MAIN VARIABLES MX Maximum number of data values allowed. D$ String array of identifiers. V(MX) Array of the data values. Z(MX) Array of the sorting order. N Number of data values in current application. F Flag on identifier usage (1=yes, 0=no). B$ Flag string to back up the input. E$ Flag string to signal end of the input. N$ String for a null identifier. 248 Basic Programs for the Atari R$ User input string. NM N—1. R Continuation option. NP Number of positive values. NN Number of negative values. NZ Number of zero values. W Sum of the values. SQ Sum of the squares of the values. M Mean value. MD Median of the values. VA Variance. SD Standard deviation. J, K Loop indices. N1.N2 Possible data locations to interchange during sorting Q Work variable. SUGGESTED PROJECTS 1. The sorting algorithm used in the program is efficient only when the number of list items is fairly small—less than 25 or so. This is because it does not do checking along the way to see when the list becomes fully sorted. If your lists tend to be longer than 25 items, you might wish to use another sorting algorithm more appropriate for longer lists. Try researching other sorts and incorporating them into the program. To get you started, try these changes: 2720Q=0:FORJ=1 TO N —1:N1 =Z(J) 2730 N2=Z(J + 1):IF V(N1)> =V(N2) THEN 2750 2745 Q=1 2750 NEXT J:IFQ = 1 THEN 2720 2760 Z(0) = 1 :RETURN If your lists are short, this routine will probably be a little slower than the current one. However, for longer lists it will save propor¬ tionately more and more time. 2. Many other statistical parameters exist to describe this kind of data. Research them and add some that might be useful to you. One such idea is classifying the data. This consists of dividing the range into a number of equal classes and then counting how many values fall into each class. Section 6 Miscellaneous Programs INTRODUCTION TO MISCELLANEOUS PROGRAMS These programs show how simple programs can do interesting things. All of them have a mathematical flavor. They are short and, as such, would be useful for study for those just learning BASIC in particular or programming in general. Monte Carlo simulation involves programming the computer to conduct an experiment. (It doesn’t involve high-stakes gambling!) PI shows how this technique can be used to calculate an approximation to the famous mathematical constant pi. PYTHAG will find all right triangles with integral side lengths. A clever algorithm is utilized to do this. Have you ever looked around your classroom or club meeting and wondered if any two people had the same birthdate? BIRTHDAY will show you what the surprising odds are. Very high precision arithmetic can be done on the ATARI with the proper “know-how.” POWERS will calculate the values of integers raised to various powers, not to the ATARI’S “normal” nine-digit precision, but up to 250 full digits of precision. BIRTHDAY PURPOSE Suppose you are in a room full of people. What is the probability that two or more of these people have the same birthday? How many people have to be in the room before the probability becomes greater than 50 percent? We are talking only about the month and day of birth, not the year. This is a fairly simple problem to solve, even without a computer. With a computer to help with the calculations, it becomes very easy. What makes the problem interesting is that the correct answer is nowhere near what most people immediately guess. Before reading further, what do you think? How many people have to be in a room before there is better than a 50-50 chance of birthday duplication? 50? 100? 200? HOWTO USE IT When you RUN the program, it starts by displaying headings over two columns of numbers that will be shown. The left column is the number of people in the room, starting with one. The right column is the probability of birthday duplication. For one person, of course, the probability is zero, since there is no one else with a possible duplicate birthday. For two people, the probability is simply the decimal equivalent of 1/365 (note that we assume a 365-day year, and an equal likelihood that each person could have been bom on any day of the year). 252 Basic Programs for the Atari What is the probability of duplication when there are three people in the room? No, not just 2/365. It’s actually 1 —(364/365 times 363/365) This is simply one minus the probability of no duplicate birthdays. The probability for four people is 1 -(364/365 times 363/365 times 362/365) The calculation continues like this, adding a new term for each additional person in the room. You will find that the result (proba¬ bility of duplication) exceeds .50 surprisingly fast. The program continues with the calculation until you enter “Q” to stop it. The program stops every 15 or 16 people and waits for you to press the RETURN key. This allows you to examine values before they scroll off the screen. SAMPLE RUN r— c :b o c : op mope ■ W M I r H SOME BIPTHC'ui' ■ f 0 ■ 1 2 , 77377E-0 7 1 1 3 . 2 0 41''E-0 3 1 f i 0 , 01635532 1 0 . 0 2 7 i 7 5 5 3 j 1 2 0 04046:5 1 0 05627572 0 . 0 7472571 0 . 0 3 4 5 2 7 3 5 ; .) ►3 . 115 3 4 3 2 1 1. 0 1411414 :: 0 16702431 ■ 0 1344107 L 4 0 . 22710 254 | 0 25230174 , - j i'l jj T T 1 L: - *jBH TO CONTINUE'■ j After the probability of 15 people with duplicate birthdays is shown, the option to quit or continue is offered. BIRTHDAY 253 PROGRAM LISTING 10 REM BIRTHDAY 15 REM COPYRIGHT 1984 DILITHIUM PRE8S 110 DIM X$<1):N=1:Q=1 120 GRAPHICS 0:POKE 82,2 130 PRINT CHR$(125):PRINT "NO. OF PROB. OF 2 OR MORE" 140 PRINT "PEOPLE WITH SAME BIRTHDAY" 150 PRINT N,1—Q 160 Q=Q*(365-N)/365 170 N=N+1:IF <*16<>0 THEN 150 180 PRINT :PRINT "ENTER Q TO QUITPRINT "OR PR ESS RETURN TO CONTINUE"INPUT X* 190 IF X$< >"Q" THEN 130 200 END EASY CHANGES 1. To make the program stop after a specific number of people (such as 50), make these changes: 170 N=N+1: IFN< =50 THEN 150 175 END MAIN ROUTINES 110-120 Initializes variables and clears screen. 130-140 Displays headings. 150-160 Calculates probability of no duplication, then displays probability of duplication. 170-200 Adds a person. Determines if screen is full. Waits for key to be entered. MAIN VARIABLES N Number of people in the room. Q Probability of no duplication of birthdays. X$ Input string. SUGGESTED PROJECTS Modify the program to allow for leap years in the calculation, instead of assuming 365 days per year. PI PURPOSE AND DISCUSSION The Greek letter pi, x, represents probably the most famous con¬ stant in mathematical history. It occurs regularly in many different areas of mathematics. It is best known as the constant appearing in several geometric relationships involving the circle. The circum¬ ference of a circle of radius r is 2xr, while the area enclosed by the circle is xr 2 . Being a transcendental number, pi cannot be expressed exactly by any number of decimal digits. To nine significant digits, its value is 3.14159265. Over many centuries, man has devised many different methods to calculate pi. This program uses a valuable, modern technique known as com¬ puter simulation. The name “simulation” is rather self-explanatory; the computer performs an experiment for us. This is often desirable for many different reasons. The experiment may be cheaper, less dangerous, or more accurate to run on a computer. It may even be impossible to do in “real life.” Usually, however, the reason is that the speed of the computer allows the simulation to be performed many times faster than actually conducting the real experiment. This program simulates the results of throwing darts at a specially constructed dartboard. Consider Figure 1 which shows the peculiar square dartboard involved. The curved arc, outlining the shaded area, is that of a circle with the center in the lower left hand comer. The sides of the square, and thus the radius of the circle, are consid¬ ered to have a length of 1. 256 Basic Programs for the Atari X Figure 1 . The PI Dartboard Suppose we were able to throw darts at this square target in such a way that each dart had an equal chance of landing anywhere within the square. A certain percentage of darts would result in “hits,” i.e., land in the shaded area. The expected value of this percentage is simply the area of the shaded part divided by the area of the entire square. The area of the shaded part is one-fourth of the area that the entire circle would enclose if the arc were continued to completely form the circle. Recall that the area of a circle is 7rr 2 where r is the radius. In our case, r=l, and the area of the entire circle would simply be v. The shaded area of the dartboard is one-fourth of this entire circle and thus has an area of ir/4. The area of the square is s 2 , where s is the length of one side. On our dartboard, s=l, and the area of the whole dartboard is 1. Now, the expected ratio of “hits” to darts thrown can be ex¬ pressed as PI 257 r>»# hits shaded area x/4 x # thrown entire area 1 4 So we now have an experimental way to approximate the value of x. We perform the experiment and compute the ratio of “hits” observed. We then multiply this number by four and we have calculated x experimentally. But instead of actually constructing the required dartboard and throwing real darts, we will let the ATARI do the job. The program “throws” each dart by selecting a separate random number between 0 and 1 for the X and Y coordinates of each dart. This is accomplished by using the built-in RND function of BASIC. A “dart” is in the shaded area if X 2 + Y 2 < 1 for it. So the program grinds away, continually throwing darts and deter¬ mining the ratio of “hits.” This ratio is multiplied by four to arrive at an empirical approximation to x. HOWTO USE IT The program requires only one input from you. This is the “sample size for printing,” i.e., how many darts it should throw before print¬ ing its current results. Any value of one or higher is acceptable. After you input this number, the program will commence the simulation and display its results. A cumulative total of “hits,” darts thrown, and the current approximation to x will be displayed for each multiple of the sample size. This will continue until you press BREAK. When you are satisfied with the total number of darts thrown, press BREAK to terminate the program execution. 258 _ Basic Programs for the Atari SAMPLE RUN The operator selects 150 for the printing sample size. 1950 darts are “thrown”. The program will continue until the BREAK key is pressed. ;A«PLE . A R T B 0 A R 0 PI CALCULATOR 1 SIZE FOR PRINTING?15OB ' l 3 5 z z ? 452 S J w k L - A L L U L A j A 3 « THROWN PI 150 2,3 ZOO 2.37777777 450 Z.OiZZZZZZ 500 Z.01ZZZZZZ 750 Z.04572777 6 ? y : 0 4 3 0 0 Z . 0 6 5 5 5 5 5 5 10 5 0 Z . 0 5 2 -3 5 7 1 4 1200 Z .09722272 1 0 4 5 ; ~ m f 125 0 2 03325325 1500 2,112 1550 7,123535 3 5 1 4 11 1300 7.17555555 ■ 1350 7,14461573 PI 259 PROGRAM LISTING 10 REM PI 15 REM COPYRIGHT 1984 DILITHIUM PRESS 160 T=0:TH=T 200 TRAP 1000 300 GOSUB 600 310 PRINT "SAMPLE SIZE FOR PRINTING"INPUT NP 320 NP=INT(NP):IF NP<1 THEN 300 330 GOSUB 600 340 PRINT "# HITS","# THROWN",CHR*<32);CHR*(32 )5 CHR* (32);"PI" 400 GOSUB 500:TH=TH+NH:T=T+NP:P=4#TH/T 410 PRINT CHR* (32) ; CHR* (32) ;TH,CHR*(32) ; CHR* (32) ;t,p 420 GOTO 400 500 NH=0:FOR J=1 TO NP 510 X=RND(1)s Y=RND(1) 520 IF (X*X+Y*Y)<1 THEN NH=NH+1 530 NEXT J:RETURN 600 PRINT CHR*<125):POKE 82,2:POSITION 6,PEEK(84 ) 610 PRINT "A DARTBOARD PI CALCULATOR" 620 PRINT :PRINT sRETURN lOOO IF PEEK(195)=8 THEN TRAP 1000:GOTO 300 EASY CHANGES 1. If you want the program to always use a fixed sample size, change line 310 to read 310NP=150 Of course, the value of 150 given here may be changed to whatever you wish. With this change, line 320 is not needed and may be deleted. 2. If you want the program to stop by itself after a certain number of darts have been thrown, add the following two lines: 315 PRINT “TOTAL # DARTS TO THROW”:INPUT ND 415 IF T > = ND THEN END This will ask the operator how many total darts should be thrown, and then terminate the program when they have been thrown. 260 Basic Programs for the Atari MAIN ROUTINES 160-200 Initializes. 300-340 Gets operator input, displays column headings. 400-420 Calculates and displays results. 500-530 Throws NP darts and records number of “hits.” 600-620 Clears screen and displays program title. 1000 Error trap for illegal inputs. MAIN VARIABLES T Total darts thrown. TH Total “hits.” NP Sample size for printing. NH Number of hits in one group of NP darts. P Calculated value of pi. X,Y Random-valued coordinates of a dart. J Loop index. SUGGESTED PROJECTS 1. Calculate the percentage error in the program’s calculation of pi and display it with the other results. You will need to define a variable, say PI, which is set to the value of pi. Then the percent¬ age error, PE, can be calculated as: PE= 100*ABS(P—PI)/PI 2. The accuracy of this simulation is highly dependent on the quality of the computer’s random number generator. TYy researching dif¬ ferent algorithms for pseudo random number generation. Then try incorporating them into the program. Change line 510 to use the new algorithm(s). This can actually be used as a test of the various random number generators. Gruenberger’s book, referenced in the bibliography, contains good material on various pseudo random number generators. POWERS PURPOSE By now you have probably learned that the ATARI keeps track of nine significant digits when dealing with numbers. For integers less than one billion (1,000,000,000), the ATARI can retain the precision value of the number. But for larger integers the ATARI only keeps track of the most significant (leftmost) nine digits, plus the exponent. This means, of course, that there is no way you can use the ATARI to deal with precise integers greater than one billion, right? Wrong. This program calculates either factorials or successive powers of an integer, and can display precise results that are up to 250 digits long. By using a “multiple-precision arithmetic” technique, this pro¬ gram can tell you exactly what 973 to the 47th power is, for example. HOW TO USE IT The program first asks you how many digits long you want the largest number to be. This can be any integer from 1 to 250. So, for example, if you enter 40, you will get answers up to 40 digits long. Next you are asked for the value of N. If you respond with a value of one, you are requesting to be shown all the factorials that will fit in the number of digits you specified. First you will get one factorial, then two factorial, and so on. In case you have forgotten, three factorial is three times two times one, or six. Four factorial is four times three times two times one, or twenty-four. 262 Basic Programs for the Atari If you enter an N in the range from 2 through 100,000, you are requesting the successive powers of that number up to the limit of digits you specified. So, if you provide an N of 23, you will get 23 to the first power, then 23 squared, then 23 cubed, and so on. Finally, after it has displayed the largest number that will fit within the number of digits you entered, the program starts over. The larger the number of digits you ask for, the longer it will take the program to calculate each number. If you ask for zero digits, the program ends. SAMPLE RUN :-e:: :n: 4 0 3-373 3 3 3 " 3 ? * ^ = 32*5521 3 3 4 10 31 3 0 7 4?06 3 3524323382^311447441 3 4 0 3 33 3 5 3"-3 44343535 u 3234? 3 2 3 5 *3 45 334 >3 442355 35255353 2 1 ? 2 4 ? 2 5 2 ? 3 * " * 5 O 3'4 O 2 1 - " 4 4 5542 ?" 4y" ! i2 *2 351334344 >3 13353352 •3142 32 -1 431 The operator wants answers up to 40 digits long in the calculations of the powers of98789. The program calculates numbers up to 98789 s and then asks for the number of digits again (in preparation for the next calcula¬ tion the operator requests). PROGRAM LISTING 10 REM POWERS 15 REM COPYRIGHT 1984 DILITHIUM PRESS 110 DIM N$(10) 120 GRAPHICS O:POKE 82,2:PRINT CHR$<125> 125 TRAP 1000 130 POSITION 9,1:PRINT "POWERS AND FACTORIALS" POWERS 263 140 PRINT :PRINT 150 DIM N(255) 160 FDR .3=1 TD 255:N(J)=0:NEXT J 170 X2=l:PRINT "NUMBER OF DIGITS INPUT M 175 IF M=0 THEN END 180 M=INT :IF M>250 OR M<1 THEN 170 190 XZ=2:PRINT :PRINT "N ";:INPUT N 200 N=INT(N) 210 IF N<1 OR N>100000 THEN 190 220 PRINT 230 F=0:IF N=1 THEN F=1:PRINT "FACTORIALS" 240 IF F=0 THEN PRINT “POWERS OF " 5 N:PRINT 250 T=10:K=1:N < O > =N 260 FOR J=0 TO M 270 IF N(JXT THEN 300 280 Q=INT=M THEN 500 340 D=0:PRINT K,CHR*(30) 5 CHR*(30);CHR$(30 )5 350 N*=STR$(N(J>) 360 D=D+1:IF D>30 THEN D=1:PRINT :POSITION 9,PEE K (84) 370 PRINT N*;:J=J-1:1F J>=0 THEN 350 380 N=N+F 390 K=K+1SPRINT 400 FOR J=0 TO M:N(J)=N(J)#N:NEXT J 410 GOTO 260 500 M=0:PRINT 510 GOTO 160 lOOO IF PEEK (195) 08 THEN 1050 1010 TRAP 1000s PRINT "** ILLEGAL ENTRY **" 1020 IF XZ = 1 THEN 170 1030 IF XZ=2 THEN 190 1050 END EASY CHANGES 1. To change the program so that it always uses, say, 50-digit num¬ bers, remove line 170, and insert this line: 170 M = 50 2. To clear the screen before the output begins being displayed, change line 220 to say: 220 GRAPHICS 0 264 Basic Programs for the Atari 3. If 250 digits isn’t enough for you, you can go higher. For 500 digits, make these changes: a. In line 150 and 160, change the 255 to 505. b. In line 180, change the 250 to 500. MAIN ROUTINES 110- 160 Displays title. Sets up array for calculations. 170- 240 Asks for number of digits and N. Checks validity of responses. Displays heading. 250 Initializes variables for calculations. 260- 300 Performs “carrying” in N array so each element has a value no larger than nine. 310- 320 Scans backwards through N array for first non-zero element. 330 Checks to see if this value would be larger than the number of digits requested. 340- 370 Displays counter and number. Goes to second line if necessary. 380- 390 Prepares to multiply by N to get next number. 400- 410 Multiplies each digit in N array by N. Goes back to line 260. 500- 510 Prepares for next request. Goes back to 160. 1000-1050 Error trap for illegal inputs. MAIN VARIABLES N Array in which calculations are made. M Number of digits of precision requested by operator. N Starting value. If 1, factorials. If greater than 1, powers of N. F Set to 0 if powers, 1 if factorials. T Constant value of 10. K Counter of current power or factorial. J Subscript variable. Q, W Temporary variables used in reducing each integer position in the N array to a value from 0 to 9. D Number of digits displayed so far on the current line (max¬ imum is 30). XZ Work variable for error trap. N$ String variable used to convert each digit into displayable format. POWERS 265 SUGGESTED PROJECTS 1. Determine the largest N that could be used without errors entering into the calculation (because of intermediate results exceeding one billion), then modify line 210 to permit values that large to be entered. 2. Create a series of subroutines that can add, subtract, multiply, divide, and exchange numbers in two arrays, using a technique like the one used here. Then you can perform high-precision calculations by means of a series of GOSUB statements. PYTHAG PURPOSE Remember the Pythagorean Theorem? It says that the sum of the squares of the two legs of a right triangle is equal to the square of the hypotenuse. Expressed as a formula, it is a 2 +b 2 =c 2 . The most commonly remembered example of this is the 3-4-5 right triangle (3 2 +4 2 =5 2 ). Of course, there are an infinite number of other right triangles. This program displays integer values of a, b, and c that result in right triangles. HOWTO USE IT To use this program, all you need to do is RUN it and watch the “Pythagorean triplets” (sets of values for a, b, and c) come out. The program displays 16 sets of values on each screen, and then waits for you to press any key (except BREAK or E) before it continues with the next 16. It will go on indefinitely until you press the E key (for “end”). The left-hand column shows the count of the number of sets of triplets produced, and the other three columns are the values of a, b, andc. The sequence in which the triplets are produced is not too obvious, so we will explain how the numbers are generated. 268 Basic Programs for the Atari It has been shown that the following technique will generate all primitive Pythagorean triplets. (“Primitive” means that no set is an exact multiple of another.) If you have two positive integers called R and S such that : 1. R is greater than S, 2. R and S are of opposite parity (one is odd and the other is even), and 3. R and S are relatively prime (they have no common integer divisors except 1), then a, b, and c can be found as follows: a=R 2 -S 2 b=2RS c=R 2 + S 2 The program starts with a value of two for R. It generates all possible S values for that R (starting at R — 1 and then decreasing) and then adds one to R and continues. So, the first set of triplets is created when R is two and S is one, the second set when R is three and S is two, and so on. SAMPLE RUN m .»»• pyTHflGOREON TRIPLETS - *«.«« V ■ : 0 U N T - - A - - — B — - c 1 1 4 5 1 I 2 5 12 13 1 1 - 7 24 25 1 f 4 15 3 17 1 5 3 4 0 41 1 6 21 2 0 2 3 11 6 0 61 3 3 5 12 37 ? 13 3 4 3 5 10 3 3 56 65 11 45 2 3 53 1 li 15 112 113 1 i T 3 3 0 3 ? 1 14 5 5 4 3 T? § i J H B 15 6 3 16 65 1 ■ 16 17 144 145 1 A -'RE5 5 5PACE T0 C 0 N T I N U E , E TO END M The program generates a screen full of Pythagorean triplets, then waits for the operator to press a key to continue. PYTHAG 269 PROGRAM LISTING 10 REM PYTHAG 15 REM COPYRIGHT 19S4 DILITHIIJM PRESS 130 R=2:K=1:D=0 150 GOSUB 350 1B0 S-R-l 190 A=R*R-S*S 200 B~2*R*S 210 C=R*R+S*S 220 PRINT K,A,B,C 230 K=K+1:D=D+1:GOTO 400 240 S~S-2:IF SOO THEN R=R+1:GGT0 ISO 250 S1=S:B1=R 260 N—INT(Bl/Sl) 270 R1=B1-0 THEN B1=S1:S1=R1:GOTO 260 300 IF SlOl THEN 240 320 GOTO 190 350 POKE 752,1:PRINT CHR*<125> 360 PRINT '•**** PYTHAGOREAN TRIPLETS ****" 370 PRINT 3S0 PRINT "COUNT","—A—","—B—","—C—" 390 RETURN 400 IF DC 16 THEN 240 420 PRINT :PRINT "PRESS SPACE TO CONTINUE, E TO END" 425 POKE 764,255 430 X=PEEK(764):IF X-255 THEN 430 440 IF X=42 THEN POKE 752,0:POKE 764,255:END 450 GOSUB 350:D~0 460 GOTO 240 EASY CHANGES 1. Alter the starting value of R in line 130. Instead of 2, try 50 or 100 . 2. If you want, you can change the number of sets of triplets dis¬ played on each screen. Change the 16 in line 400 to a 10, for example. You probably won’t want to try a value greater than 20, since that would cause the column headings to roll off the screen. 3. To make the program continue without requiring you to press a key for the next screen of values, insert either of these lines: 405 GOTO 450 or 405 GOTO 460 270 Basic Programs for the Atari The first will display headings for each screen. The second will only display the headings at the beginning of the run. MAIN ROUTINES 130 Initializes variables. 150 Displays the tide and column headings. 180 Calculates the first value of S for current R value. 190-210 Calculates A, B, and C. 220-230 Displays one line of values. Adds to counters. 240 Calculates next S value. If no more, calculates next R value. 250-300 Determines if R and S are relatively prime. 350-390 Subroutine to display title and column headings. 400-460 Checks if screen is full yet. If so, waits for key to be pressed. MAIN VARIABLES R, S See explanation in “How To Use It.” K Count of total number of sets displayed. D Count of number of sets displayed on one screen. A, B, C Lengths of the three sides of the triangle. S1, B1, Used in determining if R and S are relatively prime. Rl, N X Key pressed to continue. SUGGESTED PROJECTS 1. In addition to displaying K, A, B, and C on each line, display R and S. You will have to squeeze the columns closer together. 2. Because this program uses integer values that get increasingly larger, eventually some will exceed the ATARI’S nine-digit integer capacity and produce incorrect results. Can you determine when this will be? Modify the program to stop when this occurs. Appendix 1 Memory Usage The programs in this book were written on an ATARI 800 computer with 48K of RAM. They were also tested on ATARI 400, ATARI 600XL, ATARI 800XL, and ATARI 1200XL computers. All the programs should run unmodified on any of these machines, except for some of the longer programs (WALLOONS, GROAN) which will not run in a standard ATARI 400 or 16K ATARI 800 due to memory limitations. These programs can be modified to run by deleting some of the features in the program. Atari has recently introduced several new computers, including the 1400XL and 1450XL. These machines are purported to be software compatible with the earlier computers, but the programs in this book have not been tested on them. Several of the chapters in this book mention the limitations of the programs based on the number of “bytes free” in your system. This refers to the memory space available when Atari BASIC has been started, but no BASIC program has yet been entered. To find out how many bytes free your system has, enter the command PRINT FRE(0) after Atari BASIC is started. If this number is 10,000 or more, all the programs in this book will easily fit your system’s available RAM. If this number is 4000 or more, all but the longest programs will fit. Appendix 2 Microsoft BASIC for the Atari The programs in this book are written in standard Atari BASIC as available in the standard Atari BASIC CXL4002 plug-in ROM car¬ tridge (or built into the computer for the XL series) and defined in the Atari BASIC Reference Manual. Since this BASIC is available for all Atari computers there is no real need to adapt these programs to any other dialect of BASIC. On the other hand, there are a great many programs available written for the many versions of Microsoft BASIC, and it may be desirable to translate these programs into Atari BASIC. For those interested in experimenting, Atari Microsoft BASIC, ATARI P/N CX8126, is available on diskette. The two BASICs are similar, and programs can be adapted from one to the other with a bit of effort. The principal difference between the two BASICs is their handling of string variables, differences in some of the graphics commands, and differences in file operations. Appendices F and J of the Atari Microsoft BASIC Instruction Manual contain a summary of the differences between the languages. This summary can be used to translate programs from one BASIC format to the other. USING A DISKETTE OR CASSETTE If you purchased this book as a sofware package you will need loading instructions to get the programs into your computer. The loading instructions are in the back of this book. Once you have loaded the programs you can follow the recommendations supplied earlier except that you have saved lots of time by not having to type in the programs yourself. Bibliography BOOKS Bell, R. C., Board and Table Games From Many Civilizations, Ox¬ ford University Press, London, 1969. (WARI) Brown, Jerald R., Instant BASIC, dilithium Press, Beaverton, Ore., 1977. (Self-teaching text on the BASIC language) Cohen, Daniel, Biorhythms in Your Life, Fawcett Publications, Greenwich, Connecticut, 1976. (BIORHYTH) Crow, E. L., David, F. A., and Maxfield, M. W., Statistics Manual, Dover Publications, New York, 1960. (STATS) Croxton, F. E., Crowden, D. J., and Klein, S., Applied General Statistics (Third Edition), Prentice-Hall, Englewood Cliffs, N.J., 1967. (STATS) Gruenberger, Fred J., and Jalfray, George, Problems for Computer Solution, John Wiley and Sons, New York, 1965. (BIRTH¬ DAY, PI) Gruenberger, Fred J., and McCracken, Daniel D., Introduction to Electronic Computers, John Wiley and Sons, New York, 1961. (MILEAGE, PI, PYTHAG, WARI as Oware) Hildebrand, F. B., Introduction to Numerical Analysis, McGraw- Hill, New York, 1956. (CURVE, DIFFEQN, INTEGRAL, SIMEQN) Kuo, S. S., Computer Applications of Numerical Methods, Addison- Wesley, Reading, Massachusetts, 1972. (CURVE, DIFFEQN, INTEGRAL, SIMEQN) 274 Basic Programs for the Atari McCracken, Daniel D., and Dorn, W. S., Numerical Methods and FORTRAN Programming, John Wiley and Sons, New York, 1964. (CURVE, DIFFEQN, INTEGRAL, SIMEQN) Shefter, Harry, Faster Reading Self-Taught, Washington Square Press, New York, 1960. (TACHIST) PERIODICALS Feldman, Phil, and Rugg, Tom, “Pass the Buck,” Kilobaud, July 1977, pp. 90-96. (DECIDE) Fliegel, H. F., and Van Flandem, T. C., “A Machine Algorithm for Processing Calendar Dates,” Communications of the ACM, October, 1968, p. 657. (BIORHYTH) ERRATA OFFER All of the programs in this book have been tested carefully and are working correctly to the best of our knowledge. However, we take no responsibility for any losses which may be suffered as a result of errors or misuse. You must bear the responsibility of verifying each program’s accuracy and applicability for your purposes. If you want to get a copy of an errata sheet that lists corrections for any errors or ambiguities we have found to date, send a self- addressed stamped envelope (SASE) to the address below. Ask for errata for this book (by name). If you think you’ve found an error, please let us know. If you want an answer, include a SASE. Please keep in mind that the most likely cause of a program working incorrecdy is a typing error. Check your typing very care¬ fully before you send us an irate note about an error in one of the programs. Reread the “How To Use This Book” section, too. Software Support Errata —32 Programs for the ATARI dilithium Press 8285 S.W. Nimbus Suite 151 Beaverton, OR 97005 32 BASIC Programs for the ATARI Computer Loading Instructions Call our toll free number 800-547-1842 and ask for Software Support with any questions. What You Need To Run 32 BASIC Programs for the ATARI Computer ATARI 400, 800, 600XL, 800XL, 1200XL 16K of memory One disk drive Standard Monitor or TV with transfer switch for display BASIC cartridge (unless using an XL model). ATARI DOS Preliminary Steps Before you do anything else, make a backup copy of your 32 BASIC PROGRAMS disk (see your user's manual for instructions on how to do this). There are two sides of your program disk, (side A and side B), each containing part of the 32 programs for the ATARI Computer. You may want to copy DOS on to each side of your backup disk. This will eliminate the need for a separate DOS disk. Follow these simple steps to get the 32 BASIC Programs up and running on your ATARI computer: 1. With the label side up, insert your DOS diskette into drive 1, and close the door. 2. Turn on the monitor or TV. 3. Turn on the disk drive and then the computer, (the switch is on the right side of the computer). The following message appears: READY 4. To get a list of the programs on the diskette, type DOS and press the RETURN key. Your ATARI DOS SYSTEM menu will appear on the screen. Select Letter A and press the RETURN key. Insert the 32 Basic Programs diskette into Drive 1 with side A facing up, close the door, and press RETURN. A list of the programs on side A of the disk will appear on the screen. 5. Choose a program from the list, for example, GROAN. To run this program, type B and press RETURN. (This will clear the screen and allow you to enter further instructions.) Type RUN “D1 :GROAN” and press RETURN. Be certain to spell the name exactly as it appears in the directory listing. If you make a mistake, press RETURN (a message telling you there is an error will appear), and retype the instruction. If at any time you want to stop a program, press the break button (upper right corner). If you want to run another program, go back to Step 4. It is not necessary to get a directory each time you want to run a program. After the diskette is inserted and the computer is turned on, you can type RUN and the name of the program as shown above (remember to press RETURN). The directory is necessary only if you want to view the names of the programs. There is no warranty or representation by dilithium Press or the authors that these programs will enable the user to achieve any particular result. m ©dilithium Press 1983 8285 S.W. Nimbus Suite 151 Beaverton, OR 97005 (503) 646-2713 or toll free (800) 547-1842 This book is chock full of completely-tested ready to run programs designed specifically for your Atari computer. It includes games, applications, educational programs, graphics, and mathematics. Each chapter fully documents a program by providing a complete source listing of the program, its purpose, and how to use it. Plus the authors tell you how to adapt the programs by making simple modifications. 45078 01995 ISBN 0-flflDSt.-Dfl4-3 >>$1=1 .TS Software available in specially marked packages. Includes: 5-'A " diskette containing all of the programs, the loading instructions and a warranty card with our Forever Replacement guarantee. This diskette runs on an ATARI 1200, 800 or 400 computer with: • 16K Memory • 1 disk drive Color Monitor or Television dilithium Software offers technical support over the telephone. With our toll-free number and friendly staff, we can answer all your questions about dilithium Software. Outside of Oregon dial (800) 547-1842. in Oregon call 646-2713. ^1 si dilithium Press SOFTWARE