Pascal: how to pass arrays to subprograms? - pascal

I made a program that has a procedure with an array as one of it's parameters
program something ;
const someArray: array[1..4] of integer = (1, 2, 3, 4);
procedure name(someArray: array; a, n: integer);
begin
....
end;
begin
name(someArray, x, y)
end.
After compiling the program I get an error:
Fatal: Syntax error, OF expected but ; found (function name() is highlighted)
Why isn't this program working?

You need to declare your parameter properly, as an open array. You find the bounds of the array by using Low and High.
Here's a (useless but working) example:
program Sample;
Var x,y: Integer;
const
SomeArray: array[1..4] of Integer = (1, 2, 3, 4);
procedure Name(const AnArray: Array of Integer; const A, B: Integer);
var
OutOne, OutTwo, i: Integer;
begin
for i := Low(AnArray) to High(AnArray) do
begin
OutOne := AnArray[i] * A;
OutTwo := AnArray[i] * B;
WriteLn('One: ', OutOne, ' Two: ', OutTwo);
end;
end;
begin
//x and y have to be initialised before use
Name(SomeArray, x, y);
ReadLn;
end.

To complement Ken White's answer, in straight up (pre-open array) Pascal, array on its own in a parameter definition is unsupported.
Instead you need to declare a specific array type to do what you are trying to do here.
Here's what that could look like:
program something ;
type
TMyArray = array[1..4] of integer;
const someArray: TMyArray = (1, 2, 3, 4);
procedure name(someArray: TMyArray; a, n: integer);
begin
....
end;
begin
name(someArray, x, y)
end.

Related

TStringList.CustomSort: Compare() with variables

I am trying to custom sort a TStringList by a column in a .CSV file. My code below works (slowly, about 14 seconds for 200,000+ lines):
function Compare(List: TStringList; Index1, Index2: Integer): Integer;
function ColStr(const Ch: Char; const S: String; First, Last: Integer): String;
var
p1, p2: Integer;
function GetPos(const N: Integer; Start: Integer = 1): Integer;
var
I, Len, Count: Integer;
begin
Result := 0;
Len := Length(S);
if (Len = 0) or (Start > Len) or (N < 1) then Exit;
Count := 0;
for I := Start to Len do begin
if S[I] = Ch then begin
Inc(Count);
if Count = N then begin
Result := I;
Exit;
end;
end;
end;
end;
begin
p1 := GetPos(4, 1); // 4 should be a variable
p2 := GetPos(5, 1); // 5 should be a variable
if Last = 0 then Result := Copy(S, p1 + 1, length(S)) else Result := Copy(S, p1 + 1, p2 - p1 - 1);
end;
begin
Result := AnsiCompareStr(ColStr(',', List[Index1], 0, 1), ColStr(',', List[Index2], 0, 1));
end;
What I would want to do is not have this hard-coded but (where commented "should be a variable" depending on which column to sort). I know I can't use:
function Form1.Compare(List: TStringList; Index1, Index2: Integer): Integer;
for inserting variables, as I get the error:
Incompatible types: 'method pointer and regular procedure'.
I have searched through SO looking for instances of this error but cannot find one that fits my question. I would appreciate any pointers in the right direction.
This has to be done with Delphi 7 and Windows 11.
TStringList.CustomSort() does not let you pass in extra parameters, and it does not accept class methods or anonymous procedures. But, what it does do is pass the actual TStringList itself to the callback, so I would suggest deriving a new class from TStringList to add extra fields to it, and then you can access those fields inside the callback, eg:
type
TMyStringList = class(TStringList)
public
Count1: Integer;
Count2: Integer;
end;
function Compare(List: TStringList; Index1, Index2: Integer): Integer;
...
p1 := GetPos(TMyStringList(List).Count1, 1);
p2 := GetPos(TMyStringList(List).Count2, 1);
...
begin
...
end;
...
List := TMyStringList.Create;
// fill List ...
List.Count1 := ...;
List.Count2 := ...;
List.CustomSort(Compare);
So you are performing searching for k-th occurence of Ch and substring creation at every comparison.
You can optimize this process - before sorting make list/array of stringlists, created from every string, separated by needed character - use DelimitedText.
Inside compare function just work with this array and column numbers - sadly, you have to define them as global variables in current unit (for example, after Form1: TForm1)

Pascal program sort of freezes after input

I am trying to input
Z: 20202020
X: 202020
C: 2020
But after that, my terminal doesn't show anything. Why is that? I have tried with some other input and it works just fine.
Edit: I realized that the same problem occurs whenever the X input is more than 5 digit.
program PowerTower;
Uses Math;
var
x,
z: real;
c: int64;
hasil : real;
function pembulatan(o: real): int64;
begin
pembulatan:= Round(o);
end;
function dopower(h,y: real):
real;
begin
dopower:= power(h,y)
end;
function ayam(a, b: real) : real;
begin
begin
if (b=0) then
ayam := 1
else
ayam:= dopower(a,ayam(a, b-1));
end;
end;
begin
readln(z);
readln(x);
readln(c);
hasil:= ayam(z, x);
writeln(pembulatan(hasil) mod c);
readln;
end.

One row, multiple inputs?

In pascal, as you might know, you can assign multiple variables values in one single line (as long as you have variables to catch them in):
var x, y, z: integer;
readln(x, y, z, etc...);
But what if i wanted to have only one variable, which would sequentially recieve those values which sit, practically, in the void?
Let me explain:
Number of values?
>3 (for example)
Insert values:
4 6 9
Now, these values, would periodically be assigned to 'a' for example, and once i'm done with the number 4, i want it to recieve 6, and 9 afterwards. Is there any way i can do this?
In Extended Pascal, ISO 10206, you can do the following:
program lists(input, output);
procedure processList(length: integer);
var
list: array[1..length] of integer;
var
n: integer;
begin
writeLn('Insert ', length:1, ' values:');
for n := 1 to length do
begin
read(list[n]);
end;
{ Here you can further process `list` }
end;
var
n: integer;
begin
writeLn('Number of values?');
readLn(n);
processList(n);
end.
A better implementation uses schemata for this task:
program lists(input, output);
type
list(length: integer) = array[1..length] of integer;
var
n: integer;
l: ^list;
begin
writeLn('Number of values?');
readLn(n);
{ dynamically allocate memory for `l` }
new(l, n);
writeLn('Insert values:');
for n := 1 to l^.length do
begin
read(l^[n]);
end;
dispose(l);
end.
At compile-time undiscriminated schema data types can only be handled via pointers, which is always a little unpleasant for us programmers, but it works.

Swap two numbers in pascal

I'm trying to swap two values but I'm getting a Warning: Local variable "temp" does not seem to be initialized. I want to do it similar as how I've done it. I'm compiling it from the command line with fpc Main.pas. I've tried initializing the temp variable to 0, but it still says Fatal: there were 3 errors compiling module, stopping.
'Main.pas'
Program Main;
procedure Main();
var
n1, n2: Integer;
begin
n1 := 5;
n2 := 10;
Swap(#n1, #n2);
writeln('n1 = ', n1);
writeln('n2 = ', n2);
end;
BEGIN
Main();
END.
'Number.pas'
unit Number;
interface
type
IntPtr = ^Integer;
procedure Swap(n1, n2: IntPtr);
implementation
procedure Swap(n1, n2: IntPtr);
var
temp: Integer;
begin
temp = n1^;
n1^ = n2^;
n2^ = temp;
end;
end.
As you have already discovered, you mixed up the assignment (:=) and equality (=) operators. Thus,
procedure Swap(A, B: PInteger);
var
Temp: Integer;
begin
Temp := A^;
A^ := B^;
B^ := Temp;
end;
where PInteger is defined as ^Integer, does the job:
Swap(#Val1, #Val2); // swaps integers Val1 and Val2
However, I suggest you do this slightly differently:
procedure Swap(var A, B: Integer);
var
Temp: Integer;
begin
Temp := A;
A := B;
B := Temp;
end;
Using a var parameter is more idiomatic and it allows you to write simply
Swap(Val1, Val2); // swaps integers Val1 and Val2
and it also gives you a bit more type safety.

Delphi Generics: TArray.Sort

I am just starting to get my feet wet with this .
PNode = ^TNode;
TNode = record
Obstacle : boolean;
Visited : boolean;
GCost : double; // Distance from Starting Node
HCost : double; // Distance from Target Node
x : integer;
y : integer;
vecNeighBour : array of PNode;
Parent : PNode;
end;
OnFormShow I fill the array :
SetLength(Node,4,4);
Image1.Height:=Length(Node)*50;
Image1.Width:=Length(Node)*50;
for x := Low(Node) to High(Node) do
for y := Low(Node) to High(Node) do
begin
Node[x,y].Obstacle:=false;
Node[x,y].Visited:=false;
Node[x,y].GCost:=infinity;
Node[x,y].HCost:=infinity;
Node[x,y].x:=x;
Node[x,y].y:=y;
end;
Now I would like to Sort this array by HCost , so I tried this .
TArray.Sort<TNode>(Node , TComparer<TNode>.Construct(
function(const Left, Right: TNode): double
begin
if Left.HCost > Right.HCost then
Result:=Left.HCost
else
Result:=Right.HCost;
end));
My knowledge is seriously lacking in this thing . I get a error from Delphi
"Incompatible types:
'System.Gerenics.Defaults.TComparison and 'Procedure'
What am I doing wrong here?
A comparison function has to return an Integer value. It is defined like so:
type
TComparison<T> = reference to function(const Left, Right: T): Integer;
Your function returns the wrong type.
function CompareDoubleInc(Item1, Item2: Double): Integer;
begin
if Item1=Item2 then begin
Result := 0;
end else if Item1<Item2 then begin
Result := -1
end else begin
Result := 1;
end;
end;
....
TArray.Sort<TNode>(
Node,
TComparer<TNode>.Construct(
function(const Left, Right: TNode): Integer
begin
Result := CompareDoubleInc(Left.HCost, Right.HCost);
end
)
);
Or if you want to make it even simpler you can delegate to the default double comparer:
TArray.Sort<TNode>(
Node,
TComparer<TNode>.Construct(
function(const Left, Right: TNode): Integer
begin
Result := TComparer<Double>.Default.Compare(Left.HCost, Right.HCost);
end
)
);

Resources