I ran into some piece of Fortran code rather difficult to understand.
1. What is the name of structure of code / (i1,i1=0,nn-1) /
?
How can I print it directly in the code to see its content?
2. I'm looking for ways to change value of nn
without re-compiling, how should I do this? nn
supposed to be the length of array omega
.
3. How should I setup the length of omega
in case of changeable nn
?
I mean when I'll have no parameter (nn=20)
anymore.
program test_20140919
! test
implicit none
integer nn
parameter (nn=20)
real omega(nn)
call test_real(nn, 2.0, 4.0, omega)
print *, omega
end program test_20140919
!c ===
subroutine test_real(nn, o1, o2, omega)
integer nn
real o1, o2
real omega(nn)
print *, nn
omega = o1 + (o2*o1)*(/ (i1,i1=0,nn-1) /)/real(nn-1)
print *, real(nn)
return
end
I've compiled this with line gfortran test.f -ffree-form -o test
in terminal.
UPD Revised version of the code due to answers from Vladimir F:
module subs
implicit none
contains
subroutine test_real(nn, o1, o2, omega)
integer nn
real o1, o2
real :: omega(:)
if (.not. allocated(omega)) allocate(omega(nn))
omega = o1 + (o2*o1)*(/ (i1,i1=0,nn-1) /)/real(nn-1)
print *, real(nn)
end subrotine
end module
program test_20140920
! test
use subs
implicit none
integer nn
real, allocatable :: omega(:)
read(*,*) nn
allocate(omega(nn))
call test_real(nn, 2.0, 4.0, omega)
print *, omega
end program test_20140920