CHAPTER 3 1. Helpful Notes There are a few things that help in small ways with doing things in Chipmunk Basic. First, you should know that Chipmunk is most compatible with a PC form of BASIC used under MS-DOS called GW Basic, a product of Microsoft,Inc. But Chipmunk does more and you will find several interesting features as you get into it. A. Print Using If you wish your numbers to be printed in a certain way, consider the examples below in which the statement "print" that has been used so far is replaced by "print using" and a format indicator. Imagine in each case that the line number is 900, but it can be any number appropriate in your program. 900 print using "##.##";.86 0.86 900 print using "###.##";765.432 765.43 900 print using "##.## ";11.3,4.1,788.889,.423 11.30 4.10 78.89 0.42 Notice in the last case that 3 spaces occur before the last quote symbol in line 900. This puts 3 spaces between the numbers that are printed out. Of course, you will usually have a variable or variables to be printed, i.e., x in lieu of .86 in the first example above. But the examples show how the numbers will print regardless. Another example is 900 print using "####.##";9876.5 9876.50 so the number of # symbols corresponds to the quantity of printed digits. B. Pi and e The letters pi together as one word are reserved for the value of the ratio of a circle's circumference to its diameter to the accuracy inherent in Chipmunk. Similarly, exp(1) is the value of the natural number e (Note that exp(x) is e to the x power.). C. Stopping Chipmunk's Output If your output needs to be stoped while running and you want the program to just quit, say because you've realized something is wrong, hold down the Command key (the one with the apple on it) and type a period. This is just as if you were stopping your printer. By the way, you can't print directly from the default Chipmunk window, but rather you must use copy/paste to put whatever you want printed into something like SimpleText (or TeachText on older systems) and print the document that received the paste. Any word processor will do. You can, however, print from the graphics window. You can also create and edit documents in a word processor and the use copy/paste to paste them into the usual Chipmunk window, but hit the return key after doing so. D. Let Many forms of BASIC ask that you don't just write x=3, but rather let x=3 It doesn't hurt to do this if you want compatibility. This,of course, is just an example, and the similar idea holds whenever you define the value of any variable. E. Step sizes Actually, if you are stepping, say, from 1 to 7 step 1, the step 1 is not needed---it's the default choice. But you may want to use some case of nonintegral steps, as in this example line from a program: 220 for x=-2 to 5 step .5 In this case, the steps will be half-integral steps. You can also step downward as in this example line taken from another program: 432 for x=9 to 3 step -.1 which counts backward in steps of one tenth. F. Dividing by Zero Be careful not to have an expression in which one or more of the variables may have values that cause something to be divided by zero. The program won't work and you'll end up wondering why for a long spell of diagnostics time. G. The MANUAL If all else fails, read the manual and quick reference. Find some old books on GW Basic at your local used book stores. Quick Basic and Q-Basic are not the same as GW or Chipmunk. The newer BASICs are called "structured" and aren't like Chipmunk. Some useful information can be obtained from the book Basic Programming by John G. Kemeny and Thomas E. Kurtz, published by John Wiley & Sons, NY,NY (1967 1st ed., 1971,2nd ed.) H. Moving Up I recommend that the first move up from Chipmunk be to an older version of True Basic (1-800-TR-BASIC). With this you can use line numbers or not and can begin learning the structured basic that can be used to do amazing stuff on a Mac. The BASIC ARCHIVES can give you a lot of information if you are on the internet. Try http://www.fys.ruu.nl/~bergmann/basic.html and there are some usenet groups as well, but the BASIC in the news groups is modern stuff. 2. Using the Alphabet Besides using variables which have a numerical content, Chipmunk allows you to use variables which have alphabetical information assiciated with them. This is useful for all sorts of things like dates, names, fiddling with answers to questions, etc. For example, just as x stands for a number, x$ stands for a letter, word, phrase, sentence, or any text information. Just use a dollar sign after the symbol for the variable to make it mean text. These are called "string" variables. Here is an example program. The program begins with an optional line that will cause a Mac with the speech manager to say the words "Greek Test." 5 say "greek test" 10 print "This program tests you on Greek letters" 20 print 30 read direction$:rem Reads the string in line 800 40 print direction$ 50 print 60 read number:rem reads the no. of questions from line 850 70 n = number 80 dim response$(number):rem Dimensions list of prog.'s responses 85 response$(0) = "Can you do better" 90 response$(1) = "Actually, it is:" 95 response$(2) = response$(1) 100 for question = 1 to number 110 read question$,answer$:rem Reads strings in lines 900-950 120 wrongs = 0:rem Initializes the number of wrong answers 130 print question$; 140 input try$:rem User's input 150 if answer$ = try$ then goto 550:If correct, add points 200 print response$(wrongs); 250 wrongs = wrongs+1:rem accumulates no. of wrong answers 350 if int(wrongs) = int(1) then goto 140: rem Go on 400 print answer$:rem Otherwise, give answer 450 wrongs = 2 500 goto 130 550 n = n+1-wrongs 600 print 650 next question 700 fractionright = n/(2*number) 750 print "Your score is ";int(fractionright*100+0.5);"%" 800 data "Give the corresponding Greek letter of our letter" 850 data 8 900 data "a","alpha","b","beta","c","gamma","d","delta" 950 data "e","epsilon","k","kappa","m","mu","l","lambda" 999 end This program has many remarks that are on two-statement lines with each remark referring to the program statement on that line. The For-Next loop cycles through eight questions. A string-variable list is used for the programs responses, which are used to first ask the user for another try if a wrong answer is given. If the user is wrong twice, the correct answer is given and the user must type it to go on. In line 150 there is a conditional statement that directs the program to line 550 when a correct answer is given. Line 550 adds points to the test score for each correct answer and the program goes to line 650 to the next question. If the condition in line 150 is not met (wrong answer given), then the response list is called in the line 200 and the wrongs for this question are accumulated in line 250. If there is only one wrong, then the flow goes to line 140 again. If not, then the answer is printed and there are two wrong answers to the question and ther user must type the answer given to escape. Each t ime the points are added in line 550, the accumulated wrongs are subtracted, so n may be reduced for two wrongs, or unchanged for one wrong, or added to by one for zero wrongs. A single wrong answer folloed by a correct answer thus gives half credit. There are 2n maximum possible points and line 700 calculates the fraction of points gained out of 2n. Line 750 then prints the percent score. From: f44@cosmail1.ctd.ornl.gov (Thomas L. Ferrell)