Page 1 of 1

## 10 Replies - 1242 Views - Last Post: 19 February 2013 - 04:41 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=312702&amp;s=bcc603d6382a4da476d74bfbafd382e1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 narabo

Reputation: 0
• Posts: 7
• Joined: 19-February 13

Posted 19 February 2013 - 03:51 AM

Hi! We've been assigned the Birthday Paradox for homework and I seem to be having trouble with the looping in the check_birthdays function. I keep getting numbers in the thousands when it should really only be very few. Any help would really be appreciated! It's due 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 ame 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)
else
{ //fill that array with random numbers
for (int i=0; i<people; i++)
{
srand (time (NULL));
birthdays[i]= rand()%365;
}
check_birthdays (birthdays, people, count); //send to the next function

}

}

```

Is This A Good Question/Topic? 0

## Replies To: Birthday Paradox Looping

### #2 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

Posted 19 February 2013 - 04:02 AM

I have a feeling that you are getting the 1000's because of the srand function...why don't you comment it out and see if you get a lower value.

regards,
Raghav

• Saucy!

Reputation: 6237
• Posts: 24,002
• Joined: 23-August 08

Posted 19 February 2013 - 04:08 AM

srand should NEVER be called in a loop. It needs to be called ONCE, at the start of the program.

### #4 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

Posted 19 February 2013 - 04:10 AM

Better still, I think you can remove the srand from the for loop and put it somewhere after your line 33.

Edit : Right on...

regards,
Raghav

This post has been edited by raghav.naganathan: 19 February 2013 - 04:12 AM

### #5 narabo

Reputation: 0
• Posts: 7
• Joined: 19-February 13

Posted 19 February 2013 - 04:13 AM

Thanks for the quick response! I tried that and got back the number 6, meaning it made a big difference. So how do I assign each person a random birthday?

### #6 narabo

Reputation: 0
• Posts: 7
• Joined: 19-February 13

Posted 19 February 2013 - 04:18 AM

ok so i moved the srand to the beginning of the program and uncommented the random generation of birthdays. now i only get back 0

### #7 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

Posted 19 February 2013 - 04:20 AM

So...what does your updated code look like?

regards,
Raghav

### #8 narabo

Reputation: 0
• Posts: 7
• Joined: 19-February 13

Posted 19 February 2013 - 04:25 AM

```//Naomi Bollag- Weiss
//253

#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 ame birthday
cout<<"The number of people who share their birthday is "<<(count/100);

}

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

srand (time (NULL));

//input check
if (people<50 || people>100)
else
{ //fill that array with random numbers
for (int i=0; i<people; i++)
{
//here i removed the srand
birthdays[i]= rand()%365;
}
check_birthdays (birthdays, people, count); //send to the next function

}

}

```

### #9 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

Posted 19 February 2013 - 04:30 AM

Well, why did you do count/100...the reason you are getting 0 is because the value of count is very small and less than 100...so dividing it by 100 will give you 0.

regards,
Raghav

### #10 narabo

Reputation: 0
• Posts: 7
• Joined: 19-February 13

Posted 19 February 2013 - 04:40 AM

thanks, the professor told us to do the simulation 100 times, so i thought maybe dividing by 100 was the answer.
i got normal numbers now without dividing by 100.
Thanks so much!

### #11 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12