Question

In: Computer Science

Write a C, C++ or Java program to implement each of the following functions. For each...

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.

Solutions

Expert Solution

#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


Related Solutions

Recursion practice Write a Java program with functions for each of the following problems. Each problem...
Recursion practice Write a Java program with functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. All of your solutions should be in a single .java file. The main function of the file should demonstrate each of your solutions, by running several tests and producing the corresponding outputs. Write a recursive method to 1. calculate power of a give number 2. multiply...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings....
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings. Assume that any char array passed into the functions will contain valid, null-terminated data. The functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
Implement each of the following functions and write a basic main() function that tests each. For...
Implement each of the following functions and write a basic main() function that tests each. For convenience, you should be able to find this starter class under Mimir's assignment 4 starter code. Do not change the name, parameters, or returns of any of these functions or of the name of the class itself. There is also no need in this assignment for any global variables. You are strongly encouraged to use your solution for some of these functions in others...
For this program you will implement the following utility functions to test mastery of C strings....
For this program you will implement the following utility functions to test mastery of C strings. *******you MUST use these these function***** void removeBlanks(char *src, char *dest); void replaceChar(char *src, char oldChar, char newChar); char *flipCase(const char *src); Please read the description of these functions carefully. In the removeBlanks function you will implement a routine that takes a string in as src and outputs the same string into dest but removing any blank space character encountered. For example, if the...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
Write a program to implement linked list data structure that will have following functions: a. Append...
Write a program to implement linked list data structure that will have following functions: a. Append a node in the list b. Insert a node in the list c. Delete a node from the list d. Display list e. Find maximum value in the list f. Find how many times a value exists in the list. g. Search Portion of the code is give below. You have to write code for the items (e, f, g) Program: #include<stdlib.h> #include<stdio.h> #include<iostream>...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT