ejercicio.pas(10,9) Fatal: Syntax error, ":" expected but "(" found - pascal

I receive an error
ejercicio.pas(10,9) Fatal: Syntax error, ":" expected but "(" found
in the following code
procedure darUnPaso(var x, y: integer; vx, vy: integer);
begin
x := x + vx;
y := y + vy;
end;
function estanChocando(x1, y1, x2, y2: integer): boolean;
(* precondition: ( sqr(x2-x1))+sqr(y2-y1)) mayor o igual a 0*)
var
true: boolean;
dist((x1, y1), (x2, y2)): real;
begin
true:= (dist((x1, y1), (x2, y2))) < RADIO + RADIO;
(dist((x1, y1);(x2, y2))):= sqrt((sqr(x2-x1))+(sqr(y2-y1)));
if true then
estanChocando:= true
end;
function esPosicionValida(x1, y1: integer): boolean;
var true:
boolean;
begin
true:=((RADIO<=x1<=ANCHO-RADIO) and
(RADIO<=y1<=ALTO-RADIO));
if true then
esPosicionValida:=true
end;
function predecirColision(x1,y1,vx1,vy1,x2,y2,vx2,vy2: integer): integer;
(*ninguna de las pelotas està detenida y ambas pelotas
estàn en posiciones vàlidas*)
var true: boolean;
begin
true := estanChocando(x1,y1,x2,y2) or ((esPosicionValida(x1,y1) and (esPosicionValida(x2,y2));
if true then
predecirColision:=darUnPaso
else
predecirColision:=-1
end;
I can't find the error in the code in
dist((x1, y1), (x2, y2)): real;

You get the syntax error message because you have declared
dist((x1, y1), (x2, y2)): real;
to be a variable, but it doesn't follow the rules of variable declaration which should be
name: type;
you can not have parenteses, commas nor spaces in a name.
Perhaps you meant to write
dist: real;
and then use it like
dist := sqrt((sqr(x2-x1))+(sqr(y2-y1)));
You have also declared the variable
true: boolean;
which is in conflict with language defined constant True
I suggest you change that to something like:
b: boolean;
which then can be assigned a value like
b := True; // or b := any boolean formula

Related

What's wrong with max values of GotoXY function?

My code is doing strange thing when MoveMessage procedure takes max values(ScreenWidth and ScreenHeight). So the "Hello, world" string moves to the right down corner and doing strange thing, but the max and min values are set correctly.
Can someone tell me where is my mistake.
program HelloCrt;
uses crt;
const
Message = 'Hello, world!';
procedure GetKey(var code: integer);
var
c: char;
begin
c := Readkey;
if c = #0 then
begin
c := Readkey;
code := -ord(c);
end
else
code := ord(c);
end;
procedure ShowMessage(x, y: integer; msg: string);
begin
GotoXY(x, y);
write(msg);
GotoXY(1, 1);
end;
procedure HideMessage(x, y: integer; msg: string);
var
len, i: integer;
begin
len := Length(msg);
GotoXY(x, y);
for i := 1 to len do
write(' ');
GotoXY(1, 1);
end;
procedure MoveMessage(var x, y: integer; msg: string; dx, dy: integer);
begin
HideMessage(x, y, msg);
x := x + dx;
y := y + dy;
if x > (ScreenWidth - length(msg)) then
x := (ScreenWidth - length(msg) + 1);
if x < 1 then
x := 1;
if y > ScreenHeight then
y := ScreenHeight;
if y < 1 then
y := 1;
ShowMessage(x, y, msg);
end;
var
CurX, CurY: integer;
c: integer;
begin
clrscr;
CurX := (ScreenWidth - length(Message)) div 2;
CurY := ScreenHeight div 2;
ShowMessage(CurX, CurY, Message);
while true do
begin
Getkey(c);
if c > 0 then
break;
case c of
-75:
MoveMessage(CurX, CurY, Message, -1, 0);
-77:
MoveMessage(CurX, CurY, Message, 1, 0);
-72:
MoveMessage(CurX, CurY, Message, 0, -1);
-80:
MoveMessage(CurX, CurY, Message, 0, 1);
end
end;
clrscr;
end.

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.

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
)
);

Pascal: how to pass arrays to subprograms?

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.

Resources