Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Aray Help

  1. Jun 9, 2012 #1
    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
     
  2. jcsd
  3. Jun 10, 2012 #2
    When the size of the array is unknown at compile time, use allocatable arrays.
    Code (Text):
    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
     
  4. Jun 10, 2012 #3
    Yes...the main point being...you cannot just declare variables anywhere in the program...all declarations need to be done upfront
     
  5. Jun 11, 2012 #4

    Mark44

    Staff: Mentor

    Meaning, before (above) any executable statement, such are WRITE ..., READ ..., and so on.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Fortran Aray Help
  1. Fortran help (Replies: 0)

  2. Help in fortran (Replies: 2)

  3. [Fortran] help (Replies: 2)

  4. FORTRAN Help (Replies: 1)

  5. Fortran Help (Replies: 2)

Loading...