Stem-and-leaf plot: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(22 intermediate revisions by 7 users not shown)
Line 16:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="11l">F leaf_plot(&x)
x.sort()
V i = x[0] I/ 10 - 1
Line 39:
]
 
leaf_plot(&data)</langsyntaxhighlight>
 
{{out}}
Line 61:
 
=={{header|ACL2}}==
<langsyntaxhighlight Lisplang="lisp">(defun insert (x xs)
(cond ((endp xs) (list x))
((> x (first xs))
Line 101:
(reverse (stem-and-leaf-bins (reverse (isort xs))
0
nil))))</langsyntaxhighlight>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit
 
PROC Main()
DEFINE len="121"
BYTE ARRAY a(len)=[
12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31
125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27
44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114
96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 124 42
128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124
115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116
146]
BYTE i,j,min,max,stem,leaf
 
Put(125) PutE() ;clear screen
SortB(a,len,0)
min=a(0)/10
max=a(len-1)/10
FOR i=min TO max
DO
IF i<10 THEN Put(' ) FI
PrintB(i) Print("* | ")
FOR j=0 TO len-1
DO
stem=a(j)/10
IF stem=i THEN
leaf=a(j) MOD 10
PrintB(leaf)
FI
OD
PutE()
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Stem-and-leaf_plot.png Screenshot from Atari 8-bit computer]
<pre>
0* | 77
1* | 2388
2* | 357777778899
3* | 011112345677789
4* | 001222233344456788
5* | 23788
6* | 138
7* | 1
8* |
9* | 69
10* | 4555567999
11* | 13333444555666677778899
12* | 00112234445556777788
13* | 1239
14* | 16
</pre>
 
=={{header|Ada}}==
[[GNAT]] used for sorting, could use any other sorting method.
Does not handle negative stems properly.
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 147 ⟶ 202:
end loop;
end stemleaf;
</syntaxhighlight>
</lang>
Output:
<pre>
Line 166 ⟶ 221:
14 | 1 6
</pre>
 
=={{header|ALGOL 68}}==
===With sorting===
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
{{libheader|ALGOL 68-rows}}
Note the source of rows.incl.a68 is available on a page on Rosetta Code - see the link above.
<br><br>
Note that the wikipedia article linked to by the Task states that the stem and leaf are <i>typically</i> datum / 10 and datum MOD 10.
<br>
This sample can also also handle atypical cases.
<syntaxhighlight lang="algol68">
BEGIN # produce a stem and leaf plot of some numbers, leaf = last digit, #
# stem = leading digits #
PR read "rows.incl.a68" PR # include row (array) utilities #
[]INT data = ( 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44
, 37, 113, 124, 37, 48, 127, 36, 29, 31, 125, 139
, 131, 115, 105, 132, 104, 123, 35, 113, 122, 42, 117
, 119, 58, 109, 23, 105, 63, 27, 44, 105, 99, 41
, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121
, 58, 114, 126, 53, 114, 96, 25, 109, 7, 31, 141
, 46, 13, 27, 43, 117, 116, 27, 7, 68, 40, 31
, 115, 124, 42, 128, 52, 71, 118, 117, 38, 27, 106
, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122, 109
, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125
, 107, 114, 34, 133, 45, 120, 30, 127, 31, 116, 146
);
# generates a stem-and-leaf plot of d, the stems and leaves are derived #
# from the elements by stem and leaf. the plot starts at first stem. #
# the data is assumed to be sorted into stem then leaf order #
PROC stem and leaf plot = ( []INT d, INT first stem, PROC(INT)INT stem, leaf )VOID:
IF UPB d < LWB d THEN
print( ( "No data", newline ) )
ELSE
# there is some data to plot #
INT curr stem := stem( d[ LWB d ] );
IF first stem < curr stem THEN
curr stem := first stem
FI;
curr stem -:= 1;
BOOL first := TRUE;
FOR i FROM LWB d TO UPB d DO
INT this stem = stem( d[ i ] );
IF first OR curr stem /= this stem THEN
curr stem +:= 1;
WHILE IF NOT first THEN
print( ( newline ) )
ELSE
first := FALSE
FI;
print( ( whole( curr stem, -4 ), "|" ) );
curr stem < this stem
DO
curr stem +:= 1
OD
FI;
print( ( " ", whole( leaf( d[ i ] ), 0 ) ) )
OD
FI # stem and leaf plot # ;
 
# sort the data #
[ LWB data : UPB data ]INT sorted data := data;
QUICKSORT sorted data FROMELEMENT LWB sorted data TOELEMENT UPB sorted data;
# plot the data: stem = element / 10, leaf = element MOD 10 #
stem and leaf plot( sorted data, 0, ( INT n )INT: n OVER 10, ( INT n )INT: n MOD 10 )
 
END
</syntaxhighlight>
{{out}}
<pre>
0| 7 7
1| 2 3 8 8
2| 3 5 7 7 7 7 7 7 8 8 9 9
3| 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4| 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5| 2 3 7 8 8
6| 1 3 8
7| 1
8|
9| 6 9
10| 4 5 5 5 5 6 7 9 9 9
11| 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12| 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13| 1 2 3 9
14| 1 6
</pre>
 
=== Without external dependencies ===
 
This solution doesn't use any library code and doesn't sort the data.
 
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<syntaxhighlight lang="algol68">
PROC stem and leaf plot = ([]INT data)VOID:
BEGIN
# get lowest and highest stem values #
INT min stem := data[LWB data] % 10,
max stem := data[LWB data] % 10;
FOR i FROM LWB data + 1 TO UPB data DO
INT stem := data[i] % 10;
IF min stem > stem THEN min stem := stem FI;
IF max stem < stem THEN max stem := stem FI
OD;
# this array will store the amount of leaves per stem: #
[min stem : max stem, 0:9]INT stems;
FOR i FROM LWB stems TO UPB stems DO
stems[i,] := []INT((0,0,0,0,0,0,0,0,0,0))[@0]
OD;
# fill the array #
FOR i FROM LWB data TO UPB data DO
stems[data[i] % 10, data[i] %* 10] +:= 1
OD;
# print the histogram #
FOR i FROM LWB stems TO UPB stems DO
print((whole(i, -4), "| "));
FOR j FROM 0 TO 9 DO
print(REPR (j + ABS "0") * stems[i,j])
OD;
print(newline)
OD
END;
 
[]INT data = (12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44,
37, 113, 124, 37, 48, 127, 36, 29, 31, 125,
139, 131, 115, 105, 132, 104, 123, 35, 113,
122, 42, 117, 119, 58, 109, 23, 105, 63, 27,
44, 105, 99, 41, 128, 121, 116, 125, 32, 61,
37, 127, 29, 113, 121, 58, 114, 126, 53, 114,
96, 25, 109, 7, 31, 141, 46, 13, 27, 43, 117,
116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52,
71, 118, 117, 38, 27, 106, 33, 117, 116, 111,
40, 119, 47, 105, 57, 122, 109, 124, 115, 43,
120, 43, 27, 27, 18, 28, 48, 125, 107, 114, 34,
133, 45, 120, 30, 127, 31, 116, 146);
 
stem and leaf plot(data)
</syntaxhighlight>
{{out}}
<pre>
0| 77
1| 2388
2| 357777778899
3| 011112345677789
4| 001222233344456788
5| 23788
6| 138
7| 1
8|
9| 69
10| 4555567999
11| 13333444555666677778899
12| 00112234445556777788
13| 1239
14| 16
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">data: [
12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36
29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119
58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37
127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 13 27
43 117 116 27 7 68 40 31 115 124 42 128 146 52 71 118 117 38
27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43
120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116
]
 
tens: gather sort data => [& / 10]
 
loop 0..do last keys tens 'n [
ns: ~"|n|"
prints [pad ns 2 "|"]
if key? tens ns -> prints map tens\[ns] => [& % 10]
print ""
]</syntaxhighlight>
 
{{out}}
 
<pre> 0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">SetWorkingDir %A_ScriptDir%
#NoEnv
Data := "12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146"
Line 207 ⟶ 455:
Return ToReturn
}
</syntaxhighlight>
</lang>
Output:
<pre>
Line 227 ⟶ 475:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f STEM-AND-LEAF_PLOT.AWK
#
Line 275 ⟶ 523:
function max(x,y) { return((x > y) ? x : y) }
function min(x,y) { return((x < y) ? x : y) }
</syntaxhighlight>
</lang>
<p>output:</p>
<pre>
Line 295 ⟶ 543:
</pre>
 
=={{header|BBC BASIC}}==
==={{header|ANSI BASIC}}===
{{trans|QuickBASIC}}
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
100 PROGRAM StemAndLeafPlot
110 OPTION BASE 0
120 DIM Dat(120)
130 FOR I = 0 TO 120
140 READ Dat(I)
150 NEXT I
160 DATA 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124
170 DATA 37, 48, 127, 36, 29, 31, 125, 139, 131, 115, 105, 132, 104, 123
180 DATA 35, 113, 122, 42, 117, 119, 58, 109, 23, 105, 63, 27, 44, 105
190 DATA 99, 41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121, 58
200 DATA 114, 126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27, 43
210 DATA 117, 116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118
220 DATA 117, 38, 27, 106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122
230 DATA 109, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114
240 DATA 34, 133, 45, 120, 30, 127, 31, 116, 146
250 CALL LeafPlot(Dat)
260 END
270 REM ************************
1000 EXTERNAL SUB LeafPlot(A())
1010 CALL ShellSort(A)
1020 LET I = INT(A(0) / 10) - 1
1030 FOR J = 0 TO UBOUND(A)
1040 LET D = INT(A(J) / 10)
1050 DO WHILE D > I
1060 LET I = I + 1
1070 IF J <> 0 THEN PRINT
1080 PRINT USING "## |": I;
1090 LOOP
1100 PRINT USING "##": MOD(A(J), 10);
1110 NEXT J
1120 PRINT
1130 END SUB
1140 REM ************************
2000 EXTERNAL SUB ShellSort(A())
2010 LET N = UBOUND(A)
2020 LET Incr = INT(N / 2)
2030 DO WHILE Incr > 0
2040 FOR I = Incr TO N - 1
2050 LET J = I - Incr
2060 DO WHILE J >= 0
2070 IF A(J) > A(J + Incr) THEN
2080 REM SWAP A(J), A(J + Incr): J = J - Incr
2090 LET Tmp = A(J)
2100 LET A(J) = A(J + Incr)
2110 LET A(J + Incr) = Tmp
2120 LET J = J - Incr
2130 ELSE
2140 LET J = -1
2150 END IF
2160 LOOP
2170 NEXT I
2180 LET Incr = INT(Incr / 2)
2190 LOOP
2200 END SUB
</syntaxhighlight>
{{out}}
<pre>
0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6
</pre>
 
==={{header|ASIC}}===
{{trans|QuickBASIC|A global array used instead of subroutine parameters.}}
<syntaxhighlight lang="basic">
REM Stem-and-leaf plot
DIM A(120)
DATA 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124
DATA 37, 48, 127, 36, 29, 31, 125, 139, 131, 115, 105, 132, 104, 123
DATA 35, 113, 122, 42, 117, 119, 58, 109, 23, 105, 63, 27, 44, 105
DATA 99, 41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121, 58
DATA 114, 126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27, 43
DATA 117, 116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118
DATA 117, 38, 27, 106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122
DATA 109, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114
DATA 34, 133, 45, 120, 30, 127, 31, 116, 146
FOR I = 0 TO 120
READ A(I)
NEXT I
N = 121
NMin1 = N - 1
GOSUB LeafPlot:
END
 
LeafPlot:
GOSUB ShellSortInt:
I = A(0) / 10
I = I - 1
FOR J = 0 TO NMin1
D = A(J) / 10
WHILE D > I
I = I + 1
IF J <> 0 THEN
PRINT
ENDIF
SI$ = STR$(I)
SI$ = RIGHT$(SI$, 2)
PRINT SI$;
PRINT " |";
WEND
AJMod10 = A(J) MOD 10
SI$ = STR$(AJMod10)
SI$ = RIGHT$(SI$, 2)
PRINT SI$;
NEXT J
PRINT
RETURN
 
ShellSortInt:
Incr = N / 2
WHILE Incr > 0
FOR I = Incr TO NMin1
J = I - Incr
JPlIncr = J + Incr
WHILE J >= 0
IF A(J) > A(JPlIncr) THEN
Tmp = A(J)
A(J) = A(JPlIncr)
A(JPlIncr) = Tmp
JPlIncr = J
J = J - Incr
ELSE
J = -1
ENDIF
WEND
NEXT I
Incr = Incr / 2
WEND
RETURN
</syntaxhighlight>
{{out}}
<pre>
0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6
</pre>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SORTLIB"
Sort% = FN_sortinit(0, 0)
Line 333 ⟶ 747:
NEXT
PRINT
ENDPROC</langsyntaxhighlight>
Output:
<pre>
Line 351 ⟶ 765:
13 | 1 2 3 9
14 | 1 6
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' version 22-06-2015
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
' from the rosetta code FreeBASIC entry
#Define out_of_data 99999999 ' any number that is not in the set will do
 
Sub shellsort(s() As Integer)
' from the FreeBASIC entry at rosetta code
' sort from lower bound to the highter bound
Dim As Integer lb = LBound(s)
Dim As Integer ub = UBound(s)
Dim As Integer done, i, inc = ub - lb
 
Do
inc = inc / 2.2
If inc < 1 Then inc = 1
Do
done = 0
For i = lb To ub - inc
If s(i) > s(i + inc) Then
Swap s(i), s(i + inc)
done = 1
End If
Next
Loop Until done = 0
Loop Until inc = 1
 
End Sub
 
' ------=< TASK DATA >=------
 
Data 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124
Data 37, 48, 127, 36, 29, 31, 125, 139, 131, 115, 105, 132, 104, 123
Data 35, 113, 122, 42, 117, 119, 58, 109, 23, 105, 63, 27, 44, 105
Data 99, 41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121, 58
Data 114, 126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27, 43
Data 117, 116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118
Data 117, 38, 27, 106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122
Data 109, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114
Data 34, 133, 45, 120, 30, 127, 31, 116, 146
Data out_of_data
 
' ------=< MAIN >=------
 
Dim As String read_in
Dim As Integer i, x, y, count = -1 ' to let the index start on 0
Dim As Integer d()
ReDim d(300) ' big enough to hold data index start at 0
 
Do
Read i
If i = out_of_data Then Exit Do
count = count + 1
d(count) = i
Loop
 
ReDim Preserve d(count) ' trim the data array
shellsort(d()) ' sort data array
 
i = 0
For y = d(0) \ 10 To d(UBound(d)) \ 10
Print Using "#### |"; y;
Do
x = d(i) \ 10 ' \ = integer division
If y = x Then
Print Using "##"; d(i) Mod 10;
i = i + 1
Else
Exit Do
End If
Loop While i <= UBound(d)
Print ' force linefeed
Next
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre> 0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6</pre>
 
==={{header|GW-BASIC}}===
{{trans|QuickBASIC|A global array used instead of subroutine parameters.}}
{{works with|BASICA}}
<syntaxhighlight lang="gwbasic">
100 REM Stem-and-leaf plot
110 N% = 121: REM Array size
120 DIM A%(N% - 1)
130 FOR I% = 0 TO N% - 1
140 READ A%(I%)
150 NEXT I%
160 DATA 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124
170 DATA 37, 48, 127, 36, 29, 31, 125, 139, 131, 115, 105, 132, 104, 123
180 DATA 35, 113, 122, 42, 117, 119, 58, 109, 23, 105, 63, 27, 44, 105
190 DATA 99, 41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121, 58
200 DATA 114, 126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27, 43
210 DATA 117, 116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118
220 DATA 117, 38, 27, 106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122
230 DATA 109, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114
240 DATA 34, 133, 45, 120, 30, 127, 31, 116, 146
250 GOSUB 1000
260 END
990 REM ** Leaf plot
1000 GOSUB 2000
1010 I% = A%(0) \ 10 - 1
1020 FOR J% = 0 TO N% - 1
1030 D% = A%(J%) \ 10
1040 WHILE D% > I%
1050 I% = I% + 1
1060 IF J% THEN PRINT
1070 PRINT USING "## |"; I%;
1080 WEND
1090 PRINT USING "##"; A%(J%) MOD 10;
1100 NEXT J%
1110 PRINT
1120 RETURN
1990 REM ** Shell sort
2000 INCR% = N% \ 2
2010 WHILE INCR% > 0
2020 FOR I% = INCR% TO N% - 1
2030 J% = I% - INCR%
2040 WHILE J% >= 0
2050 IF A%(J%) > A%(J% + INCR%) THEN SWAP A%(J%), A%(J% + INCR%): J% = J% - INCR% ELSE J% = -1
2060 WEND
2070 NEXT I%
2080 INCR% = INCR% \ 2
2090 WEND
2100 RETURN
</syntaxhighlight>
{{out}}
<pre>
0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6
</pre>
 
==={{header|PureBasic}}===
{{works with|PureBasic|4.41}}
<syntaxhighlight lang="purebasic">If OpenConsole()
Dim MyList(120)
Define i, j, StemMax, StemMin
Restore MyData ; Get the address of MyData, e.g. the data to print as a Stem-and-leaf plot
For a=0 To 120
Read.i MyList(a) ; Read the data into the used Array
If MyList(a)>StemMax
StemMax=MyList(a) ; Find the largest Stem layer at the same time
EndIf
If MyList(a)<StemMin
StemMin=MyList(a) ; Find the smallest Stem layer at the same time
EndIf
Next
StemMax/10: StemMin/10 ; Remove the leafs from the Stem limits
SortArray(MyList(),#PB_Sort_Ascending) ; Sort the data
For i=StemMin To StemMax
Print(RSet(Str(i),3)+" | ") ; Print the Stem
For j=0 To 120
If MyList(j)<10*i ; Skip all smaller then current
Continue
ElseIf MyList(j)>=10*(i+1) ; Break current print if a new Stem layer is reached
Break
Else
Print(Str(MyList(j)%10)+" ") ; Print all Leafs on this current Stem layer
EndIf
Next j
PrintN("")
Next i
Print(#CRLF$+#CRLF$+"Press ENTER to exit")
Input()
CloseConsole()
EndIf
DataSection
MyData:
Data.i 12,127, 28, 42, 39,113, 42, 18, 44,118, 44, 37,113,124, 37, 48,127, 36, 29, 31,125,139,131,115
Data.i 105,132,104,123, 35,113,122, 42,117,119, 58,109, 23,105, 63, 27, 44,105, 99, 41,128,121,116,125
Data.i 32, 61, 37,127, 29,113,121, 58,114,126, 53,114, 96, 25,109, 7, 31,141, 46, 13, 27, 43,117,116
Data.i 27, 7, 68, 40, 31,115,124, 42,128, 52, 71,118,117, 38, 27,106, 33,117,116,111, 40,119, 47,105
Data.i 57,122,109,124,115, 43,120, 43, 27, 27, 18, 28, 48,125,107,114, 34,133, 45,120, 30,127, 31,116,146
EndDataSection</syntaxhighlight>
{{out}}
<pre>
0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6
</pre>
 
==={{header|QuickBASIC}}===
{{trans|BBC BASIC|But Shell sort is used.}}
<syntaxhighlight lang="qbasic">
REM Stem-and-leaf plot
DECLARE SUB LeafPlot (X%())
DECLARE SUB ShellSortInt (A%())
 
CONST MAXDATNDX = 120
DIM Dat%(MAXDATNDX)
FOR I% = 0 TO MAXDATNDX
READ Dat%(I%)
NEXT I%
DATA 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124
DATA 37, 48, 127, 36, 29, 31, 125, 139, 131, 115, 105, 132, 104, 123
DATA 35, 113, 122, 42, 117, 119, 58, 109, 23, 105, 63, 27, 44, 105
DATA 99, 41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121, 58
DATA 114, 126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27, 43
DATA 117, 116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118
DATA 117, 38, 27, 106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122
DATA 109, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114
DATA 34, 133, 45, 120, 30, 127, 31, 116, 146
 
LeafPlot Dat%()
END
 
SUB LeafPlot (X%())
ShellSortInt X%()
I% = X%(0) \ 10 - 1
FOR J% = 0 TO UBOUND(X%)
D% = X%(J%) \ 10
WHILE D% > I%
I% = I% + 1
IF J% THEN PRINT
PRINT USING "## |"; I%;
WEND
PRINT USING "##"; X%(J%) MOD 10;
NEXT
PRINT
END SUB
 
SUB ShellSortInt (A%())
N% = UBOUND(A%) + 1
Incr% = N% \ 2
WHILE Incr% > 0
FOR I% = Incr% TO N% - 1
J% = I% - Incr%
WHILE J% >= 0
IF A%(J%) > A%(J% + Incr%) THEN
SWAP A%(J%), A%(J% + Incr%)
J% = J% - Incr%
ELSE
J% = -1
END IF
WEND
NEXT I%
Incr% = Incr% \ 2
WEND
END SUB
</syntaxhighlight>
{{out}}
<pre>
0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6
</pre>
 
==={{header|uBasic/4tH}}===
<syntaxhighlight lang="text">Push 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124
Push 0, 13 : Gosub _Read ' read 1st line of data
 
Push 37, 48, 127, 36, 29, 31, 125, 139, 131, 115, 105, 132, 104, 123
Push 14, 27 : Gosub _Read ' read 2nd line of data
 
Push 35, 113, 122, 42, 117, 119, 58, 109, 23, 105, 63, 27, 44, 105
Push 28, 41 : Gosub _Read ' read 3rd line of data
 
Push 99, 41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121, 58
Push 42, 55 : Gosub _Read ' read 4tH line of data
 
Push 114, 126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27, 43
Push 56, 69 : Gosub _Read ' read 5th line of data
 
Push 117, 116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118
Push 70, 83 : Gosub _Read ' read 6th line of data
 
Push 117, 38, 27, 106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122
Push 84, 97 : Gosub _Read ' read 7th line of data
 
Push 109, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114
Push 98, 111 : Gosub _Read ' read 8th line of data
 
Push 34, 133, 45, 120, 30, 127, 31, 116, 146
Push 112, 120 : Gosub _Read ' read last line of data
 
Push 121 : Gosub _SimpleSort ' now sort 121 elements
 
i = @(0) / 10 - 1
For j = 0 To Pop() - 1 ' note array size was still on stack
d = @(j) / 10
Do While d > i
If j Print
i = i + 1
If i < 10 Print " "; ' align stem number
Print i;" |"; ' print stem number
Loop
Print @(j) % 10;" "; ' print leaf number
Next
Print ' print final LF
 
End
 
' simplest sorting algorithm
_SimpleSort ' ( n -- n)
For x = 0 To Tos() - 1
For y = x+1 To Tos() - 1
If @(x) > @ (y) Then ' if larger, switch elements
Push @(y)
@(y) = @(x)
@(x) = Pop()
Endif
Next
Next
 
Return
 
' read a line of data backwards
_Read ' (.. n1 n2 -- ..)
For x = Pop() To Pop() Step -1 ' loop from n2 to n1
@(x) = Pop() ' get element from stack
Next
Return</syntaxhighlight>
Output:
<pre> 0 |7 7
1 |2 3 8 8
2 |3 5 7 7 7 7 7 7 8 8 9 9
3 |0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 |0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 |2 3 7 8 8
6 |1 3 8
7 |1
8 |
9 |6 9
10 |4 5 5 5 5 6 7 9 9 9
11 |1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 |0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 |1 2 3 9
14 |1 6
 
0 OK, 0:2037
</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 392 ⟶ 1,197:
 
return 0;
}</langsyntaxhighlight>output<syntaxhighlight lang="text"> 0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
Line 406 ⟶ 1,211:
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 455 ⟶ 1,260:
}
}
}</langsyntaxhighlight>
<pre> 0 | 7 7
1 | 2 3 8 8
Line 473 ⟶ 1,278:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iomanip>
#include <iostream>
Line 516 ⟶ 1,321:
std::cout << std::endl;
}
}</langsyntaxhighlight>
Output:
<pre> 0 | 7 7
Line 535 ⟶ 1,340:
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">"Run the module `thestemandleafplot`."
shared void run() {
Line 558 ⟶ 1,363:
print("``formatInteger(i).padLeading(2)``| ``" ".join(stemsToLeaves[i] else [])``");
}
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(def data
[12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125
139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27
Line 603 ⟶ 1,408:
 
(stem-and-leaf data)
</syntaxhighlight>
</lang>
 
{{out}}
Line 625 ⟶ 1,430:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm;
 
void main() {
Line 642 ⟶ 1,447:
foreach (i; loHi[0]/10 .. loHi[1]/10 + 1)
writefln("%2d | %(%d %) ", i, histo.get(i, []).sort());
}</langsyntaxhighlight>
Output:
<pre> 0 | 7 7
Line 663 ⟶ 1,468:
{{trans|Ruby}}
{{works with|Elixir|1.3}}
<langsyntaxhighlight lang="elixir">defmodule Stem_and_leaf do
def plot(data, leaf_digits\\1) do
multiplier = Enum.reduce(1..leaf_digits, 1, fn _,acc -> acc*10 end)
Line 686 ⟶ 1,491:
data = ~w(12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146)
|> Enum.map(&String.to_integer(&1))
Stem_and_leaf.plot(data)</langsyntaxhighlight>
 
{{out}}
Line 708 ⟶ 1,513:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">include sort.e
 
procedure leaf_plot(sequence s)
Line 735 ⟶ 1,540:
114, 34, 133, 45, 120, 30, 127, 31, 116, 146 }
 
leaf_plot(data)</langsyntaxhighlight>
 
Output:
Line 756 ⟶ 1,561:
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System
 
let data =
Line 782 ⟶ 1,587:
printfn "")
 
plotStemAndLeafs data</langsyntaxhighlight>
Output:
<pre>
Line 802 ⟶ 1,607:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: assocs formatting grouping.extras io kernel math
prettyprint sequences sorting ;
 
Line 819 ⟶ 1,624:
120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31
116
} leaf-plot</langsyntaxhighlight>
{{out}}
<pre>
Line 840 ⟶ 1,645:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">create data
12 , 127 , 28 , 42 , 39 , 113 , 42 , 18 , 44 , 118 , 44 ,
37 , 113 , 124 , 37 , 48 , 127 , 36 , 29 , 31 , 125 , 139 ,
Line 874 ⟶ 1,679:
drop ;
 
plot</langsyntaxhighlight>
Output:
<pre>
Line 901 ⟶ 1,706:
Note that the MOD function can produce unexpected values for negative numbers, and, different computer/compiler/language combinations may produce different surprises. In this case, negative values produce negative remainder values, but the ABS function suppresses the surprise.
<syntaxhighlight lang="fortran">
<lang Fortran>
SUBROUTINE COMBSORT(A,N)
INTEGER A(*) !The array.
Line 962 ⟶ 1,767:
CALL TOPIARY(VALUES,121)
END
</syntaxhighlight>
</lang>
 
Output: (If additional spacing is desired, I2 format could be used, etc.)
Line 982 ⟶ 1,787:
14|16
</pre>
 
=={{header|FreeBASIC}}==
<lang FreeBASIC>' version 22-06-2015
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
' from the rosetta code FreeBASIC entry
#Define out_of_data 99999999 ' any number that is not in the set will do
 
Sub shellsort(s() As Integer)
' from the FreeBASIC entry at rosetta code
' sort from lower bound to the highter bound
Dim As Integer lb = LBound(s)
Dim As Integer ub = UBound(s)
Dim As Integer done, i, inc = ub - lb
 
Do
inc = inc / 2.2
If inc < 1 Then inc = 1
Do
done = 0
For i = lb To ub - inc
If s(i) > s(i + inc) Then
Swap s(i), s(i + inc)
done = 1
End If
Next
Loop Until done = 0
Loop Until inc = 1
 
End Sub
 
' ------=< TASK DATA >=------
 
Data 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124
Data 37, 48, 127, 36, 29, 31, 125, 139, 131, 115, 105, 132, 104, 123
Data 35, 113, 122, 42, 117, 119, 58, 109, 23, 105, 63, 27, 44, 105
Data 99, 41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121, 58
Data 114, 126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27, 43
Data 117, 116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118
Data 117, 38, 27, 106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122
Data 109, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114
Data 34, 133, 45, 120, 30, 127, 31, 116, 146
Data out_of_data
 
' ------=< MAIN >=------
 
Dim As String read_in
Dim As Integer i, x, y, count = -1 ' to let the index start on 0
Dim As Integer d()
ReDim d(300) ' big enough to hold data index start at 0
 
Do
Read i
If i = out_of_data Then Exit Do
count = count + 1
d(count) = i
Loop
 
ReDim Preserve d(count) ' trim the data array
shellsort(d()) ' sort data array
 
i = 0
For y = d(0) \ 10 To d(UBound(d)) \ 10
Print Using "#### |"; y;
Do
x = d(i) \ 10 ' \ = integer division
If y = x Then
Print Using "##"; d(i) Mod 10;
i = i + 1
Else
Exit Do
End If
Loop While i <= UBound(d)
Print ' force linefeed
Next
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre> 0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,121 ⟶ 1,829:
}
}
}</langsyntaxhighlight>
Output:
<pre>
Line 1,142 ⟶ 1,850:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List
import Control.Arrow
import Control.Monad
Line 1,164 ⟶ 1,872:
stems = map (flip(,)[]) $ uncurry enumFromTo $ minimum &&& maximum $ fst $ unzip stemLeaf
showStemLeaves f w (a,b) = f w (show a) ++ " |" ++ concatMap (f w. show) b
fb = length $ show $ maximum $ map abs ds</langsyntaxhighlight>
Output:
<pre>*Main> task nls
Line 1,185 ⟶ 1,893:
Or alternatively – aiming more for legibility than for economy or concision:
 
<langsyntaxhighlight lang="haskell">import Data.List (groupBy, intersperse, mapAccumL, sortBy)
import Data.Ord (comparing)
import Data.Function (on)
Line 1,257 ⟶ 1,965:
main :: IO ()
main = putStrLn $ unlines plotLines
</syntaxhighlight>
</lang>
{{Out}}
<pre> 0 | 7 7
Line 1,277 ⟶ 1,985:
=={{header|HicEst}}==
The dialog prompts for bitmap or a text image, and for the stem base. Data are read in from clipboard.
<langsyntaxhighlight HicEstlang="hicest">REAL :: workspace(1000), base=16
 
DLG(CHeckbox=bitmap, NameEdit=base, DNum, MIn=1, MAx=16) ! 1 <= stem base <= 16
Line 1,304 ⟶ 2,012:
WRITE(Format="i3, ':'") stem
ENDIF
ENDDO</langsyntaxhighlight>
Shown is the given example for bitmap=0 and base 16
<pre> 0 : 7 7 C D
Line 1,318 ⟶ 2,026:
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight lang="unicon">procedure main(A)
prune := integer(\A[1]) | 10 # Boundary between leaf and stem
every put(data := [], integer(!&input))
Line 1,329 ⟶ 2,037:
}
write()
end</langsyntaxhighlight>
Sample output from data.
<pre>->stem <stem.data
Line 1,356 ⟶ 2,064:
=={{header|J}}==
'''Solution: (Tacit)'''
<langsyntaxhighlight lang="j">stem =: <.@(%&10)
leaf =: 10&|
stemleaf =: (stem@{. ; leaf)/.~ stem
Line 1,362 ⟶ 2,070:
expandLeaves=: (expandStems e. ])@[ #inv ]
 
showStemLeaf=: (":@,.@expandStems@[ ; ":&>@expandLeaves)&>/@(>@{. ; <@{:)@|:@stemleaf@/:~</langsyntaxhighlight>
 
'''Solution: (Explicit)'''
<langsyntaxhighlight lang="j">stemleafX=: monad define
leaves=. 10 | y
stems=. y <.@:% 10
Line 1,377 ⟶ 2,085:
xleaves=. (xstems e. stems) #inv leaves NB. expand leaves to match xstems
(": ,.xstems) ; ":&> xleaves
)</langsyntaxhighlight>
 
'''Example:'''
<langsyntaxhighlight lang="j"> nls =: ; <@(_&".);._2 noun define
12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125
139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105
Line 1,418 ⟶ 2,126:
 
(showStemLeaf -: showStemLeafX) nls NB. both solutions give same result
1</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Line 1,478 ⟶ 2,186:
printPlot(plot);
}
}</langsyntaxhighlight>
{{works with|Java|1.8+}}
<langsyntaxhighlight lang="java5">import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Line 1,564 ⟶ 2,272:
;
}
}</langsyntaxhighlight>
Output:
<pre>0 | [7, 7]
Line 1,586 ⟶ 2,294:
It turns out that HTML+CSS renders the plot quite attractively.
 
<langsyntaxhighlight lang="html4strict"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
Line 1,662 ⟶ 2,370:
 
</body>
</html></langsyntaxhighlight>
 
The output looks like:
Line 1,669 ⟶ 2,377:
 
===JavaScript ES6===
<langsyntaxhighlight lang="javascript">(() => {
// main :: IO String
const main = () => {
Line 1,879 ⟶ 2,587:
// MAIN ------------------------------------------------------------------
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre> 0 | 7 7
Line 1,898 ⟶ 2,606:
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq">def stem_and_leaf:
 
# align-right:
Line 1,915 ⟶ 2,623:
else [ $stem + 1, (.[1] + "\n\($stem+1|right) | \($d % 10)" )]
end )
| .[1] ;</langsyntaxhighlight>
'''Example''':
<langsyntaxhighlight lang="jq">def data:
[ 12,127,28,42,39,113, 42,18,44,118,44,37,113,124,37,48,127,36,29,31,
125,139,131,115,105,132,104,123,35,113,122,42,117,119,58,109,23,105,
Line 1,928 ⟶ 2,636:
data | stem_and_leaf
</syntaxhighlight>
</lang>
{{Out}}
<syntaxhighlight lang="sh">
<lang sh>
$ jq -n -r -f stem-and-leaf_plot.jq
0 | 77
Line 1,946 ⟶ 2,654:
12 | 00112234445556777788
13 | 1239
14 | 16</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 1,952 ⟶ 2,660:
 
This is a rather elaborate function that creates a string depicting a stem and leaf plot. Much of the elaboration is to handle the case of negative numbers that have a stem of 0. There is also a bit of work to allow for leaf sizes other than 1 (some power of 10).
<syntaxhighlight lang="julia">
<lang Julia>
function stemleaf{T<:Real}(a::Array{T,1}, leafsize=1)
ls = 10^int(log10(leafsize))
Line 1,978 ⟶ 2,686:
return slp
end
</syntaxhighlight>
</lang>
 
'''Main'''
<syntaxhighlight lang="julia">
<lang Julia>
println("Using the Task's Test Data")
test = """12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29
Line 2,001 ⟶ 2,709:
println("Using: ", test)
println(stemleaf(test, 10))
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,061 ⟶ 2,769:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun leafPlot(x: IntArray) {
Line 2,087 ⟶ 2,795:
)
leafPlot(data)
}</langsyntaxhighlight>
 
{{out}}
Line 2,109 ⟶ 2,817:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">data = { 12,127,28,42,39,113, 42,18,44,118,44,37,113,124,37,48,127,36,29,31,
125,139,131,115,105,132,104,123,35,113,122,42,117,119,58,109,23,105,
63,27,44,105,99,41,128,121,116,125,32,61,37,127,29,113,121,58,114,126,
Line 2,132 ⟶ 2,840:
print ""
end</langsyntaxhighlight>
Output:
<pre> 0 | 7 7
Line 2,151 ⟶ 2,859:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">StemPlot := proc( datatable::{rtable,list,algebraic} )
local i, j, k, tf, LeafStemTable, LeafStemIndices;
k:=0;
Line 2,199 ⟶ 2,907:
Y := [ 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124, 37, 48, 127, 36, 29, 31, 125, 139, 131, 115, 105, 132, 104, 123, 35, 113, 122, 42, 117, 119, 58, 109, 23, 105, 63, 27, 44, 105, 99, 41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121, 58, 114, 126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27, 43, 117, 116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118, 117, 38, 27, 106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122, 109, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114, 34, 133, 45, 120, 30, 127, 31, 116, 146];
 
StemPlot(Y);</langsyntaxhighlight>
 
<pre>0 | 7 7
Line 2,218 ⟶ 2,926:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">len[n_] := RealDigits[n][[2]]; padding = len[Max@ Quotient[inputdata, 10]];
For[i = Min@ Quotient[inputdata, 10],i <= Max@ Quotient[inputdata, 10], i++,
(Print[i, If[(padding - len[i]) > 0, (padding - len[i])*" " <> " |", " |"] ,
StringJoin[(" " <> #) & /@ Map[ToString, #]]])&@
Select[{Quotient[#, 10], Mod[#, 10]} & /@ Sort[inputdata],Part[#, 1] == i &][[;; , 2]]]</langsyntaxhighlight>
{{out}}
<pre>0 | 7 7
Line 2,242 ⟶ 2,950:
=={{header|MATLAB}} / {{header|Octave}}==
 
<langsyntaxhighlight Matlablang="matlab">function stem_and_leaf_plot(x,stem_unit,leaf_unit)
if nargin < 2, stem_unit = 10; end;
if nargin < 3,
Line 2,264 ⟶ 2,972:
x = [12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146];
 
stem_and_leaf_plot(x); </langsyntaxhighlight>
Output:
<pre>
Line 2,287 ⟶ 2,995:
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">load(descrptive)$
 
data: [12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124, 37, 48, 127,
Line 2,311 ⟶ 3,019:
12|00112234445556777788
13|1239
14|16</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import tables
import math
import strutils
Line 2,376 ⟶ 3,084:
var negativePlot = StemLeafPlot()
negativePlot.init(1, negativeData)
echo negativePlot</langsyntaxhighlight>
 
{{out}}
Line 2,410 ⟶ 3,118:
The definition of the function <code>unique</code> below can be omited if one uses the [http://code.google.com/p/ocaml-extlib/ extlib].
 
<langsyntaxhighlight lang="ocaml">let unique li =
let rec aux acc = function
| [] -> (List.rev acc)
Line 2,418 ⟶ 3,126:
else aux (x::acc) xs
in
aux [] li</langsyntaxhighlight>
 
<langsyntaxhighlight lang="ocaml">let data =
[ 12; 127; 28; 42; 39; 113; 42; 18; 44; 118; 44; 37; 113; 124; 37; 48;
127; 36; 29; 31; 125; 139; 131; 115; 105; 132; 104; 123; 35; 113; 122;
Line 2,443 ⟶ 3,151:
List.iter (Printf.printf " %d") vs;
print_newline()
) keys</langsyntaxhighlight>
 
we can output the same latex code than the Perl example replacing the main function as follow:
 
<langsyntaxhighlight lang="ocaml">let () =
print_endline "\
\\documentclass{report}
Line 2,464 ⟶ 3,172:
print_endline "\
\\end{tabular}
\\end{document}"</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">my @data = sort {$a <=> $b} qw( 12 127 28 42 39 113 42 18 44 118 44
37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113
122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32
Line 2,492 ⟶ 3,200:
print " $leaf";
}
</syntaxhighlight>
</lang>
{{out}}
<pre> 0 | 7 7
Line 2,512 ⟶ 3,220:
=== LaTeX output ===
generating {{header|LaTeX}}
<langsyntaxhighlight lang="perl">#!/usr/bin/perl -w
 
my @data = sort {$a <=> $b} qw( 12 127 28 42 39 113 42 18 44 118 44
Line 2,554 ⟶ 3,262:
\end{tabular}
\end{document}
EOT</langsyntaxhighlight>
 
LaTeX output of the Perl program:
 
<langsyntaxhighlight lang="latex">\documentclass{report}
\usepackage{fullpage}
\begin{document}
Line 2,569 ⟶ 3,277:
14 & 1
\end{tabular}
\end{document}</langsyntaxhighlight>
 
The parameter to the <code>tabular</code> environment defines the columns of the table. “r” and “c” are right- and center-aligned columns, “|” is a vertical rule, and “<code>*{''count''}{''cols''}”</code> repeats a column definition ''count'' times.
Line 2,581 ⟶ 3,289:
=={{header|Phix}}==
{{trans|Euphoria}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">leaf_plot</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 2,609 ⟶ 3,317:
<span style="color: #000000;">leaf_plot</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre style="font-size: 8px">
Line 2,630 ⟶ 3,338:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de *Data
12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36
29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119
Line 2,648 ⟶ 3,356:
(sort *Data) ) )
(for I (range (caar L) (car (last L)))
(prinl (align 3 I) " | " (glue " " (cdr (assoc I L)))) ) )</langsyntaxhighlight>
Output:
<pre> 0 | 7 7
Line 2,667 ⟶ 3,375:
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">
$Set = -split '12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146'
Line 2,678 ⟶ 3,386:
@( $Stem.ToString().PadLeft( 2, " " ), '|' ) + ( ( $Data | Where Stem -eq $Stem ).Leaf | Sort ) -join " "
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,697 ⟶ 3,405:
14 | 1 6
</pre>
 
=={{header|PureBasic}}==
{{works with|PureBasic|4.41}}
 
'''PureBasic Code'''
<lang PureBasic>If OpenConsole()
Dim MyList(120)
Define i, j, StemMax, StemMin
Restore MyData ; Get the address of MyData, e.g. the data to print as a Stem-and-leaf plot
For a=0 To 120
Read.i MyList(a) ; Read the data into the used Array
If MyList(a)>StemMax
StemMax=MyList(a) ; Find the largest Stem layer at the same time
EndIf
If MyList(a)<StemMin
StemMin=MyList(a) ; Find the smallest Stem layer at the same time
EndIf
Next
StemMax/10: StemMin/10 ; Remove the leafs from the Stem limits
SortArray(MyList(),#PB_Sort_Ascending) ; Sort the data
For i=StemMin To StemMax
Print(RSet(Str(i),3)+" | ") ; Print the Stem
For j=0 To 120
If MyList(j)<10*i ; Skip all smaller then current
Continue
ElseIf MyList(j)>=10*(i+1) ; Break current print if a new Stem layer is reached
Break
Else
Print(Str(MyList(j)%10)+" ") ; Print all Leafs on this current Stem layer
EndIf
Next j
PrintN("")
Next i
Print(#CRLF$+#CRLF$+"Press ENTER to exit")
Input()
CloseConsole()
EndIf
DataSection
MyData:
Data.i 12,127, 28, 42, 39,113, 42, 18, 44,118, 44, 37,113,124, 37, 48,127, 36, 29, 31,125,139,131,115
Data.i 105,132,104,123, 35,113,122, 42,117,119, 58,109, 23,105, 63, 27, 44,105, 99, 41,128,121,116,125
Data.i 32, 61, 37,127, 29,113,121, 58,114,126, 53,114, 96, 25,109, 7, 31,141, 46, 13, 27, 43,117,116
Data.i 27, 7, 68, 40, 31,115,124, 42,128, 52, 71,118,117, 38, 27,106, 33,117,116,111, 40,119, 47,105
Data.i 57,122,109,124,115, 43,120, 43, 27, 27, 18, 28, 48,125,107,114, 34,133, 45,120, 30,127, 31,116,146
EndDataSection</lang>
 
'''Output'''
0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6
 
=={{header|Python}}==
 
Adjusting <code>Stem.leafdigits</code> allows you to modify how many digits of a value are used in the leaf, with the stem intervals adjusted accordingly.
<langsyntaxhighlight lang="python">from collections import namedtuple
from pprint import pprint as pp
from math import floor
Line 2,804 ⟶ 3,447:
 
if __name__ == '__main__':
print( stemplot(data0) )</langsyntaxhighlight>
 
'''Sample Output'''
Line 2,829 ⟶ 3,472:
 
Here is an another example using an OrderedDict and Counter
<langsyntaxhighlight lang="python">from collections import OrderedDict, Counter
 
x= [12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124, 37, 48,
Line 2,848 ⟶ 3,491:
 
stemleaf(x)
</syntaxhighlight>
</lang>
 
Output :
Line 2,871 ⟶ 3,514:
 
Or, generalising a little to write a purely declarative function (in terms of '''groupby''' and '''reduce''') which takes stem and leaf accessor functions as its first arguments:
<langsyntaxhighlight lang="python">from itertools import (groupby)
from functools import (reduce)
 
Line 2,907 ⟶ 3,550:
 
 
main()</langsyntaxhighlight>
<pre> 0 | 7 7
1 | 2 3 8 8
Line 2,916 ⟶ 3,559:
6 | 1 3 8
7 | 1
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6 </pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ sort
-1 swap
witheach
[ 2dup < iff
[ cr dip
[ 10 + dup
10 / dup
10 < if sp
echo say " | " ] ]
again
[ over - 9 + echo sp ] ]
drop ] is leafplot ( [ --> )
 
' [ 12 127 28 42 39 113 42 18 44 118 44
37 113 124 37 48 127 36 29 31 125 139
131 115 105 132 104 123 35 113 122 42 117
119 58 109 23 105 63 27 44 105 99 41
128 121 116 125 32 61 37 127 29 113 121
58 114 126 53 114 96 25 109 7 31 141
46 13 27 43 117 116 27 7 68 40 31
115 124 42 128 52 71 118 117 38 27 106
33 117 116 111 40 119 47 105 57 122 109
124 115 43 120 43 27 27 18 28 48 125
107 114 34 133 45 120 30 127 31 116 146 ]
 
leafplot</syntaxhighlight>
 
{{out}}
 
<pre> 0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
Line 2,925 ⟶ 3,615:
=={{header|R}}==
 
<syntaxhighlight lang="r">
<lang R>
x <- c(12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124, 37, 48, 127, 36,
29, 31, 125, 139, 131, 115, 105, 132, 104, 123, 35, 113, 122, 42, 117, 119, 58, 109,
Line 2,935 ⟶ 3,625:
 
stem(x)
</syntaxhighlight>
</lang>
 
Output :
Line 2,958 ⟶ 3,648:
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
(define (show-stem+leaf data)
Line 2,969 ⟶ 3,659:
(newline)))
(show-stem+leaf (sequence->list (in-producer read eof)))
</syntaxhighlight>
</lang>
 
Sample run:
Line 2,996 ⟶ 3,686:
 
Handles negative stems properly.
<syntaxhighlight lang="raku" perl6line>my @data = <
12 127 28 42 39 113 42 18 44 118 44
37 113 124 37 48 127 36 29 31 125 139
Line 3,019 ⟶ 3,709:
my $leafs = %h{$stem} // [];
say $stem.fmt($stem_format), ' | ', ~$leafs.map: * % $stem_unit;
}</langsyntaxhighlight>
 
Output:<pre> 0 | 7 7
Line 3,044 ⟶ 3,734:
 
Also, all numbers that are processed are normalized. &nbsp; Using a &nbsp; ''sparse array'' &nbsp; bypasses the need for sorting.
<langsyntaxhighlight lang="rexx">/*REXX program displays a stem and leaf plot of any non-negative numbers [can include 0]*/
parse arg @ /* [↓] Not specified? Then use default*/
if @='' then @=12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 139,
Line 3,069 ⟶ 3,759:
end /*m*/
say right(k, w) '║' space($) /*display a line of stem─and─leaf plot.*/
end /*k*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
'''output''' &nbsp; when using the (internal) defaults as input:
<pre>
Line 3,091 ⟶ 3,781:
===negative, zero, and positive numbers===
This REXX version also handles negative numbers.
<langsyntaxhighlight lang="rexx">/*REXX program displays a stem─and─leaf plot of any real numbers [can be: neg, 0, pos].*/
parse arg @ /*obtain optional arguments from the CL*/
if @='' then @='15 14 3 2 1 0 -1 -2 -3 -14 -15' /*Not specified? Then use the default.*/
Line 3,121 ⟶ 3,811:
end /*m*/
say right(k, w) '║' space($) /*display a line of stem─and─leaf plot.*/
end /*k*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
'''output''' &nbsp; when using the (internal) defaults as input:
<pre>
Line 3,131 ⟶ 3,821:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Stem-and-leaf plot
 
Line 3,163 ⟶ 3,853:
next
see nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,186 ⟶ 3,876:
=={{header|Ruby}}==
This implementation will handle negative values.
<langsyntaxhighlight lang="ruby">class StemLeafPlot
def initialize(data, options = {})
opts = {:leaf_digits => 1}.merge(options)
Line 3,299 ⟶ 3,989:
27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116
111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34
133 45 120 30 127 31 116 146</langsyntaxhighlight>
 
{{out}}
Line 3,323 ⟶ 4,013:
 
'''Simple version'''
<langsyntaxhighlight lang="ruby">class StemLeafPlot
def initialize(data, leaf_digits=1)
@leaf_digits = leaf_digits
Line 3,351 ⟶ 4,041:
27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116
111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34
133 45 120 30 127 31 116 146</langsyntaxhighlight>
{{out}}
<pre>
Line 3,373 ⟶ 4,063:
=={{header|Scala}}==
{{works with|Scala|2.8}}
<langsyntaxhighlight lang="scala">def stemAndLeaf(numbers: List[Int]) = {
val lineFormat = "%" + (numbers map (_.toString.length) max) + "d | %s"
val map = numbers groupBy (_ / 10)
Line 3,379 ⟶ 4,069:
println(lineFormat format (stem, map.getOrElse(stem, Nil) map (_ % 10) sortBy identity mkString " "))
}
}</langsyntaxhighlight>
 
Example:
Line 3,415 ⟶ 4,105:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: leafPlot (in var array integer: x) is func
Line 3,451 ⟶ 4,141:
begin
leafPlot(data);
end func;</langsyntaxhighlight>
 
Output:
Line 3,474 ⟶ 4,164:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var data = %i(
12 127 28 42 39 113 42 18 44 118 44
37 113 124 37 48 127 36 29 31 125 139
Line 3,497 ⟶ 4,187:
var leafs = (h{stem} \\ [])
say(stem_format % stem, ' | ', leafs.map { _ % stem_unit }.join(' '))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,519 ⟶ 4,209:
=={{header|Stata}}==
 
<langsyntaxhighlight lang="stata">. clear all
. input x
12
Line 3,548 ⟶ 4,238:
12* | 00112234445556777788
13* | 1239
14* | 16</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
# How to process a single value, adding it to the table mapping stems to
Line 3,611 ⟶ 4,301:
48 125 107 114 34 133 45 120 30 127 31 116 146
}
printStemLeaf $data</langsyntaxhighlight>
Output:
<pre>
Line 3,632 ⟶ 4,322:
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
digits=*
Line 3,662 ⟶ 4,352:
ENDLOOP
DO format
</syntaxhighlight>
</lang>
Output:
<pre style='height:30ex;overflow:scroll'>
Line 3,680 ⟶ 4,370:
13 1'2'3'9
14 1'6
</pre>
 
=={{header|uBasic/4tH}}==
<lang>Push 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124
Push 0, 13 : Gosub _Read ' read 1st line of data
 
Push 37, 48, 127, 36, 29, 31, 125, 139, 131, 115, 105, 132, 104, 123
Push 14, 27 : Gosub _Read ' read 2nd line of data
 
Push 35, 113, 122, 42, 117, 119, 58, 109, 23, 105, 63, 27, 44, 105
Push 28, 41 : Gosub _Read ' read 3rd line of data
 
Push 99, 41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121, 58
Push 42, 55 : Gosub _Read ' read 4tH line of data
 
Push 114, 126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27, 43
Push 56, 69 : Gosub _Read ' read 5th line of data
 
Push 117, 116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118
Push 70, 83 : Gosub _Read ' read 6th line of data
 
Push 117, 38, 27, 106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122
Push 84, 97 : Gosub _Read ' read 7th line of data
 
Push 109, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114
Push 98, 111 : Gosub _Read ' read 8th line of data
 
Push 34, 133, 45, 120, 30, 127, 31, 116, 146
Push 112, 120 : Gosub _Read ' read last line of data
 
Push 121 : Gosub _SimpleSort ' now sort 121 elements
 
i = @(0) / 10 - 1
For j = 0 To Pop() - 1 ' note array size was still on stack
d = @(j) / 10
Do While d > i
If j Print
i = i + 1
If i < 10 Print " "; ' align stem number
Print i;" |"; ' print stem number
Loop
Print @(j) % 10;" "; ' print leaf number
Next
Print ' print final LF
 
End
 
' simplest sorting algorithm
_SimpleSort ' ( n -- n)
For x = 0 To Tos() - 1
For y = x+1 To Tos() - 1
If @(x) > @ (y) Then ' if larger, switch elements
Push @(y)
@(y) = @(x)
@(x) = Pop()
Endif
Next
Next
 
Return
 
' read a line of data backwards
_Read ' (.. n1 n2 -- ..)
For x = Pop() To Pop() Step -1 ' loop from n2 to n1
@(x) = Pop() ' get element from stack
Next
Return</lang>
Output:
<pre> 0 |7 7
1 |2 3 8 8
2 |3 5 7 7 7 7 7 7 8 8 9 9
3 |0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 |0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 |2 3 7 8 8
6 |1 3 8
7 |1
8 |
9 |6 9
10 |4 5 5 5 5 6 7 9 9 9
11 |1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 |0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 |1 2 3 9
14 |1 6
 
0 OK, 0:2037
</pre>
 
=={{header|Ursala}}==
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
 
Line 3,789 ⟶ 4,394:
#show+
 
main = stemleaf_plot data</langsyntaxhighlight>
Reading from right to left on the bottom line of the <code>stemleaf_plot</code> function, we
obtain the quotient and remainder of every datum divided by ten, partition by
Line 3,825 ⟶ 4,430:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var leafPlot = Fn.new { |x|
Line 3,852 ⟶ 4,457:
34, 133, 45, 120, 30, 127, 31, 116, 146
]
leafPlot.call(data)</langsyntaxhighlight>
 
{{out}}
Line 3,871 ⟶ 4,476:
13 | 1 2 3 9
14 | 1 6
</pre>
 
=={{header|XPL0}}==
{{trans|QuickBASIC}}
{{works with|EXPL-32}}
<syntaxhighlight lang="xpl0">
\ Stem-and-leaf plot
code Rem=2, CrLf=9, Text=12;
code real RlOut=48, Float=49, Format=52;
define DataSize = 121;
integer Data;
 
procedure ShellSortInt(A, N);
integer A, N;
integer I, J, Incr, Tmp;
begin
Incr:= N / 2;
while Incr > 0 do
begin
for I:= Incr, N - 1 do
begin
J:= I - Incr;
while J >= 0 do
begin
if A(J) > A(J + Incr) then
begin
Tmp:= A(J);
A(J):= A(J + Incr);
A(J + Incr):= Tmp;
J:= J - Incr
end
else
J:= -1
end;
end;
Incr:= Incr / 2
end;
end;
 
procedure LeafPlot (X, N);
integer X, N;
integer D, I, J;
begin
ShellSortInt(X, N);
I:= X(0) / 10 - 1;
Format(2,0);
for J:= 0, N - 1 do
begin
D:= X(J) / 10;
while D > I do
begin
I:= I + 1;
if J then CrLf(0);
RlOut(0, Float(I)); Text(0, " |");
end;
RlOut(0, Float(Rem(X(J) / 10)));
end;
CrLf(0);
end;
 
begin
Data:= [ 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124,
37, 48, 127, 36, 29, 31, 125, 139, 131, 115, 105, 132, 104, 123,
35, 113, 122, 42, 117, 119, 58, 109, 23, 105, 63, 27, 44, 105,
99, 41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121, 58,
114, 126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27, 43,
117, 116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118,
117, 38, 27, 106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122,
109, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114,
34, 133, 45, 120, 30, 127, 31, 116, 146];
LeafPlot(Data, DataSize)
end;
</syntaxhighlight>
{{out}}
<pre>
0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6
</pre>
 
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">fcn leaf_plot(xs){
xs=xs.sort();
i := xs[0] / 10 - 1;
Line 3,897 ⟶ 4,592:
34, 133, 45, 120, 30, 127, 31, 116, 146 );
leaf_plot(data);</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits