RosettaCodeData/Task/Pascal-matrix-generation/XPL0/pascal-matrix-generation.xpl0

61 lines
2.0 KiB
Plaintext

\Initialises M to an upper Pascal matrix of size N
\The bounds of M must be at least 1 :: N, 1 :: N
procedure UpperPascalMatrix ( M, N );
integer M, N, J, I;
begin
for J := 1 to N do M( 1, J ) := 1;
for I := 2 to N do begin
M( I, 1 ) := 0;
for J := 2 to N do M( I, J ) := M( I - 1, J - 1 ) + M( I, J - 1 )
end \for_I
end; \UpperPascalMatrix
\Initialises M to a lower Pascal matrix of size N
\The bounds of M must be at least 1 :: N, 1 :: N
procedure LowerPascalMatrix ( M, N );
integer M, N, I, J;
begin
for I := 1 to N do M( I, 1 ) := 1;
for J := 2 to N do begin
M( 1, J ) := 0;
for I := 2 to N do M( I, J ) := M( I - 1, J - 1 ) + M( I - 1, J )
end \for_J
end; \LowerPascalMatrix
\Initialises M to a symmetric Pascal matrix of size N
\The bounds of M must be at least 1 :: N, 1 :: N
procedure SymmetricPascalMatrix ( M, N );
integer M, N, I, J;
begin
for I := 1 to N do begin
M( I, 1 ) := 1;
M( 1, I ) := 1
end; \for_I
for J := 2 to N do for I := 2 to N do M( I, J ) := M( I, J - 1 ) + M( I - 1, J )
end; \SymmetricPascalMatrix
\Test the Pascal matrix procedures
\Print the matrix M with the specified field width
\The bounds of M must be at least 1 :: N, 1 :: N
procedure PrintMatrix ( M, N, FieldWidth );
integer M, N, I, J;
begin
Format(3, 0);
for I := 1 to N do begin
for J := 1 to N do RlOut(0, float( M( I, J ) ) );
CrLf(0)
end; \for_I
end; \PrintMatrix
integer M( 1+10, 1+10 );
integer N, W;
begin
N := 5; W := 2;
UpperPascalMatrix( M, N );
Text(0, "upper:^m^j" ); PrintMatrix( M, N, W );
LowerPascalMatrix( M, N );
Text(0, "lower:^m^j" ); PrintMatrix( M, N, W );
SymmetricPascalMatrix( M, N );
Text(0, "symmetric:^m^j" ); PrintMatrix( M, N, W )
end