Commenting out a WRITE statement in FORTRAN 95 causes changes in non-related outputs

I have a stochastic dynamic program with markov process written in FORTRAN, below. If you comment out the write statement that says "uh, THIS SHOULD MAKE NO DIFFERENCE" and/or some of the other write statements that are just text, no variables being written, you will see a difference in the final array output as compared to when you do not comment any out.

experiment with commenting some of the write statements out/bringing them back in and look at the output array.

can anyone explain this? it happens on more than one computer.

PROGRAM HW4A
IMPLICIT NONE
REAL :: S !current storage
REAL :: LI !previous inflow
REAL :: R !planned current release
REAL :: P !penalty based on planned current release
REAL :: oops !cost of releasing less than planned release
REAL :: f !future costs sum
REAL :: OFP !optimum future costs
REAL :: OCR !optimum current release

INTEGER :: t,st,lin,cin,fst,re !counter indexes for loops and array addresses
!stage, storage, last inflow, future storage, release

!row is current inflow, column is current storage
REAL, DIMENSION(5,0:7) :: Of !best future costs for storage and inflow
REAL, DIMENSION(5,0:7) :: OR !best release for storage and inflow

REAL, DIMENSION(5) :: PIC !probabilities of next inflows

t=20
! WRITE (*,*)"Number of Stages to run?"
! WRITE (*,*)" Based on",t,"year SDP"
DO !for each t
st=0 !reset current storage

DO !for each S
lin=1 !reset previous inflow

DO !for each I
re=0 !reset current release
LI=lin*10.
OFP=9999999 !reset optimum future cost
CALL INFLOWPROB(LI,PIC)

DO !for each R
R=re*10.
CALL PENALTY (R,P)
f=0 !reset future costs accumulator
cin=1 !reset current inflow

DO !for each current inflow
fst=st-re+cin
IF(fst<0)THEN
oops=5000
fst=0
ELSE
oops=0
END IF
IF(fst>7)fst=7
f=f+PIC(cin)*(Of(cin,fst)+oops)
IF(cin==5)EXIT
cin=cin+1
END DO

IF(OFP>P+f)THEN
OFP=P+f
OCR=R
END IF
IF(re==5)EXIT
re=re+1

END DO
Of(lin,st)=OFP
OR(lin,st)=OCR
! WRITE(*,*)Of(3,3)
IF(lin==5)EXIT
lin=lin+1

END DO

IF(st==7)EXIT
st=st+1

END DO
! WRITE (*,*)" ____________STORAGE____________"
! WRITE (*,*)" 0 10 20 30 40 50 60 70"
! WRITE (*,*)" ________________________________"
! WRITE (*,*)"I 10:",NINT(Of(1,:))
! WRITE (*,*)"N 20:",NINT(Of(2,:))
! WRITE (*,*)"F 30:",NINT(Of(3,:))
! WRITE (*,*)"L 40:",NINT(Of(4,:))
! WRITE (*,*)"O 50:",NINT(Of(5,:))
! WRITE(*,100)Of
!100 FORMAT(1X,5(8F10.0,/),/)
IF(t==1)EXIT
t=t-1
END DO

write(*,*)"uh, THIS SHOULD MAKE NO DIFFERENCE!!!"
WRITE (*,*)" Advised Releases based on"
WRITE (*,*)" Last Season Inflow and Current Storage"
WRITE (*,*)" ____________STORAGE____________"
WRITE (*,*)" 0 10 20 30 40 50 60 70"
WRITE (*,*)" ________________________________"
WRITE (*,*)"I 10:",OR(1,:)
WRITE (*,*)"N 20:",OR(2,:)
WRITE (*,*)"F 30:",OR(3,:)
WRITE (*,*)"L 40:",OR(4,:)
WRITE (*,*)"O 50:",OR(5,:)

END PROGRAM HW4A

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SUBROUTINE PENALTY(Q,P)
REAL, INTENT(IN) :: Q
REAL, INTENT(OUT) :: P
REAL :: a = 10
REAL :: b = 2
REAL :: T = 30

! penalty function
IF(Q<T)THEN
P = a*T*(EXP(b*(T-Q)/T)-1)
ELSE
P=0
END IF

END SUBROUTINE PENALTY

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SUBROUTINE INFLOWPROB(LI,PIC)
REAL, INTENT(IN) :: LI !previous inflow
REAL, DIMENSION(5), INTENT(OUT) :: PIC !current inflow probability

IF(LI == 10)THEN
PIC(1) = 0.8
PIC(2) = 0.1
PIC(3) = 0.1
PIC(4) = 0
PIC(5) = 0
ELSE IF(LI == 20)THEN
PIC(1)= 0.3
PIC(2) = 0.3
PIC(3) = 0.3
PIC(4) = 0.1
PIC(5) = 0
ELSE IF(LI == 30)THEN
PIC(1) = 0.1
PIC(2) = 0.3
PIC(3) = 0.3
PIC(4) = 0.2
PIC(5) = 0.1
ELSE IF(LI == 40)THEN
PIC(1) = 0.1
PIC(2) = 0.3
PIC(3) = 0.3
PIC(4) = 0.2
PIC(5) = 0.1
ELSE IF(LI == 50)THEN
PIC(1) = 0.1
PIC(2) = 0.2
PIC(3) = 0.3
PIC(4) = 0.3
PIC(5) = 0.1
END IF

END SUBROUTINE INFLOWPROB

 PhysOrg.com science news on PhysOrg.com >> Hong Kong launches first electric taxis>> Morocco to harness the wind in energy hunt>> Galaxy's Ring of Fire
 Recognitions: Homework Help Hi kingfisher, One line that I noticed was this one: Code: f=f+PIC(cin)*(Of(cin,fst)+oops) I was looking at the array Of(cin,fst) and it does not appear to be initialized to any value before it is used. Were you wanting to use array Of here? I think some compilers automatically set things to zero if they are not explicitly initialized, while others just use whatever (garbage) value happens to be at that memory location. Or am I misreading your program? I don't know how that would give the behavior you are talking about though. Can you give the different outputs that you are getting when the write statement is commented out and active? Also, what compiler are you using?
 YOU ARE GREAT, AND I THANK YOU initializing the array was the solution

Recognitions:
Homework Help

Commenting out a WRITE statement in FORTRAN 95 causes changes in non-related outputs

Glad to help! That was an interesting bug; I actually got identical outputs with my compiler whether the write statement was commented out or not, so I could not reproduce the behavior.

But I'm guessing that your compiler chose a different memory location for the variable array Of(,) based on whether the write line was there or not, which caused different "random" values to be used.