The birthday paradox can be described as follows: If 23 persons are chosen at ra
ID: 3623290 • Letter: T
Question
The birthday paradox can be described as follows:
If 23 persons are chosen at random, then the chances are more than 50% that at least two will have the same birthday!
When they first hear this stated, most persons have trouble believing it and are unconvinced by mathematical proofs1. The program that you write for this assignment will allow you to test the birthday paradox to see if it is really true. This will be done by simulating birthdays with random numbers between 1 and 365.
Your main program should display a menu that reads as follows:
1) Explain birthday paradox
2) Check birthday paradox by generating 1000 sets of birthdays
3) Display one set of 23 birthdays
E) Exit
If option 2) is selected, the output would be something like this:
Generating 1000 sets of 23 birthdays and checking for matches...
Results : 511 out of 1000 (51.1%) of the sets contained matching birthdays
=============================================================================
If option 3) is selected, the program will display the results in chronological order with matches indicated. The output should needs to look like this:
Here are the results of generating a set of 23 birthdays
============================================================
January 2 January 5 January 12
January 25 February 3 February 6
February 12 February 17 March 8
April 4 April 12 June 3
June 12 July 3 August 4
September 3 September 9 September 25
(2) October 12 October 16 October 22
December 22
Hints and Suggestions:
* A "birthday" can be viewed as a random integer between 1 and 365. Recall that there is function named "rand()" in stdlib that can be used to generate such random numbers. The expression "1 + (rand() % 365)" yields a random number that is between 1 and 365 inclusive. You should use the srand() function, but be sure to not to call it more than once!
* As a bottom-up exercise, I recommend that you write the following function.
void ConvertDayOfYear (int DayOfYear, // "Converts" a day of the year, such as 32
int &MonthNumber, // to a MonthNumber, 2 and a DayOfMonth, 1
int &DayOfMonth)
* We'll do a top-down design in class that will make your work go more smoothly. The Selection Sort code can be found on website. (also inline function swap is there)
* Write one function at a time!
Explanation / Answer
please rate - thanks
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
void tostring(int d,char value[]);
string todate(int doy);
void PrintMenu();
void explain();
void generate(int[]);
void check();
bool matches(int[]);
void display();
void print(int,int);
int main()
{char choice;
srand(time(0));
do
{PrintMenu();
cin>>choice;
switch(choice)
{case '1': explain();
break;
case '2': check();
break;
case '3': display();
break;
case 'E': return 0;
default: cout<<"Invalid choice ";
}
}while(true);
}
void display()
{int dates[365];
int i,line=0;
generate(dates);
cout<<"Here are the results of generating a set of 23 birthdays ";
cout<<"============================================================";
for(i=0;i<365;i++)
if(dates[i]!=0)
{if(line==0)
cout<<endl;
print(i+1,dates[i]);
line++;
if(line==3)
line=0;
}
cout<<endl<<endl<<endl;
}
void print(int i,int d)
{if(d==1)
cout<<" ";
else
cout<<"("<<d<<") ";
cout<<todate(i)<<" ";
}
void generate(int dates[])
{int i,n;
for(i=0;i<365;i++)
dates[i]=0;
for(i=0;i<23;i++)
{n=rand()%365;
dates[n]++;
}
}
bool matches(int dates[])
{
for(int i=0;i<365;i++)
if(dates[i]>1)
return true;
return false;
}
void check()
{int i,dates[365];
int count=0;
cout<<"Generating 1000 sets of 23 birthdays and checking for matches... ";
for(i=0;i<1000;i++)
{generate(dates);
if(matches(dates))
count++;
}
cout<<"Results : "<<count<<" out of 1000 ("<<setprecision(1)<<fixed<<
count/1000.*100<<"%) of the sets contained matching birthdays ";
}
void PrintMenu()
{cout<<"1- Explain birthday paradox ";
cout<<"2- Check birthday paradox by generating 1000 sets of birthdays ";
cout<<"3- Display one set of 23 birthdays ";
cout<<"E- Exit ";
}
void explain()
{cout<<"The birthday paradox can be described as follows: ";
cout<<"If 23 persons are chosen at random, then the chances ";
cout<<"are more than 50% that at least two will have the same birthday! ";
}
string todate(int doy)
{
int days=0,i=0,n;
string mth[12]={"January ","February ","March ","April ","May ","June ","July ",
"August ","September ","October ","November ","December "};
int daysinmonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
string date;
char doys[3];
while(doy>daysinmonth[i])
{doy=doy-daysinmonth[i++];
}
tostring(doy,doys); //convert integer to string
date=mth[i]+doys;
return date;
}
void tostring(int d,char value[])
{
int t;
if(d>=10)
{t=d%10;
d=d/10;
value[0]=d+48;
value[1]=t+48;
value[2]='';
}
else
{value[0]=d+48;
value[1]='';
}
return ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.