Can anyone help me with this one? I have the following code and produces an output, but the Probability stays at 1 regardless of how many students I input....Any ideas?

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;


void main()
{
	int numStudents, DayOfBirth, cnt;
	double Probability, Power;

	cout << "Enter Number of Students in The Room: " << endl;
	cin >> numStudents;
	cout << endl;


	Power = (numStudents * (numStudents - 1)) / 2;
	Probability = (1 - pow((364)/(365) , Power));

	cnt = 0;
	while (cnt < numStudents)
	{
		DayOfBirth = rand() % 365;
		cout << "Student Date of Birth is: " << DayOfBirth << endl;
		cnt++;
	}
	cout << "Number of Students is: " << cnt << endl;
	cout << "Probability of sharing DOB: " << Probability << endl;

}

Recommended Answers

All 2 Replies

For your ratio try 364.0/365.0, you probably keep getting 0 for 364/365 since it's still considered integer division.

For your ratio try 364.0/365.0, you probably keep getting 0 for 364/365 since it's still considered integer division.

Yep, that did it! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.