Help with Passing value in and out of function

I need some advice from the smart folks here at cplusplus forum. I have a homework assignment where I am proving the birthday paradox that if 23 persons are chosen at random, then the chances are more than 50% that at least two will have the same birthday. For the most part, I've got the calculation and the coding down. The problem I am having is after I call up a function to randomly generate 23 birthdates and store that into an int array. Then call up a sorting function to put it into order. I need to pass that into a function to convert the number todays (ie: 37days into February 6). I've solved the problem of converting the raw number into the day of the year and the month of the year. My thinkinging is that I could pass that sorted array into the converting function and pass a number of day and number of month back out by reference. Problem is how do i cycle through the array to do the calculation and return the numbers back out ? Can I use a loop of some sort. Any advice would be great help.

I need your help with the DisplayBDaySet function. If you could guide me in the right direction.

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






This is what I need it to look like ultimately :)

============================================================
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
Last edited on
This is what I need it to look like ultimately :)
why?
Once you generate your 1000 arrays of 23 random numbers in the range of 1 to 365 all you need to do is count the number of 1's,2's...365's in each of the 1000 arrays using nested for loops, the first to deal with each array and the second to deal with the 23 elements in each. Use count_1, count_2 ... count_365 to keep the score for each element. after that you can convert the year_day int to date string.
Topic archived. No new replies allowed.