Recent

Author Topic: Undefined array representing a math sequence [SOLVED]  (Read 953 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Undefined array representing a math sequence [SOLVED]
« on: May 05, 2021, 06:31:03 pm »
In next code, I tried to simulate a math sequence but with a limited defined array consists of 10 terms. Can I do the representing of the sequence with undefined array, and how can I treat this array in determining which element of it I assign value or read its value while we have no defined index?


Code: Pascal  [Select][+][-]
  1.  
  2. type
  3.  
  4.  b_array = array [1..10] of real;
  5.  
  6.   sequence_a = record
  7.  
  8.     body:b_array;
  9.     procedure pour_by_rule;
  10.   end;
  11.  

in

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$modeSwitch advancedRecords}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  10.   math;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     Memo1: TMemo;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27.   b_array = array [1..10] of real;
  28.  
  29.   sequence_a = record
  30.  
  31.     body:b_array;
  32.     procedure pour_by_rule;
  33.   end;
  34.  
  35. var
  36.   Form1: TForm1;
  37.   seq_1: sequence_a;
  38.  
  39. implementation
  40.  
  41. {$R *.lfm}
  42.  
  43. { TForm1 }
  44.  
  45. procedure TForm1.FormCreate(Sender: TObject);
  46. begin
  47.  
  48. end;
  49.  
  50. procedure TForm1.Button1Click(Sender: TObject);
  51.  
  52. var
  53.  
  54.   i:low(b_array)..high(b_array);
  55.   s1,s2:string;
  56.  
  57. begin
  58.  
  59.   seq_1.pour_by_rule;
  60.  
  61.   for i:=low(i) to high(i) do
  62.   begin
  63.  
  64.   str(seq_1.body[i]:5:0,s1);
  65.   str(i,s2);
  66.   memo1.Append('Term '+s2+' = '+s1);
  67.  
  68.   end;
  69.  
  70.  
  71.  
  72. end;
  73.  
  74.  
  75. procedure sequence_a.pour_by_rule;
  76.  
  77. var
  78.  
  79.   i:low(body)..high(body);
  80.  
  81. begin
  82.  
  83.  
  84.  
  85.   for i:=low(body) to high(body) do
  86.   body[i]:=power(i,2);
  87.  
  88. end;
  89.  
  90. end.
  91.  
  92.  
« Last Edit: May 05, 2021, 10:30:03 pm by pascal111 »
La chose par la chose est rappelé.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Undefined array representing a math sequence
« Reply #1 on: May 05, 2021, 06:37:39 pm »
Use Dynamic Array:
https://freepascal.org/docs-html/ref/refsu14.html

Actually Free Pascal has a wide collection of containers:
https://wiki.freepascal.org/Data_Structures,_Containers,_Collections

TList is for general purpose usage:
https://wiki.freepascal.org/TList

But for storing strings, it is easier to use TStringList:
https://wiki.freepascal.org/TStringList
« Last Edit: May 05, 2021, 06:43:54 pm by Handoko »

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Undefined array representing a math sequence
« Reply #2 on: May 05, 2021, 06:52:52 pm »
"Dynamic arrays" seems a subject with details. Ok, as a beginning, I'll apply it in my program and use "setlength" procedure, but the the array will be "zero-based", how can I treat this problem while 1st term in math sequence has index 1 not zero, or I have to ignore element zero of the dynamic array?
La chose par la chose est rappelé.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Undefined array representing a math sequence
« Reply #3 on: May 05, 2021, 08:11:41 pm »
I fixed the "zero-bazed" index of a dynamic array like this:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$modeSwitch advancedRecords}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  10.   math;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     Memo1: TMemo;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27.  
  28.  
  29.   sequence_a = record
  30.  
  31.  
  32.     var
  33.     body:array of real;
  34.  
  35.     procedure pour_by_rule;
  36.  
  37.   end;
  38.  
  39. const
  40.     min=0;
  41.     max=20-1; // max = i-1
  42.  
  43. var
  44.   Form1: TForm1;
  45.   seq_1: sequence_a;
  46.  
  47. implementation
  48.  
  49. {$R *.lfm}
  50.  
  51. { TForm1 }
  52.  
  53. procedure TForm1.FormCreate(Sender: TObject);
  54. begin
  55.  
  56. end;
  57.  
  58. procedure TForm1.Button1Click(Sender: TObject);
  59.  
  60. var
  61.  
  62.   i:min..max;
  63.   i2:integer;
  64.   s1,s2:string;
  65.  
  66. begin
  67.  
  68.  
  69.   seq_1.pour_by_rule;
  70.  
  71.   for i:=low(i) to high(i) do
  72.   begin
  73.  
  74.   str(seq_1.body[i]:5:0,s1);
  75.   i2:=i+1;
  76.   str(i2,s2);
  77.   memo1.Append('Term '+s2+' = '+s1);
  78.  
  79.   end;
  80.  
  81.  
  82.  
  83. end;
  84.  
  85.  
  86. procedure sequence_a.pour_by_rule;
  87.  
  88. var
  89.  
  90.   i:min..max;
  91.  
  92. begin
  93.  
  94.   setlength(body,max+1);
  95.  
  96.   for i:=low(body) to high(body) do
  97.   body[i]:=power((i+1),2);
  98.  
  99. end;
  100.  
  101. end.
  102.  
  103.  
La chose par la chose est rappelé.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Undefined array representing a math sequence [SOLVED]
« Reply #4 on: May 05, 2021, 10:57:43 pm »
as a side note, you can also use a Array of Const, of just an Open Array of some type..

This allows you to make variable number of entries on the fly..

While inside the handling function you can examine the content length and the type for each entry..
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018