Name: Anonymous 2011-01-21 2:36
i was assigned a short project in c++ today
and i thought that i would share the code that calculates the birthday paradox
#include <iostream>
#include <cstdlib>
using namespace std;
double birthdayCollecter(int B[],int n,double t)
{
int C;
C=0;
for (int h=1; h<=t; h++)
{
for (int i=0; i<n; i++)
{
bool breaker = false;
B[i] = (rand() %365 + 1);
cout<<"\nB["<<i<<"] = "<<B[i];
for (int j =0; j<i; j++)
{
if (B[j] == B[i])
{
C++;
cout<<" "<<C;
breaker = true;
break;
}
}
if (breaker == true){break;}
}
}
return C;
}
int collectPeople()
{
cout<<"how many people would you like to check at once : ";
int people;
cin>>people;
while (people>100)
{
cout<<"\nSorry pick a number lower then 100, the percent it too high otherwise\n";
cout<<"how many people would you like to check at once : ";
cin>>people;
if (people<100){return people;}
}
return people;
}
int main()
{
//this is the intoduction message, it asks the user to input the wanted values
cout<<"\nwelcome to the birthday paradox calculator \n";
// the number of people to be tested is input and checked
int numPeople = collectPeople();
cout<<"\nok you want to check "<< numPeople <<" People \n";
// the number of wanted simulations is imput
cout<<"how many times do you want to run the simulation : ";
double trials;
cin>> trials;
double Count;
int birthdayArray[numPeople];
// The count of how manny people share birthdays is defined by the Birtday Collecter function
Count = birthdayCollecter(birthdayArray,numPeople,trials);
cout<<"\nthe Probablility of two people in a room of "<<numPeople<<" sharing a birthday is : "<< (Count/trials)*100<<"%" ;
cout<<"\n\n";
return 0;
}
and i thought that i would share the code that calculates the birthday paradox
#include <iostream>
#include <cstdlib>
using namespace std;
double birthdayCollecter(int B[],int n,double t)
{
int C;
C=0;
for (int h=1; h<=t; h++)
{
for (int i=0; i<n; i++)
{
bool breaker = false;
B[i] = (rand() %365 + 1);
cout<<"\nB["<<i<<"] = "<<B[i];
for (int j =0; j<i; j++)
{
if (B[j] == B[i])
{
C++;
cout<<" "<<C;
breaker = true;
break;
}
}
if (breaker == true){break;}
}
}
return C;
}
int collectPeople()
{
cout<<"how many people would you like to check at once : ";
int people;
cin>>people;
while (people>100)
{
cout<<"\nSorry pick a number lower then 100, the percent it too high otherwise\n";
cout<<"how many people would you like to check at once : ";
cin>>people;
if (people<100){return people;}
}
return people;
}
int main()
{
//this is the intoduction message, it asks the user to input the wanted values
cout<<"\nwelcome to the birthday paradox calculator \n";
// the number of people to be tested is input and checked
int numPeople = collectPeople();
cout<<"\nok you want to check "<< numPeople <<" People \n";
// the number of wanted simulations is imput
cout<<"how many times do you want to run the simulation : ";
double trials;
cin>> trials;
double Count;
int birthdayArray[numPeople];
// The count of how manny people share birthdays is defined by the Birtday Collecter function
Count = birthdayCollecter(birthdayArray,numPeople,trials);
cout<<"\nthe Probablility of two people in a room of "<<numPeople<<" sharing a birthday is : "<< (Count/trials)*100<<"%" ;
cout<<"\n\n";
return 0;
}