-1

I am trying to figure out the Birthday Paradox program for my C++ class. This is what I have so far, but it's not working. I tried looking through other questions about the same topic on here but I'm still pretty lost so any help would be greatly appreciated.

//This program calculates the likelihood of any two people in groups of 2-50 people 
//having their birthday on the same day, also known as the Birthday Paradox.

#include <iostream>
using namespace std;

int main()
{
int people, trial, count = 0, birthdays[50], numMatches, NUM_TRIALS = 5000;
double total;

//function call
sameBirthday(birthdays, people);
numMatches = 0;

for (people = 2; people <= 50; people++)
{
    //Run trials to see if people have the same birthday
    //Reset number of matches
    numMatches = 0;
    for (trial = 0; trial < NUM_TRIALS; trial++)
    {
        //Randomly generate up to "people" birthdays
        for (int i = 0; i < people, i++)
        {
            birthdays[people] = (rand() % 365) + 1;
            //Check to see if any two people have the same birthday
            for (i = 1; i < people; i++)
            {
                //birthday one
                for (int j = 0; j < i-1; j++)
                {
                    //birthday two
                    for (int k = j +1; k < i; k++);
                }
            }
        }
    }
}

bool sameBirthday(int birthdays[], int people)
{
    //if the two birthdays are the same, add one to the count 
    if (birthdays[j] == birthdays[k])
    {
        people++;
    }

    total = (numMatches / 5000.0);
    cout << "For " << people << " people, the probability of two birthdays is about " << total << endl;

}

return 0;
  • Possible duplicate (pick one) ["stackoverflow c++ birthday paradox"](https://www.google.com/search?q=stackoverflow+c%2B%2B+birthday+paradox&ie=utf-8&oe=utf-8) – Thomas Matthews Sep 01 '15 at 19:16
  • When you used the debugger, which statements are presenting the issue? – Thomas Matthews Sep 01 '15 at 19:17
  • Here's one issue: `birthdays[people] =`. The variable `people` is not changing in the loop and is outside the range of the array. Did you mean to use the first `i` variable? – Thomas Matthews Sep 01 '15 at 19:19
  • You have two loops using an index variable of `i`. Although the compiler is not confused, are you? Perhaps you should change the name of one of the `i` variables. – Thomas Matthews Sep 01 '15 at 19:20
  • Your `k` loop doesn't do anything, get rid of it or give it some content. – Thomas Matthews Sep 01 '15 at 19:21
  • In the function `sameBirthday`, the `j` and `k` variables are not defined (in the function). This function *cannot* access variable in another function (like main). I guess you'll have to **pass them** instead. Likewise with `total`, and `numMatches`. The code in this function is to small, move it to where you call the function. – Thomas Matthews Sep 01 '15 at 19:22
  • You have a lonely `return 0;` statement. I would be happier inside a function. – Thomas Matthews Sep 01 '15 at 19:24
  • In the `sameBirthday` function, you are **passing a copy** of the `people` variable. Incrementing will not affect the original variable because you passed by value/copy. Maybe you want to pass by reference instead? – Thomas Matthews Sep 01 '15 at 19:26
  • My last comment: your variable `numMatches` is set to zero, but never any other value. I highly suggest you sit with your instructor and discuss what you've learned or take a different C++ class. – Thomas Matthews Sep 01 '15 at 19:29

1 Answers1

0
for (people = 2; people <= 50; people++)
{
    //Run trials to see if people have the same birthday
    //Reset number of matches
    numMatches = 0;
    for (trial = 0; trial < NUM_TRIALS; trial++)
    {
        //Randomly generate up to "people" birthdays
        for (int i = 0; i < people, i++)
        {
            //here I use i instead of people so every time i put the new number in a different position
            birthdays[i] = (rand() % 365) + 1;

            //this loop check if some birthday is equal to the one just generated
            for(int j = 0; j < i; j++){
                if(birthday[j] == birthday[i]){
                    //here do what u want to do when 2 people have the same birthday
                }
            }                
        }
    }
}

try to use this loop, in this way you check for each number of people from 2 to 50 if there are 2 people with the same birthday.

WellDone2094
  • 175
  • 1
  • 9