So I'm not overly familiar with QB64 and the documentation is sparse so I'm having trouble.
TYPE character
FName AS STRING * 12
LName AS STRING * 12
ID_Num AS INTEGER
Year AS STRING * 2
GPA AS DOUBLE
END TYPE
DIM LENGTH AS INTEGER
LENGTH = 11
REDIM chars(0) AS character
CLS
n$ = "names1.txt"
OPEN n$ FOR INPUT AS #1
k = -1
WHILE (NOT (EOF(1)))
k = k + 1
REDIM _PRESERVE chars(k) AS character
INPUT #1, chars(k).FName, chars(k).LName, chars(k).ID_Num, chars(k).Year,
chars(k).GPA
WEND
CLOSE #1
CALL sortArray(chars(), LENGTH)
SUB sortArray (score() AS INTEGER, SIZE AS INTEGER)
DIM x AS INTEGER
DIM y AS INTEGER
DIM COMPS AS INTEGER
x = 0
y = 0
COMPS = SIZE - 1
WHILE y < COMPS
x = 0
WHILE x < COMPS
IF score(x) > score(x + 1) THEN
CALL swap2(score(), x)
END IF
x = x + 1
WEND
y = y + 1
WEND
END SUB
SUB swap2 (score() AS INTEGER, x AS INTEGER)
DIM temp AS INTEGER
temp = score(x + 1)
score(x + 1) = score(x)
score(x) = temp
END SUB
I'm receiving an error - "Incorrect array type passed to sub" on the following line:
CALL sortArray(chars(), LENGTH)
I am assuming that since I used REDIM above, that the sub isn't processing it appropriately, but I'm not really sure how to fix that. Ultimately I'm trying to read in a file to an array, sort that array and then print it out to the user. Currently I'm stuck as to how to actually sort the array.
Any help will be appreciated. Thank you!
You're passing the array chars()
, containing elements of the type character
, but your subroutine only accepts an array containing elements of type INTEGER
.
What you need is score() AS character
(better named chars() AS character
), and you can sort based on whatever criteria you desire (e.g. IF score(x).GPA > score(x+1).GPA THEN ...
). Also, there is a built-in SWAP
statement that will swap things for you, so there's no need to write your own (unless you're supposed to).