1. Getting Started
Once you have downloaded Chipmunk Basiic in its folder, put
the folder on your hard disk, open it up, and double click
on the chipmunk application icon (the chipmunk). You
should see a new window with
Chipmunk BASIC v3.2.8b
>
shown near the top of the window. Your menu above the window
shows File, Edit, and Control.
The submenus under these are self explanatory if you know your
Mac, except that the Control menu is related to the common
commands for programs. The > sign above acts as a cursor.
Enlarge the window by placing your mouse arrow on the smaller of
the two rectangles in the lower right-hand corner of the window,
depressing the button, and holding it down while you drag the
window to a bigger size.
To begin, type the following after the cursor, being sure to
hit the return key after each line, including the last
line:
10 rem Multiply two numbers
20 x=2
30 y=8
40 z=x*y
50 print x,y,z
The line 10 is a remark only---it can be omitted. Anytime you
wish to make a remark about something in the program, one way is
to just type the line number followed by rem. Anything that comes
after rem is just a remark and is not a vital part of the
program. In lines 20 and 30 the symbols x and y are given values,
respectively. In line 40 they are multiplied to give z. Line 50
tells chipmunk to print out the value of x, of y, and of z.
If you make a mistake, for now just retype the line number
and line at the bottom. Hit the RETURN key. Go under the
Control menu and select List. The program will appear as it
should. Then go under the Control menu and select Run. The
results for x, y, and z will be printed out. You have now
written a real program. As mentioned above,the rem in line
10 stands for a reminder that is just for your own future
reference. It is a good idea to make such remarks
throughout your programs; when you come back to them after a
few weeks the rem statements are essential for efficient
understanding of the program. You may also wish to put the
date as a rem statement.
Next, put your mouse arrow just to the right of the asterisk
in line 40 and click. Then delete the asterisk
(multiplication sign) and type a slash .i.e. a divide sign:
/
Hit the RETURN key and the new line appears at the bottom. List
the program again. You will see the edited line placed in your
program in place of the old line. Save your program by going
under the File menu and pulling down to Save. Then the Save
window will open and you can fiddle with the choice of what
folder you want in which to save the program by holding down your
mouse button while the arrow of your mouse is placed in the
window bar that is the header of the window (It has a downward
pointing arrowhead on the right side of the bar.) Then you can
move the selection up one level to display all the folders in the
next level above the level that is inside the Chipmunk folder. I
like to place my Chipmunk folder inside a folder titled MATH,
which contains another folder I've created titled Chipmunk
Files. Or you can just save your program into the Chipmunk Basic
folder and move it later if you so desire. Name the program
FirstProgram.bas. Note that all saved programs must have the
suffix
.bas
(later you will make files of data with the suffix .st)
Similarly change line 10 to indicate in words that you are
dividing two numbers and hit the RETURN key and then list the
program. Then Run the new program (Select Run from the Control
menu.) and see if you get what you expect. Finally, change the
commas in line 50 to semicolons, list twice, and watch what the
results look like after the next Run.
Now go under the File menu and select Save. Name the
program Divide.bas and choose where you want it saved by
the usual means of working with such windows as briefly
described above. Quit the program by closing the Chipmunk
Basic window. If you want to go back, double click on the
Chipmunk Basic application icon, go under the File menu and
select Open, find your program, select it, and click on the
OPEN button in your active window. The program will load
and you can either list or run it as you wish. You can
select any visible portion of the program by the usual mouse
down and drag process. You can copy this to the clipboard
and paste it into Simple Text if you wish to do so.
Similarly, you can write your program using Simple Text,
copy it and paste it into Chipmunk Basic in Chipmunk's
window. Then list it and save it being sure the file name
ends in a period followed by bas.
You can use any word you like in place of x in the program,
so long as you don't exceed 31 characters and don't use
words already reserved by BASIC. See the manual for these
reserved words.
The line numbers in the Chipmunk programs are not used in modern
BASIC, but they are a convenient reference just like equation
numbers in math and science books.
Instead of directly assigning a value to x and to y in the
programs above, it is possible to use an alternative method.
This is illustrated below.
100 read x,y
200 z=x*y
300 print x,y,z
400 data 2,8
500 end
This method is sometimes more convenient. The output of the
program is not changed from the original version. The end
statement of line 500 is optional.
2. A More Powerful Program
Open Chipmunk and type in the following program (You can use
copy/paste to do this if you like, but be sure to hit the
Return key following the paste from the clipboard.)
10 rem Tutorial Example Loop
15 print "sam's age", "sam's size"
20 for sam=1 to 6 step .5
30 factr=.6
40 size = sam^factr+3
50 print sam,size
60 next sam
List and then Run the program. It gives Sam's age and his size in
some imaginary unit just for fun. The line 15 shows you how to
make Chipmunk print just words; just enclose them in quotes. The
formula in line 40 is not really associated with any real-world
thing, but is just an example that shows how you can form an
exponential, i.e., sam to the .6 power (3/5), which is the same
in this case as cubing the number and then taking its fifth
root. Pretty neat! The line 20 is a statement that begins a
loop. It starts by using the first value 1 and the program goes
through to the line 50 print statement where it prints the values
and then line 60 sends everthing back to the next value, which
after the first value will be 1.5. It then goes through the
program including the print statement before it loops back to the
next value of 2, etc. until it gets to 6, the last value.
Another way of making a loop in a program is illustrated by
the following case, which also demonstrates a goto
statement.
100 sam=1
150 factr=.6
200 if sam > 6 then goto 600
300 print sam, sam^factr+3
400 sam=sam+1
500 goto 150
600 end
In this case, the variable of line 40 of the previous program is
not explicitly mentioned, but the output is the same for these
two programs. Line 200 is a conditional statement that directs
the program to go to line 600 if sam is greater than 6. Line 300
replaces lines 50 and 40. Line 400 tells the program that the
value of sam is to be replaced by sam+1, and line 500 tells the
program to loop back with the new value for sam.
Finally, another note on giving a value to a variable is
illustrated below in a program that calculates n!, that is,
n factorial. For a given number n, the value of n! is
n*(n-1)*(n-2)*...1
i.e. the product of all numbers smaller than or equal to n all
the way down to unity. For example, 3! is 3*2*1=6.
100 input n
200 nfactorial=1
300 for i=1 to n step 1
400 nfactorial=nfactorial*i
500 next i
600 print n,nfactorial
700 print
800 goto 100
999 end
The line 700 print statement is just to create an extra vertical
space to make things nicer.
When you run this program it will just print a question mark. The
user then has to pick a number for the calculation and type that
number followed by hitting the return key.. The program will then
print the number entered and its factorial. If you want to prompt
the user you can replace line 100 with
100 input "What number? ",n
Notice the space following the question mark. This helps keep
things from being a bit crowded up.
3. LISTS
Suppose you own a sandwich shop and sell four kinds of
sandwiches that are listed as choices one through four on
the menu. In this example, the price of sandwich 1 is
denoted p(1), that of sandwich 2 is denoted p(2),etc. Given
the number sold of each type of sandwich in a given day,
what are the total cash receipts? The program below answers
your question.
50 dim p(4)
100 for i=1 to 4 step 1
150 read p(i)
200 next i
250 data 2.35,2.76,2.89,2.99
300 total=0
400 for i=1 to 4 step 1
500 read numbersold
600 total=total+numbersold*p(i)
700 next i
800 print "gross receipts= $";total
900 data 12,10,8,11
999 end
The line 50 sets the dimension size of the price list (4 prices).
In lines 100-200 the stepping variable i is first set equal to
one so that a data number is read by direction of line 150 and
assigned to p(1). As the for-next loop proceeds, each of p(2),
p(3), and p(4) are successively assigned values by the direction
given in line 150. The first four data encountered are used,i.e.,
the data of line 250. The actual calculation takes place in lines
300-700. An initial value is assigned to the total. This is
needed to start recognition of total as a variable that is
stepped in a formula (line 600). In line 500 the number of
sandwiches sold is directed to be read in sequential order from
the first 4 data encountered below---the fifth through eighth
data values (which are given in line 900). Thus, for i=2 the
numbersold is obtained from line 900 to be 10. The for-next loop
accumulates the total as i is stepped, the total being the
previous total in the loop plus the corresponding price times the
numbersold.
4. MAKING A FUNCTION
The hypoteneuse of a right triangle of base length x and
height y is the square root of the sum of x squared and y
squared:
hypoteneuse
length
=sqr(x^2 +y^2)
Suppose that you had a lot of right triangles of the same base,
but of different height, but that you also had a bunch of the
same height and differing base lengths. Here's how to deal with
this:
10 rem Hypoteneuse of right triangle
20 def fnc(a,b)=sqr(a^2+b^2)
30 y=3
40 for x=1 to 5 step .5
50 hyp1=fnc(x,y)
60 print "y=";y,"x=";x,"hypoteneuse=";hyp1
70 next x
80 x1=2
90 for y1=3 to 6 step .5
100 hyp2=fnc(x1,y1)
110 print "y=";y1,"x=";x1,"hypoteneuse=";hyp2
120 next y1
Notice that the function fnc(x,y) defined in line 20 is used in
line 50 and again in line 100. But it only needs to be typed
once. In line 50 it is used for the case of triangles of varying
base, while in line 100 it is used for triangles which have
varying height. In more advanced cases a function may be used
many times.
To use this program as it is given, first double click on the
Chipmunk Basic icon (the little chipmunk) to open Chipmunk
Basic's window. Notice the menu Chipmunk gives you at the top of
your screen. If you now type this program in the Chipmunk Basic
window or if you use copy/paste to copy it into the window (being
sure to hit the Return key after pasting), then you can go under
the Control menu at the top and choose List to see how the List
command works (if you haven't already been doing this). Then, go
under the Control menu again and choose Run. Your output should
look like this:
y=3 x=1 hypoteneuse=3.162278
y=3 x=1.5 hypoteneuse=3.354102
y=3 x=2 hypoteneuse=3.605551
y=3 x=2.5 hypoteneuse=3.905125
y=3 x=3 hypoteneuse=4.242641
y=3 x=3.5 hypoteneuse=4.609772
y=3 x=4 hypoteneuse=5
y=3 x=4.5 hypoteneuse=5.408327
y=3 x=5 hypoteneuse=5.830952
y=3 x=2 hypoteneuse=3.605551
y=3.5 x=2 hypoteneuse=4.031129
y=4 x=2 hypoteneuse=4.472136
y=4.5 x=2 hypoteneuse=4.924429
y=5 x=2 hypoteneuse=5.385165
y=5.5 x=2 hypoteneuse=5.85235
y=6 x=2 hypoteneuse=6.324555
so you see what the value of the length of the hypoteneuse is in
each case. But this is repetitive in printing hypoteneuse over
and over. Edit your program using the previously described method
of editing to make the following program (Notice the spaces after
the words in line 15, and that semicolons are used.):
10 rem Hypoteneuse of right triangle
15 print "base ";"height ";"hypoteneuse"
20 def fnc(a,b)=sqr(a^2+b^2)
30 y=3
40 for x=1 to 5 step .5
50 hyp1=fnc(x,y)
60 print y,x,hyp1
70 next x
80 x1=2
90 for y1=3 to 6 step .5
100 hyp2=fnc(x1,y1)
110 print y1,x1,hyp2
120 next y1
You can play with spaces, commas, and semicolons to make your
printout easier to read. But the important thing about this
exercise is that you have used the "define function" statement of
line 20 to keep from having to rewrite the formula for different
cases---after all, the Pythagorean theorem doesn't change---it's
just your uses of it that change!
Chipmunk Basic also has many functions built into it. For
example, you can specify a variable such as x (give it a
value or use it in a FOR-NEXT loop), and have one of your
program lines something like this:
height=sin(x)
Then Chipmunk knows to take the sine of the angle x, for x in
radians. A list of built-in functions is given in the associated
documentation.
From: f44@cosmail1.ctd.ornl.gov (Thomas L. Ferrell)