

// Tenth WCU Computer Science Programming Contest (May 1999)
// Coin Flips Problem
// Description: Write a program that simulates tossing a coin
// and keeps track of the longest run of heads and the longest
// run of tails. This is a simple solution that is not especially
// efficient.


#include <iostream.h>
#include <stdlib.h>

void ComputeRuns(int& maxHeads, int& maxTails, const int NUM_FLIPS);
// This function determines the longest runs of head and of tails.

int main()
{
  int numFlips;
  int maxHeads, maxTails;

  cout << "Enter the number of coin flips: ";
  cin >> numFlips;
  ComputeRuns(maxHeads, maxTails, numFlips);
  cout << "The longest run of heads  is " << maxHeads << endl;
  cout << "The longest run of tails is " << maxTails << endl;
  return 0;
}

void ComputeRuns(int& maxHeads, int& maxTails, const int NUM_FLIPS)
{
  const int HEAD = 0;
  const int TAIL = 1;
  int headRun = 0;
  int tailRun = 0;

  maxHeads = 0;
  maxTails = 0;
  for (int i = 0; i < NUM_FLIPS; i++)
  {
    if (random() % 2 == HEAD)
    {
      cout << "Head\n";
      headRun++;
      if (headRun > maxHeads)
        maxHeads = headRun;
      tailRun = 0;
    } else
    {
      cout << "Tail\n";
      tailRun++;
      if (tailRun > maxTails)
        maxTails = tailRun;
      headRun = 0;
    }	
  }
  return;
}



