Fivenum: Difference between revisions

16,453 bytes added ,  1 month ago
m
→‎{{header|Ada}}: Fixed sub section headers
(→‎{{header|ALGOL 68}}: typo in the notes and additional notes)
m (→‎{{header|Ada}}: Fixed sub section headers)
 
(14 intermediate revisions by 10 users not shown)
Line 19:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F fivenum(array)
V n = array.len
V x = sorted(array)
Line 35:
1.04312009, -0.10305385, 0.75775634, 0.32566578]
 
print(fivenum(x))</langsyntaxhighlight>
 
{{out}}
Line 43:
 
=={{header|Ada}}==
==={{header|Direct C Translation}}===
{{trans|C}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Generic_Array_Sort;
 
Line 116:
print (Result, 9);
end Main;
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 125:
[-1.950595940, -0.627594690, 0.119158120, 1.599037385, 1.731315070]
</pre>
==={{header|Using Ada Enumeration}}===
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Generic_Array_Sort;
 
Line 206:
print (Result);
end Main;
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 218:
[ -1.950595940, -0.736050605, 0.119158120, 1.599037385, 1.731315070]
</pre>
 
 
 
=={{header|ALGOL 68}}==
{{Trans|11l}}
Includes additional test cases and adjustment to n4 for odd length array as in a number of other samples.
{{libheader|ALGOL 68-rows}}
<lang algol68>BEGIN # construct an R-style fivenum function #
<syntaxhighlight lang="algol68">BEGIN # construct an R-style fivenum function #
PROC quicksort = ( REF[]REAL a, INT lb, ub )VOID:
PR read "rows.incl.a68" PR
IF ub > lb THEN
 
# more than one element, so must sort #
INT left := lb;
INT right := ub;
# choosing the middle element of the array as the pivot #
REAL pivot := a[ left + ( ( right + 1 ) - left ) OVER 2 ];
WHILE
WHILE IF left <= ub THEN a[ left ] < pivot ELSE FALSE FI DO left +:= 1 OD;
WHILE IF right >= lb THEN a[ right ] > pivot ELSE FALSE FI DO right -:= 1 OD;
left <= right
DO
REAL t := a[ left ];
a[ left ] := a[ right ];
a[ right ] := t;
left +:= 1;
right -:= 1
OD;
quicksort( a, lb, right );
quicksort( a, left, ub )
FI # quicksort # ;
PROC fivenum = ( []REAL array )[]REAL:
BEGIN
INT n = ( UPB array + 1 ) - LWB array;
[ 1 : n ]REAL x := array[ AT 1 ];
quicksort(QUICKSORT x, FROMELEMENT LWB x, TOELEMENT UPB x );
REAL n4 = ( ( ( n + IF ODD n THEN 3 ELSE 2 FI ) / 2 ) / 2 ) ;
[]REAL d = ( 1, n4, ( n + 1 ) / 2, n + 1 - n4, n );
Line 261 ⟶ 241:
sum_array
END # five num # ;
 
PROC show = ( []REAL f )VOID:
SHOW FOR i FROM LWB f TO UPB f DO printfivenum( ( "36, "40, fixed( f[ i7, ]39, -1441, 815 ) ) ) OD;
show( fivenum( ( 36, 40, 7, 39, 41, 15 ) ) );
print( ( newline ) );
show(SHOW fivenum( ( 15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43 ) ) );
print( ( newline ) );
show(SHOW fivenum( ( 0.14082834, 0.09748790, 1.73131507, 0.87636009
, -1.95059594, 0.73438555, -0.03035726, 1.46675970
, -0.74621349, -0.72588772, 0.63905160, 0.61501527
, -0.98983780, -1.00447874, -0.62759469, 0.66206163
, 1.04312009, -0.10305385, 0.75775634, 0.32566578
)
)
END</syntaxhighlight>
)
END</lang>
{{out}}
<pre>
Line 285 ⟶ 263:
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4" -- Mac OS X 10.10. (Yosemite) or later.
use framework "Foundation"
 
Line 319 ⟶ 297:
set z to {0.14082834, 0.0974879, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.4667597, -0.74621349, -0.72588772, ¬
0.6390516, 0.61501527, -0.9898378, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
return {fivenum(x, 1, count x), fivenum(y, 1, count y), fivenum(z, 1, count z)}</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{{6, 25.5, 40, 42.5, 49}, {7, 15, 37.5, 40, 41}, {-1.95059594, -0.676741205, 0.23324706, 0.746070945, 1.73131507}}</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">fivenum: function [lst][
lst: sort lst
m: (size lst)/2
Line 353 ⟶ 331:
print [fivenum l]
print ""
]</langsyntaxhighlight>
 
{{out}}
Line 365 ⟶ 343:
[0.14082834 0.0974879 1.73131507 0.87636009 -1.95059594 0.7343855500000001 -0.03035726 1.4667597 -0.74621349 -0.72588772 0.6390516000000001 0.61501527 -0.9898378 -1.00447874 -0.62759469 0.66206163 1.04312009 -0.10305385 0.75775634 0.32566578] ->
[-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507]</pre>
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
{{trans|Phix}}
<syntaxhighlight lang="vb">#define floor(x) ((x*2.0-0.5) Shr 1)
 
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
 
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
 
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
 
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
 
Sleep</syntaxhighlight>
{{out}}
<pre>[6, 25.5, 40, 43, 49]
[7, 15, 37.5, 40, 41]
[-1.950596,-0.950596, 0.04940403, 1.049404, 0.3256658]</pre>
 
==={{header|VBA}}===
Uses [[Sorting_algorithms/Quicksort#VBA|Quicksort]].
{{trans|Phix}}<syntaxhighlight lang="vb">Option Base 1
Private Function median(tbl As Variant, lo As Integer, hi As Integer)
Dim l As Integer: l = hi - lo + 1
Dim m As Integer: m = lo + WorksheetFunction.Floor_Precise(l / 2)
If l Mod 2 = 1 Then
median = tbl(m)
Else
median = (tbl(m - 1) + tbl(m)) / 2
End if
End Function
Private Function fivenum(tbl As Variant) As Variant
Sort tbl, UBound(tbl)
Dim l As Integer: l = UBound(tbl)
Dim m As Integer: m = WorksheetFunction.Floor_Precise(l / 2) + l Mod 2
Dim r(5) As String
r(1) = CStr(tbl(1))
r(2) = CStr(median(tbl, 1, m))
r(3) = CStr(median(tbl, 1, l))
r(4) = CStr(median(tbl, m + 1, l))
r(5) = CStr(tbl(l))
fivenum = r
End Function
Public Sub main()
Dim x1 As Variant, x2 As Variant, x3 As Variant
x1 = [{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}]
x2 = [{36, 40, 7, 39, 41, 15}]
x3 = [{0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}]
Debug.Print Join(fivenum(x1), " | ")
Debug.Print Join(fivenum(x2), " | ")
Debug.Print Join(fivenum(x3), " | ")
End Sub</syntaxhighlight>{{out}}
<pre>6 | 25,5 | 40 | 43 | 49
7 | 15 | 37,5 | 40 | 41
-1,95059594 | -0,676741205 | 0,23324706 | 0,746070945 | 1,73131507</pre>
 
==={{header|Visual Basic .NET}}===
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices
Imports System.Text
 
Module Module1
 
<Extension()>
Function AsString(Of T)(c As ICollection(Of T), Optional format As String = "{0}") As String
Dim sb As New StringBuilder("[")
Dim it = c.GetEnumerator()
If it.MoveNext() Then
sb.AppendFormat(format, it.Current)
End If
While it.MoveNext()
sb.Append(", ")
sb.AppendFormat(format, it.Current)
End While
Return sb.Append("]").ToString()
End Function
 
Function Median(x As Double(), start As Integer, endInclusive As Integer) As Double
Dim size = endInclusive - start + 1
If size <= 0 Then
Throw New ArgumentException("Array slice cannot be empty")
End If
Dim m = start + size \ 2
Return If(size Mod 2 = 1, x(m), (x(m - 1) + x(m)) / 2.0)
End Function
 
Function Fivenum(x As Double()) As Double()
For Each d In x
If Double.IsNaN(d) Then
Throw New ArgumentException("Unable to deal with arrays containing NaN")
End If
Next
 
Array.Sort(x)
Dim result(4) As Double
 
result(0) = x.First()
result(2) = Median(x, 0, x.Length - 1)
result(4) = x.Last()
 
Dim m = x.Length \ 2
Dim lowerEnd = If(x.Length Mod 2 = 1, m, m - 1)
 
result(1) = Median(x, 0, lowerEnd)
result(3) = Median(x, m, x.Length - 1)
 
Return result
End Function
 
Sub Main()
Dim x1 = {
New Double() {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
New Double() {36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
New Double() {
0.14082834, 0.0974879, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.4667597, -0.74621349, -0.72588772, 0.6390516, 0.61501527,
-0.9898378, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
For Each x In x1
Dim result = Fivenum(x)
Console.WriteLine(result.AsString("{0:F8}"))
Next
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000]
[7.00000000, 15.00000000, 37.50000000, 40.00000000, 41.00000000]
[-1.95059594, -0.67674121, 0.23324706, 0.74607095, 1.73131507]</pre>
 
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 439 ⟶ 598:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 452 ⟶ 611:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 515 ⟶ 674:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000]
Line 523 ⟶ 682:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <ostream>
Line 601 ⟶ 760:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>(6, 25.5, 40, 43, 49)
Line 609 ⟶ 768:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.algorithm;
import std.exception;
import std.math;
Line 658 ⟶ 817:
writeln(fivenum(x));
}
}</langsyntaxhighlight>
{{out}}
<pre>[6, 25.5, 40, 43, 49]
Line 667 ⟶ 826:
{{libheader| System.Generics.Collections}}
{{Trans|Java}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Fivenum;
 
Line 736 ⟶ 895:
 
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 745 ⟶ 904:
[-1,9506,-0,6767,0,2332,0,7461,1,7313]
</pre>
 
=={{header|F#|F sharp}}==
=={{header|EasyLang}}==
{{trans|Ring}}
<syntaxhighlight>
func median t[] low high .
l = high - low + 1
m = low + l div 2
if l mod 2 = 1
return t[m]
.
return (t[m - 1] + t[m]) / 2
.
proc sort . d[] .
for i = 1 to len d[] - 1
for j = i + 1 to len d[]
if d[j] < d[i]
swap d[j] d[i]
.
.
.
.
func[] fivenum t[] .
sort t[]
l = len t[]
m = l div 2 + l mod 2
r1 = t[1]
r2 = median t[] 1 m
r3 = median t[] 1 l
r4 = median t[] (m + 1) l
r5 = t[l]
return [ r1 r2 r3 r4 r5 ]
.
print fivenum [ 0.14082834 0.09748790 1.73131507 0.87636009 -1.95059594 0.73438555 -0.03035726 1.46675970 -0.74621349 -0.72588772 0.63905160 0.61501527 -0.98983780 -1.00447874 -0.62759469 0.66206163 1.04312009 -0.10305385 0.75775634 0.32566578 ]
print fivenum [ 36 40 7 39 41 15 ]
</syntaxhighlight>
{{out}}
<pre>
[ -1.95 -0.68 0.23 0.75 1.73 ]
[ 7 15 37.50 40 41 ]
</pre>
 
=={{header|EMal}}==
{{trans|Java}}
<syntaxhighlight lang="emal">
type Fivenum
int ILLEGAL_ARGUMENT = 0
fun median = real by List x, int start, int endInclusive
int size = endInclusive - start + 1
if size <= 0 do Event.error(ILLEGAL_ARGUMENT, "Array slice cannot be empty").raise() end
int m = start + size / 2
return when(size % 2 == 1, x[m], (x[m - 1] + x[m]) / 2.0)
end
fun fivenum = List by List x
List result = real[].with(5)
x.order()
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = x.length / 2
int lowerEnd = when(x.length % 2 == 1, m, m - 1)
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
end
List lists = List[
real[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
real[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
real[ 0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160,
0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578] ]
for each List list in lists
writeLine(text!fivenum(list))
end
</syntaxhighlight>
{{out}}
<pre>
[6.0,25.5,40.0,42.5,49.0]
[7.0,15.0,37.5,40.0,41.0]
[-1.95059594,-0.676741205,0.23324706,0.746070945,1.73131507]
</pre>
 
=={{header|F Sharp|F#}}==
{{trans|C#}}
<langsyntaxhighlight lang="fsharp">open System
 
// Take from https://stackoverflow.com/a/1175123
Line 788 ⟶ 1,029:
Console.WriteLine("{0}", y);
 
0 // return an integer exit code</langsyntaxhighlight>
{{out}}
<pre>[(6, 25.5, 40, 42.5, 49)]
Line 795 ⟶ 1,036:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators combinators.smart kernel math
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
Line 836 ⟶ 1,077:
[ fivenum . ] tri@ ;
 
MAIN: fivenum-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 846 ⟶ 1,087:
=={{header|Go}}==
{{trans|Perl}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 882 ⟶ 1,123:
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 893 ⟶ 1,134:
 
This solution is aimed at handling larger data sets more efficiently. It replaces the O(n log n) sort with O(n) quickselect. It also does not attempt to reproduce the R result exactly, to average values to get a median of an even number of data values, or otherwise estimate quantiles. The quickselect here leaves the input partitioned around the selected value, which allows another small optimization: The first quickselect call partitions the full input around the median. The second call, to get the first quartile, thus only has to process the partition up to the median. The third call, to get the minimum, only has to process the partition up to the first quartile. The 3rd quartile and maximum are obtained similarly.
<langsyntaxhighlight lang="go">package main
 
import (
Line 957 ⟶ 1,198:
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 966 ⟶ 1,207:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
Line 1,009 ⟶ 1,250:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[6.0, 25.5, 40.0, 42.5, 49.0]
Line 1,017 ⟶ 1,258:
=={{header|Haskell}}==
{{trans|Python}}
<langsyntaxhighlight lang="haskell">import Data.List (sort)
 
fivenum :: [Double] -> [Double]
Line 1,062 ⟶ 1,303:
0.75775634,
0.32566578
]</langsyntaxhighlight>
{{out}}
<pre>[-1.95059594,-0.676741205,0.23324706,0.746070945,1.73131507]</pre>
Line 1,068 ⟶ 1,309:
=={{header|J}}==
'''Solution'''
<langsyntaxhighlight lang="j">midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #) NB. mid points of y
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@]) NB. quartiles of y
fivenum=: <./ , quartiles , >./ NB. fivenum summary of y</langsyntaxhighlight>
'''Example Usage'''
<langsyntaxhighlight lang="j"> test1=: 15 6 42 41 7 36 49 40 39 47 43
test2=: 36 40 7 39 41 15
test3=: , 0 ". ];._2 noun define
Line 1,083 ⟶ 1,324:
6 25.5 40 42.5 49
7 15 37.5 40 41
_1.9506 _0.676741 0.233247 0.746071 1.73132</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.Arrays;
 
public class Fivenum {
Line 1,128 ⟶ 1,369:
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,140 ⟶ 1,381:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">
function median(arr) {
let mid = Math.floor(arr.length / 2);
Line 1,170 ⟶ 1,411:
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578];
console.log( test.fiveNums() );
</syntaxhighlight>
</lang>
{{out}}<pre>
> Array(5) [ 6, 25.5, 40, 42.5, 49 ]
Line 1,176 ⟶ 1,417:
> Array(5) [ -1.95059594, -0.676741205, 0.23324706, 0.746070945, 1.73131507 ]
</pre>
 
=={{header|jq}}==
{{trans|Wren}}
<syntaxhighlight lang=jq>
def fivenum:
def mid($i):
.[($i - 1)|floor] as $floor
| .[($i - 1)|ceil] as $ceil
| if $ceil == $floor then $ceil else ($floor+$ceil)/2 end;
sort
| length as $n
| (($n + 3) / 2 | floor / 2) as $n4
| [mid(1, $n4, (($n + 1)/2), $n + 1 - $n4, $n)] ;
 
def x1: [36, 40, 7, 39, 41, 15];
def x2: [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43];
def x3: [
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578
];
 
[x1, x2, x3][] | fivenum
</syntaxhighlight>
{{Output}}
As for [[#Wren]].
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
Line 1,207 ⟶ 1,475:
0.75775634, 0.32566578])
println("# ", v, "\n -> ", fivenum(v))
end</langsyntaxhighlight>
 
{{out}}
Line 1,221 ⟶ 1,489:
 
As arrays containing NaNs and nulls cannot really be dealt with in a sensible fashion in Kotlin, they've been excluded altogether.
<langsyntaxhighlight lang="scala">// version 1.2.21
 
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
Line 1,256 ⟶ 1,524:
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}</langsyntaxhighlight>
 
{{out}}
Line 1,268 ⟶ 1,536:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function slice(tbl, low, high)
local copy = {}
 
Line 1,319 ⟶ 1,587:
for i,x in ipairs(x1) do
print(fivenum(x))
end</langsyntaxhighlight>
{{out}}
<pre>6 25.5 40 43 49
Line 1,327 ⟶ 1,595:
=={{header|MATLAB}} / {{header|Octave}}==
 
<syntaxhighlight lang="matlab">
<lang Matlab>
function r = fivenum(x)
r = quantile(x,[0:4]/4);
end;
</syntaxhighlight>
</lang>
 
 
Line 1,352 ⟶ 1,620:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]</langsyntaxhighlight>
{{out}}
<pre>{-3.70325, -0.686977, -0.0087185, 0.652979, 3.67416}</pre>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Fivenum;
FROM FormatString IMPORT FormatString;
FROM LongStr IMPORT RealToStr;
Line 1,463 ⟶ 1,731:
 
ReadChar
END Fivenum.</langsyntaxhighlight>
{{out}}
<pre>[6.000000000000000, 25.499999999999900, 40.000000000000000, 42.499999999999900, 49.000000000000000, ]
Line 1,473 ⟶ 1,741:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import algorithm
 
type FiveNum = array[5, float]
Line 1,505 ⟶ 1,773:
echo ""
echo list
echo " → ", list.fivenum</langsyntaxhighlight>
 
{{out}}
Line 1,519 ⟶ 1,787:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">use POSIX qw(ceil floor);
 
sub fivenum {
Line 1,550 ⟶ 1,818:
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578);
@tukey = fivenum(\@x);
say join (',', @tukey);</langsyntaxhighlight>
 
{{out}}
Line 1,558 ⟶ 1,826:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function median(sequence tbl, integer lo, hi)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
integer l = hi-lo+1
<span style="color: #008080;">function</span> <span style="color: #000000;">median</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">tbl</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">lo</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hi</span><span style="color: #0000FF;">)</span>
integer m = lo+floor(l/2)
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hi</span><span style="color: #0000FF;">-</span><span style="color: #000000;">lo</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
if remainder(l,2)=1 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lo</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
return tbl[m]
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
end if
<span style="color: #008080;">return</span> <span style="color: #000000;">tbl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span>
return (tbl[m-1]+tbl[m])/2
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">return</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">])/</span><span style="color: #000000;">2</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function fivenum(sequence tbl)
tbl = sort(tbl)
<span style="color: #008080;">function</span> <span style="color: #000000;">fivenum</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">tbl</span><span style="color: #0000FF;">)</span>
integer l = length(tbl),
<span style="color: #000000;">tbl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">))</span>
m = floor(l/2)+remainder(l,2)
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)+</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
atom r1 = tbl[1],
r2 = median(tbl,1,m),
<span style="color: #004080;">atom</span> <span style="color: #000000;">r1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tbl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span>
r3 = median(tbl,1,l),
<span style="color: #000000;">r2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">median</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">),</span>
r4 = median(tbl,m+1,l),
<span style="color: #000000;">r3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">median</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">),</span>
r5 = tbl[l]
<span style="color: #000000;">r4</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">median</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">r5</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tbl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span>
return {r1, r2, r3, r4, r5}
end function
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">r1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r5</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant x1 = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43},
x2 = {36, 40, 7, 39, 41, 15},
<span style="color: #008080;">constant</span> <span style="color: #000000;">x1</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">42</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">41</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">36</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">49</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">40</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">39</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">47</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">43</span><span style="color: #0000FF;">},</span>
x3 = {0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
<span style="color: #000000;">x2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">36</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">40</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">39</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">41</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">15</span><span style="color: #0000FF;">},</span>
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
<span style="color: #000000;">x3</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0.14082834</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.09748790</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.73131507</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.87636009</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.95059594</span><span style="color: #0000FF;">,</span>
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
<span style="color: #000000;">0.73438555</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.03035726</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.46675970</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.74621349</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.72588772</span><span style="color: #0000FF;">,</span>
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
<span style="color: #000000;">0.63905160</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.61501527</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.98983780</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.00447874</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.62759469</span><span style="color: #0000FF;">,</span>
?fivenum(x1)
<span style="color: #000000;">0.66206163</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.04312009</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.10305385</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.75775634</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.32566578</span><span style="color: #0000FF;">}</span>
?fivenum(x2)
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fivenum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">))</span>
?fivenum(x3)</lang>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fivenum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fivenum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x3</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,598 ⟶ 1,869:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de median (Lst)
(let N (length Lst)
(if (bit? 1 N)
Line 1,625 ⟶ 1,896:
0.73438555 -0.03035726 1.46675970 -0.74621349 -0.72588772
0.63905160 0.61501527 -0.98983780 -1.00447874 -0.62759469
0.66206163 1.04312009 -0.10305385 0.75775634 0.32566578 ) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 1,638 ⟶ 1,909:
 
'''Work with: Python 3'''
<langsyntaxhighlight lang="python">from __future__ import division
import math
import sys
Line 1,665 ⟶ 1,936:
 
y = fivenum(x)
print(y)</langsyntaxhighlight>
 
{{out}}
Line 1,676 ⟶ 1,947:
 
(Though these 25% and 75% values do '''not''' correspond to the Fivenum Tukey quartile values specified in this task)
<langsyntaxhighlight lang="python">import pandas as pd
pd.DataFrame([1, 2, 3, 4, 5, 6]).describe()</langsyntaxhighlight>
 
{{out}}
Line 1,691 ⟶ 1,962:
 
To get the fivenum values asked for, the [https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.quantile.html pandas.DataFrame.quantile] function can be used:
<langsyntaxhighlight lang="python">import pandas as pd
pd.DataFrame([1, 2, 3, 4, 5, 6]).quantile([.0, .25, .50, .75, 1.00], interpolation='nearest')</langsyntaxhighlight>
 
{{out}}
Line 1,706 ⟶ 1,977:
===Python: Functional – without imports===
'''Works with: Python 3'''
<langsyntaxhighlight lang="python"># fiveNums :: [Float] -> (Float, Float, Float, Float, Float)
def fiveNums(xs):
def median(xs):
Line 1,738 ⟶ 2,009:
print(
fiveNums(xs)
)</langsyntaxhighlight>
{{Out}}
<pre>(6, 25.5, 40, 42.5, 49)
Line 1,747 ⟶ 2,018:
The '''fivenum''' function is built-in, see [https://stat.ethz.ch/R-manual/R-devel/library/stats/html/fivenum.html R manual].
 
<langsyntaxhighlight Rlang="r">x <- c(0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578)
 
fivenum(x)</langsyntaxhighlight>
 
'''Output'''
Line 1,759 ⟶ 2,030:
Racket's =quantile= functions use a different method to Tukey; so a new implementation was made.
 
<langsyntaxhighlight lang="racket">#lang racket/base
(require math/private/statistics/quickselect)
 
Line 1,812 ⟶ 2,083:
-0.98983780 -1.00447874 -0.62759469 0.66206163 1.04312009 -0.10305385
0.75775634 0.32566578))
"Test against Go results x3"))</langsyntaxhighlight>
 
This program passes its tests silently.
Line 1,819 ⟶ 2,090:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line>sub fourths ( Int $end ) {
my $end_22 = $end div 2 / 2;
 
Line 1,840 ⟶ 2,111:
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
];
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,849 ⟶ 2,120:
=={{header|Relation}}==
Min, median and max are built in, quarter1 and quarter3 calculated.
<syntaxhighlight lang="relation">
<lang Relation>
program fivenum(X)
rename X^ x
Line 1,891 ⟶ 2,162:
insert 8
run fivenum("a")
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,903 ⟶ 2,174:
=={{header|REXX}}==
Programming note: &nbsp; this REXX program uses a unity─based array.
<langsyntaxhighlight lang="rexx">/*REXX program computes the five─number summary (LO─value, p25, medium, p75, HI─value).*/
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43 /*Not specified? Then use the defaults*/
Line 1,931 ⟶ 2,202:
if #//2 then p25= q2 /*calculate the second quartile number.*/
else p25= q2 - 1 /* " " " " " */
return LO med(1, p25) med(1, #) med(q2, #) HI /*return list of 5 numbers.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 15 6 42 41 7 36 49 40 39 47 43 </tt>}}
<pre>
Line 1,944 ⟶ 2,215:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
rem1 = 0
rem2 = 0
Line 1,987 ⟶ 2,258:
next
? "[" + left(svect, len(svect) - 1) + "]"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,993 ⟶ 2,264:
[7,15,37.5,40,41]
[-1.95059594,-0.67674121,0.23324706,0.74607095,1.73131507]
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ '''IF''' DUP SIZE 2 ≥ '''THEN'''
LIST→ → len
≪ len 1 '''FOR''' n
1 n 1 - '''START'''
'''IF''' DUP2 > '''THEN''' SWAP '''END'''
n ROLLD
'''NEXT''' n ROLLD
-1 '''STEP''' len →LIST
≫ '''END'''
≫ ‘'''SORTL'''’ STO
≪ → data n
≪ data SIZE n * 4 n - + 4 /
data OVER FLOOR GET n *
data ROT CEIL GET 4 n - * + 4 /
≫ ≫ ‘'''QTL'''’ STO
≪ '''SORTL''' { }
OVER 1 GET + OVER 1 '''QTL''' + OVER 2 '''QTL''' + OVER 3 '''QTL''' + OVER DUP SIZE GET +
≫ ‘'''SUMR5'''’ STO
{ 6 7 15 36 39 40 41 42 43 47 49 } '''SUMR5'''
{{out}}
<pre>
1: { 6 30.75 40 42.25 49 }
</pre>
 
=={{header|Ruby}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">def fivenum(array)
sorted_arr = array.sort
n = array.size
Line 2,023 ⟶ 2,323:
tukey_array = fivenum(test_array)
p tukey_array
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,031 ⟶ 2,331:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
#[derive(Debug)]
struct FiveNum {
Line 2,104 ⟶ 2,404:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,128 ⟶ 2,428:
 
=={{header|SAS}}==
<langsyntaxhighlight lang="sas">/* build a dataset */
data test;
do i=1 to 10000;
Line 2,140 ⟶ 2,440:
proc means data=test min p25 median p75 max;
var x;
run;</langsyntaxhighlight>
 
'''Output'''
Line 2,166 ⟶ 2,466:
=={{header|Scala}}==
===Array based solution===
<langsyntaxhighlight Scalalang="scala">import java.util
 
object Fivenum extends App {
Line 2,202 ⟶ 2,502:
}
 
}</langsyntaxhighlight>
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/8s0OdOO/2 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/Ady3dSnoRRKNhCaZYIVbig Scastie (JVM)].
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func fourths(e) {
var t = ((e>>1) / 2)
[0, t, e/2, e - t, e]
Line 2,228 ⟶ 2,528:
]]
 
nums.each { say fivenum(_).join(', ') }</langsyntaxhighlight>
{{out}}
<pre>6, 25.5, 40, 42.5, 49
Line 2,237 ⟶ 2,537:
First build a dataset:
 
<langsyntaxhighlight lang="stata">clear
set seed 17760704
qui set obs 10000
gen x=rnormal()</langsyntaxhighlight>
 
The '''[https://www.stata.com/help.cgi?summarize summarize]''' command produces all the required statistics, and more:
 
<langsyntaxhighlight lang="stata">qui sum x, detail
di r(min),r(p25),r(p50),r(p75),r(max)</langsyntaxhighlight>
 
'''Output'''
Line 2,253 ⟶ 2,553:
It's also possible to use the '''[https://www.stata.com/help.cgi?tabstat tabstat]''' command
 
<langsyntaxhighlight lang="stata">tabstat x, s(mi q ma)</langsyntaxhighlight>
 
'''Output'''
Line 2,264 ⟶ 2,564:
Another example:
 
<langsyntaxhighlight lang="stata">clear
mat a=0.14082834\0.09748790\1.73131507\0.87636009\-1.95059594\ ///
0.73438555\-0.03035726\1.46675970\-0.74621349\-0.72588772\ ///
Line 2,270 ⟶ 2,570:
0.66206163\1.04312009\-0.10305385\0.75775634\0.32566578
svmat a
tabstat a1, s(mi q ma)</langsyntaxhighlight>
 
'''Output'''
Line 2,278 ⟶ 2,578:
a1 | -1.950596 -.6767412 .2332471 .746071 1.731315
----------------------------------------------------------------</pre>
 
=={{header|VBA}}==
Uses [[Sorting_algorithms/Quicksort#VBA|Quicksort]].
{{trans|Phix}}<lang vb>Option Base 1
Private Function median(tbl As Variant, lo As Integer, hi As Integer)
Dim l As Integer: l = hi - lo + 1
Dim m As Integer: m = lo + WorksheetFunction.Floor_Precise(l / 2)
If l Mod 2 = 1 Then
median = tbl(m)
Else
median = (tbl(m - 1) + tbl(m)) / 2
End if
End Function
Private Function fivenum(tbl As Variant) As Variant
Sort tbl, UBound(tbl)
Dim l As Integer: l = UBound(tbl)
Dim m As Integer: m = WorksheetFunction.Floor_Precise(l / 2) + l Mod 2
Dim r(5) As String
r(1) = CStr(tbl(1))
r(2) = CStr(median(tbl, 1, m))
r(3) = CStr(median(tbl, 1, l))
r(4) = CStr(median(tbl, m + 1, l))
r(5) = CStr(tbl(l))
fivenum = r
End Function
Public Sub main()
Dim x1 As Variant, x2 As Variant, x3 As Variant
x1 = [{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}]
x2 = [{36, 40, 7, 39, 41, 15}]
x3 = [{0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}]
Debug.Print Join(fivenum(x1), " | ")
Debug.Print Join(fivenum(x2), " | ")
Debug.Print Join(fivenum(x3), " | ")
End Sub</lang>{{out}}
<pre>6 | 25,5 | 40 | 43 | 49
7 | 15 | 37,5 | 40 | 41
-1,95059594 | -0,676741205 | 0,23324706 | 0,746070945 | 1,73131507</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<lang vbnet>Imports System.Runtime.CompilerServices
Imports System.Text
 
Module Module1
 
<Extension()>
Function AsString(Of T)(c As ICollection(Of T), Optional format As String = "{0}") As String
Dim sb As New StringBuilder("[")
Dim it = c.GetEnumerator()
If it.MoveNext() Then
sb.AppendFormat(format, it.Current)
End If
While it.MoveNext()
sb.Append(", ")
sb.AppendFormat(format, it.Current)
End While
Return sb.Append("]").ToString()
End Function
 
Function Median(x As Double(), start As Integer, endInclusive As Integer) As Double
Dim size = endInclusive - start + 1
If size <= 0 Then
Throw New ArgumentException("Array slice cannot be empty")
End If
Dim m = start + size \ 2
Return If(size Mod 2 = 1, x(m), (x(m - 1) + x(m)) / 2.0)
End Function
 
Function Fivenum(x As Double()) As Double()
For Each d In x
If Double.IsNaN(d) Then
Throw New ArgumentException("Unable to deal with arrays containing NaN")
End If
Next
 
Array.Sort(x)
Dim result(4) As Double
 
result(0) = x.First()
result(2) = Median(x, 0, x.Length - 1)
result(4) = x.Last()
 
Dim m = x.Length \ 2
Dim lowerEnd = If(x.Length Mod 2 = 1, m, m - 1)
 
result(1) = Median(x, 0, lowerEnd)
result(3) = Median(x, m, x.Length - 1)
 
Return result
End Function
 
Sub Main()
Dim x1 = {
New Double() {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
New Double() {36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
New Double() {
0.14082834, 0.0974879, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.4667597, -0.74621349, -0.72588772, 0.6390516, 0.61501527,
-0.9898378, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
For Each x In x1
Dim result = Fivenum(x)
Console.WriteLine(result.AsString("{0:F8}"))
Next
End Sub
 
End Module</lang>
{{out}}
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000]
[7.00000000, 15.00000000, 37.50000000, 40.00000000, 41.00000000]
[-1.95059594, -0.67674121, 0.23324706, 0.74607095, 1.73131507]</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-sort}}
<langsyntaxhighlight ecmascriptlang="wren">import "./sort" for Sort
 
var fivenum = Fn.new { |a|
Line 2,421 ⟶ 2,608:
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578
]
for (x in [x1, x2, x3]) System.print(fivenum.call(x))</langsyntaxhighlight>
 
{{out}}
Line 2,432 ⟶ 2,619:
=={{header|zkl}}==
Uses GNU GSL library.
<langsyntaxhighlight lang="zkl">var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
fcn fiveNum(v){ // V is a GSL Vector, --> min, 1st qu, median, 3rd qu, max
v.sort();
return(v.min(),v.quantile(0.25),v.median(),v.quantile(0.75),v.max())
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fiveNum(GSL.VectorFromData(
15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0)).println();
println(fiveNum(GSL.VectorFromData(36.0, 40.0, 7.0, 39.0, 41.0, 15.0)));
Line 2,446 ⟶ 2,633:
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578);
println(fiveNum(v));</langsyntaxhighlight>
{{out}}
<pre>
3,022

edits