Sorting alphabetically help
Posted: Thu Nov 24, 2005 11:16 am
I have a qbasic program that needs to be able to read names from a data file, print the names, then sort and print the names alphabetically. After the names are sorted, it outputs the names sorted alphabetically to a data file. Lastly, the program performs a search where the user enters a name to search and the program outputs if the name entered was found in the list of names or not.
That?s an overview of what the program does, however I am stuck on one particular part and was hoping I can get some help here. I have everything working in the program as I want, except the sorting alphabetically section. To me it looks like the logic is correct in B020SortArray, however when the program prints the list of sorted names their not sorted alphabetically.
Here is the program logic if anyone would be willing to help me out:
I am also wondering if there is a more efficient way to do this(found in B020SortArray):
Thanks in advance for any help.
That?s an overview of what the program does, however I am stuck on one particular part and was hoping I can get some help here. I have everything working in the program as I want, except the sorting alphabetically section. To me it looks like the logic is correct in B020SortArray, however when the program prints the list of sorted names their not sorted alphabetically.
Here is the program logic if anyone would be willing to help me out:
Code: Select all
DECLARE SUB B000InputArray ()
DECLARE SUB B010PrintArray (strTitle AS STRING)
DECLARE SUB B020SortArray ()
DECLARE SUB B030WriteArray ()
DECLARE SUB B040SearchArray ()
CLS
CONST intSIZE = 15
CONST intRow = 15
DIM SHARED strNames(intSIZE) AS STRING
DIM SHARED intPageCount AS INTEGER
DIM SHARED NUM(1 TO 26)
CALL B000InputArray
CALL B010PrintArray("UNSORTED NAMES LISTING")
CALL B020SortArray
CALL B010PrintArray("SORTED NAMES LISTING")
CALL B030WriteArray
CALL B040SearchArray
END
SUB B000InputArray
DIM intSubscript AS INTEGER
OPEN "names.txt" FOR INPUT AS #1
intPageCount = 1
intSubscript = 1
DO WHILE intSubscript <= intSIZE
INPUT #1, strNames(intSubscript)
intSubscript = intSubscript + 1
LOOP
CLOSE #1
END SUB
SUB B010PrintArray (strTitle AS STRING)
DIM strNewPage AS STRING
DIM intSubscript AS INTEGER
PRINT
PRINT "Press enter to see page"; intPageCount
INPUT strNewPage
CLS
PRINT strTitle, "Page"; intPageCount
PRINT
intPageCount = intPageCount + 1
intSubscript = 1
DO WHILE intSubscript <= intSIZE
PRINT strNames(intSubscript)
intSubscript = intSubscript + 1
LOOP
END SUB
SUB B020SortArray
DIM strPass AS INTEGER
DIM intSubscript AS INTEGER
DIM sngTemp AS SINGLE
NUM(1) = A
NUM(2) = B
NUM(3) = C
NUM(4) = D
NUM(5) = E
NUM(6) = F
NUM(7) = G
NUM(8) = H
NUM(9) = i
NUM(10) = J
NUM(11) = K
NUM(12) = L
NUM(13) = M
NUM(14) = N
NUM(15) = O
NUM(16) = P
NUM(17) = Q
NUM(18) = R
NUM(19) = S
NUM(20) = T
NUM(21) = U
NUM(22) = V
NUM(23) = W
NUM(24) = X
NUM(25) = Y
NUM(26) = Z
FOR strPass = 1 TO 25
FOR i = 1 TO 26 - strPass
IF NUM(i) > NUM(i + 1) THEN
SWAP NUM(i), NUM(i + 1)
END IF
NEXT i
NEXT strPass
END SUB
SUB B030WriteArray
DIM intSubscript AS INTEGER
OPEN "namesort.txt" FOR OUTPUT AS #2
intSubscript = 1
DO WHILE intSubscript <= intSIZE
WRITE #2, strNames(intSubscript)
intSubscript = intSubscript + 1
LOOP
CLOSE #2
END SUB
SUB B040SearchArray
DIM strNameID AS STRING
DIM intSubscript AS INTEGER
DIM strFound AS STRING
INPUT "Enter a name to search, use the format: Last, First. Use quotes. ", strNameID
intSubscript = 1
strFound = "NO"
DO WHILE intSubscript <= intSIZE AND strFound = "NO"
IF strNameID = strNames(intSubscript) THEN
PRINT "NAME FOUND"
PRINT strNameID, strNames(intSubscript)
strFound = "YES"
END IF
intSubscript = intSubscript + 1
LOOP
IF strFound = "NO" THEN
PRINT strNameID; " NAME NOT FOUND"
END IF
END SUB
Code: Select all
NUM(1) = A
NUM(2) = B
NUM(3) = C
NUM(4) = D
NUM(5) = E
NUM(6) = F
NUM(7) = G
NUM(8) = H
NUM(9) = i
NUM(10) = J
NUM(11) = K
NUM(12) = L
NUM(13) = M
NUM(14) = N
NUM(15) = O
NUM(16) = P
NUM(17) = Q
NUM(18) = R
NUM(19) = S
NUM(20) = T
NUM(21) = U
NUM(22) = V
NUM(23) = W
NUM(24) = X
NUM(25) = Y
NUM(26) = Z