Smart pascal source code
var
a: array of Integer;
function MakeArray(n: Integer): array of Integer;
begin
Result := New Integer[n];
while n > 0 do
begin
Dec(n);
Result[n] := n;
end;
end;
procedure WriteLnArray(a: array of Integer);
var i: Integer;
begin
Writeln(IntToStr(a.Length) + ' elements');
for i := a.Low to a.High do
begin
Writeln(i);
Writeln(': ');
Writeln(a[i]);
end;
end;
begin
a := MakeArray(0);
a.Reverse;
WriteLnArray(a); // 0 elements
a := MakeArray(1);
a.Reverse;
WriteLnArray(a); // 0 elements
a.Swap(0,0);
WriteLnArray(a); { 1 elements
0: 0
}
a := MakeArray(2);
a.Reverse;
WriteLnArray(a); { 2 elements
0: 1
1: 0
}
a.Swap(0,1);
WriteLnArray(a); { 2 elements
0: 0
1: 1
}
a := MakeArray(3);
a.Reverse;
WriteLnArray(a); { 3 elements
0: 2
1: 1
2: 0
}
a.Swap(0,2);
WriteLnArray(a); { 3 elements
0: 0
1: 1
2: 2
}
function $NewArray(n, v) {
var r = new Array(n);
for (var i = 0; i < n; i++)
r[i] = v;
return r
}
function $ArraySwap(a, i1, i2) {
var t = a[i1];
a[i1] = a[i2];
a[i2] = t
}
var a = [];
function WriteLnArray(a$1) {
var i = 0;
WriteLn(((a$1.length).toString()+" elements"));
var $temp1;
for(i=0,$temp1=a$1.length;i<$temp1;i++) {
WriteLn(i);
WriteLn(": ");
WriteLn(a$1[i]);
}
};
function MakeArray(n) {
var Result = [];
Result = $NewArray(n,0);
while (n>0) {
--n;
Result[n]=n;
}
return Result
};
a = MakeArray(0);
a.reverse();
WriteLnArray(a);
a = MakeArray(1);
a.reverse();
WriteLnArray(a);
$ArraySwap(a,0,0);
WriteLnArray(a);
a = MakeArray(2);
a.reverse();
WriteLnArray(a);
$ArraySwap(a,0,1);
WriteLnArray(a);
a = MakeArray(3);
a.reverse();
WriteLnArray(a);
$ArraySwap(a,0,2);
WriteLnArray(a);