Solving FORTRAN Array Error w/ Code Sample

  • Context: Fortran 
  • Thread starter Thread starter fys iks!
  • Start date Start date
  • Tags Tags
    Fortran
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 3K views
fys iks!
Messages
40
Reaction score
0
Hi, I am beginning to learn FORTRAN and am a bit confused with the error i am getting. Here is my code:

program main

IMPLICIT NONE

integer :: limit

WRITE(*,*) "Please enter limit: "
READ(*,*) limit

integer :: allnums(limit)
!
! do i=1,limit
!
! allnums(i) = 0
!
! end do

end program main


all i am trying to do is create an interger array that is the "limit" long. when trying to compile i get the error:

main.f90:10.25:

integer :: allnums(limit)
1
Error: Unexpected data declaration statement at (1)


any ideas?

thanks
 
on Phys.org
When the size of the array is unknown at compile time, use allocatable arrays.
Code:
program main

IMPLICIT NONE

integer :: limit
integer,allocatable :: allnums(:)

WRITE(*,*) "Please enter limit: "
READ(*,*) limit
allocate(allnums(limit))
!
! do i=1,limit
!
! allnums(i) = 0
!
! end do
deallocate(allnums)
end program main
 
Yes...the main point being...you cannot just declare variables anywhere in the program...all declarations need to be done upfront
 
gsal said:
Yes...the main point being...you cannot just declare variables anywhere in the program...all declarations need to be done upfront
Meaning, before (above) any executable statement, such are WRITE ..., READ ..., and so on.