#include <iomanip>
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int x[10000][10000], y, z;
int death[10000];
int birth[10000];
int year;
int maxa[10000];
int ad[10000];
int mut = 5;
int up, lo;
int a;
int w;
int total;
int food = 1000;
int disp;


int ran(int b, int a)
{
 int c;
 c=rand() % (a-b+1)+b;
 return c;
}




void born(int a)
{
 z = ran(1, 1000);
 
 if (mut > z)
 {
  x[0][0]++;
  z = x[0][0];
  up = ran(-1 * (death[a] / 50), death[a] / 50);
  death[z] = up + death[a];
  birth[z] = ran(birth[a] - 15, birth[a] + 15); 
  a = z;
 }
 x[a][0]++;
 x[a][x[a][0]]++;
 //cout<<"\nA birth, population "<<x[0]<<"\n";
}

void die(int y, int a)
{
 //cout<<y<<" has died, at age "<<x[y]<<".\n";
 if(x[a][y] > maxa[a])
 {
  maxa[a] = x[a][y];
 }


 
 for (z = y; z < (x[a][0] + 1); z++)
 {
  x[a][z] = x[a][z + 1];
  //cout<<"\nThis is Z - "<<z;
 }
 x[a][0]--;
 
}

void age()
{
 total = 0;
 for (lo = 1; lo < x[0][0] + 1; lo++)
 {
  total = total + x[lo][0];
 }
 
 for (a = 1; a < x[0][0] + 1; a++)
 {
  for (y = 1; y < x[a][0] + 1; y++)
  {
   x[a][y]++;
   z = ran(1, death[a] / total);
   if (pow(x[a][y],3) > z)
   {
    //cout<<"\n Here is y "<<y<<" and here is x[0]"<<x[0]<<" and here is x[y] "<<x[y]<<", and this is z "<<z<<"\n";
    die(y, a);
   }
   z = ran(1, 1000);
   if (z < birth[a])
   {
    born(a);
   }
  }
 }
}
int main()
{
 srand( (unsigned)time( NULL ) );
 using namespace std;
 w = 15;
 cout<<"Big Bang\n";
 death[1] = 100000000;
 birth[1] = 75;
 x[0][0] = 1;
 born(1);
 start:
 year++;
 age();
 

 
 system("CLS");
/* if (x[0] == 0)
 {
  goto start;
 }
 for (z = 1; z < x[a][0]; z++)
 {
  ad[a] = ad[a] + x[a][z];
 }
 ad[a] = ad[a] / x[a][0];
 */

 for (a = 1; a < x[0][0] + 1; a++)
 {
  if (x[a][0] == 0)
  {
   goto dis;
  }
  disp++;
  if (disp > 40)
  {
   goto dis;
  }
  
  cout<<"Mutation "<<a<<setw(w)<<"Pop "<<x[a][0]<<setw(w)<<"Oldest Age "<<maxa[a];
  cout<<"\n";
  dis:
  cout<<"";
 }
 cout<<"Total Mutations "<<x[0][0]<<setw(w)<<"Year "<<year<<setw(w)<<"Total Pop "<<total<<"\n";
 disp = 0;
 if (year > 10 && total < 1)
 {
  return 0;
 }
 goto start;
}

