function "count_heads()" that takes two inputs N and R. the function should return the probability of getting exactly R heads on N successive tosses of a fair coin. A fair coin has equal probability of loading a head or a tail( i.e 0.5) on each toss
Answers
Answer:
Probability of getting K heads in N coin tosses
Given two integers N and R. The task is to calculate the probability of getting exactly r heads in n successive tosses.
A fair coin has an equal probability of landing a head or a tail on each toss.
Examples:
Input : N = 1, R = 1
Output : 0.500000
Input : N = 4, R = 3
Output : 0.250000
Answer:
Given two integers N and R. The task is to calculate the probability of getting exactly r heads in n successive tosses.
A fair coin has an equal probability of landing a head or a tail on each toss.
include <bits/stdc++.h>
using namespace std;
// function to calculate factorial
int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// apply the formula
double count_heads(int n, int r)
{
double output;
output = fact(n) / (fact(r) * fact(n - r));
output = output / (pow(2, n));
return output;
}
// Driver function
int main()
{
int n = 4, r = 3;
// call count_heads with n and r
cout << count_heads(n, r);
return 0;
}
Output:
0.250000
hope it help u