Stem-and-leaf plot: Difference between revisions
Content added Content deleted
m (BBC BASIC moved to the BASIC section.) |
(Dialects of BASIC moved to the BASIC section.) |
||
Line 600: | Line 600: | ||
13 | 1 2 3 9 |
13 | 1 2 3 9 |
||
14 | 1 6 |
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|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|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> |
</pre> |
||
Line 1,231: | Line 1,477: | ||
14|16 |
14|16 |
||
</pre> |
</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|Go}}== |
=={{header|Go}}== |
||
Line 2,946: | Line 3,095: | ||
14 | 1 6 |
14 | 1 6 |
||
</pre> |
</pre> |
||
=={{header|PureBasic}}== |
|||
{{works with|PureBasic|4.41}} |
|||
'''PureBasic Code''' |
|||
<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> |
|||
'''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}}== |
=={{header|Python}}== |
||
Line 3,976: | Line 4,060: | ||
13 1'2'3'9 |
13 1'2'3'9 |
||
14 1'6 |
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> |
</pre> |
||