Averages/Mode: Difference between revisions

Content deleted Content added
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: Line 2,093:


=={{header|REXX}}==
=={{header|REXX}}==
===Version 1===
===version 1===
Returns one mode value
Returns one mode value
<lang rexx>/*REXX program finds the mode (most occurring element) of a vector. */
<lang rexx>/*REXX program finds the mode (most occurring element) of a vector. */
/*════════vector══════════ ═══show vector═══ ════show result══════ */
/* ════════vector══════════─ ═══show vector═══ ═─═══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 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= 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= 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
v='cat kat Cat emu emu Kat' ; say 'vector='v; say 'mode='mode(v); say
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────ESORT subroutine────────────────────*/
Esort: procedure expose @.; h=@.0 /* [↓] exchange sort. */
eSort: procedure expose @.; parse arg # 1 h /* [↓] this is an exchange sort. */
do while h>1; h=h%2 /*% is integer divide.*/
do while h>1; h=h%2 /*In REXX, % is an integer divide.*/
do i=1 for @.0-h; j=i; k=h+i /* [↓] perform exchange*/
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
do while @.k<@.j & h<j; _=@.j; @.j=@.k; @.k=_; j=j-h; k=k-h; end
end /*i*/
end /*i*/
end /*while h>1*/
end /*while h>1*/
return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────MODE subroutine─────────────────────*/
mode: procedure expose @.; parse arg x /*finds the MODE of a vector. */
mode: procedure expose @.; parse arg x /*function finds the MODE of a vector*/
@.0=words(x) /* [↓] make an array from vector.*/
#=words(x) /*#: the number of elements in vector.*/
do k=1 for @.0; @.k=word(x,k); end /*k*/
do k=1 for #; @.k=word(x,k); end /* ◄──── make an array from the vector.*/
call Esort @.0 /*sort the elements in the array.*/
call eSort # /*sort the elements in the array. */
?=@.1 /*assume 1st element is the mode.*/
?=@.1 /*assume the first element is the mode.*/
freq=1 /*the frequency of the occurrence*/
freq=1 /*the frequency of the occurrence. */
do j=1 for @.0; _=j-freq /*traipse through the elements. */
do j=1 for #; _=j-freq /*traipse through the elements in array*/
if @.j==@._ then do /*this element same as previous? */
if @.j==@._ then do /*is this element the same as previous?*/
freq=freq+1 /*bump the frequency counter. */
freq=freq+1 /*bump the frequency counter. */
?=@.j /*this element is the mode,so far*/
?=@.j /*this element is the mode (···so far).*/
end
end
end /*j*/
end /*j*/
return ? /*return the node to the invoker.*/</lang>
return ? /*return the mode of vector to invoker.*/</lang>
'''output'''
'''output'''
<pre>
<pre>