Objective Recursive Functions Project The formula for computing the number of co
ID: 3809210 • Letter: O
Question
Objective Recursive Functions Project The formula for computing the number of combinations that result from choosing r different items from a set of size n is C (m, r) (n r)! where the factorial notation is defined as Develop a recursive version of the combinations formula C (m, r) and write a recursive function that computes the value of the formula. Add the function as a subfunction of a driver function that tests it. Overall you project will consist of the project function, the printHeader function, an input function that will prompt the user for n and r and error check that r S n, your recursive combinations function, and an output function. Note: Do not use the factorial function that we developed in class as a part of your program. Hint: Write out the calculation for several small combinations such as of fractions.Explanation / Answer
The recursive function that you are looking for:
C(n, r) = n! / r! * (n-r)! = (n / r) * ((n-1)! / (r-1)! * (n - 1 - r + 1)!) = (n / r) * C(n - 1, r - 1). The function will be:
int combination(int n, int r) {
if ( r > n) return 0;
if (r == 0) return 1;
return combination(n - 1, r - 1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.