110
-

l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

-

Page 2: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

~ '. "'"

.. ~

Page 3: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

1 l

/I

\

1

,!ir

COMPUTER PROGRAMS THAT WORK:

fUlly tested mathematics, science &,

games programs in BASIC

by

d D ;' .. '" ~ ~ ~,

G" ;"

SIGMf ,ECHNICAL PRE SS (Wolverhamr(

Page 4: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

90391

copyright(§)1978 by Sigma Technical Press

All rights reserved.

NO part of this book or associated computer readable materials supplied with it or reproduced from it may be reproduced by any means (including photographic, photocopying, electronic or any computer process) without the prior permission of the publisher. The only exceptions are for the purposes of review or in order to enter the programs described herein onto a computer for the sole use of the purchaser of this book.

ISBN: o 905104

Sigma Technical Press 23 Dippons Mill Close Tettenhall Wolverhampton WV6 8HH UK

03 x

Sc.,cSJct

1

11

"11)

Page 5: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

.!!(

«

~.

PREFACE This book aims to provide first time computer users with a wide

range of reliable and extensively. tested programs for use on a computer terminaL These cover some commonly used statistical and numerical calculations, simulations and calculations involving Biology, Chemistry and Physics, together with a selection of popular games which are useful for demonstration purposes. Some readers will gratefully accept the statistical programs as a means of calculating results, without any attempt to under­stand the working of the program. Others will initially run the games, and having become familiar with using a terminal may be motivated to learn BASIC, to examine the working of these programs and to write their own programs.

The BASIC language features available differ considerably from one computer to another. To aid the transferability of these programs, they arc deliberately written using an elementary subset of BASIC, which should be available to all users. The following technical points are worthy of note:

(i)

(ii)

(iii)

(iv)

(V)

(vi)

(vii)

(viii)

(ix)

All arrays and character strings are declared in the first line of th: program.

Strings are assumed to handle one character per element.

Though the circumflex • is the recommended arithmetic operator for raising to the power, some compilers only accept t or **. Wherever possible Z**2 has been replac,ed by Z*Z and so on.

The use of the logical operators AND and OR in IF statements is avoided.

Some random number generators always produce the same sequence of numbers, starting at the same place. To avoid the repetition of the same sequence of games, users are invited to type in the time, or a lucky number, which is used to call a few random numbers~ On some computers this may be simplified using RANDOMISE or RND(-l).

The functions MAT READ, MAT ZER, and MAT CON are used to read data into a matrix, or to set the elements to zero or one. They can be replaced by a READ or a LET statement inside a FOR loop to read or set the array elements appropriately.

Except in the atomic orbital program the use of character sub­str ings is a voided.

Conditional branch·statements (GOTO OF or ON GOTO) are avo'ided except in the program on atomic orbitals.

In the trial runs, all data input by the user is preceded by the prompt ? and a space.

J. D. Lee, G. Beech, T. D. Lee February 1978

iii

Page 6: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

CONTENTS Preface

Table of Contents

Mathematics Section

AREA - calculates the area under a curve using Simpson's rule.

CALENDAR C prints a calendar for any specified year.

DAY OF WEEK - calcula~es the day of the week for any date in the Gregorian calendar using Zeller's congruence.

LEAST-SQUARES - fits the best straight line through a set of points, and also gives the slope, the intercept, the standard deviation, correlation coefficient and errors.

PRIMES - calculates a list of prime numbers both by the division method and the sieve of Eratosthenes, illustrating the need for efficiency.

ROOTS - evaluates the roots of a quadratic equation, illustrat­ing the problems of robustness and numerical accuracy.

STANDARD DEVIATION - calculates the mean, the standard deviation and confidence limits for a set of data values.

Science Section

Page

iii

iv

5

8

11

16

20

23

LIFE - mathematical models to simulate the death, survival or 27 reproduction of either one or two types of 'living organism'.

PREY - a predator-prey ecological simulation between two competing species, using the Modified Euler method.

WASTE - a simulation of the effect of dumping acid wast", into a river.

TITRE - simulates the titration of a mono or diprotic acid against a base, using the bisection method to calculate the pH after each addition of base.

ENDPOINT - detects the endpoints in a potentiometric titration.

ATOMIC ORBITALS - evaluates the full Schrodinger equation and produces pictures of the common atomic orbitals.

iv

33

37

40

44

47

Page 7: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

,I'

«

II

,I

WAVES - sums a series of sine waves and produces graphical output of either the amplitude or intensity of the resultant wave.

BOMB - a simulation of the factors involved in criticality in nuclear fiSSion, using the Monte Carlo method.

SATELLITE - study of the factors involved in satellite motion, illustrating Kepler's laws.

Games Section

BATTLESHIPS - a game involving sinking the enemy fleet, illustrating the use of rectilinear coordinates, reflection and rotational symmetry.

CARS - an excellent demonstration program involving driving a racing car.

CHOMP - a game which involves taking slices of cake and avoiding eating a poisoned cherry.

DRAG - a game which simulates the design factors involved in building a successful dragster.

FRUIT MACHINE - a Simple simulation of a one-arm bandit, illustrating probability.

Page

52

56

59

64

69

74

78

83

MASTERMIND - a number guessing game, which involves Skill., 87

SPIES - a simple board game illustrating the application of two-dimensional coordinates.

TlMM - a computerised version of the ancient oriental game Nim, which can be readily mastered using binary numbers,

Advertisement

'v

91

94

Page 8: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

~ '. "'"

.. ~

Page 9: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

(t.

..

• .. t

AREA

This program calculates the area under a curve using Simpson's rule. This is a more accurate method than using the trapezium rule, but it imposes two restrictions:

(i)

(ii)

The y values (height of the curve), must be specified at equal intervals along the x axis.

The number of y values specified must be odd.

The equation for Simpson's rule is :

x interval [first y last y area:. 3 . value + value +

twice the sum four times the] of other odd y + sum of even y

values values

The program will provide full instructions if requested. The user must specify the interval on the x axis, followed by the various y values. The end of the data is indicated by typing 999999. The following checks are performed :

(i)

(ii)

the interval on the x axiS is not zero.

that an odd number of data points have been provided. If this is not fulfilled, the user is 'offered a choice from three poss­ibilities: (a) that the last data value is ignored, (b) the area is calculated using Simpson's rule as iii (a) plus the area obtained using the trapezium rule on the last two points, or (c) the user is invited to type in additional data .

The area is then calculated and printed out, and the user may choose whether to finish or to start another calculation .

1

Page 10: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

l~ DIM H(200), R$(9), 2~ PRINT "CALCULATION 30 PRINT "=========== 40 PRINT

V$ (8), 0$ (8) OF THE AREA UNDER A CURVE BY SIMPSONS RULE."

==== ===== ======== ====="

50 PRINT "WOULD YOU LIKE FULL INSTRUCTIONS?" 60 PRINT "TYPE YES OR NO AND PRESS RETURN." 70 PRINT 80 INPUT 0$ 90 PRINT 100 IF 0$ 110 IF 0$

·YES" THEN 140 "NO" THEN 140

120 PRINT "REPLY '", 0$, ". NOT UNDERSTOOD. " , 130 GOTO 60 140 PRINT "TYPE IN THE INTERVAL", 150 LET J 1 160 IF 0$ "NO" THEN 180 170 PRINT" BETWEEN READINGS, AND 180 PRINT 190 INPUT X 200 PRINT 210 IF X <> 0 THEN 250

PRESS RETURN.",

220 PRINT "AN INTERVAL OF ZERO MEANS A VERTICAL LINE" 230 PRINT "RETYPE VALUE CORRECTLY" 240 GOTO 1811 250 IF 0$ = "YES" THEN 280 260 PRINT "TYPE DATA POINTS" 270 GOTO 320 280 PRINT "TYPE IN THE FIRST DATA POINT, THEN PRESS RETURN" 291l PRINT "THEN TYPE IN THE NEXT DATA POINT, RETURN, ETC." 3110 PRINT "THERE MUST BE AN ODD NUMBER OF GENUINE DATA POINTS." 310 PRINT "TERMINATE DATA WITH DUMMY VALUE OF 999999 AND PRESS RETURN" 320 PRINT 3311 FOR I = J TO 200 340 INPUT H (I) 350 IF H(I) 999999 THEN 410 360 LET N = I 370 NEXT I 380 PRINT 390 PRINT "PROGRAM HANDLES A MAXIMUM OF 200 DATA POINTS" 400 PRINT 410 IF N >= 3 THEN 450 420 REM ENTER IF ERRORS FOUND 430 PRINT "JOB ABANDONED **** INSUFFICIENT DATA POINTS" 440 GOTO 770 450 LET R$ = "OK" 460 REM TEST FOR AN ODD NUMBER OF DATA POINTS 470 IF (2 * INT(N / 2) <> N) THEN 630 480 PRINT "AN EVEN NO. OF DATA POINTS HAVE BEEN GIVEN" 490 PRINT "TYPE IGNORE IF THE LAST VALUE IS TO BE IGNORED," 500 PRINT "TYPE TRAPEZ IF THE LAST VALUE IS TO BE INCLUDED USING THE" 510 PRINT "TRAPEZIUM RULE OR TYPE DATA IF YOU WISH TO ADD MORE DATA" 520 PRINT "POINTS. THEN PRESS RETURN" 530 PRINT 540 INPUT R$ 550 IF R$ = "DATA" THEN 6011

2

i)

(1;

Page 11: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

«

I(

~

«

«. 560

¢ 570 588 590 600

4f 610 620 630 640

.~ 650 660 670 680

C 690 700 710

~ 720 730 740 750

« 760 770 760 790

if 800 S10 820 830

i~, 840 850 860 870 • 880 890 900

(j 910 920 930 940

4 950

(i(

4,

Ii

~

II

IF R$ = "TRAPEZ" THEN 620 IF R$ = "IGNORE" THEN 620 PRINT "REPLY''': R$: "' NOT UNDERSTOOD - RETYPE CORRECTLY!" GOTO 540 LET J = N + 1 GOTO 260 LET N = N - 1 LET J = N - 1 LET Sl = 0 LET S2 = 0 FOR I = 2 TO J STEP 2 LET S2 S2 + H(I) IF I = J THEN 700 LET Sl = 51 + H(I + 1) NEXT I LET A = (X / 3) * (H(l) + i • Sl + 4 • S2 + H(N» IF R$ <> "TRAPEZ" THEN 760 PRINT "AREA WITHOUT LAST TRAPEZIUM =": A LET A = A + «H(N) + H(N + 1» / 2) * X PRINT II TOTAL. It: PRINT "AREA UNDER CURVE =": A PRINT PRINT "WOULD YOU LIKE ANOTHER RUN?" IF 0$ = "NO" THEN 810 PRINT "TYPE YES OR NO AND PRESS RETURN" PRINT INPU1' V$ PRUIT' IF V$ = "YES" THEN 880 IF V$ = "NO" THEN 940 PRINT "I>EPLY '''I V$: "' NOT UNDERSTOOD. ": GOTO 800 PRINT "NEw SET OF DATA" PRINT 11=== === ====" PRINT LET 0$ = "NO" GOTO 140 RC~i TERMINATE SUCCESSFUL JOB PRINT' "JOB COMPLETED SUCCESSFULLY" END

3

Page 12: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

TRIAL RUN

CALCULATION OF THE AREA UNDER A CURVE BY SIMPSONS RULE.

WOULD YOU LIKE FULL INSTRUCTIONS? TYPE YES OR NO AND PRESS RETURN.

? YES

TYPE IN THE INTERVAL BETWEEN READINGS, AND PRESS RETURN. ? 1.8

TYPE IN THE FIRST DATA POINT, THEN PRESS RETURN THEN TYPE IN THE NEXT DATA POINT, RETURN, ETC. THERE MUST BE AN ODD NUMBER OF GENUINE DATA POINTS. TERMINATE DATA WITH DUMMY VALUE OF 999999 AND PRESS RETURN

? 0 ? 1.0 ? 2.11 ? 3.0 ? 4.0 ? 999999 AREA UNDER CURVE = 8

WOULD YOU LIKE ANOTHER RUN? TYPE YES OR NO AND PRESS RETURN

? NO

JOB COMPLETED SUCCESSFULLY

4

Page 13: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

CALENDAR

This program automatically produces a calendar for any year. First the program uses Zeiler's Congruence to generate the day of the week on which the first of January fails in the given year. It then prints out a heading, the name of the month, followed by the days of the month. The \ name of the next month is then printed and so on, giving a twelve-month \ calendar.

The user types the year required as data.. The program automatic­ally adjusts the number of days in February for leap years. Zeller's Congruence is descr ibed in the program DAY.

The calendar which we now use was devised by Pope Gregory, and was calculated from the date of the first Nicene council in 385 A. D. It was established on 15th ()cto~rd58'2, but wa~ a~pted for use in Great Britain. America and the British Colonies in 1752. (On its introduction; the inhabitants of Shepshe~ ... .Leicestershire rioted because they believed that the Pope had shortened their lives by seventeen days:) The calendar is based on 365 days a year, With a leap year every year which is exactly divisible by 4, excluding centuries except when they are divisible by 400. The calendar thus repeats itself every 400 years, and contains 97 leap years. Thus 400 years will have 146,097 days. An astronomical year, which is the time for one revolution of the earth round the sun is 365 days, 5 hours, 48 minutes and 45.974 seconds. Thus 400 years will be 146,096 days, 21 hours, 6 minutes and 29. 6 seconds. The Gregorian calendar is remarkably accurate, and the error will accumulate to one day in 3320 years, and since the calendar started in 1582, this will not occur until the year 4902 :

5

Page 14: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

10 DIM A$(ll), E$(24) 20 PRINT .AB(21), "CALENDAR" 30 PRINT TAB(21), "========" 48 LET S$ = n~****~~**~**** .. * ••• ****n 50 PRINT "TYPE IN YEAR REQUIRED EG. 1976 THEN PRESS RETURN" 60 INPUT Y 70 IF Y <> INT(Y) THEN 50 80 IF Y < 1752 THEN 50 90 IF Y > 4902 THEN 50 100 REM *** FIND DAY OF WEEK OF JANUARY 1ST. 110 LET Y1 = INT«Y - 1) / 100) 120 LET Y2 = Y - 1 - 100 * Y1 130 REM *** THIS IS ZELLERS CONGRUENCE 140 LET D 799 + Y2 + INT(Y2 / 4) + INT(YI / 4) - 2 * Y1 150 LET D = -(D - (INT(D / 7) * 7» 160 PRINT 170 PRINT 180 PRINT 190 PRINT TAB(21)I "CALENDAR FOR"I Y 200 PRINT TAB(21)1 "======== === ====" 210 PRINT 220 LET L = 0 230 REM *** CHECK FOR LEAP YEAR 240 IF «INT(Y / 4).* 4) <> Y) THEN 280 250 IF «INT(Y I 400) * 400) Y) THEN 270 260 IF «INT (Y / 100) • 100) = Y) THEN 280 2711 LET L 1 280 FOR N = 1 TO 12 290 PRINT 300 PRINT 310 READ A$, M 320 DATA "* JANUARY *", 31, "* FEBRUARY", 338 DATA ft*. APRIL **n, 38, "*** MAY ***n, 34111 DATA "*** JULY **n, 31, "** AUGUST *;:,' 350 DATA "*.OCTOBER *", 31, "* NOVEMBER 360 PRINT TAB(4)I S$I A$I S$ 370 PRINT TAB(8)1 "SUN MON TUE 38111 PRINT TAB(4); "******"****"; SS; 5$ 390 IF N <> 2 THEN 410 400 LET M M + L 410 FOR I = 1 TO 6 420 PRINT 430 FOR J 1 TO 7 440 LET D D + 1 450 IF D > M THEN 520 460 IF D <= 0 THEN 480 470 PRINT TAB(J * 8)1 DI 480 NEXT J 490 LET J = 1 500 IF D = M THEN 520 510 NEXT I 520 LET D = - J 530 PRINT 540 NEXT N 550 FOR I = 1 TO 6

6

28, "** "".ARCH **", 31, "*** JUNE **", 31, " SEPTEMBER " 30, ". DECEMBER "

WED THU FRI

31 30 30 31

SAT"

Page 15: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

i~

,

~,

560 PRINT 570 NEXT I 580 PRINT "HAPPY NEW YEAR" 590 END

'I'RIAL RUN

CALENDAR

TYPE IN YEAR REQUIRED EG. 1976 THEN PRESS RETURN ? 1978

CALENDAR FOR 1978

************************* JANUARY ************************* SUN MON TUE WED THU FRI SAT

** •• *******************************************************

1 8 15 22 29

2 9 16 23 30

3 10 17 24 31

4 11 18 25

5 12 19 26

6 13 20 27

7 14 21 28

************************* FEBRUARY ************************ SUN MON TUE WED THU FRI SAT

************.**********************************************

5 12 19 26

6 13 20 27

7 14 21 28

1 8 15 22

2 9 16 23

3 10 17 24

4 11 18 25

************************** MARCH ************************** SUN MON TUE WED THU FRI SAT

***********************************************************

5 12 19 26

6 13 20 27

7 14 21 28

1 8 15 22 29

7

2 9 16 23 30

3 10 17 24 31

4 11 18 25

Page 16: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

DAY OF WEEK

This program calculates the day of the week for any date specified using Zeller's congruence, which is described below. Using the program enables one to find, for example, on which day of the week a person was born, or the day on which Christmas day falls, from the appropriate date (day, month and year). The program also reports if the year chosen is a leap year, and adjusts the number of days in February.

The program performs the following checks on the input data :

(i)

(ii)

The date of the month must be an integer number greater than or equal to I, and less than or equal to the maximum number of days in the month specified.

The month must be correctly spelt, and must not be abbreviated.

(iii) The year must be an integer number and within the range for which Zeller's congruence applies (1582 - 4902 A. D. ).

Zeller's congruence to find the day of the week is described in "Elementary Number Theory" by J.V. Uspensky and M.A. Heaslet, McGraw­Hill, New York 1939, p.206. A date may be expressed in the following way.

D the day of the month M = the number of the month, with January and February taken as

months 11 and 12 of the preceding year, March as 1, April as 2, ..... December as 10.

Y = the year in the century (0 - 99) C = the century.

The formula is

day of the week = {[2.6M-0.2] + D + Y + [~] + [7] -2C }mod7

The square brackets denote the largest integer contained in the term con­cerned.

Thus for D-day, the 5th June 1944, D = 5, M =4, Y =44 and C = 20. The term [2. 6M - O. 2] calculates as 10. 4 - 0.2 = 10. 2, and taking the integer gives 10. Similarly the terms Y -give 11 and C gives 5. The total

"4 "4 then becomes 10 + 5 + 44 + 11 + 5 - 40 = 35. The remainder after dividing by seven (mod 7) is zero, and this value gives the day of the week (Sunday = 0, Monday = 1, Tuesday = 2, ...... Saturday = 6). Before; performing the mod 7 operation, a large number which is an exact multiple of seven is added, thus removing the possibility of a negative result, but without changing the answer.

8

-n

Page 17: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

10 DIM A(12), B$(15), F$(9), H$(15), Z$(7) 20 PRINT TAB(5): "DAY OF THE WEEK" 30 PRINT TAB(5): "=== == === ====" 40 PRINT 50 PRINT "INPUT THE DAY OF MONTH.": 60 INPUT 01 70 IF 01 <> .NT(Dl) THEN 50 80 PRINT "INPUT THE MONTH IN WORDS.": 90 INPUT B$ 100 FOR M = 1 TO 12 110 READ H$ 120 DATA "MARCH" i "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER" 130 DATA "OCTOBER", "NOVEMBER", "DECEMBER", "JANUARY", "FEBRUARY" 140 IF B$ = H$ THEN 280 150 NEXT M' 168 PRINT "RE-INPUT THE MONTH CORRECTLY." 178 INPUT B$ 180 RESTORE 190 GCTO 100 200 LET Z$ = "WAS" 210 PRINT "INPUT THE YEAR REQUIRED": 220 INPUT Y 238 IF Y > 4902 THEN 210 240 IF Y < 1582 THEN 210 250 IF Y <> INT(Y) THEN 210 260 PRINT" IS THIS DATE IN THE FUTURE? TYPE' YES OR NO." 270 INPUT F$ 280 IF F$ = "NO" THEN 310 290 IF F$ <> "YES" THEN 260 300 LET Z$ = "WILL BE" 310 FOR X = 1 TO (12 - Mr 320 READ B$ 330 NEXT X 340 MAT REIID A 350 DATA 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28 360 IF 01 >= 1 THEN 400 370 PRINT "THE DAY OF THE MONTH CANNOT BE < 1, TRY AGAINI" 380 GOTO 470 390 REM ****OECIOE IF IT IS A LEAP YEAR 400 IF INT(Y / 4) * 4 <> Y THEN 450 410 IF INT(Y / 400) * 400 = Y THEN 430 420 IF INT(Y / 100) * 100 = Y THEN 450 430 LET A(12) = A(12) + 1 440 PRINT Y: Z$: " A LEAP YEAR." 450 IF 01 <= A(M) THEN 490 460 PRINT "THERE ARE ONLY": A(M): "DAYS IN ": H$: " TRY AGAINI" 470 INPUT 01 480 GOTO 360 490 LET Y3 = Y 500 IF M < 11 THEN 520 510 LET Y3 = Y3 - 1 520 LET Y! = INT(Y3 / 100) 530 LET Y2 = Y3 - (Yl * 100) 540 REM ***THIS IS ZELLERS CONGRUENCE 550 LET K = 2.6 * M - .2

9

Page 18: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

560 LET D = INT(K) + D1 + Y2 + INT(Y2 / 4) + INT(Y1 / 4) - 2 * Y1 + 700 570 LET D = D - (INT(D / 7) * 7) 580 PRINT "THE"; Dl; 590 IF D1 1 THEN 720 600 IF D1 21 THEN 7~0 610 IF Dl 31 THEN 720 620 IF D1 2 THEN 700 630 IF Dl 22 THEN 700 640 IF Dl 3 THEN 680 650 IF Dl 23 THEN 680 660 PRINT "TH"; 670 GOTO 730 680 PRINT "RD"; 690 GOTO 730 700 PRINT "ND"; 710 GOTO 730 720 PRINT "ST"; 730 PRINT n OF 1I~ H$; Y; Z$; II A n;

740 FOR X = 1 TO (D + 1) 750 READ H$ 760 DATA "SUN", "MON", "TUES It , "WEDNES", ItTHURS", "FRI", "SATUR" 770 NEXT X 780 PRINT H$; "DAY" 790 PRINT 8011 PRIN'!' "WOULD YOU LIKE ANOTHER RUN? (YES/NO)" 810 INPUT H$ 8211 IF H$ = "NO" THEN 880 8311 IF H$ = "YES" THEN 860 840 PRINT "REPLY ,n; H$; "' NOT UNDERSTOOD. PLEASE ANSWER YES OR NO" 8511 GOTO 8111 860 RESTORE 870 GOTO 50 880 ,PRINT "A SPECIAL DEVICE HAS BEEN INVENTED TO FIND THE DA'rE-" 8911-'PRINT "IT IS CALLED A CALENDAR!" 9011 END

TRIAL RUN

DAY OF THE WEEK

INPUT THE DAY OF MONTH.? 11 INPUT THE MONTH IN WORDS.? NOVEMBER INPUT THE YEAR REQUIRED? 1918 IS THIS DATE IN THE FUTURE? TYPE YES OR NO. ? NO THE 11 TH OF NOVEMBER 1918 WAS A MONDAY

WOULD YOU LIKE ANOTHER RUN? (YES/NO) ?NO A SPECIAL DEVICE HAS BEEN INVENTED TO FIND THE UATE­IT IS CALLED A CALENDARI

10

It)

i)

Page 19: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

<'i;

t

LEAST SQUARES

This program calculates the value of the slope m, and of the intercept c in the equation y = mx + c to the straight line which fits the experimental points most closely. The least squares method minimises the sum of the squares of the differences between the experimental points and the line. Provided that there are at least two data points the computer will always report the best straight line through the points given. Despite being the best straight line, this might in some cases be a poor fit (e. g. through a parabola or a Sine wave), or a completely unreasonable fit (e. g. through the four corners of a square). To indicate how closely the line fits the points the program calculates the correlation coefficient and the standard deviation of the differences between the y values and the line. For a line which passes exactly through each pOint, the standard deviation is zero and the correlation coefficient is either + 1 or - 1 depending on the slope of the line. As the fit gradually becomes worse, the standard deviation increases and the correlation coefficient becomes nearer to zero. Most reasonable straight lines have a correlation coefficient of ± 0.9 or better ..

The algorithm used in the program gives better accuracy than the method used in many other sources by avoiding the possibility of subtract­ing two large and nearly equal quantities. (This problem is further- dis­cussed in the program ROOTS). For those interested in the mathematics, the equations used are :

m=

c =

E (Xi - it) (y i - Y) _ 2

I:(xi -x)

EYi E (Xi - X)2 - E Xi I:(xi - X)(Yi - Y) -2

n I:(xi - x)

correlation coefficient

standard deviation

rr:;r 1n:T (if n <30)

or

(if n ~30)

where Xi and Yi are the coominates of the data points, it and yare the mean values of x and y, n is the number of data pOints, and lli values are the difference between Yi and the calculated point on the straight line.

In the program full instructions are available as an option. One pair of x and y coordinates are entered on one line separated by a comma. This is repeated for subsequent points, and finally a dummy

11

Page 20: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

value with both x and y equal to 999999 is typed. The program prints the slope, intercept, standard deviation and correlation coefficient of the best straight line, and optionally a list of differences between the actual and calculated y values. Finally the user chooses whether to have another run, or to finish.

Checks are performed to ensure that there are at least two and not more than one hundred data points. The latter limit is set by the array sizes. Further checkS on the data prevent failure which would otherwise occur either if all the x coordinates or all the y coordinates are the same.

12

Page 21: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

U DIM P$ (10), Q$ (3), X (11l0) , Y (11l1l) 20 PRINT "PROGRAM TO FIT A STRAIGHT LINE THROUGH A SET OF POINTS" 30 PRINT "======= == === = ======== ==== ======" 40 PRINT 51l PRINT "WOULD YOU LIKE FULL INSTRUCTIONS?": 61l GOSUB U21l 71/J LET Q$ = p$ 80 IF p$ = "NO" THEN 120 90 PRINT "TYPE IN X & Y VALUES SEPARATED BY A COMMA" 100 PRINT "THEN PRESS RETURN AND TYPE THE NEXT PAIR OF VALUES" 118 PRINT "TERMINATE DATA WITH BOTH X & Y EQUAL TO 999999" 120 PRINT 131/J PRINT "STARTING DATA" 140 PRINT " X, Y" 151/J REM READ IN X AND Y FOR EACH DATA POINT AND STORE IN ARRAYS 160 REM DATA TERMINATED BY X=Y=999999. 170 REM SET INITIAL VALUES TO ZERO 180 READ N, S, Yl, C, Sl, S2, S3, X2, Y2, S4, E2 190 DATA 0, 0, 0, I, 0, 0, 0, 0, 0, 0, 0 200 FOR I = 1 TO 100 210 INPUT X(I), Y(I) 220 IF X(I) <> 999999 THEN 240 230 IF Y(I) = 999999 THEN 290 240 PRINT 251/J LET N = N + 1 260 NEXT I 270 PRINT "PROGRAM CAN ONLY HANDLE A MAXIMUM OF 11l1l VALUES" 280 PRINT "INPUT DATA" 290 IF N < 2 THEN 531/J 300 REM CALL SUBROUTINE TO WORK OUT LEAST-SQUARES FIT ETC. 3U GOSUB 750 320 PRINT 330 PRINT "SLOPE en: S 340 PRINT 350 PRINT "INTERCEPT ON Y AXIS =": Yl 361/J PRINT 370 PRINT "CORRELATION COEFFICIENT en: C 380 PRINT 390 PRINT "STANDARD DEVIATION en: Sl 400 PRINT 411/J PRINT "WOULD YOU LIKE A LIST OF ERRORS?": 420 GOSUB UU 430 IF p$ = "NO" THEN 578 440 REM CALCULATE & PRINT ERROR FOR EACH DATA POINT 450 PRINT 460 PRINT "ERROR FOR EACH DATA POINT" 470 PRINT " X" I n yo, II ERRORII

480 FOR I = 1 TO N 490 PRINT X (I), Y (I), (Y (I) - Yl - S * X (I» 500 NEXT I 510 GOTO 570 520 REM ENTER IF THERE ARE NOT ENOUGH DATA POINTS 530 PRINT 540 PRINT "RUN ON THIS DATA ABANDONED BECAUSE THERE ARE" 550 PRINT "NOT ENOUGH DATA POINTS"

13

Page 22: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

560 REM DECIDE WHETHER TO FINISH OR HAVE ANOTHER RUN 570 PRINT 580 PRINT "WOULD YOU LIKE ANOTHER RUN?": 590 GOSUB 1110 600 IF p$ = "NO" THEN 700 610 RESTORE 620 PRINT "TYPE IN A NEW SET OF DATA" 630 PRINT "==== == = === === ====" 640 IF Q$ = "NO" THEN 660 650 PRINT "(TERMINATE DATA WITH BOTH X & Y EQUAL TO 999999)" 660 PRINT "X,Y" 670 LET Q$ = "NO" 680 GOTO 180 690 REM TERMINATE JOB 700 PRINT "END OF JOB" 710 STOP 720 REM SUBROUTINE TO CALCULATE LEAST SQUARES FIT, E'I'C. 730 REM ARRAYS X & Y CONTAIN N DATA POINTS 740 REM CALCULATE SUMS OF X & Y VALUES 750 FOR I = 1 TO N 760 LET S2 S2 + X(I) 770 LET S3 S3 + Y(I) 780 NEXT I 790 LET Al S2 I N 800 LET A2 S3 I N 810 FOR I = 1 TO N 820 LET X9 X(I) - Al 830 LET Y9 Y(I) - A2 840 LET X2 X2 + x9 • X9 850 LET Y2 Y2 + Y9 • Y9 860 LET S4 S4 + X9 • Y9 870 NEXT I 880 PRINT 890 IF X2 <> 0 THEN 940 900 PRINT "RUN TERMINATED BY 'rHE PROGRAM BECAUSE ALL" 910 PRINT "THE X-COORDINATES ARE THE SAME." 920 GOTO 570 930 REM CALCULATE SLOPE 940 LET,S = S4 I X2 950 REM CALCULATE INTERCEPT ON Y AXIS 960 LET Yl = (X2 • S3 - S2 • S4) I (N • X2) 970 IF Y2 = 0 THEN 1010 980 REM CALCULATE CORRELATION COEFFICIENT 990 LET C = S4 I SQR(X2 • Y2) 1000 REM CALCULATE SUM OF ERRORS SQUARED 1010 FOR I = 1 TO N 1020 LET E1 = Y(I) - Y1 - S • X(I) 1030 LET E2 = E2 + E1 • E1 1040 NEXT I 1050 REM CALCULATE STANDARD DEVIA'lION 1060 LET Sl = SQR(E2 I (N - 1)) 1070 IF N < 30 THEN 1090 1080 LET Sl = SQR(E2 I N) 1090 RETURN 1100 REM SUBROUTINE TO CHECK REPLIES

14

~

{Ij

' .. ;0;

1Q

-ij

~~

'1Ii

'ilJ

~'?

"i,~

{~

~)

if·

"t

~

1~

iili

1)

p~

1;

1;)

Page 23: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

11111 IF Q$ = "NO" THEN 11311 11211 PRINT " TYPE YES OR NO & PRESS RETURN." 1130 PRINT 1140 INPUT P$ 1150 IF p$ = "YES" THEN 1190 11611 IF P$ = "NOn THEN 1190 1170 PRINT 11 REPLY In; P$; '" NOT UNDERSTOOD."; 1180 GOTO 1120 11911 RETURN 1200 END

TRIAL RUN

PROGRAM TO Fa A STRAIGHT LINE THROUGH A SET OF POINTS

WOULD YOU LIKE FULL INSTRUCTIONS? TYPE YES OR NO & PRESS RETURN.

? YES TYPE IN X & Y VALUES SEPARATED BY A COMMA THEN PRESS RETURN AND TYPE THE NEXT PAIR OF VALUES TERMINATE DATA WITH BOTH X & Y EQUAL TO 999999

STARTING DATA X,Y 1,2

? 2,4

? 3,6

? 4,8

999999,999999

SLOPE = 2

INTERCEPT ON Y AXIS = 0

CORRELATION COEFFICIENT

STANDARD m;VIA'rION = 0

WOULD YOU LIKE A LIST OF ERRORS? TYPE YES OR NO & PRESS RETURN.

? NO

WOULD YOU LIKE ANOTHER RUN? TYPE YES OR NO & PRESS RETURN.

? NO END OF JOB

15

Page 24: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

PRIMES

A prime number is one which can be divided exactly only by itself or unity. From this simple definition it is easy to write a simple progTam to test if a number is prime by dividing successively by every whole numrer smaller than it down to the number two. If any divides into the original exactly (1. e. the remainder is zero) then the original number was not prime. For example

10 PRINT "TYPE NUMBER TO BE TESTED"

20 INPUT X

30 FOR I = 2 TO X-I

40 IF INT(X /I)=X/ I THEN 80

50 NEXT I

60 PRINT X; "IS PRIME"

70 STOP

80 PRINT X; "IS DIVISIBLE BY"; I

90 END

Whilst this progTam will work, it suffers from lack of robustness, and is gTossly inefficient. The progTam will give incorrect results if zero or a negative number is typed in. Further any non integer number will be reported as prime. These defects may' be corrected easily with suitable IF statements. Because the progTam is inefficient, testing a large number will take a considerable amount of computer time, and thought should be given to improving the logic :

(i) It is obvious that any even number gTeater than two cannot re prime, and that any odd number cannot have an even factor. There is therefore no point in dividing any number by any even number gTeater than two. This halves the amount of work done by the computer, and may be implemented by adding two lines and changing one line :

(ii)

28 LET I = 2

29 IF INT(X / 1) = X / I THEN 80

30 FOR I = 3 TO X-I STEP 2

If a number has factors, one of them must be less than or equal to the square root of the number. This greatly reduces the number of diviSions particularly with large numbers, and

16

~)

Page 25: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

t;

~:

necessitates changing one line :

30 FOR I = 3 TO SQR(X) STEP 2

It is quite easy to change this program to produce a table of prime numbers from 2 to 1000 by removing lines 10, 20, 70 and 80 and adding two lines:

20 FOR X = 2 TO 1000

80 NEXT X

This program will work, but is still very inefficient because line 20 is a loop which counts in ones, leaving lines 28 and 29 to remove the even numbers. It would be more efficient to have a loop starting at 3 with an increment of 2. Furthermore the PRINT statement (line 60) is wasteful in the time taken to print, and in the amount of paper used.

A further enhancement shown in the example using the division method is to store the prime numbers found, and only divide by these numbers when testing for a new prime. Having modified the program thus, it runs much faster than earlier versions, but is still painfully slow with large numbers. The division method is inherently slow, and to improve the speed when creating a large table of primes we must look for a better way.

The anc ient mathematician Eratosthenes (276 - 195 B. C. ) beat us to this conclusion, and his solution is now known as "the sieve of Eratosthenes". The underlying idea is to start with a complete lis~ of integer numbers from two to the maximum number to be tested. When­ever a prime is found, all Simple multiples of the prime are deleted from the list. Thus, 2 is a prime, hence 4, 6, 8 and all even numbers are deleted. The next number remaining in the list is 3, which is therefore prime, and the numbers 6, 9, 12 • .. if still present, will be deleted from the list. Similarly 5 remains in the list. so is prime, and its multiples are removed from the list. This elimination process is continued until the next prime number is greater than or equal to the square root of the maximum number to be tested. Any numbers remaining on the list at this stage are all primes. The following program will find prime numbers up to 2000.

17

Page 26: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

PROGRAM TO FIND PRIMES BY THE DIVISION METHOD

III DIM P(31l1l) 21l LET PI = Il 31l PRINT" 2 "; 41l FOR X = 3 TO 11l1l1l STEP 2 51l LET Xl = SQR(X) 69 FOR I = 1 TO PI 79 IF INT(X / P(I» = X / P(I) THEN 130 81l IF P(I) > Xl THEN 1119 99 NEXT I 11l1l PRINT X; 119 LET PI = PI + 1 121l LET P(P1) = X 130 NEXT X 141l PRINT 151l PRINT "NUMBER OF PRIMES ="; PI + 1 169 END

TRIAL RUN

2 3 5 7 11 13 17 19 23 29 31 71 73 79 83 89 97 101 103 107 149 151 157 163 167 173 179 181 227 229 233 239 241 251 257 263 3117 311 313 317 331 337 347 349 389 397 401 4119 419 421 431 433 467 479 487 491 499 503 509 521 571 577 587 593 599 6Bl 6117 613 653 659 661 673 677 683 691 7111 751 757 761 769 773 787 797 8119 853 857 859 863 877 881 883 887 947 953 967 971 977 983 991 997

NUMBER OF PRIMES = 168

18

37 109

191 269 353 439 523 617 709 811 997

41 43 47 53 59 61 67 113 127 131 137 139

193 197 199 211 223 271 277 281 283 293 359 367 373 37·9 383 443 449 457 461 463 541 547 557 563 569 619 631 641 643 647 719 727 733 739 743 821 823 827 829 839 911 919 929 937 941

Page 27: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

~)

PROGRAM TO FIND PRIMES BY SIEVE OF ERATOSTHENES

1~ DIM P(2000) 20 REM SET AFRAY ELEMENTS EQUAL TO ONE ( CREATE LIST ) 30 MAT P = CON 40 PRIN'f "PRIMES FROM 2 - 2000" 50 LET F = sQR(2000) 60 FOR I = 2 TO 2000 70 IF P (I) = 0 THEN 140 80 PRINT II 90 IF I > F THEN 140 100 REM REMOVE MULTIPLES FROM LIST ( SET TO ZERO ) 110 FOR J = I TO 2000 STEP I 120 LET P(J) = 0 130 NEXT J 140 NEXT I 150 END

TRIAL RUN , PRIMES FROM 2 - 2000

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 197 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 7B7 797 B09 811 B21 823 827 829 839 853 857 859 B63 B77 B81 BB3 BB7 907 911 919 929 937 941 947 953 967 971 977 9B3 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 llBl 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 12B3 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373 13Bl 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999

19

Page 28: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

ROOTS

A quadratic equation of the form: a x2 + bx + c = 0 may be solved from the coefficients a, band c, using the equation:

-b±/b2 - 4 ac x = 2a

The equation may be solved to give the roots, (places where the curve cuts the x axis), in a five line program:

10 PRINT "INPUT A, B, C" 20 INPUT A, B, C 30 PRINT "X="; (-B+SQR(B * B - 4 * A * C» I (2 * A) 40 PRINT "X="; (-B-SQR(B * B - 4 * A • C» I (2 * A) 50 END Under favourable conditions, this program will work and give the

correct answers, for example

x2 +2x-3=0 <r2x2 -2x-4=0 This program is not recommended because it is not robust, and will fail or give inaccurate results with many types of data, which are considered below:

(i)

(ii)

(iii)

(iv)

If a = 0; the equation is not a quadratic, and the machine will fail because it attempts to divide by zero.

If the curve does not cut the x axis, it has no real roots. This occurs when })2 - 4 ac is negative, and with such an equation, for example: x2 + 2x + 3 = 0 the program will fail because it attempts to evaluate the square root ci. a negative number.

A special case arises when ~ = 4 ac, since the curve only touches the x axis at one point, for example : x2 - 2x + 1 = O. The two roots cOincide, and though the program works it reports two different but equal roots.

The program may give wildly erroneous results for one of the roots with certain pathological data. This occurs when b is almost equal to b2 - 4 ac. All computers carry a limited number of Significant figures, hence when subtracting two almost equal numbers, the result obtained may be made up of only a few figures which are meaningful, or even no meaningful figures. This problem becomes acute when b is several orders of magnitude greater than a or c. Even with pathological data the root obtained by adding the two almost equal numbers will be reliable. Since the product of the two roots of a quadratic is equal to cIa, the second root may be evaluated rom the first root, thus avoiding the problem of numerical accuracy encountered when attempting the direct solution.

The program given overcomes the difficulties outlined.

20

)

,~

j..i.

~~

~

<it

1t

'IF

>\~

f'

~

fi

~~

~

-(Ij

~.

JJ)

11)\

~

.'1

;'

~~t

\

Z

Page 29: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

"J

\'1

'i

"

ill; j

10 DIM Q$(8) 20 PRINT "PROGRAM TO SOLVE QUADRATIC EQUATIONS" 30 PRINT "==:;;::::=== == =:::=== ::::======== ==========11 40 PRINT" EG. A*X**2 + B*X + C ; 0" 50 PRINT 60 PRINT "TYPE IN THE THREE COEFFICIENTS A,B,C" 70 PRINT "WITH A COMMA BETWEEN TERMS. THEN PRESS RETURN." 80 INPUT A, B, C 90 IF A <> 0 THEN 130 100 PRINT 110 PRINT "THIS IS NOT A QUADRATIC EQUATION." 120 GOTO 280 130 LET 0 ; B * B - 4 * A * C 140 IF 0 <> 0 THEN 170 150 PRINT "BOTH ROOTS ARE EQUAL, AND HAVE THE VALUE:"; -8 / (2 * A) 160 GOTO 280 170 PRINT 180 IF 0 > 0 THEN 210 190 PRINT "THIS EQUATION DOES NOT HAVE REAl. ROOTS" 200 GOTO 280 210 LET 0 ; SQR(D) 220 IF B >; 0 THEN 250 230 LET Rl ; (-8 + D) / (2 * A) 240 GOTO 260 250 LET Rl ; (-8 - D) / (2 * A) 260 LET R2 ; C / (A * Rl) 270 PRINT "THE TWO ROOTS ARE "; Rl; "AND "; R2 280 PRINT 290 PRINT "WOULD YOU LIKE ANOTHER RUN? (YES/NO)" 300 INPUT Q$ 310 IF Q$ ; "YES" THEN 50 320 IF Q$ ; "NO" THEN 350 330 PRINT 340 GOTO 350 PRINT 360 END

"REPLY'''; Q$; ". NOT UNDER~TOOD. PLEASE ANSWER YES OR NO" 280 "PLANTS HAVE ROOTS TOO!"

21

Page 30: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

TRIAL RUN

PROGRAM TO SOLVE QUADRATIC EQUATIONS

EG. A*X**2 + B*X + C = 0

TYPE IN THE THREE COEFFICIENTS A,B,C WITH A COMMA BETWEEN TERMS. THEN PRESS RETURN. ? .5,-2.5,3

THE TWO ROOTS ARE 3 AND 2

WOULD YOU LIKE ANOTHER RUN? (YES/NO) ? YES

TYPE IN THE THREE COEFFICIENTS A,B,C WITH A COMMA BETWEEN TERMS. THEN PRESS RETURN. ? 3,-6,-7

THE TWO ROOTS ARE 2.82574 AND -.825742

WOULD YOU LIKE ~NOTHER RUN? (YES/NO) ? YES

TYPE IN THE THREE COEFFICIENTS A,B,C WITH A COMMA BETWEEN TERMS. THEN PRESS RETURN. ? 1,-2,1 BO'I'H ROOTS ARE EQUAL, AND HAVE THE VALUE:

WOULD YOU LIKE ANOTHER RUN? (YES/NO) ? YES

TYPE IN THE THREE COEFFICIENTS ~,B,C WITH A COMMA BETWEEN TERMS. THEN PRESS RETURN. ? 1,2,3

THIS EQUATION DOES NOT HAVE REAL ROOTS

WOULD YOU LIKE ANOTHER RUN? (YES/NO) ? NO PLANTS H~VE ROOTS TOOl

22

Page 31: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

.:'#

4

I)

. ,~

STANDARD DEVIATION

This program calculates the mean (average) of a set of values, and the standard deviation (or spread of results). Optionally the 95% and 99% confidence limits may be evaluated. These limits are calculated as the mean + a given amount, so that 95% or 99% of a normally distributed pop­ulatio; of values will lie within these ranges respectively.

Full instructions are provided by the program .if required. The data values are typed in one at a time, and terminated by a dummy value of 999999. The number of data values and their mean value are printed. The standard deviation is then calculated using the equation:

standard deviation

1: (difference between each data value and mean value)2 number of terms - 1

If there are 30 or more data points, the denominator in the above express­ion is replaced by the number of terms.

Confidence limits are calculated using the equation :

confidence limit

mean + x standard deviation

value - I number of terms

For up to 30 pOints, exact values of tjlnumber of terms obtained from statistical tables are stored in arrays. For more than 30 points a slight approximation is made using a value for t (infinity).

Finally the USer is asked whether he would like to re-run the program, or finish .

23

Page 32: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

10 DIM X(100), T(30), B(30), P$(8), Q$(8) 20 MAT READ T 30 DATA 0,8.984, 2.484, 1.591, 1.241, 1.05, .925, .836, .769, .715 40 DATA .672, .635, .604, .577, .554, .533, .514, .497, .482, .468 50 DATA .455, .445, .434, .423, .413, .404, .395, .388, .38, .373 60 MAT READ B 70 DATA 0, 45, 5.73, 2.921, 2.061, 1.646, 1.401, 1.237, 1.118, 1.028 80 DATA .955, .897, .847, .805, .769, .737, .708, .683, .66, .64 90 DA'rA .621, .604, .589, .574, .561, .547, .536, .524, .514, .504 100 PRINT "STANDARD DEVIATION CALCULATION" 113 PRINT 11=::::====== ==::::==::::=:::::::: ===========" 120 PRINT 130 PRINT "WOULD YOU LIKE FULL INSTRUCTIONS?" 140 GOSUB 890 150 LET Q$ = P$ 160 PRINT 170 IF Q$ = "YES" THEN 200 180 PRINT" INPUT DATA" 190 GOTO 220 200 PRINT "TYPE IN DATA VALUES. PRESS RETURN AFTER EACH TERM." 210 PRINT "TERMINATE DATA WITH THE VALUE 999999" 220 LET N = 0 230 LET A = 0 240 FOR I = 1 TO 100 250 INPUT X (I) 260 IF X(I) 999999 THEN 330 270 LET A = A + X(I) 280 LET N = N + 1 290 NEXT I 300 PRINT 310 PRINT "PROGRAM CAN ONLY HANDLE 100 VALUES." 320 REM ABANDON RUN IF LESS THAN 2 TERMS 330 PRINT 340 IF N )= 2 THEN 390 350 PRINT "ATTEMPT TO RUN WITH THIS DATA ABANDONED" 360 PRINT "BECAUSE THERE ARE NOT ENOUGH TERMS." 370 GOTO 710 380 REM WORK OUT AVERAGE VALUE 390 LET Al = A / N 400 PRINT "NUMBER OF READINGS =", N 410 PRINT 420 PRINT "AVERAGE VALUE =", Al 430 REM WORK OUT THE DIFFERENCES SQUARED BETWEEN EACH MARK AND AVERAGE, 440 REM AND COLLECT TOTAL IN D. 450 LET D = 0 460 FOR I = 1 TO N 470 LET Dl = X(I) - Al 480 LET D = D + (D1 * Dl) 490 NEXT I 500 REM WORK OUT STANDARD DEVIATION (S) 510 LET S = SQR(D / (N - 1» 520 IF N < 30 THEN 540 530 LET S = SQR(D / N) 540 PRINT 550 PRINT "STANDARD DEVIA'rION =", S

24

~I

~)

1,),

Page 33: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

'< )

.{

!!(

1l\'

~ 560 570

(;\ 580 590 600 610

."/ 620 630 640 650

,'I 660 670 680 690 ,.,. 700

11i

710 720 730 740 , 750 760

itl 770 780 790 800

!I( 810 820 830 840

r"'l 850 860 870 880

l,1 890 900 910 920

" 930 940 950

!It, 960 970

\

,~~

, ,'(

\.'\,

<I1t

REM DECIDE WHETHER TO WORK OUT CONFIDENCE LIMITS PRINT PRINT "WOULD YOU LIKE THE CONFIDENCE LIMITS?" GOSUB 880 IF P$ = "NO" THEN 710 IF N > 30 THEN 650 LET C = T (N) • S LET Cl = B(N) • S GOTO 670 LET C = 1.96 • S I SOR(N) LET Cl = 2.576 • S I SOR(N) PRINT PRINT "95% CONFIDENCE LIMIT RANGE IS": Al - C: "TO": Al + C PRINT PRINT "99% CONFIDENCE LIMIT RANGE IS"; Al - Cl; "TO"; Al + Cl PRINT PRINT "WOULD YOU LIKE ANOTHER RUN?" GOSUB 880 IF p$ = "NO" THEN 820 PRIN'!' "TYPE IN A NEW SET OF DATA" PRINT ";;;; == = === === == ====" IF 0$ = "NO" THEN 220 PRINT "(TERMINATE DATA WITH 999999)" LET 0$ = "NO" GOTO 220 REM TERMINATE JOB IF 0$ = "NO" THEN 850 PRINT "'THERE ARE LIES, DAMNED LIES - AND STATISTICS· ••• DISRAELI" PRINT PRINT "JOB COMPLETED SUCCESSFULLY." STOP REM •• -SUBROUTINE TO CHECK REPLIES IF 0$ = "NO· THEN 910 PRINT "TYPE YES ,OR NO, THEN PRESS RETURN." PRINT INPUT P$ IF p$ = ·YES" THEN 960 IF P$ = "NO· THEN 960 PRINT "REPLY''': P$: •• NOT UNDERSTOOD. PLEASE ANSWER YES OR NO" GOTO 900 RETURN END

25

Page 34: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

TRIAL RUN

STANDARD DEVIATION CALCULATION

WOULD YOU LIKE FULL INSTRUCTIONS? TYPE YES OR NO. THEN PRESS RETURN. ? YES

TYPE IN DATA VALUES. PRESS RETURN AFTER EACH TERM. TERMINATE DATA WITH THE VALUE 999999 ? 4.4 ? 4.9 ? 5.9 ? 5.3 ? 4.6 ? 5.1 ? 5.8 ? 999999

NUMBER bF READINGS = 7

AVERAGE VALUE = 5.14286

STANDARD DEVIATION = .568205

WOULD YOU LIKE THE CONFIDENCE LIMITS? TYPE YES OR NO. THEN PRESS RETURN. ? YES

95% CONFIDENCE LIMIT RANGE IS 4.61727 TO 5.66845

99% CONFIDENCE LIMIT RANGE IS 4.3468 TO 5.93891

WOULD YOU LIKE ANOTHER RUN? ? NO JOB COMPLETED SUCCESSFULLY.

26

Page 35: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

I~

"iI(

LIFE

Life is a simulation of a hypothetical colony of living cells, which may survive, reproduce or die according to a series of mathematical rules. The original idea came from John Conway, and was subsequently p\lblished in Scientific American (October 1970).

In this version the user places any number of cells" each occupying one square, on a 30 by 30 board. Except at the edge of the board, each square is surrounded by eight other squares, and the survival or reproduct­ion of a cell depend on the extent to which the eight surrounding squares are occupied by other cells. A cell survives to the next generation if, and only if two or three of the eight surrounding squares also contain a cell. If only one or none of these squares are occupied, then the cell dies of isolation. If four or more of the squares are occupied, the cell dies through over:: population and lack of food. Any empty square which has exactly three of its eight neighbours occupied by cells will have a new cell born in it for the next generation. Births and deaths occur Simultaneously, and after each generation the new board is printed out. The program continues auto­matically either until all the cells ~ve died (or dropped off the edge of the board), or until the specified number of generations has been reached. Sometimes a completely stable configuration is attained, and often a Simple cycle of repeating patterns is formed (continue the trial run for a few more generations). These, rules are illustrated by considering the fate of the middle square in the following examples:

death from isolation

death from overcrowding

survival birth of new cell

In the second version of life, two different types of cells * and $ compete for the available space, until one type eliminates the other. The same basic rules apply, with the following modifications:

(i) the board size is 6 x 6

(ii)

(iii)

(iv)

only three starting cells of each type are permitted

after each generation, users add one new cell of each type to the board.

the type of new cell born depends on which type of parent cell is the most numerous, for example'a cell born from parents $$$ or$$. would be a $.

27

Page 36: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

Ie DIM A(16), B(30, 30) 20 PRINT TAB (20): "LIFE" 30 PRINT TAB(2e): "====" 40 PRINT 50 PRINT "INPUT THE NUMBER OF GENERATIONS" 60 INPUT G 70 IF (G - 1) * (G - 100) > 0 THEN 90 80 IF INT(G) = G THEN 110 90 PRINT "NUMBER MUST BE AN INTEGER BETWEEN 1 AND 100" 100 GOTO 50 110 MAT B = ZER 120 MAT READ A 130 DATA 1, 0, 0, I, -I, 0, 0, -I, I, I, I, -1, -I, I, -I, 1411 PRINT "INPUT CO-ORDINATES OF STARTING ORGANISMS" 150 PRINT "TERMINATE WITH ANY POINT OUT OF RANGE 1 - 30, 1 160 INPUT x, Y 170 IF (X - 3e) * (X - 1) > 0 THEN 24e 180 IF (Y - 30) * (Y - 1) > 0 THEN 240 190 IF INT(X) + INT(Y) = X + Y THEN 2211 200 PRINT "COORDINATES MUST BE WHOLE NUMBERS" 210 GOTO 1611 2211 LET B(31 - Y, X) 2311 GOTO 1611 240 FOR K = 0 TO G 250 FOR X = 1 TO 30 260 FOR Y = 1 TO 30 270 IF B(X, Y) < 111 THEN 3411 2811 FOR Z = 1 TO 15 STEP 2 290 LET Xl = X + A(Z) 31111 LET YI = Y + A(Z + 1) 310 IF Xl * (Xl - 31) * Y1 * (YI - 31) II THEN 3311 3211 LET B(XI, Yl) = B(X1, Yl) + 1 3311 NEXT Z 3411 NEXT Y 3511 NEXT X 3611 LET C II 370 FOR X 1 TO 30 3811 FOR Y = 1 TO 311 3911 LET J = B(X, Y) 41111 IF (J - 13) * (J - 12) * (J - 3) <> II THEN 4711 4111 LET B(X, Y) = 111 420 LET C = C + 1 430 LET L = X 4411 IF C > 1 THEN 480 4511 LET D = X 4611 GOTO 4811 470 LET B(X, Y) = II 480 NEXT Y 4911 NEXT X 5110 PRINT "GENERATION": K, "POPULATION =": C 510 PRINT 5211 IF C = II THEN 6411 530 FOR X = D TO L 5411 FOR Y = 1 TO 30 5511 IF B(X, Y) = II THEN 570

28

.i,i

tl

"1\1

1i~

i~

1~

f.;l

~

-1 1)1,

- 30"

~"

~)I

~jl

'I'le'

~'

t)

t~

i~)

1,-;

4~

~i

~

Page 37: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

560 PRINT TAB(YII "*"1 570 NEXT Y 580 PRINT 590 NEXT X 608 PRINT 610 IF C > 3 THEN 630 620 LET G = K + 2 630 NEXT K 640 PRINT "HOPE YOU LIVE LONG I 650 END

TRIAL RUN

LIFE

INPUT THE NUMBER OF GENERATIONS ? 3

END OF JOB"

INPUT CO-ORDINATES OF STARTING ORGANISMS TERMINATE WITH ANY POINT OUT OF RANGE 1 - 30, 1 - 30 ? 15,15 ? 15,16 ? 15,14 ? 14,15 ? 16,15 ? 99,99 GENERATION 0 POPULATION 5

GENERATION 1

GENERATION 2

GENERATION 3

* *** * POPULATION

*** • • ***

POPULATION

* * *

* * * * * POPULATION

*** ** **

*** *

B

B

12

29

Page 38: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

10 DIM A(16), B(6, 6), C(18), 0$(8), X(2), Y(2) 20 PRINT TAB(S); "COMPETING SPECIES VERSION OF LIFE" 38 PRINT TAB(S); a;;;;;;;;; ;;;;;;; ;;;;;;; ;; ;;;;"

48 PRINT 50 MAT READ A 68 DATA -1, 0, 1, 0, 0, -1, 8, 1, -1, -1, 1, -1, -1, 1, 1, 711 MAT READ C S0 DATA 3, 102, 103, 120, 130, 121, 112, Ill, 12 98 DATA 21, 30, 1020, 1030, 1011, 1021, 1003, 11102, 1012 1110 LET G ; 0 110 MAT B; ZER 1211 PRINT "EACH PLAYER MUST TYPE IN THE COORDINATES OF HIS THREE "; 130 PRINT "STARTING ORGANISMS" 140 FOR I ; 1 TO 3 150 FOR N ; 1 TO 2 160 PRINT "PLAYERn : N; II X,Y n;

170 GOSUB 990 IS0 NEXT N 190 NEXT I 200 PRINT 210 REM *** PRINT BOARD 220 GOSUB 850 230 LET G G + 1 240 PRINT 250 FOR I ; 1 TO 6 260 FOR J 1 TO 6 270 IF B(I, J) (; 99 THEN 360 2S0 LET N ; 1 290 IF B(I, J) (; 999 THEN 310 300 LET N 10 310 FOR L ; 1 TO 15 STEP 2 320 IF (I + A(L» * (I + A(L) - 7) ; 0 THEN 350 330 IF (J + A(L + 1» * (J + A(L + 1) - 7) ; 0 THEN 350 340 LET B( (I "!" A(L», (J + A(L + 1») ; B( (I + A(L», (J + A(L + 1») +N 350 NEXT L 360 NEXT J 370 NEXT I 3S0 LET Tl ; T2 ; II 390 FOR I ; 1 TO 6 400 FOR J ; 1 TO 6 410 FOR K ; 1 TO 18 420 IF B(I, J) ; C(K) THEN 460 430 NEXT K 440 LET B(I, J) ; 0 450 GOTO 520 460 IF K (; 9 THEN 500 470 LET B(I, J) 1000 4S0 LET T2 ; T2 + 1 490 GOTO 520 500 LET B(I, J) 100 510 LET Tl ; Tl + 1 520 NEXT J 530 NEXT "I 540 PRINT "GENERATION"; G; TAB(20); "* POPULATION ;"; T2 550 PRINT TAB(20); "$ POPULATION ;"; Tl

30

-t)'

Page 39: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

,(

I " :1

569 REM *** PRINT BOARD 570 GOSUB 859 580 IF T1 + T2.= 0 THEN 729 599 IF T2 <> 9'THEN 629 699 LET N = 1 619 GOTC 740 620 IF T1 <> 0 THEN 650 630 LET N = 2 640 GOTO 740 659 PRINT 660 FOR N = 1 TO 2 670 PRINT "PLAYER", N, "TYPE COORDINATES OF NEW ORGANISM. ", 680 GOSUB 999 690 NEXT N 700 GOTO 200 710 REM *** END OF GAME 720 PRINT "GAME DRAWN." 730 GOTO 75~ 740 PRINT "GAME WON BY PLAYER", N 750 PRINT "WOULD YOU LIKE ANOTHER GAME?" 760 PRINT "TYPE YES OR NO & PRESS RETURN." 770 INPUT 0$ 780 IF 0$ = "YES" THEN 100 790 IF 0$ = "NO" THEN 820 800 PRINT "REPLY''', OS, '" NOT UNDERSTOOD. PLEASE ANSWER YES OR NO" 810 GOTO 779 820 PRINT "GOODBYE" 830 GOTO 1210 840 REM *** SUBROUTINE TO PRINT BOARD 850 FOR I = 6 TO 1 STEP -1 860 PRINT I, 870 FOR J = 1 TO 6 880 IF B(I, J) = 0 THEN 930 890 IF B(I, J) = 1000 THEN 920 900 PRINT TAB(3 * J), " S", 910 GOTO 930 920 PRINT TAB(3 * J), " *", 930 NEXT J 940 PRIN1" 950 NEXT I 96~ PRINT "2 6" 970 RETURN 980 REM *** SUBROUTINE oro INPUT COORDINATES & CHECK THAT THEY ARE VALID 990 INPUT YeN), X(N) 1009 IF X(N) ( 1 THEN 1040 lB19 IF X (N) > 6 THEN 1940 1020 IF Y (N) ( 1 THEN 1040 1030 IF YIN) (= 6 THEN 1070 1040 PRINT "COORDINATES OFF THE BOARDI" 1050 PRIN'r "RETYPE CORRECTLY IN THE RIINGE 1-6" 1060 GOTO 990 1070 IF INT(X(N» + INT(Y(N» = X(N) + YIN) THEN 110~

1080 PRINT "COORDINATES MUST BE WHOLE NUMBERS. RETYPE CORRECTLY." 1090 GOTC 990 1109 IF N = 1 THEN 1160

31

Page 40: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

1110 IF ABS(X(l) - X(2» + ABS(Y(l) - Y(2» > 0 THEN 1160 1120 PRINT "BOTH PLAYERS HAVE CHOSEN THE SAME SQUARE"; Y(l); X(l) 1130 PRINT "THESE TWO ORGANISMS HAVE INSTANTLY EXTERMINATED EACH OTHER" 1140 LET B(X(l), Y(l» = 0 1150 GOTO 1200 1160 IF B(X(N), yeN»~ = 0 THEN 1190 1170 PRINT "THIS SQUARE IS ALREADY OCCUPIED" 1180 GOTO 990 1190 LET B(X(N), YeN»~ = N * 900 - 800 1200 RETURN 1219 END

TRIAL RUN

COMPETING SPECIES VERSION OF LIFE

EACH PLAYER MUST TYPE IN THE COORDINATES OF PLAYER 1 X,Y ? 4,3 PLAYER 2 X,Y ? 2,5 PLAYER 1 X,Y ? 4,2 PLAYER 2 X,Y ? 2,4 PLAYER 1 X,Y ? 4,4 PLAYER 2 X,Y ? 2,3

6 5 * 4 * $ 3 * $ 2 $ 1 0 1 2 5 6

GENERATION 1 * POPULATION 3 $ POPULATION 3

6 5 * 4 * * 3 $ '\ 2 $ 1 0 3 5 6

PLAYER 1 TYPE COORDINATES CF NEW ORGANISM. PLAYER 2 TYPE COORDINATES OF NEW ORGANISM.

6 * $ 5 * 4 * 3 $ $ 2 $ 1 0 2 4 5 6

32

? ?

HIS THREE STARTING ORGANISMS

6,6 4,6

{sl

Page 41: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

'1,

,.~

."

'I

PREY

Consider the competition between two species in an isolated environ­ment (for example" cats and mice or ants and aphids). Such species will interact, usually to the greater benefit of one of the species. The system will eventually stclill.ize either with constant numbers of each species or with one having been eliminated.

where

The Latka-Volterra equations can be used:

(dNldt) = (RiNlKi)(Ki-N(JiNm)

m=2ifi=1 orm=1 ifi=2

Ni = number of species i at time t

Ri = birth rate per individual per unit time

(1)

Ki = maximum number of species i that the isolated environ-ment can support

Ji = an interaction coefficient which is : positive if the species is preyed upon, negative if there are beneficial effects

Equation (1) may be solved by Euler's Method for a first-oider differential equation dt/dx = f(x, y). At some point i, Yi + 1 = Yi + t\x' (dy /dx)i' If y is known at some point i (e. g. the origin), the value at the next point (i + 1) can be calculated from the old value of (dy/dxh. A better approach is the Modified Euler's Method, which uses the old value of Yi and the Euler estimate of Yi + 1 to find the average value of y (y ) in this interval: av

It is easy to show, using the mean value theorem, that a better approx-imation to y i + 1 is obtained from : .

Yi + 1 = Yi + t\x(dy /dx)av

where (dy /dx~v is calculated from Yav and the mean value of x.

This method is used in our program PREY and is described in J. R. Merrill, "Using Computers in PhYSiCS", Houghton-Mifflin, Boston, 1976, pages 3 and 225. You "observe" the system for a specified time and a table and a graph of the results for ten equal time intervals are printed. The average values of the slopes are computed as Ul and U2 -the numbers of each species are stored in N(l) and N(2).

33

Page 42: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

10 DIM A(ll), B(ll), J(2), K(2), M(2), N(2), Q(11), R(2), T$ (IB) 20 PRINT TAB(4): "PREDATOR-PREY SIMULATION" 39 PRINT TAB(4): "======== ==== ==========" 40 FOR I = 1 TO 2 50 PRINT "HOW MANY OF SPECIES": I: 60 INPUT N (I) 70 IF N(I) )= 2 THEN 100 ~0 PRINT "YOU MUST HAVE AT LEAST 2 TO BREED I TRY AGAIN"

90 GOTO 50 100 IF N(I) (= 30000 THEN 130 110 PRINT "THERE CAN NOT BE MORE THAN 30000 OF THIS SPECIES" 120 GOTO 50 130 IF N(I) = INT(N(I» THEN 160 140 PRINT "NOT A WHOLE NUMBER OF ANIMALS. RETYPE CORRECTLY" 150 GOTO 50

'"l60 PRINT "HOW MANY OF THIS SPECIES CAN THE REGION SUPPORT": 170 INPUT K(I) 180 IF K(I) )= N(I) THEN 210 190 PRINT "YOU NEED SPACE FOR AT LEAST THE INITIAL NUMBER" 200 GOTO 260 210 IF K(I) (= 39000 THEN 240 220 PRINT "REGION CAN NOT SUPPORT-MORE THAN 30000" 230 GOTO 260 240 IF K(I) = INT(K(I» THEN 280 250 PRINT "NOT A WHOLE NUMBER OF ANIMALS I" 269 PRINT "RETYPE CORRECTLY" 270 GOTO 170 280 PRINT "WHAT IS THE BIRTH RATE PER ANIMAL PER UNIT TIME": 290 INPUT R(I) 300 IF R(I) > 0 THEN 330 310 PRINT "IT MUST BE MORE THAN THAT. TRY AGAIN": 320 GOTO 280 330 IF R(I) (= 100 THEN 360 340 PRINT "TYPE A POSITIVE VALUE UP TO 100" 350 GOTO 280 360 PRINT "VALUE OF INTERACTION COEFFICIENT (POSITIVE IF" 370 PRINT "ENVIRONMENTAL EFFECTS ARE ADVERSE, NEGATIVE OTHERWISE)": 380 INPUT J(I) 390 IF ABS(J(I» (= 100 THEN 420 400 PRINT "TYPE AN INTERACTION COEFFICIENT IN THE RANGE -100 TO +100" 410 GOTO 380 420 LET M(I) = 0 430 NEXT I 440 PRINT 450 PRINT "FOR HOW MANY TIME UNITS WILL YOU OBSERVE THE SYSTEM": 460 INPUT T 470 IF T > 0 THEN 500 480 PRINT "DON'T BE SILLY" 490 GOTO 450 500 PRINT 510 LET D = .01 520 LET T2 = T ! 10 530 LET S = INT(T ! .1) 540 PRINT "TIME", "NO. SPECIES 1", "NO. SPECIES 2" 550 FOR Q = 1 TO 11

34

()

Page 43: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

,t

.-,

\~

560 IF Q = 1 THEN 730 570 FOR P = 1 TO S 580 LET U1 = N(l) + R(l)*N(l)*«K(l) - N(l) - J(1)*N(2» I K(l»*D II 2 590 LET U2 = N(2) + R(2)*N(2)*«K(2) - N(2) - J(2)*N(1» I K(2»*D 2 600 LET N(l) = N(l) + R(l) * Ul * «K(l) - Ul - J(l) * U2) I K(l» * D 610 IF N (1) > 1 THEN 630 620 LET N(l) = 1 630 LET N(2) = N(2) + R(2) * U2 * «K(2) - U2 - J(2) * Ul) I K(2» * D 640 IF N(2) > 1 THEN 660 650 LET N(l) = 1 660 NEXT P 670 REM - THIS SECTION CHECKS MAX. VALUE 680 FOR I = 1 TO 2 690 IF N(I) C= M(I) THEN 710 700 LET M(I) = INT(N(I) + .5) 710 NEXT I 720 REM-STORE RESULTS FOR LATER PLOTTING 730 LET A(Q) INT(N(I) + .5) 740 LET B(Q) = INT(N(2) + .5) 750 PRINT (Q - 1) * T2, A(Q), B(Q) 760 NEXT Q 770 LET T$ = "+---------" 780 PRINT 790 PRINT "TIME"; TAB(8); "0"; TAB(22); "NO. OF SPECIES 1"; TAB(56) ;M(l) 800 PRINT TAB(8); T$; T$; T$; T$; T$; "+" 810 FOR Q = 1 TO 11 820 LET T = (Q - 1) * T2 830 PRINT T; TAB(8); "1"; 840 LET SI = INT( (A(Q) I M(l» • 50) + 8 850 LET S2 = INT«B(Q) I M(2» * 50) + 8 860 IF Sl > S2 THEN 890 870 PRINT TAB(Sl); "I"; TAB(S2); "2" 880 GOTO 900 890 PRINT TAB(S2); "2"; TAB(Sl); "1" 900 NEXT Q 910 PRINT TAB(8); T$; T$; T$; T$; T$; "+" 920 PRINT TAB(S); "0"; TAB(22); "NO. OF SPECIES 2"; TAB(56); M(2) 930 PRINT 940 PRINT "WOULD YOU LIKE ANOTHER RUN? (YES/NO)" 950 INPUT T$ 960 PRINT 970 IF T$ = "YES" THEN 40 980 IF T$ = "NO" THEN 1010 990 PRINT "REPLY "'; T$; "' NOT UNDERSTOOD." 1000 GOTO 940 1010 END

35

Page 44: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

TRIAL RUN

PREDATOR-PREY SIMULATION

HOW MANY OF SPECIES 1 ? 58 HOW MANY OF THIS SPECIES CAN THE REGION SUPPORT? 18B8 WHAT IS THE BIRTH RATE PER ANIMAL PER UNIT TIME? 3 VALUE OF INTERACTION COEFFICIENT (POSITIVE IF ENVIRONMENTAL EFFECTS ARE ADVERSE, NEGATIVE OTHERWISE)? 50 HOW MANY OF SPECIES 2 ? 2 HOW MANY OF THIS SPECIES CAN THE REGION SUPPORT? 75 WHAT IS THE BIRTH RATE PER ANIMAL PER UNIT TIME? .5 VALUE OF INTERACTION COEFFICIENT (POSITIVE IF ENVIRONMENTAL EFFECTS ARE ADVERSE, NEGATIVE OTHERWISE)? 8

FOR HOW MANY TIME UNITS WILL YOU OBSERVE THE SYSTEM? 15

TIME 8 1.5 3 4.5 6 7.5 9 18;5 12 13.5 15

TIME

8 1.5 3 4.5 6 7.5 9 10.5 12 13.5 15

NO. SPECIES 1 NO. SPECIES 2 58 2 619 4 654 8 397 15 111 27 5 48 1 53 1 63 1 69 1 72 1 74

8 NO. OF SPECIES 1 654 +---------+---------+---------+---------+---------+ 12 1 121 121 121 I 1 2 11 2 11 2 11 2 11 2 11 2 11 2

+---------+---------+---------+---------+---------+ o NO. OF SPECIES 2 74

WOULD YOU LIKE ANOTHER RUN ? (YES/NO) ? NO

36

if)

Page 45: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

WASTE

Consider a solution of a strong acid entering a river, being mixed with the river water, and causing a decrease in pH downstream; too large a change is very injurious to the ecology of the river. For simplicity, assume a monoprotic acid, HA (e. g. HCI) of initial concentration, M.

Most rivers contain appreciable amounts of atmospheric C02 as H2C03 and its derivatives, and carbonate (as CO ~ -) dissolved from the river bed (e. g. limestone, CaC03). This comprises a buffer system which partially compensates for the addition of the strong acid. There are three important equilibria:

H20 ~H+ + OH- Kw

H2C03~H+ + HCO~ Kl - + 2_

HCO 3 "" H + CO 3 K2

= [H+]

= [H+]

= [H+]

[OH ]

[ HC03] I [H2 CO 3] 2- -

[C0 3 ]/[HC0 3 ]

To these we add the mass balance and electroneutrality equations: - 2-

[H2COj + [HC0 3 ] + [C0 3 ] = AI+A2

[H+]+ 2.Al = [HCO~l + 2[CO~-] + [OH-]+[A-]

where Al is the concentration of dissolved carbonate A2 is the concentration of dissolved atmospheric CO2 and [A -] is the concentration of strong acid after mixing with the river.

+ Now, combine all of these into one single equation in H

[H+]" + {K j + 2.AI -[A-]} [H+]3 + {K j K2 +2.Al.Kj -K j (Al+A2)

-K -fA-]K} [H+]2_{K1 K2(A2+[A-J)+K j Kw}[H+]-K1 KzK =0 w w

The program WASTE solves this by the bisection method, details of which are described later for the program TITR T (following immediately after this program).

Our p!;"ogram uses this approach for various river: effluent flow ratios from I: I to a user defined maximum. Note that in the DATA state­ment we have fixed not only the equilibrium constants but also the values of Al (dissolved carbonate) and A2 (H 2C03 from the atmosphere) at typical values of 6 x 10-" and 3 x 10-3 respectively. You may use more approp­riate local values.

On a programming point: notice that we have modified the poly·' nomial to include factors which are powers of 10. This is important to

prevent "underflow" - can you see how?

37

Page 46: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

18 PRINT "PROGRAM TO SIMULATE ACID WASTE DUMPING" 20 PRINT n======= == ======== ==== ===== =======" 30 PRINT "WHAT IS THE DESIRED PERCENTAGE ACCURACY"; 411 INPUT P 511 IF (P - 1B) * (P - .1) <= II THEN 811 611 PRINT "ACCURACY SHOULD BE BETWEEN 11.1 AND 111%." 7B GOTO 3B 80 PRINT "CONCENTRATION OF ACID ENTERING RIVER (MOL DM-3)"; 90 INPUT W 1HH IF W * (W - 13) < 0 THEN 130 1111 PRINT "CONCENTRATION SHOULD BE > 0 AND < 13 MOL DM-3" 120 GOTO 80 130 PRINT "FINAL MIXING RATIO OF RIVER:WASTE"; 1411 INPUT R 1511 IF (R - 111) * (R - 31111110) <= 0 THEN 1811 1611 PRINT "MIXING RATIO SHOULD BE BETWEEN 10 & 3011110." 1711 GOTO 1311 1811 REM-CONSTANT QUANTITIES STORED IN DATA STATEMENT 190 READ K1, K2, K3, AI, A2 21111 DATA 4.3E-7, 5.6E-11, 1E-14, .11006, .003 2111 PRINT 2211 PRINT "MIXING RATIO", "PH OF RIVER" 230 FOR I = .1 TO 1 STEP .1 2411 LET A = W I (R * I) 2511 LET N = II 2611 LET HI = 0 2711 LET H2 = A 2811 LET D = .1 * (H2 - HI) 2911 LET Xl = HI + D 31111 REM - SCALE THE VARIABLE TO AVOID UNDERFLOW 3111 LET X = Xl * 1111100 3211 LET X2 = X * X 330 LET F = X2 * X2 + 11101111 * (K1 + 2 * Al - A) * X2 * X 3411 LET F = F+(lE8*K1*K2+2E8*A1*K1-1E8*K1*(A1+A2)-lE8*K3-1E8*A*K1)*X2 350 LET F = F - (lE12*K1*K2*(2*A2+A) + 1E12*K1*K3)*X - 1E16*K1*K2*K3 3611 LET N = N + 1 3711 IF N > 100 THEN 46B 38B IF F > 0 THEN 420 3911 LET HI = Xl 4011 LET D = D * 2 4111 GOTO 2911 4211 LET H2 = Xl 4311 LET D = D I 2 4411 LET PI = ABS«(H2 - HI) I H2) * 100) 450 IF PI > P THEN 2911 46B LET H = -LOG(X1) I 2.303 4711 PRINT TAB(3); R * I; ": 1", INT(H * 100 + .5) I 190 480 IF N <= 11111 THEN 500 4911 PRINT "PH IN ABOVE LINE IS UNRELIABLE DESPITE 11111 ITERATIONS" 51111 NEXT I 510 END

38

Page 47: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

TRIAL RUN

PROGRAM TO SIMULATE ACID WASTE DUMPING

WHAT IS THE DESIRED PERCENTAGE ACCURACY? e.I CONCENTRATION OF ACID ENTERING RIVER (MOL DM-3)? I.e FINAL MIXING RATIO OF RIVER:WASTE? I0eee

MIXING RATIO I0ee 1 2ee0 1 30ee 1 40ee 1 5000 1 60ee 1 70ee 1 Be0e 1 geee : 1 ueee : 1

PH OF RIVER 5.15 5.75 5.B7 5.92 5.95 5.97 5.9B 5.99 6 6.U

39

/

Page 48: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

TI TR E

This program simulates a titration between a monoprotic or diprotic acid and a base. The 'strength' of the acid is given by its dissociation constant(s). The effect of varying the acid 'strength' may be examined, and the simulation may help in the correct choice of an indicator. After each successive addition of base, the computer reports the pH of the resultant solution.

and

For a diprotic acid the equations are:

~ H++HA­

#- W+A2-

# W+OH- K w

[HA -); [H2A)

[A 2 -JI [HA-)

[OW)

From considerations of mass balance and electroneutrality :

[H2A)T= [HzA)+ [HA-)+[A 2-)

[M+] + [H+ ) = [OH-) + [HA -] + 2 [A 2_)

where T· subscript means "total" and M+ is the cation belonging to the strorig basic titrant, MOH (e. g. NaOH). Combining all of these equations gives:

W]4+(Kl +Bl)[W]3+{KdK2 +BI-AI)-Kw}[W)2 +{K1 K2 (BI -2AI)-

K1 Kw }[H+]-KjK2 Kw=O

where Al and Bl are the total concentrations of acid and base respectively. This is also applicable to a monoprotic acid if we make K2very small (e. g. 10-16 ); [IfI1can be calculated numerically by the bisection method:

(a)

(b)

(c)

(d)

(e)

The minimum possible [ W] is set equal to HI (initially this is zero). The maximum possible [W] is set to H2 (initially equal to [H0).

The range HI to H2 is divided into ten intervals and the polynomial is evaluated at each of these points.

If the polynomial value increases at any point, the width of the interval is doubled and (b) is repeated.

When the value of the polynomial changes from negative to positive we are straddling the correct value of [W) so these two points are used as the new values of HI and H2, and the width of the interval is halved.

When the gap between HI and H2 has fallen below the required precision (0.1%) the pH is calculated as - 10glO(XI).

For a given set of parameters see if you can make the correct additions of base in order to exactly calculate the end point(s).

40

Page 49: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

.J

'" 10 DIM K (2), A$ (9) 20 PRINT "SIMULATION OF TITRATIONS OF MONOPROTIC OR DIPROTIC ACIDS" 30 PRINT "========== ========== == ========== == ======== =====" 40 PRINT 50 REM-PRECISION SET IN NEXT LINE TO 0.1 PERCENT. 60 LET P = .1 70 PRINT "CONCENTRATION OF ACID USED (MOL DM-3}"; 80 INPUT A 90 IF (A - I) * (A - .001) <= 0 THEN 130 100 PRINT "CONCENTRATION SHOULD BE IN THE RANGE 0.001 TO 1.0"; 110 PRINT" RETYPE CORRECTLY." 120 GOTO 80 130 PRINT "VOLUME OF ACID (CM 3}"; 140 INPUT V 150 IF V * (V - 100.1) < 0 THEN 180 160 PRINT "VOLUME SHOULD BE > 0 AND 100.1 - RETYPE CORRECTLY" 170 GOTO 140 180 PRINT "TYPE 1 OR 2 FOR THE NO. OF DISSOCIATION CONSTANTS"; 190 INPUT Z 200 IF (Z - I) * (Z - 2) = 0 THEN 230 210 PRINT "RE-"; 220 GOTO 180 230 LET K(l) = lE-16 240 LET K(2) = lE-16 250 FOR J = 1 TO Z 260 PRINT "DISSOCIA'I"ION CONS'I'ANT NO. "; J; 270 INPUT K(J} 280 IF (K(J) - lE-10) * (K(J) - I} <= 0 THEN 320 290 PRINT "DISSOCIATION CONSTANTS MUST BE IN THE RANGE lE-10 TO 1. 0" 300 PRINT "RETYPE "; 31~ GOTO 260 320 NEXT J 330 PRINT 340 PRINT "CONCENTRATION OF BASE USED (MOL DM-3}"; 350 INPUT B 360 IF (B - I) * (B - .001) c= 0 THEN 390 , 370 PRINT "CONC. SHOULD BE IN THE RANGE 0.001 TO 1.0 - RETYPE CORRECTLY· 380 GOTO 350 390 IF (A I B - 5) * (A I B - .2) c= 0 THEN 430 400 PRINT "BASE CONCENTRATION DIFFERS BY MORE THAN A FACTOR OF FIVE" 410 PRINT "FROM THE ACID CONCENTRATION. RETYPE A SENSIBLE VALUE." 420 GOTO 350 430 LET Cl = 0 440 PRINT "VOLUME OF ADDED BASE (CM 3}"; 450 INPUT C 460 IF C * (C - 100.1)·c 0 THEN 490 470 PRINT "VOLUME SHOULD BE > 0 AND C 100.1 - RETYPE CORRECTLY." 480 GOTO 450 490 LET Cl = Cl + C 500 LET K = lE-14 510 LET Al = V * A I (V + Cl) 520 LET Bl = Cl * B I (V + Cl) 530 LET N = 0 540 LET HI 0 550 LET H2 = A

41

Page 50: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

560 LET D = .1 * (H2 - HI) 570 LET Xl = HI + D 580 REM-SCALE THE VARIABLE TO AVOID UNDERFLOW 590 LET X = Xl * 10000 600 LET X2 = X * X 610 LET X3 = X2 * X 620 LET X4 ~ X2 * X2 630 LET F = X4 + 10000*(K(l)+B1)*X3 + (lE8*K(l)*(K(2)+B1-A1)-lE8*K)*X2 640 LET F = F+(lE12*K(l)*K(2)*(Bl-2*A1)-lE12*K(l)*K)*X-1E16*K(l)*K(2)*K 650 LET N = N + 1 660 IF N > 100 THEN 750 679 IF F > 0 THEN 710 689 LET HI = Xl 699 LET D = D * 2 700 GOTO 579 710 LET H2 = Xl 720 LET D = D I 2 730 LET PI = ABS«(H2 - HI) I H2) * 100) 749 IF PI > P THEN 570 750 LET H = INT(-LOG(X1) I 2.303 • 100 + .5) I 100 760 PRINT "TOTAL VOL. BASE ="; CI; " PH ="; H; " ITERATIONS ="; N 770 IF N <= 100 THEN 800 780 PRINT "REQUIRED PRECISION NOT OBTAINED WITH 100 ITERATIONS." 790 PRINT "THE ABOVE VALUE IS UNRELIABLE OR AT THE BEST APPROXIMATE." 800 IF H <= 9 THEN 820 810 PRINT "···PH APPROACHING A VALUE THAT MAY PRODUCE UNDERFLOW···" 820 PRINT 830 PRINT "CONTINUE (YES OR NO)"; 840 INPUT A$ 850 IF A$ = "YES" THEN 440 860 I~ A$ = "NO" THEN 890 870 ~RINT "REPLY'''; A$; "' NOT UNDERSTOOD." 880 GOTO 830 890 END

42

"l,I:

Page 51: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

ti'

TRIAL RUN

SIMULATION OF TITRATIONS OF MONOPROTIC OR DIPROTIC ACIDS

CONCENTRATION OF ACID USED (MOL DM-3)? B.1B1 VOLUME OF ACID (CM 3)? 25.BB TYPE 1 OR 2 FOR THE NO. OF DISSOCIATION CONSTANTS? 1 DISSOCIATION CONSTANT NO.1? 8.6

CONCENTRATION OF BASE USED (MOL DM-3)? B.B995 VOLUME OF ADDED BASE (CM 3)? 2B.BB TOTAL VOL. BASE = 2B PH = 1.96 ITERATIONS 23

CONTINUE (YES OR NO)? YES VOLUME OF ADDED BASE (CM 3)? 4. Be TOTAL VOL. BASE = 24 PH = 2.59 ITERATIONS = 19

CONTINUE (YES OR NO)? YES VOLUME OF ADDED BASE (CM 3)? 1.88 TOTAL VOL. BASE = 25 PH = 3.16 ITERATIONS 23

CONTINUE (YES OR NO)? YES VOLUME OF ADDED BASE (CM 3)? 0.20 TOTAL VOL. BASE = 25.2 PH = 3.49 ITERATIONS 24

CONTINUE (YES OR NO)? YES VOLUME OF ADDED BASE (CM 3)? 8.10 TOTAL VOL. BASE = 25.3 PH = 3.85 ITERATIONS =·32

CONTINUE (YES OR NO)? YES VOLUME OF ADDED BASE (CM 3)1 0.06 TOTAL VOL. BASE = 25.36 PH = 4.51 ITERATIONS 34

CONTINUE (YES OR NO)? YES VOLUME OF ADDED BASE (CM 3)? 0.B3 TOTAL VOL. BASE = 25.39 PH = 9.41 ITERATIONS = 44 ***PH APPROACHING A VALUE THAT MAY PRODUCE UNDERFLOW***

CONTINUE (YES OR NO)? NO

43

Page 52: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

ENDPOINT

In this procedure it is usual to record pH (or other signal) as a function of the added volume, V, of titrant. The "end point(s)" are assoc­iated with a sharp increase in pH. This is eaSier to see if one plots the rate of change in pH, (d(PH)/dV), and looks for the maximum value(s) of this function. It is usual to approximate d(pH)/dV by l>(pH)/ l>V where l> pH indicates the change in pH for a finite change in V. This value is associat­ed with the mean value in V during this change.

The technique used in this program is to find when d(pH)/dV exceeds a "threshold" value and then to use three successive derivative values about the approximate end point to find an accurate value by simple i.nter­polation:

[ abs (d2(pH)/d y2) 1 ]

abs {(d2(pH)/d y2 h - (d2(pH)/dy2) 2 }

Both the single derivatives, d(pH)/dV, and the second derivatives, d~pH)/d~ are calculated by a "successive difference" method. Notice that the first and second values of d 2(pH)/dV2 are associated with the second and third values of V in the procedure used.

If you expected to see a certain number of end-points and the program, in fact, detects fewer, this could be due to using too large a value of the "threshold" and you should run the program again.

On a point of programming efficiency - note that the program only stores up to four successive pairs of data, so that the number of pairs can be as large as you wish - or have the patience to type:

44

Page 53: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

.",

18 DIM F(2), V(4), P(4), D(3), Y(3), Z(3), E(IIl) 20 PRINT "PROGRAM TO DETECT END POINTS IN POTENTIOMETRIC TITRATIONS" 311 PRINT "*** THIS VERSION ALLOWS UP TO III END POINTS ***" 411 LET N = 0 511 PRINT 611 PRINT "THRESHOLD (USUALLY FROM Il TO I)"; 70 INPUT T 811 PRIN'!' 90 PRINT "TYPE VOLUME AND INSTRUMENT READING (E.G. PH) ONE PAIR PER" 1011 PRINT "LINE. DO THIS WHEN YOU SEE THE MESSAGE 'DATA'. THE" 111l PRINT "VOLUME (ANY UNITS) MUST ALWAYS BE TYPED FIRST." 120 PRINT "SEPARATE THE VALUES WITH A COMMA" 130 PRINT "TO FINISH: TYPE 999,999" 141l PRINT 151l FOR I = 1 TO 4 168 PRINT "DATA"; 171l INPUT V(I), P(I) 180 LET P(I) = ABS(P(I» 1911 IF I = 1 THEN 240 200 IF V(I) > V(I - 1) THEN 238 218 PRINT "VOLUME MUST INCREASE. PLEASE CHECK AND RE-INPUT" 220 GOTO 161l 230 LET D(I - 1) = (P(I) - P(I - 1» / (V(I) - V(I - 1» 240 NEXT I 251l IF (D(2) - D(I» <= T THEN 381l 26B IF (D(2) - D(3» <= T THEN 381l 270 IF N = Il THEN 421l 28B IF (V(2) - E(N» <= Q THEN 3110 291l GOTO 420 3118 FOR J = 1 TO 3 311l. LET V(J) = V(J + 1) 321l LET P(J) = P(J + 1) 330 IF J = 3 THEN 350 3 '.8 LET D(J) = D(J + 1) 350 NEXT J 360 PRINT "DATA"; 370 INPUT V(4), P(4) 380 IF V(4) = 999 THEN 550 391l LET D(3) = (P(4) - P(3» / (V(4) - V(3» 4111l GOTO 250 410 REM-INTERPOLATION SECTION TO DETECT ACCURATE END POINT 421l FOR I = 1 TO 3 430 LET Z(I) = (V(I) + V(t + 1» / 2 440 NEXT I 450 REM-F(1),F(2) ASSOCIATED WITH V(2) ,V(3) ,RESPECTIVELY. 460 FOR I = 1 TO.2 470 LET F(I) = (D(I + 1) - D(I» / (Z(I + 1) - Z(I» 480 NEXT I 490 REM-NOW INTERPOLATE 500 LET N = N + 1 510 LET E(N) = V(3) - (V(3) - V(2» * (ABS(F(2» / ABS(F(l) - F(2») 520 REM-Q SETS A 'BANDWIDTH' TO AVOID SPURIOUS PEAK DETECTION 530 LET Q = 2 * (V(3) - V(l» 540 GOTO 31l1l 550 PRINT

45

Page 54: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

560 PRINT "END OF TITRATION" 570 PRINT 580 IF N = 0 THEN 640 590 PRINT "END POINTS DETECTED AT VOLUMES OF" 600 FOR I = 1 TO N 610 PRINT E(I) 620 NEXT I 630 GOTO 650 640 PRINT "NO END POINTS DETECTED •••• THRESHOLD TOO HIGH~" 650 END

TRIAL RUN

PROGRAM TO DETECT END POINTS IN POTENTIOMETRIC TITRATIONS *** THIS VERSION ALLOWS UP TO 10 END POINTS ***

THRESHOLD (USUALLY FROM 0 TO I)? 0.02

TYPE VOLmlE AND INSTRUMENT READING (E.G. PH) ONE PAIR PER LINE. DO THIS WHEN YOU SEE THE MESSAGE 'DATA'. THE VOLUME (ANY UNI'rS) MUST ALWAYS BE TYPED FIRST. SEPARATE 'rHE VALUES WITH A COMMA TO FINISH: TYPE 999,999

DATA? 22.00,3.3 DAT'A? 24.30,3.9 DAT'A? 24.50,4.3 DATA? 25.00,5.0 DATA? 26.00,5.7 DATA? 28.00,6.6 DATA? 49.00,8.2 DATA? 50.00,8.7 DATA? 5e.50,9.15 DATA? 51.00,9.55 DATA? 52.00,10.25 DATA? 55.00,10.85 DATA? 999,999

END OF 'l'ITRA'fION

END POINTS DETECTEC AT VOLUMES OF 24.3896 50.3636

46

Page 55: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

it'

ATOMIC ORBITALS

This program allows users to draw "personalised" pictures of most of the usually-encountered atomic orbitals as a function of nuclear charge. The pictures are actually probability ttistributions in the xy plane perpendic­ular to the z axis. These are calculated using the appropriate wave equation for the Is, 2s, 2px, 2py, 2pz, 3s, 3px, 3py, 3pz, 3dxy, 3dxz, 3dyz, 3ctx2-y2 or 3dz2 orbital. The equations hold for hydrogen and other one electron species such as He+, Li2 +, Be3+, B"+ etc. The user can alter the height of the xy plane and/or change the width of the picture, thus enabling him to gain new insights into quantum theory that he is unlikely to obtain from text books.

The equations used in the program are the standard ones to be found in any good book on quantum mechanics so users can easily add others, for example, the f orbitals.

This program uses the BASIC substring facility, which is available 0:1 very many computers. If your machine does not permit this, you will need to make A$ two-dimensional [A$(lO,lO) 1 and line 310 will have to be replaced with a few lines in which the individual symbols are stored in A$(l,l) to A$(1O, 10). The program uses a conditional branch statement in line 700 (GOTO n OF ... ; .). On some compilers this will have to be changed to ON n GOTO ... .

Do remember that many orbitals have nodes - areas in which the electron density is zero- and this means that:

1) with the height z equal to zero, no picture is obtained for the 2pz, 3pz, 3dxz and 3dyz orbitals, because they have a nodal plane in the x-y plane.

2) if the picture width is set too small, a node may coinCidentally make you think that the entire orbital has been plotted, when more outlying regions are simply outside the field of view.

Find out about the influence of nuclear charge on the Size of orbitals - can you produce pictures to confirm this? What relationships can you find between quantum numbers and the number of nodes in an orbital?

47

Page 56: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

10 DIM A$(10), B$(10), S(21, 31), C(14) 20 REM- A$ MAY NEED TO BE TWO-DIMENSIONAL ON SOME MACHINES 30 PRINT TAB(3), "ATOMIC ORBITALS" 40 PRINT TAB(3), "====== ========" 50 PRINT 60 PRINT "THE PHYSICAL SIZE OF THE PICTURE IS SET IN LINES" 70 PRINT "410 AND 420. THE VALUES OF L(LENGTH) AND H(HEIGHT) SHOULD" 80 PRINT "EACH BE ODD AND NOT EXCEED 61 AND 41 RESPECTIVELY" 90 PRINT "YOU SHOULD ADJUST THEM FOR CORRECT SIZE & SYMMETRY" 100 LET A = .0529 110 LET P = 3.14159 120 FOR I = 1 TO 14 130 READ C (I) 140 NEXT I 150 DATA 1, .176776, .176777, .176777, .176777, 7.12778E-3 160 DATA 1. 74594E-2, 1. 74594E-2, 1. 74594E-2, 5.0401E-3, 1. 74594E-2 170 DATA 1.74594E-2, 8.7297E-3, 1.74594E-2 180 PRINT 190 PRINT "TYPE SYMBOL FOR ORBITAL REQUIRED, OR TYPE QUIT", 200 INPUT B$ 210 FOR II = 1 TO 15 220 READ A$ 231iJ DATA "IS", "2S", "2PZ", "2PX", "2PY", "3S", II)PZn, 113PXIl, ")pyl1 240 DATA "3DZ2", "3DXZ", 113DYZ u , "30X2-Y2", 1130XY" I "QUIT" 250 IF A$ = B$ THEN 300 260 NEXT Il 270 PRINT "THAT'S NOT AN ALLOWED ORBITAL- TRY AGAIN." 280 RESTORE 290 GOTO 120 300 IF A$ = "QUIT" THEN 1280 310 LET A$ = " .,-+=*OZ$" 320 PRINT "NUCLEAR CHARGE", 330 INPUT N 340 IF N > 0 THEN 370 350 PRINT "IT MUST BE POSITIVE - TRY AGAIN" 360 GOTO 320 370 PRINT "WIDTH OF MAP (NANOMETRES) " , 380 INPUT W 390 PRINT "DISTANCE UP THE Z-AXIS (NANOMETRES)", 400 INPUT Z 410 LET L = 41 420 LET H = 25 436 LET X2 SQR«N * N * N) I (P * A * A * A» * C(Il) 440 LET Z9 0 450 LET Z3 N I A 460 LET Z2 Z * Z 470 LET WI W I H 480 LET W2 W I L 490 LET HI 1 + INT(H I 2) 500 LET Ll 1 + INT(L I 2) 510 PRINT 520 PRINT TAB(S), "PSI-SQUARED MAP FOR THE ", B$, " ORBITAL" 530 PRINT 540 PRINT TAB(S), "*", 550 FOR J = 1 TO HI

48

Page 57: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

;,i 560 LET Y = (HI - J) * WI 570 LET Y2 = Y * Y 58B FOR K = 1 TO Ll 59B LET PI = B 600 LET X = (K - Ll) * W2 61B LET R = SQR(X * X + Y2 + Z2) 62B LET X4 = R * Z3 630 IF R < .BB0Bl THEN 99B 640 LET Tl = Z / R 650 LET T2 = SQR(l - Tl * Tl) 660 IF T2 < .00BBl THEN 99~

670 LET D = R * T2 680 LET T3 = X / D 690 LET T4 = Y / D 7B0 GOTO II OF 720.74B.76B.780.800.820.840.860.880.900.920.940.960.980 710 REM -CHANGE ABOVE LINE TO 700 ON II GO~O 720 •••••• IF NECESSARY 720 LET PI = X2 * EXP(-X4) 730 GOTO 990 740 LET PI = X2 * (2 - X4) * EXP(-X4 / 2) 750 GOTO 990 760 LET PI = X2 * Tl * X4 * EXP(-X4 / 2) 770 GOTO 990 780 LET PI = X2 * T3 * T2 * X4 * EXP(-X4 / 2) 790 GOTO 990 800 LET PI = X2 * T4 * T2 * X4 * EXP(-X4 / 2) 810 GOTO 990 820 LET PI = X2 * (27 - 18 * X4 + 2 * X4 * X4) * EXP(-X4 / 3) 830 GOTO 990 840 LET PI = X2 * (6 - X4) * X4 * Tl * EXP(-X4 / 3) 850 GOTO 990 860 LET PI = X2 * (6 - X4) * X4 * T2 * T3 * EXP(-X4 / 3) 870 GOTO 990 880 LET PI = X2 * (6 - X4) * X4 * T2 * T4 * EXP(-X4 / 3) 890 GOTO 990 900 LET PI = X2 * X4 * (3 * Tl * Tl - 1) * EXP(-X4 / 3) 910 GOTO 990 92B LET PI = X2 * X4 * EXP(-X4 / 3) * Tl * T2 * T3 930 GOTO 990 940 LET PI = X2 * X4 * X4 * EXP(-X4 / 3) * Tl * T2 * T4 950 GOTO 99& 960 LET PI = X2 * X4 * X4 * EXP(-X4 / 3) * T2 * T2 * (2 * T3 * T3 - 1) 970 GOTO 990 980 LET PI = X2 * 990 LET S(J. K) 1000 IF S (J. K) <= 1010 LET Z9 = S(J. 1020 IF J > 1 THEN 1030 PRINT "**"; 1040 NEX:r K 1050 NEXT J

X4 * EXP(-X4 / pI * PI

Z9 THEN 1020 K) 1040

3) * T2 * T2 * T4 * T3

1060 PRINT 1070 IF Z9 1080 PRIIi'l 1090 GOTO 1100 FOR J

<> 0 THEN 1100 "ALL ZEROES 1240

IIIP.YBE WE'RE IN A NODAL PLANE!"

= 1 TO H

49

Page 58: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

1110 1120 1130 1140 llS0 1160 1170 1180 1190 1200 1210 1220 1230 1240 12S0 1260 1270 1280

PRINT TAB(S); n*n; FOR K = 1 TO L LET K1 = L1 - ABS(K - L1) LET J1 = HI - ABS(J - B1) LET D = INT(l.S + 8.9 * S(J1, K1) / Z9) PRINT TAB(S); A$(D, D); NEXT K PRINT 11*11

NEXT J PRINT TAB(S); FOR I = 1 TO L + 2 PRINT "*"1 NEXT I PRINT PRINT RESTORE GOTO 120 END

50

.. ~

Page 59: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

I~

TRIAL RUN

ATOMIC ORBITALS

THE PHYSICAL SIZE OF THE PICTURE IS SET IN LINES 41~ AND 420. THE VALUES OF L(LENGTH) AND H(HEIGHT) SHOULD EACH BE ODD AND NOT EXCEED 61 AND 41 RESPECTIVELY YOU SHOULD ADJUST THEM FOR CORRECT SIZE & SYMMETRY

TYPE SYMBOL FOR ORBITAL REQUIRED, OR TYPE QUIT? 3PX NUCLEAR CHARGE? 1 WIDTH OF MAP (NANOMETRES)? 1.7 DISTANCE UP THE Z-AXIS (NANOMETRES)? 0

PSI-SQUARED MAP FOR THE 3PX ORBITAL

******************************************* • • * •

• • • • • • • •

*

.,+;;:, ,;=+,.'

.-=Z$= =$Z=-.

.,+::;:;;:, ,:;;:;+,.

• * • • • * • • * • • • • • • • • • • * •

*.*****************************************

TYPE SnIBJL ,'OR CRBI'l'AL REQUIRED, OR TYPE QUIT? QUIT

51

Page 60: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

WAVES

A clear understanding of wave properties is essential for under­standing many physical phenomena (such as the diffraction of light or X-rays, and interference between sound waves) and as a precursor to more advanced topics such as quantum mechanics and Fourier transformations.

This program allows the summation of from two to ten sine waves of the general form :

Yi = a i sin (21Tx/"J, Hi)

where ai is the maximum amplitude, A i is the wavelength and e i is the phase angle of the wave. At a distance x from the origin, the displacement (amplitude) is Y i . If there are just two waves, the program plots a graph of the individual components with symbols 1 and 2, and in addition their resultant r.Y i using an asterisk *. If there are more than two waves then for clarity only their resultant is plotted. The user has the option of plotting intensity (Y i 2) instead of the amplitude. The graph is scaled automatically based on the largest value to be plotted.

A challenge for programming addicts is to modify the program to plot the individual components of three waves 1, 2 and 3, as well as the resultant.

The input data are checked to ensure that :

(i) there are a whole number of waves between 2 and 10.

(ii) the amplitude of each wave is greater than 0 and less than 3000.

(iii)

(iv)

(v)

l.

the wavelength of each wave is in the range 0.5 to 100 metres.

the phase angle is between 0 and 21T radians.

the maximum distance to be plotted along the x axis is in the range 5 to 100 metres. The upper limit is determined by the array sizes.

The following interesting wave forms may be produced:

"Beat" patterns from two similar wavelengths (e. g. 15 and 12 metres) but constant amplitude.

2. Fourier-synthesised square waves using

ai = b, b/3, b/5, b/7 ..... .

and Ai = c, c/3, ciS, c/7

where band c are constants. Try b = c = 42 and use just the first four terms ( ei = 0 for each case).

3. COSine waves - using a phase angle of 1T 12 = 1. 5708 radians.

52

t)

Page 61: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

'\

'I

10 DIM A(10), W(l0) , T(10), M(10, 101), S(101), Q$(15) 20 PRINT "PROGRAM FOR SINE WAVE ADDITION" 3~ PRINT "::::::::::==:::= === ===== =::;::::= ====:;:;;:==" 40 REM-SINE WAVES ARE ADDED (UP TO 10 ARE ALLOWED). IF THERE ARE 50 REM-MORE THAN 2, THE FINAL GRAPH SHOWS ONLY THE SUMMED 60 REM-AMPLITUDE (OR INTENSITY). THIS IS FOR CLARITY ON THE GRAPH. 70 LET P = 6.28318 80 PRINT 90 PRINT "HOW MANY WAVES": 100 INPUT N 110 IF INT(N) <> N THEN 130 120 IF (N - 2) * (N - 10) <= 0 THEN 150 130 PRINT "NO. OF WAVES MUS!' BE A WHOLE NUMBER BETWEEN 2 AND 10" 140 GOTO 90 150 FOR I = 1 TO N 160 PRINT 170 PRINT "PARAMETERS OF SINE WAVE ": I 180 PRINT "AMPLITUDE": 190 INPUT A(I) 200 IF A(I) * (A(I) - 3000) < 0 THEN 230 210 PRINT "AMPLITUDE MUST BE BETWEEN 0 AND ·3000 - RETYPE CORRECTLY" 220 GOTO 190 23g PRINT "WAVELENGTH (FROM 5 TO 50 METRES)": 240 INPUT WeI) 250 IF (W(I) - 5) * (W(I) - 50) <= 0 THEN 280 260 PRINT "WAVELENGTH MUST BE IN RANGE 5 TO 50 M. RETYPE CORRECTLY" 270 GOTO 240 280 PRINT "PHASE ANGLE (FROM 0 TO 2 PI RADIANS)": 290 INPUT T(I) 300 IF T(I) * (T(I) - P) <= 0 THEN 330 310 PRINT "PHASE ANGLE MUST BE IN RANGE 0 TO 2 PI. RETYPE CORRECTLY" 320 GOTO 290 330 NEXT I 340 REM-NOW ADD THE WAVES 350 PRINT 360 PRINT "WHAT IS THE MAX. DISTANCE ALONG THE X-AXIS (5 TO 100 M)": 370 INPUT D 380 IF (D - 5) * (D - 100) <= 0 THEN 410 390 PRINT "MAX. DISTANCE MUST BE BETWEEN 5 AND 100 M • RETYPE CORRECTLY" 400 GOTO 370 410 LET Ql = 0 420 PRINT 430 PRINT "TYPE AMPLITUDE OR INTENSITY TO INDICATE THE PLOT REQUIRED": 440 INPUT Q$ 450 IF Q$ = "AMPLITUDE" THEN 490 460 IF Q$ = "INTENSITY" THEN 490 470 PRINT "RE-": 480 GOTO 430 490 PRINT 500 FOR K = 0 TO D 510 LET S(K + 1) 0 520 FOR I = 1 TO N 530 LET M(I, K + 1) = A(I) * SIN(P * K / WeI) + T(I» 540 IF ABS(M(I, K + 1» <= Ql THEN 560 550 LET Ql = ABS(M(I, K + 1»

53

Page 62: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

56B IF 0$ = "INTENSITY" THEN 59B 57B LET S(K + 1) = S(K + 1) + M(I, K + 1) 58B GOTO 6BB 598 LET S(K + 1) = S(K + 1) + M(I, K + 1) • M(I, K + 1) 6BB IF S(K + 1) <= 01 THEN 62B 61B LET 01 = S(K + 1) 62B NEXT I 63B NEXT K 64B IF N <= 2 THEN 67B 65B PRINT 66B PRINT "BECAUSE OF THE NO. OF WAVES, ONLY THE SUMMATION IS PLOTTED" 67B PRINT 68B PRINT TAB(31), "MAGNITUDE" 69B LET 02 = -01 788 IF 0$ = "AMPLITUDE" THEN 750 11B PRINT "DISTANCE", TAB(10) , "0", TAB(33), 01 / 2, TAB(58), 01 72B LET K1 = !B 738 LET K2 = 5B 748 GOTO 78B 75B PRINT "DISTANCE", TAB(18), 02, TAB(35), "0", TAB(58), 01 760 LET K1 = 35 778 LET K2 = 25 788 LET 0$ = "----+----t" 798 PRINT "-------- +", 0$, 0$, 0$, 0$, 0$ 8BB FOR J =·B TO D 81B PRINT TAB(3), J, 82B LET G = Kl + INT(S(J + 1) • K2 / 01 + .5) 83B IF N > 2 THEN 99B 848 LET P = K1 + INT(M(l, J + 1) • K2 / 01 + .5) 858 LET 0 = K1 + INT(M(2, J + 1) • K2 / 01 + .5) 868 IF G + P + 0 = (99 • 3) THEN 1080 87B IF G > P THEN 95B 880 IF G > 0 THEN 920 890 PRINT TAB(G) , ".", 9BB LET G = 99 . 9!B GOTO 86B 92B PRINT TAB(O) , "2", 930 LET 0 = 99 948 GOTO 86B 958 IF P > 0 THEN 920 968 PRINT TAB(P), "1", 978 LET P = 99 988 GOTO 86B 990 PRINT TAB (G) , "*", UB8 PRINT !BIB NEXT J U2B PRINT U311 PRINT "WOULD YOU LIKE ANOTHER RUN (YES/NO) ", U4B INPUT 0$ 1050 IF 0$ = "YES" THEN 88 106B IF 0$ = "NO" THEN 1B90 11170 PRINT "REPLY"', 0$, '" NOT UNDERSTOOD • RETYPE YES OR NO .", !B8B GOTO 1B4B 1B9B PRINT "JOB FINISHED" 11B0 END

54

\71.

Page 63: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

it

.;l:.

TRIAL RUN

PROGRMI FOR SINE WAVE ADDITION

HOW MANY WAVES ? 2

PARAMETERS OF SINE WAVE 1 AMPLITUDE ? 10 WAVELENGTH (FROM .5 TO 100 METRES) ? 8 PHASE ANGLE (FROM 0 TO 2 PI RADIANS) ? 0

PARAMETERS OF SINE WAVE 2 AMPLITUDE ? 10 WAVELENGTH (FROM .5 TO 100 METRES) ? 6 PHASE ANGLE (FROM 0 TO 2 PI RADIANS) ? 0

WHAT IS THE MAX. DISTANCE ALONG THE X-AXIS (5 TO 100 M) ? 30

TYPE AMPLITUDE OR INTENSITY TO INDICATE THE PLOT REQUIRED? AMPLITUDE

DISTANCE -18.6603 MAGNITUDE

o 18.6603

o I 2 3 4 5 6 7 8 9 10 II 12 13 14 IS 16 17 18 19 20 21 22 23 24 25 26 27 28 29

+----+----+----+----+----+----+----+----+----+----+ *12--__ ~--~--__ __

1 2 *~ ....:.2;:.1_----

_2-----*1 __ *2 ----1

* 2 1 '----*1__ 2

2 2

I -* __ 2 I *2 2 *1/ *-- 1 *- I c..*12

1 ~ *

/*1-- 2

2 2

... *2 I 2 -- * T::-----...;I~ __ *1

1 2 ---* I ___ -----*2~ __ ----~--*I---2 ~* 12

------~2~~----___ *I2 ----~~2~--~*~

___ --~2~------*I~2~1----------*--;a~ I

55

Page 64: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

BOMB

This program predicts whether or not a slab of fissile material is of "critical mass". Uranium-235, for example, undergoes spontaneous fission with the emission of two neutrons,each of which may induce another

235U nucleus to fission. If this ·process continues, an explosive chain reaction results. However, some neutrons leave the material before colliding with another nucleus and these will be wasted. Therefore, the fractional change in the number of nuclei undergoing fission is reduced from

235 2(forU)toF:

F = N'/N (1)

where N is the number of nuclei which underwent spontaneous fiSSion and N' is the number that were induced to fission by the original N. If F is greater than 1.0, the reaction is 'super-critical'; if F = 1.0, the mass is just critical. The value used for N in the program is 250.

The program is an example of the "Monte Carlo" method. It generates N random coordinates, each of which represents a spontaneously fissioning nucleus. The block has a length (b) and a square cross section (a. a). The program requires the ratio (S) between the dimensions a: b, the mass (M) and the specific gravity (R). These are related thus:

M = V.R = a2 b."R = (a3/S)R

where V is the volume of the slab.

The ejected neutrons have a maximum allowed range and they are allowed to travel a random distance within this range in any random direct­ion. If they arrive at a point within the slab, N' in equation (1) is increased by one. The user should vary each of the parameters in turn to investigate their influence on criticality.

Do-it-yourself bomb makers should note one difficulty. The energy released in the chain reaction makes the temperature rise dramatically. The fissile material melts and spreads out, more neutrons escape so the value of N' decreases. Thus F decreases and the reaction becomes sub­critical.

56

·r,

Page 65: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

.j

10 DIM Q$ (10) 20 PRINT TAB(8), "BOMB" 30 PRINT TAB (8), "====" 40 LET G = 0 50 LET Q$ = "NEW" 60 PRINT "TYPE THE SPECIFIC GRAVITY OF THE FISSILE MATERIAL", 70 INPUT R 80 IF R * (R - 24) < 0 THEN 110 90 PRINT "SPECIFIC GRAVITY MUST BE > 0 & < 24" 100 GOTO 60 110 IF Q$ = "SG" THEN 380 120 PRINT "TYPE THE RANGE (IN CM.) OF NEUTRONS FOR' FISSION", 130 INPUT D 140 IF D * (0 - 100) < 0 THEN 170 150 PRINT "RANGE MUST BE > 0 & < 100 CM." 160 GOTO 120 170 IF Q$ = "RANGE" THEN 380 180 PRINT "TYPE TIH! NUMBER OF NEUTRONS PRODUCED PER FISSION", 190 INPUT N2 200 IF N2 = INT(N2) THEN 230 210 PRINT "MUST BE A WHOLE NUMBER - RETYPE" 220 GOTO 180 230 IF (N2 - 1) * (N2 - 9) <= 0 THEN 260 240 PRINT "MUST BE BETWEEN 1 AND 9 - RETYPE" 250 GOTO 180 260 IF Q$ = "NEUTRONS" THEN 380 270 PRINT "TYPE THE MASS OF FISSILE MATERIAL (IN GMS.)", 280 INPUT M 290 IF M > 0 THEN 320 300 PRINT "MUST BE HORE THAN THAT! RETYPE" 310 GOTO 270 320 IF Q$ = "MASS" THEN 380 330 PRINT "TYPE THE RATIO OF LENGTH TO BREADTH", 340 INPUT 5 350 IF 5 > ~ THEN 380 360 PRINT "MUST BE > 0. RETYPE" 370 GOTO 330 380 LET A = (M * 5 / R) ** (1 / 3) 390 LET A2 = A / 2 400 LET B = A / 5 410 LET B2 = B / 2 420 LET Nl = 0 430 LET G = G + 1 440 FOR I = 1 TO 250 450 REM-GENERATE RANDOM COORDINATES 460 LET X A * (RND(0) - .5) 470 LET Y = A * (RND(0) - .5) 480 LET Z • B * (RND(0) - .5) 490 FOR J = 1 TO N2 500 REM-REMEMBER THAT THERE ARE N2 NEUTRONS PER FISSION 510 LET Cl COS(2 * 3.14159 * RND(0» 520 LET 51 SQR(l - Cl * Cl) 530 LET C 2 2 * ( RND ( 0 ) - . 5 ) 540 LET 52 SQR(l - C2 * C2) 550 LET 01 0 * RND(0)

57

Page 66: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

569 REM-FIND TARGET POINT 579 IF ABS(X + 01 * S2 * C1) > A2 THEN 619 589 IF ABS(Y + 01 * S2 * Sl) > A2 THEN 619 599 IF ABS(Z + 01 * C2) > B2 THEN 619 699 LET N1 = N1 + 1 619 NEXT J 629 NEXT I 639 LET F = N1 / 259 648 PRINT 659 IF F <> 1 THEN 679 669 PRINT "NOW CRITICAL WITH F = 1" 679 IF F <= 1 THEN 699 689 PRINT "NOW SUPER-CRITICAL WITH F ="1 F 690 IF F >= 1 THEN 719 79B PRINT "NOW SUB-CRITICAL WITH F ="1 F 719 PRINT "TYPE NEW, SG, RANGE, NEUTRONS, MASS, RATIO, OR END" 729 IF G > 1 TH~N 769 739 PRINT "NEW ALLOWS YOU TO RE-RUN PROGRAM WITH COMPLETELY NEW DATA." 749 PRINT "SG, RANGE, NEUTRONS, MASS OR RATIO ALLOW YOU TO CHANGE JUST" 759 PRINT "ONE ITEM OF DATA, OR TYPE END TO TERMINATE THE RUN." 769 INPUT Q$ 77B IF Q$ "NEW" THEN 6B 789 IF Q$ "SG" THEN 69 799 IF Q$ "RANGE" THEN 129 89B IF Q$ "NEUTRONS" THEN 189 8lB IF Q$ "MASS" THEN 279 820 IF Q$ "RATIO" THEN 339 838 IF Q$ "END" THEN 868 840 PRINT "REPLY NOT UNDERSTOOD" 850 GOTO 719 869 PRINT "END OF JOB." 870 END

TRIAL RUN

BOMB

TYPE THE SPECIFIC GRAVITY OF THE FISSILE MATERIAL? 12 TYPE THE RANGE (IN CM.) OF NEUTRONS FOR FISSION? 1 TYPE THE NUMBER OF NEUTRONS PRODUCED PER FISSION? 2 TYPE THE MASS OF FISSILE MATERIAL (IN GMS.)? 190 TYPE THE RATIO OF LENGTH TO BREADTH? 1

NOW SUPER-CRITICAL WITH F = 1.34 TYPE NEW, SG, RANGE, NEUTRONS, MASS, RATIO, OR END NEW ALLOWS YOU TO RE-RUN PROGRAM WITH COMPLETELY NEW DATA. SG, RANGE, NEUTRONS, MASS OR RATIO ALLOW YOU TO CHANGE JUST ONE ITEM OF DATA, OR TYPE END TO TERMINATE THE RUN. ? RATIO TYPE THE RATIO OF LENGTH TO BREADTH? 18

NOW SUB-CRITICAL WITH F = .868 TYPE NEW, SG, RANGE, NEUTRONS, MASS, RATIO, OR END END OF JOB.

58

~I

Page 67: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

\ .

i.fl

SATELLI TE

This program calculates the path of a satellite round a massive central body, using Newton's Universal Law of Gravitation

F = ma = -GmM/d2 hence a = -GM/d 2

where m and M are the masses of the satellite and central body (e. g. earth or sun), G is a constant, d is the distance and a the acceleration of the satellite. The equation is simplified in the program by assuming that the product of G and M is one, thus making the results relative.

This is a two-dimensional problem, and the coordinates (x, y) and the velocity v are calculated using the equations :

6v = a· lit and 6r = v· lit where 6t is a small time interval, and 6v is the change in velocity am 6r the change in displacement (distances in both directions). Instead of this approach, the "Modified Euler's Method" described previously in program PREY could be used.

The user specifies the initial position and the velocities in the x and y directions. The tOtal time for which the trajectory is calculated must be specified. Too short a time will give only part of an orbit, and with too long a time the satellite will start to go round again. Finally the time interval between printing the pOSition of the satellite is required. A small time interval increases both the accuracy and the computing time: .A table of the satellite's poSition (x, y) is printed at the appropriate time intervals, followed by a graphical picture of the orbit. The scale factors for this picture (50 and 20) are set in lines 750, 760, 1100 and 11lO. These affect the length and height of the picture, and should be adjusted if necessary for the particular hardware used to give a circle using the initial pOSition -I, 0 and initial velocities of 0 and 1 respectively.

When an elliptical orbit is produced (see sample run) it will be noticed that the pOSitions (.) are not equidistant because of the changing speed of the satellite. Satellite motion is governed by Kepler's three laws of motion. Find out about these, then generate a family of orbits to verify that the laws are true.

59

Page 68: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

10 DIM B(20), X(102), Y(102), Q$(12) 20 PRINT TAB(5); "SATELLITE" 30 PRINT TAB(5); "=========" 40 PRINT "INITIAL X AND Y COORDINATES OF SATELLITE"; 50 INPUT XB, Y0 60 IF ABS(XB) + ABS(YB) > 9 THEN 90 78 PRINT " SORRY I YOU MUST BE ABOVE THE EARTH!" 80 GOTO 49 90 LET X(l) = XB 100 LET Xl = X0 110 LET X2 = XB 120 LET Y(l) = Y0 139 LET Yl = Y0 140 LE";l' Y2 = YII 150 PRINT "INITIAL VELOCITIES IN THE X AND Y DIRECTIONS, RESPECTIVELY"; 160 INPUT Sl, S2 170 IF ABS(Sl) + ABS(S2) > 9 THEN 2BB 180 PRINT "THERE MUST BE SOME INITIAL VELOCITY. RETYPE CORRECTLY" 19B GOTO 160 20B PRINT "TOTAL TIME FOR CALCULATION OF TRAJECTORY"; 218 INPUT M 220 PRINT "TIME INTERVALS ALONG THE TRAJECTORY"; 230 INPUT T 240 IF M / T <= 1a0 THEN 2711 250 paINT "THE NO. OF POINTS REQUIRED EXCEEDES 1011 • RETYPE THE" 260 GOTO 20B 270 LET T1 = T / 1B 280 LET I = 2 290 LET J = 1 3BB PRINT 310 PRINT "TIME", "X", "Y" 328 PRINT "----", "-", "_" 33B PRINT" 0"; TAB(lB); XB; TAB(25); Y0 340 LET T2, = B .35B LET x'c XB 369 LET Y = YB 37B LET VI = Sl 38B LET V2 = S2 39B LET C = B 4BB LET D = T1 41B LET R1 = SQR(X * X + Y * Y) 429 LET R = R1 *R1 * R1 43B IF T2 > M THEN 700 440 LET F1 = -X / R 45B LET F2 = -Y / R 460 IF T2 > 0 THEN 48B 470 LET D = T1 / 2 48B LET VI = VI + F1 * D 490 LET V2 = V2 + F2 * D 5BB LET X = X + VI * T1 510 LET Y = Y + V2 * T1 52B LET T2 = T2 + T1 53B LET C = C + T1 549 IF C < T THEN 4BB 55B PRINT (I - 1) * T; TAB(18); X; TAB(25); Y

60

1

", ."

~.

Page 69: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

,~,

"f

)~

568 LET X(I) = x 570 LET Y(I) = Y 580 LET I = I + 1 590 IF X >= Xl THEN 618 600 LET Xl = X 618 IF Y >= Y1 THEN 630 620 LET Y1 = Y 630 IF X <= X2 THEN 658 640 LET X2 = X 650 IF Y <= Y2 THEN 678 660 LET Y2 = Y 670 LET C = 0 680 GOTO 400 690 REM SCALE VALUES FOR PLOTTING 700 LET X(I) = 0 710 LET Y(I) = 8 720 FOR J = 1 TO I 730 LET P = X2 - Xl 740 LET Q = Y2 - Y1 750 LET X(J) INT(1.5 + 50 * (X(J) - Xl) / P) 768 LET Y(J) = INT(1.5 + 28 * (Y(J) - Y1) / Q) 778 NEXT J 780 PRINT 790 REM SORT VALUES READY FOR PLOTTING 880 FOR J = 1 TO I 810 LET 5 = 0 820 FOR K = 2 TO I 830 IF Y(K) - X(K) / 100 < Y(K - 1) - X(K - 1) / 18B THEN 910 840 LET 51 = Y(K) 858 LET 52 = X(K) 868 LET Y(K) = Y(K - 1) 870 LET X(K) = X(K - 1) 880 LET Y(K - 1) 51 890 LET X(K - 1) = 52 980 LET 5 = 1 910 NEXT K 920 IF 5 = 0 THEN 940 930 NEXT J 940 PRINT 950 PRINT TAB(10); "PICTURE OF ORBIT" 960 PRINT TAB(10); "----------------" 970 PRINT 980 PRINT Y2; TAB(16); "I"; 990 LET J = 1 1000 LET T = 0 1818 IF J = 1 THEN 1030 1820 LET T = Y(J - 1) - Y(J) 1030 FOR T1 = 1 TO T 1040 PRINT 1050 IF Y(J) = 1 THEN 1080 1060 PRINT TAB(16); "I"; 1070 GOTO 1090 1080 PRINT Y1; TAB(16); "I"; 1090 NEXT Tl 1100 IF Y(J) <> INT(1.5 - 20 * Yl / Q) THEN 1120

61

Page 70: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

1118 IF X(J) = INT(1.5 - 58 * Xl / P) THEN 1148 1128 PRINT TAB(16 + X(J»; n*n; 1l31l GOTO 1151l 1148 PRINT TAB(16 + X(J»; nEn; 1151l IF J = I THEN 1188 1168 LET J = J + 1 1171l GOTO 1828 1188 PRINT 1198 LET 0$ = n ____________ n

1288 PRINT TAB(16); "---"; 0$; 0$; 0$; 0$ 1218 PRINT TAB(16); Xl; TAB(35); "X VALUE"; TAB(60); X2 1228 PRINT 1238 PRINT "WOULD YOU LIKE ANOTHER RUN (YES/NO)" 1241l INPUT 0$ 1258 IF 0$ = "YES" THEN 48 1268 IF 0$ = "NO" THEN 1298 1278 PRINT "REPLY 0"; 0$; ·"0 NOT UNDERSTOOD. RETYPE YES OR NO." 1288 GOTO 1248 1298 END

62

p'

Page 71: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

,t

1"\'

SATELLITE

INITIAL X AND Y COORDINATES OF SATELLITE? -2,8 INITIAL VELOCITIES IN THE X AND Y DIRECTIONS, RESPECTIVELY? 8.1,8.9 TOTAL TIME FOR CALCULATION OF TRAJECTORY? 80 TIME INTERVALS ALONG THE TRAJECTORY? 5

TIME

8 5 10 15 28 25 38 35 4B 45 58 55 68 65 7B 75 80

3.37433

-5.33393

X

-2 .553963 3.39859 5.48813 6.99329 8.84189 8.71153 9.85832 9.88824 8.84384 8.32317 7.52921 6.45412 5.08387 3.39425 1.37111

-.88489~

Y

8 3.81472 3.37433 2.86634 2.83447 1. 86411 4.12722E-82

-.984255 -1.9776 -2.9B95 -3.75B89 -4.46821 -5.Bl749 -5.33393 -5.3B968 -4.73509 -3.18432

PICTURE OF ORBIT

! !'

!* E

*

*

* *

*

*

*

* *

* *

*

* *

-2 X VALUE 9.B8824

63

.1

Page 72: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

BATTLESHIPS

This program is based on the popular game in which the user fires shells attempting to sink the enemy fleet. Eight ships are hidden on an 8 x 8 grid or matrix :

(i) two aircraft carriers (each four squares long) (ii) two battleships (each three squares long)

(iii) two cruisers (each two squares long) and (iv) two minesweepers (each one square long).

You type the rectilinear coordinates (x, y) of a square you think is occupied by a ship, and are told which type of ship you have hit - or alternatively if you have missed. To sink a ship you must hit each of the squares that the ship occupies.

Our intelligence organisation has intercepted a map of the enemy fleet position, which may be a true map, or in one of three codes. In the first code the true positions are reflected across a vertical mirror through the centre of the map, hence a ship actually in square I, 6 would be shown in code on the map as 8,6. The second code involves reflection of the coord­inates across a horizontal mirror through the centre of the map, hence position 2,1 would be shown as 2,8. The third code involves reflection across a vertical mirror followed by reflection across a horizontal mirror, and corresponds to a rotation of 1800 about the centre point on the board. The game provides a good exercise in identifying coordinates in a two dimen­sional plane, and also in the reflection and rotation of coordinates.

The eight ships actually occupy twenty of the sixty four squares hence the chance of hitting a ship by firing at random is 20/64. Since you only have twenty five rounds of ammunition, winning the battle depends on decoding the map very rapidly.

The computer chooses the fleet position randomly, with the ships arranged along horizontal or vertical lines, avoiding touching or inter­section of ships. The code used for the first map is chosen randomly by asking the player to type in the time. At the end of a game, the player is asked if he would like another game using a different map, and if so whether he would like the same or a different code. At any time the user can abandon the game by firing a shot at the point 0, O. The computer checks that:

(i) shots are aimed only at squares on the board (ii) whole numbers are used for coordinates

(iii) answers to questions are either YES or NO.

If the fleet has not been sunk in twenty five Shots, you have lost the battle and the computer terminates the game.

64

q.

Page 73: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

" "

.0:,

.~

"

.. "

~:

HI DIM A(8), B(8, 8), C(8), F(8, 8), Q$(9) 20 PRINT TAB(8); "BATTLESHIPS" 311 PRINT TAB(8); "===========" 40 LET G = 1 50 PRINT 60 REM ***RANDOMIZE 70 PRINT "TIME CHECK" 80 PRINT "PLEASE TYPE THE NUMBER OF MINUTES PAST THE HOUR" 911 PRINT "THEN PRESS RETURN." 108 INPUT T 1111 FOR I = 1 TO ABS(T) 120 LET Cl = RND(0) 130 NEXT I 140 PRINT "THIS MAP OF THE ENEMY'S FLEET POSITIONS BAS BEEN CAPTURED." 158 PRINT "TRY TO DECODE THE MAP, AND USE IT IF YOU CAN." 160 PRINT 170 REM ***SET CODE 180 LET Cl = INT(RND(0) * 2) 190 LET Cl = 1 - Cl 200 LET C2 = INT(RND(0) * 2) 210 MAT F = ZER 220 MAT READ A 230 DATA I, 0, 0, 1, -I, 0, 0, -1 240 REM ***SET UP THE BOARD 250 FOR I 8 TO 1 STEP -1 260 LET S = INT«I + 1) / 2) 270 LET X = INT(RND(0) * 8) + 1 280 LET Y = INT(RND(II) * 8) + 1 298 LET D INT(RND(0) * 4) * 2 + 1 300 FOR J 0 TO S - 1 310 IF X + J * A(D) = 9 THEN 270 320 IF X + J * A(D) = 0 THEN 270 338 IF Y + J * A(D + 1) = 9 THEN 270 3411 IF Y + J * A(D + 1) = 0 THEN 270 1 350 IF F«X + J * A(D», (Y + J * A(D + 1») <> 0 THEN 270 :3>-10 1

360 NEXT J -;;:.. 370 FOR J = 0 TO S - 1 3811 LET Xl = X + J * A(D) 390 LET Yl = Y + J * A(D + 1) 400 FOR K 1 TO 7 STEP 2 410 IF Xl + A(K) = 9 THEN 460 420 IF Xl + A(K) = 0 THEN 460 430 IF Yl + A(K + 1) = 9 THEN 460 44B IF Yl + A(K + 1) = 0 THEN 460 450 LET F«XI + A(K», (Yl + A(K + 1») -1 460 NEXT K 470 NEXT J 480 FOR J = 0 TO S - 1 490 LET F«X + J * A(D», (Y + J * A(D + 1») I 500 NEXT J 510 NEXT I 520 REM ***PRINT MAP 530 FOR J = 1 TO 8 540 PRINT "("; (9-J);") "; 550 LET Yl = (J * Cl + (1 - Cl) * (9 - J»

65

Page 74: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

560 FOR K = 1 TO 8 570 LET Xl = (K * C2 + (1 - C2) * (9 - K» 580 IF F(X1, Y1) > 0 THEN 620 590 LET F(X1, Y1) = 0 600 PRINT" • "; 610 GOTO 630 620 PRINT F(X1, Y1); 630 NEXT K 640 PRINT 650 NEXT J 660 PRINT 670 PRINT TAB(7); "(1) (2) (3) (4) (5) (6) (7) (8)" 680 MAT B = ZER 690 IF G > 1 THEN 980 700 PRINT 710 PRINT 720 INPUT 730 IF Q$ 740 IF Q$ 7511 PRINT 760 GOTO 770 PRINT 780 PRINT 790 PRINT 800 PRINT 810 PRINT 8211 PRINT 830 PRINT 840 PRINT 850 PRINT 860 PRINT 870 PRINT 880 PRINT 890 PRINT 900 PRINT 910 PRINT 920 PRINT 930 PRINT 940 PRINT 950 PRINT 960 PRINT 970 PRINT 980 PRINT

"WOULD YOU LIKE Q$

INSTRUCTIONS? TYPE YES OR NO & PRESS RETURN"

= "YES" THEN 770 = "NO" THEN 990 "REPLY '0; 0$; n, 720

NOT UNDERSTOOD. PLEASE ANSWER YES OR NO"

"THE MAP GIVEN MAY BE CORRECT, OR IN ONE OF THREE DIFFERENT" "CODES. THE POSSIBLE CODES ARE:" "(A) THE POINTS ARE REFLECTED ABOUT A VERTICAL LINE" "(B) THE POINTS ARE REFLECTED ABOUT A HORIZONTAL LINE" "(C) BOTH (A) & (B) ARE CARRIED OUT SIMULTANEOUSLY" "THE GRID SQUARES ARE NUMBERED CONVENTIONALLY, WITH SQUARE" "1, 1 IN THE BOTTOM LEFT HAND CORNER. OTHER SQUARES ARE" "REFERENCED BY TYPING TWO NUMBERS SEPARATED BY A COMMA, TO" "INDICATE THE X & Y RESPECTIVELY" "THE BOARD COMPRISES OF TWO MINE SWEEPERS (1 SQUARE EACH)" TAB(23); "TWO CRUISERS (2 SQUARES EACH)" TAB(23); "TWO BATTLESHIPS (3 SQUARES EACH) ," TAB(19); "AND TWO AIRCRAFT CARRIERS (4 SQUARES EACH)." "WHEN INVITED TO SHOOT A SHELL, YOU TYPE THE CO-ORDINATES" "OF THE SQUARE YOU WANT TO HIT, & THE COMPUTER WILL TELL" "YOU WHETHER YOU HAVE HIT, SUNK, OR MISSED A SHIP." "TO SINK A MINE SWEEPER YOU MUST HIT IT ONCE, A CRUISER" "MUST BE HIT TWICE, A BATTLESHIP THREE TIMES, & AN " "AIRCRAFT CARRIER FOUR TIMES." "YOU ONLY HAVE 25 SHOTS TO SINK THE ENEMY FLEET" "TO ABANDON THE GAME TYPE IN 0,0 WHEN INVITED TO SHOOT."

990 LET L = 0 1000 MAT READ C 1010 DATA 3, 3, 2, 2, 1, 1, 0, 0 1020 LET S = 0 1030 PRINT "TYPE THE CO-ORDINATES FOR FIRST SHOT X,Y" 1040 INPUT X, Y 1050 IF X <> 0 THEN 1090 1060 IF Y <> 0 THEN 1160 1070 PRINT "GAME ABANDONED" 1080 GOTO 1490 1090 IF X + Y = INT(X) + INT(Y) THEN 1120 1100 PRINT "DONT CHEAT - YOU MUST USE WHOLE NUMBERS.

66

TRY AGAIN."

.11 ••

~l'

'l'J'

Page 75: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

1110

oJ 1120 1130 1140 1150

.... 1160 1170 1180 1190

"" 1200 1210 1220 1230 ., 1240 1250 1260 1270

'-; 1280 1290 1300

10! 1310 1320 1330 1340

'" 1350 1360 1370 1380

.'1j. 1390 1400 1410 1420

.-( 1430 1440 1450 1460 ., 1470 1480 1490 1500

'" 1510 1520 1530

"-1540 1550 1560 1570

~. 1580 1590 1600 1610 1620 1630 1640 1650

GOTO 1040 IF X > 8 THEN 1160 IF X < 1 THEN 1160 IF Y > 8 THEN 1160 IF Y >= 1 THEN 1180 PRINT "SHOT OFF THE BOARD. TRY AGAIN." GOTO 1040 LETS=S+l IF F(X, Y) = 0 THEN 1350 IF C(F(X, Y» < 4 THEN 1230 PRINT "WAKEY-WAKEY 1 YOU HAVE ALREADY SUNK A"; GOTO 1330 IF B(X, Y) > 0 THEN 1320 LET B(X, Yl = F(X, Yl PRINT "DIRECT HIT ON A"; GOSUB 1670 LET C(F(X, Y» = C(F(X, Y» + 1 IF C(F(X, Y» >= 4 THEN 1370 IF S = 25 THEN 1470 PRINT " - NEXT SHOT" GOTO 1040 PRINT "YOU HAVE ALREADY HIT A"; GOSUB 1670 PRINT" AT THIS POINT." PRINT "MISS!": GOTO 1290 PRINT " - AND YOU SUNK IT"; LET L = L + 1 IF L < 8 THEN 1290 PRINT PRINT PRINT "YOU HAVE WIPED OUT THE ENEMY'S FLEET"; PRINT " USING "; S; "SHOTS" IF S > 20 THEN 1490 PRINT "YOU MOST KNOW THE CODE -- A DIRECT HIT EVERY TIME." GOTO 1490 PRINT "YOU HAVE USED UP YOUR 25 ROUNDS OF AMMUNITION 1" PRINT "WAVE THE WHITE FLAG & BEG FOR MERCY." PRINT PRINT INPU'r IF Q$ IF Q$ PRINT GOTO

"WOULD YOU LIKE ANOTHER GAME" Q$ = "YES" ·THEN 1560 = "NO" THEN 1640 "REPLY'''; 0$, ". NOT UNDERSTOOD. PLEASE ANSWER YES OR NO." 1510

RES1'ORE LET G = G + 1 PRINT "WOULD YOU LIKE 1'HE SAME CODE ACAIN?" INPUT Q$ IF Q$ = "YES" THEN 210 IF Q$ = "NO" THEN 190 PRINT "REPLY '''; Q$, NOT UNDERSTOOD. PLEASE ANSWER YES 011 NO" GO TO 1590 PRINT "OK - GOODBYE" STOP

67

Page 76: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

166e REM ***SUBROUTINE TO IDENTIFY TYPE OF SHIP 167e IF F(X, Y) (= 6 THEN 17ee 168e PRINT "N AIRCRAFT CARRIER"! 16ge RETURN 17ee IF F(X, Y) (= 4 THEN 173e 171e PRINT" BATTLESHIP"! 1720 RETURN 173e IF F(X, Y) (= 2 THEN 176e 174e PRINT" CRUISER"! 1750 RETURN 176e PRINT" MINESWEEPER"! 177e RETURN 178e END

TRIAL RUN

BATTLESHIPS

TIME CHECK PLEASE TYPE THE NUMBER OF MINUTES PAST THE HOUR THEN PRESS RETURN. ? 13 THIS MAP OF THE ENEMY'S FLEET POSITIONS HAS BEEN CAPTURED. TRY TO DECODE THE MAP, AND USE IT IF YOU CAN.

8 n. 7 ~ 'If- 4 7 7 2 6 -,( 7 5 5 1 )Z ~ (!J 3 -I-- 5 4 3 -,: 5 3 6 lID 8 ~ . 2 6 7'- 8 1 I@ 0 . Q, . ""

(1) (2) (3) (4) (5) (6) (7) (8)

WOULD YOU LIKE INSTRUCTIONS? TYPE YES OR NO & PRESS RETURN ? NO TYPE THE CO-ORDINATES FOR FIRST SHOT X,Y ? 8,8 MISS! - NEXT SHOT ? 8,1 MISS! - NEXT SHOT ? 1,8 DIRECT HIT ON A CRUISER - NEXT SHOT ? 2,8 DIRECT HIT ON A CRUISER - AND YOU SUNK IT - NEXT SHOT ? 4,7 DIRECT HIT ON A MINESWEEPER - AND YOU SUNK IT - NEXT ? 8,5 DIRECT HIT ON A MINESWEEPER - AND YOU SUNK IT - NEXT

68

SHOT

SHOT

I. ,

1111

Page 77: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

{

-;

i II I •

CARS

In this program you are at the controls of a Formula I racing car attempting to complete a lap round the Brands Hatch circuit. At each of twenty two points round the circuit you are given a description of the next section of the track, and have to type three numbers separated by commas, to indicate the gear, the brake setting and the accelerator settings respectively. The clutch is automatic and former World Champion racing driver Jackie Stewart will steer for you.

The data input for gear, brake and accelerator settings are checked to ensure that the values are integers, that the gear is in the range 1 - 5, and the brake and accelerator settings are in the range 0 - 9. The new . speed is calculated based on the speed for the previous section,· plus a factor depending on the gear and accelerator settings and minus a factor depending on the brake setting and frictional losses. The engine revs are worked out from the speed and the gear. A number of checks are performed:

(i) Have you stopped?

(ii) Have you stalled?

(iii) Have you slightly over-revved the engine? You will be warned, but should you do this three times you will blow-up the engine.

(iv) Have you seriously over-revved and blown the engine?

(y) Have you exceeded the maximum safe speed for the section by 10 mph? If so you have crashed!

(vi) Have you exceeded the maximum safe speed by up to 10 mph? If so you skid and lose 25% of your speed.

Barring fatal mishaps, you are told the speed you have attained and the engine revs, and you are then given the information for the next section.

When you have either crashed or completed a lap, you are invited to attempt another lap. You may choose to drive yourself, or be shown how it should be done by an expert.

69

Page 78: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

111 DIM M$(19), Q$(10), J$(9), X(22), Y(22), Z(22), G(5), J(22), L(10) 20 PRINT TAB(8), "CARS" 30 PRINT TAB(8), "====" 40 LET J$ = "NORMAL" 50 PRINT "WOULD YOU LIKE INSTRUCTIONS? TYPE YES OR NO & PRESS RETURN." 611 INPUT Q$ 70 IF Q$ = ~YES" THEN 1111 80 IF Q$ = "NO" THEN 230 911 PRINT "REPLY'", Q$, ". NOT UNDERSTOOD. PLEASE ANSWER YES OR 1011 GOTO 611 1111 PRINT "YOU ~RE ABOUT TO DRIVE THE LATEST FORMULA 1 LOTUS" 1211 PRINT "AROUMD BRANDS HATCH. JACKIE STEWART WILL STEER FOR" 130 PRINT "YOU. THE CLUTCH IS AUTOMATIC. ALL YOU ARE REQUIRED" 140 PRINT "TO DO IS TO CHANGE THE GEAR, BRAKE AND ACCELERATOR" 150 PRINT "SETTINGS AT EACH OF 22 POINTS ON THE CIRCUIT." 1611 PRINT "THE CAR HAS FIVE FORWARD GEARS" . 1711 PRINT "MAX. SAFE ENGINE SPEED IS 10,000 RPM. DONT OVER-REV" 1811 PRINT "AT EACH POINT ON THE CIRCUIT ENTER G, B, A." 1911 PRINT TAB(6), "WHERE G = GEAR", "(1 - 5)" 2118 PRINT TAB (15) , "B = BRAKE", "(0 - 9)" 2111 PRINT TAB(6), "AND A = ACCELERATOR", "(0 - 9)" 220 PRINT "*****PLEASE FASTEN YOUR SAFETY BELT*****" 2311 MAT READ G 240 DATA 2.4, 1. 9, 1. 5, 1. 2, 1 250 MAT READ L 260 DATA II, 6, 15, 20, 25, 30, 48, 59, 80, 95 2711 MAT READ J

NO! "

280 DATA 180, 1811, 90, 180, 180, 64, 75, 1811, 125, 44J 180, 180, 94, 180 290 DATA 73, 188, 62, 31, 188, 121, 86, 188 300 MAT READ X 3111 DATA 1, 3, 5, 4, 5, 1, 3, 3, 4, 5, 3, 4, 3, 4, 5, 3, 2, 4, 2, 4, 5,4 328 MAT READ Y 330 DATA 1, II, 4, 0, 0, 9, 0, I, 1, 8, II, 0, 5, 0, 6, II, 9, 7, 0, 0, 6,11 340 MAT READ Z 358 DATA 9, 9, 5, 9, 9, 3, 3, 9, .7, 2, 9, 9, 3, 9, 1, 8, 9, 8, 9, 9, 8,9 3611 RSAD K, S, W 370 DATA II, 1, 0 380 PRINT 3911 PRINT "FLAG DROPPED - YOU'RE OFF" 400 FOR I = 1 TO 22 410 PRINT "ENTER G, B, A • 420 IF J$ = "EXPERT" THEN 650 430 INPUT G1, B1, Al 440 IF Al + B1 + G1 = INT(A1) + INT(B1) + INT(Gl) THEN 470 450 PRINT "VALUES MUST BE WHOLE NUMBERS _ ", 4611 GOTO 550 470 IF G1 < 1 THEN 490 488 IF G1 <= 5 THEN 520 4911 PRINT "PLEASE TELL THIS WOMAN DRIVER THAT THERE ARE GEARS" 500 PRINT "1,2,3,4 & 5, AND ASK HER TO ", 510 GOTO 550 520 IF B1 < II THEN 540 5311 IF B1 <= 9 THEN 570 5~0 PRINT "DONT CHEAT - BRAKE SETTINGS MAY BE II - 9 INCLUSIVE" 550 PRINT "RETYPE CORRECTLY"

70

~

..,

17)

.Ii,

),

~

, (. I

.~

, .~

~

.J,;)

Page 79: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

~

'';'

r{'

,..

" t

1

;.~J

'I'

.<l

...

" t'(

....:

~

W .. "' ....

~

560 GOTO 410 570 IF Al >= 0 THEN 600 580 PRINT "TRY PRESSING THE ACCELERATOR PEDAL DOWN - "I 590 GOTO 550 600 LET B1 = Bl + 1 610 IF Al <= 9 THEN 690 620 PRINT "YOU HAVE PRESSED THE ACCELERATOR THROUGH THE FLOORBOARDS!" 630 PRINT "TAKE YOUR BOOTS OFF AND TRY AGAIN" 640 GOTO 410 650 LET G1 = XCI) 660 LET B1 = Y(I) + 1 670 LET Al = Z(I) 680 PRINT X(I)I Y(I)I Z(I) 690 LET S = S + Al • 3.2 • G(Gl) - L(Bl) - S / 20 700 IF S > 2 THEN 730 710 PRINT "CHICKEN! YOU HAVE BRAKED TO A STANDSTILL." 720 GOTO 410 730 LET R = S • G(G1) • 61 740 IF R > 2000 THEN 790 750 PRINT "ENGINE STALLED" 760 PRINT "SELECT FIRST GEAR & START HER UP AGAIN!" 770 LET S = 0 780 GOTO 410 790 IF R <= 10000 THEN 920 800 IF R > 11900 THEN 900 810 PRINT " ••• WARNING - YOU HAVE SLIGHTLY OVER REWED ONCE "I 820 IF W = 2 THEN 890 830 IF W = 1 THEN 860 840 PRINT "& GOT AWAY WITH IT" 850 GOTO 870 860 PRINT "AGAIN" 870 LET W = W + 1 880 GOTO 920 890 PRINT "TOO OFTEN!" 900 PRINT "ENGINE SEIZED WHEN YOU REACHED" I RI " REVS!" 910 GOTO 1090 920 IF S <= J(I) THEN 1000 930 IF S < (J(I) + 10) THEN 980 940 PRINT ,,* •• *. YOU HAVE CRASHED ••• *." 950 PRINT "YOU WERE TRYING TO CORNER AT "I SI " M.P.H." 960 PRINT "THE MAXIMUM SAFE SPEED FOR THIS SECTION IS"I J(I)I " MPH." 970 GOTO 1090 980 PRINT "YOU HAVE SKIDDED AND LOST 25% OF YOUR SPEED" 990 LET S = S - S I 4 1000 PRINT "SPEED = "IINT(10*S)/101" M.P.H •• TACHO = "IINT(R)I" R.P.M." 1010 LET K = K + S 1020 IF I = 22 THEN 1060 1030 READ M$ 1040 PRINT 1050 PRINT "POINT" I I; M$ 1060 NeXT I 1070 LeT Sl = K I 22 1080 PRINT "LAP COMPLETeD - AVeRAGE speeD WAS "; S11 " M.P.H." 1090. PRINT 1100 PRINT "WOULD YOU LIKe ANOTHER GO? (YES/NO)"

71

Page 80: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

11111 INPUT Q$ 11211 IF Q$ = "YES" THEN 11611 11311 IF Q$ = "NO" THEN 15111 11411 PRINT "REPLY'"; Q$; ". NOT UNDERSTOOD. PLEASE ANSWER YES OR NO" 11511 GOTO 11111 11611 RESTORE 1178 IF J$ <> "EXPERT" THEN 121111 11811 LET J$ = "NORMAL" 11911 GOTO 2311 1288 PRINT "AFTER YOUR MISERABLE ATTEMPT AT DRIVING" 12111 PRINT "WOULD YOU LIKE A DEMONSTRATION BY AN EXPERT DRIVER (YES/NO)" 12211 INPUT J$ 12311 IF J$ = "YES" THEN 12711 12411 IF J$ = "NO" THEN 2311 12511 PRINT "REPLY'"; J$; ". NOT UNDERSTOOD. PLEASE ANSWER YES OR NO" 12611 GOTO 12211 12711 PRINT "NOW WATCH A LAP BY J. D. LEE I" 12811 LET J$ = "EXPERT" 1298 GOTO 2311 13118 DATA "ON THE STRAIGHT. 1318 DATA "SLOW FOR RH BEND. 13211 DATA "DOWNHILL STRAIGHT. " 1338 DATA "KEEP GOING NOW. 1348 DATA "SHARP CORNER. 1358 DATA "CORNER - EASING UP." 1360 DATA "STRAIGHT AHEAD. 1378 DATA "FAST L/H BEND. 13811 DATA "ANCHORS ON NOW. 139B DATA "ALL CLEAR. 14811 DATA "TRY FOR 163 M.P.H. " 14111 DATA "FAST RlH BEND. 14211 DATA "SHORT STRAIGHT. 14311 DATA "TAKE CARE. 14411 DATA "DOWNHILL STRAIGHT. " 1450 DATA "TRICKY RlH BEND. 14611 DATA "HAIRPIN. 14711 DATA "GO MAN GO. 14811 DATA "BANKED CURVE. 1490 DATA "BEND TIGHTENING UP." 151111 DATA "LAST CHANCE TO WIN." 1510 PRINT" JUST YOU DRIVE HOME CAREFULLYI" 1520 END

72

"..

J\I;,

.""

Page 81: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

",'

tI;,

"I

TRIAL RUN

CARS

WOULD YOU LIKE INSTRUCTIONS? TYPE YES OR NO & PRESS RETURN. ? NO

FLAG DROPPED - YOU'RE OFF ENTER G, B, A ? 1,0,9 ••• WARNING - YOU HAVE SLIGHTLY OVER REVVED ONCE & GO~ AWAY WITH IT SPEED 70 M.P.H. ,TACHO 10258 R.P.M.

POINT 1 ON THE STRAIGHT. ENTER G, B, A ? 4,0,9 SPEED 101.1 M.P.H. , TACHO

POINT 2 SLOW FOR RH BEND. ENTER G, B, A ? 2,4,4

7402 R.P.M.

••• WARNING - YOU HAVE SLIGHTLY OVER REVVED ONCE AGAIN YOU HAVE SKIDDED AND LOST 25% OF YOUR SPEED SPEED 71.5 M.P.H. ,TACHO 11055 R.P.M.

POINT 3 DOWNHILL STRAIGHT. ENTER G, B, A ? 5,0,9 SPEED 96.7 M.P.H. , TACHO

POINT 4 KEEP GOING NOW. ENTER G, B, A ? 5,0,9 SPEED 120.7 M.P.H. , TACHO

POINT 5 SHARP CORNER. ENTER G, B, A ? 2,4,3

5902 R.P.M.

7364 R.P.M.

ENGINE SEIZED WHEN YOU REACHED 12509.2 REVS!

WOULD YOU LIKE ANOTHER GO? (YES/NO) ? NO

JUST YOU DRIVE HOME CAREFULLY!

73

Page 82: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

CHOMP

This game is adapted from the original game Chomp published in Scientific American Qanuary 1973). The game is for two or more players, who in turn cut and eat slices of a cake which contains a poisonous cherry in the top left hand corner. The loser is the one unfortunate enough to have to eat the poisoned slice.

The cake I!hould be imagined as similar to a chess board. The initial size of the cake is chosen by the players, and is defined by the number of vertical columns and horizontal rows. The poison cherry occupies the square 1,1 in the top left hand corner of the board. Checks are performed to ensure that integer numbers are input for the number of players, columns and rows, and that the size of the cake is sensible.

In turn players type the coordinates of a square, (column, row), when this square together with the segment to the right and below are remov­ed from the cake. Checks are made to ensure that the coordinates are whole numbers, and correspond to a portion of cake still remaining.

With only two players it is possible to develop a successful strategy to ensure your long life. A strategy for three or more players is much more difficult to formulate, and can always be beaten by a coalition of two of the players who are determined to exterminate you.

74

-;:

'<I

l'

Page 83: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

'" "

"

10 DIM A(9, 9), p$ (3), Q$ (9), T$ (18) 28 PRINT TAB(20), "CHOMP" 30 PRINT TAB (20), "=====" 40 LET T$ = " RETYPE CORRECTLY." 50 PRINT 60 PRINT "WOULD YOU LIKE INSTRUCTIONS? TYPE YES OR NO & PRESS RETURN." 70 I NPU'I' Q$ 80 IF Q$ = "NO" THEN 250 90 IF Q$ = "YES" THEN 120 100 PRINT "REPLY"'; Q$; '" NOT UNDERSTOOD. PLEASE ANSWER YES OR NO" 110 GOTO 70 120 PRINT "THE GAME IS F,OR TWO OR MORE PLAYERS." 130 PRINT "THE BOARD IS A BIG CAKE, C COLUMNS WIDE & R ROWS DOWN." 140 PRINT "YOU INPUT C AND R AT THE START. IN THE UPPER LEFT" 150 PRINT "CORNER OF THE CAKE IS A POISON CHERRY IN SQUARE P." 160 PRIN'I 170 PRINT "THIS IS WHAT A 7 BY 4 BOARD LOOKS LIKE:" 180 READ F, R, C 190 DATA 1, 4, 7 200 GOTO 640 210 PRIN'I' "PLAYERS 'I'AKE A SLICE OF CAKE IN TURN, & THE ONE FORCED TO" 220 PRINT "TAKE THE POISON CHERRY LOSES. TO TAKE A CHOMP, TYPE THE" 230 PRINT "COLUMN & ROW OF A SQUARE IN THE CAKE, SEPARATED BY A COMMA." 240 PRINT "THIS SQUARE AND ALL THE SQUARES TO THE RIGHT & BELOW VANISH." 250 LET p$ = Q$ 260 LET F = 0 270 MAT A = ZER 280 PRINT 290 PRINT "TYPE IN THE NUMBER OF PLAYERS" 300 INPUT P 310 IF P > 1 THEN 340 320 PRINT "THERE MUST BE TWO OR MORE PLAYERS."; T$ 330 GOTO 300 340 IF P = INT(P) THEN 370 350 PRINT "NOT A WHOLE NUMBER OF PLAYERS!I"; T$ 360 GOTO 300 370 PRINT "'I'YPE IN THE NUMBER OF COLUMNS" 380 INPU'!' C 390 IF C >= 1 THEN 420 400 PRINT "THE NUMBER OF COLUMNS IS TOO SMALL -"; T$ 410 GOTO 380 420 IF C <= 9 THEN 450 430 PRIN'r '''rHE MAXIMUM NUMBER OF COLUMNS IS 9."; T$ 440 GOTO 380 450 IF C = INT(C) THEN 480 460 PRINT "NOT A WHOLE NUMBER OF COLUMNS!!"; T$ 470 GOTO 380 480 LET S1 = 0 490 PRINT "TYPE IN THE NUMBER OF ROWS" 500 INPu'r R 510 IF R >= 1 THEN 540 520 PRINT "NUMBER OF ROWS IS TOO SMALL -"; T$ 538 GOTO 500 540 IF R <= 9 THEN 570 550 PRINT "THE MAXIMUM NUMBER OF ROWS IS 9."; T$

75

Page 84: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

560 GOTO 500 570 IF R = INT(R) THEN 600 580 PRINT "NOT A WHOLE NUMBER OF ROWS!!"; T$ 590 GOTO 500 600 IF R * C > 2 * P THEN 630 610 PRINT "CHOOSE A LARGER BOARD." 620 GOTO 490 630 PRINT 640 FOR I = 1 TO R 650 FOR J = 1 TO C 660 LET A(I, J) 670 NEXT J 680 NEXT I 690 LET A(l, 1) = -1 700 REM ***PRINT THE BOARD 710 PRINT 720 PRINT" "; 730 FOR I = 1 TO C 740 PRINT I; 750 NEXT I 760 PRINT 770 FOR I = 1 TO R 780 PRINT I; TAB(7); 790 FOR J = 1 TO C 800 IF A(I, J)" = -1 THEN 840 810 IF A(I, J) = 0 THEN 860 820 PRINT"* "; 830 GOTO 850 840 PRINT"P "; 850 NEXT J 860 PRINT 870 NEXT I 880 PRINT 890 IF F = 1 THEN 210 900 REM ***GET CHOMPS FOF EACH PLAYER IN TUFN 910 LET Sl = Sl + 1 920 LET PI = Sl - INT(Sl / P) * P 930 IF PI <> 0 THEN 950 940 LET PI = P 950 PFINT "PLAYER"; PI; "COORDINATES OF YOUF CHOMP "; 960 IF p$ = "NO" THEN 980 970 PRINT" (COLUMN,ROW)"; 980 INPUT Cl, Rl 990 REM ***CHECK THAT NUMBERS AFE INTEGEFS 1000 IF INT(Rl) + INT(Cl) = Rl + Cl THEN 1030 1010 PFINT "COORDINATES MUST BE WHOLE NUMBERS."; T$ 1020 GOTO 950 1030 IF Rl < 1 THEN 1096 1040 IF Rl > R THEN 1090 1050 IF Cl < 1 THEN 1090 1060 IF Cl > C THEN 1090 1070 IF A(Rl, Cl) = 1 THEN 1110 1080 IF A(Fl, Cl) = -1 THEN 1180 1010 PRINT "DONT CHEAT - YOU CANNOT CHOMP AN EMPTY SPACE!" 1100 GOTO 950

76

Page 85: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

1

1110 FOR I = Rl TO R 1120 FOR J = Cl TO C 1130 LET A(I, J) 0 1140 NEXT J 1150 NEXT I 1160 GOTO 710 1170 REM "'END OF GAME DETECTED 1180 PRINT "PLAYER": PI: "HAS LOST" 1190 PRINT 1200 PRINT "WOULD YOU LIKE ANOTHER GAME?" 1210 INPUT Q$ 1220 IF Q$ = "YES" THEN 260 1230 IF Q$ = "NO" THEN 1260 1240 PRINT "REPLY''': Q$: NOT UNDERSTOOD. PLEASE ANSWER YES OR NO" 1250 GOTe 1210 1260 PRINT "OK - GOODBYE" 1270 END

TRIAL RUN

CHOMP

WOULD YOU LIKE INSTRUCTIONS? TYPE YES OR NO & PRESS RETURN. ? NO

TYPE IN THE NUMBER OF PLAYERS ? 2 TYPE IN THE NUMBER OF COLUMNS ? 4 TYPE IN THE· NUMBER OF ROWS ? 3

1 3 1 P • * 2 • * • 3 • * *

PLAYER 1 COORDINATES OF YOUR CHOMP ? 2,2 1 2 3 4

1 P * * 2 • 3 *

PLAYER 2 COORDINATES OF YOUR CHOMP ? 2,1 1 2 3 4

1 P 2 • 3

77

Page 86: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

DRAG

In drag car racing two cars compete over a straight quarter mile, which a good car will cover in about ten seconds, attaining speeds which require the use of a parachute brake. Cars are specially designed with very large engines, enormous wheels, and a suitable back axle gear ratio to match these two factors.

In the program you may choose to design just one car, and race it against a fairly good car (which can however be beaten), or alternatively you may design two different cars to race each other. To design a car you must specify the horsepower of the engine, the back axle ratio, the width of the rear tyres in inches and the diameter of the rear tyres in feet. The following checks are then performed:

(i) that the horsepower is greater than 5 and less than 3000.

(ii) that the back axle ratio is between 1 : 1 and 20 : 1

(iii) that the tyre width is between 5 and 40 inches

(iv) that the tyre diameter is between 2 and 15 feet.

A summary of the design features of the two cars is then given.

The mass of each car is calculated from the size of the engine and wheels, and the wind drag and maximum acceleration of each car is then calculated. If the maximum acceleration is exceeded, as frequently occurs at the beginning of a race, then wheel spin will occur. During the race itself, the number of engine revolutions per minute, acceleration, speed attained, and distance covered are calculated at tenth of a second intervals for each car, and the speed and distance are printed out at one second intervals until the race is completed.

It is interesting to note that it is physically impOSSible for a vehicle driven by its wheels on the earth to accelerate at a rate greater than g the acceleration due to graVity (32 feet per secopd per second, or 21.8 mph per second). Providing excessive amounts of energy in an attempt to exceed this acceleration is futile and merely results in wheelspin. Accel­erations in excess of g can be obtained using jet propelled engines or rocket motors, but drivers of such vehicles may require astronaut type training.

78

(

H.

VI

Page 87: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

,-

"~,

~DIM 1$(3), R$(9), T$(8) ,20 PRINT TAB(5); "DRAG CAR RACING" 30 PRINT TAB(5); "==== === ======" 40 PRINT 50 DIM P(2), E(2), W(2), S(2), X(2), M(2), C(2), B(2), Y(2), Q(2) .60 LET T$ =" RETYPE" 70 PRINT "WELCOME TO THE DRAG STRIP. WOULD YOU LIKE INSTRUCTIONS?"

',80 GOSUB 1500 90 LET 1$ = R$ 10~IF 1$ = "NO" THEN 160

..-ir0 PRINT "A DRAG RACE IS OVER A QUARTER OF A MILE. YOU MAY RACE" l-20~'PRINT "AGAINST ONE OF YOUR FRIENDS OR AGAINST MY DRAGSTER. YOU WILL"

~ ,130 PRINT "BE ASKED TO DESIGN YOUR OWN MACHINE, SPECIFYING THE" 140'-PRINT "HORSEPOWER, BACK AXLE RATIO (X:l), TYRE WIDTH"

.,1511 PRINT "- IN INCHES - AND TYRE DIAMETER - IN FEET." 160 LETA = 1 170 READ Q (1), Q (2), S (1), S (2), X (1), X (2), W 180 DATA 0, 0, 0, 0, 0, 0, 1 190 PRINT "WOULD YOU LIKE TO RACE AGAINST ME?"

.-200- GOS UB 1490 ~~IF R$ = "NO" THEN 250 220 PRINT" I WILL HAVE CAR 1." 230 READ P(I), E(I), W(I), D(I), A 240 DATA 700, 6, 25, 4, 2 250 FOR J = A TO 2 260 PRINT "PLEASE DESIGN CAR"; J 270 PRINT "HORSEPOWER ="; 280 INPUT P(J) 290 IF P(J) > " THEN 320

-l00 PRINT "NEGATIVE VALUES ARE IMPOSSIBLE!"; T$ 3-10 GOTO 270 320 IF P(J) > 5 THEN 350

·3311 PRINT "YOU ARE NOT DESIGNING A LAWNMOWER!"; T$ ·340'GOTO 270 .350 IF P(J) < 3000 THEN 380 .360, PRINT "YOU ARE NOT DESIGNING AN AEROPLANE!"; T$ 370 GOTO 270 380 PRINT "BACK AXLE RATIO ="; 390 INPUT E(J)

-40~'~IF E (J) > 1 THEN 430 ,,·410 PRINT "REAR AXLE RATIO MUST BE GREATER THAN 1."; T$ -42I'J GOTO 380 43~ IF E(J) < 20 THEN 460

~A40' PRINT "REAR AXLE RATIO MUST BE LESS THAN 20."; T$ 450 GOTO 380 460 PRINT "TYRE WIDTH (INCHES) ="; 470 INPUT W(J) 480 IF W(J) > 0 THEN 510 490 PRINT "NEGATIVE VALUES ARE IMPOSSIBLE!"; T$

-50'0 GOTO 460 -·!H0 IF W(J) > 5 THEN 540 --5c20'PRINT "YOU ARE NOT DESIGNING A MOTOR-CYCLE!", T$ .5~0· GO',l'O 460 ~.0 IF W(J) < 40 THEN 570

-S50 PRINT "YOU ARE NOT DESIGNING A JUMBO-JET!"; T$

79

Page 88: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

'51i If GOTO 460 570 PRINT "TYRE DIAMETER (FEET) =": 580 INPUT D(J) 5911 IF D(J) > 1 THEN 620 600 PRINT "YOU ARE NOT DESIGNING A GO-CART!": T$ 610 GOTO 570 620 IF D(J) > 2 THEN 6511 630 PRINT "A MINI HAS BIGGER WHEELS!": T$ 640 GOTO 570 650 IF D(J) < 15 THEN 680 660 PRINT "THESE WHEELS ARE BIGGER THAN A DOUBLE DECK BUS!": T$ 670 GOTO 570 680 PRINT 690 NEXT J 700 PRINT TAB(25): "SUMMARY" 710 PRINT TAB(17): "CAR 1": TAB(37): "CAR 2" 720 PRINT "HORSEPOWER": TAB(17): P(l): TAB(37): P(2) 7311 PRINT "BACK AXLE RATIO": TAB(17): E(l): TAB(37): E(2) 740 PRINT "TYRE WIDTH": TAB(17): W(l): TAB(37): W(2) 750 PRINT "TYRE DIAMETER": TAB(17): D(l): TAB(37): D(2) 760 PRINT "GOI" 770 REM *** M IS MASS OF CAR 7811 FOR J = 1 TO 2 790 LET M(J) = (500 + 1.6*P(J) + 2*W(J)*D(J) + 4*(D(J) * D(J») / 32.2 800 REM *** C IS DRAG FROM WIND 8111 LET C(J) = .0006 * EXP(LOG(M(J» * (2 / 3» + .00815 * W(J) * D(J) 820 REM *** B IS THE MAX ACCELERATION WITHOUT WHEELS PIN 830 LET B(J) = 15 + 28 * W(J) * D(J) / «W(J) + 6) * (D(J) + 1» 840 REM *** Y IS THE SCALE FACTOR FOR RPM V5 POWER 850 LET Y(J) = 3.7 - .01133 * P(J) 860 NEXT J 8711 PRINT 880 PRINT 890 PRINT "ELAPSED": TAB(16): "CAR 1": TAB(40): "CAR 2" 900 PRINT" TIME": TAB(ll): "SPEED": TAB(20): "DISTANCE": 918 PRINT TAB(35): "SPEED": TAB(44): "DISTANCE" 928 PRINT" (SEC)": TAB(ll): "(MPH)": TAB(22): "(FT)": 938 PRINT TAB(35): "(MPH)": TAB(46): "(FT)" 940 PRINT 950 LET D(l) = D(l) * 3.14159 960 LET D(2) = D(2) * 3.14159 978 FOR T = 0 TO 1118 988 FOR Tl = .1 TO 1 STEP .1 990 FOR J = 1 TO 2 1808 REM *** R IS RPM 1010 LET R = 60 * S(J) * E(J) / D(J) 1820 REM *** L8 IS ENGINE TORQUE 1830 LET Rl = R / Y(J) 1040 LET L0 = (P(J) / 42.5) * (50 + .0078 * Rl - 4E-10 * Rl ** 3) 1058 REM *** Ll IS TORQUE FROM FRICTION 1060 LET Ll = P(J) * .800012 * R 1070 REM *** L2 IS REAR AXLE TORQUE 1088 LET L2 = E(J) * (L0 - LI) 1090 REM *** F IS FORCE ON ROAD FROM TYRES 1100 LET F = 6.28318 * L2 / D(J)

80

1

Page 89: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

1110 REM ••• TEST FOR WHEEL-SPIN 1120 IF F > M(J) • B(J) THEN 1190 1130 REM •• * A IS THE ACCELERATION 1140 IF Q(J) <> 0 THEN 1170 1150 PRINT "CAR": J: "HAD WHEEL-SPIN FOR": (T + Tl): "SECONDS" 1160 LET Q(J) ; 1 1170 LET A ; (F - C(J) * (S(J) * S(J))) / M(J) 1180 GOTO 1210 1190 LET A ; B(J) - C(J) * (S(J) * S(J)) I M(J) 1200 REM *** S IS THE SPEED IN FT/SEC 1210 LET S(J) ; S(J) + A * .1 1220 REM *.* X IS THE DISTANCE IN FT 1230 LET X(J) ; X(J) + S(J) • .1 1240 NEXT J 1250 REM .*. TEST FOR FINISH 1260 IF X (1) >; 1320 THEN 1320 1270 IF X(2) >; 1320 THEN 1320 1280 NEXT Tl 1290 PRINT T+l: TAB(ll): INT(S(1)*6.81818)/10: TAB(20): INT(10*X(1))/10: 1300 PRINT TAB(35): INT(S(2)*6.81818)/10: TAB(44): INT(10*X(2))/10 1310 NEXT T 1320 IF X(l) > X(2) THEN 1340 1330 LET W ; 2 1340 LET T2 ; (X(W) - 1320) / SeW) 1350 LET T ; INT«T + Tl - T2) • 100 + .5) / 100 1360 LET X(W) ; 1320 1370 LET X(3 - W) ; INT«X(3 - W) - S(3 - W) * T2) • 10 + .5) / 10 1380 PRINT T: TAB(ll): INT(S(l) * 6.81818 + .5) / 10: TAB(20): X(l): 1390 PRINT TAB(35): INT(S(2) * 6.81818 + .5) / 10: TAB(44): X(2) 1400 PRINT TAB(25 * W - 10): "WINNER" 1410 PRINT 1420 PRINT "WOULD YOU LIKE ANOTHER GO?" 1430 GOSUB 1490 1440 LET 1$ ; "NO" 1450 RESTORE 1460 IF R$.; "YES" THEN 160 1470 STOP 1480 REM *** SUBROUTINE TO CHECK REPLIES 1490 IF 1$ "NO" THEN 1510 1500 PRINT "TYPE YES OR NO AND PRESS RETURN." 1510 INPUT R$ 1520 IF R$ ; "YES" THEN 1560 1530 IF R$ ; "NO" THEN 1560 1540 PRINT IIFEPLY IU; R$i 111 NOT UNDERSTOOD. 11:

1550 GOTO 1500 1560 RETURN 1570 END

81

Page 90: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

TRIAL RUN

DRAG CAR RACING

WELCOME TO THE DRAG STRIP. WOULD YOU LIKE INSTRUCTIONS? TYPE YES OR NO AND PRESS RETURN. ? NO WOULD YOU LIKE TO RACE AGAINST ME? ? YES I WILL HAVE CAR 1. PLEASE DESIGN CAR 2 HORSEPOWER =? 600 BACK AXLE RATIO =? 4 TYRE WIDTH (INCHES) =? 20 TYRE DIAMETER (FEET) =?

HORSEPOWER BACK AXLE RATIO TYRE WIDTH TYRE DIAMETER GO!

CAR 1 700 6 25 4

CAR 1

SUMMARY

ELAPSED TIME (SEC)

SPEED DISTANCE (MPH) (FT)

1 22.4 18.1 2 44.3 68.8 3 65.1 150.8 4 84.3 262 5 101. 7 400 6 117.1 561.9 7 130.5 744.7 8 141.9 945.5 9 151. 5 1161. 7 9.7 157.3 1320

WINNER

WOULD YOU LIKE ANOTHER GO? ? NO

CAR 2 600 4 20 3

CAR 2 SPEED DISTANCE (MPH) (FT)

82

21.1 41. 9 61. 8 80.5 97.8 113.5 127.5 l39.9 150.6 157.2

17.1 64.9 142.6 248.6 381 537.4 715.5 912.8 1126.8 1284.7

)f

\I' I

Page 91: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

1\

'.II

,~

FRUIT MACHINE

This program simulates a gaming machine in which the user places his stake, pulls a handle, and the machine then displays a row of three symbols. The possible symbols are the fruits: apple, cherry, lemon, orange, plum, and in addition there is a bar. In accordance with British gaming laws, all of these symbols have an equal chance of appearing. Certain combinations result in the user winning a prize as detailed below:

(i) Three bars wins the jackpot, and empties all the money from the machine.

(ii) Any row of three identical fruits wins ten times the original stake money.

(iii) Two identical fruits and a bar, arranged in any order, wins five times the original stake.

(iV) Two bars and a fruit in any order wins three times the original wager.

In the program the user is asked how many pence he wishes to bet each pull. The value input is checked to ensure that it is an integer number in the range 1 - 1000. Next the user chooses how many pulls he would like to have. After this number of turns he will be asked if he would like to continue or finish. The value input must be an integer number in the range 1 - 10. The program uses integer numbers from 1 - 6 to correspond to the various fruits and bar. Three random numbers in the range 1 - 6 are chosen, and the frUit symbols corresponding to these numbers are printed out. The computer assembles these three digits into one three digit number, which is checked against a list of winning combin­ations held as a block of data in the program. The user is then told the amount he has won or lost on this pull, and also the total winnings or losses to date. Should the jackpot be won, the program is automatically termin­ated, but otherwise the machine selects another row of three fruits and so on, until you have had the specified number of pulls. At this stage the user is asked whether he wishes to continue or finish.

Users may care to calculate the probability of winning the jackpot, and of winning the' other specified prizes. Neglecting the chance of some­one winning the jackpot, what percentage of the money is the machine likely to keep? If these calculations are performed, you will find that this machine will make a profit, but is far more generous than machines else­where.

83

Page 92: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

10 DIM Q$ (18) 20 PRINT TAB(6)1 "FRUIT MACHINE" 30 PRINT TAB(6)I "===== =======" 40 PRINT "WELCOME TO THE CASINO." 50 PRINT "ENTER YOUR THREE DIGIT LUCKY NUMBER, PLEASE." 60 INPUT J 70 IF J < 1000 THEN 100 80 PRINT "TRY AGAIN." 90 GOTO 50 100 FOR I = 1 TO J 110 LET N = RND(0) 120 NEXT I 130 LET S = 0 140 PRINT "HOW MUCH DO YOU WANT TO BET (PENCE) "I 150 INPUT B 160 IF B = INT(B) THEN 190 170 PRINT "I ONLY BET EVEN MONEY." 180 GOTO 140 190 IF B < 1001 THEN 220 200 PRINT "THAT'S TOO MUCH MONEY." 210 GOTO 140 220 IF B > 0 THEN 250 230 PRINT "COME ON, LET'S GET DOWN TO BUSINESS." 240 GOTO 140 250 PRINT "VERY GOOD -- AFTER HOW MANY PULLS WOULD YOU LIKE TO BE ASKED" 260 ·PRINT "WHETHER TO CONTINUE OR QUIT" 270 INPUT P 280 IF P <> INT(P) THEN 310 290 IF P < 1 THEN 310 300 IF P < 11 THEN 330 310 PRINT "RETYPE A SENSIBLE WHOLE NUMBER" 320 GOTO 270 330 PRINT 340 PRINT "WOULD YOU LIKE A LIST OF WINNING COMBINATIONS" 350 INPOIJ.' Q$ 360 IF Q$ = "NO" THEN 460 370 IF Q$ = "YES" THEN 400 380 PRINT "REPLY'" I Q$I '" NOT UNDERSTOOD. PLEASE ANSWER YES OR NO." 390 GOTO 350 400 PRINT "A ROW OF THREE BARS IS THE JACKPOT WHICH EMPTIES ALL THE" 410 PRINT "MONEY FROM THE MACHINE • ANY ROW OF THREE IDENTICAL FRUITS" 420 PRINT "WINS TEN TIMES YOUR STAKE MONEY AND ANY ROW MADE UP OF TWO" 430 PRINT "IDENTICAL FRUITS AND A BAR WINS FIVE TIMES YOUR STAKE" 440 PRINT "MONEY. ALSO A ROW OF TWO BARS AND A FRUIT WINS THREE TIMES" 450 PRINT "YOUR STAKE MONEY." 460 FOR K 1 TO P 470 LET T 0 480 FOR J = 1 TO 3 490 LET X = INT(RND(0) * 6) + 1 500 IF X <> 1 THEN 520 510 PRINT "APPLE "I 520 IF X <> 2 THEN 540 530'PRINT "CHERRY "I 540 IF X <> 3 THEN 560 550 PRINT "LEMON "I

84

....

Page 93: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

~ ,

560 IF X <> 4 THEN 580 570 PRINT "ORANGE "; 580 IF X <> 5 THEN 600 590 PRINT "PLUM "; 600 IF X <> 6 THEN 620 610 PRINT "BAR "; 620 LET T = T * 10 + X 630 NEXT J 640 IF T = 666 THEN 900 650 LET F = 10 660 FOR I = 1 TO 35 670 READ W 680 IF I <> 6 THEN 700 690 LET F = 5 700 IF I <> 21 THEN 720 710 LET F = 3 720 IF T = W THEN 770 730 NEXT I 740 LET F = 0 750 PRINT" YOU LOSE"; B, 760 GOTO 780 770 PRINT" YOU WIN "; F * B, 780 LET S = S + F * B - B 790 PRINT "TOTAL ="; S 800 RESTORE 810 NEXT K 820 PRINT 830 PRINT 840 PRINT 850 INPUT 860 IF 0$ 870 IF 0$

"WOULD YOU LIKE TO CONTINUE"; " TYPE YES OR NO AND PRESS RETURN" 0$ = "YES" THEN 460 = "NO" THEN 960

880 PRINT "REPLY'''; 0$; '" NOT UNDERSTOOD."; 890 GOTO. 840 900 PRINT "**** •• *.****************************n 910 PRINT "**** YOU HAVE WON THE JACKPOT ****" 920 PRINT "************************************" 930 PRINT 940 PRINT "YOU HAVE WON "; INT(RND(0) * 100 * B) - S 950 PRINT 960 IF S < 1000 THEN 1000 970 PRINT "THE HOUSE IS HAPPY TO SEE YOU LEAVE," 980 PRINT "WE HATE TO LOSE THAT MUCH MONEY." 990 STOP 1000 IF S > 0 THEN 1030 1010 PRINT "COME BACK SOON! YOUR MONEY IS APPRECIATED." 1020 STOP 1030 PRINT "WELL, YOU WON, BUT NOT 1040 DATA 111, 222, 333, 444, 555, 1050 DATA 161, 262, 363, 464, 565, 1060 DATA 166, 266, 366, 466, 566, 1070 DATA 661, 662, 663, 664, 665 108~ END

TOO MUCH." 116, 226, 336, 611, 622, 633, 616, 626, 636,

85

446, 556 644, 655 646, 656

....

,'J;, '" ,,;();:~: . j

,.~ .. :~.;

Page 94: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

TRIAL RUN

FRUIT MACHINE

WELCOME TO THE CASINO. ENTER YOUR THREE DIGIT LUCKY NUMBER, PLEASE. ? 123 HOW MUCH DO YOU WANT TO BET (PENCE) ? 100 VERY GOOD -- AFTER HOW MANY PULLS WOULD YOU LIK~ TO BE ASKED WHETHER TO CONTINUE OR QUIT ? 10

WOULD YOU LIKE A LIST OF WINNING COMBINATIONS ? YES A ROW OF THREE BARS IS THE JACKPOT WHICH EMPTIES ALL THE MONEY FROM THE MACHINE ANY ROW OF THREE IDENTICAL FRUITS WINS TEN TIMES YOUR STAKE MONEY AND ANY ROW MADE UP OF TWO IDENTICAL FRUITS AND A BAR WINS FIVE TIMES YOUR STAKE MONEY. ALSO A ROW OF TWO BARS AND A FRUIT WINS THREE TIMES YOUR STAKE MONEY. BAR PLUM ORANGE YOU LOSE 100 TOTAL =-100 BAR BAR APPLE YOU WIN 300 TOTAL = 100 CHERRY BAR ORANGE YOU LOSE 100 TOTAL = 0 PLUM BAR ORANGE YOU LOSE 100 TOTAL =-100 BAR PLUM APPLE YOU LOSE 100 TOTAL =-200 BAR ORANGE ORANGE YOU WIN 500 TOTAL 200 LEMON ORANGE PLUM YOU LOSE 100 TOTAL 100 ORANGE CHERRY BAR YOU LOSE 100 TOTAL 0 BAR ORANGE ORANGE YOU WIN 500 TOTAL 400 PLUM APPLE ORANGE YOU LOSE 100 TOTAL 300

WOULD YOU LIKE TO CONTINUE TYPE YES OR NO AND PRESS RETURN ? YES APPLE BAR CHERRY YOU LOSE 100 TOTAL = 200 CHERRY LEMON BAR YOU LOSE 100 TOTAL = 100 LEMON PLUM BAR YOU LOSE 100 TOTAL = 0 CHERRY BAR APPLE YOU LOSE 100 TOTAL =-100 BAR PLUM CHERRY YOU LOSE 100 TOTAL =-200 ORANGE LEMON ORANGE YOU LOSE 100 TOTAL =-300 LEMON APPLE LEMON YOU LOSE 100 TOTAL =-400 ORANGE APPLE BAR YOU LOSE 100 TOTAL =-500 CHERRY APPLE APPLE YOU LOSE 10~ TOTAL =-600 ORANGE LEMON ORANGE YOU LOSE 100 TOTAL =-700

WOULD YOU LIKE TO CONTINUE TYPE YES OR NO liND PRESS RETURN ? NO COME BACK SOON! YOUR MONEY IS APPRECIATED.

86

1;1

Page 95: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

I f,

.A

4.

MASTERMIND

This program is a computerised version of the world famous game. of Mastermind, which is manufactured by Invicta Plastics Ltd., and is protected by patents and copyright. The authors are most grateful to Invicta plastics for permission to use their trade name. The user attempts to deduce a four digit number which has been chosen at random by the computer. Each of the digits is in the range 1 - 6, and the same digit does not appear in the number more than once. After the user has made a guess, the computer reports how near this is to the correct answer in the following way:

(i) For each digit in the user's number which matches a digit in the computer's number, and is in the same position, the computer prints BLACK.

(ii) For each digit in the user's number which matches a digit in the computer's number, but is in the wrong position, the computer prints WHITE.

(iii) For any digit which is in the user's number, but does not occur in the computer's number, the computer does not print anything.

The user should use this information to make an improved guess. With practice a skilled user can get the correct answer with four or five guesses. Should the user become bored or admit defeat, the game may be terminated by typing 9999. When the correct answer has been guessed, the computer reports how many guesses were taken, and asks if the user would like to try again with a new number.

The guessed number is checked to ensure that it comprises four digits, that each digit is in the range 1 - 6, and that the same digit does not occur more than once.

Users may care to calculate the likelihood (probability) of guessing the correct number with the first guess, and also the probability of choosing a combination which gives two WHITE's with the first guess.

87

Page 96: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

10 DIM C(4), N(4), Q$(10) 20 PRINT TAB(5); "MASTERMIND" 30 PRINT TAB(5); "==========" 40 PRINT 50 REM C HOLDS THE 4 DIGITS OF THE COMPUTER'S NUMBER, N THE USER'S NO. 60 PRINT "WOULD YOU LIKE INSTRUCTIONS?" 70 PRINT "TYPE YES OR NO & PRESS RETURN." 80 INPUT Q$ 90 IF Q$ = "NO" THEN 480 100 IF Q$ = "YES" THEN 130 110 PRINT" REPLY '''; Q$; NOT UNDERSTOOD. PLEASE ANSWER YES OR NO" 120 GOTO 80 130 PRINT 140 PRINT "THE OBJECT OF THIS GAME IS TO GUESS A FOUR DIGIT NUMBER" 150 PRINT "WHICH THE COMPUTER HAS CHOSEN. EACH DIGIT IS IN THE RANGE" 160 PRINT "I TO 6. NONE OF THE DIGITS APPEARS MORE THAN ONCE." 170 PRINT "FOR EXAMPLE 1234, 5361 & 4236 ARE ALL VALID NUMBERS." 180 PRINT "123 (NOT 4 DIGITS LONG) ," 190 PRINT "5369 (9 IS OUTSIDE THE ALLOWABLE RANGE)" 200 PRINT "& 4233 (3 APPEARS MORE THAN ONCE) ARE NOT VALID NUMBERS." 210 PRINT "THE COMPUTER WILL CHOOSE A NUMBER. YOU SHOULD TYPE IN YOUR" 220 PRINT "GUESS AT THIS NUMBER. THE COMPUTER COMPARES YOUR GUESS WITH" 230 PRINT "IT'S NUMBER & PRINTS INFORMATION TO TELL YOU HOW CLOSE YOU" 240 PRINT "WERE." 250 PRINT "FOR EACH DIGIT IN YOUR NUMBER WHICH MATCHES A DIGIT IN THE" 260 PRINT "COMPUTER'S NUMBER & IS IN THE SAME POSITION" 270 PRINT "THE COMPUTER PRINTS ·BLACK·." 280 PRINT "IF A DIGIT MATCHES BUT IS NOT IN THE SAME POSITION IT"; 290 PRINT" PRINTS ·WHITE· ... 300 PRINT "FOR EXAMPLE, SUPPOSE THE COMPUTER'S NUMBER IS 2361 & YOU" 310 PRINT "GUESSED 1325 THE COMPUTER WOULD PRINT :-" 320 PRINT "BLACK (FOR THE 3)" 330 PRINT "WHITE (FOR THE I)" 340 PRINT "WHITE (FOR THE 2)" 350 PRINT "NOTHING IS PRINTED FOR THE 5 SINCE IT DOES NOT APPEAR IN" 360 PRINT "THE COMPUTER' S NUMBER." 370 PRINT "YOU SHOULD THEN TRY ANOTHER GUESS. FOR EXAMPLE" 380 PRINT "1365,WHICH WOULD GIVE THE RESULT :-" 390 PRINT "BLACK" 400 PRINT "BLACK" 410 PRINT "WHITE" 420 PRINT "WHEN YOU HAVE WORKED OUT THE NUMBER THE COMPUTER WILL PICK" 430 PRINT" A NEW NUMBER & START AGAIN." 440 PRINT "IF YOU CANNOT WORK OUT THE NUMBER, TYPE IN 9999 AND" 450 PRINT "YOU WILL BE TOLD THE ANSWER." 460 PRINT "GOOD LUCK." 470 REM CHOOSE RANDOM DIGITS IN THE RANGE 1 TO 6 480 PRINT 490 FOR I = 1 TO 4 500 LET A = INT(RND(0) * 6) + 1 510 FOR J = 1 TO I - 1 520 REM COMPARE THE RANDOM DIGIT WITH DIGITS ALREADY STORED IN ORDER 530 REM TO PREVENT DUPLICATES. 540 IF A C(J) THEN 500 550 NEXT J

88

\1

11

~

p.

lfjj

~/l

~ ...

."

-'j

,~.

'I'

Y

i,1

~,

(\

'It'

')'

~~

~~~ ,\ ,; ... ~~

i)

t;:,

Page 97: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

,.

.~

.,;,

')

l

~-w'.

, '1 ~

~

.~

560 LET C(I) = A 570 NEXT I 580 LET I = 0 590 PRINT "TYPE IN YOUR GUESS" 600 INPUT M 610 REM M IS THE USER'S GUESS AT THE NUMBER 620 IF M <> 9999 THEN 650 630 PRINT "THE CO~IPUTER'S NUMBER WAS"; C(l); C(2); C(3); C(4) 640 GOTO 1050 650 IF M < 1000 THEN 670 660 IF M < 10000 THEN 690 670 PRIN1' "PLEASE TYPE IN EXACTLY 4 DIGITS" 680 GOTO 600 690 IF M = (C(l) * 1000 + C(2) * 100 + C(3) * 10 + C(4)) THEN 1040 700 REM SPLIT THE USER'S 4 DIGIT NUMBER INTO 4 SINGLE DIGITS 710 LET N(l) INT(M / 1000) 720 LET N(2) = INT(M / 100) - 10 * N(l) 730 LET N(3) = INT(M / 10) - 100 * N(l) - 10 * N(2) 740 LET N(4) = M - INT(M / 10) * 10 750 FOR J = 1 TO 4 760 LET B = N (J) 770 IF B < 1 THEN 800 780 IF B < 7 THEN 820 790 REM INVALID NUMBER. READ A NEW ONE 800 PRINT "DIGITS MUST BE IN THE RANGE 1 TO 6" 810 G01'O 600 820 FOR K = J + 1 TO 4 830 IF B <> N(K) THEN 860 840 PRIN1' "DUPLICATE DIGITS NOT ALLOWED" 850 GOTO 600 860 NEXT K 870 NEXT J 880 REM USER'S NUMBER IS VALID. INCREASE COUNT OF TRIES .NOW COMPARE N 890 LET I = I + 1 900 REM MATCHING DIGITS IN SAME POSITION FOUND & PRINTED 910 FOR J = 1 TO 4 920 IF C(J) <> N(J) THEN 94e 930 PRINT "BLACK" 940 NEXT J 950 REM MATCHING DIGITS IN DIFFERENT POSITIONS FOUND & PRINTED 960 FOR J = 1 TO 4 970 FOR K = 1 TO 4 980 IF J = K THEN 1010 990 IF C(J) <> N(K) THEN 1010 1000 PRINT "WHITE" 1010 NEXT K 1020 NEXT J 1030 GOTO 600 1040 PRINT "WELL DONE. NUMBER FOUND IN"; I + 1; " TRIES." 1050 PRINT "WOULD YOU LIKE ANOTHER GAME (YES/NO)?"; 1060 INPUT 0$ 1070 IF 0$ = "YES" THEN 480 1080 IF 0$ = "NO" THEN 1110 1090 PRIN'r "REPLY'''; 0$; '" NOT UNDERSTOOD. PLEASE ANSWER YES OR NO" 1100 GOTO 1060

89

Page 98: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

1118 PRINT 112S PRINT "THANK YOU FOR PLAYING." 113S END

TRIAL RUN

MASTERMIND

WOULD YOU LIKE INSTRUCTIONS? TYPE YES OR NO & PRESS RETURN. ? NO

TYPE IN YOUR GUESS ? 1234 BLACK WHITE WHITE ? 1354 BLACK BLACK WHITE ? 1643 BLACK BLACK WHITE WHITE ? 1346 WELL DONE. NUMBER FOUND IN 4 TRIES. wOULD YOU LIKE ANOTHER GAME (YES/NO)? .? YES TYPE IN YOUR GUESS ? 1234 BLACK WHITE WHITE ? 1326 WHITE WHITE WHITE ? 4261 BLACK BLACK WHITE WHITE ? 6241 WELL DONE. NUMBER FOUND IN 4 TRIES. WOULD YOU LIKE ANOTHER GAME (YES/NO)? ? NO THANK YOU FOR PLAYING.

90

>;

Page 99: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

I)

1\

tI,

1'1

~

>(i\{

S PI E S

In this game the user has to guess where four Russian spies are hidden on a 10 x 10 board. To make a guess the user gives MI6 the coordinates of a square (X, y) to which our liquidation sqad is to be despatched. If the squad finds an enemy agent there, he is suitably diSpos­ed of! The approximate distance of any remaining Russians from the liquidation squad is then printed out, and the user is given another guess. If you fail to locate the spies in ten guesses, the program will tell you where they were hiding.

The distances are deliberately only given approximately to make the problem slightly harder. Beginners will find it easy to wiI;1 if they use a scale drawing of the grid and a pair of compasses to locate the spies. The program may be made more difficult by ;

(i) decreasing the maximum number of turns by changing the term G (line 30).

(ii) increaSing the size of the board defined by B (line 40).

(iii) the nuniber of spies may be increased by changing the value of S (line 20), and also the dimension of 4 for P (line 10).

91

Page 100: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

10 DIM A(4, 2), Q$ (18) 28 LET S = 4 38 LET G = IB 40 LET B = 111 50 PRINT TAB(10), "SPIES" 60 PRINT TAB(IB), "=====" 70 PRINT "WOULD YOU LIKE INSTRUCTIONS? TYPE YES OR NO & PRESS RETURN." 80 INPUT Q$ 90 IF Q$ = "YES" THEN 130 IB0 IF Q$ = "NO" THEN 220 110 PRINT "REPLY"'; Q$; "' NOT UNDERSTOOD. PLEASE ANSWER YES OR NO." 120 GOTO 80 130 PRINT "THE OBJECT OF THIS GAME IS TO LIQUIDATE"; S; "SPIES" 140 PRINT "HIDDEN ON A "; B; "BY"; B; "GRID. THE ORIGIN IS POSITION 8,8" 150 PRINT "YOU GUESS THEIR POSITION BY TYPING COORDINATES AS" 160 PRINT "TWO WHOLE NUMBERS BETWEEN 8 AND "; B-1; ", SEPARATED BY A " 178 PRINT "COMMA. THE FIRST NUMBER IS THE DISTANCE TO THE RIGHT OF THE" 180 PRINT "ORIGIN AND THE SECOND NO. IS THE DISTANCE ABOVE THE ORIGIN." 190 PRINT 280 PRINT "YOU GET"; G; "TRIES, AND AFTER EACH YOU WILL BE TOLD" 2IB PRINT "HOW FAR YOU ARE FROM EACH REMAINING SPY." 220 FOR J = 1 TO 2 230 FOR I = 1 TO S 240 LET A(I, J) = INT(B * RND(0)) 250 NEXT I 260 NEXT J 270 FOR T = 1 TO G 280 PRINT 290 PRINT "TURN NO."; T; " TYPE YOUR GUESS & PRESS RETURN"; 300 INPUT X, Y 310 IF INT(X) + INT(Y) <> X + Y THEN 360 320 IF X > B-1 THEN 360 330 IF Y > B-1 THEN 360 340 IF X < 0 THEN 360 350 IF X >= 0 THEN 380 360 PRINT "VALUES MUST BE BETWEEN 0 & "; B-1; " RETYPE CORRECTLY." 370 GOTO 300 380 FOR I = 1 TO S 390 IF A(I, 1) = -1 THEN 470 400 IF A(I, 1) <> X THEN 458 410 IF A(I, 2) <> Y THEN 450 420 LET A(I, 1) = -1 430 PRINT "YOU HAVE FOUND SPY"; I 440 GOTO 470 450 LET D = SQR«A(I, 1) - X) ** 2 + (A(I, 2) - Y) ** 2) 460 PRINT "YOU ARE"; INT (D * IB) / 10; " UNITS FROM SPY"; 470 NEXT I 480 FOR J = 1 TO 4 490 IF A(J, 1) <> -1 THEN 540 500 NEXT J 5IB PRINT 528 PRINT "CONGRATULATIONS - YOU FOUND ALL"; S; "SPIES IN"; T; "TURNS!" 538 GOTO 610 548 NEXT T 550 PRINT

92

\< I

Page 101: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

.. :.

. ."

'1

./

I)

If.-I

560 PRINT "YOU HAVE HAD THE MAX. OF": G: "TRIES. THEY WERE HIDING AT:" 570 FOR I = 1 TO S 580 IF A(I, 1) = -1 THEN 600 590 PRINT II Spy "; 11 II (II; A ( I , 1); II, n; A ( I , 2); ") 11 600 NEXT I 610 PRINT 620 PRINT "WOULD YOU LIKE ANOTHER GAME? TYPE YES OR NO & PRESS RETURN" 630 INPUT 0$ 640 IF 0$ = "YES" THEN 680 650 IF 0$ = "NO" THEN 700 660 PRINT" REPLY ''': 0$: NOT UNDERSTOOD. PLEASE ANSWER YES OR NO." 670 GOTO 630 680 PRINT S: "MORE SPIES ARE NOW IN HIDING." 690 GOTO 220 700 PRINT "GOODBYE" 710 END

TRIAL RUN

SPIES

WOULD YOU LIKE INSTRUCTIONS? TYPE YES OR NO & PRESS RETURN. ? NO

TURN NO. 1 TYPE YOUR GUESS YOU ARE 8 UNITS FROM SPY 1 YOU ARE 8 UNITS FROM SPY 2 YOU ARE 2 UNITS FROM SPY 3 YOU ARE 4.4 UNITS FROM SPY

& PRESS RETURN? 1,5

TURN NO.2 TYPE YOUR GUESS & PRESS RETURN? 1,7 YOU ARE 9.2 UNITS FROM SPY 1 YOU ARE 8.2 UNITS FROM SPY 2 YOU HAVE FOUND SPY 3 YOU ARE 6.3 UNITS FROM Spy

TURN NO. 3 TYPE YOUR GUESS YOU ARE 4.1 UNITS FROM Spy YOU HAVE FOUND SPY 2 YOU ARE 7.2 UNITS FROM SPY

PRESS RETURN? 9,5

TURN NO.4 TYPE YOUR GUESS & PRESS RETURN? 8,1 YOU HAVE FOUND SPY 1 YOU ARE 5 UNITS FNOM SPY 4

TURN NO.5 TYPE YOUR GUESS & PRESS RETURN? 3,1 YOU HAVE FOUND SPY 4

CONGRATULATIONS - YOU FOUND ALL 4 SPIES IN 5 TURNS!

WOULD YOU LIKE ANOTHER GAME? TYPE YES OR NO & PRESS RETURN ? NO GOODBYE

93

Page 102: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

T I MM

This program is based on the ancient oriental game of Nim for two players. In the game there are several rows of counters and the two players alternately remove one or more counters from anyone row. The winner is the player who eventually clears the board. In the program the user plays against the computer on a board containing only three rows. The computer randomly chooses a board and prints it out. The user is then asked if he wishes to start and if so he is asked how many counters he would like to remove from anyone row. The new board is then printed out and the computer decides its move ... and so on.

When the game is finished the running total is printed showing how many games have been won/drawn/lost. The user then decides whether he would like to continue or to finish. If the user wishes to continue and he has just lost the game, he is asked if he would like to replay the same board again.

The computer is however programmed with the ultimate strategy and will never miss a winning move if one is possible. Despite this the user should always be able to beat the computer since he can decide after seeing the board whether it is advantageous to start.

At all times in the running of the program, answers to questions are checked to ensure that they are either YES or NO and the following checks are performed on the numbers typed by the user to specify his move (row number, number of counters to be removed) :

(i) that both numbers are integers

(ii) that the row number is 1, 2 or 3

(iii) that the number of counters to be removed is greater than or equal to 1 and smaller than or equal to the maximum number of counters remaining on that row.

The winning strategy for playing Nim is described in the rest of this section. You are warned that if you read and understand thiS, you should always win, but this will detract from the pleasure and satisfaction of winning the game.

Any board exists in one of two conditions: safe or unsafe. From an unsafe board the next move may create either a safe or an unsafe board, but any move from a safe board produces an unsafe board. The aim is to ensure that after your move the board is always in a safe condition, hence the computer is forced to make a move leaving an unsafe board. Contin-

94

\)

fl.

".,

I',

i,'

{I

Page 103: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

r

I:

i\

"1

't

?'

uation of this strategy ensures that you must eventually win. If the computer moves and attains a safe state, it will continue to make moves yielding safe states, and you are bound to lose.

If the number of counters in each row of the board is expressed as a binary number, manipulation of the binary numbers indicates if the board is safe or unsafe, for example :

Row Board Denary Binary

(1) ** 2 o 1 o

(2) *** 3 o 1 1

(3) **** 4 1 o o

1 2 I

The columns of the binary digits are added (in denary), and if any of the column totals is odd then the board is in an unsafe condition. The example above is therefore unsafe. To decide how to make a move which will give a safe board one examines the most Significant odd column total (in this case the left hand total 1). The only way to make this column total even is to remove the binary 1, that is to reduce the row of four counters. In this example the right hand total is also odd, hence the move enVisaged must make both of these totals even without making the middle total odd. Inspection shows that the only way of achieving this is to remove three counters from row (3) :

** 2 o 1 o

*** 3 o 1 1

* 1 o o 1

o 2 2

Winning this game depends on you establishing whether the starting board is safe or unsafe, and deciding to start if and only if the board is unsafe. Of course you must continue to make the correct moves thereafter since the computer has no sporting instinct:

95

Page 104: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

111 DIM A(3, 3), 0$ (9), W(3), X (3), Y (3) 2B PRINT TAB(8); "TIMM" 3B PRINT TAB(8); "====" 4B LET D = R = C = B 58 PRINT 6B PRINT "WOULD YOU LIKE INSTRUCTIONS?" 78 PRINT "TYPE YES OR NO, THEN PRESS RETURN." 8B GOSOB 167B 9B IF 0$ = "NO" THEN 198 188 PRINT "IN THIS GAME THERE ARE 3 HORIZONTAL ROWS OF COUNTERS." 118 PRINT "IN ONE TURN YOU MAY TAKE AS MANY COUNTERS AS YOU LIKE" 12B PRINT "FROM ONE ROW. THE COMPUTER AND YOU HAVE ALTERNATE TURNS," 138 PRINT "AND THE WINNER IS THE ONE WHO EVENTUALLY CLEARS THE BOARD." 148 PRINT "WHEN IT IS YOUR GO, TYPE IN YOUR INSTRUCTIONS" 158 PRINT "EG. FOR ROW 3 REMOVE 2 COUNTERS TYPE 3,2" 16B PRINT "YOU MAY CONCEDE THE GAME WHEN IT IS YOUR TURN BY TYPING 8,0" 17B PRINT "A SKILFUL PLAYER CAN BEAT THE COMPUTER EVERY TIME." 188 LET C = RND(B) 19B FOR I = 1 TO 3 288 REM ***CHOOSE RANDOM NUMBERS IN THE RANGE 1-5. STORE THESE NUMBERS 218 REM ***IN X , W ARRAYS (X CHANGES W DOES NOT) 228 LET XII) = INT(RND(B) * 5 + 1) 238 LET W(I) = XII) 248 NEXT I 25B PRINT 26B ·PRINT "GAME"; (D + R + 1) 278 REM ***PRINT BOARD 288 GOSUB 1468 298 PRINT "WOULD YOU LIKE THE FIRST TURN?" 38B GOSOB 1678 318 IF 0$ = "YES" THEN 838 32B PRINT "COMPUTERS GO" 338 MAT A = ZER 34B FOR I = 1 TO 3 350 REM?**CHANGE VALUES OF X ARRAY INTO BINARY AND STORE IN A(3 , 3) 368 LETA(I, 3) INT (X (I) / 4) 378 LET A(I, 2) = INT(X(I) / 2) - 2 * A(I, 3) 38B LET A(I, 1) = XII) - INT(X(I) / 2) * 2 398 NEXT I 488 REM ***LET ARRAY Y(3) = SUM OF COLUMNS OF A(3 , 3) ARRAY. UB LET Y(l) = A(l, 1) + A(2, 1) + A(3, 1) 428 LET Y(2) = A(l, 2) + A(2, 2) + A(3, 2) 43B LET Y(3) = A(l, 3) + A(2, 3) + A(3, 3) 448 REM ***CHECK TO SEE IF ANY VALUE OF Y ARRAY IS ODD 458 FOR J = 3 TO 1 STEP -1 468 IF Y(J) = 1 THEN 59B 47B IF Y(J) = 3 THEN 598 488 NEXT J 498 REM ***MAKE MOVE IF ALL Y VALUES EVEN 58B IF X(2) = 0 THEN 550 518 LET X(2) = X(2) - 1 5211 LET I = 2 538 LET Z = 1 5411 "GOTO 7 511 558 LET I = 3

96

Page 105: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

,,..

r'

'I

560 LET X(3) ; X(3) - 1 570 GOTO 530 580 REM ***CHOOSE MOVE TO MAKE ALL Y VALUES EVEN. 590 FOR I ; 1 TO 3 600 LET Z ; 0 610 IF A(I, J) ; 1 THEN 630 620 NEXT I 630 LET A(I, J) ; 0 640 LET Z ; Z + 2 ** (J - 1) 650 LET J ; J - 1 660 IF J ; 0 THEN 740 670 IF Y(J) ; 1 THEN 690 680 IF Y(J) <> 3 THEN 650 690 IF A(I, J) ; 1 THEN 630 700 LET A(I, J) 1 710 LET Z ; Z - 2 ** (J - 1) 720 GOTO 650 730 LET A(I, J) ; 0 740 LET X(I) ; X(I) - Z 750 PRINT II ","I Z 760 LET T ; X(l) + X(2) + X(3) 770 IF T ; 0 THEN 1160 780 IF T > 1 THEN 820 790 PRINT "THE COMPUTER HAS CONCEDED THE GAME." 800 GOTO 1250 810 REM ***PRINT BOARD 820 GOSUB 1460 830 PRINT "YOUR GO" 840 INPUT E, F 850 IF E + F ; INT(E) + INT(F) THEN 880 860 PRINT "NUMBERS MUST BE WHOLE NUMBERS - RETYPE CORRECTLY" 870 GOTO 840 880 IF E ; 0 THEN 920 890 IF (E - 1) * (E - 2) * (E - 3) ; 0 THEN 950 900 PRINT "THE ROW NUMBER IS INCORRECT, TRY AGAIN." 910 GOTO 840 920 IF F <> 0 THEN 900 930 PRINT "YOU HAVE CONCEDED THE GAME!" 940 GOTO 1170 950 IF X(E) >= F THEN 980 960 PRINT "THERE ARE ONLY" 1 X (E) 1 "COUNTERS IN ROW" 1 E 1 "TRY AGAIN" 970 GOTO 840 980 IF F > 0 THEN 1020 990 PRINT "NUMBER OF COUNTERS IS INCORRECT - TRY AGAIN." 1000 GOTO 840 1010 REM *'-REMOVE F COUNTERS FROM ROW E 1020 LET X(E) ; X(E) - F 1030 IF E > 1 THEN 1050 1040 LET C = RND(0) 1050 LET B = 1 1060 FOR I = 1 TO 3 1070 IF X(I) > 0 THEN 1090 1080 LET B = B + 1 1090 NEXT I 1100 IF B = 4 THEN 1240

97

Page 106: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

1110 IF B = 3 THEN 1140 1120 GOSUB 1460 1130 GOTO 320 1140 PRINT "THE COMPUTER MUST WIN!" 1150 GOTO 11 70 1160 PRINT "HARD LUCK THE COMPUTER HAS WON" 1170 LET 0 = 0 + 1 1180 LET C = RND(0) 1190 LET 02 = 0 1200 PRINT "THE SCORE SO FAR IS:-" 1210 PRINT "COMPUTER:-", 0 1220 PRINT "YOU:-", R 1230 GOTO 1280 1240 PRINT "YOU HAVE WON, WELL DONE!" 1250 LET R = R + 1 1260 LET 02 = 1 1270 GOTO 1200 1280 IF 0 + R < 20 THEN 1310 1290 PRINT "THE COMPUTER IS WORN OUT - NO MORE GAMES." 1300 GOTO 1660 1310 PRINT "WOULD YOU LIKE ANOTHER GAME?" 1320 GOSUB 1670 1330 IF 0$ = "YES" THEN 1360 1340 PRINT "OK GOODBYE" 1350 STOP 1360 IF 02 = 1 THEN 190 1370 PRINT "DO YOU WANT TO RE-PLAY THE SAME BOARD?" 1380 GOSUB 1670 1390 IF 0$ = "NO" THEN 190 1400 REM ***LET X ARRAY = 1'1 ARRAY (STORED VALUES OF LAST BOARD) 1410 FOR I = 1 TO 3 1420 LET X(I) = 1'1(1) 1430 NEXT I 1440 GOTO 250 1450 REM ***SUBROUTINE TO PRINT BOARD 1460 PRINT "-------------,, 1470 LET I = 1 1480 IF X(I) <> 5 THEN 1500 1490 1500 1510 1520 1530 1540 1550 1560 1570 1580 1590 16G0 1610 1620 1630

PRINT "I X X X X X lit IF X(I) <> 0 THEN 1520 PRINT "I I" IF X(I) <> THEN 1540 PRINT "I X I" IF X (I) <> 2 THEN 1560 PRINT "I X X I" IF X(I) <> 3 THEN 1580 PRINT "I X X X I" IF X (I) <> 4 THEN 1600 PRINT "I X X X X I" IF I = 3 THEN 1640 PRINT' "I I" LET I = I + 1 GOTO 1480

1640 PRINT ,,-------------" 1650 RETURN

98

f.\

~.

1.'

Page 107: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

I

r;, I

,

'7

I~

~, I· I

, ( .. \. ! "\

166a REM ******SUBROUTINE TO CHECK REPLIES 1670 INPUT Q$ 1680 IF Q$ = "YES" THEN 1720 1690 IF Q$ = "NO" THEN 1720 1700 PRINT "REPLY'''; Q$; ". NOT UNDERSTOOD. PLEASE ANSWER YES OR NO" 171a GOTO 167a 1720 RETURN 1730 END

TRIAL RUN

TIMM

WOULD YOU LIKE INSTRUCTIONS? TYPE YES OR NO, THEN PRESS RETURN. ? NO

GAME 1

x X X

X X X X X

I. X X

WOULD YOU LIKE THE FIRST TURN? ? NO COMPUTERS GO

2 , 4

I X X X I I I 1 X I I X X

YOUR GO ? 1,2

I X I I X I I X X

COMPUTERS GO 3 , 2

I I I I

99

Page 108: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

~ :~ ( -_\I

Prime can help the

student-

College students at Preston, Salford, Ealing, Willesden and Kingston are already using Prime computers to speed their learning. And of course while they are at college Prime timeshare keeps their files on the computer safe from loss or damage. To learn about Prime timesharing systems call Roger Charters on 0234 65121 Prime Computer (UK) Ltd., The Merton Centre, PRIME St Peter's Street; Bedford MK40 2PN.

COMPUTER UK

y"

•. '

t,'

'I, .i

.'

Page 109: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

"". "

~

~:

Page 110: l BASIC - datassette.nyc3.cdn.digitaloceanspaces.com

/ •

•• •

, About this book •••••

• • •

This, the fi"s t · all-Briti sh book of comnu ter-aided ~alculations, simulat i ons and ga~es is a carefully-de~j gned practo cal guide a;med

,

at the firs t-time computer user. It i s J.i table fOI schools, <.:0116;€· 'In ,l pe r s ona l use. Anybody wi th access .0 a small computer t .~t

supports BASIC wi ll be able to make irr.nedia t e us e of it. -

There are 24 programs described and listed ii. t h" "reElS of biology mathematics~ chemi s~I~ and p.h~'''':'':''"'4 Some are simulat~ ons of real­li:e situa tions. For example:

\ , • Predator-;;>rey in ecology Pictures of atomic orbitals Motion of satellites

r:;Off'e games ,~re included - t:GZ' (; .::.. -pie : .:..:. ::mula ted rr") ~O..:."" car racing "":',·ii "

., .

""rl ; nd" ~ I. . •

'0 ,,-ddi tional fea tures include :

* 1.11 programs are wri tten in the mos t elementary ,ubE et o f BASTC

* Pa per tapF>s of the programs available

* Eo robu,c s to be almos t student-proof

-

'1 ,'1e authors ':..re .hased a t wughooro ;;. gh Universi ty &... "he Po './"" ~ .:':~ ;J.ic,

~lolverhamptcn . Dr Lee :5 th e aut"1,or of ··.:cnciscI'1 .... J.,. -'nic ('he -' f'q"

(Van Nos era I ,d ' b.L,· ry r Beech has W2 L tten Inorganic Tr. 'i1o dynanu( s" ( Van Nostrand); "FORTRAN Ii/ in :::l .... :.-"i st 'j" (Wiley) ; "B~i.SIC in C r~m ! I)"

(S igma) and has edited "Computer As~~~ ted Met'1ods~.. "' nce EdUCe' -"

/Per9a.mon )i he is also Project Director of the Physical Sciences PrO';ll It

1:;;;( .. nge. •

A catalogue of publi cations is available from :

Sigma Technical Press ?' Di ppons Mill Close 2 ~ t tenhall

Wo -e rhampton ~~/6 BHH U K

,

\

I:iJ3N : 0 905104 03

- . \

-, , -•

• ,

• •

• - (;")

en

I

I