Fortran Solving FORTRAN Array Error w/ Code Sample

  • Thread starter Thread starter fys iks!
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
The discussion centers around a FORTRAN programming issue related to declaring an integer array with a size determined at runtime. The original code attempts to declare an array using a variable 'limit', which leads to a compilation error due to FORTRAN's requirement for array sizes to be known at compile time. The solution provided involves using allocatable arrays, allowing the array size to be defined dynamically. Additionally, it is emphasized that all variable declarations must occur before any executable statements in the program, ensuring proper structure and compilation.
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
 
Technology news 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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 17 ·
Replies
17
Views
7K
  • · Replies 4 ·
Replies
4
Views
2K