Yep yep, and for my second post, I ‘only’ post something ‘light’ hehe. Well, kinda simple tips for programming in Pascal.
Actually, it’s been a very very long time since I use Pascal (about 3 years, as I remember). Well, it’s like that I prefer using Java
This afternoon, a friend of mine called me, and she asked me to teach her about simple Pascal programming all of sudden. Gosh, I really want to help her, but the thing is that I don’t really remember Pascal. And finally, I end up re-reading the Pascal book, looking for the compiler, and starting to code Pascal all over again, hehe. (Actuallty, it’s only for her assignment).
And in the middle of my-learning, I just thought about making a dynamic arrays in Pascal. We all know, that in Pascal, we must declare, which part is TYPE, which part is VAR, which part is BEGIN-END. The problem is, I want to declare an array, which size is determined by the user.
At first, I think about the VAR -part and the BEGIN-END-part, and I didn’t know, where should I put the declaration, where should I put the initialization. Wew,,confuse confuse confuse until I found a page here, http://www.freepascal.org/docs-html/ref/refsu14.html
Haha,,,everything is clear now. I found a solution to my simple problem. Here it is:
program ArrayDynamics;
TYPE
Point = record
X : integer;
Y : integer;
end;
TPoint = array of Point;
VAR
N : integer;
ArrayOfPoint : TPoint;
i : integer;
BEGIN
writeln('input the length : ');
readln (N);
SetLength(ArrayOfPoint, N);
for i := 1 to N do
begin
ArrayOfPoint[i].X := i;
ArrayOfPoint[i].Y := i+1;
end;
So, the solution is, make an Array type (on the TYPE-part), just declare the type of the array, is it array of integer, array of string, or array of a specific record, without the length.
Then, make an instance of the array on the VAR-part, for example, I made ArrayOfPoint as an instace of type TPoint.
And finally, one method-the key method, is SetLength (<the array>, <the lenght>). Use it to set the actual length of the array
Recent Comments