Recent

Author Topic: Dimensioning the Dynamic Arrays  (Read 10538 times)

ArtLogi

  • Full Member
  • ***
  • Posts: 194
Dimensioning the Dynamic Arrays
« on: January 15, 2016, 03:12:23 pm »
Hello I encountered a beginners problem.

How do I increase ( or decrease ) the dimensions of dynamic array of type.  This is the main question.
So how I can declare dynamically:
Code: Pascal  [Select][+][-]
  1. Array(One dimensionsional of certain length)
to Nth dímension array.
Code: Pascal  [Select][+][-]
  1. Array(first dimension of certain length, second dimension of certain length, Nth dimension of certain length)

The use is typical basic implementation of loading a data set from a typical textfile (to allow easy user editing ie. in excel and just for education for me)

FPC wiki does give example of setting length of list (1 dimensional array), but speaks nothing about adding dimensions and length of new dimensions. What I tried to look on the RTL and FCL there is no SetDimensions type of function. I assume this is piece of cake, but how you do it.  :)


There seems to be a logic inconsistency in general, since the FPC mixes 1 and 0 based indexing depending on the function. :(
« Last Edit: January 15, 2016, 03:46:19 pm by ArtLogi »
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Dimensioning the Dynamic Arrays
« Reply #1 on: January 15, 2016, 04:33:35 pm »
Use SetLength to set dimension of dynamic array. SetLength(DynamicArray, n, n, ..) for nth dimension.

ArtLogi

  • Full Member
  • ***
  • Posts: 194
Re: Dimensioning the Dynamic Arrays
« Reply #2 on: January 15, 2016, 05:03:18 pm »
Thx... I'm still making some mistake on my test code. With comments it works.

This results without comments: "Error: Wrong number of parameters specified for call to "SetLength"" in
Quote
FreePascal IDE for Win32 for i386
         Target CPU: i386
    Version 1.0.12 2014/03/06
     (Compiler Version 2.6.4)
        (Debugger GDB 7.4)
    Copyright (C) 1998-2012 by
Code: Pascal  [Select][+][-]
  1. program arrytest;
  2. {$mode objfpc}
  3.  
  4. var
  5.         arry: array of string;
  6. begin
  7.         setlength(arry,4{,1});
  8.         arry[3]:= 'three - element four';
  9.         arry[0]:= 'zero - first element';
  10.         {arry[0,0]:= 'zero, zero'}
  11.         writeln('length(arry,2) and call arry(3)',arry[3]{,arry[0,0]});
  12.         readln;
  13. end.
Also adding function call below leads to error? What I understood it should be a part of RTLs System unit.
Code: Pascal  [Select][+][-]
  1. DynArrayDim(arry)
it should return integer, but it doesn't matter if I pipe the result to integer variable or try to use it inside the writeln command, compiler still gives an error "DynArrayDim not found" or something on those lines.

I'm a bit confused.
« Last Edit: January 15, 2016, 05:50:42 pm by ArtLogi »
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
Re: Dimensioning the Dynamic Arrays
« Reply #3 on: January 15, 2016, 06:08:59 pm »
How do I increase ( or decrease ) the dimensions of dynamic array of type.

Number of dimensions for dynamic array sets once at declaration type or variable and cannot be changed at runtime.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8770
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Dimensioning the Dynamic Arrays
« Reply #4 on: January 15, 2016, 06:14:59 pm »
There seems to be a logic inconsistency in general, since the FPC mixes 1 and 0 based indexing depending on the function. :(
There was NEVER 1 based indexing on arrays, only 0 based and up-to-you based ones.
Thx... I'm still making some mistake on my test code. With comments it works.
You call SetLength with as many parameters corresponding to the number of dimensions as well. You only declare `array of string`, hence you can only set its first dimension. If you declare it as `array of array of string` you can set first and second dimension. More dimensions is the same.
Also adding function call below leads to error? What I understood it should be a part of RTLs System unit.
Code: Pascal  [Select][+][-]
  1. DynArrayDim(arry)
it should return integer, but it doesn't matter if I pipe the result to integer variable or try to use it inside the writeln command, compiler still gives an error "DynArrayDim not found" or something on those lines.

I'm a bit confused.
Works fine:
Code: Pascal  [Select][+][-]
  1. uses
  2.   typinfo;
  3.  
  4. type
  5.   tintdynarr = array of array of integer;
  6.  
  7. var
  8.   a: tintdynarr;
  9. begin
  10.   writeln(dynarraydim(typeinfo(a))); // displays 2
  11. end.
  12.  
Please read the description in documentation before calling any function, it clearly states the parameter is a type info of a dynamic array.
« Last Edit: January 15, 2016, 06:17:17 pm by Leledumbo »

ArtLogi

  • Full Member
  • ***
  • Posts: 194
Re: Dimensioning the Dynamic Arrays
« Reply #5 on: January 15, 2016, 06:49:03 pm »
Ohh, thx both of you. Now I get what were wrong..  :)


[/quote]
There seems to be a logic inconsistency in general, since the FPC mixes 1 and 0 based indexing depending on the function. :(
There was NEVER 1 based indexing on arrays, only 0 based and up-to-you based ones.
I did mean that some functions (all of them what I have used so far) are indexing from 1 and also the basic array what I have understood is indexed from 1. Why the dynamic array do brake this rule Idk, but it seems to me a logical inconsistency and now I need always to try to figure if there is an another part of the system that uses 0 index and not 1 index.  :-\

Thx... I'm still making some mistake on my test code. With comments it works.
You call SetLength with as many parameters corresponding to the number of dimensions as well. You only declare `array of string`, hence you can only set its first dimension. If you declare it as `array of array of string` you can set first and second dimension. More dimensions is the same.
So is it possible to make an array that is truly dynamic in both ways not just one (the dimensions)? I assume there is some way that is widely used to do it, but tbh I don't understand these extended datatypes in pascal yet, much confusing to figure out the do and do nots in those.

Also adding function call below leads to error? What I understood it should be a part of RTLs System unit.
Code: Pascal  [Select][+][-]
  1. DynArrayDim(arry)
it should return integer, but it doesn't matter if I pipe the result to integer variable or try to use it inside the writeln command, compiler still gives an error "DynArrayDim not found" or something on those lines.

I'm a bit confused.
Works fine:
Code: Pascal  [Select][+][-]
  1. uses
  2.   typinfo;
  3.  
  4. type
  5.   tintdynarr = array of array of integer;
  6.  
  7. var
  8.   a: tintdynarr;
  9. begin
  10.   writeln(dynarraydim(typeinfo(a))); // displays 2
  11. end.
  12.  
Please read the description in documentation before calling any function, it clearly states the parameter is a type info of a dynamic array.
I did read the documentation, unfortunately that type just doesn't say anything to me and I assumed it translates to put your "variable name here" and in general trying to crack the basics through these nonexistent FPC documentation is pain, fortunately I have old Delphi 1&2 book and somewhere is TB6 book (misplaced atm.). Those unfortunately end where I start what comes to basic manipulation of data, jumping to other tasks like printing, database applications and GUI programming (TP5..7 book were pretty good though, but it is misplaced atm.).
« Last Edit: January 15, 2016, 07:23:37 pm by ArtLogi »
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Dimensioning the Dynamic Arrays
« Reply #6 on: January 15, 2016, 08:12:34 pm »
I did mean that some functions (all of them what I have used so far) are indexing from 1 and also the basic array what I have understood is indexed from 1. Why the dynamic array do brake this rule Idk, but it seems to me a logical inconsistency and now I need always to try to figure if there is an another part of the system that uses 0 index and not 1 index.  :-\
Uhm, no idea where you got that notion from, but depending on what you meant, you are a bit off.

 _You_ determine with which index a static array starts. If you declare static array with 1 as starting index, then it is you at fault (Me for instance always use zero based arrays).

Are you perhaps mixing pascal strings with dynamic arrays ? e.g. in a Pascal string the length of the string is usually stored in the zero'th index

Maybe this is able to help you further?
Code: Pascal  [Select][+][-]
  1. program arrayplay;
  2.  
  3.  
  4. {$MODE OBJFPC}{$H+}
  5.  
  6.  
  7. procedure PlayWithDynamicArrays;
  8. var
  9.   // Declare variable with 1 dimension
  10.   OneDimension    : array of integer;
  11.  
  12.   // Declare variable with 2 dimensions
  13.   TwoDimensions   : array of array of integer;
  14.  
  15.   // Declare variable with 3 dimensions
  16.   ThreeDimensions : array of array of array of integer;
  17.  
  18.   // declare some index variables to acces individual items inside array
  19.   i,j,k           : integer;
  20. begin
  21.   // =================
  22.   // Initialize arrays
  23.   // =================
  24.  
  25.   // Initialize 1 dimensional array (5 items)
  26.   SetLength(OneDimension, 5);      
  27.  
  28.   // Initialize 2 dimensional array (5*4 = 20 items)
  29.   SetLength(TwoDimensions, 5, 4);
  30.  
  31.   // Initialize 3 dimensional array (5*4*3 = 60 items)
  32.   SetLength(ThreeDimensions, 5, 4, 3);
  33.  
  34.  
  35.   // =========================
  36.   // Display array information
  37.   // =========================
  38.  
  39.   // Display size of array
  40.   // ---------------------
  41.   // Note that when using function SizeOf() on dynamic arrays that this
  42.   // function will actually return the size of an individual item from the
  43.   // array, expressed in bytes.
  44.   // It will not return the actual bytes occupied by the complete array.
  45.   WriteLn;
  46.   WriteLn('SizeOf(integer)                = ', SizeOf(Integer)         , ' bytes');
  47.  
  48.   WriteLn('SizeOf(OneDimension)           = ', SizeOf(OneDimension)    , ' bytes');
  49.   WriteLn('SizeOf(TwoDimensions)          = ', SizeOf(TwoDimensions)   , ' bytes');
  50.   WriteLn('SizeOf(ThreeDimensions)        = ', SizeOf(ThreeDimensions) , ' bytes');
  51.  
  52.   // Display size of dimensions and individual items
  53.   // -----------------------------------------------
  54.   // Note that it doesn't matter which and how many indices are used: the size
  55.   // in bytes will be the same for each entry/dimension in the array.
  56.   WriteLn;
  57.   WriteLn('SizeOf(OneDimension[1])        = ', SizeOf(OneDimension[1])       , ' bytes');
  58.  
  59.   WriteLn('SizeOf(TwoDimensions[1])       = ', SizeOf(TwoDimensions[1])      , ' bytes');
  60.   WriteLn('SizeOf(TwoDimensions[1,1])     = ', SizeOf(TwoDimensions[1,1])    , ' bytes');
  61.  
  62.   WriteLn('SizeOf(ThreeDimensions[1])     = ', SizeOf(ThreeDimensions[1])    , ' bytes');
  63.   WriteLn('SizeOf(ThreeDimensions[1,1])   = ', SizeOf(ThreeDimensions[1,1])  , ' bytes');
  64.   WriteLn('SizeOf(ThreeDimensions[1,1,1]) = ', SizeOf(ThreeDimensions[1,1,1]), ' bytes');
  65.  
  66.   // Determine/display length of array
  67.   // ---------------------------------
  68.   // Note that only the length of the first dimension of the array is being
  69.   // displayed here
  70.   WriteLn;
  71.   WriteLn('Length(OneDimension)           = ', Length(OneDimension)   , ' items');
  72.   WriteLn('Length(TwoDimensions)          = ', Length(TwoDimensions)  , ' items');
  73.   WriteLn('Length(ThreeDimensions)        = ', Length(ThreeDimensions), ' items');
  74.  
  75.   // Determine/display length of dimensions of array
  76.   // -----------------------------------------------
  77.   // Note that the dimensions of the array are taken into account here.
  78.   //
  79.   // Note2 that it is not valid to invoke function Length() on a individual
  80.   // item inside the array, as function Length() only operates on arrays.
  81.   //
  82.   // So, Length(ThreeDimensions[1,1,1]) will result in a type mismatch -> an
  83.   // indidividual item is an integer and its size is determined by SizeOf (see
  84.   // also code above). (unless, of course, an individual item in the array is
  85.   // also an array itself).
  86.   WriteLn;
  87.   WriteLn('Length(OneDimension)           = ', Length(OneDimension)        , ' items');
  88.   WriteLn('Length(TwoDimensions[1])       = ', Length(TwoDimensions[1])    , ' items');
  89.   WriteLn('Length(ThreeDimensions[1,1])   = ', Length(ThreeDimensions[1,1]), ' items');
  90.  
  91.  
  92.   // ================================================
  93.   // Determine "range" of arrray (e.g. valid indices)
  94.   // ================================================
  95.   // Note that this will only determine the range of the first dimension
  96.   WriteLn;
  97.  
  98.   WriteLn('Low(OneDimension)              = ', Low(OneDimension));
  99.   WriteLn('High(OneDimension)             = ', High(OneDimension));
  100.  
  101.   WriteLn('Low(TwoDimensions)             = ', Low(TwoDimensions));
  102.   WriteLn('High(TwoDimensions)            = ', High(TwoDimensions));
  103.  
  104.   WriteLn('Low(ThreeDimensions)           = ', Low(ThreeDimensions));
  105.   WriteLn('High(ThreeDimensions)          = ', High(ThreeDimensions));
  106.  
  107.   // Determine the range of other dimensions (where applicable)
  108.   // ----------------------------------------------------------
  109.   WriteLn;
  110.  
  111.   WriteLn('Low(OneDimension)              = ', Low(OneDimension));
  112.   WriteLn('High(OneDimension)             = ', High(OneDimension));
  113.  
  114.   WriteLn('Low(TwoDimensions[1])          = ', Low(TwoDimensions[1]));
  115.   WriteLn('High(TwoDimensions[1])         = ', High(TwoDimensions[1]));
  116.  
  117.   WriteLn('Low(ThreeDimensions[1,1])      = ', Low(ThreeDimensions[1,1]));
  118.   WriteLn('High(ThreeDimensions[1,1])     = ', High(ThreeDimensions[1,1]));
  119.  
  120.  
  121.   // =================================
  122.   // Intialize arrays with some values
  123.   // =================================
  124.  
  125.   // Set values for 1 dimensional array
  126.   for i := Low(OneDimension) to High(OneDimension)
  127.     do OneDimension[i] := i;
  128.  
  129.   // Set values for 2 dimensional array
  130.   for i := Low(TwoDimensions) to High(TwoDimensions) do
  131.     for j := Low(TwoDimensions[i]) to High(TwoDimensions[i]) do
  132.       TwoDimensions[i,j] := i*j;
  133.  
  134.   // Set values for 3 dimensional array
  135.   for i := Low(ThreeDimensions) to High(ThreeDimensions) do
  136.     for j := Low(ThreeDimensions[i]) to High(ThreeDimensions[i]) do
  137.       for k := Low(ThreeDimensions[i,j]) to High(ThreeDimensions[i,j]) do
  138.         ThreeDimensions[i,j,k] := i*j*k;
  139.  
  140.  
  141.   // ===============================
  142.   // Display values stored in arrays
  143.   // ===============================
  144.  
  145.   // Display 1 dimensional array values
  146.   WriteLn;
  147.   WriteLn('Values inside one dimensional array:');
  148.   for i := Low(OneDimension) to High(OneDimension) do
  149.     WriteLn('Item[', i, '] := ', OneDimension[i]);
  150.  
  151.   // Display 2 dimensional array values
  152.   WriteLn;
  153.   WriteLn('Values inside two dimensional array:');
  154.   for i := Low(TwoDimensions) to High(TwoDimensions) do
  155.     for j := Low(TwoDimensions[i]) to High(TwoDimensions[i]) do
  156.       WriteLn('Item[', i, ',' , j, '] := ', TwoDimensions[i, j]);
  157.  
  158.   // Display 3 dimensional array values
  159.   WriteLn;
  160.   WriteLn('Values inside three dimensional array:');
  161.   for i := Low(TwoDimensions) to High(TwoDimensions) do
  162.     for j := Low(TwoDimensions[i]) to High(TwoDimensions[i]) do
  163.       for k := Low(ThreeDimensions[i,j]) to High(ThreeDimensions[i,j]) do
  164.         WriteLn('Item[', i, ',' , j, ',' , k, '] := ', ThreeDimensions[i,j,k]);
  165.  
  166.   // ==========================================
  167.   // Clear Arrays (e.g. release used resources)
  168.   // ==========================================
  169.  
  170.   SetLength(OneDimension, 0);
  171.   SetLength(TwoDimensions, 0, 0);
  172.   SetLength(ThreeDimensions, 0, 0, 0);
  173. end;
  174.  
  175.  
  176.  
  177. Procedure PlayWithStaticArray;
  178. Var
  179.   OneDimension  : array[15..30] of Word;
  180.   i             : Integer;
  181. begin
  182.  
  183.   // ==========================================
  184.   // Display size of array (expressed in bytes)
  185.   // ==========================================
  186.   // Note that with a static declared array, function SizeOf() will return the
  187.   // number of bytes actually occupied.
  188.   //
  189.   // This is different when applied to dynamic arrays, which will return
  190.   // the size of an individual item inside the array instead. This is because
  191.   // function SizeOf() determines these sizes at compile time, not runtime.
  192.   WriteLn;
  193.   WriteLn('SizeOf(Word)                  = ', SizeOf(Word)            , ' bytes');
  194.   WriteLn('SizeOf(OneDimension)          = ', SizeOf(OneDimension)    , ' bytes');
  195.  
  196.   // Display size of a single item in array
  197.   // --------------------------------------
  198.   WriteLn;
  199.   WriteLn('SizeOf(OneDimension[15])      = ', SizeOf(OneDimension[15])  , ' bytes');
  200.  
  201.   // Determine length of array
  202.   // -------------------------
  203.   // Note that the length of the first dimension is determined.
  204.   WriteLn;
  205.   WriteLn('Length(OneDimension)          = ', Length(OneDimension), ' items');
  206.  
  207.   // ========================
  208.   // determine range of array
  209.   // ========================
  210.  
  211.   WriteLn('Low(OneDimension)             = ', Low(OneDimension));
  212.   WriteLn('High(OneDimension)            = ', High(OneDimension));
  213.  
  214.   // ================================
  215.   // Intialize array with some values
  216.   // ================================
  217.  
  218.   // Set values for 1 dimensional array
  219.   for i := Low(OneDimension) to High(OneDimension)
  220.     do OneDimension[i] := i;
  221.  
  222.  
  223.   // =======================
  224.   // Display values in array
  225.   // =======================
  226.  
  227.   // Display 1 dimensional array values
  228.   WriteLn;
  229.   WriteLn('Values inside one dimensional array:');
  230.   for i := Low(OneDimension) to High(OneDimension) do
  231.     WriteLn('Item[', i, '] := ', OneDimension[i]);
  232. end;
  233.  
  234.  
  235. procedure PassAlong(AnArray: Array of Integer);
  236. var
  237.   i: Integer;
  238. begin
  239.   // Display length of array
  240.   WriteLn;
  241.   WriteLn('Length(AnArray)          = ', Length(AnArray), ' items');
  242.  
  243.   // Display range of array
  244.   WriteLn;
  245.   WriteLn('Low(AnArray)             = ', Low(AnArray));
  246.   WriteLn('High(AnArray)            = ', High(AnArray));
  247.  
  248.   // display array values
  249.   WriteLn;
  250.   WriteLn('Array values are:');
  251.   For i := Low(AnArray) to High(AnArray) do
  252.     WriteLn('Item[', i, '] := ', AnArray[i]);
  253. end;
  254.  
  255.  
  256. Procedure PassAlongStaticArray;
  257. var
  258.   AnArray   : array[200..205] of Integer;
  259.   i         : Integer;
  260. begin
  261.   // Display length of array
  262.   WriteLn;
  263.   WriteLn('Length(AnArray)          = ', Length(AnArray), ' items');
  264.  
  265.   // Display range of array
  266.   WriteLn;
  267.   WriteLn('Low(AnArray)             = ', Low(AnArray));
  268.   WriteLn('High(AnArray)            = ', High(AnArray));
  269.  
  270.   // fill array with values
  271.   For i := Low(AnArray) to High(AnArray) do AnArray[i] := i;
  272.  
  273.   // display array values
  274.   WriteLn;
  275.   WriteLn('Array values are:');
  276.   For i := Low(AnArray) to High(AnArray) do
  277.     WriteLn('Item[', i, '] := ', AnArray[i]);
  278.  
  279.   // Pass along the array to another function/procedure
  280.   // Note the different numbers used for index)
  281.   WriteLn;
  282.   WriteLn('Passing the array to another function/procedure');
  283.   WriteLn('Note the difference in index values used');
  284.   PassAlong(AnArray);
  285. end;
  286.  
  287.  
  288. begin
  289.   WriteLn('====================================');
  290.   WriteLn('Playing with arrays');
  291.   WriteLn('====================================');
  292.   WriteLn;
  293.  
  294.   WriteLn('--------------');
  295.   WriteLn('Dynamic arrays');
  296.   WriteLn('--------------');
  297.   PlayWithDynamicArrays;
  298.   WriteLn;
  299.  
  300.   WriteLn('------------');
  301.   WriteLn('Static array');
  302.   WriteLn('------------');
  303.   PlayWithStaticArray;
  304.   WriteLn;
  305.  
  306.   WriteLn('-------------');
  307.   WriteLn('Pass an array');
  308.   WriteLn('-------------');
  309.   PassAlongStaticArray;
  310.   WriteLn;
  311.  
  312.   WriteLn('====================================');
  313.   WriteLn('done');
  314.   WriteLn('====================================');
  315. end.
  316.  

edit: source: rectified some (wrong) comments, removed some typo's
edit2: extended comments, (hopefully) removed all typo's and added extra example passing an array to a procedure.
« Last Edit: January 16, 2016, 09:57:35 am by molly »

ArtLogi

  • Full Member
  • ***
  • Posts: 194
Re: Dimensioning the Dynamic Arrays
« Reply #7 on: January 15, 2016, 09:08:29 pm »
Well that  ;D I call spoonfeeding. Thank you, I must go through it and test compile it.

PS. That is (with the first quick skimming) so good that it might be really helpfull and extend the information value of this fpc wiki article considerably:
http://wiki.freepascal.org/Dynamic_array

PPS. It would fit also as a example program in FPC console IDE.. It is really good.
« Last Edit: January 15, 2016, 09:59:51 pm by ArtLogi »
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
Re: Dimensioning the Dynamic Arrays
« Reply #8 on: January 16, 2016, 08:07:09 am »
I would like to remind that dynamic arrays may be non-rectangular.
Example:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. type
  4.   TArr2D = array of array of Integer;
  5.  
  6. procedure  FillArr2D(Arr: TArr2D);
  7. var
  8.   i, j: Integer;
  9. begin
  10.   for i:=Low(Arr) to High(Arr) do
  11.     for j:=Low(Arr[i]) to High(Arr[i]) do
  12.       Arr[i][j] := i*10 + j;
  13. end;
  14.  
  15. procedure  ShowArr2D(Arr: TArr2D);
  16. var
  17.   i, j: Integer;
  18. begin
  19.   for i:=Low(Arr) to High(Arr) do
  20.   begin
  21.     Write('Arr[', i, ']:  ');
  22.     for j:=Low(Arr[i]) to High(Arr[i]) do
  23.       Write(Arr[i][j]:2, '  ');
  24.     WriteLn;
  25.   end;
  26. end;
  27.  
  28. procedure Pause;
  29. begin
  30.   WriteLn;
  31.   //WriteLn('Press Enter to continue...');
  32.   Write('...');
  33.   ReadLn;
  34.   WriteLn;
  35. end;
  36.  
  37.  
  38. var
  39.   Arr2D: TArr2D;
  40. begin
  41.   SetLength(Arr2D, 5, 3);  // resize on two dimensions
  42.     WriteLn('Rectangular dynamic array after resizing:');
  43.     WriteLn('-----------------------------------------');
  44.     ShowArr2D(Arr2D);
  45.     Pause;
  46.  
  47.   FillArr2D(Arr2D);
  48.     WriteLn('Rectangular dynamic array after filling:');
  49.     WriteLn('-----------------------------------------');
  50.     ShowArr2D(Arr2D);
  51.     Pause;
  52.  
  53.   SetLength(Arr2D, 10);  // resize on one dimension
  54.     WriteLn('Non-rectangular dynamic array after resizing on first dimension:');
  55.     WriteLn('----------------------------------------------------------------');
  56.     ShowArr2D(Arr2D);
  57.     Pause;
  58.  
  59.   SetLength(Arr2D[0], 9);
  60.   SetLength(Arr2D[1], 7);
  61.   SetLength(Arr2D[2], 6);
  62.   SetLength(Arr2D[3], 9);
  63.   SetLength(Arr2D[4], 5);
  64.   SetLength(Arr2D[5], 8);
  65.   SetLength(Arr2D[6], 5);
  66.   SetLength(Arr2D[7], 3);
  67.   SetLength(Arr2D[8], 1);
  68.   SetLength(Arr2D[9], 7);
  69.     WriteLn('Non-rectangular dynamic array after individual resizing on second dimension:');
  70.     WriteLn('----------------------------------------------------------------------------');
  71.     ShowArr2D(Arr2D);
  72.     Pause;
  73.  
  74.   FillArr2D(Arr2D);
  75.     WriteLn('Non-rectangular dynamic array after filling:');
  76.     WriteLn('--------------------------------------------');
  77.     ShowArr2D(Arr2D);
  78.     Pause;
  79. end.
  80.  

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Dimensioning the Dynamic Arrays
« Reply #9 on: January 16, 2016, 10:03:53 am »
I would like to remind that dynamic arrays may be non-rectangular.
Example:
That's a very nice complementary example FTurtle. Thank you, as indeed i didn't handle those inside my example

TBH, sometimes using different dim'ed arrays makes my head hurt  :-[

Since you was passing an array, i extended my original posted example with showing how that works with a static array (effectively turning it into a dynamic array from the called procedure/function 's point of view).


Well that  ;D I call spoonfeeding. Thank you, I must go through it and test compile it.
Hmz, in that case i must have done something terribly wrong  ::)

On the other hand, you still have to apply things to your own situation  ;)

But seriously, i did see that there was lacking some examples on existing tutors/wiki pages (it is however discussed on these forums, but i understand it is sometimes hard to find).

Quote
PS. That is (with the first quick skimming) so good that it might be really helpfull and extend the information value of this fpc wiki article considerably:
Thank you for having such confidence in my posted example.

Unfortunately, i'm currently a bit occupied. (as in: it's on my to do list. As many other things FPC/lazarus related i might add).

The article you referred to (thanks for the link) has some really strange references which leads to an even more stranger tutorial not effectively handling the topic of dynamic arrays and multiple dimensions (or i must have overlooked something).

In that regards, my example is a bit strange as it doesn't really fit in any of the topics about arrays as the example handles multiple topics in one go.

Quote
PPS. It would fit also as a example program in FPC console IDE.. It is really good.
I've adjusted my initial example, and removed some typo's (hopefully all of them now), extended the comments and added an example showing the aspect of what happens when you pass a static array to another procedure/function.

Feel free to do whatever one sees fit to do with my example (preferably remove errors  :P )

Actually, the whole point of the example (as also shown by FTurtle) is to always be careful with making assumptions about dynamic arrays and use the low(), high() and length() functions to determine the index-range and size of a dimension in the array (unless you are sure they are static for your situation -> but then, why use dynamic arrays to begin with...).

Have fun, and please have a good look at FTurtles example as well.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8770
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Dimensioning the Dynamic Arrays
« Reply #10 on: January 16, 2016, 02:45:19 pm »
I did mean that some functions (all of them what I have used so far) are indexing from 1 and also the basic array what I have understood is indexed from 1. Why the dynamic array do brake this rule Idk, but it seems to me a logical inconsistency and now I need always to try to figure if there is an another part of the system that uses 0 index and not 1 index.  :-\
Name them. Length, Copy, to name a few, don't care what the base index is.
I did read the documentation, unfortunately that type just doesn't say anything to me and I assumed it translates to put your "variable name here" and in general trying to crack the basics through these nonexistent FPC documentation is pain
Don't assume, it's documented. External resources are also available.
« Last Edit: January 16, 2016, 07:16:49 pm by Leledumbo »

ArtLogi

  • Full Member
  • ***
  • Posts: 194
Re: Dimensioning the Dynamic Arrays
« Reply #11 on: January 16, 2016, 05:08:00 pm »
I did mean that some functions (all of them what I have used so far) are indexing from 1 and also the basic array what I have understood is indexed from 1. Why the dynamic array do brake this rule Idk, but it seems to me a logical inconsistency and now I need always to try to figure if there is an another part of the system that uses 0 index and not 1 index.  :-\
Name them. Length, Copy, to name a few, don't care what the base index is.
strutils.PosEx comes to my mind atm. It does start indexing from 1.
Element 1 to element n .. instead of position 0 to position n-1, I would assume.

I have no further interest to go further of this discussion, it is what it is.

I did read the documentation, unfortunately that type just doesn't say anything to me and I assumed it translates to put your "variable name here" and in general trying to crack the basics through these nonexistent FPC documentation is pain
Don't assume, it's documented. External resources are also available.
It seems that it were there, it is easy when you know what to search. Delphi is not Lazarus/FPC. Nor FPC is not GNU pascal. There is differences and as a beginner I don't see and or know them.  I also finally found my old TP book which have whole TP7 RTL described and from basics to advanced level of TP7 opened.
« Last Edit: January 16, 2016, 05:16:38 pm by ArtLogi »
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8770
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Dimensioning the Dynamic Arrays
« Reply #12 on: January 16, 2016, 07:22:58 pm »
Name them. Length, Copy, to name a few, don't care what the base index is.
strutils.PosEx comes to my mind atm. It does start indexing from 1.
Element 1 to element n .. instead of position 0 to position n-1, I would assume.
[/quote]
OK, so as molly said. No problem.
It seems that it were there, it is easy when you know what to search. Delphi is not Lazarus/FPC. Nor FPC is not GNU pascal. There is differences and as a beginner I don't see and or know them. 
Well, yeah. You need time to familiarize.
I also finally found my old TP book which have whole TP7 RTL described and from basics to advanced level of TP7 opened.
Sounds like a good candidate on the programmer's (or user's?) guide? Hmm...obviously our documentation doesn't include a tutorial which covers this leveling, but what do you think about the language reference? Does it suffice?

ArtLogi

  • Full Member
  • ***
  • Posts: 194
Re: Dimensioning the Dynamic Arrays
« Reply #13 on: January 16, 2016, 09:56:46 pm »
I also finally found my old TP book which have whole TP7 RTL described and from basics to advanced level of TP7 opened.
Sounds like a good candidate on the programmer's (or user's?) guide? Hmm...obviously our documentation doesn't include a tutorial which covers this leveling, but what do you think about the language reference? Does it suffice?
Yep, the book is pretty good and going through the basic "programming thinking" a bit also in the beginning as an introduction. Published in -95 it seems, 380 pages (so not that broad, but it does have 1.44 floppy which is by now lost somewhere). Not english.

Can you open a bit more what you mean about language reference? *going through the documentation listings* It seems that I have skipped the language reference in the documentation list, while I have opened the above two (which were mainly under the hood low level compiler and hardware related documents) and then jumped to RTL and FCL. Interesting enough I were searching most of the information of the language reference from the Programmers Guide, but it only did give a mouthfull of compiler directives . Then of course I also have been lost in the wiki, which is pretty much a mess. :)

The language reference what I look at it, is rather nice, but If this would be the first time learning a language I would quickly close it and hide it somewhere (like I did with the before mentioned TP7 book with all written descriptions of mathematic unions and bitwise operations, when I were 10yrs old)..
It is heavy to read and slow to digest with all the referencing from the past with phrases like ("Unlike Delphi, Free Pascal does not support", "Just as in Turbo Pascal, Free Pascal supports..", "...the format is however the same as the one used by the GNU Pascal Compiler...") which makes a reader to wonder If he should now open also a handbook of those. Second thing what I did notice of it is that it mixes all levels of knowledge (well it is language reference after all). etc. etc. Like it does say at the middle of the first chapter " It does not, however, give a detailed explanation of the Pascal language" Which ofcourse drops its value as learning reference for beginner (even more so if you happen to be a beginner of programming as a whole). 

What comes to RTL and FCL library reference materials they do in my opinion lack of basic beginner level information (some more than others depending the type of function). System unit have proper descriptions (assuming it is for the basic decryption key for the other units that are using system functions).

On the other hand the de facto standard structure of all beginners books are boring with their 40 pages of "hello world" examples.

The best "book for pascal/freepascal language" is the "Object Pascal Tutorial Updated version of Tao Yue's Pascal Tutorial", but the whole wiki is lacking of "(free)pascal programming handbook from beginner's to advanced" that would be freedomain and truly wiki based.

I have been making a braindump and basic skeleton down to notepad for my amusement this evening actually. Doomed project since no inside understanding of pascal here.  ::)
Quote
Beginner's wikihandbook for Free Pascal

1. Introduction

   1.1 Baselines of this wikihandbook
      1.1.1 For the reader.
         - Beginner
         - If some previous programming experience..
      1.1.2 For the writer, editor and content contributors.
      
   1.2 Pascal language and its long history in short.
   
      1.2.1 The beginning 1968 - 1990.
         - Wirths book of pascal, origins in ALGOL
         - First standard
         - Object pascal in Apple corporation
         - Borland Turbo Pascal
         - Borland Turbo Vision (forefather of Delphi?)
         - 2nd Standard and its extension
         
      1.2.2 Delphi, defining GUI programming
         - Working hybrid of 3GL and 4GL concepts.
         
      1.2.3 Free Pascal (pascal + objectpascal)
         - Early years
         - Turn of millenia
         - Marriage with Lazarus project
         
   1.3 List of key Acronyms (partial)
   
      1.3.1 Specific for this wikihandbook (should be used where possible)       // shown at index page
         - <CMD>      : Programming structure specific only for Command Line programs
         - <ObjFPC>   : Programming structure only in Object (Free)Pascal
         - <TP7>    : Programming structure specific only DOS - Turbo Pascal 7
         - <NIX>      : Programming structure For unix based systems // Linux, BSD etc..?
         - <WINx>   : Programming structure for Windows only
         - <NA>      : Internal use in compiler, don't use.
         
      1.3.2 In Free Pascal
         - FPC   : Free pascal compiler
         - FV   : FreeVision, partial free substitute for TurboVision
         - GUI   : Graphical User Interface
         - TP6   : Borland Turbo Pascal
         - TV   : Borland Turbo Vision      
         
2 The general basics
   - Good to know, widens thinking.
   
   2.1 Programming language - it is all about abstaction
      - Rapid GUI development (Lazarus) the fourth layer of abstraction (4GL??)   // Only in eyes of ObjP / Pascal
      - Object pascal the third layer of abstraction (3GL?)                   // scope of this book
      - Pascal the second layer of abstraction (3GL)                         // scope of this book
      - Assembly the first layer of abstraction (2GL)                        // Only at inline ASM part
      - Machine code - 00010010 (1GL)
         * The logical level
         * The electrical switch aka transistor level U = R * I
         
   2.2   Operating system - Another layer of abstaction
      - basic concept of programming and operating system
      - A few words of Direct accessing of devices
      
   2.3 Boolean is it true?
      - If it flies and have a feathers then it is a bird
      - Few words of boolean logic and relation to program branching and statemachines.
   
   2.3 Poet of the binaries
      - Remember what we discussed in chapter 2.1, it is all about abstraction, for computers all of these are just zeros and ones.
      - bits and pieces
         * Bit // true or false
         * Dear child have many names : nibble,nyble,half-byte,tetrade aka hex-digit
         * byte
         * halfword, word, doubleword
      - Basics of low level coding: BCD, Creycode, morse code etc.
      - 2^0+2^1+2^2 basic integer 101 to binary conversion.
      - Artefacts of real values
      - Ascii, codepages and UTF
      - Communication protocol idea example, ie. Ethernet/USB?.
      
3 The basics of the freepascal language
      - Look chapter 3.x for variables in freepascal
   3.1 Boolean operators
   3.2 Arithmetic operations
      - operating with strings
      - operating with other types
   3.3 Reserved words
   3.4 ...
   3.x Variables
      - ..
      - ..
      - Two dimensional arrows through basic trigonometric drawings.
   
X What after now?
   - language reference
   - programmer's Guide etc. etc.
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8770
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Dimensioning the Dynamic Arrays
« Reply #14 on: January 17, 2016, 06:30:31 pm »
Yep, the language reference does advertise itself as not a tutorial. You can buy the one on the left or the free Start Programming using Object Pascal. Another rather cheap alternative is Pascal Tutorial from tutorialspoint.com. I believe there are more but those should suffice.

 

TinyPortal © 2005-2018