Creating an Array: Difference between revisions

m
No edit summary
Line 262:
In ANSI FORTRAN 77 or later, this is a default-indexing array declaration:
 
<lang fortran> integer a(10)</lang>
 
This array will have ten elements. Counting starts at 1.
Line 268:
If a zero-based array is needed, declare like this:
 
<lang fortran> integer a(0:9)</lang>
 
This mechanism can be extended to any numerical indices, and any of the up-to-seven-allowed number of dimensions (and of course to other data types than integers). For example:
 
<lang fortran> real a(25:29,12)</lang>
 
will be an two-dimensional, 5x12-array of type REAL, where the first dimension can be addressed numerically as 25, 26, 27, 28 or 29 (and the second dimension as 1 .. 12).
 
In ISO Fortran 95 or later, arrays can also be initialized using a robust set of array constructors and array intrinsic functions.
<lang fortran> program arrays
real :: a(100) = 0 ! entire array initialized to ZERO
real :: b(9) = (/ 1,2,3,4,5,6,7,8,9 /) ! array constructor
Line 307:
print '(4F7.0)', e(i,:) ! print row I
end do
end program arrays</lang>
 
Output:
Line 323:
 
Here is a more interesting example showing a function that creates and returns a square identity matrix of order N:
<lang fortran> module matrixUtil
contains
function identity(n)
Line 339:
! -- a scalar
end function identity
end module matrixUtil</lang>
 
<lang fortran> program muTest
use matrixUtil
integer :: n
Line 352:
end do
deallocate(i)
end program muTest</lang>
 
Example run: