Академический Документы
Профессиональный Документы
Культура Документы
Manipulation of BASIC
1.1 Manipulation of BASIC system
1.1.1 Installation of Decimal BASIC
Download BASIC744En.zip (or later) from Decimal BASIC main page ,then
open it and copy the BASICw32 folder to a regular folder or the drive where
you want to install Decimal BASIC.
To start BASIC, open the folder where you put the system of decimal BASIC
with an Explorer and double-click BASIC.EXE or .
Windows may display a Security Warning. If so, select 'Run'.
If a vacuous help window as shown below appears when you click the help
icon or the help menu, exit BASIC and follow the procedure mentioned below.
Open the folder where Decimal BASIC has been installed, double-click
BASIC.chm .
Then the following dialog appears. Remove the check on "Always ask before
opening this file", and then click "Open".
Then close the help and re-start BASIC.
Decimal BASIC is provided with many sample programs. Try to read and
execute them. Click to read in a program from the file.
Click to execute.
Some programs take time for executing. To abort a program, click .
Recommended:
ABS.BASINT.BASMOD.BASSQR.BASTAN.BAS on FUNCTION folder,
PRINT.BASPLOTPOIN.BASPLOTLINE.BAS on STATEMEN folder.
Warning:
Programs included in the Textfile folder generate files. Do not execute before you
understand the meaning of the programs.
Next
Back
2 Calculations in BASIC
2.1 Variables and numeric expressions
2.1.1 Numeric expressions
2.1.2 Parenthesis
To change the priority of operations, you can use parentheses. Double, triple,
parentheses are permitted, but only round brackets '(' and ')' are used.
When a term starting with a negative sign is written in an expression, it must be
parenthesized.
10 PRINT ((2+3)*4)^2
20 PRINT 3*(-4)
30 END
The number at the left end of each line of the program is called the line number.
Line numbers can be omitted on Decimal BASIC. In this article, line numbers
are written for the purpose of maintaining consistency with the standard, but it
is advisable to omit line numbers when you prepare programs using Decimal
BASIC.
2.1.4 Variables
[Note]
The symbol of multiplication cannot be omitted on BASIC. Even if 'xy' is
written intending x*y, 'xy' is interpreted as one variable different from x or y.
When this program is executed, the dialog as shown in the figure will be
displayed. Type 2 numbers punctuated by a comma and then press the Enter
key.
If Line 10 is PRINT ABC, it means that the value of a numerical variable ABC
is displayed.
When calculation result is output, the format can be controlled in the PRINT
statement.
2.3 FORNEXT
2.3.1 FORNEXT block
Example 1.
10 FOR n=1 TO 10
20 PRINT n,n^2
30 NEXT n
40 END
On Example 1, line 20 is executed for n=1 first and then line 20 is executed for
n=2. Then line 20 is executed for n=3. The same thing is sequentially repeated,
and finally line 20 is executed for n=10.
Select the step execution and try to check how the value of variable changes
and execution sequence.
Example 2.
10 INPUT n
20 FOR k=1 TO n
30 PRINT k
40 NEXT k
50 PRINT "Last",k
60 END
If you execute this program and enter 10 for n, you will see that the value of k
becomes 11 when the PRINT statement of line 50 is executed. In the above
program, 1 is substituted for k first, and each time the NEXT statement is
executed, 1 is added for the value of k, and when it becomes larger than n, the
repetition finishes and the control proceeds to the next line of the NEXT
statement.
And now, what happens if 0 is entered for n? In this case, line 30 is not
executed and only line 50 is executed. The value of k in this example is 1. In
this case, since the value of k is larger than n from the beginning, the control
proceeds to line 50 without executing line 30 and the NEXT statement. This
characteristic is important and should be remembered.
Example 3
10 INPUT n
20 LET S=0
30 FOR k=1 TO n
40 LET S=S+k^2
50 NEXT k
60 PRINT S
70 END
The sum is calculated using variable S. First, 0 is substituted for S in line 20,
and then k2 is added to S for k=1,2,3,,n in lines 30 to 50.
The same method can be used for calculating the product of a sequence. The
number of permutations nPr (=n(n-1)(n-2)(n-r+1)) can be calculated as
follows:
Example 4.
10 INPUT n,r
20 LET p=1
30 FOR k=(n-r+1) TO n
40 LET p=p*k
50 NEXT k
60 PRINT p
70 END
[Note] The initial value of a variable is 0 in Decimal BASIC, but the standard
does not provide so. In order to make a compatible program, line 20 cannot be
omitted in Example 3.
Use the method of successively updating the values of a variable, and you can
calculate the sequence defined in a recurrence equation.
The following program calculates the n-th term of a sequence {a n} defined by
a1=5an+1=3an+2.
Example 5.
10 INPUT n
20 LET a=5
30 FOR k=2 TO n
40 LET a=3*a+2
50 NEXT k
60 PRINT a
70 END
With this program, the correct answer can be obtained even in the case of n=1.
This is because the value of k exceeds n when 2 is substituted for k and line 40
is not executed at all.
2.3.4 STEP
In a FORNEXT block, the numerical value added to the control variable each
time can be made other than 1.
In the following program, x2 is calculated while adding the value of x in units
of 0.1 from 0 to 1.
Example 6.
10 FOR x=0 TO 1 STEP 0.1
20 PRINT x,x^2
30 NEXT x
40 END
This syntax can be used for the purpose of changing the numerical value from a
large value to a small value.
Example 7.
10 FOR k=10 TO 1 STEP -1
20 PRINT k
30 NEXT k
40 END
Question 2.
In the following program, it seems that the input of an even number for n is
assumed, but an odd number can also be entered. How does it operate?
10 INPUT n
20 FOR k=0 TO n STEP 2
30 PRINT k
40 NEXT k
50 END
If this program is executed, the results are displayed as shown in the figure, and
the entire output can be seen by operating the scroll bar at the right end with the
mouse.
The DEF statement of line 10 is an instruction to define the function. The rule
of naming a function is the same as for a variable. But no identical name can be
used for a function name and a variable name. In line 10, f is used as the
function name. The function name is followed by parentheses and a variable is
written in the parentheses. Following the equal sign, the formula to calculate is
written using the variable written in the parentheses. The variable written in the
parentheses on the left side is different from the variable used in other part of
the program (that is, the storage location of the numerical value is different).
Since the formula of the cosine law can be solved for cos A as
the measure of the angle A can be found, given the lengths a,b,c of three sides
of a triangle ABC.
To find the corresponding A from the value of cos A, the built-in function
ACOS(x) of BASIC can be used.
ACOS(x) is a built-in function to find t to become cos t= x in the range of
0t180.
Example 11.
A program to find A by entering a,b,c.
10 OPTION ANGLE DEGREES
20 INPUT a,b,c
30 PRINT ACOS((b^2+c^2-a^2)/(2*b*c))
40 END
The following shows the execution result when a=7, b=3, and c=5 are entered.
? 7,3,5
120
IN BASIC, some built-in functions are prepared to stand for special constants.
PI is the approximate value of the ratio of the circumference of a circle to its
diameter ().
MAXNUM is the largest number that can be handled with BASIC.
Example 12.
10 PRINT PI, MAXNUM
20 END
RND is a special function. This function has no parameter, but each time a
calculation is made, a different value is returned. For example, try to execute
the following program.
Example 13
10 FOR k=1 TO 10
20 PRINT RND
30 NEXT k
40 END
The numerical value returned by the RND function is known as the random
number. The RND function generates random values without bias in the range
of 0 to less than 1. The above program returns the same result each time the
execution is made. If you want the random number of a different series each
time the execution is made, execute the RANDOMIZE statement before
executing the RND function as shown below.
To use the RND function in place of a die, do as follows. With this program,
integers of 1 to 6 are obtained by multiplying the value of RND by 6, adding 1,
and taking out the integer part.
10 RANDOMIZE
20 FOR n=1 TO 20
30 PRINT INT(RND*6+1)
40 NEXT n
50 END
2.6 Graphics
2.6.1 Graphs of functions
Example 14
A program that draws the graph of the function y=x3-3x+1 in the range of
-4x4 and -4y4.
10 DEF f(x)=x^3-3*x+1
20 SET WINDOW -4,4,-4,4
30 DRAW GRID
40 FOR x=-4 TO 4 STEP 0.1
50 PLOT LINES: x,f(x);
60 NEXT x
70 END
After the execution of the program above, the figure show below is obtained.
When the mouse cursor is moved on the window, the coordinates of the point
are displayed at the bottom.
The shape of the drawing pane is square. The number of pixels is determined
adequately from the size of the screen by BASIC system, but you can select
another pixel size in the menu.
The SET WINDOW statement at line 20 sets the coordinate system in the
drawing area such that the range of x coordinates is from 4 to 4, and the
range of y coordinates is from 4 to 4.
The DRAW statement at line 30 draws a grid. This feature is not specified in
the standard, but the same function can be realized by assembling the
instruction specified in the standard.
The PLOT LINES statement at line 50 is used to draw a graph of the function
by drawing line segments to connect points (x, x3-3x+1) successively.
Generally, when PLOT LINES: x,y; is written, the points indicated by this
statement and the PLOT LINES statement to be executed next shall be
connected with a line segment.
sets the coordinate system with the left end x1, right end x2, bottom end y1 and
top end y2.
When a geometric figure is drawn, x2x1=y2y1 is normally required, but in
the case of the function graph in which the vertical axis and horizontal axis
express amounts different in property, it is not required.
For example, the graph of a sine function with the degree measure is drawn as
follows:
Example 15
10 OPTION ANGLE DEGREES
20 DEF f(x)=sin(x)
30 SET WINDOW -360,360,-4,4
40 DRAW GRID(90,1)
50 FOR x=-360 TO 360
60 PLOT LINES: x,f(x);
70 NEXT x
80 END
OPTION ANGLE DEGREES at line 10 must be written before the lines where
SIN(x) functions are written. The grid(a,b) used in line 40 is a grid of
horizontal gaps a, vertical gaps b.
For example, if PLOT LINES: x1, y1, x2, y2 is executed when the PLOT LINES
statement has not been executed yet or the PLOT LINES executed just before
does not end with a semicolon, a line segment connecting two points (x1, y1)
and (x2, y2) is drawn.
This can also be written by dividing into 2 statements like below.
PLOT LINES: x1 , y1 ;
PLOT LINES: x2 , y2
For the PLOT LINES statement, A special form of PLOT LINES statement,
which has no point list, is available. When the PLOT LINES statement
executed just before ends with a semicolon, PLOT LINES statement with no
point list is used to cancel the effect.
For example, it is used when two function graphs are drawn successively as in
the following program.
Example 16.
100 DEF f(x)=x^2
110 DEF g(x)=x^3
120 SET WINDOW -4,4,-4,4
130 DRAW GRID
140 FOR x=-4 TO 4 STEP 0.1
150 PLOT LINES: x,f(x);
160 NEXT x
170 PLOT LINES
180 FOR x=-4 TO 4 STEP 0.1
190 PLOT LINES: x,g(x);
200 NEXT x
210 END
If the above program lacks line 170, the two points (4,f(40)) and (4, g(4))
shall be connected with a line segment.
2.6.4 Parametric Curve
Example 17.
10 OPTION ANGLE DEGREES
20 DEF f(t)=3*COS(t)
30 DEF g(t)=2*SIN(t)
40 SET WINDOW -4,4,-4,4
50 DRAW grid
60 FOR t=0 TO 360
70 PLOT LINES: f(t),g(t);
80 NEXT t
90 END
Example 18.
10 DEF f(t)=SIN(2*t)
20 SET WINDOW -1,1,-1,1
30 DRAW grid
40 FOR t=0 TO 2*PI STEP PI/360
50 PLOT LINES: f(t)*COS(t), f(t)*SIN(t);
60 NEXT t
70 END
The color of the point can be changed with SET POINT COLOR. The
instruction to dot a point is PLOT POINTS. It is written in the following form.
PLOT POINTS: x,y
On the other hand, from the condition that the area remains in deforming, the
width yk+1 is determined as follows.
From these two relations, the formula only concerning xk and xk+1 can be
obtained as follows.
The following program finds the first 8 terms of this sequence xk.
Example 20.
10 INPUT a
20 LET x=a
30 FOR n=1 TO 8
40 LET x=(x+a/x)/2
50 PRINT n,x
60 NEXT n
70 END
All numerical values are expressed as decimals in Decimal BASIC. And for
such an operation that a correct value can be expressed in a 16-digit decimal, a
correct result can be obtained (not guaranteed).
For example, the calculation result of SQR(36) will accurately become 6. If the
angle measure is degree, the calculation result of SIN(30) will become 0.5. If
the true value of the calculation result cannot be expressed in 16-digit decimals,
however, the result will be an approximate value. For example, the calculation
result of 1/3 contains an error.
A numerical variable has the precision of 15 digits in Decimal BASIC. If you
substitute a numerical value that has over 15 significant digits for a variable, it
is rounded so that it has just 15 significant digits.
On the other hand, calculation results of numerical expressions have precision
of more than 16 digits. For example, the calculation result of 1/3 becomes
0.333333333333333333333333333, which has 27 significant digits.
Ordinarily, calculation results are displayed rounded to 15 digits, however, if
More Places Displayed is checked with Option menu Precision, all digits
shall be displayed in the decimal mode.
The derivative f' (a) of a function y=f(x) at x=a is defined by the limit of
Example 21.
10 DEF f(x)=SQR(x)
20 FOR i=1 TO 15
30 LET h=10^(-i)
40 PRINT h, (f(2+h)-f(2))/h
50 NEXT i
60 END
Execution Result
.1 .349241122458488
.01 .35311255026876
.001 .3535092074646
.0001 .353548971286
.00001 .35355294866
.000001 .3535533464
.0000001 .353553386
.00000001 .3535534
.000000001 .3535534
.0000000001 .353554
.00000000001 .35356
.000000000001 .3536
.0000000000001 .354
.00000000000001 .36
.000000000000001 .4
2.7.4 Pi ()
ak=
.
From the half-angle identity,
,
and so
cos90cos45cos 22.5
can be calculated successively.
So, we choose
the relation
sin 90sin 45 .
The following form is recommended for a format. For the details of the format
characters, see the standard.
####### for a positive integer.
Write #'s of the number of places. (Right-aligned on
display)
-------% for an integer (positive or negative).
Write minus signs of the number of places, and % at the
end.
#######.#### for a positive decimal.
#'s are written for integral part and decimal part.
-------%.#### for a decimal (positive or negative)
The decimal part is rounded off to the places specified by the format.
When the format is made, care should be taken so that the number of digits of
the integral part will not be short. Especially for the negative number, the place
for outputting the minus sign must be secured.
Example 23
10 FOR i=0 TO 17
20 LET n=10^i
30 PRINT USING "################### #.##############":n,(1+1/n)^n
40 NEXT i
50 END
Output result
1 2.00000000000000
10 2.59374246010000
100 2.70481382942153
1000 2.71692393223589
10000 2.71814592682522
100000 2.71826823717449
1000000 2.71828046931938
10000000 2.71828169254497
100000000 2.71828181486764
1000000000 2.71828182709990
10000000000 2.71828182832313
100000000000 2.71828182844545
1000000000000 2.71828182845769
10000000000000 2.71828182845891
100000000000000 2.71828182845903
1000000000000000 2.71828182845904
10000000000000000 2.71828182845905
100000000000000000 2.71828182845905
Example 24
10 FOR x=3.14159265 TO 3.14159266 STEP 0.000000001
20 PRINT USING "%.######### ---%.###############":x,SIN(x)
30 NEXT x
40 END
Output result
3.141592650 0.000000003589793
3.141592651 0.000000002589793
3.141592652 0.000000001589793
3.141592653 0.000000000589793
3.141592654 -0.000000000410207
3.141592655 -0.000000001410207
3.141592656 -0.000000002410207
3.141592657 -0.000000003410207
3.141592658 -0.000000004410207
3.141592659 -0.000000005410207
3.141592660 -0.000000006410207
Next
Back
3 Description of Algorithm
3.1 If ~ END IF blocks and IF statements
3.1.1 IF ~ ELSE ~ END IF
Example 25.
A program that displays the solution of the quadratic equation ax2+bx+c=0
when coefficients a, b, c are entered.
10 INPUT a,b,c
20 LET D=b^2-4*a*c
30 IF D>=0 THEN
40 PRINT (-b-SQR(D))/(2*a), (-b+SQR(D))/(2*a)
50 ELSE
60 PRINT "no solution"
70 END IF
80 END
Example 26.
100 INPUT a,b,c
110 LET D=b^2-4*a*c
120 IF D>0 THEN
130 PRINT (-b-SQR(D))/(2*a), (-b+SQR(D))/(2*a)
140 ELSEIF D=0 THEN
150 PRINT -b/(2*a)
160 ELSE
170 PRINT "no solution"
180 END IF
190 END
3.1.3 IF statement
For example, if you do not need to display no solution when D<0, the
following will do.
10 INPUT a,b,c
20 LET D=b^2-4*a*c
30 IF D>=0 THEN PRINT (-b-SQR(D))/(2*a), (-b+SQR(D))/(2*a)
40 END
Inequalities ab , ab and ab are written like a<=b, a>=b and a<>b, respectively.
Complicated conditions can be described using AND, OR, NOT and
parentheses.
For example, conditions p and q, p or q are written as p AND q ,
p OR q ,respectively. NOT is written just before the subject to be negated.
Among AND, OR and NOT, NOT precedes AND and OR. Between AND and
OR, AND precedes OR. If you want to change the priority, you can use
parentheses.
Although a<x and x<b is written as a<x<b in mathematics, this style is not
used in BASIC.
Example 27.
A Program that finds the sum of three-digit positive integers that can be divided
by 3 or 8.
10 LET t=0
20 FOR n=100 TO 999
30 IF MOD(n,3)=0 OR MOD(n,8)=0 THEN
40 LET t=t+n
50 END IF
60 NEXT n
70 PRINT t
80 END
Three positive integers x, y, and z which satisfy the equation x2+y2=z2 are
called Pythagorean numbers.
To systematically seek Pythagorean numbers, we make such a program as
outputs x, y and z= SQR(x^2+y^2) when z is an integer changing the values of x
and y successively.
Whether z is an integer or not can be known by checking whether INT(z) agrees
with z.
Example 28.
10 FOR x=1 TO 100
20 FOR y=x TO 100
30 LET z=SQR(x^2+y^2)
40 IF INT(z)=z THEN PRINT x,y,z
50 NEXT y
60 NEXT x
70 END
[Note]
The above program will operate even with such old type BASIC as N88-
BASIC. But correct answers may not be obtained.
In Full BASIC, correct answers can be obtained by the above program, since
the result of a power or square root operation must be correct if it can be
expressed within the significant digits.
3.2 DO ~ LOOP
3.2.1 DO ~ LOOP
The statements written between the DO line and the LOOP line are repeatedly
executed. In the above program, line 30 and line 40 are repeatedly executed.
[Supplement] When the above program is executed, click or select Break
from the Run menu to abort the execution.
When line 20 in Example 29 is modified to the above, the repetition stops when
the value of A becomes 100.
LOOP
In this loop structure, the condition is checked before executing the process to
be repeated, so if the condition fails at the beginning, no repetition is executed.
In Example 30, for example, if line 10 is changed to 10 LET A=100, line 30 and
line 40 are never executed.
Example 31
10 LET A=0
20 DO UNTIL A>=100
30 LET A=A+1
40 PRINT A
50 LOOP
60 END
A variable f represents the divisor (maybe a factor). In line 120, the first divisor
2 is set.
Through the repetition from lines 140 to 170, dividing by f is done as much as
possible. When dividing can no longer be done, 1 is added to f (line 180).
This will be repeated in the DO ~ LOOP of line 130 through line 190, but the
repetition must be stopped in some time. The condition is when all the factors
are taken from n, and it is n=1. This is the condition written in line 130.
3.2.5 EXIT DO
Example 33
10 PRINT "23=?"
20 DO
30 INPUT n
40 IF n=2*3 THEN EXIT DO
50 PRINT "Incorrect. Type Again."
60 LOOP
70 PRINT "Right"
80 END
211 1
2 5 1
2 2 0
2 1 1
0
Example 34.
10 INPUT n
20 DO
30 LET q=INT(n/2)
40 LET r=MOD(n,2)
50 PRINT r
60 IF q=0 THEN EXIT DO
70 LET n=q
80 LOOP
90 END
The greatest common divisor of two positive integers can be found when this
property is repeatedly used as follows. This is called the Euclidean algorithm.
In this operation, the value of b decreases each time it is repeated, and it will
surely end after some repetitions.
Example 35.
A program that finds the greatest common divisor using Euclidean algorithm.
10 INPUT a,b
20 DO
30 LET r=MOD(a,b)
40 IF r=0 THEN EXIT DO
50 LET a=b
60 LET b=r
70 LOOP
80 PRINT b
90 END
3.3 Arrays
3.3.1 DIM statement
In a DIM statement, the name of an array and the upper bound of indices are
written in the form as shown above. The rule for naming an array is the same as
for an ordinary variable. Note that only constants like 10, 20 can be written for
the upper bound of indices.
[Note] Handling of arrays is different from that in minimal BASIC, the former
standard for BASIC. Especially, it should be noted that the array index starts
with 1.
Example 37.
100 DIM m(0 TO 10)
110 MAT m=ZER
120 DO
130 INPUT n
140 IF n<0 THEN EXIT DO
150 LET i=INT(n/10)
160 LET m(i)=m(i)+1
170 LOOP
180 FOR i=0 TO 10
190 PRINT i*10;" ~ ";i*10+9,m(i)
200 NEXT i
210 END
Example 38.
A program that repeats 10000 times the simulation of finding the sum of spots
on 10 dice, totalizes the results and displays them in a histogram.
100 DIM A(10 TO 60)
110 MAT A=ZER
120 FOR k=1 TO 10000
130 LET t=0
140 FOR n=1 TO 10
150 LET t=t+INT(RND*6+1)
160 NEXT n
170 LET A(t)=A(t)+1
180 NEXT k
190 SET WINDOW 9,61,0,1000
200 FOR t=10 TO 60
210 PLOT LINES: t-0.5,A(t); t+0.5,A(t);
220 NEXT t
230 END
Since the sum of the spots is in the range of 10 to 60, an array A with indices in
the same range is prepared.
3.3.3 Sieve of Eratosthenes
Example 39.
100 DIM s(2 TO 1000)
110 MAT s=ZER
120 FOR n=2 TO 1000
130 IF s(n)=0 THEN
140 PRINT n
150 FOR k=n^2 TO 1000 STEP n
160 LET s(k)=1
170 NEXT k
180 END IF
190 NEXT n
200 END
If you desire to make the lower bound other than 0 or 1, or different for each
array, use such a DIM statement as shown below.
DIM A(-4 TO 5),B(0 TO 2, 1 TO 3)
Let's make Pascal's triangle or the table of number of combinations nCr utilizing
a two-dimensional array.
We use the following recurrence to calculate nCr.
When r = 0 or r = n, nCr = 1
When 0<r<n, nCr = n-1Cr-1 +n-1Cr
Example
100 DIM C(1 TO 10, 0 TO 10)
110 MAT C=ZER
120 FOR n=1 TO 10
130 FOR r=0 TO n
140 IF r=0 OR r=n THEN
150 LET C(n,r)=1
160 ELSE
170 LET C(n,r)=C(n-1,r-1)+C(n-1,r)
180 END IF
190 NEXT r
200 NEXT n
210 MAT PRINT C;
220 END
With the MAT PRINT statement used in line 210, the values of all indexed
variables belonging to the array are collectively output. If a semicolon is
written following the array name as in line 210, the numerical values are
closely output.
Full BASIC prepares devices convenient for processing arrays. For example,
MAT INPUT statements and MAT PRINT statements make array input and
output easy.
Example 41.
10 DIM A(5)
20 MAT INPUT A
30 MAT PRINT A
40 END
A MAT READ statement substitutes numerical values for arrays from the
DATA statements.
In the DATA statement, necessary number of numerical values should be
written by delimiting by commas.
This is convenient when you want to enter the same data each time in programs
in test stages.
Example 43.
100 DIM a(10)
110 MAT INPUT a
120 FOR i=1 TO 9
130 LET k=i ! k is the index of the smallest
value.
140 FOR j=i+1 TO 10
150 IF a(j)<a(k) THEN LET k=j
160 NEXT j
170 LET t=a(i) ! using t as a temporary variable,
180 LET a(i)=a(k) ! exchange the values in a(k) and
a(i).
190 LET a(k)=t
200 NEXT i
210 MAT PRINT a;
220 END
Lines 130~160 are the process of acquiring the index of the smallest value in
a(i)~a(10) for variable k.
In lines 170~190, a temporary variable t is used to exchange the values of a(i)
and a(k).
The portion of a line subsequent to ! is a comment. This does not affect the
execution of a program. Comments are used for explaining the meaning of the
program.
To see how this program is operating, you can move the MAT PRINT statement
in line 210 to the suitable position.
Example 44.
100 DIM a(10)
110 MAT INPUT a
120 FOR i=2 TO 10
130 LET b=a(i) ! a(i) is saved to b.
140 LET j=i ! j will means the position for
inserting b.
150 DO WHILE j>1 AND a(j-1)>b
160 LET a(j)=a(j-1) ! Shift the data backwards.
170 LET j=j-1
180 LOOP
190 LET a(j)=b
200 NEXT i
210 MAT PRINT a;
220 END
In lines 130~180, the position for inserting the value of a(i) is sought,
accompanied by the process of shifting the values in the variables backwards.
In the AND operation in line 150, the sequence of j>1 and a(j-1)>b is
important.
When j=1 , the condition j>1fails so that a(j-1)>b will not be tested.
In line 150, j=1 may occur, and so if a(j-1)>b were tested before j>1, the
index would be out of range, resulting in an error.
The zero division error can be prevented by checking the divisor in advance of
the division, but it is hardly possible to predict the overflow error. In such a
case, action must be taken only after the error has occurred.
USE
END WHEN
If the PLOT LINES at line 180 does not exist, the line segment connecting two
points (89, TAN(89) and (91,TAN(91)), for example, shall be drawn.
Next
Back
4 Program Segmentation
4.1 External Function Definition
4.1.1 Necessity of program segmentation
However, it is not easy to make such a program in many case. For example, if
Example 28 uses a, b, c instead of x, y, z as variable names, the combined
programs will not operate normally.
BASIC provides us with many built-in functions, but they are not sufficient.
DEF statements enables us to define a function, but only a function that can be
indicated by an expression. BASIC also provides us with facilities with which
we can define such a function that can only be found through more complicated
processing.
The program shown in the preceding paragraph can be more easily written by
denoting the greatest common divisor of 2 numbers a and b as GCD(a,b). To do
this, we use an external function definition, so called.
In BASIC, the portion to the END line is called a main program. And the
END line is followed by what are called external procedures. An external
function definition is one type of a external procedure.
In Example 47, the portion from line 200 to line 280 is the external function
definition to define the GCD function. An external function definition starts
with an EXTERNAL FUNCTION line and ends with an END FUNCTION
line. The EXTERNAL FUNCTION line holds the name of the function and the
parameter list, which is parenthesized. If there are two or more parameters, they
are delimited with commas.
Example 47
100 DECLARE EXTERNAL FUNCTION GCD
110 FOR x=1 TO 100
120 FOR y=x TO 100
130 IF GCD(x,y)=1 THEN
140 LET z=SQR(x^2+y^2)
150 IF INT(z)=z THEN PRINT x,y,z
160 END IF
170 NEXT y
180 NEXT x
190 END
200 EXTERNAL FUNCTION GCD(a,b)
210 DO
220 LET r=MOD(a,b)
230 IF r=0 THEN EXIT DO
240 LET a=b
250 LET b=r
260 LOOP
270 LET GCD=b
280 END FUNCTION
The main program and external procedures are called program units.
In a program unit that uses an external function, a declaration statement that
holds the name of the external function is written. In the above program, such a
declaration statement as line 100 is written to use GCD as the external function
in the main program. Writing such a declaration statement makes it possible to
do correct operation even if there is a built-in function whose name is the same
as the name of the external function. The declaration statement is written in a
line above where the function is actually used.
As for the syntax, just the needed function name is written succeeding
DECLARE EXTERNAL FUNCTION.
[Note]
With regard to the present version of Decimal BASIC, we can omit the
declaration of the external function that does not agree in name with any
built-in function, but declaration statements should be always written
because the built-in function names may increase in future. And we are
considering to change the syntax so as not to allow the omission of the
external function declaration in future version.
In the above program, the GCD function is used in line 130. This is called
an invocation of the function. Parameters, such as x and y in the above
program, written in an invocation are called arguments.
When the GCD function is invoked, the values of the arguments are substituted
for the variables a, b, and then the lines of the function definition are executed.
The value of the function is decided by a LET statement in the form of
substituting for the function name as in line 270.
Utilizing the external function definition makes it easy to find the greatest
common divisor of 3 numbers. It is done as follows:
100 DECLARE EXTERNAL FUNCTION GCD
110 INPUT a,b,c
120 PRINT GCD(GCD(a,b),c)
190 END
200 EXTERNAL FUNCTION GCD(a,b)
Same as Example 47
280 END FUNCTION
The Euclidean algorithm, which finds the greatest common divisor, is also a
typical example of recursive algorithm. This algorithm can be written in
BASIC as follows.
Example 49
10 DECLARE EXTERNAL FUNCTION GCD
20 INPUT a,b
30 PRINT GCD(a,b)
40 END
100 EXTERNAL FUNCTION GCD(a,b)
110 LET r=MOD(a,b)
120 IF r=0 THEN LET GCD=b ELSE LET GCD=GCD(b,r)
130 END FUNCTION
Example 50
10 DECLARE EXTERNAL PICTURE circle
20 OPTION ANGLE DEGREES
30 SET WINDOW -8,8,-8,8
40 DRAW circle
50 DRAW circle WITH SCALE(2)
60 DRAW circle WITH SCALE(3,2)
70 DRAW circle WITH SCALE(3,2)*SHIFT(3,4)
80 DRAW circle WITH SCALE(5,3)*ROTATE(60)
90 END
100 EXTERNAL PICTURE circle
110 OPTION ANGLE DEGREES
120 FOR t=0 TO 360
130 PLOT LINES:COS(t),SIN(t);
140 NEXT t
150 END PICTURE
The portion from line 100 to line 150 is called an external picture definition. In
the first line of the external picture definition, the picture name is written
following EXTERNAL PICTURE. In the above program, circle is the
picture name. An parameter list part can be written as in the case of external
function definitions, if necessary.
The picture circle draws a circle of radius 1 with center the origin. to be exact, a
regular 360-gon is drawn, but it looks like a circle on the display of the
computer.
Each program unit is independent as to OPTION statements. Even if OPTION
ANGLE DEGREES is written in the main program, the unit of angle measure
is not changed in an external picture definition.
A DRAW statement is used for executing a picture. Such transformation as
rotating or scaling with center the origin, or translation can be specified in a
DRAW statement.
SCALE(a,b) stands for an expansion of a-fold in the x-axis direction and b-
fold in the y-axis direction with the center at the origin, where a and b can be
negative numbers. In the case of a=b, it can be written as SCALE(a).
SHIFT(a,b) stands for a translation that moves the origin to the point (x, y).
ROTATE() stands for a rotation of angle with the center at the origin.
Transformations can be composed with *. The composing transformations are
executed from the left. For example, SCALE(3,2)*SHIFT(3,4) is scaling first
and then parallel shifting.
Example 51.
100 DECLARE EXTERNAL SUB circle
120 SET WINDOW -4,4,-4,4
130 CALL circle(1,-1,2)
140 END
200 EXTERNAL SUB circle(a,b,r)
210 OPTION ANGLE DEGREES
220 FOR t=0 TO 360
230 PLOT LINES: a+r*COS(t),b+r*SIN(t);
240 NEXT t
250 END SUB
The picture and subprogram have some characteristics different from the
function definition.
One of those is variable parameter (pass by reference).
Example 52
10 DECLARE EXTERNAL SUB double
20 LET a=3
30 CALL double(a)
40 PRINT a
50 END
100 EXTERNAL SUB double(x)
110 LET x=x*2
120 END SUB
In Example 52, the subprogram double(x) doubles the value of the variable
written as an argument.
If a variable is written as an argument of a CALL statement, the invoked
subprogram uses the actual area of the variable on the memory as the memory
area for the parameter to which the variable has been assigned. Therefore, if the
value of the parameter is changed during the execution of the subprogram, the
value of the variable also changes.
If the argument is not a variable, for example, a constant, an arithmetic
operation or a parenthesize variable, the parameter to which the argument is
assigned is allocated to a new memory area and the value of the argument is
substituted for it.
For example, if line 30 is changed to CALL double (1*a), just the numerical
value of the calculation result of 1*a is passed to the subprogram and the value
of the variable a does not changes.
4.2.4 Extended Euclidean algorithm
Example 53.
10 DECLARE EXTERNAL SUB solve
20 INPUT a,b,c
30 WHEN EXCEPTION IN
40 CALL solve(a,x,b,y,c)
50 PRINT x,y
60 USE
70 PRINT "no solution"
80 END WHEN
90 END
100 EXTERNAL SUB solve(a,x,b,y,c)
110 IF b=0 THEN
120 IF MOD(c,a)=0 THEN
130 LET x=c/a
140 LET y=0
150 ELSE
160 CAUSE EXCEPTION 999
170 END IF
180 ELSE
190 LET q=INT(a/b)
200 LET r=MOD(a,b)
210 CALL solve(b,u,r,v,c)
220 LET x=v
230 LET y=u-q*v
240 END IF
250 END SUB
A CAUSE EXCEPTION statement (in line 160) causes an exception of the
specified number. The exception numbers of 1 to 999 are reserved for users.
If the statement that has caused the exception is not enclosed by a WHEN
EXCEPTION USE, the exception is propagated to the invoking statement.
Next
Back
A character string that has no character is called a null string. a null string is
expressed by "".
In the above program, s$ is made a null string first, and each time the input
value is divided by 2, the remainder is converted to a character string and then
concatenated to the left of s$.
When all data cannot be written in one line, they can be written in multiple
DATA statements.
In such a case, no comma is written at the end of a line. (If so, a syntax error
shall occur.)
For example, the DATA statement in the above program can be split into three
statements as shown below.
10 DATA 1,2,3,4
12 DATA 5,6,7,8
14 DATA 9,10
In the above example, 5 sets of data, 2 pieces per set, are prepared, and so the
FOR~NEXT loop is repeated 5 times to read the data, but it is troublesome if
the data quantity is indefinite or unknown.
Full BASIC is provided with an instruction to read an indefinite quantity of
data.
If IF MISSING THEN EXIT DO: is written between READ and the first
variable name in a READ statement in a DO~LOOP, the repetition of the
DO~LOOP shall be finished when data is no longer available.
The following example is a program to calculate the average of data written in
a DATA statement.
Example 58.
100 DATA 34,71,85,80,58,49,52,13
110 LET n=0
120 LET t=0
130 DO
140 READ IF MISSING THEN EXIT DO: x
150 LET t=t+x
160 LET n=n+1
170 LOOP
180 PRINT t/n
190 END
A RESTORE statement makes READ statements read data from the first again.
The following program calculates the difference from the mean, or the
deviation, for each data. Since the mean must be calculated before the
calculation of the deviation, the data should be read twice.
Example 59.
100 DATA 34,71,85,80,58,49,52,13
110 LET n=0
120 LET t=0
130 DO
140 READ IF MISSING THEN EXIT DO: x
150 LET t=t+x
160 LET n=n+1
170 LOOP
180 LET m=t/n ! the mean
190 RESTORE
200 FOR i=1 TO n
210 READ x
220 PRINT x-m ! the deviation
230 NEXT i
240 END
5.3 Files
5.3.1 OPEN statement and CLOSE statement
It is posible to input data from a text file, or output results to a text file. A text
file is the simplest type a file, which can be read and written with NotePad of
Windows or other text editors.
When a file is used, it is assigned to a virtual existence called a channel.
Channels are identified by positive integers. Commonly, small numbers such as
1 and 2 are used sequentially.
The instruction to assign a file to a channel is an OPEN statement. An OPEN
statement is written in the following form.
OPEN #Channnel_Number NAME File_Name
In the above, Channnel_Number is a numeric expression and File_Name is a
string expression.
When the use of a file is completed, a CLOSE statement is executed. A CLOSE
statement is written in the following form.
CLOSE #Channel_Number
If the same content as the input reply to an INPUT statement is written in a text
file, data can be continuously input from the file. The input reply corresponding
to one execution of an INPUT statement corresponds to one line in a text file,
but we can write an input reply in multiple lines, writing a comma at the end of
a line.
When input is made from a file, an INPUT statement is written in the following
form. INPUT #Channel_Number: variable, variable, , variable
For example, if you want to input data from a file named DATA1.TXT on the
drive A, what you should make is such a program as shown below. Example 60
10 OPEN #1: NAME "A:\DATA1.TXT"
20 FOR i=1 TO 4
30 INPUT #1:s$,n
40 PRINT s$,n
50 NEXT i
60 CLOSE #1
70 END
They can also be written in 8 lines by line-feeding just after the comma.
If an INPUT statement with IF MISSING THEN EXIT DO is used in a
DO~LOOP, it is possible to prepare a program that copes with an indefinite
number of input.
In this case, IF MISSING is written to follow the channel number with a
comma.
61
10 OPEN #1: NAME "A:DATA1.TXT"
20 DO
30 INPUT #1, IF MISSING THEN EXIT DO: s$,n
40 PRINT s$,n
50 LOOP
60 CLOSE #1
70 END
Outputting to a text file is done in the same form as to the screen. That is, it is
the same form as the contents of a file in which the contents of the text output
window are stored. Since blanks are output between items, it is not suitable to
re-reading with BASIC, but it can be read into a spreadsheet such as EXCEL if
spaces is specified for the delimiter.
In case of appending, change line 20 to
20 SET #2: POINTER END
If data with more than one item is output as a text file, it cannot be re-read with
BASIC, but if internal record type is used, the data output with BASIC can be
re-read with BASIC. In order to use internal record type, RECTYPE
INTERNAL is added to the end of an OPEN statement, a WRITE statement is
used for output in place of a PRINT statement, and a READ statement is used
for input in place of an INPUT statement.
Since the actual form of internal record type of Decimal BASIC is a comma-
separated text (CSV), it can be used for data exchange with a spreadsheet
program, etc.
Example63.
Outputting in internal record type
10 OPEN #1:NAME "A:DATA2.CSV" ,RECTYPE INTERNAL
20 ERASE #1
30 FOR x=1 TO 10
40 WRITE #1: x,SQR(x)
50 NEXT x
60 CLOSE #1
70 END
Example 64.
Reading a file of internal record type
10 OPEN #1: NAME "A:DATA2.CSV" ,RECTYPE INTERNAL
20 DO
30 READ #1, IF MISSING THEN EXIT DO: x,y
40 PRINT x,y
50 LOOP
60 CLOSE #1
70 END
READ statements and WRITE statements can be used in a file of not internal
record type, but in such a case, the effect is same as for INPUT statements and
PRINT statements.
Next
Back
Appendix
The following keywords are reserved words, which cannot be used for
identifires (variable names, function names, or routine names).
NOTELSEPRINTREMPIRNDMAXNUMTIMEDATEEXTYPEEXLINEZERC
ONIDNTRANSFORM
1. Control
EXIT FOR escapes from the FOR ~ NEXT. (Similar to EXIT DO)
GOTO line_number The control is moved to the line with a specified line
number.
2. Graphics
wait for clicking with the mouse, and substitutes the
GET POINT: x, y
coordinates of that point for the variable x, y.
PLOT AREA: x2, y2 ; fills the inside of the area surrounded by a polygonal
x2, y2; ... ; xn, yn line connecting (x1, y1), (x2, y2), , (xn, yn), (x1, y1).
SET AREA
Area color is made n. (Similar to SET LINE COLOR)
COLOR n
General
Exponent/Logari
thm
Trigonometric
SIN(x) sine
COS(x) cosine
TAN(x) tangent
CSC(x) cosecant
SEC(x) secant
COT(x) cotangent
Hyperbolic
Random number
Time
Others
PI Approximate value of