Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Birthday Paradox Looping

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


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>