#Alphabetizing Program?
3 messages in this thread
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 need to make a program (in AmigaBasic!) that will sort # of letters and
then spew them out …
The reference to AmigaBasic indicates that you're working on an older Amiga
operating system, but no matter ..
Many programmers are anxious to make their sorts FAST, in which case
AmigaBasic is likely not the answer. Your best bet for speed would likely be
just to put the stuff onto a file and call the CLI/Shell command SORT, which is
quite efficient (on your older system, be sure that you have lots of stack or
you might crash), sending the output to the screen on whatever…
If you really need to do the sorting in AmigaBasic, you have a lot of possible
choices to make, a lot of possible methods to use. It's a whole area of study
in itself. The main choice looks like this:
Simple sort programs: twice the data will take four times as long to sort;
Complex sort programs: better, but harder to write.
And there are many supplementary questions to be asked. Will the data be
almost-sorted? Is there a reason to output data as the sort progresses? (the
speed is the same, but that l.o.n.g pause is reduced). Is the data known to
fall into a limited number of values (dates, values from 1 to 1000, for
example). Will sub-sorts be called for? (first name within sorted last name).
The simplest, and some say the least efficient, sort is called the "bubble
sort". You go through all adjacent pairs of records; if any pair is out of
order, you swap them; in any case, you keep going. Then you repeat the whole
procedure until you find no out-of-order pairs, at which time you're sorted.
–Jim
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