In: Computer Science
Write a C, C++ or Java program to implement each of the following functions. For each function, use a long long datatype for all integer variables and a double datatype for all decimal variables. Permutations(N,X): This function returns the number of ways X objects can be drawn from N objects in a particular order. Combinations(N,X): This function returns the number of ways X objects can be drawn from N objects ignoring the order in which the objects are drawn. Binomial(N,P,X): This function returns the binomial distribution probability of having X successes in N independent trials, where P is the probability of a success in each trial. Use the above functions in one or more main programs to solve the following problems:
1. A department contains 20 employees. The manager is going to randomly draw 4 employees and give each one a prize. a. How many ways can the 4 employees be drawn if the order in which they are drawn matters (i.e. the prizes have different values)? b. How many ways can the 4 employees be drawn if the order in which they are drawn does not matter (i.e. all prizes have the same value)?
2. A munitions warehouse contains 50 bombs, of which 3 are defective (6%). A sample of 10 bombs is drawn and tested. What is the probability that the sample will contain at most 1 defective bomb?
3. Suppose that the same warehouse contains a "very large" number of hand grenades, of which 7.5% are defective. A sample of 15 grenades is drawn and tested. What is the probability that the sample will contain at most 2 defective grenades?
Sample output for this problem (including some unnecessary debugging information):
Check for correctness of functions: permutations: P(10,4) = 5040 (should be 5040) combinations: C(10,4) = 210 (should be 210) binomial: b(2, 12, 0.06) = 0.127975 (should be 0.127975) Ways of presenting different prizes to 4 of 20 employees: 116280 Ways of presenting the same gifts to 4 of 20 employees: 4845 Probability of at most one faulty bomb in sample size 10: 0.902041 Probability of more than one faulty bomb: 0.097959 The sum of these probabilities is: 1.000000 (should be 1.0) Probability of at most two faulty hand grenades in 15: 0.902602 Turn in the source code listing for each function. For each of the solved problems, turn in the source code for the main program(s). Clearly indicate through in-line commenting the sections of code used to solve each problem. The program should also generate output clearly displaying the solutions to each problem.
#include <bits/stdc++.h>
using namespace std;
long long int factorial( int n)
{
long long int f=1;
while(n>1)
{
f*=n;
n--;
}
return f;
}
long long int Permutations(int N, int X)
{
long long int ans;
ans=(factorial(N)/factorial(N-X));
return ans;
}
long long int Combinations(int N, int X)
{
long long int ans;
ans=(factorial(N)/(factorial(N-X)*factorial(X)));
return ans;
}
double Binomials( int N,double P,int X)
{
double ans;
ans=Combinations(N,X)*pow(P,X)*pow(1-P,N-X);
return ans;
}
int main()
{
cout<<"The no of ways can the 4 employees be drawn from 10
employees if the order in which they are drawn matters
is:"<<Combinations(10,4)<<endl;
cout<<"The no of ways can the 4 employees be drawn from 10
employees if the order in which they are drawn does not matters
is:"<<Permutations(10,4)<<endl;
cout<<"The probability that the sample will contain at most 1
defective bomb is
<<Binomials(10,0.06,1)+Binomials(10,0.06,0)<<endl;
cout<<"The probability that the sample will contain at most 2
defective grenades
is:"<<Binomials(15,0.075,2)+Binomials(15,0.75,1)+Binomials(15,0.75,0)<<endl;
return 0;
}
output:
The no of ways can the 4 employees be drawn from 10 employees if the order in which they are drawn matters is:210
The no of ways can the 4 employees be drawn from 10 employees if the order in which they are drawn does not matters is:5040
The probability that the sample will contain at most 1 defective bomb is:0.882412
The probability that the sample will contain at most 2 defective grenades is:0.214365