C++ Probability of Birthday Paradox for Room of 50 People

In summary: If you're in a C class, don't use new/delete, use malloc/free.It is also possible to pass the size of the array as an additional parameter to the function. For example:bDayParadox(people, numPeople);And in the function definition:int bDayParadox(int people[], int numPeople) { // code here}
  • #1
arkturus
27
0

Homework Statement


Write a program that approximates the probability that at least two people in a room of 50 people have the same birthday. Run 5,000 trials, count up the number at least two people have the same birthday, and divide by the number of trials to get a probability.


Homework Equations





The Attempt at a Solution



Code:
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int bDayParadox (int people[]);

///////////////////////////////////////////////////

int main ()

{
  
  int probability;
  int sum = 0;
  const int trials = 5000;
  int people[49];
 
 for (int j = 0; j < trials; j++)
   {
     sum = sum + bDayParadox(people); 
   }
 
 probability = (sum / trials);

 cout <<  bDayParadox(people) << endl;
 cout << sum << endl;
 cout << trials << endl;
 cout << probability << endl;
   
  


}

/////////////////////////////////////////////////

int bDayParadox (int people[])
{

  int coincidence = 0;
  srand(time(NULL)); //generates seed for random numbers

  for (int a = 0; a < 50; a++)
      {
       people[a] = rand() % 365 + 1;
      }

  for (int b = 0; b < 50; b++)
      {
        for (int c = b+1; c < 50; c++)
	  { 
	   if (people[c] == people[b])
	     {     
	       coincidence = 1;
	     }


	   }
      }
  return coincidence;
 
}

The function I made, bDayParadox, puts random numbers (from 1 - 365) into an array of 50. The function then checks to see if at least two of the values are similar, and if so, counts it as 1 (coincidence variable). Up top, I'm running the function 5000 times and adding the values together to get sum. Sum divided by trials should give me the probability.

My output looks like this:

1 (bDayParadox run once)
5000 (sum)
5000 (trials, the constant)
353 (probability)

The value that is spit out for probability is unique each time.

My main problem with this is that probability should not be what it says. Also, my sum shouldn't always be 5000.
 
Physics news on Phys.org
  • #2
1. Correct your array size.
2. Probability is not an integer.
3. Initialize the random number generator once. You are getting the same sequence over and over again.
 
  • #3
Thank you very much. That cleared up my issues perfectly.
 
  • #4
I've come apon another small issue.

I want to make arrays of different sizes, from 2 - 50. In int main(), there is something like this:

Code:
for (numPeople = 2; numPeople <= 50, numPeople++)
              bDayParadox(people)

In the function definition of bDayParadox, I have:

Code:
for (int a = 0; a < numPeople; a++)
      {
       people[a] = rand() % 365 + 1;
      }

How do I go about getting the numPeople number from the int main() for loop into my function definition? Apparently I'm doing something wrong.
 
  • #5
Look up pointers, dynamical allocation and operators new/delete.
 

Related to C++ Probability of Birthday Paradox for Room of 50 People

1. What is an array in C++?

An array in C++ is a data structure that stores a fixed number of elements of the same data type in a contiguously allocated memory space. It allows for the efficient storage and retrieval of data, as well as easy manipulation of multiple values using loops.

2. How do I declare an array in C++?

To declare an array in C++, you use the syntax: data_type array_name[array_size]; where data_type is the type of data that will be stored in the array, array_name is the name of the array, and array_size is the number of elements the array can hold.

3. What is the difference between a fixed-size array and a dynamic array in C++?

A fixed-size array in C++ has a predetermined size that cannot be changed during runtime, whereas a dynamic array can be resized during runtime using the new and delete operators. Dynamic arrays also have the advantage of being able to allocate memory on the heap, allowing for more flexibility in memory management.

4. How do I access elements in an array in C++?

You can access elements in an array in C++ using their index values. Indexing in C++ starts at 0, so the first element in an array will have an index of 0. You can use square brackets [] after the array name to specify the index of the element you want to access. For example, array_name[0] will give you the value of the first element in the array.

5. What are some common issues with arrays in C++?

Some common issues with arrays in C++ include out-of-bounds errors, where the program tries to access an element outside the bounds of the array; memory leaks, where memory allocated for a dynamic array is not properly deallocated; and difficulties with resizing dynamic arrays, as it involves copying and reallocating memory.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
929
Back
Top