I am trying to write a program that will test for the birthday paradox. I need to run a series of experiments on randomly generated birthdays, which test this paradox for n = 5, 10, 15, 20,...,100 (n = number of people in any given room). I need to run at least 10 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 have the same birthday, But after running some lines of codes i am having problems on pointers...I need help here are the whole program
//birthday.cpp
#include "birthday.h"
Birthday::Birthday()
{
assignRandBirthday();
}
int Birthday::getMonth() {
return month;
}
int Birthday::getDay() {
return day;
}
int daysInMonth(int month) {
switch(month) {
case 1:
return 31;
case 2:
return 28;
case 3:
return 31;
case 4:
return 30;
case 5:
return 31;
case 6:
return 30;
case 7:
return 31;
case 8:
return 31;
case 9:
return 30;
case 10:
return 31;
case 11:
return 30;
case 12:
return 31;
}
}
void Birthday::assignRandBirthday()
{
month = (rand()%12)+1;
day = (rand()%daysInMonth(month))+1;
}
bool operator==(const Birthday &x, const Birthday &y)
{
if ((x->getMonth() == y->getMonth()) && (x->getDay() == y->getDay()))//i am getting errors here
return true;
else
return false;
}
birthday.h
#ifndef BIRTHDAY_H
#define BIRTHDAY_H
#include <cstdlib>
#include <iostream>
using namespace std;
class Birthday {
private:
int month;
int day;
void assignRandBirthday();
int daysInMonth(int month);
public:
Birthday();
int getMonth();
int getDay();
};
#endif
birthdayparadox.cpp
#include <vector>
#include<iostream>
#include "Birthday.h"
using namespace std;
int runExperiments(int numPeople) {
//Total of positive results
int total = 0;
// Go through 10 experiments with 'numPeople' different people
for (int i = 0; i < 10; i++) {
vector<Birthday*> people(numPeople);
bool positiveResult = false;
// Assign random birthdays to everyone
for (int j = 0; j < people.size(); j++)
people[j] = new Birthday;
//Check for same birthdays
for (int j = 0; j < people.size(); j++)
//If I don't already have a positive result
if (!positiveResult)
for (int k = j+1; k < people.size(); k++)
if (people[j] = people[k])
{
positiveResult = true;
total++;
}
// Is this proper garbage collection?
for (int j = 0; j < people.size(); j++)
delete people[j];
}
return total;
}
void testParadox() {
//Start with 5 people in room, increase by 5
for (int i = 5; i <= 100; i += 5) {
int positiveResults = runExperiments(i);
cout << "People in room: " << i << ", Positive results: " << positiveResults << '\n';
}
}
int main() {
testParadox();
return EXIT_SUCCESS;
}