Question: Combinations are not concerned with order. Given n distinct objects there is only one combination of n objects taken k at a time. The combination
Combinations are not concerned with order. Given n distinct objects there is only one combination of n objects taken k at a time. The combination function C(n,k) gives the number of different (unordered) k-element subsets that can be found in a given statement of n elements. The function can be computed from C(n,k)= n!/[k!(n-k)!] .
Use the factorial function fact (int n) provided and write a program that asks the user to input a positive integer and calls on a function comb( ) which itself calls on a function fact (int n). This, of course, means that you must write the comb() function and the main program itself! If the user enters a number less than one, have the program return an error message and use an exit statement to terminate the program.
int factorial (int n) { if (n<2) return 1; int f=1; while (n>1) f *= n--; return f; }
#include
//Function declaration. int factorial(int n); void C(int n,int k); //main function void main() { int n,k,comb; printf("Enter a number: "); scanf("%d", &n); if(n < 1) { printf("Error:value must be >0 "); exit(1); } printf("Enter k: "); scanf("%d", &k); if(k < 0 || k > n) { printf("Must be between 0 and %d ",n); exit(1); } C(n,k); } //function definition void C(n,k) { int comb;
comb = factorial(n) / (factorial(k) * factorial(n - k));//C(n,k)=n!/[k!(n-k)!] printf("%d items taken %d ways is %d ", n, k,comb); }
//function definition int factorial(int n) { int i,fac = 1; for(i=1; i<=n; i++) { fac *= i; } return fac; }
for some reason this code wont work on dev c++. am i missing a header file ? or just using the wrong statements
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
