Averages/Mode: Difference between revisions

m
→‎version 1: added/changed comments, indentations, and whitespace.
(task description: Improve formatting and add related tasks box)
m (→‎version 1: added/changed comments, indentations, and whitespace.)
Line 2,209:
Returns one mode value.
<lang rexx>/*REXX program finds the mode (most occurring element) of a vector. */
/* ════════vector══════════─════════vector═══════════ ═══show vector═══ ═─═══show═════show result═════ */
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. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
eSortsort: procedure expose @.; parse arg # 1 h /* [↓] this is an exchange sort. */
do while h>1; h=h%2 /*In REXX, % is an integer divide.*/
do i=1 for #-h; j=i; k=h+i /* [↓] perform exchange for elements. */
do while @.k<@.j & h<j; _=@.j; @.j=@.k; @.k=_; j=j-h; k=k-h; end
end /*i*/
end /*while h>1*/; return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
mode: procedure expose @.; parse arg x; freq=1 /*function finds the MODE of a vector*/
#=words(x) /*#: the number of elements in vector.*/
do k=1 for #; @.k=word(x,k); end /* ◄──── make an array from the vector.*/
call eSortSort # /*sort the elements in the array. */
?=@.1 /*assume the first element is the mode.*/
freq=1 do j=1 for #; _=j-freq /*thetraipse frequency ofthrough the occurrence. elements in array*/
if do @.j=1=@._ forthen #do; _freq=j-freq +1 /*traipseis this throughelement the elementssame inas arrayprevious?*/
if @.j==@._ then do ?=@.j /*is this element is the samemode as(···so previous?far).*/
freq=freq+1 /*bump the frequency counter. */end
?=@.j end /*this element is the mode (···so far).j*/
return ? end /*return the mode of vector to invoker.*/</lang>
end /*j*/
return ? /*return the mode of vector to invoker.*/</lang>
'''output'''
<pre>