APPLICATIONSPROGRAMMATION ★ ARRAYS AND SORTS DE-MYSTIFIED (POPULAR COMPUTING WEEKLY) ★

Arrays and sorts de-mystified (Popular Computing Weekly)Applications Programmation
★ Ce texte vous est présenté dans sa version originale ★ 
 ★ This text is presented to you in its original version ★ 
 ★ Este texto se presenta en su versión original ★ 
 ★ Dieser Text wird in seiner Originalfassung präsentiert ★ 

Tied up with strings

Arrays and sorts de-mystified on the CPC464 by Geoff Baldwin

For many people taking their first tentative steps into the wonderful world of computer programming, the most difficult aspect to comprehend is the dimensioned array. Although at first, arrays seem complicated, they really are quite easy to understand.

The main reason for using arrays is to keep related data in a program together in one easy to manipulate block. For example, if you were writing a program to store names and addresses, it would be very difficult to handle the information if each item of data for the whole list of names and addressed were stored in the same string. It would be even more difficult to use a different string to store each name and each address.

The solution is to use one string and dimension it; for example, Dim A$(5,2). This means dimension A$ 5 x 2. What you have to do is imagine a tray, partitioned off into five sections by two sections, into which you can put information to be stored as shown in Fig 1.

From Fig 1, you will see that A$ has been given two dimensions, if you like, a vertical dimension of five and a horizontal dimension of two. From the diagram you will also see that if we examine A$(3,1) this would give us “HARRY” and A$(3,2) gives us “CARDIFF”. Each element of the array is capable of storing more than the odd word as in Fig 1. You could have a full name in A$(3,l) and a complete address in A$(3,2). Try the program in Fig 2 out for yourself.

Fig.110 DIM A$(5,2)
12 12
1A$(1,1)A$(1,2)1TOMLONDON
2A$(2,1)A$(2,2)2DICKLEEDS
3A$(3,1)A$(3,2)3HARRYCARDIFF
4A$(4,1)A$(4,2)4BILLGLASGOW
5A$(5,1)A$(5,2)5MARY OXFORD

The next program enables you to input five names and addresses one after the other, then prints them out in the order that you input them. This is fine if you only wish to sort a few names and addresses but for a long list it would be better if the program could sort and display them either in alphabetical order either by the names or the addresses.

Sorting strings or string arrays is another area of programming that requires a little thought. Practically everybody can look at a short list of names and mentally sort them into alphabetical order, but when it comes to a long list, say 40 or more, it starts to get difficult. When you have a list of 500 or more it then becomes almost impossible to do mentally and very time consuming to do at all by hand. Tasks like this are where the computer begins to show its tremendous speed and power.

When we sort a list of names, we generally start at the top of the list and sort out the A's, then the B's, then the C's and so on, then we sort the A's into order, the word's second letter, then the third letter, etc etc. The computer on the other hand, being a totally inhuman beast, can compare whole words, even whole sentences. When sorting words, for instance, it compares logically, looking at each word at a time and compares it with another, then decides which is the greater or lesser of the two.

The program in Fig 3 sets up a simple one dimension array, asks for 10 words or names, sorts them into alphabetical order then displays them. The way in which it does this is to compare the first word with the second, if the first word is greater than the second, the computer exchanges their respective positions in the list, then goes on to compare the second word with the third. If the third word is greater then the second, the computer will leave the words in the same positions and proceed to compare the third word with the fourth and so on to the end of the list. It then goes through the whole list to see if it's in order, if not it starts again moving words down the list one place at a time, until the task is completed. This method is called a bubble sort. Although there are other methods this is the simplest albeit slowest method. Type in the short program and try it for yourself by typing in ten words or names. Then try typing in ten words that are very similar like Mary, Mark, Marion, Market etc, and see the results.

The next thing to try is sort a two dimension array. The program in Fig 4 does this. The first part, as in the previous listing, sets up the array and requests the inputs. The second part sorts the array. Lines 50-60 allow you to choose whether the list is sorted by Name or Address. Lines 140-150 give you the option of entering new data or re-sort the existing data.

Do have a play with the example listings given. Then see if you can write your own programs to incorporate the ideas; a proper address file or a file of your record collection perhaps.

Although the listings in this article were written for the Amstrad CPC 464, they have purposely been kept in simple Basic so that with few exceptions they can be typed into almost any machine. Have fun!

PopularComputingWeekly

★ PUBLISHER: Popular Computing Weekly
★ YEAR: 1985
★ CONFIG: 64K + AMSDOS
★ LANGAGE: ???
★ LiCENCE: LISTING
★ AUTHOR: Geoff BALDWIN



★ AMSTRAD CPC ★ DOWNLOAD ★

Type-in/Listing:
» Arrays  and  sorts  de-mystified    (Popular  Computing  Weekly)    ENGLISH    LISTINGDATE: 2025-06-15
DL: 18
TYPE: PDF
SiZE: 726Ko
NOTE: Supplied by archive.org ; 4 pages/PDFlib v1.6

★ AMSTRAD CPC ★ A voir aussi sur CPCrulez , les sujets suivants pourront vous intéresser...

Lien(s):
» Applications » Super-GateArray ROM
» Applications » Arrays variabel dimensioniert (Happy Computer)
Je participe au site:
» Pour ce titre nous ne disposons de fichier executable sur CPC (Dump, Saisie du listing) , alors si vous avez ça dans vos cartons ou vous désirez usé vos petit doigts boudinés sur votre clavier faites le nous savoir.
» Vous avez des infos personnel ?
» Vous avez remarqué une erreur dans ce texte ?
» Aidez-nous à améliorer cette page : en nous contactant via le forum ou par email.

CPCrulez[Content Management System] v8.732-desktop/c
Page créée en 399 millisecondes et consultée 62 fois

L'Amstrad CPC est une machine 8 bits à base d'un Z80 à 4MHz. Le premier de la gamme fut le CPC 464 en 1984, équipé d'un lecteur de cassettes intégré il se plaçait en concurrent  du Commodore C64 beaucoup plus compliqué à utiliser et plus cher. Ce fut un réel succès et sorti cette même années le CPC 664 équipé d'un lecteur de disquettes trois pouces intégré. Sa vie fut de courte durée puisqu'en 1985 il fut remplacé par le CPC 6128 qui était plus compact, plus soigné et surtout qui avait 128Ko de RAM au lieu de 64Ko.