chap05/sample3.f90

サンプルコードのダウンロード

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
program sample
  implicit none

  integer :: n

  ! 動的配列(実行時にしかサイズが分からない場合)
  integer, allocatable :: x(:)

  ! 以下は動的(allocatable)配列の使い方
  write(*,*) 'Input array size: '

  ! 配列サイズ
  read(*,*) n

  ! allocateされていないことを確認してからallocate
  if( .not. allocated(x) ) then
     allocate(x(n))
  else
     write(*,*) 'Error: already allocated'
  end if

  ! 確かにallocateされたか?
  if( allocated(x) ) then
     write(*,*) 'Successfully allocated'
  end if

  ! 配列の各要素を読み込み
  write(*,*) 'Input array elements: '
  read(*,*) x

  ! 出力
  write(*,*) 'allocatable array : ', x

  ! メモリを解放
  deallocate(x)

  stop
end program sample