Question: Binomial Coefficients in C++ Background: We are going to compute a binomial coefficient. A binomial coefficient calculates how many subsets, k, of a set n
Binomial Coefficients in C++
Background: We are going to compute a binomial coefficient. A binomial coefficient calculates how many subsets, k, of a set n can be chosen. For example, if you have a drawer of 4 socks, how many different ways are there to pick 2 socks? The answer is n choose k, where n>=k>= 0. {1,2,3,4} has the sets {1,2}, {1,3}, {1,4}, {2,3}, {2,4}, {3,4} so 6 different ways to choose 2 out of 4. Your execution should be able to match the examples.
Write a function chooseRec that take n and k and computes the binomial coefficient by calling itself. If k equals 0 or k equals n then return 1. This is the base case. Otherwise return a recursive call of chooseRec (n-1, k-1) + chooseRec (n-1, k).
If k = 0 or n = k, then = 1.
Otherwise, = + .
Write a driver program that takes n and k, where n >= k>= 0. If the input does not meet those requirements print invalid input to standard error (cerr) then exit(-1). Run and print the factorial function.
Example output:
Please enter n: 4
Please enter k: 6
invalid input
Example output:
Please enter n: 4
Please enter k: 2
Recursive Binomial Coefficient: 6
Example output:
Please enter n: 10
Please enter k: 3
Recursive Binomial Coefficient: 120
Example output:
Please enter n: 40
Please enter k: 4
Recursive Binomial Coefficient: 91390
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
