This assignment is worth 50 points.
n! nPr = ----- (n-r)!where n is the number of items in a set and r is the number of items to be chosen in the subsets.
For example, there are 9 students participating in programming contest practice. I need to select a team of three students, where the first will be the captain, the second will be the assistant captain and the third will be the test master. How many different teams could I form?
This is clearly a permutation, as the captain has the most power. In this case there are n=9 elements in the set and I want to select r=3 students for the team. So I can form 9P3 = 504 different teams.
You are to write a program which prompts for the number of elements in the set, and the number of elements to be selected then prints out the number of sets which can be formed. Please follow the format of the example carefully as I will be expecting your program to use the same verbiage when testing.
There is no function in C++ to compute n!, which is the product of the first n integers, so we will need to compute this ourselves. Unfortunately, the easiest way to compute n! is to use loops, but since we don't have loops, we will need to approximate the value using Sterling's approximation. Sterling's approximation states:
n! ≈ e-n nn sqrt(2π n)(see page 134, problem 3 in your book).
To handle larger numbers, as well as accommodate the use of Sterling's, your work should be performed in doubles. However, the final answers should be presented as integers. An example run would be:
Enter the number of elements in the set => 9 Enter the number of elements in the subset => 3 A set with 9 elements can form approximately 506 subsets with 3 elements.In this example, all lines are terminated with a newline and the input is shown in bold.
Notice that the answer is an approximation, and is not the same as the actual answer of 504. This is acceptable.
You may assume that all input is correct, and in the correct format.
using namespace std; double NFactorial(double n); int main() {
numerator = NFactorial(elementCount);
// code in main return 0; } double NFactorial(double n) { // variables for NFactorial double result; // computation of the result, could involve several lines. result = // return the value return result; }
~dbennett/130/p3/runTests myprogNote there is no .C or .cpp on the end. This will build an executable called myprog. If it is successful, it will run a number of tests. These tests are by no means exhaustive, I will be running others when I test your code.
My program was called solution.C and here is a transcript of running the test code.
~dbennett/130/p3/runTests solution make: 'solution' is up to date. Test 1 Success Test 2 Success Test 3 Success Success, all tests have run correctlyI stuck a clear at the top of the test driver, so it will clear your screen.
make: 'solution' is up to date is normal if the program has already been compiled in your directory.
There is a possibility that, depending on the order of operations, you code may produce a slightly different result that I have in the files. If you have a different answer and are concerned, please get in touch.