Before we begin, the p=position stuff seems to be confusing you, so forget it for the moment. A lot of computer classes assume you paid more attention in math classes than you actually did.
Normally, in a fair two-outcome probability (in this case, a 0 or a 1), the probability that you get the first outcome (a zero) is
1/
2.
Probabilities multiply, so the probability that you get two 0s in a row is (
1/
2)×(
1/
2) which is
1/
4.
This is because you have an
unlimited number of 0s and 1s to draw from. This is called a "fair coin" probability.
______________________________
Your problem changes the probability of drawing a 0 from a 'fair' to a 'biased' probability. This is because you have a fixed number of outcomes (n), and you know that k outcomes are 0s. In other words, you have only a limited number of 0s to draw before you run out. (It is also important that you have a limited number of total outcomes, since
k/
∞ is not calculable.)
What this means is that the probability of drawing a 0 as the first outcome will be
(number of 0s)/
(total number of outcomes), or
k/
n.
______________________________
The trick now is that once you get an outcome, you cannot get it again. Hence, your number of outcomes is now (n-1). We are also looking at the probability of getting
another 0. In order to get this far we have to say that we know we already drew a 0. So the number of 0s is also (k-1).
Again we multiply the probabilities together: (probability of drawing a 0)×(probability of drawing
another 0), which is (
k/
n)×(
k-1/
n-1).
So, if we have drawn two 0s, what is the probability that we draw
yet another 0? The same logic applies.
There are two caveats.
1) What is the probability of drawing (k+1) 0s? (Remember, there are only k 0s to draw.)
2) What is the probability of drawing a 0 if k=n?
______________________________
About your code. Make sure to have an
int variable for both k and n -- don't try to use
sizeof() tricks.
Also,
you don't actually need a buffer full of '0's and '1's. This is a math question. So unless your professor actually asked you for some zeros and ones to look at, don't bother with it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
using namespace std;
int main()
{
int k, n;
cout << "number of outcomes (n) ? ";
cin >> n;
cout << "number of zeros in n (k) ? ";
cin >> k;
...
return 0;
}
|
Hope this helps.