C Programming With our ability to implement user-defined functions only the foll
ID: 3872938 • Letter: C
Question
C Programming
With our ability to implement user-defined functions only the following will be permitted in the main function:
1. Declaration of variables to be passed to functions.
2. Calls to user-defined functions by main.
3. A limited amount of control structures (see chapters 5 and 6) to retain the previous two tasks within the main function. Additionally, each user-defined function may represent a single task in your larger program.
The failure to make a good use of user-defined functions as described here and in the course standards will result in a loss of ALL points on those assignments that require user-defined functions.
Problem: An industrial facility is implementing a new processes which increases their rate of microprocessor unit production but there is some concern that the new process will generate a larger number of defective units. Given the percentage of defective units produced determine the probability that there will R defective units from a sample size of N In addition the program should calculate the probability that there will be no defective units from that same sample. Use Gosper's estimate (formula on right) for factorial where needed in your solution. Example Execution #1: Enter percentage of defective units produced: 20 Enter number of units to sample: 10 Enter number of defective units in sample: 2 Average number of defective units: 20% Selecting 10 units with 2 being defective has a probability of: 0.3024 Selecting 10 units with 0 being defective has a probability of: 0.1049 Example Execution #2: Enter percentage of defective units produced: 25 Enter number of units to sample: 10 Enter number of defective units in sample: 2 Average number of defective units: 25% Selecting 10 units with 2 being defective has a probability of: 0.2819 Selecting 10 units with 0 being defective has a probability of: 0.0550 Example Execution #3: Enter percentage of defective units produced: 15 Enter number of units to sample: 10 Enter number of defective units in sample: 2 Average number of defective units: 15% Selecting 10 units with 2 being defective has a probability of: 0.2763 Selecting 10 units with 0 being defective has a probability of: 0.1924 Example Execution #4: Enter percentage of defective units produced: 45 Enter number of units to sample: 10 Enter number of defective units in sample 10 Average number of defective units: 45% Selecting 10 units with 10 being defective has a probability of: 0.0003 Selecting 10 units with 0 being defective has a probability of 0.0025Explanation / Answer
C code:
#include <stdio.h>
#include <math.h>
float fact(float x)
{
float ans;
ans = pow(x,x)*pow(2.71828,-x)*sqrt( (2*x + 0.33)*3.14 ) ;
return ans;
}
float nchoosek(float n,float r)
{
float ans;
ans = (float)fact(n)/(float)(fact(n-r)*(float)fact(r));
return ans;
}
float myfunct(float n,float p, float r)
{
p = (float)(p)/(float)(100.0);
float ans;
ans = nchoosek(n,r)*( pow(p,r) )* ( pow( (1-p), n-r) );
return ans;
}
int main()
{
float p;
float n, r;
printf("%s ","Enter percentage of defective units produced!" );
scanf("%f",&p);
p = p*1.0;
printf("%s ","Enter number of units to the sample!" );
scanf("%f",&n);
printf("%s ","Enter number of defective units in the sample!" );
scanf("%f",&r);
printf("%s %f %s ", "Average number of defective units: ", p , "percent" );
float ans = myfunct(n,p,r);
printf("%s %d %s %d %s %f ","Selecting ", (int)n, " units with ",(int)r, " being defective has a probability of : ", ans );
ans = myfunct(n,p,0);
int s = 0;
printf("%s %d %s %d %s %f ","Selecting ", (int)n, " units with ", s, " being defective has a probability of : ", ans );
return 0;
}
Sample Output:
C:UsersAkashDesktopchegg>a
Enter percentage of defective units produced!
20
Enter number of units to the sample!
10
Enter number of defective units in the sample!
2
Average number of defective units: 20.000000 percent
Selecting 10 units with 2 being defective has a probability of : 0.302599
Selecting 10 units with 0 being defective has a probability of : 0.105482
C:UsersAkashDesktopchegg>a
Enter percentage of defective units produced!
25
Enter number of units to the sample!
10
Enter number of defective units in the sample!
2
Average number of defective units: 25.000000 percent
Selecting 10 units with 2 being defective has a probability of : 0.282135
Selecting 10 units with 0 being defective has a probability of : 0.055321
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.