Recent

Author Topic: Stem and Leaf Plot and Integer Array Sort  (Read 737 times)

Boleeman

  • Hero Member
  • *****
  • Posts: 722
Stem and Leaf Plot and Integer Array Sort
« on: September 26, 2023, 03:25:42 pm »
Hi All.

Was trying to convert some CSharp code from Rosetta code to Lazarus to create a Stem and Leaf Plot.
CSharp has sort array function = Sort(arr);. How do I implement this in Lazarus

Below is my code so far:

Code: Pascal  [Select][+][-]
  1. program stemandleaf;
  2.  
  3. uses
  4.   SysUtils, Math;
  5.  
  6. procedure StemAndLeafPlot(arr: array of Integer);
  7. var
  8.   stemMax, stemMin, i, t: Integer;
  9. begin
  10.   stemMax := MaxIntValue(arr) div 10;
  11.   stemMin := MinIntValue(arr) div 10;
  12.   Sort(arr);
  13.   for i := stemMin to stemMax do
  14.   begin
  15.     WriteLn(Format('%3d | ', [i]));
  16.     for t in arr do
  17.     begin
  18.       if t < 10 * i then
  19.         Continue;
  20.       if t >= 10 * (i + 1) then
  21.         Break;
  22.       Write(Format('%d ', [t mod 10]));
  23.     end;
  24.     WriteLn('');
  25.   end;
  26. end;
  27.  
  28. var
  29.   data: string;
  30.   ints: array of Integer;
  31. begin
  32.   data := '12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 ' +
  33.     '125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 ' +
  34.     '105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 ' +
  35.     '114 126 53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 ' +
  36.     '115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 ' +
  37.     '105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 ' +
  38.     '133 45 120 30 127 31 116 146';
  39.   SetLength(ints, 0);
  40.   for var s in data.Split(' ') do
  41.     ints := ints + [StrToInt(s)];
  42.   StemAndLeafPlot(ints);
  43.   ReadLn;
  44. end.  

Thank you in advance.
« Last Edit: September 26, 2023, 03:37:17 pm by Boleeman »

Zvoni

  • Hero Member
  • *****
  • Posts: 2754
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018