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
|
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
void Menu ();
void Explain();
void Verify (int Bday[]);
void GenerateSet (int Bday[]);
void SelectionSort (int Bday[], int Last);
void Display (int Bday[], char Months[]);
int DaysInMonth(int Mon);
inline void Swap (int &A, int &B);
const int SETS=1000;
int main()
{
char Ch;
int Bday [24];//={0, 12, 13, 234, 234, 34, 5, 2, 5, 22, 35, 199, 350, 1, 356, 140, 3, 4, 6, 7, 8, 9, 10, 11};
char Months[13][11]={"nada", "January", "Febuary", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
srand(time(NULL));
do
{
Menu();
cin.get(Ch);
switch (Ch)
{
case '1': Explain();
break;
case '2': Verify(Bday);
break;
case '3': Display(Bday, *Months);
break;
case 'E':
case 'e': break;
default: cout<<"Invalid input, please enter again."<<endl;
}
}
while (Ch!='E'||Ch!='e');
return 0;
}
void Menu()
{
cout<<"\n1) Explain birthday paradox."<<endl;
cout<<"2) Verify Birthday paradox by generating 1000 sets of birthdays."<<endl;
cout<<"3) Display Results of one set of 23 birthdays."<<endl;
cout<<"E) exit."<<endl;
}
void Explain ()
{
cout<<"If 23 persons are chosen at random, chances are more than 50%"<<endl;
cout<<"that two of them will have the same birthday."<<endl<<endl;
cin.ignore(100, '\n');
}
void Verify (int Bday[])
{
int Match=0, i, Temp;
cout<<"Generating 1000 sets of 23 birthdays and checking for matches..."<<endl;
for(int k=1;k<=SETS;++k)
{
GenerateSet(Bday);
SelectionSort(Bday, 23);
Temp=Match;
i=1;
while (Match<=Temp&&i!=23)
{
if (Bday[i]==Bday[i+1])
++Match;
++i;
}
}
cout<<"Results: "<<Match<<" out of 1000 ("<<(float(Match)/SETS)*100<<"%) sets of birthdays had at least one match."<<endl;
cin.ignore(100, '\n');
}
void GenerateSet (int Bday[])
{
for(int i=1;i<=23;++i)
Bday[i]=1+rand()%365;
}
void SelectionSort (int Bday[], int Last)
{
int i, Start, SubSmall;
for ( Start = 1; Start < Last; ++Start ) // Sort items 1..Last
{
SubSmall = Start; // subscript of smallest elt.
for ( i = Start + 1; i <= Last; i++ ) // Find subscript of smallest
if ( Bday[i] < Bday[SubSmall] ) // elt. in tail end of array
SubSmall= i;
Swap(Bday[SubSmall], Bday[Start]); // Place in correct position
}
}
inline void Swap (int &A, int &B)
{
int Temp= A;
A = B;
B = Temp;
}
void Display (int Bday[], char Months[])
{
int Month, Line=0, Repeat;
cout<<"Here are the results of generating a set of 23 birthdays:"<<endl;
cout<<"==========================================================="<<endl;
GenerateSet(Bday);
SelectionSort(Bday, 23);
for (int i=1;i<=23;++i)
{
Month=1;
Repeat=1;
if (i==23) //Stops unindexed references
break;
else if (Bday[i]==Bday[i+1]) //this checks for repeated birthdays
++Repeat;
while (Bday[i]>DaysInMonth(Month)) //this loop turns the day of year number into
{ //day of month, and sets the month.
Bday[i]-=DaysInMonth(Month);
++Month;
}
if (Repeat>1)
cout<<"("<<Repeat<<") "; // this makes the repeat show,
else //if there is a repeat.
cout<<" ";
cout<<Months[1]<<" "<<setw(2)<<Bday[i]<<" ";
++Line; //this varriable is for formatting,
if (Line%3==0) //every thrid print adds a \n
cout<<endl;
}
cin.ignore(100, '\n');
cout<<Months[1]<<endl<<Months[2]<<endl<<Months[3];;
}
int DaysInMonth(int Mon)
{
switch (Mon)
{
case 2: return 28;
case 4:
case 6:
case 9:
case 11: return 30;
default: return 31;
}
}
|