Fortran - Dynamic Arrays
Fortran - Dynamic Arrays
For example,
The rank of the array, i.e., the dimensions has to be mentioned however, to allocate
memory to such an array, you use the allocate function.
allocate ( darray(s1,s2) )
After the array is used, in the program, the memory created should be freed using the
deallocate function
deallocate (darray)
Example
program dynamic_array
implicit none
! allocate memory
allocate ( darray(s1,s2) )
do i = 1, s1
do j = 1, s2
darray(i,j) = i*j
Page 2 of 5
deallocate (darray)
end program dynamic_array
When the above code is compiled and executed, it produces the following result −
Example
program dataStatement
Live Demo
implicit none
When the above code is compiled and executed, it produces the following result −
The A array:
7
8
9
10
11
The B array:
1 1 1
2 2 2
3 3 3
The C array:
4
2
5
2
6
2
Page 4 of 5
7
2
8
2
Example
program whereStatement
Live Demo
implicit none
integer :: a(3,5), i , j
do i = 1,3
do j = 1, 5
a(i,j) = j-i
end do
end do
do i = lbound(a,1), ubound(a,1)
write(*,*) (a(i,j), j = lbound(a,2), ubound(a,2))
end do
where( a<0 )
a = 1
elsewhere
a = 5
end where
When the above code is compiled and executed, it produces the following result −
The A array:
0 1 2 3 4
-1 0 1 2 3
-2 -1 0 1 2
The A array:
5 5 5 5 5
1 5 5 5 5
1 1 5 5 5