
#include <gpc.pas>

program head(input,output);

uses gpc;

{This program uses a random generator which is peculiar to the gpc pascal
compiler that runs on UNIX machines. To use this random generator
you need to include the following statements
#include <gpc.pas>
uses gpc;
GPC_Randomize;  
GPC_RandInt(2);

The GPC_Randomize; sets an initial value for the seed. If GPC_Randomize;
is not executed, then the same default seed is used on every run, so the
same random sequence is generated on every run.

The GPC_RandInt(2); 
returns the next integer in the random sequence with the maximum possible
value being the parameter.}
{The program simulates flipping a coin 10,000 times and outputs 
the longest run of heads and tails.}


Var
  numFlips : integer;
  maxhead, maxtail : integer;
  curhead, curtail : integer;
  i: integer;
  result : real;

Begin  {Main}
  {GPC_Randomize;}	{use a seed to initialize the random sequence}
			{commented out so that every run uses the same
			seed}
  maxhead:=0;
  maxtail:=0;
  curhead:=0;
  curtail:=0;
  writeln('Enter the number of coin flips.');
  readln(numFlips);
  for i:= 1 to numFlips do
    begin
      result:= GPC_RandInt(2);
      if result < 0.5 then
        begin
          writeln('Head');
          curhead := curhead +1;
          curtail := 0;
        end
      else
        begin
          writeln('Tail');
          curtail := curtail +1;
          curhead := 0;
        end;

      if curtail > maxtail then
        maxtail := curtail;

      if curhead > maxhead then
        maxhead := curhead;

    end;

  writeln('The max number of heads in a run is ',maxhead:1);
  writeln;
  writeln('The max number of tails in a run is ',maxtail:1);

End.

