2 Replies - 513 Views - Last Post: 19 February 2013 - 12:25 AM Rate Topic: -----

#1 taaliyah_2000  Icon User is offline

  • New D.I.C Head

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

Had a question about this problem?

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 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)
{
	 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
  • +

Replies To: Had a question about this problem?

#2 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,460
  • Joined: 20-March 10

Re: Had a question about this problem?

Posted 19 February 2013 - 12:15 AM

Hi,

The function looks like this

void ConvertDayOfYear (int DayofYear, int &MonthNumber, int &DayNumber)

so in your program you should call it like this

ConvertDayOfYear(Year,Month,Day);

Or values to that effect you seem to calling it like

ConvertDayOfYear();


Also please do not double post it will not get your question answered any quicker
and just annoys forum leaders and admins who have to clean up your mess... :)

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 taaliyah_2000  Icon User is offline

  • New D.I.C Head

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

Re: Had a question about this problem?

Posted 19 February 2013 - 12:25 AM

Sorry about the double response was having connective issues and was not sure how to delete it.

Thanks for the suggestion for my program.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1