chap05/kadai4.f90

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

 1program answer
 2  implicit none
 3
 4  real(8), allocatable :: vector1(:), vector2(:)
 5  integer :: i, n
 6  real(8) :: dot1, dot2
 7
 8  read(*, *) n
 9
10  allocate(vector1(n))
11  allocate(vector2(n))
12
13  read(*, *) vector1
14  read(*, *) vector2
15
16  dot1 = 0.0_8
17  do i = 1, n
18    dot1 = dot1 + vector1(i) * vector2(i)
19  enddo
20
21  dot2 = dot_product(vector1, vector2)
22
23  write(*, *) 'Inner product with do loop     : ', dot1
24  write(*, *) 'Inner product with dot_product : ', dot2
25
26  deallocate(vector1)
27  deallocate(vector2)
28
29  stop
30endprogram answer