Basic Basic
Basic Basic
Basic Basic
basic basic
AN INTRODUCTION TO COMPUTER PROGRAMMING
IN BASIC LANGUAGE
2 FJt< Y=l T0 10
s LET C=O second edition
10 F0K x= l rn ~o
20 LET F=INTC2*KNDC1>> JAMES S. COAN
30 IF F= l THE,'J 60
40 PKIN T "T"J
50 G!JT0 100
58 r<EM C CJ UN TS THE NUMBER 0 F HEADS
60 LET C=C+ 1
10 p tH N T II H "J
100 NEXT X
110 Pt"<INT
120 PRINT "HEADS "JCJ"0UT 0F 50 FLIPS"
l 2S NEXT Y
130 END
KUN
FLIP- 1
HTTTTTHTTHHTTHTTTTTTTTHTHHHHHHTTTTTHTHHHTHHHTTHHTT
HEADS 21 0UT 0F 50 FLIPS
HTTHTHHTTHTTHTHHTTTHHHTTTTTTHHHHTTHTHTHHHHTHHfTHH~
~£ADS 26 0UT 50 FLIPS 0r
HTHTTTHTTHTTHTTTTTTTTHTTHTTTHTHTHTTTHTTTTTHHHTHHHT .
HEADS 11 0UT 0F 50 FLIPS
THfTTfTTHTHTHTHHTHHHHHHTHTTTHHTTHTTTTTTTHHTHTTHHTT
HEADS 21 0UT 0F 50 FLIPS
TTHHTTTTHTHHTTHTHTHHHHTTHHTHHHTTTTTTHTHHHHTtHTHHTT
HEADS 24 0UT 0F 50 FLIPS
HTH THHHHHHH THTTTTTTTH TTHHHHHH TTTH TTTTH TH TTHH THHTHH
HEADS 26 0UT 0F 50 FLIPS
HTTTTTHTTTTHHTTHTTHTHHHHTHTHHTTHHHHTHTTHTHTHTTTTHT·
HEADS 22 0UT 0F 50 FLIPS
THTHHHHHHTHTHHHHTTTHHTHTHHHHHTHHHTHHTHHHHHHHTTTHJH
rlEAD:S 34 01.JT eJF 50 FLIPS ,
rlfTHHTHrlffrlfTTTTTHHHTTTHTTTHHTTHTHTHHHHHTTHHTH~THT
rl~AUS 24 JUT JF 50 FLIPS ·
TfHHTHHfrlHrlTHTTHTHHHTHHHTTTTHHHTTTTHTTHTHHTHHTHTHT
HEADS 26 0JT 0F 50 FLIPS
H .--\YDE~
1.M,'JE
Basic B IC
An Introduction to Computer Programming in BASIC Language
Hayden Computer Programming Series
JAMES S. COAN
[[]
HAYDEN BOOK COMPANY, INC.
Rochelle Park, New Jersey
Library of Congress Cataloging in Publication Data
Coan, James S
Basic BASIC: an introduction to computer
programming in BASIC language.
(Hayden computer programming series)
Includes indexes.
1. Basic (Computer program language).
2. Electronic digital computers-Programmi ng.
I. Title.
OA76.73.B3C62 1978 001.6'424 77-14640
ISBN 0-8104-5107-7
ISBN 0-8104-5106-9 pbk.
Copyright@ 1970, 1978 by HAYDEN BOOK COMPANY, INC. All rights re-
served. No part of this book may be reprinted, or reproduced, or utilized in
any form or by any electronic, mechanical, or other means, now known or
hereafter invented, including photocopying and recording, or in any in-
formation storage and retrieval system, without permission in writing from
the Pubiisher.
Printed in the United States of America
4 5 6 7 8 9 PRINTING
79 80 81 82 83 84 85 YEAR
Preface
With the increasing availability of computer access through remote terminals
and time sharing, more and more schools and colleges are able to introduce
programming to substantial numbers of students.
This book is an attempt to incorporate computer programming, using BASIC
language, and the teaching of mathematics. I believe the two activities support
each other.
Flowcharts are used throughout the text. The general approach is to begin
with short complete programs and then simply and methodically build them
into larger programs. Each new capability or new organization of capabilities
is presented to create a desired effect in a program. Details are introduced
only as they become necessary or useful for the writing of a program, rather
than as sets of facts to be memorized in case a particular situation should
ever arise. Over 125 prngrams are used to achieve this.
All of the elementary BASIC language capabilities are presented in the first
five chapters and Chap. 7. Chapter 6 and Chaps. 8-13 emphasize applications.
The first seven chapters may be studied in conjunction with, or at any time fol-
lowing, a first-year algebra course. Chapters 8-13 are applications oriented,
covering many of the popular topics of precalculus mathematics, with all of the
required algorithms developed in the text. Thus, this text is suitable for use
either as a supplementary text to be incorporated into existing mathematics
courses, or as the text for a course or unit to cover programming alone.
Appendices A and B, respectively, present information for the operation
of programs on paper tape and a few comments on error diagnosis. Appendix
C introduces two formatting capabilities that are available on some time-
sharing systems. Flowchart shapes are summarized in App~ndix D. A sum-
mary of BASIC statement types is provided in Appendix E and an index of
all the programs in Chaps. 2-13 is provided in Appendix F.
Many of the problems in the book are intended to enable the student to
develop interesting mathematical concepts upon seeing the printed results of
program RUNS. Possible solution prngrnms are given in Appendix G for the
even-numbered problems to give the student an indication of the correctness
of his program without being required to nm every program. However, par-
ticularly at the beginning, students derive greater beneHt from seeing programs
run (or not run) than from any other programming activity.
I wish to thank Germantown Friends School for its support in the prepara··
tion of this text. Thanks are due Mrs. Geoffrey Wilson for test teaching and
numerous students for test learning portions of the manuscript.
JAMES S. COAN
Philadelphia
Preface to the Second Edition
The First Edition of this book has been significantly enhanced by present-
ing character string handling and the use of data files. Since strings and files
involve language differences which depend on the computer, two versions are
presented. Demonstration programs are presented in Chap. 7 for both General
Electric Information Services BASIC and Hewlett-Packard BASIC.
The little used statement RESTORE is no longer discussed, and the INPUT
statement is now presented in Chap. 1.
Thanks are due to the Community Computer Corporation for assistance in
the p1eparntion of material for this Second Edition.
JAMES S. COAN
Philadelpllia
Contents
Index 263
Basic BASIC
An Introduction to Computer Programming in BASIC Language
CHAPTER 1
Introduction to BASIC
1-1 PRINT
No matter how complicated a particular set of instructions is, you will have
to tell the computer to put the results into some fo1 m discernible to yourself.
Therefore, let us begin with a discussion of the PRINT statement. If you want
the computer to write the following statement "THIS IS A SHORT PRO-
GRAM," you will type on the keybomd of the terminal as follows:
10 PRINT "THIS IS A SH0RT PR0GRAM•"
20 END
The computer, on proper instruction, will do exactly what you have set out
to do.
The two lines 10 and 20 constitute a complete program. Several comments
are in order here.
I) Note that every line in a program must begin with a positive integer.
2) The statement that we want to write out is in quotes; this may be used
to good advantage, for example, for headings and labels.
3) In many time-share systems, every program must have as its highest
numbered line the END statement. In some systems, the END statement
is optional.
1
2 Basic BASIC
4) Note that all the letters are capitals. The terminal you may use or may
not be restricted in this way. Note also that the letter "O" has a slash
mark to distinguish it from the digit "O." On some terminals the reverse
is true, the digit "O" has a slash and the letter "O" does not. On some
printers one is more nearly a circle than the other or one is nearly dia-
mond shaped. You can easily determine the method used by your equip-
ment by examining some sample output on your screen or "hard copy."
5) It is conventional although not required to use intervals of 10 for the
numbers of adjacent lines in a program. This is because any modifica-
tion in the program must also have line nu1nbers, So you can use the
!n-betv;.reer! numbers for that pt!rpo~e. !t sh0uk! be ccrnfnrt!ng ta knn·.v
at this point that the line numbers do not have to be typed in order.
No matter what order they are typed in, the computer will follow
numerical order in exeu1ting the program.
6) Each line of a program is called a program statement.
You probably think of the computer as something that more commonly
produces numerical results and you are partly correct. Suppose you wish to
multiply 23.4 by 91. One way of doing this on the computer would be to
write a program like this:
10 PRINT 23.4•91
20 ENO
Then on proper instruction the computer will type out the following and stop.
2129.4
00NE
Computers vary as to the message that gets printed here. Notice the absence
of quotes. In this case you have instructed the computer to perform an opera-
tion. Had you in fact wanted 23.4 ° 91 typed out, then you would change the
program. You might write the following:
10 PRINT "23.4>1<91=''.23•4*91
20 E·'llO
You have succeeded in instrncting the computer not only to perform an opera-
tion, but to print out the result in easily understandable form, which is desir-
able throughout mathematics. Notice the use of the comma here. The comma
may be used to separate the different parts of a PRINT statement. Used in
this way, a comma is called a delimiter. Notice too, that there are eight spaces
bctvv·ccn the C(fll~tls sign and the itUlll1Jef. r\ \Vay to eli111iuaiJ.-! an Lui 011e uf
them will be explained later, There ai e many fine points that we will discuss
as we progress, but for now we will take it in small quantities.
If we were limited to the PRINT and the END instructions, we would
quickly rctm n to using pencil and paper or an ordinary desk calculatoL With-
Introduction to BASIC 3
out some additional capability, the computer would soon disappear. This
brings us to the READ and DATA statements.
PRINT
Characters in quotes will be printed exactly as typed. Computed
results will be typed as decimal numbers or as integers.
1-2 READ-DATA
The READ statement says to look for DATA as in the following:
The computer ignores the DATA statement until it finds a READ, then it
takes the first number in the first DATA statement and assigns that value to
the first variable in the READ statement. Then, if there is a comma and
another variable in READ as in our example, the computer assigns the second
number in the DATA line to it; were there a third variable, the computer
would continue until it ran out of variables. In our program, the first time
through, A = 23.4 and B= 91. The next line says PRINT the product. Having
printed the product the computer looks for the next instrnction, which is
C0T0 20. This is a new one that means exactly what it says. So the computer
will C0T0 line 20 and execute that instruction again. At this point the com-
puter "knows" that it has already read and used the first two numbers in
the DATA line. So it goes to the third and fourth numbers and assigns them
to A and B in order and proceeds to print the product of 83 and 19, then
goes back and assigns the fifth and sixth numbers to A and B, and so on until
it runs out of numbers in the DATA line. There may be any number of DATA
lines in a given program; all you need to realize for the time being is that a
comma must be used to separate each discrete item of data and a comma
should not be placed after the last item in a particular DATA line. Also, be
careful not to usc commas to designate thousands, millions, etc. Warning: You
may not put variables or operation symbols in a DAT A line. Only numbers in
decimal form are allowed so far. Here is the computer's response to the above
program:
2129.4
1577
61 78
454. 48
804168.
Note the explicit message at the completion of the print-out. This will vary
from computer to computer.
In our examples so far, we have used only multiplication ( 0 ). The other
arithmetic operations that you may use are addition ( +), subtraction (- ),
division (/), and exponentiation (raising to a power). There are two symbols
in common use for exponentiation: one is an upwards arrow ( f), and the other
is a double asterisk ( 0 0 ). Symbols used to instruct the computer to perform
some operation are called operators. The symbols listed here are specifically
designated as the arithmetic operators. The numbers on which the operation
is to be pcrfotmcd arc called operands, Contrary to convention in algebra, the
m11ltipliC':itinn symhnl mmt hP pr·p,f:'nt. AJ3 in a!gebn1 ff!USt be '.V!itten A 0 B
for the computer. The computer assigns the same priorities to arithmetic
operations as are assigned in algebra. If there are several operations of the
same priority to be performed on the same line, the computer does them from
left to right. Several sample programs will be given soon.
READ
The READ statement looks for numbers in a DAT A statement.
READ X, Y, Z looks for numbers in groups of three.
DATA
The DATA statement supplies values for the variables designated
in the corresponding READ statement. Items of data must be sep-
arated by a comma. Numbers only are allowed.
There are two kinds of instructions of which you should be aware. We have
already discussed an instruction given by a program that you have wiitten. \Ve
have not yet mentioned an equally important kind of instruction, the system
command. We must realize that the computer does nothing by itself. There-
fore, there must be what is called an executive program which will respond to
your wishes. You need not worry about the executive program; it is taken care
of by the people who maintain the computer.
The first system command 1equired is referred to as the sign-on or log-on.
The exact form of this varies from computer to computer. So we really cannot
be specific here. It simply notifies the computer that you would like to use it.
Once you are signed on, the next most important command is RUN. After
yuu have type.l out yuur prug;ralll, ll1e curnpuie1 111usi. have a way of knowing
that you want it to execute the program. So you must type RUN and then
touch the return key on the keyboard. Only then will it respond to the pro-
grammed instructions.
Possibly next in importance is the command SCR (SCRub or SCRatch) or
Introduction to BASIC 5
CLE (CLEar) followed by depressing the return key. (Which you use will
depend on the computer you are connected with.) Suppose you have run a
program and someone else would like to run his. The ol<l program may be
erased by using the SCR command. So whenever you begin a new program
it might be wise to simply type SCR and touch the return key. The system
command must not be preceded by a number. There are severnl other com-
mands that we will take up as they seem appropriate.
RUN
Notifies the computer to execute the program instructions. Must
not have a number in front of it.
SCR or CLE
Notifies the computer that you are not going to use the current
program. The current program is erased from the working area of the
computer. Must not have a number in front of it.
1-4 LET
10 OATA 23.4,91.83·19.87.94.7fu5.98.876.918
20 Ri;;AD A, B
30 LET C=A•B
40 PRINT C
50 G0T3 20
60 END
RU'll
2129.4
1577
81 73
454. 48
804168.
LET
l
=
May be used to assign explicit values to a variable as LET X 4.56,
or may be used to assign algebraic expressions to a variable as LET
V =
X ° F + Y 0 G. Note: All variables on the right-hand side must
have been previously evaluated. On some computers LET is optional.
=
Such systems permit Z 4.56, for example.
1-5 INPUT
The INPUT statement serves much the same purpose as the READ state-
ment in that it permits us to provide numbers for the computer to work with.
For example, 100 INPUT A will cause the computer to print a question mark
and stop at line 100. The question mark is a signal to whoever is operating the
terminal that he or she is to type the desired value for A on the keyboard and
press the carriage return key to resume the run of the program. Likewise, 100
INPUT A, B, C will call for three numbers separated by commas to be typed
at the keyboard. It is advisable to have the computer print a label so that the
operator can determine the nature of the numbers required. In the following
program, note that the semicolon at the end of line 100 enables us to type the
values for A and B on the same line as the printed label. The input numbers
15, 17 following the question mark were typed at the keyboard by the program
operator.
100 PRINT "INPUT TW0 NUMBERS:";
110 INPUT A1B
120 PRINT " THE NUMBERS ARE:"; AJ B
130 PRINT" THEIR SUM IS:";A+B
140 PRINT "THEIR PR0DUCT IS:"lA*B
150 END
RUN
D0NE
INPUT
Causes the computer to request data from the keyboard.
1·98857
00NE
10 LET F=C2/S+3/7)/(3/4-113>
20 PRii'IT F
30 E'IO
RUN
1·988S7
O:iJNE
10 PRINT C2/S+3/7)/(3/4-l/3)
20 ENO
RUN
1·988S7
00NE
3/S/3/S= .04
3/CS/31S>= 9.
31SIC31S>= 1
C3/S>IC31S>= 1
C3/5/3l/S= .04
(3/5)/3/S= .04
OONE
10 PRINT "A="J2t3+1+3t2+1
20 PRl-'lT "B="J 2t C3+ 1>+3t 2+ I
30 PRINT "C="J2t3+Cl+3)t2+1
40 PRUJT "0="J2t3+1+3•C2+1>
SO PRINT "E="J2tC3+1+3Jt2+1
60 ENO
RUN
8 Basic BASIC
A= 19
8= 26
C= 25
D= 36
E= 16385
DlilNE
It is important to know the capacity of the computer you are working with.
Notice that according to the computer, (2/5 + 3/7)/(3/4 =
1/3) 1.98857.
If we work that out longhand, the result would be 1.98857142. BASIC pro-
vides from 6 to 15 digits, if they are needed, depending on the computer, with
the last digit roun<lctl off iu J.eciu1al 11u1uLt:fS, if it is the capacity digit
If the results require more than the digit capacity of the computer, the
computer prints in scientific notation as follows:
10 LET A=98781•
20 LET A1=8976
30 LET P=A•Al
40 PRINT A11"*"11Al11"="11P
50 END
RUN
98781 • 89 76 8.66658E+08
DlilNE
The E + 08 means "times ten to the eighth power" and the decimal number is
rounded off to the sixth digit. When the computer uses this notation, it is
called E-format. Again we get large spaces using the comma to delimit the
printed results. We will discuss this before we wind up chapter one.
A new item Al appears in the above program in line 20. There you will find
the statement LET Al =
8976. The computer treats this as a new variable. In
BASIC you may use any letter of the alphabet and any letter of the alphabet
followed by a single digit as a variable. Some computers have aclditiuual
simple variables. Thus a large number of variables are available.
Probably the best way to learn how the computer handles scientific notation
is by experience. So, let us run a sample program to see what happens.
x y Q p s
lo3IOOOE+IO 2ol3000E+ll 6d5023E-02 2o79030E+21 2o26100E+ll
Notice the use of Q for quotient, P for product, etc. This is a technique
that is useful not only on the computer, but throughout mathematics.
Suppose you wish to write a program to find the total cost of a purchase
in which there are different numbers of items at various prices, say 2 @ $.35,
3 @ $2.65, 11 @ $.25, 1 @ $9.49, and 35 @ $1.59. We could have many
more, but for a sample this should suffice. This program could of course be
written in several ways, but here is one possibility:
The single figure we set out to obtain is in the lower right-hand corner. The
result is $76.54; however, the other information is bound to be useful in at
least some situations. Besides, even if we only print the right-hand column,
we do not yet know how to dispose of the first four figures in that column. If
you only want to print the right-hand column, then lines 10 and 45 may be
altered thus:
10 PRINT "SUBT0TAL"
45 PRINT T
and only that column will be printed. Notice that line 10 is executed only once
and line 45 is executed five times. The G0T0 statement in line 50 only
returns the computer back to line 30. So the computer only prints the headings
once and only lets T = 0 once.
Still, in the last program, the combination of lines 25 and 40 may seem
strange, but it will not as soon as you gain a little more insight into how the
computer works. Line 25 is said to initialize the value of T at 0, i.e., give it an
initial value of 0. When the computer executes the initializing statement, line
10 Basic BASIC
Let us look at one more capability. In two of the programs of this chapter,
the results were printed out with unnecessary great spaces. You may have
noticed that we did not have these spaces in the two programs where semi-
colons were used in the PRINT statements. We have two delimiters, i.e., we
have two signals that tell the computer how closely we want the results
printed. The mies are a little complicated, but in general, the semicolon
specifies closer spacing than the comma. The comma sets up zones across the
page. The number of characters in the zones does vary from computer to
computer, but 15 characters per zone is common. This zone width does
not change with the number of digits in the numbers being printed. The
semicolon sets up different sized zones depending on the number of digits
in the number and whether it is in scientific notation. Here is the pro-
gram from p. 8 again. First we run it. Then we insert a line which
replaces the comma print delimiters with semicolon delirnite1s. And we call
for another RUN.
Introduction to BASIC 11
10 LETA=98781.
20 LET Al=8976
30 LET P=A*Al
40 PRINT A,"*"• At."="• P
50 E.''10
RUN
98 781 •
* 89 76 8·86658E+08
£.l3NE
41 PRINT AJ''*'';AIJ''='';P
RUl\J
98 '781. 89 76
* 8-86658€+08
98 781. * 89 76 8.86658E+08
D0NE
The output of this prngram is much more closely spaced. Notice that in the last
line of the printing there is a space between the 0 and 8976. The compute1
leaves a space there for a + sign but does not print it If the number printed
were negative, then there would be a minus sign printed in that space. The
same holds true for the space between the and 8.86658E + 08. Also notice
that in all program runs there is a space before the first number printed in any
line if the number is positive. However, if we write 10 PRINT "3" in a pro-
gram, then when we run the program, 3 will be printed in the very first
space. This is because the computer treats things in quotes differently from
the values of variables for printing purposes.
SUMMARY OF CHAP. I
I) We now have the PRINT statement which puts results in readable form.
It may be used for titles, headings, and labels.
2) Everything in quotes will be printed just as you type it (except more
quotes).
3) Commas or semicolons may be used between the different items to be
printed to control spacing.
4) The READ statement is used to read data. Several variables may be
read with a single READ statement by separating them with commas, or they
may be read with different READ statements. Just be sure the data is in
proper order to match the READ variables.
5) The DATA statement supplies data for the READ statements. Discrete
items of data must be separated with commas.
6) The LET statement may be used to assign any value or any algebraie
expression to any variable.
7) The INPUT statement allows the opeiator to enter data from the key-
board in response to a question mark.
12 Basic BASIC
1) Define the following items: BASIC, PRINT, END, READ-DATA, LET, RUN,
G¢T\z), statement, system command, program, remote terminal, comma delimiter,
semicolon delimiter, scientific notation, initialize, and print zone.
2) What is the greatest number of variables permissible in a single BASIC pro-
gram thus far?
3) Which of the following arc valid BASIC variables? A, XI, lB, XA, Yl2.
4) The statement was made in Chap. 1 that you cannot have the computer print
quotes by putting quotes inside quotes. Why not?
5) Write a program to add 2081, 682, 1161, and 72.03.
6) Write a program to add 1E6 and lE - 3. Comment on the result.
7) Have the computer multiply 2E3 by lE - 1.
8) Have the computer print a decimal value for 7·f:
9) Modify the purchase program on p. 9 to total the number of items.
10) Write a program that will print the sum of the first 10 counting numbers.
Put the numbers in as data.
11) Write a program that will print the product of the first 10 counting numbers.
j 12) Write a program that will multiply two binomials. In other words, for
(Ax+ B)(Cx + D), you will put in data in groups of four numbers (A, B, C, D), and
you want the computer to write out the three numbers that arn coefficients in the
product.
j 13) Have the computer print products of fractions by putting the numerators
and denominators in as data and printing the numerator and denominator of the
product as two numbers.
j 14) Do the same for adding fractions as in problem 13).
15) Have the computer print all possible arrangements of three digits using each
once. Assign the digits in a DATA line and use semicolon spacing.
16) Write programs to print decimal values for the following:
(a) 1/2 + 113
1/3-1/4
(b) 2/3 + 314
516 2/3
(c)
( 1/2 + 317)
2.33- 1
( 4::t:~-)
(d) ( 23.481 7.098)4 "'
4.98a - 87.82
° Check rparks (j) in front of problem numbers indicate the more difficult problems.
CHAPTER 2
Writing a Program
2-1 PLANNING
In Chap. 1 we looked at some programs and tried to analyze them, but
we did not really go into the development of the programs themselves. Pro-
grams do not just happen, they do not appear whole. They are planned and
developed with some considerable care. There are two important tools that we
will be using to help us write programs. One is a new BASIC statement type,
the REM statement. The other is flowcharting.
2-2 REM
XXX REM (REMark), where XXX is a line number in a BASIC program,
notifies the computer that what follows is to be ignored during the RUN of
the p1 ogram. This means that you may write any message you like following
REM. None of what you type has any effect on the execution of the pro-
gram, but you may comment or remark upon the function of a particular
line or a group of lines or the entire program.
REM
Permits the programmer to remark or comment in the body of his
program.
EXAMPLE
118 REM THE NEXT THHEE LINES PHINT THE FIHST SUM.
9 REM THIS PH0GRAM ADDS PAIHS 0F NUt-.IBERS.
2-3 FLOWCHARTING
Flowcharting, 01 block diagramming as it is sometimes called, is useful in
planning programs in any computer language or for that matter in planning
the solving of any problem, whether or not you me using a computer. We
13
14 Basic BASIC
Start
no Add 1 to
Have we done
counting
all 50?
variable
yes
Print sum
2-4 IF-THEN
XXX IF Z =
Q THEN 230 means that if Z does equal Q, then the next
line to be executed is line number 230. If Z does not equal Q, then the com-
puter is directed to simply execute the line with the next number after XXX.
The equals sign appears in the IF-THEN statement. Used here the equals
sign is clearly not the assignment operator we defined earlier. In the IF-THEN
statement the equals sign specifies a required relation (mathematical equality)
to exist between two numbers. Therefore, the equals sign is now designated as
a relational operator.
With the IF-THEN statement added to our growing list of BASIC state-
ments, we should be able to write the program directly from the .3owchart. See
Fig. 2-3(B). If we simply copy the program in Fig. 2-3(B) and run it, it looks
like the program below.
10 LET C=I
20 LET S=O
30 LET S=S+C
40 l F' C= 50 T~ E -.J 70
50 LE:T C=C+ I
60 Gt3H 30
70 PR!-.JT S
80 t::'llO
RU-.J
12 75
16 Basic BASIC
Slarl
10 LET C = 1
LET C = 1
LETS= 0 20 LETS= 0
LETS= S + C 30 LETS= S + C
40 IF C =SO THEl'J 70
yes
PRINTS
60 G¢T¢ 30
70 PRINTS
80 END
(A) (B)
Fig. 2-3. (A) Flowchart for adding counting numbers 1 to SO. (B) Pro-
gram written from flowchart
SUMI
1275
D0NE
70 PRINT "THE SUM 0F THE INTEGERS FR0M 0NE T0 FIFTY IS"; S
HUN
SUMI
D0NE
programs several weeks after you write them, unless they have good REM
statements. See SUMI.
Let us do another program, similar to SUMI, where we will add all the
odd integers from 5 through 1191. This time instead of starting the counting
variable at 1, we will have to start it at 5. Since we are only interested in
odd numbers, we will have to add 2 instead of 1 each time we add the new
number to the summing variable. We will test N (the number added) each
time through the summing step to decide whether we have reached the
desired number, in this case 1191. First we draw the flowchart in Fig. 2-4.
This flowchart is very much like the one in Fig. 2-3(A). See SUM2. Again, of
SUM2
10 LET N=5
20 LET S=O
28 HEM LINE 30 ADDS THE NEW NUMBER m THE SUMMING VARIARLF.
30 LET S=S+N
40 IF N= I I 9 I THEN 70
48 REM ADO 2 IN LINE 50 F0R 0DD NUMBERS
50 LET N=N+2
60 G0TO 30
70 PRINT "SUM OF 0DD NUMBERS FROM 5 TO 1191 IS"lS
80 ENO
RUN
SUM2
D0NE
18 Basic BASIC
Start
LET N = 5
LETS= 0
LETS= S + N
yes
PRINTS
LET N = N + 2 c END
course, we use the IF-THEN statement, because we have to decide each time
we add 2 whether or not we have reached 1191.
The IF-THEN instruction is called a conditional transfer. Unless instructed
otherwise, the computer executes the statements of the program in numerical
order. The IF-THEN statement allows us to tell the computer to alter that
order of execution on the condition that an algebiaic sentence is true. If the
algebraic sentence is false, then the computer passes to the next line in
sequence. On the other hand, the G0T0 statement is an unconditional transfer.
IF-THEN
XXX TF YYYYYY THEN ZZZ. If YYYYYY is true, transfer to line
ZZZ. If YYYYYY is false, pass to the next line after XXX.
Writing a Program 19
You may have more than one conditional transfer in the same place in a
program. This would be necessary if you wanted to test for several conditions.
Suppose in SUM2 you want to see the sum several times duri~g the RUN.
Let us look at the sum for the first two, for N =
731, and the last two.
First we should draw a new flowchmt. It is clear from the flowchart that we
have to decide each time we print the sum whether or not we have finished
or have to add 2 and take the sum again. See Fig. 2-5 and SUM3.
Note that we test N for three relations: 1) "less than 9," 2) "equals 731,"
and 3) "greater than 1188." \Ve have already seen the equals sign used as a
relational operator. The two new relational operators "less than" (<) and
"greater than"(>) are introduced here.
Start
LET N = 5
LETS= 0
LETS= S + N
yes
yes
yes
LET N = N + 2.
SUM3
D3NE
Other relational operators are "less than or equal to" (< =), "greater than
or equal to" (> =), and "not equal to" (< >). Some time-sharing systems
require a set of alphabetic relational operators (such as EQ for =) instead of
the symbols listed above.
Some facts about flowcharts should be becoming clearer. Arrowheads along
connecting lines show the direction the computer is to follow. Rectangles and
parallelograms have only one exit arrow, but they may have more than one
entrance arrow. Diamonds have two exit arrows. Can diamonds have more
than one entrance arrow?
We said previously that we did not know how to eliminate some of the print-
ing in the .SUBT0TAL column. Look at the purchase program on p. 9 again.
We had no way of preventing the computer from running out of data. Now we
can simply tack on some artificial data at the end of the DATA line, which
could not possibly be data in the problem, and use the conditional transfer to
test each time data is read to see if the computer has read the artificial data. If
the computer has read the artificial data, then we do not want to use it; but we
have a signal for the computer that it is time to plint the total and terminate
the run without reading any more data. Artificial data used in this way is
called dummy data. If we are talking about prices and numbers of items, we
can use 0 or negative numbers for dummy data. Let us use 0 for the number
of items and 0 for the price and name the program T0T AL. See the flowchart
in Fig. 2-6.
Writing a Program 21
Start
[ LETT~O
yes PRINT
r
LETT END
= T + N• P
T0TAL
D0NE
Look at lines 10 and 45 and then look at the printed result. These two
PRINT instructions are executed on the same printed line. This is accomplished
by using the semicolon at the end of the PRINT instruction in line 10. The
semicolon there tells the computer to wait after printing the $ until it executes
the next PRINT instruction in the program and to print that on the same line
right after the $. Again there is a single space for the plus sign which is not
printed. If the number were negative, there would be a minus sign there.
22 Basic BASIC
SUMMARY OF CHAP. 2
1) We are now able to remark about a program right in the program
itself by using REM. You should use REM statements so that whoever reads
your program can determine what they are intended to do. It will also help
you to remember your own programs weeks or months later when you your-
self have forgotten what they will do.
2) Flowcharting will prove a very valuable process that we will use to
develop programs to solve problems.
3) The ability to have the con1puter make decisions, using !F-THE!'-J, ::ind
nl't n<'<'orrlin~ to thP. nntf'omP. nf thP. dP.<'isions grP.atly inf'rP.asP.s the complexity
of the problems we may solve by computer.
4) We now distinguish between conditional and unconditional transfer
statements.
5) Dummy data may be used to gain a greater control over what we can
ask the computer to do after it has read the last item of data.
check for $1.98; and Sept. 17, write a cheek for $3.85. Assume that the balance was
$14 23 on Sept. 1. Have the computer print the balance after each transaction.
13) Write a program to find the amount of $100.00 deposited for one year in a
savings account at 4% per year compounded four times yearly. ·
.j 14) In the song "The 12 Days of Christmas," gifts are bestowed upon the singer
in the following pattern: the first day she received a partridge in a pear tree; the
second day two turtle cloves and a partridge in a pear tree; the third day three
french hens, two turtle doves, and a partridge in a pear tree. This continues for 12
days. On the 12.th day she received 12 + 11 + · + 2 + 1 gifts. How many gifts
were there all together?
.J 15) For problem 14) have the eom1')l]tcr print the number of gifts on each of the
12 days and the total up to that day .
.j 16) George had test scores of 83, 91, 97, 100, and 89. Write a program to com-
pute his average. Have the computer count how many tests George took.
j 17) ·write a program that will take more than one set of test scores, find the
average for each set, and print the result before going back to read the next set of
scores.
CHAPTER 3
Start
LET X = 1 10 LET X = 1
20 PRINT X;
30 IF X = 6 THEN 99
LET X = X + 1 40 LET X = X + 1
50 Gc;t>T¢ 20
END
99 END
24
Loops and Lists 25
L00Pl
10 LET X=l
20 PRINT Xl
30 IF X= 6 THEN 99
40 LET X=X+ 1
50 G0Hl 20
99 END
RUN
L00Pl
2 3 4 5 6
00NE
In L00Pl we first print the number an<l then test to see if we have printed
the last number in the sequence. If we have, then we stop. If we have not
printed the last number, then we a<l<l l an<l print the new number. The
results we obtain are entirely equivalent to the results we would get when we
test to see if the number we are about to print is too great before we print it
If it is not too great, then we print it. If it is too great, then we stop. Consider
the flowchart of Fig. 3~2 and L00P2.
Start
LET X = 1
no
PRINT X END
LE} X = X + 1
L00P2
10 LET X=l
20 IF X> 6 THEN 99
30 PKINl ;<;
40 LET X=X+I
50 G0l0 20
99 END
RUN
l00P2
2 3 4 5 6
U0NE
26 Basic BASIC
F0R-NEXT
Loops are used so routinely in programming that BASIC provides a
machine-made loop. Program L00P3 is the machine equivalent of our pro-
gram L00P2.
Notice that the two statements 10 F0R X 1 T0 6 and 50 NEXT X in
L00P3 do the work of the four statements 10, 20, 40, and 50 in L00P2.
F0R X =
1 T0 6 indicates doing everything between this statement and
NEXT X, beginning with X = 1 and ending with X = 6. NEXT X tells the
computer to add l to the old value of X and go to the beginning of the loop
again. When X =
6. L00P3 prints 6. After it prints 6, line 50 says NEXT X.
This means, add 1 and go to the beginning of the loop. At this point in the
RUN the value of X is 7, not 6 as you might think. Since 7 is greater than 6,
the F0R-NEXT combination instructs the computer to execute the next
instruction after NEXT X, which in program L00P3 is END.
L00P3
10 F"0R X=l T0 6
30 PrHNT XJ
50 NEXT X
99 END
RUN
L00P3
1 2 3 4 5 6
D0NE
A machine loop does not have to begin with 1. It may begin wherever you
require. The variable that is incremented in the machine loop may be treated
in the same way as other variables in the program. However, you are warned
against changing the value of that variable. L00P3+, which is a modification
of L00P3, prints the values of X, 2 ° X, X 10, X 0 • 3, and X/(-3).
L00P3+
D0NE
Notice lines 80, 100, 150, 220, 240, and .310 in program LUPDEM. They
are all of the form XXX PRINT. This statement is sometimes called the
blnnk PRINT. It has the effect of di1ecting the computer to turn up a new
Loops and Lists 27
line of paper at the terminal. In some cases, XXX PRINT serves to begin a
new line; in others, XXX PRINT results in a space between lines of printed
output.
LUPDEM
RUN
LUPDEM
330 LET A=S, 340 LET B=451 350 LET C=6 AND
360 rlilR V=A T0 B STEP C GIVES THESE RESLA.. TSJ
5 11 I 7 23 29 35 41
D0NE
28 Basic BASIC
F0R-NEXT
F0R X = A T0 B STEP C sets up a machine loop with first num-
ber A, last number B, and increment C. See LUPDEM for detail.
Now we will look again at some of the programs in Chap. 2 and do them
with a F0R-NEXT loop. Let us redo program SUM3 and call it SUM3+.
Of course as we should expect, the printed results for SUM3+ are identical
with those of SUM3. See the Howchart in Fig. 3-3.
LET 5 = 0
F\Z)R N = 5 TQ'>
1191 STEP 2
[:s~s+N
yes no
END~
Loops and Lists 29
Sl.a-13+
SUM 0r 000
NUMBERS rR0M
rIVE T0 IS
5 5
7 12
731 133952·
1189 354021.
1191 355212·
00NE
F0R-NEXT may be used to count the number of times the computer does
a particular operation or a set of operations. For instance, we can use a
machine loop to count the number of different items in program T0TAL of
Chap. 2 and at the same time instruct the computer to read data repeatedly.
We did not know how many items of data there were, but that does not
matter. We can simply pick a number that we are sure is greater than the
number of times we want the computer to read data. There could not possibly
be more than say 50 items.
So in T0TAL+ we can use F0R X = 1 T0 50. Then we can test for the
dummy data each time data is read, using the conditional transfer to get the
data out of the loop and to print the results, when N is 0.
T0TAL+
D0NE
30 Basic BASIC
Look carefully at line 45 in T0TAL+. This line gives the printing instruc-
tions. The counting loop calls for X to go from I to 50, but line 45 says print
X - 1. Since X counts the number of times the READ statement is executed,
1 is added even when the dummy data is read; but we do not want to count
the dummy data. So we have to tell the computer to subtract 1. An alterna-
tive method would be to use F0R X =
0 T0 50. Then we could call for
printing the value of X.
The same loop may be used several times in the same program. Every time
the computer executes the F0R statement, the limits on the incremented
vuriuble are reestablishd. Suppose in !l group of five people each person took
six tP.sts. And WP. want to read thP.ir scores and find the average for each
person. We can set up a loop F0R X =
1 T0 6 and use this repeatedly until
the computer runs out of data. The flowchart appears in Fig. 3-4 and we call
the program AVG. Note that the flowchart of Fig. 3-4 contains no END box.
This is because the computer runs out of data in the READ statement and
termination is antomatic. Notice in the program that each score is printed as it
is read. This is one way of keeping track of whose average is being printed on
each line in the printed results. Also note that each line of data is devoted to
the scores for one person. This makes it easy to organize the typing of data.
Start
Set total
at 0
READ loop
F9.)R X = 1 T9.) 6
Add scores
NEXT X
LETT= T + S
AVG
10 LET T=O
20 F'0R X=l T0 6
30 READ S
35 PRINT SJ
40 LET T=T+S
50 NEXT X
60 PRINT "AVG="J T/6
68 REM WE SEND THE C0MPUTER BACK T0 LINE 10 T0 SET T AT ZER0
AGAIN
70 G01'0 10
80 DATA 65168173185182187
02 DATA 74.87190188.01.00
84 DATA 8819h91192o90189
86 DATA 911 831 781 89179,67
88 DATA 65176.6b50160166
100 END
RUN
AVG
SUMMARY
We see that it is not necessary for us to construct repetitive operations.
This may be done automatically with the F0R-NEXT statement pair in BASIC.
AVGCNG
S F0R P= I T0 S
10 LET T=O
20 F0R X=I T0 6
30 READ S
3S PRINT SJ
40 LET T=T+S
SO NEXT X
60 PRINT "AVG="J T/6
70 NEXT P
80 DATA 6S. 68. 73,5s,52,51
82 DATA 74,87.90.88,87 .88
DATA 66•9i.9i.92,90 .69
86 DATA 91.83. 73,39, 79,87
88 DATA 6S• 76. 67, SO. 60. 66
100 ENO
~UN
AVGCNG
6S 68 73 8S 82 87 AVG= 76·6667
74 87 90 88 87 88 AVG= 8S.6667
88 97 91 92 90 89 AVG= 91·1667
91 83 78 89 79 87 AVG= 84· s
6S 76 67 so 60 66 AVG= 64
OONE
Notice that the X loop is entirely within the P loop. Loops written in this
way are called nested loops. They occur often in programming. Loops may be
nested to almost any number you may require, but the loops must be com-
pleted from within. The F0R statements and the NEXT statements must be
paired. Legal and nonlegal combinations are shown below.
Legal Illegal
F0R A=I Till 8 F0R A=I T0 8
F'0R B=2 Tlil 7 FlilR B=2 Till 7
F0R C=2· 3 Till 6. I F0R C=2• 3 Till 60 I
NEXT C NEXT A
Filltt O=A Till B NEXT C
NEXT D F0R D=A T0 B
NEXT B NEXT 0
NEXT A NEXT B
Start
F\DR Y F(Z)R Q
LET P 2000
T(b 9 TQ') 4
Compute
interest
Add
interest to
principal
NEXT Y NEXT Q
END
Fig. 3-5. Flowchart for finding the compound amount of $2000 after
nine years compounded quarterly.
CMPINT
DONE
We may want to have the limits of one loop determined by the variable in
another loop. For instanee, we ean print a triangle of numbers in which each
row piints all of the eounting numbers up to and including the row number.
34 Basic BASIC
We need one loop for rows and another for columns. We want the number of
coiumns to go from l to the row number. This is accomplished by program
TRAGL. Now you can do problem 14) in Chap. 2 very easily. (Of course you
could do the problem before, but it took a longer program.)
TRAGI...
10 r0R R=I T0 10
20 r0R C= I Tiil R
30 PR!NT G.1
40 NEXT C
50 :-~:.,~·
60 NEXT R
70 ENO
RUN
TRAGI...
2
2 3
2 3 4
2 3 4 s
2 3 4 s 6
2 3 4 s 6 7
2 3 4 s 6 7 8
2 3 4 s 6 7 8 9
2 3 4 s 6 7 8 9 10
OONE
SUMMARY
Loops may be nested inside other loops as long as we see to it that opera-
tions are done from within, much the same as we deal with sets of parentheses
within other sets of parentheses in algebraic expressions. There may be as many
loops within other loops as the problem may require up to a point. The limits
of one loop may be set by the variables of other loops. Caution is urged
against inadvertently changing the loop variable within the loop, although we
may use its value for any purpose.
LISTI
RUN
LISTl
WE CAN ALS0 USE A L00P. THE L0NGER THE LIST THE BETTER
BEING ABLE T0 USE A L00P IS
4 9 -92 8
WE CAN 0PERATE 0N THE NlMBERS IN THE LIST
B LC BJ B•LC Bl
I 4 4
2 9 18
3 -92 -276
4 8 32
Start
F¢RK= F¢R L = 1
T¢4 T¢4
PRINT
A[KJ; B[L],
NEXT K NEXT L
END
Fig. 3-6. Flowchart for program PAIRS for printing all combinations from
two four-item lists.
38 Basic BASIC
PAIRS
I 2 I 3 I 6 I 9
3 2 3 3 3 6 3 9
5 2 5 3 5 6 5 9
7 2 7 3 7 6 7 9
OONE
SUMMARY
The computer list has been introcluced. A list is like a subscripted variable
in that it takes on different values accorcling to the subscript. Each of the
numbers in a list is a variable unto itself. It may be handled in the same way
that any of our previous variables mav be handled. The numbers in a list may
be rearrangecl. In order to exchange two numbers in a list, we first have to
store one of them in another variable.
7) Prepare one list with the numbers 6, 11, 15, 17, 26, and 83 and another
with 15, Hl, 27, 83, and 91. Have the computer form a new list that contains only
those numbers that are in both lists.
8) Using the two lists given in problem 7), create a new list consisting of all
numbers that appear in either list. If the numbe~ appears in both lists, enter it only
once in the new list.
9) LET F[l] 1 and LET F[2] = 1, then fill the next eight positions in F so that
every entry is the sum of the previous two entries. Print the complete list. You have
begun to form the sequence known as the Fibonacci numbers.
,/ 10) Form a 10-item list consisting of the first 10 positive odd integers in order.
Form a second list so that each entry contains the sum of all the numbers in the
first list up to and including the location number for the second list.
11) Prepare one list containing 6, 1, 3, 7, 2, and 9 and another containing 8, 2,
3, 9, 7, and 4. Form a third list containing the sums of the corresponding elements,
=
i.e., A[I] F[I] + S[I].
12) Do problem 11), but enter the products in the third list.
,/ 13) Fill a four-element list with 9, 60, 700, and 3000. Fill a three-element list
with 7, 30, and 200. Sum up the products of all possible pairs of numbers, one
from each list.
Start
For J =I+ 1
T\i') 10
Exchange
L[I], L[J]
ARANGE
RLl'J
ARAN GE
D0NE
Loops and Lists 41
In flowcharting when we have a process of this kind, which has been used and
clearly defined, we can avoid being explicit by using a shape to indicate a
predefined process. The generally accepted shape is a rectangle with two
additional vertical lines, which appears in the flowchart of Fig. 3-7 for pro-
gram ARANGE that solves the problem we have just outlined.
In program ARANGE, the list is read in lines 20, 30, and 40. Then, for the
purpose of seeing the list in the original order, it is printed immediately in
lines 60, 70, and 80. In lines 110 and 120 two loops are set up, where the I
loop represents the first number of the pair and the J loop represents the
second number. As per line 118, why did we not let I go from 1 to 10? Had
we done that, at some point in the program the computer would have to
compare L(lO] and L[lO], which is not necessary. The first time through,
L[l] = 6 and L[2] = -19. The first element is not less than or equal to the
second. Thus, We want the computer to exchange these two elements. This is
done by lines 140, 150, and 160. As the computer leaves line 160, L[l) = -19
and L(2] = 6. It is relatively simple for us to have the computer print the list
every time it is necessary to exchange two elements of the list. All that is
required is to insert four statements exactly like 60, 70, 80, and 90. This is
done in program ARANGl in lines 200, 210, 220, and 230. This means that
the more numbers out of order, the more printing we might expect. Study
the printing of ARANGl carefully. Notice that after the first reversal, L[l] =
-19 and L[2] = 6 as promised.
Look at the three sets of lines: 60, 70, 80, 90; 200, 210, 220, 230; and 410,
420, 430, 440. You should recognize that these three sets of lines are identical.
BASIC provides a convenient program statement that allows us to type out
that set of lines only once and then call that set of lines from anywhere in the
program. The statement is G0SUB XXX, where XXX designates the first line
of the set of lines you would like repeated. The set of program statements
that is repeated is called a subroutine. When the computer encounters YYY
G0SUB XXX, it initially behaves as it would for G0T0 XXX. However, the
computer "remembers" where it was when it left YYY and will return to the
next higher numbered line after YYY when it finishes the subroutine. In order
to achieve this, the computer must "know" when it has completed the sub-
routine. You, the programmer, must notify the computer where the end is by
inserting a line ZZZ RETURN at the end of the subroutine. Then the com-
puter will "know" that it must go to the line immediately following the G0SUB
XXX it most recently encountered.
G0SUB-RETURN
YYY G0SUB XXX sends the computer to line XXX to execute all
lines it encounters until the RETURN statement, which sends the
computer back to the line following YYY. G0SUB is especially useful
in programs where the same set of lines is used several times.
42 Basic BASIC
ARAlllGl
....,,...."..::,,,
Loops and Lists 43
G0SUB
D0NE
44 Basic BASIC
ARANG2
D0NE
Loops and Lists 45
SUMMARY
I) The computer list is beginning to emerge as a powerful storage area for
keeping numbers while we have the computer perform tests on numbers in
the list.
2) We can rearrange the elements in numerical order by testing all pairs
and exchanging any that are not in the required order.
3) G0SUB permits us to use the same set of program statements many
times at many different points in a prngram without disturbing the progress
of the computer through the rest of the program.
the highest subscript, look at each entry in the sum list. If the entry is less than 10,
procce<l to the ntxl entry_ If the entry is 1nure than 9, subtra(;l 10 frL)H1 that entry
and add 1 to the entry with the next lower subscript. Print all three lists across the
page, one above the other, with the sum list last. What have you accomplished?
j 6) On seven consecutive days the high and low temperatures were as follows:
51-71, 48-67, 50-77, 55-78, 55-76, 55-75, 49-79. Write a program using lists to find
the greatest range and the number of the day on which it occurred, the average high,
and the average low.
j 7) Prepare two 10-element lists using the following numbers: 43, 65, 92, 38, -45,
0, 15, 61, -61, -15, 45, 54, 52, -14, 49, -3, 66, 72, 29, -L Arrange all the
numbers in increasing numerical ordero
j 8) The following test scores are given: 65, 71, 82, 63, 90, 58, 66, 67, and 68.
Program the computer to list the scores, calculate the average, and then find the
number of test scores that were above average and the number below average.
Also, find the score where there are the same number of scores above as below.
j 9) The Fibonacci numbers are generated by letting the first two numbers of the
sequence equal 1, and from there on each number may be found by taking the
sum of the previous two clements in the sequence. So you get 1, 1, 2, 3, 5, 8, 13,
etc. Prepare two lists: one with the first 10 and the other with the second 10. For
each clement from 2 to 19 find the difference between the square of the element
and the product of the clements immediately preceding and following. In other
words, print F[l] 0 0 2 - F[l - l] ° F[l + l].
CHAPTER 4
Computer Functions
4-1 INTRODUCTION TO INT( ), SQR( ), ABS( ), AND SGN(
The four functions discussed in the following, will prove very useful in
BASIC.
INT(X) is used in two ways. In some computers, INT(X) determines the
greatest integer not greater than X. For example, if A = INT(43.2), then
A= 43; if A = INT(6), then A= 6; and if A = INT(-2.3), then A= -3. In
other computers, INT(X) truncates the number X, i.e., it simply removes the
decimal part. So if A = INT(-2.3), then A = -2.
SQR(Y) computes the non-negative square root of Y. For example, if B =
SQR(l6), then B = 4.
Some computers will not compute if B = SQR(-16). However, if we have
many values for which we want the square roots and some happen to be
negative, we can instrnct the computer to take the square root of the absolute
value of Y. BASIC p10vides ABS(Y) for just such occurrences. For example,
ABS(18.3) = 18.3, and ABS(-24.61) = 24.61. So we can use SQR(ABS(Y)) for
the problem above.
A fourth BASIC function which you may not have much call for right now
is SGN(N). SGN(N) is + 1 if N is positive, 0 if N is 0, and -1 if N is negative.
The number in parentheses is called the argument of the function. Note that
the argument may be an explicit number, a variable, another function, or any
algebraic expression. Study the demonstration program ASIS to see how the
computer handles these functions.
47
48 Basic BASIC
ASIS
10 PRINT "X","ABSCX>"•"SQRCABSCX>>"•"INTCX>"•"SGNCX>"
20 READ X
30 PRINT X.ABSCX>.SQRCABSCX>>•INTCX>.SGNCX)
40 DATA -699913.,-35.2,-.032
50 DATA 0,.032,23.412.6391'
60 G0T0 20
70 END
RUN
ASIS
One common use of INT( ) is for factoring integers. We can look at the
quotient of two integers, and if that is an integer, then the denominator is a
factor. For example, 6515 =
INT(6515); therefore 5 is a factor of 65. So in
order to find the greatest factor, all we have to do is start with the integer, one
less than the number we are trying to factor, and test to see if it divides with-
out remainder. If it does, we use the conditional transfer and send the com-
puter to a PRINT statement. If it does not, we let the computer subtract 1 by
using a loop and try again. If we start at N, we will get NIN =
INT (NIN) the
first time through even for prime numbers. Let us also print N is prime if it is.
Start
F(Z)R D = N --1
Tc,?) 2 STEP-1
PRINT
N,D
no
PRINT
..
/ ____,
. ., ,I~
, :-
~ime.
10 REAO N
20 r0R D=N-1 T0 2 STEP -1
30 Ir N/D=INT CN/D) THEN 70
40 NEXT D
50 PRINT NJ"IS PRIME"
60 G0T0 10
70 PRINT DJ "IS THE GREATEST rACT0R 0r"JN
80 G0T0 10
90 DATA 1946,19 49•1009 . 1003
100 DATA 11001.2 40.11
110 END
RUN
PRIME!
PRIME2
10 READ N
20 r0R D=2 T0 SQRCN>
30 Ir N/D=INT CN/D> THEN 70
40 NEXT D
50 PRINT NJ "IS PRIME"
60 G0T0 10
70 PRINT N/DJ "IS THE GREATEST rACT0R 0r"JN
80 G0T0 10
90 DATA 1946,19 49•1009 ,1003
100 DATA 11001.2 40.11
110 END
RUN
PRIME2
SUMMARY
\Ve are finally ready to reduce fractions to lowest terms. Look at problems
13) and 14) in Chap. I Thne, if we had aclclecl 5/6 ancl 7 /8 we would have
gotten 82/ 48. Since, howevc1, it is customm y to 1educe fractions, we would
like to get 41/24.
Computer Functions 51
All we have to do is find the largest factor of the numerator that is also a
factor of the denominator. Only this time we have to go all the way to 2. So
we will use the procedure of program PRIMEL First we should prepare a
flowchart. See Fig. 4-2. We simply find the greatest factor of the numerator
and see if it is also a factor of the denominator. If it is, fine. If it is not, then
we go back and find the next greatest factor of the numerator and test to see
if that is a factor of the denominator. If it is, fine. If not, we go back again
and look for the next factor of the numerator. If we get all the way to 2
without a number that is a factor of both numerator and denominator, then
we print the fraction as it was given. See program REDUCE.
\Ve should try to pick the largest factor of the smaller number to reduce
the number of tries the computer has to execute.
Dimension Conversions
7
12)93
84
9
and we would say 7 ft 9 in. with no difficulty. \Ve can easily get the 7 by
using INT(93/ 12), but it is an exercise in mathematics to get the 9. Let us
look at the division problem in more detail:
7.75 ,75
12)93.00 12)9.00
84 8.4
9.0 .60
8.4 .60
.60 .00
.60
.00
\Ve see that if we divide 12 into the remainder after integer division, we get
the decimal portion of the result if we divide by 12 by decimal division. That is,
for this problem. Or in general, for N divided by D and calling the remainder
R we get
R N - INT(N/D) 0
D
52 Basic BASIC
Start
F¢R P =NT¢
2 STEP-1
PRINT
N/P, 0/P
REDUCE
10 READ N.D
20 r0R P=N T0 2 STEP -I
30 Ir N/P=INTCNIP> THEN 70
40 NEXT P
so PRINT N"/"D
60 G0Tl3 10
70 Ir D/P=INTCDIP> THEN 90
60 G0T0 40
90 PRINT N 11 /"D"="N/P"/"IJ/P
100 G0T0 10
110 DATA 5, 6
120 DATA 62,46
130 DATA 3,4
140 DATA 36, 46
150 END
RUN
REDUCE
5 I 6
62 I 46 41 I 24
3 I 4
36 I 46 3 I 4
Start
LET F LET 11
= INT (1/12) =l-F•1 2
C0NVP.T
10 READ I
20 LET F=INTCI/12>
30 LET Il=I-F*12
40 PRINT I"INCHES ="F"FEET "I I "INCHES"
45 G0T0 10
50 DATA 9,56.47,37,947 ,450
60 END
RUN
CiONiiRT
SUMMARY
1) We can now find the greatest common factor of two integers and thus
reduce fractions to lowest terms.
2) We have seen that the INT( ) function may be used to break quotients
up into their integer part and their decimal part less than 1.
3) We can find the remainder in a division problem by using R N - INT=
(N/D) " D. This allows us to convert dimensioned numbers, such as inches,
to feet and inches.
Start
Initialize
principal F\l>R Y = 1 T0
on both rates 10
Compute
new principal
for this year
at4.5%
Compute
new principal
for this year
at4.75%
NEXT Y
PRINT
headings
and P's
making changes as you develop a progrn.n-1. Thus in program BP. . ~JKl lines
130 and 150 are printed with semicolons at the end so that the printing can
be placed at the ends of those lines from PRINT instructions on other lines.
The values of the different principals will be printed, according to instructions,
on lines 140 and 160.
Note: On some computers line 10 of BANKl would be written as 10 LET
P, Pl= 0.
We can certainly obtain the information we want from the RUN of this
program in its present form. Clearly, we would get more interest by changing
banks. You will have to decide whether it is worth switching. Even so, let us
see what we can do to simpiify the results. For instance, when we talk about
money, most of us tend to round off to the nearest cent. So we should be able
to have the computer do that too. We could multiply by 100 and then take
the greatest integer, but that would give 8823 for P and we want dollars and
cents. Let us then divide by 100 again and get 88.23. However, we really
want 88.24 because the .007 is more than one half a cent. We can obtain this
by adding .5 after we multiply by 100, then taking the greatest integer and
dividing by 100 again. Adding .5 to positive numbers from .5 to .99 results in
numbers from 1.0 to 1.49, and sends positive numbers from .01 to .49 into
numbers from .51 to .99. When we take INT(the sum), the result increases by
1 for numbers .5 or more and is unchanged for numbers less than .5. Thus
BANK I
10 LET P=Pl=56.31
20 F0R Y=I T0 10
22 REM F"0R TEN YEARS
30 F0R M=I T0 12
32 REM C0MP0UND M0NTHLY AND C0MPUTE INTEREST
40 LET I=P•4.5/I00/12
50 LET P=P+I
60 NEXT M
62 REM THAT FIGURES THE INTEREST F0R THIS YEAR C0MP0UNDED
M0NTHLY
70 F0R Q= 1 T0 4
72 REM C0MP0UND QUARTERLY
BO LET ll•Pl*4•75/I00/4
90 LET Pl•Pt+I I
100 NEXT Q
102 REM THAT TAKES CARE 0F THE QUARTERLY INVESTMENT F0R THIS
YEAR
!OB REM N0W T0 C0MPUTE THE NEXT YEAR
tlONEXTY
120 PRINT "F0R TEN YEARS"
130 PRINT "@4.5% C0MP0UNDED M0NTHLY•·•"J
I 40 PRINT P
150 PRINT "@4.75% C0MP0UNDED QUARTERLY···"l
160 PRINT Pt
9999 END
RUN
BANK I
D0NE
Computer Functions 57
R0UND
10 READ X
20 LET Y=INTCX*l00+.5)/100
30 PRINT y,X
40 DATA 2.31462,2.34999,2.35001.362 .617.346,3.66149E-02
so G0T0 10
60 END
RUN
R0UND
2. 31 2.31462
2.35 2. 34999
2.35 2.35001
362 362
617.35 617-346
.04 3.66149E-02
DEF
XXX DEF FNA(X) (any legal BASIC expression). BASIC pro-
vides a program-defined function. It begins with FN followed by a
third letter which is used to identify the function. (Some computers
allow more than one argument.)
58 Basic BASIC
DEFC >
3 4 • 7S • 75 •8
6 11.2 .535714 • 54 •5
RUN
BANK 1
D0NE
The results in the above prngram are rnun<led off to the nearest cent and
the dollar signs make it clear that we a1 e dealing with monev. However, it
would be even better if we coulcl line up the decimal points. If your version
of BASIC does not p1 ovide a computer function to ove11ide the semicolon
spacing, you may write your own subroutine that will allow you to place
results exactly where you want them printed. In om pa1 ticular problem all we
want to do is move the fi1 st number th1 ee spaces to the 1ighL B11t we might
then \Vt.tilt to 111ove il1e111 Lolli [ u1 tl1c1 i.u i.l1L' 1 jgl1l. Su k·L u;-, l.t~t_·· the tiiiJC t\;
PRINT " "; in a loop and letting the high number be a variable that equals
the number of blank spaces required. The following subroutine will print X
spaces.
500 F0R S = 1 T0 X
510 PRINT " ";
520 NEXT S
530 RETURN
In BANKl, no matter where we place the numbers, we will have to put the
first number three spaces further to the right than the second number. We
may now accomplish the required spacing by first printing according to line
130 and then setting a reasonable value of X followed by G0SUB 500. Upon
getting the computer to print according to line 1.50, we next LET X = X - 3
put in three fewer spaces and G0SUB 500 again. Finally, we must be sure
that we do not let the computer enter the sub10utine accidentally. Should this
happen, the computer will attempt to execute the RETURN statement when
there was no prior G0SUB to direet it. To avoid this we can use 490 G0T0
9999. However, BASIC has the statement XXX ST0P for just such a situation.
ST0P
XXX ST0P is equivalent to XXX G0T0 9999 when 9999 is the
END statement.
Below we list the latest changes, and name the resulting program BANK2.
The entire prngram is listed to see where things fit together. As you can see,
the results are aligne<l in the RUN.
RUN
8ANK2
00NE
60 Basic BASIC
BANK2
2 DEF FNHCX>=INTCX*IOO+.Sl/100
10 LET P=Pl=S6.31
20 F0R Y= I T0 I 0
22 REM r0R TEN YEARS
30 r0R M= I T0 12
32 REM C0MPOUND MONTHLY AND COMPUTE INTEREST
40 LET I=P*4.S/100112
SO LET P=P+ I
60 NEXT M
62 REM THAT Fl GURES THE INTEREST FOR THIS YEAR COMPOlF'1DF.D
MONTHLY
10 FOR O=I TO 4
72 REM COMPOUND QUARTERLY
BO LET ll=Pl*4•7S/100/4
90 LET Pl=Pl+I I
100 NEXT Q
102 REM THAT TAKES CARE OF THE QUARTERLY INVESTMENT FOR THIS
YEAR
IOB REM NO~ TO COMPUTE THE NEXT YEAR
I I 0 ,\JEX T Y
120 Pk!NT "FOR TEN YEAkS"
130 PRINT "@4.S% COMPOUNDED MONTHLY···";
132 LET X=4
I 36 GO SUB SOD
140 PRINT "$"rNHCP>
I SO PRINT "i!4. 7S% C0MPOUNDED QUARTERLY ••• "J
I S6 LET X=X-3
I SB GOSUB SOO
160 PRINT "$"FNHCPI>
490 ST0P
SOD r0R S=I TO X
SI 0 PRINT " "J
S20 NEXT S
S30 RETURN
9999 END
RUN
BANK2
D0NE
Now as long as we have the spacing subroutine available, let us try several
values of X in line 132 an<l see what happens.
OONE
OONE
OONE
SUMMARY
1) The program-defined function DEF FNA(X) has been introduced. This
allows us to have the computer perform the same function on different
variables.
2) The ST0P statement may be used to terminate the RUN of a program
at places other than the physical end of the prngram. The end of a program
specified in this way may be referred to as the logical end.
3) We have constructed a subroutine that enables us to control more pre-
cisely than with the semicolon or comma the spacing of printed results by
putting " "; in a loop. This gives more versatility of format.
RNDC 1 >
10 F0R X=I T0 10
20 PRINT RNDC 5»
30 NEXT X
40 END
RUN
RNDC 1 >
OONE
Before we get very far trying to use RND(Z) we realize that numbers
between 0 and I do indeed limit us greatly as a source of data. Suppose we
want data selected from l to 10. First we might try to multiply the random
number by 10 before taking the INT( ) function. Let us try it in RND(2).
RNDC2>
10 F0R X=I T0 20
20 PRINT !NT< IO*RND< I> H
30 NEXT X
40 END
RUN
KNUl c>
3 5 5 2 6 3 0 2 3 9 4
2 2 6 3 9 4 3 4
OONE
Computer Functions 63
RND< 3)
10 F'0R X= 1 T0 30
20 PRINT INT< lO*RND< 1>+1 >J
30 NEXT X
40 END
RUN
RNDC 3)
5 8 4 5 3 3 7 5 5 10 9 10
8 7 7 8 3 4 2 10 9
1 6 6 3 3
D0NE
If we want decimal numbers from 1 to 11, not including 11, all we have to
do is leave out the INT( ), as in RAND3+.
RAND3+
10 F'0R X=l T0 10
20 PRINT 10*RNDC9>+1J
30 NEXT X
40 END
RUN
RAND3+
Now we have a way to determine the interval in which the numbers are
picked. If we can get 1 to 11 with 10 ° RND(Z) + 1, we ought to be able to
get 1 to 100 with 99 ° RND(Z) + 1.
RND(X)
=
XXX LET Y RND(X) will assign at random a number between 0
and 1 to Y. We can get integers from 1 to A with INT(A o RND(X)
+ 1).
64 Basic BASIC
Start
F0RX = 1
T010
LET L(X)
= INT(250•
RND(l) + 1)
Turn
F(ZSR I= 1
NEXT X switch off
T<;Z)9
LETS= 0
Exchange
and L(I + 1)
L(I)
See ARANGE
---<
Turn
switch on NEXT I
LETS= 1
PRINT
Does
END list
in order
s = 1?
ll ~
Fig. 4-5. Flowchart for arranging a list of numbers assigned from
RND( ) using comparison of adjacent pairs.
Computer Functions 65
name of the switch can be any number of things, but here we will use S. If
S = 0, the switch is off. If S= 1, the switch is on and we tell the computer to
look at the list again. If the switch is off, we want the computer to print the
ordered list. Under what conditions do you think this will be the most efficient
ordering technique? The name of this program is ARANG3 and its flowchart is
in Fig. 4-5.
ARANG3
10 F'0R X=l T0 10
20 LET LlXl=INTC250*RNDCl)+I>
40 NEXT X
56 REM TURN THE SWITCH 0F'F'!!!
60 LET S=O
70 F'0R I= I T0 9
BO IF' LCil <= LCl+ll THEN 130
90 LET K=LC I J
100 LET LCil=LCI+ll
110 LET LC I+ I l=K
120 LET S= 1
121 REM *** TURN THE SWITCH 0N ***
130 NEXT I
136 REM IS THE SWITCH 0N??
140 IF' S= I THEN 60
142 REM IF' THE SWITCH IS 0F'F' THERE WERE N0 EXCHANGES AND
143 REM THE LIST IS IN 0RDER
145 PRINT "THE NUMBERS IN 0RDER"
150 F'0R X=I T0 10
160 PRINT LCXJJ
170 NEXT X
ISO END
RUN
ARANG3
The program looks fine, but nobody could prove that we really used the
ordering routine to put the list in order, because we do not know what the
original list was. So let us put back the routine that prints the list as it is
formed.
Fine! Now we believe it. We have just put 10 random numbers in order.
It is about time we found out how to create longer lists.
66 Basic BASIC
DIM
We can usually get 10 (or 11) elements in a list. If we want longer lists
we simply notify the computer that we wish to specify a greater dimension for
our list. The BASIC statement is XXX DIM L[Z], where Z is the highest sub-
script in the list. Computers vary. Some allow a variable in parentheses, while
others require an explicit integer. If you do not know how long the list is
going to be, simply pick a number larger than you think you will need. You
need not use every location in the list. Let us dimension a list in ARANG4 up
to 7.5 and use 20 locations to see how a longer list looks.
The program seems to work nicely. Let us try a few other numbers.
7 LET N=5
RUN
ARANG5
For N =25 we list the entire program with all the changes we have made.
Notice that when we made the original change we put lines 10, 70, and 150
in terms of N so that we would not have to retype them each time we made a
minor change in the length of the list. See ARANG5.
XXX DIM A[24], B[75], L[33] dimensions three lists. The A list
has 24 as its highest subscript, B has 75, and L has 33. You may
dimension as many lists as will fit on one line.
Computer Functions 67
ARAN GS
2 DIM LC 7Sl
s PRINT "HERE IS THE LIST AS IT IS BEING F"0RMED***"
7 LET N=2S
10 F"0R X= 1 T0 N
20 LET LlXl=INT<2SO•RND<l>+l>
30 PRINT LCXlJ
40 NEXT X
42 PRINT
SB REM TURN THE SWITCH 0F"F"! ! !
60 LET S=O
70 F"0R 1=1 T0 N-1
BO IF" LCIJ <= Lll+ll THEN 130
90 LET K=LC I l
100 LET LCll=LlI+ll
110 LET LCI+ll=K
120 LET S= 1
121 REM *** TURN THE SWITCH 0N ***
130 NEXT I
13B REM IS THE SWITCH 0N??
140 IF" S= 1 THEN 60
142 REM IF" THE SWITCH IS 0F"F THERE WERE N0 EXCHANGES AND
143 REM iHE LIST IS IN 0RDER
14S PRINT "THE NUMBERS IN 0RDER"
1 SO F"0R X= I T0 N
160 PRINT LC XlJ
170 NEXT X
lBO END
RUN
ARAN GS
13B
THE NUMBERS IN 0RDER
s s 6 lS lB 30 3B 44 7S BS 9B 107
109 112 127 130 l 3B 13B 13B 162 16B 1 '77 19S 209
213
00NE
We will now generate random data for one other type of problem. If it is
4 o'clock, 10 hours later it will be 2 o'clock This concept contains the seed
of the development of modular arithmetic. First let us write a little program
to take random times and add random numbers of hours. The random times
must be numbers from 1 to 12. The iandom numbers of hours could have
virtually any range, but 1 to 36 will do. The flowchart of Fig. 4·6 should
help to organize the problem. We c.an determine the number of computations
with a loop. Here we are picking 10 pairs of numbers, with T for time and
H for hours. Then we add them and check to see if the sum is less than or
equal to 12. If the sum is less than or equal to 12, we want to have the sum
printed as the time. If the sum is greater, we want to subtract 12 and check to
see if the result is less than or equal to 12, etc. After the sum is printed we
want the computer to return and pick another pair and repeat the same process
until 10 pairs of numbers have been picked and prncessed. See CL0CK1.
68 Basic BASIC
( Start )
LETH= INT
(36 • RND(1) + 1)
LETS= T + H
LETS=S-12
NEXT P
END
Fig. 4-6. Flowchart for adding hours to times and computing times for
program CLOCK.
Now, if we want to change the number picked for hours, we can change
line 30 to 30 LET H = INT(l2 ° RND(l) + 1) and get the same range for
both T and H. But then we would have two lines using exactly the same
function:
20 LET T =
INT(l2" RND(l) + 1)
30 LETH= INT(l2 ° RND(l) + 1)
This situation is a candidate for the program-defined function:
DEF FNC(Z) INT(l2 ° RND(Z) + 1)
Then lines 20 and 30 are
20 LET T = FNC(l)
30 LETH= FNC(l)
Computer Functions 69
CL0CK I
10 F0R P=I T0 10
20 LET T=INTCl2*RNDC1>+1>
30 LET H=INTC36*RNDCl>+l>
40 LET S=T+H
50 IF S <= 12 THEN 80
60 LET S=S-12
70 G0T0 50
80 PRINT H"H0URS FR0M"T"0 'CL0CK IT WILL BE"S"0 'CL0CK"
90 NEXT P
100 END
RUN
CL0CK I
D0NE
CL0CK2
RUN
CL0CK2
D0NE
70 Basic BASIC
Modular Arithmetic
From the clock program we can easily develop the concept of modular addi-
tion. The biggest difference between modular addition and ihe last program is
that for modulo 12 addition mathematicians define the set of integers as (0, I,
2, 3, 4, 5, 6, 7, 8, 9, 10, ll), dropping 12 and appending 0. Now we may not
allow sums of 12 as before. So we will have to change line 50 to test for less
than or equal to 11 not 12. But we must not change line 60 which subtracts
12. Why? Since we defined a function in CL0CK2, we need change only line 5
to generate integers from 0 to 11. As we wrote CL0CK1, we would have had
to change two lines. Of course, we will have to change the printing and name
the new program M0Dl2.
M0012
8
2
7 + 6
+ 5
1
1
M0D
M00
12
12
70
80
Ll':T S=A*B
1-'tnNT A"*"B"= , ...
+ 9 11 M00 12 85 GOSUl:l 500
8 + 6 2 M00 12 87 Pii!NT
10 + 8 6 M00 12 90 NEXT I-'
1 + I 2 MllJD 12 490 STOI-'
1 + 3 4 M00 12 500 IFS<= 11 THEN 530
7 + 11 6 MllJD 12 510 LE.I S=S-12
10 + 9 7 M00 12 520 GOTO 500
1 + 7 8 M0D 12 530 Pti!NT S",'ICl• 12
540 i<ETU1<·\J
D0NE 9999 E1~D
iW•\J
l~AMD 12
10 + 4 2 MOD 12 10 4 12
1 + 2 3 MOD 12 l
* 2
4
?
'10.D
MOD
6 + I 7 MOD 12 6 * l
l>'
3 + 10 l M0D 12 3
* 10
6 '·10fl 12
+ 10 11 ·"iOD 12 * 10
6
10
M'1D 12
* '10lJ 12
D01\JE
'.~.'be:·~ th8!·~ fr; ~dditic~, ~~ltiplic~t.tivu. i:; bvu.11d tu fvHv-w. Suppusc wt:: 1·11ul-
tiply 5 by 7. We are accustomed to getting 35. But for M0Dl2 we only allow
0 through ll, so we subtract 12 and get 23, which is still too large. Subtract
12 again to get 11. Thus we are going to use the subtraction routine in the
multiplication part of M0D 12 also. This is a G0SUI3 situation. In the flow-
Computer F1111ctio11s 71
chart of Fig. 4-7, the G0SUB predefined prncess is the subroutine of lines 50,
60, and 70 in M0Dl2. Of course, there are more changes in printing. We call
the program MAMD12 (Multiply and Add MoD 12).
Start
LETS=A+B
Gt;l>SUB 500
LETS= A• B
END
Fig. 4-7. Flowchart for adding and multiplying mod 12 for program
MAMD12.
SUMMARY
Two majm expansions in om programming capability have occurred in this
section. \·Ve are now able to gene1ate random numbers in any range we like.
They can be limited to integers or they can be decimal numbe1 s And lists
may now Le dirne11sio11ed to the length that we requite. \·Ve have also used the
G0SU U statement to good advantage i11 a moduhu a1 ithrnetic progrnm.
72 Basic BASIC
One of the very common uses of computers is for data processing. There is
no clear cut definition for data processing that distinguishes it from other
kinds of computer activity. In a sense, all computer work is data processing.
However, data processing often implies that the computer is being used to
sort, collate, tabulate, and/or otherwise process data. Such activities as process-
ing questionnaires fall in this category.
73
74 Basic BASIC
Start
Initialize
four-item
list all O's
PRINT
headings
LETT[N] END
=T[N]+1
TV'.::.;
I0 FOi< I= 1 TO 4
20 LET Hl.l=O
30 i'lEX T I
31 rff.M EACH ELEME.\JT IN THE LI ST IS \10v' '.'.EHJ
40 HEAD N
49 nEM TEST FOk THE END JF DATA
50 IF N=-1 THEN ~O
~9 HEM INCHEASE THE TABULATING ELEMENT FOR N SETS BY ONF
60 LET TlNl=TlNl+l
70 GOTO 40
80 Pl<INT "NO. OF TV'S","NO. OF FAMILIES"
89 REM NOW PRINT THE NUMBER OF SETS AND THE NUMBER OF FAMILIES
90 FOR I=l TO 4
100 PRINT I,T[IJ
I 10 NEXT I
498 HEM EACH ITEM OF DATA IS THE NUMBEk OF TV'S IN ONE FAMILY
500 DATA 1,3,4, 1,2, 1,3, J, 1,2,4, 1,3, J,2,4, 1,3, t, J, J,Lh 1,3,,2,
2, J, 2
510 DATA 2J11,3,3,2,2 ,1,1,1,2,2,,3 ,4,4,2,4,1,L 1,2,4,2,1 .. 2,1
520 DATA • 1
999 END
RUN
TV'S
iVJ. OF Iv·:;, NU. Ur· FAMILIES
1 20
2 15
3 8
4 9
D0NE
Elementary Data Processing 75
There are some more things that we can do with TV'S. We might have the
computer count the number of families or count the total number of television
sets. These figures may be computed as the data is being read. There can be a
counting statement LET C = C + 1 somewhere between lines 50 and 70, and
there can be a summing statement in the same part of the program. LET S =
S + N will total the number of sets. Then as long as we have the total number
of sets and the total number of families, we might just as well compute the
average number of sets per family. These are left as exercises.
With just a few modifications TV'S can be extended to handle data pertain-
ing to several different things.
Suppose in taking a census, we ask not only how many television sets the
family has, but also how many cars, homes, and bathrooms. All that is neces-
sary is to have four counting lists instead of one. We need one list for each
item being counted. In lines 10, 20, and 30 we initialize four lists at 0 for up
to eight items in any one category. This could be more or less for any par-
ticular problem. vVe check for dummy data in line 50 and then update the
four lists in lines 60 through 90. In the printing routine, I determines the
element number in each list and so is the number of items in each list. T[I]
is the number of families that have I television sets, C[I] is the number of
families that have I cars, etc. See program TCHB.
From the results we see that there were nine families with one car, seven
families with two television sets, etc. We could also do more data processing
in TCHB. We could find the average number of cars per family, etc.
TCHB
10 F0R I= 1 T0 6
20 LET TCil=CCil=H (Il=BCil=O
30 NEXT I
31 REM ALL TABULATING LISTS ARE HHTIALilED AT ZER0
40 READ T, c,H• B
49 HEM CHECK F0R DUMMY DATA
50 IF T=-1 THEN 110
59 HEM 60 T0 90 ENTER THE LATEST DATA IN THE F0UR LISTS·
60 LET H Tl=TC Tl+ 1
70 LEI CCCJ=CCCJ+l
80 LET H(HJ=H(HJ+l
90 LET 8(8J=8(8]+1
100 G0T0 40
i09 HEM HERE THE HEADINGS ARE PRINTED
110 PHi NT "NUMBER, TV'S, CARS, H0MES, BATHS"
119 REM HERE THE RESULTS ARE PRINTED
120 F0R I=l T0 6
130 PRI1~T IITCIHC(IJJ HCIJJBCIJ
140 NEXT I
349 RE~ EACH GH0UP 0F F0UR NUMBERS IS F0R 0NE FAMILY-
350 DATA 1,1,1,1,2,1 ,1,2,3,2,1,2 ,4,3,2,6,4,2 ,115
355 DATA 2, 1, 1,3, 1, 1, 1,3,2, 1, 1,2, 1.1, 1, 1,2, 1, 1.1
360 DATA 2,2,2, 6, 11 1, 1, 4, 3, 4, 2, 6, 1. 2, 1,2, 2, 2• 2,6
365 DATA 2, 1, 1,2,-1,0,0,0
400 ENO
RUN
TCHB
IJ0NE
0 this pe1 son does not want to take the comse, and where K is I he does. So
when K =0, the tabulating statement adds 0 to the previous value in the C
list, which does not change the number there. This is what we want for the
person who does not want to take the c0t11 se. However, if K 1, then the
tabulating statement adds I to the previous value of the en ti y in the C list,
which is exactly what we want the computer to do for a pe1son wanting to
take the course. Again the dummy data is -L
From C0UHS1, we can easily see that seven people want to take chemistry,
five people want to take physics, etc.
One last thing we might try to consider in this section is getting larger
amounts of C.:ata in a program similar to C0UH.Sl. Suppose you want to see
what results might look like for say 500 people . Well, you could ask 500
pcGp?c tlit\.1 thcx1 lypt: uul (_ln i.iiai. t1ala. 01 you couid generate 1 andon1 data,
with the understandin g that the results will be random and may not simulate
the real situation. However, knowing that the numbers will be random will
help you spot seiious errors if there are any. For .500 people and random data,
each course should <l1 aw about 250 yeses. If the results show 96 or 600 yeses
Elementary Data Processing 77
COUHS I
10 FOK l=I TO 5
20 LET CCll=O
30 NEXT I
40 FO:< H= I TO 5
50 :<EAD t<
60 IF t<=-1 THEN 100
70 LET C[Hl=C[Hl+t<
80 NEXT :-1
90 GOTO 40
I 00 P1<1 '\J T "CHEM I STKY", "f'HY 51 CS", "Ff<ENCH", "SPAN I SH"• "CALC 1 IL' IS"
110 F:l1< l=I T0 5
120 PKINT CCIJ,
130 NEXT I
990 KEM DATA IS IN OHDER CHEMISTKY PHYSICS FRENCH SPA'\JISH
CALClfLIJS
995 <EM ' I ' MEANS YES 'O' MEANS NO
1000 DATA 1,0, 1 .. 1,,0 .. 0,0 ... 1, 1 .. 0 .. 1.. l.11 .. 1 .. 1.. 0 .. 0,, l.11,,0 .. 1 .. 1 .. 0 .. 0 .. 1
1010 DATA Q,, 1, I .1 Q, 1.1 Q, Q, Q, l .1 Q, l .1 l .1 Q, l .1 Q, 1, 1, Q, Q, 1, t, O, Q, Q, I
I 020 DATA o,o, 1,0, 1, 1,0, 1,0,0
1100 DATA - I
9999 END
RUN
C0URSI
D0NE
in some course, then you must search for the error. One nice thing about using
random data is that you do not have any data to type in. So in C0URS1 we
may eliminate lines 1000, 1010, 1020, and llOO. Now the REM statements
are not quite relevant. Line 60 can be deleted as we are not testing for dummy
data and line 50 is deleted as we are not going to read data anymore. Line 90
will be taken care of by putting in a loop 1 to 500 to simulate 500 people. To
get random numbers 0 or 1 we need INT(2 ° RND(l)). The initializing, the
tabulating, and the printing of C0URS1 can be used in the new program
C0URS2, where the results are reasonably close to 250.
SUMMARY
We have seen lists used to analyze data from questionnaire-type questions
having numerical or yes-no type answers. The tabulating may be done using
one or several lists depending on the problem itself. Random numbers may be
used to try out such programs with many numbers. The random nature of
these numbers may help to spot serious program errnrs, which might not show
up with small amounts of data unless you check the totals by hand.
C0URS2
10 F0R 1=1 TO 5
20 LET C[IJ=O
30 NEXT I
33 REM THIS L00P SIMULATES 500 PE0PLE
35 F"0R X= I T0 500
40 F"0R R=l T0 5
42 REM THIS L00P L00KS AT FIVE C0URSES F0R EACH PERS0N
48 REM PICK A RAND0M NUMBER ZER0 0R 0NE
50 LET K=INTC2*RNDCI>>
70 LET CCRl=CCRJ+K
78 REM NEXT C0URSE
80 NEXT R
86 REM NEXT PERS0N
90 NEXT X
100 PRINT "CHEM! STRY", "PHY SI CS", "FRENCH"," SPAN I SH", "CALCULUS"
110 FOR I=I n; 5
120 PRINT C[I),
130 NEXT I
9999 END
RUN
C0URS2
D0NE
2) Modify program C!Z)URSI to find the number of people who want to take
chemistry and physics.
3) Modify CIZ)URS2 to generate twice as many yeses as nos.
4) Modify C0URS1 to find the number of people who want to take physics but
not calculus.
j 5) Consider a questionnaire in which there are 14 questions which call for yes,
no, or other answers. Let 1 be yes, 2 be no, and 3 be other. Set up three separate
lists for yes, no, and other. Generate 25 sets of 14 numbers 1, 2, or 3 and find the
number of each type of answer for each question number. Print the results in
decipherable form.
6) Modify CIZ)URS2 to generate yes-to-no answers in a ratio of 3 to 4.
5-2 ARRAYS
So far we have only been able to store numbers in a simple variable or in
a list. There will be situations where we will want to store more numbers than
is convenient in a list. While we have seen that we can use several lists very
effectively, BASIC provides a two-dimensional list for such situations. It may
be called an array. You may think of an array as being similar to a checker-
board. Instead of the familiar single subscript we have been using for lists, we
will need double subscripts; one for rows and the other for columns. (As with
lists, <'nmrintP.rs v<1ry SomP. will "llow 0 snhs<'ripts, othP.rs hP.gin with 1.) For
an array designated as A, A[l, 1) is the number in the upper lefthand corner.
(In some cases, it will be A[O, O].) A[l, 2) indicates the number in row 1
and column 2; A[5, 8) indicates the number in row 5 and column 8, etc. In
other words, the first subscript indicates the row starting at the top and the
Elementary Data Processing 79
second subscript indicates the column starting at the left. Thus, A[R, CJ indi-
cates the number in row R and column C.
An array is just a set of numbers arranged in columns and rows, This per-
fectly matches the printed result in program TCHB in Sec. 5-1. We may use
each column of an airay in the same manner that we used each list in that
program, and we can use each row to keep track of the number of families
having that number of the item being tabulated. But before we tackle TCHB
in an array, we should see a little more how arrays operate.
Very often we will use a nested loop, with one loop taking the computer
through the columns and the other loop going through the rnws . The structu1 e
of an array is shown in Table 5-1. For students without 0 subscripts, consider
the dashed outline to exclude the 0 row and 0 column. For students who have
0 subscripts, consider the dashed outline to suggest that it is optional whether
or not you use them at this time.
1------------ -----1
: [O,OJ -~·~-~~-~,3J _ _[~4l__[~~I
I I
I [1, OJ I [1, 1J [1, 2] [1, 3J [1, 4] [1, SJ
I I
I I
I [2, OJ I [2, ·11 [2, 2J [2, 3] [2, 4J [2, Sl
I I
I I
J~3_'._0u [3, 1 J [3, 2] [3, 3J [3, 4] [3, SJ
ARRAY I
D0NE
page and then down, just as we read the printed page. In this program we
have called the array T . .1.A... ny letter of the alphabet may be used. Ho·vvevcr, do
not use the same letter to name both a list and an array in the same program.
This is because the computer treats a list as an array with just one column or
one row, depending on the computer.
We have filled the array just as the printed message states. However, as
was noted in Chap. 1, in order for the work of the computer to be useful,
we must eventually get back from the computer some printed results. Note
that we say eventually. The more advanced we get in programming, the
more we will do things that are not immediately printed. Nonetheless, just
to restore your faith in the computer, let us ask it to print some values from
the array we just created. After line 80 we will insert a variety of printing
ARRAY2
TC 3, 41 = 14
6 7 B 9 10
II 12 13 14 15
D0NE
Elementary Data Processing 81
with labels and comments much as we did earlier in the introduction to lists.
SeeARRAY2 .
The elements of an array constitute variables just as do the elements of a
list \Ve may operate on any element or elements in the array we choose.
Consider ARRA Y3.
ARRAY3
10 LET A=I
20 F'0R R=I T0 3
30 F'0R C=I T0 5
40 LET ACR1Cl=A
50 LEf A=A+ I
60 NEXT C
70 NEXf rl
90 PrlINT "WE PRINT THE 0RI GINAL ARRAY"
100 G0SUB 900
110 PRINT "WE CAN MULTI PLY EVERY ELEMENT IN THE 4TH C0LUMN
BY 6"
120 F'0R R=I T0 3
130 LET A(R,4l=ACR1 4l*6
140 NEXT R
150 G0SUB 900
160 PRINT "WE CAN SUBTRACT THE 3RO R0 W F'R0M THE 2N 0 R0 W"
170 PrlINT "ANO PUT THE RESULT IN THE 3RD R0W"
180 F'0R C=I T0 5
190 LET A[3,Cl=AC21 Cl-AC3.Cl
200 NEXT C
210 G0SUB 900
880 ST0P
690 REM ****PRINTING SUBR0UTINE IS HERE****
900 F'0R R=I T0 3
910 F'0rl C=I T0 5
920 PRINT A(k1ClJ
930 NEXT C
940 PRINT
950 PRINT
960 NEXT R
970 RETURN
999 END
;(UN
ARRAY3
6 7 8 9 10
II 12 13 14 15
6 7 8 54 10
11 12 13 84 15
6 7 8 54 10
-5 -5 -5 -30 -5
D0NE
82 Basic BASIC
We oan even change the size of the array during a program. In ARRAY4
we begin with the original 3 by 5 array of ARRAY3 and tack on an extra row
to enter the sums of the entries in the first three columns. Notice that in both
ARRAY3 and ARRAY4 we are able to use G0SUB to save writing the printing
routines more than once.
You should begin to see that we have exactly the same control over the
contents of an array that we do over the contents of a list
Now let us look. again at our census program TCHB. There we used an
8-row by 5-column array in which the first column simply contained the row
number and the other four columns each contained tabulated results for a
different item. We may now put the READ statement in a loop going from 2
to 5 and let the loop variable determine the column in which the tabulation
takes place. The other features of the program are procedures that we haVE
used before. See TCHB+.
ARRAY4
10 LET A= l
20 F'0R R=l T0 3
30 F'0R C=l T0 5
40 LET A(R,Cl=A
50 LET A=A+ 1
60 NEXT C
70 NEXT R
80 P1HNT "HERE IS THE 0RI GINAL ARRAY!"
100 F'l3R R=l T0 3
110 F'0R C=l T0 5
120 PRINT ACR.ClJ
130 NEXT C
140 PRINT
150 PRINT
160 NEXT R
168 REM SET ALL ELEMENTS IN THE 4TH R0W T0 Z:ER0
l 70 F'0 R I = l HJ 5
180 LET A(41Il=O
190 .'IEXT I
200 PRINT "Nl31<1 WE HAVE THE 4 BY 5 ARRAYJ"
210 G0SUB 500
219 KEM THIS R0UTINE ADOS C0LUMNS ANO PUTS THE SUM IN THE 4TH
R01<1
220 F0R C=l T0 5
230 F0R R=l T0 3
240 LET A(4,CJ=AC41CJ+AlR1C J
250 NEXT R
260 NEXT C
270 PRINT "THE F'0URTH R01<1 C0NTAINS THE SUMS 0F THE F'IRST 3
R0WS."
280 G0SUB 500
490 SHIP
498 REM **THIS IS THE PRINTING R0UTINE F0R THE 4 BY 5 ARRAY**
500 F'0K R=l T0 4
51 0 F'0 I< C= 1 T0 S
520 PRINT AC R, CJJ
530 NEXT C
540 PRINT
550 Pl<INT
560 NEXT I<
570 l<ETUKN
999 ENO
Elementary Data Processing 83
RUN
ARKAY4
6 7 8 9 10
11 12 13 14 IS
6 7 8 9 10
11 12 13 14 15
0 0 0 0 0
THE F"0URTH R0W C0NTAINS THE SUMS 0F" THE F"I RST 3 R0 WS.
I 2 3 4 5
6 7 8 9 10
II 12 13 14 15
18 21 24 27 30
D0NE
TCHB+
10 F"0R R=l T0 8
14 REM HERE THE R0W NUMBER IS ENTERED IN THE F"IRST C0LUMN
15 LET SlR. I J=R
20 F"0R C=2 T0 5
30 LET SCR. CJ=O
40 NEXT C
50 NEXT R
68 REM WE AijE ENTERING F"IGURES IN C0LUMNS 2 THR0UGH 5 0NLY
70 F"0R C=2 T0 5
80 READ N
85 IF" N=-1 THEN 110
88 REM N DETERMINES THE R0W NUMBER WHICH KEEPS TRACK 0F N
ITEMS
90 LET srn. CJ=S[N, Cl+ I
100 NEXT C
105 601'0 70
110 PRINT '"NUMBER1TV'S, CARS, H0MES.BATHS"
119 REM HERE THE RESULTS ARE PRINTED
120 F"0R R= I T0 8
130 F"0R C= 1 T0 5
140 PRINT SC R. CJJ
150 NEXT C
155 PRINT
160 NEXT R
349 REM EACH GR0UP 0F" F0UR NUMBERS IS F"0R 0NE FAMILY- T.C.H.B
350 DATA I. 1.1. 1.2, l1 l121 3,2, 1,2, 4, 312181 4,2, I• 5
355 DATA 2. 1, 1,3, 1, 1, 1,3,2, 1.1,2.1.1.1, 1,2, 1, 111
360 DATA 2, 2, 2, 6, 1, 1, t. 4, 3, 4, 2, 61 1, 2, 1, 2, 2, 2, 2, 8
365 DATA 2, 1, 1, 2, - t. o, Q, O
400 END
84 Basic BASIC
00NE
SUMMARY
We may now use a powerful extension of the list concept, the array. An
array may be thought of as an arrangement of numbers in which there are
rows and columns. Numbers in an array may be accessed by designating the
location by a double subscript such as H[3, 7) for the number in array named
H which is located in the row numbered 3 and the column numbered 7. As
you may have guessed, you will not need a DIMension statement as long as
you do not exceed a subscript of [ 10, 10 J.
three possible answers, Generate random data and print the results in a 10 by 4
array, Use the first column for the question number and the other three for yes, no,
or other. Have the computer generate 50 questionnaires.
Start
Initialize 15 by 5
array cols. 2-5 at 0
Col. 1 to contain row no.
PRINT PRINT
headings array
F¢RQ = 1 READ
T¢ 15 A
LET S[Q, Pl
NEXT Q
= S[Q, Pl+ A
two dimensions: one for rows and one for columns. We want an array with 15
rows and 5 columns (4 for categories and 1 for the question numbers). DIM
8[15, 5] will provide just such an array.
DIM (TWO-DIMENSIONAL)
DIM A[R, CJ sets up an array designated as A with highest column
number C and highest row number R. The statement is required if
either R or C exceeds 10. Some computers require expiicit integers, I
others allow variables in DIM statements. ___J
In our questionnaire problem, there are three things that we must keep
track of: 1) the category of the respondent, 2) the question number, and 3) the
response. We may organize the data and results according to Table 5-2.
Array
Code in
Column Number Use DATA Line
It will be easier to organize the data, if we reserve an entire data line for
each person. Then we can put the category code (2 through 5) in the first
location and the response (0 or 1) in the next 15 locations. A DATA line will
look like this:
XXX DATA 4, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1
where the 4 indicates that the respondent is female and 21 or over, and the
l's and O's mean yes and no in response to the 15 questions. We could count
the number of people in advance or use dummy data so that the printing can
be done after all data is read.
40 f'{H C=2 TO 5
48 t<EM LINE 50 SETS THE LAST 4 C3L 1J;>!\JS AT ZERO
50 LET S(i<,CJ=O
60 NEXT C
70 NEXT 1<
78 REM BO ·°'EADS THE CATELORY FOR THE NEXT PICRSON IN THF SIFlVFY
BO .<EAD P
90 IF P=-1 THEN 200
9B kEM Q GOES TH-<OUGH THE. 15 f.lUcSTl:JNS
100 FOR 0=1 TO 15
110 READ A
120 LET S(Q,PJ=S(Q,PJ+A
130 NEXT 0
I 38 REM LI NE I 40 SENDS THE C::JMPIJTER BACK TO REl'.\D ANO THFfl LI NF'
OF DATA
140 GOTO BO
198 REM THE Prt!NTING BEGINS HERE
200 PHl•\JT "OUEST MALE MALE FEMALEFEMALE"
210 ?t<INT "NIJ:-1flE•< 21+ U'lDEt< 21+ IJNDE•l"
220 FOR R=I T3 15
230 FOR C=I TJ 5
250 PRINT S(K,CJ;
260 NEXT C
270 PRl·'IT
2ao NEXT R
49B REM ***A LINE Ll~E 500 MAY HELP TO LINEUP THE DATA LINES
499 REM IN TYPING***
500 rtEM I• I, I• I, I, I, I• I• I• I• I• I• I• I• I• I
501 DATA 4, 1.0.1.1.1.0.0.1.1.0.1.0.1.0, 1
so2 DATA 4, 1.o.o.0.0.1.1.0.1.1.o.o.o.1.1
so3 DATA J.1.1. 1.1.0.0.1.0.1.o.o.1.1.o.o
504 DATA 5, 1, 1, 1, o, o, O, 1, O, o, o, 1, t. I• 1, 0
sos DATA 2.1.1.1.0.0.1,0.1.0.0.1.1.1.1.0
506 DATA s.0.0.1.0.1.0.0.0.1, 1.1.0.0.1.1
507 DATA s.0.0.0.1. 1.1.0.1.0.1.0.1.0.0.1
SOB DATA 2.0.0.1.1.0.o.1.1.o.1.o.1.o.o.1
509 DATA 4, 1, I• t. 1, 1, 1, 1.0, o,o, 1,0, l•O•O
s10 DATA 2.1.1.0.0.1.0.1.o.o.o.o.1.1.1.1
900 DATA -I
9 99 END
JH1\J
SUK\/EY
D0NE
Notice in SURVEY that while there are four categories in the original
problem, there are five additional categories generated by the conditions of the
problem. They are male, female, under 21, 21 or over, and total. We may
88 Basic BASIC
further process the tabulated results after line 140 in SURVEY by totaling up
t11e appropriate columns to get these latest categories tabulated. Of course, we
will have to change the DIM statement to DIM S[l5, 10). This is done in
SRVEYl. Study lines 145 through 190 carefully to assure yourself that the
correct values are being tabulated there.
There are many other results that we might try to find. There are other
SRVEYI
!O D!M S(!5:!03
20 F0R R=I T0 15
28 REM Li NE 30 ENTERS THE Rll\' NUMBER IN THE Fl RST C::JLll"lN
30 LET SU<. I l=r<
40 FOR C=2 TO 10
48 rtEM LINE SO SETS THE LAST 9 COLUMNS AT ZERO
50 LET SCR1CJ=O
60 NEXT C
70 :\JEXT I<
78 REM 80 READS THE CATEGORY FOR THE NEXT PERSON l'IJ THE S 1 1RVl'Y
80 READ P
90 IF P=-1 THEN 145
96 REM 0 GOES THf<OUGH THE 15 OUESTI ONS
100 FOR Q=l TO 15
110 READ A
120 LET sru.PJ=SCO.Pl+A
130 NEXT Q
138 REM LINE 140 SENDS THE C0MP'JTEH BACK TO READ ANOTHER Ll'IJF
::JF DAT£\
140 G0T0 80
145 F0R R=l TO 15
150 LET SCR16l=SCF<12J+SCR13 J
160 LET SCR. 7l=SCk14l+SC:;;,5J
I 70 LET sr R. 8 J =SC I<. 3 J + S[ ·~. 5 J
180 LET SCR19l=SCl<12l+SC ll• 41
185 LET SCR1IOJ=S£:l.6l+SCl< 17l
190 NEXT R
198 REM THE PRINTING BE,GINS HE:-<F:
200 PRINT "QUEST MALE MALE FEMALEFEMALE"
210 PRINT "NUMBER 21+ UNDER 21+ UNDER MALE FEMALE IJNDEq
21+";
211 PRINT " TOTAL"
220 FOR R=l TO 15
230 FOr< C=l TO 10
250 PRINT SCR.CJJ
260 NEXT C
2 70 PRINT
280 NEXT N
498 r<EM ***A LINE LIKE 500 MAY HELP TO LINE•JP THF DAT/\ LINF<;
499 REM IN TYPING***
500 REM 1, 1, 1.1, 1, 1, 1, 1.1, 1.1.1, 1.1. 11 I
501 DATA 4, 1.0.1.1.1.0.o.1.1.o .1.o.1.o.1
502 DATA 4, 1, o, o, Q, o, l • 1, O, 1, I• o. 01 o, l • 1
503 DATA 3.1. 1.1.1.0.0.1.0.1.o.o .1.1.o.o
504 DATA 5,1.1.1.0.0.0.1.o.o .o.1.1.1.1.o
505 DATA 2, I• 111,0,0.1,0, 1,0,0.1, l• 1, 1,0
506 DATA 5,0,0, 1,0, 1,o,o.o, 1, 1, 1,0.0.1, 1
50 7 DATA 5, o, o, o, I, 1, 1, o, 1, o, 11 o, 1, o. o, 1
508 DATA 2,0,0, 1, 1.o,o, 1, 1,0, 1,o, 1.0.0, I
509 DATA 4,1.1.1.1.1.1.1.0.0 .0.1.0.1.0.0
510 DATA 2.1.1.0.0.1.0.1.o. o.o.o.1.1.i.1
900 DATA -1
999 END
Elementary Data Processing 89
tWN
SR VEY I
00.'-lE
totals that could be tabulated. At the time P is read, we could total the
number of people in each of the original four categories and enter these
totals in row 16. Then we could compute averages. There are numerous ratios
that we could evaluate. We could have the computer generate random data to
get larger numbers in the printed result. That would require random integers
2 through 5 for P in line 80 and random 0 or 1 in line 110 for the yes-no
responses.
SUMMARY
We see that the two-dimensional array permits tremendous flexibility. We
may determine its size exactly. The array serves as a vast storage area for
large amounts of data or tabulated results. We may process the contents of
an array and enter results in other parts of the same array with tremendous
maneuverability.
The DIM statement may be used to specify subscripts greater than 10 in
the two-dimensional array much as it was used for lists.
6) Fill an array with the multiplication table up to 12 X 12, and print the last
three rows.
j 7) In a 12 by 12 array enter all l's in the upper left to lower right diagonal and
the left-most column, and all O's elsewhere" Then beginning in the third row, second
column, enter the sum of the entry in the same column of the row immediately
above and in the column one to the left and the row immediately above, through
the 12th row, 11th column. Print the result.
\
CHAPTER 6
Specific Applications
N I"D--t
13398 = (1)[7854] + (6-1)
=
7854 (1)[5544] + 2310 (6-2)
=
5544 (2)[2310] + 924 (6-3)
=
2310 (2)[ 924] + 462 (6-4)
=
924 (2)[ 462] + 0 (6-5)
According to Euclid the greatest common factor of 13398 and 7854 is 462, be-
=
cause 462 was the divisor when the remainder was 0. Indeed 13398 29 " 462
and 7854 = 17 ° 462. That took only five tries. How many would it have taken
using the old method? Now all we have to do is figure out why it works.
Look carefully at Eq. (6-5). 924 is divisible by 462 because the remainder
is 0 and 0 is divisible by any nonzero number. This 0 remainder is the key to
the entire proposition. Now look at Eq. (6-4). Since 924 is divisible by 462, so
is (2)[924] + 462, which makes 2310 divisible by 462. Now look at Eq. (6-3).
91
92 Basic BASIC
Since 2310 and 924 are both divisible by 462, so is 5544. This makes 7854
divisible by 462, which in turn makes 13398 divisible by 462, which is the
original contention. The argument we have just presented is hardly a proof of
the Euclidean algorithm, but it should be convincing.
Now, how do we get the computer to carry out this process? First, from
Eqs. (6-1) through (6-5) we should see that we have simply taken the old
divisor D and made it the dividend and the old remainder R and made it the
divisor. So we will get the computer to LET N D and LET D R after we
look at the remainder to see if it is 0. If the remainder is 0, we direct the
computer to print the last divisor as it is the greatest common factor.
Now we shouid be abie to draw the flowchart (Fig. 6-1) and write the
program C0MF AC.
Start
END
LET I
= INT(N/D)
LET R
= N-1 • D
LET N =D
LETD = R
Fig. 6-1. Flowchart for using Euclidean algorithm for program C\ZlMFAC.
Specific Applicati ons 93
C0MF"AC
N1D?1339 81922251
G·C•r• = 33
N1D?7412 79192225 1
G·C•r• = 33
N, D? 133981 78 54
G·C·F"o = 462
N1D?9911 199
G.C•r• I =
N, D?2 728 51• 2461 56
G·C·r• = 281
N, D?O, 0
D0NE
1) Write a program to add fractions given the numerator s and denominat ors.
Print the sum reduced to lowest terms.
2) Do problem 1) for multiplica tion.
the
,/ 3) INPUT two pairs of coordinates. Have the computer find the slope and
Y-intercept of the straight line containing the points and print the results as rational
be
numbers reduced to lowest terms. If the result is negative, have the numerator
the negative number.
4) As a project, write a program to factor quadratic expressions with integer
coefficients. Be sure to allow for 0 eoclficicnts and factor out greatest common
factors of all three coefficients.
149/2 ° 0 7 = 1 + 21/2 ° 0
7
or 149 = 1 ° (2 ° 0 7) + 21 (6-6)
21 = 0 ° (2 ° 0 6) + 21 (6-7)
21 = 0 ° (2 °. 5) + 21 (6-8)
21 = l (2 °. 4) +
0
5 (6-9)
5 = 0 • (2 °. 3) + 5 (6-10)
5 =
1 • (2 ° 0 2) + 1 (6-11)
l =
0 ° (2 • 0 1) + 1 (6-12)
l = 1 ° (2 ° 0 0) + 0 (6-13)
By successive substitution we see that
Specific Applications 95
149 = 1 ° (2 ° 0 7)
+ 0 0 (2 00 6)
+ 0 0 (2 00 5)
+ 1 0 (2 00 4)
+00(2003)
+ 1 0 (2 00 2)
+ 0 0 (2 00 l}
+ 1 0 (2 00 0)
So that
14910 = 100101012
Equation (6-6) may be written in general as
N =I 0
(2 ° 0
E) +R
where N is the number, I is the integer quotient, E is the exponent on the
base-2, and R is the remainder after integer division. Therefore
I = INT(N/(2 ° 0
E)
and, solving for R we get
0
R=N I (2° 0 E}
Now, looking at Eqs. (6-6) through (6-13} we see that we have an iterative
process in which the new number is to be the old remainder and the exponent
on the base-2 is reduced by 1 until it gets to 0. This looks like a loop in which
the loop variable is the exponent on the base-2 and stops at 0. Where does it
start? Earlier we decided that the greatest exponent on 2 could be 20. Now
we should be able to assemble our problem into a flowchart (Fig. 6-2}.
BASE
10 READ N
20 PRINT NJ "BASE TEN =";
30 F0R E=20 T0 0 STEP -1
40 LET l=INTCN/2•EI
50 PR! NT I ;
60 LET R=N-1*2•E
70 LET N=R
80 NEXT E
8 5 PR! NT "BASE T ,,,.0"
86 PRINT
90 GClT0 10
100 DATA 999999.,1,16
110 END
RU'J
BASE
BASE TWO
Fig. 6-2. Flowchart for conversion
Start from base-10 to base-2 .
F(l'>R E = 20 LET I .I
Ti/J 0 STEP-·1 - - - = INT(N/2 •• E)
1
I
LET R
=N-1•2••E
NEXT E LETN = R
I BASE. TE.N 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
BASE Two
16 BASE TEN 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
BASE. TwO
Looking carefully at the printed results in BASE, we can see that 16 base-10
does equal 000000000000000010000; however it is difficult to sort that out.
Printing the variable I is controlled by semicolon spacing which will not place
one-digit numbers in adjacent spaces. We can however, get the digits next to
each other by printing them explicitly. If we say PRINT "l"; the next printed
character will be printed in the next space. So, instead of 50 PRINT I; we
insert
45 IF I=l THE.N 55
50 PHINT "Q";
52 G0TO 60
55 PRINT "l"J
Specific Applications 97
RUN
BASE-2
By not worrying too much about the fact that we were going to require a
large number of digits, we have succeeded in printing numbers with 21 digits.
Quite often in programming, as in any problem-solving situation, you will
solve seemingly impossible problems by emphasizing those things that you
can do rather than holding back because of all the things you think that you
will be unable to handle.
Let us reassemble the program as it now stands in BASE-2 and insert
another set of data just to see a few more results.
BASE-2
I0 READ N
20 PRINT N; "BASE TEN =";
30 F'0R E=20 T0 0 STEP -1
40 LET I=INTCN/2•E>
4 5 I F' I = I TH EN 5 5
50 Pf<INT "O";
52 GOT0 60
55 Pf<INT "!";
60 LET R=N-1*2•E
70 LET N=R
80 NEXT E
85 Pf<INT " BASE TwO"
86 PRINT
90 GOTO 10
100 DATA 999999., 1, 16
110 END
100 DATA 45,9875,123456
iWN
EJASE-2
Of course we really are not finished with the program yet. We should
eliminate the leading O's. Then the printed results will be in more familiar
form. This is left as an exercise.
98 Basic BASIC
In general, the more control we have over a number in the computer, the
more complex the problems we might expect to be able to handle. So, for the
purpose of learning to control a number in the computer digit by digit, let us
write a program to take the digits of an integer and print them one at a time.
Consider the number 8394. The 8 means 8 thousand which may be written
8 ° 10 ° 0 3; the 3 means 3 hundred which may be written 3 ° 10 ° 0 2; the 9
means ninety which may he written 9 ° IO 0 0 1; and the 4 means four which
may be written 4 ° IO 0 0 0. Looking at the numbers step by step,
8394 = 8 ° IO 0 0
3 + 394
394 = 3 " IO 0 0
2 + 94
94 = 9 ° IO 0 0
1 + 4
4=4°I0° 0 0+ 0
This is an example of the general relationship
0
N=I I0° 0 E+R
R=N-I 0
I0° 0 E
DIGIT
DONE
would be to print the <ligits across the page with semicolon spacing. We can
do that by changing line 60 to rea<l 60 PH.INT I; an<l call for a HUN.
60 PRINT I l
RUN
DIGIT
DONE
.100 Basic BASIC
Now let us see the program with the change and try another number.
(See DIGITl.)
DI GIT I
D0NE
One last consideration is that we might want to eliminate the leading O's.
We leave this as an exercise.
FIRST$
95 REM *
F'IRST STRING PR0GRAM
96 REM
100 LET A$ =
"THIS IS A"
110 READ 8$, C$
120 PRINT A$J " "J 8$J " "J C$J
130 INPUT D$
140 PRINT
J50 PRINT A$J " "J 8$J " "J C$J " "J D$
J55 REM
J60 DATA "PR0GRAM T0''. "DEM0NSTRATE"
J70 END
RUN
F'IRST$
THIS IS A PR0GRAM T0 DEM0NSTRATE?STRINGS
THIS IS A PR0GRAM T0 DEM0NSTRATE STRINGS
95 REM *
PRINT
C0MPARES STRINGS F'0N ORDEN
JOO
110 PRINT "A$"J
120 INPUT A$
130 IF A$ ="ST0P" THEN 240
J40 PRINT "8$"J
J50 INPUT 8$
J60 IF' A$ < 8$ THEN 220
J70 IF' A$ =.
8$ THEN 200
J60 PRINT A$J IS GREATER THAN "; 8$
J90 G0T0 JOO
J95
200
REM
PRINT A$J .IS EQUAL T0 "; 8$
2JO G0T0 JOO
2J5
220
REM
PRINT A$J .IS LESS THAN "J 8$
230 G0T0 JOO
240 END
RUN
0RD$
A$?WHAT' S THIS
8$?WHAT' S THAT
WHAT'S THIS IS GREATER THAN WHAT'S THAT
A$?WHAT'S THIS
B$?WHAT' S WHAT
WHAT'S THIS IS LESS THAN WHAT'S WHAT
A$?WHAT'S WHAT
8$?WHAT' S WHAT
WHAT'S WHAT IS EQUAL T0 WHAT'S WHAT
A$?ST0P
ment 100 PRINT A$(4) will cause the computer to output the character sb'ing
stored in string variable A$, beginning with the fourth character, whereas on
another, the same statement will cause the computer to output the fourth string
of the string list A$. It is because of these differences that we present two dis-
tinctly different schemes for handling strings in the next two sections.
0
7-2 STRINGS-T HE SUBSTRING SCHEME
In the substring scheme, strings may be considered as a complete entity by
referring to A$, B$, etc., or we may consider segments of A$ by using one or
two subscripts. A$(I) specifies the segment beginning with the Jt character and
11
continuing to the end of the string. A$(I,J) [some computers using this scheme
may require A$(I:J)] specifies the segment from the Jt 11 character through the
=
Jill character inclusive, provided I == J. If I J, then A$(I,J) is a single char-
acter. This scheme does not provide for string arrays. (It has been extended on
some computers, however, by using A$(I;J ,K), where the I designates which
string in the single dimension array is referred to and the J,K pair designates
the segment from the J! 11 through the Kth character.)
As with arrays, it is necessary to specify the capacity of any string variable
we intend to use (for more than one character) in a DIMension statement, Thus,
100 DIM A$(10),B$(1 6),C(2,ll) provides for up to 10 characters in A$, up to
16 characters in B$, and two rows and 11 columns in a numeric array G The
C dimensioning is included here merely to demonstrate that string and array
dimensioning may be intermixed in a single statement. The LEN( ) function is
provided to count the number of chmacters actually stored in a string. LEN(Z$)
takes on the value of the number of characters stored in string variable Z$.
In program SEG$1, note the dimensioning in line 100, the use of the LEN(
function in lines 140 and 150, and the printing of segments in line 160.
SEG$1
A$=8ASIC
LENCA$l= 5
A$C I , 2 l =BA
A$C 3 , 4 l =SI
A$C 5 , 6 l=C
The ability to isolate a segment of a string has many uses. We may wish to
pack related information into a single string such as
100 LET D$ = "JANFEBMARAPRMAYJUNJULAUGSEP0CTN0VDEC"
Now we. may select the desired month according to its position in D$, Or, we
might want to use a single string to contain the names of a group of individuals,
last name first, but to print only the last name and first initial.
One common use of string segments is to format numbers in printed results.
For instance, the appearance of the output produced by program SEG$1 could
be improved by using string output to print I and I + 1 in line 160. See lines
110 and 160 in program SEG$2. Notice the compact appearance of the printed
result there.
SEG$2
95 REM *
PRINTING A SINGLE DIGIT NUME~IC
96 REM USING STRING 0UTPUT
100 DIM A$[8J,D$f.9J
110 LET D$="1234567B9"
120 READ A$
130 IF A$="ST0P" THEN 210
140 PRINT "A$="JA$
150 FOR I=l T0 l.ENCA$l STEP 2
160 PRINT "A$C"JD$[ 1, IJJ","; 0$( I+ 1, I+ 1 H ">=";A:£[ I, I+ 11
170 NEXT I
180 PRINT
190 G0T0 110
195 REM
l'!OO DATA "ABCDEF","BASIC","ST0P"
210 END
RUN
SEG$2
A$=BASIC
A$Cl,2l=BA
A$C 3, 4) =SI
A$(5,6>=C
use the technique of program DIGIT in Sec. 6-3. That is, we must isolate the
digits of our number one at a time. Once we have the digit to be printed stored
in I, we must print D$(1 + l,I + 1) since zero is the first digit in D$. This step
Strings and Files 105
SUMMARY
We have used strings to store nonnumeric data. Any string may be con-
sidered in its entirety, or any segment may be isolated using subscripts. A$(I,J)
designates the substring from the Jth to the J! 11 characters, inclusive. By placing
the ten digits in a dummy sb·ing, we gain complete control over the printing
of numerics by using string output.
I SUNDAY
2 M0NDAY
3 TUESDAY
4 WEDNESDAY
5 THURSDAY
6 FRIDAY
1 SATURDAY
Suppose we wish to work with the days of the week. We can easily read the
names of the days of the week into an array. Then these names can be printed
later as labels whenever needed, as shown in program DAYSOl.
It is useful to be able to manipulate data in sh"ing variables. We might want
to know the number of characters in one of them, for example. There are two
ways to find out. One is to use the LEN function. LEN(A$) returns the number
of characters iil A$. Another is to use the CHANGE statement. CHANGE A$
T0 A stores the number of characters in A$ in A(O), converts each of the char-
acters in the string A$ to a numeric equivalent code, and then stores that nu-
meric in a corresponding position of the one-dimensional A array. The code
__ _1 I'_ 11. ! - !- A C'r"ITT / A . __ .. ! _ . _ C" 1 •• _l ... ~1 r"I _ ~1 - r _.. T . I'. . . ,- 1 • T • . 1 .. \
u;::,c;u J.Ul uu;:, 1;:, .t'l.L.1'..1.1.,l \I"llUClJ.L'dU i..Jld.HUdlU '-'uuc lUl .1U1U11Ud.l1UU lULClL'HUUbCJ.
0
The programs of this section were run on the General Electric Information Services
time sharing system.
Strings and Files 107
CHANGE
98 REM * DEM0NSTRATE CHANGE STATEMENT
100 DIM AC30>,8<1>
110 PRINT "STRING"J
120 INPUT A$ •
130 CHANGE A$ T0 A
140 PRINT LENCA$)J "CHARACTERS IN '"J A$J "'"
150 PRINT
160 LET 8<0> = I
170 PRINT "CHAR ASCII C0DE"
180 F"0R I = I T0 A<O>
190 LET 8< I> = ACI>
200 CHANGE 8 T0 8$
210 PRINT "'"J 8$J "' "J AC!)
220 NEXT I
230 END
RUN
probably best be demonstrated with a program. See especially lines 130 and
200 of program CHANGE.
Notice that it required four statements to extract the Jt 11 character of A$.
In program CHANGE, we used statements 130, 160, 190, and 200 to do this.
The EXT$ function is available for just this purpose. EXT$(A$,1,J) extracts the
group of characters beginning with I and ending with J for string A$. Some
computers use SEG$ for this. Using EXT$, program CHANGE becomes
CHANGF.
CHAN Gr
98 REM * DEM0NSTRATE CHANGE STATEMENT
100 DIM AC30>
110 PRINT "STRING"J
120 INPUT A$
130 CHANGE A$ T0 A
140 PRINT LENCA$>J "CHARACTERS IN "'J A$J " " '
150 PRINT
170 PRINT "CHAR ASCII C0DE"
180 r0R I = I T0 ACO>
210 PRINT "'"J EXT$(A$,I,I>J "J ACI>
220 NEXT I
230 END
RUN
STRING? #I&+: J
6 CHARACTERS IN '#!&+:]'
.,.
CHAR ASCII C0DE
35
'!' 33
'&' 38
•+• 43
•:• 58
• J' 93
108 Basic BASIC
We may form strings from the characters of strings in some rearranged se-
quence. \~le might print a string backwards or with the characters in alphabetic
order. In order to arrange the characters of a string in alphabetic order, we can
simply provide a one-dimensional array with the corresponding ASCII code
numerics in increasing order. Program ORDER$ does exactly this.
0RDER$
'?AAGEFFGHHHIIIIKMNNN00STTTTW
SUMMARY
Whenever subscripted array string variables may be used, A$(I,J) specifies
the string stored in row I, column J. We may use CHANGE A$ T0 A to con-
vert the characters in the string variable A$ to the equivalent ASCII code
numerics in corresponding positions of the A array. We may also reverse this
process by using CHANGE A T0 A$. We also find the number of characters
in A$ stored in A(O). Alternatively, we may use the LEN function. We may
extract a group of characters with the EXT$(A$,I,J) function. This may be im-
plemented as SEC$.
We may assign, PRINT, INPUT, and READ string variables in much the
same way that we handle these operations with numeric variables. Strings may
be placed in DATA statements, and string arrays must be DIMensioned if a
subscript is to exceed 10.
s M T '(I T F s
u 0 u E H R A
N N E D u I T
D D s N R D u
A A D E s A R
y y A s D y D
y D A A
A y y
y
s M T w T F s
u 0 u E i-l R A
N N E D u I T
D D s N R D u
A A D E s A I<
y y A s D y D
y D A A
A y '{
'{
work without the need for printing at the terminal, although it is good pro-
gramming practice to prnvi<le some printe<l output to help keep track of what
the computer has done. After we have seen several examples, we will gain
confidence that the computer is really performing the expected operations.
The fundamental concept is that we may write or print data into a file and
that we may retrieve that data under program control. Several versions of pro-
gram statements are used to achieve these purposes. The next two sections
explain the use of files as defined by two different systems. We have chosen
Hewlett-Packard and General Electric versions of BASIC for this.
This is the very first program we have run which does something useful
without any printed output. (As a general rule, however, it is good practice to
have programs produce some meaningful printed output at the terminal.) State-
ments 100 and 120 introduce the first two file handling statements. Statement
100 is called the FILES statement. It is the statement which makes the file
whose name is TEST available to the program. Statement 120 instructs the
computer to print data into the film instead of onto the paper in the terminal.
In that statement, the # 1 specifies the first file named in the files statement. We
may be able to name eight or more files, separated by commas. In addition,
some computers allow us to replace any file originally named during program
execution by using the ASSIGN statement. In the PRINT # statement, every-
thing past the semicolon is printed into the file. We may list several data items
here, and strings and numerics may be intermixed.
In order to allocate the file space in the first plac~, we used the OPEN com-
mand. OPEN-TEST, 1.5 designates a file space, called TEST, that contains
15 segments called records or sectors. Typically, a sector is large enough to
store 32 numbers, or about 128 alphameric characters. More recent Hewlett-
J:'ackard computers aiiow the option of specifying record size through the
CREATE command. On such a machine, CREATE TEST, 15,106 provides 15
records, each allowing up to 53 numerics, or about 212 alphameric characters.
(106, 212, and 319 are storage efficient numbers to use in the CEATE command.)
In counting space for strings, we must add two to the number of characters
Strings and Files 111
for each string an<l add one if there is an odd number of characters. Thus three
characters require the same storage space as four.
Now let's examine a program to rea<l the contents of our file TEST.
READ
90 REM *
PROGRAM TO READ NUMBERS FROM A FILE
JOO flLES TEST
110 READ lllJY
120 PRINT Y;
130 GOTO 110
140 END
RUN
READ
3 17 11 31
END-OF-FILE/END 0F RECORD IN LINE 110
READO I
90 REM *
PROGRAM READ WITH IF END 'TRAP'
100 FILES TEST
105 IF END Ill THEN 140
11 0 READ 111 J Y
120 PRINT Y;
130 GOTl'l l 10
140 END
RUN
READO!
3 17 II 31
150 READ X
160 PRIN1 lllJX
170 PRIN1 XJ
180 NEXT I
190 DA1A 19,2,6
200 END
RUN
PRINT I
19 2 6
Note that PRINT I will also print numbers into an empty file. Consequently,
we can eliminate the need for program PRINT. Now we run program READO!
to verify for us that the file now contains numbers printed into it in separate
runs of two programs.
RU'J
READO I
3 17 11 31 19 2 6
When we used file TEST above, we simply printed numbers one after an-
other into the file without any concern for exactly where in the file those num-
bers were placed. Used in this way, file TEST is called a serial file. However,
we could have directed the computer to print each of those numbers on a
different record of the file. We need the following expanded file PRINT state-
ment for this purpose:
999 PRINT #l,R;X
This statement allows us to specify that the data following the semicolon is to
be printed in the Rth record of file #1. See line 130 of program PRINT2.
PRINT2
90 REM *
PRINT T0 REC0RD R IN A FILE
100 FILES TEST
110 F0R R= I T0 4
120 READ X
130 PRINT #t,R;X
140 PRINT X;
150 NEXT R
160 DA TA 3, 17 • I 1, 31
170 END
RUN
PRINT2
3 17 II 31
Now to read the Rth value we needn't read through all R items. We may
read it directly with the statement,
999 READ #1,R;X
Since this structure allows us to select at random any starting point in the file,
we refer to the file as a random access file. See program READ02.
Strings and Files 113
READ02
90 REM *
DEMONSTRATE RANDOM ACCESS
100 FILES TEST
110 PRINT "ITEM II";
120 INPUT R
130 IF R=O THEN 170
140 READ 111, R; X
150 PRINT X
160 G!H0 110
170 END
RUN
READ02
ITEM #?4
31
ITEM II? 1
3
ITEM #?0
One of the uses of data files is to rearrange data and store it in rearranged
form. For example, let's enter the names of seven people along with their dates
of birth and death in file TEST, one person to a record, and arrange them in
alphabetical order using the technique of program ARANGE in Sec. 3-4.
Program ENTERA reads the data from DAT A statements and prints it in the
first seven records of the file.
ENTERA
90 REM *
FILE PRINT 0NE T0 A RECORD
100 DIM N$(72J
110 FILES TEST
120 F0R I=l T0 7
130 READ N$,A,8
140 PRINT #l,I;N$,A,8
150 NEXT I
160 DATA "J0NES, J0HN PAUL", 1747, 1792
170 DATA "ANTH0NY, SUSAN 8. ", 182011906
180 DATA "WASHINGT0N, 800KER T.",185911915
190 DATA "BELL, ALEXANDER GRAHAM''. 1847• 1922
200 DATA "EDISON, TH0MAS ALVA", 184711931
210 DATA "F0RD, HENRY", 1863, 1947
220 DATA "8L00MER, AMELIA JENKS", 181811894
230 END
RUN
ENTERA
Program READA reads from file TEST and prints at the terminal.
READA
90 REM *
READ NAMES FR0M A FILE
100 DIM N$C72J
110 FILES TEST
120 PRINT " DOB NAME"
130 FOR I=l Hl 7
140 READ #l,IJN$,A,8
150 PRINT AJN$
160 NEXT I
170 END
RUN
READA
114 Basic BASIC
D0B NAME
1747 J0NES, ,HlHN PAUL
1620 ANTH0NY, SUSAN B,
1659 WASHINGTON, B00KER T·
1647 BELL., ALEXANDER GRAH1eM
1647 EDIS0N, ~H0MAS ALVA
1663 FORD, HENRY
1616 BL00MER, AMELIA JENKS
Program ORDERA arranges the data in the file alphabetically. Note that
line 190 is required so that when the comparison for order is made in line 160
after an exchange has taken place, A$ stores the appropriate string. This is
necessary because data is stored in two places-in the file and in the variables
of the program. It is the programmer's job to keep these two storage areas
coordinated.
0RDERA
FILE ALPHABETIZED
And once again we run READA to see that the data is properly arranged in
the file.
RUN
REA DA
D0B NAME
1620 ANTH0NY, SUSAN l:J,
1647 BELL, ALEXANDER GRAHAM
1616 BL00MER, AMELIA ,JENKS
1647 EDIS0N• TH0MAS ALVA
1663 FllJRD, HENRY
1747 J0NES, JllJHN PAUL
1659 WASHINGT0N• B00KER T.
SUMMARY
The FILES statement is used to make files accessible to a program. We may
be able to replace the files named during execution using the ASSIGN state-
ment. We may print data into a file using PRINT #N;A,B,C$ to print in the
next available space serially. Or we may use PRINT #N,R;A,B,C$ to specify
Strings and Files 115
ASCII Files
ASCII files behave in many ways just like the DAT A statements of a pro-
gram. The data must be read sequentially, beginning with the first data item
in the file. There is no way to access data at random points. The file may be
filled from the keyboard exactly as DAT A statements of a program are typed,
but omitting the word DAT A. The file may be listed at the keyboard with the
LISt command, just as programs may be listed. Lines may be corrected in a
file by retyping them. Lines may be removed by typing the line number fol-
lowed by a carriage return. In order to make a file available for future use, it
must be SAVed, just as a program must.
Perhaps the best way to learn about files is to study an example. Let's type
an ASCII file containing test score data for a class. Suppose we consider a class
of only five people and enter their test scores on six tests.
File SC0RE has been typed at the keyboard and SAVed as described earlier.
We list the file here:
LI ST
SC0RE
Now, to gain some file handling experience, let's make our first program
merely print the contents of the file under program control rather than use the
LIST command. This approach makes it possible to print labels and arrange
the data in an easy-to-read form. See program READTEST.
READ TEST
AL THEA LARGE
.
C:UGMn Hi~Gi...Ei·i,:u-,; ,
91
88
00
92
'u
90
',
99
UV
92
'u
90
GERTRUDE SMITH 7 l 86 tl7 90 88 92
gram. The file must exist to execute the program. The statement 130 READ
#l, N$ is like a DATA READ statement except that the "#1" appears to notify
the computer to read from the first file named in the FILES statement. We may
name up to eight files there by separating them with semicolons. Statement 160
is another file read statement. A statement 999 READ #N, A,B,X$,T would
read three numerics and one string from the Nth-named file in the FILES state-
ment. The statement 230 IF M0RE #1 THEN 130 has the ability to "look
ahead" in the file to "see" if there is more data in the file. If there is more data,
the computer is transfered to line 130; if not, then control passes to the next line.
Now that we are able to read the file, let's perform the necessary operations
to find each student average and the class average. We will require two vari-
ables to store running totals. In program AVERAGE, T2 is the running total
for the class, and Tl is the student running total.
AVERAGE
Now that we know how to read an ASCII file under program control, let's
see how to write data into such a file under program control. Suppose that we
consolidate the data in file SC0RE, retaining just the names and averages to
write into a new file, SC0REL To do this, we begin by naming both files in
the FILES statement. We may enter data into an ASCII file with the WRITE
#N statement. However, before writing into the file, it must be prepared for
writing with the SCRATCH #N statement. SCRATCH #N sets a pointer to
the beginning of the Nth-named file and prepares it for writing. In program
WRITEA VG, we print each name at the terminal just to show the progress
of execution during the program r'un. For large amounts of data, we might
simply print the number of names moved. See lines llO and 140 of program
WRITEAVG.
118 Basic BASIC
WRITEAVG
Additional files statements include APPEND #N, which sets a pointer to the
end of data in a file and prepares the file for the write mode in a way similar
to that of the SCRATCH #N statement, and RESTORE #N, which sets a
pointer to the beginning of the file and prepares it for the read mode so that
we may read the data in a file more than once in a single execution of a program.
Binary Files
Binary files may be used only under program control. They may be either
sequential or random access. Sequential binary files are treated for programming
purposes exactly like ASCII files except that where pound signs (#) appear for
an ASCII file, a colon (:) is used for a binary file.
RllD
Notice that we are able to fill the file without regard to position in the file
because we are exactly filling each record as we go. This is not always the case.
To select locations at random within the file, we need the SETW statement.
SETW N T0 X places a pointer in file N to the Xth word in the file without
regard to file dimensions. Thus, in our file of six words per record, the ninth
word is the third word on the second record. To think in terms of records and
words within a record, we need a formula to determine the value of X. For the
Cth word in record R where there are W words per record, the value of X is
Wo(R-1) + C. Now let's write a short program to find selected positions in file
NUMB. See program PICK. Notice that the REST0RE statement is not re-
quired for random access files. REST0RE:N is equivalent to SETW N T0 1.
PICK
HOW, COL? 3, 6
FOUND O. 647165
HOW,COL? O,O
For our final example we will use a binary file to arrange the student data
from our ASCII file SC0RE1 in order of increasing test average. We must
write the necessary data into a binary file, arrange it, and then print the results.
This can be done with three different programs or with a single program. We
will use a single program here. See 0RDERA UG.
To determine the size records required, we must know the number of char-
acters in the name strings. vVe find a maximum of 14 characters . \Ve should
go to at least 16 since that is the next multiple of four. In practice, to make
such a file generally useful, we would probably go even higher. Allowing for
16 characters , we need four words for storage of string data, plus one word
for control, plus one word for the numeric. For this problem, a file with five
records containin g six words per record is sufficient. We get that with CRE
SC0RE2, (RAN(6,5 )).
0RDERAVG
SUMMARY
The FILES statement is used to make files accessible to a program. The files
of this section are of two types: ASCII and BINARY. ASCII files are sequential
and may be accessed from the keyboard or through a program. Binary files
may be either sequential or random access and may be accessed only through
a program. We may use READ #N, WRITE #N, SCRATCH #N, APPEND #N
or RESTORE #N to handle data in an ASCII file. For sequential binary files,
all of the above statement types may be used by replacing the pound signs (#)
with colons (:). For random access files, we have the additional statement
SETW N T0 X which sets a pointer at the Xth word of a file in preparation
for the next READ or WRITE statement. A file is made random access in the
CREate command.
PROBLEMS FOR SEC. 7-6
1) Type a few inventory items with quantity and price data into an ASCII
file. Write a program to print the value of each item and the total value of
inventory at the terminal.
2) Write a program to print a list of an unknown number of names in an
ASCII file at the terminal in alphabetic order. Use RESTORE #N and repeated
reading of the file for this purpose. Assume that there are no duplicates.
3) Consider a random access file containing five words per record and six
records filled with numbers. Write a program to find the largest number in each
record and the largest number in each "column."
4) You are presented with a random access file with a set of ten names in it;
each name was entered first name first, followed by a space, followed by last
name. Since this ordering is difficult to alphabetize, you are to replace each
entry in the file rearranged so that the last name is first, followed by a comma,
a space, and the first name. You know that each string is to be allocated eight
words of storage.
5) (Pro;ect) Print some names into a random access file. Place a list of point-
ers to those names in an ASCII file so that if the pointers are read sequentially
from the ASCII file and used to access the names in the random access file with
the SETW statement, the names will be accessed in alphabetic order. Use the
ASCII file to print the names in alphabetic order.
CHAPTER 8
8-1 ZEROS
Often in mathematics we would like to find the zeros of a quadratic func-
tion. For some sets of coefficients, we may factor the expression on the right in
Eq. (8-1) and set each factor equal to 0. This would be the method to use
for f(X) =
X 2 + 3X + 2. We would find zeros as follows:
x2+ 3X+ 2= 0
Factoring,
(X + l)(X + 2) = 0
and (X + 1) = 0 or (X + 2) = 0
So X=-1 or X=-2
and the truth set is {-2, -1 }.
However, in general for nonfactorable as well as factorable quadratic ex-
pressions on the right in Eq. (8-1), we may use the quadratic formula, which
may be derived by the method of completing the square. The zeros of
=
f(X) AX 2 + BX + C are
XI = -B + v'B~ - 4.AC
2A
X
2
= -B - yB~ - 4AC
2A
122
The Quadratic Function 123
Since we are going to insert these equations into a program we will write
ZZZ LET Xl = (-B + SQR(B 00
2- 4 °A 0
C))/(2 ° A)
and
ZZZ + 10 LET X2 = (-B - SQR(B 00
2- 4 ° A ° C))/(2 ° A)
Now all we need is some data and some printing instructions (see QUADl),
which seems to work well enough. You will want to modify QUADl to
account for nonreal zeros. You may want to just print a message or you may go
ahead and compute the nonreal values. As the program stands though, if
B 0 0 2 - 4 ° A ° C is negative, the computer will at best print an error
message and at worst it will terminate the RUN.
QUAD!
A 8 c XI X2
I 3 2 - I -2
I 2 -3 I -3
2 4 -6 I -3
6 13 6 -.666667 - 1. 5
5 -7 2 I •4
D0NE
f(X)=A[x 2 + ! x+ 4 n;2 -
4 n;
2 J+c
- ~ 4AC-B2)
( 2A ' 4A
=
You should know, too, that the line whose equation is X -B/2A is called the
axis of symmetry. We should now be able to write a program to print three
items of information: 1) the maximum or minimum status of the parabola,
2) the equation of the axis of symmetry, and 3) the coordinates of the turning
point. Let us collect things into a flowchart (see Fig. 8-1), and write program
QUAD2.
END
LETM PRINT
= SGN(A) minimum
LETX =-Bl
(2•A)
QUAD2
10 READ A.B.c
15 IF B=-·001 THEN 9999
20 PRINT AlBJC
25 IF A <> 0 THEN 30
26 PRINT "A=O THE EXPRESSI0N IS ~0T QUADRATIC"
27 G0T0 20
28 REM DETERMINE MAX. 0R MIN.
30 LET M=SGNCA>
40 IF M= 1 THEN 70
50 PRINT "MAXIMUM PARAB0LA"
60 GOT0 80
70 PRINT "MINIMUM PARAB0LA"
78 REM FIND THE AXIS 0F SYMMETRY
80 LET X=-B/C2*A>
90 PRINT "AXIS 0F SYMMETRY IS X ="IX
98 REM FIND THE EXTREME VALUE
100 LET Y=C4*A*C-Bt2l/C4•A>
110 PRINT "THE TURNING P0INT IS C"lXl".''lYJ"}"
115 PRINT
120 G0T0 10
150 DATA 1,3,2, 1.2.-3,6, 13•6
I 55 DATA -3,5,11
160 DATA 4,-.001, 1
9999 END
RUN
QUAD2
I 3 2
MINIMUM PARAB0LA
AXIS 0F SYMMETRY IS X =-1·5
THE TURNING P0INT IS c-1.5 .-.25
I 2 -3
MINIMUM PARABOLA
AXIS 0F SYMMETRY IS X =-1
THE TURNING P0INT IS C-1 ,-4
6 13 6
MINIMUM PARAB0LA
AXIS 0F SYMMETRY IS X =-1.08333
THE TURNING P0INT IS C-1.08333 ·-1.04167
- 3 5 11
MAXIMUM PARABOLA
AXIS 0F SYMMETRY IS X = .333333
THE TURNING P0INT IS C .333333 • 13.0833
D0NE
=
will be required. We will first graph Y X 00 2. We will put" ";in a loop to
get the printing head to the point that we want plotted. Any printed character
may be used to represent the plotted points. The range you select will depend
on the width of the carriage on your terminal. Selecting the domain for X as
-7 to +7 we will require a range of 0 to 49.
When X = -7, we want the printing head to step out 48 spaces, then print
a character, and then RETURN. Now we want X to go to -6 and the printing
head will have to step out only 35 spaces, print a character, and RETURN. As
this process is repeated, it too will be put in a loop with X going from - 7 to
+7 incrementing by 1. It will be convenient to define a function here, not as
a saving now, but to fit in with later plotting problems. Before writing the
program PL0Tl, let us draw a flowchart (see Fig. 8-2). Notice that we intend
printing the spaces followed by a semicolon and the plotted points also fol-
lowed by a semicolon. After the point has been plotted, we do not want the
printing head to step the rest of the way across the carriage as that would be
a waste of computer time for this particular plot. So line 62 is used to re~urn
the printing head to the left margin. We should observe that the procedure
we are developing is not especially efficient in the first place, and so should
be used sparingly.
Start
DEF FNQ(X)
= x •• 2
F(t)RX Fq)RY
=-7T(1')7 = OT(t) 70
PL0Tl
30 DEF FNQCX>=X•2
60 F0R X=-7 T0 7
62 PRINT
88 REM LINE 90 HAS THE EFFECT 0F NUMBERING THE SPACES
89 REM ACR0SS THE PAGE 0 T0 70
90 F0R Y=O T0 70
120 IF Y=FNQCX> THEN 210
148 REM IF Y D0ES N0T EQUAL FNQCX> THEN PRINT A BLANK SPACE
1 so PRINT " "J
180 NEXT Y
210 PRINT "*"J
212 REM PL0T THE P0INT AND G0 T0 NEXT X
240 NEXT X
270 END
RUN
PL0Tl
*
*
*
*
*
*
*
*
*
*
*
*
*
*
D0NE *
PL0Tl was not bad for our first try. If we are going to plot other para-
bolas, we will have to make a provision for values of Y less than 0. So we
may change line 90 to read 90 F0R Y =-
M T0 70 - M, where M is the
number of spaces to the left of 0, and then we can put M on INPUT:
90 F0R Y= -M T0 70-M
S INPUT M
4 PRINT "INPUT THE NUMBER 0F SPACES DESIRED T0 THE LEFT 0F ZER0"J
88
89
6 PRINT
RUN
PL0T2
RUN
PL0T3
*
*
*
*
*
*
0
*
*
*
*
*
*
*
*
DONE.
94 IF X=O THEN 98
95 PRINT "t ";
96 Gorn 100
RUN
PLOT4
The Quadratic Function 129
*
*
*
*
t *
*
'*
0
'*
*
*
*
*
*
D0NE *
Finally, we may put in a Y-axis. Let us settle for having the Y-axis along
the leading side of the graph. By putting the Y-axis there, we will be able to
print the scale without interfering with the graph itself. For the particular
graph we have been plotting a range from 0 to 50 is reasonable.
0 10 20 30 40 50
---~-----t---------1---------t---------t---------t---------t---------r
*
*
*
*
*
T * *
0
T *
*
*
* ,,
*
DONE
*
130 Basic BASIC
At this point, the program is scattered all over the place and some of the
line numbers are very close together. So we renumber beginning with line 10
and print the entire program in PL0T5.
PLCH5
There are still several considerations regarding this program for plotting.
For instance, as the program is written, it will not plot the X-axis if the Y
value is less than 0. The scale is fixed. There is provision for only one function
to be plotted. Also, consider what happens if the value of Y is not an integer.
All of these comments suggest areas in which the program could be improved.
Let us insert a different function and call for one last RUN of PL0T5.
Trigonometry
132
Trigonometry 133
Fig. 9-1
Taking the graph of Fig. 9-1 an<l <lropping the perpen<licular from (X, Y)
in the first quadrant to the X-axis we get Fig. 9-2. We have forme<l a right
triangle in which the length of the hypotenuse is R, the length of the base is
X, an<l the length of the altitu<le is Y. Redrawing the triangle without the
coordinate system, we get triangle ABC with the trigonometric ratios as in
Fig. 9-3.
134 Basic BASIC
TRI Gt
y
(X,Y)
R y
fig. 9-2
c
a
B Fig. 9-4
We also know from geometry that LA and LB are complements, i.e., their
sum is 90 degrees or 1T/2 radians.
Let us solve a problem: George has a 36-ft ladder which he is going to use
to paint his father's house. He believes that the angle formed by the ladder
and the side of the house should be not less than 14 degrees and not more
than 15 degrees. He needs to know how far out from the house to place the
foot of the ladder. See Fig. 9-4.
We may use either SIN(G) = B/L or C0S(90 -· G) = B/L. Let us choose
the sin function and solve for B:
B= L 0
SIN(G)
We will have to convert degrees to radians. This is the purpose of line 10 in
program LADER.
LADEil
10 LET C=3.14159/180
20 LET L=36
30 PRINT "36' LADDER BASE MUST BE OUT FROM THE H01JSE IN FEET"
40 PRINT "N0T LESS THAN''. "NCJT MORE THAN"
50 PRINT L•SIN<C•l4>•L•SIN<C•IS>
60 END
RUN
LADER
DONE
We really do not need more than hundredths, so let us round off. Also,
since George may want to change the length of the ladder to reach different
heights, let the ladder length go from 36 ft to 20 ft. See LADERI.
136 Basic BASIC
LADEk I
10 LET C=3·14159/160
30 PRINT "LADDER BASE MUST BE 0UT FR0M THE HO\JSE IN FEET"
40 PRINT "NOT LESS THAN", "N0 T MORE THAN", "LADDER LENGTH"
45 F0R L=36 TO 20 STEP -2
50 PRINT INTCL•SIN<C•l4l*I00+.5l/IOO ,INTCL•SIN<C•l5l*I00+.5l/IOO ,
51 PRINT L
55 NEXT L
60 END
RUN
LADER I
90NE
ATN(X)
Suppose we know the lengths of the sides of a right triangle and we need
to know the angles. If we are using printed tables in a book, we may look up
the angle whose sin, cos, or tan is known. Not so with the computer. An addi-
tional computer function is required for this. ATN(X) is the function usually
available, though some systems will provide others as well. ATN(X) computes
the angle whose tangent is X. If
TAN(G)=X
then
ATN(X)=G
where ATN stands for arctangent and G is in radians.
8
Trigonometry 137
Suppose we lean a 36-ft ladder against a building with the base 8 ft out
and we would like to know the angle formed by the ground and the ladder,
as in Fig. 9-5. We can say
TAN(G) =H/8
which means
ATN(H/8) = G
H = SQR(36 ° 0 2 - 8 ° 0
2)
G = ATN(SQR(36 ° 0
2- 8 ° 0
2)/8)
and the angle in degrees may be found by
0
LET G = G 180/17'
See LADER2.
LADER2
10 LET G=ATNCSQRC~6•2-8t2)/8)
15 PRINT GJ"RADIANS"
20 LET G=G*l80/3.14159
30 PRINT GJ "DEGREES"
40 END
RUN
LADER2
1. 346 7 RADIANS
77. 1605 DEGREES
DONE
SUMMARY
We now may apply the computer to the trigonometry of the right triangle
using SIN (X), C0S (X), and TAN (X) to find sides when angles are known and
using ATN(X) when we wish to compute angles. We must always be aware of
the need to use radians for the argument of the computer trigonometric
functions.
7) A right triangle has one angle 42°25' and the side opposite that angle has a
length of 10.0". Find the other sides of the triangle.
8) Standing 1000 ft from the base of a lighthouse on level ground, the angle of
elevation is 7°30'. Find the height of the lighthouse.
Fig. 9-6
Similarly we get
b sin Cl
c= (9-6)
sin Bl
And finally, the area may be found by
Area = Jfab sin Cl (9-7)
All we have to do is put all that into a program. We may do that almost
directly from Eqs. (9-4)-(9-7). These four equations appear in order in lines
60, 70, 80, and 90 of program LAWSIN.
In writing the program, we have done only slightly more work than we
would do preparing to do the calculation by hand. However, we are letting
the computer take the drudgery out of the actual calculation. We also have the
program available to do large numbers of calculations at a later date with
virtually no additional effort required. However, we continue to be totally
responsible for the mathematics required.
If we reflect for a moment upon the congruence of triangles, the various
congruence conditions come to mind. They are side-angle-side, angle-side-angle,
side-side-side, and angle-angle-corresponding side. There are special cases for
right triangles. We should see then, that if any of these four sets of measures
is known, we should be able to find the remaining three parts uniquely. And
so we can. We have just used LA WSIN for two angles and a nonincluded
side. We should be able to handle two angles and the included side with only
slight modifications of LAWSIN. However, you should see that we cannot
handle side-side-side or side-angle-side with the Law of Sines. For these we
need the Law of Cosines.
140 Basic BASIC
LA\o,•SIN
A B c
THE ANGLES ARE 24 51 105
THE SI DE.S ARE I0 19. 11 23. 75
AND THE AREA IS 92. 20
A B c
THE ANGLES ARE 30 60 90
THE SIDES ARE 15 25.98 30
AND THE AREA IS 19 4, 86
A B c
THE ANGLES ARE 45 45 90
THE SIDES ARE 20 20 2s.2s
AND THE AREA IS 200
DONE
Law of Cosines
For any triangle AIBlCl we may place a vertex at the origin of a rectangu-
lar coordinate system and designate the vertices as shown in Fig. 9-7.
TT • - . ,1 Tio ol ol , n 1
v::,111g, u1e .c yu1ag,uu:~a11 u1eu1eu1, we ruay CUIUJ?Ule: a- uy
c a
A1 b C1
(0, 0) (b, 0)
Fig. 9-7
Rearranging terms
a2 = b 2 + c2(cos 2 Al + sin 2 Al) - 2bc cos Al
Since cos 2 Al + sin 2 Al = 1, we finally get
a2 = b 2 + c2 - 2bc cos Al (9-8)
Equation (9-8) is the statement of the Law of Cosines solved for vertex Al at
the origin. Placing Bl at the origin we would get
b2 = a2 + c2 - 2ac cos Bl (9-9)
and placing Cl at the origin would produce
cz = a2 + b2 - 2ab cos Cl (9-10)
In the form of Eqs. (9-8)-(9-10) the Law of Cosines is appropriate for
handling problems in which two sides and the included angle are given. Once
you obtain the third side by taking the square root of the right side of the
equation, you may use the Law of Sines to obtain a second angle, or you may
proceed as for the side-side-side congruence.
If we solve Eq. (9-8) for cos Al, we get
b2 + c2 - a2
(9-11)
cos Al= 2bc
So, if we are faced with a side-side-side congruence, we may easily find the
value of cos Al. Now our only problem is to get the value of Al from the value
of cos Al. This will require the ATN (X) function. We should know that
sin Al
tanAl=-Al
cos
and for Al between 0 and 180 degrees, sin Al is always positive. Thus,
sin Al = yl - cos 2 Al (9-12)
142 Basic BASIC
Thus
2
tan Al = yl - cos Al
cos Al
And so,
Al = ATN ( yl - cos2 Al ) (9-13)
cos Al
Now, we will be able to translate Eqs. (9-11)-(9-13) into BASIC program
statements. From Eq. (9-11) we get
LETT= (B 00
2 +C 00
2- A 00
2)/(2 ° B ° C)
and from Eq. (9-12) we get
00
LET Tl= SQR(l T 2)
and finally from Eq. (9-13),
LET Al = ATN(Tl/T)
These three statements constitute the heart of our program LAWC0S which
reads three sides from data and prints all six parts of the triangle. See espe-
cially lines 50, 60, and 70.
We could have done the work of lines 90 through 120 by shuffling the data
around and using lines 50 through 80 as a subroutine.
SUMMARY
This section has been devoted to solving triangles which may be uniquely
determined. We have developed the Law of Sines into a program to solve the
case of two angles and a nonincluded side and indicated that, with a few
changes, the angle-side-angle case is solvable by the Law of Sines also.
The Law of Cosines has been used to find the angles of a triangle whose
sides are known. It has been indicated that the case of side-angle-side is appro-
priate for the Law of Cosines also. This covers the uniquely determined cases
except hypotenuse-leg. There remains the ambiguous case. If two sides and a
nonincluded angle are given, there may be two, one, or no triangles possible.
If solvable, such triangles are solvable by the Law of Sines. This is left to the
student in the exercises.
A B c
THE SIDES ARE 3 4 s
THE ANG.ES ARE 36.87 53. 13 90
THE SIDES ARE 300 400 soo
THE ANG.ES ARE 36.87 53. 13 90
THE SIDES ARE 1.73205 I 2
THE ANGLES ARE 60 30 90
THE SIDES ARE 2 2 3
THE ANG.ES ARE 41. 41 41. 41 97. 18
D0NE
and containing point (X, Y). If we call the distance R and the measure of the
angle G, we may designate a new ordered pair (R, G). Refer to Fig. 9-1.
Ordered pairs of this kind are called polar coordinates. The ray consisting of
the origin and the positive portion of the X-axis is called the polar axis and
the origin is called the pole. Our new coordinate system appears in Fig. 9-8.
Such a coordinate system is particularly adapted to plotting periodic functions
k(R,GI
with finite upper and lower bounds.
Fig. 9-8
..
POLE POLAR AXIS
which is equivalent to
x2 + Y2-2X = 0
which turns out to be a circle with radius 1 and center at the point (I, 0).
However, other polar equations are not so easily identifiable when converted
and so are more appropriate to plot on a polar coordinate system. Consider,
R=l-2cosG (9-14)
R= 2 + sin2G (9-15)
R= l + 2 cos G - 3 sin~ G (9-16)
No matter how you approach plotting any of these, you run into a tremendous
amount of calculating.
Trigonometry 145
We can easily get the coordinates of the points to plot for all three of these
in the same computer program.
In program P0LAR we have simply defined a function for each of the Eqs.
(9-14), (9-15), and (9-16), an<l put the value of the angle G in a loop to get
values every 15 degrees. We are not obligated to define functions, but with
converting to radians and rnunding off to hundredths this seems a reasonable
approach. Now if we want different functions we only need change the
printing in line IO and redefine the new functions in lines 30, 40, and 50. Of
course the actual plotting is left to the student to do on polar coordinate paper.
P0LAR
5 LET K=3.14159/180
10 PnINT "ANa.E","1-2CO SC G>"• "2+ SINC2G>"• "1+2C0SC Gl-3SINC Gl •2"
20 DEF FNRCXl=INTCX *lOQ+.5l/100
30 DEF FNACXl=1-2*C0S CK*Xl
40 DEF FNBCXl=2+SINC2 *K*Xl
50 DEF FNCCXl=1+2*C0 SCK*Xl-3*SINCK *Xlt2
60 F0R G=O T0 360 STEP 15
70 PRINT G, FNR ( FNA CG>» FN RC FN B <Gl l . FN R <FNC <Gl l
80 NEXT G
90 END
RUN
P0LAR
DONE
SUMMARY
The computer is an invaluable aid to obtaining values of ordered pairs of
polar comdinates for polar equations.
146 Basic BASIC
Complex Numbers
10-1 FUNDAMENTAL OPERATIONS
In the development of mathematics we find that we cannot solve the
equation
x2 + i = o
if we are limited to real numbers. We want to say
X=y-1 or X= -y-1
However, such numbers are not allowed in the real number system. So we
define a new number i such that
i2 = -1 or i = y-1
Then, if we should try to solve X:? + 2X + 2 = 0 using the quadratic formula,
we get
x= -2 ± v-:r=lf or X= -2± v=-4
2 2
and we decide to call y-4 the same number as iy'4 which is 2i. So now
-2 ± 2i
X= 2
or X = -1 + i
= -1-i
These two numbers are representative of complex numbers in rectangular form.
In general, rectangular form is written as a + bi, where a and b are real mun-
bers. Another number could be written c + di. Of course, the computer cannot
handle a + bi because it is limited to real numbers. But we can deal with the
two real numbers a and b. This means that we will be working with complex
numbers in ordered pair form or (a, b) form. Since the computer terminal is
limited to capital letters, we use (A, B).
147
148 Basic BASIC
(A, B) = (C, D)
if and only if
ADOA.8
FIRST NUMBER?J,4
SEC0ND NUMBER?O,Q
THE SUM IS < I , 4
FIRST NUMBER?l.5
SEC0ND NUMBER?316
THE SUM IS < 4 , I3
FIRST NUMBER?-4671902
SEC0ND NUMBER?56,-1234
THE SUM IS C-411 ,-332
FIRST NUMBER?999,I
This is a little more complicated than the othe1 ope1ations, but still manage-
able.
SUMMARY
0 (A,B)
Fig. 10-1
(A, B). We use R for the distance and call it the absolute value of the complex
number (A, B). R is found from (A, B) by
R = I(A, B) I = v'A2 + B2
We use C for the angle. C may be found from (A, B) by
C = arctan ( A/B)
C is sometimes called the argument of the complex number. We may now
refer to complex numbers in polar form as ( R, C). This form for complex
numbers is the same as the form for (X, Y) ordered pairs converted to polar
form in Chap. 9, with the one exception that we prefer not to allow R to be
negative for complex numbers.
So we see that for every complex number we may choose a rectangular
form or polar form depending on which form is appropriate to the problem at
hand. We saw in Sec. 10-1 that addition and subtraction worked out easily
in (A, B) form, but that multiplication and division were more cumbersome.
Let us look at multiplication in (R, G) form.
It turns out that a third form will be helpful in establishing the multiplica-
tion algorithm. From (R, G) we get that A = R cos G and B = R sin G, and
similarly for (Rl, Cl) we get that C =Rl cos Cl and D = RI sin CL Using
the old formula to multiply (A, B) by (C, D) we get
(R cos G, R sin C) (Rl cos GI, Rl sin Cl)
=
(RRl cos G cos Cl - RRl sin C sin Cl,
RRl sin G cos Cl+ RRl cos G sin Cl)
After factoring, the right side becomes
(RRl(cos C cos Cl sin G sin Cl), RRl(sin G cos Cl+ cos G sin Cl))
(10-5)
ML TR, G
10 DEF' F'NCCZ>=INTCZ•RNDCZ>+l>
20 F'0R X= 1 T0 6
30 LET R=F'NCC 1O>
40 LET G=F'NCC 360>
50 LET Rl=F'NC< 10>
60 LET Gl=F'NCC360>
70 PRINT "C"JRJ","JGJ .. >*<"JRlJ","JGlJ">=C"JR*RlJ".1"JG+GlJ")"
80 NEXT X
90 END
RUN
MLTR, G
D0NE
SUMMARY
Complex numbers may be represented in polar form as (R, C), where R is
the absolute value and C is the angular location starting at the positive end of
the A-axis on an (A, B) graph. We have seen that while addition and subtrac-
tion are easily done in (A, B) form, multiplication and division are better
suited to (R, C) form. (R, C)(Rl, Cl) = (RRl, C + Cl) and (R, C)/(Rl, Cl) =
(R/Rl, C Cl). To multiply in polar form, multiply absolute values and add
arguments. To divide in polar form, divide absolute values and subtract
arguments.
152 Basic BASIC
R00TS
..
( ). 0 )
). 120
1. 240
Polynomials
Multiplication
Clearly we will perform operations on the computer by working with the
coefficients and being careful to line things up properly. This means being very
much aware of missing terms and inserting zero coefficients where necessary.
Let us begin with an example, say (2X + 7)(3X:! + llX - 5). By hand we get
154
Polynomials 155
3x2 + llX - 5
2X+ 7
21x2 + 77X - 35
6X3 + 22X 2 - lOX
6X3 + 43x2 + 67X - 35
where all the xx were known in advance and <lo not depend on the coefficients.
So the problem could have been done in the following manner:
3+11- 5
2+ 7
21+77 - 35
6+22-10
6 + 43 + 67 - 35 (11-1)
The program can be set up by putting 3, 11, and -5 in one computer list,
2 and 7 in another, and making provision for putting 6, 43, 67, and -35 in a
third list. We may find the organization to be a little easier by thinking of the
computation in Eq. (11-1) as being set up in columns numbered from right to
left. (If your computer permits 0 subscripts in a list, you may use that to
good advantage here by starting with 0.)
3 2 1 0 Column numbers
4 3 2 1 Column numbers
3 +
21
6 +22 -10
6 +43 +67 -35
where we initialize the P list at 0. Program TRI 0 BI multiplies the two poly-
nomials of our example.
It will be left as an exercise to modify TRI " BI to multiply pairs of poly-
nomials of various degrees.
.156 Basic BASIC
TR1*BI
3 11 -5 Tl MES 2 7 YIELDS 6 43 67 - 35
DONE
Division
When working with polynomials we often wish to perform the operation of
division. It is especially frequent that we wish to divide by a polynomial of the
form X - R where R i2 a constant. Let us divide 2x:i - 3X 2 - lOX + 3 by
X - 3 and see what can be done to computerize the operation. As with multi-
plication, we will end up considering only the coefficients. First we do the
division by hand:
2x2 + 3X- l
X - 3)2Xa - 3X 2 - lOX +3
2X3 - 6X2
3X 2 - lOX
3x2- 9X
X+3
x 3
Every term in the computation that will be written twice in every problem
appears in bold face. Now if we simply decide not to write things twice and
at the same time compress the problem vertically, we get
Polynomials 157
2x2 + 3X - 1
X - 3)2Xa - 3x2 - lOX + 3
- 6X2 - 9X + 3
3X2 X
We saw that for multiplication, as long as everything was lined up correctly,
we could eliminate all the X's. Also note that we are dividing only by
binomials of the form X - R, so the coefficient of X will always be 1. Let us
not even write it Now we have the division in the following form:
2+3-1
-3)2 - 3 - 10 + 3
- 6 - 9+3
3- 1
Since the coefficient of X in the divisor is always 1, the coefficient of each term
in the quotient will always be the same as the coefficient of the leading term
of the expression into which we divide the X term. Thus it is no accident that
we see 3 - 1 in the bottom row as well as in the answer. So, if we agree to
simply insert the leading coefficient of the polynomial into which we are
dividing X - R in front of the bottom row of figmes, we will always have
the coefficients of the quotient polynomial and we would not need the top
row. We now have reduced the problem to an iteration involving "multiply
and subtract" repeatedly, and the division looks like
-3)2 - 3 - 10 + 3
-6- 9 3
2+3- 1
which we got by the following set of steps: 1) copy down the first coefficient
of the original polynomial 2; 2) multiply 2 by -3 to get -6 and write it down
under the second term of the original polynomial; 3) subtract to get 3, mul-
tiply 3 by -3 to get -9; 4) write it down beneath the next term to the right
and subtract to get -1; 5) multiply -1 by -3 to get +3 and write it down
beneath the next term; 6) subtract to get 0 and we have a 0 remainder. So we
see that 2 + 3 - 1 is interpreted as 2X2 + 3X - 1.
Since subtracting a number may be accomplished by multiplying the num-
ber to be subtracted by -1 and adding, we may convert "multiply and sub-
tract" to "multiply and add" if we multiply the -3 by -1 to get 3. Or for
X - R we just use R. Let us complete the development of this algorithm by
inserting the 0 in the last column to the right to indicate a remainder of 0.
3)2 - 3 - 10 +3
6 9-3
2+3- +o
Dividing 3X! - 2X 2 + 5X - 2 by X + 2 results in
-2)3 + 0 - 2 + 5 - 2
- 6 + 12 - 20 + 30
3 - 6 + 10 - 15 + 28
yielding a quotient of 3X3 - 6X 2 + lOX - 15 and a remainder of 28.
158 Basic BASIC
SYNOI V
SYNTHETIC DIVIS10N:
2 -3 -10 3 DIVIDED BY X - 3 YIELDS
2 3 -I 0
00NE
SYNTHETIC DIVISI0N:
3 -1 4 -s DIVIDED BY X - 2 YIELDS
3 5 14 23
SUMMARY
You should be able to add and subtract polynomials easily using computer
lists. We have written an elementary program for multiplication of two poly-
nomials, and we have written a program to perform synthetic division using
X - R as the divisor.
Y=P<x>
and its graph. The values of X for which Y = 0 are called the zeros of the
function. You have solved many quadratic functions in which there were always
two zeros. Sometimes they were equal, sometimes integral, sometimes real,
and sometimes complex. It can be shown that every Nth-degree polynomial
equation has exactly N complex zeros. Before we actually look for any zeros
of Y = P (X) we need to have some theorems available.
Remainder Theorem
According to the Remainder theorem, if a polynomial is divided by X - Z,
then the remainder is the value of the polynomial when Z is substituted
for X. Dividing P <X> by (X - Z) we get
p(X) - R
(X- Z) - Q<X> + (X - Z)
where Qcx> is the quotient polynomial. Multiplying through by (X - Z) we get
160 Basic BASIC
p(X) = Q(X)
0
(X - Z) +R
and we can see that if we substitute Z for X, then X - Z = 0 and
p<Z> =R (11-2)
Looking at SYNDIV we see that substituting 3 for X in 2xa - 3X2 - IOX
+ 3 gives 54 - 27 - 30 + 3 or 0, confirming that P <a> 0, which is the
=
remainder after dividing by X - 3. We also see that substituting 2 for X in
3X3 - X2 + 4X - 5 gives 24 - 4 + 8 - 5 or 23, confirming that P <2 > 23, =
which is the remainder after dividing by X - 2.
Factor Theorem
The Factor theorem states very simply that if the value of R in Eq. (11-2)
is 0, then X - Z is a factor of P <X» Looking at SYNDIV again, we see that
X - 3 is a factor of 2X 3 - 3X2 - IOX + 3 while X - 2 is not a factor of
3X3 - X2 + 4X - 5. Now all we have to do is find a value of Z so that
Por,> = 0 and Z is a zero of the function.
find at least one zero. Then after we test all possible factors of P[l], we test
to see if the switch is on. If it is, we read more data. If the switch is off,
there were no zeros, so we print a message and then read more data. See
the flowchart in Fig. 11-1.
INTZER works well for polynomials of the same degree; but suppose we
have polynomials of several different degrees we wish to study using the same
program? Well, we could define a different function for each degree or we
could define a function of the highest degree we anticipate and fill in with
leading zeros. But suppose we want up to ninth or tenth degree? The function
would not fit on one line on some terminals. We could define two functions
INTZER
Start
Open X loop
Turn switch
to search for
off
factors of
LET K = 0
P[l]
Turn switch
Close X
on
loop
LET K = 1
and add them. But then we have la1 ge 11umbc1 s of leading zeros to worry
about. All of these complications may be eliminated by using a subrnutine to
define a function instead of a DEF statement NotiC'e that evaluating an
Nth-degree polynomial is equivalent to summing up N + l terms which look
like AiXi where i goes from N to 0. If you have 0 subscripts, this is perfect.
For those of us without 0 subscripts, we must use a term similar to
P[I] 0 X 00 (I - 1), whe1e the value of I goes from N to 1 for N equal to one
TTil11-P th:in thP r]p~rPP nf thP rnlynmni:i]
We may now define a polynomial function in a five-line subrnutine for any
degree with no f urthe1 complications and no l uss over leading zeros and such:
500 LET P 0
510 F0H I N T0 1 STEP -1
Polynomials 163
SUMMARY
We have seen that by combining the Remainder theorem, the Factor
theorem, and the fact that the product of all zeros multiplied by (-1)\ where
N is the degree of the polynomial, gives the constant term, we are able to
find all integral zeros. We simply test all integral factors of the constant term
to see if the remainder is 0. If the remainder is 0, then we have a zero of the
polynomial. If it is not 0, then we do not have a zero of the polynomial. We
have two alternative methods of evaluating a polynomial for a specified value
of X: one is to use a DEF statement, and the other is to write a subroutine to
sum up terms.
2) Generate random intcp;crs in sets of three. Have th(' computer print the poly-
nomial having those three integers as zeros I3c smc to get some n('gative integers.
3) Do problem 2) for sets of four integers.
4) In IZEH¢>1 have the computer determine if P(XJ is within two units of 0 for
each factor of the constant term.
5) Prepare a table of ordered pairs (X, P<XJ) such as would be appropriate for
plotting points. Sketch a graph on graph pap('r. How would you estimate non-
intcgrnl zeros?
IZER0 l
20 PRINT
22 PRINT
23 READ N
2S F'0R S=N T0 I STEP -I
30 READ PCSJ
40 PRINT PC SH
SO NEXT S
SS PRINT
60 PRINT "INTEGRAL ZER0C S> 1 "J
68 REM TURN SWITCH 0F'F'
70 1..ET K=O
78 REM STUDY 1..INE 80 CAREF'LLI.. Y!
80 F'0R X=-PCIJ T0 PCIJ STEP SGNCP[IJ>
88 REM L.INE 90 PREVENTS AN ERR0R MESSAGE CAUSED BY
89 REM DIVIDING BY ZER0
90 IF' X=O THEN l 40
98 REM IS X A F'ACT0R 0r PCIJ?
100 IF' PCll/X <> INTCPCIJ/X) THEN 140
IOS G0SUB SOO
108 REM IS THE REMAINDER ZER0?
110 IF' P < > 0 THEN l 40
118 REM IF' THE C0MPUTER GETS THR0UGH HERE, THE
119 REM VALUE 0F' XIS A ZER0 0F' THE F'UNCTI0N
120 PRINT XJ
128 REM TURN THE SWITCH 0N - WE HAVE A ZER0
130 1..ET K=l
140 NEXT X
lSO IF' K=I THEN 20
160 PRINT "N0NE F'0UNO"J
16S G0T0 20
170 DATA 4,1,-2,-11.12
180 DATA 4, 1, 1,-s,-2
190 DATA 4, 1,-2,3,-4
200 DATA s.2.-1.-11.11.-2
210 DATA 1,2,-S,-6,9,9,-39,36
490 REM SUBR0UTINE SOO THR0UGH S40 TAKES THE Pl.ACE 0r A
491 REM DEF' STATEMENT ANO EVALUATES A P01..YN0MIAI.. 0F'
492 REM 0r DEGREE N-1·
SOO LET P=O
SIO F'0R l=N T0 I STEP -I
S20 1..ET P=P+PCil•XtCI-1>
S30 NEXT I
S40 RETURN
999 ENO
RUN
IZE:R0 I
I -2 -11 12
INTE:GRAI.. ZER0CS>1 -3 4
I l -s -2
INTE:GRAL ZER0CS>& 2
I -2 3 -4
INTEGRAL. ZER0CS>I N0NE F'0UND
2 ·I -11 II -2
INTEGRAL ZE:R0 <S)I 2
2 -s -6 9 9 -39 36
INTE:GRAL ZE:R0 CS>i 3
follows: If point (X 1 , P<Xil) and point (X2 , Pcx J) are on opposite sides of the
2
X-axis, then the graph must cross the X-axis between (X 1 , 0) and (X 2 , 0).
We may now search for real zeros by finding intervals in which the graph
crosses the X-axis. In order to find out if the value of the function is positive
for one value of X and negative for another, we may simply test the product.
If the product is negative, they are of opposite sign. If the product is positive,
then they are of the same sign and we are not concerned with those values of
X. Since we anticipate more than one zero, let us make a provision for putting
the information in a list. For that, we need a counter. It seems reasonable to
list the left number of the interval. As long as we know the increment, we
should be able to see the right number of the interval. It is usual to start
looking for real zeros in an increment of one unit. Let us prepare a flowchart
for this problem and call the program REAL. See Fig. 11-2.
Thus, we have found that the three zeros of 12Xa - 64X 2 + l 7X + 195 fall
in the three intervals -2 to -1, 2 to 3, an<l 3 to 4. That is fine to know, but
we generally prefer more precision than that So, we should try to improve on
Start
Define
function
FNT(X)
Set counter
F¢RX = -5
at O
T¢5
LET A= 0
LET S1 = FNT(X)
LET S2 = FNT(X+1)
LETA=A+1
NEXT X
LET S(A) = S1
REAL
REAL to get smaller intervals. There are several very satisfactory procedures
one might try. Let us develop a program that permits us to make decisions
about what to try for the limits and the increment of the search. That calls
for INPUT statements. We can change line 70 to 70 F0R X = F T0 L STEP
S and INPUT F, L, S for First, Last, and Step. We may also use S = 0 as a
flag to terminate the RUN. After we get the computer to search for a change
of sign in a particular interval, we want it to come back and permit us to
either look for more precision in that same interval or to search in a different
interval. We should also provide for the situation where there has been no
change in sign. This will happen for one of several reasons. Either the search
is not including the zeros within its limits, or the increment is large enough
that two zeros are included in one interval, or there might be no real zeros.
We will discuss this in Sec. 11-4. We can determine that no change of sign
has been found by testing the value of A after line 130. If A is still 0, then
there were no changes of sign and we should print a message to that effect.
We make the above changes and call the program REALI.
REALI
90 LET S2=rNTCX+S>
100 I r S1*S2>0 THEN 130
110 LET A=A+I
120 LET SCAl=X
130 NEXT X
140 I r A>O THEN 190
150 PRINT "N0 INTERVALS r0UND **** TRY AGAIN "
Polynomials 167
D0NE
Since we are using INPUT often in this program, we should pick limits and
increments carefully. We should also be prepared to make up our mind quickly.
Some of the things we should not try are -50 to 50 STEP .01, or 50 to -50
STEP 1. A little care should avoid such blunders.
Let us define a new function and obtain another RUN.
50 DEF' F'NT<X>=Xt3+49.16b9*X•2+2.67761*X-15223·B
RUN
REAL2
OONE
One of the contingencies that we have not accounted for in REALl is the
possibility that the polynomial has integral zeros. As the program stands, if Sl
or S2 equals 0, then the value of X used for Sl will be identified as the
number at the beginning of the interval in which a real zero will be found.
It will be left as an exercise to identify a zero more explicitly if Sl or S2
does equal 0.
SUMMARY
We have used the Location Principle to find intervals within which real
zeros are expected to occur. It should be noted that the Location Principle
may be applied to any continuous function and is not limited to polynomial
functions.
and
Start
END
LET F = 2•P[3]
LET A= - P[2]/F
LET B = SQR
(ABS(D))/F
PRINT PRINT
nonreal real
(A,B), (A,-B) A+B,A-B
C0MP- l
10 PRINT
20 READ PC3J,PC2J,PCIJ
30 IF PC3l=O THEN 170
40 PRINT PC3JJPC2JJPCIJ
SO LET D=PC2l•2-4•PC3l•PC ll
60 LET F=2•PC3l
70 LET A=-PC2l/F
80 LET B=SQRCABSCD>>IF
90 IF D<O THEN 130
100 PRINT "REAL ZER0SI"
110 PRINT A+BJ"AND "JA-8
120 G0T0 10
130 PRINT "N0N-REAL ZER0S:"
140 PRINT .. C"JAJ","JBJ") AND C"JAJ","J-BJ">"
150 G0T0 10
160 DA TA 1, 2. 3, I 1 - 3, 2, 1, 3, 2, 1, 3, I 3, - 1, 2, - 3, I• 3, 12, o, Q, 0
I 70 END
Polynomials 171
HUN
C0MP-l
1 2 3
N0N-REA L ZEr10S:
C-1 • 1·41421 > AND C-1 , - 1 • 41 421
1 -3 2
REAL ZER0S:
2 ANO
1 3 2
REAL lER0S:
-1 AND -2
1 3 13
N0N-REA L ZER0S:
C- 1 • 5 , 3 • 2 78 72 ) AND c-1.s • -3· 278 72
-1 2 •3
N0N-REA L ZER0S:
C 1 ·-1.4142 1 > AND C 1 , 1. 41 421
I 3 12
N0N-REA L ZER0S:
c-1. s • 3-1225 ) AND c-1.s ·-3·122 5
D0NE
ALLZER
8 REM INTZER BEGINS HERE C WE HAVE REM0VED THE REM STATEME NTS>
9 REM SEE THE PR0GRAM r0R REM STATEME NTS
10 DEr rNPCX>= PC4J*X• 3+PC3l* X•2+PC2 l*X+PCll
20 PRINT
30 PRINT
40 r0R S=4 T0 1 STEP -I
50 READ PCSJ
60 Ir PCSJ=.0 101 THEN 430
70 PRINT PC SlJ
80 NEXT S
90 PRINT "INTEGR AL ZER0: "J
100 r0R X=-PClJ T0 PCll STEP SGNCPC ll>
110 I r X=O THEN 160
120 I r PCll/X <> INTCPC ll/X> THEN 160
130 Ir rNPCX> <> 0 THEN 160
140 PRINT X
150 G0T0 I 70
160 NEXT X
164 REM INTZER ENDS HERE *** SYNDIV BEGINS HERE
1 70 LET R=X
180 PRINT "SYNTHE TIC DIVISl0N BY X -"JRJ"Y IELDS:"
190 PRINT PC 4JJ
200 r0R X=3 T0 1 STEP · I
210 LET PCXl=PC Xl+PCX+ ll*R
220 I r X>l THEN 240
230 PRINT "REMAIND ER ="J
240 PRINT PtX]J
250 NEXT X
252 REM SYNOIV ENDS HERE
258 REM HERE WE M0VE EACH ENTRY IN THE P LIST
259 REM T0 THE L0CAT!0 N 0NE SUBSCRI PT L0WER
172 Basic BASIC
260 F"0R X= 1 T0 3
270 LET P[Xl=P[X•ll
280 NEXT X
290 PRINT
298 REM C0MP-1 BEGINS HERE
300 LET D=PC2Jt2-4•Pt3l•PC1l
310 LET F"=2•PC3l
320 LET A=-Pt2J/F"
330 LET B=SQRCABS<D>>IF
340 IF D<O THEN 380
350 PRINT "REAL ZER0S:"
360 PRINT A+BJ "AND "J A-B
370 G0T0 20
380 PRINT '~0N-REAL ZER0S1"
390 PRINT "C"JAJ"11'°JBJ"> ANO C"JAJ"1"J-8J">"
400 G0T0 20
405 DATA 112,-1,-2
410 DATA l1-l101-48.110.01-l. l101011.1.-1.11-l
415 DATA 61-771-1891-90
420 DATA ·0101
430 END
RUN
ALLZER
1 2 -I -2 INTEGRAL ZER0:
SYNTHETIC DIVISI0N BY X - I YIELDS:
I 3 2 REMAINDER = 0
REAL ZER0S:
-I AND -2
I 0 0 -1 INTEGRAL ZER0:
SYNTHETIC DIVISI0N BY
I I I
x I-
REMAINDER = 0
YI ELDSI
N0N-REAL ZER0S:
<-·5 .0666025 AND (-. 5 .-.666025
I 0 0 INTEGRAL Z£R0: - I
SYNTHETIC DI VI SI0N BY x --1 YIELDS:
I - I I REMAINDER = 0
( • 5 .
Ni2JN-REAL ZER0S:
o666D25 AND ( • 5 1-.866025
I -I ·I INTEGRAL ZER0:
SYNTHETIC DIVISI0N BY X - I YIELDS:
I 0 I REMAINDER = 0
N0N-REAL ZER0S:
< 0 , I >AND< 0 1-I
00NE
Polynomials 173
the way in which the quotient was stored in SYNDIV. It turns out that the
quotient polynomial can be stored right back in the P list instead of creating
the new Q list. This is done in line 210 of ALLZER. Then in order to avoid
changing the subscripts in C0MP-l it seems reasonable to simply take the
quotient polynomial, which also stores the remainder in the lowest subscripted
location, and move every entry into the location one subscript lower. Instead
of having the quotient polynomial in P[4], P[3], and P[2], we are putting the
quotient polynomial in P[3], P[2], and P[l], which exactly fits program
C0MP-l. This is done in lines 260, 270, and 280.
As always, some interesting problems have been left for you to solve. For
instance, suppose we have third-degree polynomials with two nonreal zeros
and a real zero that is not an integer, or what about higher degree poly-
nomials? These considerations are left as exercises in the problems set for
Sec. 11-4.
DES CRT
10 READ N
15 IF N=O THEN 999
20 F0R X=N T0 I STEP ·1
30 READ PCl.Xl
40 PRINT PCl.XlJ
48 REM ENTER THE SAME C0EFF'ICIENT IN THE SAME C0LUMN
49 REM 0r THE SEC0ND R0W
50 LET PC2.Xl=PCl,Xl
58 REM Ir THE EXP0NENT 0N X IS 0DD THEN CHANGE THE SIGN
60 Ir CX+l>l2=INTCCX+l>l2 > THEN 80
70 LET PC2,Xl=-PC2,Xl
80 NEXT X
90 PRINT
100 PRINT N- IJ "C0MPLEX ZER0S"
200 LET Vl=V2=0
210 r0R X=N-1 T0 1 STEP -1
218 REM L00K AT P0SITIVE ZER0S
220 Ir PCl.Xl•PCl,X+ll>O THEN 240
230 LET Vl=Vl+l
238 REM L00K AT NEGATIVE ZER0S
240 IF PC2,Xl•PC2,X+ll>O THEN 260
250 LET V2=V2+1
260 NEXT X
300 PRINT VIJ"MAX P0SITIVE"
310 PRINT V2J "MAX NEGATIVE"
320 PRINT
340 G0T0 10
500 DATA 4.6.103.201.90
510 DATA 5, 1,3,4,.3,2
520 DATA 6, - 4, - 3, 5, -2, 1, - 3
600 DATA 0
999 END
RUN
DES CRT
6 103 201 90
3 C0MPLEX ZER0S
0 MAX P0SI TI VE
3 MAX NEGATIVE
3 4 -8 2
4 C0MPLEX ZER0S
2 MAX P0Sl Tl VE
2 MAX NEGATIVE
-4 -3 5 -2 -3
5 C0MPLEX ZER0S
4 MAX P0 SI Tl VE
0
MA X NE GA TI VE
D0NE
You might reasonably ask, just what have we done that could not be done
quicker bv hand. Well, maybe not much, but look at 6 103 201 90
in DESCRT. If we run these coefficients through INTZER, the computer tests
P[I]/X 180 times (from -90 to 90 skipping 0). We may now use DESCRT
and change the limits on the test loop in INTZER to test no positive values
of X. So the computer will now test a maximum of 90 values of X. V\Te could
take this one step further and use the fact that the maximum number of
Polynomials 175
negative zeros is three to transfer out of the loop after the third value is found
if they all are integ!al.
There is more to Descartes' Rule of Signs than appears in program DESCRT.
The rule states that zero coefficients are to be dropped. DESCRT does not
provide for that. You will find that when zero mefficieuts appear, we may
consider polynomials such as
P!Xl = 3X + 2X:
4 1
- 5X:! - 7
Vl for positive zeros gives us l. The eoefficients fo1 P!-Xl are 3, -2, -5, -7,
and V2 is l. Since there are no 0 zeros, there are a total of two real zeros.
Since there are 4 eomplex zeros, we find that there are two nonreal zeros
for p (X)"
SUMMARY
Once again we have used polynomial coefficients stored in a computer list.
This time we find all zeros whenever no more thau two zeros are nonintegral.
In addition, we have used Descartes' Rule of Signs to obtain the possible
numbers of positive and negative zeros and outlined a procedure for deter-
mining the possible numbers of nonreal zeros.
MAT-I
-1 - I - I - I
- I ·I - I - I
-I - I - I - I
OONE
.176
MAT Instructions in Basic 177
MAT-2
10 DIM AC 3, 41
20 MAT READ A
30 MAT PRINT AJ
40 DATA -1,-i.-1,-1,-1,-1, -1, -1, -1, -1,-1,-1
50 END
RUN
MAT-2
- I - I - I - I
- I - I - I - I
- I - I - I - I
OONE
MAT-3
10 DIM AC 3, 51
20 MAT READ A
30 MAT PRINT AJ
40 DATA 1.2. 3, 4, 5, 6. 1.a.9, 10.11. 12. 13.14, 15
50 END
RUN
MAT-3
2 3 4 5
6 7 8 9 10
11 12 13 14 15
D0NE
178 Basic BASIC
It should be clear now that MAT READ enters numbers just as we read
across the printed page. It reads and enters across until it runs out of space
in the row and then reads the next data item into the first location of the next
row. This is the method we have been using in all array programs throughout
this text.
It was stated earlier that a list is just a special array consisting of a single
column or a single row, depending on the computer. Now we will look at
arrays of just one column or just one row. See MAT-4 and MAT-5.
MAT-4
10 DIM AC 5, l l
20 MAT READ A
30 MAT PRINT A
40 DATA I 1 2, 3, 4, 5
50 ENO
RUN
MAT-4 MAT-5
10 DIM AC 11 SJ
20 MAT READ A
2 30 MAT PRINT AJ
40 DATA 1. 2, 3, 4, 5
3 50 ENO
RUN
4 MAT-5
5 2 3 4 5
00NE 00NE
Some systems may permit you to dimension a list as DIM A[5] and then
MAT READ A. If this works, then you can determine whether your system
thinks of a list as a row vector or a column vector, by having it MAT PRINT
A when A is a list.
The MAT READ statement has an optional redimensioning capability.
MAT READ A[R, C] redimensions A to have rows numbered up to R and
columns numbered up to C and then reads data into that redimensioned array.
See MAT-6.
MAT-6
10 DIM AC616J
20 MAT READ AC 21 51
30 MAT PRINT AJ
40 DATA 6, 3, 4, 61 - 1, o, 1 7, 31, 699 • 10
50 ENO
RUN
MAT-6
6 3 4 6 -1
0 I7 31 699 10
OONE
MAT Instructions in Basic 179
Some systems permit the use of MAT READ A[R, CJ to perform the initial
dimensioning within certain limits (usually up to [10, 10]).
An array of just one column is called a column vector by mathematicians.
An array of one row is called a row vector. Mathematicians use the term matrix
to describe all arrays. Thus the term MAT is used in BASIC.
MAT READ X
Reads data into the array named X according to previously deter-
mined dimensions.
MAT PRINT P; Q, R;
Prints array P with semicolon spacing, then prints array Q with
comma spacing, and then prints array R with semicolon spacing.
Even though you use MAT READ in a program, you are not required to
use MAT PRINT. You may often want to use nested loops to print an array
as we have been doing up to this section. You will do this if you do not want
the blank line between printed rows and if you want to print headings in front
of each row or if you only want to print a p01 tion of the array. Note too,
that you may use MAT PRINT even if you have not used MAT READ. This
will be the case iJ·we analyze data and enter results into an array as we did
in Chap. 5.
5) Use the data of problem 4), but this time multiply each entry by the product
of the row and column number. MAT PRINT the result.
6) Fill a 2 by 5 array with the following numbers: 3, 67, 32, 45, 90, 2, 9, 57, -3,
and 1. Multiply each entry by -3 if the sum of the row and column numbers is odd
and by --1 if the sum of the row and column numbers is even. Print the result.
7) Fill a square array so that the locations along the top left to bottom right
diagonal are filled with l's and all other entries are O's. MAT PRINT the array.
8) Fill an array with all l's and print it.
9) Have the computer read the following array:
1 6 11
2 7 12
3 8 13
4 9 14
5 10 15
and have it create the new array:
l 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Write a program that will find lolal expenses for the week, total expenses for each
day of the week, and total expenses in each of the five categories listed on the
expense sheet.
To get from a certain town to another town one must travel over a toll
road, through a toll tunnel, and over a toll bridge. At the beginning of the
trip there is a sign posted, listing the rates as in Table 12-1.
MAT Instructions in Basic 181
TABLE 12-1.
On a particular day there were five caravans which traveled this route. The
caravans consisted of different types of vehicles as shown in Table 12-2.
TABLE 12-2.
Caravan 1 1 3 4 2
Caravan 2 1 5 3 6
Caravan 3 2 4 2 5
Caravan 4 1 6 3 2
Caravan 5 3 0 2
The Road Commission would like to have a report which would include the
amount each caravan paid in tolls at each toll booth. The problem will be
solved, when we are able to fill in Table 12-3.
TABLE 12-3.
Tolls Paid
Road Tunnel Bridge
Caravan 1 A B c
Caravan 2 D E F
Caravan 3 G H
Caravan 4 K L
Caravan 5 M N ¢
Before we actually attempt the problem solution, let us write just the por-
tion of our program that we will use later, which will read the data into two
arrays. One array, which we call A, stores the values of Table 12-1, the other
.182 Basic BASIC
array, which we call B, stores the values of Table 12-2. Then let us print the
two arrays with headings so that we may later concentrate on the actual
problem solution, having taken care of the mechanics of getting the data into
the proper arrays. Taken by itself, the task of getting the data into the two
arrays is reasonably straightforward. See program T0LL-l.
T0LL- I
20 DIM A(4,3J,B(S.4l
40 MAT READ A
60 DA TA 6, 3, 2• 5, 3, 2• 4, 3, 2, 3, 21 I
80 PRINT "T0LLS PER VEHICLE"
100 PRINT "R0AD TLINL BRIDGE"
120 MAT PRINT Al
140 MAT r<EAD B
I 60 DATA I, 3, 4, 21 I• 5, 31 6, 21 4, 2, S. I 1 6, 3, 2, 3, I• o. 2
180 PRINT "VEHICLES PER CARAVAN"
200 PRINT "TRUCK BUS CARS M0T0RCYCLES"
220 MAT PRINT BJ
240 ENO
t<UN
T0LL- I
s 3 2
4 3 2
3 2
s 3 6
2 4 2 s
6 3 2
3 0 2
00NE
\Vriting the program in parts like this will help us isolate any errors that
we might encounter while writing the program. We may now strike out lines
80, 100, 120, 180, 200, and 220 as we will not need these values printed again.
Now to tackle the problem itself. We can find out how much Caravan 1
paid at the road toll booth. It had one truck which paid $6, three buses at $5
e::!C::~ fo!· $!5, fo'..!!' <:'l'.!o '.1.t $4 l"'.'r:h fnr *lR, <>nrl two motmf'yf'lP.s >it $~ P.~lf'h
for $6. Totaling 1 ° 6, 3 ° 5, 4 ° 4, and 2 ° 3 we get $43. So, $43 is the value
of A in Table 12-3. How much did Caravan 1 pay at the tunnel? It paid
1 ° 3 for the truck, 3 ° 3 for the buses, 4 ° 3 for the cars, and 2 ° 2 for the
motorcycles for a total of $28, which is the value of B in Table 12-3. We
MAT Instructions in Basic 183
Start
Step through
Step through
columns of
rows of MAT B LET C[R, CJ = 0
MATA
Ft/>R R = 1 T\2'>5
FQ)R C = 1 T\LJ 3
Step columns in
Band rows in A
FOR X = 1 T\2)4
f0LL-2
20 DIM AC4o3loBC5o4J
25 DIM CC 51 3l
40 MAT READ A
60 DA TA 61 31 21 5, 3, 21 4o 3• 21 31 2o 1
140 MAT READ B
160 DATA l • 31 4, 2. I 1 5, 3, 61 2, 4o 2• 5, lo 6• 3, 21 3, l 1 01 2
235 REM WE STEP THR0UGH R0WS 0F' B THE CARAVANS
240 F'0R R= 1 T0 5
255 REM WE STEP THR0UGH C0LUMNS 0F' MAT A
2 56 REM THE T0L.L 800 TH IDEN Tl F'I CA TI 0N
260 F'0R C=l T0 3
.................................
1'"11' TT Al T":JI'~
61 39 24
SS 34 2·1
S4 34 22
29 16 10
OONE
5) Let
A~[:10
2
5
8
11 Ii] and B= [~
A=G
2
6
3
7 :J and B= [10
14
11
15
12
16
13
17
J
Why could not we perform the set of operations of program T0LL-2?
Multiplication
From the discussion in Sec. 12-2, we should see that the dimensioning must
conform as
B[R, M] 0
A[M, CJ = C[R, C]
Calling for a product of two nonconfm ming matrices will generate an error
message from the computer. The program statement for multiplication is
0
XXX MAT C B A
We may now have the computer do everything from lines 240 through 380
with a single statement Having worked through the operation in considerable
detail in Sec. 12-2, you should have little difficulty in having the dimensions
correctly provided for. See T0LL-3.
T0LL-3
20 DIM A[4,3J,BC514l
25 DIM C[ 5, 3.1
40 MAT READ A
60 DATA 61 3, 2, 5, 3, 2, 4, 3, 2, 3, 2, I
140 MAT READ B
160 DATA 1,3,4,2,1,5,3,6,2,4,2,5, 1,6,3,2,3,1,0,2
200 MAT C=B*A
210 PRINT "R0AD","TUNNEL"• "BRIDGE"
230 MAT PRINT C
999 END
RUN
T0LL-3
61 39 24
55 3A 21
54 34 22
29 16 10
D0NE
XXX MAT D = A - B
or XXX MAT A = A - B
Neither addition nor subtraction is defined for arrays of different dimensions.
Scalar Multiplication
You may multiply each element of an array by some constant or algebraic
expression using
XXX MAT Z = (SIN(G)) 0
X
which multiplies every entry in MAT X by SIN(G) and enters the product in
MATZ.
Equality
A matrix may be created to be identical to an already existing matrix by
XXX MAT P=Q (12-2)
or, in systems which do not permit Eq. (12-2), you should be able to achieve
the same result by
XXX MAT P = (1) 0
Q
Special MAT's
The1e are three special matiices available with a single assignment state-
ment in BASIC. They are
xxx MAT A= ZER (12-3A)
xxx MAT B = ZER[R, C] (12-3B)
yyy MAT C =C0N (12-4A)
yyy MAT D = C0N[R, CJ (12-4B)
zzz MATE= IDN (12-5A)
zzz MAT F = IDN[N, N] (12-5B)
Equation (12-3A) sets all entries in MAT A equal to 0 according to previ-
ously determined dimensions, while Eq. (12-3B) sets the dimensions of B at
[R, CJ and fills MAT B with O's. Equation (12-3B) is often used to change the
dimensions of a matrix during the RUN of a program.
Equation (12-4A) sets all entries in MAT C equal to 1 according to pre-
viously determined dimensions, while Eq. (12-4B) sets the dimensions of MAT
D at [R, CJ and fills it with l's.
Equation (12-5A) requires that ivfAT E be a square array, and fills the
upper left to lower right diagonal with l's and all other locations with O's.
Equation ( 12-5B) has the same effect as Eq. ( 12-.SA), but the dimensions are
set at [N, NJ. The matrix c1eated in this form is called an identity matrix.
Prog1am MATSPl is intended to show how these special arrays are established.
188 Basic BASIC
MAT SP I
0 0 0 0
MAT B=C0NC 3, 7J
I I
MAT C=lDNf 4, 41
I 0 0 0
0 0 0
0 0 0
0 0 0
MAT A=C0NCl.6J
I I I
00NE
SUMMARY
We have introduced the matrix operations-multiplication, addition, subtrac-
tion, and scalar multiplication, The special matrices ZER, C0N, and IDN have
been specified.
[ 21
-~
3
-3
5
-1
-2
1
-1
-'] [=n
-3
11
-1
and
MAT Instructions in Basic 189
[: -7
3
-3
-1
3
5
-2
1
-1
1
then we are really finding the values of W + 3X -
-'11] [fl
-3
-1
an<l
2Y - +Y
-7W + 5X - Y + llZ,
an<l 3W - X + Y - Z.
Z, 2W - 3X 3Z,
3) Multiply
n [: -~]
0 7
G
an<l multiply
0
0
by 5
0
[~ n G ~]
0 6
0 by 1
0 0
Any conclusions about the matrix of O's?
4) Let
5) Let
[i
2
-1
-2
-n ["' by .52
.12
-.24
.44
.64
-.2
-.2
·']
8) Let
A- [-2
- 1.5 ~.5] and B=G !]
0 0
Find A B and B A.
./ 9) ·write a program to raise a matrix to a power. Let the power be determined
by an item of data.
10) Enter the integers 1 through 12 into a row vector and into a column vector,
using MAT READ. (You can avoid typing the data twice by using REST0RE.)
Find the 12 by 12 product matrix and print it.
190 Basic BASIC
( 12-6)
A 1X
A~X
+ B 1Y + C 1 Zl [D11
+ B~ Y + C 2 Z = D2 ( 12-7)
[
A:;X + B:1Y + C3 ZJ D3 j
Defining equality for two matrices as existing if and only if for all I, J pairs
the entry of one matrix equals the corresponding entry of the other, or for
MAT A and MAT B, A[I, J] =
B[I, J], we may say that
A 1X + B 1Y + C 1Z = D 1
A 2X + B 2Y + C 2 Z = D 2 ( 12-8)
A3X + B3Y + C 3 Z = D 3
which constitutes a system of three linear equations.
Equations (12-6), (12-7), and (12-8) are simply three different ways of writ-
ing the same set of equations. If we can find the values of X, Y, and Z in
Eq. (12-6), we will have solved the set of linear equations in Eq. (12-8).
Let us rewrite Eq. (12-6) as
0
C S =K
where
We may call its inverse the matrix with entries A, B, C, and D such that
~] = [~
Finding the product on the left, we get
5A + 7ll
[ 5C+7D
6A
6C + 8D
J
+ 8B = [l
0 ~]
Two matrices are equal if their conesponding entries are equaL So we get
the following fom equations:
5A + 7B 1 6A +BB= 0
5C + 7D 0 6C + 8D 1 =
Solving these for A, B, C, and D we get A -4, B 3, c 3.5, and
D =-2.5. So
6]-l- [-4
8 - 3.5 -~.5 J
BASIC prnvides a statement to find the inverse of a square matrix, if it
exists. After we have arranged for proper dimensioning, we may use
XXX MAT X INV(A)
MA TI NV
10 DIM xc2.21.Ac2.21.Pc2.21
20 MAT READ A
30 MAT X=INV<A>
40 PRINT "0RIGINAL MATRIX"
50 MAT PRINT AJ
60 PRINT "INVERSE MATRIX"
70 MAT PRINT XJ
80 P1HNT "THE PR0DUCT IS"
90 MAT P=X*A
100 MAT PRINT Pl
110 DA TA 5, 6, 7, 8
120 END
RUN
MAT INV
0RIGINAL MATtHX
5 6
7 8
INVERSE MATRIX
-4. J.
THE PR0DUCT IS
1 0
00NE
192 Ba.sic BASIC
and matrix X will be the inverse of matrix A. We may easily verify our calcu-
lations for finding the inverse above. See MA TINY.
(You are cautioned that the computer may be susceptible to slight errors
when using the INV( ) statement.)
So with the MAT INV( ), we should be able to solve sets of simultaneous
linear equations such as the following:
4W- X + 2Y + 3Z = - 3 (12-9A)
- W+4X+ 2Y =-15 (12-9B)
W + 2X - Y+ 3Z = - 3 (12-9C)
-4W+ 3X+ 2Y + Z= -17 (12-9D)
We let
~]
-1 2
-1 4 2
C= [ 41 2 -1
-4 3 2
where C is usually referred to as the coefficient matrix, and we let
K= [=':]
-17
Now we can read the data into two matrices C and K, have the computer find
the inverse of C, and multiply it by K to get matrix S consisting of the values
for W, X, Y, and Z, which satisfy Eq. (12-9). See program S0LVE.
S0LVE
10 DIM C[4,4),K[4,IJ,SC4.IJ,I[4,4J
20 MAT READ C.K
30 MAT !=INVCC>
40 MAT S=l*K
50 Pt<INT "S0LlJTI0NS;"
60 MAT PRINT S
70 DATA 4, .. 1, 2, 3, - I• 4, 2, o, 1, 2. - 1, 3, - 4, 3, 2, J, - 3, - I 5, - 3, - I 7
80 END
RUN
S0l. VE
S0LUTl0NSJ
1.
-2
-3.
-I
D0NE
MAT Instructions in Basic 193
SUMMARY
We have seen that sets of simultaneous linear equations may be solved by
=
considering an equivalent matrix equation C X K, where C is the coeffi-
0
cient matrix, X is a column vector which contains the values of the variables
in the original set of linear equations, and K is a column vector containing the
constant terms in the original set of linear equations. We may solve for X by
finding the inverse of matrix C, so that X = C- 1 0 K. The inverse may be found
with the BASIC statement MAT I= INV(C[. For systems of simultaneous
linear equations having a unique solution, MAT C will always be square, which
is one of the requirements for having an inverse.
A= [ :
-3
-4
1
9
_;]
Find and print A -1, A 0
A -1, and A -1 ° A.
2) Let
B= [-8 -3]0 -1
Find B-1 and print it Verify by hand-computing the inverse of R Find aml print
B 0 B-1 and B-1 ° B.
10) Let
-n ~]
-2 -4
-1 and 13
3 2 -5
Find and print (A 0
B)-1 and B-1 ° A-1.
j 11) Write a program that can solve sets of simultaneous linear equations having
different numbers of equations. Have an item of data that is the number of equa-
tions and redimension all matrices accordingly.
Suppose you have just solved a set of lU simultaneous imear equations. The
10 values of the 10 unknowns are entered into a column vector that is called
X in Sec. 12-4. Calling for l\IAT PRINT X prints the 10 values down the page
with a blank line between every two. This takes up a lot of space. It might
MAT Instructions in Basic 195
be convenient to enter these same values in a row vector and MAT PRINT
that on one line. What \\le want is to create a new matrix whose row corre-
sponds to the column of the matrix X and whose columns correspond to the
rows of matrix X, i.e., an exchange of rows and columns. Of course we could
make the exchange element by element or we could do the printing entry by
entry, but both are unnecessary. BASIC provides a program statement to per-
form this set of exchanges. XXX MAT A= TRN(B) fills matrix A so that its
rows correspond to the columns of B and its columns correspond to the rows of
B. This set of exchanges creates a matrix called the transpose of B. We write
the transpose of B as Bt.
As noted earlier, the transpose will enable us to have more compact print-
ing in some programs. The transpose also introduces some matrix properties
of theoretical interest. Some of these properties may be suggested by the
exercises.
TRP0Sl is simply a demonstration program that finds and prints the trans-
pose of a IO-element column vector.
TRP0Sl
D0NE
The transpose differs from the inverse in that every matrix has a transpose.
If MAT A has M rows and N columns, then Al has N rows and M columns.
Let us write a second demonstration program to print a 2 by 4 matrix and its
transpose. See TRP0S2.
TRP0S2
10 DIM A[2,4J,B[4,2J
20 PRINT "2 BY 4 MATRIX"
30 MAT READ A
40 MAT PRINT AJ
~O MAT B=TRN<A>
60 PRINT "TRANSP0SE 0F" THE AB0VE MATRIX"
70 MAT PRINT BJ
60 DATA 3.6.1.-s.o.1s.999,11
90 END
196 Basic BASIC
RUN
TRP0S2
2 l:lY 4 MATRIK
3 6 1 -S
0 18 999 11
999
-S II
OONE
MAT X = TRN(Y)
Creates a matrix X so that for all I, J pairs, X[I, J) =
Y[J, I]. Dimen-
sions must be correctly provided for. X is called the transpose of Y.
~
-n
-2
A= [ 1
-3 4
Find and print At, At+ A, andAt A.
2) Let
~]
3
A=D -2
9
Print At, A+ At, A - At, and At - A.
3) Let
A=[~ !]
Let B =At and let C = A-1. Print B-1 and Ct.
4) Let
2 -1 3
A= 5 0 and 5
[ 4
-3 -2
Print [A 0
B]t, Bt 0
At, and At 0
Bt.
CHAPTER 13
Elementary Probability
13-1 INTRODUCTIO N
It is the purpose of this chapter to introduce some fundamental concepts
of probability and to develop program routines for some of these applications.
Taking an intuitive approach to probability, we may think of rolling a die.
The term experiment is used to describe a particular trial, or in the case of
rolling a die, an experiment is the actual rolling of the die. The outcome is the
number that comes up. There are six possible outcomes for rolling a die. We
may say that the probability of the die coming up 2 is one in six or Yri,
because there is only one 2 and there are six different numbers, each of which
is equally likely to come up. We refer to the outcome we are looking for as a
success and all others as failure. We define probability so that the probability
of success P added to the probability of failure Q is 1, or P + Q = 1.
Often our requirements for success permit more than one single outcome,
all of which are equally likely to occur. We define probability as the quotient
of the number of outcomes that constitute success and the total possible
number of outcomes:
P=S/T
where P is the probability of success, S is the number of outcomes that
constitute success, and T is the total number of possible outcomes. All out-
comes are equally likely to occur.
So, before we work with probability, we will have to develop ways of
counting the numbers of outcomes of various kinds of experiments.
13-2 ENUMERATIO N
Fundamental Principle of Enumeration
The Fundamental Principle of Enumeration states that, if one choice can
occur in A ways and then a second choice can occur in B ways, the total
number of ways that the two choices may occur is the product of A and B,
or A" B.
197
198 Basic BASIC
So, if you are going to buy a car tbat comes in five models and seven
colors, the number of cars you have to choose from is 5 ° 7, or 35. The
Fundamental Principle of Enumeration may be extended to cover any number
of choices so that, if in buying the car you also may choose airconditioning
and whitewalls and you have four engines from which to clioose, the number
of cars available is 5 ° 7 ° 2 ° 2 ° 4, or 560.
Permutations
How many four-letter combinations may be formed using the letters of the
word FLAG each used once?
We could approach this problem in one of several ways. We could sit down
with pencil and paper and try to write them all, or we might write a program
to write them all.
The techniques required for this vary so greatly from system to system
that we wiU not present the program, but only the RUN.
RUN
FLAG
D0111E
We can easily see that the number of different combinations is 24. Each of
the combinations listed is a permutation of the four letters F, L, A, and G, and
each is different from the others because the letters are in a different order.
In other words, when we talk about permutations, order matters.
One other approach to solving the original problem will lead us to a more
general enumeration technique. We observe that to form a four-letter word
using four different letters once, we may use any one of the four letters for
the first letter. Now there are only three letters left from which to choose the
second letter, two left from which to pick the third letter, and finally we have
exactly one letter for the fourth letter of the new word. Using the Fundamental
Principle of Enumeration, there are four choices. The first can occur in four
ways, the second can occur in three ways, the third in two ways, and the
fourth in one way. This makes 4 ° 3 ° 2 ° 1 or 24, ways that the four choices
can occur.
This kind of calculation occurs often in mathematics and so is given a special
name. 4 ° 3 ° 2 ° 1 is called 4 factorial written as 4! . In general,
N(N - l)(N - 2) · · · (2)(1) = N!
where N is a positive integer. Let us write a routine to compute factorials
(see program N!).
Elementary Probability 199
N!
DONE
RUN
N!
DONE
We should see that for the number of arrangements of R letters taken from
among N different letters with no duplications we get N!/(N - R)!. This
defines the number of permutations of N things taken R at a time written as
N!
= (N - R)! (13-1)
NPn
Writing the right side of Eq. (13-1) as the quotient of products, we get
N(N - l)(N - 2) · (N - R + 1) (N - R)(N - R - 1) · · · (2)(1)
(N - R)(N - R - 1) · · · (2)(1)
Dividing we get
:-iPn = N(N - l) · · (N - R + 1)
which is ideal for computing with a loop that goes from N to N - R +1
STEP -1. See line 40 of p1 ogram NPR.
200 Basic BASIC
NPR
10 READ N,R
20 IF N=O THEN 100
30 LET P= I
40 FflR X=N Tel N-R+I STEP -I
50 LET P=P*X
60 NEXT X
70 Pr<INT N; "THINGS"! RI "AT A TIME HAVE"! P; "PE.RMUTATIO"lS"
80 G0T0 10
90 DATA a,3,4,4,0,0
I 00 END
RUN
NPR
D0NE
Combinations
The distinction between combinations and permutations is order. For com-
binations, order does not matter. We may think of combinations as selections
of items while permutations are arrangements. The number of combinations of
four letters selected from among four letters is one. The number of combina-
tions of N different things taken R at a time is written :-;Cn. We may find the
number of combinations of N things taken R at a time by looking at the
number of permutations. Each combination of R things could be arranged in
R! ways and that gives us the number of permutations. So
8P3 = 336
Combinations pertain to such things as committees and dealing cards where
order does not matter.
If we want to know the number of five-member committees that can be
selected from among ::lU people, we get ~ 0 t:~. !<'or the purpose ot wntmg a
computer program, we might think of ~ 0 C,1 as ~ 0 P 0 /R!. One approach is to
compute ~ 0 P 0 and then successively divide by the integers from 5 down to L
Let us draw a flowchart (Fig. 13-1) and call the program NCK
Elementary Probability 201
END )
LETC = C•X
F¢R X = R Tq') 1
NEXT X
STEP-1
LETC = C/X
PRINT
NEXT X
results
NCR
10 READ N, R
20 IrN=OTHENl50
30 LET C= 1
40 REM 30 TO 50 FIND NPR
50 r0R X=N T0 N-R+l STEP -I
60 LET C=C*X
70 NEXT X
80 REM 60 T0 80 DIVIDE BY R!
90 FOR X=R T0 I STEP -I
100 LET C=C/X
llONEXTX
120 PRINT NJ"THINGS"JRJ"AT A TIME HAVE"lCl"C0:-1BP.JATI O'JS"
130 GOTO 10
140 DATA 3,3,4,4,20.5.0.0
I 50 END
202 Basic BASIC
RUN
NCR
D0NE
Partitioning
In how many ways can we arrange three X's and five Y's? We get 8!/3!51.
We might ask this question in the following way: In how many ways can we
put eight different things in two groups where one group contains three things
and the other contains five and order does not matter?
In how many ways can we arrange three X's, five Y's, and six Z's? We get
141/3!5!6!. We could ask the question in the following way: In how many
ways can 14 different items be put into three groups of three, five, and six
items?
The second version of each of the last two problems are examples of parti-
tioning. In general, if we have RI> R2 , . . . , R11 items such that R 1 + R2
=
+ · · · + R0 T, then the number of ways that we can put the T items in n
groups of R 1 , R 2 , ••• , R 0 is
Note that all the problems treated under pe1mutations and combinations
were really special cases of partitioning. The combinations of N things taken
R at a time may be thought of as partitioning into two groups of R and N - R
items. The problem of arranging SNOWSHOES may be thought of as parti-
tioning into six groups of three items for the S's, two items tor the U's, and one
item each for the four remaining letters N, W; H, and E. Finally, the permu-
tations of N different items taken R at a time may be thought of as R + I
groups of N - R in the first group and one item each for the other R groups.
Elementary Probability 203
SUMMARY
This section has been devoted to introducing the Fundamental Principle of
Enumeration and the enumeration of permutations, combinations, and parti··
tionings of objects. In counting permutations order matters. Permutations count
such things as arrangements of letters in a word and books lined up on a
bookshelf. When counting combinations order does not matter. We use com-
binations for such things as the number of different committees formed from
a group of people and hands dealt in a game of cards.
20) How many different sets of two five-card hands can be dealt from a 52-card
deck?
21) How many words can be formed using all the letters in MISSISSIPPI?
CLASS
10 READ N.K
20 G0SUB 500
30 LET Cl=C
32 REM Cl ST0RES THE 1'dUMBER 0F' C0MMITTEES 0F' WHICH
33 REM Y0U ARE A MEMBER
40 READ N, R
50 G0SUB 500
60 LET P=C!/C
70 PRINT "THE PR0BABILI TY THAT Y0U GET 0N A 4 MEMBER"
75 PRINT "C0MMI TTEE F'R0M A CLASS 0F 29 IS": P
1:10 STeP
490 KEM F'Ii\ID C0MBINATI0NS 0F' N THINGS TAKEN R AT A TIME
500 LET C= I
510 F'0ri X=N T0 N-R+I STEP -1
!>20 LET C=C•X
530 NEKT X
540 F'0R Y=R T0 I STEP -I
550 LET C=C/Y
560 NEXT Y
570 RETURN
600 DATA 26.3
610 DATA 2914
RUN
CLASS
OONE
Elementary Probability 205
an<l the probability that at least two people have the same birthdate is 1 - P.
We can write a short program to compute 1 - P. See BIRTH.
The chances are about 71%, which is much higher than many people would
BI kTH
10 LET r=l
cO FOk 0=365 TO 336 STEP -1
30 LET P=P*D/365
40 NEXT D
SO LET '.!= 1-P
1)0 PidNT "THE PrWBA':llLITY OF n:o 0." MOf,E"
70 i"klNT "IDC,J!JCAL 9lrHHDAlF.:S A 1 ~0NG 30 PF.:OPLF.: IS";O
8D E~~ D
i<UN
BI 1-<TH
DONE
206 Basic BASIC
guess before doing the problem. Note that this is not the probability that
someone else in the room has the same birthday that you have. That problem
is left as an exercise.
SUMMARY
We have initially defined probability as the quotient of the number of ways
to constitute success and the total number of possible outcomes for equally
likely outcomes. We see that this can easily be applied to situations of enumer-
ation. Independent events which have individual probabilities P and Q occur
together with a probability of P 0 Q. This produces an extended definition of
probability which does not always require enumeration, but requires only that
we know individual probabilities for successive events.
FLIP
5 LET C=O
10 FOK X=l TO 50
20 LET F=I\ITC2*>lNDCl ll
30 IF F= 1 T'"EN 60
40 P.<I\IT "T"J
50 GQH3 100
58 REM C COUNTS THE 'JU:~f3ER CJF YEADS
60 LET C=C+l
70 PRINT "H"J
I 00 NEXT X
110 PRINT
120 P:<!NT "rlEADS ";C;"OIJT- 'JF 50 FLIPS"
1 30 END
RU~
FLIP
Let us begin by having the computer flip a coin 50 times. See program FLIP.
We get 23 heads out of 50 flips. One of the intriguing things about flipping
a coin many times is that we do not get heads for half of the flips for each
experiment. In fact, it is possible to flip a coin 50 times and get no heads or
to get all heads. Of course the probability of all heads or no heads is very
small compared to the probability of half heads. We will be able to compute
those probabilities in the next section. For now we are concentrating on
simulation.
In many ways, flipping a coin 50 times is the same as flipping 50 coins once.
Let us put program FLIP in a loop to perform the experiment 10 times to
see a range of results. Sec FLIP-1.
We get a range of 17 to 34 heads for this RUN of the program, and it
turned out this time that none of the trials came out 25 heads.
One of the nice features of simulation by computer is that we can have the
computer perform hundreds or thousands of trials of an experiment that might
take days to do with physical apparatus.
FLIP- 1
2 F<lk Y= 1 TJ 10
S LET C=O
10 F0R X=l T0 50
20 LET F=INTC2*RNDCl ll
30 IF F= 1 THE.\! 60
40 PKli'JT "T";
50 G0T0 100
S8 REM C C<JUNTS THE NUMBER 0F HEADS
60 LET C=C+ 1
70 PtHN f "H"J
100 .~EXT X
110 PRINT
120 PRINT "HEADS "JCJ"OUT ·JF 50 FLIPS"
!2~ .~EXT Y
130 END
208 Basic BASIC
HUN
Ji'l...IP- 1
HTTTTTHTTHHTTHTTTTTTTTHTHHHHHHTTTTTHTHHHTHHHTTHHTT
HEADS 21 0UT 0F SO FLIPS
HTTHTHHTTHTTHTHHTTTHHHTTTTTTHHHHTTHTHTHHHHTHHTTHHH
HEADS 26 0UT 0F SO Ji'l...IPS
HTHTTTHTTHTTHTTTTTTTTHTTHTTTHTHTHTTTHTTTTTHHHTHHHT
HEADS 17 0UT 0F SO FLIPS
TH TTTTTTHTHTHTHtHHHHHHHTHTTTHHTTHTTTTTTTHH TH TTHHTT
HEADS 21 0UT 0F SO FLIPS
TTHHTTTTHTHHTTHTHTHHHHTTHHTHHHTTTTTTHTHHHHTTHTHHTT
HEADS 24 0UT 0F SO F1..IPS
HTHTHHHHHHHTHTTTTTTTHTTHHHHHHTTTHTTTTHTHTTHHTHHTHH
HEADS 26 0UT 0F SO FLIPS
HTTTTTHTTTTHHTTHTTHTHHHHTHTHHTTHHHHTHTTHTHTHTTTTHT
HEADS 22 0UT 0F SO FLIPS
THTHHHHHHTHTHHHHTTTHHTHTHHHHHTHHHTHHTHHHHHHHTTTHTH
HEADS 34 0UT 0F SO FLIPS r
HTTHHTHHTTHTTTTTTHHHTTTHTTTHHTTHTHTHHHHHTTHHTHHTHT
HEADS 24 0UT 0F SO FLIPS
TTHHTHHTHHHTHTTHTHHH THHHTTTTHHH TTTTH TTHTHH THH THTHT
HEADS 26 0UT 0F SO FLIPS
OONE
Let us set up an experiment to roll six dice 1000 times, counting the num-
ber of times 1 comes up for each roll of six dice. The possibilities are from
zero to six. Then let us count the number of times each of those seven numbers
occurs. \Ve can keep track of all seven totals in a 1 by 7 row vector. We will
count the number of times no l's come up in column 1 and the number of
times one 1 comes up in column 2, etc. See the flowchart in Fig. 13-2 and
program R0LL.
R0LL
DflNE
Elementary Probability 209
Start
Initialize
vector at 0
FcpR X= 1
T<,2) 1000
LET C = 0
Set counter i--~~F¢RR=1T¢61--~-1111 LET U =INT (6
Roll 6 dice •RND(1)+1)
at O
Enter C into
NEXT X
vector
END
Fig. 13-2. Flowchart for rolling six dice 1000 times.
times both were defective, the number of times the light bulb was defective, and
the number of times the can opener was defective.
5) A regular tetrahedron has four equilateral triangles as faces. Let an experi-
ment consist of numbering one face 1 and the remaining faces 2, and tossing the
tetrahedron into the air to determine which number faces down. Write a program
to toss the tetrahedron 500 times and count the number of times the 1 faces down.
6) Roll a die 500 times. Count the number of times the 1 or the 5 comes up.
7) Roll a die and toss the tetrahedron of problem 5) 1000 times. Count the
number of times both come out 1 and count the number of times both come out 2.
8) An experiment consists of rolling a die until a 1 comes up. Write a program
to perform the experiment 500 times. Count the number of rolls for each experiment.
9) An experiment consists of flipping a coin untii it comes up heads. Write a
program to perform the experiment 1500 times and count the number of flips
required for each. Print the distribution.
10) Roll 10 dice 500 times. Count the number of l's that come up for each roll.
Print the distribution.
11) Suppose 10% of the population is left handed. Write a program to pick groups
of 10 people at random. Count the number of left-handed people. Print the
distribution.
12) Pro;ect: Write a program to make the computer the dealer in a game of 21.
not come up 1 and the green die come up 1. The probability of this is
(5/6) 0 (l/6). Now, if we roll the two dice, the probability that we get
0
exactly one 1 is the sum of the above two possibilities, or (5/6) (1/6)
+ (1/6) (5/6)· Or we can say that the probability
0 of exactly one 1 is two
times the probability of getting a 1 on the green die and not a 1 on the red die
which is 2 ° (5/6) 0 (1/6).
Now suppose we roll four dice colored red, green, blue, and white. What is
the probability that we get exactly two l's? The probability that the red and
0
the green dice are l's and the blue and white are not is (1/6) (1/6) (5/6)
0
0
(5/6). But we might get the l's on the green and blue with the same proba-
bility, or we might get l's on the red and white dice with the same probability.
In fact, there are 4 C~ ways that we could select two dice from the four to come
up with l's. Each selection has probability of (1/6) (1/6) (5/6) (5/6) .
0 0 0
DICE
10 READ R
15 LET C=I
20 r0R X=IO T0 10-R+I STEP -I
30 LET C=C•X/ CIO-X+ I)
40 NEXT X
50 LET P=C•Cl /6>•R•C 5/6)tCI O-R>
60 PRINT P
65 DATA 2
70 END
RUN
DICE
·29071
D0NE
Program DICE is for exactly two I's. What about the other possible
num-
bers of l's? ·with just a few changes in program DICE, we can
answer that
=
question. Instead of comput ing for R 2 only, we can let R
go from 0 to
10 in a F0R-N EXT loop. This can be done by changin g only lines
10 and 65
in DICE. See DICE-I .
In DICE- I we have defined 11 events that cover all possible outcom
es in
this experiment. There can be no outcomes that do not give from
0 to 10 l's.
It is also true that no two of the events have any outcomes in common
. Events
which do not have any outcomes in common are called mutuall
y exclusive
DICE-I
5 PRINT "0NES PR0BAB ILITY"
10 r0R R=O T0 10
15 LET C= I
20 r0R X=IO T0 10-R+I STEP -I
30 LET C=C•X/ CIO-X+ I)
40 NEXT X
50 LET P=C•Cl /6)•R•C 5/6)tCI O-R>
60 PRINT RIP
65 NEXT R
70 END
RUN
DICE-I
0NES PR0BABILITY
0 • 161506
I ·323011
2 ·29071
3 ·155045
4 5.42659 E-02
5 1·30238 E-02
6 2·17063 E-03
7 2·48072 E-04
8 1·86054 E-05
9 8.26908 E-07
10 1·65382 E-08
D0NE
212 Basic BASIC
events. If we have a set of mutually exclusive events that also cover all pos-
sible outcomes, then the sum of the individual probabilities must total 1. We
can verify that the sum of the probabilities in DICE-1 is in fact 1. Of course
one way to do that would be to rewrite the program to total the probabilities in
DICE-1.
Suppose we know that 10% of a certain population is left handed. If we
select 100 people at random, what is the probability that exactly 10 of them
will be left handed? The probability that a particular set of 10 people will be
left handed will be (1/10) 10 0 (9/10) 9µ and from 100 people there are 100C 10
ways that 10 of them can be left handed. So the probability is
p = iooC10 " (1/10)10 " (9/10)90
This too, can be done with a short program. See LEFT.
LEF'T
10 LET C=I
20 F'0R X=IOO T0 100-10+1 STEP -I
30 LET C=C•X/CIOO-X+I>
40 NEXT X
SO LET P=C•<·l>•I0*<·9>•9 0
60 PRINT P
70 END
RUN
LEF'T
o 131865
OONE
Experiments that behave in this way are called binomial experiments because
the values of NCR 0 pu 0 QN - I t are the terms of the expansion of the binomial
(P + Q) raised to the Nth power.
Binomial Theorem
Looking at (P + Q)N, we should be able to see the general term in the
product. (P + Q)N means, write (P + Q) as a factor N times. So
(P + Q )N = (P + Q )(P + Q )(P + Q) · · · (P + Q)
When we multiply this out, we are actually taking one term from each factor
in such a way that we can sum up all possible products of combinations of N
factors one from each (P + Q) factor. How many factors are there in the
product? There is one term that takes P as a factor N times. There is one term
that takes P as a factor N - 1 times and Q as a factor once. There is also a
Elementary Probability 213
term that takes P as a factor N - 2 times and Q as a factor twice, etc., down
to the term that takes Q as a factor N times. That makes N + 1 terms. Now,
for a particular term, say P3QN- 3, we want three P's and N minus three Q's·
We can select three P's from among N terms in NC 3 ways and so the value
of this term is :-;C 3 " P 3 QN-3. For the Rth term we get NCn" PHQN-n, which
is exactly what we get for a probability of R successes in N trials where the
probability of success on a single trial is P and the probability of failure on a
single trial is Q. So to find (P + Q)N we simply evaluate NCn "PHQN-n for
all values of R from 0 to N.
Taking a look at the probability of any binomial experiment, we see that
=
since P + Q 1 and the sum of all :-;Cn" PHQx-n terms is (P + Q)X, we get
if P+Q=l then
which can be verified by summing up the probabilities in program DICE-I.
+
Finally, if we look at (X Y)X for X and Y both equal to 1, we get the
general term in the expansion to be :-;Cnl 1tIN-n, which is the same as NCn,
so that the numerical coefficients of any binomial expansion are simply the
correspondin g values of NCn. Since the values of X and Y are both 1, we are
really finding the value of 2N if we sum up all of the coefficients. Let us
write a program to print the coefficients for values of N from 0 to 11. See
program PASCAL.
You may recognize these numbers as Pascal's Triangle which has many
interesting properties. Problem 9) in Sec. 11-1 and problem 7) in Sec. 5-3
should also have given the results of program PASCAL.
PASCAL
10 F'0R N=O T0 11
20 F'0R R=O 1'0 N
30 LET C=I
40 F'0R X=N T0 N-R+I STEP -I
so LET C=C•XICN-X+ I>
60 NEXT X
70 PRINT CJ
80 NEXT R
90 PRINT
100 NEXT N
110 END
RUN
PASCAL
1
2 1
3 3 1
4 6 4 I
s 10 10 s I
6 IS 20 lS 6 I
7 21 3S 3S 21 7 I
8 28 S6 70 S6 28 8 I
9 36 84 126 126 84 36 9 1
10 4S 120 210 2S2 210 120 4S 10 1
11 SS 16S 330 462 462 330 16S SS II
00NE
214 Basic BASIC
215
2.16 Basic BASIC
LINE FEED keys at the end of each line. (There may be a special key for
RETURN.)
If you make a typing error, you may correct it in one of several ways. If
it is the previous character or within a few characters, depress the backspace
button on the tape punch apparatus once for each character you wish to
erase. Then touch the RUBOUT key once for each backspace. The RUBOUT
key punches out all holes in the row and will be ignored by the computer
and will not print. Alternatively, you may depress the SHIFT key and the
0 key once for each character you wish to erase. A backwards arrow will be
printed for each correction. Spaces do count as characters for this purpose. If
the entire iine is a iost cause, simply RETURN, LINE FEED, and begin
typing from the beginning including the line number. After you have finished
typing, touch RETURN and LINE FEED. Then generate blank tape as you
did before typing your program. The idea is to get some paper that is not
filled with holes so that you may write some kind of identification on the
tape. As soon as you have your second tape, the need for this will become
obvious. After tape preparation is completed, tear it off and roll it up. It is
suggested that you not roll it less than about 2 in. in diameter, as the tape
will take on a permanent curl and that may cause trouble in the reader later.
With the terminal on-line and the previous program erased, you are ready
to enter your program into the computer via the tape reader. Again computers
vary, but most require a system command, and for some the command is
TAPE followed by turning the tape reader on. After the tape is read in,
remove your tape from the reader and roll it up again. If the computer
requires a system command to enter tape mode, then a second command will
be required to remove it from that mode. The command KEY removes the
computer from tape mode ancr prepares it for instructions from the keyboard.
The command RUN usually will serve the same purpose.
At this time the computer takes your entire program and compiles it. To
compile a program means to put all instructions in order and convert it to a
form that the computer uses to actually perform the instructions. In order to
run, all statements must be legal BASIC statements and the entire program
must fit certain requirements that vary depending on the computer.
At this point, you should read all of Appendix B and then return and finish
this appendix.
of the necessary changes or after it becomes clear that you cannot make all of
the necessary corrections in a reasonable length of time at the keyboard, you
may want a new tape of the program in its latest form. Here again, time-
Storing Programs on Paper Tape 217
share systems will vary, but you will have a way of getting the computer to
punch your program. Some will automatically provide blank leading and
trailing tape, others will require you to use the method you used when you
typed off-line. Two of the system commands in use are PUNCH and LIST-
N0-HEADER.
Now you have two tapes for the same program. Most likely you will want
to throw the old one away. Be sure to write some identification on the new tape.
APPENDIX B
Error Diagnosis
B-1 INTRODUCTION
From the time you type the last line of your program to the completion
of a successful RUN, there are three types of errors that may show up: 1) those
errors that prevent a RUN; 2) those errors that allow a RUN to begin but
cause it to terminate during execution; and 3) those that permit a complete
RUN, but cause the computer to produce an unexpected or incorrect result
The whole process of taking a program that does not work and turning it into
one that does is called debugging. Let us look at the errors by type.
10 LT~ET X=5
20 PRllJNT X
30 ENO
RUN
Nill STATEMENT TYPE F"llJUNO IN LINE 20
The exact wording will vary from computer to computer, but the message
is clear. We retype line 20 as in the following:
20 PRINT X
RUN
OllJNE
Even though BASIC does use English words, you may not get too conversa-
tional as in the following:
218
Error Diagnosis 219
10 READ X AND Y
20 PRINT X1 Y
30 DATA 41S171-ll
40 G0 T0 10
SO END
RUN
CHARACTERS ArTER STATEMENT END IN LINE 10
10 READ X1Y
RUN
4 s
7 -11
10 LET X=S
20 LET Y=IO
30 LET Z=XY
40 PRINT Z
SO END
RUN
CHARACTERS ArTER STATEMENT END IN LINE 30
30 LET Z=X*Y
RUN
so
D0NE
Let us put many errors in one program and see what happens.
10 LET X= S
20 READ Y • Z
30 LER W=Y* Z
40 PRINT X
SO LET A*B=X
60 READ W
70 G0T0 9010
80 LET S=<AX+B>*<CX+D>
90 LET Al
100 G0 BACK T0 10
110 LET X=3*4+S>
220 Basic BASIC
Now let us see what we can do to fix the program so it will run. First v
would like to see what is left of our program. The system command LIST w1
do that for us.
LIST
10 LET X=5
20 READ y,z
40 PRINT X
60 READ W
70 G0T0 9010
120 DATA 5.11.25.-5
130 PRINT Wt2
140 G0Hl 60
The computer automatically wiped out all of the statements that did n<
conform to BASIC requirements. So what remains ought to run, right? Wroni
The computer has only eliminated the errors that are self-contained in sing]
statements. These are sometimes called syntax errors or errors of form. Th
computer has not yet looked to see if the statements fit together into a set c
executable statements. To achieve that we type RUN again:
RUN
UNDEFINED STATEMENT REFERENCE IN LINE 70
We can easily see that the compute1 cannot G0T0 9010 as there is 1
line 9010 in the program. We take line 70 out by typing 70 followed
RETURN and try again.
70
RUN
LAST STATEMENT N0T 'END' IN LINE 140
9999 END
Rl_ll\1
5
625
25
Now we have gotten something printed, but the program is all over the
place. We can assemble it again with the LIST command or we may use a
new command first. We can get nice numbering by the command RENUM-
BER. Some computers use EDIT RESEQUENCE. We RENUMBER here and
LIST the program in its present form.
RENUMBER
LIST
10 LET X=5
20 READ y,z
30 PRINT X
40 READ W
50 DATA 5, 11•25, - 5
60 PRINT Wt2
70 G0T0 40
80 END
RUN
5
625
25
Not al! computers will print the out of data message, but they will termi-
nate after the last item of data is read if the program sends it back to READ
again.
You might instruct the computer to perform an illegal operation as follows:
A 8 A-8 AtB
I 2 - I J
5 3 2 J25
3. 4J 2 1. 41 JI. 628 J
1.23 4 -2.77 2·28887
0 0 0
LER0 T0 LER0 P0WER IN LINE 30
Zero to the zero power is not defined. So the computer notifies you that it
has come to this undefined condition and halts execution awaiting your pro-
gram change. There are many more errors that will halt execution, but these
examples should demonstrate the principle involved.
J5 LET X=5
25 LET Y=JO
30 LET Z=Xt2
40 LET W=Yt 2
70 END
RUN
D0NE
We got three blank lines and that is not what we wrote the program to do.
It is reasonably obvious that we forgot to put in any PRINT statements.
50 PRINT X;Y;Z;W
JO PRINT "X Y Xt2 Yt2"
l<llN
x y Xt2 Yt2
5 10 25 JOO
D0NE
Error Diagnosis 223
If you think that you have the PRINT statements and do not get any
printed results, look for a G0T0 that causes the computer to bypass the
PRINT statements.
The ways in which programs can give incorrect results are unlimited. And
to make matters worse, the computer has no way of determining that the
result is correct. This is the responsibility of the p10grammer. Consider the
following program to read pairs of numbers and print their sum and their
product and the number of pairs:
A 8 SUM PROD
10 20 30 200 PAIRS S•~ F'AR
A 8 SUM PROD
11 9 20 99 PAIRS S0 F'AR
A 8 SUM PROD
2 3 2 PAI RS S0 F'AR
A 8 SUM PROD
-45 18 -27 -810 PAIRS S0 F'AR
A 8 SUM PROD
We certainly do not need to have the headings printed more than once.
So we want to change the G0T0 in line 60 as follows:
60 GLH020
RU.\I
A 8 SUM PROD
10 20 30 200 PAIRS S0 F'AR
11 9 20 99 PAIRS S0 F'AR
I 2 3 2 PAIRS S0 F'AR
-4:; 18 -2 '7 -810 PAIRS S0 F'AR
But now we still have to find out why the computer prints 1 for the number
of pairs each time. Line 40 is LET C ::: C + 1. C must be 0 each time the
computer comes to line 40. This is because the G0T0 sends the computer
to line 20 which is LET C ::: 0. So we change line 60 again. This time we want
the computer to go only to the READ statement in line 30.
224 Basic BASIC
60 G0Hl 30
KtJN
A B SUM PiWi)
10 20 30 200 I PAI RS S0 l'AR
11 9 20 99 2 PAIRS S<I FAR
1 2 3 2 3 PAIRS S0 FAR
-45 18 -27 -BIO 4 PAI RS S<J FAR
SUMMARY
We begin to see some of the types of errors and the way in which they
affect the running of our program. There are errors of language or syntax
errors. There are errors that prevent execution such as G0T0 870 when there
is no line 870 in the program. Some errors do not affect the computer until it
tries to evaluate an expression that calls for an undefined condition. And
finally we have seen some errors that give incorrect results. As we are making
changes in programs we may find the LIST command helpful to see the
program in its present form. As we change programs, the line numbers may
become very scattered or very close together. The command RENUMBER or
EDIT RESEQUENCE makes 10 the first line number and the intervals 10.
APPENDIX C
C-1 TAB(X)
The TAB(X) function is available on many BASIC systems. The TAB(
function numbers the spaces across the terminal carriage and uses this number
to designate where to print. XXX PRINT TAB (10); "HELL0 THERE" is
an instruction to the computer to skip out to the space whose number is 10
and begin printing there. See program HELL0.
HELL0
HELL0 THEf<E
HELLiil THERE
OiilNE
Some systems call the leftmost space zern an<l others call it one. Some
systems treat the argument of the TAB( ) function mod 72, so that
TAB(lOO) and TAB(28) mean the same thing. Others use mod 75, in which
case TAB(lOO) means the same as TAB(25).
There may be several TAB( )'s in the same PRINT instruction. The argu-
ment of TAB( ) may be a variable as in program TAB(l)·
TAl::!C 1)
IQ LET X=lO
20 Pf<INT TABCX>JXJTABCX+10>JX+10JTABCX+25)JX+25
30 ENO
f<UN
TABC I>
10 20 35
DiilNE
225
226 Basic BASIC
Notice that TAB( ) counts from the left margin every time it appears, not
from the previous printed character·
We may use the TAB( ) function to make the formatting a little simpler
in program BANK2 in Sec. 4-3. Turn back to that program and look at lines
132, 138, 156, 158, and 500 through 530. All of these lines were required
to achieve flexible format. We may eliminate all of these as well as line 490
and replace line 140 with 140 PRINT TAB(X); "$";FNH(P) and replace line
160 with 160 PRINT TAB(X);"$";FNH(Pl). Now all we need to take care of
is a value for X. Let us try 35 the first time through. Anywhere before line
=
:140 we may insert ZZZ LET X 35 and call for a RUN.
530
520
510
500
490
15B
156
138
132
140 PRINT TABCIOJ "$"J FNHCP>
160 PRINT TABCXH"$";FNHCPI>
135 LET X=35
RUN
BANK2
The printed results are aligned nicely. Now let us list the new program in
full with the value of X at 31 and call for a final RUN.
BANK2
2 DEF FNHCX>=INTCX*I00+.5)/100
10 LET P=Pl=S6.31
20 FOR Y=l TO 10
22 REM F0R TEN YEARS
30 F0R M=l T0 12
32 REM C0MP0UND M0NTHLY AND COMPUTE INTEREST
40 LET I=P*4.S/100/12
SO LET P=P+I
60 NEXT M
62 REM THAT FIGURES THE INTEREST FOR THIS YEAR C0MP0ll·\JDED
M::JNTHLY
70 FOR Q= 1 T0 4
72 REM C0MP0UND QUARTERLY
BO LET Il=Pl*4·75/I00/4
90 LET Pl=Pl+I 1
100 NEXT Q
102 REM THAT TAKES CARE 0F THE QUARTERLY INVESTMENT FOR THIS
YEAR
108 REM NOW TO COMPUTE THE NEXT YEAR
110 NEXT Y
120 PRINT "F0R TEN YEARS"
130 PRINT "@4. 5% C0MP0UNDED M0NTHLY· •• "J
135 LET X=31
Special Formatting Functions 227
D0NE
to achieve the results of the last RUN above. The IMAGE statement begins
with a colon and the number signs specify the locations of the digits in the
numerical results. In an IMAGE statement, you may specify the location of the
decimal point and the number of digits on either side with the number of
number signs. The IMAGE statement may specify printing for several num-
bers by having several sets of number signs. You may also specify E-format
by following the number signs with four exclamation marks (... ##!!!!). In
our problem above, if we decide to change the location of the printed results,
we simply retype lines 140 and 160.
APPENDIX D
228
APPENDIX E
Summary of Statements in
BASIC
NOTE: Not all statements which appeal' in this appendix will nm on all systems,
and the list here docs not cover every statement from some systems.
229
230 Basic BASIC
MATRIX INSTRUCTIONS
MAT READA Enters data into the array named A. Several arrays
can be read in the same MAT READ statement by
rn.,.-.01••'\f.;,,,-r +'ha ri1•1"•'\1r ",,,..,,"Y'IOC' ,'lr;fl, nrVn'"\1..,onC'
........... 1......................... b ....... ....., ............... ) ............................. ..... ...,...._, .................... .
~
MAT PRINT A Prints the array named A with comma spacing. Sev-
eral arrays may be printed with the same MAT
PRINT statement by separating array names with
a comma or a semicolon. The delimiter specifies
the spacing for the preceding array.
Summary of Statements in BASIC 231
MAT INPUT Enters data into an array from the keyboard (not dis-
cussed in text).
MAT C =Ao B Enters the product of A and B into array C.
MATA=B + C Enters the sum of Band C into array A.
MAT A= B-C Enters the difference of B and C into array A.
MAT A= (K)oB Multiplies each entry of B by the scalar K and enters
the result into A.
MATA= ZER Creates the zero matrix (fills each entry of A with
zero). ZER may be followed by redimensioning
specifications in parentheses.
MAT A= C0N Fills each element of A with 1. C0N may be followed
by redimensioning specifications in parentheses.
MATE=IDN Forms the identity matrix E. E must be square. All
elements with equal row and column numbers are
1 and all other elements are 0. IDN may be fol-
lowed by redimensioning specifications in paren-
theses.
MAT X = INV(A) Finds the inverse of A (if it exists) and enters it in X.
MAT A = TRN(B) Fills A with the transpose of B.
FUNCTIONS
FILES
Hewlett-Packard Files
ASCII Files
READ #N, Reads data from the Nth file named in the program
into the variables of the variable list following the
comma.
WRITE #N, Writes data from the variable list following the
comma to the file. The variables in the list may
be separated by semicolons or commas to achieve
corresponding spacing in the file.
IF M0RE #N Determines whether or not there is more data in the
file.
IF END #N Determines whether or not the end of the file has
been reached.
APPEND #N Allows additional data to be written to an existing
file by setting the file pointer to the end of the Nth
file and placing the file in the WRITE mode.
SCRATCH #N Sets the pointer of the Nth file to the beginning of
the file, erases the file, and places it in write mode.
RESTORE #N Sets the pointer of the Nth file to the beginning of
the file and places it in the READ mode.
235
236 Basic BASIC
Answers to
Even-Numbered Problems
N)
>!>. APPEND IX G
c:;,
Each two-page spread should be read from top to bottom as one individual page.
CHAP. 1
Problem No. 2 Problem No. 16(dl Problem No. 12
Problem No. 4
DONE
110
RW
END
/
N0.12
"'"'
6
10
IS
20
LET N"S
LET T::iO
LET S=O
JO
JI
32
REH
REH
REH
G KEE:PS TRACK OF' THE NU11BER OF' GlF'TS
0 IS THE DAY NUMBER
T TOTAl.S THE NU11BER OF' GlF'TS ON A GI VEN DAY
1.oooooE•06 JO LE"T S:iS+N 40 I.ET T•O
JS LET T"T• I SO I.ET T•T+I
DONE 40 JF"N:IJ91THEN70 60 I.ET G•G+T
SQ LET N"N+2 70 lF' T,.O THEN BO
60 GOTO JO 75 GOTO 50
Problem No. 8 70 PRlNT "S•JM DF' DOD NUMBErtS FROM 5 TC 1191 15"1 S BO LET O•O• I
7S Pi'!INT "THE NUMBEH OF NU1'1BERS SUMMED IS"JT 90 IF' D <• 12 THEN 40
10 P~INT 2/"J 90 ENO 100 PRINT "T0TAI.. Nl.1'4BER OF' GIF'TS IS"J G
?.O ENO RUN 120 ENO
RUN 'l(!,4 RUN
8 NO. l-4
su;.i OF' GOD NUMBE~S F',tQH s TO 1191 IS JSS212·
·666667 THE NUMBER OF' NUMBERS Sl..1'1HEO IS 594 TOTAi.. NU'1BER OF' GI f"TS JS J6-4
Problem No. 12
200
RUN
NO•b
END
.,.,
200
N0· 16
EHD
"12"
.1
9.09091£-02
Problem No. 16la) a. JJJJJE·02
DONE
10
20
N=l/2+1/J
D=l/J-l/4 "
I•
1·692JIE-02
7o l4286E-02
Problem No. 4
JO N/O
"" 6o66667E-02
.0625 10 FO<l: ;w;;:\ TC 25
..,
5.BB2J5E-02
HUN
" 20 P1HNT X, \/;>';
""
IOA 5.55556E-02 JO NE;>";T ;w;
5.26ll6E-02 40 L'10
20
OCNE
21
22
0047619
4o54545E-02
""'
2J 4ol47BJE-02
2• 4·16667E-02
Problem No. 16lb) 2'
.JJJJJJ
10 LET A:t2/Jl/C5/6J .~5
QONE
20
JO
LET 8:(J/4l/C2/Jl
PRINT A•B ·'o\66667
40 END ·142'157
R'-" .125
·111111
Problem No. 10 10 ·I
9.09()91£-1)2
l ·925
5 REM THIS 15 N0. )0
12 " 'l.JJJJJE-02
""
DONE 7.692.31£-02
20 DATA 2,.35,3,2.65.11,.25,1.9.4 9,J5,1.59,o,o
7o l4266E-1)2
25 LET T=O
Problem No. 16(c) JO
40
AEAO N, P
IF N:O THEN 100
"" bo66667£-IJ?.
~ •)6 ~5
"""
10 L.ET A"'( l/2•J/7J/C2ol• J-1 l s.sa2J5E-o?.
50 LET T:T+\
20 L.ET 8:C(MJ·J•2l/Sl/Cll/4l 5055556£-02
60 G0TO JO
JO PRINT A•8 5·"6316£-02
100 PRINT "THE NUMBER OF DIFFERENT ITEMS IN THE ORDER IS"IT
40
RUN
END 110
RU'
END
?.O
21 ·"
00476!9
4,545115£-02
NO. ID
4.)47'13£-0"!
-J.J261J 4o\6.;67E-O?.
THE NUMBER OF DI FF"ERENT ITEMS IN THE ORDER
"""°
IS 5 2'
DONE DONE
DONE
"""'
.... eeo 1'8R J•I T8 A
....
999 IS THE LARGEST AHO JS IN POSITION 9 ...
230
e•o
260
PRINT TCJJJ
NEXT J
DATA 6,, 11,, 1s. n,,e6.83,, 1 s. l9•2l· 831 91
[NO
""'
20
JO
40
so
60
f"OR X,. I TO I 000
LET S:oS•t/X
NEXT X
P~lNT
EN!J
"SUM or ttf.Cl~dCCA-'i f'riC'1 i TO 10-J) IS.. •S
Problem No. 1O
S LET l•O
10 ,.OR R•I TO S
20 F'O:l C•I TO 10
Nll-8
P'IRST LIST 6
SEClllHD LIST J5
6 11 15
II
19
15
03
••
91
19
03
., 91
""'
N0•6 25 LE.T l"l•I
JJ PUNT II
DONE
"
IB 19 20 60 F'OR K•I TO J
..
10 F'Ori 0,..1 TO 4 II 13 16
20 LtT l•oOl•P 21 22 " 23 2' 25 26 27 U U H 10 LET S•S•l.CKJ
JIJ LS::T P"P•I 31
., ., "
32 33
"" 36 37 39 39 •0 80 NEXT K
90 LET MtJJaS
...
49 .. 9 50
40
SO
60
NEXT 0
PiHNT "AMOIJNT AF'TF:R ONE YEAR lS"H'
ENO
"
DONE
" 100 PRINT H[JJI
110 NEXT J
"'' .....
1eo
NOoB
O:JNE:
Problem No. 12
10
20
JO
40
PrllNT "50'J.\q£"J
F'Ott X•O TO 9
l'r!INT XJ
NEXT )(
.... .
NO•IO
I 9 . •• 36 . .. Bl 100
45 PdlNT
Problem No. 10 SQ PIHNT
Problem No. 12
10 r(~el.0 !,) 70 ro~ L•O TO 20 STE" 10 10 P'OR X•I T8 6
i:?U lF 020 THi:N 110 75 PntNT L; ea READ P'CXJ
JO L'.:T S11Q ao f"Orl Cao TC 9 30 PRltlT F'CXJJ
40 FO~ X11 1 TO 0 90 PrilNT tL•CH2J -"O NEXT X
51J L~T SsS•X JOG NE:XT C .. , PRINT
60 N::xr x 105 PttlNT 50 F'OR Y•l TO 6
70 PMJNT "S'.N Ui" TO"JOJ"Dll.fS IS"IS 106 P.-HNT 60 READ SCYl
90 GOTO 10 110 NEXT L 70 PRINT SCYU
10') OATA 1~~Jo.o 13') ENO 80 NEXT Y
110 ENO 1WN 85 PRINT
HUN NOo 12 90 P'OR I•1 re 6
NC.JO SOUA>{£ 0 100 LET AtIJ•F'Cll•StJl
110 PRINT ACllJ
SU;ot urr TO 12 DAYS IS 78 120 NEXT I
SUl4 UP TO JO DAYS JS 465 130 DATA 6•h3•7•2•9•8,,2•3•9•7••
l):lNt
10
20
IOJ
400
121
441
144
46..
169
529
196
576
2::!5
625
256
676
2a9
729
J2..
784
J61
.....
1-"0
Ne.1e
END
SEC. 3·2 6
OONt
Problem No. 2
SEC. 3-3
'..a
DONE
2 9 63 I.. :Jti
10 ro~ ("I TG
?.O F"Grl G= I TO I~ Problem No. 2 SEC. 3-4
J'.) P·l!.'H (•Cl
41J N;::xr c 10 FOi'l I•t TO 10
~O PMlillT 20 LET t..CIJzJt2 Problem No. 2
61) .~EitT .l JO illEXT I
40 f"C:i J"l TO 10 S PRINT "ORIGINAL OROER"'I
7·) 1:"10
50 i"!IINT JJLtJl 10 F'OR X•l TO 5
~ •}.'/
1~0. '=! 60 NEXT J 20 READ LCXl
70 END 25 PRINT LtXlJ
HU.V 30 NEXT X
N0.2 35 PRINT
.-o roR 1111 TO "
50 P'OR J•l TO 5
60 Ir LCIJ o LtJJ THEN 110
70 LET S•Lt I l
16 BO LET LC ll•LCJl
90 LET LtJJ•S
" 110 NEXT J
" a "
120 NEXT I
130 PRINT "l.EAST F'IRST ••1
1'40 PRINT Lt I JI
' 81
10 100 150 FOR U•2 TO 5
160 IP' LCUJ•LtU-1) THEN IBO
DONE 170 PRINT l.CUH
Problem No. 4 180 HEXf U
190 DATA 45•76.•76•<45•98
10 PRINT "F"IRST LIST"J 200 END
]) 20 r;'OR X•I T0 5
I'>;\
"' JO
AO
READ nxl
PRINT r;'CXlJ
RUN
He .. 2
SO NEXT X DRJGIHAL DROER 45 76 •76 45 98
SS PRINT LEAST F'IRST •76 AS 76 98
60 PRINT "SECOND LJST"J DBHE
Problem No. 4 10 F'OR Y• I TS A
80 R£AD SCYl Problem No. 4
5 f>-!ltlT "YEA:".!. P.~JNC!PLE" 90 PRINf SCYlJ
100 NEXT Y 10 L£T A"O
JU L"'.T />=1000
20 F0:-1 Y•>l TO 10
105 PRINT IS LET T•O
JO F~b •.j:! TO 4
110 PRINT "ALL PAIRS" 20 nm X•I TD 6
40 LET ! =P•·0~/"4
120 nm A•I TD s 30 READ S
50 LET f'o:?•) 130 F'BR B•I TO A 3S lF' Sa- t THEN 130
60 NEXT·.!
140 PRINT F"CAlJStBl• 40 PRINT SJ
70 f»UNT YIP
I SO NEXT 8 SO 1..ET T•T•S
flO NF.:ii:T Y
ISS PRINT 60 NEXT X
90 mo 160 NEXf A 10 LET G•T/6
165 DATA 6.A.11.s1.11.s1.12.1 1.16 80 PRINT "AVG ""IG
N0 •. 4
170 ENO 90 LET A"'A+l
.
RUN 100 LET GCAl•G
YEAH N0oo4 120 GOTO IS
PiltNCIPLE
130 F'OR X•I TD A
1050095
11040<19
F'IRST LIST 6 II 51 17 140 L£T HC Xl•X
. .
SECOND LIST SI 150
.
NEXT X
.
I 160o?5 12 II 16
ALL PAIRS 155 LET SAO
1219089
12B2o04
6 51 6 12 6 II 16
160 F'DR Y•I re A-1
IJ4?oJS 51 12 II 16
170 IF G(Yl ,.. GCY•ll THEN C40
II 51 II 12 II II II 160 L£T SlaGCl'l
1415099 16
51 51 51 12 51 II 51 16 190 LET GfYJ•GlY+ll
148fiolJ
17 51 17 12 17 II 17 200 LET G(Y+ll•SI
156Jo94 16
205 LET S2•HCYl
1643-62
OSNE 210 LET HtYl•HCY•ll
DONE 220 LET HCY+ll•S2
2JO LET S11t
Problem No. 6 240 NEXT Y
Problem No. 6 250 IF' S.,1 THEN 155
10 PRJNT "TOTAL COST • S"J
10 PrllNT "ttATE\YEARS''. l.-::?.Jo4 20 DATA 2 •• JS.Ja2o65.11 •• 2s.1,9 • .-9,35,1.5910.-0 252 PRINT
15 P•HNT 25 LET T•O 255 PRINT "GRAD£ AVERAGE"•"tlRIGINAL LOCATleN"
20 F"CR ~"'"' TO 27 LET l•O 260 F'OR S•I TO A
5,5 STEP
25 LET P: 1000 28 LET l•l•I 270 PRINT GCS),HtSl
30 P..tlNT rlo JO READ NCll•P(ll 280 NEll:T S
40 FOR Y=I TO 4 JA Ir NC I l•O THEN '45 290 DATA 65.68.73.as.82.a1
50 F"Dtt Q:I TO 4 '40 LET T•T•Nt I l•PC I l 292 DATA 1.-.81.90,9a.81.8e
60 LET f=P•(,1.1'100>1'4 42 GOTO 28 294 DATA 86.91191192;90.89
70 LET P•f>•I '45 PRINT T 296 DATA 9118J;76•89,79.87
80 NEXT 0 70 ENO 298 DATA 65.16167.SQ,60.66
90 P~JNT ;>, RUN 299 DATA -t
100 NEXT Y NB.6 300 END
120 N£XT H R ...
130 £NO TOTAL COST • S 76.SA
N0o6 DONE ,.
65 68
87
73
90
85
88
82
87
87
88
AVG
AVG
•
•
16·6667
85.6667
88 97
RATEVl'EAN!:i
104006
Problem No. B 91
65
83
76
"
78
67
92
89
50
90
"
60
89
87
66
AVG
AVG
AVG
•
•
.,
91 .. 1667
8Ao5
6A
J08::?oli6 11:n ••w 117?..SR 5 REH USE THREE LISTS r.s.r F'OR F"IRST SECOND ANO THIRD
•·5 1045.77 l09J.62 ll4J.67 1 IY6.0!
10
20
PRINT "rtRST LIST"J
F'OR X•I TO 6
GRADE AVE:RAGE
91.1667
BRIGlNAL L0CATION
3
JO READ F"CXl aS.6667 2
1050.95 110•1.49 1160075 1?.l9.R9 <40 LET TCXl•F'tXl a.-.s
5.5 1056-14 so PRINT nxu 76.6667
1115044 11713:007 l'>;-t<1.~I 60 NEXT X 6•
70 PRINT
OGrH: 80 PRINT "SECOND LIST"J DSNE
90 F'OR Y•I TO 5
Problem No. B 100 READ SCYl Problem No. 6
110 PRINT S(YlJ
10 READ S
120 NEXT Y 5 LET L•O
!JO PRINT 6 LET H•O
20 LET P•l
28 F'BR F•2 Te 10
IAO LET A•6 10 reR X•I TO 1
30 READ N
145 PRINT ,.HUl18ERS IN LIST F' OR LIST S" 20 READ LCXl•HtXl
50 IF S ,.. N THEH 80 150 FOR X•l TO S JO LET L•L•Lt Xl
60 LET S•N
160 F'OR J•t re A 40 L£T H•H+HtXl
70 LET P•F' 170 IF' SCXl•Ttll THEN 210 SO LET RtXJ•HCXl•L[Xl
80 NtxT F' 180 NEXT I 60 NEXT X
"° 90 PRINT SJ,.IS THE LARGEST AND IS lN POSITION,.JP 190 LET A•A+l 10 PRINT "AVG Lew •"L/7
·-·
-109 • 6 a-65"' 27 35
HIGHEST RANGE " JO
ON Ohl NUHBER 7
109 =-654 27
,.
36 GCf'c 9
GCF'• 2
DONE
·I' •-654
• 654
.. -654
.. -654 "
12 20 GCF'• A
1009 DONE
65 71 52 56
SEC. 4-2 SEC. 4-3
AVERAGE ::: 70
6
90
, . ..,
N0.2
110 END 20 F'IJR Y•I TS 25
RUN
N0.2 5
62
,, '<6 • 5 ,, 2•
' :10
"40
F'OR H•l TO 12
LET P•P•S
J • J Sp F'OR O•t TO JO
l6 , •6 • J 60 LET l•P••0"45/J60
10 LET P•P+I
973 • 2 • 1946
278 DONE 60 NEXT D
• 7 .. 1946
90 NEXT H
139 • 1"4 .. 19"46
100 NEXT '(
I • 19"46 • 19"46 l 10 PRINT "S"IJNT<POIOO+o5)o.O t
120 ENO
Problem No. 4
1"43 • 7 .. 1001 10 PRINT .. INCHES •"•"YARDS F'EET INCHES"
""'
91 • 11 " 1001 20 READ I
77 25 IF" l•O THEN 999
s 2778.71
• IJ " 1001
• 1001 • 1001 JO PRINT t.
DONE
"40 LET Y•INT<Il'J6>
SO LET 11•1•36•Y
OON( 60 LET F'•INTCil/12>
10 LET l2•IJ-F'•12 Problem No. 6
SO PRINT Y"'Jl2
90 GOTO 20 10 OEr F'NHOO•lNT!IOO•X•·5 )•.0l
Problem No. 4 100 DATA 20.191.150.60S.el.Jl ·O 20 LET Pl•P2•P3•99
999 £NO 30 F'OR Y•I re 15
5 PRINT "NUMBER"•"ABS0LUTE VALUE" RUN .. o reR H•I re 12
10 READ A 50 LET Pl•Pl+Pl•"4·75/I00/12
20 PRINT A. 60 NEXT K
JO GO SUB I 000 INCHES • YARDS F'E£T INCHES 70 raR Q•I TO ..
20 0 I 80 LET P2•P2+P2•5/I00/4
40 GOTO 10
1000 IF' A "" 0 THEN 1020
'"
150
5
• 0
I '
5 90
100
NEXT Q
FllR O•I TO 36S
1010 LET A•-A
1020 PRINT A 606
" 20
'
6 110
120
LET PJ•PJ+P30"4oS/100/365
IOJO RETURN 63 2 II NEXT 0
1040 DATA 11·3•0,wJ7,13,31.2. -ll.J JI 0 2 1 !JO NEXT Y
1050 ENO 1"40 PRINT " •••S99o00 f'OR 15 YEARSooo"
ROO DaNE 150 PRINT "14.751 COHPOOOOED MONTHLY •• "I
N0.4 160 LET ;o;a5
170 GOSUB 500
180 PRINT FNHCPl >
NU18£R ABSOLUTE VALUE Problem No. 6 190 PRINT "051 COMPOIJoiOEO QUARTER1.Y••"I
11.3 11.J 200 LtT x.. x+ I
0 0 10 READ H.a.DO.N 210 Gosua 500
-17 17 15 PRINT "DOLLARS","HALF' QUARTER DIMES NICKEL PENNIES" 220 PRINT F'NHCP2>
ll IJ 20 R£AD 0 2JO PRINT "0"4.51 COHPOLNOEO DAILYo."J
J1.2 31·2 21 IF D•O THEN 9999 2"40 LET X•X+2
11·1 25 PRINT Q, 250 GOSUB 500
27 LET D•D•IDO 260 PRINT FNHCPJ>
OUT OJ: DATA JN LJNE 10 30 LET Hl•INTCDl'H> 270 STOP
"40 LET 01•0-Hl•H 500 F'OR Z•I TO X
50 LET Ql•INT<Dl/0) 510 PRINT'' "J
60 LET Dl•Dt-01•0 S20 NEXT Z
Problem No. 6 70 LET D2•1NT<Dt/OO> 530 PRINT "S"J
80 LET Ol•Dl-D2•DO 5"40 RETURN
I~
17
PRINT
READ N
ff N2 0 THEN 110
90 LET Nl•INTCDl/N)
100 LET Ol•Dl-NJ•N
110 PRINT HIJOIJD2JNUOI
....
999
N0.6
ENO
120 GOTO 20
20 F'DR 0•2 TO SORCABSCN> >
1010 DATA 50.25.10.s •••S99.00 F'OR 15 YEARS• . .
JO IF li/O•INTCN/0) THEN 70 1020 DATA J.56 •• J5•1•76 1.4. 751 C0HPOUN0£D MONTHLY., s 201.59
40 NEXT D
1021 DATA •Ol•O l~tC8HPOUND£0 QUARTtRLY•• s 208.61
50 PRINT l"•"N":"N
SJ IF' N"'O THEN 60
52 PRINT -1"•"-N'"•"N
.....
9999 END
N9·6
U.51 COHPOOODED DAILY••
DONE
S 19'4o4J
60 GOT0 10
70 PRINT Nl'O"•"D"•"N
DOLLARS HALF' QUARTER DlHES NlCKEl. PENNIES
71 IF' N"'O THEN 75
7J PRINT -N/0"•"-D"•"N
75 IF' D•N THEN 10
.,,
1.s6 3
0
0
I
0
I
I
0
I
0
J.76 J I 0 0
"'CJt°
.i:..
SO GOTO 40
65 DATA 71l.991.-991.45J.-6S4•1009.~I009•900l.O
110 ENO
.01
DONE
0 0 0 0
I
!
2SS LET A•2.0
360 PRINT PCKU
Ni SEC. 4-3 Cont'd 31'0 NEXT I<
270
eas
F'OR Ul•I TO 20
F'OR U• i TO 20
.i:.. 500 DATA 7.6.h0•9•8•1'•1•2a9
300 1' UtuJ•TtUI J THEN ses
o:i Problem No. 8
SOI OATA a.1.o.3.6.5a9.e.o.1
315 NEXT U
REH NO WE COtLD NOT US£ JUST lllO LlSTSOoo•••o
....
S99
330 LET A•A+ I
600 END
10 ou 'NH<C>•e•cre•a•c-1 34S LET UCAl•TtUll
eo PRINT .. x Y" 360 NEXT UI
NO.a
30 FOR x-.6 TO e 37S PRJNT "ELEMENTS Of' EITHER SET"
<40 PRINT XJP'NHCX> 390 F'OR X'"i TO A
SO NEXT X .llOS PRINT UCX)J
60 END 420 NEXT X
1
RU'f 435 PRINT
N£hB DONE PRINT "ELEMENTS WHICH APPEAR IN BOTH LISTS"
450
465 f'OR X•I TO B
x y 480 PRINT l(X]J
Problem No. 1O
-·-·
-6 ea 495 NEXT X
9 500 JF' B•O THEN SIO
10 DEi' f'NRCX)•JNT(XoRNO(l))
-1 SOS PRINT "NO ELEl1£NTS COHHON TO BOTH LISTS"
-3 ·? 20 READ N STOP
SlO
·2 -9 25 Jf' N•O THEN 999
520 STOP
30 F'BR Z•I TO 5
-1 ·7
40 LET A•F'NRUU
s2s LET e•e• 1
0 ·I 540 LET HSl•UtUl
50 LET B•F'NRCN)
I 9
SS LET X•A+B
555 tr U•20 TK[ff 360
e ea 570 GOTO 315
60 PRINT A.. •"B"•
DONE 70
80
G9SUB 700
LET X•A•B
.....
585
N0· 18
END
SEC. 4-4 105 PRINT " 88 -77 54 -85 -9• -.e •76 16 90 76 ·58
110 NEXTZ
115 PRINT 80 96 -•8 51 33 <O •8
Problem No. 2 .,
..
GOTO 20 SECOND LIST
.
120
700 l.ET l•INT<X/N) 28 33 •5 80 -28 16 13 •5 -'9 50 38
S OEf' F'NR<X>•INTC.40l•RNO( 0-200>
710 l.ET W•X-l•N
10 DIM LC25J PRINT W"HOO "JNJ 77 -82 8
100 -96 23 96
720
eo F'BR X•J T0 25 0,.
.. .. ..
730 RETURN ELEMENTS EITHER SET
30 LET Ltxl•F'NR<X> 88 ·77 5• -85 -u •97 -'2 -76 16 90 76 -56
900 DATA S.6,0
....
. -· ..,
40 PRINT LtXJJ ENO
999
50 NEXT X 26 33 80
..
80 96 -49 -•8 51 33 40 •8
60 PRINT
.,
..
N0-10
10 LET 1•26
.. ..
13 50 38 77 -8e
.
-e8
REH WE CAN IMPROVE THE EF'P'ICIENCY BY HAVING THE
"
.
75 • 3 2 HDO
• 3 5
.
2 HOO
REH COl'fPUTER LOCK AT DiE LESS NUMBER EACH TIHE THROUGH
.
76 • l HOO HOO -100 •96 23
0 5 • 1
..
80 l.ET 1•1-1 • 0 5 • 0 0 HOO ELEHE.NTS \HUCH APPEAR IN BOTH LISTS
.
HOO
90 LET S•O
·•9
.
• l 0 HOO 5 • 1 HOO 33 80 96
100 F'OR X•l TO l•I
.
• l 5 •I HOO
..
0 HOO DONE
...
110 IF' LtXJ o LCX+ll THE'I 160
120 LE.T Sl•L(X)
. .
• 5 HOO 6 5 • 5 I HOO Problem No. 20
...
130 LET L(X)Dl.[)(+ll • 2 HOO
• 2 2
140 LET LCX+ll•Sl
150 LET S•I
• s
• 2
0
5
I
HOO
HOO
HDO
''
6
0
5
•
•
5
2 . 0 KBD
HOO
5 PRINT "TIME UOW"•"ADO TlHE"•"TIHE LATER"
10 DEF' f'NTCT>:1lNTCTORNO(l)+IJ
20 F'OR Pool TO 10
160 NEXT X
• 0 3 • 0 0 HOO
170
180
IF' S•I THEN BO
F'OR Y•I TO 25
3 HOO
' 30 LET HsF'NTCl2J
40 LET HooF'NTC S9l
190 PRINT Lt YlJ SO LET Hl:if'NTC36>
DONE 60 LET Ht.:1f'NTCS9)
200 NEXT Y
.....
210
Na.e
ENO
Problem No. 12
70 l.ET H2.:1Ht+H
75 LET H2:1H+HI
80 IF H2c60 THEN 110
90 LET H2,.H2-60
-190 116 -200 -37 169 -52 •31 5 REM SUBTRACT S F'ROH f' HOO 7 100 U:T H.?•H2+ I
106 -151 -79 -117
10 READ r.s 110 If' H2 ca 12 THEN ISO
59 82 161 26 122 -12<1 -130 191 109 IS IF' F•.01 THEN 999 130 LET H:?•H2~12
155
20 l.ET Oaf'-S 140 GBT0 110
•60 30 IF' O..:O THEN I 00 ISO PRINT H"1"~1.Hl"•"Hl•H2"r"H2
-eoo -190 -1s1 -130 -12.e; -111 -79 -60 -se -"41 -37 •31 40 If' Del THEN 60 160 NEXT ?
SO LET D•D•'T 170 ENO
60 PRINT F"'-"S"• "O"HOD 'T"
. ...
II ' 36 36 ' SS ' 31
Problem No. 4 ' '•'
3 27 '
ND.12 ' 20
.,
10
20
30
.oo
50
DIM F'C 18l•GCl8l
LET A•I
l.ET GCll•I
l.ET P'Ctl11F'C2l•I
f'BR X•3 TB 18
-
- 0
- 4
• 2
6 II
:r
11
•
4
4
3
3
HDD
HOO
HOD
HOO
6
'
2
II
'
'
' 23
• 17
36
13
34
12
20
'
•
•
•
'
SS
28
21
2
14
'6
II
2
7
''
'31
..
9
25
DINE
I 13 21 ,.
RUN
N8ol•
... ... 1302 ocr
ocr • 6
80 NEXT R
81 11' AC I J•O THEN 90
82 IF' Al2l•O THEN 90
83 LET P•P•I
,., ,.,
306 1195 GC1' • I
90 GOTO •O
... ...
I 00 PRINT ..CHtNI STRY ... "PHYSICS"• "'RtNCH" ... SPANJSH"•"CALCll.US ..
ocr
Problem No. 6
5 ROI V[ ADO 3"4.563.218•650.986,5•3•612 Tl
665
93
233
11'7
ocr
ocr
ocr
. ,,
• 3
•
•I
!
110 F"eR l•I T8 5
120 PRINT en h
l:!O NEXT I
1-'0 PRINT .. THE NlMBtR OF' PEGPLE TAKING CHDUSTRY AHO ...
6 REH 12•6•0.980•-'92•015••62•1S9 HO GC1' • 5 145 PRINT "PHYSICS IS"P
10 OIM 1Jt20l.T(20J.sce1>
20 '8R X•I Te 20
30 READ ICXl
,.,
815 635
1387
GC1'
acr
• 5
•I
990 REN DATA IS IN ORDER CHEtUSTRY PHYSICS ,RENCH SPAHISH CALClLUS
995 REM 'I' MEANS YES •o• MEANS NO
'40 NEXT X 1000 DATA I •O• I• l•O•O•O• I• I •O• I• I• 1, 1, 1.0.0. 1.1,0, 1 •I •0•0• l
DINI
SO F'BR Y•I TD 20 1010 DATA 0• I• 1•0• 1•0•0•0• 1•0• h 1.0, 1,0,. h l•O•O• I• 1•0•0•0• I
60 READ f(YJ 1020 DATA O•O•l•O•l•l•O•l•O•O
70 NEXT Y
Problem No. 16 1100 OATA •t
9999 [NO
80 '9R H•20 TU I STEP .. J 10 0£1' ,-NR<X>•IMT<X•RHDt2>•1> RI.I<
90 LET SCH•l l•UCHJ•TCHl 20 F'OR X•I Tl 10
100 NEXT H ND·2
JO LET l•F'NROOO)
110 P'8R A•21 TD 2 STEP •I -'O LtT 1 l•F'NRCIOO)
CHlMISTRY PHYSICS ,RENCH SPANISH
120 Ir SCA)•IO THtN eoo •S PRINT UJJ ...Lot •"I 1
CA1.CtA.US
5
130 LET SIAJ•.StAJ .. 10
l.tO LtT !CA•ll•SCA•tl+I
200 NEXT A
50
60
10
'8R T•l Tl 2 STl:P •I
IF' JJ'T•INT<IJ'T> THEN 200
Hl:XT T
' 6
THt Nt.ttBER 81' PtOPLt TAKING CHDUSTAY ANO PHYSICS ts <I
6
"" ""
DINE LCM • 3685 60 1' C••I THEN 100
LCM • '123 61 LET ACRJ•C
..
e'
",,"
LCM • 133 10 LET CCRJ•CtRl•C
Problem No. 8 LCM • If-' 80 NEXT R
,."
LCM • 1095 Bl IF' AC2l•O THEN 90
5 Rt" Vt Mll.TIPLY 1610981129 BY 810365'201
10 DIM 8t10J.TCIOJ.Pl20J 36 '91 LCM
LCM
•
•
-'68
3276
82 IF' A(SJ•l THEN 90
83 LET P•P• 1
20 '9R Y•I T8 10
30 READ eCYJ
•O NEXT Y ODNt
" LCM • "4680 90 OCT() "40
100 PRINT "CHEM! STRT"•"PHYSICS"•"F'RENCH..... SPANl SH°'•"CALCll..US ..
110 1'0R l•I TO 5
50 F'SR 11:•1 Tl 10 120 PRINT Ct I J.
60 READ TIXl Problem No. 18 130 NEXT I
10 Nl:XT X l•O PRINT "TAKING PHYSICS BUT NOT CALCll.US"P
72 1'1R Q•I T8 20 IS DIM 8C2oJ.TC20l.tc20J.ut•OJ
990 R[H OATA lS IN Oll0£R CHEMISTRY PHYSICS 1'H:£NCH SPANISH CALCll.US
1• LET PCQJ•O 30 DEF F'NR(Xl•INTC20l•RND<t>•IOO> 995 RCM • 1 • HEAHS YES •o• MEANS HI
76 NEXT Q •S LET B•O
1000 DATA 1.o.1.1.o.o.o.1.1.0.1.1.1.1.1.0.0 .1.1.o.1 .. 1.0.0.1
80 '8R F'•IO T8 I Sf[P '"I 60 PRINT "F'IRST LIST''
1010 DATA 0.1.1.0.1.0.0.0.. 1.0.1. i.o.1.0.1.1.0.0.1.1.o.o.o.1
90 ,llR S•IO Tif I STEP .. , 75 1'0R X•I re 20
90 LET l(XJ•rNR( I> 10?0 DATA O•O•l•O•l•l•O•l•O•O
100 LET PCP'+SJ•Pt,+Sl•tt1'l•TCSl 1100 DATA '"l
110 NEXT S 105 LET U(Xl•O[XJ 9999 tHO
120 NEXT t 120 PRINT 8[XH RUN
130 1'8R A•20 T8 2 STtP wJ 135 N[Xf X Nllo'4
140 I,. PCA)<tlO THtH 300 1'0 PRINT
ISO LET l•INTtP(AlJ'IO> "' PRINT .. Sl:CIND LIST .. CHtMISTRT PHYSICS
190 '8R Y• I TO 20 P'RENCH SPNUSH CALCtA.US
160 LET PlA)•P(AJ•IO•J
110 LtT P[AwlJ•PCA•ll+J US LET TtYJ•,NR<I >
' ' ' ' 6
.
N0.9 2
52 IF C>J THEN 58 I 3 2
I 2
54 LET C•O F'lRST ARRAY I
56 GOTO 70 210 25S lSS IJ2 -132 --479 -IS6
II
12
I
3
0
I
2
0
2
2 '
2
2
3
58 LET C" I -Jo; 332 2•6 -0 2.o10 14 .1121
..
3 3 2
70 LET CCRl=CCRJ+C -69 2<14 -<116 -16• 13.li -37 66
13 2
2
I
0
2
j '
2 2 3 2
78
60
REM
NEXT R
NEXT COURSE 2.til 335 343 -<152 -293
2 TIHES ENTRIES OF' f'IRST ARRAY
-196 -52
"
15 2 0
s
2 2 2
.,
' 2
' 6
79
aa
90
RE.M
NEXT X
NEXT PERSON •20
-614
516
66-4
J16
492
264
-16
-26•
"460
-956
26
-J72
6S4
16
"
25 32 30
"
DONE
I 00 PRINT "CHE Ml STRY"•"PH"SICS"• "F'RENCH" •"SPANISH"• "CALCULUS" -1 JS 468 -8J2 -J68 266 -74 I 36
110 F"£lR !"I TO 5 "462 670 686 -904 -566 -396 - fo.t
120 PRINT CCJl• Problem No. 4
\JO NEXT l DONE
9999 ENO 10 Dll1 SC !SdOl
RUN 20 F"OR R:t TO 15
REM LINE JO ENTERS THE ROW NU!1SER IN THE F"JRST COLUMN
NQ.6
Problem No. 10 26
JO LET StR, I l*R
CHEMISTRY PHY SJ CS f'RENCH SPANISH CALCULUS <10 F'OR C"2 TO 10
232 205 219 10 F'OR A=Ire 10 "48 RE11 LINE SO SETS THE LAST 9 COLUMNS AT ZERO
20 F'OR B:J TO 10 50 LET SCR,Cl"O
JO LET H[A,Bl:A•B 60 NEXT C
OONE .liO NEXT B 70 NEXT R
50 NEXT A 80 F"OR N=I TO 50
60 F'OR I•t TO 10 90 LET P=l.~TC4•RNDC\>•2>
70 FOR J•I TO 10 98 ·REH O GQES THROUGH THE 15 QUESTIONS
SEC. 5-2 60 PRINT Ht l•JlJ 100 F'OR O"I TO 15
90 NEXT J 110 LET A"lNH2•i<N00>l
Problem No. 2 95 PRINT 120 LET S(Q,pl:S(O,pJ+A
100 NEXT I \JO NEXT 0
10 F"OR R"I TO 6 110 END J.tO NEXT N
20 f'OR C•t TO J R<RI I A.5 F'OR R:J TO 15
JO LET B[R•Cl•l NC.10 150 LET StR,6l"S(R,2l+SCR,JJ
-40 NEXT C 160 LET S(R17J:S[R,4l+S{R,Sl
50 NEXT R 10
' 5 6 8
'18 170 LET S(R,Sl=S(R,3J+S(R,Sl
. • .••
60 REM ARRAY 8 IS F"ILLEO 11 TH ONES 12 16 u
70 nm R•l TO 6
'12
8
12
10
15 18
" "
21 n H
100 LET srn.9J=S{R,2l•SCR.4l
LET S[R,JOl•SCR,6J+SCR17l
80
90
FOR C=t
PRINT BtR•ClJ
TO J 8 16 u
"
H
u
H
H
e •m 165
190 NEXT R
100
110
NEXT C
PRlNT
10
12
15
18
21
20
2'
28
"
H
H
H
ft H "H "w
198
200
REM THE PRINTING BEGINS HERE
PRINT "QUEST 11ALE HALE
PRINT "NUMBER 21• l.R'/DER
F'EMALEF"EMALE"
21• UNDER HALE FEMALE UNDER 21•"1
120 N£XT R " • H u H H
210
!JO
RUN
ENO
16
18 "
27
32
36 H
m " H
m
H
H
81
"
211
220
PRINT " TOTAL"
F'OR R" 1 TO J 5
N(I02
10
DONE
20 30 •O
" " 100 230
250
260
F"OR C"I TO JO
PRINT SCR,Cll
NEXT C
270 PRINT
260 NEXT F
9999 END
Problem No. 12 RUN
20 F'OR l"-1 TO 5
30 F'OR J•i TO 5 QUEST 11ALE HALE F"EMALEf'E11ALE
ODNE f'EHALE UNDER 21• TOTAL
60 LET P:Cl-1 >•<J-ll NUMSER 21• UNDER 21• UNDER MALE
70 ff P'<S THEN 90 I 5 10 5 3 15 8 13 10 23
5 8 13 12 13 12 25
Problem No. 4 75 LET P"-P-5 2 8
' 1
10 F'OR R•I TO i
80
90
Gt:TO 70
LET PU,J]•P
'
1
II
6
7
8
6
6
8
20
13
15
"
""
18
12
15
II•
1·1" 27
2'
"
100 NEXT J 8
20 F'OR C• 1 TO 7 II 13 31
JO LET OCR.Cl•O
I 10 NEXT l. II
'6 2
'6 20 18
12 10 22
40 If R o C THEN 60
l.tO PRINT "HULTIPLJCATION MCD 5" 7 3 13
' 26
SO
60
LET OCR1CJ•I
NEXT C
150
160
FDR I•I TO 5
FOR J•I TO 5
'10
6
8 5'
7
6
'10 10
13
7
"" 13
15
8
"12
I·' 20
2'
70 NEXT R
170 PRINT P{f,J)J
'7 ' 3
' 13
15 8 12 II 23
60 F'OR P•I TO 1
160 NEXT J II B
' ' II II II II 22
90 F'OR 0•1 TO ,.
190 PRINT 12 7
' ' '6 27
100 PRINT DtP,Q]J
200 NEXT 1 13 10
7 '6 1
7
' "
13
13
"
10
13 "
14 27
110
120
NEXT Q
PRINT
205
210
220
PRINT
F"CR M"l TO 6
LET A•INT<S•RNO(J))
"
15
' 7
' II 16 16 II
IJO NEXT P
2JO LET B•ItlT<5•RNOCll}
1•0 ENO
RLW
240 PRINT A"•"B"="P{A+ 1.a+ I )"'100 5"
Ne • .- 250 NEXT H Problem No. 6
260 ENO
10 DIM Htl2•12l
RUN
20 1"9R R•I T0 12
ND.12
30 F'OR C•I TO 12
40 LET HCR;Cl•R•C:
HUL.TIPLICATION MOO 5 SO NEXT C
0 0 0 0 60 NEXT R
.
O I 2 3 10 f'OR R•IO Ta 12
2 < I 80 F"OR C•I TB 12
3 90 PRINT MCR1ClJ
DONE
100 NEXT C
110 PRINT
• 2 HOO 5 120 NEXT R
Problem No. 6
10 otr F'NRCC>•INT<C•RNOCl>>-50
20 PRINT "rlRST ARRAY" ..
•I
'I
• 2
• 2
.'
• 0
•I
HOD 5
HOO 5
HOD 5
HOD 5
....
130
ND.6
ENO
BO PRlNT
90 NEXT R SEC. 5-3
12 48 60 72 84 96 108 120 132 I••
95 PRINT "S£CONO ARRAY" DONE
100 F'OR R•I Te 3
110
Problem No. 2
f'DR C•I TO 7
120 LET TCR.Cl•FNRC200) 10 DIM Stl6.10l CHAP. 6
130 PRINT f'CR•Cll 20 F'OR R•I TO 16
1'40 NEXT C 28 REH LINE 30 ENTERS THE Rew NUMBtR IN TH£ "'1RST COLUMN
150 PRINT 30 LET SCR;tJ•R SEC. 6-1
160 NEXT R 40 roR C•2 ra 10
170 F'DR R•l TO 3 48 RtH LJN£ 50 SETS THE LAST 9 COLUMNS AT ?£RD Problem No. 2
lBO TOR C•I TB 7 50 LET SCR,.Cl•:l
190 LET F'CR.Cl•rtR.Cl+ECR.Cl 60 NEXT C 10 PRINT "Hut.TIPl.Y TWa F'RACTICN5''
200 NEXT C 10 NEXT R 20 PRINT " TO ST0P RUN INPUT .QI F"DR NI.,
210 NEXT R 18 REH BO READS TH£ CATAGORY l"OR THE NEXT PERSON IN THE: SURVEY 30 PRINT
220 PRINT "SUHS ENTERED IN SECOND ARRAY" 80 READ P 3S PRINT "tH10l"J
230 'OR R•I TO 3 90 tr P•-1 THEN l4S 40 INPUT Nl•DI
2•0 F'OR C•I TB 7 98 R£M 0 GO£S THROUGH THE IS QUESTIONS 42 lf' Nl••OI THEN 999
2!10 PRINT F'tR.ClJ 100 l"BR Q•t TO 15 50 PRINT "N2.02"J
260 NEXT C 110 READ A 60 INPUT N2.02
270 PRINT llS LET SC16,.Pl•Stl6;PJ+A 70 l.ET N•Nl•N2
280 NEXT R 120 LET S(Q,,PJ•SCQ.Pl+A 75 LET N3•N
290 END 130 NEXT Q 80 1.ET 0•01•02
RLW 138 REH LINE t 40 SENDS THE COMPUTER BACK TO READ ANOTHER LINE or DATA SS LET 03•0
90 REH HERE IS THE EUCLIDEAN ALGORITHM
.. -··
N8•6 140 GOTO BO
l-15 F"OR R•I TB IS 100 LET I•INTCN/0)
F"lRST ARRAY 150 LET S[R,,6J•StR;2J+S[R;3l 110 LET R•N-UO
19 -33 IS -<2 2B -1 160 LET StR.7J•S[R,,4l+StR.5l 120 If' R•O THEN 160
-12
33
-13
I -•5
26
-31
16 _,. 9
23
-39
-;
170
180
LET S[R;6J•StR.3J+StR..Sl
LET SCR.9l•SCR.2l+SCR.4l
I.JO
140
LET N•O
LET O•R
SECOND ARRAY 185 LET SCR.!Ol•SCR.6J+SCR,.1l ISO GOTll 100
•6 •1 128 99 139 36 65 188 f'BR C•S TS 10 160 PRINT "PRODUCT I S"NJ/0"/"03/0
-35 101 180 GSTO j)O
.. ..
5• 56 1•8 19 37 190 LET SC16.Cl•SCl61Cl•StR .. Cl
13 39 1• 31 -•2 -30 97 192 NEXT C 999 [NO
..
SUHS ENT£R£0 IN SECOND ARRAY 195 NEXT R RLW
_.,
65 55
• 9•
95
30
117
82
97
164 88 -2
198
200
REH THE PRINTING BEGINS HERE
PRINT ugu£ST HALE HAl..E FEHALErEHALE"
N0•2
•o
-31 0 -76 -7 90 210
21 l
PRINT "NUHBER 21+
PRINT .. TOTAL"
OOOER 21+ OOOER HALE F"EHALE I.HOER 21+"J Mll..TIPLY TWO l"RACTlONS
TO STOP RIM INPUT .01 F"BR NI
OONE 220 l"OR R•I TG 16
230 roR C• I TO 10 N1.on1 .. 2
250 PRINT srn.c11 N.2.0214•3
260 NEXT C PRODUCT JS 2
Problem No. 8 270 PRINT
260 NEXT R Nl.01?<115•72
5 PRINT "FIRST ARRAY"
498 REM • . .A LINE LIKE 500 HAY HELP TO LINEUP THt DATA LINES H2102121;8
10 reR X•l Te " 499 R£H IN TYPING••• PRODUCT JS 105
20 f'OR Y•I TB 7
30 LET ICX•Yl•INT<IOOl•RNDCl)-500> 500 R£H l;l;l;l1l1l•l•l•l•l•l•l•l•l•l•I
40 NEXT Y 501 DATA 4•1;0;1•1•1•0•0•1•1•0•1•0•1•0"1 Hl.Dl?·Ol• I
so2 DATA 4 .. 1.o.o.o.0.1.1.0 .. 1.1.0.o.o.1.1
50 N£XT X
503 DATA 3•1. I; I• 1.0,.0,.1.0,,1,.0.0,. 1,. 1,,0.0 09N£
60 G9SUB 1000
504 DATA 5.1.1.1.0.0.0.1.o.o.o.1.1 .. 1.1 .. o
70 ,.OR I•! TO 4
505 DATA 2 .. 1 .. 1 .. 1.0.0 .. 1.0 .. 1,.0 .. 0.1,,1.1,,1 .. 0
80 ,.BR Y•I TO 1
506 DATA 5.0 .. 0 .. 1 .. 0.1.o .. o .. o.1.1.1.o.o .. i.1
SEC. 6-2
90 LET ltl.Yl•2•H1 .. Yl
so; DATA 5,,0 .. 0 .. 0.1.1 .. 1~0.1.o.1.o.1 .. o.o.1
100 NtxT '(
508 DATA 1?.o .. 0.1 .. 1.0 .. o.i.1 .. o.1.o.1.o.o.1
Problem No. 2
IJO NEXT I
1:-.:l 509 DATA 41l .. t1l1l•l•l1l10;0;0;J;O•l•O"O 5 DIM Lt21l
~
s10 DATA 2.1.1 .. 0.0.1.0 .. 1.o .. o.o .. o.1.1.1.1 10 READ N
900 DATA -I 15 LET T•O
999 END 20 J'DR X•l TO N
150 LET Nl•Nl•I•!Ot£l 110 PRINT "BS .. J
~ SEC. 6-2 Cont'd teo INPUT BS
160 LE:T R•N-l•IOrE
C.ll 170 LE:T N•R 130 CHANGE BS TB B
0 180 NEXT£ 140 F'BR I • J TO 8(0)
30 R£AO L( Xl 190 PRINT N2JNl"G.C.f'. 150 LET A(BCI)) • ACBCI)) + I
35 LET T•T•LCXJo2t<N-Xl 200 LET I•lNT<N2/NI l 160 NEXT I
"'O JF L(X]•l THEN 70 210 L£T R•N2-J•NI
50 PRINT "O"J 170 LET B<O> ,. 1
220 Jf' R•O THEN 260 180 PRINT "CHAR NUH CODE"
60 GOTO 80
70 PRINT "l"J
230
2"'0
LET N2•NI
LtT Nl•R
190 F'eR I =0 TO 127
80 NE::U X 200 IF' ACI > ,. O THEN 240
250 GOTO 200
90 PRINT .. BASE TWO •",...lASt TCN" 260 PRINT NI 210 LET Bet> ,. I
100 GOTO 10 280 GOTO 30 220 CHANGE 8 TO AS
110 DATA 5.1.0.i..t.i 350 ENO 230 PRINT "'"J ASJ "' '"J ACI>J " "J 1
120 DATA 2• l•O RLH 240 NEXT I
130
999
DATA 15.1.0.0.o.1.1.o.o.1.1.o.o.o.1.o
tND
N0.4
....
eso END
""'
Ne.2 INPUT 1NTEG£R72S
I
N0o2
25 52 G.c.f', •
10111 BA.SE TWO • 23 BAS!'. TCN BS7 SUPP0SE I Da'.PT WISH TD PLAY THIS GAME
10 BASt TWO • 2 BASE TEN INPUT 1NTEGER7"'!56 CHAR NUM C00E
100011001100010 BASE TWO• 18018 BASE TtH '456 65'4 G•C•f'• • 6 1 32
OUT 0,. DATA IN LrNE 10 INPUT JNTEGER1779 "A'
I
2
39
65
779 977 G.c.f'. "' 1 •o• t 66
Problem No. 4 'E' 2 69
INPUT 1NTEG£R10
10 READ N 'G' l 71
IS LET T•O DON£ 'H' 2 72
20 PRINT NJ"BASE TEN •"J 'I' 3 73
30 f'OR E•20 TO 0 STEP - I 'L' I 76
"O LET l•JNTtN/JtEl 'H' 1 77
"'2 LET T•T•l CHAP. 7 'N' j 78
U Jf' T•O THEN 60 '0' 3 79
<1!5 If' 1•1 THEN 55
SEC. 7-2 •p• 3 80
.n IF 1•2 THEN 58 •s• 4 83
50 PRINT "O"I
52 GOTO 60
Problem No. 2 'T' J 64
'U' I 65
55 PRINT ''l"J
57 GDTa 60 95 REM • PRINTING A NUMERIC OF' MOHE •w• 87
50 PRINT .. 2'"1 96 REM THA~ ONE DIGIT USING STl{lNG 89
60 LET R•N-I•3•E 97 REM SEE LINES 165~ 182 AND 165
70 LET H•R 100 DIM OS( !Ol
80 NEXT E 110 LET OS,."0123456769" Problem No.
85 PRINT " BASE THREE"' 120 PRINT "POSITIVE INTEGER LE!:iS THAN 1000000"·
90 GOTO JO 130 INPUT N 100 DIM WSC7>
100 DATA '1'19999 .. 1•16 140 IF' N:O THEN 260
110 CNO 105
150 PRINT """JNJ""" JOB REM • READ DATA AND FINO L.eNGEST STRING
""'
NO ... 160
165
PRINT "S"J
LET T=O
110
120
LET L=O
F0R I : I Te 1
999979. 8AS£ T[H •1212!10202000 DAX THRH 170 F"OR E=5 TO 0 STEP - I 130 READ WSCI)
I BASE TEN •I BA.SE THU:E 180 LET I=INTCN/IOtE) 140 lF LEJHWSCI» <= !.. THEN 160
16 BAS£ TtN •121 BAS£ THREE 182
165
LET T:T+l
ff T=O THEN 200
ISO LET L =
LENCWSCI»
160 NEXT 1
OUT 01 DATA IN LJHl 10 190 PRINT DSU+l.I+llJ 165
200 LET R=N-I•IOrE 168 REM * PRINT DAYS 0F' THE: WEEK VERTICALLY
210 LET N=rR 170 F"eR I =I T0 L
Problem No. 6 220 NEXT E 180 FOR J = I T0 7
230 PRINT "S" 190 PRINT TABC3•.J>J EXTSCWSCJ>,I.lH
5 REH NI IS NUMBER Of' DIGITS 240 PRINT
10 READ NI 200 NEXT J
250 GOT0 120 210 PRINT
15 LET N•O
260 END 220 NEXT I
20 f'OR X•Nl TO I STEP ~1
22 RtAO A
24 If' A•l THEN 30 ""'
N0.2
225
226 REM *
DATA
25 If' A"2 THEN 34 230 DATA SUNDAY• M0NDAY, TUESDAY• WEDNESDAY
26 PRINT "O"J POSITIVE INTEGER LESS THAN 10000007189231 240 DATA THURSDAY. FRIDAY. SATURDAY
28 GOTO AO I 169231. I 250 ENO
JO PRINT "l"J S189231S RUN
32 GOTO AO N0.4
34 PRINT "2"J POSITIVE INTEGER LESS THAN 1000000?0
°'O LtT N•N•A•Jt (X-1 l s M T w T F s
SO NEXT X
60 PRINT " SASE THREE"
70 PRINT "EQUALS"
Problem No. 4
u
N
•
N
u
E
E
0
0 0 s N
110 LET T•O
130 f'GR E•20 TO 0 STEP •I
REM **
NOTICE THAT !:iHOHTEk
USE OF A A D E
REMTHEN A PHOG~AM y y A s
1'40 LET l•INTCN/2tE)
REMSTRINGS D
150 LET T•T+I
OlM OS{ 121 A
160 If' T•O THEN 210
170 lf' 1•1 THEN 200 8 QS::"0123456789TE"
180 PRINT "O"; 10 READ N•B
190 G0TC 210 20 LET T=O
200 PRrnt "l"I 30 PRINT NJ "BASE: TEN ="!
Problem No. 6
210 LET R•N-h2fE 40 FOR E=20 TO 0 STEP - t
220 LET N•R • 50 LET l =INTCN/Br El
100 DIM wsc7>
230 NEXT £ 105
2"40 PRINT " BAS£ TWO"
60
70
LET T=T+I
IF T= 0 THEN 430
106 REM *
READ DATA AND FIND LONGEST STRlillG
2"45 PRINT 110 LET L=O
80 PRINT DS(l+J,I+llJ
250 GOTB 10
430 R=N-I•BrE
120 FOR 1 =
1 TO 7
260 DATA 5.1.0 .. 0 .. 1 .. 2 130 READ WSCI>
26~ OATA I0 .. 2 .. 0 .. 0 .. 0 .. 1 .. 1121 l•O; I 440 LET N=R 140 IF LEN(WSCI)) <= L THEN' 160
.....
270
N0.6
£NO 450
460
470
NEXT E
PRINT "
GOTO 10
BASE "J B
150
160
165
LET L = LENCWS<I>>
NEXT 1
~ 130 IF' T•O THEN 160 9B REH * TABULATE CHARACTER FREQUENCY CURIE, MARIE
1"40 LET El.,£1+1 100 DIM AC127> .. BC72) VERNE, JULES
SEC. 7-5 Cont'd 170 LET SIS = "ZZzz·· Problem No. 4
~ Problem No . .J
160
190
RESTORE I I
FOR I = 1 TO C 10 LET G•ATNCSORC96Jn?>
t-o 200 READ #I, NS
20 LET G•G•ISO/J.14159
90 REM *
ALPHABETIZE ilAMES IN A FILE
204 REM *
GET THE NEXT NAME GREATER THAN THE LAST
30
40
LET D=INT<G>
LET ~l•INTCCG-0>•60+.5>
92 REM *
CA'JY NUMBER JF' NAMES>
205
210
REM NAME PRINTED
IF' NS <= SS THEN 240
4S
50
PRINT "TWO ANGLES ARE"
PRINT D"OEGREES "H"HJNUTES"
100 DIM AS(72J,8S(72J
220 IF NS> SIS THEN 240 60 LET Cil•l80•2•G
110 F"ILES TEST
230 LET SlS = NS 65 PRINi' "THE THlRO ANGLE HEASURES"
112 IF END #I THEN 119
240 NEXT I 10 PRINi' lNT<Gl )"DEGREES "J
114 FOR N=I TO 1000 60 PRINT INTCCGJ-INT<G1»•60••5>"HINUTES"
250 PRINT SIS
116 READ #1,NJAS
260 LET SS = SIS 90 ENO
118 NEXT t~ RUN
270 NEXT P
119 LET N=N-! NtJ.4
280 END
120 FOR 1=! TO N-1
130 READ IJ,JJAS,A .. AJ
RUN TWO ANGLES ARE
N0.2
140 F'OR J=l+ I TO N 76 DEGREES 26 MINUTES
150 READ 11 .. J1Bs .. a .. s1 THE THJRO ANGLE MEASURES
~RISTIE AGATHA
160 IF" AS <= es THEN :mo 2J DEGREES 4 MINUTES
GOOSE MOTHER
170 PRINT 11 .. 11ss .. a .. s1
TRUMAN HARRY DONE
180 PRINT 11 .. J1AS1A1Al
NIST OLIVER
190 READ IJ,IJAS,A,Al
WHITE SNOW
200 NEXT J
210 NEXT I
Problem No. 6
220 PRINT "FILE ALPHA3ETIZED" Problem No. 4 I0 LET G•ATlH 12,.Sl
230 ENO 20 LET c; .. G•IBO/J.14159
RUN 100 FILES NAMES! JO LET O,.JNT<Gl
NQ.4 110 PRINT " ** BEFORE **"J TABC25H " ** AFTER**" 40 LET ~!•INT<CG•Ol•60+.S)
120 FOR I = l TO I 0 SO PRINT O"OEGREES "M"HINUTES"
F"ILE ALPHABETIZED 130 SETW 1 TO B•Cl-1} + i 60 LET GID90-G
140 READ 1 J.1 AS 70 PRINT INT<GI >"DEGREES "J
GET-NO• 3 BO PRINT INT<CGl•INT<G1>>•60•.5l"HlNUTES"
150 PRINT ASJ TAB<25H
RUN 90 ENO
160 FOR J = I TO LEN<ASl
NO. 3 170 IF' EXTSCAs .. J,J) =" "
THEN 210
RUN
NB·6
180 NEXT J
008 NAME 190 PRINT "SPACE MISSING" 61 DEC.REES 2J MINUTES
1820 ANTHONY.1 SUSA>/ 0. 200 GOTO 280 22 DEGREES 37 HlNUTES
1847 BELL, ALEXANDER GRAHAM 210 LET BS = EXTSCAS,J+ l.1LENCASl l
1620 BRONTE, ANNE 220 LET CS = ", " DONE
1667 CURIE, MARIE 230 LET OS= EXTSCAs,1,J-ll
1747 JONES1 JOHN P4UL 240 LET AS SS + CS + OS
1628 VE~E .. JULES 250 SETW l TO 8• C1- I l + I Problem No. 8
1480 VERRAZAN01 Gl3VANN1 260 WRITE I l.1 AS
1813 WAGNER.1 WILHELM RICHARD 270 PRINT AS 10 LET il"IOOO•TAN<C7+30,.6D>*J•l<lt59,.IB0l
1859 WASHINGTIJN.1 E<!OKER T. 2D PRINi' "HEIGHT I S"H"fEET"
280 NEXT I
30 ENO
290 END RU'i
RUN
Problem No. 6 NO. 4
N0•6
N0·6
ENO
BO
90
PRINT -B/C2•AU-SQRC-Ol/C2•A>
GCTC 10
499
500
REM '
DATA
- I' MEANS STOP
1·24.51.JO
99 ENO SIO DATA Q,901601 IS
520 DATA - I
NAMES IN 0ROER RUN 999 ENO
008 NAME N0.2
1620 ANTHONY. SUSAN 8. RUN
1647 BELL, ALEXANDER GRAHAM I 3 2 REAl. ZEROS -I N0.2
1620 BRONTE. ANNE I I I
1867 CURIE, MARIE NON-REAl. ZEROS -.S oB6602S -·866025 A 8 c
1747 JONES, J0HN PAUL 3 -2 .. THE ANGl.ES ARE 24 51 105
1626 VERNE. JULES
NON-REAl. ZEROS •333333 l • 1055<1 .JJ33JJ -1.1oss.- THE SIDES ARE I0 2J.75
1460 VERRAZAl'rn .. GICVANNl
DONE A
1613 WAGNER, WILHELM RICHARD
THE ANGl.ES ARE JO
1859 WASHINGTON,. BOOKER T.
THE SIDES ARE IS "
2S·96
90
30
-·
-9 193
500 PRINT "END OF FILE" 500 DATA J,SJ. IJ.S
600 NEXT 1
-3 31 540 DATA o, o, 0
999 END 0 1000 END
RUN 13 Rili
NO·B 58
139
A c
RECORD !
F"IRST 999812. 7612 -123. 45 DBNE
" 256
THE SIDES ARE
THE ANGl.ES ARE
J
J6.87
J.99999
SJ.13
5
90
RECORD 2
SECOND RECORD
DONE
RECORD 3
END OF FILE
RECORD 4
CHAP. 9 Problem No. 6
1234 12456 -999999. 123
RECORD 5 5 PRINT " "•" A"•" B"." C"
END OF FILE 10 DEF' F'NRCX)=INTCX•IOO•oS)/100
SEC. 9-2 15 l.ET K=180/Jol<llS9
20 READ a1.c.a
SEC. 7-6 Problem No. 2 22 PRINT
2S IF' St=O THEN 9999
10 l.ET G=ATNCSORtJ6t2-B•2)/8)
Problem No. 2 IS Pf.flNT Gl"RAOIANS"
JO JF' ASS<SIN<Sl/I0-8/C)c.00001 THEN 1000
40 IF' BcC•SIN<Sl/K) THEN 1100
17 PRINT" OR" 60 IF a .. c THEN 1200
NAMES 20 LET G=G•IBO/J.14159 BO PRINT "THERE ARE TWO TRlANGl.ES"
2B l.ET O=INTCG> 90 LET Sl=I
100 CHHISTIE AGATHA JO PAINT 0"0£GREES" 95 PRINT "ONE"
110 TRUMAN HARRY JS LET H"60•CG-1NTCG>> 100 GOTO 1210
120 WHITE SNOW AO l.ET Ml,,lNTCM> 110 PRINT "TWO"
130 Gl30SE MOTHER AS PRINT Hl"HINUTE~" IJO l.ET CJ;JBO-CI
140 TWIST OLIVER 50 l.ET S:::tH-HI 140 LET Sl;O
55 PRINT S•60"SECONOS" I SO GOTO I 2JO
60 ENO I 000 PRINT "RIGHT TRIANGl.E"
NO• 2
RUN 1010 LET A"SGRCC•2•8•2>
N0.2 1020 PRINT "SJOES ARE".f'NRCA>.s.c
100 F"ILES NAMES
104 REM * FIRST COLN T NAMES 1. 3467 RADIANS
IOJO PRINT "ANGl.ES ARE"• 90-81•SIo90
110 LET C =
0 OR
10<10 GOTO 20
120 LET C =
C + I 7? OEGKEES
1100 PRINT "NO TRIANGl.E"
1110 GOTO 20
130 READ #1,. NS 9 MINUTES 1200 PRINT "SINGl.E TRIANGLE"
"°
~
140
150
160
IF MORE #l THEN 120
LET 5$ :: "AAAAA"
FOR P = 1 TD C DONE
J7. 7 161 SE CON OS 120S l.ET Sl"O
1210 l.ET S"C*SIN<Bl/K)/8
1220 l.ET Cli:ATNfS/SORCl-S•2>>•K
-.es .75 60
- • <43 .43 Problem No. 4
kl SEC. 9-3 Cont'd .13 .46 .13 . .6 75
0 0 90
Q-(
ol:l.
0
-.13 ' ·46 .13 -·<lB 105
10 READ A.a.c.o
11 Ir A"•OI THEN 999
..,,.,
1230 LET A!cl60-CBl+CI - • .tJ .43 -.75 120
.2s 12 PRINT
.. ,
1240 LET A=SINCAl/K,.B,"SlNCBJ /KJ
12SO
1260
PRINT "SIDES ARE" A.-8.C
PRJNT "ANGLE:S AR£",f'NR<Al>.f'NR<81), F'NRCC1>
-.11
-.25 ·"
.75
-·71
-.43
-.13
135
ISO
165
20 PRINT "C"A"•"B">•C"C"•"D" l•C"A•C"•"B-0">"
JO PRINT "<"C","O">•<"A"•"B"> •<"C-A".''O-B">"
.,
- ... a . .6 40 PRINT "("A"1"8")/C"C"1"D") •C"J
.....,
126S IF' Sl"i THEN 110 0 160
0 0 .o\S PRINT CA•C•B•Dl/ CCT2+D•2>"•"<B•C-A• O>/(Cf2+0T2)")"
1270 GOTO 20 -·13 195
!JOO DATA J0.6.-9 - • .ts -.13 ··A:B
210
SO PRINT "<"C"•"O"J/C"A"•"B"> •C"I
.43 ..75 -·43 SS PRINT CA•C•B•D}/(Af2+9T2) ","(A•0-8•C>/ (Af2+B•2>">"
IJIO DATA J0•9•6 225
I 320
1400
DATA J0•2• I
DATA o,Q,Q .25
-.13
·" -.•a
.71 -.71
-.43
-.13
-·71
-.75
- ... a
2'0
255
60 GOTO 10
100 OAT" J.0.0.1.1.2.3,4,s.-a .2.4
110 DAT" .01.0.0.0
9999 END 0 0 270 999 END
0 ·I
RW
NOo6
-.2s
.13
..,
- • .i11a -.13
-.43
. .6
.75
.,,
265
300
RUR
NG.4
-.11
...
- • .tJ ·"
.25
-.13
-·71
-.1s
.... a .,,
.43
JI s
JJO
"' ''
l 0 .'
• 0 l-{
)•( 1
0
0' }•(
>•(•I
j
.'
.··•'
SINGLE: TRIANGLE 0 0 0 360
I )/( 0 0
0'
8 l' • 0 J•(
SIDES ARE 14·9905
'30 .' )•( 0
..
ANGLES ARE 12J·61 26.J9 l 0 J/C I •
. ..
DONE
l' • 2 }•( J • 4 hC•2 ,-2
THERE ARE T'<IO TRIANGLES
ONE >-<I • 2 I•( 2 • 2 >
Problem No. 2(e),(f) ' J •• oa
. ..
• 2 )/( 3 }•( o.o\4
....
SID£S ARE 14·4086 l l
11s.11 30 34.23 'J • 4 )/( 1 • 2 J•( 2·2
ANGLES ARE
10 LET K•J.14159/180
T"O
SIDES ARE Io 17965 'J45,77 20 PRJNT " R•SINCJG>"•" RCOStGl•I"
' 5
, )-( 2 }a( J .-12
ANGLf.S ARE
RIGHT TR JANGLE
4o2J 30 25
JO
JS
PRINT " X"•" Y"•" X"•" Y"1"ANGLE"
DEF' F'NXCR)•R•CDS<G•IO
OEr F'NYCR).,R•SIN(G•Kl
l
l
(
2
s
2
.·•
• 4
• 4 J•( 5
)/( 2
J/( s ··•
··6 l•(-J
1•(-lol
hC-.24T191
• 12
.-1.a
, .404494
SIDES ARE 2•24 I 40 OE'.f fNRCX)•INTCX•IOO+.S l/100
ANGLES ARE 60 30 90 so F'GR G•O TC 360 STEP 15 DONE
60 LET E•SJN<:l•G•KJ
65 PRINT F'NRCF'NXCE> >.FNRCF'NYCE:»•
DONE 70 1" ABSCCDSCG•I0><.0001 THEN 90 Problem No. 6
.,
60
87
LET F'"1/C0S<G•I()
PRINT FNR<F'NXCF")J,f'NR<F'NY <f>J,G
GDTO 170
S FOR X1r1 TC 5
READ C,Q
10
PRINT " X DR Y UNOEF'lNED"• G 20 PRINT ··c1.o>/("C"•"O"l•("C /(C•2•0•2>"•"'
. ...'
IS
17 LET B1•82•8J/60+841 J600 0 0 0 0 (1,0)/( 1 • 2 J:( .2 .-.4
20 LET TaC0SC81/l<l
JO LET 8•SORCAf2+Cf2·~ •A•C•T>
o66
•• 7
." '
'
·27
"
JO
C l•O>/CI
Cl.OJ/( 0
,
,
0
I
J•(
J•( 0
l • 0
.-1
.s .. ~.230769
40 I.ET Tl•CB12+Cf2-AH J/C2•B•C>
SO I.ET Al•K•ATNCSQRC I• Tl f2)/TJ)
.s ' 1.73 "
60
(1,0)/(
C!.0)/(-J
2 • J 1" ( .153846
j;i(-.12
''
0 0
...
75
60 I.ET Cl•180-CAl+BIJ
65 PRINT "THE SIDES Al'E''.A.a.c
··IB
0 .,-.66 J.7J
X OR Y lf-IDEF"INEO 90 DONE
70 PRINT "THE ANGLES I RE" -.66 -3.1J 105
60 L£T A2•lNTCAI >
90 LET AJ•INTC<Al-A2» 60)
0
".s
0
.s
I
I .,
-l.7J 120
135 SEC. 10·2
100 LET A4=INTC<CA1•A: )•60-AJ)•60+oSJ -.51 .s -.s5 ISO
Problem No. 2
110 I.ET C2•INT<CI >
120 LET CJ•lNTCCCl-C2 •601
130 I.ET C4:r:INT<«Cl-C!~>•60-CJ>•60•·S>
1 JS PRINT "ANGLE DEG HIN SEC"
-.66
0
...
•• 7
.s
0
...
• 16
.s
.s
I
I
'I
-·21
I
0
.27
.SB
165
160
195
2!0
225
10 LET R•i
20 LET G•4S
JO LET Rt•i
I'
140 PRINT "Al • "A2JA.JJA4 40 LET Gt•O
l~O PRINT "Bl • "B21a:u8.o\ 0 0 1.1J 2<0
SO f'OR N• I TO 6
...
160 PRINT "Cl " "C2JC:uC4 --16 -.66 J.1J 255
X CR Y UNDEF'INEO 270 60 LET Rl•Rl•R
165 PRlNT 0 •I
-J.1J 265 70 I.ET Gi•Gl•G
170 GOTO 10 -·68
' 60 PRUIT ''(l,4S>'"N''•<"Rl'"•"G I")"
399 REH 0 H S
500 DATA 3.sJ.1 • .ota.s -.s
0 0
.s '' .,
-I .1J 300
315 90 NEXT N
100 ENO
.s -·58 330
SJO DATA tO.Jl• 16•5.-2<>
540 DATA o.o,o.o.o
-.67
-.66 d6 'I -·27 3'5 RUN
NQ.2
1000 ENO 0 360
RUN Cl;4'5)• I
Na.a OONE Cl.4Sl• 2
{J,45)1 3 135
"
90
NG.2GH
100
t to
,,. a.. o THEM 1so
PRINT ""D.O>""
120 GlllT8 30
Problem No. 2(a),(b) R•1•RC9S<G> R•SIN<G>•CUS(G)
130
UO
PRINT •8"•270)"
GCITS JO
5 LET K•3ol4159J'l80 y y ANGLE 150 PRINT 8",9D>"
X OR Y Lf.IOEF'INEO
10 DE1' FHR<X>•JNTCX•100••5)'100
2Bo3S 106 '
lol8
0
.32
0
IS
160 GDTB 30
.....
12 PRINT .. cesc20G>"'•.. CISC3•G> .. 110 LET R•SQR(AJC+Bt2>
15 PRJNT • X"".-"' Y'"•" X".-" Y"1 .. ANGLE" 6· .. 6 J.7J 1.18 ·68 30 180 LET G•ATfHB/Al/K
30 FBR G•O TB 360 STEP 15 2o4I 2o41 I I •5 190 I'· A.. O TH£H eeo
40 LET A•CDSC2•G•K> l t.73 l•IB 60 200 LtT G•0• 180
50 LET B•COSC3•0•K) .35 1·3 lol8 7S 210 GOTO 2 ..0
60 PRINT fNRCA•C8SCG•K))1FNRCAOSINCG•K>>• 0 I 0 I 90
70 PRINT P'HRCB•COSCG•K>>.-F'NRCB•SIHCG•K>>.-G -.21 .77 -·18 ·•8 105
220
230
IF' 8.,0 THEN 240
LET G•G•360
.
-.JJ 058 -·18 -32 120
90 NEXT G PRINT R.. ,"G">"
...
240
100 £HD
- ... 1 ..... 0 0 135 e5o Get& 30
Rt.t< - ... 6 .21 .32 ··18 ISO
NOo2AB .... 9
•o5
·13
0 I
-·18
0
165
180
....
260
N8o4
£HD
.....
COSC2•G> C8SC3•G>
·•8
...
x y x y - ... 6 -.21 lol8 210
ANGLE CONVERT ,.RSM <A.B> TO ClhG>
I 0 .... 1 - .... I I 225
I 0 0
·8• d8
•oJJ -.sa lolB 240 A.BfO,I
·•3 ·25
·•8
0 0
"
30
-.21 -077 J.18 255 <R.G> ,BRM • ( I 190)
.....
0 0 •• 5 •• 5 0 •I 0 I 270
-.es
-·22
.... 3
•• 5 ..
•• 5
-·18
-.81
-·68
•5
••
75
.35
I
-1·3
•t.73
-·18
-.18
·•8
-32
285
300
A•Bf•l••l
<R•G> F'8RM • < lo .. 1 ..21 , 22S•
...
••,., 2 ... 1 •2o-41 0 0 315
......
0
..
·I 0 0
.22 -·84 -.18
60 .. 6 •Jo7J -.1s 330 A18f•oOl10
...
·•8 28035 •106
·25 .... J •• 5 ·87 -· 18 3'5
X OR Y lfiD£J'IN£0 360
0
- • .tJ
0
0 0
135
ISO DONE
' 0 08NE
-.8 .. .22
•I 0
-.19 165 Problem No. 6
I 0 180
-·8"' -·22 ·68 ·18 195 CHAP. 10 10 PRINT ""IND INTEGRAL POWERS 8,. <A•B>"
0
...
....3
.22
0
··25
..,
0
·8•
I
0
··5
•• 5
-·18
0
0
•• 5
-.87
-·68
0
210
225
2 ..
255
270
SEC. 10-1
Problem No. 2
20
30
.. o
50
60
PRINT
PRINT "WHAT P8WER"J
INPUT N
PRINT '"A•B"J
INPUT A18
•o22
•o25 ..,
·8• •o18
•• 5
.08
·87
285 10
20
PRINT "RdOTS OF AXt2+BX .. C"
PRINT
10
80
LET C•C•I
LET D•,.•O
.....,
300
0 0 •• 5 .5 315 JO PRINT "A•B•C"J 90 1DR X•I re N
-025 0 0 330 .. 0 INPUT A•B•C I 00 LET C•A•£·B•,.
-.22 .08 •olB 3'5 50 IF A•O THEN 160 110 LET D•A•F'•B•£
I 0 I 0 360 60 LET O•Bt2• .. •A•C 120 LET E•C
70 ff O<O THEN 120 130 LtT F'•D
DONE BO PRINT "REAL R60TS" 140 PRINT X"<"C"1"0"}"
90 PRINT C-B .. SQR<O>J/'C2•A)J ISO NEXT X
NOo6
ENO
N0o2
END A1B?•h•I
I
2
(•J ••!
( 0 • 2
10 LET D•SIN<2•G•K> J ( 2 .-2
75 PRINT F'NR<C•COSCG•K>>1F'NRCC•SlN<G•K>>• ROCITS OF AXt2+8X+C .. ,... * 0
80 PRINT F'NRCO•CBS<G•K>>,F'NR<O•SIN<G•K>>•G
90 NEXT G A.S.C76.13•6 D8N[
100 ENO REAL ROOTS
Rl>I ··666667 -1.s
N£h2CD
SEC. 10-3
A.e.c12.1.6 Problem No. 2
CBSU•G> NON•REAL ROOTS
SIN<2•G>
"'°
en.
en.
.... J
-·71
·13
-.2s
-·71
.,,
·•8
.71
..,
d3
.71
IS
30
•5
A.a.c10.o.o
DONE
BO PRINT "TH£"N''."N"TH ROOTS er t"A''."B"> AR[I"
IOD LET R•SQRCA'2 .. Bt2)
102 1r A o 0 THEN 12CI
'20 N()(T 260 IF' H•l THEN 10
l>:i SEC. 10·3 Cont'd :no PRttlT "NONE 1"(1UN0"
'1
Q) 10"' LET G•3·1'1159/2
125
130
F'OR z,.A+B· I
PRINT PCZlJ
NEJO;T Z
TO ~ STEP - I
280
290
GOTO 10
OAT/\ 4, 1.--2,3,-.o11
I 06
108
If" B>O THEN 2"'0
GOTO 160
"'
1<0
I <5
Pt<INT
PRINT
JOO
JlO
OAT!\ s,2,-1,-11,11.--2
OAT/\ ... 1.J.-10.-eo
120 LET G•ATNCB.-A) Jl5 OATI\ -·01
150 NEXT T
Ir A>O THEN 200 J20 IF' 1\BS<P) "" 2 THEN 160
I 40 155 ENO
160 LET G•G+J.1<4159 JJO LET 1<{0• l l"'P
160
200
GOT(l 2410
Ir B>O THEN 2410
"""
NC.6 J•O
JSO
LET l<CQ,2J"X
LET O•Q+l
220 LET G•G•2•Jo I 4159 0 J60 GCTIJ 160
2-40 F'CR X•l TON I• J70 LET P•O
260 LET RI •Rt C l/N) JO JBO FOR I•N TO 1 STEP -I
280 LET Gl•G/N 390 LET P•P+P( ll•X• <I-I l
JOO LET Al•Rl•COS<Gl l 400 N(X7 1
320 LET Sl•Rl•SIN<Gl > '
I• 410 RETURN
.'
340 PRINT "C"F'NRCA1)",·•f'NRCB1l"1" -112 420 ENO
360 LET G•G•2it3· 1"'159 HUN
380 NEXT X NO·•
400 PRJNT
420 GOTO 60 2&
430
440
460
460
Rl.Ot
OATA Q,J,4
OATA 1, 1,2
DATA
ENO
!.013 .,.' 'IJ
110
.
182 15'
1
2
-2
-1
J
INTEGRAL ZERO cS l 1
-11
INTEGRAL ZE!lOCSll
-4
NONE FOUND
11
2
-2
NClo2 ABSCPl•2
DONE x '
TAKE: ROOTS \IF' COMPLEX NUMBERS IN CA,B> F'ORH
THE A 4 TH ROOTS OF' C 0 , I ! AREt
< ,924 , ,3g3 1 J -78 -so
c-.JBJ , .9<!4 INTEGRAL ZEROCS)I 6
c-.924 .-.JeJ Problem No. 8 DONE
' ,353 .-.924
.
170 I I 270 END
190 f'OR )(:110 TO I STE!' 2 j HUN
200 LET SCXl=OtXl-TtX
210
215
NEXT X
LET S"O
' '
'15
10
'
10
I
5 SEARCH 1'0R HEAL Zt.RCS or A POLYNOMIAL
FOR X.:110 TO 2 STE!'
.
220 STA'iT• [NQ, INCHEMENT7-195, l95.1
. '21
20 15 I
2JO LET S=S•SCX) 1 I INTEMVAL.<Sl dtGIN ATt
""" "
21
240 IF S:>O THEN 260 2 J
"
6 26 10 26 6 I -.i!
250 PRINT StXl"X1 .. X-\' I NOW STA.tT. ENQ, INCHEHENT72,J,.J
260
270
NEXT X
PRlNT SC 11
10
II
""
55
120
165
126
210
'30
126
252
'62
"
210
'62
"
120
'30
•5
165
10
55
I
II
INTE~VAL<Sl
2.6
BEGIN ATI
-·
DONE
•-1 X• J • 0
10
15
20
JO
DEF' F'NRCXl•INTCX•RNO<il•X./2l
F'OR T•l TO 3
?iUNT
PrtJNT "ZEROS AREt
20
JO
40
50
60
L£T
,-OH
LET
L£T
A•O
X,.-5 TO 5
Sl•F'NTCXl
S2•F'NTCX .. I >
I" Sl•Sl?•O TH£N 90
40 LET S•I
Problem No. 4 50 F'OR X•I TO 4
70 LET A•A .. I
fJO L£T StAl•X •
20 DIM PC20l 60 LET P( X)•O 82 IF X o F' THEN 90
100 rDR W•l TO 10 10 NEXT X 84 IF' Sl ... 0 THEN 90
120 LET f'CWl•S(W)•PCWl•PCW+!Ol•O BO LET F'C2l•S[2J•I 86 PRINT X"I S A ZEHO"
l"O NE:itT w 90 LET A•B•2 90 NEXT X
160 READ A 100 LET F'[ ll•F'NRC20) 92 JF' S2 o 0 THEN I 00
165 I,. A•- I THEN 620 110 PRINT -F'CllJ 9"4 PnlNT x.. S"IS A ZEno"
IBO F'OR X•A TD ! STCP -I 120 LET SCIJ•F'NRC20l 100 PIHNT "INT£RVAL<S> SEGIN ATt"
200 READ rt Xl IJO PRlNT -StllJ 110 FOH J•I TO A
220 PRINT F'tX)J 140 F'OR I•! TO A 120 PrilNT SCIJJ
2-40 NEKT X 150 F'OR J•I TO B I JO NEXT I
260 PRINT 160 LET PCl•J-ll•PCl•J-ll+F'tll•SCJl l.tO PHINT
280 R£AD 8 170 NEXT J 150 Pri!Nf
300 F'OR Y•B TO I STEP - I !BO NEXT I 160 F'OR l•I TO A
320 READ S(Yl 190 IF' S•O THEN 270 170 LET F'•S£ IJ
3•0 PRINT S{YlJ 200 LET S•O 180 LET 11•1
360 NEXT Y 210 F'OR X•I TO 4 190 LET S•f"•J I
380 PRINT 220 LET F'[Xl•P(X} 200 LET D•<ll•F'NT<f'll/f,-NTCF'l-,-NTCS))
"00 F'Or1 1•1 TO A 230 LET P( Xl•O 210 LET X•F'•O
"20 F'OR J•t TO B 240 NEXT X 220 IF' F'NTOO o 0 THEN 250
""o LET PCl+J-IJ•P(l•J-ll•F"CIJoS(Jl 250 LET A•.3 230 PRINT Xl"IS A ZERC"
"60 NEXT J 260 GOTO 120 2.tO GOTO J.tO
.. ao NEXT I 2'10 PltlNT ?.50 IF' ABSCF'NTCXl>>oOOOI THEN 280
500 FOR l•A•B-1 TO I ST£P - I 215 PRINT "THE COEFF'JCIENTS AREi ••1 260 PIUNT XJ"YIELDS F'NTOO •"IF'NTOO
520 PRINT P[ZJJ 280 F'On X•4 TO I STEP •I :?10 GOTO .3"40
SAO NEXT Z 290 PRINT PCXJJ 280 IF' F'NTCF'>•F'NTCX>•O THEN JIO
560 PRINT JOO NEXT X 290 LET 11•0
580 PRJNT J05 PRINT JOO GOTO 190
585 GOTO 100 310 NEXT T 310 L£T I 1•11-0
590 DATA 5,3,2.0.2.1 J20 ENO J20 LET F'•X
595 DATA J,2, 5,2 RUN J30 GOTO 190
600 DATA 2,3,2 N0.2 J.tO NEXT i
605 DATA 2•2•3 .350 END
610 OATA -I <UN
620 ENO ZEROS AHE 1 I 4 N0·4
Rt.fl THE COEF'F"JCIENTS ARE1 I -12 J9 •28
NO•o4 INTEH:VAL<S> BEGIN ATI
ZEROS AREi -• B -2 2 J
THE COEF'F'ICIENTS AREi l
-l.4.t1l4 YIELDS F'NTCXl a 6.10352E-05
12 ZEROS AREi 6 -1 2·8751.l YIELDS F'NTCX> •-6. IOJ52E-05
THE COEF'F'ICIENTS AREi I -252 Jo90475 YIELDS F'NT(X) •-J.05116£-05
2
J DONE
IJ
""
115 NE1.T J
250 GOTO JO F'O~ X•N- I TO I STEP - I
MAT PrllNT AJ 61 SS
SEC. 11-4 Cont'd
·~
•::Il
,)::)
F:Nf.)
12J
250 LET P(XJ•POO+P(X>ll•R
260 IF Xotl THEN 280
270 PH INT P[ Xj •
280 NEXT X
JOO F'Ori X• I TO N-1 Problem No. 8
JIO LET P[X)•P{X+l) 10 OH Ac2 .. ::i:1 .. ':!c2.21.cr2.:n.ou'!.2l
J20 NEXT X ?:O ;~AT .~EAO fl., d
325 LET PCNJ"O 30 OATA -2.1.1.s.-.s.1.2.:114
JJO IF" N•4 THEN J60 -41) MAT C•A•d
J 40 LET N•N-1 51) "'r<INT '"A•tl"
J50 GOTO I JO SEC. 12-2 60 MAT f>HINT CJ
J60 LET O•PC2)T2•·0P( ll•P(ll 10 ~IAT O•!:i•A
370 LET F"•2•PCJJ Problem No. 2 RO ,,,lJNT "~•A"
JBO LET A•-Pt2l/F' 9'.l •.::n P,HNT DI
20 OIH AC4.3J.8[1,4J,Ctl•JJ
390 LET 8•SOR<A85<01J 'f' 100 F.tJO
<40 P'!AT READ A
IF' D«O THEN ""O 60 DATA 6• J,2, 5, 3,2, 4, 3,.2, 3, 2• I n!JN
PH'JNT "REAL ZEROS' "I 1.:10 MAT READ B NC.a
420 PHINT A+SJ"ANO 'JA-B
160 DATA 0.0,010
430 GOTO 40
240 FOR Rzr.1 TO I
4'10 PRINT "NON-1-lEAL z:ROS1'"
450 PRINT "(''JAJ"•"l8•"1 ANO t"JAl"•"•·dJ"~"
260 F'OR C•I TO 3
460 GOTO .110
2so LET crn.cJ .. o
JOO F'OR X• l TO 4
470 DATA .11,1,2,-1,-2
480 DATA s.2.s.-:11.-21.<115
320 LET CCR.Cl•CtR.Cl•B!R.xJoACX.Cl
3<40 NEXT X A•A
490 DATA •0101 1
360 NEXT C
500 E:ND
360 NEXT A
500 PRINT "ROAO"•"TUNNEL"•''FIHIDGE"
520 MAT PRINT C
999 ENO OCNE
I ?. - I -2 INTEGRAL ZERO I
SYNTHETIC OlVtSJON 0Y X - I YlELOSI RUN
NOo2
1 J 2 RtAL ZEROS! ·I ANO Problem No. 10
2 5 -JI -21 45 INTEGRAL ZEHOI HOAO TUNNEL BHIOGE 10 01'1 A[J,12l•Btl2•1J.CCl2•121
SYNTHLTJC OIVJSlON SY X --5 YIELOS1 0 0 0 21) MAT r<EAO A
2 -5 •6 9 INTEGf<AL ZERO 1 JO l'l'i:STOr'IE
SYNTHETIC DIVISION BY X - I YIELOS1 OCNE 4•) "IAT READ 6
2 •J -9 HEAL ZEROSt J AND 51) MAT C•9•A
Problem No. 4 60 '1AT PRJ~T CJ
20 DIM AC-4•Jl.8(4,4},C(-4.J)
too DATA j,l!;,J·•·5·6·1·ff·9· IO. I I· 12
200 ENO
40 MAT READ A
CHAP. 12
60
140
DATA 6,J,2,S.:l.2,4,3,2,3,2,1
MAT REAO 6
""'
NO.JO
160 DATA 0.0.0.1.o.o.1.o.o. j,0.0.1.0.0.0
10
SEC. 12-1
240
260
F'CH R= I TO 4
F'OR C• I TO 3
.
-
280 LET CCR,.Cl•O n n u
Problem No. 2 300
J20
J""-R X"l TO <4
"T CCR.Cl•CCR,Cl•BCR,X}oA(X,Cl
10 1?.
" IA
..
21 ~ JJ J6
20 HAT READ A J60 NEXT C
JO F'OR X•I TO 12 J60 NtXT R
40
50
LET ACJ,XJ11A!2•Xl•:\( 1.X]
NEXT X
500
520
999 ENO
PRINT "ROAO","TUNN£L"1"BHIOGE"
MAT PRINT C
" ?.O u
" • H m
60 MAT PRINT AJ
RUN
65 DATA 1,2,3,4,5.6,7,23,51,41,S6.2J.:i ~ a M
66
67
DATA 2.-3,43,9Q,45,32.-a9.6S • .:i3.-96.o.1
DATA Q,Q,Q,O,Q,Q,Q,Q,Q,Q,O,Q
N0o4
10
" 20 ?.5 J5 5$
J5
2J
" •7 56 2' "
_,
.. ..
" .lllJ 6" IJIJ
" 2' J?. 56 RO 96
OCNE
DONE
"' Problem No. 6
$0 'l>J
10 20
" '° 10 9') 11)0 110 l?.0
..
10 PIUNT "BE.CAUSE THE OlMENSlONS ARE"
Problem No. 4 20 PRINT "NON-CONF'CRHING"
JO END
..
10 DIH At.:i,;l) :??. JJ 5$ 99 110 121 132
20 MAT MEAD A RUN
JO
40
MAT PRINT AJ
F'OR R,.I TO 4
N0.6
., !JP.
SO
60
F'OR C•I TO 3
LET ACR.CJsJ•Atrl.Cl
BECAUSE THE OIHENSICNS ARE
NON-CONFORM I NG
12
" 60 H !!4 9JS, lOfl l?.O 144
"'"
UATA J,6"/,J?.,45,91),~,9,57,.3,1
>'.NO
..,0
Problem No. 4
10
20
JO
DIM CCJ,3J,K(3.1l,S(J,1J.UJ,Jl
HAT READ C•K
HAT !•!NVCC!
..
410 HAT S•l•K
SO HAT PRINT S
S7
,_, 100
105
DATA 2,-9,-5,7,-6,S,9,-6,5
DATA 2,-J5,-39
-201 -J:;> 110 ENO
R<.N
N0o41
-2.
OON'i: I CONCLUDE THAT HATRI X
MULTIPLICATION JS NOT .999995
COHMUTATI VE
Problem No. 8 -l
OONE
10 0111 Xt"!.6l
20 F'01t tt=J TC 2
J J F'O.t C= I TV I> Problem No. 6 00NE
<10 Ll:T xt11,CJ-=!
o.H'I t\£2•2l-'H'=',?J,ct~.~J.IJ(:>,~J
SD N'O:·q C \'!
12 !JJ ·1 E:C ;>, 21, F' f ~ • .> J ,G[ ·,?, ~ l
Problem No. 6
61} ~£lliT -r
20 ~AT l'.:ll,L) A, B, C 10 DIM Ct 41, 41l1K[ 41, I), H 41,41), SC -t• IJ
7 l "lf!.T '""<INT r.1
R0 )0 UATA 1,:>,.1,4,- l,-.,,'),J,?.,-11,:><;, JI 20 MAT READ C.K
M) "'!AT D•A•'> JO MAT l"INllfC)
-?•JN
'.>O 'IAT F.:"A•C 410 MAT S•l•K
NC.i'I
'>0 ."'\AT l'=U•F'. 50 HAT PRINT S
70 P-!INT "A•lhA•C" 100 OATA 6,J,6,S,-7,S,-7,-1
R<l .'IAT P•!INT f"1 102 DATA -J,1,3,6,-2,-41,41,-7
90 "1AT G"lhC 105 DATA -12,71,31,-76
1')0 !>'AT "'"~*G 110 ENO
l 111 f>1/JNT "A•[4•tl" R<.N
l?O MAT P.{!NT F'I N0.6
130 END
·>·
Problem No. 10
10 DIM A(?,;i)
?.J ' F'C ~ X: l TO ;> A•~Hl•C
JV >O·: Y=l TC R 61 55
40 L'.:T A(X,y}::"1
t'° SIJ N£.H 'I'
Cll
co 6".I Ni:XT X
DONE
l'() SEC. 12·4 Cont'd <40 NEXT ;~
O')
a
_, 50
60
F'OR Xn'4 l(I J STEP •I
L£T Cnc.-x
Problem No. 8 "10 N£XT ;(
60 PRINT C"STRAIGHT CDMMJTTEE:S"
DIM CtJ.J],l({J, I 1.sc: • l J,{[J.JJ 90 LE'T C,.13•12/<2•1>
~O MAT READ C,I( TRN<A>•TRN(B> 100 LET Cl•l7•16/C20l
Jf} MAT J•INV<C> :i JI 12 110 LET C2•C•CI
40 MAT S:l•I< 120 PRINT "TWO GIRLS AND TWO BOYS.. C2
50 1'1AT ?RJNT S -11 lJO LET C•1J•l"IOl6•15,CU2•3)
100 DATA 2141•3•·9,-J,Q, .9,5,5 i "O PRJNi "ONE DOY ANO THREE GJRLS"C
105 DATA -I! .91SA.S16601 SB 75 150 LET C•l7016415•1,.l<••l•2•1>
110 ENO 160 PRINT "Al.I.. GJRLS"C
rl'JN 170 LET C•13•12oll•IO/C4'•J•2•l>
NO.a DONE 180 PRINi "ALL IJOYS"C
200 ENO
R'-"
CHAP.13 Na. is
o6rlOOVI
27'105 STRAIGHT CBMMITT££S
.~99998
SEC. 13-2 TWO GIRLS AND TWD BOYS 10608
ON£ Bl'IY A!IO THR££ GIRLS 88o40
DCNE Problem No. 2 AL.L. GIRLS 2J80
10 LET N"I AL.L. BOYS ?IS
Problem No. 10 20 F'Orl X:'14 TO I STEP -1
JO LET N•N•X DONE
10 OH-• cuo.101.Kt10.11 uio.101.sno.11
.tO NEXT X
I?. REM W£ AL.LOW !JP TO Tl:N UNKNOWNS
20 READ N 50 LET N•N/2 Problem No. 20
?.S Ir N•O THEN 999 60 PRINT N
70 END 10 LET H"Hl•I
JO MAT !IEAO CtN.NbKCN. 11
20 Fen; )(n52 TC 52-5•1 STEP •1
40
50
?HINT '"COEFPICIENT M•1T1UX"
MAT PHINT CJ """
N0.2 30
o40
LET H"HOX
NE.XT :t
60 PRINT "CONSTANT T£HM;;"
4.J5891E+IO 50 FOR Xn5 TD I STEP •I
70 MAT Pl{INT I(
55 L.ET HnH/X
80 MAT I•ZE1HN.Nl
OONE 60 NE.XT ;<
90 MAT S•ZEtUN, 11
100 FOR it•Se-s TO (52-S>-5•1 STEP -1
100 MAT I•INV<C>
110 L.ET lll•HlaX
110 MAT S•I•K Problem No. 4 120 NEXT X
120 P1HNT "SCLUTIONS"
I JO MAT P'ttlNT S JO t.ET P•J 130 FBR :t•S TB I STtP •I
\ 40 GOTO 20 20 FOrl X•25 TO I J STEP ·I lo40 I.ET lll•Hl/X
JO l.ET p,,,p•X 150 NEXT X
?.01) DATA 21J• l•5•-J17.?.
?.10 DATA J,?,,J,-i•J•O•l11••?.,-5 40 NEXT X 160 PRINT H•Hl"PAIRS er FIVE CARO HANDS"
?.?.O DATA 21hl),6 so •on x"' 12 TCl I STEP - I 200 END
?.30 DATA 0 60 t.ET P:mP•X R'-"
999 ENO 70 NEXT X Nll•20
RON 80 PRINT P
NO.JO 90 ENO J.9866SE .. 12 PAIRS er FIVE: CARO HANDS
RUN
CC£F"1Clf.NT MATIHX N0,4 DONE
3
1-55112£•25
DONE
SEC. 13-3
,
CONSTANT TEltMS
Problem No. 6 Problem No. 2
21 10 LET C=I
10 LET N.,2
20 Fl:IR X• 52 TO 52- 5+ I STEP -1
SCL!JTIONS JO l.ET C•c..-.;
20 ron X.. 9 TO I STEP • j
3 30 I.ET N"N•X
40 NEXT X
50 POR )';•5 TO I STEP •I
.-o NEJ;T l:
50 I.ET On!
60 LET C•C/X
60 ,.OR x .. 10 Ta t STEP -1
70 NEXT X
COEF"F'ICIENT MATRIX
80 PRINT C"HANDS"
70 l.tT o.. O•X
?. J -1 BO NEXT Jt
90 ENO
90 PRINT NIO
RUN
., 100
RUN
ENO
CONSTANT TERMS
20 DONE
2·59B96E+06 HANDS
..
NO.e
OON£
Problem No. 4
..
SOl.UTlCNS
,_
Problem No. 8
5 LtT N•t
10 nJR X•365 Te
20 LET N•NOX
J65·S•1 STCP •I
10
eo
30
<110
L.ET Nril
,.DR x.. se re
LtT N"N•X
NEXT ll
52-5• I STEP •I
10 .
"
2J
17
i. 30236£-02
5-426S9E-02
·155045
o0JS462
6o97279E-02
·224773
"
..
12
lJ
16
15
II
1
.29071
0323011
• 161506 ..
o5154BJ
0838495
,.
JJ 10
20
LET P•<J••OOl•0•·999rl00
PRINT t-P q
1.a12soE-OJ
3·9062SE:-03
lo95312E•03
J5 JO END
10 '1076562£•04
RUN
DONE DONE
Index
Index
ABS (Y) function, 47 Compound interest, 55
Addition calculating, 32
matrix, 186 Computer list, 35-36, 40-"15
modular, 67 dimensioning, 66
Algorithm, Euclidean, 91-92 Computer functions
Argument, definition of, 47 ABS(Y), 47
APPEND N statement, 118 ATN(X), 136
Arithmetic, modular; see Modular arith- DEF FNA(X), 57, 58, 61
metic INT(X), 47, 48, 51
Arithmetic operators, definition of, 4 RND(X), 62-63
Arrays, 78-79, 84 SGN(N), 47
DIM (two-dimensional), 85, 86, 89 SQR(Y), 47
dimensioning, 85-86 trigonometric, 133, 136
.tvIAT instructions in, 176 C0N, 187
structure of, 79 Conditional transfer, 18, 19
Artificial data, 20 Congruence conditions, 139
ASCII, 102, 115, 116 Conversions
Assignment operator, 10 base-10 to base-2, 93-96
Assignment statement, 10 degrees to radians, 133
ATN (X) function, 136 dimensions, 50-51
Axis of symmetry, 123, 124 inches to feet and inches, 51, 54
Coordinate systems, 128
Base-2, 93, 94 polar, 144, 150, 151
Binary files, 118 rectangular, 132, 138, 143, 147
Binomial experiments, 212 C0S(X) function, 133
Binomial theorem, 212 Cosines, Law of, 139-142
Binomial trials, 210-213 CREATE command, 110
Birthday problem, 205
Block diagramming; see Flowcharting Data files, 109-121
access, 115
Circular functions, 132, 133 ASCII, 116-118
CLE; sec SCR binary, 118
Coefficient matrix, 192 data storage, 115
Column vector, definition of, 179 General Electric, 11.'5
Combinations, 200 Hewlett-Packard, 110-115
Comma, use of, 2, 3, 101 random access, 112, 118-120
Comma delimiter, 10-11 Data processing. 2
Complex numbers arrays, 78 ·
addition of, 148 questionnaires, 85
de Moivre's theorem, 152 tabulation, 73-77
division of, 148 DATA statement, 3-4, 101, 116
multiplication of, 148 Debugging, 218
polar form, 150, 151 Decision-making, 14
rectangular form, 147, 149 DEF, 57, 58
roots of, 152 DEG(X), 133
subtraction of, 148 Degrees, conversion to radians, 133
Complex zeros, 168-175 Delimiter, 73
Compound fraction, decimal value for, comma, 10-11
6 semicolon, 10-11
265
266 Basic BASIC
This is the follow-up BASIC text for those who want to extend their
expertise. It offers advanced techniques and applications, including
coordinate geometry, area, sequences and series, polynomials,
graphing, simulations , and games. # 5855-1, paper, # 5856-X, cloth,
192 pages.
III
HAYDEN BOOK COMPANY, INC.
Rochelle Park, New Jersey
ISBN 0-a104-6106-9