10 Replies - 2740 Views - Last Post: 03 October 2012 - 06:58 PM Rate Topic: -----

#1 Charlwillia6  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 17-September 12

C++ Finding the Birthday Paradox

Posted 02 October 2012 - 10:16 AM

I have a couple problems with the code below and was hoping you could take a look at it.

First problem:
Run-Time Check Failure #2 - Stack around the variable 'Set' was corrupted.
I get this when I select menu 2 to check the birthday paradox.

Second problem:
When the results are displayed, my matches are too high. I should be getting 50%, maybe a little more, out of a set of 1000. I am getting more matches between 600 and 700 out of a 1000. So my "paradox" is too high.

Third problem:
For the life of me, I cannot discover why my Average formula isn't working in the DisplayBirthdayParadoxResults function. It always comes out to 0% when it is displayed.


#include <iostream>
#include <ctime>
#include <iomanip>

using namespace std;

void DisplayMenu();
void MenuChoice(char Choice);
void VerifyBirthdayParadox();
void DisplayBirthdaySet();
void GenerateBirthdaySet(int BirthdaySet[]);

void SelectionSort(int List[], int  Last);
void DisplayBirthdayParadoxResults(int Matches);

const int SAMPLE_SIZE = 23;
const int NUMBER_OF_SETS = 1000;
const int DASH_LENGTH = 60;

int main()
{
	char Choice;

	srand((unsigned)time(0));

	do
	{
		Choice = '\0';

		DisplayMenu();

		cin>>Choice;
		cout<<"\n";

		MenuChoice(Choice);
	}
	while(Choice != 'e' && Choice != 'E');
}

void DisplayMenu()
{
	cout<<"1)	Explain birthday paradox\n";
	cout<<"2)	Check birthday paradox by generating 1000 sets of birthdays\n";
	cout<<"3)	Display one set of 23 birthdays\n";
	cout<<"E)	Exit\n\n";
}

void MenuChoice(char Choice)
{
	switch(Choice)
	{
		case '1':
			cout<<"If 23 people are chosen at random, then the chances are more than 50%\n"
				<<"that at least two will have the same birthday!\n\n";

			break;

		case '2':
			VerifyBirthdayParadox();

			break;

		case '3':
			//DisplayBirthdaySet();
			cout<<"Menu 3\n\n";

			break;

		case 'E':
		case 'e':

			break;

		default:
			cout<<"Invalid selection\n\n";
	}
}

void VerifyBirthdayParadox()
{
	int Set[SAMPLE_SIZE];
	int NumberOfMatches = 0;

	for(int k = 0; k <= NUMBER_OF_SETS; k++)
	{
		GenerateBirthdaySet(Set);

		for(int j = 0; j <= SAMPLE_SIZE; j++)
		{
			if(Set[j] == Set[j - 1])
				NumberOfMatches++;
		}
	}
	
	DisplayBirthdayParadoxResults(NumberOfMatches);
}

void GenerateBirthdaySet(int BirthdaySet[])
{
	for(int i = 0; i < SAMPLE_SIZE; i++)
		BirthdaySet[i] = (rand() % 365) + 1;
	
	SelectionSort(BirthdaySet, SAMPLE_SIZE);
}

void DisplayBirthdayParadoxResults(int Matches)
{
	int Average = (Matches / NUMBER_OF_SETS) * 100;

	char DashLine[DASH_LENGTH];

	cout<<"Generating "<<NUMBER_OF_SETS<<" sets of "<<SAMPLE_SIZE
		<<" birthdays and checking for matches...\n\n";

	cout<<"Results : "<<Matches<<" out of "<<NUMBER_OF_SETS<<" ("
		<<setprecision(1)<<Average
		<<"%) of the sets contained matching birthdays\n";

	for(int i = 0; i < DASH_LENGTH; ++i)
	{
		DashLine[i] = '=';
		cout<<DashLine[i];
	}

	cout<<"\n\n";
}		


Thanks ahead of time for any help you can bring me to this.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Finding the Birthday Paradox

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4019
  • View blog
  • Posts: 12,406
  • Joined: 25-December 09

Re: C++ Finding the Birthday Paradox

Posted 02 October 2012 - 10:41 AM

Quote

First problem:
Run-Time Check Failure #2 - Stack around the variable 'Set' was corrupted.
I get this when I select menu 2 to check the birthday paradox.

Let's start by looking at your function.
void VerifyBirthdayParadox()
{
	int Set[SAMPLE_SIZE];
	int NumberOfMatches = 0;

	for(int k = 0; k <= NUMBER_OF_SETS; k++)
	{
		GenerateBirthdaySet(Set);

		for(int j = 0; j <= SAMPLE_SIZE; j++)
		{
			if(Set[j] == Set[j - 1])
				NumberOfMatches++;
		}
	}
	
	DisplayBirthdayParadoxResults(NumberOfMatches);
}

The first thing I notice is that you are using the operator<= in your for loops, this is usually a mistake and leads to accessing arrays out of bounds. Which is the case in the above case.

Look at your inner for loop, remember set was defined with a size of SAMPLE_SIZE. Also remember that arrays in C/C++ start at 0 and end at size - 1. So when this loop reaches SAMPLE_SIZE it is accessing the array out of bounds.

For you second problem take care of all the issues mentioned above then retry your results. Remember you have multiple places where you are accessing the arrays out of bounds, you need to fix them all.


Quote

Third problem:
For the life of me, I cannot discover why my Average formula isn't working in the DisplayBirthdayParadoxResults function. It always comes out to 0% when it is displayed.

The problem is being caused by the following line:
int Average = (Matches / NUMBER_OF_SETS) * 100;

You are using integer math, integers have no fractions, so Matches/ NUMBER_OF_SETS should yield a value of zero. If you want fractional numbers you must use floating point numbers.


Jim

This post has been edited by jimblumberg: 02 October 2012 - 10:42 AM

Was This Post Helpful? 1
  • +
  • -

#3 Charlwillia6  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 17-September 12

Re: C++ Finding the Birthday Paradox

Posted 02 October 2012 - 01:32 PM

Ok, I still get the same error for Set and my average is now coming up with 6e+001 as the answer. Code now:

#include <iostream>
#include <ctime>
#include <iomanip>

using namespace std;

void DisplayMenu();
void MenuChoice(char Choice);
void VerifyBirthdayParadox();
void DisplayBirthdaySet();
void GenerateBirthdaySet(int BirthdaySet[]);

void SelectionSort(int List[], int  Last);
void DisplayBirthdayParadoxResults(float Matches);

const int SAMPLE_SIZE = 23;
const int NUMBER_OF_SETS = 1000;
const int DASH_LENGTH = 60;

int main()
{
	char Choice;

	srand((unsigned)time(0));

	do
	{
		Choice = '\0';

		DisplayMenu();

		cin>>Choice;
		cout<<"\n";

		MenuChoice(Choice);
	}
	while(Choice != 'e' && Choice != 'E');
}

void DisplayMenu()
{
	cout<<"1)	Explain birthday paradox\n";
	cout<<"2)	Check birthday paradox by generating 1000 sets of birthdays\n";
	cout<<"3)	Display one set of 23 birthdays\n";
	cout<<"E)	Exit\n\n";
}

void MenuChoice(char Choice)
{
	switch(Choice)
	{
		case '1':
			cout<<"If 23 people are chosen at random, then the chances are more than 50%\n"
				<<"that at least two will have the same birthday!\n\n";

			break;

		case '2':
			VerifyBirthdayParadox();

			break;

		case '3':
			//DisplayBirthdaySet();
			cout<<"Menu 3\n\n";

			break;

		case 'E':
		case 'e':

			break;

		default:
			cout<<"Invalid selection\n\n";
	}
}

void VerifyBirthdayParadox()
{
	int Set[SAMPLE_SIZE];
	float NumberOfMatches = 0;

	for(int k = 0; k < NUMBER_OF_SETS; k++)
	{
		GenerateBirthdaySet(Set);

		for(int j = 1; j < SAMPLE_SIZE; j++)
		{
			if(Set[j] == Set[j - 1])
				NumberOfMatches++;
		}
	}
	
	DisplayBirthdayParadoxResults(NumberOfMatches);
}

void GenerateBirthdaySet(int BirthdaySet[])
{
	for(int i = 0; i < SAMPLE_SIZE; i++)
		BirthdaySet[i] = (rand() % 365) + 1;
	
	SelectionSort(BirthdaySet, SAMPLE_SIZE);
}

void DisplayBirthdayParadoxResults(float Matches)
{
	float Average = (Matches / NUMBER_OF_SETS) * 100;

	char DashLine[DASH_LENGTH];

	cout<<"Generating "<<NUMBER_OF_SETS<<" sets of "<<SAMPLE_SIZE
		<<" birthdays and checking for matches...\n\n";

	cout<<"Results : "<<Matches<<" out of "<<NUMBER_OF_SETS<<" ("
		<<setprecision(1)<<Average
		<<"%) of the sets contained matching birthdays\n";

	for(int i = 0; i < DASH_LENGTH; ++i)
	{
		DashLine[i] = '=';
		cout<<DashLine[i];
	}

	cout<<"\n\n";
}		

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: C++ Finding the Birthday Paradox

Posted 02 October 2012 - 02:21 PM

Where's your SelectionSort implementation?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4019
  • View blog
  • Posts: 12,406
  • Joined: 25-December 09

Re: C++ Finding the Birthday Paradox

Posted 02 October 2012 - 02:30 PM

Your program seems to run properly the first time (after I remove the call to selectionsort()), but not for further times. This is being caused by the the setprecision(1) in your DisplayBirthdayParadoxResults() function. The setprecision is "sticky" meaning that it will keep that value until you change it again. 6e+001 is actually 60, which is correct for a precision of 1. To fix this issue you have some choices. First you can add the following lines:
cout.setf(ios::showpoint | ios::fixed);
cout << setprecion(1);

Before the start of your output. Or you could print Matches as in int by casting:
cout << static_cast<int>(Matches);



Jim

This post has been edited by jimblumberg: 02 October 2012 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

#6 Charlwillia6  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 17-September 12

Re: C++ Finding the Birthday Paradox

Posted 02 October 2012 - 07:30 PM

Quote

cout.setf(ios::showpoint | ios::fixed);
cout << setprecion(1);

Before the start of your output. Or you could print Matches as in int by casting:
cout << static_cast<int>(Matches);



Jim


Ok, I got the average to work. Thank you. Here is the code from a file called sel-sort.cpp that someone else wrote that I am using for selectionsort()

/************************  Swap  *****************************************

  DESCRIPTION  Interchanges the contents of A and B.

  PARAMETERS   IN-OUT  A and B, the variables to swap

  NOTE         This function is declared to be inline so that the overhead
               of a function call is avoided.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
inline void Swap (int &A, int &B)/>
{
  int Temp= A;
  A = B;
  B = Temp;
}

/*************************  SelectionSort *********************************

DESCRIPTION  Sorts the members of the array List[1..Last]. (The array
             element in position 0 is ignored.) This version of the
             selection sort works by finding the smallest element of the
             entire array and placing it in its correct position (1).
             Next the smallest element in List[2]..List[Last] is found
             and placed in position 2. After step N, the elements
             List[1]..List[N] are in their correct places.

PARAMETERS
  List       IN-OUT, an array with subscripts from 0..Last of int

  Last       IN, the subscript of the last array member to be sorted

CALLS        Swap, an inline function to swap array elements.

NOTE:        To sort List[0..Last] change for loop to:

             for ( Start = 0; Start < Last; ++Start )

Precondition: Last does not go outside scope of array
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
void SelectionSort (int List[], 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 ( List[i] < List[SubSmall] )        //   elt. in tail end of array
        SubSmall= i;

    Swap(List[SubSmall],  List[Start]);      // Place in correct position
    }
}


Maybe I should make my own bubble sort. I guess I better look that up. Unless you can pinpoint my problem.

Maybe this is the source of my average being too high too.

This post has been edited by Charlwillia6: 02 October 2012 - 07:31 PM

Was This Post Helpful? 0
  • +
  • -

#7 Charlwillia6  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 17-September 12

Re: C++ Finding the Birthday Paradox

Posted 02 October 2012 - 07:55 PM

Ok, I changed:

SelectionSort(BirthdaySet, SAMPLE_SIZE);


to:

SelectionSort(BirthdaySet, SAMPLE_SIZE - 1);


and I no longer get the error. I don't know if this is correct.

Also, if I change the code in the sel-sort.cpp file from:

for ( Start = 1; Start < Last; ++Start )   // Sort items 0..Last


to:

for ( Start = 0; Start < Last; ++Start )   // Sort items 0..Last


it doesn't seem to make a difference without changing the SAMPLE_SIZE - 1 part. However, the problem is my averages are still really high. In the 60% to 73% range. The average for this program should be closer to 50% almost the majority of the time. So I don't really know what is wrong at this point for sure.
Was This Post Helpful? 0
  • +
  • -

#8 Charlwillia6  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 17-September 12

Re: C++ Finding the Birthday Paradox

Posted 02 October 2012 - 11:33 PM

When selecting menu option 3, I am supposed to get something that looks like:

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

Whenever the same month with the same day shows up, it is supposed to only show it once with a (NumberOfMatches) (ex: (2) October 12), and then go on to the next birthday without a match. However I am getting something that looks like this:


1) Explain birthday paradox
2) Check birthday paradox by generating 1000 sets of birthdays
3) Display one set of 23 birthdays
E) Exit

3

Here are the results of generating a set of 23 birthdays
======================================================================
January 10 January 17 January 21
January 27 February 1 April 1
April 9 May 8 May 17
May 17 May 29 June 2
June 29 July 7 July 27
August 7 August 19 September 21
October 11 October 30 November 14
November 20 December 10

As you can see, as in May 17 showing up twice, that it is still outputting the birthday twice if there is a match, and it is not adding up and putting the total of matches next to the matched birthday in the first column.

My code so far:

#include <iostream>
#include <ctime>
#include <iomanip>

using namespace std;

void DisplayMenu();
void MenuChoice(char Choice);
void VerifyBirthdayParadox();
void GenerateBirthdaySet(int BirthdaySet[]);
void DisplayBirthdayParadoxResults(int Matches);
void SelectionSort(int List[], int  Last);

void DisplayBirthdaySet();
void ConvertDayOfYear(int DayOfYear, int& MonthNum, int &DayNum);
int DaysInMonth(int MonthNum);
void ConvertMonthNumberToMonthName(int MonthNum[], int SetNumber);
void PrintResultsAsColumns(int Match[], int Month[], int Day[]);

const int SAMPLE_SIZE = 23;
const int NUMBER_OF_SETS = 1000;

int main()
{
	char Choice;

	srand((unsigned)time(0));

	do
	{
		Choice = '\0';

		DisplayMenu();

		cin>>Choice;
		cout<<"\n";

		MenuChoice(Choice);
	}
	while(Choice != 'e' && Choice != 'E');
}

void DisplayMenu()
{
	cout<<"1)	Explain birthday paradox\n";
	cout<<"2)	Check birthday paradox by generating 1000 sets of birthdays\n";
	cout<<"3)	Display one set of 23 birthdays\n";
	cout<<"E)	Exit\n\n";
}

void MenuChoice(char Choice)
{
	switch(Choice)
	{
		case '1':
			cout<<"If 23 people are chosen at random, then the chances are more than 50%\n"
				<<"that at least two will have the same birthday!\n\n";

			break;

		case '2':
			VerifyBirthdayParadox();

			break;

		case '3':
			DisplayBirthdaySet();

			break;

		case 'E':
		case 'e':

			break;

		default:
			cout<<"Invalid selection\n\n";
	}
}

void VerifyBirthdayParadox()
{
	int Set[SAMPLE_SIZE];
	int NumberOfMatches = 0;

	for(int k = 0; k < NUMBER_OF_SETS; k++)
	{
		GenerateBirthdaySet(Set);

		SelectionSort(Set, SAMPLE_SIZE - 1);

		for(int j = 0; j < SAMPLE_SIZE; j++)
		{
				if(Set[j] == Set[j - 1])
					NumberOfMatches++;
		}
	}
	
	DisplayBirthdayParadoxResults(NumberOfMatches);
}

void GenerateBirthdaySet(int BirthdaySet[])
{
	for(int i = 0; i < SAMPLE_SIZE; i++)
		BirthdaySet[i] = (rand() % 365) + 1;
}

void DisplayBirthdayParadoxResults(int Matches)
{
	float Average = (static_cast<float>(Matches) / NUMBER_OF_SETS) * 100;

	cout<<"Generating "<<NUMBER_OF_SETS<<" sets of "<<SAMPLE_SIZE
		<<" birthdays and checking for matches...\n\n";

	cout<<"Results : "<<Matches<<" out of "<<NUMBER_OF_SETS<<" (";
	cout.setf(ios::showpoint | ios::fixed);
	cout << setprecision(1)<<Average
		<<"%) of the sets contained matching birthdays\n";

	cout<<setfill('=')<<setw(70)<<"="<<endl;

	cout<<"\n\n";
}	

void DisplayBirthdaySet()
{
	int MonthNumber, DayNumber, Set[SAMPLE_SIZE], DaySet[SAMPLE_SIZE], 
		MonthSet[SAMPLE_SIZE], MatchSet[SAMPLE_SIZE], Match = 0;

	cout<<"Here are the results of generating a set of 23 birthdays\n";

	cout<<setfill('=')<<setw(70)<<"="<<endl;
	cout<<setfill(' ');

	GenerateBirthdaySet(Set);

	SelectionSort(Set, SAMPLE_SIZE - 1);

	for(int i = 0; i < SAMPLE_SIZE; i++)
	{
		ConvertDayOfYear(Set[i], MonthNumber, DayNumber);

		DaySet[i] = DayNumber;
		MonthSet[i] = MonthNumber;

		if(MonthSet[i] == MonthSet[i + 1])
		{
			MatchSet[i] = Match++;
			MonthSet[i + 1] = MonthSet[i + 2];
			DaySet[i + 1] = DaySet[i + 2];
		}
		else
			MatchSet[i] = 0;
			Match = 0;
	}

	PrintResultsAsColumns(MatchSet, MonthSet, DaySet);

	cout<<"\n\n";
}

void PrintResultsAsColumns(int Match[], int Month[], int Day[])
{
	for(int i = 0; i < SAMPLE_SIZE; ++i)
	{
		if(Match[i] > 0)
			cout<<" ("<<Match[i]<<") ";
		else
			cout<<"     ";

		ConvertMonthNumberToMonthName(Month, i);

		cout<<right<<setw(10)<<Day[i];

		if((i == 2) || (i == 5) || (i == 8) || (i == 11) || (i == 14)
			|| (i == 17) || (i == 20))
			cout<<endl;
	}
}

void ConvertMonthNumberToMonthName(int MonthNum[], int SetNumber)
{
		if(MonthNum[SetNumber] == 1)
			cout<<left<<setw(10)<<"January";
		else if(MonthNum[SetNumber] == 2)
			cout<<left<<setw(10)<<"February";
		else if(MonthNum[SetNumber] == 3)
			cout<<left<<setw(10)<<"March";
		else if(MonthNum[SetNumber] == 4)
			cout<<left<<setw(10)<<"April";
		else if(MonthNum[SetNumber] == 5)
			cout<<left<<setw(10)<<"May";
		else if(MonthNum[SetNumber] == 6)
			cout<<left<<setw(10)<<"June";
		else if(MonthNum[SetNumber] == 7)
			cout<<left<<setw(10)<<"July";
		else if(MonthNum[SetNumber] == 8)
			cout<<left<<setw(10)<<"August";
		else if(MonthNum[SetNumber] == 9)
			cout<<left<<setw(10)<<"September";
		else if(MonthNum[SetNumber] == 10)
			cout<<left<<setw(10)<<"October";
		else if(MonthNum[SetNumber] == 11)
			cout<<left<<setw(10)<<"November";
		else if(MonthNum[SetNumber] == 12)
			cout<<left<<setw(10)<<"December";
}

void ConvertDayOfYear(int DayOfYear, int& MonthNum, int &DayNum)
{
	MonthNum = 1;

	while(DayOfYear > DaysInMonth(MonthNum))
	{
		DayOfYear = DayOfYear - DaysInMonth(MonthNum);
		MonthNum++;
	}

	DayNum = DayOfYear;
}

int DaysInMonth(int MonthNum)
{
	switch(MonthNum)
	{
		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;
	}
}


Also, my average results when going to menu option 2 is also still way too high. Thank you for any help that you can give me. The following is about how high my results are all the time:

1) Explain birthday paradox
2) Check birthday paradox by generating 1000 sets of birthdays
3) Display one set of 23 birthdays
E) Exit

2

Generating 1000 sets of 23 birthdays and checking for matches...

Results : 659 out of 1000 (65.9%) of the sets contained matching birthdays
======================================================================

1) Explain birthday paradox
2) Check birthday paradox by generating 1000 sets of birthdays
3) Display one set of 23 birthdays
E) Exit

2

Generating 1000 sets of 23 birthdays and checking for matches...

Results : 713 out of 1000 (71.3%) of the sets contained matching birthdays
======================================================================
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: C++ Finding the Birthday Paradox

Posted 03 October 2012 - 02:21 AM

View PostCharlwillia6, on 02 October 2012 - 10:55 PM, said:

Ok, I changed:

SelectionSort(BirthdaySet, SAMPLE_SIZE);


to:

SelectionSort(BirthdaySet, SAMPLE_SIZE - 1);


and I no longer get the error. I don't know if this is correct.


Yes it is correct, which you can tell from reading the documentation in that file:

PARAMETERS
  List       IN-OUT, an array with subscripts from 0..Last of int

  Last       IN, the subscript of the last array member to be sorted


Note the Last parameter. If your SAMPLE_SIZE is 10, then what is the subscript of the last array member to be sorted?

View PostCharlwillia6, on 02 October 2012 - 10:55 PM, said:

Also, if I change the code in the sel-sort.cpp file from:

for ( Start = 1; Start < Last; ++Start )   // Sort items 0..Last


to:

for ( Start = 0; Start < Last; ++Start )   // Sort items 0..Last


it doesn't seem to make a difference without changing the SAMPLE_SIZE - 1 part. However, the problem is my averages are still really high. In the 60% to 73% range. The average for this program should be closer to 50% almost the majority of the time. So I don't really know what is wrong at this point for sure.


If someone provides you with a function that does what you need, you don't go changing that function because you didn't read the documentation properly.

void DisplayBirthdaySet()
{
	int MonthNumber, DayNumber, Set[SAMPLE_SIZE], DaySet[SAMPLE_SIZE], 
		MonthSet[SAMPLE_SIZE], MatchSet[SAMPLE_SIZE], Match = 0;

	cout<<"Here are the results of generating a set of 23 birthdays\n";

	cout<<setfill('=')<<setw(70)<<"="<<endl;
	cout<<setfill(' ');

	GenerateBirthdaySet(Set);

	SelectionSort(Set, SAMPLE_SIZE - 1);

	for(int i = 0; i < SAMPLE_SIZE; i++)
	{
		ConvertDayOfYear(Set[i], MonthNumber, DayNumber);

		DaySet[i] = DayNumber;
		MonthSet[i] = MonthNumber;

		if(MonthSet[i] == MonthSet[i + 1])
		{
			MatchSet[i] = Match++;
			MonthSet[i + 1] = MonthSet[i + 2];
			DaySet[i + 1] = DaySet[i + 2];
		}
		else
			MatchSet[i] = 0;
			Match = 0;
	}

	PrintResultsAsColumns(MatchSet, MonthSet, DaySet);

	cout<<"\n\n";
}


Do you expect that Match variable to get set to 0 only when if(MonthSet[i] == MonthSet[i + 1]) is false?
Was This Post Helpful? 0
  • +
  • -

#10 Charlwillia6  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 17-September 12

Re: C++ Finding the Birthday Paradox

Posted 03 October 2012 - 08:15 AM

View PostJackOfAllTrades, on 03 October 2012 - 02:21 AM, said:

If someone provides you with a function that does what you need, you don't go changing that function because you didn't read the documentation properly.


Excuse me? I did read the documentation properly, and I didn't change anything about the function that it didn't say to change depending on my needs. So I don't really think that was necessary.

Quote

Do you expect that Match variable to get set to 0 only when if(MonthSet[i] == MonthSet[i + 1]) is false?


Really, I am lost at this point I am lost. I know the code is wrong, but all in all, I don't know how to get to do what I want it to do, which is print the match, when there is one, and not print it when there isn't one. I also can't get to only print the match month and day once. Also, if there are two matches, then the match counter goes to 2 instead of starts back at 1. But everytime I put in the code for it to be reset back to 0, then it doesn't print the match counter at all in the results, even when there are matches.
Was This Post Helpful? 0
  • +
  • -

#11 Charlwillia6  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 17-September 12

Re: C++ Finding the Birthday Paradox

Posted 03 October 2012 - 06:58 PM

Ok, worked this all out on my own. Anyone need to know what the finished code looks like, pm me. Thanks for the help everyone.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1