20 likes | 176 Views
Exercise 3. Suppose that we’re holding a party and that “n” people show up. If “n” is 1, we are guaranteed that nobody will have the same birthday as somebody else. for n = 1, probability of no shared birthdays = 1
E N D
Exercise 3 Suppose that we’re holding a party and that “n” people show up. If “n” is 1, we are guaranteed that nobody will have the same birthday as somebody else. for n = 1, probability of no shared birthdays = 1 If the party is a bit more successful and two people show up, the probability of nobody having the same birthday as somebody else is the probability that the second person wasn’t born on the same day as the first person. If we assume that all possible birthdays are equally probable, this is 365/366. for n = 2, probability of no shared birthdays = 1 * (365/366) If things really start to warm up and we get three people, the probability of nobody having the same birthday as somebody else is the probability that the first two people don’t have the same birthday (see above) multiplied by the probability that the third person wasn’t born on the same day as either of the first two people. for n = 3, probability of no shared birthdays = 1 * (365/366) * (364/366) And so on and so on. For six people, for example, the probability of nobody having the same birthday as somebody else is: 1 * (365/366) * (364/366) * (363/366) * (362/366) * (361/366) In all cases, the probability of at least two people sharing a birthday is 1 minus the probability that there are no shared birthdays. probability of shared birthday = 1 – probability of no shared birthdays Throughout the above, probabilities have been expressed as they usually are in mathematics – as a number between zero and one. A probability of zero means that something is impossible, and a probability of one means that something is certain. In everyday life (e.g. in weather forecasts) probabilities are often expressed as percentages, with a probability of 100% meaning that something is bound to happen. Going from one convention to the other is as simple as multiplying (or dividing) by 100.
You are to write a program which reads in the number of people at a party and outputs the probability (expressed as a percentage) that at least two people will share the same birthday. Running the sample executable supplied should give the idea. Try using it to work out how large a party must be for there to be a better than even chance that two people will share the same birthday. You may be surprised at how few people are required. Some notes: 1/ Your program should loop, like the sample executable, and repeatedly calculate probabilities until zero or a negative value is entered. 2/. In presenting formulae, it is reasonable to write 365/366. Writing the same thing in a C++ program, on the other hand, can cause problems… 3/. Don’t forget to consider that parties can get really big (e.g. have more than 366 people show up). 4/. If a number is really close to 100.0, it will appear as 100.0 when it is output (unless manipulators are used to force the display of a large number of decimal places). To get around this problem, have your program output “it is almost certain…” when the calculated probability exceeds 99.9999%. To see this feature in operation, try the sample executable for 96 and 97 people. For more advanced students (this part is optional): 1/. Modify the formulae to reflect the fact that fact that February 29th is less likely than other birthdays. 2/. Experiment with using I/O manipulators to force the display of a greater number of decimal places. 3/. Have your program give users the option of working out how many people must be present at a party to achieve a specified probability of at least two people sharing a birthday.