Question
Write C programs for the following: There's a birthday in our class today! Or will there be two ? How likely is it that two
Write C programs for the following: There's a birthday in our class today! Or will there be two ? How likely is it that two people in a class of 30 students have the same birthday ?Suggested Approach:
Prepare an integer array, a[0], a[1], a[2],, a[29], that holds birthday data for 29 students.
Generate a random number between 1 and 365 and assign the number to each element of a[]. Use the % operator (Example: 300%7 returns the remainder of 300 divided by 7).
Compare a[0] with the rest of the students' birthdays (i.e. a[1], a[2], ..... a[29]) and if there is a match, get out of the loop, increment the counter by 1 and go to #2 (the next round of simulation).
Compare a[1] with the rest of the students' birthdays (i.e. a[2], a[3], ..... a[29]) and if there is a match, get out of the loop, increment the counter by 1 and go to #2 (the next round of simulation).
Compare a[2] with the rest of the students' birthdays (i.e. a[3], a[4], ..... a[29]) and if there is a match, get out of the loop, increment the counter by 1 and go to #2 (the next round of simulation).
Compare a[3] with the rest of the students' birthdays (i.e. a[4], a[5], ..... a[29]) and if there is a match, get out of the loop, increment the counter by 1 and go to #2 (the next round of simulation).
.
After n simulations, compute the value of counter/n.
Example of goto statement (to get out of the loop):
for (i=0;i |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started