Program 3, Permutations

Short Description:

Write a program which given the number of elements in a set, and the number of elements in a subset, computes the approximate number of subsets which can be formed.

This assignment is worth 50 points.

Goals

When you finish this homework, you should:

Formal Description

A permutation is an ordered arrangement of objects. A common Math 104 problem is to compute the number ways a fixed sized subset of a given set can be created. This is done using the permutation formula:

         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.

Building a function

You will need to compute n! twice in your program, so you may, if you wish develop a function. To do this you need to perform the following steps.

Discussion

Testing

I have provided a simple script to test your program for some of my input. You may use this program if you wish. If your program is named myprog.cpp or myprog.C you can type
~dbennett/130/p3/runTests myprog
Note 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 correctly

I 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.

Required Files

A single source code file.

Submission

Email your final source code file to danbennett360@gmail.com.