This exercise will get you started with programming in C.
For this exercise, you will need to download the template
ex1.c, and enter your code in the
methods (functions) provided.
You are welcome to create your own methods if you like.
You will also need to download two additional
C programs - the file uniform.c
and the file ex1test.c
into the same directory.
Do not make changes to uniform.c. You may change
or add test code to ex1test.c if you like.
Both files need to be in the same directory as your ex1.c.
Before you write your first line of code, you ought to try compiling
the template as follows:
gcc -o ex1 ex1test.c ex1.c uniform.c -lm
This will create an executable called ex1 that you can
execute (on Unix):
./ex1
In this exercise you will estimate two probabilities:
- Given a number M, the probability that in any random group of M people, at least
two people share a birthday.
- Given numbers M and K, the probability that in any random group
of M people, you find a subset of K people whose birthdays fall on K
consecutive days.
This is why there are two methods called
sharedBirthdayProbability (for the first problem) and
consecutiveBirthdaysProbability (for the second problem)
that you will implement. Let's look at the signature of each of these:
- First one:
double sharedBirthdayProbability (int numPeople, int numTrials)
{
/* INSERT YOUR CODE HERE */
}
This method takes in two parameters, the number of people (M) and the
number of random trials that you will use in your estimate.
- Second one:
double consecutiveBirthdaysProbability (int numPeople, int numConsecutive, int numTrials)
{
/* INSERT YOUR CODE HERE */
}
This method takes in three parameters: the number of people (M), the
number of consecutive days (K) and the number of trials to be used
in the estimate.
What do we mean by "number of trials"? First, consider how we are
going to solve each problem:
- Let's examine one approach to solving the first problem,
and let's consider one trial for now. Create an array of length 365
where each entry is initialized to zero. For each of M people, pick
a random day (a random spot in the array) as a birthday. If any spot
was picked by two or more people, we know that a birthday has been
shared. Of course, if we perform just one trial, we could be lucky
and observe a shared birthday or we could be unlucky and observe
no shared birthdays. So, to get an accurate estimate, we need
to repeat this experiment many times (perhaps 10000 times) and take
the average. In other words, we need to compute the fraction of trials
(out of 10000 trials) in which a shared birthday occurred.
- The second problem can be solved in a similar way, except that
you have to look for K consecutive birthdays.
Note: in this problem you have to worry about
"wrap around": for example (when K=3), the three days could be Dec 31, Jan 1 and Jan 2
(and should therefore count).
How does one pick a random day for a particular person? You will
use the discrete_uniform() method in the file uniform.c.
The call
i = discrete_uniform (1, 10);
returns an integer selected at random between 1 and 10 inclusive.
You can easily modify this to return a randomly selected day.
For practice, you might want to write the program in Java as well. To use the
equivalent program in Java, you will need to download
the file UniformRandom.java
and use the following method:
i = (int) UniformRandom.uniform (1, 10);
Deliverables:
- The source code for your program. Please write all your code in the
same file (ex1.c).
- The following estimates:
- Shared-birthday probability for M=10.
- What is the least value of M for which the shared-birthday
probability is larger than 0.5?
- The consecutive-birthdays probability for M=10 and K=3.
- With K=3, what is the least value of M for which the consecutive-birthdays
probability is larger than 0.5?
Put these numbers (manually) in a plain text file called results.txt in
your directory. For each estimate, use at least 10,000 trials
of the experiment.
- You do not need to write/submit four programs. Simply change the
parameter values for four executions.
- NOTE: Your code will be graded
on both correctness and documentation (comments).
Submission:
- You have already created a directory
called ex1
directory in your Unix account, following
these instructions
for setting up your Unix account and for submitting work.
- Make a zip of this folder and upload into Blackboard in the
right place.