#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)
cout<<"Error, please try again.";
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
}
}
Birthday Paradox Looping
Page 1 of 110 Replies - 204 Views - Last Post: 19 February 2013 - 04:41 AM
#1
Birthday Paradox Looping
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!!
Replies To: Birthday Paradox Looping
#2
Re: Birthday Paradox Looping
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
regards,
Raghav
#3
Re: Birthday Paradox Looping
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
Re: Birthday Paradox Looping
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...JackOfAllTrades
regards,
Raghav
Edit : Right on...JackOfAllTrades
regards,
Raghav
This post has been edited by raghav.naganathan: 19 February 2013 - 04:12 AM
#5
Re: Birthday Paradox Looping
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
Re: Birthday Paradox Looping
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
Re: Birthday Paradox Looping
Posted 19 February 2013 - 04:20 AM
So...what does your updated code look like?
regards,
Raghav
regards,
Raghav
#8
Re: Birthday Paradox Looping
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)
cout<<"Error, please try again.";
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
Re: Birthday Paradox Looping
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
regards,
Raghav
#10
Re: Birthday Paradox Looping
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!
i got normal numbers now without dividing by 100.
Thanks so much!
#11
Re: Birthday Paradox Looping
Posted 19 February 2013 - 04:41 AM
Most welcome
Glad to help 
regards,
Raghav
regards,
Raghav
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|