WeixiangYan/CodeTransOcean huggingface dataset:
Task: Bitwise operations






PHP: function bitwise($a, $b)
{
    function zerofill($a,$b) { 
        if($a>=0) return $a>>$b;
        if($b==0) return (($a>>1)&0x7fffffff)*2+(($a>>$b)&1); // this line shifts a 0 into the sign bit for compatibility, replace with "if($b==0) return $a;" if you need $b=0 to mean that nothing happens
        return ((~$a)>>$b)^(0x7fffffff>>($b-1)); 

    echo '$a AND $b: ' . $a & $b . '\n';
    echo '$a OR $b: ' . $a | $b . '\n';
    echo '$a XOR $b: ' . $a ^ $b . '\n';
    echo 'NOT $a: ' . ~$a . '\n';
    echo '$a << $b: ' . $a << $b . '\n'; // left shift  
    echo '$a >> $b: ' . $a >> $b . '\n'; // arithmetic right shift
    echo 'zerofill($a, $b): ' . zerofill($a, $b) . '\n'; // logical right shift
}






Ada: with Ada.Text_IO, Interfaces;
use  Ada.Text_IO, Interfaces;

procedure Bitwise is
   subtype Byte is Unsigned_8;
   package Byte_IO is new Ada.Text_Io.Modular_IO (Byte);

   A : constant Byte    := 2#00011110#;
   B : constant Byte    := 2#11110100#;
   X : constant Byte    := 128;
   N : constant Natural := 1;
begin
   Put ("A and B = ");  Byte_IO.Put (Item => A and B, Base => 2);  New_Line;
   Put ("A or B  = ");  Byte_IO.Put (Item => A or B,  Base => 2);  New_Line;
   Put ("A xor B = ");  Byte_IO.Put (Item => A xor B, Base => 2);  New_Line;
   Put ("not A   = ");  Byte_IO.Put (Item => not A,   Base => 2);  New_Line;
   New_Line (2);
   Put_Line (Unsigned_8'Image (Shift_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right_Arithmetic (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Right (X, N)));
end Bitwise;
Task: Bitwise operations






PHP: function bitwise($a, $b)
{
    function zerofill($a,$b) { 
        if($a>=0) return $a>>$b;
        if($b==0) return (($a>>1)&0x7fffffff)*2+(($a>>$b)&1); // this line shifts a 0 into the sign bit for compatibility, replace with "if($b==0) return $a;" if you need $b=0 to mean that nothing happens
        return ((~$a)>>$b)^(0x7fffffff>>($b-1)); 

    echo '$a AND $b: ' . $a & $b . '\n';
    echo '$a OR $b: ' . $a | $b . '\n';
    echo '$a XOR $b: ' . $a ^ $b . '\n';
    echo 'NOT $a: ' . ~$a . '\n';
    echo '$a << $b: ' . $a << $b . '\n'; // left shift  
    echo '$a >> $b: ' . $a >> $b . '\n'; // arithmetic right shift
    echo 'zerofill($a, $b): ' . zerofill($a, $b) . '\n'; // logical right shift
}




COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. bitwise-ops.

       DATA DIVISION.
       LOCAL-STORAGE SECTION.
       01  a                       PIC 1(32) USAGE BIT.
       01  b                       PIC 1(32) USAGE BIT.

       01  result                  PIC 1(32) USAGE BIT.
       01  result-disp             REDEFINES result PIC S9(9) COMP.

       LINKAGE SECTION.
       01  a-int                   USAGE BINARY-LONG.
       01  b-int                   USAGE BINARY-LONG.

       PROCEDURE DIVISION USING a-int, b-int.
           MOVE FUNCTION BOOLEAN-OF-INTEGER(a-int, 32) TO a
           MOVE FUNCTION BOOLEAN-OF-INTEGER(b-int, 32) TO b

           COMPUTE result = a B-AND b
           DISPLAY "a and b is " result-disp

           COMPUTE result = a B-OR b
           DISPLAY "a or b is " result-disp

           COMPUTE result = B-NOT a
           DISPLAY "Not a is " result-disp

           COMPUTE result = a B-XOR b
           DISPLAY "a exclusive-or b is " result-disp

           

           GOBACK
           .
Task: Stem-and-leaf plot



C#: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        const string 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";

        int[] ints = data.Split(' ').Select(int.Parse).ToArray();

        StemAndLeafPlot(ints);

        Console.ReadKey();
    }

    public static void StemAndLeafPlot(int[] arr)
    {
        int stemMax = arr.Max() / 10;
        int stemMin = arr.Min() / 10;
        Array.Sort(arr);

        for (int i = stemMin; i <= stemMax; i++)
        {
            Console.Write("{0,3} | ", i);
            foreach (var t in arr)
            {
                if (t < 10 * i)
                    continue;
                if (t >= 10 * (i + 1))
                    break;
                Console.Write("{0} ", t % 10);
            }
            Console.WriteLine("");
        }
    }
}









Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Gnat.Heap_Sort_G;
procedure stemleaf is
	data : array(Natural Range <>) of Integer := (
	0,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); 

	procedure Move (from, to : in Natural) is
	begin data(to) := data(from);
	end Move;
	
	function Cmp (p1, p2 : Natural) return Boolean is
	begin return data(p1)<data(p2);
	end Cmp;
	
	package Sorty is new GNAT.Heap_Sort_G(Move,Cmp);
	min,max,p,stemw: Integer;
begin
	Sorty.Sort(data'Last);
	min := data(1); 
	max := data(data'Last);
	stemw := Integer'Image(max)'Length;
	p := 1;
	for stem in min/10..max/10 loop
		put(stem,Width=>stemw); put(" |");
		Leaf_Loop:
			while data(p)/10=stem loop
				put(" "); put(data(p) mod 10,Width=>1);
				exit Leaf_loop when p=data'Last;
				p := p+1;
		end loop Leaf_Loop;
		new_line;
	end loop;
end stemleaf;
Task: Stem-and-leaf plot

C: #include <stdio.h>
#include <stdlib.h>

int icmp(const void *a, const void *b)
{
	return *(const int*)a < *(const int*)b ? -1 : *(const int*)a > *(const int*)b;
}

void leaf_plot(int *x, int len)
{
	int i, j, d;

	qsort(x, len, sizeof(int), icmp);

	i = x[0] / 10 - 1;
	for (j = 0; j < len; j++) {
		d = x[j] / 10;
		while (d > i) printf("%s%3d |", j ? "\n" : "", ++i);
		printf(" %d", x[j] % 10);
	}
}

int main()
{
	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 };

	leaf_plot(data, sizeof(data)/sizeof(data[0]));

	return 0;
}











Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Gnat.Heap_Sort_G;
procedure stemleaf is
	data : array(Natural Range <>) of Integer := (
	0,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); 

	procedure Move (from, to : in Natural) is
	begin data(to) := data(from);
	end Move;
	
	function Cmp (p1, p2 : Natural) return Boolean is
	begin return data(p1)<data(p2);
	end Cmp;
	
	package Sorty is new GNAT.Heap_Sort_G(Move,Cmp);
	min,max,p,stemw: Integer;
begin
	Sorty.Sort(data'Last);
	min := data(1); 
	max := data(data'Last);
	stemw := Integer'Image(max)'Length;
	p := 1;
	for stem in min/10..max/10 loop
		put(stem,Width=>stemw); put(" |");
		Leaf_Loop:
			while data(p)/10=stem loop
				put(" "); put(data(p) mod 10,Width=>1);
				exit Leaf_loop when p=data'Last;
				p := p+1;
		end loop Leaf_Loop;
		new_line;
	end loop;
end stemleaf;
Task: Stem-and-leaf plot


C++: #include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>

const int dataset[] = {
     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
};
const int datasize = sizeof(dataset) / sizeof(dataset[0]);

int main()
{
    typedef std::pair<int,int> StemLeaf;
    std::vector<StemLeaf> stemplot;

    for (int i = 0; i < datasize; ++i)
    {
        stemplot.push_back(StemLeaf(dataset[i] / 10, dataset[i] % 10));
    }

    std::sort(stemplot.begin(), stemplot.end());  

    int lo = stemplot.front().first; 
    int hi = stemplot.back().first; 

    for (std::vector<StemLeaf>::iterator itr = stemplot.begin(); lo <= hi; ++lo)
    {
        std::cout << std::setw(2) << lo << " |"; 

        
        for ( ; itr != stemplot.end() && itr->first == lo; ++itr)
        {
            std::cout << " " << itr->second; 
        }

        std::cout << std::endl;
    }
}










Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Gnat.Heap_Sort_G;
procedure stemleaf is
	data : array(Natural Range <>) of Integer := (
	0,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); 

	procedure Move (from, to : in Natural) is
	begin data(to) := data(from);
	end Move;
	
	function Cmp (p1, p2 : Natural) return Boolean is
	begin return data(p1)<data(p2);
	end Cmp;
	
	package Sorty is new GNAT.Heap_Sort_G(Move,Cmp);
	min,max,p,stemw: Integer;
begin
	Sorty.Sort(data'Last);
	min := data(1); 
	max := data(data'Last);
	stemw := Integer'Image(max)'Length;
	p := 1;
	for stem in min/10..max/10 loop
		put(stem,Width=>stemw); put(" |");
		Leaf_Loop:
			while data(p)/10=stem loop
				put(" "); put(data(p) mod 10,Width=>1);
				exit Leaf_loop when p=data'Last;
				p := p+1;
		end loop Leaf_Loop;
		new_line;
	end loop;
end stemleaf;
Task: Stem-and-leaf plot




Go: package main

import (
    "fmt"
    "sort"
    "strconv"
    "strings"
)

var data = `12 127 28 42` 

func main() {
    
    m := make(map[int][]string)
    for _, s := range strings.Fields(data) {
        if len(s) == 1 {
            m[0] = append(m[0], s)
        } else if i, err := strconv.Atoi(s[:len(s)-1]); err == nil {
            m[i] = append(m[i], s[len(s)-1:])
        } else {
            panic("non numeric data")
        }
    }
    
    s := make([]int, len(m))
    var i int
    for k := range m {
        s[i] = k
        i++
    }
    sort.Ints(s)
    
    for k := s[0]; ; k++ {
        v := m[k]
        sort.Strings(v)
        fmt.Printf("%2d | %s\n", k, strings.Join(v, " "))
        if k == s[len(s)-1] {
            break
        }
    }
}








Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Gnat.Heap_Sort_G;
procedure stemleaf is
	data : array(Natural Range <>) of Integer := (
	0,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); 

	procedure Move (from, to : in Natural) is
	begin data(to) := data(from);
	end Move;
	
	function Cmp (p1, p2 : Natural) return Boolean is
	begin return data(p1)<data(p2);
	end Cmp;
	
	package Sorty is new GNAT.Heap_Sort_G(Move,Cmp);
	min,max,p,stemw: Integer;
begin
	Sorty.Sort(data'Last);
	min := data(1); 
	max := data(data'Last);
	stemw := Integer'Image(max)'Length;
	p := 1;
	for stem in min/10..max/10 loop
		put(stem,Width=>stemw); put(" |");
		Leaf_Loop:
			while data(p)/10=stem loop
				put(" "); put(data(p) mod 10,Width=>1);
				exit Leaf_loop when p=data'Last;
				p := p+1;
		end loop Leaf_Loop;
		new_line;
	end loop;
end stemleaf;
Task: Stem-and-leaf plot





Java: import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class StemAndLeaf {
	private static 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 };
	
	public static Map<Integer, List<Integer>> createPlot(int... data){
		Map<Integer, List<Integer>> plot = new TreeMap<Integer, List<Integer>>();
		int highestStem = -1; 
		for(int datum:data){
			int leaf = datum % 10;
			int stem = datum / 10; 
			if(stem > highestStem){
				highestStem = stem;
			}
			if(plot.containsKey(stem)){
				plot.get(stem).add(leaf);
			}else{
				LinkedList<Integer> list = new LinkedList<Integer>();
				list.add(leaf);
				plot.put(stem, list);
			}
		}
		if(plot.keySet().size() < highestStem + 1  ){
			for(int i = 0; i <= highestStem; i++){
				if(!plot.containsKey(i)){
					LinkedList<Integer> list = new LinkedList<Integer>();
					plot.put(i, list);
				}
			}
		}
		return plot;
	}
	
	public static void printPlot(Map<Integer, List<Integer>> plot){
		for(Map.Entry<Integer, List<Integer>> line : plot.entrySet()){
			Collections.sort(line.getValue());
			System.out.println(line.getKey() + " | " + line.getValue());
		}
	}
	
	public static void main(String[] args){
		Map<Integer, List<Integer>> plot = createPlot(data);
		printPlot(plot);
	}
}







Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Gnat.Heap_Sort_G;
procedure stemleaf is
	data : array(Natural Range <>) of Integer := (
	0,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); 

	procedure Move (from, to : in Natural) is
	begin data(to) := data(from);
	end Move;
	
	function Cmp (p1, p2 : Natural) return Boolean is
	begin return data(p1)<data(p2);
	end Cmp;
	
	package Sorty is new GNAT.Heap_Sort_G(Move,Cmp);
	min,max,p,stemw: Integer;
begin
	Sorty.Sort(data'Last);
	min := data(1); 
	max := data(data'Last);
	stemw := Integer'Image(max)'Length;
	p := 1;
	for stem in min/10..max/10 loop
		put(stem,Width=>stemw); put(" |");
		Leaf_Loop:
			while data(p)/10=stem loop
				put(" "); put(data(p) mod 10,Width=>1);
				exit Leaf_loop when p=data'Last;
				p := p+1;
		end loop Leaf_Loop;
		new_line;
	end loop;
end stemleaf;
Task: Stem-and-leaf plot







Python: from collections import namedtuple
from pprint import pprint as pp
from math import floor

Stem = namedtuple('Stem', 'data, leafdigits')

data0 = Stem((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),
             1.0)

def stemplot(stem):
    d = []
    interval = int(10**int(stem.leafdigits))
    for data in sorted(stem.data):
        data = int(floor(data))
        stm, lf = divmod(data,interval)
        d.append( (int(stm), int(lf)) )
    stems, leafs = list(zip(*d))
    stemwidth = max(len(str(x)) for x in stems)
    leafwidth = max(len(str(x)) for x in leafs)
    laststem, out = min(stems) - 1, []
    for s,l in d:
        while laststem < s:
            laststem += 1
            out.append('\n%*i |' % ( stemwidth, laststem))
        out.append(' %0*i' % (leafwidth, l))
    out.append('\n\nKey:\n Stem multiplier: %i\n X | Y  =>  %i*X+Y\n'
               % (interval, interval))
    return ''.join(out)

if __name__ == '__main__':
    print( stemplot(data0) )





Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Gnat.Heap_Sort_G;
procedure stemleaf is
	data : array(Natural Range <>) of Integer := (
	0,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); 

	procedure Move (from, to : in Natural) is
	begin data(to) := data(from);
	end Move;
	
	function Cmp (p1, p2 : Natural) return Boolean is
	begin return data(p1)<data(p2);
	end Cmp;
	
	package Sorty is new GNAT.Heap_Sort_G(Move,Cmp);
	min,max,p,stemw: Integer;
begin
	Sorty.Sort(data'Last);
	min := data(1); 
	max := data(data'Last);
	stemw := Integer'Image(max)'Length;
	p := 1;
	for stem in min/10..max/10 loop
		put(stem,Width=>stemw); put(" |");
		Leaf_Loop:
			while data(p)/10=stem loop
				put(" "); put(data(p) mod 10,Width=>1);
				exit Leaf_loop when p=data'Last;
				p := p+1;
		end loop Leaf_Loop;
		new_line;
	end loop;
end stemleaf;
Task: Stem-and-leaf plot

C: #include <stdio.h>
#include <stdlib.h>

int icmp(const void *a, const void *b)
{
	return *(const int*)a < *(const int*)b ? -1 : *(const int*)a > *(const int*)b;
}

void leaf_plot(int *x, int len)
{
	int i, j, d;

	qsort(x, len, sizeof(int), icmp);

	i = x[0] / 10 - 1;
	for (j = 0; j < len; j++) {
		d = x[j] / 10;
		while (d > i) printf("%s%3d |", j ? "\n" : "", ++i);
		printf(" %d", x[j] % 10);
	}
}

int main()
{
	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 };

	leaf_plot(data, sizeof(data)/sizeof(data[0]));

	return 0;
}
Task: Stem-and-leaf plot



C#: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        const string 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";

        int[] ints = data.Split(' ').Select(int.Parse).ToArray();

        StemAndLeafPlot(ints);

        Console.ReadKey();
    }

    public static void StemAndLeafPlot(int[] arr)
    {
        int stemMax = arr.Max() / 10;
        int stemMin = arr.Min() / 10;
        Array.Sort(arr);

        for (int i = stemMin; i <= stemMax; i++)
        {
            Console.Write("{0,3} | ", i);
            foreach (var t in arr)
            {
                if (t < 10 * i)
                    continue;
                if (t >= 10 * (i + 1))
                    break;
                Console.Write("{0} ", t % 10);
            }
            Console.WriteLine("");
        }
    }
}
Task: Stem-and-leaf plot


C++: #include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>

const int dataset[] = {
     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
};
const int datasize = sizeof(dataset) / sizeof(dataset[0]);

int main()
{
    typedef std::pair<int,int> StemLeaf;
    std::vector<StemLeaf> stemplot;

    for (int i = 0; i < datasize; ++i)
    {
        stemplot.push_back(StemLeaf(dataset[i] / 10, dataset[i] % 10));
    }

    std::sort(stemplot.begin(), stemplot.end());  

    int lo = stemplot.front().first; 
    int hi = stemplot.back().first; 

    for (std::vector<StemLeaf>::iterator itr = stemplot.begin(); lo <= hi; ++lo)
    {
        std::cout << std::setw(2) << lo << " |"; 

        
        for ( ; itr != stemplot.end() && itr->first == lo; ++itr)
        {
            std::cout << " " << itr->second; 
        }

        std::cout << std::endl;
    }
}