|
Post by jarychk on Jul 16, 2022 22:12:57 GMT
Here is a sample program for sorting but it is dropping the last character of any given string. I looked and thought but do not know why. No matter what string to sort the characters given, the very last character is lost. Maybe someone can find why.
What I try to make the program do: User give a word or some aphabetical string. Put the string characters in order into an array. SORT the array holding the characters into alphabetical order.
variable called flagswitched is to help to know when the WHILE loop does not need to be repeated.
dim gave$(80) input "give us a nice string. ";phrase$
sz=len(phrase$) for i=1 to sz-1 gave$(i)=mid$(phrase$,i,1) next i
flagswitched=1 while flagswitched=1 flagswitched=0 for i0=1 to sz-1 if gave$(i0)>gave$(i0+1) then let tmp$=gave$(i0) let gave$(i0)=gave$(i0+1) let gave$(i0+1)=tmp$ flagswitched=1 end if next i0 wend
print "SEE RESULTS " for i1=1 to sz print gave$(i1); next i1 print
END
|
|
|
Post by jarychk on Jul 16, 2022 23:30:49 GMT
FOUND THE PROBLEM. This next one after modification works.
dim gave$(80) input "give us a nice string. ";phrase$
sz=len(phrase$) for i=1 to sz gave$(i)=mid$(phrase$,i,1) next i
flagswitched=1 while flagswitched=1 flagswitched=0 for i0=1 to sz-1 if gave$(i0)>gave$(i0+1) then let tmp$=gave$(i0) let gave$(i0)=gave$(i0+1) let gave$(i0+1)=tmp$ flagswitched=1 end if next i0 wend
print "SEE RESULTS " for i1=1 to sz print gave$(i1); next i1 print
END
'superstupid way two for loops for i0=1 to sz-1 for i1=1 to sz-1 if gave$(i1)>gave$(i1+1) then tmp$=gave$(i1) gave$(i1)=gave$(i1+1) gave$(i1+1)=tmp$ end if next i1 next i0
Not all of the code and its history was taken out. The two FOR loops were an intermediary try only, and while did not hurt, were not part of the problem.
The problem was in the code section which filled the array, gave$(). Loop limit was cutting off the last character, because my thoughts were confused between assigning the array elements and performing the sorting. I had previously in the assigning part, this faulty code:
sz=len(phrase$) for i=1 to sz-1 gave$(i)=mid$(phrase$,i,1) next i
|
|