Reputation: 31
Here is the code, it takes 5 names and alphabetizes them. I have a sub that sorts the names but apparently i'm not calling it in the correct way.
DIM i AS INTEGER
DIM q AS INTEGER
DIM a AS STRING
DIM n(5) AS STRING
DIM z AS INTEGER
DIM x AS INTEGER
DIM y AS INTEGER
DIM g AS INTEGER
start:
CLS
FOR i = 1 TO 5
INPUT "Name:", n(i) 'receiving 5 names
NEXT i
call Sort() **This is the line with the error**
PRINT "Names in Alphabetical Order:"
FOR i = 1 TO 5
PRINT n(i)
NEXT i
INPUT "Again(y, n)", a
IF LCASE$(a) = "y" THEN GOTO start
IF LCASE$(a) <> "y" THEN STOP
'end of main program
SUB Sort (`enter code here`)
FOR i = 1 TO 5
FOR q = 1 TO 4
IF LCASE$(n(q)) > LCASE$(n(q + 1)) THEN SWAP n(q), n(q + 1)
IF LCASE$(n(q)) = LCASE$(n(q + 1)) THEN
z = LEN(n(q))
x = LEN(n(q + 1))
IF z > x THEN g = x
IF z < x THEN g = z
IF z > 1 AND x > 1 THEN
FOR y = 2 TO g
IF LCASE$(MID$(n(q), y, 1)) > LCASE$(MID$(n(q + 1), y, 1)) THEN
SWAP n(q), n(q + 1)
END
END IF
IF y = g THEN SWAP n(q), n(q + 1)
NEXT y
END IF
END IF
NEXT q
NEXT i
END SUB
I have tried putting a parameter in the sub, "SUB Sort(n())", the array that has the names in it, but it said incorrect array type.
Upvotes: 2
Views: 1698
Reputation: 1165
Here is an easy way to pass an array to a sort subroutine in qb64:
DIM n(5) AS STRING
n(1) = "zachary smith"
n(2) = "xerox johnson"
n(3) = "yackle jones"
n(4) = "arnold aglet"
n(5) = "beth anderson"
CALL Sort(n())
FOR i = 1 TO 5: PRINT n(i): NEXT
END
SUB Sort (n() AS STRING)
FOR i = 1 TO 5
FOR j = i + 1 TO 5
IF n(i) > n(j) THEN SWAP n(i), n(j)
NEXT
NEXT
END SUB
Here is an easier way to call a sort subroutine without specifying a parameter:
DIM SHARED n(5) AS STRING
n(1) = "zachary smith"
n(2) = "xerox johnson"
n(3) = "yackle jones"
n(4) = "arnold aglet"
n(5) = "beth anderson"
CALL Sort
FOR i = 1 TO 5: PRINT n(i): NEXT
END
SUB Sort
FOR i = 1 TO 5
FOR j = i + 1 TO 5
IF n(i) > n(j) THEN SWAP n(i), n(j)
NEXT
NEXT
END SUB
Upvotes: 2
Reputation:
Your problem is the fact that you typed SORT()
when there are no arguments.
Invoking a SUB
or FUNCTION
with correct syntax can be a bit strange in QB at first, and the syntax for the CALL
keyword makes things even more confusing:
' No arguments means you MUST NOT include the () afterward.
CALL SubNameE
' One non-array argument: n
CALL SubNameX(n)
' Two non-array arguments: a, b
CALL SubNameY(a, b)
' An array argument and a non-array argument: arr(), v
CALL SubNameZ(arr(), v)
Notice how arrays are passed to SUB
and FUNCTION
procedures using name()
. The interpreter probably gets confused when you write call SORT()
as SORT()
is the syntax for an array, not a SUB
/FUNCTION
call with no arguments.
You can also write the above lines of code without the CALL
keyword if that helps you:
' No arguments means only the procedure name is listed.
SubNameE
' One non-array argument: n
SubNameX n
' Two non-array arguments: a, b
SubNameY a, b
' An array argument and a non-array argument: arr(), v
SubNameZ arr(), v
Note that for a SUB
or FUNCTION
to accept an array argument, it must have them declared, like this:
SUB SORT (n() AS STRING)
' or if you wanted to use a type suffix
SUB SORT (n$())
Upvotes: 2