Alphabetizing Program?
28-Aug-95 22:11:10
Sb: #48002-Alphabetizing Program?
Fm: Stewart 100241,173
To: COLIN -. GIBSON 102567,1406
Colin
<<Help! I'm A 14 year old amiga basic programmer, and I need to make a program
that will sort # of letters and then spew them out in numarcel order, etion ,
there were more e's than any other letter, t was next, If any one can tell me
how to write such a program, your karma will be increased, and you have done
your good deed for the day! Thankz! >>
I don't think that you are trying to sort the letters but count them.
The easiest way to do that would be to declear an array with the same number of
elements as there are different letters(26 if you are only counting lowercase,
52 if you are counting upper and lower, and an extra for every other character
you wish to count i.e .!@#$%^). Every time you come to a letter you add 1 to
the number in its position in the array i.e. if you are storing the letter 'a'
in position 1 and you encounted an 'a' your code would something like
MyArray[1] = MyArray[1] + 1
It can be a bit of a pain working out which position the variouse letters are
stored in if you don't know how. The best way is to use the ORD function which
returns the ASCII value of the character that you pass it e.g
ORD("A")
would return 65 as 65 is the ASCII value for the letter 'A' and 66 is the ASCII
value for 'B' etc. If you are only storing uppercase letters you code may look
like
while more letters
MyArray[ ORD(letter) – 65 ] = MyArray[ ORD(letter) – 65 ] + 1
Get next letter
wend
ORD(letter) – 65 for the letter 'A' would give 0, this assumes 0 based indexing
of arrays
Once you have counted all the letters you know need to print them in order of
most to least. The easiest way for you to do this would me something like this
dim LargestPos 'position where largest count was found
dim Largest 'the value in the LargestPos
dim StillMoreLetters 'boolean value set to true when no more letters
dim LargestFound 'boolean value set to true when largest count found
dim i
StillMoreLetters=TRUE
while StillMoreLetters = TRUE
LargestPos = 0
Largest = 0 'set the largest value to 0
i = 0
StillMoreLetters = FALSE
for i = 0 to NUMBER_OF_LETTERS
if MyArray[i] > Largest then
LargestPos=i
Largest=MyArray[i] 'set the largest to this one
' as it is larger then the one we
had
StillMoreLetters = TRUE 'the largest
value wasn't 0 therfor
'we have to keep going
endif
next i
MyArray[LargestPos] = 0
PRINT CHR$(LargestPos + 65) 'print the letter
wend
Thats probably the easiest way to print your letters, there are much better
ways and if you like I can tell you how. One dissadvantage of this method
apart from its speed is that the array has no info in it when you are finished.
If you require the data in the array you will have to copy it first.
There may be a few bugs in this code as I havn't coded in basic for some time
now. If you have any problems then let me know and I will try to help you.
Stew