Page 1 of 1

## 0 Replies - 921 Views - Last Post: 19 February 2013 - 12:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=312686&amp;s=961a19fea0d791c6fba8e560d29efc78&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 taaliyah_2000

Reputation: 0
• Posts: 34
• Joined: 15-November 09

Posted 19 February 2013 - 12:07 AM

The birthday paradox says that the probability that two people in a room will have the same birthday (month and date only) is more than half as long as n, the number of people in the room, is more than 23. This property is not really a paradox, but many people find it surprising. Design and develop a C++ program that can test this paradox by a series of experiments on randomly generated birthdays, which test this paradox for n= 5, 10, 15, 20, … , 100. You should run at the number of experiments for each value of n and it should output, for each n, the number of experiments for that n, such that two people in that test will have the same birthday.

``` #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 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()
{

srand(int (time(NULL)));
{
break;
break;
//case 3: DisplayBDaySet();
break;
//case 4: Exit();
break;
default:;
}

}

{
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;

}

/********************** Birthday Paradox Explanation Function **************************/
{
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 ***************************/
{
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)
{
MonthNumber = 1;

while (DayofYear > DaysInMonth(MonthNumber))
{
DayofYear = (DayofYear - DaysInMonth(MonthNumber));
++MonthNumber;
}
DayNumber=DayofYear;
}

```

I keep getting a error (144): error C2660: 'ConvertDayOfYear' : function does not take 0 arguments and I cant seem to figure it out.

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }