Need help with Binary Search algorithm error
Using the g95 compiler, I have an error:
Error: Operands of comparison operator '.EQ.' at (1) are LOGICAL(4)/LOGICAL(4)
I have no idea what this means. I'm includin开发者_运维技巧g the subroutine. Do you have any ideas how to fix this?
Thanks so much for your time.
SUBROUTINE search(iarray, ItemSought, Found, Location)
CHARACTER(20), DIMENSION(50),INTENT(IN)::itemarray
CHARACTER(20)::ItemSought
LOGICAL, INTENT(OUT)::Found
INTEGER, INTENT(OUT)::Location
INTEGER:: First, Last, Middle
WRITE(*,'(1x,A)',ADVANCE="NO"),"What are you searching for? "
READ*, ItemSought
First=1
Last=SIZE(Iarray)
FOUND = .FALSE.
DO
IF ((First > Last) .OR. Found) RETURN
Middle = (First+Last)/2
IF (ItemSought < Iarray(Middle)) THEN
Last=Middle-1
ELSE IF (ItemSought > Iarray(Middle)) THEN
First=Middle+1
ELSE
Found = .TRUE.
Location = Middle
END IF
END DO
IF (Found == .TRUE.) THEN
PRINT*, Itemsought
END SUBROUTINE
I'm not going to admit the last time I used FORTRAN, but it sure looks a lot different than I remember. So this is just a guess.
Based on the error message I'd say it's on this line (you didn't say which):
IF (Found == .TRUE.) THEN
Again just guessing, you usually don't test a logical value by comparing to true/false, you use it directly:
IF (Found) THEN
The .EQ.
(or ==
)relational operator, just like .NE.
(/=
), .LT.
(<
) and so on, is for comparing numbers only, for comparing logical values you should use .EQV.
and .NEQV.
精彩评论