Combinations are not concerned with order. Given n distinct objects there is onl
ID: 3682641 • Letter: C
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 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 <stdio.h>
#include <stdlib.h>
#include <math.h>
//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
Explanation / Answer
The following code works in dev C++ which I have debugged from the code that you provided.
The comments in CAPITALS show where the changes have been made. Also, I would recommend you to use "long long int" type of variables as their range is large and would accomodate the factorial of larger values.
//REMOVE THE FACTORIAL FUNCTION DEFINITION ABOVE THIS AS YOU HAVE PROVIDED DEFINITION LATER
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Function declaration.
int factorial(int n);
void C(int n,int k);
//main function
int main()
{
int n,k,comb; //long long int comb; CAN BE RATHER USED TO STORE nCr VALUE FOR LARGE VALUES OF N AND K
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(int n,int k) //MENTION THE TYPE OF ARGUMENTS IN DEFINITION, CHANGED FROM void C(n,k)
{ int comb; //long long int comb; CAN BE RATHER USED TO STORE nCr VALUE FOR LARGE VALUES OF N AND K
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; //long long int fact; CAN BE RATHER USED TO STORE FACTORIAL VALUE AS IT EXPONENTIALLY GROWS FOR LARGE VALUES OF N AND K
for(i=1; i<=n; i++)
{
fac *= i;
}
return fac;
}