Solving FORTRAN Array Error w/ Code Sample

  • Context: Fortran 
  • Thread starter Thread starter fys iks!
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary

Discussion Overview

The discussion centers around a FORTRAN programming error related to declaring an array with a size that is determined at runtime. Participants explore the correct approach to declare such arrays and the rules regarding variable declarations in FORTRAN.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • The original poster describes an error encountered when trying to declare an integer array with a size based on user input, indicating confusion about the FORTRAN syntax.
  • One participant suggests using allocatable arrays to handle the situation where the size of the array is not known at compile time, providing a code sample that includes the use of the `allocate` statement.
  • Another participant emphasizes that variable declarations in FORTRAN must be done before any executable statements, reiterating the importance of declaration order.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper variable declaration in FORTRAN, but there are different approaches suggested for handling dynamic array sizes, indicating multiple views on the best practice.

Contextual Notes

Limitations include the assumption that the reader is familiar with FORTRAN syntax and the implications of using allocatable arrays versus fixed-size arrays.

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.
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · 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