Program using Input --- C++ Program
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
//*************************************************************
// function prototype
int n_rand();
int checker (int[4600], int[4600], int);
//***************************************************************
// main program
void main()
{
int found = 0;
int z;
int amy[4600];
int clone[4600];
int t,yy,xx;
long int c=0;
cout<<"Please enter the number of chromosomes the species has "<<endl;
cin>>z;
// build amy
for (xx=1;xx<=z;xx++)
{
yy = n_rand();
if(yy==1)
{
amy[xx]= xx*100;
}
if(yy==2)
{
amy[xx]=xx;
}
}
c=0;
t=0;
while(found!=1)
{
c=c+1;
t=t+1;
cout<<endl;
for (int i = 1;i<=z;i++)
if (t>1000000)
{
cout<<"c = "<<c<<endl;
t=0;
}
for (xx=1;xx<=z;xx++)
{
yy = n_rand();
if(yy==1)
{
clone[xx]= xx*100;
}
if(yy==2)
{
clone[xx]=xx;
}
}
found = checker(amy,clone,z);
}
if(found==1)
{
cout <<"found after "<<c<<" times"<<endl;
}
}
//***********************************************************
//function n_rand to generate a random integer
// between 1 and 2 - inclusive
int n_rand()
{
const double a=1.0,b=3.0;
double x;
int y;
x= a+double(rand())*((b-a)/RAND_MAX);
y =int(x);
return(y);
}
//***********************************************************
// function to check for duplicates
int checker(int a[4600],int s[4600],int zz)
{
for (int m=1;m<=zz;m++)
{
if(s[m]!=a[m])
{
return(0);
}
}
return (1);
}
//**********************************************************************