Discussion:
Function does not return correct value
(too old to reply)
Anonymous
2007-10-22 16:59:24 UTC
Permalink
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
Auric__
2007-10-22 17:41:06 UTC
Permalink
Post by Anonymous
Hello,
In the code below the "BinarySearch" function returns 0, although
it is assigned a value of 8.
Anyone any idea?
[snip]
Post by Anonymous
PRINT m 'm has the correct value here(8),but the function returns 0
BinarySearch = m
BinarySearch i(),a,m,Find
ELSE
BinarySearch i(),m+1,b,Find
END IF
[snip some more]

You're recursing but not catching the return values from the
recursion. Try something like this:

IF Find = @t[m] THEN
FUNCTION = m
ELSEIF Find < @t[m] THEN
FUNCTION = BinarySearch (i(),a,m,Find)
ELSE
FUNCTION = BinarySearch (i(),m+1,b,Find)
END IF
--
auric dot auric at gmail dot com
email sent to the above address is not treated as private
*****
Don't demand justice; you too will be judged.
--
Posted via a free Usenet account from http://www.teranews.com
Anonymous
2007-10-22 18:59:28 UTC
Permalink
Mucho gracias! Problem got solved.
--
Post by Auric__
Post by Anonymous
Hello,
In the code below the "BinarySearch" function returns 0, although
it is assigned a value of 8.
Anyone any idea?
[snip]
Post by Anonymous
PRINT m 'm has the correct value here(8),but the function returns 0
BinarySearch = m
BinarySearch i(),a,m,Find
ELSE
BinarySearch i(),m+1,b,Find
END IF
[snip some more]
You're recursing but not catching the return values from the
FUNCTION = m
FUNCTION = BinarySearch (i(),a,m,Find)
ELSE
FUNCTION = BinarySearch (i(),m+1,b,Find)
END IF
--
auric dot auric at gmail dot com
email sent to the above address is not treated as private
*****
Don't demand justice; you too will be judged.
--
Posted via a free Usenet account from http://www.teranews.com
Peter Manders
2007-10-22 18:43:57 UTC
Permalink
Post by Anonymous
BinarySearch i(),a,m,Find
ELSE
BinarySearch i(),m+1,b,Find
Change to:

BinarySearch = BinarySearch(i(),a,m,Find)
ELSE
BinarySearch = BinarySearch(i(),m+1,b,Find)
--
Peter Manders.

"640 HP ought to be enough for anybody."
Loading...