252 98 19MB
English Pages 176 [180] Year 1982
Thank
You!
E
SUSAN DRAKE LIPSCOMB and MARGARET ANN ZUANICH have a unique combination of skills that contributed to the creation of BASIC FUN. Margaret Zuanich’s experience
in the
computer
field
has
included
everything
from
pro-
gramming to management consulting. She earned her Master’s Degree in Business and is now involved in computer systems training. Susan Lipscomb holds a Master’s Degree in Education and has spent fourteen years in the area of language and learning disabilities. They both live in Palo Alto, Califor-
nia.
Avon Books are available at special quantity discounts for bulk purchases for sales promotions, premiums, fund raising or educational use. Special books, or book excerpts, can also be created to fit specific needs. For details write or telephone the office of the Director of Special Markets, Avon
Books,
Dept. FP, 1790 Broadway,
New York, New York 10019, 212-399-1357.
BASIE FUN "7a
FLARE
IN
snes
PUZZLES, AND PROBLEMS CHILDREN CAN URITE Susan Drake Lipscomb and Margaret Ann Zuanich
AN AVON oa
BOOK
BASIC FUN: COMPUTER GAMES, PUZZLES, AND PROBLEMS. CHILDREN CAN WRITE is an original publication of Avon Books. This work has never before appeared in book form. 6th grade reading level has been determined by using the Fry Readability Scale. Front cover photograph shows Apple® computer. Apple is a registered trademark of Apple Computer, Book design by Joyce Kubat.
Inc.
AVON BOOKS A division of The Hearst Corporation 1790 Broadway New York, New York 10019 Copyright © 1982 by Susan Drake Lipscomb and Margaret Ann Zuanich
Published by arrangement with the authors Library of Congress Catalog Card Number: 82-4057 ISBN: 0-380-80606-1 All rights reserved. which includes the right to reproduce this book or portions thereof in any form whatsoever except as provided by the U.S. Copyright Law. For information address Avon Books First Camelot Printing, July, 1982 Library of Congress Cataloging in Publication Data Lipscomb, Susan Drake BASIC fun (An Avon Camelot book)
Summary: Introduces the fundamentals of computer programming using the BASIC programming language. Includes descriptions and listings of programs de-
signed to amuse and challenge users
1. Games—Computer programs — Juvenile literature 2. Games—Data
3.
Basic
digital
processing—Juvenile
(Computer
program
computers—Programming.
literature
language)—Juvenile 2.
Basic
literature.
(Computer
3. Games—Computer programs. 4. Computers] IL Title, Ill, Title: BA-S.LC. fun GV1469 2.L57 794 82-4057 ISBN 0-380-80606-1 AACR2
1. Zuanich,
CAMELOT TRADEMARK REG U S. PAT OFF, AND IN OTHER COUNTRIES. MARCA REGISTRADA. HECHO ENU
Printed is the DON
10
U.S.A 9
8
S A
[1.
program
Electronic language).
Margaret Ann
We are indebted to the Palo Alto Unified School District Computer Services and the students and staff at Loma Vista Elementary School.
TABLE OF CONTENTS Introduction
11
ONAGRWNH
PRINT: How the Computer Speaks Problems . Poem 14 . Flag 15 . Love 16 Face 17 . Stellar 18 Space 19
. KNOCK KNOCK . Riddles 22
21
WBDNADOARWNHH
LET: Adding 2 and 2 Problems . Recycle 24 . Baby Sister 26 . Paper Route 27 . Retirement 28 . Moon 29
. Pizza
13
23
30
. Swim Team . Inheritance . Sleep-over
31 32 33
GOTO: Showing the Way Problems 1. Stars 36 2. Counting 38 3. Power 39 4. Hunt 40 5. Gambler 42 6. Family Tree 44
35
TABLE OF CONTENTS
* 7
INPUT: Talking to the Computer Problems . Conversion 47 . Silicon Valley Game 48 . The Great Magician 50 . Batting Averages 52
OWNDNARPWNHH
4
. Year 2000 . . . .
46
54
Averages 55 Savings Account Mileage 58 Recipe 60
56
5
STRINGS: Using Words with the Computer 62 Problems 1. Nonsense 63 2. A Man from Peru 64 3. A Young Lady Named Maude 4. Race 66 5. Rhyme 68 6. Names 70 7. Author val
6
IF.
ODNDUPWNHH
..THEN: How the Computer Makes Decisions 73 Problems . Tax 74 . Foreign Currency 76 . Mystery Code 78 . Ten Speed 79 . Time Differences 81 . Interior Decorating 83 . Diver’s Test 85 . Spelling 87 . You Are a Detective 89
8 + BASIC FUN
FUNCTIONS (INT, RND, ABS): Doing Complex Things the Easy Way 92 Problems . Soccer 93 . Shell Game 95 . Dice Game 97 . Number Guessing Game 100 . Number Sequences 103 . How Warm Is Your Heart? 105
NOOBRWNH
7
8
. War
FOR.
. . NEXT: The Computer Repeats Itself 110 Problems 1. Fancy Counting 111
2. Blast Off 3. Pinball
9
107
112 113 ©
4. Paper, Rock,
Scissors
5. Ring the Bell
118
115
FANCY PRINTING (TAB): Writing It Your Way 121 Problems 1. Rectangle 122 2. Design-a-Scarf 124
10
3. HIHO
126
4. Lifeguard 5. Shape
129
127
6. Party
131
7. Fleet 8. Award
133 135
SUBSCRIPTS: Where to Store Your Information 137 TABLE OF CONTENTS
* 9
Problems 1. Most Valuable Player 138 2. Bank Statement 141 3. Pascal’s Triangle 143 4. Sea Battle 145 11
SUBROUTINES: How the Pros Do It Problems 1. Birthday 150 2. Math Quiz 153 3. Tennis 157
12
GAMES: Now You Can Do Your Own Thing 163 Problems 1. Ski Weekend 164 2. Backpack 170 Appendix A
10 * BASIC FUN
175
149
INTRODUCTION Are you getting bored with running packaged computer games? You can design your own once you learn how to communicate with the computer. BASIC Fun shows step-bystep how you can write computer programs—to solve your own problems, print your own pictures and invent your own
games. Special languages are used to communicate with a computer. For micro-computers, the language is called microBASIC. Each word in this language is called a programming statement. Computer programs are made up of programming statements which tell the computer what you want it to do. BASIC Fun teaches you micro-BASIC by giving you simple programs showing how different programming statements work and where they belong in a program. First you should type and run the programs as they are printed in the book. Then see if you can make the few changes we’ve suggested, or use your own ideas to check your understanding of a programming statement. Finally, try to design your own programs or games just using ours as a guide.
Warning! You should use all the programs within each chapter—or at least read and understand those you don’t use—before moving on to the next chapter. Following chapters build on and expand ideas learned in earlier ones—so work through the programs in the order they appear in the book. Above all, we hope you have as much fun exploring your computer’s posssibilities and inventing programs as we did in writing this book. The programs included in this text can be used on any computer that uses the programming language BASIC. They INTRODUCTION * 11
were written in Applesoft BASIC using the Apple ii Plus and tested on the Atari, Radio Shack TRS-80, Texas Instruments TI-99/4A, Hewlett-Packard HP-2000, IBM Personal Computer and the Osborn. We used a standard version of BASIC which is compatible with all of these computers. However, there are a few areas in which the computers differ. We have indicated in the Computer Notes following relevant programs any changes necessary to use the program on a particular computer. The Appendix contains a more detailed discussion of these differences. Before you begin using the programs, read the Appendix, and if necessary, refer to the Programmer’s reference manual for your computer. In order to make the programs as general as possible, we avoided using certain features included in one version of BASIC but left out in others. Once you become familiar with your own computer, you can add enhancements provided by your particular version of BASIC. If you have trouble making a program work on your computer, we recommend the following steps: 1. Check your program line-by-line against the listing in the book. 2. Make sure you have made the changes for your computer given in the Computer Notes following each program. 3. Check the BASIC manual for your computer for any changes required for your version of BASIC. If you still cannot make a program work on your computer, and think it is a genuine bug, send a description of the problem and a self-addressed, stamped envelope to the publisher. We will respond as quickly as we can to all such correspondence.
12 + BASIC
FUN
PRINT How the Computer Speaks This programming statement allows you to make the computer type out anything you
want
on the screen
ONADAHRWNHH
PROBLEMS . Poem
. Flag . Love . Face
. . . .
Stellar Space KNOCK KNOCK Riddles
or paper.
POEM You can turn your favorite poem whenever
into a computer program. Then,
you want to read your poem,
just run your program.
Here is a poem we wrote. Put this in your computer by typing each line exactly as shown in the Program Listing. Once
you
have our poem
working
on your computer,
try writing
one of your own. SAMPLE RUN ROSES ARE RED VIOLETS ARE BLUE YOU CAN LERRN TO WRITE FOR MOST THINGS YOU DO
PROGRAMS
PROGRAM LISTING 100 110 120 130 140
Note:
PRINT PRINT PRINT PRINT END
“ROSES ARE RED” “YIOLETS ARE BLUE” “YOU CAN LEARN TO WRITE PROGRAMS” “FOR MOST THINGS YOU DO”
Lines 100-130: When this program is run, the computer prints out every letter between the two “ marks on each line. Line 140: last line in computers this text so
14 + BASIC FUN
The end statement tells the computer that it is the the program. This statement is required for some and optional for others. We have included it in the programs can be used on most computers.
FLAG This program draws the American flag. Can you design a your own and write a program that draws your flag?
flag of
SAMPLE RUN SOOO
eee?
eee?
xe@oe?e
POSS
RK KA AAR ARARAKARAAAARRARA A
:
Sia
:
TT NOKIA RICH
HEM
RIKI
x
FROM | esaron +
0
REE IERA RST URRNRRRICRERR DONS
Use
PROGRAM
RADA
LANIER
REAR
LISTING
100
PRINT
“eeeeeooooxx xxx xXXXXXXXXXXXAXAKR RAR
110
PRINT
“eee
120
PRINT
“@
130 140 150
PRINT PRINT PRINT
“ee eee x" “© © © © @XXKXXXXXXXXXKXAXXKXAAAK KARA” “XxX x"
160)
PRINT
“XXXXXXXXXXAXXRXRAAXXAXAARRARAXAARRAAAX ”
170
PRINT
“xX
180)
PRINT
“XXXXXXXxXXXKARAKRAAKKKKAAXA RK AARKARAA RK ”
190 200 210
PRINT PRINT END
“xX x" “XXXXXXXXXXXXAKARARRAAKARARA KAA RAR AKA ”
Note:
©
eo ©
©
xe @XXXXXXXXXXKXKXXXRXAARARRARX ”
x”
Notice on the Program Listing that each line has a number. These tell the computer in what order the lines are to be run. You add a new line between two existing lines by giving the
new
line a number
which
falls between
the existing line
numbers.
PRINT:
How the Computer Speaks * 15
LOVE This program prints a very familiar word. You can do the same thing with your own name or initials. SAMPLE RUN x x x a x x td x XXRX
x x x x % x x
x x R x x x
x %
PROGRAM 110 120 130 140 150 160 170) 180° 190 200
x
PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT END
a x x x x
x x fad x a
teTatatad x x a RARK x a rad XxX
x x x x xx x
LISTING “
a Kx
*
16 * BASIC FUN
aK %
4 « x « *%
x « K a x a4 xa KX «
FACE This program draws a person’s face. See if you can change the expression, or add some hair. “ What happens if you forget a when you are typing a PRINT statement?
SAMPLE RUN
PROGRAM LISTING 130 140 150 160 170 180 190
PRINT PRINT PRINT PRINT PRINT PRINT END
“ “ “ ” “ “
PRINT:
How the Computer Speaks * 17
STELLAR This program draws a very familiar constellation of stars. Try writing a program that draws a different constellation.
SAMPLE RUN
THIS
I5
4 DRAWING
OF
THE
SIG
DIPPER
PROGRAM LISTING
PRINT PRINT " * RINT PRINT PRINT "THIS IS A DRAWING UF THE 816 DIPPER" ENC! Note:
Lines 165 and 166: prints a blank line.
18 * BASIC FUN
The PRINT
statement without any “ ”
SPACE This program
draws a U.S.
rocket ship which blasts off from your
video terminal. See if you can design your own model.
SAMPLE RUN
PROGRAM 100° 110 120 130 140 150 160 170 180 190 200 210 220 230 e440 250 260 270 230
LISTING
PRINT ” PRINT “ PRINT “ PRINT “ PRINT “ PRINT “ PRINT “ PRINT “ PRINT “ PRINT “ PRINT “ PRINT “ PRINT ” FOR I=1 FOR J=1 NEXT J PRINT NEXT I END
an 4 a
oN =
a I Iu I vl Ry el yo! I ea I “ eeoeoeo ooo 70 TO
. ee
o”
15 100
PRINT: How the Computer Speaks * 19
Note:
This program shows you that you can put any number, letter or special character in a PRINT statement. Lines 230-270: These statements make the rocket ship blast
off your video terminal. They are explained further in Chap-
ter 8.
Special credit to Chris Carison
20 + BASIC FUN
KNOCK KNOCK The computer can tell KNOCK KNOCK jokes. Here is one that we like. Can you write a program that tells some of your favorite KNOCK KNOCK jokes?
SAMPLE
RUN
KNOCK
KNOCK
WHO’S
THERE?
AMOS AMOS
WHO?
AMOS
QUITO
BIT
ME?
PROGRAM LISTING 100 110 120 130 140 150 160 170 180 190
PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT END
“KNOCK
KNOCK”
“WHOS
THERE?”
“AMOS” “AMOS
wHO?”
“AMOS
QUITO
BIT
ME?”
PRINT:
How the Computer Speaks * 21
RIDDLES You can put riddles into a computer program too. Here are 3 riddles. See if you can figure out the answers. Write a riddle program of your own using your favorite riddles.
SAMPLE WHICH 1 » 2. 3.
RUN
HMSHERS
60
TO
EACH
OF
THE
FOLLHING
RIOULES?
WHY DIO THE CHICKEN CROSS THE ROAD? WHY DIO THE FARMER CALL HIS PIG INK? WHAT HAPPENS TO DUCKS WHEN THEY FLY UPSIDE
DOWN?
ANSHERS BECAUSE
A BB.
THEY
C.
TO
GET
PROGRAM 164 116) 124 124 144 15@ 164 176 4
HE
GUACK
TO
NEWER
STAYED
IN
HIS
PEN
LF
THE
OTHER
SIDE
LISTING
PRINT “WHICH ANSHERS 60 TO EACH OF THE FOLLHING RIDDLES?" PRINT PRINT “1. HH DIO THE CHICKEN CROSS THE ROAD?" PRINT " WHY DIO THE FARMER CALL HIS PIG INK?" PRINT " HHAT HAPPENS TO OUCKS HHEN THEY FLY UPSIDE OOHN?" PRINT " ---" PRINT PRINT “ANSHERS” PRINT PRINT "A. BECHUSE HE MHEVER STAYED IN HIS PEN" PRINT "B. THEY GUACE LIP" PRINT "C. TO GET TO THE OTHER SIDE" ENO
Special credit to Paul Zuanich
22 + BASIC FUN
LeT Adding 2 and 2 You use this programming statement when you want to tell the computer to do arithmetic.
WONKDUNHWNHH
PROBLEMS
. Recycle . . . . . . . .
Baby Sister Paper Route Retirement Moon Pizza Swim Team Inheritance Sleep-over
RECYCLE You
and two of your friends are collecting aluminum
cans. If you
collect 200 cans, and one friend collects 140 cans and the other 325, how many will you have altogether? This program gives you the answer. How would you change the program if you collected 300 cans? If another friend wanted to join your group, how would you change the program in order to add in the cans this friend collected? You can use this same program to add other things, like test
scores, money you have earned or the number of records you own. SAMPLE
RUN
200
140
+
+
325
=
665
TOTAL
CANS
COLLECTED
PROGRAM LISTING 100 110
LET LET
A=e00 B=140
120
LET
C=325
130 140
LET D=A+B+C PRINT AS" + COLLECTED” END
150
Note:
"SBS"
+
“SC3"
=
“$D5"
TOTAL
CANS
Lines 100-130: A, B, C and D are numeric variables, and they can be set equal to any number or calculation. Line 130: The symbol +
tells the computer to add.
VARIABLE LIST Tells you what the variables in a program are used for. A—Number of cans you collected B—Number of cans one friend collected
C—Number of cans the other friend collected D—Sum
of A, B, and C
24 + BASIC FUN
COMPUTER NOTES
Radio Shack TRS-80 Color BASIC Does not use the word LET. Just leave this word out when entering lines 100-130. For example:
Instead of
LET A=200
A=200 Use Remember to leave out the word LET in all the programs in the rest of the text too.
LET: Adding 2 and 2 + 25
BABY SISTER You are 10 years old now, and your baby sister is 2. How old will your sister be when you are 20? This program tells you. How would you change the program if you were only 8 years old now? What if your sister is older than you, can you still use this program?
SAMPLE RUN IF YOU AND YOUR SISTER YOUR SISTER WILL BE 1e
PROGRAM 100 110 120 130 140 150 160
Note:
ARE 8 WHEN
YEARS YOU ARE
APART 20
IN
AGE
LISTING
LET A=10 LET S=2 LET D=A-S$ LET B=e0-D PRINT “IF YOU AND YOUR SISTER ARE “3D3~ YEARS IN AGE” PRINT “YOUR SISTER WILL BE “3B5~ WHEN YOU ARE END
Lines 120 subtract.
VARIABLE
and
130:
The
symbol -
LIST
A—Your age now S—Your sister’s age now
D—Years difference B—Your sister’s age when you are 20
26 * BASIC FUN
tells the
APART 20.”
computer
to
PAPER ROUTE You need to earn some money for Christmas presents. Your friend suggested that you deliver papers. If you expect to earn $20 a week
and there are 2'/2 months until Christmas, how much money will you have for presents? Use this program to get the answer.
Try changing the number of months or the amount of money you will earn each week. SAMPLE
RUN
IF YOU ERRN $ YOU WILL HAYE
20 $
200
EACH WEEK AFTER
2.5
MONTHS:
PROGRAM LISTING 100 110 120 130 140) 150
LET wW=e0 LET M=e.5 LET T=4emMew PRINT “IF YOU EARN $"5W3" ERCH WEEK” PRINT “YOU WILL HAYE $ "STS" AFTER “3M3" END
Note:
Line 120: The symbol * tells the computer to multiply.
MONTHS”
Line 120: This program uses four weeks in a month to make the calculations easier.
VARIABLE LIST
W—Amount
of money you earn each week
M—Number of months until Christmas T—Total amount of money you will earn
LET: Adding 2 and 2 + 27
RETIREMENT If you want to earn $900,000 before you retire and you can earn $50,000 a year, how many years will you have to work before you can retire? This program gives you the answer. Suppose you want to earn $1,500,000. Can you change the program to give you the right answer?
SAMPLE
RUN
IF YOU EARN $ Soong, PER yOU WILL HAVE TO WORK FOR 18 IN ORDER TO EARN $ 300000,
PROGRAM 100 110 120 130 140 150 160
Note:
YEAF YEARS
LISTING
LET A=900000 LET B=50000. LET C=A’B PRINT “IF YOU EARN $“5B5~ PER YERR” PRINT “YOU WILL HAVE TO WORK FOR “$C3" PRINT “IN ORDER TO ERRN $ “SA END
YEARS”
Line 120: The symbol / tells the computer to divide.
VARIABLE
LIST
A—Total amount you want to earn
B—Amount you can earn each year C—Number of years you must work
28 - BASIC FUN
MOON If you weigh 50 kilograms on Earth, how much would you weigh on the moon? This program can calculate the answer. What if you weigh 45 kilograms? How would you change the program?
SAMPLE RUN I CAN CONVERT YOUR IF YOU WEIGHT SO YOu WILL WEIGHT &.
ERRTH WEIGHT TO YOUR KILOGRAMS ON EARTH ON THE MOON
MOON
UVEIGHT
PROGRAM LISTING 100 110 120 130 140 150 160
Note:
PRINT “I CAN CONVERT YOUR ERRTH WEIGHT TO YOUR MOON WEIGHT” C=1/76 0 M=WeC PRINT “IF YOU WEIGH “$W5” KILOGRAMS ON ERRTH™ PRINT “YOU WILL WEIGH “3m5" ON THE MOON” END
Lines 110-130: M, W and C are called numeric variables, and they can be set equal to any number.
VARIABLE
LIST
C—Conversion factor—Earth to moon weight W—Your weight on Earth
M—Your weight on the moon
LET: Adding 2 and 2 + 29
PIZZA Five of you are going out for pizza and cokes. You know that the
pizza will cost $10 and a coke will cost $1. How
much
money
should you take with you to pay for your share? Use this program
for the answer. Can you change the price of the pizza or the number of friends in this program? What if you want 2 cokes? How would you change the program?
SAMPLE RUN TO PAY YOUR AND BUY AS
PROGRAM
PRINT PRINT END
Note:
Line
VARIABLE
SHARE 1
OF THE $ 10 COKE» YOU WILL
PIZZA NEED $
3
LISTING
“TO PAY YOUR SHARE OF THE $"5P5" “AND BUY A S$"SC3" COKEs YOU WILL
130: The computer
LIST
P—Price of pizza C—Cost of coke F—Number of friends
30 * BASIC FUN
divides first, then adds.
PIZZA" NEED $"5m
SWIM TEAM In your workouts with the swim team, your coach has you swim 30
laps a day. If you swim 4 days a week, how many miles will you swim each week? This program gives you the answer for a pool 25 yards long. Can you change the program so that you can find out how far you swim in a month? What if your pool is only 20 yards long? How would you change the program?
SAMPLE RUN IF YOU SWIM 30 yOU WILL SWIM @
LAPS TOTAL
A LAY FOR 4 OF 1.70455
DAYS
MILES
PROGRAM LISTING 100 110 120 130 140 150 160 170
Note:
LET L=30 LET D=4 LET N=LeD LET Y=Nee5 LET M=Ye3-S2ean PRINT “IF YOU SWIM “SL5" LAPS A PRINT “YOU WILL Swim A TOTAL OF END
DRY FOR “sDs" "sms" MILES"
DAYS”
Line 140: The computer multiplies first, then divides.
VARIABLE LIST L—Number of laps per day D—Number of days per week N—Total number of laps per week Y—Total number of yards per week M—Number of miles per week
LET: Adding 2 and 2 * 31
INHERITANCE You have just received the news that you are to inherit some money
from a distant uncle. You will receive $200 a month until you are 18. If you are 10 now, how much money will you inherit altogether? Use this program to find out. If you were to receive $300 a month, how would you change this program? What if you are only 8 years old?
SAMPLE RUN IF YOU RECEIVE $ YOU WILL PECEIVE
PROGRAM 100) 110 120 130° 140) 150 160
Note:
cOG FOR 3 19200
THE IN
MONTHS
LISTING
LET A200 LET Y=10 LET M=(18-Y)*12 LET T=M*kA PRINT "IF YOU RECEIVE $ "3M; MONTHS" PRINT "YOU WILL RECIEVE END
Line 120: multiply.
VARIABLE
NEXT 96 TOTAL
The
(_ )
";A;" $"3T3;"
tell the computer
LIST
A—Monthly payment Y—Your current age
M—Number of months before you turn 18 T—Total amount you will receive
32 * BASIC FUN
FOR IN
THE
NEXT
TOTAL"
to subtract first, then
SLEEP-OVER You are planning a sleep-over with 5 friends. Your mother told you to buy 2 hot dogs, 3 candy bars and something to read for yourself
and each guest. You also need some soda, and you know that 1 liter of soda is enough for 3 of you. How much food will you buy at the store? Use this program to get the answer.
What if one of your friends cannot eat candy? How would you change the program?
SAMPLE RUN FOR ‘YOU 2
A SLEEP-OVER WITH 5S NEED 12 HOT DOGS, 18 LITERS OF SODA AND 6
FRIENDS CANDY EARS comic BOOKS
PROGRAM LISTING 100 110 120 130 150 160 170 180 190 200 210
Note:
A+1) oR LET F=cA+120C LET G=c¢A+1>-D PRINT “FOR A SLEEP-OVER WITH “3A5" FRIENDS” PRINT “YOU NEED “SES” HOT DOGS: “$F” CANDY BARS” PRINT G3" LITERS OF SODA AND “3A+13" COMIC BOOKS” END
Lines 150, 160 and 170: The (_) first and then multiply or divide.
tell the computer to add
VARIABLE LIST A—Number of guests B—Number of hot dogs per person LET: Adding 2 and 2 + 33
C—Number of candy bars per person D—Number of guests per 1 liter of soda E—Total number of hot dogs needed F—Total number of candy bars needed G—Total number of liters of soda needed
34 + BASIC FUN
GOTO Showing the Way This programming statement allows you to tell the computer which line number to execute next.
This lets you repeat some lines in your program over and over.
aAnRWNH
PROBLEMS . Stars . Counting . Power Hunt . Gambler
. Family Tree
STARS Write this program for your star-gazing friends. when you are tired of seeing stars. Create
your
own
universe
or message
Hit the break key
by substituting your
own
lines.
SAMPLE
RUN
°
oe
oo
. °
°
°
. °
°
°
o
° o
.
ooo
o
ooo
o °
o
° oo
° .
@
° oo
ZEEING
° °
°
=TARS?
oo
°
° °
o
o
°
° .
°
ooo
°
ooo
°
PROGRAM
LISTING
REMeeTHI® PRINT " PRINT PRINT
“eo “
PRINT
“o
PRINT
“
PRINT
”
PRINT
~
PRINT
~
PRINT
°
"
PRINT " PRINT " 6O0TO 105 ENE
36 + BASIC FUN
PROGRAM °
If
FOR
2 TARR
° eo
o°
.
GAZERSee oo
°
se
-
Py
ooo
°
a
o
°
.
” rag
oo
SEEING
2TARS
ooo -
° @
o
Note:
Line 100: REM is a “remark” statement that appears in the listing but will not print when the program is run. It’s the programmer’s way of storing information for future reference. Line 160: GOTO 105 tells the computer to return to line 105 and run through the program again.
Special credit to Felicia Lo.
GOTO:
Showing the Way * 37
COUNTING This program counts by 2’s. Hit your computer’s break key to stop the program. See if you can change it to count by 5’s.
SAMPLE CAN
COUNT
BY
2
on
pr
I
RUN
PROGRAM 100) 110 115) 120 125 1300 Note:
LISTING
PRINT “I CAN COUNT LET ASO LET A=A+2 PRINT A 560TO 115 END
BY 2"
Line 110: Starts A at zero. Line 115: Adds 2 more to A each time the computer returns to that line.
Line 125: Tells the computer to return to line 115 and run through the program again.
38 » BASIC FUN
POWER This program prints all the powers of 2. Use your computer’s break key to stop the counting. Can you change it to print all the powers of 3 or 4?
SAMPLE RUN IT
CAN
SHOW
YOU
ALL
THE
POWERS
OF
2
PROGRAM LISTING 100 110 115) 120 125 130 135
Note:
PRINT “I CAN LET Bee LET A=B PRINT A LET A=BeR GOTO 120 END
SHOW
YOU
ALL
THE
POWERS
OF
2”
Can you tell what the computer is doing in line 125? Notice that the variable A is on both sides of the = sign.
VARIABLE LIST B—Number starting with A—Result of calculation
GOTO:
Showing the Way * 39
HUNT This program is a scrambled adventure. Thanks to the GOTO statement, the computer can unscramble and print out the adventure in a flash. Can you beat the computer? Design a scrambled program of your
own for your friends to try. SAMPLE RUN YOU ARE ON RAN EXPEDITION SEARCHING FOR THE LOST ARK. A FRIEND SHOWS YOU A LONG LOST MAP AND “X~ MARKS THE Spot WHERE IT WAS BURIED. yYOU SEE THE “X° ON THE MAP. IT IS BY THE TEMPLE OF THE ANCIENT SUN GOD. YOUR CREW STARTS DIGGING. THEY UNCOVER A PIT FULL OF SNAKESs BUT THE ARK IS THERE?! YOU ENTER THE PIT WITH ROPES» AVOIDING THE SNAKES» AND SECURE THE ROPES TO THE ARK. A HELICOPTER SAFELY HOISTS IT GUTs BUT THERE ARE NO ROPES LEFT FOR YOU. TOUGH LUCK?! THINK OF YOUR OWN SOLUTION.
PROGRAM LISTING 100 110 120 130 140) 150 160 170 180 190 200 210 220 230
PRINT “YOU ARE ON AN EXPEDITION SEARCHING FOR THE LOST ARK.” GOTO 260 PRINT “YOUR CREW STARTS DIGGING.” GOTO een PRINT “AND SECURE THE ROPES TO THE ARK. A HELICOPTER SAFELY” GOTO 280 PRINT “WHERE IT WAS BURIED.” «=GOTO 240 PRINT “TOUGH LUCK?! THINk OF YOUR OWN SOLUTION.” GOTO 340 PRINT "IS THERE?!” GOTO 300 PRINT “THEY UNCOVER A PIT FULL OF SNAKES> BUT THE ARK” GOTO 200
40 + BASIC FUN
240 250 260 270 e380 290 300 310 320 330 340
PRINT “YOU SEE THE “xX* ON THE MAP. IT IS BY THE TEMPLE OF THE” GoTo 320 PRINT “A FRIEND SHOWS YOU A LONG LOST MAP AND “x” MARKS THE SPOT” GOTO 160 PRINT “HOISTS IT OUT» BUT THERE ARE NO ROPES LEFT FoR vou.” GoTo 1980 PRINT “YOU ENTER THE PIT WITH ROPES» AYOIDING THE SNAKES» “ GOTO 140 PRINT “ANCIENT SUN GOD.“ GOTO 120 END
GOTO:
Showing the Way ¢ 41
GAMBLER If you bet $2 at the blackjack tables at 2 to 1 odds and let your winnings ride, how quickly would your money grow? This program tells you. Use your computer’s break key to stop the program. How would you change the program if you wanted to start with a
$5 bet? RUN
THIS PROGRAM TRBLE WILL GROW IF RIDE
SHOWS YOU
aw & tote
ROUND
YOU KEEP
HOW
A
WINNING
$2
BET
AND
AT LET
THE
BLACKJACK
YOUR
WINNINGS
WINNINGS ba) 12.5 31.25 78.13 195.31 488.25 1220.7 3051.76 APAAABBAAA
SAMPLE
PROGRAM LISTING 100 105 110 115 120 125 130 135 140
145 150 155
PRINT "THIS PROGRAM SHOWS YOU HOW A $2 BET AT THE BLACKJACK TABLE" PRINT "WILL GROW IF YOU KEEP WINNING AND LET YOUR WINNINGS RIDE" PRINT LET A=2 LET B=0 PRINT " ROUND WINNINGS" LET A=A+A+A/2 LET B=B+1 LET C=INT(AK100+.5)/100
PRINT " GOTO 130 END
42 - BASIC FUN
"SB"
$"3C
Note:
Line 140: This formula rounds your winnings to the nearest cent.
Line 145: A; ora, is required in a PRINT STATEMENT between a numeric variable and the quotation marks. A ; uses fewer spaces than a , . However, the spacing varies among computers so you may have to experiment with yours.
GOTO:
Showing the Way * 43
FAMILY TREE This program demonstrates how the number of people in your family can increase. Use your computer's break key to stop the program. See what happens if you change the number of children each person has, or the time between generations. Write a similar program to estimate the number of offspring your pet could have
SAMPLE RUN THIS PROGRAM FRMILY CRN INCREASE
SHOWS
HOW
THE
NUMBER
OF
PEOPLE
IN
YOUR
STARTING WITH 2 DESCENDENTS IF EACH PERSON HAS 2 CHILDREN WITH 20 YERRS BETWEEN GENERATIONS YERRS 20 40 60
DESCENDENTS 4 a 16 64 128 256 Sle 12 204 4
PROGRAM 100
LISTING
105) 115) 1200
PRINT “THIS PROGRAM SHOWS IN YOUR FRRILY PRINT “CAN INCREASE” PRINT «~T=en
125)
Y=0
44 + BASIC FUN
HOW
THE
NUMBER
OF
PEOPLE
RINT PRINT PRINT
“STARTING WITH "SAS" DESCENDENTS” “IF ERCH PERSON HAS “5C3" CHILDREN” “WITH “STS "YEARS BETWEEN GENERATIONS”
PRINT
“
YEARS
PRINT " GOTO 17 END
“SY3"
DESCENDENTS”
o
a
130 135 140 145 150 155 160 165 170 175 190 185 190 195
VARIABLE LIST T—Years between generations A—Number of descendants in first generation C—Number of children per person
GOTO:
Showing the Way * 45
INPUT Talking to the Computer This programming statement allows
you to enter different numbers into your program for the same problem each time it is run.
OMANDOBWNHH
PROBLEMS . Conversion . Silicon Valley Game . The Great Magician . Batting Averages . Year 2000 . Averages
. Savings Account . Mileage . Recipe
CONVERSION Do you know how many centimeters tall you are? Do you know how many centimeters tall your father is? You can use this program to convert inches to centimeters.
You could also use a very similar program to convert pounds to kilograms or gallons to liters. To stop this program, hit the break key instead of entering a
number. SAMPLE RUN CONVERT
INCHES
INCHES 760 152.4
TO CENTIMETERS CENTIMETERS
INCHES 7100 e254 CENTIMETERS
PROGRAM LISTING 100 120
PRINT PRINT
130 140 150) 160 170 180
PRINT “INCHES"S$ INPUT I LET C=lee.54 PRINT C3" CENTIMETERS” GOTO 1e0 END
Note:
“CONVERT
INCHES
TO CENTIMETERS”
Line 140: The INPUT statement allows you to type in differ-
ent numeric values for I each time this statement is run. VARIABLE LIST
I—Inches C—Centimeters
INPUT: Talking to the Computer * 47
SILICON VALLEY GAME If you were a Silicon Valley executive and you spend, how many Mercedes could you buy? You gram to get the answer. To stop the program, hit stead of entering a number. What if you decided to economize and only how would you change this program?
had $200,000 to can use this prothe break key inspend
$100,000,
SAMPLE RUN YOU YOU HOW
ARE A SILICON VALLEY HAVE 20teoo0 DOLLARS MANY CAN YOU BLY?
TYPE IN THE PRICE yOu COULD Buy «6 AND HAVE cond
PROGRAM
EXECUTIVE AND TO SPEND ON MERCEDES
OF
ONE MERCEDES? 20000 MERCEDES DOLLARS LEFT OVER.
LISTING
100° 110
PRINT PRINT
“YOU “YOU
ARE A SILICON VALLEY HAYE 200.000 DOLLARS
120 140)
MERCEDES” PRINT “HOW PRINT
150 160 170)
PRINT “TYPE IN THE INPUT A LET Beeonnon.A
MANY
CAN
YOU
PRICE
"STs" ; GOTO “END
Note:
EXECUTIVE AND” TO SPEND ON
BUY?"
OF
ONE
MERCEDES"$
“SBS” MERCEDES" DOLLARS LEFT OVER.”
140
Line 150: The ; stops the computer from moving to the next line. This keeps the input on the same line as the print.
Line
180: This statement forces B to be a whole number.
Chapter 7 explains how the INT (Integer) function works.
48 + BASIC FUN
VARIABLE LIST
A—Price of one Mercedes B—Number of Mercedes you could buy T—Money you have left over
INPUT: Talking to the Computer + 49
THE GREAT MAGICIAN Teach the computer a magic trick! Think of a number, follow the instructions and this program will guess your number. Don’t be surprised if the computer is right every time. Can you figure out how it does the trick? Can you write a magic trick of your own?
SAMPLE
RUN
HI> I AM THE GREAT MAGICIAN. THINK OF A ONUMBER> ANSWER MY GUESTION AND TI WILL TRY TO GUESS YOUR NUMBER. Oks THINK OF A NUMBERBUT DON’T TELL
ME
YET.
NOW» TREE YOUR NUMBER AND ADD Ss. MULTIPLY THE RESULT BY 3 AND THEN SUBTRACT MULTIPLY THIS RESULT RY & AND THEN ADD 4. WHAT DO yOU HAVE ‘OUR NUMBER If 10
PROGRAM
&.
LISTING
100 110° 120° 120 140)
REM@eNUMBER GUESSING GAMEoe PRINT “HIs I AM THE GREAT MAGICIAN.” PRINT “THINK OF A NUMBER: ANSWER MY QUESTION” PRINT “AND I WILL TRY TO SUESS YOUR NUMBER.” PRINT “Oks THINK OF A NUMBERBUT DON’T TELL ME
150)
PRINT
160
PRINT "NOW TAKE YOUR NUMBER AND ADD RESULT BY 5.” PRINT “NOW MULTIPLY BY d+ DIVIDE BY SUBTRACT 1." PRINT “WHAT DO YOU HAYES INPUT B LET C=¢B+1-5S)e5-805-3 PRINT “YOUR NUMBER IS "Ss END
170° 130 190 200 210 220
Note:
3. S
DIVIDE AND
ADD
YET.”
THE 5.
Line 200: The computer does calculations with numeric variables. This is called a numeric expression.
50 + BASIC FUN
VARIABLE LIST B—Result of calculations C—Number to guess
INPUT: Talking to the Computer * 51
BATTING AVERAGES This program calculates batting averages. You could use it to help your coach pick the players for your All Star team.
SAMPLE RUN CALCULATE
A
PLAYER’S
BATTING
AYERAGE
NUMBER OF TIMES AT BAT? 30 NUMBER OF HITS 76 NUMBER OF WALK S712 YOUR BATTING AYERAGE 18 3.323332 NUMBER OF TIMES AT BAT715 NUMBER OF HIT NUMBER OF WALK S70 YOUR BATTING AVERAGE If.
PROGRAM LISTING PRINT
“CALCULATE
A PLAYER’S
PRINT
“NUMBER
OF
TIMES
INPUT
B
PRINT INPUT
“NUMBER H
OF
HITS"3
PRINT
“NUMBER
OF
WALES'S
LET ASH- «B-W? PRINT “YOUR BATTING 6O0TO END
Note:
AT
AYERASE
BATTING
AVERAGE”
BATS
IS
"3A
120
Notice that this program uses 3 different input statements.
52 * BASIC FUN
VARIABLE LIST
B—Number of times at bat H—Number of hits W—Number of walks A—Batting average
INPUT: Talking to the Computer * 53
YEAR 2000 How old will you be in the year 2000? How old will your best friend be? You can use this program to find out. How would you change this program if you wanted to know your age in the year 2025?
SAMPLE RUN FIND
OUT
HOW
OLD
YOU
WILL
BE
IN THE YEAR
HOW OLD ARE YOU NOW71e2 WHAT YEAR If IT NOW?71952 IN THE YEAR 2000. YOU WILL
BE
30
HOW OLD ARE YOU NOW? Ss WHAT YEAR If IT NOW?71922 IN THE YEAR 20s YOU WILL
BE
53
PROGRAM 100
eood
LISTING
PRINT
“FIND
QUT
HOW
OLD
YOU
WILL
BE
IN
THE
eoog"
PRINT PRINT “HOW OLD ARE YOU NOW"s INPUT A PRINT “WHAT YERR If IT NOW" s INPUT ¥ FEMeenOW CALCULATE THE NUMBER OF YEARS LET T=e000-7+A PRINT “IN THE YEAR 200s YOU WILL BE “$T 6O0TO 1e0 END
VARIABLE
LIST
A—Your age now Y—Current year
T—Your age in the year 2000
54 + BASIC FUN
YEAR
AVERAGES This program calculates the average of 5 numbers. You can use it to calculate test averages, the average age of 5 of your friends, or the average temperature over 5 days. Can you think of other uses for
this program? What if you need the average of 6 numbers? Can you change this program to calculate your answers?
SAMPLE RUN CALCULATE INPUT THE
THE AVERAGE OF FIVE NUMBERS NUMBERS LIKE THIS 1sé2324s5
NUMBERS 7105203 30+40950 AVERAGE IS 30
PROGRAM LISTING 100 110 120 130 140 150 160 170) 180
Note:
PRINT “CALCULATE THE AYERAGE OF FIVE NUMBERS” PRINT “INPUT THE NUMBERS LIKE THIS 192s394:5" PRINT PRINT “NUMBERS"$ INPUT A> B>C>DsE LET T=(A+B+C+D+E>“5 PRINT “AVERAGE IS “sT PRINT END
Line 150: The (_)
tell the computer to add all 5 numbers to-
gether before dividing. Line 140: Notice that you can put in more than 1 number in an INPUT statement.
VARIABLE LIST
T—Average of 5 numbers
INPUT: Talking to the Computer * 55
SAVINGS ACCOUNT Suppose your grandmother gives you some money to save for your college education.
If you put it in the bank at the current interest
rate, how much money will you have when you are ready to start college? You can change the interest rate or the original amount, how much money you have.
and see
SAMPLE KUN FIND OUT HOW ACCOUNT
YOUR
MONEY
WILL
GROW
IN
YOUR
SAYINGS
ORIGINAL AMOUNT?7100 NUMBER OF YEARS7S YEARLY INTEREST RATE CIN AFTER 5 YEARS, YOU
DECIMAL) 7.055 WILL HAYE 130.7
DOLLARS
ORIGINAL AMOUNT? 200 NUMBER OF YEARS710 YEARLY INTEREST RATE CIN AFTER 10 YEARS» YOU
DECIMAL) 7.15 WILL HAYE 309.11
DOLLARS
PROGRAM LISTING 100
PRINT
“FIND
SAYINGS
OUT
HOW
YOUR
120 120) 140
PRINT PRINT INPUT
150
PRINT
“NUMBER
OF
160 170
INPUT PRINT
N “YERRLY
INTEREST
180 190
INPUT I LET T=Reci+I>
200 210
LET T=INT9 7100 PRINT “AFTER “3N5" YEARSs DOLLARS” PRINT 60TO 120 END
220 230 240
MONEY
WILL
GROW
IN
YOUR
ACCOUNT™ “ORIGINAL A
56 - BASIC FUN
AMOUNT" S$
YERRS” RATECIN
DECIMAL?“S$
eo YOU
WILL
HAVE
“Ss Ts"
Note:
Line 190: The symbol ** tells the computer to raise a number to a power, ie. LETX = 2° *4 tells the computer to raise 2 to the 4th power. Please note—your computer may use a
different
symbol.
Refer
to
your
BASIC
manual
under
exponentiation to find out what your computer uses.
Line 200: This statement rounds the dollar amount to the nearest cent.
VARIABLE
LIST
A—Original amount of money N—Number of years to save
I—Yearly interest rate T—Amount of money you have for college
INPUT:
Talking to the Computer * 57
MILEAGE You are planning on taking a driving vacation with your parents. Your father wants to know how long it will take to get to your destination and how much money gas will cost. You can use this program to get the answers. Can you add to the program to include food and lodging expenses for your trip? What would you change if you wanted to use this program for a trip on a plane or train?
SAMPLE RUN TAKING A YACATION 7 USE THIS PROGRAM CALCULATE YOUR DRIVING TIME AND COST OF GASOLINE MILES TO TRAVEL7500 MILES PER GALLON715 PRICE PER GALLON71.30 MILES PER HOUR7SS IT WILL TAKE 9.09091 DOLLARS.
HOURS
AND
TO
COST
43.3333
PROGRAM LISTING 100 110 120 130) 140 150 160 170 180 190 200 210 e220) 230 240 e50
PRINT “TAKING A YACATION 7 USE THIS PROGRAM TO" PRINT “CALCULATE YOUR DRIVING TIME AND” PRINT “COST OF GASOLINE” PRINT PRINT “MILES TO TRAVEL"$ INPUT M PRINT “MILES PER GALLONS =INPUT A PRINT “PRICE PER GALLON’S INPUT P PRINT “MILES PER HOURS INPUT $ LET T=m/s LET C= (MADoP PRINT “IT WILL TAKE “3T3" HOURS AND COST “$C3" DOLLARS." END
58 * BASIC FUN
VARIABLE LIST
M—Miles to travel A—Miles per gallon P—Price per gallon S—Miles per hour
T—Number of hours to reach destination C—Total cost of gasoline
INPUT: Talking to the Computer + 59
RECIPE The computer can also help you out in the kitchen. Suppose you need to increase the size of your favorite recipe in order to feed a large crowd. This program converts the amount of ingredients you will need. Use this program to make a recipe smaller also.
SAMPLE RUN THIS PROGRAM ALTERS YOUR FAVORITE RECIPE. NUMBER THE RECIPE ORIGINALLY SERVED74 NUMBER NEW RECIPE TO SERWE7S NOW INPUT THE ORIGINAL GUANTITY OF ERCH INGREDIENT CIN DECIMAL?72 NEW GUANTITY 4 INGREDIENT CIN DECIMAL? 7.5 NEW GUANTITY 1 INGREDIENT *©IN DECIMAL? NEW GUANTITY 2.2
71.4
PROGRAM LISTING 100 110 120° 130 140° 150 160 170 200 210 220 230
Note:
PRINT “THIS PROGRAM ALTERS YOUR FAYORITE RECIPE.” PRINT “NUMBER THE RECIPE ORIGINALLY SERVED"$ INPUT F PRINT “NUMBER NEW RECIPE TO SERWE™S INPUT N PRINT “NOW INPUT THE ORIGINAL QUANTITY OF ERCH” PRINT “INGREDIENT CIN DECIMAL?“3 INPUT I PRINT “NEW QUANTITY “S1ecN-F> PRINT GOTO 1460 END
Line 200: A numeric expression can be used in a PRINT statement. The computer just prints the result of the calculation.
60 + BASIC FUN
_ VARIABLE
LIST
F—Number of people recipe originally served N—Number of people the new recipe is to serve I—Original quantity of an ingredient
INPUT: Talking to the Computer * 61
STRINGS Using Words with the Computer A string variable contains letters or words instead of numbers. You can print strings, combine them to
form words or sentences and input strings just as you can numbers.
NDOBPWNHH
PROBLEMS . Nonsense . A Man From Peru . A Young Lady Named Maude . Race
. Rhyme . Names . Author
NONSENSE This program prints out
a message.
Can you see where each word
is stored in the program? -Can you write a scrambled message of your own? Which lines in the program must you change?
SAMPLE RUN DID YOU THINK
THIS
WOULD
PRINT
NONSENSE?
PROGRAM LISTING 100 DIM AS(10)+BSC10) 5C$ 010) -DSC10)-ESC1 0) sFSC10] 110 LET “SENSE?” 120 LET DID yOu TH" 130 LET “INK THIS * 140 150 160 170) 180
Note:
LET “INT NON” LET "LD PR” LET FS="WOL" PRINT BSSCSSFSFESs DSS AS END
Line 100: This is a dimension (DIM) statement. If you want a string variable to have more than one letter in it, you must use the DIM statement to save the spaces. In this case, A$ can have 10 letters, B$ can have 10 letters, etc.
Lines 110-160: A$, B$, etc., are used to store letters in the program. These variables are called string variables. The tells the computer this is a string variable.
COMPUTER
$
NOTES
TI-BASIC Do not use the DIM statement for string variables; the computer saves space automatically. Therefore, take out the string variables in the DIM statements in all the programs in this text. See Appendix A for a more detailed explanation.
STRINGS:
Using Words with the Computer * 63
A MAN FROM PERU You can write a limerick like this one yourself. Just substitute your own words for A$, B$ and C$. If you have another limerick you like better, try putting it on the computer.
SAMPLE RUN THERE WAS AN OLD MAN FROM PERU WHO DRERMED HE WAS EATING HIS SHOE HE WOKE UP IN THE NIGHT» WITH A TERRIBLE TO FIND OUT IT WAS PERFECTLY TRUE.
FRIGHT
PROGRAM LISTING 100) 110 120° 120° 140 150 160 170) 130
Note:
DIM A$C10)-BS(10)-C$(10) LET AS=" LET 6LET PRINT “THERE WAS AN OLD MAN FROM “$RS PRINT “WHO DREAMED HE WAS EATING HIS “SES PRINT “HE WOKE UP IN THE NIGHT» WITH A TERRIBLE FRIGHT” PRINT “TO FIND QUT IT WAS PERFECTLY “S8C$5"." END
Lines 140-170: You can use the string variables A$, B$ and C$ in a PRINT statement just as you used the numeric variables.
64 » BASIC FUN
A YOUNG LADY NAMED MAUDE Here is another limerick. Input your own strings for your original version. It is easier to do it with this program, because the program asks you for each word.
SAMPLE RUN THIS PROGRAM WRITES AR LIMERICK INPUT 3 WORDS THAT RHYME FIRST WORD?7MAUDE SECOND WORD7BROAD THIRD WORD?70 LORD THERE WAS RA YOUNG LADY NAMED MAUDE WHOSE FRAME WAS EXCEEDINGLY BROAD TO EAT AT THE TABLEs SHE SCARCELY WAS BUT OUT IN THE PANTRYs O LORD
ABLE
PROGRAM LISTING 100 110 120 130 140 150 160 170) 180 190) 200 210 220 230 240 eso
Note:
DIM AS$(25]>BS$(25] >C$le5) PRINT “THIS PROGRAM WRITES A LIMERICK” PRINT “INPUT 3 WORDS THAT RHYME” PRINT “FIRST WORD" INPUT AS PRINT “SECOND WORD"S INPUT BS PRINT “THIRD WORD": INPUT C$ PRINT PRINT PRINT “THERE WAS A YOUNG LADY NAMED “SRS PRINT “WHOSE FRAME WAS EXCEEDINGLY "3 BS PRINT “TO EAT AT THE TABLEs SHE SCARCELY PRINT “BUT OUT IN THE PANTRYs “ END
Lines
140,
160,
180: The
INPUT
WAS
ABLE”
statement works for string
variables as well as for numeric variables.
STRINGS:
Using Words with the Computer + 65
RACE This program tells the story of a crazy race using names, a noun and an adjective that you choose. Can you change the program to broadcast a game or tell a story? SAMPLE RUN
remeron)
WELCOME NEED NEED NEED NEED NEED
TO A A A A AN
THE
=CRAMBLED
SWEEPSTAKES?
PLURAL NOUNTHUNCHBACK © NAME?TIM CONWAY SECOND NAME7ZETEVYE MARTIN THIRD NAME7ALAN ALLA ADJECTIVE ?7FANTAETIC
ALL RIGHTs THE RACE I ABOUT TO BEGIN? THIS IS YOUR ANNOUNCER SPEAKING. THE HUNCHBACKS ARE AT THE STARTING GATE... AND THEY “RE OFF? STEVE MARTIN If IN THE LEAD. ALAN ALDARA IS NEXT> TRAILED BY TIM CONWAY. THE HUNCHBACKS ARE COMING INTO THE 2TRETCH. ALAN ALDARA 12 AHEAD AND STEVE MARTIN AND TIM CONWAY ARE MOVING UP. IT’ GOING TO BE CLOSE FOLKS... AND... STEVE MARTIN CROSSES THE WIRE FIRSTES? WHAT
A
FANTASTIC
PROGRAM 150) 160° 170) 180 200 205 210 220 230 240 e250
DAY
AT
THE
TRACK?
LISTING
DIM A$[25)>B$le5) >C$l25)]+>D$ (25) -E$(25) PRINT PRINT “WELCOME TO THE SCRAMBLED SWEEPSTAKES?” PRINT PRINT “I NEED A PLURAL NOUN"S INPUT AS PRINT “I NEED A NAME"S INPUT BS PRINT “I NEED A SECOND NAME"S INPUT CS PRINT “I NEED A THIRD NAME”?
66 + BASIC FUN
260 350 360 370 380 390 400 410 420 430 440 450 460 465 470 480 490 Soo
INPUT ps PRINT “I NEED AN RDJECTIVE”$ INPUT es PRINT THE RACE IS ABOUT TO BEGIN?” PRINT “ALL RIGHT» PRINT “THIS IS YOUR ANNQUNCER SPEAKING.“ PRINT “THE "3AS5" ARE AT THE STARTING GATE..." PRINT “AND THEY’RE OFF?™ PRINT IS IN THE LEAD. “$DS$s" IS NEXT> TRAILED” PRINT CS“ PRINT “BY “3BS3"." PRINT “THE "$AS3" ARE COMING INTO THE STRETCH.” PRINT DS3." IS AHERD AND “3CS5" AND “3 BS” RRE MOWING up.” PRINT “IT’S GOING TO BE CLOSE FOLKS... AND..." PRINT C83" CROSSES THE WIRE FIRST?22~ PRINT PRINT “WHAT A “SES5" DAY AT THE TRACK?” END
VARIABLE LIST
A$—Plural noun B$—First name C$—Second name D$—Third name E$—Adjective
STRINGS:
Using Words with the Computer + 67
RHYME Your teacher told you to write a poem, and you are struggling with words to rhyme with your lines. Use this program to test out various possibilities. Can
you change
it to handle
words
starting with digraphs(th, wh)
or blends(st, tr)?
SAMPLE RUN TEST RHYMING FOSSIBILITIES WHAT WORD ARE YOU RHYMING? ?CAT NEW FIRST LETTER?H HAT NEW BAT
FIRST
LETTER?B
NEW SAT
FIRST
LETTER?S
PROGRAM 100 110
WITH
THIS
FROGRAM
LISTING
DIM Z$(10),X#$(10),D$(1) PRINT “TEST RHYMING FOSSIBILITIES WITH PROGRAM" PRINT "WHAT WORD ARE YOU RHYMING?" XS
INPUT
Z%
=
MIDS
PRINT
"NEW
THIS
(2%,2) FIRST
LETTER";
INFUT DS PRINT D#; xX PRINT GOTO 145 END
Note:
Line 140: This statement puts Z$ into X$ starting with the second letter of Z$. The
string function
MID$
string. The format is MID$
68 + BASIC FUN
allows you to take out part of a (string,n,m) where:
string—the name of the string variable n—the position of the first letter to use m—number of letters to use, m is not required if you want to use the entire string.
VARIABLE LIST Z$—Contains word trying to rhyme D$—Contains the letter input to make a new word X$—Contains the letters in Z$ starting with the second letter
COMPUTER NOTES HP-2000 BASIC
\
Replace line 140 with:
Atari BASIC
140 LET X$ = Z$(2)
TI-BASIC
Not suitable for the Tl. See Appendix A.
STRINGS:
Using Words with the Computer + 69
NAMES This program makes up a child’s name by combining a first name and a last name. What names can you come up with? Use a similar program to make up names for your soccer team or
club. Try changing the program so you can use it for compound words.
SAMPLE RUN USE THIS PROGRAM IF PARENT’S WITH NAMED THEIR THE CHILD’S
TO EXPERIMENT WITH NAMES A LAST NAME OF 7WAHL
CHILD 7BRICK FULL NAME WOULD
BE
BRICK
WAHL
PROGRAM LISTING 100 110 120 130 140 150 160 170 180 190
DIM ASC15]»B$(15) PRINT “USE THIS PROGRAM TO EXPERIMENT PRINT “IF PARENTS WITH A LAST NAME OF INPUT AS PRINT PRINT “NAMED THEIR CHILD “5 INPUT BS PRINT “THE CHILD’S FULL NAME WOULD BE GOTO 140 END
VARIABLE LIST A$—Parents’ last name B$—Child’s first name
70 * BASIC FUN
WITH “5
“$BS3"
NAMES”
“$
AUTHOR Change the words in a sentence to alter the message or improve your writing style. Change line 110 if you want to start with a different message.
You can only have 30 characters in variable A$. What happens if you try to use more than 30? How would you change the program
if you wanted more than 30? SAMPLE RUN SHERRI IS A PRETTY WHAT WITH LYN WHAT WITH LYN WHAT WITH LYN
GIRL
WORD DO YOU WANT TO WHAT ?LYN IS A PRETTY GIRL WORD DO YOU WANT TO WHAT?SMART IS A SMART GIRL WORD DO YOU WANT TO WHAT ?PERSON IS A SMART FERSON
CHANGE?SHERRI
CHANGE?PRETTY
CHANGE?GIRL
PROGRAM LISTING 100) 110 120 130 140 150 160 170 180 182 185 190 200 210 220 230 240 250 260 270
DIM AS$(30), BS (39) ,C#(10) ,DS(10) LET AS = " SHERRI IS A PRETTY GIRL" PRINT AS PRINT "WHAT WORD DO YOU WANT TO CHANGE"; INPUT CS REM *x CREATE A NEW STRING PRINT "WITH WHAT"; INPUT D& REM ¥XFIND FIRST FOSITION IN AS OF CE LET F = LEN (C#) LET A = LEN (AS) FOR H=1T0A IF MID® (AS,B,F) = C$ THEN 240 NEXT B PRINT C$" NOT IN",AS,"TRY AGAIN" GOTO 130 LET HS = AS AS = LEFTS (B$,BR - 1) + DS + MIDS GOTO 120 END
STRINGS:
(B$,B
+
F)
Using Words with the Computer * 71
Note:
Line 182: The LEN (length) statement determines the actual length of the string in A$. In this case, A is set equal to the actual length of A$.
Line 200: This statement determines the starting position of the word C$ in the sentence A$. Line 250:
This statement changes
A$ by replacing the old
word C$ with the new word D$. The string function LEFT$ allows you to use only the first part of a string variable (the left-most letters). The format is LEFT$(string, n) where: string—name of the string variable n—number of letters to use.
VARIABLE LIST
A$—Expression starting with B$—Temporary
variable used to hold expression in A$ while
changes are made C$—Word D$—Word
replacing adding
A—Length of A$ F—Length of C$ B—Starting position of C$ in A$
COMPUTER HP-2000
NOTES
BASIC
Replace lines 190-210 with:
190 LET B=POS(A$,C$) Replace line 250 with:
250 LET A$(B,B+D) = D$ 255 LET A$(B+D+1) = B$ (B+F+1) The
POS
(position)
function
determines
the
starting position of the word C$ in the sen-
Atari BASIC TI-BASIC
}
72 + BASIC FUN
tence A$. The format is POS(string1,string2) where: stringl—name of string containing string2 Not suitable for use with these versions of BASIC
IF. . . THEN How the Computer Makes Decisions This programming statement
makes the computer decide which line number to execute next.
WOMONDNAHPWNH
PROBLEMS . Tax . Foreign Currency
. Mystery Code . . . .
Ten Speed Time Differences Interior Decorating Driver’s Test
. Spelling . You Are a Detective
TAX Sales tax is added to the price of most things you buy. In order to be sure you take enough money with you, use this program to find out exactly how much money you need to buy that rocket you’ve been looking at. Or, any other item you are thinking of buying. Your parents may find it useful too.
This program uses a tax of .065¢. If you live in a different state, the tax might be different. Can you change the program for a different tax rate?
SAMPLE RUN T WILL FIGURE OUT HOb mt AND ADD IT TO YOUR PURC INPUT “O° TO STOP.
PRICE OF ITEM?4.98 vOUR ITEM WILL COET PRICE OF ITEM716 YOUR ITEM WILL © PRICE OF ITEM72. YOUR ITEM WILL
$
TAX
WILL BE CHARGED ON AN JUST INPUT THE PRICE.
5.3
PROGRAM LISTING 100
120) 130 140) 150 160 170) 130° 190
PRINT “I WILL FIGURE OUT HOW MUCH TAX WILL BE CHARGED ON AN ITEM" PRINT “AND ADD IT TO YOUR PURCHASE. JUST INPUT THE PRICE.” PRINT “INPUT “O° TO TOP.” PRINT PRINT “PRICE OF ITEM"S INPUT P IF P=0 THEN 200 LET P=Pe, H6é5+P PRINT “YOUR ITEM WILL COST $"$ INT (Pe100+.597100 GOTO 140
eno
END
110°
74 * BASIC FUN
ITEM
Note:
Line 160: This program uses another way to stop. If the input is equal to zero, then the program skips to line 200 and ends.
VARIABLE LIST
P—Price of an item Special credit to Slaton Lipscomb
IF . . . THEN:
How the Computer Makes Decisions * 75
FOREIGN CURRENCY Are you planning a vacation to a foreign country? You may have to change U.S. dollars into foreign currency. Do you know what the exchange rate will be? If you do, you can use this program to convert the U.S. dollar amount to foreign currency. Can you write a program that converts foreign currency into U.S. dollars?
SAMPLE RUN THIS PROGRAM CURRENCY INPUT A OO TO INPUT INPUT 100
THE I
OUR
DOLLAR
TO
ANY
FOREIGN
PROGRAM
DOLLAR AMOUNT? e500 UW.5. DOLLARS EOUAL
THE DOLLAR AMOUNT? S23 U.2. DOLLARS EQUAL Se.
PROGRAM 100
STOP
CONVERT
THE FOREIGN EGUIYALENT OF THE DOLLAR AMOUNT? 100 U.2. DOLLARS EQUAL 99.82
INPUT
INPUT S32
CAN
A
DOLLAR. IN
2495.5
FOREIGN
IN
IN
9982 CURRENCY
FOREIGN
FOREIGN
CURRENCY
CURRENCY
LISTING
PRINT “THIS PROGRAM CAN CONVERT OUR DOLLAR TO ANY FOREIGN CURRENCY PRINT “INPUT A oO TO TOP PROGRAM” PRINT PRINT “INPUT THE FOREIGN EGUIVALENT OF A DOLLARS INPUT F PRINT “INPUT THE DOLLAR AMOLINTs INPUT [ IF D=t} THEN 160 LET N=FeLl PRINT DS "U2. DOLLARS EQUAL “SINT ON@100+.59 71008" IN FOREIGN CURRENCY" PRINT
GOTO
125
END
76 * BASIC FUN
Note:
Line 135: This is called an IF . . . THEN statement. If the condition is true, the program goes to the line given. Otherwise, the program goes to the next line.
VARIABLE
LIST
F—Foreign equivalent of U.S. dollar D—The dollar amount N—Foreign currency
IF . . . THEN:
How the Computer Makes Decisions * 77
MYSTERY CODE In order to finish this program, you have to decipher a secret message while the program is running. Can you write a mystery code program like this one and let your friends try to solve it?
SAMPLE RUN YOU ARE A BRITISH DIPLOMAT BEING HELD CAPTIVE BY A TERRORIST GROUP. SOMEONE JUET SLIPPED YOU A SECRET MESSAGE. DECODE IT AND YOU CAN ESCAPE. HURRY THE TERRORISTS ARE RETURNING! HINT-CROES OUT WORDS THAT PHYME THE CODE IS....... PIATRMATEFATSPATSCATLBATERAT 7A KEY IS UNDER THE BOOK
PROGRAM 100 110°
LISTING
DIM Asti) PRINT “YOU BY
TYATTHATEZATRNATA
A
ARE
TERRORIST
A
BRITISH
DIPLOMAT
BEING
120) 130
PRINT PRINT
140 150 160 170)
TERRORISTS ARE RETURNING!” PRINT “HINT-CROSS OUT WORDS THAT RHYME” PRINT “THE CODE IS......." PRINT PRINT “PSATRMATEFATSPATSCATLBATERATTYAT
130 190 200 210
INPUT AS IF AS="A" THEN PRINT “@e*e*BRANG GOTO 230
e220
PRINT
230
END
Note:
HELD
CAPTIVE
GROUP.”
“SOMEONE JUST SLIPPED YOU A SECRET MESSAGE. “DECODE IT AND YOU CAN ESCAPE. HURRY THE
“KEY
IS
ect BANG UNDER
THEY THE
SHOT
THATEZATRNATAR™
YOUeee”
BOOK”
Line 190: Notice that a string variable can be used in an IF . . . THEN statement too.
78 * BASIC FUN
“
TEN SPEED The computer can help you save toward a goal. This program calculates the time it will take you to save for a ten speed bicycle.
If you
change
values for the
interest rate,
the total amount
needed, and the monthly amount, you can use the program to calculate how long it will take to save for a vacation, for college, or anything else you want.
SAMPLE RUN yOU HAVE DECIDED TO SAVE FOR A NEW TEN SPEED IF THE BANK PAYS 5S 172-% INTEREST COMPOUNDED MONTHLY HOW LONG WILL IT TAKE TO SAYE FOR YOUR TEN SPEED? THIS PROGRAM WILL GIYE YOU THE ANSWER HOW HOW
MUCH MUCH
WILL YOUR BIKE CAN YOU SAYE A
FOR YOUR $ 193.95 A MONTHs YOU CAN HAYE $ 185.45
COST?7133.95 MONTH?T1S TEN
SPEED: IN
12
SAYING
$
15
MONTHS.
PROGRAM LISTING 100 105 110 115 120 125) 1230 135) 140 145 150 155 160 165 170) 175°
PRINT “YOU HAVE DECIDED TO SAYE FOR A NEW TEN SPEED” PRINT “IF THE BANK PRYS 5 172 % INTEREST COMPOUNDED MONTHLY” PRINT “HOW LONG WILL IT TAKE TO SAYE FOR YOUR TEN SPEED?” . PRINT “THIS PROGRAM WILL GIVE YOU THE ANSWER” PRINT PRINT “HOW MUCH WILL YOUR BIKE COST"$ INPUT A LET R=.055 PRINT “HOW MUCH CRN YOU SAYE A MONTHS INPUT M LET T=0 LET N=0 LET N=N+1 LET T=T+M LET T=Te#c1+k-12) IF T>AR THEN 185
IF . . . THEN:
How the Computer Makes Decisions * 79
180
GOTO
185) 190
PRINT PRINT “FOR ® MONTH: ”
195
PRINT “YOU CAN “S$N5 “MONTHS, “ END
200)
Note:
160
YOUR
$
“3A3“
HAYE
$
TEN
SPEED»
SAVING
“SINT (Te1004+.5>71003"
$
“SMe” IN
Line 175: The symbol > means greater than. This IF. . . THEN statement stops the program as soon as T, the total saved,
is greater than A, the amount
VARIABLE LIST
A—Amount needed for purchase R—Current interest rate M—Monthly savings T—Total earnings N—Number of months
80 » BASIC FUN
needed.
TIME DIFFERENCES If you live on the West Coast and you want to call your cousin in Boston, you might want to know what time it is in Boston. This program lets you convert from your time to the time anywhere else in
the world. How
would
you change
the program
if you lived in New
York
and wanted to call a friend in Seattle? SAMPLE RUN I CAN TELL JUST INPUT HIT “0” TO
YOU WHAT TIME IT THIS INFORMATION STOP.
IS
ANYWHERE
YOUR TIME IN HOURS7S THE TIME DIFFERENCE IN THEIR TIME IS 11
HOURS?
YOUR TIME IN HOURS?73 THE TIME DIFFERENCE IN THEIR TIME IS 5
HOURS?7-3
IN
THE
WORLD
id
o*“cLack
“CLOCK
PROGRAM LISTING 100 105 110 115 120) 130 132 135 140 145 150 155 160 165 170 175
PRINT "I CAN TELL YOU WHAT TIME IT IS IN THE WORLD" ANYWHERE PRINT "JUST INPUT THIS INFORMATION" PRINT "HIT *0? TO STOP." PRINT PRINT "YOUR TIME IN HOURS"; INPUT T IF T=0 THEN 175 PRINT "THE TIME DIFFERENCE IN HOURS"; INPUT D LET N=T+D IF N BS(1) PRINT “TRY TO FIGURE OUT THE MISSING THIS WORD” PRINT PRINT “C_ROM_7" PRINT “THE FIRST LETTER IS"$ INPUT AS IF AS="H" THEN 180 GOTO e40 PRINT “RIGHT - “5 PRINT “THE SECOND LETTER IS"$ INPUT BS IF BS="E" THEN 260 PRINT “WRONG - TRY AGAIN" GOTO 190 PRINT “WRONG: TRY AGAIN” GOTO 140 PRINT “RIGHT» YOU SPELLED CHROME” END
IF . . . THEN:
LETTERS
IN
How the Computer Makes Decisions * 87
Note:
Line 160: If you input “H” then the computer goes to line 180; otherwise it drops down to line 170.
88 * BASIC FUN
YOU ARE A DETECTIVE Each time you run through this program,
you can have a
different
adventure. Watch out though, only one path is correct. You
can write your own
mystery programs too. Just think of a
story and use the IF . . . THEN statements to send your detective on different adventures.
SAMPLE RUN YOU ARE A DETECTIVE. YOU RECEIVE A CALL FROM MF. DREW. HE ASKS YOU TO COME OVER. HE BELIEVES HIS LIFE IS IN DANGER. IF YOU DECIDE TO GO TO-HIS HOUSE TYPE YES. IF YOU DECLINE TYPE NO. FYES MR. DREW MEETS YOU AT HIS HOUSE. HE TELLS YOU HE SUSPECTS HIS WIFE IS TRYING TO KILL HIM. HE ASKS YOU TO STAY FOR DINNER $O YOU CAN MEET HER AS WELL AS HIS NIECE LUCY AND A FRIEND DR. QUIMBY. IF YOU ACCEPT HIS INVITATION TYPE MDREW IF YOu DECIDE TO GO HOME TO WATCH YOUR FAVORITE TY SHOW SAYING YOULL CALL MR. DREW IN THE MORNING TYPE CALL ?MDREW MRS. DREWs A RATHER PLUMP PLEASANT WOMAN IS VERY GRACIOUS AT DINNER. AFTER DINNER MR. DREW OFFERS HIS GUESTS A GLASS OF BRANDY. SINCE YOU DECLINE YOU DECIDE TO STROLL AROUND THE GARDEN. SUDDENLY vYOU HEAR A CRASH FOLLOWED BY A SCREAM? YOU RUSH IN TO FIND MR. DREW HAS BEEN POISONED? THE POLICE ARE CALLED. BEFORE THEY ARRIVE - YOU DECIDE TO INTERVIEW THE WITNESSES. IF YOU CHOOSE TO SPEAK TO THEM AS A GROUP, TYPE GROUP. IF YOU INTERVIEW THEM INDIVIDUALLY TYPC ONE. 7ONE DURING YOUR INTERROGATION OF MR. DREW S NIECEs SHE MAKES A FATAL SLIP. You DISCOVER SHE HAD THE OPPORTUNITY TO PUT ARSENIC IN MR.DREW’S BRANDY BECAUSE SHE WANTED HER INHERITANCE MONEY NOW? WITH IF . . . THEN:
How the Computer Makes Decisions + 89
THE POLICE COOPERATION SHE IS ARRESTED AND CHARGED WITH HIS MURDER. GREAT GOING? MRS. DREW REWARDS YOU HANDSOMLY?
PROGRAM LISTING 60 100 110 120 130 150 160 165 170) 180 190 195 200 210 220 230 240) 260 e270 2280 290 292 294 296 297 298 300 310 320 330 340 400 410 420 430 440 450 460 465 470 475
DIM X$(10]>57$(10]52$(10) PRINT “YOU ARE A DETECTIVE. YOU RECEIVE A CALL” PRINT “FROM MR. DREW. HE ASKS YOU TO COME” PRINT “OVER. HE BELIEVES HIS LIFE IS IN DANGER." PRINT “IF YOU DECIDE TO GO TO HIS HOUSE TYPE YES.” PRINT “IF YOU DECLINE TYPE NO.” INPUT x$ IF X$="YES" THEN 200 6IF X NO” THEN 300 PRINT “I NEED A YES OR NO ANSWER. TRY AGAIN.” GOTO 160 REMee DECIDE TO MEET MR DREW. PRINT “MR, DREW MEETS YOU AT HIS HOUSE. HE TELLS" PRINT “YOU HE SUSPECTS HIS WIFE IS TRYING TO” PRINT "KILL HIM. HE ASKS YOU TO STAY FOR” PRINT “DINNER SO YOU CAN MEET HER AS WELL” PRINT “AS HIS NIECE LUCY AND A FRIEND DR. QUIMBY.” PRINT “IF YOU ACCEPT HIS INVITATION TYPE MDREW” PRINT “IF YOU DECIDE TO GO HOME TO WATCH YOUR” PRINT “FAYORITE TY SHOW SAYING YOU“LL CALL” PRINT “MR. DREW IN THE MORNING TYPE CALL” INPUT Y$ “MDREW" THEN 400 CALL” THEN Soo I NEED A MDREW OR CALL - TRY AGAIN.” GOTO e9¢e PRINT “YOU OBVIOUSLY RREN“T VERY INTERESTED” PRINT “IN KEEPING UP YOUR BUSINESS. SOON" PRINT “YOU ARE FORCED TO FIND ANOTHER JOB.” PRINT “BETTER LUCK NEXT TIME!” GOTO 999 REM*eDECIDE TO STAY FOR DINNER PRINT “MRS. DREWs A RATHER PLUMP PLEASANT WOMAN” PRINT “IS VERY GRACIOUS AT DINNER. AFTER DINNER” PRINT “MR. DREW OFFERS HIS GUESTS A GLASS OF” PRINT “BRANDY. SINCE YOU DECLINE YOU DECIDE” PRINT “TO STROLL AROUND THE GARDEN. SUDDENLY” PRINT “YOU HERR A CRASH FOLLOWED BY A SCREAM?” PRINT “YOU RUSH IN TO FIND MR. DREW HAS BEEN “ PRINT “POISONED? THE POLICE ARE CALLED. BEFORE” PRINT “THEY ARRIVE - YOU DECIDE TO INTERWIEW THE WITNESSES.“
90 - BASIC FUN
476 480 490
PRINT PRINT PRINT
“IF YOU CHOOSE TO “THEM AS A GROUPs “IF YOU INTERVIEW
491 492 493 494 495 499 Soo S10 seo 530 540
INPUT
2$ “GROUP” THEN 600 “ONE” THEN 700 “I NEED A GROUP OR
550 560 600 605 610 620 630 640 699 700 710 7eo 730 740 750 760 770 780 790 999
Note:
SPEAK TO” TYPE GROUP.” THEM INDIVIDUALLY
ONE
-
TRY
TYPE
AGAIN.”
REM@eDIDN’T STAY FOR DINNER PRINT “YOU CALL THE DREW’S HOUSE IN THE MORNING." PRINT “THE POLICE ANSWER THE PHONE. MR. DREW" PRINT “WAS MURDERED LAST NIGHT. HE WAS POISONED” PRINT “AT DINNER. YOU ARE WANTED AS A WITNESS” PRINT “BUT YOUR DETECTIVE SERWICES ARE NOT REQUIRED.“ PRINT “TOO BADE” GOTO 999 REM@e*INTERVIEW THE GROUP PRINT “WHEN YOU INTERVIEW THE GROUPs YOU FIND” PRINT “OUT NOTHING USEFUL. ‘YOU ARE FORCED” PRINT “TO TURN THE CASE OVER TO THE POLICE” PRINT “WHEN THEY ARRIVE. OUT OF LUCK?” 6oTd 9399 REMeeINTERVIEW INDIVIDUALLY PRINT “DURING YOUR INTERROGATION OF MR. DREW*S” PRINT "NIECE» SHE MAKES A FATAL SLIP.” PRINT “YOU DISCOVER SHE HAD THE OPPORTUNITY” PRINT “TO PUT ARSENIC IN MR.DREWS “ PRINT “BRANDY BECAUSE SHE WANTED” PRINT “HER INHERITANCE MONEY NOW? WITH" PRINT “THE POLICE COOPERATION SHE IS“ PRINT “ARRESTED AND CHARGED WITH HIS" PRINT “MURDER. GREAT GOING? MRS. DREW” PRINT “REWARDS YOU HANDSOMLY? ~ END
Line 165: Notice that you THEN statements too.
IF . . . THEN:
can
use words
in your
IF...
.
How the Computer Makes Decisions * 91
FUNCTIONS (INT, RAND, ABS) Doing Complex Things the Easy Way Functions are programs built-in to the computer that you can use to make it easy for you to do complicated operations in your own programs.
The functions you need for games are: INT Integer function—Changes any decimal number to a whole number.
INT (6.15) = 6 RND_
Random function— Generates random numbers from 0 to 1.
ABS
Absolute value function— Eliminates the minus sign in a negative number.
ABS(-
10) = 10
NAOHRWNH
PROBLEMS . . . . . . .
Soccer Shell Game Dice Game Number Guessing Game Number Sequences How Warm Is Your Heart? War
SOCCER One of the rules of AYSO
(American Youth Soccer Organization)
soccer is that everyone on the team must play at least 2 quarters. If all 15 players show up for a game, it is difficult to have everyone play an equal amount of time. This program can help you out. It calculates the number of quarters everyone can play, depending on how many show up for the game. How can you change the program for your basketball team? Alter the program to tell you how many innings everyone can play on your Little League team.
SAMPLE RUN T WILL FIND OUT HOW MANY GUARTERS EACH MEMBER OF THE TEAM CAN PLAY NUMBER OF PLAYERS FOR THIS GAME7T15
EVERYONE PLAYS 2 WHILE 14 PLAYERS
QUARTERS WILL PLAY
3
GUARTERS
PROGRAM LISTING 100° 105 110° 115) 120 125 127 128 135 140) 145) 150 155 160
PRINT “I WILL FIND QUT HOW MANY QUARTERS” PRINT “ERCH MEMBER OF THE TEAM CAN PLAY" PRINT “NUMBER OF PLAYERS FOR THIS GAME"S INPUT N LET P=1104 4 THEN 155 LET A=INTCAD LET Q=P-Ren PRINT “EWERYONE PLAYS “SAS” GUARTERS PRINT “WHILE "S03" PLAYERS WILL PLAY “SA+13" GUARTERS” GOTO 160 PRINT “EVERYONE NEEDS TO PLAY 4 GUARTERS” END
FUNCTIONS:
Doing Complex Things the Easy Way + 93
Note:
Line 125: The INT function forces the result of P/N to be a whole number, or integer. It cuts off any fractions without rounding. ex. INT(3.25)
INT(3.95) )
3 =3
VARIABLE LIST N—Number of players at this game P—Total number of quarters available for the game A—Number of quarters needed for everyone to play Q—Extra quarters some players can play
94 + BASIC FUN
SHELL GAME In this game, you play the original carnival shell game with the computer. Needless to say, this computer’s “hand” is definitely quicker than your eye! Design your own slight of hand game or have your friends guess a prize behind door number 1, 2, or 3.
SAMPLE RUN READY TO PLAY THE SHELL GAME? SEE IF YOU CAN GUE WHICH CUP HAE Ok PLACE YOUR RET?E5 WHICH CUP HAS THE BEAN 1 2 OR 37 73 YOU ARE RIGHT? YOU WON $ 50 YOUR TOTAL WINNING? ARE $ SO WANT TO TRY AGAINTY Ok PLACE YOUR BET7eE5 WHICH CUP HAS THE BEAN 1 2 OR 3? re ‘YOU ARE RIGHT? yOu wON $ 50 YOUR TOTAL WINNINGS ARE $ 100 WANT TO TRY AGAIN? Ok PLACE YOUR BET?25 WHICH CUP HAS THE BEAN 1 2 OR 27 71 I FOOLED YOU THE BEAN WAS IN CUP 2 SORRY . . . YOU LOST $ 25 YOUR TOTAL WINNINGS ARE $ 75 WANT TO TRY AGRINZN
THE
BEAN
PROGRAM LISTING 100 105 110 115 120 125 130 135
DIM ASCII PRINT “READY TO PLAY THE SHELL GAME?” PRINT “SEE IF YOU CAN GUESS WHICH CUP HAS LET T=0 PRINT “Ok PLACE YOUR BET"S INPUT B LET R=INTCRND¢1) ©3+1> PRINT “WHICH CUP HAS THE BEAN 1 2 OR 37" FUNCTIONS:
THE
BERN”
Doing Complex Things the Easy Way * 95
140 145 150 155 160 165 170 175 130 185 190 195 200 205 210 215 e220
Note:
INPUT A IF A=R THEN 155 GOTO 185 PRINT “YOU ARE RIGHT?” LET B=Bee LET T=T+B PRINT “YOU WON $ “3B PRINT “YOUR TOTAL WINNINGS ARE 6oTO ens PRINT “I FOOLED YOU. THE BEAN T=T-B PRINT “SORRY...YOU LOST $ “3B PRINT “YOUR TOTAL WINNINGS ARE PRINT “WANT TO TRY AGRIN™S INPUT AS IF AS="Y" END
THEN
“3T WAS $
IN
CUP
“sR
“3T
120
Line 130: This statement gets a random number from 1 to 3. The RND(0) function gives a number from 0 to .9999. Try calculating this expression yourself using different random numbers. This is called playing computer. If you want a number from 1 to 6, just change the 3 toa 6.
VARIABLE LIST T—Total winnings B—Bet R—Cup with the bean A—Your guess of the cup
COMPUTER
NOTES
The format for the RND function varies with each computer. Appendix A for the proper format for your computer.
96 - BASIC FUN
See
DICE GAME This program pretends that the computer can throw dice. The person playing the game starts with some money, then wins or loses
depending on the throw of the dice as follows: Original amount
Dice throw
of money is:
6 or 12 8 or9 7orll
Tripled Doubled Lost
All others
No change
Try changing the betting odds for the game.
SAMPLE READY
RUN TO
PLAY
THE
DICE
WHAT IS YOUR BET?710 DICE THROW IS 5 NO CHANGE YOUR TOTAL WINNINGS AGRINTY WHAT IS YOUR BET?725 DICE THROW IS 7 you LOST! YOUR TOTAL WINNINGS AGAIN?TY WHAT IS YOUR BET725 DICE THROW Is 7 vou LOST! YOUR TOTAL WINNINGS AGARINTY
ARE
GAME?
0
ARE-25
ARE-S0
WHAT IS YOUR BET750 DICE THROW IS 3 YOU WIN? YOU DOUBLED YOUR BET? YOUR TOTAL WINNINGS ARE SO AGAINTY
FUNCTIONS:
Doing Complex Things the Easy Way * 97
WHAT IS YOUR BET?10 DICE THROW 12 5 NO CHANGE YOUR TOTAL WINNING? AGAINZN
PROGRAM
ARE
SO
LISTING
DIM
ASC1)
LET
T=0
PRINT “PEADY TO PLAY THE PRINT PRINT “WHAT I= YOUR BETS INFLT ™ LET A=INTCRND 11 e6415 LET B=INTCRND 1: @6+19 LET C=A+B PRINT “DICE THROW If “SC THEN 195
DICE
GAME?"
THEN e210 THEN 225 11 THEN e25 “NO CHANGE”
PRINT
“YOU
WIN!
YOU
TRIPLED
YOUR
BET!"
GOTO 240 LET M=mMec PRINT “YOU
WIN?
YOU
DOUBLED
YOUR
BET!”
LET
GOTO
PRINT
M=mez
e4n
“YOU
LOST?”
LET T=T-" 607TO e245
LET
PRINT
T=T+m
“YOUR
TOTAL
PRINT “AGAIN? INPLIT AS IF AS="Y" THEN END
Note:
WINNINGS
ARE
115
Lines 130, 135: These statements work as if the computer was rolling the dice. They set the numeric variables A and B to a random number between 1 and 6.
98 - BASIC FUN
VARIABLE LIST M—Your bet A—Die throw #1
B—Die throw #2 C—Total of both dice T—Total winnings
FUNCTIONS:
Doing Complex Things the Easy Way * 99
NUMBER GUESSING GAME In this game the computer picks a number from 1 to 100, then 2 players try to guess the number. The player closest to the number wins 10 points.
What if the number was from 1 to 200? What would you change? SAMPLE RUN WELCOME TO THE NUMBER GUESSING GAME THIS If A GAME FOR TWO PLAYERS THE COMPUTER PICKS A NUMBER FROM 1 TO 100 EACH PLAYER GUESSES A NUMBER THE PLAYER CLOSEST TO THE CORRECT NUMBER GETS A PLAYER GETS 50 POINTS IF THE PLAYER GUESSES Ok INPUT YOUR GUESS? PLAYER 1 PLAYER 2770 PLAYER & WIN? THE COMPLITER’® NUMBER THE 2CORE It - PLAYER PLAYER AGAIN? Ok INPUT YOUR GUES= PLAYER 1750
PLAYER
WAS 64 1 0 2 10
27100
PLAYER 1 WINE THE COMPLITER’S IS SCORE THE
NUMBER PLAYER PLAYER
WAS 51 1 10 2 10
AGAIN? Qk INPUT YOUR GUESS PLAYER 1720 PLAYER e780 PLAYER 1 WINS THE COMPLITER’® NUMBER THE SCORE IS - PLAYER PLAYER
WHE 4c 1 20 2 10
100 + BASIC FUN
10 POINTS THE NUMBER
PROGRAM LISTING 100 105 110 115 120 125 130 135 140 145 150 155 160
DIM AS[1) PRINT “WELCOME TO THE NUMBER GUESSING GAME” PRINT “THIS If A GAME FOR TWO PLAYERS” PRINT “THE COMPLITER PICKS A NUMBER FROM 1 TO 100" PRINT “EACH PLAYER Ul ES A NUMBER” LET $=0 LET iM PRINT “THE PLAYER CLOSEST TO THE CORRECT NLUMBEF GETS 10 POINTS” PRINT “A PLAYER GETS S50 POINTS IF THE PLAYER GUESSES THE NUMBER” PRINT PRINT “Ok INPUT YOUR GUESS" PRINT “PLAYER 15 INPUT FP PRINT “PLAYER 2s INPUT N R=INTCRAD C13 1+.
IF Bs 1
7 THE 3
11 NEXT 9
OU 3
WERE
RIGHT» 10 17
YOU
WERE
RIGHTs
=HOULD
40
15 NUMBER er
ao
a
19 I3 1
CONGRATULATION 24 21
CONGRATULATION:
PROGRAM LISTING 100 105
110 115 120 125 130 135 140
PRINT “THIS PROGRAM PRINTS A PRINT “SEE IF YOU CAN FIGURE NUMBER £HOULD BE”
OF NUMBERS” THE NEXT
NTCRND CL» @c+1> NTCPND 1) eS413 INT CRAD (12 10412 INT CRNDID C13 @3412 : x s=1
THEN
145 150 155 160
REMee THIS N=AeC oo I GOTO 170 REMee THIS
165
N=A+l eR
170) 175
SEGUENCE OUT WHAT
I=1+1 IF I>S
THEN
12
AN
APITHMETRIC
SEQUENCE
190
FUNCTIONS:
Doing Complex Things the Easy Way * 103
BE
177 180 185 190 195 200 205 210 215 220
LET N=INT(N) PRINT N; GOTO 140 INPUT M IF M=N THEN 210 PRINT "SORRY, THE NEXT GOTO 110 PRINT "YOU WERE RIGHT, GOTO 110 END
Note:
Line 150: The computer calculates a geometric sequence.
NUMBER
IS
":N
CONGRATULATIONS!"
Line 165: The computer calculates an arithmetic sequence.
VARIABLE LIST S—Determines the type of sequence S=1 arithmetic sequence
S = 2 geometric sequence A—For arithmetic sequence —first number For geometric sequence— multiplier B—For arithmetic sequence—common difference C—For geometric sequence—common ratio M—Next number in sequence
104 » BASIC FUN
HOW WARM IS YOUR HEART? How warm is your heart? Use this program to find out. You type in a number from 1 to 5 and the computer decides how warm you really are. Don’t be surprised if the computer changes its mind. After
all, computers don’t know everything. You can add responses of your own to this program. Try writing a program of your own that evaluates someone’s personality or athletic ability.
SAMPLE RUN TO SEE HOW WARM TYPE
RA NUMBER
YOUR
FROM
HEART
1S
TO
5
2 HEART IS A NUMBER
ON FIRE?? FROM 1 70
5
2 HEART IS A NUMBER
IN THE FROM 1
NORTH 70 5
POLE?
2 HERRT
IN
NORTH
POLE?
IS
1
THE
PROGRAM LISTING 100 105 110 115 leo 125 130 135 140 145 150 155 160 165 170 175 180 185
PRINT PRINT
“TO SEE “TYPE A
INPUT
N
IF
N>S
THEN
HOW WARM YOUR NUMBER FROM 1
HEART TO 5“
IS”
NORTH
FOLE?”
105
S=INTCRND C1) 2) @(-2) +1
R=INT CRND (1) #541) T=N+S or
IF
T>1
PRINT
GoTo IF
T>2
THEN “YOUR
150 HEART
THEN
HEART
PRINT “YOUR GOTO 105
HEART
IF
T>4
IN
THE
IS
IN
A
IS
THAWING
IS
STARTING
165
PRINT “YOUR GOTO 105 IF T>3 THEN
PRINT
IS
105
THEN “YOUR
DEEP
FREEZE?”
180 OUT?”
195 HEART
FUNCTIONS:
TO
GLOW!”
Doing Complex Things the Easy Way + 105
1390
6oTO 105 PRINT “YOUR 60TO 105 END
195 eon 205
Note:
HEART
IS
ON
FIRES?™~
Line 120: Pretend that you are the computer, and try to calculate what value S can have when this statement is run.
VARIABLE LIST N—
R— S
T
Number input Random number 1 to 5 Determines if random number is added the number input Determines which response is printed
106 * BASIC
FUN
to or subtracted from
WAR Play the card game WAR with the computer. You and the computer each start with 26 cards. When it is your turn, input a number from 1 to 5. If you match with the computer, you have a BATTLE. Input a 1 or 2 to decide the outcome. If you pick the same number as the computer, you win. If it is different, the computer wins. The winner of the BATTLE gets all of the opponent's cards since the last BATTLE. The WAR is won when one of you gets more than 42
cards. Good Luck! Can you write games?
a program
that plays one
of your favorite card
SAMPLE RUN WELCOME TO THE GAME WAR WE EACH START WITH 26 CARDS NUMBERED 1-5 WHEN IT IS YOUR TURN - INPLIT YOUR CARD?1 COMPUTERS CARD 1 THIS IS WAR. PICK A TOSS WAS A 1 YOU WIN 1 CARDS YOUR CARD?2 COMPUTERS CARD 4 YOUR CRRD?S COMPUTERS CARD 4 YOUR CARD?2 COMPUTERS CHRD 4 YOUR CARD?4 COMPUTERS CARD 1 YOUR CARD?1 COMPUTERS CARD 4 YOUR CARD?4 COMPUTERS CARD 2 vYOUR CARD?3 COMPUTERS CARD 4 YOUR CRRDTe COMPUTERS CARD 2 THIS IS WAR. PICK A TOSS WAS A 1 YOU WIN 3 CARDS
1
OR
271
OR
271
1
FUNCTIONS:
A
NUMBER
1
-
5
Doing Complex Things the Easy Way + 107
PROGRAM LISTING DIM ASC1) PRINT “WELCOME TO THE GAME WAR” PRINT “WE ERCH START WITH 26 CARDS” PRINT “NUMBERED 1-5" PRINT “WHEN IT IS YOUR TURN - INPUT REMeeCOMPUTERS CARDS LET T=e6 REMeeYvOUR CARDS LET S$=e6 LET U=0 LET Y=0 PRINT PRINT “YOUR CARDS INPUT N LET V=V41 C=INT CRND C1) o54+15 PRINT “COMPUTER’S CARD “SC LET
NUMBER
U=Li+1
IF C=N THEN 220 IF UST THEN e210 REMee USED ALL CARDS IN LET o IF ¥ THEN 160 LET a 6OTO 160 REMeeHH A MATCHeo PRINT “THIS IS WAR. PICK INPUT P R=INT CRND C1) @c+1> PRINT “TOSS WA IF R=P THEN cat PRINT “I WIN
PILE
A
IF T>42@ THEN GOTO 145 PRINT
A
“YOU
WIN
"SUS"
CARDS”
LET 2=4+u LET -u IF £942 THEN 310 GOTO 145 REMeerLATER WINS GHMEoo PRINT OU WIN GAME” 60TO u FEMeeCOMPLTER WINtee PRINT “I WIN GAME” PRINT “I HAVE PRINT “YOU HAVE
108 + BASIC FUN
1
—
OR
TURN
2"s
PILE
OVER
1
-
5"
340 345)
PRINT PRINT
350 355 360 365 370
INPUT IF AS= THEN 105 PRINT PRINT “BETTER LUCK END
Note:
“TRY
AGAINGY7No"5
NEXT
TIME?”
Line 175: This statement randomly assigns a number from 1 to 5 to the numeric variable C. This is how a programmer makes the computer play games. Lines 125, 135: Notice how the REM statement is used to identify variables. This makes it easier to change or fix a program later.
VARIABLE LIST T—Number of cards the computer has S—Number of cards the player has U—Nunmber of cards computer has turned up V—Number of cards the player has turned up C—Computer’s number (1-5) N—Player’s number (1-5) P—Player’s guess during a battle (1-2) R—Random number determining winner of a battle
FUNCTIONS:
Doing Complex Things the Easy Way * 109
FOR... NEXT The Computer Repeats Itself The FOR. . . NEXT statements allow you to tell the computer to repeat some |
lines in your program as many times
as you want. 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. |
| PROBLEMS | 1. Fancy Counting | 2. Blast Off | 3. Pinball || 4. Paper, Rock, Scissors
5. Ring the Bell
FANCY COUNTING This program counts by fives. It is very similar to one in Chapter 3, but this one uses a new programming technique. Which program do you like better? Can you change this program to count by 2’s or by 10’s? SAMPLE RUN THIS
PROGRAM
COUNTS
BY
FIYES
-
WATCH
So
HOW DID YOU LIKE THAT? PROGRAM LISTING 100 110 120 130 140) 150
Note:
PRINT “THI® PROGRAM COUNTS BY FIYES FOR I=5 TO 50 2TEP 5S PRINT I NEXT I PRINT “HOW DID VOU LIKE THAT?” END
Lines 110-130: computer
This is called
executes
lines
110,
a FOR 120,
-
WATCH”
. . . NEXT and
130
and
loop. The then
goes
back to 110 and starts over. Each time line 110 is executed,
the variable I is increased by 5. When | is greater than 50 the’ computer skips around the loop and goes to line 140.
FOR
. . . NEXT:
The Computer Repeats Itself + 111
BLAST OFF This program counts backward from 10 to 1 and then Blasts Off! You can change the count-down sequence by changing line 30.
SAMPLE RUN HELLOs I AM HERE IS THE 10 9 i} 7
PERDY COUNT
TO BLAST DOWN
OFF
6
5
4
3 e 1 LAST
OFF
PROGRAM LISTING 10 20 #0 40)
PRINT “HELLOs I AM PRINT “HERE IS THE FOR I=10 TO 1 STEP PRINT I
So 60) 7Q
NEXT I PRINT “BLAST END
Note:
READY COUNT -1
TO BLAST DOWN”
OFF”
OFF"
Line 30: The FOR . . . NEXT statement can also use a minus step like this one. Then the computer stops the loop when! = 1.
112 + BASIC FUN
PINBALL The computer can play Pinball too. this game is 50; see how well you do. if your terminal has a bell. Try changing the characters (the during the game. You might use
The highest possible score in This game makes some noise letters or numbers) displayed names or nouns instead of
numbers. Try changing the program so the player has to pay a quarter to play, and then keep track of the total amount of money the player spends.
SAMPLE RUN TYPE
A
NUMBER
TO
START YOUR eoTwhee
BALL
OFF?74 oor IYEoo ooSl Xoo
ooTWOoe
oF lVEoe @oTHREEoo eos lXoo oF OURee
eos
Xoo
ooTWOee
YOUR YOUR
SCORE TOTAL
IS 41 SCORE IS
41
PROGRAM LISTING 100) 105 110) 115) 120 125 130) 135) 140 145 150 155) 160
«=DIM AS$C1) T=0 PRINT “TYPE A NUMBER TO START YOUR INPUT A S=0 FOR I=1 TO 10 R=INTCRND¢1) 641) S=S+R IF R>1 THEN 155 PRINT TABCRO10> »CHRS C7) » “eeDNEeo" GOTO 2e0 IF R>2@ THEN 170 PRINT TAB CRO10 sCHRS (7) »s “eoTWwOee” FOR
BALL
OFF"$
. . . NEXT: The Computer Repeats Itself * 113
SOTO
een
IF R>2 THEN 185 PRINT TAB SR@1 0 sCHRS 17) +" e*THREEOo" 60TO0 @en IF R>4 THEN & sCHRS (7). "eof OLIFoe” PRINT TARCRe1
6oTO
een
IF P>S THEN 215 PRINT THE CR@10) sCHRS
(7).
“oF
LVE oo"
PRINT TAB fR@1 i) sCHRS (7). NEXT I PRINT “YOUR SCOPE ITi"+2
“O*l
Xoo”
SOTO
een
TH=T+8 PRINT PRINT ” INPUT ASF IF AS="" END
Note:
SCORE THEN
Line 145: CHR$(7)
I2"+T
110
rings a bell on your terminal.
Line 150: You can use a GOTO end of the loop.
statement to jump to the
Line 125, 220: Notice how many lines of code are included in this
VARIABLE
FOR.
. . NEXT loop.
LIST
T—Total score S—Game score R—Points for each round
114 + BASIC FUN
PAPER, ROCK, SCISSORS You can play this game with your computer. Remember . . . rock breaks scissors, scissors cut paper and paper covers rock. Try changing the program so the computer keeps score.
SAMPLE RUN FLAY THE GAME —- FAFER, ROCK, SCISSORS MAKE YOUR CHOICE AND THEN TYFE IN R FOR ROCK, S FOR SCISSORS, F FOR PAFER TYPE X TO STOP OR YOU WIN ** COMPUTER HAD S Ss COMPUTER WON ** COMPUTER HAD R 2S WE MATCHED PR YOU WIN ** COMPUTER HAD S oS YOU WIN ** COMPUTER HAD F PF YOU WIN ** COMPUTER HAD R 2x
PROGRAM 1900 105 110 115 120 125 130 135 140 145 150 155 160 165 170
WITH
THE
COMPUTER
LISTING
DIM H$(4),A8(1) LET BS = "FSRX" FRINT "PLAY THE GAME - FAFER, ROCK, SCISSORS — WITH THE COMPUTER" PRINT "MAKE YOUR CHOICE AND THEN TYPE IN" PRINT "R FOR ROCK, S FOR SCISSORS, F FOR FAFER" PRINT "TYPE X TO STOF" LET C = INT ( RND (1) * 3 + 1) INPUT At REM **FIND OUT WHAT LETTER WAS TYFED IN FOR I= 1704 IF AS = MIDS (B%,1,1) THEN 170 NEXT I PRINT "I NEED A R,S,P,X PLEASE" GOTO 135 IF I = 4 THEN 250
FOR
. . . NEXT: The Computer Repeats Itself + 115
175 180 185 1990 195 200 205 210 215 220 225 230 235 240 245 250
Note:
REM **GOT A MATCH IF C = I THEN 240 REM **GOT P,R OR R,P IF C + I = 4 THEN 225 IF C > I THEN 210 PRINT “YOU WIN ** COMPUTER HAD "; MID$ GOTO 130 PRINT "COMPUTER WON ** COMPUTER HAD "3; GOTO 130 REM **FLAY WON IF C > I THEN 200 REM **xCOMPUTER WON GOTO 210 PRINT "WE MATCHED" GOTO 130 END
(B,C,1) MID$
(B$,C,1)
Lines 145-155: These statements translate the input character into a number which the program can use. Lines 160-165: This “error routine” is needed in case the input character is wrong.
VARIABLE LIST C—Computer’s choice I—Player’s choice in numeric A$—Character input by player COMPUTER HP-2000
NOTES
BASIC
Replace line 150 with:
150 IF A$=B$(I,1) THEN
170
Replace line 200 with:
200 PRINT “YOU HAD ”; B$(C,C)
WIN
**
COMPUTER
Replace line 210 with:
210 PRINT “COMPUTER PUTER HAD ”; B$(C,C)
116 * BASIC FUN
WON
** COM-
Atari-BASIC
Replace line 150 with:
150 IF A$=BS$(I,I) THEN 170 Replace line 200 with:
200 PRINT “YOU HAD ”, B$(C,C)
WIN
** COMPUTER
Replace line 210 with:
210 PRINT “COMPUTER PUTER HAD ”; B$(C,C) TI-BASIC
WON
** COM-
Replace line 150 with:
150 IF A$ = SEG$ (B$,I,1) THEN 170 Replace line 200 with:
200 PRINT “YOU WIN HAD ”; SEG$(B$,C,1)
**
COMPUTER
Replace line 210 with:
210 PRINT “COMPUTER WON PUTER HAD ”; SEG$(B$,C,1)
FOR
** COM-
. . . NEXT: The Computer Repeats Itself * 117
RING THE BELL Now is your chance to see how strong you are. In this game you are trying to ring the bell by hitting the platform with a sledgehammer—all pretend of course! The computer tells you how well you do. Try changing the game by adding carnival prizes after a certain
number of points. SAMPLE RUN YOU
ARE TRYING TO WIN THE STRONG MAN CONTEST AT THE OCAL CARNIVAL YOU ARE GOING TO SWING THE SLEDGE HAMMER AND TRY TO RING THE BELL TO SWING, TYFE A LETTER FROM A-Z THIS WILL COST YOU .25 - FUT YOUR QUARTER ON THE TERMINAL NOW YOU YOUR YOU THIS
MARE YOUR SWING?TE CAN HARDLY FICK UF - TOTAL SCORE 1S HAVE SPENT $.25 WILL COST YOU
THE
NOW MAKE YOUR SWING?A YOUR 90 YEAR OLD AUNT COULD HAVE SCORED 3 YOUR TOTAL SCORE IS 5 YOU HAVE SFENT $.5 THIS WILL COST YOU .25 - FUT YOUR QUARTER ON
THE
TERMINAL
190 1905 110° 115
QUARTER
2
TERMINAL
TO
YOUR
SCORED
THE
PROGRAM
FUT
YOU
ON
NOW MARE YOUR SWINGTF YOU MOVED THE WEIGHT UF YOUR TOTAL SCORE IS YOU HAYE SFENT #.75
-
HAMMER, 2
4 9
LISTING
DIM AS(1),BS¢(8) LET B& = "BCPOUVIR" FPRINI "YOU GRE TRYING TO WIN THE STRONG MAN CONTEST AT THE LOCAL CARNIVAL” PRINT "YUU ARE GOING 10 SWING THE SLEDGE HAMMER AND TRY TG RING THE BELL"
118 + BASIC FUN
12 125 130 135
145 150
PRINT "TO SWING, TYFE A LETTER FROM A~-Z" LET T=0 LET B oO PRINT "THIS WILL COST YOU .25 - FUT YOUR QUARTER ON THE TERMINAL" PRINT PRINT "NOW MAKE YOUR SWING"; INFUT A LET R= 8B + .25 REM *xCHECK FOR INPUT EQUAL TG ONE OF CHARACTERS IN BS FOR I= 1708 IF AS = MIDS (B$,1,1) THEN 175 NEXT I GOTO 185 LET C = INT ((I + 1) / @) GOTO 190 LET C=0 LET D = INT ¢ RND (1) &* 6 + 1) LETS =C+D ON S GOTO 210, 220, 250, 239, 240, 260,270 GOTO 280 PRINT "BETTER EAT YOUR WHEATIES, YOU ONLY SCORED 1" GOTO O PRINT "YOU CAN HARDLY PICK UF THE HAMMER, YOU SCORED 2" GOTO 300 FRINT "YOU MOVED THE WEIGHT UF TO 4" GoTo a PRINT "YOU SCORED A S - YOU MUST BE FRACTICING GOTO Te) YOUR 90 YEAR OLD AUNT COULD HAVE SCORED 3"
GOTO 300 PRINT CHRS (7) FRINT "CONGRATULATIONS! ! YOU RANG PRINT "YOU GET 8 FGINTS!!" LET T=T+S5S PRINT "YOUR TOTAL SCORE IS ",T PRINT "YOU HAVE SFENT $"3B GOTO 135 END
FOR
THE
BELL!!"
. . . NEXT: The Computer Repeats Itself * 119
Note:
Lines 155-180:
If the letter entered by the player is one of
the 8 letters stored in B$, this routine adds a bonus to the player’s score. Line 200: This GOTO
statement replaces 7 single GOTO’s. It
is very useful when a variable such as S can have many different values.
VARIABLE LIST T—Player’s total score B—Amc:nt player has spent A$—Input character B$—Contains a list of letters that add a bonus to the player’s score
D—Random number (1-6) C—Bonus point if player enters one of the letters in B$
S—Player’s score for this game COMPUTER NOTES
HP-2000 BASIC
The multiple GOTO
on line 200 has a differ-
ent format on the HP-200. with:
200 GOTO S OF 240, 260, 270
210,
Replace
220,
line 200
250,
Replace line 160 with:
160 IF A$ =B$ Atari BASIC
(I, I) THEN 175
Replace line 160 with:
160 IF A$=BS$(I,1) THEN 175 TI-BASIC
Replace line 160 with:
160 IF A$ = SEG$ (B$,1,1) THEN 175
120 * BASIC
FUN
230,
FANCY PRINTING (TAB) Writing It Your Way This function makes it easy for you to draw designs with the computer. It works like the tab key ona typewriter. It tells the computer to move a certain number of spaces across
the line and then print. You can use numbers,
variables or numeric
expressions inside the (_
ONaDORWNH
PROBLEMS . Rectangle . Design-a-Scarf
. HIHO . . . . .
Lifeguard Shape Party Fleet Award
).
RECTANGLE You can use the computer to draw lots of shapes. This program draws a rectangle. Use different numbers for the height and width and see what your rectangle looks like. Try writing a program that draws a triangle.
SAMPLE RUN THIS
PROGRAM
TYPE IN 710.20
DRAWS
HEIGHTs
° ° ° ° ° ° ° °
1 1 1 1 1 1 € é 2 2
Note:
RECTANGLE
° ° ° ° ° . ° °
PROGRAM 100
A
WIDTH
LISTING
PRINT “THIS PROGRAM DRAWS A RECTANGLE” PRINT “TYPE IN HEIGHTs WIDTH" INPUT Held FOr I=1 TO WwW PRINT “-"S NEXT I PRINT FOF I=1 TO H-2 PRINT “eS TAB CWA-135 "0" NEXT I FOR I=1 TO WwW PRINT “-"i NEXT I END
Line 140: The ; tells the computer to remain on the same print line. Try changing the ; toa , and see what happens.
122 + BASIC FUN
Line 180: This TAB function tells the computer to print the next * in column (W-1).
VARIABLE LIST H—Height of rectangle W—Width of rectangle
COMPUTER NOTES Atari BASIC Does not have the TAB function. Appendix A describes a method of converting these programs for the Atari.
FANCY
PRINTING:
Writing It Your Way * 123
DESIGN-A-SCARF Use your own initials to design a scarf. You can using this program. Notice that it is very similar to a rectangle. Try using something besides your initials for the What must you change if you want to use 4 design?
find a nice design the one that made design. characters in your
SAMPLE RUN DESIGN A INITIALS FMAZ
SCARF
I IT T I I I I I I I I I IMAZ I IT MAZ I I MAZ I
WITH
YOUR
INITIALS
MRAZ
I I af I I 1 J I I I I I I I I I I I
MAZ MAZ MAZ MAZ MAZ
DONE
PROGRAM LISTING
nh >
GIN [#30 PRINT "DESIGN A SCARF PRINT “INITIAL: INPUT I% LET H LET on
164 114 126 128 146 154)
124 + BASIC FUN
WITH
YOUR
INITIALS"
166 17@ 186 198 20@ 216 226 23@ 24@ 256 268 276 286 2396
FOR I =1TOH PRINT "-"s NEXT I PRINT FOR I = 1 70H - 2 STEP 2 PRINT “I"G LET R = INT ¢ RND (1) # CH PRINT TABC Rosl$s TABC H PRINT "I"s TABC H - 195"I" NEXT I FOR I =1T0H =PRINT "-"; NEXT I END
- 693 4+ 1 i3s"I"
VARIABLE LIST I—Characters to use in design H—Height of scarf outline
W—Width of scarf outline R—Column to print design
FANCY
PRINTING:
Writing It Your Way * 125
HIHO The TAB function helps make interesting patterns like this one. Can you write a program that prints WIN in the shape of a V?
SAMPLE RUN WATCH
WHAT
PROGRAM 100) 110° 120
I CAN
MAKE
LISTING
PRINT “WATCH WHAT PRINT PRINT FOR k=1 TO 3 PRINT TAB CKO S “HI"S
FOR
PRINT NEXT END
k=11
TO
20
I
CAN
MAKE”
TAR CO0-K3
5 "HO"
TABCE(-k> 3 "HOS TABCK? 3s "HI"
Kk
126 * BASIC FUN
LIFEGUARD You can have some fun with the TAB function too. In this game, you hear a swimmer calling for help and you try to throw him a life ring. If you hit him, you can save him—otherwise—it is Davy dones’s locker for him. Change the program so it only gives you four tries to save the swimmer.
SAMPLE RUN YOU MISSED: TRY AGAIN? WHERE YOU WANT IT TO LAND?S ia) HELP YOU MISSED: TRY FIAINE WHERE YOU WANT IT TO LANDS HELP O YOU MISSED: TRY AGAIN! WHERE YOU WANT IT TO LAND? 3 QO HELP YOU MISSED» TRY AGAIN? WHERE YOU WANT IT TO LANDS THANKS? YOU SAVED My LIFE?
PROGRAM LISTING 100° 110) 120 130 140 150 160 170) 1809 190 200 210 220 230 240
PRINT SE THIS PROGRAM TO HELP A DROWNING PRINT QO THROW THE LIFE RING: INPLIT 1-10" PRINT “WHERE YOU WANT IT TO LANDS LET X=INT¢CRND C139 e10+1> INPUT © IF S=x% THEN 230 IF S>% THEN 190 PRINT TABCS)$"O"3 TAB CX) § “HELP” =GOTO eon PRINT TARCX>$ “HELPS TABCS) $ “0” PRINT PRINT “YOU MISSED» TRY AGAIN?” GOTO 1e0 PRINT “THANKS! YOU SAYED My LIFE!” «END
FANCY
PRINTING:
SWIMMER”
Writing It Your Way * 127
VARIABLE LIST S—Column
where life ring is thrown
X—Column where swimmer is located
128 + BASIC FUN
SHAPE You can make 3-dimensional shapes like this one. The secret for success is to be very careful to count out each space. Once you understand how this program works, modify it to make a shape of your own.
SAMPLE RUN FAMILIAR
SHAPE
ooo
°
ere
*
eo @ e+reooreeeoeoe
°
°
°
° ¢
eooeoeoee
°
Ff YERY
°
°
PRINTS
eo ooo
ee
PROGRAM
°
THIS
PROGRAM LISTING 100 110 120 130 140
PRINT “THIS PROGRAM PRINTS A YERY PRINT TABCIS>3"¢@ FOR $=1 T0 6 PRINT TABC1S-S23"e"3 TAB CIS+ 994 "0" NEXT § FANCY
PRINTING:
FAMILIAR
SHAPE”
Writing It Your Way * 129
150 160 170 190 190 200 210 220 230 24) 250 Note:
FOR T=5 TO 1 STEP PRINT TAB(9)$"e"S TAB(2193"@ NEXT T FOR U=1 TO 8 PRINT TABS) $e" 3 NEXT U FOR ¥=5 TO 1 STEP PRINT TABCIS-¥) 3" NEXT Y PRINT TABCIS)3 "0" END Notice that each FOR tion of the cube.
130 + BASIC FUN
-1 TAB CIS-T)$ "@"3 TABCIS+T) 3 "05 TAB CIS? $"e"3 TABCZ193 "0" -1 e"3 TAB CIS) $e" 3 TAB CIS +9) 3 "0"
. . . NEXT
loop prints a different sec-
PARTY Use this program to print party invitations for your next party. This one asks for your name, date and time of the party and your
address. Try making an invitation of your own.
SAMPLE RUN MAKE UP YOUR OWN PARTY INVITATIONS YOUR NAME7BECKY YOUR ADDRESS745e25 ELMWOOD DRIVE DAY OF THE PARTY?MAY 20 TIME OF THE PARTY?1200 PLM. O.k. HERE IS YOUR INVITATION
PLEASE
To
COME
my
BIRTHDAY
PARTY
DATE: TIME: PLACE:
BY
MAY 30 1:00 PLM. 4525 ELMWOOD
DRIVE
BECKY
PROGRAM LISTING
100 110) 120 120 140) 150
DIM ASC25)]-BSC25) -C$l25) -DSlz5) PRINT “MAKE UP YOUR OWN PARTY INVITATIONS” PRINT “YOUR NAME"S INPUT DS PRINT “YOUR ADDRESS” INPUT AS FANCY PRINTING:
Writing It Your Way * 131
PRINT INPUT PRINT INPUT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT PRINT END
“DAY OF THE PARTY'S BS “TIME OF THE PARTY"S cs “O.K. HERE IS YOUR INVITATION"
TAR C15) 3 "PLEASE TAB (18)
“To
TABC14>3
"BIRTHDAY
TAB CIS) TABCIS TABCL
TABC1S)
132 * BASIC FUN
COME”
my"
§ "DATES
PARTY”
FLEET Watch this fleet of rocket ships blast off from your video terminal. Try adding your own ships to this fleet.
SAMPLE WATCH
RUN THIS
FLEET
II I] II mma
OF
ROCKET
SHIPS
BLAST
OFF?
II Il II —--*
PROGRAM LISTING 100 105 106 107 108 109° 110 120 125 130) 135 140 150 160 170 180 190 200 210
PRINT “WATCH THIS FLEET OF ROCKET SHIPS BLAST PRINT PRINT PRINT PRINT PRINT LET R=INTCRND C1) e20+2> PRINT TABCRD$"*"S TABCR+15) $ FOR I=1 TO 3 PRINT TABCR-1)3"I I“5TABCR+1495"I 1" NEXT I PRINT TABCR-2)5"7___s"3 TAB CR+1395 "7___s" FOR J=1 TO 15 FOR I=1 TO 100 «NEXT I PRINT NEXT J GOTO 110 END
FANCY
PRINTING:
OFF?"
Writing It Your Way * 133
Note:
Lines 150-190: These statements blast off your video screen. Lines 160-170: what happens.
134 + BASIC FUN
make
Slows the computer
down
the
{ieet of ships so you can see
AWARD Award yourself a certificate of excellence in horseback riding camp. We designed this one for a friend. Once you have this program working, try making up some of
your own. You forms.
can
use a
similar program
to print blank
checks
or club
eeeeeeeeeoeeeoe
DEER
CREEK
CERTIFICATE AWARDED TO
If
CoS
S
SSS
HES HHH
HHH
HHH
HSS
HHH
HHH
HH HHEHHT
HHS
PROGRAM LISTING 100
DIM
110 120
PRINT INPUT
As$Ce5)
130 140
PRINT “eee ee ooo eoeeee” FOR m=1 TO 5
150)
PRINT
“INPUT AS
‘OUR
NAME"S
ee
eee
eee
eeeereoeeoe
“e"sS TARCEO
FANCY
PRINTING:
Writing It Your Way * 135
CHT
HHH
LYN LIPSCOMB FOR SUCCESSFULLY COMPLETING A PROGRAM IN HORSEMANSHIP AND RIDING INSTRUCTION AT HORSE CAMP IN PALO ALTOs CALIFORNIA
HHTHHEH CTH
CAMP
HHH
HHH
THIS
HORSE
HEH
HHH
HHH
HH
OS
INPUT YOUR NAME?LYN LIPSCOMB eooeoeoeoeeoeoeoeoeo eee eee
EHS
SAMPLE RUN
160 170
NEXT M PRINT “@"STABRCE193 "DEER
180
PRINT
“e's
190) 200
PRINT PRINT
“@"3 TABC s"THIS CERTIFICATE IS“3 TABC60)3 "0" “e"S TABCZ6)3 "AWARDED TO"s TABCEDD $ "0"
e210 220
FOR X=1 TO 3 PRINT "“@"3 TABRCEDD Ss "oO"
240 250
PRINT PRINT
220
60 270 eso 290
300 305 310
NEXT
TABC
CREEK
HORSE
CAMP”$ TRB C6035"
“eo
X
“STAB C22) 5 AS3 TAB(60) 3 "oO" “eS TABC11>3 "FOR SUCCESSFULLY
| COMPLETING
A
]
PROGRAM'S TABCE0) 3 "oe" PRINT “e"S TABC1O>3"IN HORSEMANSHIP AND RIDING INSTRUCTION ATS TAB CED "oO" PRINT “e"S TAB C1495 "HORSE CAMP IN PALO ALTO? CALIFORNIA" TABC6O) 3 "eo" FOR m=1 TO 5 PRINT “e@"STABCED? Se"
NEXT M PRINT "“e ®@ ee eereveererereoerereoeoereoeoeveoe ooo e eee o” END
VARIABLE
A$—Name
LIST
to use on the form
136 * BASIC FUN
4
10 SUBSCRIPTS Where to Store Your Information You can store more than one number in a numeric variable by using subscripts. A subscripted variable is written A(3), where A is the variable name and the number inside the ( ) is the subscript. The subscript determines which of the numbers stored in the variables to use. For example: if you have 3 numbers in A, then A(1) is the first number, A(2) is the second, etc. You can input, print, and do calculations with subscripted variables just as with other numeric variables.
PROBLEMS 1. Most Valuable Player 2. Bank Statement 3. Pascal’s Triangle 4. Sea Battle
MOST VALUABLE PLAYER Help your coach pick the most valuable player on your soccer team. You rate each of the best 6 players in 3 categories, and this program calculates a MVP score. Can you fix this program so it can use more than 6 players? Can you change the program to add another rating category?
SAMPLE RUN THIS FROGRAM HELFS YOU FIND THE MOST VALUABLE FLAYER ON YOUR SOCCER TEAM RATE EACH OF THE 6 FLAYERS YOU THINK ARE THE ON THE FOLLOWING CATEGORIES 1 - 5(S BEST) SOCCER SKILLS ATTITUDE ATHLETIC ABILITY THE COMPUTER WILL CALCULATE A MYF SCORE
BEST
FLAYER
FLAYER PLAYER PLAYER THE RATINGS PLAYER 1 2 3 4 ie] 6
ARE
SKILL 1 2 1 4 2 =
AS
FOLLOWS
ATTITUDE 2 ze) i] 3 3 3
ABILITY 3 s i] 4 4 4
MVF 11 18 18 23 17 26
PROGRAM LISTING 95 100 110 120 130
DIM S(6),A(6),6(6),T(6) PRINT "THIS PROGRAM HELPS YOU FIND THE" PRINT "MOST VALUABLE PLAYER ON YOUR SOCCER TEAM" PRINT "RATE EACH OF THE 6 FLAYERS YOU THINK ARE THE BEST" PRINT "ON THE FOLLOWING CATEGORIES 1 - S(S BEST)"
138 + BASIC FUN
140 150 160 170 180) 190 200 210
PRINT PRINT PRINT PRINT PRINT FOR I PRINT INFUT
TAB( 10),"SOCCER SKILLS" TAB( 10),"ATTITUDE” TAR( 10),"ATHLETIC ABILITY" "THE COMPUTER WILL CALCULATE
MVF
SCORE"
=1 1706 "PLAYER "313 S(T),A(1),G(1)
+ "THE
ACI)
RATINGS
320 350
Note:
Line 95:
The
It saves
DIM space
under the same each number.
+
ARE
"FLAYER SKILL = 1706 " " " "sG(I)s" "sTC1) NEXT IT END
too.
A
GCI) AS
2
FOLLOWS"
ATTITUDE
ABILITY
"ssc(Io3"
statement for you
name.
&
"SACT)3"
is used for numeric
to store
Then,
MVF"
you
more
than
one
variables number
use subscripts to get to
Line 210: The ratings for player 1 are put in S(1), A(1) and G(1). The ratings for player 2 are in S(2), etc. Line
240:
Another
name
for a subscripted
variable
is an
array. VARIABLE
LIST
S—Soccer skills ratings A—Attitude ratings
G—Athletic ability ratings T—MVP score COMPUTER
NOTES
The spacing produced by the , (comma) and the ; (semicolon) varies with each version of BASIC. Therefore, this program may not SUBSCRIPTS:
Where to Store Your Information + 139
line up on your computer as shown in the sample output. Experiment with the spaces used in the print statements in order to fix it for your computer.
Atari BASIC
Does not allow subscripted variables in an input statement. To convert, replace line 210 with:
205 206 207 210
140 + BASIC FUN
INPUT X, Y, Z LET S(l)=X LET A(l) =Y LET Gil) =Z
BANK STATEMENT Now you can print your own bank statement. Use this program to show how your money increased (or decreased) during the last few months.
SAMPLE RUN BANKER*S PROGRAM - PRINTS A BANE STATEMENT INPUT BEGINNING BALANCE? 100 INPUT TOTAL NUMBER OF DEFOSITS AND WITHDRAWALS?4 NOW INPUT EACH DEFOSIT OR WITHDRAWAL WITHDRAWALS ARE (—) AMOUNT 1 fs) AMOUNT 27300 AMOUNTS?-S00 AMOUNT 47100
KK
BANE
STATEMENT
BEGINNING
PUNK
#
BALANCE
YOK
$100
AMOUNT
ACCOUNT BALANCE
200 300
Bao
-500 100
ENDING
BALANCE
$200
PROGRAM LISTING 100 110 120 130 140)
DIM T(15),D(14) PRINT "BANKER*S PROGRAM - PRINTS A BANK STATEMENT" PRINT "INPUT BEGINNING BALANCE"; INPUT A PRINT "INPUT TOTAL NUMBER OF DEFOSITS AND WITHDRAWALS";
SUBSCRIPTS:
Where to Store Your Information » 141
150 160 170 180 190 200 210 220 230 240 250 260 261 270 280 281 290 300 301 310 320 323 330 340 350 360 361 370 380
Note:
INPUT N PRINT "NOW INPUT EACH DEPOSIT OR WITHDRAWAL" PRINT "WITHDRAWLS ARE (-)" FOR I = 1 TON PRINT "AMOUNT";13 INPUT D(I) NEXT I T(1) = A FOR I= 1 TON T(I +1 ) = TCI) + DCI) NEXT I PRINT PRINT PRINT TAB(9) 3 "*xKK BANK STATEMENT sO" PRINT PRINT PRINT TAB(13); "BEGINNING BALANCE $";3A PRINT PRINT PRINT "#", "AMOUNT", "ACCOUNT" PRINT TAB(29):; "BALANCE" PRINT FOR I = 1 TON PRINT I,D(1),T(I+1) NEXT I PRINT PRINT PRINT TAB(15); "ENDING BALANCE #"3;T(N + 1) END
Line 240: Try playing computer, and do this calculation yourself. Why does the program store the result in T (I+ 1)?
VARIABLE
LIST
A—Beginning balance D—Deposits/withdrawals T—Running balance
COMPUTER
NOTES
Atari-BASIC
Replace line 200 with: 200 Input X
205 Dil) =X 142 » BASIC FUN
PASCAL'S TRIANGLE The numbers in this program have an interesting relationship. Can you figure out what it is? This is called Pascal’s triangle, named after
the mathematician numbers.
who
invented
this interesting arrangement
of
SAMPLE RUN HERE
IS
PASCAL’S
TRIANGLE
1 4
1 3 10
2 6
1 3 10
1 4
1 5
1
1
PROGRAM LISTING 100 110) 120 130 140) 150 160 170 180 190 200 205 210 220 240 250 e60 270 280 290 300 316
PRINT “HERE IS PASCAL“S TRIANGLE” DIM Ef10.10) LET M=35 REMeePUT NUMBERS INTO ARRAY LET ECis1)=0 LET N=6 FOR R=e TON ECRs1]=1 FOR TO R-1 ELCRs ECR-155-1] +E (R-1+5] NEXT & ECR»sR]=1 NEXT R REMeenNOW PRINT TRIANGLE FOR I=1 TON PRINT TABCM-¢I-1) e695 FOR J=1 TO 1 PRINT ECIsJ15" 3 NEXT J PRINT NEXT I END
SUBSCRIPTS:
Where to Store Your Information + 143
Note:
Line 190: This statement calculates each row of numbers. Notice that you can add together numbers in the same array. Line 110:
The array E has 2 subscripts. This is called a 2 di-
mensional array. A 2 dimensional array has rows and columns. In this case, the array E has 10 rows and 10 columns. VARIABLE LIST E—Contains the numbers for Pascal’s Triangle N—Number of rows to print
144 + BASIC FUN
SEA BATTLE In this program you and the Deploy your ships on a 4 x 4 square, and the computer will If this gets too tame for you, increase the size of the grid or
computer play the Sea Battle game. grid with each ship occupying one do the same. Then fire away! see if you can change the program to the number of ships.
SAMPLE RUN BATTLESHIP
WOULD YOU LIKE INSTRUCTIONS ¢Y/ND 7Y YOU HAVE A FLEET OF 6 SHIPS. EACH SHIP IS NUMBERED 1> @> OR 3¢2 OF ERCH NUMBER>. USING A 4 X 4 GRID YOU AND THE COMPUTER FIRE TORPEDOES AT EACH OTHER. WHEN YOU ARE ASKED» INPUT YOUR SHOT SUCH AS es3 STAYING WITHIN 4). WHEN THE COMPUTER SHOOTS; IF IT HITS A SHIPs INPUT WHICH SHIP IT HIT. WHEN THE COMPUTER MISSES» INPUT A 4. THE FIRST ONE TO DESTROY THEIR OPPONENT’S FLEETs WINS. GOOD LUCK???
SHOOTING AT @ 74 I HAVE A SCORE NOW YOU TRY YOU MISSED SHOOTING AT 1 74 I HAVE A SCORE NOW YOU TRY oa
°
°
1 OF 6 INPUT YOUR SHOTT2 +3 OF 6 INPUT YOUR SHOTT1+1 °
E
YOU HIT A 3 YOUR SCORE If 3 SHOOTING AT & se gee ° ° ° o E I HAVE A SCORE OF 32 NOW YOU TRY - INPUT ‘YOUR yOu MISSED SHOOTING AT 3 21
ia}
a
m
°
°
°
o
fe)
N
6
°
«
°
°
SUBSCRIPTS:
SHOTTese
Where to Store Your Information * 145
*4 I HAVE A SCORE OF 3 NOW VOU TRY — INPUT YOUR ° ° ° YOU HIT A 1 YOUR SCOPE If
PROGRAM
°
BE
HOT?
ie}
Qa
4
LISTING
Dim ust) REMeeTHI® PROGRAM SIMULATES A GAME OF BATTLESHIPoo PRINT TABRC27> “BATTLESHIP” PRINT PRINT PRINT PRINT “WOULD YOU LIKE INSTRUCTIONS c¥- No "Ss
INFLT
150 155 160 165 170) 175
US
IF US="N" THEN 205 PRINT “YOU HAYE A FLEET OF 6 SHIPS. ERCH SHIP IS NUMBERED 15” USING A 4 X 4 GRID® OF 3¢2 OF EACH NUMBER). “2s PRINT AT EACH TORPEDOES FIRE “YOU AND THE COMPUTER PRINT OTHER. ~ AS SHOTCSUCH YOUR INPUT YOU ARE ASKED. “WHEN PRINT PRINT IF" PRINT
“STAYING
WITHIN
“IT
A
HITS
SHIP:
49.
WHEN
INPUT
THE
WHICH
WHEN THE COMPLITER™ THE FIRST A 4. INPUT “MISSESs PRINT THEIR OPPONENT’2” PRINT “FLEETs WINS. GOOD LUCKI 22” PRINT PRINT PRINT PEMe@esET ZIZE OF GRILee DIM Cl4s4]+P (4:4) Dim Tle) PEMeelET ARRAYS TO CEROoe For I=1 TO 4
146 + BASIC FUN
COMPUTER
SHOOTS»
SHIP
IT
HIT.
ONE
TO
DESTROY
REMeesET L TO SIZE OF GRITeo L=4 mM=0 REMeeSET UP COMPLITER’S SHIPS FOR TO 2 FOR I=1 TO 3 R=INTCRAD 19 L941 NTCRND C19 oL 3 +1 CCRsS]=1 NEXT I NEXT J REMeeSET COMPLITER’S TARGEToo M=M+1 IF M>LeL THEN 465 R=INT CRND C13 L941 S=INTCRND 19 L341 IF PCRsS]>0 THEN 320 PRINT HOOTING AT INPUT H PCRs S]=H IF H=4 THEN 280
PRINT ad
“o ad
°
°
o
ON
Eg
L
*
L
GRIDee
A
N
6
°
is}
m
hd
Sn
TC1J=TC1]+H IF TC1}>11 THEN 465 PRINT “I HAYE A SCORE OF “#TC1) PRINT “NOW YOU TRY - INPUT YOUR
SHOTS
INPUT Pe IF CCRs: 5 THEN 415 IF CCRsS]>0 THEN 425 PRINT “YOU MISSen” GOTO 3
PRINT
6oTO
SPLASH!
PRINT
ALREADY
SHOT
THERE?!
B
ie}
CHES ¢7>
PRINT Ad
YOU
3e0 “o ad
°
°
°
a
PRINT “YOU HIT A “SC CRs 2) TC2]=T C2] +C (RS) PRINT “YOUR SCORE IS “#TCel CRs $]=5 IF Tl2]>11 THEN 465 60TO 320 PRINT TABR‘23: “GAME OVER” IF Tl2]>TC1] THEN 485 PRINT GOTO 4 PRINT PRINT
PCOMPLITER fyOu
WIN
“
WIN?
SUBSCRIPTS:
Where to Store Your Information * 147
495 sog 505 510 S15
PRINT PRINT PRINT “WANT A NEW FLEET INPUT US IF US="Y" THEN 115
S20 S25 S30 535
PRINT PRINT PRINT END
Note:
“BETTER
Line 130: statement
When gives
LUCK
NEXT
AND
A
NEW
GAME CY~ND
“5
TIME?”
the instructions to a game are long, this the player the option of skipping the
instructions.
Line 205: C and P are called 2 dimensional arrays. These arrays look like the grid you use to play this game. Each number occupies one cell in the grid. Line 355: Each time the computer shoots, the result of the shot is put in the player’s array. This keeps the computer from shooting at the same place twice.
VARIABLE
LIST
C—Computer’s ships on the 4 x 4 grid P—Player’s ships on the 4 x 4 grid T—Score for each side
148 + BASIC FUN
1] SUBROUTINES How
the Pros Do It
Subroutines are small programs that
a larger program uses over and over. Instead of including them every time you need them, they only appear once near the end of a program. A GOSUB statement in the large program tells the computer to leave that program and go to the subroutine. When
the subroutine is finished, a
RETURN statement tells the computer to go back to where it came from in the main program. PROBLEMS 1. Birthday 2. Math
Quiz
3. Tennis
BIRTHDAY How many days before your birthday? How many days between now and Christmas? This program can tell you. It converts each date into a Julian date and then calculates the difference. The Julian date is the number of days since some predetermined date, and in this program, that date is 1960. Notice that the program has to add extra days for leap years.
SAMPLE RUN WEE THIS PROGRAM BIRTHDAY
INPUT TODAY’? YEAR 71931 MONTHS
1-129
TO
FIND
DATE
AS
YOUR
NEXT
OUT
HOW
MANY
DAYS
UNTIL
YOUR
FOLLOWS
711
DAY ?4 INPUT DATE OF EAR? 1° MONTH «1-129 74 DAY? 1 THERE ARE 147 AGAIN
DAYS
BIRTHDAY
UNTIL
YOUR
BIRTHDAY
Ee
INPUT DATE OF YOUR NEXT BIRTHDAY TEAR 713281 MONTH© 1-123 711 DAS 0 THERE APE 26 DAYS UNTIL YOUR BIRTHDAY AGAIN 7N
PROGRAM
110° 115
LISTING
DIM ASC) De) PRINT “USE THI? PROGRAM TO FIND OUT HOW UNTIL YOUR BIRTHDA PRINT “INPUT TODAY’ 2 DATE AE FOLLOWS” GOLUB 195 BOSUB
e7t
PEMeNOmM BOSUE
MANY
FIND 310
c=T
150 + BASIC FUN
OUT
HOW
MANY
DATS
SINCE
1960
DAYS
140°
PRINT
“INPUT
DATE
1550 160 165 170
N=T-C PRINT PRINT INPUT
“THERE ARE “AGAIN” AS
D=TO
OF
YOUR
"SNS"
DAYS
NUMBER
255 260 265 270 275) 280)
RETURN REMee SUBROUTINE REMee TERR MONTH PRINT INPUT PRINT “MONTH(1-122"5
285 e290)
INPUT PRINT
M™ “DAY"S
295
INPUT
A
300 305
RETURN REMe@e
310 315)
REMe*e CALCULATES T=Y¥-1960
320 32S)
L=INTCT~4) T=¢(T-L) e365+Le366
330
FOR
335 340 345 350 355 360
T=T+DCI) NEXT I REMee ADD T=T+A RETURN END
Note:
I=1
NEXT
BIRTHDAY"
UNTIL
OF
DAYS
AND
YOUR
IN
EACH
BIRTHDAY”
MONTH
DAY
SUBROUTINE
TO
NUMBER
OF
DAYS
SINCE
1960
M-1
IN
DAYS
OF
THIS
MONTH
Line 115: This GOSUB statement tells the computer to execute statement number 195 next. SUBROUTINES:
How the Pros Do It * 151
Line 255: The RETURN
statement tells the computer to go
back and execute the line after the GOSUB statement. Subroutines are useful when you want to do the same calculation with different numbers. Lines 315-320: This statement figures out how years since 1960. Lines 330-340:
Add
many
in all the days of each month
leap of the
current year.
Lines 195-250: Since the number of days in each month won't change, it is easier to put these values into the program. Otherwise, you would have to input them every time.
VARIABLE LIST D—Array containing the number of days for each month in the year Y—Year M—Month A—Day
T—Number of days since 1960 for today’s date C—Number of days since 1960 for next birthday N—Number of days between T and C L—Number of leap years since 1960
152 + BASIC FUN
MATH QUIZ Here is a chance to test your math skills on the computer. You can do addition, subtraction, multiplication and division. The computer gives you the problem, and you input the answer. Can you change this program so it keeps track of the number of
correct answers? SAMPLE RUN INPUT MATH OPERATION + ADDITION - SUBTRACTION @ MULTIPLICATION * DIVISION “
YOU
WANT
TO
TRY
STOP
TO CHANGE OPERATION: TYPE INPUT OPERATION 2YMBOL?+
INPUT
LIMITS
OF
YOUR
INPUT
NUMBER
OF
INCORRECT
+
=7s
bal
YOU THAT
ARE
2
~1
AE
A
ANSWER
NUMBERS? PES
RIGHT
IS
é€
TRIES
MEOL?INPUT LIMITS OF YOUR NUMBERS?1+10 INPUT NUMBER OF INCORRECT RESPONSES 6 -1 =75 YOU ARE RIGHT 2-1 >
~Ss
="
INPUT OPERATION
MEOL?#
PROGRAM LISTING 100 105 110 115 120
DIM 0%(10),AS(1) LET O$="+-%/X" PRINT "INPUT MATH OPERATION PRINT "+ ADDITION" PRINT "- SUBTRACTION"
YOU
SUBROUTINES:
WANT
TO
TRY"
How the Pros Do It + 153
PRINT "x MULTIPLICATION” PRINT "/ DIVISION" PRINT"X STOP" PRINT "TO CHANGE OPERATIONS TYPE -1 AS AN ANSWER” PRINT "INPUT OPERATION SYMBOL "35 INFUT AS REM xxkxFIND OUT WHAT OPERATION NUMBER GOSUB S05 PRINT "INPUT LIMITS OF YOUR NUMBERS (L,H)" INPUT J,k PRINT" INPUT NUMBER OF INCORRECT RESPONSES"; INPUT T REM ¥*xGET 2 RANDOM NUMBERS M AND N GOSUB 370 1=0 ON A GOTO 210,245,285, 325 REM *xkkADDITIONK&AX L=M+N PRINT Mp "+ " 3 N53 " = "G INPUT PF IF P«O THEN 145 IF P=L THEN 435 GOSUBR 455 GOTO 220 REM **k SUET L=M-N PRINT Mp" - "She" = "3 INFUT FP IF Pio THEN 145 IF FP=L THEN 425 GOSUB 455 GOTO 260 REM ** MULT L=M4N
PRINT Mp" # INPUT F IF P*O THEN IF F=L THEN GOSUB 455 GOTO 200 REM *x DIV L=M/N PRINT M3" / INPUT F IF E®=T THEN PRINT
"SNS"
"3
=
"
145 435
"gNe"u 305
IF
W=6
THEN
250
IF IF
FO F=L
THEN THEN
145 425
154° BASIC FUN
=
355 360 365 370 375 380 385 390 395 400 405 415 420 425 430 435 440 445 450 455 460 465 470 475 480 485 490 495 S00 505 510 SiS 520 S25 S30 S35 540
Note:
GOSUB 455 GOTO 300 REM *x*xSUBROUTINE REM **KFINDS 2 RANDOM NUMBERS WITHIN THE LIMITS OF J & K M=INT(RND (1) #K +1) IF M O THEN 485 s=0 M=M+S PRINT "YOU MADE ";S;" MILES TODAY" PRINT “YOU HIKED "3M;"MILES TOTAL" IF M = = 50 THEN 720 D=D-1 F< oF = 2 IF Fo +1 = D THEN 540 IF G = © THEN S25 Get GOTO S35 FRINT "YOU RAN OUT OF FOOD" GOTO 705 PRINT "YOU ARE RUNNING LOW ON FOOD - BETTER SFEED UF TOMORROW" IF S + 6 THEN 585 PRINT "YGU HIFED LESS THAN 7 MILES TODAY" PRINT "YGU MUST MARE A DRY CAMF AND DRINK FROM YOUR CANTEEN" IF Wo» © THEN S75 FRINT "WHOOPS — YOU DON’T HAVE ANY WATER IN YOUR CANTEEN!!" IF —E = 1 THEN 79S FRINT "YOU HAVE TO GET WATER TOMORROW OR YOU LOSE” ES f w= 9 GOTG 385 2Ww = 1 FE
FO
LET R = INT ¢ RND (1) * & + 1) ON RF GOTO 595, 610,640,649, 640,640 PRINT "BEAR CARRIES OFF YOUR FACK CANTEEN"
-
LEAVES
YOUR
Fo GoTo 155 FRINT “SQUIRRELS GET INTO YOUR FACE - EAT 2 DAYS Or FOOD" FHrF-2 GOTO 155 PRINT "YOU SLEFT WELL ~ READY FOR A GOOD DAY OF HIE ING" GOTO 155 FRINT "YOU ARE IN A 50 MILE CROSS-COUNTRY BACKPACK RACE
GAMES:
Now You Can Do Your Own Thing * 173
655 660
FRINT "¥OU START WITH S DAYS GF FOOD AND 1 DAY OF WATER" PRINT "YOU CHOOSE YOUR HIFING SFEED EACH DAY AS FOLLOWS" PRINT " 1 - HIKE LESS THAN 10 MILES" PRINT " 2 - HIKE 10 MILES" FRING “ 3 - HIFE MORE THAN 10 MILES" FRINT " 4 - STAY IN CAMP" PRINT "“YQU LOSE AND HAVE TQ RADIO FOR HELF IF" PRINT “YOU GO WITHOUT WATER FOR 2 DAYS" PRINT "OR YOUR FOOD SUFFLY IS LOW FOR 2 DAYS" GOTO 120 FEM
*#HAVE
TO
DROF
OUT
OF
FRINT "RADIO FOR HELP, YOU GOTO 765 REM kx*YOU REACH THE FINISH
PRINT "CONGRATULATIGNS'' LINE! '"
730
R=
INT
€(
RND
35h = R * 10 740 IF R > 0 THEN
745 750 755 760 765 770 775 780
Note:
(1)
&
RACE
AREN*T
GOING
LINE
YOU REACHED
TO
MAKE
THE FINISH
3)
755
PRINT "YOU WON ~- FIRST PLACE!!" GOTO 765 PRINT "THERE ARE "3Ri" HIKERS AHEAD PRINT "BETTER LUCK NEXT TIME" PRINT "WANT TO TRY AGAIN"; INFUT A& IF AS = "Y" THEN 100 END
Notice that different things can fast the hiker chooses to travel.
happen
OF
YOU"
depending
on how
VARIABLE LIST F W D M S T J E
—Days of food left — starts with 5 —Water in canteen — starts with 1 —Days left to hike —Total miles traveled —Miles traveled during the current day —Flag used to control options, if T>0, program specifies speed —Hiking speed input —Set to 1 if player runs out of water. If player is out of water for 2 days in a row, forces player to radio for help. G —Set to 1 if player runs out of food. Forces player to radio for help if player is out of food for 2 days in a row. 174 » BASIC FUN
IT
APPENDIX A Differences Between the Versions of BASIC
BREAK KEY Every computer has a break—or program interrupt—key. However, some use a different name, or require a combination of keys. Use the programmer’s reference manual included with your computer to determine what it uses. In this text, we have adopted the convention of referring to this key as the break key.
RANDOM
NUMBER
this function
FUNCTION varies.
The
(RND)
Apple,
The format for
HP-2000,
Atari,
Os-
born and IBM use RND(1), the TRS-80 uses RND(0), and the TI uses RND. The Osborn and IBM computers also require the statement RANDOMIZE in the beginning of the program in order to get a different series of random numbers each time the program is run. PRINT
statement
spacing version such as columns puter as
using
, (comma)
or
; (semicolon)
The
produced by the , and the ; varies with each of BASIC. Therefore, some of the programs, MVP in Chapter 10, which print a heading and of numbers, may not line up on your comshown in the sample output. Experiment with
your own
computer to determine the proper spacing.
IBM BASICA, Osborn CBASIC, BASIC-Plus and Altair Extended Basic These versions of BASIC allow string arrays. The programs in this text use standard BASIC which does not have string arrays. In order to run these programs, leave out the string variables in the DIM statements. APPENDIX
A * 175
Atari-
Does not have a TAB function. You BASIC convert programs that use this function as follows: a) replace TAB(10);“*” -withPRINT “(10 spaces)”;“*” ~ b) replace TAB(W);“*” -with-
can
FOR I=1TO W-1 PRINT “”; NEXT I PRINT “*” Does not allow use of DIM statement with string TI-BASIC variables. In order to run the programs in this text, simply eliminate the string variables in the DIM statement in each program. —Does not have string functions needed in order to combine strings. Therefore, the following programs in Chapter 5 cannot be run on the TI:
RHYME AUTHOR Radio
Does not use the Shack—TRS-80 Color BASIC Therefore, leave ts. statemen word LET in assignment statements. For these entering this word out when example: Instead of: LETA=B + 1 A=B+1 Use:
The format for the. multiple GOTO HP-2000 BASIC statement is different from the one used in the programs in this text. Instead of: ON X GOTO line *,line*,..etc GOTO X OF line#,line*,..etc Use:
176 * BASIC FUN
MORE BASIC THAN AEC KIDS! Write your own computer games! PARENTS! Your kids can show you how!
TEACHERS! classes!
Use this book as a text for programming ;*s
There’s so much you can do on a complUter—once you know how. This easy-to-follow guide can serve as an introduction to any computer which uses the BASIC programming language. BASIC FUN Regins with the most simple programs for beginners and progresses to more difficult programs for expert programmers. There's everything from drawing a rocket ship that blasts off your video terminal to figuring out time differences around the world, and calculating your favorite ballplayer’s batting average to teaching your computer a magic trick. These programs are for anytime, anywhere —home and school—and anybody who's ever been interested, curious, or fascinated by computers.
80606 AVON