1.
Find the 89-th largest
number from the list below (there are 200
numbers).
#10 lecture
a[200]={-1.39337, 10.3727, 15.9689, -7.10784, 11.778, -10.5861, 12.0269,
5.00089, -11.0546, 11.4257, 15.3895, 12.21, 16.2293, -7.78524,
17.0547, -9.2576, 11.6139, -2.81999, -5.73792, -14.4236, 7.27583,
-11.9733, 2.99635, 12.1867, 12.9151, 12.9556, -10.1303, -6.45815,
-3.67015, -11.7578, 4.65925, 13.8269, 14.9337, 8.97302, -11.301,
-0.467219, -9.61519, 11.7593, 8.98323, 0.271277, 13.1378, 19.6523,
16.2813, 5.17932, -13.6924, 13.8395, 18.4999, 11.5759, 2.20747,
1.56491, 1.37076, -7.26253, -3.59428, 7.70514, 10.7563, 18.7167,
7.19543, -2.02771, 6.48599, 10.1368, -10.4495, 4.13261, -5.21241,
4.30214, -13.2603, 11.5031, -0.312813, -6.30661, -6.15517, -0.405827,
9.72587, 1.54422, -10.6891, 8.32254, 14.3815, 2.30532, -8.96836,
14.4565, 16.0185, -3.07305, 0.229761, 9.85458, 0.637397, -9.7952,
-10.9266, 0.573555, -14.9854, -4.04496, 11.7241, -5.7671, -12.2193,
19.6379, 3.98946, 4.52508, -4.89169, -13.6319, 18.3039, -1.71856,
-5.85436, -8.0559, 14.162, 9.67661, 17.4084, 8.20915, -5.03675,
-10.3582, -10.1659, 5.95529, 18.8937, -12.5357, -0.740132, 12.4677,
-14.6572, 12.2239, -7.65577, -2.20275, -4.94527, -10.2728, -10.7105,
16.7323, -1.29954, -6.26693, -6.72612, 11.7878, -6.70938, 7.4103,
9.11681, 5.67036, -8.74336, 4.32963, 18.1047, -6.81381, 16.862,
17.4475, 13.0254, -1.56797, 6.76823, -1.80294, -4.54855, 9.78953,
-12.6283, -4.6273, 12.8411, -14.7345, -4.59939, -14.2311, -13.685,
8.58404, -4.0693, 8.37347, 17.8449, -7.5319, -2.68309, 1.44628,
3.65815, 7.14528, 6.25226, 12.6715, -12.115, 2.35859, 13.7662,
0.352634, 1.02687, 12.8877, -8.20454, 6.0551, 14.966, -10.9182,
-3.61947, 10.6442, 18.3244, 7.24864, 6.87792, 0.781124, -1.62436,
19.3366, -13.3064, -12.0147, -1.6737, -7.04164, 5.47671, -10.5729,
7.95191, 4.50753, -4.51829, 18.2905, 15.2672, -6.6864, -2.65211,
-12.2424, 19.9174, 0.460172, 9.58282, 16.2347, -5.08104, 6.73703,
7.83834, 19.5304, -5.69334, 19.4565};
2.
There's a birthday in our class today! Or will there be two ?
How likely is it that at least two people in a class of 24 students have the same birthday ?
Suggested Approach:
- Prepare an integer array, a[0], a[1], a[2],…, a[23], that holds birthday data for 24 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[23]) 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[23]) 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[23]) 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[23]) 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<N-1;i++) for (j=i+1;j<N;j++)
if(birthday[i]==birthday[j])
{count++;goto ExitLoop;}
ExitLoop:;
.....
|
Try 100,000 simulations five times and show their results.