Reputation: 11
How can I calculate the mode of a series using Fortran?
For example:
1,2,2,3,3,3,4,4,5
Mode = 3
Upvotes: 1
Views: 942
Reputation: 9661
You can find already made code out there, if you need it and it is not just an exercize; e.g. Mode at wiki rosettacode.org. If it is an exercize, try first to follow the algorithm given in the other answer.
Upvotes: 1
Reputation: 881093
If your numbers are sorted (as they appear to be), the pseudo-code is simple:
set maxval to -1
set maxcount to -1
set count to -1
set lastval to list[0] - 1
for every val in list:
if val is not equal to lastval:
if count is greater than maxcount:
set maxval to lastval
set maxcount to count
set count to 0
set lastval to val
set count to count plus one
if maxcount is not equal to -1:
print "mode is " maxval " with count of " maxcount
Keep in mind that this will return only the first mode if there is more than one.
Upvotes: 1