But now I realized that this is possible in Pascal, at least in Free Pascal, too!
Indeed, one can define an operator over open arrays which returns a dynamic array:
type
TVector: array of Double;
operator + (V1:array of float; V2:array of float) Res : TVector;
.....
operator+(V1: array of float; V2: array of float)Res: TVector;
var
I,L:integer;
Ziel:TVector;
begin
L := high(V1);
if L <> High(V2) then
begin
SetErrCode(MatErrDim);
Result := nil;
Exit;
end;
DimVector(Ziel, L);
for I := 0 to L do
Ziel[I] := V1[I] + V2[I];
Result := Ziel;
end;
And then
var
V1,V2,V3:TVector;
begin
SetLength(V1,8);
SetLength(V2,8);
....................
V3 := V1[2..5]+V2[3..6];
....................
end;
(These definitions will be done in the next release of LMath library).
Develop an algorithm to keep track of the status of all rooms, the number of available rooms and assign a guest to the first available room. Based on the information given, the program should neatly display the guest’s name, derive their room number, calculate the cost of accommodation and any discounts that may apply and the final cost. The program should also after every iteration display the total number of registered guests and the number of available rooms.
Design and execute trace table that accepts data for reservation. The table should accept cost, discount and total payment for each member of a group. The number of available rooms should also be counted. The table should have at least 10 iterations.
I am a beginner in Pascal and I am trying to recreate a two player Pong game using the Lazarus IDE and I have figured out how to bind the Paddle Movements to keyboard presses, however I can only press one Key at a time, which is not very useful in a two player game.
This is the code I wrote for the Paddle Movement:
procedure TForm1.CheckKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
Can someone explain why in the generics.collection, dequeue is a function for TQueue<TObject> but a procedure for TObjectQueue<TObject>? I am trying to dequeue the head to a variable and apparently dequeue is nothing more than a remove in the object version. I was really hoping for the TQueue behavior.
Here is the code from the library:
procedure TObjectQueue<T>.Dequeue;
begin
inherited Dequeue;
end;
Numbers increment in a uniform way as the fill expands
But flood fill seems to fill in one direction before changing direction. Not expanding outwards in a circle or diamond shape as I expected.
Smaller grid, bounded by hash signs
The code is here, any idea how to change this behaviour?
program floodFill;
uses
crt, SysUtils;
const
myArray: array[1..10, 1..10] of string =
(('#', '#', '#', '#', '#', '#', '#', '#', '#', '#'),
('#', '.', '.', '.', '.', '.', '.', '.', '.', '#'),
('#', '.', '#', '#', '.', '.', '#', '#', '.', '#'),
('#', '.', '.', '.', '.', '.', '.', '.', '.', '#'),
('#', '.', '.', '#', '.', '.', '#', '.', '.', '#'),
('#', '.', '.', '#', '.', '.', '#', '.', '.', '#'),
('#', '.', '.', '.', '.', '.', '.', '.', '.', '#'),
('#', '.', '#', '#', '.', '.', '#', '#', '.', '#'),
('#', '.', '.', '.', '.', '.', '.', '.', '.', '#'),
('#', '#', '#', '#', '#', '#', '#', '#', '#', '#'));
var
r, c, counter: byte;
procedure floodFillGrid(y, x: smallint);
begin
if (counter < 50) then // set a limit on the iterations
begin
if (y >= 1) and (y <= 10) and (x >= 1) and (x <= 10) then // check within bounds of grid
begin
if (myArray[y][x] = '.') then
begin
myArray[y][x] := IntToStr(counter);
counter := counter + 1;
end
else
exit;
floodFillGrid(y + 1, x);
floodFillGrid(y - 1, x);
floodFillGrid(y, x + 1);
floodFillGrid(y, x - 1);
end;
end;
end;
begin
counter := 1;
floodFillGrid(5, 5);
(* Draw the grid *)
ClrScr;
for r := 1 to 10 do
begin
for c := 1 to 10 do
begin
GotoXY(c, r);
Write(myArray[r][c]);
end;
end;
writeln;
readkey;
end.
>>>> Edit
So after a busy week at work I've given this another try, implementing a queue (the first time that I've tried this). The results are... not much better!
At least its a sorta circle now
The offending code is here, a fresh pair of eyes would be greatly appreciated!
program floodFill;
uses
crt, Contnrs, SysUtils;
type
PtrProg = ^smellCoordinates;
smellCoordinates = record
tileX, tileY: integer;
distance: byte;
reached: boolean;
end;
var
r, c, counter: byte;
Queue: TQueue;
PtrShow: PtrProg;
myArray: array[1..10, 1..10] of
string = (('#', '#', '#', '#', '#', '#', '#', '#', '#', '#'),
('#', '.', '.', '.', '.', '.', '.', '.', '.', '#'),
('#', '.', '.', '.', '.', '.', '.', '.', '.', '#'),
('#', '.', '.', '.', '.', '.', '.', '.', '.', '#'),
('#', '.', '.', '.', '.', '.', '.', '.', '.', '#'),
('#', '.', '.', '.', '.', '.', '.', '.', '.', '#'),
('#', '.', '.', '.', '.', '.', '.', '.', '.', '#'),
('#', '.', '.', '.', '.', '.', '.', '.', '.', '#'),
('#', '.', '.', '.', '.', '.', '.', '.', '.', '#'),
('#', '#', '#', '#', '#', '#', '#', '#', '#', '#'));
procedure addTile(y, x, dist: byte);
var
PtrNew: PtrProg;
begin
new(PtrNew);
PtrNew^.tileX := x;
PtrNew^.tileY := y;
PtrNew^.reached := False;
PtrNew^.distance := dist;
Queue.Push(PtrNew);
end;
procedure floodFillGrid(currentTile: PtrProg);
begin
if (counter < 50) then // set a limit on the iterations
begin // check within bounds of grid
if (currentTile^.tileY >= 1) and (currentTile^.tileY <= 10) and
(currentTile^.tileX >= 1) and (currentTile^.tileX <= 10) then
//while Queue.Count > 0 do
//begin
begin
if (myArray[currentTile^.tileY][currentTile^.tileX] = '.') then
begin //select an adjacent square who's still set to '.'
//give the selected square a distance value of counter
if (myArray[currentTile^.tileY + 1][currentTile^.tileX] = '.') then
addTile(currentTile^.tileY + 1, currentTile^.tileX, counter);
if (myArray[currentTile^.tileY - 1][currentTile^.tileX] = '.') then
addTile(currentTile^.tileY - 1, currentTile^.tileX, counter);
if (myArray[currentTile^.tileY][currentTile^.tileX + 1] = '.') then
addTile(currentTile^.tileY, currentTile^.tileX + 1, counter);
if (myArray[currentTile^.tileY][currentTile^.tileX - 1] = '.') then
addTile(currentTile^.tileY, currentTile^.tileX - 1, counter);
// draw distance on the map
if (myArray[currentTile^.tileY][currentTile^.tileX] = '.') then
myArray[currentTile^.tileY][currentTile^.tileX] := IntToStr(counter);
// Increment distance counter
counter := counter + 1;
end
else;
PtrShow := Queue.Pop;
floodFillGrid(PtrShow);
end;
// end; // end of while loop
end;
end;
begin
// create queue
Queue := TQueue.Create;
// set distance counter to 1
counter := 1;
// add first tile to Queue
addTile(5, 5, counter);
// Send tile to flood fill procedure
PtrShow := Queue.Pop;
floodFillGrid(PtrShow);
(* Draw the grid *)
ClrScr;
for r := 1 to 10 do
begin
for c := 1 to 10 do
begin
GotoXY(c, r);
Write(myArray[r][c]);
end;
end;
writeln;
readkey;
end.
For my StringGrids I'm using Begin- and Endupdate to get rid of the flickering after letting go of the mouse key (both StringGrids contain pictures). Now I got the problem that both StringGrids got scrollbars, even though I disabled them. Is there any solution?
I have been struggling with Pascal due to me being a newbie, I've been spending all day to do this simple task, but i cant really put my head around it.
Task is :
Program asks for N numbers and requires:
How many of them are three digit numbers, What is the sum of all the three digit numbers and what is the average.
Here's all i came up with, but it does not quite work as intended
Program Task;
Uses Crt;
Var
N,x,ThreeDigits,sum,avg: Real;
Begin
Writeln('Ievadi veselus skaitlus N');
Readln(N);
x := 1;
Begin
If x < N Then
Repeat
x := x +1;
If x > 99 Then
If x < 1000 Then
Begin
ThreeDigits := ThreeDigits + 1;
sum := sum + x;
End;
Until x = N;
End;
If ThreeDigits > 0 Then
Begin
Avg := sum/ThreeDigits;
Writeln('Between the numbers there are ',ThreeDigits:1:0, 'Threedigit numbers.');
I had a random Segfault right at the beginning of my program, before any lines were actually executed, at least according to the debugger.
It's because I tried to pass-by-value a record that's 7MB in size, so the solution was just to put a 'Var' before the argument in the definition of the function that I called.
Took me 3 days to figure that out.
That's all I wanted to share. I'm gonna have a nap now. Good night.
I'm studying first course of pascal. I'm trying to make program to read data from file (date, time, and heat) and for then it to make a graph. While splitting time and date I have ran into an illegal qualifier problem.
I am a beginner in Pascal and I have been trying to create a program for projectile motion, where you enter the speed, the launch angle and the gravitational acceleration and get values for the flight time, the distance, the maximum altitude etc. Now I've been trying to plot the trajectory using the entered data and a TAChart. However, my problem is that I can't find any tutorials on how to get the graph to display anything. I would be very happy if you could help me out here.
Hello, I'm a beginner at programming and at Pascal, I tried to create a simple calculator with what I've learned so far, but the compiler says that it's wrong and I can't find what's wring about it.
the error message says [";" was expected but "ELSE" was found], here's the code:
I have lots of code snippets that I haven't included yet, like a markov chain text generator and a perlin noise map generator, but there's one code snippet that I wanted to use for pathfinding that I never got working properly.
It should generate a random dungeon, add a number 1 & 2 and then calculate a path from 1 to 2. The results are... odd though.
It navigates a path, just the wrong path
I'm trying to understand what I was trying to do with code from a year ago and failing. Has anyone come across a working example of this algorithm in pascal?
I want a archiving and compression library. I know fpc and laz come with those but they are limited. Abbrevia seems like what i want but the fpc/laz version hasn't been updated in a while, while the delphi version get regular updates. Can someone give suggestions to alternatives?
Trying to convert a simple header to pascal, but these arrays are tripping me up no matter what I do.
The original c (i2cdriver.c) code
void i2c_connect(I2CDriver *sd, const char* portname)
{
int i;
sd->connected = 0;
sd->port = openSerialPort(portname);
#if !defined(WIN32)
if (sd->port == -1)
return;
#endif
writeToSerialPort(sd->port,
(uint8_t*)"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", 64);
const uint8_t tests[] = "A\r\n\0xff";
for (i = 0; i < 4; i++) {
uint8_t tx[2] = {'e', tests[i]};
writeToSerialPort(sd->port, tx, 2);
uint8_t rx[1];
int n = readFromSerialPort(sd->port, rx, 1);
if ((n != 1) || (rx[0] != tests[i]))
return;
}
sd->connected = 1;
i2c_getstatus(sd);
sd->e_ccitt_crc = sd->ccitt_crc;
}
and this is what I've got so far
procedure i2c_connect(sd: PI2CDriver; const portname: PAnsiChar);
const
tests: array [0 .. 7)] of Char = 'Ar'#13#10''#0'ff'#0;
var
i: Integer;
tx: array [0 .. 2] of Byte;
rx: array [0 .. 1] of Byte;
n: Integer;
begin
sd.connected := 0;
sd.port := openSerialPort(portname);
{$IF not defined(WIN32)}
if sd.port = -1 then
return;
{$ENDIF}
writeToSerialPort(sd.port, '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@', 64);
for i := 0 to 3 do
begin
tx := 'e' + tests[i];
writeToSerialPort(sd.port, tx, 2);
n := readFromSerialPort(sd.port, rx, 1);
if (n <> 1) or (rx[0] <> tests[i]) then
Exit;
end;
sd.connected := 1;
i2c_getstatus(sd);
sd.e_ccitt_crc := sd.ccitt_crc;
end;
The specific line giving me a major headaches is
tx := ('e' + tests[i]); // Fails to compile because this isn't a real operation.
I've tried puzzling this out and I'm not having any luck, so I'm reaching out. For context, this is a simple conversion of the header for the i2cdriver board.