Write the following in C program: 1. There\'s a birthday in our class today! Or
ID: 3812187 • Letter: W
Question
Write the following in C program:
1. There's a birthday in our class today! Or will there be two? How likely it is that two people in one section of our class (40 students have the same birthday? Suggested Approach 1. Prepare an integer array, a 40, to hold birthdays for 40 students. 2. Generate a random number between 1 and 365 and assign the number to each element of all. Use the operator (Example: 300%7 returns the remainder of 300 divided by 7). 3. Compare al0] with the rest of the students' birthdays (i.e. a[1], a[2], a122]) 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). 4. Compare alij with the rest of the students' birthdays (i.e. a[2], a[3], a122]) 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). 5. Compare a12] with the rest of the students' birthdays (i.e. a a 4], a122]) 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). 6. Compare a13] with the rest of the students' birthdays (i.e. a[4], a 5, a1221) 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). 8. After n simulations, compute the value of counter/n.Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
main()
{
int a[40]; /*array a to store 40 students Birthdays*/
int i=0,j=0; /* i and j are loop variables*/
int counter=0; /* stores total birthdays on the same day */
int rem=0; /* stores remainder */
float value=0.0; /* stores value after N similations */
clrscr(); /*to clear output screen */
for(i=0;i<40;i++) /*for loop begins */
{
rem =random(365)+1; /* It generates a random value between 1 to 365 and stored as Array element */
a[i]=rem%7; /* stores remainder*/
printf(" Array Elements :%d",a[i]); /* prints array values */
}
for(i=0;i<39;i++) /* i loop begins i=0..38*/
{
for(j=i+1;j<40;j++) /* j loop begins j=1..39 */
if(a[i] == a[j]) /*checking is there any other student birthday on the same day */
{
counter++; /* if found increment counter by 1 */
goto ExitLoop; /* goto immediately transfers control to specified label Exitloop */
} /* end of j loop */
ExitLoop: ; /*label*/
} /*end of i loop */
value=(float)counter/40; /* After N similations finding the value */
printf(" The value of counter/n is %-6.2f",value); /* printing the value */
} /* end of main function */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.