chap05/kadai7.f90

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

 1program answer
 2  implicit none
 3
 4  integer :: i, l, m, n
 5  integer, allocatable :: array1d(:)
 6  integer, allocatable :: array3d(:, :, :)
 7
 8  write(*, *) 'Input three positive integers (L, M, N) :'
 9  read(*, *) l, m, n
10
11  allocate(array3d(l, m, n))
12  allocate(array1d(l * m * n))
13
14  write(*, *) '--- 3D array ---'
15  write(*, *) 'size   (should be equal to L*M*N)               : ', size(array3d)
16  write(*, *) 'shape  (should be equal to 1D array (/L, M, N/) : ', shape(array3d)
17  write(*, *) 'lbound (should be equal to 1D array (/1, 1, 1/) : ', lbound(array3d)
18  write(*, *) 'ubound (should be equal to 1D array (/L, M, N/) : ', ubound(array3d)
19
20  write(*, *) '--- 1D array ---'
21  write(*, *) 'size   (should be equal to L*M*N)               : ', size(array1d)
22  write(*, *) 'shape  (should be equal to 1D array (/L*M*N/)   : ', shape(array1d)
23  write(*, *) 'lbound (should be equal to 1D array (/1/)       : ', lbound(array1d)
24  write(*, *) 'ubound (should be equal to 1D array (/L*M*N/)   : ', ubound(array1d)
25
26  ! reshape and substiute into 1D array
27  do i = 1, l * m * n
28    array1d(i) = i
29  enddo
30  array3d = reshape(array1d,(/l, m, n/))
31
32  deallocate(array1d)
33  deallocate(array3d)
34
35  stop
36endprogram answer