Recent

Author Topic: Stem and Leaf Plot  (Read 8280 times)

Boleeman

  • Hero Member
  • *****
  • Posts: 658
Stem and Leaf Plot
« on: September 29, 2023, 05:19:06 am »
With the Stem and Leaf plot I was wondering how to include the in between 10's values in the stem

When there are no values there like for 10, 45, 55, 42 I get

1  |  0
4  |  5    2
5  |  5

What I wanted was the inclusion of 20's and 30's in the stem and the numbers in ascending order.

1  |  0
2  |
3  |

4  |  2    5
5  |  5

I think it has something to do with putting the numbers in an array, sorting them and finding the:

  stemMax := MaxIntValue(arr) div 10;
  stemMin := MinIntValue(arr) div 10;

so as to fill in the missing 10's values in the stem and having all the numbers sorted before being put into the plot. I thought of originally putting all the integers in a stringlist on a form and sorting them, then finding stemMin and stemMax to create the stem and lastly put the values in the leaf.
« Last Edit: October 15, 2023, 02:12:04 pm by Boleeman »

TRon

  • Hero Member
  • *****
  • Posts: 3176
Re: Operator not Overloaded Error How to fix?
« Reply #1 on: September 29, 2023, 05:32:14 am »
The error is in the line      if plot.stem in plot then
Code: [Select]
plot[i].stem is of type string while plot is an array of records of type TPlot.

That is never going to work. What is it that you are actually trying to accomplish with that particular if .. in construction ? e..g what do you expect it would do ?
All software is open source (as long as you can read assembler)

Boleeman

  • Hero Member
  • *****
  • Posts: 658
Re: Operator not Overloaded Error How to fix?
« Reply #2 on: September 29, 2023, 05:40:50 am »
I thought plot.stem was a string from the plot array which was contained in TPlot.

Wanted to enter some numbers separated by spaces and have them separate in stems and leafs.  Kinda really confused?
« Last Edit: September 29, 2023, 05:51:36 am by Boleeman »

TRon

  • Hero Member
  • *****
  • Posts: 3176
Re: Operator not Overloaded Error How to fix?
« Reply #3 on: September 29, 2023, 05:51:43 am »
I thought
Code: [Select]
plot[i].stem was a string from the plot array which was contained in TPlot. Kinda really confused?
For sure that
Code: [Select]
plot[i].stem is a string in a single TPlot item. It is just that the Plot variable is an array of TPlot. Array of TPlot <> string

The if..in construction works for enums.

So, I'll try ask again: what do you expect that the statement as written in your code would do ? If we know that then we are able to help you explain and solve whatever it is that you are trying to accomplish  :)

No matter if you are confused or not, if we are able to present you with what you expected then it perhaps becomes more clear for you ?

edit:
otherwise we can only guess and let me try take a shot in the dark. with the statement
Code: [Select]
if plot[i].stem in plot then
you expect that the stem string is searched for in the array of tplot and if the string exist in your array of plot that the then statement is executed ?
« Last Edit: September 29, 2023, 05:56:59 am by TRon »
All software is open source (as long as you can read assembler)

Boleeman

  • Hero Member
  • *****
  • Posts: 658
Re: Operator not Overloaded Error How to fix?
« Reply #4 on: September 29, 2023, 06:07:13 am »
Yes, expect that the stem string is searched for in the array of plot which is in Tplot if the string exist in your array of plot  the then statement is executed.


TRon

  • Hero Member
  • *****
  • Posts: 3176
Re: Operator not Overloaded Error How to fix?
« Reply #5 on: September 29, 2023, 06:18:05 am »
Yes, expect that the stem string is searched for in the array of plot which is in Tplot if the string exist in your array of plot  the then statement is executed.
Ok, that makes it clear.

Well, I have good news and bad news. The bad news is that FPC is not smart enough to figure that out on its own. You need to search the list yourself.

The good news is you can encapsulate that into a operator.

Code: Pascal  [Select][+][-]
  1. operator in (const A: string; const B: array of TPlot): boolean;
  2. var
  3.   C: TPlot;
  4. begin
  5.   result := false;
  6.   for C in B do
  7.   begin
  8.     if C.stem = A then exit(true);
  9.   end;
  10. end;
  11.  

Then your code should be able to compile. It might be you need to declare a type for the array (e.g. type TPlotArray = array of TPlot) and use that to make sure that all variables are of the correct type.

Edit: it just occurred to me that you might perhaps want to have the operator as record operator, see https://www.freepascal.org/docs-html/ref/refse63.html because my implementation is a global one.
« Last Edit: September 29, 2023, 06:22:00 am by TRon »
All software is open source (as long as you can read assembler)

Boleeman

  • Hero Member
  • *****
  • Posts: 658
Re: Operator not Overloaded Error How to fix?
« Reply #6 on: September 29, 2023, 06:20:48 am »
So the code is:

Code: Pascal  [Select][+][-]
  1. program project1;
  2. uses crt, sysutils;
  3.  
  4. type
  5.   TPlot = record
  6.     stem: string;
  7.     leaf: string;
  8.   end;
  9.   plot= array of TPlot;
  10.  
  11.   operator in (const A: string; const B: array of TPlot): boolean;
  12.   var
  13.     C: TPlot;
  14.   begin
  15.     result := false;
  16.     for C in B do
  17.     begin
  18.       if C.stem = A then exit(true);
  19.     end;
  20.   end;
  21.  
  22.  
  23. var
  24. //plot: array of TPlot;
  25. input: string;
  26. lines: array of string;
  27. num, leaf, stem: integer;
  28. i: integer;
  29.  
  30. begin
  31.  
  32.   clrscr;
  33.  
  34.   write('Enter input: ');
  35.   readln(input);
  36.  
  37.   lines := input.Split(#10);
  38.   SetLength(plot, Length(lines));
  39.  
  40.   for i := 0 to Length(lines) - 1 do
  41.   begin
  42.     num := StrToInt(lines[i]);
  43.     leaf := Abs(num mod 10);
  44.     stem := num div 10;
  45.  
  46.     if (num < 0) and (num > -10) then
  47.       stem := -0;
  48.  
  49.     plot[i].stem := IntToStr(stem);
  50.  
  51.     if plot[i].stem in Tplot then
  52.       plot[i].leaf := plot[i].leaf + '  ' + IntToStr(leaf)
  53.     else
  54.       plot[i].leaf := IntToStr(leaf);
  55.  
  56.     writeln(plot[i].stem, plot[i].leaf);
  57.   end;
  58.  
  59.   readln;
  60. end.
« Last Edit: September 29, 2023, 06:30:07 am by Boleeman »

TRon

  • Hero Member
  • *****
  • Posts: 3176
Re: Operator not Overloaded Error How to fix?
« Reply #7 on: September 29, 2023, 06:23:30 am »
Yes (*), you can see the operator implementation as a 'normal' function/procedure so should be located at the same position as those. Ofc you need to have the operator declared before making actual use of it. For more information for the solution I presented see https://www.freepascal.org/docs-html/ref/refse107.html#x227-25100015.6

edit: (*) actually no. That is not exactly what I meant with declaring a separate type for your array of TPlot . I meant:
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses crt, sysutils;
  6.  
  7.  
  8. type
  9.   TPlot = record
  10.     stem: string;
  11.     leaf: string;
  12.   end;
  13.  
  14.   TPlotArray = array of TPlot;
  15.  
  16. var
  17.   plot: TPlotArray;
  18.   minput: string;
  19.   lines: array of string;
  20.   num, leaf, stem: integer;
  21.   i: integer;
  22.  
  23. operator in (const A: string; const B: TPlotArray): boolean;
  24. var
  25.   C: TPlot;
  26. begin
  27.   result := false;
  28.   for C in B do
  29.   begin
  30.     if C.stem = A then exit(true);
  31.   end;
  32. end;
  33.  
  34.  
  35. begin
  36.   clrscr;
  37.  
  38.   write('Enter input: ');
  39.   readln(minput);
  40.  
  41.   lines := minput.Split([#10]);
  42.   SetLength(plot, Length(lines));
  43.  
  44.   for i := 0 to Length(lines) - 1 do
  45.   begin
  46.     num := StrToInt(lines[i]);
  47.     leaf := Abs(num mod 10);
  48.     stem := num div 10;
  49.  
  50.     if (num < 0) and (num > -10) then
  51.       stem := -0;
  52.  
  53.     plot[i].stem := IntToStr(stem);
  54.  
  55.     if plot[i].stem in plot then
  56.       plot[i].leaf := plot[i].leaf + '  ' + IntToStr(leaf)
  57.     else
  58.       plot[i].leaf := IntToStr(leaf);
  59.  
  60.     writeln(plot[i].stem, plot[i].leaf);
  61.   end;
  62.  
  63.   readln;
  64. end.
  65.  
« Last Edit: September 29, 2023, 06:30:12 am by TRon »
All software is open source (as long as you can read assembler)

TRon

  • Hero Member
  • *****
  • Posts: 3176
Re: Operator not Overloaded Error How to fix?
« Reply #8 on: September 29, 2023, 06:31:27 am »
Bump because I edited previous answer to show what I meant.
All software is open source (as long as you can read assembler)

Boleeman

  • Hero Member
  • *****
  • Posts: 658
Re: Operator not Overloaded Error How to fix?
« Reply #9 on: September 29, 2023, 06:33:23 am »
Actually now I see. Sorry.
« Last Edit: September 29, 2023, 06:35:51 am by Boleeman »

TRon

  • Hero Member
  • *****
  • Posts: 3176
Re: Operator not Overloaded Error How to fix?
« Reply #10 on: September 29, 2023, 06:35:44 am »
Now I get  project1.lpr(38,17) Error: Variable identifier expected    How is var plot defined in
See my edit in post #7  :)
All software is open source (as long as you can read assembler)

Boleeman

  • Hero Member
  • *****
  • Posts: 658
Re: Operator not Overloaded Error How to fix?
« Reply #11 on: September 29, 2023, 06:39:57 am »
Yes now it compiles but when I run it I get an econvertor error

Invalid integer

TRon

  • Hero Member
  • *****
  • Posts: 3176
Re: Operator not Overloaded Error How to fix?
« Reply #12 on: September 29, 2023, 06:46:51 am »
Yes now it compiles but when I run it I get an econvertor error

Invalid integer
Yups !

But that has nothing to do with the original topic. What do you input ? what is stored in Lines ? can each line be converted into a integer ? Try to add some writeln's
All software is open source (as long as you can read assembler)

Boleeman

  • Hero Member
  • *****
  • Posts: 658
Re: Operator not Overloaded Error How to fix?
« Reply #13 on: September 29, 2023, 06:47:58 am »
12 32 34 54 34

Just numbers separated with a space

I assumed minput.Split([#10])    was splitting at spaces
« Last Edit: September 29, 2023, 06:50:24 am by Boleeman »

TRon

  • Hero Member
  • *****
  • Posts: 3176
Re: Operator not Overloaded Error How to fix?
« Reply #14 on: September 29, 2023, 06:51:55 am »
12 32 34 54 34

Just numbers separated with a space

I assumed minput.Split([#10])    was splitting at spaces

Almost correct. See for example https://www.ascii-code.com/

#10 is linefeed. You should be thinking of #32  :)
All software is open source (as long as you can read assembler)

 

TinyPortal © 2005-2018