PROGRAM d8r5(input,output,dfile);
(* driver for routine INDEXX *)
CONST
   np=100;
TYPE
   glsarray = ARRAY [1..np] OF real;
   gliarray = ARRAY [1..np] OF integer;
VAR
   i,j : integer;
   a : glsarray;
   indx : gliarray;
   dfile : text;

(*$I MODFILE.PAS *)
(*$I INDEXX.PAS *)

BEGIN
   glopen(dfile,'tarray.dat');
   readln(dfile);
   FOR i := 1 to 100 DO BEGIN
      read(dfile,a[i])
   END;
   close(dfile);
(* generate index for sorted array *)
   indexx(np,a,indx);
(* writeln original array *)
   writeln('original array:');
   FOR i := 1 to 10 DO BEGIN
      FOR j := 1 to 10 DO BEGIN
         write(a[10*(i-1)+j]:6:2)
      END;
      writeln
   END;
(* writeln sorted array *)
   writeln('sorted array:');
   FOR i := 1 to 10 DO BEGIN
      FOR j := 1 to 10 DO BEGIN
         write(a[indx[10*(i-1)+j]]:6:2)
      END;
      writeln
   END
END.
PROGRAM d7r10 (input,output);
(* driver for routine IRBIT1 *)
(* calculate distribution of runs of zeros *)
CONST
   nbin=15;
   ntries=500;
VAR
   i,iflg,ipts,iseed,j,n : integer;
   twoinv : real;
   delay : ARRAY [1..nbin] OF real;

(*$I MODFILE.PAS *)
(*$I IRBIT1.PAS *)

BEGIN
   iseed := 1234;
   FOR i := 1 to nbin DO BEGIN
      delay[i] := 0.0
   END;
   writeln ('distribution of runs of n zeros');
   writeln ('n':6,'probability':22,'expected':18);
   ipts := 0;
   FOR i := 1 to ntries DO BEGIN
      IF (irbit1(iseed) = 1) THEN BEGIN
         ipts := ipts+1;
         iflg := 0;
         FOR j := 1 to nbin DO BEGIN
            IF ((irbit1(iseed) = 1) AND (iflg = 0))
            THEN BEGIN
               iflg := 1;
               delay[j] := delay[j]+1.0
            END
         END
      END
   END;
   twoinv := 0.5;
   FOR n := 1 to nbin DO BEGIN
      writeln ((n-1):6,(delay[n]/ipts):19:4,twoinv:20:4);
      twoinv := twoinv/2.0
   END
END.
PROGRAM d7r11(input,output);
(* driver for routine IRBIT2 *)
(* calculate distribution of runs of zeros *)
CONST
   nbin=15;
   ntries=500;
VAR
   i,iflg,ipts,iseed,j,n : integer;
   delay : ARRAY [1..nbin] OF real;

FUNCTION twoton(n : integer) : integer;
BEGIN
   IF (n=0) THEN twoton := 1
   ELSE twoton := 2*twoton(n-1)
END;

(*$I MODFILE.PAS *)
(*$I IRBIT2.PAS *)

BEGIN
   iseed := 111;
   FOR i := 1 to nbin DO BEGIN
      delay[i] := 0.0
   END;
   ipts := 0;
   FOR i := 1 to ntries DO BEGIN
      IF (irbit2(iseed) = 1) THEN BEGIN
         ipts := ipts+1;
         iflg := 0;
         FOR j := 1 to nbin DO BEGIN
            IF ((irbit2(iseed) = 1) AND (iflg = 0)) THEN BEGIN
               iflg := 1;
               delay[j] := delay[j]+1.0
            END
         END
      END
   END;
   writeln ('distribution of runs of n zeros');
   writeln ('n':6,'probability':22,'expected':18);
   FOR n := 1 to nbin DO BEGIN
      writeln ((n-1):6,delay[n]/ipts:19:4,1/twoton(n):20:4);
   END
END.
