so this is what i have so far and am wondering if it is correct.
The problem asks if you have a class the size of 18 people and you want to divide the class into programmin teams of 3 members compute the different teams that can be arranged using this formula
n!
___
r!(n-r)!
write a c++ program that determines the number of potential team arrangements:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
const double PI = 3.14;
double n = 18.0;
double r = 3.0;
double nmr = 15.0;
double nFact = exp(-n) * pow(n, n) * sqrt(2 * PI * n);
double rFact = exp(-r) * pow(r, r) * sqrt(2 * PI * r);
double nmrFact = exp(-nmr) * pow(nmr, nmr) *sqrt(2 * PI * nmr);
//using sterlings formula
// cout <<setprecision(2)
// <<n<<"! = "
// <<exp(-n) * pow(n, n) * sqrt(2 * PI * n)<<endl;
cout << "The number of teams that can be arranged ="
<<nFact/(rFact*nmrFact)<<endl;
system("PAUSE");
return 0;
}
the output is:
The number of teams that can be arranged is 839.89
I was wondering if this is correct. Any help would be greatly appreciated
You used two approximations, one is Stirling's approximation, the other is a value of PI to just three significant digits. Consequently your answer is very approximate.
It would be simpler to do this:
1 2 3
double n = 18.0;
double r = 3.0;
double answer = fact(n) / (fact(r)*fact(n-r));
The factorial function itself fact() can be written in three or four lines of code, and this will give an exact answer.
* * * * *
Of course, if you wanted to work this out by hand, it would be simply
18*17*16 / 3*2
= 3*17*16
= 816, which can be used to verify the result.