!par20_ex7_t32
PROGRAM Vote32
! input constants
! N is the number votes
! M is maximum allowed number
INTEGER, PARAMETER :: N = 20, M = 4
INTEGER, PARAMETER :: seed = 42
! normal variables
INTEGER :: winner = 0, wincount = 0
INTEGER :: countnum_p = 0, countnum_n = 0
! votes is the input array
INTEGER, DIMENSION(N) :: votes
INTEGER, DIMENSION(-1:M) :: counts;
! init random number generator
i = RAND(seed)
! init votes
DO i=1,N
votes(i) = RAND() * (M+3) - 2;
END DO
WRITE(*,"(A,100I3)") 'Input is: ', votes
! TODO
! find which was most voted number
DO j=1,N-1
countnum_p = count(votes == votes(j))
countnum_n = count(votes == votes(j+1))
if (countnum_n > countnum_p) then
winner = countnum_n
wincount = votes(j+1)
else
winner = countnum_p
wincount = votes(j)
END if
END DO
WRITE(*,*) 'Most votes was: ', wincount
WRITE(*,*) 'Winners were: ', winner
END PROGRAM Vote32