59 lines
1.6 KiB
Text
59 lines
1.6 KiB
Text
include xpllib; \for Print and RlSort
|
|
|
|
func real OurMedian(X, Start, EndInclusive);
|
|
real X; int Start, EndInclusive;
|
|
int Size, M;
|
|
[Size:= EndInclusive - Start + 1;
|
|
if Size <= 0 then
|
|
[Print("Array slice cannot be empty\n");
|
|
exit 1;
|
|
];
|
|
M:= Start + Size/2;
|
|
if rem(0) then return X(M);
|
|
return (X(M-1) + X(M)) / 2.0;
|
|
];
|
|
|
|
func FiveNum(X, Result, XLen);
|
|
real X, Result; int XLen;
|
|
int I, M, LowerEnd;
|
|
[for I:= 0 to XLen-1 do
|
|
[if X(I) # X(I) then
|
|
[Print("Unable to deal with arrays containing NaN\n\n");
|
|
return true;
|
|
];
|
|
];
|
|
RlSort(X, XLen);
|
|
Result(0):= X(0);
|
|
Result(2):= OurMedian(X, 0, XLen-1);
|
|
Result(4):= X(XLen-1);
|
|
M:= XLen/2;
|
|
LowerEnd:= if rem(0) then M else M-1;
|
|
Result(1):= OurMedian(X, 0, LowerEnd);
|
|
Result(3):= OurMedian(X, M, XLen-1);
|
|
return false;
|
|
];
|
|
|
|
proc Show(Result, Places);
|
|
real Result; int Places;
|
|
int I;
|
|
[Format(1, Places);
|
|
Print("[");
|
|
for I:= 0 to 5-1 do
|
|
[RlOut(0, Result(I));
|
|
if I < 4 then Print(", ");
|
|
];
|
|
Print("]\n\n");
|
|
];
|
|
|
|
real Result(5), X1, X2, X3;
|
|
[X1:= [15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0];
|
|
if not FiveNum(X1, Result, 11) then Show(Result, 1);
|
|
X2:= [36.0, 40.0, 7.0, 39.0, 41.0, 15.0];
|
|
if not FiveNum(X2, Result, 6) then Show(Result, 1);
|
|
X3:= [ 0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
|
|
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
|
|
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
|
|
0.75775634, 0.32566578
|
|
];
|
|
if not FiveNum(X3, Result, 20) then Show(Result, 9);
|
|
]
|