Permutations Assignment Using Double Variables

Hi friends! I'm new to C++...in fact, this is my second programming class. I understand a bit of it, but not 100%. My assignment this week is to determine the number of potential team arrangements using the double type variable.

- If there are 18 people in your class and you want to divide the class into programming teams of 3 members, you can compute the number of different teams that can be arranged using this formula (n !⁄r !(n-r)!).

The coding that I have does not create the correct answer and I am also getting an error. Any help is appreciated!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41


//Include statements #include <iostream> #include <string> using namespace std;
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std; 
//Global declarations: Constants and type definitions only -- no variables
//Function prototypes int main()
int main()
{
//In cout statement below SUBSTITUTE your name and lab number
cout << "" << endl << endl;

//Variable declarations
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);
//Program logic

//using sterlings formula
// cout <<setprecision(2)
// <<n<<"! = "
// <<exp(-n) * pow(n, n) * sqrt(2 * PI * n)<<endl;

cout<<"Total amount of people in class: 18";
cout<<"                 ";
cout<<"Programming teams of: 3";
cout<<"                 ";
cout << "The number of potentiall teams that can be arranged is "
<<nFact/(rFact*nmrFact)<<endl;

//Closing program statements system("pause");

return 0;
}
//Function definitions 
http://www.cplusplus.com/forum/beginner/92924/
OP is copy-paste spam of a five year old post. Very strange. They went through the trouble of creating a burner account and using the code tags, marking the post as solved, so they actually know how to use this site already. Would have been less effort just to do the assignment.

Is the goal to use this as a add-spam account later on?
Last edited on
Hi newbieg...first, THANKS for your quick response!!

I tried that coding and it works perfect to my solution.

My last class was a Visual Basic and made more sense, I'm struggling through this one. So I really appreciate your help :)
@j3n1096,
You are falling into the trap of seeing a formula involving factorials for convenience ... and automatically assuming that you need to code it using factorials.

Factorials rapidly overflow integers, whilst using doubles leads to unnecessary floating-point rounding error. And Sterling’s formula is only an asymptotic approximation (for large n). It would be hopeless for 3 factorial.

18C3 is just (18*17*16)/(1*2*3) and that is easily computable in integers.

18!/15! cancels down to 18*17*16 - it is completely unnecessary to work out 18! and 15! separately.
Last edited on
Topic archived. No new replies allowed.