Anonymous
2007-10-22 16:59:24 UTC
Hello,
In the code below the "BinarySearch" function returns 0, although it is
assigned a value of 8.
Anyone any idea?
--
#COMPILER PBCC 4.04
#COMPILE EXE
#DIM ALL
#DEBUG ERROR ON
#TOOLS ON
'-----------------------------------------------------------------------------------------
FUNCTION BinarySearch(i() AS LONG,BYVAL a AS LONG,BYVAL b AS LONG,BYVAL Find
AS LONG) AS LONG
TRY
REGISTER m AS LONG
LOCAL t AS LONG PTR
t = VARPTR(i(0))
IF b - a <= 0 THEN FUNCTION = -1:EXIT FUNCTION
m = (a+b)/2
IF Find = @t[m] THEN
PRINT m 'm has the correct value here(8),but the function returns 0
BinarySearch = m
ELSEIF Find < @t[m] THEN
BinarySearch i(),a,m,Find
ELSE
BinarySearch i(),m+1,b,Find
END IF
CATCH
? "Error:" + STR$(ERR)
END TRY
END FUNCTION
'-----------------------------------------------------------------------------------------
FUNCTION BinSearch(t() AS LONG,BYVAL n AS LONG,BYVAL Find AS LONG) AS LONG
LOCAL Retv AS LONG
Retv = BinarySearch(t(),0,n,Find) ' Retv = 0.Should be 8
BinSearch = Retv
END FUNCTION
'-----------------------------------------------------------------------------------------
FUNCTION PBMAIN () AS LONG
LOCAL Find,i AS LONG
DIM ary(0:9) AS LOCAL LONG
FOR i = 0 TO UBOUND(ary())
ary(i) = i
NEXT i
Find = 8
PRINT BinSearch(ary(),UBOUND(ary()),Find) 'Should print 8, but prints 0
WAITKEY$
END FUNCTION
In the code below the "BinarySearch" function returns 0, although it is
assigned a value of 8.
Anyone any idea?
--
#COMPILER PBCC 4.04
#COMPILE EXE
#DIM ALL
#DEBUG ERROR ON
#TOOLS ON
'-----------------------------------------------------------------------------------------
FUNCTION BinarySearch(i() AS LONG,BYVAL a AS LONG,BYVAL b AS LONG,BYVAL Find
AS LONG) AS LONG
TRY
REGISTER m AS LONG
LOCAL t AS LONG PTR
t = VARPTR(i(0))
IF b - a <= 0 THEN FUNCTION = -1:EXIT FUNCTION
m = (a+b)/2
IF Find = @t[m] THEN
PRINT m 'm has the correct value here(8),but the function returns 0
BinarySearch = m
ELSEIF Find < @t[m] THEN
BinarySearch i(),a,m,Find
ELSE
BinarySearch i(),m+1,b,Find
END IF
CATCH
? "Error:" + STR$(ERR)
END TRY
END FUNCTION
'-----------------------------------------------------------------------------------------
FUNCTION BinSearch(t() AS LONG,BYVAL n AS LONG,BYVAL Find AS LONG) AS LONG
LOCAL Retv AS LONG
Retv = BinarySearch(t(),0,n,Find) ' Retv = 0.Should be 8
BinSearch = Retv
END FUNCTION
'-----------------------------------------------------------------------------------------
FUNCTION PBMAIN () AS LONG
LOCAL Find,i AS LONG
DIM ary(0:9) AS LOCAL LONG
FOR i = 0 TO UBOUND(ary())
ary(i) = i
NEXT i
Find = 8
PRINT BinSearch(ary(),UBOUND(ary()),Find) 'Should print 8, but prints 0
WAITKEY$
END FUNCTION