Basic Basic

Download as pdf or txt
Download as pdf or txt
You are on page 1of 286

FLIP- l

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

COMPREHENSIVE STANDARD FORTRAN PROGRAMMING


James N. Haag
BASICS OF DIGITAL COMPUTER PROGRAMMING (Second Ed.)
John S. Murphy
BASIC BASIC: An Introduction to Computer Programming in BASIC Language (Second Ed.)
James S. Coan
ADVANCED BASIC: Applications and Problems
James S. Coan
DISCOVERING BASIC: A Problem Solving Approach
Robert E. Smith
ASSEMBLY LANGUAGE BASICS: An Annotated Program Book
Irving A. Dodes
PROGRAMMING PROVERBS
Henry F. Ledgard
PROGRAMMING PROVERBS FOR FORTRAN PROGRAMMERS
Henry F. Ledgard
FORTRAN WITH STYLE: Programming Proverbs
Henry F. Ledgard and Louis J, Chmura
COBOL WITH STYLE: Programming Proverbs
Louis J. Chmura and Henry F. Ledgard
BASIC WITH STYLE: Programming Proverbs
Paul Nagin and Henry F. Ledgard
SNOBOL: An Introduction to Programming
Peter R. Newsted
FORTRAN FUNDAMENTALS : A Short Course
Jack Stelngraber
THE BASIC WORKBOOK: Creative Techniques for Beginning Programmers
Kenneth E. Schoman, Jr.
BASIC FROM THE GROUND UP
David E. Simon
APL: AN INTRODUCTION
Howard A. Peelle
ic BASIC
SECOND EDITION
An Introduction to Computer Programming in BASIC language

JAMES S. COAN

Community Computer Corporation


Germantown Friends School

[[]
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

Chapter I-Introduction to BASIC I


1-1 PRINT . I
1-2 READ-DATA 3
1-3 System Commands 4
14 LET 5
1-5 INPUT 6
1-6 Sample Programs 6
1-7 Comma and Semicolon in PRINT Statements 10
Summary and Problems for Chap. 1 11
Chapter 2-Writing a Program 13
2-1 Planning 13
2-2 REM 13
2-3 Flowcharting 13
2-4 IF-THEN 15
Summary and Problems for Chap. 2 22
Chapter 3-Loops and Lists 24
3-1 Introduction to Machine-Made Loops 24
Summary and Problems 31
3-2 More on Loops 31
Summary and Problems 34
3-3 Introduction to Lists 35
Summary and Problems 38
3-4 More on Lists 40
Summary and Problems 45
Chapter 4-Computer Functions 47
4-1 Introduction to INT( ), SQR( ), ABS( ), and SGN( ) 47
Summary and Problems 50
4-2 Reducing Common Fractions and Dimension
Conversions 50
Summary and Problems 54
4-3 Program-Defined Functions 55
Summary and Problems 61
4-4 Random Number Generator 62
Summary and Problems 71
Chapter 5-Elementary Data Processing 73
5-1 Introduction to Data Processing 73
Suu.1rnary and Problcrn:; 77
5-2 Arrays 78
Summary and Problems 84
5-3 A More Detailed Questionnaire Analysis 85
Summary and Problems 89
Chapter 6-Specific Applications 91
6-1 Euclidean Algorithm 91
Problems 93
6-2 Change Base 93
1 1
T>
rrouiems 98
R-8 T.ookinrr at Tnterrers Dirrit bv Dirrit 98
Proble~s ~ ~ · ~ 100
Chapter 7-Strings and Files 101
7-1 Introduction to Strings 101
7-2 Strings-The Substting Scheme 103
Summary and Problems 105
7-3 The String Array Scheme 106
Summary and Problems 108
7-4 Introduction to Data Files 109
7-5 Hewlett-Packard Files llO
Summary and Problems ll4
7-6 General Electric Files ll5
Summary and Problems 121
Chapter 8-The Quadratic Function 122
8-1 Zeros 122
8-2 Axis of Symmetry and Turning Point 123
8-3 Plottjng the Parabola 125
Summary and Problems for Chap. 8 131
Chapter 9-Trigonometry 132
9-1 Introduction to SIN(X), C0S(X), and TAN(X) 132
9-2 Right Triangles and Arctangent 133
Summary and Problems 137
9-3 Law of Sines and Law of Cosines 138
Summary and Problems 142
9-4 Polar Coordinates 143
Summary and Problems 145
Chapter IO-Complex Numbers 147
10-1 Fundamental Operations 147
Summary and Problems 149
10-2 Polar Coordinates 149
Summary and Problems 151
10-3 Powers and Roots 152
Problems 153
Ch~pte1· ll-P0!y!!0!n!2!s 154
ll-1 Fundamental Operations 154
Summary and Problems 159
ll-2 Integral Zeros 159
Summary and Problems 163
11-3 Real Zeros 163
Summary and Problems 168
11-4 Complex Zeros 168
Summary and Problems 175

Chapter 12-MAT Instructions in BASIC 176


12-1 Introduction to MAT Instructions 176
Problems 179
12-2 Solving a Problem 180
Problems . 183
12-3 Operations and Special Matrices 185
Summary and Problems 188
12-4 Solving Simultaneous Linear Equations 190
Summary and Problems 193
12-5 Transpose of a Matrix 194
Problems 196
Chapter 13-Elementary Probability 197
13-1 Introduction 197
13-2 Enumeration 197
Summary and Problems 203
13-3 Simple Probability 204
Summary and Problems 206
13-4 Random Simulation 206
Problems 209
13-5 Binomial Trials 210
Problems 214
Appendix A-Storing Programs on Paper Tape 215
A-1 Introduction 215
A-2 Punching Paper Tape Off-Line 215
A-3 Reading Paper Tape 216
A-4 Getting the Computer to Punch Paper Tape 216
Appendix B-Error Diagnosis 218
B-1 Introduction 218
B-2 Errors that Prevent RUN 218
B-3 Errors that Terminate a RUN 221
B-4 Errors that Cause Unexpected or Incorrect Results 222
Summary 224
Appendix C-Special Formatting Functions 225
C-1 TAB(X) 225
C-2 IMAGE Statement 227

Appendix D-Summary of Flowchart Shapes 228

Appendix E-Summary of Statements in BASIC 229


Matrix Instructions 230
Functions 231
Files 232
Appendix F--Index of Programs in Text 235

Appendix G-Answers to Even-Numbered Problems 240

Index 263
Basic BASIC
An Introduction to Computer Programming in BASIC Language
CHAPTER 1

Introduction to BASIC

In working with a computer, you, the programmer, must communicate


with the computer. In order to do that you will have to use a language that
the computer will understand. There are many languages written for this pur-
pose. The language of this text is called BASIC. The actual physical com-
munication is rather complicated and 'we will ignore most of the mechanics
except for the apparatus at our end of things. Th.e device we will be using is
called a remote terminal. It will have a specific name depending on the manu-
facturer. The remote terminal has a keyboard, which is the part we are most
concerned about.

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

This time the result will be as fo"Ilows:


23. 4•91= 2129.4
0111NE

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:

10 DATA 23.4,91.83.19.67•94. 76.S.98.876.918


20 READ A, 8
30 PRINT MB
35 GJT0 20
40 E"ID

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.

<!UT 0r DATA Pl LI 'IE 20


4 Basic BASIC

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.

1-3 SYSTEM COMMANDS

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

At this point you do have enough information to write quite a few


programs. However, another statement type that may be use<l to make life
easier is the LET statement. The LET statement may be use<l to assign any
number or any algebraic expression to any variable. Using a LET statement,
the last program would look like this:

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.

0UT 3F DATA IN LINE 20

We obtain the same results as before. In this pm ticular program, we really


did not save anything. However, in any situation where we nee<l to write the
value of A 0 B several times or the expression is more involved, we will see
that a saving may result. There are many things that you could not do
without a LET capability.
/ 6 Basic BASIC

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

INPUT TW0 NUMBERS:?15.17


THE NUMBERS ARE: 1 5 I7
THEIR SUM rs: 32
THEIR PR0DUCT IS: 255

D0NE

INPUT
Causes the computer to request data from the keyboard.

If we want the computer to obtain a decimal value for a compound frac-


tion, there may be several programs that will do the job. Here we will have
to use our knowledge of the order of operations as determined in algebra.
Introduction to BASIC 7

Three programs follow that find a decimal value for


2/5 + 3/7
3/4-1/3
10 LET i\1=2/5+317
20 LET 0=314-113
30 PRINT N/O
40 El\IO
RUN

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

Parentheses serve as a powerful tool in grnuping terms properly for the


desired results. Keep in mind that priorities are exactly as they are in algebra
and that if several operations of the same priority appear in the same line, they
are executed from left to right.
Carefully study the programs which follow to see how the placement of the
parentheses affects the results.
10 PRINT "3/S/3/S="J 3/S/3/S
20 PRINT "3/(5/31S>="J 3/C5/31S>
30 PRINT "3/S/C31S>="J 3/S/C3/Sl
40 PRINT "C3/S)/C31S>="I C3/S)/(3/S)
SO PRINT "C3/S/3l/S="IC3/S/3)/5
60 PRINT "C3/S)/3/5="J C3/Sl/3/5
70 ENO
RUi'I

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.

5 PRINT "X",, nyn,, "Q",, "P",, "S"


10 DATA 1·31E+l0o2·13E+ll.1 ·16132E-05.2.83E+0 6
20 READ x,y
26 LET Q=X/Y
40 LET P=X•Y
50 LET S=X+Y
60 PRINT x,y,Q,p,5
65 GlilTlil 20
70 END
RUN
Introduction to BASIC 9

x y Q p s
lo3IOOOE+IO 2ol3000E+ll 6d5023E-02 2o79030E+21 2o26100E+ll

lol6132E-OS 2·83000E+06 4· 10360E-12 32.8654 2·33000E+06

0UT 0f OATA IN LINE 20

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:

10 PRINT "ITEMS","UNIT PRICE","C0ST","SUBHlTAL"


20 DATA 2,. 35, 3, 2· 65, 11,. 251 1,9. 49, 35, 1. 59
25 LET T=O
30 READ N, P
40 LET T=T+N*P
45 PRINT 1\1,P,N*P,T
50 G!H0 30
70 END
RIJ>'l

ITEMS U'lIT PRICE C0ST SUBrnTAL


2 • 35 • 7 • 7
3 2-65 7. 9 5 'l· 65
11 .25 2· 75 11. 4
I 9. 49 9. 49 20.'39
35 1. 59 55.65 76. 54

0UT 01' DATA !'I LI·~E 30

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

25 LET T = 0, it "says" that there is a location in the computer storage area


vihich thi::; progn:un vvi!l call T and that this program also requires that the
number zero be stored in that location for now. If we then say 26 LET T = 5,
then the computer will put the number 5 in that iocation designated as T and
zero will no longer be there. If we write a program that says 25 LET T = 0
followed by 26 LET T = T + 1, then the computer goes to the location where
it is storing the value for T, "sees" 0, adds 1 to it, and returns the result to the
location from which it just got 0, thereby replacing 0 (the old value) with 1
(the new value). So we see that in BASIC (as in other computer languages) =
does not mean "two names for the same thing." It means, instead, that the
number on the right is to be placed in a location whose name is specified on
the left. Thus we see that the equals sign as used here really specifies an opera-
tion for the computer to perform. So the equals sign is called an assignment
operator or a replacement operator, and the LET statement is called the
assignment statement or replacement statement.
Let us go through the program line by line. The lowest numbered line is a
PRINT statement. So, right off, the computer prints the headings. Then it
recognizes that the next statement is a DAT A statement and ignores it. Line
25 assigns the value 0 to T. Then in line 30 the computer reads the first two
numbers in the DATA line. Line 40 says that the previous value of Tis to be
taken out of storage and added to N times P. So, the first time through line
40, the value of T on the left will be 0 (from storage) plus the cost of two
items at $.35, or $.70, and the computer returns the value .70 to the location
in storage called T. Line 50 sends the computer back to read the next two
numbers in the DATA line and to add their product (7.95) to .70 to get 8.65.
It should be clear that we are printing the values of N, P, N times P, and T
each time we read two new numbers. This process continues until the com-
puter finds no more data. This causes the computer to terminate the RUN.

1-7 COMMA AND SEMICOLON IN PRINT STATEMENTS

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

8) The G0T0 statement is used to alter the prngress of the computer


during the execution of a program.
9) The END statement may or may not he required. If 1 equired, it must
carry the highest line number in the program.
10) The system commands to date are RUN and SCH or CLE. System com-
mands must not be preceded by line numbers.

PROBLEMS FOR CHAP. I 0

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

introduce flowcharting by an example. Suppose we want to add the counting


uu111bers frorn 1 to 50 including l and 50. 'l'lc Vvyill need t·.vo "/~1riab!es: one
for counting and the other to keep track of the sum. We want to start the
counting variable at 1 and the summing variable at 0. Then for every value
of the counting variable we want to add the counting variable to the old
value of the summing variable to get a new value of the summing variable.
Figure 2-1 represents a rough flowchart for such a process.
Figure 2-1 attempts to break the problem into its most fundamental steps.
By using a diagram of this kind, we are able to show the direction we must
foliow to do the problem. vVe would like to have each step small enough for
the computer to handle with one BASIC statement. However, this will not
always be practical. In our example, though, it will be both practical and
reasonable to have each step be a BASIC statement. With that in mind we
redraw the diagram using statements more nearly like those in BASIC lan-
guage. At the same time we will introduce the more standard practice of
having different shapes for boxes that indicate different kinds of functions.
The shapes used for this example are listed in Fig. 2-2 and the new flow-
chart is Fig. 2-3(A).
This time we are very close to being able to write the program directly
from the flowchart. Of the statements in Fig. 2-3(A), the only one for which
we do not yet have a corresponding BASIC language statement is decision-
makihg. BASIC has a statement type that allows us to alter the path of the
computer through a program depending on whether an algebraic sentence is
true or false.

Start

Start counting variable


Cat 1

Start summing variable


S atO

Add summing variable


and counting variable

no Add 1 to
Have we done
counting
all 50?
variable
yes

Print sum

Fig. 2-1. Diagram for adding counting numbers from 1 to 50.


Writing a Program 15

Used for beginning and


( Terminal )
ending of programs.

Operation Indicates that a computation


is to be performed.

Shows that a question is being asked


and a decision is being made.

Indicates that results are


PRINT to be printed and will also
READ be used to indicate the
READ statement.

Small circle shows that we are


going to transfer to another
statement in the program. N
will match another small circle
in the same chart.
Fig. 2-2. First five shapes
Arrowheads will indicate direction in all cases. used for flowcharting.

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

LET C = C +1 END SO LET C = C +1

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

BASIC allows us to give programs names. This requires a system com-


mand and will vary with the system tied in with your terrninaL Some systems
use the command NAME-, while others use NAME without the hyphen. After
the system command, you type the name to be used. Being able to name pro-
grams will be helpful to us here as we will be able to refer to programs by
name from now on.
We will give the last program a name, insert some REM statements to
explain the function of certain lines, and add a label to make the printed
result clearer. It is always recommended that you wtite programs with the
thought that someone else will be reading them and you may not be there to
do the explaining. You may even find that you cannot understand your own
Writing a Program 17

SUMI

3 HEM THE EXECUTIVE PH0GRAM ALL0WS US T0 GIVE 0UR PH0GRAM A


·~AME
~ HEM THE HESTHICT!0NS 0N NAMES VARY FR3M SYSTEM T0 SYSTEM
8 HEM *****
9 HEM WE AHE ADDJ,\JG INTEGEHS 0NE THR0UGH 50 IN THIS PR0GRAM
10 LET C=I
2D LET S=O
30 LET S=S+C
38 HEM HAVE WE ADDED 50 C THE LAST NUMBER T0 BE ADDED> YET?
40 IF C= 50 THEN 70
48 HEM WE HAVEN'T ADDED 50 YET*** S0 WE ADD 0NE
~O LET C=C+ I
60 G0T0 3D
68 HEM WHEN C=SD WE PHINT S <THE SUM> IN LINE 70
70 PRINT S
80 END
RUN
SUMI

1275

D0NE
70 PRINT "THE SUM 0F THE INTEGERS FR0M 0NE T0 FIFTY IS"; S
HUN
SUMI

THE SUM 0F THE INTEGERS FR0M 0NE T0 FIFTY IS 1275

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

SUM 0F 0DD NUMBERS FROM 5 TO 1191 IS 355212.

D0NE
18 Basic BASIC

Start

LET N = 5

LETS= 0

LETS= S + N

yes
PRINTS

LET N = N + 2 c END

Fig. 2-4. Flowchart for adding


odd integers from 5 to 1191

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.

Fig. 2-5. Flowchart for changing program SUM2 so that


the sum is printed several times during the program.
20 Basic BASIC

SUM3

S PRINT "SUM 0F' 000"


6 PRINT "NUMBERS FR0M"
7 PRINT "FIVE Tlil"• "IS"
10 LET N=S
20 LET S=O
28 REM LINE 30 ADDS THE NEW NUMBER Tlil THE SUMMING VARIABLE·
JO LET S=S+N
40 IF N<9 THEN 90
SO IF N= 731 THEN 90
60 IF' N> 1168 THEN 90
68 REM .l\DD 2 !N L!NE 70 !"!Zit? @DD NUMBERS
70 LET N=N+2
tlU u0 11.1 .JO
90 PRINT N,S
100 IFN<ll91 THEN 70
110 ENL>
RUN
SUM3

SUM 01' 000


NUMBERS FR0M
Fl VE rn IS
5 s
7 12
731 133952.
1189 354021·
1191 355212·

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

Fig. 2-6. Flowchart for using dummy data in program T¢TAL.

T0TAL

5 HEM THIS PR0GRAM IS A M0DIFICATI0N 0F A PR0GRAM THAT WE DID


BEF!JRE.
10 PRINT "T0 fAL C0ST =$";
20 DATA 2,.35,3,2.6s.11,.2s,1,9.49,35,1.s9,o,o
21 KEM THE DUMMY DATA IN THIS DATA LINE IS , , ,
25 LET T=O
30 HEAD N, P
34 IF N=O THEN 45
40 LET T=T+N*P
42 G0 T0 30
45 PRINT T
70 END
RU,~
T0TAL

f0TAL ClilST =$ 76. 54

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.

PROBLEMS FOR CHAP. 2


Unless instructed otherwise, draw a flowchart for every problem that calls for
a computer program to be written. Also use REM liberally.
1) Write a short description of each of the following terms: flowchart, dummy
data, IF-THEN, REM, conditional transfer, unconditional transfer.
2) In the program TIZ)TAL, why did we use two O's for dummy data? Why
couldn't we have used just one 0 since line 34 only tests to see if N O?
3) Bill took four tests. His marks were 100, 86, 71, and 92. vVhat was his average
score?
4) Modify the program SUM2 to count the number of odd numbers from 5 to
1191 by first modifying the flowchart.
5) Three pairs of numbers follow in which the first number is the base and the
second number is the altitude of a triangle: 10, 21; 125, 8; 289, 114. Write a
program to print in good form the base, the altitude, and the area for the three
triangles.
6) Find the number of and the sum of all positive integers greater than 1000
and less than 2213 divisible by 11.
7) A man is paid 1¢ the first <lay t1n the job, 2¢ the second day, 4¢ the third <lay,
and so on, doubling each day on the job for 30 days. You are to calculate his wages
on the 30th day and his total for the 30 davs
8) \ii/rite a program to print the inlegt'rs from 1 to 2.5 pairt'd with their reciprocals.
9) Write a program to print tl1e integers from 75 to 100 paired with their
reciprocals.
10) Rewrite the program T0T AL to count the number of different itt'ms in the
order and print the total.
11) A C'Ustomt'r put in an order for four books which retail at $5.95 and carry a
25% <liseou11t, three records at $.3.})8 \\·ith a 15% discount, and one record player for
$3\:J.\:J5 on which there is 110 discount. ln addition, there is a 2.io discount aiiowed
on the total order for prompt paymt'nl. \\'rite a program to compute the amount
of the order.
12) \Vrite a program to balance a checkbook that includes the following transac-
tions: Sept. 2, deposit $9.00; Sept. 5, write a check for $3.24; Sept. 10, write a
Writing a Program 23

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

Loops and Lists


3-1 INTRODUCTION TO MACHINE-MADE LOOPS
A computer loop may be defined as a self-repeating sequence of program
statements. This being true, loops are nut new to us. Most of the programs
we wrote in Chap. 2 used a loop. In those programs we initialized a variable
with the idea that we would be adding a fixed numher repeatedly and doing
something each time we added the fixed number. Let us draw a flowchart and
write a program to simply print the integers 1 through 6. See L00Pl and
Fig. 3-1.

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

Fig. 3-1. Flowchart for L00P1 for printing six integers.

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

Fig. 3-2. Flowchart for L0(Z)P2 for testing X before it is printed.

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+

5 PRINT "X 2*X X-10 Xt3 XI ( - 3 >"


10 F"0R X=l T0 6
30 PRINT X!2*X!X-!OlX•3lXl(-3)
50 NEXT X
99 END
t<UN
L00P3+

x 2*X x-10 X•3 XI ( ·-3>


1 2 -9 I -.333333
2 4 -8 8 -.666667
3 6 - 7 27 -1
4 8 -6 64 -1.33333
5 10 -5 125 -1.66667
6 12 -4 216 -2

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

10 REM **THIS PR0GRAM IS INTENDED T0 DEM0NSTRATE S0ME 0r


20 REM **THE CAPABILITIES 0r THE r0R-NEXT STATEMENT PAIR
30 REM
40 PRINT "50 r0R X=l4 T0 20 PR0DUCES THE r0LL0WING VALUES r0R X"
50 r0R X=14 T0 20
60 PRINT XJ
70 NEXT X
80 PRINT
90 REM BASIC ALL0WS US T0 INCREMENT A L00P BY VALUES 0THER THAN 0NE
100 PRINT
ilO PRINT "120 r0R X=I T0 19 STEP 2 PR0DUCESJ"
120 r0R X=l T0 19 STEP 2
130 PRINT XJ
140 NEXT X
150 PRINT
160 REM THE STEP NEED N0T INCREASE THE VALUE 0r X
1 70 PRINT
180 PRINT "190 r0R X=345 T0 282 STEP -9 GI VESJ"
190 r0R X=345 T0 282 STEP -9
200 PRINT XJ
210 NEXT X
220 PRINT
230 REM DECIMALS ARE ALL0WED IN BASIC
240 PRINT
250 PRINT "260 r0R X=91. 5 T0 3 STEP - 15· 7 YI ELDSJ"
260 r0R X=91.S T0 3 STEP -15.7
270 PRINT XJ
280 NEXT X
300 REM VARIABLES MAY BE USED T0 SET UP A MACHINE L00P IN BASIC
310 PRINT
320 PRINT "330 LET A= s, 340 LET B= 45, 350 LET C= 6 AND"
325 PRINT "360 r0R V=A T0 B STEP C GIVES THESE RESLl. TSJ"
330 LET A=S
340 LET B=45
350 LET C=6
360 r0R V=A T0 B STEP C
370 PRINT VJ
380 NEXT V
390 END

RUN
LUPDEM

50 r0R X=14 T0 20 PR0DUCES THE rlilLL0WING VALUES r0R X


14 I 5 16 I 7 18 19 20

120 r0R X= 1 T0 19 STEP 2 PR0DUCESJ


I 3 5 7 9 11 13 15 I 7 19

190 r0R X=345 T0 282 STEP -9 GI VESJ


345 336 327 318 309 30D 291 282

260 r0R X=9 l. 5 T0 3 STEP - 15. 7 YI ELD SJ


91.s 1s.8 60.1 44.4 13.

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.

__s_ta~0 Fig. 3-3. Flowchart for using machine loop for


program SUM3 from Chap. 20

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+

4 REM THIS PR0GRAM IS A M00IrICATI0N 0r SUM3 rR0M CHAPTER TW0


5 PRINT "SUM 0r 000"
6 PRINT "NUMBERS rR0M"
7 PRINT "rl VE T0''. "IS"
10 LET S=O
20 r0R N=5 T0 1191 STEP 2
28 REM LINE 30 ADOS THE NEW NUMBER T0 THE SUMMING VARIABLE•
30 LET S=S+N
40 I r N<9 THEN 90
50 lf' N=731 THEN 90
60 1 r N< 1188 THEN I 00
90 PRINT N. S
100 NEXT N
110 ENO
RUN
SUM3+

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+

5 REM THIS PR0GRAM IS A M00IFICATI0N 0F A PR0GRAM THAT WE DID


BEr0RE
20 DATA 2 •• 35, 3,2. 65, I I• .25, 1,9. 49, 35, I· 59, o,o
21 REM THE DUMMY DATA IN THIS DATA LINE IS ' • '
25 LET T=O
27 r0R X=I T0 50
30 READ N, P
34 I r N=O THEN 45
40 LET T=T+N*P
42 NEXT X
45 PRINT "T0TAL C0ST $"; n "THERE ARE"J x- t; "DirrERENT ITEMS"
70 ENO
RUN
T0TAL+

T0TAL C0ST $ 76.54 THERE ARE 5 DI rrERENT ITEMS

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

PRINT

7j Fig. 3-4. Flowchart for averaging test


scores for several people,
Loops and Lists 31

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

65 68 73 85 62 87 AVG= 76. 666 7


74 87 90 86 87 88 AVG= 85.6667
88 97 91 92 90 69 AVG= 9lol667
91 83 78 89 79 87 AVG= 84. 5
65 76 67 50 60 66 AVG= 64

0UT 0F' DATA IN LINE 30

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.

PROBLEMS FOR SEC. 3-1


Draw flowcharts for all programs unless instructed otherwise.
1) Add the counting numbers from 1 to 50 using F0R-NEXT.
2) Do problem 6) in Chap. 2 using a machine loop.
3) Do problem 7) in Chap. 2 with F0H-NEXT.
4) Do problem 8) in Chap. 2 using a machine loop.
5) Do problem 9) in Chap. 2 with FQlH-NEXT.
6) Find the sum of the reciprocals of all the integers from I to 1000.
7) Find the sum of the reciprocals of the integers from 900 tu 1000. Compare
this number with the result of problem 6).
8) Do problem 13) in Chap. 2 using a machine loop.
9) Find the sum of the squares of the reciprocals of the integers from 1 to 1000.
10) If you were given $1.00 today, $2.00 tomorrow, $3.00 the next day, and so
on for 12 days, how many dollars would you have been given? Suppose this went on
for 30 <lays. Then how much? Compare this with problem 3).

3-2 MORE ON LOOPS


In program AVG in Sec. 3-1, we went through the read and sum loop five
times, once for each person's test data. When we have the computer do the
same set of ope1 ations five times, we are actually using a loop. So let us
rewrite AVG with a loop F0R P 1 T0 5 and call it AVCCNC.
32 Basic 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

Suppose we want to calculate compound interest on $2000 at 4% com-


poun<led quarterly for nine years. When you take your savings account book
to the bank after the first of the year, it calculates the interest four times at
1% each time. In nine years you take the hook to the bank nine times. This is
an ideal example for nested loops. One loop goes from 1 to 9, with a loop
inside going from I to 4. This provides a good computer model for the actual
problem. As the program is written, it is a simple matter to have the principal
printed at the end of each year. A line may be inserted between 50 and 60
to prir.t the <w1uu11t after ead1 year. The program couid aiso have been written
using F0R X = .25 T0 9 STEP .25, or even F0R X = 1971.25 T0 1980
STEP .25. If you want to be able to <lo several problems at several interest
rates, then substitute a variable, which may be assigned as data, for .01 in
line 40. See CMPINT and Fig. 3-5.
Loops and Lists 33

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

8 REM START THE PRINCIPAL P AT $2000


10 LET P=2000
18 REM G0 FOR 9 YEARS
20 F'0R Y=l TO 9
28 REM G0 F0UR QUARTERS EACH YEAR
30 FOR Q= 1 TO 4
38 HEM COMPUTE THE INTEREST F0R THI~ QUARTER
40 LET J=.Ol*P.
48 REM ADD THE INTEREST TO THE PRINCIPAL
50 LET P=P+I
52 HEM *** WE CCULD HAVE USED LET P=P+.Ol*P HERE
60 NEXT Q
62 HEM AFTER FOU~ QUARTERS THE COMPUTER GFTS TO NEXT Y
70 NEXT Y
80 PRINT "AFTER 9 YEARS THE AMOUNT IS $"JP
90 END
RUN
CMPINT

AFTER 9 YEARS THE AMOUNT IS I 2861.54

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.

PROBLEMS FOR SEC. 3-2


1) In TRAGL we printed from I to the row number. Write a program to print
from the row number to 10 for ten rows.
2) Print the multiplication table up to 12 X 12.
3) Print the addition table up to 12 + 12.
4) Find the compound interest on $1000 at 5% compounded quarterly for 10
years. Print the amount after each year with the year number.
5) In problem 4), have the computer print the interest cumulatively each year.
6) Print a table showing compound interests on $1000 for one, two, three, and
four years at 4%, 4J2%, 5%, and 5)2% compounded quarterly. Print year 1 through 4 at
the top, and put the interest rate in the first column of eat:h row. Put the rate in a
loop F<l)R R =4 TO 5.5 STEP .5.
Loops and Lists 35

7) Redo problem 14) in Chap. 2 using nested loops.


/ 8) Write a program to read 10 numbers from data, find the largest number, print
it and the position it oecupied in the data line. This requires only one loop, but you
will have to read the first number directly from data outside the loop and then
have the loop begin with 2 to read the rest of the data. (This is essentially a problem
of storing values.)
/ 9) Write a program to print all sets of three integers less than 20 so that they
can be the sides of a right triangle. Print no duplications, i.e., if you have the
computer print 3, 4, 5, do not print 4, 3, 5.
10) Write a program to print the integers 1 through 50 in order in 5 rows of 10
columns eaeh.
11) Write a program to print the integers 1 through 50 in order in 10 rows of
5 columns each.
/ 12) Print a table of the squares of positive integers 1 through 29. Label the
columns 0 through 9 and the rows 0, 10, and 20. Make the entry in the table be
the square of the sum of the row and column labels.
13) Have the computer print the product and the sum of all possible different
pairs of integers from 15 to 20.

3-3 INTRODUCTION TO LISTS


Recall that in Chap. I it was stated that when a program specifies a vari-
able, the computer sets up a location with the variable as its name. This
provides a means for the computer to store numbers for later use. You are
about to meet a very powerful extension of that concept, the computer list. A
computer list sets up not a single location for a variable, but many locations
for a variable. If we use the computer for a list of say four items (we may
have many more) and choose to call it L, the different locations of the list
will be L[I], L[2], L[3], and L[4]. If we want the list to contain the numbers
4, 9, -92, and 8, this could be accomplished by saying LET L[I] =
4, LET
L[2] = 9, LET L[3] = -92, and LET L[ 4] = 8. The numbers in brackets
may be thought of as subscripts and they designate at which location of the L
list the number is being stored. However, the LET statement with explicit
subscripts is not really any better than assigning a different variable for each
number. So values are usually assigned in a loop with the subscript being the
variable in the loop. In the demonstration program LISTI we are letting S
go from I to 4 and reading L[S] horn data. There may be several lists in the
same program Any -letter of the alphabet may be used to designate a list.
At this point we are limited to 10 or 11 items in a list, depending on the
computer. If we have 10, they are numbered I through 10. Some computers
start at 0.
As you can see from the RUN of LISTI, we may use any or all of the
numbers in a list. We can print them forwards or backwards. We can re-
arrange them at will. We may look at the numbers in any identifiable manner.
Lists are inc1edibly useful when you learn to handle them.
Let us use lists and loops to write all possible combinations of four digits
in one list taken in pairs with four digits in another list. First we draw a
flowchart as in Fig. 3-6. We call the program PAIRS.
36 Basic BASIC

LISTI

8 REM WE ARE READING F"llJUR ITEMS llJF DA TA WI TH A LllJllJP


10 FllJR S=l T0 4
20 READ LCSl
30 NEXT S
38 PRINT "WE CAN PRINT THE ITEMS 01' THE LIST EXPLICITLY"
40 PRINT "LC l l LC 2l LC 3l LC 4l"
so PRINT LCllJLC2lJLC3lJLC4 l
60 PRINT
70 PRINT "WE CAN ALS0 USE A L00P· THE Lli!l\I GER THE LI ST THE
BETTER"
80 PRINT "BEING ABLE T0 USE A L011JP I~'
90
100 PRINT LCXJJ
120 NEXT i\
130 PRINT
135 PRINT
140 PRINT "WE CAN 0PERATE 0111 THE NUMBERS IN THE LI ST"
145 PRINT " B LCBl B>l<LCBJ"
150 F0R B= I Tl2I 4
155 PRINT BJLCBlJB>l<LCBl
160 NEXT B
170 PRINT
180 PRINT "WE CAN PRINT THE LIST BACKWARDS WITH F'121R X=4 Tl2I I
STEP -I"
190 F0R X=4 T0 1 STEP -I
200 PRINT LCXll
210 NEXT X
220 PRINT
225 PRINT
230 PRINT "WE CAN REL0CATE ITEMS IN THE LI ST"
240 PRINT "250 LET Z=LC!l, 260 LET LC1l=LC2l AND 270 LET
LC2J=Z"
245 PRINT "GIVE THE 1'0LL0WING RESLl.T"
250 LET Z=LCll
252 REM >1<>1<HERE WE ARE ST0RING THE VALUE 01' LC 1 l IN Z
260 LET LC1l=LC2l
262 REM WE HAVE PUT THE VALUE 0F LC2l INT0 LC 1 J
270 LET LC2l=Z
272 REM HERE THE llJLD VALUE 01' LCIJ IS PUT INT0 LC21 FR0M Z
280 PRINT "LC 1 l ="ILC 1 l
290 PRINT "LC2l ="ILC2l
300 PRINT "L00K CAREFULLY AT THE 0RIGINAL LIST"
310 PRINT "WE HAVE EXCHANGED ITEMS 0NE AND TW0"
320 PRINT
330 PRINT "WE CAN PUSH EVERY NUMBER UP F0UR LllJCATI0NS IN THE
LIST"
340 FllJR P=l T0 4
350 LET LCP+4l=LCPJ
360 NEXT P
370 F0R 111=1 T0 8
380 PRINT LCNJJ
390 NEXT Ill
500 DATA 4, 9, -92. 8
9999 END

RUN
LISTl

WE CAN PRINT THE ITEMS llJF" THE LIST EXPLICITLY


LCll LC2l LC3l LC4l
4 9 -92 8
Loops and Lists 37

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

WE CAN PRINT THE LIST BACKWARDS WITH F0R X=4 T0 I STEP -I


8 -92 9 4
WE CAN REL0CATE ITEMS IN THE LIST
250 LET Z=LCIJ. 260 LET LCIJ=LC2l AND 270 LET LC2l=Z
GI VE THE F0LL0 WING RESULT
LCll = 9
LC 2J = 4
L00K CAREFULLY AT THE 0RIGINAL LIST
WE HAVE EXCHANGED ITEMS 0NE ANO T\10
WE CAN PUSH EVERY NUMBER UP F0UR L0CATl0NS IN THE LIST
9 4 -92 8 9 4 -92 8
OONE

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

10 REM 20 T0 40 READ THE A LIST


20 F"0R I=I T0 4
30 READ ACil
40 NEXT I
45 REM 50 T0 65 READ THE B LIST
50 F"0R J=I T0 4
60 READ BCJl
65 NEXT J
67 REM HERE IS AN0THER NESTED L00P
70 F"0R K= I T0 4
80 F"0R L= I T0 4
r ,,. "\a,....,.,
90 PRINT I\
Mli.h..I" DLL.., ..
I "\

91 REM ***N0TICE THE USE 0r THE SEMiC0L0N AND THE C0MMA


100 NEXT L
110 PRINT
120 NEXT K
500 DATA 1, 3, 5, 7
510 DA TA 2. 3, 61 9
999 END
RUN
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.

PROBLEMS FOR SEC. 3-3


1) Using one READ statement in a loop, prepare a nine-element list using the
following numbers: 6, -89, 200, 31, 999, -999, 0, 1, and 18. Print out the list
across the page first in the order given, then in reverse order.
2) Fill a IO-element list with the squares of the subscripts. Print the element
number and the listed value in order in two columns down the page.
3) Prepare a IO-element list using the following numbers: 17, 18, 281, -722, 0,
-5, --16, 11, -1, and 10. Find the largest number in the list and its location. Print
them. Then exchange the largest number with the first element in the list and print
the new list with a loop.
4) Prepare one list with the numbers 6, 4, 11, 51, and 17 and another with 51,
12, 11, and 16. Now print all possible pairs using one number from each list.
5) Hepeat problem 4), without printing a pair if the numbers are the same.
6) Redo program T0TAL in Chap. 2 using ;m N list for numbers of items and
a P list for prices. Instead of N ° P use N[I] 0 P[l].
Loops and Lists 39

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]

PRINT NEXT I NEXT J


list

--..__E_N_~~ Fig. 3-7. Flowchart for arranging a 10-item list in


numerical order
40 Basic BASIC

3-4 MORE ON LISTS


We will now discuss the arrangement of a list in numerical order. If we
look at every pair of numbers in a list of numbers and they are all in numerical
order, then we are assured that the entire list is in order. Thus, we must
instruct the computer to look at all the pairs and determine whether or not
they are in order. If the pair is in numerical order, then we want the com-
puter to look at the next pair. If it is not, then we want the computer to
exchange the two numbers. We can accomplish this in the same manner as
was done in program LISTI. In other 'Nords, we store one of the numbers in
a new variable. Then we put the second variable's value into the first variable
and the original value of the first variable into the second variable from the
storage variable. The three statements look like this:
xxx LETS= L[I]
yyy LET L[I] = L[J]
zzz LET L[J] = S

ARANGE

10 REM WE ARE READING THE LIST F"R0M DATA


20 F"0R X=I T0 10
30 READ L(Xl
40 NEXT X
SO PRINT "HERE IS THE LI ST IN 0RI GIN AL 0RDER"
60 F"0R Y= I T0 I 0
70 PRINT Lt YlJ
80 NEXT Y
90 PRINT
100 REM N0W WE TEST PAIRS 0F" NUMBERS T0 SEE IF" THEY ARE IN
0RDER
110 F"0R I=I T0 9
118 REM WHY D0N'T WE SAY F"0R I=I T0 10????
120 F0R J=I+I T0 10
130 IF" L t l l <= L(Jl THEN 300
140 LET S=Lt 1l
ISO LET LCil=LCJl
160 LET LCJl=S
170 REM WE HA\IE RE.VERSED TW0 ELEMENTS 0F' THE LIST
180 REM **** SEE PR0GRAM LISTI ****
300 NEXT J
400 NEXT I
40S PRINT "AND HERE IS THE LIST IN 0RDER F"R0M SMALLEST T0
GREATEST"
410 F"0R Y=I T0 10
420 PRINT LlYH
430 NEXT Y
500 DATA 6, • 19.28.20.-32, 111. 19,28• 23• 43
999 END

RLl'J
ARAN GE

HERE IS THE LIST IN 0RlulNAL 0KU~K


6 -19 28 20 -32 74 19 28 23 43
AND HERE IS THE LIST IN 0RDER F"R0M SMALLEST T0 GREATEST
-32 -19 6 19 20 23 28 28 43 74

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

10 REM WE ARE READING THE LIST rR0M DATA


20 r0R X=l T0 10
30 READ LCXl
40 NEXT X
SO PRINT "HERE IS THE LIST IN 0RIGINAL 0RDER"
60 r0R Y=l T0 10
70 PRINT LCYJJ
80 NEXT Y
90 PRINT
95 PRINT "riERE WE ARE ARRANGING THE LIST"
!CO REM N0 ~! t-JE TEST PAI RS 0 F MUMBERS T0 SEE ! F THEY ARE IN
0RDl::k
110 r0R I= 1 T0 9
il8 REM WHY D0N'T WE SAY r0R I=l T0 10????
120 r0R J=I+ I T0 10
130 Ir LCI.l <= LCJl THEN 300
140 LET S=LC I l
150 LET Lll l=LC.Jl
160 LET LC.Jl=S
170 REM WE HAVE REVERSED TW0 ELEMENTS 0F THE LI ST
180 REM **** SEE PR0GRAM LISTI ****
200 r0R Y=l T0 10
210 PRINT LCYJJ
220 NEXT Y
230 PRINT
300 NEXT ,J
400 NEXT I
405 PRINT "AND HERE IS THE l.IST IN 0RDER rR0M SMALLEST T0
GREATEST"
410 r0R Y=l T0 10
420 PRINT LC YlJ
430 NEXT Y
440 PRINT
500 DATA 6.-19.28.20.-32.74.19.28.23.43
999 ENrl
RUN
ARANGl

HERE IS THE LIST IN 0RIGINAL 0RDER


6 -19 28 20 -32 74 19 28 23 43
HERE WE ARE ARRANGING THE LIST
-19 6 28 20 -32 74 19 28 23 43
-32 6 28 20 -19 74 19 28 23 43
-32 -19 28 20 6 74 19 28 23 43
-32 -19 20 28 6 74 19 28 23 43
-32 -19 6 28 20 74 19 26 23 43
-32 -19 6 20 28 74 19 28 23 43
-32 -19 6 19 28 74 20 26 23 43
-32 -19 6 19 20 74 26 26 23 43
-32 -19 6 19 20 28 74 28 23 43
-32 -19 6 19 20 23 74 26 28 43
-32 -19 6 19 20 23 28 74 28 43
-32 -19 6 19 20 23 26 26 74 43
-32 -19 6 19 20 23 26 26 43 74
A.'\ID HERE IS THE LIST IN 0RDER FR0M SMALLEST T0 GREATEST
-32 -19 6 19 20 23 28 28 43 74

....,,...."..::,,,
Loops and Lists 43
G0SUB

10 PR!NT "THIS PR0GRAM IS INTENDED T0 DEM0NSTRATE G0SUB'S


BEliAV10UR"
20 G0SUB 700
30 r0R X= 1 T0 3
40 G0SUB 500
45 G0SUB 700
50 NEXT X
60 G0SUB 400
70 PRINT 70
75 G0SUB 700
80 G0SUB 400
90 PRINT 90
95 G0SUB 700
100 LET X=4
110 G0SUB 500
115 G0SUB 700
120 G0SUB 400
130 PRINT 130
135 G0SUB '100
1 40 G0 SUB 600
150 PRINT 150
155 G0SUB 700
399 G0Tlil 999
400 PRINT "HERE WE ARE AT LINE"J
410 RETURN
500 PRINT "fHIS IS G0SUB 500"JXl"TlMES"
510 RETURN
600 PRINT "CALL G0SUB 400 l'R0M G0SUB 600"
610 G0SUB 400
620 RETURN
700 PRINT
710 RETURN
999 END
RUN
G0SUB

THIS PR0GRAM IS INTENDED T0 DEM0NSTRATE G0SUB'S BEliAVI0UR

l'HI s IS G0SUB 500 TIMES

THIS IS G0SUB 500 2 TIMES

THIS IS G0SUB 500 3 TIMES

HERE WE ARE AT LINE 70

HERE WE ARE AT LINE 90

THIS IS G0SUB 500 4 TIMES

HERE WE ARE AT LINE 130

CALL G0SUB 400 l'R0M G0SUB 600


HERE WE ARE AT LINE 150

D0NE
44 Basic BASIC

Let us look at a demonstration program before we use G0SUB in ARANGI.


Go through program G0SUB line by line to be sure you see what has hap-
pened. Line 10 is reasonably clear. Line 20 says G0SUB 700. Line 700 says
PRINT and the next line is RETURN. Thus the computer generates one blank
line and goes to line 30, which sets up a loop. Inside the loop, G0SUB 500
and 700 are called three times, once each for X =
1, 2, and 3. This program

ARANG2

10 REM WE ARE READING THE LIST !"R0M DATA


20 F'0R X=l T0 10
30 READ LCXl
40 NEXT X
SO PRINT "HERE IS THE LIST IN 0RIGINAL 0RDER"
60 G0SUB 410
9S PRINT "HERE WE ARE ARRANGING THE LIST"
100 REM N0W WE TEST PAIRS 0F' NUMBERS T0 SEE IF' THEY ARE IN
0RDER
110 F'0R I=l T0 9
118 REM WHY D0N'T WE SAY F'0R I=l T0 10????
120 F'0R J=I+l T0 10
130 IF' LC I l <= LCJl THEN 300
140 LET S=Ltl l
ISO LET LCIJ=LCJl
160 LET LCJJ=S
170 REM WE HAVE REVERSED TW0 ELEMENTS 0F' THE LIST
180 REM **** SEE PR0GRAM LISTI ****
200 G0SUB 410
300 NEXT J
400 NEXT I
40S PRINT•"AND HERE IS THE LIST IN 0RDER F'R0M SMALLEST T0
GREATEST"
407 G0SUB 410
408 G0T0 999
410 F'0R Y=I T0 10
420 PRINT LC Y lJ
430 NEXT Y
440 PRINT
4SO RETURN
SOO DATA 6,-19,28.20.-32, 74, 19.28.23, 43
999 END
ARANG2

HERE IS THE LIST IN 0Rl GINAL 0RDER


6 -19 28 20 -32 74 19 28 23 43
HERE WE ARE ARRANGING THE LIST
-19 6 28 20 -32 74 19 28 23 43
-32 6 28 20 -19 74 19 28 23 43
-32 -19 28 20 6 74 19 28 23 43
-32 -19 20 28 6 74 19 28 23 43
-32 -19 6 28 20 74 19 28 23 43
-32 -19 6 20 28 74 19 28 23 43
-32 -19 6 19 28 74 20 28 23 43
-32 -19 6 19 20 74 28 28 23 43
-32 -19 6 19 20 28 74 28 23 43
-32
-32
-19
-i9
6
0
19
i9
20
20
23
2;,
74
2ti
28
,,.
28
2ti
43
,..:;
-32 -19 6 19 20 23 28 28 74 43
-32 -19 6 19 20 23 28 28 43 74
AND HERE IS '!'HE LIST IN 0RDER F'R0M SMALLEST T0 GREATEST
-32 -19 6 19 20 23 28 28 43 74

D0NE
Loops and Lists 45

is not intended to actually achieve any particular result except to give us a


chance to trace out the path of the computer through several G0SUB
statements.
You might wonder why 399 G0T0 999 is in there. Consider what would
happen if it were not there. Line 155 says G0SUB 700, which means go to
line 700, execute a line feed, and return. Then what? Line 400 is next. Print
"HERE WE ARE AT LINE," and "RETURN." RETURN where? RETURN
in this subroutine responds only to G0SUB 400 and there was no such
statement. The computer cannot execute such a set of instructions and will
print a message to that effect. So you must build a barrier in front of sub-
routines to prevent the computer from accidentally beginning without the
proper G0SUB statement. Notice that lines 500, 600, and 700 are already
protected by RETURN statements.
Now we should be ready to enter the G0SUB concept into ARANGI. This
program is called ARANG2. Examine lines 60, 200, and 407. See the barrier
at line 408 to prevent accidentally beginning the subroutine.

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.

PROBLEMS FOR SEC. 3-4


1) Write a program to print the following numbers in decreasing numerical order:
34, -67, 10, 0, -99, 103, and L Count the number of times the computer has to
exchange two numbers and the number of comparisons.
2) Write a program to print the following numbers in increasing numerical order:
45, 76, - 76, 45, and 98. Do not print the duplicated number, but leave it in the list.
3) Program the computer to list the numbers in order in problem 1) by comparing
elements one and two first, then elements two and three, then elements three and
fom, etc. Create a switch S = 0 for off and S= 1 for on. Tum the switch off, then
if an exchange is required, tum the switch on. After testing the last two elements,
look at the switch. If it is on, go through the list again. If it is off, print the list;
it must be in order. Count the number of tests and the number of exchanges.
4) Prepare a five-clement list using the averages of the test scores from program
AVG in Sec. 3-1. Then arrange the averages in decreasing order and print a number
representing the position in the original list. This latter can be clone by setting up
a second list containing 1, 2, 3, 4, 5, then exchanging these numbers each time the
corresponding averages are exchanged.
5) Prepare one list with the numbers 0, 6, 1, 3, 7, 2, 3, 1, 4, and 9 and another
with 0, 8, 2, 3, 9, 7, 4, 1, 2, and 4. Prepare a third list with the sums of the corre-
sponding elements. So far this is similar to problem 11) in Sec. 3-3. Beginning with
46 Basic BASIC

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.

INT(X) computes the greatest integer of X.


SQR(X) computes the positive square mot of X.
ABS(X) computes the absolute value of X.
SGN(X) is +1 if Xis positive, 0 if X 0, -1 if X is negative.

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

x ABSCX> SQRCABSCX» INTCX> SGN CX:


-8999!3:: 8999!3:: 948e 637 -8999!3e - !
-35.2 j5.2 5.93296 -36 - I
- 0032 .032 • I 7666 5 - i -1
0 0 0 0 0
.032 .032 • 1 76665 0 I
23.412 23.412 4. 6366 23 I
6391 6391 91.6024 6391 I

0UT 0r DATA IN LINE 20

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.

Fig. 4-1. Flowchart for factoring integers.


Compute r Functions 49
PRIME I

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!

9 73 IS THE GREATEST F"ACT0R 0r 1946


1949 IS PRIME
1009 IS PRIME
59 IS THE GREATEST rACT0R 0r 1003
3667 IS THE GREATEST rACT0R 0r 11001
120 IS THE GREATEST rACT0R 0r 240
II IS PRIME
0UT 0r DATA IN LINE 10

So we stop at 2 rather than 1. First we draw the flowchart in Fig. 4-1,


then
write the program PRIME I.
In PRIME l the compute r tested 1949/D with 1947 different values for
D
before it decided that 1949 is prime. That is a lot of tries. Whenev er reasonab
le,
we should try to improve the efficiency of our program. What do we
know
about factors of integers? We know that the smallest possible factor is 2.
So the
greatest could be N/2. For 1949 then, we can reduce the number of tries
to
975. But we also know that if we try all possible divisors down to the
square
root of the number we are trying to factor, then the quotient s will also
be less

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

913 IS THE GREATEST rACT0R 0F 1946


1949 IS PRIME
1009 IS PRIME
59 IS THE GREATEST rACT0R 0r 1003
3667 IS THE GREATEST rACT0R 0r 11001
120 IS THE GREATEST rACT0R 0F 240
11 lS PRIME
0UT 0F DATA IN LINE 10
50 Basic BASIC

than the square root. So we might try F0R D =


N - 1 T0 SQR(N) STEP -1.
Well, SQR(l949) is approximatel y 44 and this means 1904 tries, which is much
worse. But why not go from 2 up to SQR(l949)? Now we have only 43 tries
and if we do get divisibility for other numbers, we will have the smallest
factor and we can get the greatest factor by dividing the number by its small-
est factor. This seems worth making the necessary changes in PRIMEL Only
lines 20 and 70 require changing. Line 20 is the line which sets up the loop
to test for divisibility and line 70 is the PRINT statement. In the PRINT
statement we want N/D printed now, whereas we wanted D printed before.
See PRIME2.

SUMMARY

Four computer functions were introduced.


1) INT(A) evaluates the greatest integer of A.
2) SQR(B) finds the positive square root of B.
3) ABS(C) computes the absolute value of C.
4) SGN(D) becomes +I if Dis positive, 0 if D is 0, and 1 if D is negative.
The value in parentheses is called the argument of the function.

PROBLEMS FOR SEC. 4-1

I) Modify PRIME2 to write all pairs of factors.


2) Modify the program in problem I) to print no duplications.
3) 'Write a program that will print only prime factors of integers.
4) V\lritc a subroutine that will perform the work of ABS( ), without using
another computer function.
5) \Vrite a subroutine that will perform the work of SGN( ), without using
another computer function.
6) \Vritc a program to print all different pairs of factors of the following set of
integers: 711, 991, -991, 453, -654, 1009, -1009, 9001.
7) vVritc a program to print all of the prime positive integers less than 100. Do
not let the computer try numbers divisible by 2.
8) Print the prime integers from 1000 to 1500. Do not let the computer test the
even numbers.
/ 9) For each of the following pairs of numbers, find two numbers so that the
sum of your two is the first number in the given pair and the product is the second
number in the given pair: 3, 2; 7, 12; 11, 28; -11, 28; 3, -28; 76, 1003; 7, 8;
34, 289.

4-2 !\EDUCING COMMON FRACTIONS AND DIMENSION


CONVERSIO NS

\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

\Ve find the INT( function useful in simplifying dimensioned numbers


to simplest form. Suppose we change 93 in. to feet and inches. By hand we
would divide 93 by 12 and the whole number in the result would be in feet.
Then the remainder would be in inches. The problem would appear as follows:

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,

9/12 93/12 - INT(93/12)

for this problem. Or in general, for N divided by D and calling the remainder
R we get

R/D = N/D - INT(N/D)

i\Iultiplying both sides by D 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

Fig. 4-2. Flowchart for reducing common fractions"

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

0UT 0r DATA IN LINE 10


Compu ter Functions 53
So all we need is a progra m statem ent LET R = N - INT(N
/D) " D. See line
20 in progra m DEMR EM.
DEM REM

5 PRINT "NUMER AT0R"• "DEN2J MINAT0 R"•


"REMA INDER" • "INTEG ER QU2JTI ENl'"
10 READ N•D
15 REM F"IND THE REMAIN DER WHEN 'N' IS DIVIDE
D BY 'D'
20 LET R=N-IN T<NID> *D
30 PRINT N.D.R. INT<N ID>
40 G0T0 10
50 DATA 93,12. 100.25 .365,5 2.365. 7
52 DATA 365, 12, 52• 13• 5280• 440, 55, 6
60 END
RUN
DEMREM

NUMER Al'0R DEN0M INAT0R REMAIN DER INTEGE R QU0TIE NT


93 12 9 7
100 25 0 4
365 52 1 7
365 7 1 52
365 12 5 30
52 13 0 4
5280 440 0 12
55 6 1 9
0UT 01' DATA IN LINE 10

Now we can easily conver t numbers in inches to feet and


inches. First see
the flowchart in Fig. 4-3 and then the progra m C0NV RT.

Start

LET F LET 11
= INT (1/12) =l-F•1 2

Fig. 4-3 Flowcha rt for convert ing


number s in inches to feet and inches.
54 Basic BASIC

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

9 INCHES 0 FEET 9 INCHES


86 INCHES 7 FEET 2 INCHES
47 INCHES 3 FEET 11 INCHES
37 INCHES 3 FEET 1 INCHES
947 INCHES 78 FEET 11 INCHES
480 INCHES 40 FEET 0 INCHES

0UT 0F DATA IN LINE 10

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.

PROBLEMS FOR SEC. 4-2


1) Write a program to add two simple fractions and print the sum reduced to
lowest terms.
2) Improve the eHiciency of program REDUCE by putting the smaller number in
the P loop in line 20.
3) Write a program to convert improper fractions to mixed numbers.
4) Convert inches to yards and feet and inches.
/ 5) Write a program to multiply two fractions, converting the result to a mi.xed
number in reduced form.
/ 6) Convert dollars in decimal form to the equivalent in coins.
7) Do problem 5) for adding two fractions.
8) For each of the following pairs of numbers, find the greatest common factor:
19~ 1083;27,35; 27,36; 16,34; 12,30.
9) For each of the following pairs of numbers, find the least common multiple:
190, 1083; 25, 745; 187,84.
10) Prepare a list consisting of the first 10 Fibonacci numbers. Find the greatest
common factor for every pair in the list, prepare a list of these \Yith no duplications,
and print them.
/ 11) Write a program to find the greatest common factor of sets of three numbers
assigned as data.
Computer Functions 55

4-3 PROGRAM-DEFINE D FUNCTIONS


Suppose we have $56.31 in a savings account bearing 4Jf% interest com-
pounded monthly and we hear of a bank that is offeiing 4Y.1% compounded
quarterly. Should we change banks? We did wot k with compound interest
earlier. So this should be a matter of doing two calculations in the same pro-
gram. Let us leave the $56.31 in each bank for 10 years and see if there is
enough difference to change banks. For compounding monthly, we use the
yearly rate divided by 12, and calculate and add the interest 12 times per year.
For quarterly compounding, we use the yearly rate divided by 4, and calcu-
late and add interest four times per year. In this case, use one loop for the
years and a 1 to 12 loop for monthly compounding and a 1 to 4 loop for
quarterly compounding, both inside the same 1- to 10-year loop. The flowchart
in Fig. 4-4 should help to sort out this plan.
Since the intent is to develop several concepts in this program that will
require changing the pt inting, the variables will be printed individually on
sepat ate lines. This technique may often save typing when you anticipate

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

Fig. 4-4. Flowchart for computing END


compound interest at two rates.
56 Basic BASIC

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

F0R TEN YEARS


@4.5% C0MP0UNDED M0NTHLY·•• BB.2374
@4.75% C0MP0UNDED QUARTERLY··· 90.2943

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

0UT 0F DATA IN LINE 10

we have a rounding function all our own as follows:


LET Y INT(X 0
100 + .5)/100
Let us try this with a few numbers to see that it actually works, before we
insert it in our banking problem. See R0UND. (It may often be wise to perfect
a technique in a smaller uninvolved program before trying it in a longer more
complicated one. There should be fewer sources of error in the final program.)
R0UND wmks out well. However, we often have more than one variable
that we want to round off. BASIC has a way of doing this. We may define
any function of our own using DEF FNA(X) =
ZZZZZZZZZZZZ, where X is
a dummy variable. It simply hokls a place where we will later enter the
variable for which we want the function evaluated. The format of our round-
ing function looks like this:
XXX DEF FNH(Z) = INT(Z 0
100 + .5)/100
XXX is the line number of the statement number of the DEFining statement
in a BASIC program. We may substitute any letter of the alphabet for H.
Thus, we may for example, DEF FNI( ) and DEF FNJ( ) for other func-
tions in the same program. The third letter is the one that identifies which
function we are calling for. We may define another function that rounds off
to tenths as ZZZ DEF FNT(G) INT (G • 10 + .5)/10 and whenever we call
for FNT( ), we round off to tenths. Let us see how this works out in program
DEF( ).

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 >

20 DEF FNH<H>=INTCH*I00+ .5)/100


30 DEF FNT<T>=INTCT*I0+.5 )/10
40 PRINT "X"11 "Y", "XIY", "FNH< X/Y >", "FN TC X/Y >"
45 READ x,y
50 PRINT x,y,X/Y,FNHCXIY>,FN TCXIY>
60 DATA 1, 11, Io, 3, 3, 4, 6. 11. 2, 3. 125, B, 6324
70 G0T0 45
80 END
RUN
DEF<>

x y X/Y FNHCX/Y) F1\il\X/Yi


11 9.09091E-02 .09 .1

10 3 3,33333 3.33 3.3

3 4 • 7S • 75 •8

6 11.2 .535714 • 54 •5

3. 125 8.6324 • 362008 • 36 •4

0UT 0F DATA IN LINE 45

Now we can alter om compound interest program BANKL We only need


to change two lines and insert the DEF statement. It is common practice to
put all DEF statements at the beginning of the program. Let us also put in
dollar signs ($).

2 DEF FNH<X>=INT<X*l00+ .51/100


140 PtUN T "$"FNH< P>
1 60 P i1 INT "$" FN HCP I >

RUN
BANK 1

F0H TEN YEARS


@4.5:4 C0MP0U.'IDE.D M0NTHLY···$ 88·24
@4,75z C0MP0U~DED QUARTE~LY ••• $ 90.29

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\;

devel<>p a sub1 outine.


\Vhat we me trying to do is to gain control oYer the number of spaces
bel\\ecn items ol p1inted ontp11L This implies getting the comp11tc1 to print
difle1cnt numbers ol spaces acrn1ding to ou1 ncccl This suggests putting XXX
Computer Functions 59

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.

132 LET X=4


1 38 G0 SUB 500
156 LET X=X-3
1 58 G0 SUB 500
490 ST0P
500 r0R S=l 1'0 X
510 PRINT " "J
520 NEXT S
530 RETURN

RUN
8ANK2

r0R TEN YEARS


@4.5% C0MP0UNDED M0NTHLY·•• $ 88.24
@4.75% C0MP0UNDED QUARTERLY•• • $ 90.29

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

FOR TEN YEARS


i!4.S% COMPOUNDED MONTHLY··· $ 68.24
@4.7S% COMPOUNDED QUARTERLY··· $ 90.29

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.

132 LET X=IO


RUN
BANK2

r0R TEN YEARS


@4o5% C0MP0UNOEO M0NTHLYooo $ BBo24
@4.75% C0MP0UNDED QUARTERLY··• $ 90.29

OONE

132 LET X=20


RUN
BANK2
Computer Functions 61

F0R TEN YEARS


114.5% C0MP0lJllOEO M0NTHLY··· $ 88024
114.75% C0MP0UNOEO QUARTERLY••• $ 90.29

OONE

132 LET X=3


RIJll
BANK2

F0R TEN YEARS


11405% C0MP0UNOEO M0NTHLY··· $ 88.24
114075% C0MP0UNOEO QUARTERLY•••$ 90.29

OONE

Note: See Appendix C for TAB( ) and PH.INT USING formatting


functions.

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.

PROBLEMS FOR SEC. 4-3


1) Find the square roots of the integers from 11 to 23. Print the integer, its
square root, and its square root rounded off to the nearest thousandth and to the
nearest tenth with appropriate labels.
2) How much money will you have in the bank, if you deposit $5 at the begin-
ning of every month for 25 years in a savings account which pays 4J2% compounded
monthly?
3) Define a function for Y = -3X2 + 7X - 3. Print pairs of values for
=
X -4 to 5.
4) Do problem 2), but for daily compounding. Ignore leap year; use 12, 30-day
months.
/ 5) Set up a table of amounts that $100.00 will be at the end of 5, 10, 15, and
20 years at 4%, 4J2%, 414%, and 5% per year compounded monthly. Put the rates in
a rate loop. Print the years across the top and the rates in the first column of each
row.
/ 6) Write a program to compare $99.00 compounded monthly at 4%%, quarterly
at 5%, and daily at 4%% for 15 years. Print with the decimal points lined up.
7) Define a function for Y = 3X + 4. Print pairs of values for X and Y for
X = -5 to 5.
8) Define a function for Y= 2X2 + BX - L Print pairs of values for X and Y
for X = -6 to 2.
62 Basic BASIC

4-4 RANDOM NUMBER GENERATOR


The last computer function we will consider in this chapter is RND(X).
Often in programming we want numbers to try out a new program. Also, there
are many events in mathematics and science that occur at random. If we do
not have any real data or we want a very large number of numbers, it is
desirable to have the computer pick the numbers for us. This can be done in
BASIC with the computer function RND(X).
RND(X) picks at random a decimal fraction between 0 and 1. It will not
pick 0 or I. DeP,ending on the computer, the value of .X n1ay be immaterial,
but there must be a number in parentheses. If the argnment does not affect
the numbers generated, it is called a dummy argumqnt. Some computers use
the argument to determine the numbers generated. Computers vary as to the
actual set of random numbers generated. Some have a fixed set of numbers that
is the same every time RND( ) is used in the same program. Such a random
number generator is called a pseudo random number generator. Others give a
different set of numbers each time you run the same program. Program
RND(l) is a short routine that prints a few random numbers.

RNDC 1 >

10 F0R X=I T0 10
20 PRINT RNDC 5»
30 NEXT X
40 END
RUN
RNDC 1 >

0 766345 0665051 • 595169 0265522 0656563

6o97632E-02 .209305 • 12793 • 363604 • 651426

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

Program RND(2) seems only to give integers 0 through 9. However, RND(Z)


will never take on 1 as a value, and therefore multiplying by 10 will never yield
10 as the product. But we can add 1 to each of the above integers and both
include 10 and exclude 0, which is exactly what we set out to do. The 1 can
be added either before or after taking INT( ). We get 1 to 10 in program
RND(3).

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+

10·0205 3.06177 7.18546 4·55652 1.66971 8000928

2.02798 9. 08411 5.25241 8· 75757


D0NE

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

Now, what shall we do with randomly assigned numbers? The possi-


bilities are endless. We could put some in a list and arrange them in numerical
order. Remember ARANGE? Instead of reading data, we can use randomly
assigned numbers. This time, let us not print the list after every exchange,
but only after it is in order. How about picking integers from l to 250? This
will require INT(250 ° RND(l) + n
This time let us rewrite the program to
look at successive adjacent pairs in the list. This method was outlined in
problem 3) of Sec. 3-4. As we have the computer look at each pair, we have
it decide whether the first is less than or equal to the second. If it is, then we
do not exchange-exactly as in ARANGE. But if the first is greater than the
second, we cail for the exchange. However, there is no guarantee that the list
is in order after the first time through. So we have to turn on a switch after
each exchange. Then after the computer has gone through the list comparing
1 and 2, then 2 and 3, then 3 and 4, etc., we have it check the switch. The

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 NUMBERS IN 0RDER


12 67 75 96 109 161 162 199 221 231
D0NE

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.

5 PRINT "HERE IS THE LIST AS IT IS BEING F'0RMED***"


30 PRINT LCXH
42 PRINT
RUN
ARANG4

HERE IS THE LIST AS IT IS BEING F'0RMED***


94 156 216 22 64 65 195 210 129 11
THE NUMBERS IN 0RDER
11 22 64 65 94 129 156 195 210 216
D0NE

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.

2 DIM L< 75>


7 LET N=20
10 F0R X=l T0 N
70 F'0R I = I T0 N- I
150 F0R X=l T0 N
RUN
ARANG5

HERE IS THE LIST AS IT IS BEING F0RMED***


41 246 236 83 248 119 107 195 85 128 134 25
73 93 27 204 111 208 122 241
THE NUMBERS IN 0RDER
25 27 41 73 83 85 93 107 111 119 122 128
134 195 go4 208 236 241 246 248
D0NE

The program seems to work nicely. Let us try a few other numbers.

7 LET N=5
RUN
ARANG5

HERE IS THE LIST AS IT IS BEING F0RMED***


71 86 6 141 I 72
THE NUMBERS IN 0RDER
6 71 86 141 I 72
OONE

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

HERE IS THE LIST AS IT IS BEING F"0RMED***


107 19S BS 130 13B 3B 112 209 127 s l S l 6B

s 13B 162 109 7S 9B 44 6 lB 1 77 30 213

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 )

F(l)R P = 1 LETT= INT


T9) 10 (12•RND(1) + 1)

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

8 H0URS FR0M 6 0 'CL0CK IT WILL BE 2 0 'CL0CK


33 H0URS FR0M 9 0 'CL0CK IT WILL BE 6 0 'CL0CK
27 H0URS FR0M 5 0 'CL0CK IT WILL BE 8 0 'CL0CK
33 H0URS FR0M 5 0 'CL0CK IT WILL BE 2 0 'CL0CK
31 H0URS FR0M 9 0 'CL0CK IT WILL BE 4 0 'CL0CK
32 H0URS FR0M 12 0 'CL0CK IT WILL BE 8 0'CL0CK
2 H0URS FR0M 9 0 'CL0CK IT WILL BE 11 0 'CL0CK
28 H0URS FR0M 4 0 'CL0CK IT WILL BE 8 0 'CL0CK
8 H0URS FR0M 10 0 'CL0CK IT WILL BE 6 0 'CL0CK
4 H0URS FR0M 11 0 'CL0CK IT WILL BE 3 0 'CL0CK

D0NE

In CL0CK2 we change lines 20 and 30 and insert line 5 to define FNC(


and list the program in full.

CL0CK2

5 DEF FNC<Z>=INTC 12*RNDCZ>+ I>


10 F0R P=l T0 10
20 LET T•FNCC I>
30 LET H•FNCC 1 >
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
CL0CK2

6 H0URS FR0M 6 0 'CL0CK IT WILL BE 12 0'CL0CK


7 H0URS FR0M 8 0 'CL0CK IT WILL BE 3 0 'CL0CK
7 H0URS FR0M 12 0 'CL0CK IT WILL BE 7 0 'CL0CK
8 H0URS FR0M 3 0 'CL0CK IT WILL BE 11 0 'CL0CK
5 H0URS FR0M 7 0 'CL0CK IT WILL BE 12 0 'CL0CK
4 H0URS FR0M 4 0 'CL0CK IT WILL BE 8 0 'CL0CK
7 H0URS FR0M 5 0 'CL0CK IT WILL BE 12 0 'CL0CK
4 H0URS FR0M 11 0 'CL0CK IT WILL BE 3 0 'CL0CK
11 H0URS FR0M 3 0 'CL0CK IT WILL BE 2 0 'CL0CK
10 H0URS FR0M 12 0 'CL0CK IT WILL BE 10 0 'CL0CK

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

5 DEF FNCCZ>=INT<l2*RNDCZ >>


10 FllJR P= 1 Till 10
20 LET T=F'NCC 1 >
30 LET H=FNCC 1 >
40 LET S=T+H
so IF' S <= 11 THEN 80 MAMO! 2
60 LET S=S-12
70 Gill Till 50 5 DEF FNC<l>=INf<l2*1iND<~»
80 PRINT H"+"T"="S"M0D 12" l0 r'=l TO 5
F::J,;
90 NEXT P 20 LET A=F·\JC<l>
100 END .)0 LF1 8=PJC< l >
RUN 40 LET S=A+B
MllJD12 50 PRINT A"+"B"=
60 G0Sll3 500

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

Get two num-


FOR p = 1
-bers 0 to 11
T¢5
A,B

LETS=A+B

Gt;l>SUB 500

LETS= A• B

END

NEXT P GqJSUB 500

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

PROBLEMS FOR SEC. 4-4


1) Print a list of 30 randomly assigned numbers from 2.00 to 20.00 with tenths
and hundredths permitted but no digits to the right.
2) Print a list of 25 integers from -200 to 200 assigned by a random number
function in increasing order.
3) Print the list in problem 2) to guarantee that there are no duplications. In
other words, if you generate a number that has already been used, generate another.
4) Prepare a list of the first 18 Fibonacci numbers. For all nonequal pairs find
the greatest common factor. Enter the greatest common factors in a list with no
duplications and print t.l:ie result.
5) Prepare a list of the first 20 Fibonacci numbers. For 1 2 to 19 print F[I] " 0 2 =
- F[I 11 ° F[I + l].
6) Use three lists to add two 20-digit numbers. Use one list for each number and
enter the digits one by one as elements in the list. Use the third list as the sum list.
Be sure to carry if the sum of the two corresponding digits is 10 or more.
7) Do problem 6) using two lists instead of three.
,/ 8) Use three lists to multiply two 10-digit numbers digit by digit. (Could this
be done with two lists?)
9) Modify program MAMD12 to find the remainder after dividing the value of
S by 12 to replace the subroutine that uses successive subtraction.
10) Write a program to do arithmetic mod 5 and mod 6, five problems each.
Put 5 and 6 in a data line and write one random function so that it generates 0 to 4
for mod 5 and 0 to 5 for mod 6.
11) Have the computer print the addition table and the multiplication table for
mod6.
12) Have the computer do subtraction mod 7.
13) Write a program in which the mod and the number of problems are selected
at random, and the problems are generated with random data.
14) Have the computer generate pairs of integers and find the greatest common
factor.
15) Have the computer generate sets of three integers and find the greatest
common factor.
16) Generate pairs of integers and find the least common multiple.
17) Generate sets of four integers and treat them as coefficients of two binomials
and find the three coefficients of the product; i.e., generate A, B, C, and D
in (AX B)(CX + +
D) and find E, F, and G in EX 00 2 FX G. Print all + +
five numbers in two groups, one group for A, B, C, and D and another for E, F,
andG.
I 18) Form two 20-element lists with integers in the same range. Form two other
lists. One list is to contain all numbers that appear in both lists, i.e., the intersec-
tion of the two lists. The other list is to contain a number if it is in either of the
original two lists, but only entering it once if it is in both lists. In other words, find
the union.
19) Fill a 25-element list with the first 25 positive odd integers. Fill a second
25-element list with the sum of all the entries of the first list up to and including
the subscript number of the second list.
C){\\
_ ...,./
11..f~.:I;,..
........................ J
rr olrv1 ·~ hn-..lln ,;_n_ ,_ hn··--
- - - .......................................................................................................
n-..l _,_ ... __
"'4 .......................................,.
CHAPTER 5

Elementary Data Processing


5-1 INTRODUCTION TO DATA PROCESSING

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.

Tabulating One Item


Let us ask some families how many television sets they have in their homes.
The answers will be numbers, one number per family. We can set up a list so
that the first element counts the number of families with one set and the Nth
element counts the number of families with N sets. Before we begin counting,
there will be zero families having each number of sets. So we will have to
initialize each element of the list at 0. Then when the number for a family is
read, we will add 1 to the element in the list corresponding to that number of
television sets. If the first family has one set, then we have the computer look
at T[l]. T[l] = 0 to start, and adding 1 makes T[l] = 1. The next time a
family has one set we have the computer add 1 to T[l], and then T[l] will
equal 2. The process is repeated until all data is read. We will have to use
dummy data, since we want to print the results only after we have tabulated
all data. We can draw a simple flowchart. See Fig. 5-1.
Of course we could allow for a larger number of sets by simply using a
longer list. We could have provided for zero sets by letting T[ l] tabulate 0,
T[2] tabulate 1, T[3] tabulate 2, etc. Then line 60 in program TV'S would read
60 LET T[N + l] = T[N + l] + 1
because, when N is 0, you want T[l] and when N is 1, you want T[2], etc.
Or we could use 0 subscripts if they are available.

73
74 Basic BASIC

Start

Initialize
four-item
list all O's

PRINT
headings

LETT[N] END
=T[N]+1

Fig. 5-1. Flowchart for tabulating number


of television sets per family.

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.

Tabulating Several Items

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.

Tabulating Yes-No Answers


vVe are not limited to numerical quantities. Suppose you .we1e to question
each of yom classmates about comses they want to take. If you ask, "Do you
want to take chemistry?" and the answer is "no," you can call that 0, and sim-
ilarly call "yes" 1. Let us ask people if they want to take the following courses:
chemistry, physics, French, Spanish, calculus. If someone says he wants to
take chemistiy, F1ench, and Spanish, his data will be: 1, 0, 1, 1, 0. We can use
one list to count all comses The fit st element of the list will count people who
want to take chemistry, the second will count people who want to take French,
etc. Before reading any data, we will have to initialize each element of the list
at 0. Then after 1eading the first person's data, we wa11t the list to be
1, 0, 1, 1, 0, which can be done by adding the number representing yes or no
to the number already in that location of the list vVe can get the computer to
read the data in g1 oups of five by using a loop F0R R = 1 T0 5, with the
HEAD statement and the tabulating statement inside. The real works of the
prngram will be the tabulating statement
LET C[R] = C[H] + K
where R is the loop available and goes horn 1 to 5 for each pe1so11's data, If
H 1, the comse is chemistry; if H 2, the comse is physics, etc. vVhere K is
76 Basic BASIC

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

NUMBER, TV'S, CARS, H0MES• BATHS


1 s 9 12 3
2 7 5 4 5
3 2 I 0 2
4 2 1 0 I
5 0 0 0 1
6 0 0 0 2
7 0 0 0 0
6 0 0 0 2

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

CHEM I srnY PHYSICS F"RENCH SPA"IISH CALCULUS


7 5 7 6 6

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.

PROBLEMS FOR SEC. 5-1


1) Modify program TV'S to total the number of television sets and the number
of families, and find the average number of sets per family rounded off to the nearest
hundredth.
78 Basic BASIC

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

CHEMISTRY PHYSICS FRENCH SPAN! SH CALCULUS


253 257 237 249 256

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.

TABLE 5-1. ARRAY STRUCTURE.

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

9 REM INITIALIZE A AT ONE


10 l.ET A= I
19 REM R0 WS G0 FR0M I Hl 3
20 F'0R R= I T0 3
29 REM COLUMNS G0 FR0M I T0 5
30 F0R C=I 10 5
40 LET HR, CJ=A
50 LET A=A+I
59 REM NEXT C0L,UMN
60 NEXT C
69 REM NEXT R0 W
70 NEXT R
80 PRI,\IT "AT THIS P0INT THE ARHAY IS FILLED"
999 ENO
RUN
ARRAY!

AT THIS P0INT THE ARRAY IS FILLED

D0NE

It is time for another demonstration program. In ARRAYl we simply fill a


3-row by 5-column array with integers 1 through 1.5 going first acrnss the
80 Basic BASIC

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

9 REM INITALIZE A AT 0NE


10 LET A=I
19 REM R0WS G0 FR0M I T0 3
20 F"0 R R= I T0 3
29 REM C0LUMNS G0 FR0M I T0 5
30 F"0R C= I T0 5
40 LET TCR.Cl=A
50 LET A=A+ I
59 REM NEXT C0LUMN
60 NEXT C
69 REM NEXT R0W
70 NEXT R
BO PRINT "AT THIS P0INT THE ARRAY IS FILLED"
65 PRINT
69 REM LET'S PRINT TC314l
90 PRINT "TC 3, 41 ="J TC 3, 4.1
100 PRINT
110 PRINT "WH0 LIVES AT C2,5l?"JTC2.5JJ"LIVES THERE"
120 PRINT
130 PRINT "LET'S L00K AT THE EN TI RE ARRAY"
139 REM INCREMENT R0W~
I 40 F"0 R R= I T0 3
149 REM INCREMENT C0LUMNS
I 50 F"0R C= I T0 5
160 PRINT TCR.ClJ
I 70 NEXT C
l 75 PRINT
I 76 PRINT
ISO NEXT R
999 END
RI.JN
ARRAY2

AT THIS P0INT THE ARRAY IS FILLED

TC 3, 41 = 14

WH0 LIVES AT C215l? 10 LIVES THERE

LET'S L00K AT THE ENTIRE ARRAY


I 2 3 4 5

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

WE PRINT THE 0RI GINAL ARRAY


I 2 3 4 5

6 7 8 9 10

II 12 13 14 15

WE CAN MULTIPLY EVERY ELEMENT IN THE 4TH C0LUMN BY 6


I 2 3 24 5

6 7 8 54 10

11 12 13 84 15

WE CAN SUBTRACT THE 3RD R0W F'R0M THE 2ND R0W


AND PUT THE RESLLT IN THE 3RD R0W
I 2 3 24 5

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

HERE IS THE 0RI GINAL ARRAY!


I 2 3 4 5

6 7 8 9 10

11 12 13 14 IS

N0W WE HAVE THE 4 BY 5 ARRAYJ


I 2 3 4 5

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

NUMBER, TV's, CAHS, H0MES1BATHS


I 5 9 12 3
2 7 5 4 5
3 2 I 0 2
4 2 I 0 I
5 0 0 0 I
6 0 0 0 2
7 0 0 0 0
8 0 0 0 2

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.

PROBLEMS FOR SEC. 5-2


1) Print an array with 3 rows and 6 columns filled with O's.
2) Print an array with 6 rows and 3 columns filled with l's.
3) Set up an array with 4 rows and 9 columns and fill it with random numbers
from -999 to +999. Print the array.
4) Fill the addresses along the top left to bottom right diagonal of a square
7 by 7 array with l's and all other locations with O's and print the result.
5) Fill two arrays of the same dimensions with random integers and print each
array. Then fill a third array with the sums of the corresponding entries from the
first two and print the result.
6) Fill two arrays of the same dimensions with random integers and print each
array. Then fill one of these two arrays with the sums of the corresponding entries
from each array and print the result.
7) Fill a 3 by 7 array with the integers 0 through 20. Print that array. Then
multiply each entry by the sum of the row number and the column number and
print the result.
8) Fill a 4 by 7 array with random integers from -500 to +500 and print the
result. Then multiply each entry by 2 and print that result. Insert the printing
routine using G0SUB.
9) Fill a 10 by 10 array with the addition table.
10) Fill:> 10 hy 10 :urny with thP m11ltiplir•ntinn tnhlP
11) Fill a 5 by 5 array with the addition table mod 5. Then have the computer
generate addition problems with a random number function and find the sum by
accessing the appropriate entry in the additon array.
12) Do problem 11) for the multiplication table mod 5.
j 13) Consider a questionnaire containing 10 questions with yes, no, or other as the
Elementary Data Processing 85

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.

5-3 A MORE DETAILED QUESTIONNAIRE ANALYSIS


Consider a questionnaire submitted to four categories of people: male-21 or
over, male-under 21, female-21 or over, and female-under 21. On this ques~
tionnaire there are 15 questions calling for yes-no answers. Our task is to
present a tabulated summary of the data collected. 'vVe can provide sample
data for say 10 people for the purpose of getting a first test RUN. Let us
refer to this first problem and the program as SURVEY. The flowchart for
SURVEY is drawn in Fig. 5~2.
The first computer problem we run into is, how do we get 15 rows in an
array? The answer is that we may dimension an array much the same as we
dimensioned lists. In the array DIM (DIMension) statement, we must specify

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

Fig. 5-2. Flowchart for program SURVEY.


86 Basic BASIC

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.

TABLE 5-2. CHART TO ORGANIZE SURVEY.

Array
Code in
Column Number Use DATA Line

1 Question number Position in line


2 Male 21 or over 2
3 Male under 21 3
4 Female 21 or over 4
5 Female under 21 5

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.

9 REM DIM SCJS,Sl SETS UP A'J A'<RAY \.:ITH 'HIGH<:.ST' UlCAT!(}'J


c 1 s. s1
I0 DIM SC I 5, 5 J
20 F3R R=I TO 15
28 HEM LINE 30 E"JTERS THE HO\, .'IU·'l8ER l''J THE FIFST COL'J.'l\J
30 LET scH.ll=H
Elementary Data Processing 87

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

QUEST MALE MALE FEMALEFEMALE


NUMB EK 21+ UNDE:R 21+ UNDEK
I 2 3 I
2 2 I I
3 2 2 2
4 I 2 I
5 I 0 2 2
6 I 0 2
7 2 I 2 I
B 2 0 I I
9 0 I 2 I
10 I 0 I 2
11 I 0 2 2
12 3 I 0 2
13 2 I 2 I
14 2 0 2
15 2 0 2 2

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

QUEST MALE MALE f"EMAL Er EMAL E


NUMBER 21+ UNDEK 21+ UNDEK MALE f"EMALE UNDER 21+ rnTAL
1 2 I 3 I 3 4 2 5 7
2 2 1 I 1 3 2 2 3 5
3 2 I 2 2 3 4 3 4 7
4 1 I 2 1 2 3 2 3 5
:; 1 D 2 2 1 4 2 3 5
6 i 0 2 I 1 3 I 3 4
7 2 1 2 I 3 3 2 4 6
8 2 0 I I 2 2 1 3 4
9 0 I 2 1 1 3 2 2 4
ID 0 I 2 3 2 2 4
II 1 0 2 2 4 2 3 5
12 3 1 0 2 4 2 3 3 6
13 2 I 2 1 3 3 2 4 6
14 2 0 1 2 2 3 2 3 5
15 2 0 2 2 2 4 2 4 6

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.

PROBLEMS FOR SEC. 5-3


I) Modify SURVEY to handle 75 questionnaires with random data.
2) Modify SRVEYI to tabulate the totals discussed with that program in the 16th
row of the S array.
3) Modify SURVEY to handle yes, no, and other as possible answers. Create
three arrays: one for yes, a second for no, and a third for other responses. Use
random data and 50 questionnaires.
4) Modify SRVEYI to generate random data for 50 questionnaires.
5) Modify SRVEYI to tabulate the results as percentages of the total number of
yes responses. Do not create a second array.
90 Basic BASIC

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

6-1 EUCLIDEAN ALGORITHM


In Chap. 4 when we first reduced common fractions to lowest terms, even
though the computer did the work, it was done the hard way.
For two integers N and D,
N/D =I+ R/D
or N =I 0 D + R

where I is the integer quotient and R is the remainder. If we successively


divide the remainder into the previous divisor until the remainder is 0, the last
divisor is the greatest common factor. This will always happen, even for
mutually prime pairs, as the last divisor will be 1.
Let us see what hap ~ for 13398 and 7854.

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

10 PRINT "N, D"I


20 INPUT N, D
25 IF" N=O THEN 120
28 REM F"IND THE INTEGER QU0TIENT
30 LET I=INTCNID >
38 REM F"IND THE REMAINDE R
40 LET R=N-1*D
48 REM IF" THE REMAINDE R IS ZER0 THEN D IS THE G,C•r•
so IF" R=O THEN 90
58 REM R WAS N0T ZER0, S0 WE ITERATE
60 LET N=D
70 LET D=R
80 G0Tlil 30
90 PRINT "G•C•r• ="JD
100 PRINT
110 G0T0 10
120 END
RUN
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

PROBLE MS FOR SEC. 6-1

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.

6-2 CHANGE BASE


In this section we are going to develop a program to convert base-10
numbers to base-2 numbers. You will recall that for base-2, only the digits 0
and I are permitted and each digit represents a power of 2 instead of 10.
94 Basic BASIC

One of the widespread uses for base-2 numbers is in computers themselves.


This is because in base-2, all numbers may be expressed by a set of switches
with 0 being off and l being on.
One difficulty that we quickly encounter is that whatever the digit capacity
of the computer we have access to, that number of digits provides a much
smaller number in base-2 than it does in base-10. We will use up to six digits
in the base-10 number for our program. In base-2 100000 is only 32
base-10 and
1111112 = 1 0 2 00 0 or 1
+1~2..,lll
or + 2
+ 1"2 "" 2 or + 4
+10200 3 or + 8
+10200 4 or +16
+10200 5 or +32
6310
which we could handle :asily with pencil and paper. Clearly, we are going
to have to work with more than six digits in base-2.
Let us assume that we can provide for as many digits as are needed. How
many digits do we need to represent the base-10 number 999999 in base-2?
We could write a program that would give that information, but we can also
figure it out ourselves. We can begin with 2 ° 0 5.
2 °0 5 = 32
=
2 ° 0 IO 32 ° 0 2 =
1024
0 =
2 ° 20 1024 ° 0 2 1048576 =
So, if we provide for up to 2 ° 0 20, we can handle six-digit integers with
room to spare. We know how many digits we need, now we have to figure
out how to make the conversion.
Let us run a sample conversion before we attempt to write the program.
We use 149 base-IO here. First find the greatest integer power of 2 that is
less than 149. It is 2 ° 0 7 or 128.

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

999999. BASE [EN 0 0 0


0 0 0 0 0 0

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

GUT GF DATA F-1 LINE. I 0

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

and call for a RUN:

RUN
BASE-2

999999. BASE TEN =011110100001000111111 BASE TWO

BASE TEN =000000000000000000001 BASE TWO

16 BASE TEN =000000000000000010000 BASE TWO

0UT OF' DATA IN LINE 10

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

45 BASE TEN =000000000000000101101 BASE TWO

9875 BASE TEN =00000001001101001001 I BASE TWO

123456. BASE TEN =000011110001001000000 BASE TWO

0UT 0F' DATA IN LINE 10

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

PROBLEMS FOR SEC. 6-2


1) Eliminate the leading O's in BASE-2. Be careful not to eliminate all O's.
2) Write a program to convert base-2 numbers to basc-10. It may help to put
the digits of the base-2 number in a list.
3) Write a program to add two numbers in base-2.
4) Have the computer convert numbers in hase-10 to hase-3.
j 5) Write a program to convert from base-10 to base-12. It is conventional to use
T for 10 and E for 11 in base-12.
j 6) Have the computer convert base-3 numbers to base-2.
/ 7) \Vrite a program to convert bn.se-10 numbers to any ba~e up to 12 \vith the
base determined from data.

6-3 LOOKING AT INTEGERS DIGIT BY DIGIT

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

where I is the integer quotient found by


00
I= INT(N/IO E)
and an iterative process whereby the 11ew N is the old R and the value of E
is decreased by 1 for each iteration. Solving for R we get

R=N-I 0
I0° 0 E

All of this should begin to look familiar.


For six-digit intPgPrs th1~ vnl111~ of F. will h:ivP tn hP!'.in :it ~ ;mrl gr_> tr_i (\
STEP -I. Carefully study prngram DIGIT and you will see that we have
indeed broken integers into their separate digits. However, as always, we should
look for ways to improve our programs. One change that will save a little paper
Specific Applications 99

DIGIT

I0 PRINT "INPUT ANY INTEGEH"l


20 INPUT N
30 IF N=O THEN 999
40 FOK E= 5 TO 0 STEP - l
so LET I=INTCN/IO•E>
60 PRINT I
70 LET R=N-I*IO•E
80 LET N=R
90 ·'llEX T E
100 PRINT
110 GOTO 10
999 E.'llD
RUN
DIGIT

INPUT ANY I NTEGE1l? 123456


I
2
3
4
5
6

INPUT ANY l'llTEGER?819045


8
I
9
0
4
5

INPUT ANY INTEGER?53627


0
5
3
6
2
7

INPIJT ANY INTEGEK?O

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

INPUT ANl' INTEGER?l23456


I 2 3 4 5 6
INPUT ANY I NTEGER?9 75432
9 7 5 4 3 2
INPUT ANY INTEGER? 5362 7
0 5 3 6 2 7
INPUT ANY INTEGE1<?0

DONE
.100 Basic BASIC

Now let us see the program with the change and try another number.
(See DIGITl.)

DI GIT I

10 PRINT "INPUT AN'{ INTEGER"I


20 INPUT N
30 IF N=O THEN 999
40 F0R E=5 T0 0 STEP -I
50 LET l=INTCN/IO•E>
60 PRINT IJ
70 LET R=N-1¢JQ!E
60 LET N=R
90 NEXT E
100 PRINT
110 G0T0 10
999 END
HUN
DI GIT I

INPUT ANY INTEGER?666666


6 6 6 6 6 6
INPUT ANY INTEGER?O

D0NE

One last consideration is that we might want to eliminate the leading O's.
We leave this as an exercise.

PROBLEMS FOR SEC. 6-3


1) Eliminate the leading O's in DIGIT Be careful not to eliminate all zeroes.
2) Test integers for divisibility by 3 by summing the digits.
3) Construct the integer formed by reversing the order of the digits in an INPUT
integer. Print the result as an integer.
j 4) Test integers with the integer formed by reversing the order of the digits to
find the greatest common factor.
j 5) Find all three-digit integers that are prime. Form new integers by reversing
the digits and see if the new number is also prime. Print a number only if it and
its reverse number is prime. There arc 43 pairs of numbers, some of which will
appear twice. You should pay particular attention to efficiency in this problem.
CHAPTER 7

Strings and Files

7-1 INTRODUCTION TO STRINGS


To a BASIC programmer, a string is a set of characters. We use strings every
time we print a message by enclosing it in quotes in a PRINT statement. BASIC
provides the ability to save strings in a special string variable, identified by
using a trailing dollar sign ($). We may use A$, B$, etc., to designate string
variables. Some computers allow Al$, B8$, etc., and some allow A$(R,C) to
designate string arrays. The use of strings enables us to process alphabetic data,
such as names and addresses, and descriptive data of all kinds.
We may work with string variables in many of the ways that we do with
numeric variables. For instance, in BASIC prngrams we may use such state-
ments as

100 LET A$ = "FlkST"


100 READ A$, 8$
100 INPUT A$, 8$
100 PRINT A$, 8$

In order to READ A$, B$, we must provide a corresponding DAT A state-


ment. Some systems require all strings in DATA statements to be enclosed in
quotes. Others require quotes only when the string contains a comma or 'looks
like' a number. For PRINT A$, B$, the output will have "comma spacing." That
is, the page will be arranged in 15-character columns. If we replace the comma
with a semicolon, the two strings will be printed with no space between them.
We will use a short program named FIRST$ to <lemonsb·ate LET, READ,
INPUT, and PRINT.
101
.102 Basic BASIC

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

BASIC allows us to compare strings for order in accordance with a sequence


known as ASCII (American Standard Code for Information Interchange). For
strictly alphabetic strings, this code will alphabetize in the conventional order.
ASCII places the digits 0 through 9 ahead of the letters of the alphabet We
can easily write a short program to demonstrate order comparison. See ORD$.
0RD$

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

In the handling of strings, we find that different computers have significantly


different BASIC language definitions. For example, on one computer, the state-
Strings and Files 103

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

95 REM * DEM0NSTRATE S STRING SU8SCKIP1S


100 DIM A$(8)
110 READ A$
120 IF A$="STOP" THEN 210
130 PRINT "A$="JA$
140 PRINT "LENCA$)-:";L ENCA$l
150 F0R I=l TO LENCA$) STEP 2
160 PRINT "A$C"J IJ","J I+IJ"):";A$[ 1, l+ l l
17 0 NEXT l
!BO PRINT
190 GOTO 110
195 REM
200 DATA "Ai:lCDEF","l: lA!>l0","::.fOr' "
210 END
;:UN
SEG$l

A$= ABC DEF


LEN CA$>= 6
A$( I , 2 >=AB
A$< 3 , 4 >=CD
A$< 5 , 6 >=EF'
0
The programs of this section were run on a Hewlett-Packa rd computer.
104 Basic BASIC

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$= ABC DEF


A$CJ,2l=AB
A$C 3, 4) =CD
A$C5. 6>=EF

A$=BASIC
A$Cl,2l=BA
A$C 3, 4) =SI
A$(5,6>=C

We see in SEG$2 the beginning of a technique for printing a numeric using


string output. Obviously missing are the ability to print zero and the ability to
handle more than one digit. We can take care of zero by using LET D$
"f\1(,,')Af::'Q'"1Q(\H L __ ... ---!-J.! ___ -----1---·- ~f --~--~ .1..1.., ___ ~--- _l!.--!J.. -·--·--: ____ Ll __ .a.
=
----
V..LLn.J"'XUV. UV ' LIUl. .PJ.U..ll..UJ.O UUU.tUVli::J V.l UlVlV l.Ua.U vuc; u151L .lCil{Ull(;.") LUa.t.. WV

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

is taken in program DIGIT2. The numeric output is placed between # signs,


and the string output is placed between $ signs.
DIG I T2

95 REM * PRINTING A NUMERIC 0r M0RE


96 REM THAN 0NE DIGIT USING STRING
97 REM 0UTPUT
100 DIM D$[10l
110 LET D$•"0123456789"
120 PRINT "INPUT ANY INTEGER"J
130 INPUT N
140 Ir N•O THEN 260
150 PRINT "#"JNJ "II ..
160 PRINT "$"J
170 r0R E=5 T0 0 STEP -I
180 LET I=INTCN/IO•E>
190 PRINT D$[I+l,I+IJJ
200 LET R=N-I*lOtE
210 LET N•R
220 NEXT E
230 PRINT "$"
240 PRINT
250 G0T0 120
260 END
RUN
DIGIT2

INPUT ANY INTEGER?93617


II93617. II
$093617$

INPUT ANY INTEGER?O

It is left as an exercise to eliminate the printing of the leading zero in the


output of DIGIT2.

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.

PROBLEMS FOR SEC. 7-2


I) Write a program to print the characters of a string in reverse order.
2) Eliminate leading zeros in the output of DIGIT2. Be careful not to eli-
minate all zeros.
3) Write a program to arrange the characters of a string in order using the
technique of program ARANG5 of Sec. 4-4.
4) Use string formatting to print the output in problem 7 of Sec. 6-2.
5) Write a program to convert a string integer to a numeric .
./ 6) Write a program to convert a numeric input to a string output if the nu-
meric input is allowed to contain a decimal point and be negative.
.106 Basic BASIC

7-3 THE STRING ARRAY SCHEME"


In the string array scheme, A$(1) names the string stored in the position
numbered I of a string single-dimensioned array, and A$(I,J) names the string
stored in row I and column J of a string two-dimensional array. As with arrays
used elsewhere, a DIMension statement is required if we intend to have either
subscription exceed 10. The maximum number of characters which may be
stored in any one array position varies from computer to computer but ranges
from 6 to the thousands.
We may do many things with string arrays that we do with numeric arrays.
Vie may READ, PRINT, INPUT, assign, and cumpare for order elements of
the array. We may even be able to use the statement LET A$ "XYZ" -+ =
"ATV" to assign "XYZATV" to A$.
DAYSOl
100 DIM W$C7)
105
108 REM *
READ DAYS 0F THE WEEK
110 F0R I = 1 T0 1
120 READ W$CI>
130 NEXT I
135
138 REM *
PRINT DAYS 0F THE WEEK
140 F0R I 1 T0 1=
150 PRINT I; W$CI>
160 NEXT I
165
168 REM * DATA
170 DATA SUN DAY• M0NDAY, TUESDAY• fJEDNESDAY
180 DATA THURSDAY, FRIDAY. SATURDAY
190 END
RUN

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.

CHANGE A T0 A$ makes the conversion in the opposite direction. This can

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

STRING? TRY THIS


8 CHARACTERS IN 'TRY THIS'
CHAR ASCII C0DE
'T' 84
'R' 82
•y• 89
32
'T' 84
'H' 72
'I' 73
•s• 83

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$

100 DIM AC 100>


II 0 PRINT "A$"J
120 IN PUT AS
i30 PRINT
140 CHANGE A$ T0 A
150 F0R I =1 T0 A<O> - I
160 F0R J = I + I T0 A< O>
17 0 IF AC I> < = A< J > TH EN 21 0
175 REM * EXCHANGE 0UT 0F 0RDER C0DES
180 LET S = A< I>
190 LET A< I> A<J>
200 LET A<J> = S
210 NEXT J
220 NEXT I
230 CHANGE A T0 A$
240 PRINT A$
250 END
RUN

A$? WHAT' IF I CAN'T THINK 0F S0METHING?

'?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.

PROBLEMS FOR SEC. 7-3


1) Write a program to print the characters of a string in reverse order.
2) Write a progam to accept string input, and tabulate the number of times
each character appears.
3) Write a program to alphabetize the strings of a single-dimension string
array.
4) Write a program to produce the following output, using the days of the
week as stored in W$ in program DAYSOI.
Strings and Files 109

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

5) Modify program ORDER$ to eliminate duplicates.


6) Write a program to produce the following output, using the days of the
week as stored in W$ in program DAYSOl.

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 '{
'{

7-4 INTRODUCTION TO DATA FILES


So far in our programming work all of the data used by our programs has
been entered through DATA statements, INPUT statements, or LET statements.
Consequently, we have had to store the data as part of the prngram or type the
data directly at the keyboard of our terminal. This works out all right for small
amounts of data that we wish to process just once. But, if we have large
amounts of data or we expect to carry out several processes on our data, then
we need to separate the data from the program. We can do this by using data
files.
A data file is simply a storage space in the computer where we store data,
much as a program may be stored in a storage space. (In fact, in some com-
puters, files and programs are indistinguishable until we type certain commands.
Obviously, we cannot RUN a data file.) By designating a separate storage space
for data, we gain many capabilities. We may now store much larger amounts of
data than we could possibly store in the data statements of a program. We may
alter the data to accommodate the results of program calculations. We may
rearrange the data according to prngram specifications. The possibilities are
limited only by our ability to think of problems to solve.
Most computer processing done today utilizes data flies. Data files are used
for inventory, bookkeeping, and data processing of all kinds. Just considering
the data handled by the Internal Revenue Service and the Census Bureau, the
use of data files can be seen as a very complex business indeed. So we will
attempt here to present only some rudiments of files processing in BASIC.
As we said earlier, a file is a storage space accessible to the computer. This
space may be used to store programs and data, which may be accessed dming
program execution. One of the features of these files that makes them mysteri-
ous is that they are invisible. But then, so are programs during execution. How-
ever, it is now possible to carry out tremendous amounts of useful computer
110 Basic BASIC

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.

7-5 HEWLETT-PACKARD FILES


Just to get an idea about how data gets into files and how file data is ac-
cessed, let's look at two short programs. The first is a program to enter some
numbers into a file. See program PRINT.

PRINT

90 REM * FIRST FILE DEMONSTRATION


100 FILES TEST
110 FOR I= 1 T0 4
115 READ X
120 PRINT llBX
130 NEXT I
140 DATA 3, 17, 11,31
150 END
RUN
PRINT

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

The printed output produced by program READ should convince us that


those numbers really came from a computer file as they do not appear anywhere
in the program itself. We also got an error message which is exactly analogous
to the 0UT 0F DATA IN LINE n message we have seen before.
There are several ways to avoi<l terminating with this error. One is to keep
track of the number of entries in the file; another is to place an item of artificial
data at the end of the real data just as we <li<l in DATA statements in programs.
However, BASIC provides a special statement just for this purpose. It is the IF
END statement. See line 105 of program READOl.

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

Statement 105 caused the computer to "remember" that if at any time we


ask it to read beyond the data, it is to then execute line 140 as the next state-
ment. In our case, that causes the program execution to terminate through the
END statement.
The IF END "trap" may also be used to find the end of data in a file so that
we may begin at that point to print additional data into it. Program PRINTl
does exactly this.
PRINT!

90 REM PRINT WITH IF' END 'TRAP'


*
100 FILES TEST
110 IF END Ill THEN 140
120 READ II J; X
130 GOT0 120
140 FOR !=I Tl'J 3
112 Basic BASIC

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

90 REM * ALPHABETIZE NAMES IN A FILE


100 DIM A$[72J,B$l72J
110 F"ILES TEST
120 F0R I=l T0 6
130 READ #!,IJA$,A,Al
140 F0R J=I+ 1 T0 7
150 READ #l,JJB$,B,Bl
160 IF A$ <= B$ THEN 200
170 PRINT #l,I;B$,B,Bl
160 PRINT #l,J;A$,A,Al
190 READ #l,IJA$,A,Al
200 NEXT J
210 NEXT I
220 PRINT "FILE ALPHABETIZED"
230 END
RUN
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

that the printing be at the beginning of record R. This approach is referred to


as random access. We have the same options in the file READ statement. READ
#N;A,B,C$ reads the next available data serially and READ #N,R;A,B,C$ reads
at record R. The IF END statement allows us to determine when we are read-
ing past the end of data in the file or are trying to read or print past the physical
boundaries of the file itself.

PROBLEMS FOR SEC. 7-5


1) Use the IF END "trap" to avoid reading empty records or past the physi-
cal end of the file in program READ02.
2) Modify ENTERA so that it will accept varying numbers of names and can
be used to add names to a file without "losing" data.
3) Modify READA to read any number of names.
4) Modify ORDERA to handle any number of names.
5) Modify ORDERA to arrange the data in increasing order of date of birth.
6) Write a program to print the names in file TEST at the terminal in alpha-
betical order without altering the arrangement in the file itself.
7) Write a program to print the names from file TEST in order of increasing
age at death without altering the arrangement within the file itself.
8) Since strings and numbers may be intermixed in a file and an attempt to
read one when the other is next will result in an error condition, it is desirable
to be able to distinguish between them. The TYP( ) function is provided for
this purpose. TYP(N) takes on a value of one if the next item in the file is a
numeric, two if the next item is a string, three if the next item is the end of file,
and, if N is negative, four if the next item is end of record. The absolute value
of N is the position of the file in the files statement. In order to get positioned
at the beginning of a record without reading data, we can READ #N,R. Using
the TYP( ) function and the positioning READ statement, write a program to
read the unknown contents of a file and print them record by record at the
terminal.

7-6 GENERAL ELECTRIC FILES


The files we are concerned with in this section are referred to as extemal files
since they store data externally to any programs. Files are generally charac-
terized in two ways: the access type and the data storage type.
Data in files may be accessed sequentially or at random. Sequential access is
similar to the way in which DATA statements of a program are accessed. Ran-
dom access is similar to the way in which the elements of an array are accessed.
As long as we know the exact position of a data item in a file, we may access
it directly.
The data contained in a file may be stored either as ASCII character codes
or as the binary representations of ASCII character codes and the numbers
being stored. \,Ye do not need to be concerned with the details of this distinction
when writing BASIC programs. We need only identify the slight differences in
program statement syntax required. ASCII files may be accessed only sequen-
tially whereas binm y files may be accessed either sequentially or at random.
116 Basic BASIC

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

100 MARK UNDERWl'll'JD,65,83,92,77,68,79


110 SUSAN STALBERG,73,88,82,77,69,79
120 EDGAR ANGLEMAN,74,86,73,79,80,7~
130 ALTHEA LARGE,91,92,90,99,92,90
140 GERTRUDE SMITH,71,86,87,90,83,92

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

94 REM * READ FROM A FILE AND PKINT ON lHE fERMINAL


100 FILES SCORE.
110 PRINT "1~AME","TESTl TEST2 1ESf3 1EST4 TES!:'> rESl6"
130 READ # 1, N$
150 PRINT N£; TAi:lC 15);
160 FOR I = l TO 6
17 0 READ 11 l, X
190 PRINT X; " ";
200 NEXT I
210 Pi<INT
230 IF MORE Ill THEN 130
260 END
RUN

NAME TEST! TEST2 TEST3 TESf4 fEST:i TESf6


MARK UN DERW000 65 il3 92 77 60 79
82 77 69 79
SUSAN ST ALBERG 73

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

In program READTEST, there are just four statements of a file-handling


nature. The statement 100 FILES SC0RE makes the file available to the pro-
Strings and Files 117

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

94 REM * CALCULATE AVERAGES FkOM A FILE


100 FILES SCORE
110 PRINT "NAME","TESTI TEST2 TEST3 rEST4 TESTS fEST6 AVE1,AGi:."
12!) LET Tl=O
130 READ #l, N$
140 LET T2 = 0
150 PRINT N$l TABC 15H
160 FOR I = 1 TO 6
170 READ #1, X
180 LET T2 = T2 + X
190 PRINT XJ " ";
200 NEXT I
210 PRINT T2/6
220 LET Tl = Tl + T2
230 IF MORE #I THEN 130
240 PRINT
250 PRINT "CLASS AVERAGE = "Tl/30
260 END
RUN

NAME TEST! TEST2 TEST3 TESTL1 TEST:, TEST6 AVEl-<AGt.


MARK UNDERWOOD 65 83 92 77 68 7 ';/ 77.JJJ3
SUSAN STAL BERG 73 88 82 77 69 79 7S
EDGAR ANGLEMAN 74 86 78 79 so 7i:J 79. I 667
AL THEA LARGE 91 92 90 99 92 90 92.JJjj
GERTRUDE SMITH 7 I 86 87 90 Bo 92 3:,. 6667
GLASS AVERAGE = 82.5

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

94 REM * READ SCORE - WRITE SCORE!


100 FILES SCOREJ SCORE!
110 SCRATCH 112
120 READ 01, Ns.x1,x2,x3,x4,xs,x6
130 PRINT N$
140 WRITE 112, N$; CXl+X2+X3+X4+X5+X6)/6
150 IF MORE #I THEN 120
160 END
RUN
MARK UNDERWOOD
SUSAN STALBERG
EDGAR ANGL.EMAN
AL THEA LARGE
GERTRUDE SM ITH

Since this is an ASCII file, we may LISt it at the keyboard as follows:


SCORE I

100 MARK UNDERWOOD, 77,3333,


110 SUSAN STALBERG, 78 ,
120 EDGAR ANGLEMAN, 79.1667,
130 ALTHEA LARGE, 92.3333,
140 GERTRUDE SMITH, 85.6667,

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.

Random Access Files


Random access files may be segmented into blocks of storage called records.
We may dictate the size of each record and the number of records in a file when
we create it, much as we dimension a two-dimensional array. The record size
is measured in words of storage. The word requirements for data are as follows:
One word per numeric
One word per four string characters, or fraction thereof
Orn" wnril f'F'r strine fm· intprmi l f'nmrintt>r f'nntrnl

The exact arrangement of data within a file is completely the programmer's


responsibility. We must know exactly where data is to be found and what it
means. The situation is no different from data handling within an array except
that once data is in a file, it seems more invisible.
Strings and Files 119
'
For our first example, let's simply write three rows of six numbers each into
a binary file with one program and then select some of them for printing at the
keyboard with another program. The storage requirements amount to just three
records, each containing six words. We obtain such a file with the CREate com ..
mand, as follows:
CRE NUMB,(RAN(6,3))
See program RND.

RllD

94 REM * LOA~ RANDOM NUMBERS INTO A BINARY FILE


100 FILES NUMB
110 F0R I = I T0 3
120 F'OR J = 1 T0 6
130 LET X = RNDCX)
140 loJRITE : 1, X
150 PRINT XJ
160 NEXT J
170 PRINT
180 NEXT I
190 END
RUN
o.98385 0.362274 0.250535 o. 338 07 4
0.250009 0.342306
o. 676737 0.820017 0.290332 Q.68319 0.373523 0.853779
o. 151996 0.975866 0.811924 0.448439 0.139030 o. 84716:;

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

94 REM * SELECT A NUMBER FHOM A FILE AT HA~DOM


100 FILES NUMB
110 PRINT "R0W,COL";
120 INPUT R, K
130 IF R = 0 THEN 190
140 SETW I TO 6*CR-I > + K
150 READ : 1, A
160 PRINT "F'OUN D"; A
1'70 PRINT
180 G0TO 110
190 END
RUN
RO\v,COL? 2,3
FOUND o. 290332
120 Basic BASIC

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

100 FILES SCOHEll SCOHE2


104 HEM * WRITE DATA INTO BINAHY FILE
110 FOR I = 1 re 5
120 READ Ill, NS,Al
130 SETW 2 TO 6•<1-11 + 1
140 11JRITE :2, NS.Al
150 NEXT I
154 REM * NOvJ Al,HANGE THE DATA ACCORDii"G TO AVERAGES
160 FOR I = 1 TO 4
17 0 FOR J = I + 1 TO 5
160 SEHJ 2 TO 6*Cl-ll + 1
190 i-<EAD :2, NS,AI
200 SET'll 2 TO 6*CJ-l I + 1
210 READ :2, MS,dl
22 0 I F A 1 < = i:J 1 THE," 2 7 0
230 SET>I 2 TO 6•<I-ll + 1
240 WHITE :2, MS18l
250 SET'd 2 TC 6* CJ-1 I +
260 !Jf<ITE : 2, '"':,Al
270 NE.AT J
280 NEXT l
?J34 REM * AND NOW PRI1"T THE RESUL. rs
290 PRINT " NAME","AV Ei<AGE"
300 PRINT
310 FGH I 1 TC 5
320 SETW 2 TO 6*11-11 + l
330 READ :2, N:;;,Al
340 PR!Mf NS,Al
350 i"E;\T I
360 END
RUN

MARK UNDERWOOD 77. J.1.'33


SUSAN STAL8E.l<G 73
EDGAR ANGLEMAN 79. 1667
GERTRUDE Si~ l TH 135, 6667
AL THEA LARGE 92. 3;333
Strings and Files 121

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

The Quadratic Function

We define a quadratic function as a real function of the form


f(X) = AX 2
+BX+ C (8-1)
where A does not equal 0.

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!

5 PRINT " A 8 c ", "X t "~ "X2"


10 READ A.s.c
15 Ir A=O THEN 99
20 LET Xl=C-B+SQRCB•2-4*A*C>>IC2*A>
30 LET X2=C-B-SQRCB•2-4*A*C>>IC2*A>
40 PRINT A;s;c,x1.x2
45 G0 Tl2I I0
50 DATA 1,3,2
60 DATA 1,2,-3,2,4,-6,6,t3,6,5,-b2
70 DATA Q,Q,Q
99 END
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

8-2 AXIS OF SYMMETRY AND TURNING POINT

The graph of a quadratic function is called a parabola. In examining the


graph of a quadratic function we often want to know where the axis of
symmetry is and where the turning point is. By completing the square on
the right
f (X) = AX 2 + BX+ C

f(X)=A[x 2 + ! x+ 4 n;2 -
4 n;
2 J+c

f(X)=A [ x-+Ax+ 4A2


., B n2 J -
n2
4A +c
we get
2
B ] 4AC - B2
f(X) =A [ X + 2A + 4A
124 Basic BASIC

Now, when X = -B/2A, X + B/2A = 0. The value of f(X) is minimum if A


is positive and maximum if A is negative, and the value of f(- B/2A) is
( 4AC - B2) / 4A. Thus the coordinates of the turning point are

- ~ 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.

Fig. 8-1. Flowchart for finding axis


Start of symmetry, turning point, and
maximum-minimum status for
parabolas.

END

LETM PRINT
= SGN(A) minimum

LETX =-Bl
(2•A)

LET Y = (4• A• C 1----'lil PRINT


-B •• 2)/(4 •A) x, y
The Quadratic Function 125

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

8-3 PLOTTING THE PARABOLA


One last consideration for the parabola is to plot its graph. This works well
right on the te1minal itself. We may use the spaces across the carriage as one
axis and the paper lengthwise as the other axis. Since the line feed is auto-
matically set on the terminal, the X-axis should run perpendicular to the
carriage and the Y-axis should run across the page. This means that one line
represents one unit on the X-axis and one space represents one unit on the
Y-axis. This is rotated 90 degrees clockwise from the conventional system.
Let us start out with the simplest possible graph and see what refinements
126 Basic BASIC

=
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

Fig. 8-2. Flowchart to plot Y = X ** 2.


The Quadratic Function 127

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

INPUT THE NUMBER 0F SPACES DESIRED T0 THE LEFT 0F ZER0?10


*
*
*
*
*
*
*
*
*
*
*
*
*
*
D0NE *
128 Basic BASIC

We have indeed graphed Y =X "" 2; however, the graph is not clearly


defined because there are no axes to specify the coordinate system. Let us
build up the coordinate system by first putting in the origin by plotting a 0
there. Immediately, we are faced with a decision. If the graph contains the
origin, do we want the plotted point or the origin designation? Since the
=
absence of the plotted point for X 0 would indicate that it should have been
plotted at the origin, let us plot the 0 at the origin as first priority. So, before
anything gets done for a particular value of X, we ask if the value of Y is 0.
If it is, we next look for the point at which X is also 0. At (0, O) we print 0.
Having printed 0, we next look to see if FNQ(X) is greater than 0. If it is, we
send the printing head on across the page.

92 IF Y <> 0 THEN 120


94 IF X <> 0 THEN 120
95 REM IF THE C0MPUTER GETS THR0UGH HERE THE
96 REM PRINTING HEAD IS AT THE 0RIGIN
98 PRINT "O";
100 IF FNQCXl>O THEN 180
102 REM IF FNQCX> > 0 GO FIND WHERE IT IS
103 REM 0THERWISE GET THE NEXT VALUE OF X
106 G0T0 240

RUN
PL0T3

INPUT THE NUMBER OF SPACES DESIRED T0 THE. LEFT 0F ZER0?6

*
*
*
*
*
*
0
*
*
*
*
*
*
*
*
DONE.

As long as we have the X-axis located, we might just as well put it in


the graph. All that is necessary is to have a PRINT instruction whenever
=
Y 0 but X does not.

94 IF X=O THEN 98
95 PRINT "t ";
96 Gorn 100
RUN
PLOT4
The Quadratic Function 129

INPUT THE NUMBER 0F SPACES DESIRED TO THE LEFT OF ZER0?9

*
*
*
*
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.

8 PRINT " ";


10 F0R X=O T0 50 STEP 10
12 PRINT " "JXJ
14 NEXT X
15 PRINT
16 F0R X=l T0 70
ls IF X/IO=INTCX/IO> THEN 24
20 PHINT "-";
22 G0T0 26
24 PRINT "t "J
26 NEXT X
RUN
PL0T5

INPUT THE NUMBER OF SPACES DESIHED TO THE LEFT OF ZER0?9

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

10 PRINT "INPUT THE NUMBER 0F SPACES DESI RED T0 THE LEFT 0F


ZER0"J
20 INPUT M
30 PRINT
40 PRINT " ";
50 F0R X=O T0 50 STEP 10
60 PRINT " ";X;
70 NEXT X
80 PRINT
90 F0R X=l T0 70
100 IF X/lO=INTCX/10> THEN 130
110 PRINT"-";
120 G0T0 140
130 PRINT "t ";
140 NEXT X
150 DEF FNQCX>=Xt2
160 F0R X=- 7 T0 7
1 70 PRINT
160 FGR Y=-M T0 70-M
190 IF Y <> 0 THEN 260
200 IF X=O THEN 230
210 PRINT "t";
220 G0T0 240
230 PRINT "O"J
240 IF FNQCX>>O THEN 310
250 REM IF FNQCX> > 0 G0 FIND WHERE IT IS
260 REM 0THERWISE GET THE NEXT VALUE OF X
270 G0T0 340
260 IF Y=FNQCX) THEN 320
290 REM IF Y DOES N0T EQUAL FNQCX> THEN PRINT A BLANK SPACE
300 P1<INT " ";
310 NEXT Y
320 Pi<INT "*";
330 REM PL0T THE P0INT AND G0 T0 NEXT X
340 NEXT X
350 END

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.

150 DEF FNQCX>=<X-2)t2+3


160 FllJR X=-5 T0 6
RI.IN
PLOTS
The Quadratic Function 131

INPUT THE NUMBER 0F SPACES DESI RED T0 THE LEFT 0F ZER:<l?9

_________ 0, _________ ,10_________ 20


, _________ ,30_________ ,40_________ 50
, _________ ,
*
*
*
' *
'
0 *
*
*
*
*
*
*
*
*
00NE *

SUMMARY FOR CHAP. 8


There are several things that can be done with the quadratic function on a
computer: 1) we can calculate the zeros; 2) we can find the various constants
that specify the appearance of the graph; 3) and we can even use the terminal
itself to plot a graph of the function. Of course the graphing program may be
used to plot other functions as well.

PROBLEMS FOR CHAP. 8


I) Write a program that finds the results of QUADI, but prints rational zeros as
fractions reduced to lowest terms.
2) Modify QUAD I to compute nonreal zeros.
3) The Y-coordinate of the turning point of a parabola may also be found by
evaluating f(-B/(2 ° A)). Rewrite QUAD2 by defining a function.
4) For sets of coefficients in data lines, have the computer print coordinate pairs
(X, Y) for a reasonable range.
5) Modify PL(Z)T5 to permit the X-axis to be printed for Y-coordinates less than
0. Also provide for the point to be plotted where the graph crosses the X-axis.
CHAPTER 9

Trigonometry

9-1 INTRODUCTION TO SIN(X), C(}S(X), AND TAN(X)

We choose to define the circular functions in terms of a point (X, Y) plotted


in a rectangular coordinate system. Consider the point (X, Y). It is at a dis-
tance R from the origin. We may find R from X and Y by using the
Pythagorean theorem:
R = yX~ + y:i
It is conventional to use Greek letters for angles. However, since computer
terminals do not provide them, we may use any letters we wish. Let us use G
to measure the angle whose initial side is the non-negative portion of the
X-axis and whose terminal side is the ray that has its endpoint at the origin and
contains the point (X, Y). See Fig. 9-1.
From Fig. 9-1 we define th1ee circular functions as follows:
cos G = X/R
sin G =Y/R
tan G =Y/X
whet e cos stands for cosine, sin stands for sine, and tan stands for tangent.
In BASIC it is required that the angles be measured in 1adians. 1 radian
may be defined as the central angle subtended by an arc length of R on the
circumference of a circle of radius H. Since the circumle1e11ce of a circle of
radius H is 21TR, we see that
21T radians = 3GO degrees
1T radians= 180 degrees
1 radian 180/1T degrees
1T/l80 radians= 1 degree

132
Trigonometry 133

Fig. 9-1

Although some time-share systems provi<le the special computer functions


RAD(X) an<l DEG(X) to convert from degrees to ra<lians and from radians to
degrees, respectively, you should be prepared to make the required conversions.
The usually available computer trigonometric functions are SIN(X),
C0S ( X), and TAN ( X). They are used in much the same way that all other
computer functions are used. Just be sure that the argument of the function
is in radians.
In mathematics, it is customary to define three additional circular functions
as follows:
sec G =RIX or sec G = l/cos G
csc G R/Y or csc G = l/sin G
cot G X/Y or cot G l/tan G
where sec stands for secant, csc stands for cosecant, an<l cot stands for
cotangent. Some eomputers provide these three functions in addition to the
earlier three, but we may always use the appropriate reciprocal. As always,
shoul<l an expression become too cumbersome, we have the option of defining a
program function using DEF.
Let us get the computer to print a small table of values of sin, cos, and
tan for 0 to 80 degrees in intervals of 10 degrees. We stop short of 90 degrees
to avoi<l having an undefine<l value for the tangent of 90 degrees. To write
program TRIGl, we will have to convert <legrees to radians, so we multiply
by7T/l80.

9-2 RIGHT TRIANGLES AND ARCTANGENT

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

5 PRINT "SINE''. "C0SINE"• "TAN GENT"• "RAOI ANS"• "DEGREES"


9 REM WE C0MPUTE A C0NVERSI0N C0NSTANT
10 LET C=3·14159/180
20 r0R G=O T0 80 STEP 10
30 PRINT SINCG*C>.C0SCG•C>.T ANCG*C>. G•C• G
40 NEXT G
50 END
RUN
TRI Gl

SINE C0SINE TANGENT RADIANS DEGREES


0 I• 0 0 0
• I 73648 .984808 ·176327 • I 74533 10
.34202 0939692 • 3639 i • 349066 20
.5 0866025 ,57735 • 523599 30
• 642 788 • 766044 ·8391 • 698132 40
• 766044 0 642788 1. 191 75 • 8 72665 50
0866025 • 5 Io 73205 1.0472 60
.939692 034202 2o 74748 1. 221 73 10
0984808 o I 73648 5o 67129 1. 39626 80
1)0NE

y
(X,Y)

R y

fig. 9-2

c
a

cos LA= b/c


<::in/
~···
A==-lr
'--'. -· -
\ tan LA= alb
b
fig. 9-3
Trigonometry 135

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

36' LADDER BASE MUST BE OUT FilOM THE HOUSE IN FEET


N0T LESS THAN NOT MORE THAN
3.10913 9.31143

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

LADDER BASE MUST BE 0UT FROM THE HOUSE IN FEET


N0T LESS THAN N0T MORE THAN LADDER L. EN GTH
s.11 9.32 36
s.23 B·8 34
7.74 8·28 32
7o26 7•76 30
6.77 7.25 28
6.29 6073 26
5o8l 6021 24
5.32 5.69 22
4o84 5.J8 20

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)

Therefore, G may be found in radians by

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.

PROBLEMS FOR SEC. 9-2


1) Modify LADER2 to give the angle in degrees and minutes.
2) Modify LADER2 to give the angle in degrees, minutes, and seconds.
3) Rewrite LADER2 so that the number of radians is given in terms of rr.
4) If the sides of a triangle are 10, 10, and 4, find the angles of the triangle to
the nearest minute.
5) Find the angles of a 3, 4, 5 right triangle to the nearest minute.
6) Find the angles of a 5, 12, 13 right triangle to the nearest minute.
.138 Basic BASIC

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.

9-3 LAW OF SINES AND LAW OF COSINES


Law of Sines

By drawing a triangle successively with each of its vertices at the origin of


a rectangular coordinate system, we may compute its area in three ways.
Referring to Fig. 9-6, the area is found by
Area= Jfb(Hl) or Jfa(H2) or Jfc(H3) (9-1)
We should see that
sin Cl= HI/a
sin Bl= H2/c
sin Al= H3/b
Solving for the heights we get
Hl =a sin Cl
H2 = c sin Bl
H3 = b sin Al
Substituting in Eq. (9-1) we get
Area= Jfb(a sin Cl) or )fa(c sin Bl) or Jk(b sin Al) (9-2)
Therefore we may find the area of any triangle by taking one half the product
of two sides and the sine of the included angle.
Since the area of a triangle is unique, we may set the three expressions for
area in Eq. (9-2) equal to get
Jfba sin Cl= )fac sin Bl = Jkb sin Al
By clearing of fractions and dividing through by abc, we get
sin Cl sin Bl sin Al
(9-3)
c b a
Equation ( 9-3) is ca.lied the [,aw of Sines. It enables us to find all parts of a
triangle if we are given any two sides and the angle opposite one of them, or
if we are given any two angles and any one side (provided, of course, the
triangle exists).
Let us write a program to find the remaining parts of a triangle ABC given
Al, Bl, and a. Since the sum of the measures of the angles of a triangle is
180 degrees, we first gee
Cl= 180° - (Al+ Bl) (9-4)
Trigonometry 139
y y y
C1
B1
A1
b c
H2i
A1 C1 a B1 X B1 c A1
x

Fig. 9-6

The Law of Sines gives us


sin Al sin Bl
a b
Solving for b gives
b _ a sin Bl
- sinAl (9-5)

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

8 kEM wE COMPUTE THE CONVERSION FACTOR


I0 LET K=J.14159/180
18 HEM DEFINE THIG FUNCTION FO~ DEGREES
20 DEF FNTC Gl=SINC G*K)
28 RE,'1 DEFINE A R0UNDING FU1~CTI0N
30 DEF FNRCXl=INTCX*I00+.5)/100
3g REM' Al AND Bl ARE ANGLES AND A IS A SIDE
40 f<EAD Al18l1A
50 IF AI=O THEN 999
58 HEM FIND THE THIHD ANGLE
60 LET Cl=l80-CAl+Bll
68 REM 70 AND 80 COMPUTE THE OTHER TWO SIDES
70 LET B=A*FNTCBll/FNTCAll
80 LET C=B•FNTCCll/FNTCBll
Sil HEM COMPUTE AREA
90 LET A2=.5•A•B•FNTCCI>
98 REM NOW PRINT THE RESULlS
100 PRINT" ","A"," B",," C"
110 PRINT "THE ANGLES ARE'',Al1Bl1CI
120 PRINT "THE SIDES ARE'',FNRCA»FNRCB»FNRCC>
130 PRINT "AND THE AREA IS "; FNRCA2>
140 PRINT
I 50 G0T0 4D
500 DATA 24. 511 IO
510 DATA 301 601 1 5
520 DATA 45,45,20
530 DATA o,o,o
999 END
RUN
LAWS IN

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

a 2 = (c cos Al - b) 2 + (c sin Al) 2


Simplifying the right side we get
a2 = c2 cos 2 Al - 2bc cos Al + b 2 + c2 sin2 Al
Trigonometry 141
y

(c cos A1, c sin A1)


B1

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.

PROBLEMS FOR SEC. 9-3


1) Write a program to solve the angle-side-angle case.
2)• Write a program to handle given two angles and a nonincludcd side, and two
angles and the included side. Use an item of data to determine which kind of data
is provided.
J) lviodiiy LAW~Cy:/5 to use lines 50, 60, 70, a11U OG as a ~uUruuiiue auU :,11ufilc
the data as discussed in text.
4) Write a program to solve the side-angle-side case.
5) Write a program to handle given three sides, and two sides and the included
angle. Use an item of data to designate which set of data is provided.
Trigonometry 143
LAWC0S
10 DEF' F'NRCX>=INTCX•IOO+.S>llOO
lS PRINT " .. ,, " A",," B".t" C0
20 READ A.e.c
30 IF' A.. O THEN 999
40 PRINT "THE SIDES ARE".A.e.c
48 REM T IS REALLY C0SCAI>
so LET T=C8t2+Ct2-At2>1C2• B•C>
S8 REM Tl IS REALLY SINCAI>
60 LET Tl=SQRCl-T•2>
68 REM Al IS THE ANGLE INCLUDED BY SIDES B AND C
70 LET AI =ATNC TllT>
78 REM C0NVERT RADIANS T0 DEGREES
80 LET Al=Al•l8013.141S9
88 REM WE N0W REPEAT THE PR0CESS T0 FIND ANGLE Bl
90 LET T=CAt2+Ct2-B•2>1C2• A•C>
100 LET Tl=SQRCl-T•2>
110 LET Bl=ATNCTl/T>
120 LET Bl=Bl•18013.141S9
130 PRINT "THE ANG.ES ARE"• F'NRCAI >. F'NRCBI >. 180-CF'NRCAI >+F'NRCBI >>
140 PRINT
I SO G0T0 20
soo DATA 3, 4, s. 300. 400. SOO
SIO DATA 1.132os.1.2
S20 DATA 2• 2• 3
S30 DATA o.o.o
999 END
RUN
LAWC0S

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

,/ 6) Write a program to solve the ambiguous case. Be sure to provide for no


triangles, one triangle, or two triangles.
j 7) Rewrite problem 3) to handle degrees, minutes, and seconds.
j 8) Rewrite problem 4) to handle degrees, minutes, and seconds.
9) Proiect: Write a single program to process data in four unic1ucly determined
cases. You might include the HL case.

9-4 POLAR COORDINATES


Every point in a rectangular coordinate system may be named by a unique
pair of real numbers. The pair is usually designated (X, Y). If we plot a point
(X, Y), we find that we may determine anotfier ordered pair of numbers, one
of which is the distance from the origin and the other is an angle measured
from the positive portion of the X-axis to the ray with endpoint at the origin
144 Basic BASIC

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

Note that there is no one-to-one correspondence between ordered pairs and


plotted points for the polar coordinate system. How do we designate the
origin? (0, 00)? How about calling it (0, 10°) or (0, -25°)? Also note that
(1, 45°) and (1, 405°) name the same point. Any particular ordered pair does
name a unique point, but every point may be named by an unlimited number
of ordered number pairs in this polar coordinate system.
Looking at the polar equation R = cos G suggests that for some values of G
we would like to allow R to take on negative values. So we extend the defini-
tion of R to permit this. The absolute value of R is the distance of the point
from the pole and we define (-R, G) and (R, G + 180°) to name the same
point.
Some polar equations are relatively easy to convert to rectangular form.
For instance,
R = 2cosG
is equivalent to

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

ANGLE 1-2cosc G> 2+SINC2Gl 1+2COSC Gl- 3Sl·\J( Gl • 2


0 -1 2 3
15 - • 93 2.5 2. 73
30 -.73 2· 8 7 1. 98
45 - • 41 3 • 91
60 0 2· 8 7 -.25
75 • 48 2.5 -1. 28
90 1 2 -2
105 1. 52 I• 5 -2.32
120 2 I· 13 -2.25
135 2. 41 1 - I• 9 I
150 2. 73 I• 13 - I. 48
165 2.93 I· 5 - I .J 3
180 3 2 - I
19 5 2. 93 2· 5 - 1. I 3
210 2. 73 2· 8 7 -1. 48
225 2. 41 3 - 1.91
240 2 2· 8 7 -2.25
255 1. 52 2.5 -2.32
270 2 -2
285 • 48 1• 5 - 1. 28
300 0 1·13 -.25
315 - • 41 I • 91
330 - • 73 I· 13 I. 98
345 - •93 I· 5 2. 73
360 - I 2 3

DONE

SUMMARY
The computer is an invaluable aid to obtaining values of ordered pairs of
polar comdinates for polar equations.
146 Basic BASIC

PROBLEMS FOR SEC. 9-4


1) Obtain polar coordinates for plotting any of the following polar equations. IIt
would be instructive to plot the graph as well.)
(a) R =cos 2G
(b) R = cos3G
(c) R =cos 4G
(d) R =sin 2G
(e) R= sin3G
(f) R cos G =I
(g) R= 1 + R cos G
(h) R = sin G + cos G
2) Write a program to convert from polar coordinates to rectangular coordinates
for any of the polar equations in problemn
I 3) \Vrite a program to store rectangular coordinates in an array for any of the
polar equations in problem I) except (f) and (g) and then rearrange the ordered pairs
in order of increasing values of X. Print the resulting set of ordered pairs. Plot the
graph on rectangular coordinate paper and compare it with the plot obtained in
problem I).
CHAPTER 10

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

For two complex numbers (A, B) and (C, D) we define equality:

(A, B) = (C, D)

if and only if

A=C and B=D (10-1)


Their sum is found by

(A,B) + (C,D) = (A+C,B+D) ( 10-2)


and their product is found by
0
(A, B) (C, D) = (AC - BD, AD + BC) (10-3)

Equations (10-1 ), ( 10-2), and ( 10-3) are relatively straightforward consid-


erations for a computer program. We can test a pair of real numbers for
equality with another pair or we can perform the addition of Eq. (10-2) or the
multiplication of Eq. (10-3). As an example, let us write a sh01t program to
add two complex numbers on INPUT. See ADDA, B.

ADOA.8

10 PRINT "THIS PR0GRAM ADDS TW0 C0MPLEX NUMBERS IN A,8 FORM"


20 PRINT
30 PRINT " FIRST NUMBER"J
40 INPUT A, B
50 IF A=999 THEN 999
60 PRINT " SEC0ND NUMBER";
70 INPUT c, D
60 PRINT "THE SUM IS C"JA+CJ''."JB+DJ"l"
90 G0T0 20
999 END
RUN
ADDA.B

THIS PR0GRAM ADDS TW0 C0MPLEX NUMBERS IN A,B FORM

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

Subtraction and multiplication are also relatively straightforward.


Now consider division:
(A, B)/(C, D) = (X, Y) ( 10-4)
Complex Numbers 149

Equation (10-4) may be defined in terms of multiplieation:


(A, B) = (X, Y) 0
(C, D)
(A, B) (XC - YD, XD + YC)
By the <lefiniton of equality for complex numbers,
A= XC-YD and B XD + YC
Solving for X and Y we get
AC+BD BC-AD
y
X = C:.! + D~ and c~ + D:.!

This is a little more complicated than the othe1 ope1ations, but still manage-
able.

SUMMARY

The computer may be programmed to wmk with complex numbeJs, if we


represent them as ordered pairs of real numbers. The four fundamental oper-
ations of addition, subtraction, multiplication, and division may all be <lone
by formula.
PROBLEMS FOR SEC. 10-1
1) Write a program to give the sum, difference, product, and quotient for pairs
of complex numbers assigned as data.
2) Write a program to compute and print the complex roots of quadratic
equations.
3) Write a program to test the commutative properties of both addition and
multiplication for five pairs of complex numbers,
4) Demonstrate that subtraction and division are not commutative.
5) Write a program to generate random complex numbers. Then test the asso-
ciative property for both addition and multiplication.
j 6) Find the reciprocal of complex numbers assigned as data.
7) Whenever we talk about ordered pairs of real numbers, the rectangular
coordinate system should come to mind. Think of (A, B) as a plotted point on a
graph with an A-axis and a B-axis. Write a program to find the distance from the
origin of five complex numbers assigned as data.

10-2 POLAR COORDINATES

If we think of ordered pairs of real numbers as being associated with a


rectangular coordinate system, we may plot a point representing (A, B) as
shown in Fig. 10-1, where the distance from the B-axis is the absolute value
of A and the distance from the A-axis is the absolute value of B.
Whenever we plot an ordered pair of real numbers on a rectangular
coordinate system, we may associate the point with another ordered pair of
real numbers. In the new pair, the first number is the distance from the origin
and the second is the angle whose initial side is the positive A-axis and whose
terminal side is the ray with an endpoint at the origin and containing the point
150 Basic BASIC

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)

cos C cos Cl - sin G sin Cl =cos (G +Cl) ( 10-6)


and
sin G cos Cl + cos G sin Cl = sin (G + GI) ( 10-7)
Complex Numbers 151

Substituting Eqs. (10·6) and (10-7) into (10-5) we get


(RRl cos (C +Cl), RRl sin (C + Cl))
which is a plotted point associated with a distance from the origin of RRl and
an angle of C + CL So
(R, C)(Rl, Cl)= (RRl, C +Cl) (10-8)
This means that to multiply two complex r:umbers in polar form we should
multiply their absolute values and add their arguments. This is less cumber-
some than the method of Sec. 10-1.
It follows from Eq. (10-8) that to divide two complex numbers in polar
form we divide their absolute values and subtract their arguments:
(R, C)/(Rl, Cl)= (R/Rl, C - Cl)
Again this is less cumbersome than the formula of Sec. 10-1.
Let us multiply some randomly assigned complex numbers in polar form.
We generate arguments in degrees and absolute values in units 1 to 10. See
MLTR, C.

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

10 , 135 >•C 2 , 311 >=< 20 , 446


6 , 98 >*C 9 , 341 >=< 54 , 439
9 , 6 >*( 1 , 231 >=< 9 , 237
10 , 95 >•C 8 , 307 >=< 80 , 402
6 , 60 >*< 5 , 356 >=< 30 , 416
( 10 , 139 >*C 2 , 343 ) =< 20 , 482

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

PROBLEMS FOR SEC. 10-2


1) \Vrite a program to find the quotient of two complex numbers in polar form.
2) \Vrite a program to print the positive integral powers of (I, 45°) from I to 8.
3) \Vrite a program to convert from (R, G) form to (A, B) form .
./ 4) Write a program to convert complex numbers from (A, B) form to (R, G) form.
You will want to use the ATN(X) computer function here and be sure you have the
angle in the correct quadrant. To check this, simply try numbers in all four quadrants .
./ 5) Write a program to take two complex numbers in (A, B) form and print their
product in (R, G) form.
j 6) \\'rite a program to print the positive integral powers of a complex number in
(A, B) form. Keep the result in (A, B) form .
./ 7) ~fodify ML TR, G to generate negative as well as positive numbers for angles.
Print the resulting angle as a value between -360 and +360 degrees.

10-3 POWERS AND ROOTS


\Ve have seen that for multiplying two complex numbers the polar form
provides a very convenient algorithm. If we wish to square a complex number,
i.e., multiply it by itself, we get
(R, G)~ = (R~, G + G) or (R~, 2G)

\Ve also see that for a positive integer n,


(R, G)n = (Rn, nG) (10-9)
It can also be shown that an nth root of (R, G) may be found by
(R, G)l/n = (R 1n, G/n)
1 (10-10)
where Rl/n means \)IR. Equations (10-9) and (10-10) constitute a portion of
De Moii:re's theorem. It can also be shown that every nonzero complex mun-
ber has exactly n complex nth roots.
Let us find the four complex fourth mots of unity. By taking the square roots
of the square mots of 1, we should get 1, i, -1, and -i, which in (R, G) form
are (1, 0°), (1, go 0 ), (1, 180°), and (1, 270°). Using De l\foivre's theorem,
(1, 0°)1/4 = (11/4, 0/4°) Of (1, 0°)
However, there should be three more. Now we see that there is a tremendous
advantage associated with the nonuniqueness for polar coordinates. By writing
unity (1, 0°) as (1, 360°) we may apply Eq. (10-10) again:
(1, 360°)1/4 = (1, go 0
)

Writing (1, 0°) as (1, 720°) we get


(1, 720°) 11 4 = (1, 180°)
and finally (1, 0°) = (1, 1080°) gives
(1, 1080°) 114 = (1, 270°)
Complex Numbers 153

R00TS

10 PRINT "TAKE R00TS QJF' C0MPLEX NUMBERS IN P0LAR FORM"


20 READ R, G•N
30 PRINT "l'HE"JNJ","JNJ "TH R00TS 0F" <"JR;",,";G;"> ARE:"
40 F'0R X= 1 T0 N
50 PRINT "(";Rt C l/N)J ",, "; GINJ ">"
60 LET G= G+ 360
70 NEXT X
80 PRINT
90 G0T0 20
100 DATA 1,0,4
110 DATA 1, o, 3
120 DATA 1,45,2
130 DATA 3,90,3
140 END
RUN
R00TS

TAKE R00TS 0F' C0MPLEX NUMBERS IN P0LAR F'QJRM


THE 4 • 4 TH R00TS 0F < • 0 ARE:
< 1. • 0
I. , 90
I. • 180
I. • 270

THE 3 . . 3 TH R00TS 0F' ( I , 0 > ARE:

..
( ). 0 )
). 120
1. 240

THE 2 . . 2 TH R00TS 0F' ( I , 45 > ARE:


(
(
1.
). . 22.5
202.5
)

THE 3 • 3 TH R00TS 0F < 3 • 90 > ARE:


( 1.44225 • 30 )
I• 44225 • 150
) • 44225 • 270

0UT 0F' DATA IN LINE 20

as expected. Suppose we add 360 degrees again. Then G 1440° and =


1440(1/4) = 360° which we have already in (1, 0°). Finding roots of complex
numbers in polar fo1 m becomes a ve1 y straightforward computer program.
See R00TS.

PROBLEMS FOR SEC. 10-3


1) In program R00TS have the computer convert the roots to (A, B) form.
/ 2) \Vrite a program to find the n complex nth roots of complex numbers in (A, B)
form and print the results in (A, B) form.
/ 3) In program H00TS print the roots in both (A, B) and polar form.
CHAPTER 11

Polynomials

11-1 FUNDAMENTAL OPERATIONS

We define a real polynomial in X as an expression that can be written in


the form
ANXN + AN_ 1XN-l + · · · + A2 X2 + A1 X +Au
where N is a non-negative integer, X is a complex number, and the AN are
constant real coefficients. The following are examples of polynomials in X:
5 X-3 X8 + 3XG - X +1 x2 + 3X-4
For the polynomial 5, note that 5 = 5X0 so the polynomial consists of the term
Au, which is 5. The number 0 is considered a polynomiaL All real polynomials
except the zero polynomials have degree i where AiX 1 is the term of the
polynomial with the greatest value of i for A1 not equal to 0. Polynomials may
be used to describe many physicai problems. For instance, the trajectory of a
projectile is described by a second-degree polynomiaL
We may perform operations on polynomials much as we perform opera-
tions on explicit numbers. You have had considerable experience adding and
subtracting such expressions. You have often multiplied two binomials of the
form (AX+ B)(CX + D). One of the problems in Chap. 1 was to perform just
that multiplication by computer. We now develop a program to multiply two
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

We observe that when we multiply two numbers in column 1, we put the


result in column l; when we multiply a number from column 1 by a number
from column 3, we put the result in column 3; and when we multiply a
number in column 2 by a number in column 3 we put the result in column 4.
This suggests that multiplying a number in column I by a number in column J
calls for the result to go in column (I + J - 1). [If 0 is allowed, then the
result goes in column (I + J).] So, if we store the two polynomials being
multiplied in an F list and an S list and the product in a P list, our computer
program will have an instruction to store F[l] 0 S[J] in P[I + J - l]. We must
also provide for subtotals. Thus the program statement will be

XXX LET P[I + J- l] P[I + J- l] + F[I] 0


S[Jl

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

B HEM LINES 10 THR0UGH 40 READ AND PRINT THE FIRST P0LYNOMIAL


10 FOR X=3 T0 1 STEP -1
20 READ H XJ
30 Pr<INT FC XJ;
40 NEXT X
50 PRINT "TIMES "J
58 REM LINES 60 THROUGH 90 READ AND PRINT THE SECOND P0LYNOMIAL
60 F0R Y=2 TO 1 STEP -1
70 READ 5( Yl
BO Pi<INT S[YJJ
90 NEXT Y
98 REM 100 THROUGH 120 SET THE RESIJL T LI ST AT ALL ZER0S
100 F0R W=l TO 4
110 LET P(WJ=O
120 NEXT \,•
128 REM LINES 130 THROUGH 170 DO THE ACTUAL MULTIPLYING
1 30 F0 R I = I T0 3
140 F0R J=l T0 2
150 LET PU+J-ll=P[!+J-ll+F[IHS(JJ
160 NEXT J
170 NEXT I
180 PRINT "YIELDS ";
188 llEM AND NOl-1 \.,'E PRINT THE 'ANSWER LI ST'
190 FOR Z=4 TO l STEP -1
200 PRINT P[ZJJ
210 NEXT Z
218 REM THE FIRST THREE NUMBERS REPRESENT 3X•2+11X-5
219 REM THE NEXT TWO Nl™BERS REPRESENT 2X+7
220 DATA 3, 11,-5,2, 7
230 END
IWN
TR1*81

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

Division by the algorithm we have just developed is usually called synthetic


division. Since this is essentially an iterative process, we should be able to get
the computer to perfo1m division in this way. We put the original polynomial
in a P list and the quotient polynomial in a Q list. Let us store the division
constant in R. For every division problem of the kind we are working with
here, the first coefficient in the quotient polynomial is the same as the first
coefficient in the dividend polynomial. So we need a line in the program which
says LET Q[4] = P[4]. See line 70 in program SYNDIV.

SYNOI V

5 PRINT "SYNTHETIC DIVISl0N:"


8 REM READ THE OIVIS0R
10 READ R
18 REM REAO ANO PRINT 0RIGINAL P0LYN0MIAL IN LINES 20 THRU 50
20 F0R N=4 T0 I STEP ·I
30 READ PCNl
40 PRINT PCNlJ
so NEXT N
60 PRINT "DIVIDED BY X ·"JRJ"YIELOS"
68 REM FIRST QU0TIENT C0EFFICIENT EQUALS FIRST
69 REM C0ErrICIENT 0F 0RIGINAL P0LYN0MIAL
70 LET QC 4l=PC 4l
80 PRINT QC 4JJ
90 F0R X=3 T0 I STEP -1
98 REM "MULTIPLY AND ADD"
100 LET QCXJ=PCXJ+QCX+ll*R
110 PRINT QCXJJ
120 NEXT X
130 DATA 3,2,-3,-10,3
140 ENO
RUN
SYNOI V

SYNTHETIC DIVIS10N:
2 -3 -10 3 DIVIDED BY X - 3 YIELDS
2 3 -I 0

00NE

In SYNDIV, 2 3 -1 0 is to be interpreted as 2X2 + 3X - 1 with


a remainder of 0. Let us try another:

130 DATA 2, 3, - 1, •I. - 5


RUN
SYNDI II

SYNTHETIC DIVISI0N:
3 -1 4 -s DIVIDED BY X - 2 YIELDS
3 5 14 23

The 3 5 14 23 is to be interpreted as 3X~ + 5X + 14 with a


remainder of 23.
Polynomials 159

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.

PROBLEMS FOR SEC. 11-1


1) vVrite a program to find the sum of two polynomials assigned as data. Be sure
to avoid printing leading zero coefficients when adding pairs similar to 3XI + 6X - 4
and - 3X 4 + sxa - 3X + 1.
2) Do problem 1) for subtraction.
3) Write a single program to add or subtract pairs of polynomials as determined
by an item of data. (Example: use S = 1 for add and S = 0 for subtract.)
4) Prepare a program to multiply two polynomials of varying degrees.
5) Write a program to multiply three polynomials.
6) Generate pairs of random polynomials of random degree and multiply them.
Print the original polynomials and the product. Be sure to allow negative coefficients.
/ 7) Extend SYNDIV to divide X - R into polynomials of any degree. Also have the
computer print the remainders with a message to the effect that the remainder
equals whatever it comes out to.
/ 8) Write a program to print the first 11 integral powers of (X+ 1).
/ 9) Write a program to divide any polynomial by any polynomial of equal or
lesser degree. Suggestion: get data from problem 6.

11-2 INTEGRAL ZEROS


It is common practice to abbreviate any polynomial and call it P<Xl for a
polynomial in X (read as P of X). We often look at the polynomial equation

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.

Search for Integral Zeros


What integers do we try for Z to test P<Z> for O? We have assumed that
there are N complex zeros. Let us call them ZN, ZN_ 1, .. ., Z 2 , Z1 • It can be
shown that
(X - ZN)(X - ZN-1) ... (X - Z2)(X - Z1)
= ANXN + AN_ 1XN-l + · · · + A1X + A0
Multiplying the left side out we should see that the only constant term in the
product is (-ZN) (-ZN_ 1) · · · (-Z 2 ) (-Z 1) which simplifies to (-l)N (ZN)
(ZN_ 1 ) · · · (Z 2 ) (Z 1 ) and must equal the constant term in the product poly-
nomial which is A0 • And so it follows that if a polynomial has any integral
zeros, they must be factors of the constant term A0 • That is not to say that all
integral factors of A0 are zeros of the polynomial.
This should provide sufficient basis for writing a computer program to find
the integral zeros of a polynomial function. We can define a computer func-
=
tion and test for FNP(X) 0 for all integral factors of the constant term. If
we continue to enter the coefficients of polynomials in computer lists as we
have been doing, then we know that the constant term will always be P[l]. For
our first program, let us define our function using the list entries as coefficients
in a DEF statement and look at only third-degree polynomials.
One feature of the program that requires comment is the finding of num-
bers to test for factors. These numbers must be in the interval -P[l] to P[l]
including the end numbers. Well, if P[l] is negative, we want to step -1
arnl if Fi 1] is positive, we want to step + l. This 1s a perfect place to use
SGN(P[l]). See line 80 of program INTZER. It would be useful to print that
there are no integral zeros if that turns out to be the case. In order to do
that, we need a switch which is off initially and which we turn on only if we
Polynomials 161

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

10 DEr rNP<X>=PC4 1*X•3+PC31* Xt2+Pt21*X+P Ctl


20 PRINT
22 PRINT
25 r0R S=4 T0 1 STEP -I
30 READ PtSJ
40 PRINT PCSJJ
50 NEXT S
60 PRINT "INTEGRAL ZER0<S>r "J
66 REM TURN SWITCH 0rr
70 LET K=O
76 REM STUD-Y LINE 60 CAREFULLY!
60 r0R X=-Pttl T0 PCIJ STEP SGN<Ptll>
66 REM LINE 90 PREVENTS ~~ ERR0R MESSAGE CAUSEO BY
69 REM DIVIDING BY ZER0
90 IF" X=O THEN 140
96 REM IS X A F"ACT0R 0r PCll?
100 IF" PCIJ/X <> INT<PClJ/X) THEN 140
106 REM IS THE REMAINDER ZER0?
110 IF" F"NPCX> <> 0 THEN 140
116 REM IF" THE C0MPUTER GETS THR0UGH HERE• THE
119 REM VALUE 0F" X IS A ZER0 0F" THE rUNCTl0N
120 PRINT XJ
126 REM TURN THE SWITCH 0N - WE HAVE A ZER0
130 LET K= I
140 NEXT X
t 50 Ir K=t THEN 20
160 PRINT "N0NE r0UND"J
165 G0T0 20
170 DATA 1.-2.-11,12
160 DA 1' A I , I • - 5, - 2
190 DATA J,-2,3,-4
200 DATA 2,-3,-10, 3
210 END
RUN
INTZER

-2 -11 12 INTEGRAL ZER0CS>: -3


-s -2 INTEGRAL ZER0CS>1 2
-2 3 -4 INTEGRAL ZER0<S>1 N0NE r0UND
2 -3 -10 3 INTEGRAL ZER0<S>: 3

0UT 0F" DATA IN LINE 30


162 Basic BASIC

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

Fig. 11-1. Flowcha1t for finding integral zeros of polynomial

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

520 LET P = P + P[I] 0


X 0 0
(I - 1)
530 NEXT I
540 RETURN
Let us insert G0SUB 500 after line 100 in INTZER, insert line 23 READ N,
where N is the number of terms in the polynomial, and change line 25 to
=
read F0R S N T0 I STEP -1. See IZER01.
This program will handle up to ninth-degree polynomials. (Tenth, if you
have 0 subscripts.) If we want to work with polynomials of greater degree,
all we need is a DIM statement to specify a longer list for P.

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.

PROBLEMS FOR SEC. 11-2


1) For each of the polynomials to follow: (a) find an integral zero, (b) use syn-
thetic divbion to find the resulting factor after dividing by (X - Z), and (c) search
for zeros of the depressed polynomial. Repeat until all integral zeros are found and
then print the remaining polynomial.
10X3 - 11x2 - 76X + 32
6XR - 32X7 - 23X6 - 3XG - 12X4 - 36X:l - X~ +BX - 12
8XG - 18X4 - sxa - 32X~ + 2X + 3
2X4 + 5X3 - 31X2 - 21X + 45

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?

11-3 HEAL ZEROS


It can be shown that for a polynomial, if P 1x 11 > 0 and P 1 x~ 1 < 0, then
=
there is a value of X between X 1 and X~ such that P (x 1 0. This is called the
Location Principle. In graphical terms, the Location Principle may be stated as
164 Basic BASIC

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

0UT 0F' DATA lfll 1..INE: 23


Polynomials 165

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

END Fig. 11-2 Flowchart for searching for


real zeros in program REAL
166 Basic BASIC

REAL

50 DEF FNTCX>=t2• Xt3-64*Xt2+1 7•X+l95


60 LET A=O
70 F0R X=-5 T0 5
so LET st=FNTcx>
90 LET S2=FNTCX+I)
100 IF' St•S2>0 THEN 130
110 LET A=A+I
120 LET SCAl=X
130 NEXT X
190 PRINT "lNTERVALCS> BEGIN AT:"
200 r0R I=I T0 A
210 PRINT SClll
220 NEXT
270 END
RUN
REAL

INTERVALCS> BEGIN AT:


-2 2 3
00NE

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

10 PRINT "SEARCH F'0R REAL ZER0S 0F' A P0LYN0MIAL"


20 PRINT "START, END. INCREMENT"J
30 INPUT F'•L• S
40 IF' S=O THEN 270
50 DEF FNTCX>=t2• Xt3-64*Xt2+1 7*X+l95
60 LET A=O
70 r0R X=r T0 L STEP S

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

160 PRINT "WI TH El THER GREATER LIMITS 0R SMALLER INCREMENT"


170 PRINT
160 G0T0 20
190 PRINT "INTERVAL<S> BEGIN AT:"
200 F'0R I= I T0 A
210 PRINT SC I lJ
220 NEXT I
230 PRINT
240 PRINT
250 PRINT "N0W "J
260 G0T0 20
270 END
HUN
REAL I

SEARCH F'0R REAL ZER0S 0F' A P0LYN0MIAL


START. END. INCREMENT?-5.5. I
INTERVAL<S> BEGIN AT:
-2 2 3

N0W START. END. INCREMENT?-3,-2, .1


N0 INTERVALS F'0lJ'ID **** TRY AGAIN
WITH EITHER GREATER LIMITS '0R SMALLER INCREMENT

START. END. INCREMENT?-2.-1 • . i


INTERVAL<S> BEGIN AT:
-1. 5

N0W START. END. INCREMENT?-1·5.-1.4. . 0I


INTERVAL<S> BEGIN AT:
-1.45

N0W START, END. INCREMENT?2,3,.1


INTERVAL<S> BEGIN AT:
2.s
N0W START. END, INCREMENT?t.2.0

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

SEARCH F'0R REAL ZER0S 0F' A P0LYN0MIAL


START. END• INCREMENT?-10, 10, I
N0 INTERVALS F'0LND **** TRY AGAIN
WITH EITHER GREATER LIMITS 0R SMALLER INCREMENT

START, END, INCREMENT?-100.100,5


INTERVAL<S> BEGIN AT:
-40 -30 15

N0W START. END. INCREMENT?-40,-35,.)


INTERVAL< S> BEGIN AT:
-39.3
.168 Basic BASIC

N0W START• END• INCREMENT?-39,3,-39.2,.01


INTERVALCS> BEGIN AT:
-39.22

N0W START• END. INCREMENT?IS1201ol


INTERVAL CS> BEGIN AT:
1s.3

N0W START. END. INCREMENT?IS.J.!S.4. . 01


INTERVALCS> BEGIN AT:
1 s. 33

N0~J START. END, INCREMENT?O, Q, 0

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.

PROBLEMS FOR SEC. 11-3


1) Modify REALI so that if the value picked for X in line 70 gives either Sl
or S2 equal to 0, we get a message and the value of the zero printed.
2) In program REALI, after the computer has found the initial intervals for all
real zeros, we do not want the computer to search the entire intervals specified in
subsequent searches in the X-loop. vVe want the computer to print immediately
after finding the change in sign without searching the rest of the interval. Incorporate
this into the program.
3) Modify REALI to read data for more than one polynomial. You may use
some dummy value for S in line 30 as a signal to read the next set of data.
j 4) Write a program to search for real zeros by first finding the unit intervals and
then using linear interpolation uutil FNT(X) is within 1()-4 of zero. You may want
to specify less or greater precision.

11-4 COMPLEX ZEROS


The simplest real polynomial for which we may find complex zerns is the
second-degree polynomial A~X'.!+ A 1X + A0 . vVe may use the general quad-
ratic formula
X = -A 1 ± yA 1'.! 4A'.!Ao
( 11-3)
2A 2
Polynomials 169

Letting the radicand equal D we get


D = A12 -4A2Ao
D is called the discriminant of the quadratic expression. We can see that if D
is negative, the zeros are nonreal. We can rewrite Eq. ( 11-3) as
-Ai VD
X=u-± 2A
2 2
and finally, considering X as being associated with two numbers A and B, we let

and

If D is greater than or equal to 0, the real zeros are


Xl =A+ B and X2=A-B (11-4)
But if D is less than 0, we get the nonreal zeros
Xl =(A, B) and X2 =(A, -B) (11-5)
So, in our computer program we compute D, A, and B. Then we test D. If
D is negative, we print as in Eq. (11-5) and if D is not negative, we print as
in Eq. (11-4). See Fig. 11-3 for the flowchart. We call the program C0MP-l.
It turns out that there is no convenient general procedure for finding non-
real zeros for polynomials of higher degree than two. But for any polynomial
that has exactly two nonreal zeros, we may find the real zeros first, then for
each real zero Z we may divide out the corresponding X - Z using synthetic
division and if after all division is carried out the result is a second-degree
polynomial, we may apply the technique of program C0MP-l. We demon-
strate this procedure by an elementary example: Find all zeros of the following
polynomials, given that each has at least one integral zero:
X3 + 2X2 - X - 2
X 3 - X 2 - 48
X3 - l
X3 +1
X 3 -X 2 + X-1
6X3 - 77x2 - 189X - 90
This is of course a special case, but it should help us develop a more gen-
eral approach. Since we have a thii cl-degree polynomial with one integral
zero, we may take program INTZER to find the integral zero Z and then use
program SYNDIV to divide the given polynomial by X - Z. The polynomial
we get is called a depressed polynomial. We know that in this problem each
depressed polynomial will be a second-degree polynomial. So we may then
use program C0MP-1. In each of these earlier programs the polynomials were
all represented with the same variable P list. So all that will have to be
changed is the various READ statements and the variable in which the integral
zero in INTZER is called X, while in SYNDIV the corresponding number was
stored in R. Thus the need for line 170 in program ALLZER. We also changed
170 Basic BASIC

Start

Fig. 11-.3. Flowchart for finding real


and nonreal zeros of second-degree
polynomials

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 - I 0 -46 INTEGRAL ZER0: 4


SYNTHETIC DIVISI0N BY x - 4 YIELDS:
1 3 12 REMAINDER = 0
N0N-REAL ZER0S:
c-1. 5 .3.1225 AND c-1.5 .-3.1225

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

6 -77 -189 -90 INTEGRAL ZER0: 15


SYNTHETIC DIVISI0N BY X - 15 YIELDS:
6 13 6 REMAINDER = 0
REAL ZER0S:
-.666667 AND -1.5

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.

Descartes' Rule of Signs


We may define the variation in a sequence of numbers as the number of
changes in sign found by comparing successive pairs of adjacent numbers.
For example, for the sequence 1, 3, 4, -8, 2, the value of V is 2. There is no
change for 1 to 3 or 3 to 4. There is one change for 4 to -8 and for -8 to 2.
If zeros appear in the sequence, we drop them. The sequence -2, 8, 0, 5, -3,
6 becomes -2, 8, 5, -3, 6 in order to determine the number of variations
which is 3.
Descartes' Rule of Signs says that for
ANXN + AN_ 1 XN-l + · · · + A1 X + A0
the number of positive zeros depends on the number of variations in the
sequence
AN, AN_ 1 , ..• , Al> A0
in the following manner. If V is the number of variations, then the number
of positive zeros is either V or V - 2 or V - 4, etc., but not less than 0. This
may be written V - 21, where I is a positive integer.
It turns out that we may find a corresponding number for negative zeros
by finding positive zeros for P <-x). Substituting -X for X will change the
sign of all terms which have an odd exponent on X. So if Pcxl = -4X5
- 3X4 + 5X3 - 2X 2 + X - 3, the value of V is 4 and there must be 4 or 2 or 0
positive zeros. Now we find P <-X) = +4XG - 3X4 - 5X3 - 2x2 - X - 3,
and V is L So there must be exactly one negative zero.
This is something we can get the computer to do for us. We may read the
coefficients into the first row of a P array and change the sign of the coefficients
of the terms with odd exponents on X and put the new coefficient list in the
second row of the P array. Then we may look for changes in sign and provide
two counters: one for the first row keeping track of changes of sign for the
positive zeros, and the other for the second row counting sign changes for the
negative zeros. These are Vl for the positive zeros and V2 for the negative
zeros in program DESCRT.
174 Basic BASIC

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.

PROBLEMS FOR SEC. 11-4


1) Modify DESCRT to permit zero coefficients. Read all coefficients into a P list
and then eliminate the zero coefficients as you enter them into a two-row array.
2) Modify ALLZER to handle polynomials of degree greater than three which
have for degree D at least D - 2 integral zeros.
j 3) Write a program to generate polynomials of random degree D which are
guaranteed to have exactly D - 2 integral zeros and two nonreal zeros.
4) Proiect: Modify your program in problem 2) to handle D - 2 real zeros using
linear interpolation until P<Xl is within .001 of 0. (You may want to change the
tolerance.)
5) Pro;ect: Use DESCRT to modify ALLZER to reduce the number of tests for
polynomials similar to 6Xa + l03X:! + 201X + 90.
CHAPTER 12

MAT Instructions in BASIC

12-1 INTRODUCTIO N TO MAT INSTRUCTION S


MAT instructions are BASIC statements which allow us to manipulate
entire arrays in the computer without being required to do it entry by entry.
This capability will enable us to write shorter programs, using arrays, than we
have been able to write thus far.
We have had to assign values of array entries one at a time. We have been
putting LET A[I, J] =
or READ A[I, J] in a nested loop for assignment and
then in order to print the array, we have been putting PRINT A[I, J]; in
another nested loop. In order to print out a 3 by 4 array consisting of -l's, we

MAT-I

8 REM LINES 10 T0 50 'ASSIGN VALUES


10 f"0R R=I T0 3
20 F0R C=l T0 4
30 LET A[R,Cl=-1
40 NEXT C
50 NEXT R
98 REM LINES 100 T0 160 PRINT THE ARRAY
100 F0R R=I T0 3
I I0 F0R C= 1 T0 4
120 PRINT AtR.ClJ
130 NEXT C
I 40 PRINT
150 PRINT
160 NEXT R
200 END
RUN
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

would proceed as in MAT-1, using programming statements and techniques


with which we are familiar. MAT-1 c~rtainly does what we said we would do.
But consider MAT-2, which is a five-line program that does what required 13
lines to do in MAT-1. In MAT-2, line 10 instructs the computer to set up a
3 by 4 array. Then line 20 reads the data into the array named and dimen-
sioned in line 10. (Some of you who had 0 subscripts will find that as soon
as you specify a MAT instruction for a particular variable you also lose 0
subscripts for that variable. Others will find the situation unchanged. This
depends on the system.) Note in line 20 a semicolon appears after the array A.
Used in this way we are specifying semicolon spacing. To get comma spacing,
we may place a comma there or leave it blank. If we wish to specify printing
for several arrays in one print instruction, we may do so as follows:
XXX MAT PRINT A, B; C
In this case A and C will be printed with comma spacing, and B will be
printed with semicolon spacing.
In MAT-2 it may not be clear just how the computer takes the numbers in
the data line and enters them in the array locations. MAT-3 is intended to
show what numbers are entered where in the array.

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 READ Y[R, CJ


Dimensions or redimensions an array named Y with R rows and C
columns and reads data into the array Y. R and C may be explicit
integers or variables.

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.

PROBLEMS FOR SEC. 12-1


1) Fill an array with the numbers 1, 2, 3, 4, 5, 6, 7, 23, 51, 47, 56, and 234 and
fill another array of the same dimensions with the numbers 2, -3, 43, 90, 45, 32,
-89, 65, 43, -96, 0, and 1. Fill a third army of the same dimensions with the sums
of the numbers in order. The sum array should contain the numbers 3, -1, 46, 94,
etc.
2) Use the data of problem 1). Dimension a 3 by 12 array. MAT READ the
above data into the first two rows and O's into the third row and then replace the
O's with the sums of the entries in the first two rows column by column.
3) Fill an array with the multiplication table up to 12 X 12. MAT PHINT the
result.
4) Fill a 4 by 3 army with the following numbers: 2, 56, 78, 3, 20, 45, 3, 9, 673,
564, 90, and 234. Have the computer multiply each number in the array by 3 and
enter the product to replace the old number. Print the result.
.180 Basic BASIC

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

IO) Fill a 2 by 8 array with all O's and print it.


11) A company has salesmen on the road four days a week. At the end of each
week each salesman turns in an expense sheeL Here is a sample expense sheet:

Mon Tue. Wed Thur.

Lodging $12.00 $11.00 $10.50 $14 00

Meals $ 4 00 $ 7.50 $ 6.90 $ 740


Transportation $ 2 00 0 0 $ 3.50
Customer
entertainment 0 $18 ..00 $ 450 $ 450
Miscellaneous $ 2.31 $ 1 . 84 $ 3 . 15 $ 1.83

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.

12-2 SOLVING A PROBLEM

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.

Tolls per Vehicle


Road Tunnel Bridge

Trucks $6.00 $3.00 $2.00


Buses $5.00 $3.00 $2.00
Passenger Cars $4.00 $3.00 $2.00
Motorcycles $3o00 $2.00 $1.00

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.

Vehicles per Caravan


Trucks Buses Cars Cycles

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

T0LLS PER VEHICLE


r<0AO TUNL BRI OGE
6 3 2

s 3 2

4 3 2

3 2

VEHICLES PER CARAVAN


TRUCK BUS CARS M0T0RCYCLES
I 3 4 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

repeat this process at the bridge substituting 2, 2, 2, and 1 for 3, 3, 3, and 2


and sum up 1 ° 2, 3 ° 2, 4 ° 2, and 2 ° 1 getting a total of $18 for the value
of C in Table 12-3. Then we would go to Caravan 2 and step through the
road tolls, then the tunnel tolls, and finally the bridge tolls. Then on to the
next caravan until we have gotten results for all the five caravans. This is
just the kind of repetitive process that we use the computer for.
We will find it helpful to think of Table 12-3 as an array with C[I, J] as
the values of the entries rather than A, B, ... , M, N, 0. Calling that array C
we get C[l, l] =A, C[l, 2] = B, etc. down to C[5, 3] = 0.
Note that after we step across row 1 in MAT Band down column 1 in MAT
A, the final sum is entered in row 1 and column 1 of MAT C. When we step
across row 1 of MAT Band down column 2 of MAT A, the sum is entered in
row 1 column 2 of MAT C. You should see that stepping across row R of MAT
B and down column C of MAT A results in a sum that is entered in row R,
column C of MAT C. Note too, that the row headings of MAT B correspond
to the row headings of MAT C and the column headings of MAT A cmre-
spond to the column headings of MAT C, and that the row headings of i\1AT A
and the column headings of MAT B coincide. With some experimentation, you
should be able to convince yourself that this is a natural consequence of the
problem and not mere coincidence for this particular set of data. If you
change the number of toll booths, all of the above statements still hold.
Summarizing, we have just tried to establish that we sum up the following
products:
0
B[R, X] A[X, C] (12-1)
where R is the row number in MAT B, C is the column number in MAT A,
and X goes from 1 to the number of columns in l'vlAT B which is the same as
the number of rows in MAT A. Having found the sum of all products in Eq.
(12-1) for a fixed [R, C] pair, we enter that sum in C[R, CJ. We do this for all
rows of MAT B and all columns of MAT A.
You should run through the calculations by hand with pencil and paper to
verify the procedure and to become more familiar with it. We draw a flowchart
in Fig. 12-1 and call the program T0LL-2.

PROBLEMS FOR SEC. 12-2


1) Suppose on a particular day there were four caravans. Caravan 1 had one
truck, Caravan 2 had one bus, Caravan 3 had one car, and Caravan 4 had one
motorcycle. Have the computer print the amounts that each caravan f>aid at each
toll booth.
2) Suppose there were no vehicles on a partin~lar day. 'What would the Road
Commission report look like?
3) Suppose there were three caravans, each having one vehicle of each type.
Print the schedule of payments for this situation.
4) On a given day there were four caravans. Caravan 1 had one motorcycle,
Caravan 2 had one car, Caravan 3 had one bus, and Caravan 4 had one tnwk. Have
the computer print the schedule of payments.
184 Basic BASIC

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

LET C[R, CJ=


C[R, CJ + Bll<, XJ
• A[X, CJ

NEXT R NEXT C NEXT x

Fig. 12-1. Flowchart for finding tolls paid


END by caravans at various toll booths for program
T0LL-2.

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'~

260 LET CCR1Cl=O


295 REM X STEPS THR0UGH THE R0WS 0F A ANO THE C0LUMillS 0F B
296 REM THERE WE F'li\10 'TRUC~S BUSES CARS M0T0RCYCLES'
297 REM IN EACH ARRAY
300 F'0R X=l T0 4
320 LET CCR1Cl=CCR1Cl+ BCR1Xl•ACX.Cl
335 REM G0 T0 THE NEXT C0LUMN 0F' B ANO THE NEXT R0W 0F' A
340 NEXT X
MAT Instructions in Basic 185
3SS REM G0 T0 l'HE NEXT C0LUMN 0F" MAT A
360 NEXT C
37S REM G0 T0 THE NEXT R0W 0F" MAT B
380 NEXT R
soo PRINT "R0AD"•"TUNNEL"• "BRIDGE"
S20 MAT PRINT C
999 END
RUN
T0LL-2

R0AD TUNNEL BRIDGE


43 28 18

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= [~

Perform the operations of this section to get MAT C.


2
6
3
7 :]
6) Suppose we let

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?

12-3 OPERATIONS AND SPECIAL MATRICES


While the MAT operations have specialized and rigid definitions in matrix
algebra, we will find at times that some of the MAT capabilities will help us
in writing prngrams not deeply involved in a matrix algebra setting. It is the
purpose of this section to list the MAT capabilities, but not to develop the
matrix algebra to any great extent. For such a treatment, you should see any
text in advanced algebra.

Multiplication

The requirements of the Road Commission report in Sec. 12-2 led us to


evolve a set of steps that occurs often in both applied and theoretical mathe-
matics. The set of steps carried out there exactly fits the definition of matrix
multiplication. Using the array names of Sec. 12-2, we define the product of
B and A as the array C, which is written as C B 0 A.
.186 Basic BASIC

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

R0AD TUNNEL BKIDGE


43 28 18

61 39 24

55 3A 21

54 34 22

29 16 10

D0NE

Addition and Subtraction


Some past problems have asked you to add two a1 ravs. For two an ays of
the same dimensions, the sum is defined as an an av containing the sums of
corresponding entiies of the given anays. In othe1 words, for all I, J pairs,
S[l, J] = A[l, J] + B[l, J], where the sum array is S. lvlatrix addition is accom-
plished with the program statement
XXX MATS A+B
or XXX MAT A =A + B
may be used if you no longer need l\!AT A.
Subtraction is defined just as you would expect For A - B, the difference
must be an array so that for all I, J pairs, D[I,]] A[I, J] - B[I, Jl The pro-
gram statement is
MAT Instructions in Basic 187

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

10 DIM AC2.4J.sc2.121.cc 10.111


20 PRINT "MAT A=ZER **
PREVI0USLY DIMENSI0NED AT 2BY4"
30 MAT A=ZER
40 MAT PRINT A
50 PRINT "MAT B=C0N(3, 71"
60 MAT B=C0NC 3, 71
70 MAT PRINT BJ
BO PRINT "MAT C=IDNC 4, 4]"
90 MAT C=IDNC 4, 41
100 MAT PRINT CJ
110 PRINT "MAT A=C0NC I• 61"
120 MAT A=C0NC I• 61
130 MAl PRINT AJ
140 END
RUN
MAT SP I

MAT A=ZER ** PREVI0USLY DIMENS10NED AT 2BY4


0 0 0 0

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.

PROBLEMS FOR SEC. 12-3


1) Redo program T¢TAL using a row vector for the numbers of items and a
column vector for the prices. Obtain the total cost \vith a single MAT statement.
2) Have the computer iind the product of

[ 21

-~
3
-3
5
-1
-2
1
-1
-'] [=n
-3
11
-1
and
MAT Instructions in Basic 189

If we think of the above as

[: -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

A=[~ l!] and B= [-12


3
28]
-7
0 0
Find the product A B and the product B A. What do you conclude?

5) Let

A=U !] B= [-1 5 -~J c-


-
[ 2
25
-11]
31
Find [A 0
BJ ° C and A 0
[B 0
C].
6) Using A, B, and C from problem 5), find A 0
[B + CJ and A 0
B + A ° C.
7) Multiply

[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-4 SOLVING SIMULTANEOUS LINEAR EQUATIONS


You should see that the matrix equation

( 12-6)

can be multiplied out on the left-hand side to obtain

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

It would be very convenient if we could just divide both sides by C. But it


turns out that the division of one 111atrix by allother is not an easily describable
process. However, we may instead multiply each side by the multiplicative
inverse of C. \Ve write that as C- 1• Doing that we get
S = C- 1 ° K
We note he1e without elaboration, the following facts:
1) In order to have an inverse, a matrix must be square.
2) Not all matrices have inverses.
3) The product of a square matrix and its inverse is the identity matrix
To see more clearly what the inverse of a matrix is, let us find the inverse of
MAT Instructions in Basic 191

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

The column vector

is to be interpreted as, W =I, X = -2, Y = -3, and Z -1. We may now =


substitute these values in Eq. (12-9) to verify that they <lo in fact constitute the
unique solution.

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.

PROBLEMS FOR SEC. 12-4


1) Let

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.

3) Solve for X and Y:


-2X- SY= -16
- X + 4Y = 31

4) Solve for X, Y, and Z:


2X- 9Y- 5Z =2
7X - 6Y + 5Z = -35
9X - 6Y + 5Z = -39
194 Basic BASIC

5) Solve for X, Y, and Z:


3X+ 4Y + Z = 7
5X- 6Y + 3Z B =
3X+4Y+Z =-3

6) Solve for W, X, Y, and Z:


6W + 3X + 6Y + SZ = -12
- 7W + SX - 7Y - Z = 77
-3W + X + 3Y + 6Z = 31
-2W - 4X + 4Y - 7Z = -76

7) Solve for W, X, Y, and Z:


-3W + 6X - SY - Z = --32
W + 9X - SY 2Z = 9
W + 6Y + SZ= 2
- 7W + 4X - Y + SZ -B6 =
B) Solve for X, Y, and Z:
2X + 4Y -
3Z -11.9 =
-9X - 3Y SB.S=
-9X + BY + SZ = 66.6

9) Solve for V, W, X, Y, and Z:


7V + 6W - 3X - Y + 9Z = 26.3
-9V + 2W + 9X + SY + Z = 91.1
-3V + 4W + SX + SZ= 62.9
6V - BX - 2Y - 6Z = -S5.6
-3V - 9W + 5X + 7Y + 3Z = -25.9

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.

12-5 TRANSPOSE OF A MATRIX

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

6 REM A IS A C0LUMN VECT0R AND B IS A R0W VECT0R


10 DIM At 10• I l • B[I • I 0 l
20 MAT READ A
30 DATA 1, 2• 3, 4, 5, 6. 7, 6, 9, 10
40 MAT B=TRNCA>
45 PRINT "TRANSP0SE 0F" C0LUMN VECT0R A"
so MAT PRINT BJ
60 END
RUN
TRP0SI

TRANSP0SE 0F" C0LUMN VECT0R A


1 2 3 4 5 6 7 6 9 10

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

TRANSP0SE 0r THE AB0VE MATRIK


3 0
6 18

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.

PROBLEMS FOR SEC. 12-5


1) Let

~
-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

FLAG FLGA FALG FAGL FGLA FGAL


LFAG LFGA LAFG LAGF LGFA LGAF
AFLG AFGL ALFG ALGF AGFL AGLF
GFLA GFAL GLFA GLAF GAFL GALF

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!

10 PRINT "FIND THE FACTORIAL OF"J


20 INPUT N
30 LET F=I
40 F0R X=N TO I STEP -I
50 LET F=F*X
60 NEXT X
70 PRINT NJ"FACTORIAL ="lF
80 END
RUN
N!

FIND THE FACTORIAL OF?4


4 FACTORIAL = 24

DONE

Of course for larger integers, N! becomes very large.

RUN
N!

FIND THE FACTORIAL OF?20


20 FACTORIAL= 2.43290E+l8

DONE

Suppose we want to find the number of three-letter words that can be


formed from the letters of the word COMPUTER without duplication. For
the first letter we may pick from among eight, for the second we may pick
from among seven, and for the third we may pick from among the remaining
six letters. This makes 8 ° 7 ° 6, or 336, different words. Since the order is
different, these are 336 different permutations. Notice that
8 °7 °6 °5 °4 °3 °2° 1 8! 8!
8 0 7 0 6 = 5 0 40 30 20 1 = 5f = (8 - 3)!

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

8 THINGS 3 AT A TIME HAVE 336 PEflMIJTATIO,\JS


4 THINGS 4 AT A TIME HAVE 24 PERMIJTATIO·\JS

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

and solving for NCu we get


:-;Pn N!
:r.;C1t = ----ru- = (R!)(N R)!
Thus, the number of combinations of three letters selected from eight different
letters with no duplications is
8!
sCs = 3!5! = 56
while the number of permutations is

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

Fig. 13-1. Flowchart for computing


(_Start combinations of N things taken R at
a time.

END )

LETC = 1 F</>R X = N Tq')


N--R+1 STEP-1

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

8 THINGS 3 AT A TIME HAVE 56 C0MBINATIDNS


4 THINGS 4 AT A TIME HAVE I CGMBINATI0~S
20 THINGS 5 AT A TIME HAVE 15504 CCMBINATIDNS

D0NE

Permutations of Things Not All Different


Suppose we want to know the number of arrangements possible for the let-
ters of the word PROGRAM. Sir1ce there are two R's and we cannot tell which
is which, taking 7! counts every distjnguishable arrangement twice, because
the R's may occupy two positions in 2! ways. Therefore, the number of words
is 71/21. How many ways can we arrange the letters of the word ABSENTEE?
Well, if the E's were distinguishable, we would get 8!; but that counts the
indistinguishable anangements 3! times, because three E's can be arranged in
three locations in 3! indistinguishable ways. So we get 81/3!. The letters of the
word SNOWSHOES can be arranged 91/2!3! ways, because the two O's can
be ananged in 21 ways and the three S's can be arranged in 3! ways.

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.

PROBLEMS FOR SEC. 13-2


1) In how many orders can 15 people enter a classroom?
2) In how many different ways can 15 keys be put on a circular key ring?
3) Cars come in 18 colors, seven models, four engines, and there are 15 options
such as whitewalls, outside mirror, radio, etc. How many different cars are available?
4) You have 25 different books and two bookshelves, one of which holds exactly
12 books and the other holds exactly 13 books. In how many ways can the books
be arranged on the shelves?
5) In a class of 30, a six-member committee is to be selected. How many differ-
ent committees are possible? If there are 15 girls in the class, how many of the com-
mittees consist of six girls?
6) How many different five-card hands may be dealt from a deck of 52 cards?
7) How many different 13-card hands may be dealt from a deck of 52 cards?
8) There arc five people in a room. In how many ways can they all have
different birthdays? Use a 365-day year and ignore Feb. 29.
9) In how many ways can 10 people have all different birthdays? Ignore Feb. 29.
10) If a state uses three letters followed by three digits for its license plates,
how many different license plates can it produce?
11) You have five different flags with which to form signab by arranging them
all on a flagpole. How many signals can you form?
12) You have five different flags with which to form signals by arranging up to
five of them on a flagpole. How many signals can you form? Let zero flags constitute
a signal.
13) You have 10 different flags with which to form signals by arranging up to
five of them on a flagpole. How many signals can you form?
14) You have 50 friends. You are going to have a party and can only invite 25
people. How many different guest lists could you have?
15) In how many ways can 15 people sit in a row of 15 chairs?
16) Do problem 15) if t\\ o of the people must sit next to each other.
17) How many different \\'ords can he formed from the letters of the word
COMPUTEHS if 1) you must use all of the letters and 2) you must leave out one
letter?
18) A class consists of .30 students of which 17 arc girls. In how many ways can
\\ e select a committee of four? Ho\\' many will have two boys and t\\ o girls? How
many will have one hoy and three girls? How many will have fom girls? How
many will have four boys?
19) How many outcomes arc pos:;iblc fm rolling two dice followed by drawing
three cards from a 52-card deck?
204 Basic BASIC

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?

13-3 SIMPLE PROBABILITY

We defined probability in Sec. 13-1 as S/T, where S is the number of


ways in which an outcome may constitute a success and T is the number of
possible outcomes, and all outcomes are equally likely. For flipping a coin,
we see that the probability of coming up heads is 1/2 or .5. For drawing a
card from a 52-card deck, the probability of getting the ace of spades is 1/52
or about .0192.
Suppose you are in a class of 29 students and a committee of four members
is to be selected at random. What is the probability that you get on the com-
mittee? Well, the total number of committees possible is 29 C 4 • Now all we
have to find is how many of those committees count yourself as a member.
We can find out by saying in effect, "Let us put you on the committee and
pick the other three members from the remaining 28 class members." This
means that you will be on 28 C 3 of the committees, and the probability that
you get on the committee is 28 C 3 / 29 C 4 • Let us write a program to compute
this probability. We can use lines 30 through llO of program NCR as a
subroutine to first find 28 C 3 and then find 29 C 4 • See program CLASS. You can
see that your chances are about 14%. You should also see that the probability
that you do not get on the committee is about 1 - .14 or .86.

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

THE PR0BABILI TY THAT Y0U GET 0N A 4 MEMBER


C0MMITTEE F'K0M A CLASS 0F' 29 IS 0137931

OONE
Elementary Probability 205

Suppose we roll a die. The probability that a 3 comes up is one in six or


1/6. Now roll the die again. Again, the probability of a 3 is 1/6. We can see
that if we roll the <lie twice, the probability of both rolls coming up 3 is
(1/6) 0 (1/6), or 1/36. We define an event as a set of outcomes for a particular
experiment. If we have two events A and B such that the probability of suc-
cess for A is P and the probability of success for B is Q, the events A and B
are said to be independent if the probability of success for A and B both is
P 0 Q. This is exactly the case for rolling a 3 on each of two <lice, which
enables us to arrive at probabilities without actually enumerating outcomes.
Thus we have extended our definition of probability.
For rolling two dice, the events associated with the first die are independent
of the events associated with the second die. The same may be said of rolling
the same die twice. Flipping two coins are independent. Drawing a card from
a deck is independent of rolling a die. So, the probability of getting a 1 and
an ace upon rolling a die and drawing a card is (1/6) 0 (4/52), or (1/78).
Let us look at a problem often referred to as the "birthday problem." Sup-
pose you are in a room with 29 other people. What is the probability that
at least two people have the same birthdate? We can say that if the probabil-
ity of no two people having the same birthdate is P, then the probability
that at least two do have the same birthdate is 1 - P. The birthdates for two
people are independent events, so we may multiply individual probabilities.
Picking any person first, we say that his probability of having a different birth-
date from those already picked is 365/365. Picking a second person, the
probability that his birthdate is different from the first person's is 364/365.
For the third person we get 363/365 as the probability that his birthdate is
different from the first two, and for the 30th person we get 336/365 as the
probability that his birthdate is different from each of the first 29 birth<lates.
So, the probability that all are different is

365 364 336


p = 365 ° 365 • ' ' . 0
365

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

THE P:<OHABIL I TY :JF H!O O!< MO«E


IDENTICAL BlklHDATES AMONG 30 PF.:1PL~ JS ·706316

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.

PROBLEMS FOR SEC. 13-3


1) A class of 29 has 16 girls. A committee of five is selected at random. What
is the probability that all five committee members are girls?
2) Ten people are to sit in a row of 10 chairs. What is the probability that two
particular persons sit next to each other?
3) What is the probability of being dealt the ace of spades, the three of clubs,
the eight of hearts, the seven of diamonds, and the 10 of clubs?
4) What is the probability of being dealt the ace, king, queen, jack, and 10 of
spades from a 52-card deck?
5) What is the probability of the first six flips coming up heads and the last
four tails when flipping a coin IO times?
6) What is the probability of getting all heads when flipping a coin 10 times?
7) You have a list of 20 true-false questions from which 10 will be selected at
random for a test. Of the 20, there are 15 you are guaranteed to get right and five
that you are guaranteed to get wrong. What is the probability that you will get
exactly eight right?
8) An experiment consists of drawing a card from a 52-card deck until the first
ace appears. Find the probability of the first ace appearing on the fourth draw.
9) For the experiment of problem 8), find the probability of the first ace appear-
ing on draws one through ten.
10) An experiment consists of rolling a die until it comes up 2. Find the proba-
bility of the first 2 coming up on the fourth roll, on the tenth roll.
j 11) Refer to the birthday problem. How many people must be in a room to have
the probability of at least two identical birthdates be .5?
12) You are in a room with 29 other people. What is the probability that one of
them has your birthdate?
j 13) How many people must be in a room for the probability of another person
to have your birthdate be .5?

13-4 RANDOM SIMULATION


We may use the random number generator to simulate experiments that
occur at random. We can have the computer flip a coin by generating two
random digits. We can roll a die by generating six random digits, etc.
Elementary Probability 207

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

H TTHTTTH TTHHHTTTHHH THTTHH-ITTTT>-HH rlHTTHTHTH TTTTTH Trl ;


HEADS 23 OUT (JF 50 FL I PS

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

S REM THE MAT INSTRUCT10NS ARE C0NVENIENT HERE


10 DIM L[ I• 7l
20 MAT L=ZER
38 REM THE C0MPUTER 00ES SO THR0UGH 110 1000 TIMES
40 F0R X=l 10 1000
SO LET C=O
SS REM C IS G01NG T0 C0UNT I'S
SS REM LINES 60 THR0UGH 100 R0LL 6 DICE ANO C0UNT I'S
60 F0R R=l T0 6
70 LET U=INT<6•R NOC!J+l)
80 IF U> I THEN I 00
90 LET C=C+ I
100 NEXT R
110 LET u1.c+1l=L ll1C+ll+I
120 NEXT X
130 PRINT "N0NE 0NE TW0 THREE F0UR Fl VE SIX"
140 MAT PRINT LJ
I SO END
RUN
R0LL

N0NE 0NE TW0 THREE F0UR Fl VE St X


343 410 193 44 9 I 0

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

NEXT R LET C = C+1

Enter C into
NEXT X
vector

END
Fig. 13-2. Flowchart for rolling six dice 1000 times.

PROBLEMS FOR SEC. 13-4


I) Have the computer flip six coins 1000 times and print the distribution of
outcomes.
2) Sketch a graph of the distribution for problem I) and the distribution for
program R0LL.
3) Write a program to deal five-card hands from a 52-card deck. Be sure not
to deal the same card twice.
4) A company manufactures light bulbs and can openers. For light bulbs it is
known that I in 20 is defective and for can openers I in 25 is defective. Write a
program to select at random one light bulb and one can opener 1000 times. Total
each of the following: the number of times neither was defective, the number of
210 Basic BASIC

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.

13-5 BINOMIAL TRIALS

Suppose we roll two dice. What is the probability that a 1 comes up


exactly once? If we use one red die and one green die, we may more clearly
describe the results. There are two ways that that we could get exactly one 1.
First, we could have the red die come up 1 and the green die not come up 1.
The probability of this is (1/6) (5/6). Second, we could have the red die
0

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

. So the probability of exactly two l's up for a roll of four dice is

which simplities to 25/2rn.


Suppose we have 10 dice. What is the probability that exactly two dice
come up 1 when all 10 are rolled? For a particular selection of two dice, we
get (l/6)2 ° (5/6) 8 and we can select the two dice in 10 C~ ways. So,
P = 10C2 ° (1/6) 2 (5/6) 8
0
Elementary Probability 211
We can write a short program to find the value of P. Note that
in program
DICE lines 15 through 40 comput e the value of C .
10 2

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

In general, we should see that if an outcome has probability P of success


and Q of failure and we perform an experiment consisting of N trials, the
probability of exactly R successes is
p =NCR 0 pR" QN-R

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

PROBLEMS FOR SEC. 13-5


1) Moclify PASCAL to sum up the coefficients. Print the values of R and the
sum of the coefficients. Do not print the coefficients.
2) Modify DICE-I to sum up the individual probabilities. Have the loop go
from 10 to 0.
3) It is known that 1% of the population has a certain type of blood. In a class
of 25 persons, what is the probability that exactly two people have this hloocl type?
4) A company makes bolts. It is known that 1 in 1000 is defective. You buy a
box of 100 bolts. \Vhat is the probability of getting exactly one defective bolt?
5) For the <:ompany in probiem 4), what is the probability of getting iO defective
bolts.
6) For the <:ompany in problem 4), what is the probability of getting at least one
defective bolt.
7) For the company in problem 4), what is the probability of getting less than
five defective bolts.
8) Find the probabilities of getting zero through six 1's when rolling six dice.
Compare your result-; with the random simulation in program R0LL.
9) What is the probability of getting zero through 10 heads when flipping 10
coins.
10) What is the probability of getting more heads than tails when flipping 10
coins.
11) A test consists of 25 true-false questions. You know that your probability of
guessing right on any given question is 75%. Find the probability of getting 76% on
the test, if you guess on all questions. Find your probability of getting 76% or better.
j 12) An experiment <:0nsists of Hipping a coin until it comes up heads. Find the
probability of success for 1 to 10 flips.
j 13) An experiment consists of rolling a die until it comes up 1. Find the proba-
bility of su<:eess for 1 to 10 flips. Find the probability that success will require more
than IO rolls. Find the probability that success will require more than 20 rolls.
APPEND IX A

Storing Programs on Paper


Tape

A-1 INTRODUC TION


Once you have written your program, you would like to have the computer
execute it In order to execute a program it must be typeg into the com-
puter. Ideally, we should all be expert typists, but many of us are not.
So, many time-share terminals provide for punching programs on paper tape
when the termi.nal is not connected to the computer. Then the high-speed
tape reader may be used for reading the program in on-line. Even so, the
considerate student will do his utmost to improve his typing speed so as not
to tie up the terminal when others would like to be typing. One suggestion
is to type all programs in advance before sitting in front of the terminal to
punch tape. You will benefit in two ways: by getting practice in typing and
by being able to read the program easily.

A-2 PUNCHING PAPER TAPE OFF-LINE


Programs may be stored on a narrow strip of paper tape by punching rows
of holes in a code. Each row represents a character, space, line feed, carriage
return, or other nonprinting character.
There are so many variations from one time-share company to the next,
that we cannot list them all here. But we can outline the general procedure.
First the terminal must be switched to local. Then the tape punch apparatus
must be turned on. Now you want some blank leading tape so that the tape
reader will be able to read the first character of your program. Some terminals
gene1 ate the blank leader by depressing the HERE IS key. On others, you
may have to depress the REPT and RUBOUT keys simultaneously until suffi-
cient tape shows, or try depressing CTRL, SHI.FT, REPT, and Pall at once.
Now you may type your program. As you type, holes will be punched,
which the tape reader will interpret when you feed the finished tape back.
Be sure to begin each line with a line number and touch the RETURN and

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.

A-3 READING PAPER TAPE

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.

A-4 GETTING THE COMPUTER TO PUNCH PAPER TAPE


Having read Appendix B, you can see that after you have read in a program
1 "I l•o• A ('o 1 1 11
on tape, you may maKe rna11y cna11g,es or auu1uuus. n.1 Le1 yuu 11a ve 111a(,e a11
1

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.

B-2 ERRORS THAT PREVENT RUN


These are very often simply typing errors:

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

As far as BASIC is concerned, line 10 says READ X. The AND Y is not


part of the language and so is rejected. Since there is no way to know just
what the characters after X mean, if the first one is not a comma or a single
digit, the computer will not take a guess at what you meant. You must say
exactly what you mean; the computer is not clairvoyant. So, change line 10
as follows:

10 READ X1Y
RUN

4 s
7 -11

0UT 0r DATA IN LINE 10

To multiply X times Yin algebra we write XY.

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

However, even though in algebra XY is understood to mean X times Y, you


must be more explicit for the computer, and write X 0 Y.

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

120 DATA 5o 11• 25. -5


130 PRINT Wt2
140 G0T0 60
150 DATA 2. 67.-1•
RUN
N0 STATEMENT TYPE F0UND IN LINE 30
MI SS ING ASSIGNMENT 0PERAT0R IN LINE 50
MISSING RIGHT PARENTHESIS IN LINE 80
MISSING ASSIGNMENT 0PERATi2JR IN LINE 90
N0 STATEMENT TYPE F0UND IN LINE 100
CHARACTERS AFTER STATEMENT END IN LINE 110
MISSING 0R ILLEGAL DATA ITEM IN LINE 150

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

0UT 0F DATA IN LINE 60


Error Diagnosis 221

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

0UT 0r DATA IN LINE 40

There is no substitute for experience.

B-3 ERRORS THAT TERMINATE A RUN


The possible errors in this category become more and more plentiful as
you use more and more advanced capabilities. However, the error messages
are rather explicit and so the most fundamental examples should serve to
demonstrate how termination errors operate. Probably the most common error
for beginning programmers is that the data is either missing or not matched
correctly for the READ variables.

10 PRINT "THE RUN HAS BEGUN"


20 LET A=2
30 READ B,C,D
40 PRINT B*C•A
50 PRINT "D ="JD
60 PRINT "WE G0T T0 LINE 60 AT LEAST"
70 READ X
80 PRINT X
90 DATA 3, I 7, 11
100 END
RUN

THE RUN HAS BEGUN


867
D = II
WE G0T T0 LINE 60 AT LEAST

0UT 0r DATA IN LINE 70


222 Basic BASIC

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:

10 PRINl "A","B",,"A-8 11 1"At8"


20 READ A.8
30 PRINT A,8,A-8,AtB
40 G0Hl 20
50 DATA 1, 2, 5, 3, 3. 41, 2, 1. 23, 4, o, o, 4. 03, 5
60 END
RUN

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.

B-4 ERRORS THAT CAUSE UNEXPECTED OR INCORRECT RESULTS


These are the most difficult errors to find. Suppose you write a program and
the computer prints nothing, but notifies you that it has run the program in
the usual manner.

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:

10 PRINT "A 8 SUM PROD


20 LET C=O
30 READ A, B
40 LET C=C+ I
50 PRINT AJBJA+B;A*BlCJ"PAIRS S0 F'AR"
60 G0T0 10
70 DATA I 01 20, I I 1 9
80 DATA 1. 2. -45. 18
90 END
RUN

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

0UT 0F' DATA IN LINE 30

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

0UT 0F' DATA IN LINE 30

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

0UT ilF DATA IN LINE 30

f. . nd novv the program is as follovvl's:

10 PKINT "A B SUM PKOD


~o LET C=O
30 KEAD A,8
4Ll LET C=C+ 1
!>O PtUNf AJBJA+B;A*BJC;"PAit<S S0 FAW'
60 G:H0 30
70 OAfA lQ,;:!Q,l!.9
tlO DATA 1, 2, -45, 18
90 t:ND

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

Special Formatting Functions

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

5 PRINT "HELL0 THEf<E"


10 PRINT TABC 10)J "HELL0 THERE"
20 ENO
KUN
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

F0R TEN YC:AkS


@4.51 C0MP0UNDED M0NTHLY••• $ BB· 24
~4.751 C0MP0UNDED QUARTERLY ••• $ 90.29

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

140 PRINT TAB< X > J "$"; FNH< P >


150 PRINT "84. 75% C0MP0UNDED QUARTERLY••.";
160 PRINT TAB<X>J "$"; FNH<Pl)
9999 END
RUN
BANK2

F0R TEN YEARS


84.5% C0MP0UNDED M0NTHLY... $ 88.24
84.75% C0MP0UNDED QUARTERLY ••• $ 90.29

D0NE

C-2 IMAGE STATEMENT


There may be an IMAGE statement available on your system. An IMAGE
statement provides the printing pattern for a PRINT statement in yet another
statement. For our BANK2 program, we would use the following set of
statements:
130 PRINT USING 140, FNH(P)
140 :@4.5% C0MP0UNDED M0NTHLY $ ## ##
150 PRINT USING 160, FNH(Pl)
160 :@4.75% C0MP0UNDED QUARTERLY $ ## ##

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

Summa of Flowchart Shapes

( TerminaQ Used for beginning and ending of program.

Indicates data entered into the computer or results


returned by the computer.
Input
Output READ MAT READ READ#
PRINT MAT PRINT READ:
INPUT MAT INPUT WRITE#
WRITE:

Indicates that a decision is being made.


IF xxxxxx THEN yyy

Indicates a sequence of program statements not in-


Predefined cluded in the flowcha1 t May be used for G0SUB
process
statement.

Connector. Indicates transfer from one statement to


another other than the next higher numbered
0 statement in the program. N matches another N
elsewhere in the same flowchart.

Used for anything not already specified.


Operaiion NEXT X
Opens loop
Closes loop LET
RETURN
ST0P

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.

END It is the highest numbe1ed statement of every BASIC


program. It is optional on some systems and re-
quired on others.
PRINT Prints values of variables, calculated values, and
literal expressions inside quotes. Spacing is con-
trolled by semicolons or commas.
READ Enters values stored in DATA statements into vari-
ables named in the READ statement. All legal
BASIC va1iables (string and numeric) may be read
in a single READ statement by separating them
with commas.
DATA Stores values for READ statements. Items of data
must be separated by commas. Some systems re-
quire that strings be in quotes.
INPUT Same as READ except that data is to be typed on the
keyboard of the remote terminal.
LET Assignment statement. The word LET is optional on
many systems. Stores the value on the right of an
equals sign in the variable named on the left. May
be used to assign string variables. Multiple assign-
ment is available on most systems.
G0T0n Names n as the next line number to be executed by
the computer.
IF-THEN n Tests the truth of an algebraic sentence placed be-
tween the IF and the THEN. Sends the computer
to line n if the sentence is true. Control passes to
the next line of the sentence is false.

229
230 Basic BASIC

REM Permits the programmer to remark upon the program


in the program itself without affecting the program
operation. Some systems allow an apostrophe, ', to
serve the same purpose.
=
F0RX AT0B Opens a machine loop with first value for X at A, last
STEPC number B, and increment C. If C is omitted, the
step defaults to an increment of 1.
NEXTX Closes machine loop. Sends the computer to the
corresponding F0R statement to increment and
test X.
G0SUBn Sends the computer to a subroutine beginning at line
n. Upon executing a RETURN statement, the com-
puter returns to the line immediately following
G0SUB n.
RETURN Closes a subroutine.
DEF FNA(X) Program-defined function. The letter pair FN desig-
nates that a function is called for. The function
name is A and the argument is X. Any letter of
the alphabet may be placed where the A is. Some
systems permit several variables as arguments.
DIM A( ),B$( ).... Declares dimensions for one- or two-dimensional nu-
meric arrays or string arrays or both. One number
is required in the parentheses for a list, and two
numbers separated by a comma are required for a
two-dimensional array.
ST0P Execution of ST0P statement causes termination of
the RUN at that point.
REST0RE Restores all data in the program. The next item of
data to be read will be the very first data item in
the program (not discussed in text).
CHANGE A$ T0 A Stores the ASCII code of the characters of the string
A$ in the array A with the length of the string in
characters stored in A(O).
CHANGE A T0 A$ Stores a string in A$ with length specified in A(O)
and characters determined by the ASCII code
stored in the array elements of the A list.

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

SQR(X) Computes the nonnegative square root of X. X must


be nonnegative.
ABS(X) Computes the absolute value of X.
SGN(X) Returns the value 1 for X positive, 0 for X equals
zero, and -1 for X negative.
INT(X) Returns integer part of X. For some systems this is
the mathematically greatest integer function. For
others, the computer simply chops off the digits
to the right of the decimal point. (The results are
the same for nonnegative numbers.)
RND(X) Generates a random number. In some systems the
set of random numbers accessed is determined by
the value of X. Some systems generate the same
set of numbers each time the program is run,
whereas others provide a different set and still
others provide an option. See RND below.
RND Returns a random number. The numbers will be the
same on successive nms of the program if the
RAND0MIZE statement is not present in the
program and different on successive runs if the
RAND0MIZE statement is present.
RAND0MIZE Causes the random numbers generated in successive
runs of the same program to vary.
SIN(X),C0S(X), T AN(X) Computes the sin, cos, or tan of X, where X must be
in radians.
232 Basic BASIC

ATN(X) Computes the arctan of X. ATN(X) is in radians.


The program must be written to determine the
correct quadrant for the result.
L0G(X) Computes the logarithm of X using base e.
EXP(X) Computes the number whose L0G base e is X.
TAB(X) Moves the printing mechanism to the (X + l)st posi-
tion of the carriage unless the printing mechanism
is already past that point, in which case there is
no effect
LEN(A$) Returns the number of characters in the string A$.
EXT$(A$,I,J) String extract function. Isolates a substring in A$
from the Ith to the Jth character inclusive.

FILES

Hewlett-Packard Files

FILES Names files to be used by the present program and


makes them available for access. File names are
separated by commas.
READ #N,R Sets the file pointer to the beginning of the Rth rec-
ord of the Nth file named in the files statement. In
addition, when followed by a semicolon and vari-
able list, this statement reads values from the file
to the variables.
READ #N; When followed by a variable list, this statement reads
from the file at a point previously established.
MAT READ# Reads values from a file with the same options al-
lowed for READ #.
PRINT #N,R Sets tl;e file pointer in the Nth file named in the files
statement to the beginning of the Rth rcord and
erases the contents of that record. In addition,
when followed by a semicolon and a variable list,
this statement causes the contents of the variables
to be printed into the file.
PRINT #N; When followed by a variable list this statement
causes the contents of the variables to be printed
wherever the file pointer has been previously set.
IF END #N THEN n When executed, this statement sets a flag. If at any
later time an attempt is made to read past the end
of data or past the physical end of the file or to
i~rjnt f''1<t tJlP I'hydr>n J Pnr] nf t]lP filp, NmtrnJ
passes to line n.
TYP(N) The TYP (N) function takes on values from 1 to 4,
depending on the nature of the next information
in the file TYP(N) becomes 1 for number, 2 for
Summary of Statements in BASIC 233

string, and 3 for end of file. If the argument is


negative, the value 4 will be returned for end of
reconl.

General Electric Files

FILES Names files to be used by the current program and


makes them available for access. File names are
separated by semicolons.

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.

Binary Sequential Files

Binary sequential files may be prncessecl by all of the above statements by


substituting a colon (:) for the pound sign (#). Binary files should be less ex-
pensive to work with; however, ASCII files are very convenient due to the fact
that they may be listed at the terminal.

Random Access Files

READ :N, Same as ASCII.


WRITE :N, Same as ASCII.
IF M0RE :N Tests true, except when the file pointer is at the phy-
sical encl of file.
IF END :N Tests false, except when the file pointer is at the phy-
sical encl of file.
SCRATCH :N Places the file pointer at the beginning of the file and
fills the file with binary zeros.
234 Basic BASIC

REST0RE :N Places the file pointer at the beginning of the file


without altering the contents of the file.
SETW NT0X Places the file pointer to the Xth word of file N. To
access a random file by record, the formula
W 0 (R - 1) + 1 places the pointer at the begin-
ning of the Rth record if there are W words per
record.
APPENDIX F

Index of Programs in Text

Program Description Page


ADDA,B Adds complex numbers in (A,B) form 148
ALLZER Finds nonreal zeros for some fourth-
degree polynomials 171-172
ARANGl Prints list after each exchange in
ARANGE 42
ARANG2 Uses G0SUB for printing in ARANGl 44
ARANG3 Orders a list of random integers 65
ARANG5 Orders lists in excess of 10 elements 67
ARANGE Orders list of 10 numbers from data 40
ARRAY! Demonstrates filling an array 79
ARRAY2 Prints contents of ARRAY! 80
ARRAY3 Detailed array manipulation 81
ARRAY4 More detailed than ARRA Y3 82-83
ASIS ABS( ), SQR( ), INT( ), and SGN(
demonstration 48
AVERAGE Calculates test average from file SC0RE
(GE) 117
AVG Average test scores 31
AVGCNG AVG done with nested loops First nested
machine loops 32
BANKl Compound amount on $56.31 at two
rates 56
BANK2 BANK! with rounding and spacing 60
BASE Change base-10 numbers to base-2 96
BASE-2 Base with digits printed closely packed 97
BIRTH Probability of two identical birthdates 205
CHANGE Demonstrates change statement (GE) 107
CHANGF Demonstrates EXT$ function (GE) 107
CLASS Probability of membership on a commit·
tee 204
CL0CK1 Clock arithmetic with time of day 69
CL0CK2 Modifies CL0CK1 69

235
236 Basic BASIC

Program Description Page


CMPINT Finds compound amount of $2000 after
nine years 33
C0MFAC Finds greatest common factor using the
Euclidean algorithm 93
C0MP-l Finds nonreal zeros for trinomials 170-171
C0NVRT Converts inches to feet and inches 54
C0URS1 Finds numbers of persons taking five
courses 77
C0URS2 C0URS1 with random data 78
DAYSOl Reads days of week into MAT W$(GE) 106
DEF( ) Introduces DEF 58
DEMREM Demonstrates finding remainder after in-
teger division 53
DESCRT Uses Decartes' Rule of Signs 174
DICE Probability of two l's on 10 dice 211
DICE-1 Probability of 0 to 10 l's on 10 dice 211
DIGIT Prints base-10 numbers digit by digit 99
DIGIT2 Prints integers using string output (HP) 105
ENTERA File prints one name to a record (HP) 113
FIRST$ First string program 102
FLIP Simulates flipping a coin 50 times 207
FLIP-1 Simulates flipping 50 coins 10 times 207-208
G0SUB G0SUB demonstration program 43
INTZER Finds integral zeros of polynomials 161
IZER01 INTZER with a subroutine-defined func-
tion 164
LADER Uses SIN( ) in right triangle 135
LADERl Gets several values for LADER 136
LADER2 Introduces arctangent function 137
LAWC0S Given three sides of a triangle 143
LAWSIN Given two angles and nonincluded side 140
LEFT Probability of 10% success on 100 bi-
nomial trials 212
LISTI First list demonstration program 44-45
L00Pl First loop introduction 25
L00P2 Second loop introduction 25
L00P3 First machine loop using F0R-NEXT 26
L00P3+ Uses the loop variable 26
LUPDEM Demonstrates several loop properties 27
MAMD12 Multiply and add mod 12 70
MAT-1 Preintroduction to matrix instructions 176
MAT-2 First MAT READ and MAT PRINT 177
MAT-3 Shows the order in which MAT READ
reads 177
MAT-4 Prints column vector 178
MAT-5 Prints row vector 178
MAT-6 Shows redimensioning of a matrix 178
MATINV Takes the inverse of a matrix 191
MATSPl Special matrices in BASIC 188
Index of Programs in Text 237

Program Description Page


MLTR,G Multiplies two complex numbers in polar
form 151
M0Dl2 Mod 12 addition 70
N! Computes factorial N 199
NCR Computes combinations of N things R
at a time 201-202
NPR Computes permutations of N things R
at a time 200
0RD$ Compares strings for order 102
0RDER$ Alphabetizes characters of a string (GE) 108
0RDERA Alphabetizes names in a file (HP) 114
0RDERAVG Arranges student data by test average in
a binary file 120
PAIRS Prints all possible pairs from two sets of
four numbers 38
PASCAL Prints numbers of Pascal's triangle 213
PICK Selects numbers from a random access
binary file 119
PL0Tl-PL0T5 PL0Tl tluough PL0T5 develop a plot-
ting program 127-130
P0LAR Prints coordinates from polar graphing 145
PRIME I Finds prime integers from data 49
PRIME2 More efficient than PRIMEl 49
PRINT First HP file program 110
PRINTl Uses 'IF END' in HP file 111
PRINT2 Prints to a file random access (HP) 112
QUADl Finds real roots of
AX t 2 + BX + C = 0 123
QUAD2 Analyzes parabola 125
RAND3+ Random decimal numbers 1. to 11 63
READ Reads a data file (HP) 111
READOl Uses 'IF END' in HP file 111
READ02 Reads from a file random access (HP) 113
READA Reads names from a file (HP) U.3
READTEST Reads file SC0RE (GE) 116
REAL Finds intervals for real zeros of poly-
nomials 166
REALI REAL with input to reduce interval size 166-167
REDUCE Reduces common fractions 52
RND Loads a binary file with 18 random num-
bers (GE) 119
RND(l) Introduces random number generator 62
RND(2) Random integers 0 to 9 62
RND(3) Random integers 1 to 10 63
R0LL Simulates rolling 6 dice 1000 times 208
R00TS Takes roots of complex numbers in polar
form 153
R0UND Uses INT(X " 100 + .5)/100 for first time 57
SC0RE Listing of ASCII file (GE) 116
238 Basic BASIC

Program Description Page


SC0REI Listing of ASCII file (GE) 118
SEG$I Demonstrates string subscripts (HP) I03
SEG$2 Prints one digit numeric using string out-
put (HP) 104
S0LVE Solves four equations and four unknowns I92
SRVEY I More processing of data' in SURVEY 88-89
SUMI Adds integers I through 50 I7
SUM2 Modifies SUMI 17
SUM3 Modifies SUM2 20
SUM3+ SUM3 using machine loop 29
SURVEY Uses a I5 by 5 array to analyze a ques-
tionnaire 86-87
SYNDIV Divides a polynomial by X - R 158
TCHB Tabulates four items per family 76
TCHB+ TCHB done with an array 83-84
T0LL-I Reads and prints data for T0LL-2 182
T0LL-2 Prints schedule of tolls paid I84-I85
T0LL··3 T0LL-2 with matrix operation 186
T0TAL Finds total cost of five different items 21
T0TAL+ T0T AL using machine loop 29
TRAGL Demonstrates variable loop limit 34
TRI 0 BI Multiplies a trinomial by a binomial 156
TRIG I Prints trig table 0 to 80 degrees 134
TRP0SI Takes the transpose of a column vector 195
TRP0S2 Takes the transpose of a 2 by 4 matrix 195-196
TV'S Tabulates survey of TV sets per family 74
WRITEAVG Loads file SC0REI from file SC0RE
(GE) 118
APPENDIX G

Answers to
Even-Numbered Problems
N)
>!>. APPEND IX G
c:;,

Answers Eve u bered Problems

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

The answer to th s wi 11 vary from 286 ior computers 10 LET N=<2Jo461-7o048)14


LET 0:4.96tJ-67o8•2
10 PRINT "INITIAL BA!.ANCE" S1<1.2J'"
20 20 LET B::t.o1.2J
permitting only single letters of the alphabet. upward. JO PRINT N/"D JO READ T
Some systems permit dollar signs and ampersands as 40
RUN
ENO JS
<10
IF' T"O THEN 110
1..ET a-..a .. r
variables. Other sy,.tems permit several letters of the PRINT B
alphabet, such as ABX. as simple variables. "'
-9.61374
50
60 GOTO JO
100 OATA 9,-J.2.o1.-1.9a.-J.as .. o

Problem No. 4
DONE
110
RW
END
/
N0.12

The quote IS an nstruction to the computer that the INITIAi... BALANCE"


2J.23
Sl.t.23

printed message 1s terminated. Here again. systems dif- CHAP. 2 19,9<,i


fer. Some systems r erm1t printing double quotes (") by Problem No. 2
18-01
l.4ol6
including them bel\;een Single quotes ('), such as: 'HE
SAID. "HELLO." ' The READ statement calls for two variables. A Single DONE

zero would have caused the program to terminate, as


the variable P would find no data.
Problem No. 6
10 ><£AU A,Ji
Problem No. 14
20 PrtINT A+~
25 OATA 1 ·E•06 •• 0QI
Problem No. 4
10 LET G•O
JO ENO ~ .-.CEM THE LINES USED TO MODIFY l-'1W(HAM SUM2 AriE 75• 15 ANU J5 20 LET D"I

"'"'
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

OCNE DONE OONE


Problem No. 10 Problem No. 6 Problem No. 16
10 l~AO A,!!•C•O•E•F'•G•H•l•J 10 Lt::T N"IOOI
2U ... NINT A•=i•C•IJ•E•F•G•Y•!+J
10 LET T"O
20 LET S=O
JO OATA 1,2,3,4,5,f,.?•'i•9• 10 15 LET C•O
JO LET T=O
40 ENO
2D READ S
JS LET T11T• \
A'JN
JO IF" SaD THEN 70
40 LET S"'S•N 40 LET CsC• I
IO 45 LET N;N+ 1 l
50 LET T"T•S
50 If' N >;: 2213 THEN
60 GOTO 20
70 GOTO JS
70 PRINT "GEORGE TOOl("J CJ "TESTS"
100 PRINT "THE SUM ar THE NU'16ERS I S"J s BO PRINT "FOR AH AVERAGE OF""J T/C
DONE 110 PRINT "'THE NUMBER OF NLNBERS I S"J T
90 OATA BJ,91,97, ID0,09,0

Problem No. 12
200
RUN
NO•b
END
.,.,
200

N0· 16
EHD

10 READ A.a.c.o THE SUM OF' THE NUMBERS IS 178266.


20 Pl'IINT A•C•A•D•>J•C.lhD GEORGE T0£1K 5 TESTS
THE NLHBER OF NUMBERS IS 111
JO DATA l•?.,J,4,2.-J,J,? FSA AN AVERAGE OF" 92
40 GCTO 10 DONE
50 END OONE
RUN

Problem No. 8 CHAP. 3


10 LET 1
20 ?!<INT
i=
ll \/)
SEC. 3-1
OUT OF DATA IN LINE
JO IF l=25 THEN 50
JS LET !=I•!
Problem No. 2
Problem No. 14 40 (illTIJ 20 10 LET C=O
50 END 20 L(T P•O
10 l'IEAO NJ,Ql,N2,Q2 RUN JO FCi< N:\001 TO 2213 STEP 11
20 LET tl=Nl•02+o'l2•DI No.a 40 LET C•C• 1
JO LET Q;Ol•!J~ 50 LET T=T+N
40 i>nINT "C l/2l•CS/6) I 60 NEXT N
50 DATA J.'.?,S,.6 .; 70 PRINT "TOTAL lS"JT
61) ENO
.,,
.JJJJJJ 80
90
PRINT .,THE NU118Eli OF" NUMBERS IS"lf
END
.2
• I 66667
R"'
N0·2
( l/2)+{5/6) .)"42657
.12s TOTAL IS 178~66,
-111111 TH£. NUMBF:tl: OF Nl.l'1BERS l 5

"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

"°~ SEC. 3·1 Cont'd


Problem Ne,. 6
10 LET S•O
N8•8

....
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

Nlt4SERS tN LIST F 8R LlST S


17 26
., 17
83

03
••
91

19
03

., 91
""'
N0•6 25 LE.T l"l•I
JJ PUNT II
DONE

SUl1 or RECIPrii'.:CALS f'rlO'i 1 TJ 1000 ts 7.45547 40 Nl'.:XT C Problem No. 1 O


SO l'.tlNT
DONE 60 NEXT R 10 P'OR 1•1 TB 10
70 END 20 LET LCIJ,.2•I·l
Problem No. 8 "'"'
NOolO
30
•O
50
NEXT I
F'OR J•I Te 10
LET S•O
1 B 9 10
S LS::T P•IOO I 2
". ' 3
'" 6

"
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

Al10'JNT Af"T£'1: ONE YEAR 1 S 104006

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

t; 100 DATA s1.32.1s3.-999.816;321.~2.18.999·3


110 END
200 LET TCAJ.SCXJ
210 NEXT )(
BO PRINT "AVG HIGH •"H/'J
90 LET R•R( I l
Problem No. 8
~
SEC. 3-4 Cont'd ""'
NC.6
10 READ r.s
20 IF' F'•O THEN 999

"" Problem No. 6 Cont'c


100
110
L£T O•l
F"OR X•2 TO 7
2J7
79



J
9
711

"
"
711
?I I
711
25
30
AO
50
PRINT F'JS."GCF'•"J
F'0R X•F' TO I STEP - I
IF' S/X•INTCS/X) THEN 60
CHH0 10
120 ff R >• RCXl THEN lSl 60 IF' F'/X•INTCF'/Xl THEN
I JO LET fl•f(( Xl 10 NEXT X:
I 40 LET O"X 60 PRINT l
150
160
NEXT X
PRINT "HIGHEST RANGE :o"R
.,' •-991
• 991 "-991 90
110
GOTO 10
PRINT X
170 PRINT "'ON oA·t NUMBER "O 120 GOTO 10
11s DATA s1.11,Aa.61.so. n.ss.1a.ss.16.ss.1s.~9,.79 1'1 • 3 "453
I • 453 • 453 200 OATA I YO. 1063.271 351211 J61 16,. 34> 121201 Q,. 0
180 ENO 999 ENO
Rl.W R ....
-327 • 2 •-654
Ni'.h6 N0.6
327 •·2 .. -654
-216 • 3 •-65'4
AVG LOW a SI 08571 190 1063 GCF'c 19
216 •-3 •-654
AVG HIGH a 1'4,7143 GCI'• I

·-·
-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

Problem No. 8 •-1009


• 1009
"-1009
•-1009 Problem No. 10
10 LET X110
9001 10 LET F"rl}•I
20 LET T•O LET F't2l•I
20
JO LET X•X•l JO f'OR Jr.J rn 10
<10 Rf.:AO 1.CXJ 40 l.ET f'( IJ•F'Cl-ll•F'ct-21
50 H" LCXl•O THEN 90 OONE
50 NEXT ':
60 PRINT LOOI F'OR InJ TO 9
60
70 LET T•T•L{Xl
BO GOTO JO Problem No. 8 70 F'OR J11I+l TO 10
60 l.E'i F'nF'Cil
90 LET A:of/(J::-1) 90 l.ET SHF'{J]
PRINT 10 F'OR X•'iOOi TO 1500 STEP 2
95
20 F'OR 1=3 TO SQR<X> STEP 2 100 Gesuo 1000
100 PRINT "AVERAG£ •"A 110 NEXTJ
JO IF' X/I»lNT<X/U THEN 60
110 LET S•O NEXT l
40 NEXT I 120
120 ,.OR B•I Ta X-2 F'0R Z•I TO C
~O PRINT XI 130
130 IF' L{Bl o LCB•IJ Tl EN ISO
60 NEXT X IAO PRINT UZH
140 LET Sl•L[Sl ISO NEXT Z
70 ENO
ISO LET LC8JaLC8•1l
Rt>< 160 GOT0 I 120
160 LET LCB•ll•SI I 000 ,.OR X•F TO I STEP - I
NO.g
170 LET S= I
1010 IF SIX•JHTCS/XJ THEN 1030
I BO NEXT 0 1039
1009 10JJ 1019 1021 1031 1033 1020 GOTO IOAO
190 II' 5 7 1THEN110
IDJO 1' l"IX•WTCF'IX> THEN 1060
200 F'OR C• I TO x- I 1093
1061 1063 1069 1067 1091 1040 NEXT X
210 II' A•LtCl THEN 240
1050 GOTO 1110
220 PRINT C- i"SCORES WEl!E BELOW AVE:RAGE" 1129 1151 1153 1163 reR Y•i TO c
1103 1109 1117 1123 1060
230 Gore 260
1010 IT :t•l.tYl TH£N 1110
240 NEXT C 1217 1223
1111 llSI 1167 1193 1201 1213 1060 NEXT Y
250 PRINT "TROUBLE"
1090 Lt:T C•C• I
260 F'OR O"X-1 TO ! STEP -I
1229 1231 1237 1249 1259 1217 1219 1263 1100 I.ET 1...cc1 .. x
210 lf' Acl..,[DJ THEN 300
1110 Rt:TURN
260 PRINT X-C"TEST SCORl:S WERE ABOVE AVERAGE" 1307 1319 1321 ENO
1269 1291 1297 !JOI 1303 1120
290 GOTO 320
RUN.
300 NEXT 0 1409 1423
1321 1361 1367 1373 1361 1399 NO.to
310 PRINT "TROUBLE"
J20 PRINT LC INTC<X-1 )/2 ··5>J"IS THE MEOlAN SCORE" 1<141 1451 1<153 l.459
1<139
325
J26
OATA 65,71,62>63190 56166,67,6S
OATA 0
1<127 1<129 1<133
OONE '
1<171 1<181 1483 1467 1469 1493 1<199
J30 ENO
DONE
it UN
NO.a

65 71 52 56
SEC. 4-2 SEC. 4-3
AVERAGE ::: 70
6
90

SCORES WERE BELOW WERAGE


" 65
Problem No. 2
Problem No. 2
J TEST SCOH ES \IERE A lOVE AVERAGE 10 READ N10
IS THE MEDIAN SCIJR~ 11 IF' N•O THEN 150 10 l.tT P 11 5
12 PRINT N"l"'D"""J 20 roR y .. 1 TO es
DONE 13 IF' N•O THEN 11 JO F'OR 11111 TO 12
IA L.ET X•O AO LET JnPo.045/12
15 Lt:T Y•N 50 LET PnP+I
CHAP. 4 16 GIJTO 20 60 LE:T PnP•S
11 LET Y•O 70 NEXT 11
80 NEXT Y
SEC. 4-1 16 LET X•N
90 PRINT P
20 F'OR P1rX TC 2 STEP -1
IF' X/PcJNTOVPl THEN 70 100 om
Problem No. 2 JO
10 PRINT
IS READ N 40 NEXT P
SO RUN
17 IF' N•O THEN 110 PRINT N"/"O
60 GOTO 10 N8•2
20 F'DR 0•2 TO SORHI>
30 IT N/D•INTCN/0) THEN 70 70 IT Y/P,.fNHY/P) THEN 90
eo GOTO -10 e780o36
"40 NEXT 0
90 PRINT N/P"l'"O/P
SO PRINT l"•"N"•"N DONE
100 GOTO 10
60 GOTO 10
70 PRINT N/O"•"D"•"N 110 DATA s.6.a2.'48.3•"4•36.4S
120 DATA o.O
7S IT O•N THEN 10
ISO ENO Problem No. 4
ao GOTO 40
RUN
es DATA 19"46.1949.1001·0 10 L£T P•O

, . ..,
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

90 PRINT A"O"S"• "J


_.,
..,
100 GB SUB 700 f'JRST LIST

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"

... 28 59 BO 12 JOB 109 116 122 155 161 169 RUN


70 GOTO 10 N0•20
100 LET O•O+'T
110 GOTO 60 TJHE NOW AOO TlHE TIHE LATER
DONE • 26
soo DATA 3.6 • .0.0.0 • .0.s.2.e.s •• 01.o '
12
"
.... .. .. ..
' 30 ' 56
999 ENO 9 ' 36 12 ' 3 9 : 39
12

. ...
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

- 5 • .0 HOD ' •9 33 ' 29 II ' 18


60 l.ET f't;tlaf'[X-ll+f'CX-2
70 NEXT X DONE
DONE
60 f'OR X•l TO 17
90 ''R Y•X•I re 18
100 ,IR Z•P'tXl Te GtAJ+I STEP •I Problem No. 14
110 I' 'CXllZ•INTtr[XJ/Z> THtN 1.0 CHAP. 5
120 NtXT Z 10 OU ,.NR<X)•JNT<X•RND<2>•1 >
130 GITI 210
20 'llR X*'l Tl 10
140 I ' P'tYll'Z o INTfr(l'l/Z> THEN 120
30 LET J•,.NRC999> SEC. 5·1
150 ,.IR V•t TG A '40 LET Il•'NRCl500>
160 I, Z•GCVJ THLN 210
'45
50
PRINT t1It.-'"GC1 •"1
,.GR T•I re 2 STEP •I
Problem No. 2
170 NtXT V
180 60 11' Jl'T•INTCJJ'T> THEN 200 10 F8R l•l TO 5
LET A•A• I
190 LET GCltJ•Z 10 NEXT T 20 LrT Ctl J •O
210 80 PRINT 1 30 NEXT I
NEXT Y
220 85 GGTI •oo 35 LET P•O
NEXT X
930 'IA 8•1 TB A 200 11' Jll'T•INT<lll'T> TH£H 300 •O FOR R•l TO 5
2'40 205 GBTS 70 50 READ C
PRJNT GCBJJ
300 PRINT T 60 11' C•'" I THEN 100
250 NEXT B
260 [NO '400 N[XT X 61 LET AtRJ•C
99' CHO 70 LtT CtRJ.,CCRJ+C
RUN
NllJ••

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

300 'OR Z•l Te 21 00 PRINT ?el I DIN[


310 PRINT SCZH as one •oo
320 Ntl'l:T Z 200 I ' 11/T•INTU l/f) THtN 300
500 DATA 3•••5•6•l•2•1•8•6•!•0•9•B•6•5•'413 •6•l•2 205 GOTO 10
501 DATA 1.2.,.... o.9.s.o.•.9•2•0•1•s .... 6.e.6.5.9 300 PRINT l•J ll'T Problem No. 4
600 END '400 NtXT X
RUN 999 t.HO 10 1'DR l•I TC S
Nt.6 RUN 20 LET Ct I J•O
N0.16 30 N[XT I
JS LET P•O
99 39 LCM • 1287
0 I
• LCM • 2
<10 FOR R•I TC 5
SO RLAO C

"" ""
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

~ 300 NEXT A 210 i-RtNT TIYJI


TAKING PHYSICS BUT NIT CALCll.US :
350 1'1R K•I Tl ao 22! NEXT 'f
~ 2.tO PRINT DINE
115 PRINT "2 THIES ENTRIES OF' FIRST ARRAY"
RUN
t-o SEC. 5-1 Cont'd 120 GOSUB IOOO
N0•2
.;:.. 130 ST0P
QUEST HALE HALE f'EHALEF'EHALE

°' Problem No. 6


POR J1q TO 5
1000
1010
1020
F'OR A"I TO 4
F'OR B•<I TO 1
PRINT J{A,BlJ
NUMBER 21+
I
2
2
2
UNDER
I
21+
3
I
UNDER HALE
l
I
3
3
F'EHA1..E ~DER
2
2
21 +
5
3
TOTAL
1
5
20 LET C{Il"O 1030 NEXT B 2 2 3 3 7
2
JO NEXT I 10.110 PRINT 2 j 2 2 5
JJ REM THIS LOOP SIMULATE! 500 PEOPLE 1050 NEXT A 5
2 2 I 2
JS F'OR X"I TO 500 1060 RETURN I
40 f'OR R"I TO 5 9999 ENO 2
42 REM THIS LOOP LCOKS A' F"IVE COURSES f'OR EACH PERSON RUN I
50 LET C"JNTC7•RNOC 1))

.
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

30 F"EJR R• I TO 3 • 2 • 3 MOD 5 10 30 50 70 BO 90 100 110 120


<40 f'OR C•t TO 1
SO LET EtR•Cl•f'NR( 100> DONE
22 SS 66
60 PRINT £CR.CH
70 NEXT C " 110 121 132

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

I 0012 BAS£ THREE


480 DATA 99862 ... 12179324• ,.9
t85 DATA 6412,.999999.,3
168 REM *
PRINT DAYS OF THE WEEX AT A SLANT
17 0 FOR I = I TO L
EQUALS 490 END
10101 JO RAS£ TWO
180 FOR J = ! TO 7
J<JN
190 PRINT TAB<4•J+I>J EXTSCWS(Jbl;l>J
NO. 4
2000! 12101 BASE THRE£ 200 NEXT J
£QUALS 210 PRINT
99862· BASE TEN =4995T BASE 12
1001101101001010 BAS£ TWO 220 NEXT I
79324• BASE TEN =130727 BASE 9
225
64 BASE TEN = l 000000 BASE 2 228 REM • DATA
999999, BASE TEN =1212210202000 BASE
CUT OF' DATA IN LINE 10 230 DATA SUllOAY,. MONDAY,. TUESDAY,. WEDNESDAY
240 DATA THURSDAY,. FRIDAY• SATURDAY
OUT OF" DATA IN LINE 10
250 END
RUN
SEC. 6-3
Problem No. 6 NO. 6
Problem No. 2
100 DIM DSC IOJ
110 DS:"O 123456789" T w T r
5 PRWT "TEST F'9R OlVISIBILITY BY THRE£" u 0 u E H R A
6 PRINT 120 GOTO I 40
130 PRINT "OUT OF' RANGE" N N E 0 u 1 T
I 0 PRINT "INPUT INTEGER" J u
20 INPUT N 140 PRINT 0 0 5 N R 0
150 PRINT "l"J A A 0 E 5 A R
25 LET T•O y
y y A 5 0 0
30 H' N•O THEN 999 160 INPUT N
y 0 A A
°'O F'DR E•S T0 0 STEP -I 170 IF ABS<Nl<.000001 THEN J30
SO LET I•INHN/IOtE> IF A85CN)>999999. THEN 130 A y y
180
60 LET T•T•I 190 PRINT "l"JNJ"I"
70 LET R•N-1•10'£ 200 PRINT "S"J
80 LET N•R 2IO IF N>O THEN 240
90 NE11:T E
220 PRINT "-"J
100 PRINT "SL-"! OF' DIGITS IS"T
230 LET N=ABSCN>
IOS IF' T/3•1NT<T/3) THEN 130
110 PRINT "NOT 01VJSIBL£ BY THREE" 240 FOR E=5 TO 0 STEP - 1 SEC. 7-5
250 IF N >= lOtE THEN 280
120 GDT0 6
130 PRINT "3 IS A F'ACTOR" 260 NEXT E
Problem No. 2
l-"O GOTO 6 "10 GOT0 350
999 ENO 280 F'OR EI =E TO 0 STEP -1 90 REM * F'ILE PRINT ONE TO A RECORD
RUH 290 LET I=INT<N/lOtEll 92 REM • WITH RESTART FEATURE
N0o2 300 PRINT DS(l+l .. I+tlJ 100 DIM NSC72l
310 LET R=N-1* IOtEl 110 FILES TEST
TEST '9R OIVISIBILJTY BY THREE 320 LET N=R 120 FOR I= I TO 1000
330 NEXT El 121 IF END 11 THEN 130
INPUT INT£GER7e3o4972 340 IF" R=O THE-"l 430 122 READ 11 .. IJNS
SUM OF' DIGITS IS 27 350 PRINT ","J 123 GOTO 150
3 IS A F'ACTOR 360 FDR El=-i TO -6 STEP -I 130 READ NS,A,B
370 LET IcINTCN/IOTEI+• 05l 132 IF' NS="STOP" THEN 230
INPUT INTEGER737 380 PRINT DS[l+l11+llJ 138 IF END II THEN 155
St.tt OF DIGITS IS 10 390 LET R:N-l*IOrEI 140 PRINT 1t .. IJNS .. A18
NOT DIVISIBLE BY THREE 400 LET N=R 145 PRINT NS
410 IF N<.000001 THEN 430 150 NEXT 1
INPUT INTEGER?O
420 NEXT El 152 STOP
430 PRINT "$" 155 PRINT "FILE FULL"
DBNE
440 ENO 170 DATA "WAGNER, WILHELM RICHARD"1 l813, 1883
RUN 180 DATA "VERRAZA."10• GIOV!lNNI",1480,1527
Problem No. 4 NO• 6 190 DATA "BRDNTE .. ANNE"• 18201 1849
210 DATA "CURIE• MARIE"• 1867; !934
30 PRINT 220 DATA "VER-IE.- JULES", 18281 1905
-"O PRINT "INPUT INTEGER"J l?-t.10023 225 DATA "STOP" .. o.. o
SO INPUT N 1-1.10023 230 END
S5 LET N2•N S-i.100235 R\.N
60 LET T•O N0.2
70 ff N•O THEN 350
80 LET Nl•O
90 LET £1•-l SEC. 7-3
WAGNER, WILHELM RICHARD
100 F'OR £•S TD 0 STEP -1
110 LET l•INTCN/10,E> Problem No. 2 VERRAZAt>Hh GIDVANNl
l'° 120 LET T•T•I BRONTE1 ANNE

~ 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

HEIGHT IS 131·652 FEET


100
110
DIM A:SC72l1BSC72J1AC250l
FILES TEST
** BEFORE ** ** AFTER **
J'JGATHA CHRISTIE CHRISTIE, AGATHA ODNE
120 REM 'II' COOOT NAME! AND SAVE KARRY TRUMAN TRUMAN• HARRY
130 REH POSITION IN A ARRAY srnw WHITE WHITE1 SNOW
140 If' END 11 THEN 190 MOTHER GOOSE GOOSE. MOTHER
150 FBR I'" i TO 250 OLIVER TWIST TWIST~ OLIVER
160 READ ll.1UAS SPADE, SAMUEL
SEC. 9-3
SAMUEL SPADE
170 LET AC IJ.,I LEMOOT CRANSTON CRANSTON. LEMONT
180 NEXT I DELORES SPIELER SPIELER, DELORES Problem No. 2
190 LET I .. I-1 EDGAR MARKS MARKS~ EDGAR
200 REM *ARRANGE PO! I TI 0N S IN DOROTHY WOODSON W(IOOSON.1 DOROTHY
10
20
LET Y.:rJ.14159/160
DEF' f"NTCGJ«SJN<G•IO
210 REM A ARRAY JO OEf f"NRCX>sINTCX•IOO+.S>,.100
220 nm R=I TO 1-1 35 READ X
230 READ ll.1ACRlJAS J6 ff X«• I THEN 999
240 F'OR J .. R+ I TO I CHAP. 8 37 IF' X:oO THEN 62
250 READ fl.1ACJJJBS AO READ Al.Bl.A
260 IF AS <::: BS THEN 310 Problem No. 2 SB REM F'INO THE THIRD ANGLE
27 O LET X=At RJ 60 l.ET CJ:rl60-(Al+BI>
280 LET ACRl=A(Jl 10 REAO A.s.c 61 GBTB 70
290 LET ACJJ:::X 15 I f A•O THEN 99 62 READ c1,e1.A
16 PRINT AIBJCJ 64 LET Al,.160-(Cl+BI)
300 LET AS=BS
17 LET D•Bt2•0A•C 68 REM 10 ANO 60 COMPUTE THE 0THER TW0 SIDES
310 NEXT J IS IF' 0,.0 THEN 72 70 LET OsA•f"NT<BIUf"NTCAI)
320 NEXT R
330 REM *NOW PRINT f,AMES
20
JO
LET Xl•C-B+SGRC8t2-<l•A•C»,.C2•Al
LET X2•<·B-SGR(8t2-4'•A•C)},.C2•A)
60
9B
LET C"B•F'NTCCll/f"NT<Bll
REH UDW PRHH THE RESULTS
340 PRINT ''NAMES IN llRDER" 410 PRINT "REAL ZEROS "XIJX2 100 PRitlT " "•" A".'' 8"1" C"
350 PRINT " 008 NAME" 45 GCT(I 10 110 PRltiT "THE ANGL£S ARE"•At.a1.c1
360 FOR N=l TO l SO DATA I• 3,2 120 PRlf/T "THE SIDES ARE"1F'NRCAhFNR(8)1fNRCC>
370 READ #l1AtNJJAS,,1 60 OATA j,J,J,3,-2,4 I 40 PRHIT
380 PRINT AJAS 70 DATA o,o,o ISO GOTO JS
390 NEXT N 72 PRINT Ht.M •I• Mt.AN~ AA!)
75 PRINT "NCN-REAJ.. ZERCS "-B/<2•A>JSQRC-0)/<2•A), .498 REM 'O' MEANS ASA
.....
400

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

Problem No. 8 DONE


90 DIM AS(72l Problem No. 4
toO FILES TEST
105 IF' ENO 11 THEN 999 10 READ A.8.C Problem No. 4
110 FOR I=I TO 1000 20 IF A" .OJ THEN 999
30 PRINT "X"•A"Xt2 +"B"X +"C 2 DEF' FNRCX»:JNTCX•IOO•oSl/100
115 READ 111,1 S LET l<=IBO/J.J41S9
AO FOR X•-12 TO 12 STEP 3
116 PRINT SO PRINT X•A•Xt2+B•X•C 7 PRINT •• "•" A"•" 8".'' C"
120 PRINT "RECORD" I 1 60 NEXT X 10 READ A10!1C
200 ff TYPC -1 )> ! THEN JOO 70 GOTO 10 IS IF" A=O THEN 1000
205 READ llJA BO DATA 2,-:J.4 20 LET TcCOS<Sl/Kl
220 PRINT Al 90 DATA oOl; I• I 30 l.ET B=SQl{CAt2+Ct2-2•A•C•Tl
230 GOTO 200 999 ENO AO l.ET Tl•CBr2+Ct2-At2l/£2•B•C}
300 IF TYP!-1)~2 THEN 400 RUN SO l.ET Al:K•ATNCSQRCl-Tlt2l/TI>
310 READ llJAS NO.A 60 LET Cl,.180-CAl+Sl>
6S PRINT "THE SIDES ARE".A.a.c
320 PRINT ASJ
x 2 Xt2 +-J 70 PRINT "THE ANGLES ARE''.F'NRCAl»F'NRCi3ibF'NRCCI>
330 GOTO 200 SO PRINT
400 IF' TYPf-ll=4 THEN 600 -12 328
,. 90 GOTO 10


-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>"•"'

Problem No. 8 "


170 NEXT G
180 ENO
25 PRINT -O/CCr2+0•2>">"
JO NEXT X
RUN 40 DATA l12..l•O•O•i•2•J•-3••. ll
S LET l<<1160/J.t4159 NGo2EF SO END
? PRINT " "•" A"•" B" "' C" RUN
10 READ A.-82,83184,C RBSlNCJG> RCOSCGh f N0•6
IF A•O THEN 1000 x y x y ANGLE

. ...'
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

THE SIDES ARE


THE ANGLES ARE
'
3.99999 Problem No. 2(gJ,(hJ
<l•<lS)f
Cl•45H 5
4 •(
•(
180
225
<1.•S>• 6 •C • 270
ANGLE DEG HIN SEC 10 LET K"J.\4159/160 •(
( l•45)f 7 • 315 '
Al " 36 52 12 20 PiHNT " ft=! •RCOSCGl"•" R=SlN(GJ+COS<G>" 6
Cl,45Jt
Bl • 53 7 48 25 PRINT " X"•" Y"•" X"•" Y"."ANGLE"
Cl • 90 0 O JO OEf' f'NXCR>=R•COSCG•Kl ODNE
TH£ SIDES AR£ 12.5186 .J::O uEF FNY(Rl,.R•SIN(G•IO
TH£ ANGLES ARE .:iO DEF FNROO::slNT<X•I00 ... 5)/100 Problem No. 4
ANGLE DEG HIN SEC 50 FOR a.. o TO J60 STEP I 5 10 LET K•J•l .. IS9/180
Al II 2.. 23 .... 90 IF ABS<J-COS<G•K»<·OOCI THEN 110 20 PRINT "CONVERT ,.ROH CA18) TO <R•G>"'
81 • 31 18 5 100 LET Gl=l/<l-COSCG•K))
105 PRINT f'NRCFNXCGl>>,FNRCf'NYCGI>>• 30 PRINT
Cl " 12.. 18 11
107 GOTO 120 .tO PRINT "A•B"J
110 PRINT " 50 INPUT A•B
X OH Y UNDEFINED''.
60 IF A•-·01 THEN 260
00NE 120 LET H=StNtG•K>•COS<G•IO
I JO PltlNT FNRCFNXCH)) ,FNRCFNYfH) > •G 70 PRINT "CR.Gl ,.8RM • C"J
eo ,,. A .... 0 THEN 110
170 NEXT G
90 IF Bco THtH I JO

SEC. 9·4 .....


!BO ENO

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

-049 -.13 1.18 -32 195

.....
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

Problem No. 2(c),(d)


100
110
120
PRINT <•8-SQRC0))/C2•A>
Gate 20
PRINT "NDN•REAL ROOTS"
.....
160

NOo6
ENO

S LtT K•Jol.tlS9/180 130 PRINT "C"·B/C2•A>"•"SOIHABSCD) )/(2•A>">"


10 PRINT " COS< .. •G>"•" SIN<2•G)"
F'JND INTEGRAL POWERS or <A.B>
140 PRINT "~"·8/C2•Al"•"·S01HABSCD> )/C2•Al">"
IS PRINT•• X"•'' 'I'"•" X.. , .. 'l'"•"ANGL£" 150 GOTO 20 WHAT POWERU
20 DEF" F'NRCX>•INT<X•I00+.5J/'IOO
30 F'9R G•O Te 360 STEP IS
60 LET C•COSC.t•G•K>
.....
160

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>

... r-.25 • I oTIJ91 20 DE' F'NR<X>•INT<X•l000•o5>11000


y x y ANGLE r-.25 .-1.11391 ..0 PRINT "TAKE RBl3TS DF' COMPLEX NUMBERS lN (A,B> F'ORM"
0 0 0 0 60 READ A•B•N

"'°
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

THE 2 2 TH R001 S OF' ! AHEI


I0 PRINT "POWERS OF C X+ I)••
'1.099 • ,455 20 DIM Ptl2J.F"Clll
( • j .099 .-.455 FOR W•I TO 12
JO
40 LET PCWJ•O
SEC. 11-3
TH£ 3 • J TH tlOClS OF > AREi
(-oS o!l66
50 NEXT W Problem No.
60 LET Ft!J•FC2J•j
<-.5 ,-.B66
70 LET SC I 1•5(2)•1 10 PMINT "SEARCH '°ilR REAL ZEROS 0,. A POLYNOMIAL"
60 LET A•Sor:2 20 PRINT "START, END• INCREMENT"J
90 PRlNT FC I JJ1''[2l 25 LET SJ•O
OUT OF DATA IN LlNE 6C 100 FOil T•I TO 10 JO INPUT ,.,L,5
I I0 FOR I• I TD A 40 I,. S•O THEN 27U
CHAP. 11 120 F'OR J•I TO 8 SO DEF' ;'NTCX>•12•X•J-64•X•2•17•X•l95
!JO LET P(J+J-lJ•PCl+J-11+.Cil•SCJJ LET A•O
SEC. 11-1 140 NEXT J
60
70 FOR X•F TO L STCP S
Problem No. 2 150 NEXT i 60 LET Sl•rNf(X)
160 F"OR X•! TO 11 90 LET S2•F'NTCX•S>
5 REH SUBTRACT TWO POLYNOMIALS 170 LET F'OO•P{XJ 100 IF' Si•S2•0 THEN !JO
10 FOR X"I TO 10 160 NEXT X 110 LET A•A•I
20 LET O(XJ:HXJ=SCXJ:>O 190 FOR Z•A+l TO l STEP -J 120 LET S!Al•X
JO NEXT x 200 PRINT PC Zll 125 If SJ•I THEN 190
40 READ N 210 NEXT Z IJO NEXT X
50 FOR X"'N TO 2 STEP • 1 220 F'OR H•I TO 12 I •O IF' A>O THEN 190
60 READ (l(Xl 2JO LET PCMJ•O !SO ?HINT "NO INTERVALS FOUND •••• TRY AGAIN"
70 PRINT O[X)"Xt"X-1''•"1 240 NEXT M 160 PMINT "WITH EITHER GREATE.t LIHITS OR SMALLER INCREMENT"
60 NEXT X 250 LET A•A+I I 60 GOTO 20
90 READ or 1 l 260 PRINT 190 PRINT "INTERVALCSl BEGIN ATI"
100 PRINT 0( 1 l 270 NEXT T 200 FOR J•l TC A
110 READ NI 260 END 210 PRINT SttJJ
120 FOR X=N l TO 2 STE! RUN 220 NEXT I
lJO READ HXl NOoB 225 Lt.T SJ•I
140 PRINT TCXJ"X•"X-1' +"1 230 PHINT
150 NEXT X 250 P.t!NT "NOW ''J
160 READ Tt 11 POWERS OF CX• 1 J 260 GOTO 20
PRINT Tl 1 l

.
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

280 DATA 4,.3,2,6,-6 NOW STAJo<T. ENO, INCKEHENTll• 1>0


290 DATA 5.6.-7.s.o.1
299 END DONE DONE
RUN
SEC. 11-2 Problem No. 4
N0•2
Xf J • 2 JI• 2 .. , Problem No. 2 10 DEF' FNT<X>•12•Xt.3•64•Xt2•11•X .. 195


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

Problem No. 4 SEC. 11-4


DONE
10
20
PRINT
READ N
Problem No. 2
Problem No. 6 25 ff N•-·01 THEN "420 IO DEF' F'NAC Xl•P[ 9 l•X.t 8 .. P[ 8 J•Xt7•PC 7 l •X• 6•P[ 6) •XI 5•P[ 51 •X• 4
JO F'OR S•N TO I STEP - I 20 DEF' F'N!HX>•PC4)•XtJ+P(Jl•X•2•PC2l•X.•P( I l
5 OlM P[20l .tO READ PC SJ JO DEF' F'NPCX.>•F'NAC)()•F'N0(Xl
10 OEF" F'NP<X>s:INTCXORNO<JO+l-X/2) 50 PRINT PC SJJ AO P>1lNT
IS DEF' F'NR<Xl•JNTCX•~NOClO+I) 60 NEXT S Al FOH X•I TO 9
20 l"O:f T=t TO 4 10 PRINT 50 LET PtXl•O
~5 f"Ort ~·I TC 10 80 LET Q•I 60 NEXT X
)•) LET F"[Wl•S{lol)•P('1)'*P(IH\0)•0 90 PRINT "INTEGRAL ZERD<S>1 70 H:EAO N
JS NEXT '.I 100 LET lhO 15 !IC N•.6101 THEN 500
"O Li:T A:F'N:~< I 0) 110 F'IlR X•-PC!l TO P[IJ ST£P SGN(P[IJ> t!O F'OH SsN TO I STEP ·I
.,.5 F'C.;: X•A TO I STEP •I 120 IF' X•O THEN 180 90 HEAD P[SJ
50 LET F'(XJ=F'NPP')) 130 IF' p[IJ/X <> INT<PCll/X) THEN 180 I 10 PtHNT PCSJJ
55 ;>~INT f'tXlJ
I "40 GOSua 310 120 NE.'H S
60 NEXT X 150 IF' P ... 0 TH£N J20 I JO PRINT "JNTEGHAL ZEHOI
65 ?HINT 160 PRINT XI \AO F'Olt X"'-P{\) TO Piil STEP SGN<Ptll>
'M LET ?l•F'Nrl:C5)
170 LET K•I 150 IF' X•O THEN 200
15 F'C:~ Y=tJ TC I STE~ -1
180 NEXT X 160 IF' P{ll/X .... INT<P{l]/Xl THEN :?00
IJV LET S(Y)#F'NIHl6> 190 H" 0• I THEN 260 170 IF' fNPtX> <> 0 THEN 200
85 i"tUNT S[Yll
200 PRINT I RO PRINT X.
90 NEXT Y
210 PRINT "ABS<P> .. 2" 190 GOTO 210
95 ?~INT
215 PRINT" X P" 200 NEXT X
100 F'CR l•l TO A 220 ,-OR X•I TO 0-1 210 LET ~=X
NJ 105 F'CR J• I TO B 230 PRINT KCX121J1<CX1ll ~20 ~1HNT "SYNTHETIC DIVISION BY X ~"JtO"YIELOS1"
Cit 110 LET ?tl•J-IJ.aPCI•J•ll•f'til•.S(J}
2"40 NEXT X 2J0 PHINT P[Nl I

""
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

10 DP1 A(J,121 J•O NEXT X 15 u

..
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$

ROAD TUNNEL BiHOGE. u a u


70 END 12 16 J6 0 M
2
JO
" 72

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

70 NEXT C DONE DONE:


60 NEXT A
as P:tINT
90 HAT t>l{JNT At SEC. 12-3 SEC. 12-4
95 DATA 2• 56. 78; 3,20, 45, 3, 9, 673• 564190• 234
IOD ENO Problem No. 2
RUN DIM A!4,4J.'l[ 4,) J.Ct4d l Problem No. 2
N0.4 2t} '1AT ifEAO A
JO OATt\ l1J,-~.-1,2.-J.1,-J,-7,s.-1,11,J.-1,1,-1 10 OJM ec2.2J,cc2.21.oc2.2i
40 MAT H!:AD B 20 MAT REAO B
50 DATA -1,2,-3,;i JO HAT C•lNll<B>
6U HAT C=A•!;J 410 PRINT "INVfBl"
70 MAT ""11NT C 50 HAT PRINT C
!JO !:NO 60 HAT D•B•C
RUN 70 PRINT *'B• INll< B>"
564 90 234 NCo2 75 HAT PRINT 0
60 MAT O•C•B
90 PRINT "'JNVCBl•B"
l 00 HAT PRINT 0
-2l 200 DATA -8.-J,o,-1
ll5 JOO ENO
R<.N
N0o2
-12
1692 702 INll<B>
DONE --125 ·J75
t.lON!'.:
Problem No. 4
Problem No. 6
JO UIM A(?..~J.~!?..?.l.Ct?..:n.Dc2.?) B•INllCB>
READ A,8
-!I)
DIM ;>;(?.,51
MAT HEAD lt
20
JO
MAT
DATA l• 12, \,4,-1?.,;?k,J,-7 '
:JO ;1AT PRINT Xl 4'l Mt\T C•A•3
40 f"Orl fl:>! TO .., SO MAT 0:1H•A
':>O f"O•< C"I TO '.:! 60 Pl{JNT "A•'l" JNllCBhB
60 IF cq•Cl/?.=INTCCrl•Cl/?.) THEN 90 70 MAT PitlNT Cl I
70 LET xot.t:l=-J•X('l,Cl ~O P>tJNT "9•A"
RO GCTO 110 91) MAT PrllNT DJ
9') LET lt(t(,CJ:·l•X[11,c1 100 PHINT "! COtlCL!JOE THAT HATHIX"
110 NEXT C 110 P-~INT "M'.JLTIPLICATICN !SNOT"
l~O NE;>;T •1 120 P~fINT "COMMUTATIVE" 09NE
I JO MAT PRINT ltJ I JO ENU
140
15')

"'"
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

30 NEXT X 50 ,-OR X"S TO l STEP •I


.-o PRINT N 60 LET N11ff/X
5D CHO 70 NEXT J:
SEC. 12-5 Rt>< 80 PRINT ll'N
NO•B 90 ENO
Problem No. 2 Rt><
6o30255E+12 N0.4\
10 0111 A!;l.JJ,8[;),J),C[J,Jl
20 MAT READ A J.8.t'769£-07
DONE
30 MAT B=TRNCA) DONE
40 PRINT "TRNCA>"
50 HAT PRINT Bl Problem No. 10
60 HAT C=A•B Problem No. 6
I0 PRINT 26t 3• I Ot J"DJf'F'ERENT PLATES"
70 PRINT "A+TRN(Al" 20 ENO 10 PRINT Cl/2)tl0
BO HAT PRINT CJ RLN 20 END
90 MAT C«A-8 ND• ID RU<
100 PRINT "A-TRNCA>" NC.6
120 HAT PRINT CJ
1•75'760E+0'7 011f'EREHT Pl.ATES 9o76562E-04
I JO MAT C=B-A
140 PRINT "TRN!Al-A" DONE DONE
\SO MAT PRINT CJ
JOO DATA 5,3,1,6,-2,9,3,9,1
999 ENO Problem No. 12 Problem No. 8
RUN
S LET T•O 10 LET P•C•B/S2>•C4'7/Sl>•C46/SO>•C·4/"49>
N0.2
10 f'OR F'•S TO 0 STEP -1 20 PRINT P
15 LET P•! 30 ENO
TRIHA)
5
20 nm X•S TO s-r+t STEP -I RUN
30 LET P•P•X N9•8
410 NEXT X
6o3B817E-02
50 L.E:T T•T+P
60 PRINT f'",.LAGS"P"SJGNALS" OCNE
70 NEXT F'
A+TRN<A)
75 PRINT "TOTAL NUMBER OF' SIGNAL.S I S"T
BO ENO
Problem No. 10
" ' Rt><
N0.12
10
20
PRINT CS/6)tJ•<tl6l
PRINT CS/6)t9•<116>
50 ENO
F'l.AGS 120 SIGNALS RUN
'"LAGS 120 SIGNALS No.10
F'l.AGS 60 SIGNALS
A-TRNCA> 9·6"11506E-02
F'LAGS 20 SIGNALS
0 -J -2 3·2301 IE-02
F'LAGS 5 SIGNALS
0 F'l.AGS I SIGNALS OONE
TOTAi. NlR18ER OF' SIGNALS IS 326

DONE Problem No. 12


TRNCA>-A
Problem No. 14 l0 PRINT 1- C36"41365) '28
0 J
20 ENO
10 LET G•l RUN
20 f'OR XcSO TO 50-25+ l N0.12
30 I.ET G•G•X
"40 NEXT X 7 ·39399£-02
60 F'0R X•25 TO I STEP - t
70 LET G•G/X DONE
00NE
SO NEXT X
90 PRINT G''GUEST 1.ISTS"
Problem No. 4 100 ENO
10 DIM A(J,JJ,B(J,J),C[J,JJ.O(J,JJ.E(J,Jl RU« SEC. 13-4
20 HAT REAO A.B NO.t"4
30 HAT C=A•B
Problem No. 4
410 HAT Q;TRNCC) 6o"4"11695E-26 GUEST 1.ISTS 10 HAT S•ZERCl14l
50 PRJNT "TRN<A•Sl" 20 PRINT " DEFECTIVE"
60 HAT PRINT OJ DONE 30 PRINT "'1.lGHT OPENE:R BOTH NEITHER"
70 HAT C=TRNCA) "110 F'GR X• I TO 1000
80 HAT O=TRtHBl
90 MAT E:O•C
Problem No. 16 50
60
LET L• INTC20•RNOCI))
LET C•INTC25•RNOCI>>
100 PRINT "TRNCBhTRNCAl" 10 LET N'"I 70 IF' 1.>0 THEN 120
110 MAT PRlNT EJ 20 F'OR X11!4 Tll I STEP -I 80 I.ET S(l.dl•Stl•ll+I
120 HAT E=C•O JO I.ET N•N•X 90 lF' C>O THEN 140
130 PRINT "TRNCA>•TRN<B>" 40 NE:XT X 100 I.ET S{l1?l•S{l12J+I
l 40 MAT PRINT El <45 LET N•N•2 105 IF' L>O THEN 1"110
300 OATA 2.-bJ.s.o.s.-3 • .i:i.2 50 PRINT N 110 LET Stl13l•S{l•Jl+I
JIO OATA e..J.a.9.s • .i:i.11.-2.0 60 ENO I 15 GOTO l"40
999 END Rt>< 120 IF' C•O THEN 90
RUN N0.t6 130 LET st1~"11l•S{1."4)+l
1"110 NEXT X
lo74357E•l 1 150 HAT PRINT SJ
TR.•HA•B> 160 ENO
36 116 DONE Rt><
Na • .-
-s
Problem No. 18 OEF'ECTIVE
l-.o 12 40 -e LIGHT OPENER BOTH NEITHER
10 I.ET C•I 42 35 J 926
~ TRN<Bl•TRNCA)
20
JO
F'CR X•JO TC 30•"11+1 STEP -I
LET c... c•X OBNE
Problem No. 10 9o52127E-02
k> SEC. 13-4 Cont'd 10 OJM S[l. Ill
C') 1S HAT S•ZER
DONE
k)
Problem No. 6 20 F'DR X• 1 TD 500
Problem No. 8
25 LET C•O
LET C•O JO F'OR R•~ TD JO 5 PRINT "ONES PRD0A01L1TY"
20 F'OR X• t TO 500 40 LET Rl•INT<6•RNOCl>+ll 10 l.ET N•6
30 LET R•INT<6•RN0(1)+1> SO JP' Rl"'i THEN 100 20 F'OR R•O TO 6
40 IF' R:1I THtN 100 60 LET C•C+I JO l.ET C:I
SO IF' R•S THEN 100 100 NEXT A SO FOR X"N TO N-R• I STEP - j
60 GOTO 200 110 I.ET S[l,C•ll•SCl•C•ll•1 60 LET C•C•X
100 LET C•C•I ,JO NEXT X 70 NEXT X
200 NEXT X 140 PRINT "NCNE CHE TWO THREE"J 90 FOR XsR TC I STEP - I
2t0 PRINT C"rlVE OR CNt UP" 1 SO PRINT " F"OUR F'JVE SIX SEVEN"J 1 00 LET C•Cl'X
220 END 160 PRINT " EIGHT NINE TEN" 110 NEXT X
RUN 110 MAT PRINT SJ 120 PRINT RJCa(S/6)r(N-RU(l/6ltR
N0.6 160 ENO 130 NE1'T R
162 F'IVE DR 0NE UP RUH ISO ENO
ND.JO RUN
DDNE No.a
N0NE ONE TWO THREE F'6UR nve: SIX SEVEN EIGHT NINE TEN
160 t SJ 69 32 6 O 0 O 0 O ONES PROBAB 1 l. J TY
Problem No. 8 0 .J34B98
I •401878
10 LET Nl•J
DONE 2 ·200939
20 OJH StSOOl s. 35837E-02
JO MAT S•ZER
fl·037SSE-OJ
-'O P'CR :it• I TO 500 6o4J004E-04
SO LET N•I
60 LE.T R•INTC6•RN0<1>•1> SEC. 13-5 2ol4JJ5E•OS
70 IF' A• I THEN 120
DONE
60 LET SCNJ•SCNl+l Problem No. 2
90 IF' N<o:NI THEN 5 PRINT "BNES PROBABll.ITY .. •"TOTAl."
100 LET Nl•N 1 l.ET T•O
110 GOTO 140 10 FOR R•IO TO 0 STEP -f
120 l.ET N•N• I 15 \.ET C•I
130 GOTO 60 20 FOR X•IO TO 10-R•l STEP -t
140 NEXT X 30 l.ET C•C•X/(10-X•l) Problem No. 10
150 PRINT "ROl.l.S TJI. 01'<£"•"NUHBER OF TIME 40 NEXT X
160 roR X•I TO NI 5 LET T•O
SO LET P•C•Cl/6>tR•t5/6)•CIO-R> 10 F0R R"6 T0 10
170 PRINT x.stX}
55 LET T•T•P 15 LET CE!
180 NEXT X 60 PRINT RJP.T 20 F0R X•IO TO IO•R•l STEP ~J
190 END 65 NEXT R JO LET C:oCo)(/'(10-X•I)
Rlf' 70 ENO 40 NEXT X
NO.a
RLW SO l.ET P•C•<ll2l•ROCl/2)f(IO~R>
N{lo2 60 LET T•T+P
ROLLS TIL (IN£ NUMSE:R OF TIMES
65 NEXT R
8• ONE:S PROBABILITY TOTA.l.. 68 PRWT T
62 10 l•653B2E-OB I ·65JB2E-oa 10 ENO
60 9 8o26906E-07 B•43447E-07 RUH
37 I •86054E:-OS l o94489E:-05 NllolO
35
39 2046072£-04 2o67521E-04
2· 17063£-03 2•43816£-03 o3769SJ

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

15 5 OCNE Problem No. 12


16 5 5 PRINT "Fl.JPS PROBABILITY"
11 1 Problem No. 4 10 l.ET P•I
18 20 FOR R"I TC 10
10 REM COMB 100 THINGS I AT A Tll'IE•JOO
"
20
21
20
JO
LET P•IOO•oOOltlOo999t99
PRINT P
30 LET P=P•<l/2)
40 PRl!lT RIP
SO NEXT R
22 40 ENO
60 ENO
2J RlJf
RUN
2• NOo 12
25
26 9005693£-02 FL.JPS PRCBABILITY
21 ' ,5
28 DONE 2 .25
29 .J25
JO .0625
0 03125
JI
J2 Problem No. 6 .015625

,.
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

De Moivre's theorem, 152 Greatest common factor, 51, 54, 91


Depressed polynomial, definition of, 169
Descartes' Rule of Signs, 173, 175 Identity matrix, 187
Dimension conversions, 50-51 IDN, 187
Dimensioning IF END statement, 111
arrays, 85-86 IF-THEN statement, 15, 18-19
lists, 66 IMAGE statement, 227
DIM statement, 66, 103 Initializing, 9-10
two-dimensional, 85, 86, 89 INPUT statement, 166
Division Integers
of polynomials, 156-158, 159 computing greatest, 47
synthetic, 158 factoring, 48-49
Dummy argument, definition of, 62 Integral zeroes of polynomials, 159-163
Dummy data; see Artificial data INT(X) function, 47, 48, 51
INV( ) statement, 191-192
E-format, 8-9
END statement, 2, 12 Law of Cosines, 139-142
Enumeration Law of Sines, 138-139, 141, 142
combinations, 200 LEN( ) function, 103
factorial, 198 LET statement, 5-6, 11, 56
partitioning, 202 as assignment statement, 10
permutations, 198-199, 200, 202 Lists; see Computer list
Principle of, 197-198 Location principle, 163
Equality, matrix, 187, 190 Logical end, definition of, 69
Equals sign, 10 Log-on; see Sign-on
in IF-THEN statement, 15 Loops,24-25,31-34
as relational operator, 15 F0R-NEXT, 26, 28
Error diagnosis, 218-224 machine-made, 26, 28-30
Euclidean algorithm, 112-113 nested, 32
greatest common factor, 91
Event, definition of, 205 Machine-made loops, 26, 28-30
Executive program, 4 MAT instructions, 176
Experiment in probability, definition of, MAT READ, 178-179
197,207 MAT PRINT, 177, 178, 179
Exponentiation, symbols used for, 4 Matrix, 179; see also Arrays
coefficient, 192
Factorial, 198 creating zero matrix (ZER), 187
Factoring integers, 48-49 filling locations with 1 (C0N), 187
Factor theorem, 160 forming identity matrix (IDN), 187
Failure in probability, definition of, 197 identity, 187
Fibonacci numbers, 3tl, 46 inverse of, 190-191
FILES statement, 110, 117 transpose of, 195
Flowcharting, 13-14, 20 Matrix addition, 186
F0R-NEXT statement, 26, 28, 31, 32 Matrix algebra, 185-188
Fractions, reducing, 50-51 Matrix equality, 187, 190
Functions Matrix inverse, 190, 191
circular, 132, 133 difference from transpose, 195
computer, see Computer functions Matnx multiplication, H>5-H>o
scalar, 187
G0SUB statement, 41, 45 Matrix subtraction, 186
G0T0 statement, 3, 12, 18 Maximum, 124
Index 267

Minimum, 124 multiplication of, 155


Modular arithmetic, 67 polynomial function, 160
addition, 70 quotient, 173
multiplication, 70 real zeros, 163, 165-167
Multiplication Remainder theorem, 159
matrix, 185-186 second-degree, 168
modular, 70 synthetic division, 158
of polynomials, 155 variation in sequence, 173
scalar, 184 Predefined process, 41
Multiplication symbol, use of, 4 Prime numbers, 48-49
Mutually exclusive events, 211, 212 Principle of Enumeration, 197-198
PRINT statement, 1, 2, 101
NAME command, 16 PRINT USING function, 61, 227
Nested loops, 32 Probability
Nomeal zeros, 169 binomial trials, 210-213
Numerical order, 40-41 "birthday problem," 205
definition of, 197, 204
event, definition of, 205
OPEN command, 110 experiment, definition of, 197
Operands, definition of, 4 failure, definition of, 197
Operators independence, 205
arithmetic, 4 outcome, definition of, 197
assignment, 10 success, definition of, 197
relational, 15, 19, 20 Program, definition of, 1
Ordering routine, 65 executive, 4
Outcome, definition of, 197 Program defined functions, 55-60
Pseudo random number generator, def-
Papertape,215 inition of, 62
Parabola Pythagorean theorem, 132
definition of, 123
plotting, 125-131
Quadratic formula, 122, 168
Parentheses, 7-8
Quadratic function
Partioning, 202
definition of, 122
Pascal's Triangle, 213
graph of, 123-124
Permutations, 198-199, 200, 202
quadratic formula, 122, 168
Polar axis, definition of, 144
zeros of, 122-123
Polar coordinate system, 151
Questionnaire, 77, 85
complex numbers, 150
Quotient polynomial, 157
polar axis, 144
storing of, 173
pole, 144
Pole, definition of, 144
Polynomials Radians, definition of, 132, 133
abbreviation of, 159 RAD(X) function, 133
complex zeros, 168-175 Random access, 115
definition of, 154 Random access files, 112, 118-120
depressed, 169 Random number generator, 62--71, 206-
Descartes' Rule of Signs, 173, 175 207
division of, 156-158, 159 dummy argument, 62
Factor theorem, 160 pseudo random number generator, 62
integral zeroes of, 159-163 RND(X), 62-63
Location Principle, 165 READ statement, 3-4
268 Basic BASIC

READ #N statement, 117 Statements


Real zeros, 163, 165-167 DATA, 3-4
Records, 118 DEF, 57, 58
Rectangular coordinate system, 132, DIM, 66, 85, 86
138, 143 END, 2, 11
complex numbers, 147, 149 F0R-NEXT, 26, 31, 32
Relational operators G0SUB, 41, 45
~quals sign as;, 15 G0T0, 3, 11, 18
greater than, 19 IF-THEN, 15, 18-19
"greater than or equal to," 20 INPUT, 166
"less than," 19 LET, 5-6, 10, 11, 56
"less than or equal," 20 READ, 3-4
"not equal to," 20 REM, 13, 16-17
REM statement, 13, 16-17 REST0RE, 108, 110
Remainder, 51, 91 RETURN, 41, 44-45
Remainder theorem, 159 ST0P, 59
Remote terminal, definition of, 1 ST0P statement, 59
Replacement operator; see Assignment Strings, 101-109
operator String array scheme, 106-108
REST0RE #N statement, 118, 119 String variable, 101, 103
RETURN statement, 41, 44-45 Subroutine, 41, 45
Right triangles, 133, 135 Subscripts, 35, 103
computing angles, 136 Substring scheme, 103
RND(X) function, 62-63 Subtraction, matrix, 186
Roots, complex numbers, 152 Success in probability, definition of,
Rounding off, 57 197
Routine, ordering, 65 Sum array, 186
Row vector, definition of, 179 Symmetry
RUN command, 4, 5 axis of, 123
definition of, 124
Scalar multiplication, 187 Syntax errors, 220
Scientific notation, 8-9 Synthetic division, definition of, 158
SCR command, 4-5 System commands, 4-5
SCRATCH #N statement, 117, 118 NAME, 16
Selections, 200 RUN,4, 5
Semicolon delimiter, 10-11 SCR, 4-5
Sequential access, 115 Sign-on, 4
SETW statement, 119
SGN(N) function, 47 T AB(X) function, 225
Sign-on, 4 Tabulating
Simulation, random one item, 73-75
random number generator, 206-207 several items, 75
Simultaneous linear equations yes-no answers, 76-77
printing results, 194-195 TAN(X) function, 133
solving, 192-193 Tape, paper, 215
Sines, Law of, 138-139, 141, 142 Tape punch, 215
nTlt. T /'\T\ ( • • '1 f"\f"\
u11~\A..J 1u11cuu11, 1-•JV Tape reaU.er, 21G
Spacing, 58-59 Transpose, of a matrix, 195
comma, 10, 11 Transfer
semicolon, 10, 11, 96 conditional, 18
SQR(Y) function, 47 unconditional, 18
Index 269

Triangle THN( ), 195-196


area of, 138 Two-dimensional list; see Arrays
Law of Cosines, 140-142
Law of Sines, 138-139 Unconditional transfer, 18
right triangles, 1.33, 135, 136
Trigonometric ratios, 133
Trigonometry Variation in number sequence, defini-
area of triangle, 138 tion of, 173
circular functions, 1.32 Vectors
complement, 135 column, 179
congruence conditions, 139 row, 179
Law of Cosines, 139-142
Law of Sines, 138-139, 141, 142 WHITE #N statement, 117
polar coordinate system, 144, 150,
151 ZEH, 187
Pythagorean theorem, 132 Zero subscripts, 155
radians, 132 Zeros of polynomials
rectangular coordinate system, 132, complex zeros, 168-175
138,143 of function, 159
trigonometric computer functions, integral zeros, 159-163
133, 136 nonreal zeros, 169
trigonometric ratios, 133 real zeros, 16.3, 165-167
BASIC BASIC: An Introduction to Computer Programming in
BASIC Language, Second Edition
James S. Coan

" . . . an excellent introduction to the use of BASIC through remote


terminals and time sharing . . . clearly written and well-organized,"
said Computing Reviews about the first edition of this popular
introductory book. Anyone can learn to write computer programs
in BASIC using this book. Not only does it describe the essential
statements of BASIC and use them in sample programs, but this
new edition now includes a chapter on strings and files.
The author uses over 100 sample programs to illustrate the
essential techniques of the language and to Integrate BASIC pro-
gramming with mathematics. Each language statement or capabil-
ity is clearly explained at the time that it is first used in a sample
program. Every section is followed by practice problems. Solutions
to even-numbered problems appear in the text, the remainder in
the separate Teacher's Guide.

Other Books ot Interest .••


ADVANCED BASIC: Applications and Problems
James S . Co an

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.

THE BASIC WORKBOOK: Creative Techniques for Beginning Programmers


Kenneth E. Schoman, Jr.

Here is a hands-on approach to learning BASIC and the fundamen-


tals of problem-solving using a computer. Through many exercises,
readers develop a workable BASIC vocabulary, a feeling for the
logic and intrigue of programming algorithms, and the self-confid-
ence needed to use a computer in a variety of applications. #5104-2,
paper. 128 pages.

GAME PLAYING WITH BASIC


Donald D. Spencer

Enjoy the challenge of competition by playing such computer games


and puzzles as 3-D Tic-Tac-Toe, Nim, Roulette, Black Jack, Magic
Squares, the 15 Puzzle, Go-Moko, Keno, Morra, Baccarat, and many
others. The author writes in a nontechnical style and includes the
rules of each game, how each game works, illustrative flowcharts
and diagrams, and the output produced by each program. #5109-3,
paper, 176 pages.

III
HAYDEN BOOK COMPANY, INC.
Rochelle Park, New Jersey
ISBN 0-a104-6106-9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy