10 Replies - 470 Views - Last Post: 19 February 2013 - 04:41 AM Rate Topic: -----

#1 narabo  Icon User is offline

  • New D.I.C Head

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

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!!

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



Is This A Good Question/Topic? 0
  • +

Replies To: Birthday Paradox Looping

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

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
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

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.
Was This Post Helpful? 2
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

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

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

Was This Post Helpful? 0
  • +
  • -

#5 narabo  Icon User is offline

  • New D.I.C Head

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

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?
Was This Post Helpful? 0
  • +
  • -

#6 narabo  Icon User is offline

  • New D.I.C Head

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

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
Was This Post Helpful? 0
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Birthday Paradox Looping

Posted 19 February 2013 - 04:20 AM

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

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#8 narabo  Icon User is offline

  • New D.I.C Head

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

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


Was This Post Helpful? 0
  • +
  • -

#9 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

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
Was This Post Helpful? 0
  • +
  • -

#10 narabo  Icon User is offline

  • New D.I.C Head

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

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!
Was This Post Helpful? 0
  • +
  • -

#11 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Birthday Paradox Looping

Posted 19 February 2013 - 04:41 AM

Most welcome :) Glad to help :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1