Contents / Part 1 / Previous chapter / Next chapter / Index


6 Sample programs

Most of the rest of this book is concerned with introducing the various parts of the BBC BASIC language which the computer understands and other features of the machine. But first, here are a few complete programs which you can try to type in yourself. They must be typed in as accurately as possible and can then be run. If a program fails to run properly, then almost certainly you have typed a line in incorrectly - for instance, you may have typed ; when you should have typed : or typed O instead of 0.

Most of the sample programs are too big to fit on the screen in one go. If you LIST a program you have typed in, for example to check that you have made no mistakes, you may find that the lines you want to look at disappear off the top of the siren. To prevent this you can specify the range of lines you want to be listed. For example

LIST 100,200

will only list those lines numbered between 100 and 200.

Alternatively you can enter "page mode" by pressing, CTRL N (hold down CTRL and press N). In this mode the listing will stop after every "page" and will continue only when you press the SHIFT key. "Paged mode" is switched off by pressing CTRL O and you should always remember to do this after you have listed the program.

Typing in programs will help you to get a feel for the keyboard and, if you save them on cassette after you have satisfied yourself that they do run properly, will enable you to start to build up a library of them.

Learning to use the computer is a little like learning to drive a car - when you first start you find that there are an enormous number of things to think about at once. Many of the things you come across from now on will be bewildering at first, But as you get further into the book and as you gain experience in using BASIC, the various parts of the jig-saw puzzle should begin to fall into place. So don't worry if, for instance, some of the comments about the following programs are difficult to understand at first.

Note: In the program listings which follow, extra spaces have been inserted between the line numbers (10, 20, etc) and what follows on each line. This is to improve the readability of the programs. However, although it will do no harm, there is no reason to type in any spaces after the line number. For example in the first program, called POLYGON, when entering line 250, all you need type is

250MOVE 0,0

POLYGON

This program draws polygons (many sided shapes) in random colours.

Lines 120 to 180 move to a random place on the screen which will be the centre (origin) of the next shape. Lines 210 to 290 calculate the X and Y co-ordinates of each "corner" of the polygon and store the values in two 'arrays' for future use. In addition the shape is filled with black triangles (lines 260 and 200) thus making it appear that the new polygon is in front of older ones. Lines 310 to 370 draw all the lines that make up the polygon.

Lines 50 to 70 set the actual colours of logical colours 1, 2 and 3 to red, blue and yellow. You can change these if you wish to use other colours.

10 REM POLYGON

20 REM JOHN A COLL

30 REM VERSION 1 / 16 NOV 81

40 MODE5

50 VDU 19,1,1,0,0,0

60 VDU 19,2,4,0,0 0

70 VDU 19,3,3,0,0,0

80 DIM X(10)

90 DIM Y<10)

100

110 FOR C=l TO 2500

120 xorigin=RND(1200)

130 yorigin=RND(1000)

140 VDU29,xorigin;yorigin;

150 radius=RND(300)+50

160 sides=RND(8)+2

170 MOVE radius,0

180 MOVE 10,10

190

200 GCOL 0,0

210 FOR SIDE=1 TO sides

220 angle=(SIDE-1)*2*PI/sides

230 X(SIDE)=radius*COS(angle)

240 Y(SIDE)=radius*SIN(angle)

250 MOVE0,0

260 PLOT 85,X(SIDE), Y(SIDE)

270 NEXT SIDE

280 MOVE0,0

290 PLOT 85,radius,0

300

310 GCOL 0,RND(3)

320 FOR SIDE=1 TO sides

330 FOR line=SIDE TO sides

340 MOVE X(SIDE), Y(SIDE)

350 DRAW X(line), Y(line)

360 NEXT line

370 NEXT SIDE

380 NEXT C

You may like to try this alternative for line 200

200 GCOL 0, RND(4)

MONTHLY

This program plots a set of "blocks" on the screen which might represent prices over a twelve month period. In its present form it will only work on a Model B Computer. With minor modifications (eg changing line 100 to MODE 5) it can be made to run on Model A. In this example the height of the bars is randomly selected at line 170. Lines 180 to 270 then draw a "solid" bar and the edges are marked in black by lines 290 to 330. Lines 340 and 350 print out one letter representing the month of the year at the bottom of each bar.

Notice that lines 60 and 70 set up two of the function keys.

Key 0 resets the computer to MODE 7 and then lists the program. Key 9 can be used to run the program.

10 REM MONTHLY

20 REM JOHN A COLL

30 REM VERSION 1 / 16 NOV 81

40 REM MODEL B

50

60 *KEY 0 "MODE7 |M LIST|M"

70 *KEY 9 "RUN |M"

80 M$="JFMAMJJASOND"

90 C=0

100 MODE 2

110 VDU 5

120 VDU 29,0;100;

130

140 FOR X=0 TO 1100 STEP 100

150 GCOL 0,C MOD 7+1

160 C=C+1

170 H=RND(400)+300

180 MOVE X,0

190 MOVE X,H

200 PLOT 85,X+100,0

210 PLOT 85,X+100,H

220 MOVE X+70,H+50

230 MOVE X,H

240 PLOT 85,X+170,H+50

250 PLOT 85,X+100,H

260 PLOT 85,X+170,50

270 PLOT 85,X+100,0

280 GCOL0,0

290 MOVEX,H

300 DRAW X+100,H

310 DRAW X+170,H+50

320 MOVE X+100,H

330 DRAW X+100,0

340 MOVE X+10,50

350 PRINT MID$(M$,C,1)

360 NEXT

370

380 GCOL 4,1

390 PRINT TAB(0,16)"----------------"

400 VDU4

410 PRINTTAB(3,0)"critical level"

The height of each bar is set randomly by the variable H. If you want to put in your own values (data), then type the following extra lines. Line 170 must also be deleted by typing 170 followed by RETURN.

50 DIM data(12)

82 FOR J=l TO 12

84 PRINT "Input data for month" MID$(M$,J,1);

86 INPUT data(J)

88 NEXT J

89 INPUT "CRITICAL LEVEL", level

155 H=data(C+1)

390 MOVE 0, level:PRINT"-------------------"

QUADRAT

This program can be used to solve equations of the form

Y=Ax²+Bx+C

The "roots of the equation" are printed to two decimal places. The number of decimal places is set by line 90.

The main program between lines 110 and 170 uses three procedures - one for each of the three types of result. The main program is surrounded by

REPEAT

'

'

'

UNTIL FALSE

which has the effect of keeping the program going for ever - or until the ESCAPE key is pressed.

Line 170 PRINT''' prints three blank lines to separate one set of results from the next.

10 REM QUADRAT

20 REM JOHN A COLL BASED ON A PROGRAM

30 REM BY MAX BRAMER, OPEN UNIVERSITY

40 REM VERSION 1.0 / 16 NOV 81

50 REM SOLVES AN EQUATION OF THE FORM

60 REM A*X^2 + B*X +C

70 ON ERROR GOTO 350

80 MODE 7

90 @%=&2020A

100 REPEAT

110 PRINT "What are the three coefficients ";

120 INPUT A,B,C

130 DISCRIM=B^2-4*A*C

140 IF DISCRIM<0 THEN PROCcomplex

150 IF DISCRIM=0 THEN PROCcoincident

160 IF DISCRIM>0 THEN PROCreal

170 PRINT'''

180 UNTIL FALSE

190 END

200

210 DEF PROCcomplex

220 PRINT "Complex roots X=";-B/(2*A)

230 PRINT " +/ "; SQR(-DISCRIM) /(2*A) "i"

240 ENDPROC

250

260 DEF PROCcoincident

270 PRINT"Co-incident roots X=";B/(2*A)

280 ENDPROC

290

300 DEF PROCreal

310 X1=(-B+SQR(DISCRIM))/(2*A)

320 X2=(-B-SQR(DISCRIM))/(2*A)

330 PRINT "Real distinct roots X=";X1;" and X=";X2

340 ENDPROC

350 @%=10:REPORT:PRINT

>RUN

What are the three coefficients ?1,-1, -2

Real distinct roots X=2.00 and X=-1.00

What are the three coefficients ?3,3,3

Complex roots X=-0.50 +/- 0.87i

What are the three coefficients ?1,2,1

Co-incident roots X=1.00

What are the three coefficients ?

Escape

>

FOURPNT

This program draws a pattern (lines 80 to 140) and then changes foreground and background colours with a half second pause between each change.

10 REM FOURPNT / DRAWS A PATTERN WITH 4 POINTS

20 REM JOHN A COLL

30 REM VERSION 1 / 16 NOV 81

40 REM MODEL A

50 MODE 4

60 VDU 29,640;512;

70

80 FOR A=0 TO 500 STEP 15

90 MOVE A-500,0

100 DRAW 0,A

110 DRAW 500-A,0

120 DRAW 0,-A

130 DRAW A-500,0

140 NEXT A

150

160 FOR B=0 TO 7 :REM CHANGE THE COLOUR

170 FOR C=1 TO 3

180 T=TIME :REM WAIT A WHILE

190 REPEAT UNTIL TIME-T>50

200 VDU 19,3,C,0,0,0

210 VDU 19,0,6,0,0,0

220 NEXT C

230 NEXT B

TARTAN

This program builds up a changing pattern by overdrawing lines on the screen.

The main program between lines 90 and 140 loops for ever and calls various subroutines as necessary. The use of subroutines with implied GOTO (e.g. line 170) results in a structure which is not easy to follow! It would be better to use "structures" such as procedures (see page 86).

10 REM TARTAN

20 REM BASED ON RESEARCH MACHINES DEMO

30 REM VERSION 1.0 / 16 NOV 81

40 MODE 2: REM ALSO WORKS IN MODE 5

50 R=l: D=l: X=0

60 Y=RND(1280)

70 MOVE X,Y

80

90 REPEAT

100 ON D GOSUB 160,260,350,430

110 IF RND(1000)<10 THEN R=D-1

120 GCOL R,(D*1.7)

130 DRAW X,Y

140 UNTIL FALSE

150

160 X=X+1024-Y

170 IF X>1280 THEN 220

180 Y=1024

190 D=2

200 RETURN

210

220 Y=1024/1280-X

230 X=1280: D=4

240 RETURN

250

260 Y=Y-1280+X

270 IF Y<0 THEN 310

280 X=1280: D=3

290 RETURN

300

310 X=1280+Y

320 Y=0: D=1

330 RETURN

340

350 X=X-Y

360 IFX<0 THEN 400

370 Y=0: D=4

380 RETURN

390

400 Y=-X: X=0: D=2

410 RETURN

420

430 Y=Y+X

I40 IF Y>1024 THEN 480

450 X=0: D=1

460 RETURN

470

480 X=Y-1024

490 Y=1028: D=3

500 RETURN

PERSIAN

This program produces a pattern by drawing hundreds of lines. Random colours are selected by lines 60 and 70. Line 80 moves the "origin (middle) of the picture to the middle of the screen". It runs on Model B only.

10 REM PERSIAN

20 REM ACORN COMPUTERS

30 REM VERSION 2 / 16 NOV 81

40 MODE1

50 D%=4

60 VDU 19,2,RND(3)+1,0,0,0

70 VDU 19,3,RND(3)+4,0,0,0

80 VDU 29,640;512;

90 J1%=0

100 FOR K%=500 TO 380 STEP -40

110 REPEATJ2%=RND(3):UNTILJ2%<>J1%

120 J1%=J2%

130 GCOL 3, J1%

140 FOR I%=-K% TO K% STEP D%

150 MOVE K%,I%

160 DRAW -K%,-I%

170 MOVE I%,-K%

180 DRAW -I%,K%

190 NEXT

200 NEXT

SQR ROOT

This program calculates the square root of a number by repeating a simple operation (lines 90 and 200) until the calculated result stays steady. The program also indicates how long the calculation takes.

This program illustrates an important mathematical technique - but of course you don't have to work out square roots this way, the (unction SQR is provided in BASIC (see page 355).

10 REM SQROOT

20 REM VERSION 1.0 / 16 NOV 81

30 REM TRADITIONAL ITERATION METHOD

40 REM TO CALCULATE THE SQUARE ROOT

50 REM OF A NUMBER TO 3 DECIMAL PLACES

60 MODE 7

70 ON ERROR GOTO 300

80 @%=%2030A

90 REPEAT

100 count=0

110 REPEAT

120 INPUT "What is your number ",N

130 UNTIL N>0

140 DELTA=N

150 ROOT=N/2

160 T=TIME

170 REPEAT

180 count=count+1

190 DELTA=(N/ROOT-ROOT)/2

200 ROOT=ROOT+DELTA

210 UNTIL ABS(DELTA) <0.001

220 T=TIME-T

230 PRINT

240 PRINT "Number ",N

250 PRINT "Root ",ROOT

260 PRINT "Iterations", count

270 PRINT "Time",T/100;" seconds"

280 PRINT''

290 UNTIL FALSE

300 @%=10:PRINT:REPORT:PRINT

>RUN

What is your number ?34

Number 34.000

Root 5.831

Iterations 5.000

Time 0.070 seconds

What is your number ?125

Number 125.000

Root 11.180

Iterations 6.000

Time 0.080 seconds

What is your number?

BRIAN

This program prints a "path in the grass".

It is a fine example of a "non-structured" use of BASIC, you might like to try and "structure" it.

90 REM BRIAN2

100 REM (C) BRIAN R SMITH 1980

110 REM ROYAL COLLEGE OF ART, LONDON

120 REM VERSION 1.0 / 16 NOV 81

130 INPUT "NUMBER OF CYCLES e.g. 1 to 5 ",T

140 INPUT "BACKGROUND SYMBOL e.g. + ",D$

150 INPUT "MOTIF (<20 CHR$.)",A$

160 INPUT "TEXT AFTER DESIGN", B$

170 CLS

180 F=1

190 READ A,G,S,C,D,N

200 H=(D-C)/N

210 X=0

220 J=1

230 X=X+S

240 Y=SIN(X)

250 Y1=1+INT((Y-C)/H+0.5)

260 I=0

270 I=I+1

280 IF I=Yl THEN 310

290 PRINT D$;

300 GOTO 420

310 Z=Z+F

320 IF Z>0 THEN 350

330 F=-F

340 GOTO 450

350 IF Z<=LEN(A$) THEN 390

360 F=-F

370 Z=Z-1

380 GOTO 310

390 S$=LEFT$(A$,Z)

400 PRINT S$;

410 I=I+Z

420 IF I<40 THEN 270

430 PRINT

440 GOTO 230

450 J=J+1

460 IF J>T THEN 490

470 Z=Z+1

480 GOTO 310

490 FOR K=1 TO 39

500 PRINT D$;

510 NEXT K

520 PRINT

530 PRINT B$

540 DATA 0,6.4,0.2,-1,1,20

>RUN

NUMBER OF CYCLES e.g.1 to 5 ?3

BACKGROUND SYMBOL e.g. + ?.

MOTIF (<20 chrs.)?Hello David !!

TEXT AFTER DESIGN?That's all foLks

SINE

This program draws a sine wave on the screen. The computer can draw dotted lines and the feature is used to fill in one part of the sine wave (line 130).

The computer can also print letters anywhere on the screen not just on a 40 by 32 grid. Lines 190 to 220 print a message in the shape of another sine curve.

10 REM SINE

20 REM JOHN A COLL

30 REM VERSION 2 / 16 NOV 81

40 REM MODEL A

50 MODE4

60 VDU5

70 GCOL 0,1

80 VDU19,1,1,0,0,0

90 MOVE 16,400

100

110 FOR X=0 TO 320

120 IF X<150 THEN MOVE 4*X+16,400

130 PLOT 21,4*X+16,300*SIN(X/48)+ 400

140 NEXT

160 GCOL 0,1

170 AS="SINE WAVES ARE FAR MORE INTERESTING . . . . ."

180

190 FOR X=1 TO 39

200 MOVE X*1280/40,300*SIN(X/6)+512

210 PRINT MID$(AS,X,1)

220 NEXT

230

240 VDU4

250 END

DOUBLE HEIGHT

Here is an example of an 'assembly' language program embedded within a BASIC program between the two brackets [ and ] which enables you to type in double height letters on the screen.

10 REM DOUBLE HEIGHT IN TELETEXT

20 WIDTH 36: MODE7

30 VDU 28,0,23,39,0

40 write=!&20E AND &FFFF

50 DIM PROG 100

60 FOR PASS=0 TO 1

70 P%=PROG

80 [

90 OPT PASS*3

100 CMP #&D : BNE noter

110 PHA : JSR write

120 LDA #&8D : JSR write

130 LDA #&0A : JSR write

140 LDA #&08 : JSR write

150 LDA #&8D : JSR write

160 PLA : RTS

170 . noter CMP #&20 : BCS legal

180 JMP write

190 . leqal PHA : JSR write

200 LDA #&0B : JSR write

210 LDA #&08 : JSR write

220 PLA : PHA : JSR write

230 LDA #&0A : JSR write

240 PLA : RTS

250 ]

260 NEXT PASS

270 !&20E=!&20E AND &FFFF0000 OR PROG

280 END

Line 270 changes the "write character" routine indirection vector so that all output is sent to the new routine given above. This routine tests for a "return" code (line 100) and if it finds one it issues Teletext double height control codes on to the next two lines. Otherwise the routine just prints the characters on two lines one above the other so as to produce a double height character. This routine has a quite different effect in non-Teletext modes. Try it. Press BREAK after you have finished with this program.

Before we leave this section, here are a few points about entering lines into BASIC.

  1. Contol characters, for example CTRL B, will only be 'reflected' in BASIC and not entered into any program lines, strings etc.

  2. Spaces entered in lines will be preserved including those at the end of the line. This allows blank lines to be entered eg

    10 space RETURN

    to separate program sections. Some of the programs above have such blank lines. Because of this you should avoid using COPY past the true end of a line.

  3. Most keywords can be abbreviated using a full stop, eg L. for LIST, SA. for SAVE. See page 483 for a list of abbreviations.

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001