Reputation: 39
I checked this code many times and could not figure out how to correct it.
program main
implicit none
real, parameter :: dx=1
real, parameter :: dy=1
real :: a1, a2, a3, a4, a5
a1=dy/dx
a2=dx/dy
a3=-2*(dy/dx+dx/dy)
a4=dx/dy
a5=dy/dx
REAL, DIMENSION(5,1):: a
DATA a/a1,a2,a3,a4,a5/
!write (*,*) a(1),a(2)
pause
endprogram
thank you very much and the error:
error #6236:A specification statement cannot appear in the executable section
.
error #6404: This name does not have a type, and must have an explicit type
. [A]
error #6211: A symbol must be a defined parameter in this context
.
Upvotes: 0
Views: 2249
Reputation: 59998
You are mixing specification (data declaration) statements and executable statements. The declaration statements must go first and the executable statements can only follow after them inside each compilation unit or block.
Also, entity used for initialization in the DATA
statement must be a constant expression.
One way to fix your code is:
program main
implicit none
real, parameter :: dx=1
real, parameter :: dy=1
real, parameter :: a1=dy/dx
real, parameter :: a2=dx/dy
real, parameter :: a3=-2*(dy/dx+dx/dy)
real, parameter :: a4=dx/dy
real, parameter :: a5=dy/dx
REAL, DIMENSION(5,1):: a
DATA a/a1,a2,a3,a4,a5/
!write (*,*) a(1),a(2)
end program
Don't use the PAUSE
statement. It is deleted from modern Fortran and it is not clear (portably) what should it actually do even in the older versions.
You can also initialize the array using an array constructor. You don't need constant expressions in that case:
program main
implicit none
real, parameter :: dx=1
real, parameter :: dy=1
REAL, DIMENSION(5,1) :: a
a = reshape([dy/dx, dx/dy, -2*(dy/dx+dx/dy), dx/dy, dy/dx], [5,1])
end program
Upvotes: 2