write a main program into a subroutine



My main program is divided into two parts:
first part produces some values and followed by the 2nd part.
The second part of the main program can produce the result I want:
count= 3.000000
t= 1
cid= 3 flag= 1 sum= 0.0000000E+00 t= 1
Press any key to continue



However, when I modify it into a subroutine, the result is not what I
want:
count= 3.000000
t= 1
t= 3
t= 2
cid= 4 flag= 1 sum= 2.000000 t= 2
Press any key to continue
When it founds the t=1, it should not go to find the t=3, t=2. It
should report the cid, flag value when t=1.

I wonder if there is anything I need to modfiy when I write a main
program into a subroutine. I double checked it several times and they
are all the same.

This main program has previous values from the first part of the main
program: count, order(j), j, and I call the subroutine right after the
first part of the main program.
Please help me !
This is the second part of the main program.
if (count ==1 .and. vwz(v,w,j) .ne. 0) then
cid=j
flag=1
else if (count ==1 .and. vwz(v,w,j)==0) then
flag=0
cid=0
elseif (count .gt. 1 ) then
c cindx(j)=0: content j is not selected so far
do i=1,count
cindx(i)=0
enddo
cid=0
sum=0
flag=0

do while (sum .lt. real(count+1) .and. flag==0)

call digran(dseed,p,count,t)
if (cindx(t) ==0) then
write(*,*)'t=',t
inner: do j=1,n1
if(t==order(j) .and. vwz(v,w,j) .ne. 0) then
flag=1
cid=j
cindx(order(j))=1
exit


elseif (t==order(j) .and. vwz(v,w,j) ==0) then
cindx(t)=1
sum=sum+1
exit

else
endif
17 enddo inner

else
endif
if (sum== real(count)) exit

enddo


else
endif

This is my subroutine:
subroutine search(v,w,n1,order,count,p,dseed,flag,cid,sum,t )
real sum,p,count,vwz(v,w,n1)
integer j,cid,flag,n1,cindx(0:count),order(0:n1),t

double precision dseed

if (count ==1 .and. vwz(v,w,j) .ne. 0) then
cid=j
flag=1
else if (count ==1 .and. vwz(v,w,j)==0) then
flag=0
cid=0
else if (count .gt. 1 ) then

do i=1,count
cindx(i)=0
enddo
cid=0
sum=0
flag=0

do while (sum .lt. real(count+1) .and. flag==0)

call digran(dseed,p,count,t)
if (cindx(t) ==0) then
write(*,*)'t=',t
inner: do j=1,n1
if(t==order(j) .and. vwz(v,w,j) .ne. 0) then
flag=1
cid=j
cindx(t)=1
exit


elseif (t==order(j) .and. vwz(v,w,j) ==0) then

cindx(t)=1
sum=sum+1
exit

else
endif
77 enddo inner

else
endif
if (sum== real(count)) exit

enddo


else
endif
return
end

.



Relevant Pages

  • automatic variables are not initialized correctly?
    ... Is this below really a Fortran thing or a compiler bug? ... The next call to the same subroutine finds that the variable to be .FALSE., ... IF(flag .EQV. ... print *,' Huh? ...
    (comp.lang.fortran)
  • Re: automatic variables are not initialized correctly?
    ... also sets the storage class to static (although Fortran doesn't use ... Is this below really a Fortran thing or a compiler bug? ... The next call to the same subroutine finds that the variable to be .FALSE., ... IF(flag .EQV. ...
    (comp.lang.fortran)
  • Re: State of WORKING-STORAGE in Statically-Called Subroutines
    ... >There is a chain of three programs, A, B and C. B contains a static call ... It is also true for a dynamically called subroutine, ... >flag; based on this flag A will call C directly (remember, ... No, because when A call's C it passes its data in the Linkage Section, ...
    (comp.lang.cobol)
  • Re: automatic variables are not initialized correctly?
    ... Is this below really a Fortran thing or a compiler bug? ... The next call to the same subroutine finds that the variable to be .FALSE., ... IF(flag .EQV. ... and assigned a value by an assignment statement ...
    (comp.lang.fortran)
  • Re: Simple User RPL Looping Question
    ... Using the DO WHILE loop was ... FREEZE to display 7 lines of data. ... formatted from variable data within each subroutine. ... 'IF flag THEN ELSE END' block, ...
    (comp.sys.hp48)