Atari Basic
Atari Basic
Atari Basic
ATARI BASIC
BOB ALBRECHT
LE ROY FINKEL
and
JERALD R. BROWN
Dymax Corporation
Menlo Park, California
Albrecht, Robert L
Atari BASIC.
v
How to Use this Book
vii
Contents
To the Reader
How to Use this Book
Chapter 1 Your ATARI Personal Computer 1
Chapter 2 Getting Started 11
Chapter 3 Assignment Statements, Stored Programs, and Branching 41
Chapter 4 Decisions Using IF-THEN Statements 81
Chapter 5 READ and DATA Work Together 120
Chapter 6 FOR-NEXT Loops 153
Chapter 7 Subscripted Variables 180
Chapter 8 Double Subscripts 220
Chapter 9 String Variables and String Functions 260
Chapter 10 Color Graphics and Sound 283
Final Self-Test 315
Appendixes 325
BASIC Functions 325
ASCII Character Codes 328
Error Messages 329
Index 331
ix
ATARI BASIC
CHAPTER ONE
Your ATARI
Personal Computer
This chapter will introduce you to the ATARI 400 and the ATARI 800 Personal
Computers for home, school, club, or business use. When you complete this
chapter, you will have a thorough introduction to these two computers and the
expansion components which are currently available for them. You will also be
able to use the following words and phrases from the language of the computer
world.
Computer language
BASIC and, specifically ATARI BASIC
CPU or Central Processing Unit
Keyboard
Screen or television screen
Printer
Program
Program recorder
ROM cartridge containing ATARI BASIC
Operating System program
ROM module
RAM memory
RAM memory module
BASIC statements
Line numbers
1
1 ATARI BASIC
You may be using this book with either the AT ARI 400 or the ATARI 800
personal computer system. The 400 consists of the 400 computer console with a
flat-panel keyboard, your television set, and, possibly, the optional ATARI 410
program recorder. The 800 has a typewriter-like keyboard and comes with the
program recorder. There are other differences between the two systems which we
will note as we go along.
~. • .. ;;: "l<' _ -
~- Q .. IL • ( .. II 1 C) to "Ie ~ ,...-
_ .. , n f " .. ; .. ~ .... "l~ :>
Before you go any farther, it would be a good idea to set up your computer
so that you can use it as you read this book. Packed in the carton with your
ATARI 400 or 800 you will find the Operators Manual. This pamphlet will tell
you how to attach the television, turn on the power to your computer, and use
the program cartridges which come with it. The Operators Manual contains a lot
of information in a small space so don't feel you must read every word of it right
away. But do get your computer "up and running in BASIC" if you haven't
done so before, and then come back to this book.
Welcome back! Now let's get better acquainted with your ATARI computer.
Of course, the most obvious feature of the computer is its keyboard:
11IIIIIIIII111118B8111111111
_11111111811118111111111-
iii II II II a 11111111111111& _
_ aDIIIIIIIIIIIIIIIIII.
YOUR ATARI PERSONAL COMPUTER 3
BASIC
3. We mentioned the "SK Operating System" that helps the computer inter-
pret our instructions to it. The "SK" is a way of measuring the size of computer
memory. The K stands for thousands of bytes of memory. Byte is a computer
term that refers to units of memory. Each memory byte can store approximately
one letter, number, or other character, so 8K can hold around .S thousand
characters.
Therefore, 16K would indicate the capacity to store about _ _ _ _ _ __
characters.
16 thousand
4 ATARI BASIC
4. Both the 400 and the 800 have a second memory section which contains 8K
of RAM or Random Access Memory . This is where the computer stores informa-
tion that you enter through the keyboard. We often refer to RAM memory as
our workspace. We can write in it or erase it with just a few keystrokes.
RAM is different from ROM, the Read Only Memory. The computer uses
the ROM, but ROM is permanent in that it cannot be erased or changed by you
from the keyboard.
Answer the following questions by writing RAM and/ or ROM.
(a) Which can be erased? _ _ _ _ _ _ _ _ _ __
(b) Which is permanent? _ _ _ _ _ _ _ _ _ __
(c) Which has a size indicated by such notations as 8K?
(d) Which is used to store information entered from the keyboard?
(a) RAM
(b) ROM
(c) RAM and ROM
(d) RAM
5. If you have an ATARI 800 you can purchase extra RAM memory modules
to expand the workspace in your computer. You won't need extra memory while
you are using this book, but in the future you may find you want to write longer
programs which require more RAM for storage.
Make sure you have the AT ARI BASIC cartridge in the cartridge slot of
your computer (follow the instructions in your Operators Manual). Inserting a
cartridge does for the computer what adding an extra piece of already-educated
gray matter would do for your brain. It makes the memory bigger and gives it
more information. This cartridge contains another preprogrammed 8K ROM; the
computer instantly knows how to speak AT ARI BASIC when this cartridge is in
place. Now you need to learn ATARI BASIC to communicate instructions to the
computer. A set of one or more instructions to the computer that tells it what to
do is called a program. Your program, or instructions, must be entered or typed
in to the computer in BASIC, following the rules for BASIC you will learn in
this book. Your instructions to make the computer do what you want it to do;
and following the rules of BASIC, is called a _ _ _ _ _ _ _ _ __
program
YOUR ATARI PERSONAL COMPUTER 5
6. Let's pretend ypu are typing a program into the computer. As soon as the
computer receives a character from the keyboard (as you begin typing) it dis'plays
that same character on the television screen, thus providing us with a record of
what we have, typed.
Therefore, the television screen is an output device, for it takes what you
put in, and puts it back out again. This allows you to make sure that you did, in-
deed, enter what you thought you did. Later, when the computer follows the in-
structions in your programs, the results are also displayed on the screen. This is
another example of the TV as an ___________ device.
output
7. ' Look at the photo of the ATARI 400 computer connected to a television on
page 2.
(a) We use the keyboard to type information into the computer. What happens
to the information that we type? _________________
(b) How does the computer communicate with us, the users?
8. If you have an ATARI 800, you also have a program recorder to use with
your computer. It is similar to a home cassette recorder. (You may buy the
recorder to go with your 400 system if you like.)
The program recorder is a memory storage device which is separate from
the computer's own electronic memory. It can be used to store or save your com-
puter programs and information (called data) such as mailing lists, recipes, ap-
pointments, and budget or accounting figures. Once you have mastered the
BASIC computer language, you will find this kind of st()rage device quite handy
and easy to use. In addition to cassette tapes, there are other ways to store 'in-
formation and programs separate from the computer. All 'such external storage
methods are convenient ways to feed or "load" programs and information you
often use into the computer without taking all the time needed to type them in
from a keyboard. Magnetic disks are another external storage device. ,However,
in this book you need only the three main parts of a computer system we have
discussed earlier, which are______________________
9. Think back over frames 1 through 8. We have described the ATARI com-
puter. For our purposes, the important ingredients are these:
(1) The computer itself-the central processing unit with its supporting
electronic circuitry hidden away inside the console.
(2) An input device-the keyboard, which we use to type information into
our computer.
(3) An output device-the TV screen, which displays information typed
on the keyboard and/or sent by the computer.
(4) BASIC installed in the computer, ready to go.
In this book, we will show you many programs in AT ARI BASIC, and will help
you learn to read, understand, and use these programs for your own enjoyment.
We will concentrate on applications that we think will be of interest to users of
home/school/personal computers. And so . .. for our appetizer, a computer
game.
YOUR ATARI PERSON.<\L COMPUTER 7
100 REMARK *** THIS IS A SIMPLE COMPUTER GAME These lines, down to
110 LET X = INT(100*RND(1»+1 the word RUN, are a
120 PRINT
130 PRINT "I'M THINKING OF A NUMBER FROM 1 TO 100." computer program.
140 PRINT "GUESS MY NUMBER!!!"
150 PRINT I PRINT "YOUR GUESS"; I INPUT G
160 IF G<X THEN PRINT "TRY A BIGGER NUMBER." I GOTO 150
170 IF G>X THEN PRINT "TRY A SMALLER NUMBER." I GOTO 150
180 IF G=X THEN PRINT "THAT'S IT!!! YOU GUESSED MY NUMBER." I GOTO 110
RUN
Look again at the program shown in our computer game. The program
consists of nine (9) lines, each containing one or more BASIC s(atements. Each
line begins with a line number.
,
13.0 PRINT "I'M THINKING OF A NUMBER FROM 1 TO 1.0.0."
This is a statement.
8 ATARI BASIC
10. The program in frame 9 was typed, one line at a time, on the keyboard. As
we typed it, the program was stored in the computer's memory and also
displayed on the _ _ _ _ _ _ _ _ _ __
11. First, we typed in the entire program (lines 100 through 180). At the end of
each line we pressed the RETURN key. This process is called "entering the pro-
gram." This stored the program in the computer's memory. Then we typed
RUN. This tells the computer to RUN, or carry out, the program. Computer
people also say "to execute the program." In other words, after storing the pro-
gram, we then told the computer to follow the instructions (statements) of the
program, or execute the program.
If there is a program in the computer's memory, then typing RUN tells the
computer to
12. During the RUN, the computer obeyed the instructions (statements) in the
program, as follows: First, the computer generated a random number from 1 to
100, inclusive (line 110). This number is an integer-a "whole" number with no
fractional part.
Next, the computer typed instructions to the player (lines 120, l30, 140, and
150).
After the player typed a guess, and pressed the RETURN key, the computer
compared the guess with its secret number and gave the player the appropriate
response (lines 160, 170, and 180).
If the player did not guess the computer's number, the computer went back to
line 150 and asked for another guess. But, if the lucky player did guess the secret
number, the computer acknowledged the correct guess and went back to line 110
to "think" of another number.
RUN
10 ATARI BASIC
Now, to see how much you've learned from this first chapter, try the Self-
Test. Then on to Chapter 2 where you'll start to learn how to actually use
the computer.
SELF-TEST
Try this Self-Test, so you can evaluate how much you have learned so far.
1. What does your computer use for an output device?
2. What do the following abbreviations stand for?
(a) CPU _ _ _ _ _ _ __
(b) RAM _ _ _ _ _ _ __
(c) ROM _ _ _ _ _ _ _ __
3. Since a computer doesn't have legs, what do we want a computer to do
when we tell it to RUN? _ _ _ _ _ _ _ _ _ __
. Answers to Self-Test
The frame numbers in parentheses refer to the frames in the chapter where the
topic is discussed. You may wish to refer to these for quick review.
1. A screen or television set (frame 6)
2. (a) Central Processing Unit or computer
(b) Random Access Memory or user workspace
(c) Read Only Memory (frames 2, 4)
3. Execute the program (follow the instructions we give it) (frames 11, 12)
CHAPTER TWO
Getting Started
1. Now we begin "talking" with the computer. We assume that your ATARI
computer system is set up and ready to go, with the BASIC language cartridge
inserted in the language cartridge slot. You can find directions on how to do this
in the Operators Manual for your computer. If you own an ATARI 400, you will
consult the very friendly and helpful ATARI 400™ OPERATORS MANUAL. If
you are using an AT ARI 800, you will avidly peruse the ATARI 800™
OPERATORS MANUAL. As you work through this book, you will find your
ATARI OPERATORS MANUAL a constant source of know-how and inspira-
tion!
11
12 ATARI )JASIC
Ready? Turn on the computer. >I< CLICK! The TV screen will look like this:
Your ATARI computer is ready for you to use. You know that it is ready
because it typed READY and turned on the tiny rectangle called the
cursor
• • • • B • • • • BB • • • • ~
l1li II 1111 II 8 a .. D a ••• _
.... aa II liD liD a 111 liI •
• • • aa •••••••
In the diagram, there is a large black arrow pointing to the key labeled
RETURN
*Remember, we assume that you know how to hook up the various components of your
ATARI system and turn it on. If not, consult your friendly ATARI OPERATORS
MANUAL. '
GETTING STARTED 13
3. Your computer is waiting patiently for you to do something. So, try this:
Type your name and press the RETURN key. The computer will probably print
an error message on the screen. Here is what happened when Karl typed his
name.
+
As he typed, the cursor moved to
the right.
Hmmm ... Why did that happen? Well, you see, the computer is of quite
limited intelligence. It simply did not understand the word, KARL.
(a) If you type something, then press RETURN. and the computer types
ERROR, what is the computer trying to tell you? _ _ _ _ _ _ _ _ __
(b) After typing ERROR- KARLO, what did the computer do? _ __
The cursor is the computer's way to let you know that everything is OK. No
damage has been done. The computer is very patient and forgiving. After you
make a mistake, it will display the error and then turn on the cursor to let you
know it is ready for you to try again.
*By it, we mean the computer. Saves space in writing this book!
14 ATARI BASIC
4. . I I I I
FInd the SHIFT key and the CL~AR key. Press the SHIFT key and,
while holding it down, press the CLEAR key. The screen will now look like this:
Except for the cursor, the screen is blank! Pressing SHIFT and CLEAR together
erases the screen, leaving only the cursor in its home position.
(a) Where is the home position of the cursor? _ _ _ _ _ _ _ _ _ _ _ __
Another way to clear the screen and home the cursor is to press CTRL and
CLEAR together. CTRL is an abbreviation for CONTROL. The CTRL key is
located on the left of the keyboard, directly above the SHIFT key.
GETIING STARTED 15
It types:
Along with the burglar message, we could also arrange (as we will see later) to
have the computer sound an audible alarm. For example, it might play The
Jailhouse Blues in four part harmony! Hopefully, this might remind the burglar
of the possible consequences of crime and thus prevent the attempted theft. More
about that later!
16 ATARI BASIC
Since quotation marks define the beginning and the end of a string, do you
think they can be used as a character in the string?
No, they cannot. (That would really confuse the computer!) However, single
quotes (') can be used in a string. For example:
We type: PRINT "THEY SAID, 'ALL RIGHT.'"
Complete the statement so that the computer types what we say it types.
We type: PRINT _ _ _ _ _ _ _ _ _ __
Did you remember the quotation marks? Someday, of course, the computer will
turn off the oven! Almost anything electric can be controlled by the computer,
with the proper electronic connections and program, of course.
GETIING STARTED 17
7. Have you made any typing mistakes yet? In case you should make a typing
error, BASIC has a dandy way of fixing it. Watch while we make a typing error.
We type: PTINT "DENTIST APPOINTMENT TODAY"
We type
Then press DELETE BACK S
Press DELETE BACK S again
ABCDE • _____
ABCD...
ABC.
e
and yet again AB.
The cursor will move two places to the left, erasing A and B.
9. The DELETE BACK S key is great for deleting errors that you have just
made. But, suppose you are typing a long line and are almost to the end when,
alas, out of the left corner of your left eye, you spot a mistake way back at the
beginning. If you complete the line and press RETURN, you will probably get an
error message. You would like to just abort the line-that is, erase it from the
beginning.
Well you can! Hold down the SHIFT key 'and press the DELETE BACK S
key.
We type: PRIMT "ONCE UPON A TIME THERE WAS A
The computer erases the entire line and turns on the cursor. Whenever we see the
cursor, what is the computer telling us?
10. We can also tell the computer to do arithmetic and print the answer. In
other words, we can use the computer as a calculator.
We type: PRINT 7 + 5
It types: 12
It types: 13 + 6 It types: 19
I I
This is a string. The computer prints This is a numerical expression (no
it exactly as it appears. quotation marks). The computer does
the arithmetic and prints the result.
GEITING STARTED 19
Your turn. Complete the following, showing what the computer typed.
We type: PRINT 23 + 45
It types: _ _ _ _ _ _ _ _ _ __
We type: PRINT 1 + 2 + 3 + 4 + 5
It types: _ _ _ _ _ _ _ _ __
It types: _ _ _ _ _ _ _ _ __
68
15
1 + 2 + 3 + 4 + 5
We type: PRINT 7 - 5
It types: 2
It types: - 12 • 2 7
We type: PRINT 29 - 13
Ittypes: _ _ _ _ _ _ _ _ _ __
It types: _ _ _ _ _ _ _ _ _ __
16
-500
1500 - 2090
20 ATARI BASIC
12. BASIC uses + for addition and - for subtraction, just as we do with paper
and pencil. However, for multiplication, BASIC uses the asterisk (*).
We type: PRINT 7 * 5
It types: 35
It types: 5.61741
We type: PRINT 9 • 8
It types:
It types:
72
62.8
It types: 1.4
It types: 0.8125
It types: ____________________
Ittypes: ____________________
8
0.785
GETTING STARTED 21
1.23456789 OR 1.234567898
3.14159265 OR 3.141592653
15. Write PRINT statements to tell the computer to evaluate each of the
following numerical expressions. Also show the result printed by the computer.
(b) 15 - 8
(c) 2375
(d) 3 x 13
11 ATARl BASIC
(e) 8 -:- 3
16. The computer does arithmetic in left to right order, with all multiplications
(*) and/or divisions (I) performed before additions (+) and/or subtractions (-).
Shown below are some BASIC expressions in which two or more operations
are used. For some of these expressions, we have shown the value computed by
the computer after it does the indicated arithmetic. You complete the rest.
26 2*3+4*5 = 6 + 20 = 26
9 2+3*4-5 = 2 + 12-5 = 9
-16 2*3+4*5-6*7 = 6 + 20-42 = -16
•
GETTING STARTED 23
17. Here are more examples and exercises, using division (I). Study the ex-
amples and then complete the exercises. Be sure· to follow the computer's order
described in frame 16.
Expression Value computed How it did the
by computer arithmetic
3/4+5 5.75 3/4+5 = .75+5 = 5.75
2-3/4 1.25 2-3/4 = 2-.75 = 1.25
2*3 + 4/5 6.8 2*3 + 4/5 = 6 +.8 = 6.8
3/4 + 5*6
2-3 / 4+5
18. Following the computer's rules, give the computed value for each of the ex-
pressions below. (Remember, do arithmetic in left to right order.)
19. For each of the following numerical expressions, show the order in which
the computer does the arithmetic by putting numbers 1, 2, or 3 in the circles
above the operation symbols . We have completed the first three for you.
(a)
~~
2+4-4 (b)
~~
2-3 * 4
(c)
~~
2 * 3 / 4 (d)
QQ
2 / 3 / 4
QQ Q QQQ
2-3*4+1
(e) 2+3*4-2 (f)
(g)
QQQ
2+3/4*2 (h)
QQ
2 + 3 * 4
----------
(d)
~~
2/3/4
(e)
~ ~~
2+ 3*4-2 (f)
~~CV
2-3 * 4+1
$ Q)~ ~CJ)
(g) 2+3/4*2 (h) 2 + 3 * 4
20. If you want to change the order in which operations are done by the com-
puter, use parentheses. Operations in parentheses are donefirst.
Starting with the leftmost inner set of parentheses, the computer does
arithmetic within each set of parentheses in left to right order, with all multiplica-
tions (*) and/or divisions (I) before additions (+) and/or subtractions (-).
;} before { :
GETTING STARTED 25
Look at these examples of how the order of operations can give different results.
2*3+4 = 10
but 2*(3+4) = 14 Compute 3 + 4 first because it is inside
the parentheses, then multiply the result
by 2.
2+3*4+5 = 19
but (2+3)*(4+5) = 45 Compute 2 + 3, then compute 4 + 5, and
then multiply these two results.
21. Let's take another look at the order in which arithmetic is done. In the ex-
pressions below, the numbers in the circles show the order in which the opera-
tions are carried out. Write the final value for each expression. The operations
are always done in the innermost set of parentheses first.
Q~ CP~0
2 + 3 * (4 - (5 + 6 * 7»
~cp~~ Q
(3 * 4 + 5 * 6 - 7) / 8
-127
4.375
16 ATARI BASIC
22. Your next task is to write a proper PRINT statement to tell the computer to
compute and print the value of each expression listed below. We have shown the
actual value printed by the computer. Remember to indicate all mUltiplication
and division operations with the proper BASIC symbol.
PRINT 2*3+6/7
PRINT 16*(33-21) (Did you remember the asterisk?)
PRINT 3.14*2*2
PRINT (88-52)/(18+47)
23. For each numerical expression, show the order of operations by putting
numbers 1, 2, 3, and so on, in the circles above the operation.
QQ QQQQ
(a) 2 + (3 - 4) (b) 2 / (3 / 4) * (2 + 3)
QQ QQ
(c) 2 / (3 * 4) (d) (2 + 3) * 4
(e)
QQQ
(2 + 3) / (4 + 1) (f)
QQ
2 / (3 + 4)
Q QQ Q Q QQQQ
(g) 2 + «3 + 4) + 8) (h) 1 + 1 / (2 + 1 / (3 + 1 / 4»
----------
~cp ~~~~
(a) 2 + (3 - 4) (b) 2 / (3 / 4) * (2 + 3)
~cp cp G
(c) * 4)
2 / (3 (d) *
(2 + 3) 4
(e)
cp~~ ~~
(2 + 3) / (4 + 1) (f) 2 / (3 + 4)
(g)
CV~~ (h)
~ ~ ~~~~
2 + «3 + 4) + 8) 1 + 1 / (2 + 1 / (3 + 1 / 4»
GETTING STARTED 27
h---:l
We press thelSHIFTland ~ keys together, to tell the computer t6 raise a
number to a power.
We type: PRINT 2 ~ 3
It types:
We type: PRINT 2 ~ 4
It types:
We type: PRINT 2 ~ 5
It types:
We type: PRINT 7 ~ 2
It types:
----------
8 2~3 23 2X2X2 = 8 (The computer gave 7.99999986)
16 2~4 24 2X2X2X2 = 16 (The computer gave 15.9999993)
32 2~5 2 5
2X2X2X2X2 = 32 (The computer gave 31.99993444)
49 7~2 72 7X7 = 49 (The computer gave 48.99999631)
28 ATARI BASIC
PRINT 3 A 7
PRINT 8 A 5 (PRINT 8*8*8*8*8 will also produce the desired result.)
PRINT 1.06 A 10
26. Suppose we put $123 into a savings account that pays 6070 interest per year,
compounded yearly. The amount of money in the account after N years can be
computed using the compound interest formula shown below.
A = P(1 + R/l00)N
where:
P = Principal, or original amount put into the account.
R = Rate of interest per year, in percent.
N = Number of years.
A = Amount in 'account at the end of N years.
In our problem, P = $123, R = 6070, and we want to know the value of A for
N = 2, N = 5, and N = 12 years.
Your turn. Write the PRINT statement for N = 12. We have supplied the result
printed by the computer.
We type:
It types: 247.5 After 12 years our money doubled.
GETTING STARTED 19
27. Computers use a special notation to indicate very large numbers, or very
small decimal fractions. This method of representing numbers is called jloating
point notation. You decipher it in the same way you do the scientific notation
commonly used in mathematics and science textbooks.
For example, the population of the earth is about 40 billion people.
40 billion = 40,000,000,000
Now let's ask the computer to print the population of the earth.
4E+1O
Mantissa
tY
Exponent
The mantissa and the exponent are separated by the letter ______
NOTE: In typing numbers into the computers, we may not use commas as we
normally do when writing numerals. Commas have a special use in BASIC
PRINT statements. Please be patient. We will get to it soon.
28. Here are some examples showing numbers in good old everyday notation
and again in floating point notation.
One trillion
ordinary notation: 1,000,000,000,000
floating point notation: IE + 12
Volume of the earth in bushels
ordinary notation: 31,708,000,000,000,000,000,000
floating point notation: 3.1708E + 22
30 ATARi BASIC
29. Here are more examples, showing how the ATARI computers print floating
point numbers.
It types: 12345789
It types: 1.23456788E+Ul
It types: 1.2345678E+1S
Our ATARI computer prints at most nine digits for the mantissa and chops the
mantissa at the ninth digit. The mantissa is printed as a nonzero digit to the left
of the decimal point and up to eight digits to the right of the decimal point.
It types:
It types:
3.33333333E+ll
6.66666666E+11
GETTING STARTED 31
The mantissa is printed with how many digits to the left of the decimal
point? Up to how many digits to the right of the decimal
poip.t? _ _ _ __
one; eight (See frames 28 and 29 for examples of mantissas with fewer than 8
digits to the right of the decimal point.)
Therefore, 4E + 09 = 4,000,000,000
Now you try it: 1,2345678E + 13
(a) (b)
Therefore, 1.2345678E + 13 = _ _ _ _ _ _ _ _ __
32. When the exponent is negative, we simply change the direction that the
decimal point moves.
(a) Write the mantissa separately.
(b) Move the decimal point of the mantissa to the left the number of
places specified by the exponent. If necessary, add zeros.
Example: 7.9E-06
(a) 7.9 (b) ~9 (Move point 6 places and add 5 zeros)
Therefore, 7.9E-06 = .0000079
(a) 1.23456
(b) .~23456
.0000000123456
33. Write each of the following floating point numbers in ordinary notation.
Floating point notation Ordinary notation
(a) 1.23456E + 06
(b) 1.23456E-06
(c) 1.23456E + 07
(d) 1.23456E-09
(e) lE+ 11
(0 lE-11
34. Write each "ordinary" number in AT ARI BASIC floating point notation.
Ordinary notation Floating point notation
(a) 1,234,560,000
(b) .000000123456
(c) 10,000,000,000
(d) .000000001
(e) 1234567888888
(f) .000001234567888888
(g) 6.02 x 10 21 (See note.)
(h) 1.67 x 10- 11 (See note.)
Note: the numbers in (g) and (h) are written in scientific notation commonly used ·
in mathematics and science.
(a) 1.23456E + 09
(b) 1.23456E-07
(c) lE+ 10
(d) lE-09
(e) 1.23456788E + 12 Mantissa truncated to 9 digits
(f) 1.23456788E-06 Mantissa truncated to 9 digits
(g) 6.02E + 21
(h) 1.67E-ll
35. Perhaps you have heard of the ancient story of the wise person who did a
great service for a wealthy king. The king asked this person what reward would
be appropriate. The person's request was simple. She asked only for grains of
wheat, computed as follows. For the first square on a chess board, one grain of
wheat; for the second square, 2 grains of wheat; for the third square, 4 grains of
wheat and so on, doubling at each square. It goes on as shown below.
And so on. For square N, there will be 2N - 1 grains. Let's find out how many
grains on square 16.
We type: PRINT 2 ,.. 15 Since N = 16, N-l = 15.
It types: 32768
Your turn. Write a PRINT statement to find out how many grains of wheat for
square number 64.
We type:
It types: 9.2233704E+18 That's a lot of wheat!
PRINT 2 " 63
GETIING STARTED 35
SELF-TEST
Try this Self-Test, so you can evaluate how much you've learned so far.
2. Assume that you are typing a statement into the computer, and you make a
typing error. How would you correct the error?
3. Write the symbols used in ATARI BASIC for the following arithmetic
operations.
addition
subtraction
multiplication
division
exponentiation
(raising a number to a power)
It types:
It types:
It types:
3(i ATARI BASIC
C = ~(F - 32)
9
Degrees Celsius 1
Degrees Fahrenheit
t
Write a PRINT statement to tell the computer to compute and print the degrees
Celsius (value of C) if F = 72 degrees.
It types:
10. Indicate which of the following numerical expressions are valid, as written,
in BASIC.
_ (a) 2*3*4*5
_ (b) 7(8 +9)
_ (c) 1.23 ~4.567
_(d) 3"2+4"2
_ (e) 1
37 - 29
- (f) (l + 7/ 10W
- (g) 2"2"2
11. For each invalid numerical expression in problem 10, write a valid BASIC
numerical expression. _ _ _ _--:-_ _ _ _ _ _ _ _ _ _ _ _ _ __
GETTING STARTED 37
I
12. For each of the following numerical expressions, show the order in which
the computer does the arithmetic by putting numbers 1, 2, 3, and so on, in
the cin.;les above the operation symbols .
(a)
QQ
2+4-4 (b)
QQ
2 * 3 * 4
QQ (d)
QQ
(c) 2 * 3 / 4 2 / 3 / 4
(e)
QQ
2/3 * 4 (f)
QQ
2-3*4
QQ QQ
(g) 2 +3 / 4 (h) 2 t 3 * 4
(i)
QQ
2 * '3 t 4 (j)
QQ
2 t 3 / 4
QQ QQ
(k) 2/3 t 4 (1) 2 t 3 t 4
(m)
QQQ
1 + 2 * 3 t 4 (n)
QQQQQ
2 * 3 - 4 t 3 + 5 t 2
13. For each numerical expression, show the order of operations by putting
numbers 1, 2, 3, and so on, in the circles above the operation.
(a)
QQ
2 + (3 - 4) (b)
QQ
2 / (3 / 4)
(c)
QQ
2 / (3 * 4) (d)
QQ
(2 - 3) * 4
(e)
QQ
(2 + 3) / 4 (f) 29(394 )
(g)
QQ
2 t (3 t 4) (h)
Q Q QQQQ
1 + 1 / (2 + 1 / (3 + 1 / 4))
14. Write each of the following floating point numbers in ordinary notation.
Floating point notaticm Ordinary notation
(a) 1.23456E + 05
(b) 1.23456E-05
(c) 1.23456E + 08
(d) 1.23456E-08
(e) lE+ 12
(f) lE-12
38 ATARI BASIC
15. Write each "ordinary" number in ATARI BASIC floating point notation.
Ordinary notation Floating point notation.
(a) 1,234,560,000,000
(b) .00000000123456
(c) 10,000,000,000
(d) .0000000001
(e) 12345678999
(t) .00000123456789
(g) 6.02x10 23 (See note.)
(h) 1.67xlO-21 (See note.)
Note: The numbers in (g) and (h) are written in scientific notation common-
ly used in mathematics and science.
Answers to Self-Test
The frame numbers in parentheses refer to the frames in the chapter where the
topic is discussed. You may wish to refer to these for quick review.
1. Executes the statement, then "forgets" it (frame 5)
2. Press DELETE BACK S to delete the mistake, then type the correct
character (frames 7-9)
3. addition +
subtraction
mUltiplication *
division /
exponentiation
(frames 10-24)
4. ERROR - PLEASE DO THE HOMEWORK (The computer does not
understand what we want.) (frame 3)
5. PLEASE DO THE HOMEWORK ON PAGE 157 (frames 5- 6)
6. 98.6 (frames 16-24)
7. PRINT 5/ 9*(F-32) or PRINT (5 / 9)*(F-32) Did you remember the
asterisk? By the way, we will also accept PRINT 5*(F-32)/9 (frames
20-23)
8. Only the cursor shows. Entire line deleted. (frame 9)
9. Evaluate the numerical expression 23 + 45 and print the result. The com-
puter will add 23 and 45, getting 68, and print 68 . (frame 10)
GETfING STARTED 39
10. - Expressions (a), (d) and (g) are valid. In (g), the computer will evaluate
2 2 2, as follows: 2 2 2 = 4 2 = 16. In other words, the computer
A A A A A
(a) 2+4-4
cp~
(b)
~~
2 * 3 * 4
12.
cp~ CJ)~
(c) 2 * 3/4 (d) 2/3/4 ,
cp~
(f)
~~
2-3*4
(e) 2 / 3 * 4
C})(P cp~
(g) 2+3/4 (h) 2 t 3 * 4
~~ cp~
(i) 2 * 3 t 4 (j) 2 t 3 / 4
~~ cp~
(k) 2 / 3 t 4 (1) 2 t 3 t 4
~~CJ) ~~~~~
2*3-4t3+5t2
(m) 1+2*3t4 (n)
(frames 16-25)
13. (a)
~ CD
2 + (3 - 4) (b)
c!)CD
2 / (3 / 4)
~CJ) ~ ~
(c) 2 / (3 * 4) (d) (2 - 3) * 4
cp~ ~ CD
(e) (2 + 3) / 4 (f) 2 t (3 * 4)
~ cp ~~$~~CD
(g) 2 t (3 t 4) (h) 1 + 1 / (2 + 1 / (3 + 1 / 4»
(frames 16-25)
40 ATARI BASIC
Assignment Statements,
Stored Programs, and Branching
This chapter introduces some of the most useful BASIC statements. From here
on, we can work with more interesting programs to illustrate home, school, and
personal applications of computers.
In this chapter, you will learn the function and format for the statements
and commands listed below.
Statements: LET, INPUT, GO TO, PRINT, REMARK
Commands: NEW, RUN, LIST
You will learn how you can store programs for automatic and repetitive execu-
tion. You will also learn about variables and be able to supply values for
variables used in BASIC programs. When you have finished this chapter, you
will be able to:
41
41 ATARl BASIC
1. To illustrate the concept of variable and the function of the LET statement
in BASIC, imagine that there are 26 little boxes inside the computer. Each box
can contain one number at anyone time.
A 7 H 0 V
B 5 I P 4E+09 W
C J 4 Q X 2.5
D K R Y
E L S -6 Z
F 2 M T
G N U
We have already stored numbers in some of the boxes. For example, 7 is in box
A and 5 is in box B.
(a) What number is in box F? _ __
(b) In J? _ __
(c) -6 is in box _ __
(d) 2.5 is in box _ __
(e) What box contains a floating point number? _ __
(a) 2; (b) 4; (c) S; (d) X; (e) P (The floating point number is 4E + 09.)
NI
L __ 2_7----J
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCHING 43
3. When the computer puts a number into a box, it automatically erases the
previous content of the box, just as you did. In order to put 27 into box N, you
first erased the previous content, 12.
We call A, B, C, ... , Z variables. The number in box A is the value of A;
the number in box B is the value of B; the number in box C is the value of C,
and so on.
We use the LET statement to instruct the computer to "put a number in a
box." To say it more technically, we are assigning a numerical value to a
variable.
It types: 7
In this program, the variable is ____ and the value assigned to it by the
LET statement is _ _ __
A; 7
It types: It types:
It types: It types:
----------
(a) 23
(b) -1
(c) 2 (The statement LET A = 2 replaced the value previously assigned by LET
A= 1.)
(d) 7 (LET W = D copies the value in D into W. The value is still in D, also.)
44 AT AJU BASIC
5. Write a LET statement to assign the value 3.14 to P and a PRINT state-
ment to print the value of P.
We type:
It types:
LET P 3.14
PRINT P
3.14
Note: In ATARI BASIC, the word LET can be omitted. For example, we can
type P=3.14 instead of LET P=3.14.
We type: LET A = 7
L 'ET B = 5
We type: PRINT A + B
It types:
We type: PRINT A/B
It types:
35
1.4
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCmNG 45
7. The variables that we have used so far are called numeric variables. The
value assigned to a numeric variable must be a number.
BASIC has another type of variable, called a string variable. The value
assigned to a string variable must be a string. A string variable is indicated by a
letter followed by a dollar sign($).
Before we use a string variable, we must first tell the computer how long
the string might be. That is, we must tell the computer how much memory might
be needed to hold the string. We do this with a DIMension statement. For exam-
ple, the following DIM statement tells the computer to make room for a string of
up to 5 characters in length.
It types: READY
We have told the computer that a string variable called N$ can have as many as 5
characters in length.
N$l JERRY I
Now, let's print the value of N$.
We type: PRINT N$
It types: JERRY
It types:
MY HUMAN UNDERSTANDS ME
Our DIM statement reserved space for up to 100 characters. Our actual string, in
this case, is shorter than 100 characters. Chapter 9 will describe strings in much
more detail.
46 ATARI BASIC
8. Let's look at a problem. We have three bicycles with wheels of 16-, 24-,
and 26-inch diameters. For each bike, we want to know how far the bike travels
during one revolution of the wheel. This distance, of course, is the circumference
of the wheel. Let's use D to represent the diameter of the wheel and C to repre-
sent the circumference.
c= 1TD
T
D
~
where 1T ,= 3.14159 .. .
It types: 50.24
We type: PRINT 3. 14 • 24
It types: 75.36
It types: 81. 64
We type: LET D = 16
PRINT 3. 14 • D
It types: 50.24
We type: LET D 24
PRINT 3. 14 • D
It types:
We type: LET -
It types: 81.64
75.36
D = 26
PRINT 3.14 * D
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCHING 47
9. Now we are ready to take the big step from one-at-a-time direct statements
to a stored program. We will store this program in the memory of our computer.
(Just look at it, then read on.)
10 LET D = 16
20 PRINT 3.14 • D
The above program has two statements, each on a single line. Each statement
begins with a line number (in this case, the 10 and 20). A line number can be an
integer from 1 to 32767.
When we type statements with line numbers, the statements are not
executed when you press RETURN. Instead, the statements are stored in the
computer's memory for later execution.
As you learned in Chapter 2, statements without line numbers are called
direct statements. The computer executes a direct statement immediately and then
forgets it. However, this does not happen when we type a statement with a line
number. Instead, what does happen?
The statement is stored in the computer's memory for later execution. That is,
the computer remembers the statement.
10. The line numbers tell the computer the order in which it is to follow
statements in the program. It is not necessary for line numbers to be consecutive
integers (e.g., 1, 2, 3, 4, 5 ... ). It is common practice to number by tens as we
do in the following program. Then, if we wish, we can easily insert or add more
statements into the program between existing statements.
10 LET D = 16
20 PRINT 3.14 • D
How many additional lines could be added betwen line 10 and line 20?
9 (lines 11, 12, 13, 14, 15, 16, 17, 18, 19)
Of course, you don't have to number by tens. If you prefer numbering by thir-
teens or fives or jumping around, help yourself!
11. Before we store a program, we must first remove or erase any old programs
that may already be stored in the computer's memory.
48 ATA.RI BASIC
The computer has erased the portion of its memory that stores BASIC programs.
It is now ready to accept a new program.
How do we erase, remove, or delete an old program from the computer's
memory?
Our computer doesn't appreciate puns, but if we type NEW correctly and press
RETURN, all is well.
12. Now we are ready to store our two-line program from frames 9 and 10.
First, we must erase any old program by typing and pressing the
RETURN key. To store the program, we type the first line or statement and
press the RETURN key, then type the second line and _ _ _ _ _ _ _ _ __
We type: NEW
It types: READY
We type: 10 LET D = 16
20 PRINT 3.14 • D
If we make a typing error, we start over. The program is now stored. The com-
puter is waiting patiently for our next statement or command.
v
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCHING 49
The command LIST tells the computer to type all of the program statements that
are currently stored in its memory. After listing the program, the computer types
READY to let us know it is our turn again. If there is no program stored, the
computer will simply type READY.
How do we tell the computer to type out the program that is currently
stored in its memory? ________________________
- - - - - 1- - - - -
14. Listing a .BASIC program lets us know whether or not a program is stored
in the computer's memory. We may want to add more statements to the prog-
ram. Or we may wish to see if we have typed the statements correctly. If we
want the computer to execute the program, we type . If we want the
computer to tell us if there is a program stored in its memory, and to type out
the program for us, we type ______
RUN, LIST
15. After you type RUN to tell the computer to execute a program, or after
you type LIST to command the computer to tell us "what's on its mind" (that
is, what is stored in its memory), what else do you have to do before the com-
puter will respond?
16. Assume you have typed our little program into the computer.
The program is stored in the computer's memory. Now you want the computer
50 ATARI BASIC
to execute (carry out) the program. Type RUN and press the RETURN key.
We type: RUN And press RETURN.
It types: 59. 24
READY
The computer has RUN the program. That is, it has executed the
statements of the program in line number order. First, the computer was told to
assign a value to a variable with this statement: 10 LET D = 16. That means that
the computer placed the value ____ in the box identified by the variable
16; D
17. After following the instruction in the statement with line number 10, the
computer went onto the next statement in line number order: 20 PRINT 3.14*D.
Part of line 20 tells the computer to multiply two numbers. One number is given
in the statement. The other number is stored in a place labeled by the variable D.
What are the two values that the computer uses to multiply?
18. In the last frame we saw that part of line 20 tells the computer to mUltiply
two values. What else does line 20 tell the computer to do? _ _ _ _ _ _ _ __
19 LET D = 16
29 PRINT 3.14 • D
39 END
19. Let's review what happens when we use our little program that calculates
the circumference of a bicycle wheel.
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCHING 51
The program is still stored in the computer's memory. What will happen if we
now type RUN ___________________________________________________
The computer will execute the program again. Since nothing in the program has
changed, the same result (50.24) will be printed.
We type: RUN
Suppose we had seen the mistake immediately after we had typed it. How
could we have corrected it? ______________________
52 ATARI BASIC
We could have pressed the DELETE BACK S key, then the correct letter.
We type: Ie LET 0 = 24
LIST
It types: Ie LET 0=24 Here is the new line 10, and the old
213 PRINT 3.14*0 line 20.
We type a new line with the same line number as the line we wish to replace.
Note: We do not type NEW because we want line 20 to remain in the computer
while we change only line 10. Typing NEW would erase both statements from the
computer's memory .
We type: RUN
It types: 75.36
We type: , Ie LET 0 = 26
We type: LIST
It types:
From now on, we will
usually omit the word
We type: RUN READY in this book.
It types:
40 LET A
50 PRINT A
p. ( 1 +R/ 11/J1/J) ~N (Did you remember the asterisk? Did you remember to use '"
to tell the computer to raise to a power?)
24. We will store and RUN the program, then ask you to make some changes.
We type:
RUN
31/J LET N =5
54 ATARI BASIC
RUN
It types: 1718.19
20 LET R=7
30 LET N=8
26. Now show how the computer will respond if we type LIST.
We type: LI ST
It types:
10 LET P=1000
20 LET R=7
30 LET N=8
40 LET A=P·(I+R/100)~N
50 PRINT A
27. LET statements are all fine and good, but what a hassle to change all those
LET statements every time you want to change the values of variables. Ah, but
leave it to BASIC to come up with a clever solution-the INPUT statement.
The INPUT statement allows the computer user to assign different values to
variables each time a program is RUN without modifying the program itself.
When the computer comes to an INPUT statement in a program, it types a ques-
tion mark and waits for the user to enter a value for a variable. Below is an
example:
We type: NEW First, we erase any old program, then enter this
10 INPUT A program.
20 PRINT A
RUN Then we tell the computer to RUN the program.
It types: ?I The computer types a question mark, turns on the
cursor, then waits.
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCHING 55
28. The INPUT statement causes the, computer to type a question mark, then
stop and wait. It is waiting for a value to assign the variable which appears in the
INPUT statement. Computers are very patient. If we don't cooperate by typing a
value, the computer will simply wait, and wait, and wait.
So let's cooperate with our ever-patient computer and type in a value for A.
We will enter 3 as a value to be assigned to A, then press RETURN. The com-
puter will put our value into box A , then continue running the program.
NEW
1111 I NPUT A
2111 PR I NT A ......~-----, After we typed 3 as the value for A and then pressed
RUN RETURN, the computer went on to line 20 and
?3 printed our value.
3 ......~----------~
After we typed RUN and pressed the RETURN key, the computer typed a
_____________________ . We then typed 3, which is our value for the INPUT
variable . The computer then executed the PRINT A statement (line 20)
and printed the of A.
29. The program can be RUN again with a different value of A supplied by the
user. Pretend you are the computer, and show how a RUN would look if your
human computer user typed 23 as the value of A.
56 ATARI BASIC
RUN
?23
23
30. Now let's use an INPUT statement to enter (type in) data for our familiar
bicycle wheel problem. Data is the name for information used by a computer
program.
We type: NEW
10 INPUT D
20 PRINT 3.14*D
RUN
It types: 7
The computer wants a value for 0, the diameter of our bicycle wheel. It will then
compute the distance traveled in one revolution, and print it. Let's do it for
0=16,0=24, and 0=26.
RUN First, we RUN the program for D = 16. When the computer
?16 typed a question mark, we entered 16 as the value of O. The
5Jil.24 computer computed and printed 3.14*0, then stopped.
RUN We typed RUN again and, when the computer typed the ques-
tion mark, we supplied 24 as the value of O. The computer zap-
124
75.36 ped out the answer, and stopped.
RUN
Your turn. Complete the third RUN.
?26
81.64
3l. One INPUT statement can be used to assign values to two or more
variables.
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCHING 57
When the computer typed a question mark, we typed two numbers separated by
a comma, then pressed the RETURN key. The computer assigned the first
number as the value of A and the second number as the value of B. Note the
following.
RUN
17,5 ... No comma after last number
The value 7 is assigned to the variable A, and the value 5 is assigned to the
variable B.
Here is the summary; you fill in the blanks. When a program containing an
INPUT statement with multiple variables is RUN, the first value typed in by the
user after the INPUT question mark will be assigned to the
variable that appears in the INPUT statement; the value typed in
by the user will be assigned to the second variable appearing in the INPUT state-
ment, and so on. Both the variables in the INPUT statement in the program, and
the values typed in by the user when the program is RUN, must be separated by
32. Here is another RUN of the program in frame 31. We want to enter 73 as
the value of A and 59 as the value of B. .
RUN
1.
1 73 Oops! We absentmindedly hit the RETURN key.
The computer typed another question mark and turned on the cursor. This means
"Didn't you forget something?"
We then completed the RUN by entering the second number, the value of B.
58 ATARl BASIC
RUN
? 73
? 59
132
Note: If you enter more numbers than there are variables, the computer will ig-
nore the extra values.
33. Suppose you and a bunch of friends, all bicycle aficionados, are gathered
about your computer, and they are marveling at your newly acquired computer
programming skills. You decide to demonstrate how the computer works by
using the program to compute the distance traveled in one turn of the wheel.
However, you have to do a separate RUN of the program for each friend. But
wait-first add a new statement to the program.
IS INPUT D
2m PRINT 3.14*D
3m GO TO 1m 1-------
...... Something new-a GO TO statement.
10
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCHING 59
34. Let's see what happens when we RUN the program. (Assume that you, or
someone, has typed NEW, then entered the program.)
RUN
716 Following each question mark, we typed the value of D. ~he
518.24
724 computer then computed the value of 3.14*D, printed the result,
75.36 and typed another question mark.
726
7.
81.64
Well, we are finished, but the computer doesn ' t know that we are finished. It is
hung up on line 10, waiting for INPUT data. How do we get out of this situation?
Easy! Just press the BREAK key. I I
? f----------We press the BREAK key.
......
S TOPPED AT LI NE 118 --of---It types the line number where the BREAK occurred
• and turns on the cursor
Suppose the computer is executing an INPUT statement and has typed a question
mark . If we press the BREAK key, what does the computer do?
35. Remember: BASIC statements are executed in line number order, unless a
GO TO statement changes the order. In the program from frame 33, the
statements are executed in the order shown below by the arrows.
RUN
Q
318 GO TO III! 1'--__---'
Do not RUN this program yet! Instead, draw arrows as we did on the previous
page to show how the computer executes the program.
RUN
27 GO TO 13
RUN
We have used line numbers 13 and 27 to remind you that you don't have to
number by 10, although we usually do.
36. Now let's enter the program of frame 35 and RUN it. Remember, to stop
the computer, press the BREAK key.
RUN
TO STOP ME. PRESS THE 'BREAK' KEY
TO STOP ME, PRESS THE 'BREAK' KEY
TO STOP ME, PRESS THE 'BREAK' KEY
TO STOP ME, PRESS THE 'BREAK' KEY
TO STOP ME, PRESS THE 'BREAK' KEY
The computer executed line 13 over and over again, because the GO TO state-
ment in line 27 repeatedly told the computer to go back to line 13. This is a
"forever" loop-it just keeps going around and around until someone interrupts
it. Sometimes this is referred to as an "infinite" loop, because it does not stop
automatically.
How do we interrupt, or stop, the computer when it is running a program?
37. Now that you know how to stop a runaway computer by pressing the ·
BREAK key, let's write a "Happy Computer" program. This program will
quickly fill the screen with laughter! Remember: before entering the program,
type NEW to erase any old program. Then, enter the following program. The
program contains some old statements, such as PRINT and GO TO, and a new
statement called REMARK. We use the REMARK statement to tell something
about the program to a human who may be reading the program. The computer
simply ignores REMARK statements.
120 GO TO 110
OK, run this program. Did the screen fill with laughter? _ _ _ _ _ _ _ _ __
We think you answered YES. When we did it, the entire screen quickly filled
with HA HA.
38. Try the above program with line 110 modified as follows:
Run the modified program. Did only the left portion of the screen fill with
HA HA? _ _ _ _ _ _ _ _ __
Again, we think you answered YES. The semicolon causes the computer to print
across the screen from left to right. When the computer gets to the right edge, it
simply continues on the next line. Without the semicolon, the computer starts a
new line after each PRINT statement.
62 ATARI BASIC
39. The REMARK statement has a short form that you can use. The short
form is simple, the first three letters in REMark. When executing a program, the
computer only "looks" at the first three letters, and if those three letters are
REM, then the computer will skip on to the next statement in line number order
without considering the rest of the statement. Therefore, you could use the word
REMEMBER instead of REMark and the effect would be the same.
Rewrite line 100 in frame 37 using the short form of the REMark
statement
40. When the computer executes a PRINT statement that has only the word
PRINT following the line number and nothing else in the statement, it does the
same thing that happens when you press the RETURN key. It spaces down one
line on the screen. This has the effect of leaving a blank or "empty" line on the
screen. An "empty" PRINT statement is very handy for making the printout of
a program easier to read.
Here is a RUN from our bicycle wheel program from Frames 33 and 34.
RUN
7 16
5"'.24
724 Crowded, isn't it?
75.36
726
7.
81.64
Well, let's make it less crowded! We will add the following empty PRINT state-
ment to the program.
25 PRINT
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCHING 63
We type: Ul INPUT D
20 PRINT 3.14*D
25 PRINT
30 GDTD 10
716
50 . 24
.....
C - - -- Note the line space. The "empty" line is provided by line 25.
724
75.36
~ Another line space.
726
81.64
~
Still another line space.
?I and so on.
Compare the two RUNs. What does 25 PRINT tell the computer to do?
Print a line space ("empty" line) after printing the value of 3.14*D. This hap-
pens before the computer executes GO TO 10.
41. Now, in order to make things really clear when dealing with INPUT
statements, we need a way to inform the user what the INPUT statement is ask-
ing for. Let's add this statement to the bicycle program:
5 PRINT "WHEEL DIAMETER" ;
See the semicolon at the end of the PRINT statement? When a semicolon is used
at the end of a PRINT statement, the computer stays on the same line of the
screen instead of going to the beginning of the next line. Here is our revised
program .
5 PRINT "WHEEL DIAMETER" I
Ul INPUT D
20 PRINT 3.14*D
25 PRINT
30 GO TO 10
64 ATARI BASIC
50.24
WHEEL DIAMETER? •
42. Now what the computer wants is clearly identified by the string enclosed in
quotation marks in the PRINT statement. We use the same approach in a
PRINT statement to identify the calculation performed by the computer for that
statement (see line 20).
5 PRINT "WHEEL DIAMETER"
10 INPUT D
20 PRINT "DISTANCE IN (lNE TURN IS " I 3.14*D
25 PRINT
30 GOTO 10
The statement 20 PRINT "DISTANCE IN ONE TURN IS " ; 3.14*D tells the
computer to:
(1) print the string DISTANCE IN ONE TURN IS, then
(2) compute and print the value of 3.14*D.
Let's try it. Complete the RUN.
RUN
WHEEL DIAMETER? 16
DISTANCE IN ONE TURN I S 50.24
WHEEL DIAMETER? 24
DISTANCE IN, ONE TURN IS - - -_ _ _ _ _ _ __
WHEEL DIAMETER? 26
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCHING 6S
75.36
DISTANCE IN ONE TURN IS 81.64
--------Our
WHEEL DIAMETER? • -.... everfriendly cursor.
43. Rewrite the program in frame 42 so that a RUN looks like the one shown
below.
RUN
WHEEL DIAMETER?16
DISTANCE IN ONE TURN IS 50.24
WHEEL DIAMETER?24
DISTANCE IN ONE TURN IS 75.36
44. The INPUT statement, like the LET statement, belongs to the class of
BASIC instructions called assignment statements. The value we type in response
to a question mark is assigned to the variable in the INPUT statement. If the
INPUT variable is a string variable, we can type in a string as the value and it
will be assigned to the string variable. Here is a simple example. You do the parts
that we omit.
5 DIM NS(lRJ01
10 PRINT "WHAT IS YOUR NAME",
20 INPUT NS
30 PRINT NS
40 PRINT
50 GO TO 1RJ
R,tJN
WHAT IS YOUR NAME? JERALD R. BROWN
JERALD R. BROWN
WHAT IS YOUR NAME? LEROY FINKEL
LEROY FINKEL
FIREDRAKE, THE DRAGON
45 . Here's a program that lets you use the computer as an adding machine, by
repeating an "adding routine" with a GO TO loop. A "routine" is one or more
statements to complete a computing task.
17~ PRINT
18~ PRINT " X="; Lines 170 through 220 are a GO TO
19~ INPUT X loop. These lines are done for each
2~~ LET T=T+ X
21~ PRINT "TOTAL SO FAR IS " ;T
number entered by the user.
22~ GOTO 17~
Notice the LET statements using the variable T in lines 160 and 200. Line
160 is outside the GO TO loop. It is executed once before the loop begins, setting
T equal to zero . This is called initializing, giving an initial or starting value to a
variable.
Line 200 is inside the GO TO loop. Therefore line 200 will be executed each
time through the loop. In line 200, a new value of T is computed by adding the
old value stored in box T to the INPUT value of X entered by the computer
user.
(a) Suppose the old value of T is zero and the INPUT value of X is 12.
What is the new value of T? _ _ _ __
(b) Suppose the old value of Tis 12 and the INPUT value of X is 43. What is
the new value of T? ______
46. Note how the PRINT statements (lines 110-150) are used to provide the
user with an explanation and instructions for using the program. Are the PRINT
statements inside or outside the GO TO loop? _ _ _ _ _ _ _ _ _ __
outside
Therefore, lines 110-150 will be done once, when you first type RUN. Of
course, if you interrupt the computer (by pressing BREAK) and RUN the pro-
gram again, lines 110-150 will be done again.
47. It's RUN time. Let's see how the progam works.
RUN
X=? 12
TOTAL SO FAR IS 12
X=? 43
TOTAL SO FAR IS 55
X=? 33
TOTAL SO FAR IS 88
X=? 92
TOTAL SO FAR IS 180
X=? 76.25
TOTAL SO FAR IS 256 . 25
X=? f--------
..... Do you remember how to get out of this? If not,
check frame 34.
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCHING 69
From the listing in frame 45, let's focus on the statement in line 200. For
the RUN above, the first time through the program the values of the variables to
the right of the = symbol in 200 LET T = T + X will be:
LET T = 13 + 12
So the new value for T is 12. The value is printed. Notice that the computer
substitutes the current values in the boxes for the variables to the right of the
sign each time it executes line 200.
For the second time through the "loop" section of the program show the
values:
LET T = _-,--_ + _ __
LET T = 12 + 43
55
-t1!ifo :ra.~
III~;:: )(.',001 Jr" , • ••
70 ATARI BASIC
48. Now that you know how to stop a "runaway" computer, try this program,
which causes the computer to print counting numbers, 1, 2, 3, 4, 5, and so on,
until you press BREAK.
10 LET N=1
·20 PRINT N
30 LET N=N+l
40 GO TO 20
RUN
2
3
4
5
6
7
STOPPED AT LINE 20
]]
20 PRINT N
-0
30 LET N=N+l
0-
40 GO TO 20
30 LET N =N + 1
NeWValue~J
Old value
(a) Suppose the old value of N is 1. What will the new value of N be? _ _ __
(b) Suppose the old value of N is 2. What will the new value of N be? _ _ __
(a) 2; (b) 3 (Line 20 will print the new value next time around the loop.)
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCHING 71
49. With a counting loop and a few other statements, we can write a program
to show how our money grows, year by year. Here it is.
150 PRINT
160 PRINT "PRINCIPAL",
165 INPUT P
170 PRINT "INTEREST RATE",
175 INPUT R
RUN
PR I NC I PAL? 1000
INTEREST RATE? 6
YEAR = 1
AMOUNT 1059.99999 In Chapter 4, we will show you
how to round off to the nearest
YEAR = 2 penny.
AMOUNT 1123.59997
YEAR = 3
AMOUNT 1191.01596
And so on, until someone presses BREAK. Which statements are part of the
loop? Give the line numbers. _____________________
190,200,210,220,230,AND240
We suggest that you also draw a box around the loop with a felt tip pen. Note
that lines 100 through 180 are outside the loop. They are done once during a
RUN, while lines 190 through 240 are repeated over and over until you press
BREAK. '
72 ATARI BASIC
SELF-TEST
Try this Self-Test, so you can evaluate how much you have learned in this
chapter.
1. Before entering a program, we usually first type NEW and press RETURN.
Why? ____________________________________________________
Describe how to replace the second statement (line 20) without erasing the
entire program.
7. Each of the following statements contains an error. Mark the error and
show the statement in correct BASIC format (syntax, that is).
(a) 2~ PRINT DISTANCE IN ONE TURN ISI3.14·D
(b) 3~ GO TO 3.14
(c) 3~ GO TO - l~~
(d) 1~ INPUT, Z
(e) HI INPUT Z,
(b) 1~ LET A 7
2~ LET B 5
3~ PRINT X + Y
74 ATARI BASIC
RUN
A=? 7
B=? 5
F = ~ C+32
RUN
YOU ENTER DEGREES CELSIUS.
I WILL TYPE DEGREES FAHRENHEIT.
DEGREES CELSIUS? 0
DEGREES FAHRENHEIT 32
DEGREES CELSIUS? 37
DEGREES FAHRENHEIT 98.6
DEGREES CELSIUS?
And so on.
12. Congratulations! You are the big winner on a TV show. Your prize is
selected as follows.
A number from 1 to lOOO is chosen at random. Call it N. You then select
one, and only one, of the following prizes . You have 60 seconds to make
your selection.
ASSIGNMENT STATEMENTS, STORED PROGRAMS, AND BRANCHING 7S
RUN
N=? UIJ/IJ
PRIZE NIIl. 1 1/IJ/IJ Take PRIZE NO . 1
PRI ZE NIIl. 2 2.7/IJ4781277
N=? 5/IJ/IJ
PRIZE NIIl. 1 5/IJ/IJ Take PRIZE NO.1
PRIZE NIIl. 2 144.772491
N=? 1/IJ/IJ/IJ
PRI ZE NIIl. 1 1/IJ/IJ/IJ Take PRIZE NO.2
PRI ZE NIIl. 2 2/IJ959./IJ741
13. Write this program to assist you in performing that tiresome task called
"balancing the checkbook." Here is a RUN of our program.
RUN
I WILL HELP YOU BALANCE YOUR CHE CKBOOK.
ENT~R CHECKS AS NE GATIVE NUMBER S AND
DEPOSITS AS POSITIVE NUMBER S.
14. Suppose we enter this program and then type RUN. Show the first seven
numbers printed by the computer.
10 LET N
20 PRINT N
30 LET N N + 1
50 GO TO 20
RUN
15. The U.S. is going metric, so here is a metric problem. Write a program to
convert feet and inches to centimeters, as indicated by the following RUN.
RUN
Answers to Self-Test
The frame numbers in parentheses refer to the frames in the chapter where the
topic is discussed. You may wish to refer to these for quick review.
1. This erases, or removes, any old program that might be in the computer's
memory. If we don't do this, statements of an old program might be inter-
mingled with statements of the new program, thus causing mysterious and
unpredictable behavior when we try to RUN the new program. (frames 11,
12)
2. Type LIST and press the RETURN key. (frames 13, 14)
3. Type RUN and press the RETURN key. (frames 14, 16)
4. Type a new line numbered 20 and press the RETURN key. The old state-
ment 20 is automatically erased and we may now repiace it. If we type the
line number (and only the line number) of a statement and press RETURN,
the computer deletes from its memory the statement (if any) with that line
number:. (frame 21)
5. Direct statements do no! have line numbers and are executed immediately
after we press RETURN. A statement to be stored must have a line
number; the computer "remembers" it for later execution (at "RUN time"
as we sometimes say). (frame 9)
6. Press the BREAK key. (frames 34-36)
7. ()
a 20 PRINT j(DISTANCE IN ONE TURN IS / ',3.14·0 Q ' mark
uotatlOn ' .
s mlssmg
9. (a) The program does not have a line number 12. Therefore, the
computer cannot execute the statement 40 GO TO 12. Instead, it will
type ERROR- 12 AT LINE 40. (frames 33, 35, 36)
(b) The PRINT statement does not use the same variables assigned values
in the first two statements. (In ATARI BASIC, the computer would
print a 0 (zero) for the PRINT statement.) We probably should have
done one of the following.
18 LET A 7 18 LET X 7
28 LET B 5 20 LET Y 5
38 PRINT A + B 30 PRINT X + Y (frames 1-6)
lS0 LET B =B + x
Ll lcheCk or deposit
old balance
new balance
(frames 47-49)
14. RUN This program illustrates a counting loop. Once you type
1
RUN, the computer will begin counting. It wiU count and
2
3 count and count and keep on counting until the power fails or
4 the computer breaks down or you press BREAK to stop the
5
6
computer. (frames 47-49)
7 AND SO ON.
80 ATARI BASIC
(frame 42)
CHAPTER FOUR
Decisions Using
IF -THEN Statements
By now you have probably gotten the idea that a computer only does what you
very specifically tell it to do. So how can a computer ever decide anything on its
own? Well, it can't decide "on its own," but it can make certain comparisons
that you instruct it to make, and then either execute or skip other statements in
the program according to whether the comparison you specified is true or false.
The statement that you use to set up such comparisons is the IF-THEN state-
ment. (As you will see, IF-THEN is actually a whole family of statements.) When
you finish this chapter, you will be able to:
• use the IF-THEN statement;
• use the following comparisons in an IF-THEN statement:
Symbol Meaning
< less than
> greater than
equal to
<> not equal to
>= greater than or equal to
<= less than or equal to
• use the RND function to generate random numbers;
• use the INT function to "drop" the fractional part of a number;
• use the INT and RND functions together to generate random digits;
• use the ON-GOTO statement to selectively branch to various statements
in a program;
• use a colon (:) to separate multiple statements in the same line as a
means to organize your programs and to save computer memory space;
• use multiple statements per line to increase the usefulness of IF- THEN
statements.
81
82 ATARI BASIC
Follow this
path if the Follow this path .if the
condition is condition is TRUE.
FALSE.
(a) TRUE. The computer will print the message: YOUR NUMBER IS
POSITIVE.
(b) FALSE. The computer, will not print the message.
(c) FALSE. The computer will not print the message.
3. The symbol > in an IF-THEN comparison means "is greater than." The
symbol = means "is equal to." You can guess that the symbol < used in an IF-
THEN comparison means ____________
RUN
WHEN I ASK, YOU ENTER A NUMBER AND I
WILL TEL L YOU WHETHER YOUR NUMBER IS
POSITIVE, NEGATIVE OR ZERO
WHAT IS YOUR NUMBER? -7 ~.~-------- This message printed by line 160.
YOUR NUMBER IS NEGATIVE
WHAT IS YOUR NUMBER? ~.~--------- And so on. (Do you ~emember how to
stop a program waiting at the INPUT
question mark?)
5. In running the program from frame 4, the computer executes lines 100
through 130 once, since they are "outside the loop." Lines 135 through 180 are
included in the loop and are executed for each value of X supplied by the user
after an INPUT question mark. Suppose the user runs the program and types 13
after the INPUT question mark, then presses RETURN. This assigns the value 13
to the variable X. Now look back at the program. Since X = 13 (13 is the value
of X), the condition in line 150 is TRUE and the conditions in lines 160 and 170
are FALSE. So, the computer will print the message in line 150, but will not
print the messages in lines 160 and 170.
(a) Suppose X = -7. The condition in line 160 is (TRUE or FALSE)
_ _ _ _ _ _ _ _ _ _ _ and the conditions in lines 150 a'n d 170 are
(TRUEorFALSE) _________________
(a) TRUE; FALSE. The computer will print the message in -line 160, but will
not print the messages in lines 150 and 170.
(b) line 170; lines 150 and 160. The computer will print the message in line 170,
but will not print the messages in lines 150 and 160.
6. The program on the following page compares two numbers, A and B, and
prints an appropriate message. Complete lines 160 and 170 so that the program
will RUN as shown.
DECISIONS USING IF-THEN STATEMENTS 85
170
180 GOTO 120
RUN
WHEN I ASK, ENTER VALUES FOR A AND B.
A =7 1
B =7 2
A IS LESS THAN B
A =7 7
8 =7 2
A IS GREATER THAN B
A =7 55
8 =7 55
A IS EQUAL TO 8
A =7
Tells the computer: If the value of X is equal to -1, then go to line 230.
If the value of X is not equal to-1, the computer
continues in usual line number order.
The statement could be almost any BASIC statement. The condition is usually a
comparison between a variable and a number, between two variables, or between
two BASIC expressions. On the following page is a handy table of comparison
symbols.
86 ATARI BASIC
(a) M> 10
(b) Z < = A "2 or Z <= A *A
(c) X < >Y
(d) 3*P = Z *Q
8. So far you have seen two members of the IF-THEN family of statements.
IF-THEN PR I NT (message in quotes)
IF-THEN GOTO (line number)
The second IF-THEN statement shown above tells the computer to branch
or GOTO the line number given, if the comparison is true. You may omit the in-
struction GOTO following THEN, and just specify the line number. Show how
you could write the following statement in shorter form.
190 IF X = - 1 THEN 2 30
IF Y = 3 THEN LET X = 1
IF X* X + Y*Y<25 THEN PRINT "YOUR GUESS IS INSIDE THE CIRCLE"
It's your turn to practice writing the IF-THEN statements that will assign
values if the comparison is true. From the following descriptions, write an IF-
THEN statement.
(a) If A does not equal B then assign the new value 10 to variable B.
(b) IF H is more than 100, have the computer say "OOPS! TOO MANY
HOURS."
(c) If X is greater than 2 times Y, then print the message X IS MORE THAN
YDOUBLED. _________________________________________
(d) If Z does not equal -1, then INPUT a new value for X.
(a) IF A <= 10 THEN 100 (The 001'0 is optional and usually omitted).
(b) IF A < 2 *s THEN T = T+ 1 or
IF A < 2*S THEN LET T = T+1
(The LET may be included or omitted.)
(c) IF X>2*Y THEN PRINT " X IS MORE THAN Y DOUSLED"
(d) IF Z<>-l THEN INPUT X
11. One common use of the IF-THEN statement is to recognize a signal called
a "flag" that terminates one process and begins another. Here is another version
88 ATARI BASIC
of the "World's Most Expensive Adding Machine" which you first encountered
in Chapter 3, frame 45 . You may wish to review that frame before plunging on-
ward.
100 REM***WORLD'S MOST EXPENSIVE ADDING MACHINE
110 PRINT "I AM THE WORLD'S MOST EXPENSIVE ADDING"
120 PRINT ":MACHINE. EACH TIME I TYPE' X=7' YOU"
130 PRINT "TYPE A NUMBER. WHEN YOU ARE FINISHED,"
140 PRINt "TYPE -1 AND I WILL TYPE THE TOTAL"
150 PRINT "OF YOUR PREVIOUS NUMBERS."
160 T =0
170 PRINT
180 PRINT "X =" I
Lines 170 through 220 are a OOTO loop. However, if
190 INPUT X someone types -1 for the value of X, line 200 will
2mm IF X = -1 THEN 23m cause the computer to jump out of the loop and go to
210 T = T + X
22m GOTO 170 line 230.
X =7 6.95
X =7 .47
X =7 8.49
X =7 3.06
The flag used in our program is -1; statement 200 checks each and every in-
put value of X and, if it is -1, causes the computer to jump out of the loop to
line 230. Lines 230 through 260 print the total (T) and then cause the computer
to jump back to line 110 and start over.
Any unusual number that will not be used as a normal INPUT value could
be used as a flag.
Modify the program so that, instead of using -1 as the flag, we use 999999
as the flag. You will have to change lines 140 and 200
140 ___________________________________________________
200 _____________________________________________________
DECISIONS USING IF-THEN STATEMENTS 89
12. In our program in frame 11 we first used -1 as the flag. This may not be a
good idea if some of the values we wish to use are negative. For example, here
are temperatures recorded during one cold week in Minneapolis, Minnesota.
S M T W T F S
10 3 -9 -15 -23 -25 -30
In this case, using 999999 as the flag would prevent confusion between a
temperature of -1 and an end-of-data flag of -1.
With a few changes, we can modify the program in frame 11 and obtain a
program to compute the mean, or average, of a set of numbers. The formula for
determining the mean of a set of N numbers is as follows.
In the Friendly "Mean" Progam, we use the variable T for the total (sum)
of the numbers, and N for the number of nu~bers. Complete the program.
RUN
I WILL COMPUTE THE MEAN OF NUMBERS .
WHEN I TYPE ' X =7' TYPE A NUMBER. WHEN
FINISHED ENTERING NUMBERS, TYPE 999999.
X =7 10
X =7 3
X =7 -9
X =7 -15
X =7 -23
X =7 -25
X =7 -39
14. You have helped us write several computer programs using IF-THEN com-
parisons. Now it is time for you to do a solo flight and write a program on your
own. Think carefully about the use of IF-THEN comparisons, and what the com-
puter will do if the conditions are TRUE or if they are FALSE. On the following
page is a RUN of the program we want you to write.
DECISIONS USING IF-THEN STATEMENTS 91
RUN
INPUT A NUMBER AND I WILL TELL YOU IF
IT IS U3 OR LESS, OR OVER 133.
YOUR NUMBER? 99
YOUR NUMBER IS 133 OR LESS.
YOUR NUMBER?
15. Soon we will enter the fun-filled realm of computer games. But first, you
should learn about random numbers and the unpredictable BASIC function
known as RND.
Random numbers are numbers chosen at random from a given set of
numbers. Many games come with dice or a spinner or some other device for
generating random numbers. Roll the dice; they come up 8. Move 8 spaces.
Functions are automatic features of BASIC that you use to perform special
operations. These functions are like built-in programs; most of them could be
replaced by a program or segment of a program. However, computers are called
upon often enough to do the operations accomplished by these functions that it is
worthwhile to "build them in" to the computer language.
BASIC provides a special function, called the RND function, that generates
numbers that seem to be chosen at random, like picking numbers out of a hat.
The program on the following page shows the use of the RND function. We
92 ATARI BASIC
show you the program (enclosed in a box) and two different runs of that pro-
gram. We interrupted the first RUN by pressing BREAK, then typed RUN again.
10 PRINT RND(I)
20 GOTO 10
RUN RUN
0.2772S64208 S.4468536376
0.0455932617 0.212661"7431
0.6158294677 0.7585601806
S.0782165524 S.5531005859
S.2481689453 S.1878662109
S.1948242187 0.S785217285
0.9686126708 6.83401722E-03
S.5149536132 S . 2S4711914
0.8614654541 STOPPED AT LINE 10
1
S. 3824920654 ~
STOPPED AT LINE 10
Two runs of the program are shown. Are the lists of random numbers in
the two runs the same? _ _ _ __
No. In fact, don't expect to enter our program into your computer, type RUN,
and get either list. That's the idea of random numbers. They are, well, random!
16. The statement 10 PRINT RND(I) causes the computer to produce a dif-
ferent list of random numbers each time the program is RUN. The RND function
generates numbers that appear to be chosen at random. On our ATARI com-
puter, the RND function is written RND(I).
We use the number 1 in parentheses. However, any positive number is OK,
even numbers with decimal fractions! On our computer, a positive number in
parentheses following RND will cause the computer to produce a different list of
random numbers each time.
Examine the random numbers in frame 15.
(a) Is any number less than zero (negative)? _ _ _ __
(b) Is any number equal to zero? _ _ _ __
(c) Is any number greater than one? _ _ _ __
(d) Is any number equal to one? _ _ _ __
DECISIONS USING IF-THEN STATEMENTS 93
(e) From the evidence, it appears that random numbers produced by the RND
function are _ _ _ _ _ _ _ zero and _ _ _ _ _ _ _ one.
(a) no
(b) no
(c) no
(d) no
(e) greater than; less than. However, we haven't shown much evidence-only a
few random numbers. We suggest you run off a bunch of random numbers
on your computer in order to get more evidence. (But remember! Evidence
is not proof.)
17. It's true that random numbers produced by the RND function are greater
than zero and less than one. Another way to say it: random numbers produced
by the RND function are between 0 and 1. Or, in still another way: 0 < RND(l)
<1.
However, random numbers between 0 and 1 are not always convenient.
Sometimes we would like random digits (the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, and
9) or random integers (whole numbers) from 1 to 100. Below is a RUN in which
the computer acts as a teaching machine to teach one-digit addition to children.
RUN
Undoubtedly, you are anxious to see the program. Patience! Let's build it,
piece by piece. First, how do we generate random digits?
The random numbers produced by the RND function are uniformly
distributed between 0 and 1. That is, they are "spread evenly" between 0 and 1.
Each random number is about as likely (or unlikely) to occur as any other ran-
dom number.
RND(l) is between 0 and 1, but is never 0 or 1. Therefore, lO*RND(1) is
between 0 and ______
18. Below is a program to print random numbers between 0 and 10. We show
you two RUNs to remind you that you get a different list each time.
HI PRINT 10*RND( 1)
20 GO TO 10
RUN RUN
7."''''8'''5664 1.17"'19653
8.1372"'7"'3 5.948"'2856
2.74749755 1.483612"'6
6."'7574462 3.89282225
3.9"''''299'''7 9.85137939
1.66"'''''''366 4.83612061
4.94537259 2."'3781128
"'.382461547
6.671295166
4.277496337 STOPPED AT LINE 1'"
Each random number in the runs is greater than zero and less than ten.
Each random number can be thought of as having an integer part to the left of
the decimal point and a fractional part to the right of the decimal point.
The integer part of 7.00805664 is 7 and the fractional part is .00805664.
The integer part of 0.382461547 is zero (0) . The fractional part is
.382461547.
(a) What is the integer part of 1.66oo0366? _ _ _ _ _ _ _ _ _ _ _ __
The fractional part? _ _ _ _ _ _ _ _ _ __
(b) What is the integer part of 4.83612061? _ _ _ _ _ _ _ _ _ _ _ __
The fractional part? _ _ _ _ _ _ _ _ _ __
(a) 1; .66000366
(b) 4; .83612061
19. Now you write three short programs to print random numbers in the ranges
specified.
(a) between 0 and 100
DECISIONS USING IF·THEN STATEMENTS 9S
(a) Iii! PRINT 1 Ii! Ii! *RNO( 1) (b) Iii! PRINT 51i!*RNO(I)
21i! GO TO Iii! 21i! GO TO Iii!
RUN RUN
73.331i!8125 28.9513492
12.7318966 35.5718117
37.9114539 44.6959996
87.88851i!27 11.4345026
2.11i!936991 6.48894357
29.1256433 15.2396994
69.5791i!792
4.261i!22968
(~ 19 PRINT 6*RNO(I)
21i! GOTO 10
RUN
2.691i!18182
1.31444039
3.24957255
9.1153584267
0.92287558154
2.90792334
5.72199528
2.93216994
20. For each random number between 0 and 10, the integer (whole number)
part is a single digit. Wouldn't it be nice if we could direct the computer to delete
the fractional part and keep the integer part?
Well, as you may suspect, we can. BASIC has another clever and useful
function called INT. Here are some examples.
(a) 7
(b) 0
(c) 2
(d) 9
21. Caution. INT works as shown 'in frame 20 only for positive numbers or
zero.· In general, INT(X) computes the greatest integer less than or equal to X.
For example:
INT (3.14) = 3 but INT(-3.14) =-4
INT (7) = 7 and INT (-7) = -7
INT (.999) = 0 but INT (-.999) = -1
For positive numbers, or zero, INT (X) computes the integer part of X. In
a program, the value assigned to X will be substituted for X in the INT paren-
theses when the program is RUN. The computer then performs the INT function
on the numerical value. Of course, any variable could be used instead of the let-
ter X, as long as it has been assigned a value earlier in the program.
You be the computer and· show what you will print when your human tells
you to RUN the following programs.
(a) Ita X=15.77 (b) 113 A=99. 999 (c) 113 F=9B.6
2ta PRINT INTCX) 213 PRINT INTCA) 213 PRINT INTCF)
RUN RUN· RUN
22. Instead of a number, we can write a variable, a function, or any BASIC ex-
pression in the parentheses that follow the word INT.
INT( ) .
L Any BASIC: number, variable, function or expression here
You should be able to distinguish between values, variables, expressions,
junctions, and strings. To show that you can, identify each of the following with
one of the words given above in italics.
(a) X
(b) X*2
DECISIONS USING IF-THEN STATEMENTS 97
(c) RND(1)
(d) "A + B IS AN EXPRESSION"
(e) 22
(0 1+ 1
(g) INT(52.88)
(h) " "
(i) 4*(3 +X)
U) Which of the above could be used in the parentheses of a function such as
INT or RND? _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __
(a) variable
(b) expression
(c) function
(d) string
(e) value
(0 expression
(g) function
(h) string
(i) expression
(j) a, b, c, e, f, g, and i
23. Remember, the general form for the INT function is as follows.
INT( )
L Any BASIC number, variable, function or expression here
The program on the following page causes the computer to generate and
print random digits. Digits are the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, or 0
to 9, inclusive. Note the "inclusive" means it includes both 0 and 9, as well as
the digits between 0 and 9.
98 ATARI BASIC
10 PRINT INT(10*RND(I»
20 GOTO 10
RUN RUN
5 4
3 6
2 3
e 9
6 4
3 lil
4
2
7
0; 9
24. Now write a short program to print random integers (whole numbers) from
o to 19, inclusive.Our RUN looks like this.
RUN
3 . . . I----Write
. your program here.
10
17
5
9
19
7
2
25. What if we want random integers from 1 to 20, inclusive, instead of from 0
to 19? Well, that's easy. Simply add a 1 to the random integer. You could do it
in two ways.
10 PRINT INT(20*RND ( I)+I)
20 GOTO 10
DECISIONS USING IF-THEN STATEMENTS 99
R UN
14
20
20
16
19
7
20
2
15
The" + 1" can be added to the random number either before or after tak-
ing the integer part of 20*RND(l). The resulting random integer will be the same
in either case.
Now you write three simple programs to print a list of random integers.
(a) from 1 to 1~, inclusive (b) from 1 to 100, inclusive
----------
(a) 10 - PRINT INT( 1"*RND( 1) )+1 or 1" PRINT INT( 1" * RND( 1)+1)
2" GO TO 1" 2" GoTo 1"
(b) 1" PRINT INT( 1""*RND( 1) )+1 or 10 PRINT INT( 1""*RND( 1 )+1)
2" GO TO 1" 2" GoTo 1"
(c) 1" PRINT INT( 12*RND( 1) )+1 or 1" P R I NT INT (12*RND( 1 )+1)
2" GoTO 1" 20 GO TO 1"
(a) The function INT (6*RND(l)) will generate what random digits? _ _ __
100 ATARI BASIC
(a)
(b)
°
+5
to 5 inclusive, that is, 0, 1, 2, 3, 4, and 5, a total of 6 digits.
0 to 5
+5 +5
5 10
(c) +11 0 to 5
+11 +11
11 16
27. Look back at the RUN in frame 17 before we continue. Now we are ready
to build the addition practice program. Here is the first section of the program
that produced the RUN .
Line 320 is something new. The statement PRINT A; " + " ; B; " = " ;
tells the computer to print the value of A, then print +, then print the value of
B, then print =. These items are separated by semicolons. The fourth semicolon,
at the very end, tells the computer not to go to the next line down on the CRT.
In other words, it tells the computer to stay where it stopped printing.
Do you remember? The INPUT statement causes the computer to print a
question mark. This question mark will be printed on the same line as the in-
formation from the PRINT statement, because of that semicolon at the end of
line 320.
DECISIONS USING IF-THEN STATMENTS 101
For example, if A = 7 and B 5, then lines 320 and 330 will cause the
computer to print the following.
7 + 5 = ?
from line 32~ :OJ I L from line 330 (INPUT question mark)
3 + 4 = ? (This, of course, is the random problem which helps the eager young
learner practice addition.)
29. After the learner types an answer and presses the RETURN key, the com-
puter assigns the answer value to variable C and continues.
Note how we are using REM (REMARK) statements to tell something
about each piece of the program. This doesn't help the computer, but it does
help people read and understand how the program works.
400 RE M*** I S THE ANSWER CORR E CT ?
410 IF C=A+B THE N 610
If the student's answer (C) is correct, the computer will go to line _______
610 (If the answer is not correct, the computer continues in regular line number
order.)
30. If the student's answer is not correct, then the IF-THEN condition is false.
The computer next does the following as it continues on in the program in line
number order.
500 RE M***ANSWER I S NO T CORRECT
510 P R I NT " HMMM .. . I GET A DI FF E REN T ANSWER ."
520 GOTO 310
Note: In a previous book, we had the computer type YOU GOOFED. TRY
102 ATARI BASIC
AGAIN. Now that we know more about learners, we wish to make the computer
seem friendly and compassionate, rather than nasty and authoritarian. Remember
that what the computer "says" depends on the person who writes the program,
and not the machine itself.
31. Review frame 29, which shows lines 400 and 410 of the program. If the
learner's answer is correct, line 410 causes the computer to go to line 610.
6SS REM***ANSWER IS CORRECT
61S PRINT "RIGHT ON •.. GOOD WORK!!!"
62" GOTO 21"
The computer generates a new problem (new values for A and B) and prints the
new problem.
Change line 210 so that the value of A is a random integer from 0 to 19, in-
clusive, instead of 0 to 9.
21" LET A =
33. Careful on this one! Change line 220 so that the value of B is a random in·
reger from 10 to 20, inclusive.
22Z -LET B = _______________________
16 + 14 =7 3Z
RIGHT ON ... GOOD WORK!!!
16 + 15 =7 31
RIGHT ON ... GOOD WORK!!!
4 + lZ =7 15
HMMM ... I GET A DIFFERENT ANSWER.
4 + lZ =7 14
RIGHT ON .•. GOOD WORK!!!
6 + 16 =7 And so on.
34. When the learner's answer is correct, the computer always prints: RIGHT
ON ... GOOD WORK!!! To relieve the monotony, let's modify the program so
that the computer selects at random from three possible replies to a correct
answer. The changes are in the portion of the program beginning at line 600.
1,2, and 3 (Not 0, 1, and 2, because we added + 1 at the end of line 610.)
36. We made the changes in response (frame 34) from the original program
(frame 32) and ran the modified program. Here is what happened.
RUN
9 + 7 =? 16
THAT'S GREAT! KEEP IT UP!
6 + 3 =? 9
YOU GOT IT! TRY ANOTHER.
8 + 2 =? 10
THAT'S GREAT! KEEP IT Up!
5 + 9 =? 14
RIGHT ON ••. GOOD WORK!!!
o +. 7 =1 7
RIGHT ON ... GOOD WORK!!!
8 + 5 =? 14
HMMM ••. 1 GET A DIFFERENT ANSWER.
8 + 5 =1
If the learner's answer is incorrect, the computer always prints: HMMM
.. I GET A DIFFERENT ANSWER. Modify the program in frame 32 so that,
for an incorrect response, the computer selects randomly one of the two follow-
ing responses.
5 ll'l LET R=
52 0 IF
DECISIONS USING IF-THEN STATEMENTS lOS
53" IF
54" PRINT
55" GO TO 31"
56"
Again, we are trying to make 'the computer seem friendly and helpful, in-
stead of harsh and unforgiving.
If R=l J J=2 tf
If R=3
With this change, the program segment in frame 34 can be rewritten as
follows.
(c) In this case, which line will the ON R GOTO statement send the computer
to? ___________
(a) 1, 2, 3
(b) 2, INT(3*.34319)+1 INT(l.02957) + 1 1 + 1 2
(c) line 650
520 ON
540 PRINT "H MMM. .. 1 GE T A DIFFER ENT ANSWER ."
550 GOTO 310
560 PR I NT "T RY A 0 I F FE RENT ANS WER. GOOD L UCK ~ "
570 GOTO 3 10
39. And now, a wonderful space saving method! Here is the first part of our
addition practice program, featuring multiple statements per line.
100 REM***ADDITION PRACTICE PROGRAM
200 REM***GENERATE RANDOM NUMBERS, A AND B
210 LET A=INT(20*RND(I»
220 LET B=INT(II*RND(I»+10
300 REM*·*PRINT PROBLEM AND ASK FOR ANSWER
310 PRINT I PRINT A I " + " I B ; " " ; : INPUT C
400 REM** *1 S THE ANSWER CORRECT? Lines 540 and 560
410 IF C=A+B THEN 600
500 REM***ANSWER IS NOT CORRECT each have 2 state-
510 LET R=INT(2*RND(I»+1 mentsperline.
520 ON R GO TO 540,560
540 PRINT "HMMM ••• 1 GET A DIFFERENT ANSWER." I GOTO 310
560 PRINT "TRY A DIFFERENT ANSWER. GOOD LUCK!" I GO TO 310
DECISIONS USING IF-THEN STATEMENTS 107
In the program, line 310 contains three statements, and lines 540 and 560
each contain two statements. In a line that contains more than one statement,
what symbol, or character, is used between statements?
a colon (:)
rcolon COlon.
310 PRINT,PRINT AI" +" IB; "= ""INPUT C
/
cOlon~
40. For readability, we usually put a space on each side of the colon. However,
this is not necessary. For example, we could have typed line 310 as shown below.
310 PRINT,PRINT A; "+ "I B; "= "I , INPUT C
However, we will include spaces to make the statements easier to read. The
computer understands either way.
We didn't show you the entire program in the last frame, because we want
you to rewrite the part of the program beginning with line 600 as shown in frame
37, using mUltiple statements in those lines where it makes sense to group
statements to work together.
630
640
650
41. Here is the computer game we promised you earlier in the chapter. Note the
use of multiple statements per line in lines 150, 160, 170, and 180.
100 REM***GUESS MY NUMBER - A COMPUTER GAME
110 LET X=INT(100*RND(I»+1
120 PRINT
130 PRINT "I'M THINKING OF A NUMBER FROM 1 TO 100"
140 PRINT "GUESS MY NUMBER!"
150 PRINT : PRINT "YOUR GUESS" ; • INPUT G
160 IF G < X THEN PRINT "TRY A BIGGER NUMBER." : GOTO 150
170 IF G > X THEN PRINT "TRY A SMALLER NUMBER." : GOTO 150
180 IF G '" X THEN PRINT "THAT'S IT! YOU GUESSED MY NUMBER! " • GOTO 110
RUN
YOUR GUESS? 50
TRY A BIGGER NUMBER.
YOUR GUESS? 75
TRY A SMALLER NUMBER.
YOUR GUESS? 68
TRY A BIGGER NUMBER.
YOUR GUESS? 72
TRY A BIGGER NUMBER.
YOUR GUESS? 73
THAT'S IT! YOU GUESSED MY NUMBER!
Line 110 tells the computer to generate a random number and store it in
variable X. This number will be a(n) _ _ _ _ _ _ _ _ _ from _ _ _ __
to _ _ _ __
42. Multiple statements per line give us another nice shortcut. You'll recall that
if the comparison in an IF-THEN statement is false, the computer skips the rest
of the statement and goes on to the next line in the program in line number
order. The nice thing is that the computer will also skip any statements that
follow a false IF-THEN comparison if they are on the same multiple statement
line. The rest of the statements on a line will be executed if the IF-THEN com-
parison is true. Therefore, in one line, you can have the computer do more than
one thing if an IF-THEN comparison is true. Consider line 170 as it is shown on
the following page. The computer will both PRINT and GO TO if the condition
DECISIONS USING IF-THEN STATEMENTS 109
170 IF G > x I TRUE :> THEN 1PR I NT "TRY A SMALLER NUMBER. " GoTo 150 1
t
il
. A
L
S All of this is done if the condition is TRUE.
E None of this is done if the condition is FALSE.
43. Line 150 causes the computer to print YOUR GUESS? When the player
types a guess, the computer stores it in variable G. Lines 160, 170, and 180 com-
pare the guess G with the random number X. Let's look at line 160.
If the guess G is less than the number X, the computer will print the
message TRY A BIGGER NUMBER and will then GOTO line 150 to ask for
another guess. However, if G is greater than X or equal to X, neither the PRINT
nor the GOTO will be done. Here is another way to "picture" that idea.
160 IF G < x I TRUE :> THENIPRINT "TRY A BIGGER NUMBER. " : Go T o 150
1
t
il All this is done if the condition is TRUE.
N one of it is done if the condition is FALSE.
So, ifG < X is FALSE, the computer goes on to line 170. Describe what
happens when the computer executes line 170.
(a) the computer will print the message TRY A SMALLER NUMBER and then
OOTO line 150 for another guess
(b) neither the PRINT nor the OOTO will be done
44. If O<X is FALSE and 0> X is FALSE, the computer will finally arrive at
line 180. Here is a picture of the entire process of arriving at line 180.
160 IF G < x I TRUE :> THEN PR I NT " TRY A BIGGER NUMBER " : GOTO 150
1 70 IF
il
G > x TRU E :> THEN PR I NT "TRY A SMALLER NUMBER " GOTO 150
1 B0 IF
il
G = x TRUE :> TH~N PRINT " THAT ' S IT ! YOU GUESSED MY N UMBER":GOTO 110
Suppose the player has guessed the number. Therefore, O<X is FALSE,
O>X is FALSE, and, of course, 0 = X is TRUE. What happens?
. The computer prints the message THAT'S IT! YOU OUESSED MY NUMBER,
then goes to line 110, where it is directed to "think of" a new number and start
the game again.
DECISIONS USING IF-THEN STATEMENTS 1lI
45. For very young children, we may wish to reduce the range of integers
generated by the statement that uses the RND functions (line 110). For example,
instead of generating a number from 1 to 100, we may wish a number from 1 to
25. Conversely, advanced players may prefer a larger range, say 1 to 1000.
(a) Modify lines 110 and 130 so that the range is 1 to 25 .
110 __________________________________________________
130 __________________________________________________
(b) Modify lines 110 and 130 so that the range is 1 to 1000.
110 ____________________________________________________
130 __________________________________________________
Now the range is 1 to R, where R is assigned a value in line 105, then used
in lines 110 and 130. So, to change the range, you simply change line 105. Sup-
pose you want the range to be 1 to 500. What do you write for line lOS?
105 _____________________________________
47. Using IF-THEN statements and multiple statements per line, modify and
rewrite your NUMBER SIZE PROGRAM from frame 14 so that it will RUN like
the one on the following page.
112 ATARI BASIC
RUN
INPUT A NUMBER AND I WILL TELL YOU IF
IT IS LESS THAN 1.1,,' , BETWEEN lIH~ AND
lIiHillIJ , OR OVER 111J1IJ1IJ.
YOUR NUMBER? 99
YOUR NO. IS LESS THAN 111J11J
YOUR NUMBER?
48. Now we want you to put your accumulated knowledge of BASIC to work
and write a program to provide practice for a person learning or reviewing the
"times table," that is, multiplication from 0 times 0 to 12 times 12. Of course,
we are calling this program COMPASSIONATE MULTIPLICATION PRAC-
TICE. Study the RUN and our notes, then build your program. If you have a
computer handy to use, check your own program before looking at our way of
doing it.
Generate a problem, using random numbers from 0 to 12. Print the prob-
lem and ask for an answer. Compare the player's answer with the correct answer.
If th.e answer is smaller than the correct answer, print TRY A BIGGER
NUMBER and repeat the problem.
DECISIONS USING IF-THEN STATEMENTS 113
If the answer is bigger than the correct answer, print TRY A SMALLER
NUMBER and repeat the problem.
If the answer is correct, tell the player that she or he has typed the cor-
rect answer.
Our program uses three different replies to a correct answer. These are
chosen at random from the following three possibilities.
THAT'S IT!
CORRECT ANSWER
GOOD WORK! KEEP IT UP
RUN
2 x 4 =7 8
GOOD WORK: KEEP IT UP:
2 x 2 =7 4
GOOD WORK : KE EP IT UP:
2 x 8 =7 15
TRY A B I GGER NUMBER.
2 x 8 =7 16
CORRECT ANSW ER :
Lei x 1 =7 HI
CORREC T ANSW ER:
10 x 9 =7 100
TR Y A SMA LLER NUMBER.
10 x 9 = ? 90
GOOD WORK: KEEP IT UP :
11 x 7 =7
49. We mentioned in Chapter 2 that some of the older versions of BASIC re-
quired an END statement as the last statement in a program. Most versions of
BASIC for home computers do not need this final END. However, the END
statement, and its relative, the STOP statement, can be handy for terminating a
program in some place other than the last line-numbered statement.
It is often handy to be able to end or stop a program as a result of an
IF-THEN decision. END or STOP can be the condition to fulfill after THEN if
the condition is TRUE. Examples:
2~ IF X 1~ THEN STOP
2~ IF X = 1~ THEN END
With ATARI BASIC, using STOP will cause the computer to type a message, as
shown below, giving the line number where the STOP statement was encountered
and the RUN ended.
STOPPED AT 2~
•
However, a RUN that ends when an END statement is encountered just gives the
standard READY.
In a multiple-statement line with IF-THEN, END or STOP could be used
as below:
12~ IF Y • Q = 11lJ~ THEN PRINT "THAT'S ALL, FOLKS" I END
12~ IF Y • Q = 11lJ~ THEN PRINT "THAT'S ALL, FOLKS" : STOP
At the end of a math drill or game playing program, you could use the following
approach.
In a counting loop program, insert a line to tell the computer to quit count-
ing when F is greater than 6.
1~ LET F = 1
2~ ______________________
3~ PRINT "F ="; F
4~ LET F = F + 1
5~ GOTO 2~
SELF-TEST
Try this Self-Test, so you can evaluate how much you have learned in Chapter 4.
2. Describe each IF-THEN statement in words. That is, describe what the
statement tells the computer to do.
(a) IF G = X THEN 2mm ________________________________________
(c) IF N <> INT(N) THEN PRINT "N IS NOT AN INTEGER." I GOTO 21m
A
(d) IF AA2+SA2=C 2 THEN PRINT "YES. IT IS A RIGHT TRIANGLE."
4. Complete the following program to tell how many years are needed to
"double your money." Use multiple statements in line 190.
RUN
IF YOU TYPE THE AMOUNT OF PRINCIPAL AND THE
INTEREST RATE PER YEAR, I WILL SHOW YOU HOW
MANY YEARS TO DOUBLE YOUR MONEY.
5. What will be the result of running the following program? Show the RUN.
1m LE~T K=1
2m PRINT k
3m LET K=K+1
40 IF K<=5 THEN 20
RUN
DECISIONS USING IF-THEN STATEMENTS 117
(b) 2*RND(I)
(d) INT(2*RND(l» + 1
(f) 3.14159*RND(l)
118 ATARI BASIC
RUN
HEADS
HEADS
TAILS
TAILS
HEADS
HEADS
HEADS
HEADS
TAILS
HEADS
TAILS
TAILS
HEADS
HEADS
Answers to Self-Test
The frame numbers in parentheses refer to the frames in the chapter where the
topic is discussed. You may wish to refer to these for quick review.
1. is equal to
< is less than
> is greater than
< = is less than or equal to
> = is greater than or equal to
<> is not equal to
(frame 7)
2. (a) If the value of G is equal to the value of X, go to line 200. Otherwise
(G = X is FALSE), continue in the usual line number order.
(frames 1-8)
(b) If the value of X is greater than or equal to zero, increase the value of
C by 1. Otherwise. (X> = 0 is FALSE), do not execute the LET por-
tion. In either case, continue in regular line number order. (frame 10)
(c) If N<>INT(N) is TRUE, this means that the value of N is not an
integer. In this case, print the string N IS, NOT AN INTEGER and
then go to line 210. However, if N is an integer, N<>INT(N) will be
FALSE. In this case, neither the PRINT nor the GOTO will be ex-
ecuted and the computer will simply continue in regular line number
order. (frames 1-8, 20-23, 42)
(d) If the value of A squared plus B squared is equal to the value of C
squared, print the string YES, IT IS A RIGHT TRIANGLE.
DECISIONS USING IF-THEN STATEMENTS 119
(frames 1-10)
4. 190 IF A<2*P THEN LET N = N + 1 • GOTO 18.0
In other words, if the newly computed amount A is still less than twice the
original principal P, increase the year N by 1, and go back to line 180 to
compute a new amount A. (frames 9-10)
5. RUN The PRINT statement is inside the loop. Therefore, it is done
every time.
2
3
4
5
(frame 8)
6. RUN This time, the PRINT statement is outside the loop. It is done
5 only once, after the loop has been completed. (frame 8)
7. (a) Numbers between 0 and 1. Each random number is greater than zero,
but less than one. This answer is also acceptable:
O<RND(I)<l. (frames 16, 17)
(b) Numbers between 0 and 2. Each random number will be greater than
0, but less than 2. Also: 0<2*RND(I)<2. (frames 17-19)
(c) o or 1 No other values are possible. (frames 20, 23-26)
(c) 1 or 2 (frames 20, 23-26)
(e) -1, 0, or 1 (frames 20, 23-26)
(f) Numbers between 0 and 3.14159. Each random number is greater than
0, but less than 3.14159. Also: 0<3.14159*RND(I)<3.14159. Since
3.14159 is an approximation to TI, we might also say, although
somewhat imprecisely, that these numbers are between 0 and
TI. (frames 20, 23-26)
8. 110 LET C = INTC2*RND(1» (frames 26, 27)
CHAPTER FIVE
This chapter is designed to give you practice using the BASIC statements and
programming skills you have learned so far and to add to your bag of program-
ming tricks. You will be able to extend your understanding of the capabilities of
the PRINT statement, in order to better control the output of your programs and
write more efficient instructions or programming code. In addition, you will
learn two frequently used statements that always work together to assign values
to variables: the READ and DATA statements. When you finish this chapter,
you will be able to:
1. You've seen how LET statements and INPUT statements can be used to
assign values to variables. (We hope that you have used them on your computer,
too.) A third method uses two statements in combination, READ and DATA, to
assign values to variables.
120
READ AND DATA WORK TOGETHER 121
1.0 READ X
2.0 P RINT " THIS TIME THROUGH THE LOOP, X " ; X
3.0 GOTO 1.0
4.0 DATA 1.0, 15, 7, 3.25, II
RUN (
THIS TIME THROUGH THE LOOP, X 1.0
THI S TIME THROUGH THE LOOP, X 15
THIS TIME THROUGH THE LOOP, X 7
THIS TIME THROUGH THE LOOP , X 3 . 25
THIS TIME THR OUGH THE LOOP, X II
ERROR- 6 AT LINE 1.0
This statement 10 READ X tells the computer to READ one value from the
DATA statement, and assign the value to the variable X. Every time the READ
statement is executed (each time through the loop) , the computer reads the next
value from the DATA statement, and assigns the new value to the variable X.
The computer keeps track of each value as it is read out, in effect, moving a
pointer across the items in the DATA statement, one notch at a time .
How many values are in the DATA statement? _ _ __
2. As the computer executed the program, each time through the loop it read
and printed one value from the DATA statement. Then on the sixth trip through
the loop, it tried to find still another number. Since it couldn' t find another
number to read from the DATA statement, the computer printed ERROR- 6 IN
LINE 10 which means "Out of Data error in line 10." It isn't really an error. It
just informs you that the computer has used up all the available data, tried to
find more, but couldn ' t.
When the program in frame 1 was RUN, how many times was a new value
assigned to the READ variable?
DATA statements may contain whole numbers, numbers with decimal frac-
tions (such as 3.25 above), numbers in floating point or "E" notation, or
negative numbers.
DATA statements may not contain variables, arithmetic operations, other
functions, or fractions.
This is OK: 991 DATA 3, 8, 2.5
This is NOT OK: 95 DATA 2+3, 1/4, 2/5, 7*8
68 DATA 342,1256,2915,691.25,-412,2,915E8
Your line number may be different. And remember, no commas can be used in
large numbers, such as 1256 above. However, floating point or "E" notation
may be used.
1.0 READ N
20 DATA 123
3.0 PRINT N
yes
5. These DATA statements are not written in correct BASIC. Tell what is
wrong with each one.
(a) 291 DATA, IS, 32, 85, 66 ________________________________________
READ AND DATA WORK TOGETHER 123
213
3a PRINT
40 PRINT "INCHES = "II
5a PRINT "CENTIMETERS ";2.54·1
60 GOTO 2RJ
9RJ DATA I, 8, 12
RUN
INCHES = I
CENTIMETERS 2.54
INCHES = 8
CENTIMETERS 20.32
INCHES = 12
CENTIMETERS = 30.48
ERROR- 6 AT LINE 20
20 READ I
7. Now you write a program to convert ounces to grams. One ounce = 28 .35
grams (rounded to two decimal places). Use a DATA statement to hold the
values in ounces that you want converted to grams. Here is what your program
should print when it is RUN.
124 ATARI BASIC
RUN
OUNCES = 1
GRAMS = 28.35
OUNCES = 13
GRAMS = 368.55
OUNCES = 16
GRAMS = 453.6
ERROR- 6 AT LINE 20
RUN
X = 12
TOTAL SO FAR IS 12
x = 43
TOTAL SO FAR IS 55
x = 33
TOTAL SO FAR IS 88
x = 92
TOTAL SO FAR IS 180
x = 76.25
TOTAL SO FAR IS 256.25
ERROR- 6 AT LINE 120
READ AND DATA WORK TOGETHER 125
RUN
N =7
TOTAL = -89
MEAN = -12.71428571
To RUN this program for a different set of data, simply replace line 910 by
one or more DATA statements containing the new data and the flag 1E97.
(You'll recall from Chapter 3 that to replace a statement, just type the new state-
ment using the old line number.) For example, suppose we wish to use this data:
63,72, 50, 55, 75, 67, 59, 61, 64. Write the DATA statement.
126 ATARI BASIC
10. The following program causes the computer to read numbers from a DATA
statement and print only the numbers that are positive (greater than zero).
Numbers that are less than zero or equal to zero are not printed.
10 READ X
20 IF X>0 THEN PRINT "x = " ; X
30 GDTO 10
40 DATA 3,7,0,~2,5,-1,7,8,0,-3
RUN
X 3
X 7
X = 5 ' Note: 7 is printed twice because it occurs twice in the DATA
X = 7 statement.
X = 8
ERROR- 6 AT LINE 10
(a) 3,7,5,7,8
(b) print the message X = followed by the value of X
(c) 0, -2, -1, 0, -3
(d) Nothing. The PRINT portion of line 20 is not executed and the computer
goes on to line 30.
11. What will the RUN look like for this program?
10 READ X
20 IF X<0 THEN PRINT "x = " IX
30 GOTO 10
40 DATA 3,7,0,-2,5,-1,7,8,0,-3
RUN
READ AND DATA WORK TOGETHER 127
RUN
X = -2
X = -1
X = -3
ERROR- .. ------Did
6 AT LINE 1m ~ you remember this?
12. Complete the following program so that the RUN will occur as shown ..
1" READ X
2"---------------------------------------------
3" GOTO 10
4" DATA 3, 7, '" -2, 5, -1, 7, 8, ", ..,3
RUN
X =" Two zeros are printed because there are two zeros in
X ="
ERROR- 6 AT LINE 1" the DATA statement.
13. For more practice, do each of the following. If possible, try each one on
your computer.
(a) Complete line 20 so that only nonzero numbers are printed.
2" IF _ _ _ _ _ _ _ THEN PRINT "X = " IX
(b) Complete line 20 so that the computer prints numbers that are greater than
or equal to zero.
2" IF _ _ _ _ _ _ _ THEN PRINT "X = " IX
(c) Complete line 20 so that the computer prints numbers that are less than or
equal to 3.
2" IF _ _ _ _ _ _ _ THEN PRINT "X =" IX
(a) X <> " (THE COMPUTER WILL PRINT 3,7,-2,5,-1,7,8, AND -3.)
(b) X >= " (THE COMPUTER WILL PRINT 3,7,0,5,7,8, AND 0.)
(c) X <= 3 (THE COMPUTER WILL PRINT 3,0,-2,-1,0, AND -3.)
14. In BASIC you may have more than one variable following a READ instruc-
tion. On the following page is an example. Use commas to separate the variables.
128 ATARI BASIC
This will cause the computer to take two values from the DATA statement
and assign them in order to the three READ variables.
Complete the RUN of this program (fill in blanks).
10 READ X , Y
20 PR INT X , Y
30 GD TD 10
40 DA TA 10, 20, 3.0 , 40
RUN
10 20
ERROR- 6 AT LINE 10
The second time through the loop, these values were assigned to X and Y and
printed by line 20. (We'll discuss the spacing of items in PRINT statements later
on.)
15. Fill in the blank spaces in the program and in the RUN.
10
20 PR IN T A + S
30 GOTD 10
40 DATA 3 ,5,6. 4 , 7 , 9
RUN
8
ERROR- 6 AT LINE 10
10 READ A,S
RUN
8
l.0
16
READ AND DATA WORK TOGETHER 129
16. You may have more than one READ statement in a program. However, all
READ statements assign values to their variable(s) from the same DATA state-
ment. An item from DATA is assigned to a READ variable in the order that the
computer comes to READ statements when the program is RUN.
III READ P
211 READ Q
311 PRINT P
411 PRINT Q
511 GDTO III
6lJ DATA 3,5,6,4,7,9
RUN
~ } -First data item assigned to P, second assigned to Q.
~ } - Second trip through the loop: third data item is assigned to P, fourth
7 to Q.
9
ERROR- 6 AT LINE
For the third trip through the loop, what value is assigned to P? _ _ _ __
What value is assigned to Q? _ _ _ __
7;9
1m READ X
2e READ Y
3e READ Z
413 PRINT X + Y + Z
513 GOTO 1m
6e DATA 3,5,6,4,7,9,2,5,2
RUN
14
213
9
ERROR- 6 AT LINE 1m
Each of the 50 responses was either 1 (YES) or 2 (NO). The responses are shown
below in five DATA statements. The last response is followed by -1, the flag
signalling end of data.
gee REM***DATA* l=YES, 2=NO, -l=END OF DATA
9113 DATA 1,2,2,2,1,2,1,2,1,2
9213 DATA 2,1,1,1,2,1,2,2,2,1
93e DATA 2,2,2,1,2,1,2,2,1,2
9413 DATA 1,1,1,1,2,1,2,2,1,1
9513 DATA 2,2,2,2,1,1,1,2,1,2,-1
yc=J
READ AND DATA WORK TOGETHER 131
19. Here is a program to read the answers from the DATA statements and
count the number of YES answers and NO answers. The variable Y IS used to
count YES answers. The variable N is used to count NO answers.
100 REM***QUESTIONNAIRE ANALYSIS PROGRAM
110 REM***INITIALIZE: SET COUNTING VARIABLES TO ZERO
120 Y=0 : N=0
200 REM***READ AND COUNT VOTES
210 READ A : IF A=-1 THEN 410
220 IF A=1 THEN LET Y=Y+l , GOTO 210
230 IF A=2 THEN LET N=N+l , GOTO 210
400 REM***PRINT THE RESULTS Note the multiple statements in
410 PRINT lines 120, 210, 220, and 230.
420 PRINT "YES: " ; Y
430 PRINT" NO, " ; N
900 REM***DATA* I=YES, 2=NO, -1=END OF DATA
9 10 DATA 1, 2 , 2 , 2 , 1 , 2 , 1 , 2 , 1 , 2
920 DATA 2,1,1,1,2,1,2,2,2,1
930 DATA 2,2,2,1,2,1,2,2,1,2
940 DATA 1,1,1,1,2,1,2,2,1 , 1
950 DATA 2,2,2,2,1,1,1,2,1,2,-1
RUN
YES: 23
NO I 27
(a) Which section of the program is a loop that is repeated for each value in
the DATA statements? Lines _____ to _ _ _ __
(b) Which statement reads a value corresponding to one vote and puts it into
box A? _ _ _ _ _ _ _ _ _ __
(c) Which line determines whether a vote is YES and, if it is, increases the YES
count by one? _ _ _ __
(d) Which line determines whether a vote is NO and, if it is, increases the NO .
count by one? _ _ _ __
(a) lines 210 to 230; (b) line 210; (c) line 220; (d) line 230
132 ATARI BASIC
Modify the program in frame 19 so that the computer counts the YES, NO,
and SOMETIMES answers. Use the variable Y to count YES answers. Use the
variable N to count NO answers. Use the variable S to count SOMETIMES
answers. Use the following data.
2, 1, 3,2, 3, 3, 1, 3, 3, 2, 1,2, 1,2, 1, 1, 3, 3, -1
Using this data , the results when the program is RUN should be printed as
follows.
YE S : 6
NO : 5
SOM ETI ME S: 7
We deleted lines 920, 930, 940, and 950 from the program of frame 19. If the
program is in the computer, we can do this by typing the line number and press-
ing RETURN. If you have a computer handy, LIST the program to show the
modifications and to prove that the deleted lines have disappeared from the com-
puter's memory. On the following page is our listing.
READ AND DATA WORK TOGETHER 133
LIST
For example:
PRINT 7 +5
numerical expression
A PRINT statement of this form tells the computer to compute and evalute
(do the arithmetic of) the numerical expression and then print the result.
The following PRINT statement tells the computer to evaluate four
numerical expressions and print the four results.
We type: PRINT 7 + 5, 7 - 5, 7 * 5, 7 I 5
In the above, draw arrows connecting each numerical expression with its
computed and printed value. We have drawn the first arrow, connecting the ex-
pression, 7 + 5, with its printed result, 12.
PRINT 7 + 5, 7 - 5, 7 * 5, 7 I 5
12 ~ 2/ 35/ 1.4/
134 ATARI BASIC
22. If a PRINT statement has more than one expression, then the expressions
are separated by commas.
PRINT 7+5. 7 - 5. 7*5, 7/5
The following PRINT statement directs the computer to compute and print
the values of 2+3,2-3,2*3, and 213. However, we forgot to put in commas.
Please insert commas in the correct BASIC form .
PRINT 2+3 2-3 2*3 2 /3
PRINT 2+3,2-3.2*3.2/3
23. Complete the following. (Remember, for direct statements, you do not need
a line number and you only have to press RETURN to have the computer
execute the statement.)
We type : PRINT 3 * 3 ·, 5 * 5, 7 *7
It types:
We type: P R I NT 2 *3+4*5 , (2 + 3 ) * ( 4 + 5 )
It types:
9 25 49
26 45
24. Your turn. Let's go back to our three bicycles, with wheels of 20-, 24- and
26-inch diameters. You want to find out, for each bike, how far it travels during
one turn of the wheel. In other words, you want to evaluate the following three
expressions.
Write a PRINT statement to tell the computer to evaluate the three expres-
sions and PRINT the results.
You type:
It types: 62.8 75.36 81. 64
25. ATARI BASIC has four standard print positions. A comma in a PRINT
statement causes the cursor to move to the next available standard print position.
Look at the example below and fill in the blanks.
It types: 2 3 4
t
Position 1 Position 2
t t
Position
t
Position __
3; 4
26. Positive numbers, or zero, are printed without an explicit plus sign ( + ).
Watch what happens when negative numbers are printed.
It types: -1 -2 -3 -4
t
Position 1 Position 2
t t t
Position 3 Position 4
How does the printing of negative numbers differ from that of positive
numbers?
Negative numbers are printed with a minus sign (-) followed by the digits of the
number.
136 ATARI BASIC
27. What happens if there are more than four items in a PRINT statement?
Look at the direct statement below and also at how the computer executed it.
It types: 2 3 4
5 6 7
The computer printed the 7 numbers on 2 lines, with 4 numbers on the first line
and 3 numbers on the second line.
28. Following the rules for items or expressions separated by commas, show
what the computer will print in response to the following PRINT statement.
It types:
1 2 3 4
5 6 7 8
9 1 III
It types: -1-2-3-4-5-6-7
It types:, 12345
semicolons
READ AND DATA WORK TOGETHER 137
30. The screen can hold up to 38 characters on each line. That is, it has 38
character positions, numbered from 0 on the left to 37 on the right. Complete the
following diagram of character positions on the screen.
1 111 1 11111222222222233333333
012345 6 7890 1 23456789012345678901234567
t
Character
t
Character
,
Character
t
Character
position 7 position 20 position __ position __
32 t 37
31. The four standard print positions, used in comma spacing, begin at
character position O. We' ve marked print positions 1 and 2; you circle print posi-
tions 3 and 4 on the following diagram .
1 1 11111 11 1222222222233333333
012345678901 2 3456789012345678901234567
t t
Print Print
position 1 position 2
1 1 1 11 1 1 111~ 22222222a3333333
01234567890 1 2345678 ~ 123456789~ 1234567
t t t t
Print Print Print Print
position 1 position 2 position 3 position 4
32. Now watch what happens when we put a comma at the end of a PRINT
statement.
10 LET N= 1
20 PRINT N. ~ ___ _ _----'I
30 LE T N=N+ 1 Comma
40 Go To 20
RUN
1 2 3 4
5 1\ 7 8
9 U 11
STOPPED AT LINE 2~
RUN
1 3 5 7
9 11 13
33. Now, what do you suppose happens if we put a semicolon at the end of a
PRINT statement? As always with computers, if we don't know, we EXPERI-
MENT. Let's try it.
U LET N=1
2~ PRINT NI
3~ LET N=N+l
4~ GO TO UJ
RUN
12345678910111213141516171819 We pressed BREAK. (Bet you weren't
STOPPED AT LINE 20 this quick!)
Rewrite line 20 so that the computer puts a space after each number it prints.
20 PR I NT _ _ _ _ _ _ _ _ _ _ _ _ __
20 PRINT N; "
34. We have spent some time with comma spacing and semicolon spacing with
numbers. Next, let's try strings.
II
PRIN T II A" , " B rr I II C t "0
11
A B C D
t
Print Print
t t
Print
t
Print
position 1 position 2 posit ion 3 position 4
Your turn. Complete the output for this direct statement the way the com-
puter would do it.
PRINT II AII , 11 8
11
, li e ", ; I D II , IIE II,
I' ~ I I
r •
I I G II
A B C D
E F G
With strings, as with numbers, the comma causes the computer to move to the
next standard print position.
RUN
INCHES CENTIMETERS
1 2.54
8 20.32
12 30.48
ERROR- 6 IN 30
Note the use of comma spacing in lines 20 and 40. The numerical values
printed by line 40 line up nicely under the headings printed by line 20.
Your turn. Rewrite the ounces to grams program (frame 7), so that a RUN
looks like the one on the following page.
140 ATARl BASIC
RUN
OUNCES GRAMS
28.35
13 368.55
16 453,6
ERROR- 6 AT LINE 30
Semicolon sPflcing causes the computer to catenate strings, that is, to print
them one after the other with no spaces.
Show what happens when the computer executes the following PRINT
statement.
THISISCoMPUTERPR oG RAMMIN G?
39. The output in frame 38 is rather crowded and hard to read. If you want
spaces between strings, you must put them where you want them. For example:
PRINT "THIS ";"IS ";"COMPUTER "; " PROGRAMMING?"
READ AND DATA WORK TOGETHER 141
40. Let's try a program that causes strings stored by string variables to print
information.
5 DIM A$(20),B$(20),C $ (20) First, we make room for the strings.
10 LET A$="COMPUTERS "
20 LET B$="ARE" Note the spaces inside the quotation
30 LET C$=" IN TERE S TIN G."
40 PRINT A$;B$;C$
marks in lines 10 and 30.
50 PRINT A$,B$,C$
Show what the computer will print when the program is RUN.
RUN
RUN
COMPUTERS ARE INTERESTING.
COMPUTERS ARE I NTERESTI NG.
+
Position 2
t
Position 3
41. You may have guessed by now that a READ statement may also be used to
assign strings to string variables. Show how you think the RUN for the following
program to print the names of computer club members wiIi look.
5 DIM M$(20)
10 READ M$ Remember, this means "up to 20."
20 PRINT M$
30 GOTO 10
40 DATA JERRY,BOBBY,MAR Y.DA NN Y
50 DATA MIMI,KARL.DOUG,SCOTT
142 ATARIBASIC
RUN
JERRY
BOBBY
MARY
DANNY
MIMI
KARL
DOUG
SCOTT
ERROR- 6 AT LINE HI
Well, we could go on like this for a long time. But, there are other things for you
to know. SO, EXPERIMENT! What happens if??? Try it and find out. Try short
strings, long strings (what if a string is more than lO characters). Mix up strings
. and numbers. Try strings with leading spaces; trailing space. Aha! You and your
friendly ATARI computer can do it all.
And now, it's your turn again to write a program. You have probably used
dice before, either on "board" games or other games. We want you to apply
your accumulated knowledge of BASIC to write a program that simulates -
(imitates) the roll of a die. (A "die" is one "dice. ") Below is a RUN of our
program. Examine it and use it as a guide to writing a program that will produce
a RUN similar to this one. Use standard print positions. Take your time, and try
your solution on a computer before looking at ours, if possible.
RUN
ONE SIX
Don't look at the answer until you have written your program.
READ AND DATA WORK TOGETHER 143
SELF-TEST
Try this Self-Test, so you can evaluate how much you have learned so far.
1. -Pretend that you are the computer and complete each RUN.
(a) 10 RE AD A (b) 10 RE AD A
20 PRI NT A 20 RE AD B
30 DAT A 27 30 P RINT A-B
RUN 40 DA TA 27 , 1 5
RUN
(a) 10 READ X
20 PR INT X , ............------Comma at end of PRINT statement
30 GD T D 10
40 DATA 3 . 7 , 0 , -2 , 5,-1 , 7 , 8 , 0 , -3
RUN
(b) 10 READ X
2111 PRINT Xl" ; ......1--_ _ _ Semicolon at end of PRINT statement
3111 GO TO 10
4111 DATA 3,7,111,-2,5,-1,7,8 , 0,-3
RUN
(a) 10 READ .X
2111 IF X >= III THEN PRINT "x = " x , ............_ _ _ _ Comma
3111 GO TO 1111
4~ DATA 3,7,0t-2 , 5, - 1,7,8,~t - 3
RUN
YES 23 46
NO 27 54
TOTAL 5R1 lRiRI
Hint: Rewrite beginning with line 420. The required information is printed
in three columns, occupying the first three standard print positions.
RUN
WHEEL DIAME TE R : 16
DI S TANCE I N ONE TU RN: 5 0.24
Remember, distance in
WHEEL DI AMETER : 20 one tum is circumference
DI S TANCE IN ONE TU RN: 62 . 8
of the wheel. C = 3.14*D
WHEEL DI AME TE R : 24 where D = diameter of
DI S TANCE IN ONE TURN: 75 . 36
the wheel.
WHEEL DI AMETER : 26
DI STA NCE I N ONE TUR N: 8 1. 64
WHEE L DI AM ETER : 27
DI S TANC E I N ON E TURN : 84.78
~Line number of your
ERRDR- 6 AT READ statement
7. Write a program to simulate coin flipping . The program should direct the
computer to do the following steps. Type H for HEADS and T for TAILS
across the page as shown in the RUN below. Also ask how many flips the
user wants and do exactly that many, then stop. In other words, count the
flips, and, when the count has reached the number requested, stop. Here
are two RUNs of our program.
RUN
H H H H T T T H T H H T T H T T T T H
T
RUN
H H H H T T H T T T H H H T H T T H T
T H T H H T T T H T H H T T H H H T H
H T H H H H H H T T T T H T H T H H T
T H H T H H H H H H T H T H T T H T T
H H T H H T H T H T H T H H T T T H T
T T T H T
The first RUN of our program on our computer produced 9 heads (H) and
11 tails (T). The second RUN produced 53 heads (H) and 47 tails (T) . Your
computer may give quite different results.
READ AND DATA WORK TOGETHER 147
8. Why not let the computer count the number of heads and the number of
tails? Modify yOUJ program for question 7 so that the computer counts the
number of heads and tails. Use the variable H to keep track of the number
of heads and the variable T to keep track of the number of tails. Two
RUNs of the modified program are shown below.
RUN
T H H T T H H H T H
T
RUN
t
The flag
Our program RUNs like this.
RUN
THERE ARE 25 ITEMS OF DATA.
10. Now write a program that will print a pattern of asterisks according to a
plan entered as DATA statement values. This might be a way of laying a
pattern of floor tiles, or loom weaving patterns, or just "computer art."
Use a flag to avoid an out-of-data error message. Shown on the following
page are our DATA statement values and the pattern produced by our
program. Our program prints five different lines of asterisks and spaces.
READ AND DATA WORK TOGETHER 149
3.4.4.5. 1.5. 1 .5. 1 .5. 2.2 . 2 .5. 1.5. 1.5.1 .5 . 4 . 4 . 3 ~ DATA values
RUN
*********************
***********
***********
*** *** *** ***
******* *** *******
*** *.* *** ***
******* *** *******
**. *.* *** ***
******* *** *******
*.* *.* *.* *** Hint: The pattern consists of five dif-
*** ***** ***** *** ferent types of lines. The first and last
*** ***** ***** *** lines are "type 3" lines. The first and
*** ***** ***** ***
*** *** *** *** last numbers in the DATA statement
******* *** ******* are 3. Got the idea?
*** *** .** ***
******* *** *******
*** *** *** **.
******* **. *******
*** *** *** ***
***********
*. * * * * * * * * * *
*********************
Answers to Self-Test
The frame numbers in parentheses refer to the frames in the chapter where the
topic is discussed. You may wish to refer to these for quick review.
2. (a) RUN
3 7 0 -2
5 -1 7 8
0 -3
ERROR- 6 AT LINE 10
(b) RUN
3 7 o -2 5 -1 7 8 0 -3
ERROR- 6 AT LINE 10
(frames 1-5)
3. (a) RUN
x = 3 X = 7 X 0 X 5
X = 7 X = 8 X 0
ERROR- 6 AT LINE 10
150 ATARI BASIC
(b) RUN
X = 3X 7X = 5X = 7X 8X
ERROR- 6 AT LINE H'
How would you rewrite line 20 in order to provide more space between items? '
For example: X = 3 X = 7 etc.
(frames 32-33)
4. RUN
ANSWER NO. OF PERCENT OF
(Y OR N) ANSWERS TOTAL
(frame 35)
(frame 1)
There is more than one way to write this program. In ours, we use the
variable K to count the number of times the coin has been flipped. In line
140, we set K to zero. Then, each time through the loop, we increase K by
1 (line 180) and compare K with N (line 190). If K is still less than N, we
increase K by 1 and go around again. If N is less than one, no flips are
done-this is checked by line 120. Your program may be entirely different.
If it works, you have solved the problem! (frames 37, 42)
READ AND DATA WORK TOGETHER 151
Do you recognize the numbers in the DATA statements? They are all the
prime numbers (those numbers with themselves and 1 as their only factors)
less than 100. (frames 10, 11, 19)
152 ATARI BASIC
..* .*.
.** *.* •• * •••
******* *******
••• • *• * ••
******* **. *******
*** *** *** ***
**'. ***** ***** ***
*** ***** .* •••• *.
*** ***** ***** ***
*** *** *** ***
******* **. *******
*.* * •• .**
* •••••• ••• *******
•• * *.* •• * .* •
*.*
******* ••• *******
* •• •• * ••• • ••
1 * * * * * * * * *
* * * * * '" '" * * *
*********************
19.0 GO TO 2.0
9.00 DA TA 3,4 , 4,5,1,5,1 , 5,1 , 5,2,2
91.0 DA T A 2 , 5,1 , 5,1,5 , 1 , 5,4,4,3 , -1
(frames 18,19)
CHAPTER SIX
FOR-NEXT Loops
153
154 ATARl BASIC
5 REM***LOOP DEMONSTRATION
1.0 LET F=l
2.0 PRINT "F = ", F
3.0 LET F=F+1
4.0 IF F <= 6 THEN 2.0
RUN
F
F 2
F 3
F 4
F 5
F 6
1.0 LET F = 1
2.0 PRINT "F = ", F I LET F=F+1 I IF F <= 6 THEN 2.0
RUN !
F This means go back and start executing line 20 again
F 2
F 3 (only if the comparison is true, of course). Notice that
F 4 line 10 would also fit on this line, but the value for F
F 5
F 6
would get "initialized" back to I every time the state-
ment was executed. So we could not put the entire
program in just one line.
In every FOR-NEXT loop, the FOR statement is the beginning point of the
loop and the NEXT statement is always the last statement in the loop. The state-
ment or statements between FOR and NEXT are executed, in order, over and
over again, with the FOR statement indicating to the computer how many times
the loop is to be executed.
(a) You can see from the RUN of the FOR-NEXT loop that each time through
the loop the value of F is automatically increased by
(b) How many times did the computer go through the loop? _ _ __
FOR-NEXT LOOPS ISS
(c) Why did the computer stop after going through the loop the above number
of times?
(a) 1
(b) 6
(c) because the FOR statement told it to go from 1 to 6.
(d) a NEXT statement
3. As you can see in the program below, the computer will continue with the
rest of the program when it has completed the loop as specified by the FOR
statement.
5 REM*·*ANOTHER FOR-NE XT LOOP
10 FOR D=5 TD 10 ....... . - - - - - - - - Note that thp bop doesn't have to
20 PRINT "0 = "; D start with 1.
30 NEXT D
40 PRINT
50 PRINT "AHA! OUT OF THE LOOP BE CAUSE"
60 PRINT "D = "; D. " WHICH EXCEEDS 1.111."
RUN
D 5
D 6
D 7
D 8
D 9
D 10
/
156 ATARI BASIC
1.0 FOR N 1 TO 3
In line 10, N is set equal to 1.
Q
2m PRINT N
1)
Let's look at the FOR statement.
When N goes past this value, the computer
stops executing the loop and continues on
with the rest of the program past the
FOR-NEXT loop (if there is more to the
1m FOR N = 1 TO 3 ......1 - - - - program). We call this the limit of N, or
t
This is the
the limit of the FOR variable.
FOR-NEXT loop
control variable.
This is the
first value N will have.
Each time the computer comes to a NEXT N statement, it increases the value of
N by one, and checks the new value against the limit for N. In this case, the limit
is 3, because the FOR statement reads: FOR N = 1 TO 3. When the value of N
is greater than 3, the computer continues on to the next statement after the
NEXT statement, if there is one. If not, the computer has finished executing the
program and stops. Got that? Let's see.
Statement 10 means that for the first time through the loop, N = 1. The
second time through, N = N + 1 = 1 + 1 = 2. The third time through, N
FOR-NEXT LOOPS 157
N N+l=2+1 3
5. Write a program with three statements that will print the word LOOP six
times. Use a FOR-NEXT loop, and use C as the FOR-NEXT loop control
variable. Show what your program will print when it is RUN.
10 FOR C = 1 TO 6
20 PRIN T "L OOP "
30 NE XT C
RUN
LOOP
LOOP
LOOP
L OOP
LOOP
LOOP
UJ FOR N I
v-'J
TO 5 I PRINT NI " " I I NE X T N I PRINT "N NOW EQUALS" I N
RUN
2 3 4 5
N NOW EQUALS 6
When the computer had finished the FOR-NEXT loop and had made its
exit from the loop, what was the value for N? _ _ __
158 ATARI BASIC
Notice that the value for N after loop exit is one more than the upper limit for N
as given in the FOR statement. That is how the computer recognized that it had
performed the proper number of loops and was ready to go on to the next state-
ment in the program beyond the FOR-NEXT loop.
. TESTING 1 2 3 4
8. Finish writing this program so that the computer will produce the RUN
shown. Use a FOR-NEXT loop with Y as the control variable.
30 _____________________________________________________________________
40 _____________________________________________________________________
RU N
THI S YEAR CA THY IS 10 YEARS OLD
AND THE NE XT YEAR SHE WIL L BE 11
AND THE NE XT YEA R S HE WILL BE 1 2
AN D TH E NE XT YEAR S HE WIL L BE 13
AN D TH E NE XT YE AR S HE WI LL BE 14
20 FOR Y = 11 TO 14
30 PRINT "AND THE NE XT YEAR SHE WILL BE " ; Y
40 NEXT Y
FOR-NEXT LOOPS 159
Rewrite the FOR statement (line 20), substituting numerical values for variables
A and B. Use the values that were assigned by the program above.
20 __________________
211J FOR C = 3 TO 8
10. Play computer and show the RUN for this FOR-NEXT demonstration
program.
RUN
131234
11. Now you write a program where variables are used to set the initial and up-
per limits of the FOR-NEXT loop control variable. Have your program READ
the values to be used for the initial and upper limits from a DATA statement.
Use Z for the control variable, and use X and Y for the variables that set
the initial and upper limits. Select your DATA statement values so that your pro-
gram will produce the following RUN.
RUN
II1JI1J Il1Jl I11J2 1133 I11J4 I11J5
160 ATARl BASIC
10' READ X
20' READ Y
30' FOR Z=X TO Y
40' PRINT " " I Z
50' NEXT Z
60' DATA 10'0'. HIS
12. In this next program, an INPUT value (line 40) is used to establish the up-
per limit of the FOR statement (line 100), which tells the computer how many
times to repeat "X = ?" When the program is RUN, the PRINT statements in
lines 10-80 tell the user how to use the program.
5 REM***FRIENDLY MEAN FROM INPUT VALUES
lit! PRINT "FOR MY NEXT ENCORE I WILL COMPUTE"
20' PRINT "THE MEAN OF A LIST OF NUMBERS."
30' PRINT
40' PRINT "HOW MANY NUMBERS IN THE LIST" I : INPUT N
50 PRINT
60 PRINT "EACH TIME I TYPE 'X =1' YOU TYPE IN"
70 PRINT "ONE NUMBER AND THEN PRESS RETURN."
80' PRINT
90 T=0
100 FOR K=l TO N
110 PRINT "X "I I INPUT X
120 T=T+X
130 NEXT K
140 M=T/N
150' PRINT
160 PRINT "TOTAL = "; T
170' PRINT "MEAN = "; M
13. In the program of frame 12, which line in the FOR·NEXT loop will keep a
running tally of the values entered for line l1O? _ _ _ _ _ _ _ _ _ _ _ __
120 T = T + X
RUN
FOR MY NEXT ENCORE, I WILL COMPUTE
THE MEAN OF A LIST OF NUMBERS.
~ :~ ~: }-----
X
X
=7
=7
112
23
Values entered by user.
TOT AL = 235
MEAN = 4 7
Show the numerical values in the FOR statement for the above RUN.
100 FOR K = ________ TO
RUN
FOR MY NEXT ENCORE, I WILL COMPUTE
THE MEAN OF A LIST OF NUMBERS.
How many times will "X = ?" be printed? _____ How many times will
the statements between the FOR·NEXT statements be executed?
161 ATARI BASIC
4;4
X =7 2
X =7 4
X =7 6
X =7 8
TOTAL = 216
MEAN = 5
16. Complete the following program to compute the product (P) of N numbers.
Think carefully about the effect of your statements when the program is RUN.
1416 ________________~----------~-----------------------------------------
1516 PRINT I PRINT "PRODUCT ", P
RUN
YOU WANT ANOTHER ENCORE? I'LL COMPUTE
THE PRODUCT OF A LIST OF NUMBERS .
X =? 7
X =? 12
X =? 4
X =7 3
X = ? 19
PR ODU CT = 19152
100 LET P =1
11 0 F OR K= 1 TO N
140 NE XT K
FOR·NEXT LOOPS 163
17. Any BASIC expression may be used to set both the initial and the max·
imum value of a FOR·NEXT loop control variable. The computer evaluates these
expressions (that is, does the arithmetic) be/ore the loop is executed the first
time, and does not recompute these values each time through the loop.
Look at the FOR statement in this next program, and then decide whether
the computer executed the loop the proper number of times.
U' 0=4
20 FOR p=o TO 2*0-1
30 PRINT" "; P;
40 NEXT P
RUN
456 7 The computer was right!
In the following program, fill in the blanks in line 20 with expressions using
the variable Q, so that when the program is RUN, it will produce the printout
shown below.
10 0=4
20 FOR P-_______ TO
30 PRINT" "; P;
40 NEXT P
RUN
2 3 4 5 6 7 8 9 10 11 12
Note: If your answer is different and you think it is correct, try it on your com-
puter and see if you get the same RUN that we did.
18. In the FOR-NEXT loops you have seen so far, the FOR-NEXT loop con-
trol variable takes the first value given in the FOR statement, and keeps that
value until the computer comes to the NEXT statement. Then the FOR variable
increases its value by one each time through the loop until it reaches the max-
imum value allowed by the FOR statement.
FOR X = 5 TO 10
However, you can write a FOR statement that causes the value of the
FOR-NEXT loop control variable to increase by multiples of other than one, or
164 ATARI BASIC
by frational increments. You can also have the value of the FOR-NEXT variable
decrease each time through the loop.
Ie FOR X=l TO Ie STEP 2
I
I
Tells the computer to increase the value of X by 2
every time through the FOR-NEXT loop, until X is
greater than 10.
I
Tells the computer to increase the value of Y by 1.5
every time through the FOR-NEXT loop, until Y is
greater than 6.
RUN
1 3 5 7 9
The PRINT statement in line 40 "bumps" the computer off the line where
it is held by the semicolon at the end of line 20. The PRINT statement in line 50
causes the space before line 60 is printed.
Note that the loop starts with the first value in the FOR statement (1) and
increases by increments of 2, until the value of B = 11 which exceeds the max-
imum value allowed (10). At that point, the computer terminates the loop and
continues running the rest of the program.
Play computer again, and fill in the RUN for this program.
Ie 0=3 : FOR F=O TO 4*0 STEP 0 I PRINT" "; F; : NEXT F
RUN
FOR·NEXT LOOPS 165
3 6 9 12
19. In this example, the STEP in the FOR statement js followed by a negative
number. STEP may be used to decrease the value of tl1e FOR variable in any size
step, going from a large value to a smaller one.
1e FOR J=1ee TO 1e STEP -1e
211 PRINT" "I JI
3e NEXT J
RUN
11111 911 811 711 68 58 48 30 28 111
Now you write one where the FOR-NEXT loop control variable E decreases
in steps of 3 from 27 to 18. Show the program and the RUN.
18 FOR E = 27 TO 18 STEP -3
28 PRINT" "I EI
3e NE XT E
RUN
27 24 21 18
Show the RUN for this program if we changed line 10 to the following.
111 FOR X = 5 TO 7.5 5TEP . 5
RUN
RUN
5 5.5 6 6.5 7 7.5
166 ATARI BASIC
21. The FOR-NEXT loop is useful for such things as repeated calculations,
counting or keeping tallies, and dealing with cyclical or recurring events.
One such recurring event is the monthly compounding of interest on a sav-
ings account. In the following program, monthly interest (I) is calculated in line
180 by multiplying the initial amount of money (P for Principal) by the Rate of
interest (R).
The rate of interest is converted to a decimal fraction: R = 5 percent =
5/100 = .05
Since 5 percent is the yearly rate of interest, only 1112 of the calculated
amount of interest is added to the principal each month.
RUN
PRINCIPAL? 2mm
YEARLY INTEREST RATE (IN X)? 5
HOW MANY MONTHS? 6
MONTH AMOUNT
1 2mm This table shows the amount of money
2 2mm.833333 in the account at the beginning of each
3 2m1.67m138
4 2m2.51m43 month.
S 2m3.354223
6 2m4.2m1532
22. Yes, we admit, it is rather strange to see a "dollars and cents" answer
printed as 200.833333 or 201.670138. Fortunately, BASIC has a nifty way for
rounding off numbers to a desired number of decimal places.
Recall how the INT function works (Chapter 4, Frame 20). The INT func-
tion chops off a number at the decimal point, keeps the integer part and discards
the decimal fraction part. For example,
INT (200.833333) = 200
However, when dealing with dollars and cents, we don't want to lose the cents!
So, we do it like this.
(1) Multiply by 100. 100*200.833333 = 20083.3'33
(2) Add .5. 20083.333 +.5 = 20083.8333
(3) Keep the integer part. INT(20083.8333) = 20083.
(4) Divide by 100. 20083/100 = 200.83
Your turn. Use the above procedure to round $123.45678 to the nearest penny.
(1) Multiply by 100. 100*123.45678 = _ _ _ _ _ _ __
13S A = U0*A
140 PRINT " ( 1 ) MULTIPLY BY 1$110. A "IA
150 A = A + .5
16" PRINT "( 2) ADD • 5. A "IA
17" A = INT(A)
18e PRINT "( 3) KEEP INTEGER PART. A "IA
190 A = A/U0
"( 4) DIVIDE BY 1"0.
\ 28" PRINT A = "IA
RUN
One more example. You fill in the values of A. (Try using our program on your
Atari!)
(1 ) MULTIPLY BY 100. A
(2 ) ADD .5. A
(4 ) DIVIDE BY 100. A
203.1249
(1) 20372.49
(2) 20372.99
(3) 23S72
(4) 203.·72
FOR-NEXT LOOPS 169
24. In our rounding program, the ac;tual work of rounding is done by lines 130,
150, 170 and 190. We can combine these four lines into a single statement to
round a number, as follows.
A INT(lSS*A + .5)/10S
(I) tU~jPIY IL
by 100
(4) Divide by 100
(2) Add .5
(3) Keep the integer part
Remember, the above statement rounds the value of A to two decimal places.
(1) Write a statement to round the value of P to two decimal places.
Try this. Add the statement 195 P = INT(100*P + .5)/ 100 to the MONTHLY
INTEREST COMPOUNDING program in frame 21. This should cause the
numbers under AMOUNT to be rounded to the nearest penny. A RUN will look
like this.
RUN
PRINCIPAL?200
YEARLY INTEREST RATE (IN %)?5
HOW MANY MONTHS?6
MONTH AMOUNT
1 200
2 200.83
3 201. 67
4 202.51
5 203.35
6 204.2 .. Read this as 204.20
170 ATARI BASIC
25. Can you have a FOR-NEXT inside another FOR-NEXT loop? Absolutely.
It is called nested FOR-NEXT loops and is perfectly "legal" provided you follow
the rule illustrated below.
10 FOR X = 1 TO 5
20 FOR Y =
30 PRINT"·";
1 TO 6
-- This inside loop must be completely inside
the other or outside loop. Otherwise you'll
40 NEXT Y get an error message.
50 NE XT X
RUN
******************************
Count the stars. How many are there? Do you see any relationship between
the number of stars printed and the numbers 5 and 6 which appear in the two
FOR statements?
A loop within a loop is called a FOR-NEXT loop. The
inside loop must be the outside loop.
nested; inside
program A
RUN
NE S TED
NE S TE D
NE S TED
LOOP
NE S TED
NE S TED
NE S TED
LOOP
NE S TED
NESTED
NE S TED
LOO P
NE S TE D
NES TED
NE S TED
LOO P
5 9 PRINT
69 NEXT R
RUN
* ••••• *
•••••••
•••••••
(1) What is the purpose of the empty PRINT statement in line 50?
(1) It moves the cursor to the beginning of the next line after a row of stars has
been printed by the inside loop.
(2) The outer loop, controlled by lines 10 and 60, causes the inner loop to be
executed for R = 1, 2, and 3. Each time, a row of stars i~ printed.
RUN
111 The computer will print four (4) rows, each with three (3) ques-
111
111
tion marks.
111
FOR-NEXT LOOPS 173
30. Write a program to print 7 rows with 12 dollars signs ($) in each row. A
RUN should look like the following.
RUN
$$$$$$$$$$$$
$t$$$$$$$$$$
$$$$$$$$$$$$
$ $$,$$ $$$$$ $$
$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$
SELF-TEST
Now that you have completed Chapter 6, you have acquired enough understand-
ing of computer programming to be able to learn a lot more by experimenting at
a computer terminal. As you look at our demonstration programs, you may see
some possibilities that we do not specifically deal with. Build on your knowledge
by trying out your own ideas.
But right now, find out if you really know how to use FOR-NEXT loops by
doing the following programs.
3. Examine this program. Which of the three RUNs was produced by the
program? _ _ __
10 N=;j
20 F OR K=l TO N
30 PRINT II .. II ;
40 NE XT K
50 P RINT
60 N=N +l
70 I F N> 10 TH EN END
80 GO TO 2 0
RUN
FIRS T NUMB E R? 40
LAS T NUMBER? 45
N N-SQUARED N-CUBED
40 1600 64000
41 1681 68921
42 1 764 74088
43 1849 79507
44 193 6 85184
45 2025 91 125
10 5=0
20 FOR K=I TO 7 STEP 2
30 S=S+K
40 NEXT K
50 PR I NT 5
RUN
21m -------------------------------------------
220 -------------------------------------------
230 ___________________________________________
24m
RUN
INITIAL POPULATION? 230
GROWTH RATE? 1
I~ITIAL VALUE OF N? 0
FINAL VALUE OF N? 1m0
STEP SIZE? 25
N POPULATION
m 23m
25 295
5m 378
75 485
!m0 622
RUN
INITIAL POPULATION? 205 For U.S.A., 1970 (in millions of
GROWTH RATE? 1
INITIAL VALUE OF N? 0 people).
FINAL VALUE OF N? 1m0
STEP SIZE? 10
N POPULATION
m 2m5
1m 226 Results are expressed in millions,
20 250 rounded to the nearest million.
3m 276
4m 3m5
50 337
6m 372
70 411
8m 454
90 502
100 554
7. Write a program to compute and print the sum of whole numbers from 1 to
N where the value of N is supplied in response to an INPUT statement. A
RUN might look like the one on the following page.
FOR-NEXT LOOPS 177
RUN
GIVE ME A NUMBER (N) AND 1 WILL COMPUTE
THE SUM OF THE NUMBERS FROM 1 TO N.
WHAT IS N? 3
THE SUM IS 6 Because 1 + 2 + 3 = 6
WHAT IS N? 5
THE SUM IS 15 Because 1 + 2 + 3 + 4 + 5 15
WHAT IS N? And so on.
8. Look back at the simple number guessing game in Chapter 1, frame 9. Use
a FOR-NEXT loop to modify the program so that the user has only eight
chances to guess the number. If he fails in eight guesses print an appro-
priate message before starting over again.
9. What will soon appear on the screen if we store and run the following
program?
169 PRINT
179 NEXT R
Answers to Self-Test
The frame numbers in parentheses refer to the frames in the chapter where the
topic is discussed. You may wish to refer back to these for a quick review.
1. RUN
10
The answer is the sum of the values of K defined by the FOR statement
(K = 1,2, 3, and 4). (frames 1-7)
2. RUN
24
The answer is the product of the values of K defined by the FOR statement
(K = 1,2, 3, and 4). (frames 1-6, 16)
3. RUN 3. The FOR-NEXT loop (lines 20, 30, 40) causes the computer to
print a row of N stars . The loop is done for N = 1, 2, 3, ... to.
(frames 1-6, 28)
5. RUN
16
Similar to question 1, but this time the values of K defined by the FOR
statement are K = 1,3,5, and 7. (frame 18)
9. The "stars" will come out. About 116 of the screen will show stars; the
other 5/6 will be "blanks." The stars and blanks are selected at random by
lines 120, 130, and 140. Happy stargazing!
CHAPTER SEVEN
Subscripted Variables
In Chapters 7 and 8 we will present another useful tool, the subscripted variable.
First we will discuss BASIC variables with a single subscript.
You will learn, for example, how to count votes from a survey and how to
accumulate or count dollars and assign them to different groupings. And you will
get lots more practice using FOR-NEXT loops.
Many new programming ideas are introduced in this chapter. Read the
chapter slowly and carefully. Experiment on your computer; you will find that
these new techniques give you much more range and flexibility. When you com-
plete this chapter, you will be able to:
• recognize and use subscripted variables with a single (one) subscript;
• assign values to subscripted variables;
• use subscripted variables with variables for subscripts;
• use one-dimensional arrays to store the values of subscripted variables;
• use the DIM statement to tell the computer the maximum size of the
array(s) used by a program.
180
SUBSCRIPTED VARIABLES 181
1. The concept we discuss here will require your close attention. Take it
slowly, and read carefully as we enter the mysterious realm of subscripted
variables.
Until now, we have used only simple BASIC variables. A simple variable
consists of a letter (any letter A to Z) or a letter followed by a single digit (any
digit 0 to 9). For example, the following are simple variables.
P R K PI P2
P(5)
SUbscriPt)
X(l); C(23)
Note: X, Xl, and X(I) are three distinct variables. All three can appear in the
same program. They may confuse you, but the computer will recognize them as
three different variables.
182 ATARI BASIC
2. A subscripted variable (like the simple variables we have been using) names
a memory location inside the computer. You can think of it as a box, a place to
store a number.
P(O)
P(1)
P(2)
A set of subscripted variables is also
P(3) called an array. This set of subscripted
variables is a one-dimensional array,
P(4) also known as a vector. In Chapter 8,
we will discuss two-dimensional arrays.
P(5)
P(6)
P(7)
P(8)
Pretend you are the computer, and LET P(2) = 36. In other words use
your pencil or pen and write the number 36 in the box labeled P(2) in the draw-
ing above. Then LET P(3) = 12. (Do it.) Now LET P(7) = P(2) + P(3). Check
yourself by looking at our answer following the dashed line.
P(O)
P(1)
P(2) 36
P(3) 12
P(4)
P(5)
P(6)
P(7) 48
P(8)
SUBSCRIPTED VARlABLES 183
Y(3) 5 X(l) 2 C 3
Y(4) 6 X(2) 3 D 4
----------
4 1 4
-3 2 -3
5 2 3
4.7 9.2 6 ,
Notice that the expr.essions inside the subscript parentheses are computed
using the same rules for BASIC arithmetic as an expression in a PRINT state-
ment, or inside a function parentheses.
184 ATARI BASIC
Now you complete some examples as we did above, showing both the value
calculated for the subscript and the value assigned to the subscripted variable
with that subscript. (Refer to the boxes in frame 3.)
Y(A+2) '= _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __
Y(2*A-l) =
Y(A+B) =
Y(B*C-D) = ______________________
Y(A+3) = _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __
Y(D-3) =
Y(D-C+A) =
Y«C + D)-(A + B)) =
5. You will recall that previously you had to tell the computer how much
space to reserve in its memory for the strings to be assigned to string variables in
your program. In a similar fashion, you must also DIMension subscripted
variables for the maximum number of values to be assigned to a particular
subscripted variable. That is, you must tell the computer the largest subscript it is
to permit for a subscripted variable by using a DIM statement. "DIM" is the ab-
breviation for the "dimension" of an array of subscripted variables. The DIM
statement(s) in your program must be placed so that they are executed before the
subscripted variable(s) are actually used in the program, or an error message will
result and the execution of the program will stop. The format for dimensioning
an array is shown below. Note that it is similar to dimensioning a string variable.
IRJS DIM xc llillill
1J1JJ1J
6. Suppose we wanted to specify that the maximum subscript is 50. Write the
DIM statement.
1J1J5 _______________________________
RUN
HOW MANY NUMBERS?5
X=?37
X=?23
X=?46
X=?78
X=?59
THE TOTAL IS 243
xes)
· 186 ATARI BASIC
,
8. Notice especially lines 170 and 180 in the previous frame: First, the input
value is assigned to the simple numeric variable X. Then the subscripted variable
X(K) is assigned the same value by letting X(K) = X.
Now suppose the computer is running the program in frame 7. It has just
completed the FOR-NEXT loop in lines 150 to 190. The numbers entered by the
user are now stored as follows.
N 5
X(l) 37
X(2) 23
X(3) 46
X(4) 78
X(5) 59
The computer is ready to proceed with line 200. This statement initializes
the variable T, that is, gives T its first value. Show the value of T after line 200
has been executed.
TIL...--- _ - - - l
9. Next, the computer will ex~cute the FOR-NEXT loop in lines 210 to 230.
How many times will the FOR-NEXT loop be executed? _ _ _ _ _ _ _ _ __
10. The FOR-NEXT loop in lines 210 to 230 will be ddne 5 times, first for K =
1, then for K = 2, K = 3, K = 4, and finally for K = 5. Let's look at line 220,
where K is used as a subscript.
221i! LET T=T+X(K)
This statement tells the computer to add the value of X(K) to the old value of T
and then assign the results as the new value of T.
What is the value of T after line 220 has been executed for K = I?
_____ ForK 2? _ _ _ _ For K = 3? _ _ _ _ For K = 4?
_ _ _ _ ForK 5? _ _ __
11. Let's use the World's Most Expensive Adding Machine to compute the sum
of whole numbers, 1 through 12.
RUN
X =? 1
X =? 2
X =? 3
X =? 4
X =? 5
X =? 6
X =? 7
X =? 8
X =? 9
X =? 1 Ii!
X =? 1 1
ERROR- AT LINE 1 8 Ii!
This time we got an error message telling us that we had a bad subscript. What
was the largest subscript for X(K) that the computer would accept before it gave
us an error message? ___________
1 Ii!
12. Change line 110 to allow us to input more numbers by substituting the
following statement:
lllil DIM X( 1 Ii! Ii! )
Now RUN the program again using the 12 numbers that gave us trouble before.
188 ATARI BASIC
RUN
X =1 1
X =1 2
X =1 3
X =1 4
X =1 5
X =1 6
X =1 7
X =1 8
X =1 9
X · =1 10
X =1 11
X =1 12
THE TOTAL IS 78
Now the program can be used to compute the sum of at most how many
numbers? _ _ __
100. If 100 numbers are entered they will be stored in X(1) through X(100), the
limit specified by the DIM statement in line 110. We can, of course, also use the
program to compute the sum of fewer than 100 numbers.
13. A DIM statement may have its dimensions assigned by a variable. Look at
the examples below.
DIM XCN) DIM ACB) DIM DCY+2)
You may only dimension an array once during the RUN of a program. The com-
puter will give you an error message and stop executing the program if it comes
to a DIM statement for the same array a second time and tries to redimension
the array during the same RUN.
Look at the program segment below, and the beginning of the RUN. We
have deleted line 110 and placed the DIM statement at the beginning of line 150.
RUN
HOW MANY NUMBERS,8
What will be the dimensioned size of the array during this RUN of the program?
SUBSCRIPTED VARIABLES 189
14. Where else in the program segment shown in frame 13 could DIM X(N) be
placed?
In line 130 after INPUT N, or in line 140 either before or after PRINT. But
notice that the DIM statement must be placed after the input value has been
entered, but before the FOR-NEXT loop so that the array X(K) is dimensioned
only once during the same RUN. The DIM X(N) statement could also be on a
single line by itself, using a line number between 130 and 140 or between 140
and 150.
15. Look at the' first FOR-NEXT loop (lines 150-190) in the program in frame
7. The first value assigned to K is 1, so the first subscripted variable X(K) is
X(l) . Since the array could start with X(O), an array dimensioned as DIM X(100)
can actually hold how many values? _ _ _ _ __
Show how we could modify the FOR statement so that we could take ad-
vantage of every element (subscripted variable value) in the array for storing
values, provided that N = 100. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __
un
FOR K=0 TO N
16. Instead of using an INPUT statement to get values for X(l), X(2), and so
on, we can use READ and DATA statements. We'll put the value of N and the
values of X(I) through X(N) in a DATA statement, as follows.
K;L
17 . If we had wanted to, could we have used] as the subscript in all three
places? ___________
Yes. These are three separate and distinct FOR-NEXT loops. We could have used
any variable as the subscript except N, T, or X.
18. The following questions refer back to the program in frame 16.
(a) For the second set of data, which statement assigns the first item in the
DATA statement to a variable? ____________________
(b) Which statements assign the rest of the data to a subscripted variable?
SUBSCRIPTED VARIABLES 191
(c) Which statement prints values stored in an array instead of from the
DATA statement?
(d) Which statement tallies or adds up all the values stored in the array?
19. Suppose we RUN the program in frame 16. Show what the RUN will look
like. (Hint: Check all the PRINT statements.)
RUN
N=S
X(1) THRU XIS) ARE.
37 23 46 78 59
THE TOTAL IS 243
N=12
X(1) THRU X(12) ARE.
1 2 3 4 5 6 7 8 9 10 11 12
THE TOTAL IS 78
20. So that you can better understand and use subscripted variables in your
programming, here's a little more practice at doing what a computer does when
dealing with subscripted variables.
For this segment of a computer program, fill in the boxes showing the
values of D(G) at the affected locations after this FOR-NEXT loop has been run.
19 DIM 0(3)
29 FOR G=l TO 3
39 D(G)=2*G-1
49 NEXT G
D(1) 1 For G = 1, 2 *G - 1 = 2 *1 - 1 = 2 - 1 = 1
21. For the following FOR-NEXT loop, fill in the boxes showing the values in
R(I) through R(4) after the loop has been carried out.
19 DIM R(19)
29 FOR R=l TO 4 (Remember Rand (R) are different
3m R(R)=R+2 variables.)
49 NEXT R
R(1) 3 For R = 1, R + 2 = 1+ 2 =3
R(2) 4 For R = 2, R + 2 = 2+2 = 4
22. Let's do one more of these. Show what values will be in the boxes after this
FOR-NEXT loop has been executed.
1l! DIM P( 1l!!)
21iJ FOR N=1 TO 6
31iJ P(N)=2*N
41iJ NEXT P
P(l)
CJ P(2)
CJ P(3)
CJ
P(4)
CJ P(5)
CJ P(6)
CJ
----------
P(1)
CJ P(2)
CJ P(3)
~
P(4)
~ P(5)
~ P(6)
~
23. Next, assume that numbers are stored in C(l) through C(5), as follows:
C(l)
~ C(2)
~ C(3)
GJ
C(4)
~ C(5)
~
What will be printed if the following FOR-NEXT loop is carried out?
RUN
1 B 34 12 21iJ 17
194 ATARI BASIC
24. Suppose numbers are stored in C(1) through C(5) as shown in frame 23.
What will be printed if the following FOR-NEXT loop is carried out?
lIiJ9 DIM C( lIiJ)
119 FOR A=5 TO 1 STEP -1
129 PRINT" ", C(A),
139 NEXT A
RUN
RUN
17 29 12 34 1B
The values are printed in reverse order. If you missed this, review Chapter 6,
frames 18 and 19.
25. Assume that an election is approaching and you have conducted a poll
among your friends, using the following questionnaire.
Let's write a program to count the votes each candidate received in the poll.
You have 35 responses to your questionnaire, each response being either a "1"
\ or a "2". First, record the votes in DATA statements.
17
SUBSCRIPTED VARIABLES 195
26. How many votes did Gabby Gruff receive? ____ (Do your answers
total 32?)
15
27. To answer those last two questions, you probably counted the l's in the
DATA statements to find out how many votes Sam Smoothe received. Then you
counted the 2's to find out how many votes Gabby Gruff received.
Your computer can count votes by using subscripted variables to keep a
running total of the l's and 2's read from the DATA statements. When it comes
to the end-of-data flag (-1) it stops counting and prints the results. Here is a
program to count the votes.
ISS REM···VOTE COUNTING PROGRAM
lIS REM*'*INITIALIZE
12S DIM C(2) I C(I)=S I C(2)=S
2SS REM**·READ AND COUNT VOTES
21S READ V
22S IF V=-1 THEN 31S
23S C ( V ) =C ( V ) + 1 ......
._-------- Crucial vote-counting statement.
24S GO TO 2U1
3sm REM**·PRINT RESULTS
31m PRINT "SAM SMOOTHE,", CO)
32S PRINT "GABBY GRUFF,", C(2)
RUN
SAM SMOOTHE:17
GABBY GRUFF,15
28. After the computer executes' line 120, what are the values of C(1) and C(2)?
C(2) [::==J
196 ATARI BASIC
Both have a value of O. These are the initial values prior to counting votes. We
call the process initializing as shown in the REMark statement in line 110.
29. When your programs get so long that they start exceeding the computer's
memory capacity, delete the REMarks to save memory space, as well as to save
you time in typing in the programs you don't intend to sto~e for later use.
To save memory space, which statements could be deleted from the prog-
ram and still have it RUN as shown in frame 27? _ _ _ _ _ _ _ _ _ _ _ __
30. Look again at the crucial vote-counting statement from frame 27.
230 LET C(V)=C(V)+l
When the preceding program is RUN, what values will the computer have
stored for C(1) and C(2) after the first vote has been read and processed? (That
is, lines 210 through 230 have been done for the first vote in tpe first DATA
statement. )
C(l)~ C(2)~
C(l)~ C(2)~
C(l)~ C(2)~·
31. In the program we have been discussing (frame 27), line 220 checks for the
end-of-data flag with the following statement.
IF V=-1 THEN 310
If we switch lines 220 and 230, this section of the program would look like this:
210 READ V
220 LET C(V)=C(V)+1
230 IF V=-1 THEN 310
240 GOTO 210
(a) What would be the last value assigned to V from the DATA statement?
(b) If the computer used this value for V in line 220, what would the
subscripted variable look like? _ _ _ _ _ __
(c) Since negative subscripts are not allowed, what error message would our
computer print? _ _ _ _ _ _ _ _ _ __
(a) -1
(b) C(-1)
(c) ERROR-a bad subscript. Moral: Beware of where you place the test for
the end-of-data flag. The best place is usually right after the READ or
INPUT statement where the flag might appear.
198 ATARI BASIC
t
Did you remember the flag?
Line 910 is the value of N. In this case, N is 2 and possible votes are 1 or 2.
How would the data for the questionnaire in frame 32 be placed in DATA
statements?
200 ATARI BASIC
91616 REM
9161 REM .--- THE DATA
FIRST DATA IS NUMBER OF
9162 REM
-- POSSIBLE CHOICES
9116 DATA
919 REM
_. THE VOTES & THE FLAG (-1)
9216
9316 ______________________________
9416 ______________________________
9116 DATA 3
9216 DATA 2,2,2,1,2,1,1,2,1,1,3,2,1,3
9316 DATA 2,1,1,3,1,3,2,2,1,1,3,2,1,3
9416 DATA 1,1,2,1,2,1,1,-1
34. Your turn. Write a program to read and count votes for a questionnaire
with N different possible answers (votes), where N is less than or equal to 20.
You will have to do the following things.
(1) DIMension for the maximum subscript for C. Remember, we said N is less
than or equal to 20.
(2) Read the value of N.
(3) Set C(1) through C(N) to zero. (Use a FOR-NEXT loop.)
(4) Read and count votes until a flag is read.
(5) Print the results, as shown in the sample runs below.
Example: N = 2 Example: N 5
RUN RUN
ANSWER /I 1 • 17 ANSWER /I 1 I 12
ANSWER /I 2 • 15 ANSWER
ANSWER
/I 2 I 7
/I 3 I 9
ANSWER /I 4 9
ANSWER /I 5 I 116
SUBSCRIPTED VARIABLES 201
9U DATA 5
92lJ DATA 4,3,4,2,4,1,1,5,5,3,5,4,5,1
93lJ DATA 3,2,5,5,4,4,5,1,2,3,3,3,5,2
94lJ DATA 2,3,1,5,4,1,1,1,2,3,1,4,1,5
95lJ DATA 1,2,3 ,4, 1,-1
Write your program below. (You need not show the DATA statements.)
35. The problems in frames 25-34 dealt with counting votes. Each time through
the loop that reads data (a data loop) you added one (1) to an array element
(LET C(V) = C(V) + 1). Almost any vote counting application you might want to
try is going to be similar to the solutions in frame 34. Now you can volunteer to
be the official ballot counter for all the elections in your community-PTA, com-
puter club, school class offices, church groups, ad infinitum.
On to more serious business. A similar, but not identical application of
simple one-dimension arrays deals with counting things or money, instead of
votes.
Let's set the stage. You are the sponsor for the neighborhood computer
club for eight local kids. The kids want to buy a new superduper color graphics
terminal for the neighborhood computer system. It's only $1200 in kit form.
Raising funds is the problem. The kids agree to sell milk chocolate candy bars
for $1.00 each to raise money. (The club makes 55<1: profit per sale, not bad!).
But you must do the recordkeeping. You assign each kid a number. Whenever a
kid comes by for candy bars, you identify the kid, by number, and note how
many bars were taken. The money will be turned in later.
Here are the ID numbers assigned to each club member.
When Danny first takes 6 bars you note it as 4,6. The 4 is Danny's ID
number for the computer, and 6 is the number of candy bars he took. When
Doug takes 12 bars, your 'note is _ _ __ ____ . When Mary takes 6, the
note is _ _ __
7,12; 3,6
36. After a few weeks of this, you've accumulated quite a pile of notes. It's
time to tally and see how much money you've raised thus far. The information
from your notes will be placed into DATA statements. (You could enter the in-
formation using INPUT, but it could take much too long!) Start at line 910 (a
good place to put DATA assuming the rest of the program won't have line
numbers past 900.)
SUBSCRIPTED VARIABLES 203
(a) two
(b) Mimi took 6 bars
(c) Two. Each data element has two numbers. Two values will be read with
one READ statement. An ERROR would result if there were only one
(flag) value for the two variables in the READ statement.
37. Just as you complete entering the DATA, Bobby shows up to take another
six bars. Show how you could add this data without changing any of the existing
DATA statements. Use the highest line number permissible and still have the
end-of-data flags as the last values read by the program.
929 DATA2,6
The line number cannot be larger than 929, or the flags in 'line 930 will not be the
last data item read by the program, Moral: Be careful where you add DATA into
an existing program,
38, We need an array with eight elements, That means we need a subscripted
variable with subscripts from 1 to 8, to represent the eight members of the club,
The value added to each element (or subscripted variable) in the array will be the
number of candy bars each kid took, But first, initialize the array by writing one
multiple-statement line that will dimension the array and assign a zero to each
element. We will call this the A array and use A(X) for the subscripted variable.
1"" REM***CANDY BAR COUNTER
11" REM***INITIALIZE
12S DIM _ _ _ _ _ _ _ _ _ _ _ _ _ _ ____
12" DIM A(8) • FOR X=l TO 8 • LET A(X)=l! I NEXT X You could omit the LET.
204 ATARI BASIC
39. Now let's read the data in pairs (two items at a time). Use K for kid and Q
for quantity of candy bars, and test for end of DATA, all on one line. If we run
out of data, have the program branch to line 310.
200 REM***READ DATA AND TALLY
210 _________________________
40. Now the hard one! We must accumulate the number of bars in the correct
array element corresponding to the kid who took them. Think of the subscript K
in the subscripted variable A(K) as the kid's ID code.
220 LET ACK)=ACK)+Q I GOTO 21e
I I
I
Quantity to be added / This means, "Go back
to sales count for ·kid and read some more."
ID numberK.
If K (for kid) is 2 and Q (quantity) is 4, then line 220 will cause array
element A(_____) to increase by _______
A C 2) I 4
41. If the array elements look like those on the left be/ore, how will they look
after reading and adding the additional DATA given here?
A(l) = 8 A(l) =
(2) = 4 (2) =
(3) = 6 (3) =
(4) = 4 (4) =
(5) = 3 (5) =
(6) = 2 (6) =
(7) = 12 (7) =
(8) = 7 (8) =
Before After
SUBSCRIPTED VARIABLES 205
A(l) 8
(2) 4
(3) 14
(4) 13
(5) 3
(6) 8
(7) 14
(8) 7
Using the data in the DATA statement, show below how the report will
look after running our program.
206 ATARI BASIC
RUN
KID ID II QUANTITY
1 17
2 4
3 14
4 11
5 13
6 6
7 24
8 24
READY
43. So far our report shows us who has what but doesn't give iIs names or
totals or profits. With some help, the computer can do all those things.
Let's start with the total. In BASIC each array has an element we haven't
used yet. It's the zero (0) element. Remember, when you DIMension an array of
eight elements with a DIM statement, DIM A(8), you really get 9: 0, 1, 2, 3, 4, 5,
6,7, and 8.
Since we haven't assigned any kid the ID number 0 (zero), the "A array"
element with the subscript 0 has not been used or assigned a value. So the A(O)
element can be used for accumulating totals, although we could use some other
variable. Look at statement 410, which accumulates the total number of bars sold
in A(O).
400 REM ** TALLY TOTAL SALES & PROFIT
410 FOR X=1 TO 8.A(0)=A(0)+A(X),NEXT X
If the A array starts at zero, we must remember to change the FOR state-
ment in the initializing routine to start assigning zeros at A(O) . Replace (rewrite)
the initializing line for this program so that all the array elements are given the
initial value of zero.
130 ______________________________________________________________
44. The statement that tallies up all the candy sold by the club uses the A(O)
element in the A array to accumulate the value stored in A(l) through A(8).
If the array looks as it did in frame 41 (after reading data), then what value
will be stored in A(O) after one time through the loop in line 41O? _ _ _ __
After three times through the loop in 41O? _________
8,26
SUBSCRIPTED VARIABLES 207
45 . Write lines 420 and 430 to print the total and the profit (55¢ on each bar).
Here is what we want line 420 to print:
TOTAL IS 113
PROFIT IS 62.15
420 ________________________________________________________________________
430 _____________________________________________________________________
RUN
KID ID QUANTITY
II
17
2 4
3 14
4 11
5 13
6 6
7 24
8 24
TOTAL IS 1 13
PROFIT IS 62 , 15
Write a DIM statement to initialize N$(X) for a string long enough to use
for the longest name of any member of the computer club.
140 ______________________________________________
47. Let's say that we have placed the club members' names in a DATA state-
ment, in the same order as their ID numbers.
DATA JERRY, BOBBY, MARY, DANNY, KARL, MIMI, DOUG, SCOTT
(Note that on your video screen this line will "wrap around" to the next line.
The computer doesn't care and will consider names or numbers that are "split"
between the end of one display line and the beginning of the next as unbroken.)
Since our BASIC does not allow arrays of strings, only numeric arrays, we
must tell the computer to READ and PRINT each name at the time that it is
printing the report. Therefore, the statement that reads the names from the data
statement must be inside the FOR-NEXT loop that prints the report (lines 320
and 340).
Notice that no "end of data" flag is needed since the FOR-NEXT loop only goes
around 8 times, and there are 8 names in the DATA statement.
There is now the touchy problem of where to place the DATA statement
that contains the club members' names. Keep in mind that we want to avoid an
error message just because a READ statement ~ith a numeric variable came upon
a DATA statement containing strings!
Where should the DATA statement with club members names be placed in
theprogram? _________________________________________________________
At the end, since all numeric data is read earlier in the program.
48. Now here is the real test of your understanding. Ready to play computer?
Show what a RUN of this program would look like.
SUBSCRIPTED VARIABLES 209
RUN
NAME QUANTITY
JERRY 17
BOBBY 4
MARY 14
DANNY 11
KARL 13
MIMI 6
DOUG 24
SCOTT 24
TOTAL IS 113
PROFIT IS 62.15
49. Let's look at one other application of singly-subscripted variables. This one
neither counts votes nor accumulates anything.
No doubt you've read about computer dating. Ever wonder how it works?
You answer a series of questions that are then stored in the computer; other peo-
ple who wish to be matched do likewise. Then the responses are compared to test
for "compatibility." Let's do a simplified version of a computer dating program
which you can alter for your own uses.
We'll use a multiple-choice questionnaire with only five questions. (Ask
anything you'd like!) Responses will be stored in DATA statements, with the
name first, and the five responses following. My responses appear in line 910 on
the next page.
210 ATARI BASIC
Now, you create DATA statements in lines 920-970 for these respondents,
plus an end-of-data flag.
Ql Q2 Q3 Q4 Q5
JOAN 1 4 2 2 1
TONI 2 2 2 3 3
LAURA 2 3 3 1 2
MARY 3 3 4 2 1
IRENE 3 1 4 2 1
92a ________________________________________
93a ________________________________________
95a ________________________________________
96a ________________________________________
----------
92a DATA JOAN, I , 4 , 2 , 2 , I
93a DATA TONI,2,2,2,3,3
94a DATA LAURA,2,3,3,I,2
95a DATA MARY, 3, 3,4,2, I
96a DATA I RENE, 3 , I .4,2 , I
97a DATA END
50. Now, you write the rest of the program with some gentle supervision. First,
you will need a DIM statement that will allow the program to compare "his"
responses (stored in array C), with "her" responses (stored in array A). Also
initialize string variable N$ for "his" and H$ for "her."
laa REM···COMPUTER DATING SIMULATION
la5 REM···INITIALIZE
lla ________________________________________
IU DIM C(5),A(5),NS(U),HS(la)
Note: You can DIMension more than one array in one DIM statement.
51. Next, READ the first data statement which contains "his" name, and
READ "his" five responses. The responses should be read into array C using the
method shown in frames 8 and 18.
Then print "his" name and responses all on one line, so that later you can
compare them with "hers."
SUBSCRIPTED VARIABLES 211
210 READ NS
2 20FOR X=l TO 5.READ C.C{X)=C. NE XT X
230 PRINT NS,
240 FOR X=l TO 5.PRINT C{ X )," "1 .NEXT X.
PRINT _ ......_ _--,
52. Next, READ one set of " her" names and responses into array A, and print
"her" responses so you can visually compare the "his" responses with "her"
responses. After you read " her" name in line 310, check to see if it is the end-
of-data flag (the word END) . Use the string comparison IF H$ = "END"
THEN END. The output we want so far should look like this.
RUN
LEROY 33421
JOAN 14221
Complete the program segment to produce this output or printout.
300 REM • • 'HER' NAMES & RESPONSES
310 _____________________________________________________________________
320 _____________________________________________________________________
330 ____________________________________________________________________
340 _____________________________________________________________________
53. Now comes the crucial part. This is where you compare the contents of ar-
ray C with the contents of array A and add the number of matches into variable
M. Think about that and complete the program below, so that a RUN of the
complete program looks like the one here.
RUN
LEROY 3 3 4 2 1
JOAN 1 422 1
2 MATCHES
TONI "2 2 2 3 3
9 MATCHES
LAURA 23312
1 MATCHES
MARY 3 3 4 2 1
5 MATCHES
IRENE 3 1 4 2 1
4 MATCHES
499 REM .*
COMPARE & COUNT 'MATCHES'
419 M=9
429 FOR X=1 TO 5
479 PRINT
419 M=9
429 FOR X=1 TO 5
439 IF C( X )<>A(X) THEN 459
449 M=M+1
459 NE XT X
469 PRINT M,t! MATCHES"
479 PRINT
489 Goto 319
SUBSCRIPTED VARIABLES 213
RUN
LEROY 3 3 4 2
JOAN 4 2 2
2 MATCHES
TONI 2 2 2 3 3
0 MATCHES
LAURA 2 3 3 1 2
1 MATCHES
MARY 3 3 4 2 1
5 MATCHES
IRENE 3 1 4 2 1
4 MATCHES
214 ATARI BASIC
SELF-TEST
If you can complete the Self-Test on subscripted variables, you will be ready for
the next chapter, which will expand your programming ability to include the use
of more complex subscripted variables. Therefore, it is important that you have
the information in this chapter well in hand.
Q~ A(l)~
A~ A(2)~
Al~ A(3)~
A(4)~
Remember, A, AI, and A(1) are distinct variables. Write the value of each
variable below.
(a) A(Q)
(b) A(A)
(c) A(Al)
(d) A(A(2»
(e) A(A(Q»
(f) A(A-Q)
(g) A(A+Al)
(h) A(2*Q)
SUBSCRIPTED VARIABLES 215
910 DATA 12
920 DATA 3,6,-2,0,9,0,7,3,-5,4,-1,7
5. Modify the vote-counting program of frame 27 so that the total votes (for
both candidates) are also printed. The printou~ might look like this . .
RUN
SAM SMOOTHE. 19
GABBY GRUFF. 16
TOTAL VOTES. 35
RUN
RUN
8. Your computer club kids decide they want to find out who is the best
programmer in their group. As club sponsor, you decide to give them a
multiple choice test on the programming concepts and correct the test using
the computer. The mUltiple choice answers will be the numbers 1, 2, 3, 4,
or 5. There are 10 questions on the test. You enter the 10 correct answers in
the first DATA statements in the program. These are read into array K. In
subsequent DATA statements, you first enter the name of the club member,
followed by the 10 answers that person gave for the test. (Enter in array R.)
Your task is to write a BASIC program that will correct the tests and print
a report that looks similar to the one below.
RUN
NAME SCORE
DANNY 5
KARL 5
MIRIAM 4
SCOTT 7
SUBSCRIPTED VARIABLES 217
9. Are you curious about your chances of winning at any game using dice? If
you developed a program to simulate the rolling of one die and counted
how many times each side appeared, you would get a better idea of your
chances of winning.
Write a BASIC program to simulate the roll of one die, 1000 times (or
make that an input variable). After each simulated roll, accumulate or add
to the correct array element one appearance or "vote." After 1000 rolls,
print the contents of your array in report form showing how many times
each die side appeared during the computer simulation. Your report should
look like the one below. There is room to write your program on the next
page.
RUN
HOW MANY SIMULATED ROLLS? 11a1a1a
DICE ROLL NO. OF OCCURENCES
1 159
2 152
3 173
4 142
5 189
6 185
Answers to Self-Test
The frame numbers in parentheses refer to the frames in the chapter where the
topic is discussed. You may wish to refer back to these for quick review.
l. (c), (f), and (h) are legal subscripted variables. Longer variable names such
as item (e) may be legal on your computer. Check the reference manual for
your computer. (frame 1)
2. (a) 4
(b) 23
(c) 37
(d) 19 A(A(2» = A(4) = 19
(e) 19 A(A(Q» = A(A(2» = A(4) 19
(f) 37 A(3-2) = A(l) = 37
(g) 19 A(3 + 1) A(4) 19
(h) 19 A(2+2) = A(4) = 19
(frame 4)
218 ATARI BASIC
3. RUN
23 37 " 67
(frames 7-8)
4. 10
The computer would print an error message.
(frames 11-12)
33" PRINT
340 PRINT "TOTAL VOTES,",C(1l+C(2)
(frames 43-46)
7. lIa REM
15 REM
2" DIM NS(I"),T(5),G=",FOR X=1 TO 5,T(
X)=0,NEXT X
25 REM
30 READ P,D . IF P=-1 THEN 6"
40 T(P)=T(P)+D,G=G+D,GOTO 30
50 REM
60 PRINT "NAME","AMOUNT COLLECTED"
70 FOR P=1 TO 5.READ NS:PRINT NS,T(P).
NEXT P
60 PRINT "TOTAL.",G
90" REM
910 DATA 2,45,1,75,3,25,4,10",3,25
920 DATA 5,125,3,50,1,5",2,12",2,4"
930 DATA 5,75,-1, - 1
94" DATA FRED,JOANN,MARYJO,JERRY,BOB
RUN
NAME AMOUNT COLLECTED
FRED 125
JOANN 205
MARYJO
JERRY
1""
lIa"
BOB
TOTAL
2""
73"
8. U REM
2e REM
3e DIM K(le),R(le),N$(le)
4e REM
se PRINT "NAME","SCORE" I PRINT
6e REM
7e FOR X=1 TO Ie
8e READ K I K(X)=K
90 NEXT K
ue REM
lie READ N$ I IF N$="END" THEN END
12e FOR X=1 TO Ie
130 READ R : R(X)=R I NEXT X
140 S=0
Ise REM
160 FOR X=1 TO Ie
17e IF K(X)<>R(X) THEN 190
18e S=S+1
1ge NEXT
2ee PRINT N$,S I GO TO 110
gee REM
9 1 e DATA 3,4,3,3,5, 1 ,2,3,2, 1
92e REM
93e DATA DANNY,1,2,3,4,S,1,2,3,4,S
94e DATA KARL,1,3,2,4, 1,2,3,2,1
9se DATA MIRIAM,3,2,2,1,4,1,2,3,1,2
960 DATA SCOTT,1,2,3,3,S,1,2,3,2,2
970 DATA END
RUN
NAME SCORE
DANNY 5
KARL 5
MIRIAM 4
SCOTT 7
(frame 49-53)
9. 10 REM
20 DIM 0(6)
30 FOR X=1 TO 6 I D( X)=e : NE XT X
4e PRINT "HOW MANY SIMULATED ROLLS";
se INPUT R
60 FOR X=1 TO R
7e N=INT(6*RND(I»+1 I D(N)=D(N)+1
8e NEXT X
ge PRINT "SPOTS","II OF OCCURENCES"
1e0 REM
110 FOR X=l TO 6
12e PRINT X,D(X)
13e NEXT X
RUN
HOW MANY SIMULATED ROLLS?leee
SPOTS II OF OCCURENCES
1 168
2 144
3 172
4 182
5 159
6 175
(frames 25-31)
CHAPTER EIGHT
Double Subscripts
+ + ;
One subscript One subscript One subscript
220
DOUBLE SUBSCRIPTS 221
T(2,3)
4.
Two subscripts,
separated by a comma
1; 2
3;4
A(2,3)
Rolt
Column
A(3,2)
222 ATARI BASIC
L ET B(I . 3)=0
LET B( I.I )=49
LET B(2.3)=B(2. 1) - B ( 1 , 1 )
LE T B( 1,2)=2*B(2,1)
LET B (2 , 2) =I N T (B(2,1) / B(2 , 3 »
P(7,5)
7. Let's assume that the following values (in the boxes) have been assigned to
the corresponding variables. Note that there are both simple and subscripted
variables.
8 7
2 1
3 7
8 5
o 9
16 T(R+l,C-2) = T(2+1,3-2) = T(3,1)
124 ATARl BASIC
8. Election time again. (Before starting on this, you may wish to review the
vote-counting application in Chapter 7.) The questionnaire below requires two
answers.
Since there are two questions, each reply consists of two numbers: the
answer to question 1 and the answer to question 2. We will use V (for Vote) to
represent the answer to question 1 and A (for Age) to represent the answer to
question 2.
/V,A~
Answer to question 1 Answer to question 2
lor2
9. We sent out some questionnaires. Some typical replies are shown below.
Reply Meaning
1,1 one vote for Sam· Smoothe, voter is under 30
1,2 one vote for Sam Smoothe, voter 30 or over
3,1 no opinion, voter is under 30
Under 30 30 or over
In other words, C(1,1) will hold the count for Sam Smoothe by people
under 30. C(1,2) will hold the total for Sam Smoothe by people 30 or over.
C(2,1) will hold the total for _ _ _ _ _ _ _ _ _ _ by people
What subscripted variable will hold the No opinion count for people 30 or
over? _ _ _ _ _ __
11. Here are 29 replies to our questionnaire. Remember, each reply is a pair of
numbers and represents one vote. The first number of each pair is the answer to
question 1. The second number of each pair is the answer to question 2.
Under 30 30 or over
C(l,l) 4 C(1,2) 1 5
C(2,1) 7 C(2,2) 1 5
C(3,1) 6 C(3,2) 1 2
12. Naturally, we want the computer to do the counting. Below is the beginning
of our program.
100 REM*** VO TE COUN TIN G , TW O QUESTIONS
1 10 DIM C(3,2)
The DIM statement (line 110) defines an array with at most 3 rows and 2
columns. That is, the DIM statement defines an arr~y of doubly-subscripted
variables in which the maximum value of the first subscript is 3 and the max-
imum value of the second subscript is 2. You must always DIM doubly-
subscripted arrays or you'll get an error message.
DIM (3,2)
Maximum valuef
of first subscript
l Maximum value
of second sUbscript
Next, we want to set all counts to zero. That is, we want to assign zero to
C(I,I), C(1,2), and so on up to C(3,2). Even though other versions of BASIC
might do this automatically, it is good programming practice to initialize every
program. You complete this part of the program.
200 REM***INITIALI ZE : SET ALL COU NT S TO ZERO
DOUBLE SUBSCRIPTS 227
Method 4
210 FOR K=1 TO 3 I FOR L=1 TO 2 I LET C(K,L)=0 I NEXT L I NEXT K
We will usually use Methods 3 and 4 because they are easily generalized to arrays
of different sizes. Using Method 3, we can add more rows by changing line 210,
more columns by changing line 220. (Of course, we would also ha,ve to change
the DIM statement.)
l3 . The array is now set up . Let's READ and count the votes .
Line 320 is the crucial vote-counting statement. It adds 1 (vote) to the array
element specified by V and A. Suppose this is the array before executing lines 310
and 320.
C(l,l) o C(1,2) 4
C(2,1) 2 C(2,2) 7
Show how the array would look after this additional data was processed.
910 DATA 3,1,2,2,3,2,1,2
. C(2,1) C(2,2)
C(3,1) C(3,2)
C(2,1) 2 C(2,2) 8
C(3,1) 6 C(3,2) 1
14. Since line 310 is a READ statement, some DATA statements must be given
somewhere. Here they are, featuring the data from frame 11.
If the computer could not find a value for READ variable A as well as variable
V in line 310, it would print a data error message and stop.
15. Only one task remains-print the results! For the data shown in frame 14,
the results should look like the one on the. following page when the program is
RUN.
DOUBLE SUBSCRIPTS 229
RUN
CAND. UNDER 30. 30 +
SMOOTHE 4 5
GRUFF 7 5
NO OPIN 6 2
You will have to change lines 110, 210, 410, 430, 440, and 450.
110 __________________________________________________
210 __________________________________________________
410 __________________________________________________
430 ________________________________________________
440 ________________________________________________
450 __________________________________________________
Note: Even though we changed the questionnaire, we didn't have to change the
crucial vote-counting statement in line 320.
DOUBLE SUBSCRIPTS 231
17. Here is a LISTing of the modified program and new DATA statements for
the questionnaire in frame 16. You be the computer and print the output for the
program when it is RUN.
RUN
SMOOTHE 4 5 5
GRUFF 6 3 5
NO OPIN. 6 5 5
232 ATARI BASIC
Note that a PRINT statement must end lines 430 and 440. You must use PRINT
to tell the computer to start a new line at character position O. Also, the above
program lines will each occupy more than one line on your display.
19. Do you remember the problem on counting candy bars? You might want to
review it in Chapter 7 before we go on.
Before, we were selling one chocolate candy bar for $1.00, on which the
profit was $.55. Now let's add another product-a bag of jelly beans which sells
for $.50, on which we profit $.30. Our job is to re-program our computer to
tabulate individual sales and overall profits. Here's the report we'd like to
produce. RUN
KID 10 /I TOTAL $ CHOC J.BEANS
2
"3 "3 "
3
"
4 6" "3 "6
5 U, 6 8
6 18
7 "
18 18
18
18
8 12 6 12
TOTALS. 31 18 26
PROFITS: 17.7 9.9 7.8
(a) From the report above, who had the greatest overall dollar sales?
(b) Who sold the most chocolate bars? __________
(c) The most jelly beans? ______
(d) Can you tell who made us the most profit? _________
DOUBLE SUBSCRIPTS 233
(a) 8 (c) 8
(b) 5 and 8 (d) not directly from the report. (It's number 8.)
20. For record keeping purposes we will prepare some preprinted forms to keep
track of who takes what. Now, when one of the kids asks for candy we fill in a
form like the one on the following page.
Name Danny
IDNo. 4
1. Milk Chocolate 3
2. Jelly Beans _6__
This shows that Danny (number 4) took 3 chocolate bars and 6 bags of jelly
beans. When we convert that into computer data we use this format.
DATA
,
Kid ID No.
4, 1, 3,
~
Quantity
.
,
Kid ID No.
4,2,6
~
Quantity
i. i.
Chocolate Jelly beans
(Notice how we designed the form to conform to our system of data entry!)
From the three forms below, complete the DATA statements. Include end-
of-data flags.
930 _______________________________________________________________
1 = Jerry
2 = Bobby
3 = Mary
4 = Danny
5 = Karl
6 = Mimi
7 = Doug
8 = Scott
(1,1) (1 ,2)
(2,1) (2,2)
(3,1) (3,2)
(4,1) (4,2)
(5,1) (5,2)
(6,1) (6,2)
(7,1) (7,2) .
(8,1) (8,2)
!
Kid ID No.
locolaw
i
Quantity ! ~ellY
Kid ID No.
beam
i
Quantity
Your first task is to write the statement(s) to initialize the array and zero it out,
that is, set the dimensions and set the. values for all the variables to zero. Don't
forget the zero element. We'll be using it, too!
DOUBLE SUBSCRIPTS 235
13S __________________________________________________________________
14S __________________________________________________________________
159 __________________________________________________________________
1) 29 DIM A(8,2)
13S FOR X=9 TO 8 . FOR Y=9 TO 2
14S A( X,Y)=S
15S NE XT y ,NE XT X
22. Now READ a set of data (K,C,Q) and test for the end-of-data condition.
When you encounter the end-of-data flag, jump to 410.
2~~ RE M* * *READ DAT A AND TEST F OR F L AG
2 1 ~ __________________________________________________________________
23. The heart of this program is found in line 220, where the "accumulation"
takes place.
(a) The value of what array element (subscripted variable) will be increased?
(a) A(4,1)
(b) 3
236 ATARI BASIC
(1,1) 7 (1,2) 10
(2,1) 0 (2,2) 0
(3,1) 6 (3,2) 8
(4,1) 5 (4,2) 12
(5,1) 3 (5,2) 0
(6,1) 6 (6,2) 8
(7,1) 8 (7,2) 9
(8,1) 3 (8,2) 10
/locolate i
Quantity / lny beans
i
Quantity
Kid ID No . Kid ID No .
How will it look after accumulating the data in these DATA statements?
910 DATA 4,1,3, 4,2,6
920 DATA 5,1,6, 5,2,8, 2,1,3, 2,2,0
930 DATA 8, 1,6, 8,2, 12
A A
(1,1) (1,2)
(2,1) (2,2)
(3,1) (3,2)
( 4,1) (4,2)
(5,1) (5 ,2)
(6,1) (6,2)
(7,1) (7 ,2)
(8,1) (8,2)
----------
DOUBLE SUBSCRIPTS 237
A A
(1,1) 7 (1,2) 10
(2,1) 3 (2,2) 0
(3,1) 6 (3,2) 8
(4,1) 8 (4,2) 18
(5,1) 9 (5,2) 8
(6,1) 6 (6,2) 8
(7,1) 8 (7 ,2) 9
(8,1) 9 (8,2) 22
Now let's print a preliminary report like the one in frame 19, but without
any totals. You fill in the blanks in the program on the following page. This is
how the report should look when the program is RUN.
RUN
KID ID /I CHOC J.BEANS
0 0
2 3 0
3 0 0
4 3 6
5 6 8
6 0
7
"
" "
8 6 12
238 ATARI BASIC
400 REM***REPORT WI
450 PRINT
460 NEXT C
-----------------, . The comma is important.
470 PRINT
480 NEXT
26. In frame 25, why are the commas placed at the end of lines 430 and 450?
to suppress the normal carriage return after the PRINT (that is, to keep the com-
puter output on the same line)
to force a carriage return (start a new line of display), so the next KID ID No.
will be printed in the correct place or position when line 420 is executed again.
DOUBLE SUBSCRIPTS 239
'2 3 /lJ
3 /lJ /lJ
4 3 6
5 6 8
6 /lJ /lJ
7 /lJ /lJ
8 6 12
(1,0)
,...Cl) (2,0)
Cl)
.c:
0 (3,0)
0.0
~
.~ .
(4,0)
CJl
Cl)
~
CJl (5,0)
~
+"
0
Eo-< (6,0)
(7,0)
(8,0)
You fill in the blank lines in the program on the following page. Line 120 is
fixed to set all array elements (boxes) to zero, starting at the 0,0 element. Line
320 adds the total sales per kid (remember, jelly beans sell for $.50 per bag). And
line 350 adds units sold.
240 AT ARI BASIC
340 FOR C=
360 NEXT C ,
370 REM ** CALCULATE PROFITS
29. Did you understand what went on in lines 320, 330, and 350? If J?,ot, go
back and review the problem a little more. These statements are crucial for your
complete understanding of this type of problem.
Write the line numbers in the labeled boxes below to show which lines
generate values for those boxes.
(1,0)
(2,0)
(3,0)
(4,0)
(5,0)
(6,0)
(7,0)
(8,0)
DOUBLE SUBSCRIPTS 241
(1,0) 320
(2,0) 320
(3,0) 320
(4,0) 320
(5,0) 320
(6,0) 320
(7,0) 320
(8,0) 320
330 A(0.0)=AC0.0)+ACK.0)
31. In line 390, how would you describe what the statement P = PI + P2
accomplishes?
390 P = Pi + P2
32. Time to redo our report printing section to update everything shown in the
RUN on the following page (the same RUN as in frame 19). Fill in the blanks.
242 ATARI BASIC
RUN
KID 10 /I TOTAL $ CHOC J.BEANS
0 0 0
2 3 3 0
3 0 0 0
4 6 3 6
5 10 6 8
6 0 0 0
7 0 0 0
8 12 6 12
TOTALS, ,3 1 18 26
440 FOR C= _ _ _ _ _ _ _ _ _ _ __
450 PRINT A _ _ _ _ _ _ _ _ _ __
460 NEXT C , _ _ _ _ _ _ _ _ _ __
470 NEXT K
480 PRINT "TOTALS,",A(0,0 l,A(0,1l,A(0,2)
Now the program is done and the RUN should look just like the one above. On
the following page is a complete listing of the program.
DOUBLE SUBSCRIPTS 243
32. Here is one final double array application. In a small class of 8 students,
each student has taken 4 quizzes, The scores are shown below.
Let S(B,J) be the score obtained by student B on quiz J, S(5,2) is the score
obtained by student __ on quiz __ , What is the value of S(5,2)? __
5; 2; 82
244 ATARI BASIC
33. Another class might have 30 students and 5 quizzes per student. Still
another class might have 23 students and 7 quizzes per student, and so on. Let's
begin a program to read scores for N students and Q quizzes per student.
30; 10
34. Next, we want to read the values of Nand Q for a particular set of
scores-in this case, the scores shown in frame 32. For this set of scores the value
of N (number of students) is ____ and the value of Q (number of quizzes) is
8; 4
35. We will put the values of Nand Q and the scores in DATA statements.
Now the program looks like this.
Your turn. Complete line 120, below, to READ the values of Nand Q.
120 _ _ _ _ _ _ _ _ __
36. The values of Nand Q read by line 120 (in frame 35) will be read from
which DATA statement? Line ______
910
37. Now let's read the N by Q array of scores using FOR-NEXT loops. Fill in
the blanks.
130 FOR X=1 TO N. FOR y= _____ ;READ _____,NE XT y , NE XT X
38. The numerical values read by line 130 are stored in the DATA statements,
lines through ______
920; 990
39. Now that we have the array in the computer, what shall we do with it? One
thing someone might want is the average score for each student. Let's do it,
beginning at line 200.
200
210
220
230
240
FOR B= 1 TO N
LET T=0
-FOR J= 1 TO Q
J--
REM •• COMPUTE & PRINT AV ER AG ES
PRINT "STUD. II", "AVERA GE"
268. This is the sum of the 4 scores for student 1. Remember, Q = 4. Therefore,
line 250 will be done for J = 1, J = 2, J = 3, and J = 4.
146 ATARI BASIC
RUN
STUD. /I AVERAGE
1 67
2 87.25
3 80.75
4 43.25
5 82.25
6 7f!J
7 96.75
8 78.75
,
Your turn. Beginning with line 300, write a program segment to compute
and print the average score for each quiz. For the data used in the program, the
results might look like this.
RUN
QUIZ /I AVERAGE
75.5
2 72.75
3 78.125
4 76.625
300 REMARK COMPUTE AND PRINT AVERAGE OF EACH QU I Z
310 PRIN T " QU I Z /I"," AVERAGE"
320 FOR J = 1 TO Q
330 LET T= 0
340 FOR B=1 TO N
350 LET T=T +5(B , J )
360 NE XT B
370 LET A=T /N
380 PRINT J , A
390 NE XT J
42, Suppose some students take a multiple-choice quiz, 10 questions with four
possible answers per question. We want to know how many students gave answer
number 1 to question number 1, how many gave answer number 2 to question
number 1, and so on.
Here are the answers given by 7 students. Each set of answers is in a DATA
statement. The last DATA statement is a "fictitious student" and really mean~
"end of data."
900
910
-920
REM
DATA
DATA
-- STUDENT ANSWERS TO QUI Z
2,3,1,1,1,2,4,3,4,1
2,3,2,4,1,2,4,2,1,1
In each line of data, the first number is
the answer to question 1, the second
930 DATA 2.3,2.4.1.2,4t ~ tltl number is the answer to question 2, and
940 DATA 3,2,4,1,1,2,3,3,4,1 so on.
950 DATA 2 • 3 , 4 • 1 t 1 , 3 , ;4 • 3 • 4 • 1
960 DATA 2,1,2,3,1,2,4,3,4,2
970 DATA 3,4,1,1,1.4.3,1 t 4, 2
980 DATA -1,0,~,0,0,~,0,0,0,0 "Fictitious student"
Student 1 (line 910) gave answer 1 to question 3. Student 5 (line 950) gave
answer ____ to question 9. Student 7 (line 970) gave answer ____ to
question 1.
4; 3
148 AT Am BASIC
43. Complete the following table showing the number of students giving each
answer (1,2, 3, or 4) to questions 1,2, and 3.
4 1
2 3 o 2
44. In frame 43, with your help, we have shown how the 7 students answered
the first 3 questions. The totals look like a 3 by 4 array. If we had continued for
all 10 questions the totals would have looked like a _ _ _ _ _ by 4 array.
10
45. Let's define an array T with 10 rows and 4 columns to hold the totals.
Complete the following DIM statement.
100 REM***aUIZ ANALYSIS PROGRAM
110 DIM _ _ _ _ _ _ _ ___
46. For each student there are 10 answers. Let's define a list of answers A(l)
through A(IO). Complete the following DIM statement.
120 DIM A( 10 l
DOUBLE SUBSCRIPTS 249
47. We can save space by combining the two DIM statements into one DIM
statement.
110' DIM T(10',4l,A(10'l
48. Here is the beginning of a program to read the students' answers and com-
pute the totals array.
Next, we want to initialize the totals array. That is, we want it to be a zero array.
You do it.
120' REM ** INITIALIZE
130'------------------------------------------------------------------___
140' _____________________________________________________________________
150' _____________________________________________________________________
49. Write a statement to read the list A of answers for one student.
20'0' REM ** READ ONE SET OF ANSWERS
210'--_________________________________________________________________
220' ___________________________________________________________________
230' _____________________________________________________________________
AU)= -I
51. If the data are for a real student, we want to update the running tally in the
T array. We did it this way.
300 REM ** UPDATE THE iOTAL S ARRA Y
310 FOR 0=1 TO 10
320 T(O,A(O»=T(O,A(O»+I
330 NEXT 0
Here are the answers for one student. These are the values of A(1) through
A(10).
2,3,1,1,1,2,4,3,4,1
Suppose Q = 1. Then A(Q) = _ _ _ and T(Q,A(Q)) is T(_ _ _ _ _ _).
52. In the above case (frame 51), what happens when the computer obeys line
320? _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ____
The total in T(1,2) is increased by one. (It's just like counting votes!)
53. Since line 320 is in a FOR-NEXT loop, it will be done for each value of Q
specified by the FOR statement. That is, it will be done for Q = 1, 2, 3, 4, 5, 6,
7, 8, 9, and to. When Q = 10, which element of the T matrix is increased by one?
T(_ _ _ _ _ _.) .
54. Let's move on. After tallying the answers for one student, we want the
computer to return to line 210 and read another set of answers. (See frame 49.)
400 REM**ANOTHER SET OF ANSWERS
410 GO TO 210
Then the IF-THEN statement (frame 50) is encountered again. The IF-THEN
statement causes the computer to go to line 510 if a fictitious student has been
read. In that case, we want to print the headings and results, then stop.
Here is what the RUN should look like:
RUN
Alii AII2 AII3 AII4
5113 0 5 2 0
5112 4
5113 2 3 0 2
5114 4 0 2
5115 7 0 0 0
5116 0 5
5117 0 0 2 5
5118 2 4 0
5119 2 0 0 5
51110 5 2 0 0
530 _____________________________________________________________________
540_____________________________________________________________________
SELF-TEST
Good for you! You have reached the Chapter 8 Self-Test. These problems will
help you review the BASIC instructions you have learned for dealing with arrays
of numbers, using variables with double subscripts.
Column 1 Column 2
Row 1 1 2
Row 2 3 4
Row 3 5 6
9. Your city officials seek to conduct a census of the citizenry. Among other
things, they want to analyze the age and sex breakdown of the community.
They ask you to program your home computer to take data from the form
below and prepare a report like the RUN shown.
4. 22-30
- 5. 31-40
r---
6. 41-50
r-
7. 51-65
r-
8. over 65
'--
RUN
AGE TOTAL MALE FEMALE
<13 3 2
13-15 3 2
/ 16-21 7 4 3
22- 33 4 2 2
31-43 6 2 4
41-53 5 3 2
51-65 6 2 4
>65 6 3 3
TOTALS 43 19 21
156 ATARI BASIC
10. Your brother-in-law owns the local "jeans" store. He carries a large inven-
tory with tremendous variety but now finds he must cut back. The question
is which items to cut back. He asks you to use your computer to analyze his
sales and help him make the "which-item" decisions. He wants to analyze
sales of children's pants first. He carries 9 styles in children's pants, each
style with 3 colors. For your program, you should number the styles 1
through 9 and the colors 1 through 3, so that each piece of data will look
like that below.
DATA 1, 2, 12
RU N
511 TOT$ COLOR COLOR 2 COLOR 3
1 51 24 13 14
2 96 34 10 52
3 153 58 49 46
4 66 61 10 15
5 111 86 13 12
6 72 48 12 12
7 39 . 13 13 13
8 42 14 14 14
9 57 12 15 27
Answers to Self-Test
The frame numbers in parentheses refer to the frames in the chapter where the
topic is discussed. You may wish to refer back to these for quick review.
1. (b), (d), (e), and (0 are legal (Longer variable names such as item (c) may
be legal on your computer. Check the reference manual for your
computer.) (frames 5-7)
2. 3,2 meaning 3 rows, 2 columns (frames 2-4)
3. 100 DIM A(3,2) (frame 12)
4. A(3,2) (frames 9-11)
5. (a) 1; (b) 5 (frame 11)
6. (a) 4; (b) 6 (frame 11)
7. 4 (frame 11)
8. 10'DIMM(I0',lIin
20' FOR R=1 TO 10
30' FOR C=1 TO 10
40' M(R,C)=0
50' NEXT C
60' NE XT R
(frame 12)
9. 100' REM •• POPULATION AGE/SEX ANALYSISI0'
0' REM •• POPULATION ANALYSIS
110 REM •• INITIALIZE
120' DIM V(8,2),A$(6)
130' FOR A=0' TO 8,FOR S=0' TO 2
140' V(A,S)=0'
150' NE XT S,NE XT A
20'0 REM •• READ DATA,TEST,ACCUMULATE
210' READ A,51IF A=-1 THEN 310'
220' V(A,S)=V(A,S)+IIV(0,0)=V(0',0')+1
230 V(0,S)=V(0',S)+I,V(A,0)=V(A,0')+1
240' GOTO 210
30'~ REM •• PRINT REPORT
310' PRINT "AGE","TOTAL","MALE","FEMALE"
320' FOR A=1 TO 8
330' READ A$,PRINT A$,V(A,0),
340' FOR 5=1 TO 2
350 PRINT V(A,S),
360' NEXT S,PRINT ,NEXT AIPRINT
370' PRINT "TOTALS", V(0', 0'), V( 0',1), V( 0,2)
900' REM •• DATAl BY AGE GROUP,SEX
910 DATA 1,2,2,1,3,2,4,1,5,2,6,2,7,2
920 DATA 8,1,1,2,1,1,2,1,2,2,3,1,3,1
930' DA T A 3,2, 3, 1 , 3 , 2 ,4, 1 , 4 , 2 , 5 , 1 , 5 , 1
940' DATA 5,2,5,2,6,1,6,2,7,1,7,2,7,2
95" DATA 7,2,8,1,8,1,8,2,8,2,3,1,4,2
960' DATA 5,2,6,1,6,1,7,1,8,2,-1,-1
969 REM •• TABLE HEADINGS
970 DATA <10,10-15,16-21,22-30',31-40',41-
50,51-65,>65
(frame 19)
DOUBLE SUBSCRIPTS 259
(frame 32)
CHAPTER NINE
String Variables
and String Functions
5 DIM T$(SIlI)
llll LET T$="STRING FOR THE STRING VARIABLE '1"$"
\Th"IS IS a .
st~mg . ble.
vana
260
STRING VARIABLES AND STRING FUNCTIONS 261
You identify a string variable by using any legal variable name followed by
a dollar sign ($). String variables permit you to manipulate alphanumeric data
with greater ease. String variable instructions include LET, PRINT, INPUT,
READ, DATA, and IF-THEN, plus special string functions.
As we noted in earlier chapters, you must use a DIM statement at the
beginning of the program that is executed only once, to tell the computer how
much memory space to save for the string variables used in the program. A string
assigned to a string variable using a LET assignment statement is limited to about
99 characters. This is due to the limitations on statement line length, including
the "wrap around" to the next lines on the display. How many characters in the
string variable T$ above? ___________ (Did you count blank
spaces?)
33 characters
2. As you know, you can assign values to a string variable using an INPUT
statement that asks for one or more string variables to be entered by the user.
Example 1
5 DIM NS(30)
10 PRINT "WHAT IS YOUR NAME"; .........- - - - - - - One string input
20 INPUT NS
Example 2
5 , DIM NS(30),SS(30)
10 PRINT "YOUR NAME AND SUN SIGN"; t----:--Two string inputs
....
20 INPUT NS,SS
Example 3
5 DIM NS(30),CS(30),SS(30)
10 PR I NT "YOUR NAME, CITY, AND STATE YOU ~ Three string inputs
LIVE IN";
20 INPUT NS,CS,SS
You may also assign numeric and string variables in the same INPUT statement
as shown here.
Example 4
5 DIM N$(30)
10 PRINT "WHAT IS YOUR NAME AND AGE" I
20 INPUT NS, A ...~
....._ _ _ _ _ _ _ _ _ _ _ _.J.
I Mixed inputs, ~ne string
and one numenc.
When an input statement with more than one variable, string or numeric, is en-
countered by the computer while executing a program, it types a question mark
and waits for the user to enter the input string or value from the keyboard.
262 ATARI BASIC
Let's say we have entered Example 2 above into the computer. We type RUN
and hit the RETURN key. The display shows:
RUN
YOUR NAME AND SUN SIGN?
RUN
YOUR NAME AND SUN SIGN? JERALD R. BROWN
RUN
YOUR NAME AND SUN SIGN? JERALD R. BROWN
? SCORPIO
If I had typed both of the things asked for after the first question mark, they
would have both been assigned to N$. If I had just pressed RETURN without
entering anything after a question mark, a "null string" (a string with no
characters) would have been assigned to the string variable.
5 DIM N$(3.0)
1.0 PRINT "WHAT IS YOUR NAME AND AGE" ;
2.0 INPUT N$,A
3.0 PRINT "NAME: " ;N$
4.0 PRINT "AGEl " ; A
Show what the computer will print and how you should enter the information re-
quested if the above program is RUN.
RUN
RUN
WHAT IS YOUR NAME AND AGE? PAUL ARMITIGE
?33
NAME. PAUL ARMITIGE
AGE. 33
STRING VARIABLES AND STRING FUNCTIONS 263
3. 10 DIM NS(15)
20 PRINT "YOUR NAME"I
30 INPUT NS
40 PRINT "YOUR NAME IS "INS
RUN
YOUR NAME? JERALD RICHARD BROWN
YOUR NAME IS JERALD RICHARD
What happened? Did the computer forget my last name? Take a look at the DIM
statement. Since string variable N$ was only dimensioned to accept 15 characters,
it assigned the first 15 characters I entered (in response to the question) to the
string variable N$, and just "ignored" anything beyond that.
Suppose the program is RUN again.
RUN
YOUR NAME? JOHN JACOB JINGLEHIEMER SCMIDT
4. Write a program to enter and print an auto license plate that has a 3-letter
alphabetic string and a 3-digit number (such as SAM 123). Enter the letters as a
string variable and the number as a numeric variable. Show what the RUN
should look like.
HI DIM AS(3)
20 INPUT AS,B
30 PRINT ASIB
RUN
?SAM
?123
SAM123
l64 ATARl BASIC
5. You can also enter string variables by using READ and DATA statements.
1 REM •• STRING READ/DATA COURSE LIST
5 DIM A$(18),C$(1)
H' PRINT "COURSE ", "HOURS", "GRADE"
29 READ A$,B,C$
39 PRINT A$,B,C$
49 GO TO 29
59 DATA ENGLISH 1A,3,B,SOC 139 ,3,A
69 DATA BUS ADM lA,4,B,STAT 19 t 3,A
RUN
COURSE HOURS GRAOE
ENGLISH 1A 3 B
SOC 139 3 A
BUS ADM 1A 4 B
STAT 19 3 C
HUMANITIES 3 A
HISTORY 17A 3 B
CALCULUS 3A 4 C
Look at tine 10 of the preceding program. Notice the trailing spaces included in
the string "COURSE." Since we have reserved two columns of standard print
positions for course names, the extra spaces cause "HOURS" to be printed at
the third print position, and "GRADE" at the last print position column.
Another way we could have used would be to include an empty or "null string"
for printing nothing at the second ~rint position!
12 PRINT "COURSE", "" , "HOURS","GRADE"
Since any string of 8 characters or less will occupy one print position column, we
also included trailing spaces in the DATA statements (see lines 50 and 60) where
the course name was eight or less characters in length. This forces the course
names to use two print position columns. The following rules pertain to strings as
DATA statement items.
(1) The first string item in a DATA statement can have no leading spaces,
but will include trailing spaces up to the comma that separates the first
string from the second item.
912 DATA HOW, NOW, BROWN, COW
(3) The other items in a DATA statement will include any leading or trail-
ing spaces as part of the string assignment.
910 DATA HOW NOW BROWN COW
/t ,
The two spaces before and two spaces after the word NOW will .be
included in the string assignment.
6. In the program in frame 5, how long is the string for the fourth data item
in line 50? ___________
7. How many print position columns will be occupied by the string SOC 130
when it is read from the DATA statement and printed by line 30? _ _ _ _ __
8. The string LET assigns a particular string to a string variable. Note that
you must enclose the string in quotes as in these two examples.
10 DIM A$(3l,B$(2l,C$(3l
20 LET A$="YES"
30 LET B$="NO"
40 LET C$=A$ ,. C$ now contains "YES"
Show what will be printed by this program when it is RUN.
10 DIM A$(20l,B$(20l
20 LET A$=" A GOOD EXAMPLE"
30 LET B$="THIS IS"
40 PRINT B$IA$
RUN
RUN
THIS IS A GOOD E XAMP LE
266 ATARI BASIC
9. Next we want you to write a program similar to the one in frame 5. Instead
of courses and grades, the data should be books in your office or home library.
Your DATA should include author, title, and number,of pages. Our data looks
like this:
9~~ REM ** ORDER OF DATA:
9~1 RE~ ** AUTHOR, TITLE, PAGE S
91~ DATA BROWN,INSTANT BASIC,178
92~ DATA WHITE,YOUR HOME COMPUTER,225
93~ DATA KEMENY,BASIC PROGRAMMING,15~
94~ DATA OSBORNE,INTRO TO MICROCOMPUTERS
,384
95~ DATA NELSON,COMPUTER LIB /DREAM MACHI
NES, 186
96~ DATA THIAGI,GAMES FOR THE POCKET CAL
CULATOR,54
Before you write your program, look at line 940 above, and the corresponding
line in the RUN . Review frame 3. Note that we have controlled the display of
long titles, in this case, by limiting the string length assigned to variables by pro-
per dimensioning of string variables. If a title is short, i.e. , less than 9 characters,
include trailing spaces so that it will occupy two columns. Limit the length of
authors' names in the same manner. Go to it!
10. Modify the program you just wrote for frame 9 so that it will only print
those books with fewer than 200 pages.
11. The string IF-THEN allows you to compare the contents of two string
variables.
le DIM A$(3l,B$(2l
2e B$="NO"
3e PRINT "DO YOU WANT INSTRUCTIONS";
4e INPUT A$
se IF A$=B$ THEN 14e
6e PRINT "THIS SIMULATION PERMITS YOU TO
REGULATE ••• "
14e PRINT "THE SIMULATION BEGINS •.. "
Line 50 compares the contents of the string variable A$ (YES) to the contents of
string variable B$ (NO). If you responded YES to the INPUT statement, A$ and
B$ are not equal (they do not have the same contents), the computer will execute
the next statement, line 60, which in this case will print the instructions. If you
respond NO to line 40 above, the program will branch to line 140 and continue
execution there.
The comparison in line 50 is between
12. You can compare the contents of a string variable to a string enclosed in
quotes.
le DIM A$(3l
2e PRINT "DO YOU WANT INSTRUCIONS? YES
OR NO";
31iJ INPUT A$
4e IF A$="NO" THEN 14e
SIiJ PRINT "THIS SIMULATION PERMITS YOU TO
REGULATE .•• "
14e PRINT "THE SIMULATION BEGINS ••. "
The comparison in line 40 above is between a._______________
and a ___________________
string assigned to a string variable (the contents of a string variable) and a string
enclosed in quotes
But you can change a string variable into its numeric equivalent using the VAL($)
function . In the above example, if A$ contained a number (entered as a string
variable) you could compare it with numeric variable B by changing line 110 to
read as follows.
110 IF VAL(A$) = S THEN 140
Here is another demonstration program. Circle the correct RUN for the program.
5 DIM A$(10),S$(10),C$(10)
10 A$="32",B$="1.115",C$="-10"
30 PRINT A$ , PRINT VAL(A$)
40 PRINT B$,PRINT VAL(S$)
50 PRINT C$,PRINT VAL(C$)
RUN
RUN
23.115
15. Using the STR$ function, you can convert a numeric variable to a string or
place a numeric variable into a string variable.
RUN
112
112
STRING VARIABLES AND STRING FUNCTIONS 269
16. In a string IF-THEN, the comparison is made one character at a time. For
example, if a space is introduced in the wrong place, it may cause a comparison
other than what you expect.
5 DIM A$(2.0)
1.0 INPUT A$
2.0 IF A$="MCGEE" THEN 14.0
If the user enters MC GEE in response to the computer's prompt during the
RUN, the comparison will not be equal. Why will this comparison not be equal?
17. You can compare strings using the same symbols you used earlier: <>, <,
>, < =, >= , and =. It's a little tricky so you should use caution with these com-
parisons. The comparison is still made one character at a time from left to right.
The first difference found determines the relationship. The relationship is based
on position in the alphabet; C is "less than" S; T is "greater than" M.
5 DIM A$(5),B$(5)
1.0 A$="SMITH"
2.0 B$="SMYTH"
3.0 IF A$ < B$ THEN 1.0.0
In line 30 above, will the program branch to line 100 or continue to the next
statement in sequence? ____________
Jump to line 100. The first difference is the third character and since I is "less
than" Y, the IF-THEN condition is TRUE.
Line 180. D$ is "less than" E$. It is smaller in size, therefore "less than" E$.
Now which statement will be executed after the comparison of line 140?
The little program below prints a quotation froI11 John Wayne that is found
in the DATA statement in ASCII code numbers.
10 READ X : IF X = -1 THEN E ND
20 PRINT CHR$(X)I : GOTO 10
30 DATA 89,85,80,-1
RUN
YUP
Referring to that table of ASCII code numbers, tell what the following
program will print when RUN.
10 READ A : IF A = -1 THE N END
20 PRINT CHR$(A); : GO T O 10
30 DATA 72,65,80,80,89,32,67,79,77,80,85,84,73,78;71 , -1
RUN
HAPPY COMPUTING
22. Using the ASC function you can fin d out the ASCII code number for a
string character. Here's a demonstration.
5 DIM X$ (1),Y$ ( 1),Z$(1)
19 X$="A":Y$="B":Z$=" c "
20 PRINT "A = ";ASC( X$), "B '!;ASC(Y$),"
C = "IASC(Z$)
RUN
A =
65 B = 66 C = 67
Write a program using the ASC function to provide you with the ASCII
code for the following characters, so that the RUN looks like this.
RUN
CHAR ASCII CODE
$ 36
61
? 63
23. Now, let's return to string comparisons using the IF-THEN statement.
Strings are compared using a character by character process and the computer
uses ASCII code numbers to do the comparing. For example, in frame 12, we
saw this program segment.
5DIMA$(1.0)
1.0 INPUT A$
2.0 IF A$ = "MCGEE" THEN 140
When the program is RUN and the user enters MC GEE, the computer compares
ASCII code, numbers as follows.
"MCGEE" A$ (user-entered)
M=77----M=77
C = 67 C = 67
G = 71 space = 32
E = 69 G = 71
E = 69 E = 69
E = 69
The computer finds the two strings equal for the first two characters, but the
third character comparison is unequal (71 VS. 32).
Show how the computer, using ASCII codes, will compare the strings in the
following segment, just as we did above.
5 DIM A$(3)
1.0 INPUT "DO YOU WANT INSTRUCTIONS? YES OR NO"; A$
2.0 IF A$ = "NO" THEN 14.0
RUN
DO YOU WANT INSTRUCTIONS? YES OR NO? YES.
A$ "NO"
A$ "NO"
Y = 89 N = 78
E = 69 0=79
S = 83
The comparison is false.
24. Before you proceed, we need to introduce the RESTORE statement and its
use in connection with READ and DATA statements. A READ statement causes
the next item(s) of data to be read from the DATA statements. You may find
that you want the program to read through the data from the beginning again.
STRING VARIABLES AND STRING FUNCTIONS 273
To do so, use a RESTORE statement, which causes the next DATA item to be
READ to be the first piece of data in the first DATA statement. In other words,
with the next READ statement that the computer comes to, it starts at the begin-
ning of the DATA. See Line 30 below.
Now that you have seen how to use string variable comparisons, you 'can
understand this simple information retrieval program that permits retrieving in-
formation from DATA statements.
The program in frame 5 prints courses, hours, and grades. The program
below permits the operator to enter the course: the computer will then print the
course, hour, and grade.
1 REM •• COURSE INFO RETRIEVAL
5 DIM A$CISl,D$CISl,C$CI)
1.3 PRINT "ENTER COURSE NAME";
2.3 INPUT D$
3.3 RESTORE
4.3 READ A$,B,C$, IF A$=D$ THEN 6.3
5.3 GOTO 4.3
6.3 PRINT A$,B,C$:PRINT ,GOTO 1.3
9.3.3 REM •• COURSE,UNITS,GRADE
91.3 DATA ENGLISH IA,3,B,SOC I3.3,3,A
92.3 DATA BUS ADM IA,4,B,STAT I.3,3,C
93.3 DATA HUMANITIES,3,A,HISTORY I7B,3,B
RUN
ENTER COURSE NAME? HUMANITIES
HUMAN I TIES 3 A
RUN
ENTER COURSE NAME? SOCI3.3
ERROR- 6 AT LINE 40
The course name is stored SOC 130, but the user typed SOC130 without a space
between SOC and 130.
Note also that you cannot leave leading and trailing spaces when you INPUT
from the keyboard, so do not leave leading or trailing spaces in DATA statement
string items that are to be compared to INPUT items, or the comparisons will
not work as expected.
274 ATARI BASIC
(a) to test whether or not the course read from the DATA statement is the
course requested in the INPUT statement
(b) · when the course read by line 40 is not the course requested in the INPUT
statement
26. Modify the program in frame 24 so it will print the message "NO SUCH
COURSE" instead of the data error message, indicating that the course . entered
by the user does not exist in the computer's information system. You should put
a flag at the end of the regular data.
27. It's your turn. Write a program that contains the names and phone
numbers of your friends and business associates that you would like to
"retrieve" using your computer. When you type in a name, the computer should
respond with the correct phone number, as shown below.
NAME? ADAM OZ
ADAM OZ 415-555-2222
NAME? JERRY
ERROR- 6 AT LINE 4e
STRING VARIABLES AND STRING FUNCTIONS 275
28. Next, we will show you another function that allows you to manipulate and
examine parts of strings, called substrings. A substring is a part of a string and is
defined by using subscripts after the string variable, A$(10) or A$(1,5).
5 DIM A$ (30)
10 LET A$="MY HUMAN UNDERSTANDS ME "
20 PRINT A$( 10) .........- - - - - - - T h e substring begins at the 10th
RUN
character and includes all the characters
that follow
UNDERSTANDS ME
Replace Line 20 with PRINT A$(15). What will be printed when the new Line 20
is RUN? _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ __
STANDS ME
29. Now look at these examples. To isolate one character you need to use the
value twice indicating the first and the last character of the substring.
5 DIM A$(30)
10 LET A$="MY HUMAN UNDERSTANDS ME"
20 PRINT A$ (4,4) .........- - - - - - - W i l lprint H, the 4th character in the
RUN
string. (A space counts as one character)
H
Here we have a substring that starts at character 1 and includes all of the
characters through and including the 9th character.
5 DIM A$(30)
10 LET A$="MY HUMAN UNDERSTANDS ME"
20 PRINT A$(l,9)
RUN
MY HUMAN
In the program directly above, change Line 20 to read PRINT A$(4,8). What will
be printed when the new Line 20 is executed? _______________
276 ATARI BASIC
HUMAN
RUN
COMPUTERS PLAY GAMES
31. Here are parts of a program to print the string variable A$ backwards, one
character at a time. Fill in the blanks and show the RUN.
S DIM AS(_)
10 LET AS="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
20 FOR X=_TO_STEP -1
30 PRINT AS(X, _ _ ),
40
S DIM AS(26)
10 LET AS="ABCDEFGHIJKLMNOPQRSTUVW XY Z"
20 FOR X=26 TO 1 STEP -1
30 PRINT AS(X,X);
40 NEXT X
RUN
ZYXWVUTSRQPONMLKJIHGFEDCBA
STRING VARIABLES AND STRING FUNCTIONS 277
32. Let's go all the way back to frame 9 of Chapter 1 and change our number
guessing game to a letter guessing game. Here's the old program. All its logic
should still apply.
100 REMARK** *T H I S I S A SIMPLE COMPUTER GAME
110 LE T X = INT ( 1 00*RND( 111+1
120 PRINT
130 PRINT " I ' M TH I NKIN G O F A NU MBER FROI~ 1 TO 100 . "
140 P RINT " GU E SS MY NUMBER : : : "
IS0 PRINT : INPU T " YOUR GUE SS " ; G
160 IF G< X T HEN PR I NT " TR Y A BI GG ER NUMBER" : GO T O 150
170 IF G> X THEN PRIN T " TR Y A SMAL LE R NU MBER" : GO TO 1 50
180 I F; G= X THEN PRINT " TH AT ' S IT : : : YOU GUE SS ED MY NUMBER . " : GOT O 110
We should first add line 101 to DIM A$ and INPUT variable G$; also X$ to
store the computer's letter to be guessed. Line 105, a LET statement with all the
letters of the alphabet assigned to A$.
101 __________________
105
33. Now change line 110 to randomly select a number between 1 and 26.
110 ______________________________________---------------
150 ____________________________________________________________________
36. Change lines 160, 170, and 180 as necessary. RUN your new game.
160
170
160
RUN
I'M THINKING OF A LETTER FROM A TO Z
GUESS MY LETTER!!!
YOUR GUESS? L
TRY A SMALLER LETTER.
YOUR GUESS? G
TRY A BIGGER LETTER.
YOUR GUESS? H
TRY A BIGGER LETTER.
YOUR GUESS? K
TRY A SMALLER LETTER.
YOUR GUESS? I
TRY A BIGGER LETTER.
YOUR GUESS? J
THAT'S IT!!! YOU GUESSED MY LETTER.
37. Using the data from the prograIl! in frame 27, write a program that
will print a list of phone numbers of people ""hose first name begins with the
letter M.
RUN
MARY JAY 213-555-0144
MARY MMM 213-555-1212
ERROR- 6 AT LINE 4"
STRING VARIABLES AND STRING FUNCTIONS 279
38. Modify your program in frame 37 so that the computer will print telephone
numbers and names located in area code 415 (you are going there for a visit and
want to call old friends).
RUN
415-555-8117 TONY BOD
415-555-2222 ADAM OZ
ERROR- 6 AT LINE 40
39. Sometimes we need to know the length of the contents of a string variable
(how many characters). And, of course, BASIC has a special function, LEN, to
help you find the answer. LEN(A$) will tell you the number of characters
including blanks in the string A$. Here are some examples.
10 LET A = L EN(A$)
20 PRINT LEN(C$)
30 I F L EN(A$)<> L EN(B$)
You can use the substring specifier to check answers to questions, to "edit"
responses, and a host of other things. Below is a portion of a history teaching
program. The program asks a question, requests a user response, and then checks
the answer and makes an appropriate remark.
280 ATARI BASIC
10 DIM A$(30)
20 PRINT "WHO WAS THE FIRST PRESIDENT OF
THE U.S.",IINPUT A$
30 FOR X=1 TO LEN(A$)-9
40 IF A$(X,9:+X)="WASHINGTON" THEN PRINT
"CORRECT",END
S0 NEXT X
60 PRINT "YOUR ANSWER IS WRONG"
70 GO TO 20
RUN
WHO WAS THE FIRST PRESIDENT OF THE U.S.?
CORRECT. To see how the computer checks through your response, add this
line and RUN the program with the same input response.
39 PRINT A$(X,X+9)
40. Using the program in frame 39, if the user answered WASHINGTON
IRVING to the question, what will the computer print?
CORRECT (We only tested for WASHINGTON. Think of what might happen if
we only tested for WASH-even WASHING MACHINE would qualify for a
correct answer!)
41. The LEN function can be used to "edit" data entered by users that might
have to be a certain size, and no bigger because of forms you use or because of
the size of your computer memory. Examine the example below and answer the
questions which follow.
10 DIM A$(30),N$(30)
20 PRINT "YOUR NAME";:INPUT N$
30 IF LEN(N$»20 THEN PRINT "LIMIT YOUR
NAME TO 20 CHARACTERS PLEASE",GOTO 20
40 PRINT "YOUR ADDRESS", ,INPUT A$
50 IF LEN(A$»15 THEN PRINT "PLEASE ABBR
EVIATE YOUR ADDRESS":GOTO 30
How many characters are allowed for name? ____ For address? _ _ __
20; 15
STRING VARIABLES AND STRING FUNCTIONS 281
SELF-TEST
Try this Self-Test, so you can evaluate how much you have learned so far.
1. Write a program to permit INPUT of a 5-letter word and then print the
word backwards.
3. Modify the program in question 2 to print only words that begin with A
and end with S.
4. Some years ago, the auto industry was hard-pressed to come up with names
for new cars. They used a computer to generate a series of 5-letter words.
Write a program to generate 100 5-letter words with randomly selected con-
sonants in the first and third and fifth places and randomly selected vowels
in the second and fourth places.
281 ATARI BASIC
Answers to Self-Test
In this chapter we introduce one of the mast exciting capabilities of the ATARI
computer. You will learn to control the color of the objects that appear on the "
video screen and also the color of the background on which the objects appear.
In addition, you will learn to "paint" pictures which you have designed yourself.
Several new graphic and color statements and commands will aid these enhance-
ments to the output of your programs. When finished with this chapter, yoU will
be able to:
1. You can draw lines on the screen by selecting a screen mode that allows
graphics to be mixed with text in several different ways. This is done by giving
the GRAPHICS command (which may be abbreviated GR.). Eight different
modes are provided. They are selected by a number 0-7. For example:
1.0 GR. 3
would select a 40 column by 20 row graphics grid with provision for 4 lines of
text.
283
2114 ATARI BASIC
(a) GRAPHICS
(b) GR. (Yes, the period is necessary!)
(c) eight (0 through 7)
GRAPHICS 3
3. In graphics mode 3, the top portion of the screen provides for a 40 x ·20
grid for plotting points. The smaller portion at the bottom is provided for print-
ing text material. Four lines of text can be used.
(a) Where do you plot points on the screen in graphics mode 3?
4. You have been using graphics mode zero (GRAPHICS 0) throughout the
book so far. When your ATARI is turned on, the screen mode is automatically
set to GRAPHICS O. When answering the following question, remember that
mode 3 (GRAPHICS 3 or GR. 3) allows for 4 lines of text below the graph area.
Enter and run the following program. When you have finished, type:
GRAPHICS 0 and press the RETURN key. This returns you to the text mode.
COLOR GRAPHICS AND SOUND 285
10 GRAPHICS 3
20 PRINT "LINE 1"
30 PRINT "LINE 2"
40 PRINT "LINE 3"
50 PRINT "LINE 4"; (Note: don't forget the semicolon)
6J?J GOTO 60
LINE 1
LINE 2
LINE 3
LINE 41
IJ?J GRAPHICS 4
If you run this revised program, will there be more room for text?
7. When we are using graphics, we use the PLOT statement to tell the com-
puter what point we want plotted. Both the column and the row desired must be
given in the PLOT statement. The screen positions are numbered from the
upper-left corner of the screen starting with the number zero (0). Thus, the posi-
tion 2,5 would be the third position to the right (0, 1,~) and the sixth position
down (0,1,2,3,4,~). Suppose we execute the statement:
30 PLOT 2,5
10 GR.3
20 PLOT 2,5
30 PRINT "THIS IS PLOT POSITION 2,5"
What does each line in the program tell the computer to do?
(a) Line 10 sets _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __
(b) Line 20 tells where _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __
(c) Line 30 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __
15 COLOR 1
Insert this line into the program of frame 8 and run the program.
(a) Did you see the point? _ _ _ __
(b) If line 20 is changed to PLOT 10,5, would the point plotted be to the right
or would it be below our original point? _ _ _ _ _ _ _ _ _ __
10. To return from GR.3 to the text mode: Type GR.O, then write a program
to plot the four points shown below. Use GRAPHICS 3 and COLOR 1.
(1) 5,5
(2) 15,5
(3) 5, 15
(4) 15,15
Our program:
1.0 GR. 3
2.0 COLOR 1
3.0 PLOT 5,5: PLOT IS,S We are using multiple statements per
4.0 PLOT 5, IS . PLOT 15,15 line.
11. When your program is run, which plotted point (1,2,3 or 4) is:
(a) at the upper left? _ _ _ __
(b) at the lower right? _ _ _ __
(c) at the upper right? _ _ _ __
(d) at the lower left? _ _ _ __
12. Complete the program below to put a point in each corner of the graphics
area, using graphics mode 3. Remember, rows and columns start numbering with
zero (0). Consult the table in frame 5. Type GR.O to return to fullscreen text.
Enter and try your completed program.
la GR. 3
2a COLOR 1
13. According to the table in frame 5, graphics modes 4 and 5 provide 79x40
points. This gives us 79 columns (0 through 78) and 40 rows (0 through 39).
Since the columns and rows are numbered from zero, the upper right hand
corner of the screen would be plotted by the statements:
1l! GR. 4
2a COLOR 1
3a PLOT _ _ , _ _
3a PLOT 78,a
14. Write a program, using graphics mode 6, that will place a point in.each cor-
ner of the graphic area for this mode. Refer to frame 12 to see how it was done
for mode 3.
15. Type GRAPHICS 0 to return to the text mode. Type NEW, enter and run
the program below. Then draw a sketch of the result. Type GRAPHICS 0 when
your sketch is finished to return to the text mode.
COLOR GRAPIDCS AND SOUND 289
1.0 GRAPHICS 3
2.0 COLOR 1
3.0 PLOT .0,.0. PLOT 1 , 1 : PLOT 2,2
4.0 PLOT 3,3. PLOT 4,4: PLOT 5,5
5.0 PLOT 6,6. PLOT 7,7. PLOT 8,8
6.0 PLOT 9,9. PLOT 1.0 , 1.0 I PLOT 11 , 1 1
7.0 PLOT 12,12. PLOT 13,13 . PLOT 14,14
8.0 PLOT 15,15 : PLOT 16, 16: PLOT 17, 17
9.0 PLOT 18, 18 I PLOT 19,19
.~
I•
••
II
I.
II
...
I
16. You can also use a FOR-NEXT loop to plot points. Type NEW, enter and
run the following:
1.0 GRAPHICS 3
2.0 COLOR 1
3.0 FOR 1=.0 TO 19 STEP 2
4.0 PLOT 1,1
5.0 NE XT I
How do the results of this run compare with that of frame 14?
17. A third way to plot a straight line is to use the DRA WTO statement along
~ith the PLOT statement. The PLOT statement tells the computer where to start
the line, and the DRA WTO statement gives the position where the line will end.
The line is drawn from the PLOT position to the DRAWTO position.
Enter and run this program.
11" GR. 3
20 COLOR 1
30 PLOT 0,IIl
40 DRAW TO i9,19
(c) Compare the results of this program with those in frames 15 and 16.
18. Complete this program which will draw a line from the top left to the lower
right of the graphics area. Use graphics mode 4.
10 GR. 4
211l COLOR 1
30 PLOT 1Il,0
411l _ _ _ __
40 DRAWTO 78,39
19. After running the program in frame 18, the screen should look similar to
this sketch.
COLOR GRAPHICS AND SOUND 291
Now, add two lines to the program of frame 18 so that a second line will be
drawn from the upper right corner to the lower left corner. A very large X
should now be displayed as in this sketch.
Complete these lines and add them to the program of frame 18.
50 PLOT ____________
60 DRAWTO ____________
50 PLOT 78,0
60 DRAWTO 0,39
20. Add another line to the same program so that the text window will
say: X MARKS THE PLOT.
70
21. Given the following program, draw a sketch of the results which will appear
on the screen.
1l'l GRAPHICS 4
20 CO LOR 1
30 FOR N= 39 TO 0 STEP - -13
40 PLOT 0,0
50 DRAW TO 78,N
60 NEXT N
.t _
292 ATARI BASIC
22. Add or change these lines to the program of frame 21. After running the
program, press the BREAK key to stop. Then type 'GRAPHICS 0' to return to
the text mode as before.
Add 15 FOR M =
Z TO 1
Change 2Z COLOR M
Add 7Z NEXT M
Add 8Z GOTO 15
You can see each line being drawn and 'erased' repeatedly.
23. Now that you can use graphics with color, let's see how we can change the
colors. We select a color with the statement:
SETCOLOR Z,C,2
The variable C, can be any integer from 0 through 15. Each value for C will give
us a different color for the plotted points .
The statement used to change color points on the screen is
SETCOLOR ",C,2
24. A complete color graphics program must have the three statements:
GRAPHICS, COLOR, and SETCOLOR. Of. course, we must also plot some
points.
COLOR GRAPHICS AND SOUND 293
1.0 GR. 3
2.0 COLOR 1
3.0 SETCoLoR .0 • .0.2
4.0 PLOT .0.3
5.0 DRAWTo 38.3
6.0 PRINT "WHAT COLOR DO YOU SEE ? "
Examine the program before you run it. What do you think will appear on the
screen
(a) in the graphics area? ______________________
(c) Run the program to confirm your answers to (a) and (b).
I
(a) a long grey color bar (color may vary for your TV).
(b) The words: WHAT COLOR DO YOU SEE?
(c) We trust your answers were correct. If not, you have learned something
new.
25. A few changes and additions will convert the program of frame 24 to show
each of the 16 colors available. The colors are changed by the values of the .
variable in the SETCOLOR statement in line 30.
(a) Complete the program
1.0 GR. 3
2.0 COLOR 1
Add 25 FOR N .0 TO 15
(b) The color bar varies from grey through green with several shades of the
same color.
26. You have just observed the 16 colors available on your AT ARI. They were
demonstrated by changing the color of the bar in the graphics area. We will now
try a variation. You decide what happens on the screen.
Change line 30 in the program of frame 25 to:
30 SETCOLOR 2.N.2
The color bar is gold throughout the run, but the color of the text window goes
through the 16 colors.
27. One more variation can be observed by changing line 30 once more.
Change line 30 to:
30 SETCOLOR 4.N.2
Run the program again with this change. Describe the new results.
This time, the background of the graphics area changes. The bar remains gold,
and the text window turns dark and remains dark.
28 . In frames 25, 26, and 27, we varied the values for the variables A and N in
the statement: SETCOLOR A,N,2.
(a) When A was set to 0 where, on the screen, were the colors varied?
29. The effects shown in frames 25, 26, and 27 can all be shown in one
program.
(a) Fill in the missing blanks in lines 60, 80, 90 and 100.
19 GR. 3
29 COLOR 1
39 PRINT "WATCH MY COLORS CHANGE!! !"
49 FOR M 9 TO 4 STEP 2
59 FOR N = 9 TO 15
90 NEXT
10/IJ NEXT
(b) First the color bar varies through 16 colors, then the text window varies
through 16 colors, and finally the background of the graphics area varies
through 16 colors.
30. You can now plot points and create many colors on the video screen. Here
is a program that combines both. Before you run the program, see if you can
show where the points will be plotted. Shade the points in on the grid which is
provided beside the program.
Plot the points that you think will be colored on this grid.
1/IJ GR. 3
29 COLOR 1
-. .--
30 SETCOLOR 4.1.2
3,5 9,5
49 PLOT 5,5. DRAW TO 7,5
5/IJ PLOT 6,6. DRAWTO 6,10
69 DRAW TO 4,15
70 PLOT 6,10. DRAWTO 8,15
89 PLOT 5,7. DRAWTO 3,9
99 PLOT 7,7. DRAWTO 9,9
190 PRINT "WHAT A FUNNY MAN! ! !"
3,15 9,15
-. .--
296 ATARI BASIC
31. By changing the screen modes, we can change the size of the figure "drawn"
in frame 30. Make these changes and additions to the program of frame 30.
(a) The man appears to move from the upper left corner, and his size increases.
(b) 3
32. The program of frame 31 changed the graphics mode from many small divi-
sions, to fewer very large divisions. Pictures can be drawn in more detail when
many smaller points are used rather than larger points. In the program of
frame 31,
(a) Which statement caused the change in graphics mode? _ _ _ __
(b) The first value used for the variable A in the FOR-NEXT loop was
______ , the second value was _ _ _ _ _ , and the third value was
33. The table in frame 5 shows the number of graphics positions for the screen
modes. Modes 6 and 7 have 158 x80, or 12,640 individual points. Modes 4 and 5
have 79X40, or 3,160 points. Mode 3 has 39 x20, or 780 points. This program will
allow you to examine some rectangles in the three sizes.
10 GRAPHICS 7
20 COLOR 1
30 SETCOLOR 0,15,2
3 5 X = 80 : Y = 48
40 PLOT X , Y
50 PLOT X-2,Y-2: DRAW TO X+2,Y-2
60 DRAW TO X+2,Y+2 : DRAWTO X-2,Y+2
70 DRAW TO X-2, Y-2
80 PL OT X- 4, Y- 4. DRAWTO . X+4,Y-4
90 DRAWTO X+4,Y+4: DRAWTO X-4, Y+4
100 DRAWTO X-4,Y-4
First run the program and notice the width of the lines that were drawn and
the size of the rectangle.
Run the program again and compare the line width and rectangle size to
your first run.
(a) How did the line width change for runs 1, 2, and 3?
34. You can create an interesting visual effect by combining the three graphics
modes in one program. Our revised program looks like this.
298 ATARl BASIC
HI M = 1
20 FOR N = 7 TO 3 STEP-2
30 GRAPHICS N
40 COLOR 1
50 SETCOLOR 0,1,2
60 X =
80/M: Y 48/M =
70 PLOT X,Y
80 PLOT X-2.Y-2. DRAWTO X+2,Y-2
90 DRAWTO X+2 ,Y+2: DRAWTO X-2,Y+2
100 DRAWTO X-2, Y- 2
110 PLOT X-4,Y-4. DRAW TO X+4,Y-4
120 DRAWTO X+4,Y+4, DRAWTO X-4,Y+4
130 DRAW TO X-4,Y-4
140 FOR W =
1 TO 200. NEXT W
150 M = 2*M
160 NEXT N
170 GOTO 10
(a) line 50
(b) BREAK
(c) line 140
35. You now have color graphics under control, so .we'll move on to the
discovery of sound. Four variables are used with the SOUND statement. We'll
call them V (for yoice), N (for ~ote), T (for Tone), and L (for !eoudness).
20 SOUND V,N,T,L
(c) T
(d) L
(a) V = voice
(b) N = note
(c) T tone
(d) L = loudness
COLOR GRAPHICS AND SOUND 299
36. Enter and use this two-line program to adjust the volume of your TV set to
a reasonable level. Press the BREAK key when you have the volume adjusted
satisfactorily.
Ie SOUND e,115,le,8
2e GO TO 2e
Tell what each value used with the SOUND statement represents.
(a) 8
(b) 115 _ _ _ _ _ _ _ __
(c) 0
(d) 10
(a) 8 = loudness
(b) 115 = note
(c) 0 = voice
(d) 10 = tone
37. We'll experiment with loudness first. Values for the variable L may be any
integer from 0 through 15 . Using 0 for voice, 115 for note, and 10 for tone, write
a program to generate all 16 sound levels. Use an INPUT statement for L. Insert
a statement: IF L = -5 THEN to shut the sound off when finished.
38. In the program of frame 37, each sound level will keep playing until a new
input is received. Write a new program that uses a time delay to keep the note
playing for a reasonable length of time. Use a FOR-NEXT loop to change the
level of loudness.
300 ATARI BASIC
39. For normal use, the loudness level can be left at 8. Let's try the tone
variable next. We have used a value of 10 so far. The variable N may range from
o through 15. Write a revision of our program of frame 38 so that T (tone) will
be varied. Set the loudness level at 8.
40. Would you care to describe the sounds produced by the last program?
I heard a couple of nice sounding tones, some funny noises, and some nice quiet.
41. Now let's put the tone back to 10, leave the loudness at 8, and stay with
voice O. This time we'll vary the note. The values used may range from 0 through
255. Shorten the time delay of the program of frame 39 to 50 and re-write the
program to vary the notes.
42. If you were writing a ·song, you probably wouldn't need all those 256 notes.
Here is a table of values we will use that covers two and one-half octaves (when
tone is set to 10).
N Note N Note
26 D# 61 C
27 D 65 B
29 C# 69 A#
31 C 73 A
32 B 77 G#
34 A# 82 G
36 A 86 F#
38 G# 92 F
41 G 97 E ·
43 F# 103 D#
46 F 109 D
48 E 115 C#
51 D# 122 C
54 D 129 B
58 C# 137 A#
145 A
(a) From the results of the program in frame 41, which value of the note table
gives the highest note? _ _ _ _ _ __
(b) Which gives the lowest? _ _ _ _ _ __
(a) 26
(b) 145
43. To playa musical scale, we can store notes in a table and read them in
when we need them.
Fill in the data values in this program. Do NOT use sharps (#). Start with
the lowest note shown in the table in frame 42.
1~ FOR A = 1 TO 16
2~ READ N
3~ SOUND ~,N,I~,8
40 FOR W = 1 TO 20~: NE XT W
50 SOUND 0,~, 1~,8
60 FOR W = 1 TO I~: NE XT W
70 NE XT A
80 DATA
---'---'---'---'---'---
90 DATA
---'---'---'---'---'---
100 DATA ___ ' ___ ' ___ ' ___
302 ATARI BASIC
80 DATA 145,129,122,109,97,92
90 DATA 82,73,65,61,54,48
100 DATA 46,41,36,32
44. To make color and sound work together, add these lines to the program of
frame 43.
5 GR. 5
15 COLOR 1
34 SETCOLOR 0,A-1,2
35 PLOT 5,11 , : DR AWTO 15,11
(a) When you run this program, how many notes will be played?
(b) How many colors will be displayed? _ _ _ _ _ _ _ _ __
(a) 16
(b) 16
45. Cold is blue, and warm is red. Complete this program to play 8 notes.
Display warm colors with high notes and cold colors with low notes. Use these
colors:
dark purple yellow-green
blue yellow
blue-green orange
green red
Put both NOTE and COLOR in the data statements.
1.0 GR. 3
20 FOR A = 1 TO 8
30 COLOR 1
40 READ N,C
46. As your programming ability increases, the size of your programs also tend
to increase. Long programs can be more easily written and understood if you
break them up into functional parts. Often, you will find some of those func-
tional parts being used at several places in your program. Rather than repeat
them each time, a SUBROUTINE can be used. The GOSUB statement tells the
computer to leave your main program and go to the subroutine. When the
subroutine has been finished, a RETURN statement in the subroutine tells the
computer to go back to the main program to the line that follows the GOSUB
statement.
Here is a short program to demonstrate the use of the GOSUB and
RETURN statements. Try it on your computer.
1~ N = 1 22
2~ GOSUB 1~~
3~ N = 1~9
4~ GOSUB 1~~
5~ N = 97
6~ GOS UB 13~
7~ END
(a) 100,110,120,130,140
(b) 3
(c) 3
47. Here is a program that uses "nested subroutines." At line 110 subroutine
1000 is called. At line 1020 in the first subroutine, subroutine 2000 is called.
Subroutine 2000 returns from line 2010 to line 1030 of the first subroutine. Then
the program goes back to the main program. The program below plays a one-
octave scale and plots the notes on the video screen in color.
304 AT ARI BASIC
UI GR. 3
20 COLOR 1
30 SETCOLOR 1,10,2
40 FOR N = 5 TO 13 STEP 2
50 PLOT 0,N: DRAWTO 38,N
60 PLOT 0,15: DRAWTO 35,15
70 PLOT 30,15, DRAW TO 35,15
80 GOSUB 2000
90 SETCOLOR 4,4,2
100 COLOR 2
110 X=3 : Y=15. N=122. GOSUB 1000
120 X=5 : Y=14: N=109: GOSUB 1000
130 X=7 : Y=13: N=97: GOSUB 1000
14'" X=9: Y=12. N=92. GOSUB 1000
150 X= 11 : Y= 11 : N=82: GOSUB 1000
160 X=13: Y=10: N=73: GOSUB 1"'0'"
170 'X= 15: Y=9: N=65: GOSUB 10"'0
180 X=17: Y=8. N=61 : GOSUB 1000
190 END
Replace line 190 and add lines which will come back down the scale in sound and
plot the notes. (Our program only plays from low to high.)
48. Your AT ARI sound system allows the use of four voices simultaneously by
varying V in the sound statement:
SOUND V,N,T,L
We have only used one voice so far, but that is going to change now.
10 L 0 = 8 : Ll = 0
20 N0 = 41 : Nl = 129
30 GOSUB 1000
40 Ll 8 : GOSUB 1000
50 L0 = 0: GOSUB 1000
60 L 1 = 0: GOSUB 1000
70 S TOP
(a) How many notes will you hear when line 30 is executed? _ _ _ _ _ __
(b) How many notes can be heard when line 40 is executed? _ _ _ _ _ __
(c) How many notes can be heard when line 60 is executed? _ _ _ _ _ __
(d) Which voice does line 50 turn off? _ _ _ _ _ __
49. The following program demonstrates a simple tune with two voices. Three
subroutines are used to play the notes for the proper length of time. Each line of
the main program "calls" a subroutine to playa pair of notes.
1I!lliHI SOUND 0. _ _ • _ _ • _ _
1030
20 3 0
3010 SOUND 1. _ _ • _ _ • _ _
3030
so. Change the subroutines in the last program so that graphics mode 3 is used.
In subroutine 1000 plot a blue bar near the middle of the screen. For subroutine
2000 make the bar gold, and for subroutine 3000 make the bar red.
un 1
1.012
1.013
1.014
2.0 11
2.012
2.013
3.0 11
3012
3.013
3014
1.0 1 1 GR. 3
1012 COLOR 1
1.013 SET COLOR 0,8,2
1014 PLOT 0.12 I DRAWTO 5,12
2011 GR. 3
2.012 COLOR 1
2.013 sETCOLOR 0 , 1,2
2.014 PLOT .0, 12 : DRAWTO 1.0,12
3.0 11 GR. 3
3.012 COLOR 1
3.013 sETCOLOR .0,4,2
3014 PLOT .0 , 12 : DRAW TO 15. 1 2
S1. The next program will cover frames Sl to SS. We will add some two, three,
and four part harmony to our sound . The program is quite long so we'll break it
up into several sections.
One of the voices plays a repeating background series of three notes .
Therefore, we have used three subroutines for the sound statements with each
subroutine containing one of these background notes. In addition, the sub-
routines also contain the sound statements for two other voices. Since the sub-
routines are the most important part of the program, we'll look at them first.
308 ATARI BASIC
(a) 3
(b) the FOR-NEXT loops at lines 1030, 2030, and 3030.
52. Now let's look at the main beginning of the program, which sets up the
notes to be played.
Enter this much of the program along with the subroutines and run it.
(a) How many notes heard at line 10? _ _ _ __
(b) Does line 20 change the sound?
(c) Which line causes a second note to be heard?
COLOR GRAPHICS AND SOUND 309
(a) 3
(b) yes
54. The next section continues in the same manner except for the last three
lines.280-320.
190 N1=46:N2=36:GOSUB 100Z
200 GOSUB 2ZZZ
210 N1=48, GOSUB 300Z
220 N1=54:N2=34: GOSUB 1000
23Z Nl=61:N2=36 : GOSUB 2ZZZ
24Z Nl=69:N2=41,GOSUB 3ZZ0
25Z Nl=73:N2=46: GOSUB lZZZ
260 GOSUB 20Z0
270 Nl=69:N2=54, GOSUB 3000
280 SOUND 0,183,1Z,8: This time we can't
290 SOUND 1,82,10,8 use any of our three
300 SOUND 2,69,10,8
310 SOUND 3,61,10,8 subroutines, so we do
320 FOR W=1 TO 50:NEXT W it in the main program.
330 END
(a) How many voices are heard during the execution of line 320?
(b) How does the time delay compare with that of the three subroutines?
310 ATARI BASIC
(a) 4
(b) twice as long (FOR W = 1 TO 50)
(a) 3
(b) three times as long
COLOR GRAPHICS AND SOUND 311
SELF-TEST
0.0-.
2. Shade in the rectangle for the points that would be plotted by the state-
ment:
211J PLOT 4.1
3. Complete this program so that it draws a vertical line from the top of the
screen at the left side to the text window at the extreme right side of the
screen.
II1J GRAPHICS 4
211J COLOR 1
311J PLOT _ _ _ _ __
411J DRAWTO _ _ _ _ _ __
4. Write a program, using graphics mode 6, that will draw the largest possible
rectangle in the graphics area. Also draw the diagonals of the rectangle as
shown below.
311 ATARI BASIC
5. Use the following program and your computer to answer questions 5 (a),
(b), and (c).
1l1J GR. 3
20 COLOR 1
30 FOR M a TO 4 STEP 2
40 FOR N 0 TO 15
50 SETCOLOR M,N,2
60 PLOT 2,113. DRAWTO 12 , 10
70 FOR W = 1 TO 21313: NEXT W
80 NEXT N
90 NEXT M
Fill in the blanks with the words BAR, TEXT WINDOW, or BACKGROUND.
(a) When the value of M is 0, what changes color? _ _ _ _ _ _ _ _ _ __
(b) When the value of M is 2, what changes color? ___________
(c) When the value of M is 4, what changes color? _ _ _ _ _ _ _ _ _ __
6. If the following program is run, describe what would happen on the video
screen.
10 GR. 3
20 COLOR 1
30 FOR N "TO 15
40 SET COLOR 0,N+3,2
50 PLOT 2,5. DRAWTO 2,10
60 FOR W = 1 TO 2013. NEXT W
70 SETCOLOR 4,N,2
813 FOR W = 1 TO 2013: NEXT W
913 NEXT N
7. Write a program to fill the plotting area for graphics mode 3 with alternate
points (that is, 0,0; 0,2; 0,4 ... 2,0; 2,2 .... ). Use nested FOR-NEXT
loops. Choose your favorite colors.
10. Using the table in frame 42, write a program to playa one octave scale (C
to C) with no sharps.
write the subroutines so they play the notes. Subroutine 1000 should hold
the notes for a count of 50 and subroutine 2000 for a count of 100.
Answers to Self-Test
3. 30 PLOT o ,Ill
40 DRAWTO 78,39
4. 10 GRAPHICS 6
20 COLOR 1
3 III SET COLOR Ill, 12,2 (Your setcolor values may differ.)
40 PLOT 0,0
5 III DRAWTO 157,0 Lines 50-110 may be different for your
60 DRAWTO 157,79 program. As long as your rectangle
70 DRAWTO 0,79
80 DRAW TO 0,0 looks like the sketch, it's OK,
99 DRAW TO 157,79
10 III PLOT 9,79
119 DRAW TO 157,9 (frames 5,7,18,20)
314 ATARI BASIC
10 FOR A = 1 TO 8
20 READ N
30 SOUND 0, N, 10,8
40 FOR W = 1 TO 200. NEXT W
50 NEXT A
60 DATA 122,109,97,92
70 DATA 82,73,65,61
Now that you're done with the book, try this final examination to test all your
skills. The answers follow the test. Use separate paper for your programs and
calculations.
315
316 ATARI BASIC
RUN
ENTER YOUR GUESS? 1.0
**
ENTER YOUR GUESS? 2S
***
ENTER YOUR GUESS? 50
******
ENTER YOUR GUESS? 6.0
****
ENTER YOUR GUESS? 45
******
ENTER YO UR GUESS? 42
*****
ENTER YOUR GU ESS? 4 7
*******
ENTER YOUR GUESS? 48
WINNER ! ! !
2. You may have used matrix commands on some other computer. (Matrix is
just another name for array.) Versions of BASIC for home computers generally
do not include matrix commands. However, they are easy to simulate. Try it.
You have two 5 by 3 arrays called A and B, filled with small numbers that
are read into the elements or boxes in the array from DATA statements. Write a
program to add the contents of each element in array A to the corresponding ele-
ment in array B, and place the results in the proper place in a third array, C.
A 7 8 9 B 3 6 8 C 10 14 17
3 4 1 2 4 7 5 8 8
6 8 2 1 3 1 7 11 3
1 4 1 8 4 9 9 8 10
8 10 2 6 6 6 14 16 8
RUN
A ARRAY CO NTENTS
-,. 8 9
3 4 1
6 8 2
1 4 1
8 10 2
3 6 8
2 4 7
J 3 1
8 4 9
6 6 6
C ARRAY CONTEN T S
10 14 J7
5 8 8
7 11 3
9 8 10
14 16 8
3. Your child plans to set up a weekend sidewalk stand selling "junk" from
around the house (also known as a garage sale). Your youngster has no trouble
finding merchandise and no trouble pricing it.. But he or she really doesn't
understand the money system and is worried about making change when people
buy these" goods."
Write a program to allow the child to enter the amount of the sale and the
amount of money received, and have the computer then print exactly how much
and what kind of change to give back. The RUN is shown below.
RUN
ENTER TOTA L SA LE S AMOUNT? . 35
ENTER AMOU NT RECEI VED? 1.00
CO I N CHANG E .65
1 HALF DOLLAR
1 DIME
1 NICKEL
CO IN CHANGE .55
1 HALF DOLLAR
1 NICKEL
318 ATARI BASIC
Warning: Some versions of BASIC will round off numbers in peculiar ways (deep
inside the computer) because of the way the electronics perform arithmetic. So
beware! 4.9999 pennies is no fair. Write your program to avoid this kind of
problem by checking for integer values for change that includes pennies.
RUN
00 YOU WANT CONVERSIONS FOR
(1) METRIC TO U.S. STANDARD MEASURES
(2) U,S. STANDARD MEASURES TO METRIC
TYPE 1 OR 2? 1
WANT LIST OF CONVERSIONS (Y OR N)? Y
SELECT CONVER$ION FROM THIS LIST.
(1) CENTIMETERS TO INCHES
(2) METERS TO FEET
(3) KILOMETERS TO MILES
(4) KILOGRAMS TO POUNDS
(5) GRAMS TO OUNCES
(6) LITERS TO QUARTS
(7) DEGREES CELSUUS TO DEGREES FAHRENHEIT
ENTER NUMBER OF CONVERSION DESIRED? S
And so on.
FINAL SELF-TEST 319
From anyone of these subroutines, return to the subroutine that sent it, and
from there, return immediately to the main program to start over again.
Here are the conversion values to include in the program.
3. 19 REMARK***COIN CHANGER
29 PRINT "ENTER TOTAL SALES AMOUNT"; .INPUT T
39 PRINT "ENTER AMOUNT RECEIVEO";.INPUT M
49 IF M<T THEN PRINT "NOT ENOUGH MONEY RECEIVEO".GOTO 29
59 IF M=T THEN PRINT "EXACT CHANGE, NO CHANGE REQUIREO".PRINT.GOTO 29
69 LET C=M-T.IF C<I.99 THEN 159
65 PRINT
79 LET B=C-(C-INT(C».PRINT "BILL CHANGE IS It; B
75 LET C=C-B
89 LET Bl=INT(B/29).IF BI=9 THEN 199
85 PRINT Bl; " S29 B I LL"
99 LET B=B-(Bl*29)
199 LET B2=INT(B/19).IF B2=9 THEN 129
19'5 PRINT B2; " SI9 BILL"
119 LET B=B-(B2*19)
129 LET B3=INT(B/5l.IF B3=9 THEN 149
125 PRINT B3, " S 5 BILL"
139 LET B=B-(B3*5)
149 IF B<1 THEN 159
145 PRINT B; " Sl BILL".PRINT
159 LET C=INT( (C+.995)*199)
152 LET C=INT(Cl
155 PRINT "COIN CHANGE It; C/199.PRINT
169 IF C<59 THEN 189
179 PRINT "I HALF DOLLAR".LET C=C-59
IS9 IF C<25 THEN 299
199 PRINT "1 QUARTER".LET C=C-25
299 LET C=INT(C/19l.IF D=9 THEN 229
219 PRINT 0, " OIME".LET C=C-(O*19l
229 LET N=INT(C/5),IF N=9 THEN 249
239 PRINT N; " NICKEL" , LET C=C-(N*Sl
249 IF C<1 THEN PRINTIGOTO 29
259 PRINT INT(Cl; " PENNy"IPRINT,GOTO 29
FINAL SELF-TEST 323
3100 REM***INCHES TO CM
3110 PRINT "HOW MANY IN CHES"; : I NPU T I
3120 PRINT II "INCHES ="; 1*2.54; "CENTIMETER-S"
3130 PRINT : RETURN
3300 REM***MILES TO KM
3310 PRINT "HOW MANY MILES"; INPUT M
3320 PRINT M; "MILES ="; M* 1. 609; "KILOMETERS"
3330 PRINT I RETURN
3400 REM***POUNDS TO KG
3410 PRINT " HOW MANY POUNDS"; INPUT P
3420 PRINT P; "POUNDS = " ; P*.45; "KILOGRAMS"
34~0 PRINT I RETURN
BASIC FUNCTIONS
This Appendix describes some of the more common and useful functions in-
cluded in most versions of BASIC used on personal computers. It is by no means
an exhaustive list, and the functions may perform differently on the computer
you use. Check the reference manual for your computer system for the complete
list of functions available in your version of BASIC.
Arithmetic Functions
(In the following, "exp" stands for any BASIC expression, variable, or number.)
ABS(exp): Gives the absolute value of the expresion, i.e., ABS(A)=A if A>=O,
ABS(A) = -A if A<O.
Example: IF ABS(X-G»= 64 THEN PRINT "*,,
FRE(O): Used to determine how much space is left in the computer's memory
that is not being used by BASIC or the program stored in memory. Enter it in
a print statement, with zero in the parentheses, and the number of unused
bytes is given.
Example: PRINT FRE(O)
See also FRE(string variable) under String Functions.
325
326 ATARI BASIC
INT(exp): Computes the greatest integer less than or equal to expo Notice that
INT(3.14)=3, but INT(-3.14) =-4. You may wish to check the reference
manual for your version of BASIC for the similar function FIX.
SQR(exp): Gives the positive square root of expo Expression in parentheses must
be zero or a positive value (no negative values) .
Trigonometric Functions
SIN(exp), COS(exp), TAN(exp), ATN(exp): The SIN, COS, and TAN functions
give the sine, cosine, or tangent of exp, where exp is assumed to be given in
radians. ATN computes ,the principal value of the arctangent, in radians. The
value of ATN(exp) will be in the range: -nl2<ATN(exp)<nl2.
String Functions
ASC(string): Gives the ASCII code numeric value for the first character in a
string.
Example: PRINT ASC(B$)
FRE(string variable: Gives the number of free (unused) bytes in the memory's
reserved string space. See also FRE function under Arithmetic Functions.
LEN(string): Gives the number of characters included in the string; spaces are
always counted as characters.
Examples: FOR K-l TO LEN(X$)
-
ASCII CHARACTER CODES
Decimal Character Decimal Character Decimal Character
000 NUL 043 + 086 V
001 SOl1 044 087 W
002 STX 045 088 X
003 ETX 046 089 Y
004 EOT 047 / 090 Z
005 ENQ 048 0 091 [
006 ACK 049 1 092 \
007 BEL 050 2 093 ]
008 BS 051 3 094 t
009 HT 052 4 095 «-
,
010 LF 053 5 096
011 VT 054 6 097 a
012 FF 055 7 098 b
013 CR 056 8 099 c
014 SO 057 9 100 d
015 SI 058 101 e
016 DLE 059 102 f
017 DCI 060 < 103 g
018 DC2 061 104 h
019 DC3 062 > 105
020 DC4 063 ? 106 j
021 NAK 064 @ 107 k
022 SYN 065 A 108 I
02 3 ETB 066 B 109 m
0:2.:.: CAN 067 C 110 n
025 EM 068 D 111 0
026 SUB 069 E 112 p
027 ESCAPE 070 F 113 q
028 FS 071 G 114 r
029 GS 072 H 115 s
030 RS 073 I 116 t
031 US 074 J 117 u
032 SPACE 075 K 118 v
033 076 L 119 w
034 " 077 M 120 x
035 # 078 N 121 Y
036 $ 079 0 122 z
037 % 080 P 123 {
038 & 081 Q 124 I
039 082 R 125 }
040 ( 083 S 126 'V
041 ) 084 T 127 DEL
042 * 085 U
LF = Line Feed FF = Form Feed CR = Carriage Return DEL = Delete
APPENDIXES 329
ERROR MESSAGES
addition, 19 exponent, 29
array, 182,203,221,236,243, negative, 31
one dimensional, 182 exponentiation, 27
two dimensional, 222 expressions, 96
ASC function, 270
flag, 87
ASCII,270
floating point notation, 29, 30
assignment statement, 66
FOR-NEXT, 154, 159, 185,232
Atari keyboard, 12
control variable, 163
BREAK key, 59 initializing, 196
multiple statements, 157
CHR function, 270 nested loop, 170
CLEAR key, 14 STEP instruction, 164
COLOR statement, 287, 302 functions, 96
compound interest, 28, 53
control variable, 163 GOSUB,303
CTRL key, 14 GOTO statement, 58, 67
cursor, 12, 13 GRAPHICS command, 283
GR,284
data, 56
DATA statement, 120, 189, 194,247, IF-THEN statement, 82, 85, 127, 154,
264 250
flag, 89, 195, 199 comparison symbols, 81, 86
format, 121 flag, 87
DELETE BACK S key, 17 GOTO,86
DIM statement, 45, 184, 188,210,226, PRINT,86
249 initializing, 67, 196
direct mode, 11 INPUT statement, 54, 55, 185
statements, 15 two or more variables, 56
division, 20 INT function, 95, 97
DRA WTO statement, 290
LEN function, 279
END statement, 50, 114 LET statement, 42, 44, 70, 87, 265
errors, 51 line number, 47
DELETE BACK S key, 52 LIST,49
replace line, 50 loops, 70, 153
331
332 ATARI BASIC
Published
in coopera tio n
with
)Il
ATARr
A Warner Commun icat ions C~mpany D
•
~16A SELF-TOCHING WIDE
Loo k for t hese and forthcoming Wiley Self-Teaching Guides at your local bookstore .
For a co mplete listing of c urre nt and forth co ming STGs. w rite to : STG Editor
JOHN WILEY & SONS, INC.
605 Third Avenue , New York , NY. 1001 6
New York. Chichester. Brisbane. Toronto