We were assigned the birthday paradox as a homework assignment. The program intakes a number of people to check and returns the number of people with a shared birthday.
I seem to be having trouble figuring out the loops for the function check_birthdays. I input a number and a number (usually arund 3000) is returned. Any help would be appreictaed!! Have to hand it in tomorrow!!! EEK!

#include <iostream>
using namespace std;
#include<time.h>



void check_birthdays (int birthdays[], int num, int count=0)
{
        for (int i=0; i<num; i++) //to check each person in the group
        {
            for (int j=i+1; j<num; j++) //against every other person in the group
            {
                if (birthdays[i]==birthdays[j])
                   count++; 
            }
        }


    //print out the number of people with same birthday
   cout<<"The number of people who share their birthday is "<<count;

}



int main()

{
 //create a variable for an inputted number of people
    int people, count;
    cout<< "Please input a number of people: "<<endl;;
    cin>>people;

   int birthdays[people];

    //input check
   if (people<50 || people>100)
        cout<<"Error, please try again.";
    else
    { 
        for (int i=0; i<people; i++)
        {
            srand (time (NULL));
            birthdays[i]= rand()%365;
        }
        check_birthdays (birthdays, people, count); 



    }


}

line 34 should be

int * people = new int[people];

see if that makes a difference. Also on line 51 put

delete [] people;

That is because in main you pass an initial value for count to check_birthdays but you do not initialise the value you pass. This is actually undefined behaviour.

A quick fix is to initialise the variable count in main (to 0) or to call check_birthdays as check_birthdays (birthdays, people); thereby using the default parameter value of 0. However since there seems to be no need to pass an initial count value to check_birthdays a better fix might be to remove the count parameter from the function birthdays and instead use a variable local to the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.