lundi 10 octobre 2016

Fortran 95 - Counting repeated elements in array

Here is a small part of my program, the part that matters anyway. I'm new to Fortran programming and this problem is clearly related to that.

program mtd

implicit none

real(kind=8), dimension(100) :: random1
integer(kind=8), dimension(100) :: rand
integer(kind=8) :: c, s, i, h, n
real(kind=8) :: nmax

print *,"How many randoms do you want? "
read *, n

c = 16807
s = 18127                             
nmax = 2147483647

random1(1) = s

do i = 2, n          

    random1(i) = mod(c*random1(i-1), nmax)         

end do

open(70,file='test.txt')
do i=1,n

    rand(i) = int(((random1(i)/nmax)*(10)))
    write(70,*) rand(i)+1

end do
close(70)

do i=1,10

print *, count(mask=rand==i)

end do

end program mtd

Ok, this program starts by using the congruential method to generate random numbers into an array random1(i) - first do loop.

I then grab these numbers, divide them by nmax, to get numbers ranging from [0,1], multiply them by 10 and add 1 to get numbers ranging from [1,10]. I write these into a file and store them in a new array random(i) - second do loop.

I now want to find out how many times 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 showed up, so I can run a Chi Square test.

I went looking arround and found the Count() Function (I think this is what I need). I set it up in a do loop to count how many times each number from i=1,10 shows up. However, its wrong!

If I run this, it says "10" came up 0 times, while If I go to the test.txt file, I can count up to 7 "10's"

If I change the code to include numbers from [0,10]. Despite the array only being dimension 100, it says the number "0" came up 99911 times... What?

All I need is to sort/count these numbers, know how many times each one shows up, can anyone help me please?

Thank you!




Aucun commentaire:

Enregistrer un commentaire