1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <ctype.h>
#include <time.h>
using namespace std;
const int SAMPLE_SIZE = 23;
const int NUMBER_OF_SETS = 1000;
void GetandDisplayMenu();
void ExplainBDayParadox();
void VerifyBDayParadox();
void DisplayBDaySet();
void GenerateBDaySet(int B[]);
void SortBDaySet (int B[]);
void ConvertDayOfYear (int DayofYear, int &MonthNumber, int &DayNumber);
int DaysInMonth (int MonthNumber);
void Exit();
void main()
{
int MenuChoice;
GetandDisplayMenu();
cin >> MenuChoice;
srand(int (time(NULL)));
switch (MenuChoice)
{
case 1: ExplainBDayParadox();
break;
case 2: VerifyBDayParadox();
break;
//case 3: DisplayBDaySet();
break;
//case 4: Exit();
break;
default:;
}
}
/*************************** Menu Display Function *************************************/
void GetandDisplayMenu()
{
cout << " Please select one of the option below!" << endl;
cout << " ====================================" << endl;
cout << " 1. Explain birthday paradox" << endl;
cout << " 2. Check birthday paradox by generating 1000 sets of birthdays" << endl;
cout << " 3. Display one set of 23 birthdays" << endl;
cout << " E. Exit" << endl;
cout << " ----------------------------------" << endl;
cout << " Please enter your selection: ";
}
/********************** Birthday Paradox Explanation Function **************************/
void ExplainBDayParadox()
{
cout << "\nThe birthday paradox can be described as follows: " << endl;
cout << " If 23 persons are chosen at random, then the chances are more " << endl;
cout << " than 50% that at least two will have the same birthday!" << endl;
}
/************************ Verify Birthday Paradox Function ***************************/
void VerifyBDayParadox()
{
int k, j;
const int SIZE = 23;
int B[SIZE+1];
float Result, matches=0;
for (k=1; k<=NUMBER_OF_SETS; k++)
{
GenerateBDaySet(B);
SortBDaySet(B);
for (j=0; j<SIZE; j++)
{
if (B[j]==B[j+1])
{
matches++;
break; //BREAK THE LOOP
}
}
}
Result = (matches/1000) * 100;
cout << "\nGenerating 1000 sets of 23 birthdays and checking for matches... " << endl;
cout << "Results : " << matches << " out of " << NUMBER_OF_SETS << " (" << Result << "%)"
<< " of the sets contained matching birthdays." << endl;
cout << " ================================================================== " << endl;
}
/************************ Generate Birthday Set Function *******************************/
void GenerateBDaySet(int B[])
{
int i=0;
for(i; i<23; i++)\
{
B[i]=(1+rand()%365);
}
}
/************************ Sorting Birthday Function ************************************/
void SortBDaySet(int B[])
{
int temp, min, i=0, j=0;
for(i; i < 23; i++)
{
min = i;
for (j=i+1; j < 23; j++)
{
if (B[j] < B[min])
min = j;
}
temp = B[i];
B[i] = B[min];
B[min] = temp;
}
}
/************************* Display Birthday Set Function *******************************/void DisplayBDayset()
{
const int SIZE=23;
char *MonthOfYear[13]={"ERROR","January","February","March","April","May","June",
"July","August","September","October","November","December"};
int B[SIZE+1];
int MonthNumber = 0, DayNumber = 0;
GenerateBDaySet(B);
SortBDaySet(B);
ConvertDayOfYear(B);
}
/************************* Days in Month Function **************************************/
int DaysInMonth (int MonthNumber)
{
switch (MonthNumber)
{
case 2: return 28;
break;
case 4: return 30;
break;
case 6: return 30;
break;
case 9: return 30;
break;
case 11: return 30;
break;
default: return 31;
}
}
/*********************** Convert Day of Year Function **********************************/
void ConvertDayOfYear (int DayofYear, int &MonthNumber, int &DayNumber)
{
int MonthNumber = 1;
while (DayofYear > DaysInMonth(MonthNumber))
{
DayofYear = (DayofYear - DaysInMonth(MonthNumber));
++MonthNumber;
}
DayNumber=DayofYear;
}
|