Undefined array representing a math sequence [SOLVED] lazarus / 21-General ---------------------------------------------------------------------- pascal111 · 2021-05-05 16:31 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  type  b_array = array [1..10] of real;   sequence_a = record     body:b_array;    procedure pour_by_rule;  end;  in Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+}{$modeSwitch advancedRecords} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,  math; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Memo1: TMemo;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private   public   end;   b_array = array [1..10] of real;   sequence_a = record     body:b_array;    procedure pour_by_rule;  end; var  Form1: TForm1;  seq_1: sequence_a; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin end; procedure TForm1.Button1Click(Sender: TObject); var   i:low(b_array)..high(b_array);  s1,s2:string; begin   seq_1.pour_by_rule;   for i:=low(i) to high(i) do  begin   str(seq_1.body[i]:5:0,s1);  str(i,s2);  memo1.Append('Term '+s2+' = '+s1);   end;   end;  procedure sequence_a.pour_by_rule; var   i:low(body)..high(body); begin     for i:=low(body) to high(body) do  body[i]:=power(i,2); end; end. ---------------------------------------------------------------------- Handoko · 2021-05-05 16:37 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 ---------------------------------------------------------------------- pascal111 · 2021-05-05 16:52 "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? ---------------------------------------------------------------------- pascal111 · 2021-05-05 18:11 I fixed the "zero-bazed" index of a dynamic array like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+}{$modeSwitch advancedRecords} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,  math; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Memo1: TMemo;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private   public   end;     sequence_a = record      var    body:array of real;     procedure pour_by_rule;   end; const    min=0;    max=20-1; // max = i-1 var  Form1: TForm1;  seq_1: sequence_a; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin end; procedure TForm1.Button1Click(Sender: TObject); var   i:min..max;  i2:integer;  s1,s2:string; begin    seq_1.pour_by_rule;   for i:=low(i) to high(i) do  begin   str(seq_1.body[i]:5:0,s1);  i2:=i+1;  str(i2,s2);  memo1.Append('Term '+s2+' = '+s1);   end;   end;  procedure sequence_a.pour_by_rule; var   i:min..max; begin   setlength(body,max+1);   for i:=low(body) to high(body) do  body[i]:=power((i+1),2); end; end. ---------------------------------------------------------------------- jamie · 2021-05-05 20:57 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..