1program answer
2 implicit none
3
4 integer, parameter :: nh = 10
5 integer :: i, n, hist(nh)
6 real(8) :: binc(nh)
7 integer, allocatable :: score(:)
8
9 read(*, *) n
10
11 allocate(score(n))
12
13 read(*, *) score
14
15 call histogram(score, 100 / nh, binc, hist)
16
17 do i = 1, nh
18 write(*, *) binc(i), hist(i)
19 enddo
20
21 deallocate(score)
22
23 stop
24contains
25 !
26 ! ヒストグラムを作成するサブルーチン
27 !
28 subroutine histogram(score, binw, binc, hist)
29 implicit none
30 integer, intent(in) :: score(:) ! 点数(人数分)
31 integer, intent(in) :: binw ! ビンの幅(例えば10点)
32 real(8), intent(out) :: binc(:) ! ビンの中央値(例えば5, 15, ..., 95)
33 integer, intent(out) :: hist(:) ! 各ビン内の人数
34
35 integer :: i, j, n, m
36
37 n = size(score)
38 m = size(binc)
39 hist = 0
40
41 do i = 1, n
42 j = score(i) / binw + 1
43
44 ! 添字範囲をチェック
45 if(j < 1 .or. j > m) then
46 write(*, *) 'Invalid input'
47 stop
48 endif
49
50 hist(j) = hist(j) + 1
51 enddo
52
53 do i = 1, size(hist)
54 binc(i) = (i - 1 + 0.5_8) * binw
55 enddo
56
57 endsubroutine histogram
58endprogram answer