100
PROGRAMS
FOR THE
ACORN ELECTRON


Other books published by Prentice-Hall International   

   ELECTRON

BASIC PROGRAMMING ON THE ACORN ELECTRON,
Neil Cryer and Andrew Cryer
(book and cassette)

   BBC MICROCOMPUTER

BASIC PROGRAMMING ON THE BBC MICROCOMPUTER,
Neil Cryer and Pat Cryer

GRAPHICS ON THE BBC MICROCOMPUTER,
Neil Cryer, Pat Cryer and Andrew Cryer
(book and cassette)

BBC MICROCOMPUTER FOR BEGINNERS,
Seamus Dunn and Valerie Morgan

100 PROGRAMS FOR THE BBC MICROCOMPUTER,
John Gordon
(book and cassette)

THE BBC MICROCOMPUTER DISK COMPANION,
Tony Latham


100
PROGRAMS
FOR THE
ACORN ELECTRON


John Gordon
MEDC, Paisley College, Scotland

Prentice/Hall International

Englewood Cliffs, NJ London New Delhi Rio de Janeiro
Singapore Sydney Tokyo Toronto Wellington


British Library Cataloguing in Publication Data

Gordon, John
   100 programs for the Acorn Electron
   1. Electron (Computer) - Programming
   I. Title
   001 64'25 QA76.8.A
   ISBN 0-13-634858-0

Electron and Acorn are trademarks of Acorn Computers Ltd

© 1984 by JOHN GORDON

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmited in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior permission of the author.
For permission within the United States contact Prentice-Hall Inc., Englewood Cliffs, NJ 07632.

ISBN 0-13-634858 0
ISBN 0-13-634866 1 {CASSETTE}

Prentice-Hall International, Inc., London
Prentice-Hall of Australia Pty Ltd, Sydney
Prentice-Hall Canada, Inc., Toronto
Prentice-Hall of India, Private Ltd, New Delhi
Prentice-Hall of Japan, Inc., Tokyo
Prentice-Hall of Southeast Asia Pte Ltd, Singapore
Prentice-Hall, Inc., Englewood Cliffs, New Jersey
Prentice-Hall do Brasil Ltda, Rio de Janeiro
Whitehall Books Ltd, Wellington, New Zealand

10 9 8 7 6 5 4 3 2

Printed in Great Britain by A. Wheaton & Co., Ltd., Exeter


To Teresa and Jayne for putting up with me.


CONTENTS


SECTION 1

INTRODUCTION
P1Nitemare6
P2Musak8
P3Tunes10
P4Pattern12
P5Graph Plotting14
P6Bouncing Ball 117
P7Bouncing Ball 218
P8Jimmy19
P9Clock21

SECTION 2

GAMES
P10Wheel of Fortune25
P11Mastermind26
P12Guess the Number28
P13Reaction Test30
P14Gobble31
P15Tennis33
P16Bombs35
P17Bat'n'Moths38
P18Monster Island41
P19Battleships44

SECTION 3

BUSINESS
P20Loan Repayment Period48
P21Depreciation50
P22Four-weekly Moving Average52
P23, P24, P25Stock Control55
P23 Stock File Creation
P24 Transaction File Creation
P25 Stock File Update and Report
P26VAT Calculator58
P27True Rate of Interest59

SECTION 4

AT HOME
P28Simple Tax Calculator60
P29Loan Repayments62
P30Monthly Accounts66
P31Conversion66
P32Birthday Lists68
P33Diary70
P34Calendar72
P35Telephone List74
P36Investments76

SECTION 5

MATHEMATICS
P37Quadratic Equations78
P38Factorization80
P39Factorial83
P40Greatest Common Divisor85
P41Secant Method86
P42Method of Bisections88
P43Trapezoidal Rule90
P44Simpson's Rule92

SECTION 6

PAINTING ON THE SCREEN
P45Circles93
P46Interference95
P47Picture96
P48Picture Editor97
P49Zoom100
P50Worm102
P51Drawing104
P52Writing Text At The Graphics Cursor105

SECTION 7

DATA HANDLING PROGRAMS
P53Pie Chart107
P54Bar Chart109
P55Mean and Standard Deviation111
P56Bubble Sort114
P57Shell Sort116
P58Merge118
P59Binary Search120
P60Permutations122
P61Combinations123
P62Least Squares124

SECTION 8

RECREATION
P63Number of Days126
P64Digital Clock127
P65Kitchen Timer128
P66Recipes129
P67Encoder134
P68Decoder136
P69The Game of Life138
P70Biorhythms141
P71Dog Race143
P72Magic Matrix145
P73Dice147
P74Pools Program148
P75Shuffle149

SECTION 9

THE SCIENCE LAB
P76Number Base Conversion151
P77Colour Codes for Resistors154
P78Volumes of Solids156
P79 and P80Physics159
P79 Moment of Intertia
P80 Focal Length
P81Resistors164
P82Calculator165
P83Coordinate Conversion166

SECTION 10

GRAPHICS ROUTINES
P84Shading168
P85Translation170
P86Parallelogram172
P87Drawing Circles174
P88Shape Grabber177
P89Rotation179
P90Transformations182
P91General Transformations185
P923D-Rotation--1187
P933D-Rotation--2190
P94Perspective192
P95Rotating House196

SECTION 11

PROGRAMS FOR THE SCHOOL
P96Arithmetic Tutor199
P97 and P98French and German Tutorial
P97 French Tutorial
P98 German Tutorial
P99Spelling204
P100Counting207


100
PROGRAMS
FOR THE
ACORN ELECTRON



INTRODUCTION


It is with a sense of satisfaction that I present to you this selection of computer programs.

I have, in my selection, attempted to answer the question "What do you use a microcomputer for?". You will find routines in this book which cover the use of micros at home, in business, in school and for pleasure.

It is intended that after you have entered any of these programs then you would wish to record them onto tape. To help you, here is a reminder as to the saving of programs.

To save a program:


Once your program has been recorded successfully, you can reload it at any time by using the following procedure:

The Layout of The Book.


That's enough technical data, let us return to the task in hand, 100 programs for the ELECTRON microcomputer.

Each program is laid out in the following format:

Program layout


There has been much ballyhoo about structured programming in the computer press and there have been many attempts to discredit the BASIC language.

The ELECTRON dialect of BASIC is an attempt to overcome some of the limitations of "traditional" BASIC. It is possible to write fairly well laid out and well structured code in ELECTRON BASIC.

These programs represent an attempt to exploit these features of ELECTRON BASIC. However, the language does have its limitations. You will occasionally find some rather long IF statements, which can be difficult to read. This is due to a lack of a multi-line IF statement, as is given in other versions of BASIC such as COMAL.

It is possible to lay out programs using what is known as LISTO features, as used in the User Guide. I have not used this feature of the ELECTRON machine. I prefer to layout code in a manner that I find more natural.

When writing the programs, I first of all sketched out the program using a pseudo form of BASIC, which was fairly well structured. Thus when I come to code the programs, I already know the layout I required, so I did not use the layout features of the machine.

The Machine Code Routine FNchar


In a couple of the programs, you will find a rather strange looking routine, FNchar, which is used to inspect the screen to find out what character is at the position being inspected.

The routine is:

    830 DEF FNchar(U,V)
    840 PRINT TAB(U,V);
    850 A%=135
    860 =(USR(&FFF4) AND &FF00)/&100

Line 840 places the text cursor at position (u,v) on the screen. Line 850 places the value 135 (87 hex) into the accumulator. The routine called by USR(&FFF4) is one of the OSBYTE calls documented in the User Guide. ANDing the integer returned from the USR call with &FF00, masks off the least significant byte. Division by &100 leaves us with the ASCII code for that character.

Keying in Your Programs


To help you to key in your programs take note of the following points:

Bugs


It is possible, of course, for things to go wrong, for bugs to be introduced into your code.

Part of the fun in programming is the finding and correcting of bugs. Indeed, correcting bugs often contributes to the process of introducing bugs.

Bugs can be introduced in various ways. I might have left a few bugs in the code. Hopefully, through the efforts of Prentice-Hall in carrying out field tests, those will be at a minimum.

Bugs can be introduced when you are keing in your programs. To catch these bugs you have to go through each section of the program in turn and try to locate it bye eye and by testing the section. Using the page mode feature of the ELECTRON micro can be very helpful at this stage.

Common errors are:

Developing The Programs


In a sense, none of these programs are complete. They could all be expanded in various way.

One of the joys of programming is to take a simple routine and give it a professional user friendly appearance. I have not attempted to make any programs complete in this sense. This is left up to you.

Do not feel shy about using the programs as routines within others. Consider this book to be a software library.

However, if you do use a routine within work of your own, I would be pleased if you were to acknowledge the source.

GO ON ENJOY YOURSELF.