!goto $wims_read_parm

:def
title=Data median
synonyme=statistic median, median of statistics
input=data1d
!exit

:proc

cnt=!itemcnt $formula
fml=
!for i=1 to $cnt
 t=!item $i of $formula
 fml=!append item $[$t] to $fml
!next i
fml=!sort numeric item $fml
!if $[$cnt%2]=1
 result=!item $[($cnt+1)/2] of $fml
!else
 t1=!item $[$cnt/2] of $fml
 t2=!item $[$cnt/2+1] of $fml
 result=$[($t1+$t2)/2]
!endif

!exit

:output

<p class="wimscenter">
Median =
!htmlmath $result
</p>
Data: $cnt numbers { $formula }
<p>
The <em>median</em> of sorted data 
\(\{x_1, x_2, ..., x_n\}\) 
is equal to
!set ins_align=middle
!insmath \frac{x_{n/2}+x_{n/2+1}}{2}
 when \(n\) is even, or to \(x_{\frac{n+1}{2}}\)
when \(n\) is odd.
</p>
!exit
