Anonymous user
Averages/Mode: Difference between revisions
m
→version 1: added/changed comments, indentations, and whitespace, simplified the subroutine and the function.
m (added whitespace before the TOC (table of contents), added section headers.) |
m (→version 1: added/changed comments, indentations, and whitespace, simplified the subroutine and the function.) |
||
Line 2,093:
=={{header|REXX}}==
===
Returns one mode value
<lang rexx>/*REXX program finds the mode (most occurring element) of a vector. */
/*
v= 1 8 6 0 1 9 4 6 1 9 9 9 ; say 'vector='v; say 'mode='mode(v); say
v= 1 2 3 4 5 6 7 8 9 11 10 ; say 'vector='v; say 'mode='mode(v); say
v= 8 8 8 2 2 2 ; say 'vector='v; say 'mode='mode(v); say
v='cat kat Cat emu emu Kat' ; say 'vector='v; say 'mode='mode(v); say
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
do while h>1; h=h%2 /*In REXX, % is
do i=1 for
do while @.k<@.j & h<j; _=@.j; @.j=@.k; @.k=_; j=j-h; k=k-h; end
end /*i*/
end /*while h>1*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
mode: procedure expose @.; parse arg x /*function finds the MODE of a vector
call
?=@.1 /*assume
freq=1 /*the frequency of the occurrence. */
do j=1 for
if @.j==@._ then do /*is this element the same as previous?
freq=freq+1 /*bump the frequency counter. */
?=@.j /*this element is the mode
end
end /*j*/
return ? /*return the
'''output'''
<pre>
|