Solving FORTRAN Array Error w/ Code Sample

  • Fortran
  • Thread starter fys iks!
  • Start date
  • Tags
    Fortran
In summary, the conversation is about a person seeking help with their FORTRAN code. They are trying to create an integer array of a certain length, but are getting an error when trying to compile. The solution is to use allocatable arrays instead of declaring variables anywhere in the program. All declarations must be done before any executable statements.
  • #1
fys iks!
40
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
  • #2
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
 
  • #3
Yes...the main point being...you cannot just declare variables anywhere in the program...all declarations need to be done upfront
 
  • #4
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.
 
  • #5



Hi there, it looks like the error is occurring because you are trying to declare an integer array within your main program. In FORTRAN, arrays need to be declared outside of the main program, usually in a separate module or subroutine. Here's an example of how you could declare and initialize your array:

program main

IMPLICIT NONE

integer :: limit
integer :: allnums(limit)

! Declare a subroutine to initialize the array
subroutine initialize_array(array, size)
integer, dimension(size) :: array
integer :: size
integer :: i
do i=1,size
array(i) = 0
end do
end subroutine initialize_array

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

! Call the subroutine to initialize the array
call initialize_array(allnums, limit)

end program main

I hope this helps! Good luck with your learning.
 

1. What is FORTRAN and why is it used?

FORTRAN stands for Formula Translation and it is a programming language commonly used for scientific and engineering applications. It was developed in the 1950s and has been continuously updated to handle complex calculations and large datasets efficiently. It is primarily used for its strong support for mathematical operations and its ability to handle arrays and matrices easily.

2. What is an array error in FORTRAN?

An array error in FORTRAN occurs when there is a mistake in the way an array is declared or used in a program. This can include errors such as using an incorrect array index, not allocating enough memory for the array, or attempting to perform operations on arrays of different sizes. These errors can lead to unexpected results or even cause the program to crash.

3. How can I solve FORTRAN array errors?

The best way to solve FORTRAN array errors is to carefully check the code for any mistakes in the way arrays are declared or used. Make sure that all array indices are within the correct range and that the correct amount of memory is allocated. Debugging tools such as print statements or a debugger can also be helpful in identifying the source of the error.

4. Can you provide a code sample of a FORTRAN array error?

Yes, here is an example of a common array error in FORTRAN:

INTEGER :: array(10)
array(11) = 1


In this code, an array of size 10 is declared, but then an attempt is made to assign a value to the 11th element of the array, which is outside the declared range. This will result in an array error.

5. Are there any best practices to avoid FORTRAN array errors?

Yes, there are a few best practices to avoid FORTRAN array errors:

  • Always double check the array indices and make sure they are within the correct range.
  • Use the DIMENSION statement to explicitly declare the size of the array.
  • Avoid using hard-coded array sizes and instead use variables or parameters to define the array size.
  • Use built-in FORTRAN functions, such as SIZE, to check the size of an array before performing operations on it.

Similar threads

  • Programming and Computer Science
Replies
4
Views
625
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top