Pascal program sort of freezes after input - pascal

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.

Related

How to make loop in Pascal

I just started learning Pascal and have issue.
Need to create a program that outputs the following result:
Input real number and press enter!: 4
****
***
**
*
I've come this far, but not figure out what to do next.
program project1;
var
i, x:byte;
y:char;
begin
write('Ievadiet veselu skaitli: ');
readln(x);
y:='*';
for i:=x downto 1 do writeln(y:3);
readln;
end.
Two loops do have to be involved, but breaking out that inner loop functionality into a separate procedure can make it much easier to understand what's going on.
We can also declare y as const.
program project1;
const
y = '*';
var
i, x : byte;
procedure writeln_n_times(x : char; n : byte);
var
i : byte;
begin
for i := 1 to n do
write(x);
writeln();
end;
begin
write('Ievadiet veselu skaitli: ');
readln(x);
for i := x downto 1 do
writeln_n_times(y, i);
end.
use two loops:
program project1;
var
i, j,x:byte;
y:char;
begin
write('Ievadiet veselu skaitli: ');
readln(x);
y:='*';
for i:=x downto 1 do
begin
for j:=i-1 downto 1 do
begin
write(y);
end;
writeln();
end;
end.
output:
Ievadiet veselu skaitli: 7
******
*****
****
***
**
*

Pls correct these functions of float number (type: double) under binary form in delphi

I've written 2 functions to convert a float number into/from binary, but the result is incorrect. Please help me to find its bug. (I found some FAQ for this topic, but they was written for C/C++)
function MyFloatToBin(d: double): String;
var
d_ptr: ^Int64;
d_str: string;
i: Integer;
ch: char;
begin
d_ptr:= #d;
d_str:= '';
for i:= 0 to 63 do begin
if (d_ptr^ and (1 shl i)) > 0 then
ch:= '1'
else
ch:= '0';
d_str:= d_str + ch;
end;
Result:= 'F' + d_str;
end;
function MyBinToFloat: Double;
var
d_str: String;
i64: Int64;
d_ptr: ^double;
i, len: Integer;
begin
d_str:= pop;
len:= length(d_str);
if (pos('F', d_str) <> 1)and(len <> 65) then begin
push(d_str);
exit;
end;
i64:= 0;
for i:= 2 to len do
if d_str[i] = '1' then
i64:= i64 or (1 shl (i - 2));
d_ptr:= #i64;
Result:= d_ptr^;
end;
Using
temp: string;
f: double;
temp:= MyFloatToBin(pi);//pi = 3.14....
f:= MyBinToFloat(temp);//result at f is 0
I wonder the variable f should be 3.14... but..???
Please help me correct them.
Thanks
Your problem is essentially the mask computation. You do 1 shl I which gives a 32 bit value. You must do UInt64(1) shl I to get a 64 bit value.
Here is your code fixed:
function MyFloatToBinString(d: double): String;
var
VP : ^UInt64;
I : Integer;
begin
VP := #d;
Result := 'F';
for I := 63 downto 0 do begin
if (VP^ and (UInt64(1) shl I)) <> 0 then
Result := Result + '1'
else
Result := Result + '0';
end;
end;
function MyBinStringToFlat(S : String) : Double;
var
V : UInt64;
I : Integer;
J : Integer;
begin
if (Length(S) <> 65) or ((S[1] <> 'F') and (S[1] <> 'f')) then
raise Exception.Create('Invalid format');
V := 0;
for I := 65 downto 2 do begin
case S[I] of
'1': V := V or (UInt64(1) shl (65 - I));
'0': { Nothing to do };
else
raise Exception.Create('Invalid format');
end;
end;
Result := PDouble(#V)^;
end;
And if you want to test:
procedure TForm1.Button1Click(Sender: TObject);
var
V1 : Double;
V2 : Double;
S : String;
begin
V1 := 3.1416;
Memo1.Lines.Add(IntToHex(PUInt64(#V1)^));
S := MyFloatToBinString(V1);
Memo1.Lines.Add(S);
V2 := MyBinStringToFlat(S);
Memo1.Lines.Add(V2.ToString);
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.

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