• Aucun résultat trouvé

Simple Shuffle

Dans le document the Programming (Page 98-101)

Sorting, Searching, Shuffling

Program 4-14. Simple Shuffle

Another shuffling program, "Queens," is listed as Program 4-15. It positions queens on an N X N chessboard so that no queen attacks another queen. Rather than test completely random boards, it retains most of an unsuccessful test and switches an attacking queen at random, producing results quite rapidly. The speed tapers off with larger boards; solving the problem of a 20 X 20 board may take many hours.

Program 4-15. Queens

Refer to the "Automatic Proofreader" article (Appendix C) before typing in this program.

9 REM *** GENERATE RANDOM STARTING POSITIONS FOR B

Effective Programming in BASIC

220 IF K<>Q(J) THEN PRINT "w" i :rem 230 230 IF K= Q(J) THEN PRINT "'0"; :rem 164

240 NEXT: PRINT :rem 156

250 NEXT: PRINT :rem 157

300 GOTO 30 :rem 47

It's sometimes handy to use random numbers to help solve simulation prob-lems. At a simple level, Program 4-16 prints the total of two dice, and also keeps a running score so it can print the average number of throws between 7's. This takes some math skill to do analytically; the answer is that it takes six throws on average to score 7.

Program 4- 16. Dice

o

REM ** DICE SIMULATION **

10 S=S+l

20 D1%=1 + 6*RND(1) 30 D2%=1 + 6*RND(1) 40 PRINT D1% D2%

50 IF D1%+D2%=7 THEN PRINT "SEVEN";: N=N+1: T=T+S:

S=0: PRINT TIN 60 GOTO 10

Data Structures

BASIC's data structures include files, DATA statements, variables, and RAM storage.

Files, which store data externally on tape or disk, aren't limited by available RAM, and are necessary for handling large amounts of data. Disk files are generally more versatile than tape, since several disk files can be accessed at once and search time is far less. Chapters 14 and 15 give details of tape and disk programming respectively.

You've seen program examples using DATA. Obviously DATA cannot be changed in the way variables can.

Simple variables are widely used in BASIC. Chapter 6 explains exactly where they are placed in memory and how their values are stored.

Arrays (subscripted variables) offer a powerful extension to the concept of vari-ables and are worth mastering for many serious applications. They provide a single name for a whole series of strings or numbers, using one or more subscripts to distinguish the separate items (called elements).

One-dimensional arrays have a single subscript which may take any value from 0 to the value used in the DIM statement which defined the array (or to 10, if DIM wasn't used). DIM A$(50), N%(100), SX(12) defines three arrays (string, integer, and real number respectively), and space is allotted in memory for them (except for the strings).

These arrays can be visualized as a set of consecutively numbered pigeonholes, each capable of storing one value, and initialized with contents zero. A string array might hold values like this: A$(O)="ZERO", A$(1)="ONE", and so on, so PRINT

A$(J) would print the string at pigeonhole J, provided

J

fell into the correct range. It might hold strings ready for sorting, so that A$(O), A$(l), and so on would be ar-ranged alphabetically after sorting.

Numeric arrays can store the results of calculations; many of the examples of this section use such arrays. For example, numbers 1 to 52, stored in an array, can represent playing card values; similarly, 1 to 8 in an array can represent the position of queens on a chessboard, where 1-8 denotes the file in which that column's queen is placed. It's often worth setting up and saving tables of calculation results, which can be looked up rather than recalculated. Chapter 13's sound calculations illustrate how this is done.

Array variables are slower than simple variables, because of the processing over-head, but they are more versatile. DIMA$(50) gives control over 51 variables and as-signs each a unique name. Without this facility, you'd have to define 51 individual names, and the resulting loss of speed would be significant.

Two-dimensional arrays have two subscripts. DIM C(8,8) defines a number array with room for 81 numbers, which might be used to record a chess position, with positive and negative numbers, in which sign represents color and magnitude represents the type of piece.

Two-dimensional arrays are also valuable for storing data for business reports.

For example, sales figures may be available for ten types of items, in 12 different outlets. An array can keep the sets of data distinct; subtotals and overall totals can be conveniently stored in zeroth elements.

Integer arrays which store numbers from -32768 to 32767 are particularly ef-ficient in storing data and can be loaded from disk as a block. It's possible to tele-scope surprisingly large amounts of data into memory like this, although the programming is likely to be difficult.

Arrays with more than two dimensions can be created, but they aren't used much, probably because of the difficulty of visualizing the data's storage pattern.

DIM X(2,2,2) can be pictured as three dimensional tic-tac-toe, with element X(l,l,l) in the center position of the center plane. After this, depiction becomes progressively more complicated. In practice, large numbers of dimensions soon exhaust VIC's memory.

Matrix arithmetic, which manipulates arrays with rules for addition and mul-tiplication, is perfectly feasible on the VIC. Briefly, a matrix is an array of form A(R,C), where Rand C indicate the numbers of rows and columns. Its arithmetic rules are important to the solution of simultaneous equations. Complex simultaneous equations, and analogous applications, include predictions in biology and economics.

Matrix multiplication is essential to the solution of such problems.

Effective Programming in BASIC

Dans le document the Programming (Page 98-101)