Averages/Median: Difference between revisions
→{{header|Bracmat}}: Reimplemented using recently introduced floating point functionality.
(→{{header|Bracmat}}: Reimplemented using recently introduced floating point functionality.) |
|||
Line 1,888:
=={{header|Bracmat}}==
Each number is packaged in a little
▲Each number is packaged in a little list and these lists are accumulated in a sum. Bracmat keeps sums sorted, so the median is the term in the middle of the list, or the average of the two terms in the middle of the list.
<syntaxhighlight lang="bracmat">( ( median
, med med1 med2 num number
. 0:?list▼
. ( convertToRational
& whl▼
& (
& (ufp..export)$(Q.V)
▲ & whl
' (
&
& (!rationalnumber.)+!list:?list
& !list:?+[?end▼
& ( !end*1/2:~/▼
& !list
: ?
+ (?med1.?)
+ (?med2.?)
+ ?
& !med1*1/2+!med2*1/2:?med▼
| !list:?+[(div$(1/2*!end,1))+(?med.)+?▼
)
& (new$(UFP,'(.$med)).go)$
)
& out
$ ( median
$ ( "4.4"
"2.3"
"-1.7"
"7.5"
"6.6"
"0.0"
"1.9"
"8.2"
"9.3"
"4.5"
)
▲ & !list:?+[?end
▲ & ( !end*1/2:~/
▲ & !list:?+[!(=1/2*!end+-1)+(?med1.)+(?med2.)+?
▲ & !med1*1/2+!med2*1/2:?med
▲ | !list:?+[(div$(1/2*!end,1))+(?med.)+?
)
);</syntaxhighlight>
Output:
<pre>4.0999999999999996E+00
▲<pre> median$" 4.1 4 1.2 6.235 7868.33"
4.4500000000000002E+00
3.0000000000000000E+00
3.5000000000000000E+00</pre>
▲ median$"1, 5, 3, 2, 4"
▲ median$"1, 5, 3, 6, 4, 2"
=={{header|C}}==
|