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

Bad syntax above, try this:

bool operator==(const Birthday &x, const Birthday &y) {
  return (x.getMonth() == y.getMonth()) && (x.getDay() == y.getDay());    
}
bool operator==(const Birthday &x, const Birthday &y)
{
  return (x.getMonth() == y.getMonth()) && (x.getDay() == y.getDay()); 
  }

the code saysa pointer to a bound function may only be used to call the function

Line 128 of your original code should be

if (*(people[j]) == *(people[k]))

Otherwise im not sure what the error is.

  1. getMonth and getDay both need to be const methods they are called through const references in operator==

  2. daysInMonth doesn't return a value for all input values

  3. daysInMonth is missing a Birthday:: from its definition

  4. You have some comparisons between signed and unsigned integers

i am still having problems on the last implementation part which says Expression must have class type

bool Birthday::birthday ()
if ((x.getMonth==y.getMonth) && (x.getMonth==y.getMonth))

{
return TRUE;
}

else return FALSE;

my plan was actually to make a function to handle the condition if month==date to be able to return TRUE else FALSE, is there any way to do this?

This article has been dead for over six months. Start a new discussion instead.