Needing help I am new to this.

Here is the problem I am working with.....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)!).


//Include statements
#include <iostream>
#include <string>
using namespace std;

//Global declarations: Constants and type definitions only -- no variables

//Function prototypes

int main()
{

//Variable declarations
double n = 18.0;
double r = 3.0;
double answer = fact(n) / (fact(r)*fact(n-r));

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

//Closing program statements
cout << "The number of teams that can be arranged =" <<nFact/(rFact*nmrFact)<<endl;
system("pause");
return 0;

}
//Function definitions


But my code is not working and keeps erroring out. I have no idea what I am doing or what I am doing wrong. If I can get any help that would be awesome. Thank you in advance.
your fact function is probably wrong. Does it work off doubles or integers? If it works off integers, does your integer support 18! ? That is 6402373705728000.... it will not fit in smaller integer types.
you can cancel the terms in the division, or you can use a bigger integer type...
Last edited on
OP's code, formatted and put into the code blocks they deleted while making their post:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

// Global declarations: Constants and type definitions only -- no variables

// Function prototypes

int main() {
  // Variable declarations
  double n = 18.0;
  double r = 3.0;
  double answer = fact(n) / (fact(r) * fact(n - r));

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

  // Closing program statements
  cout << "The number of teams that can be arranged ="
       << nFact / (rFact * nmrFact) << endl;
  system("pause");
  return 0;
}


I don't mean to be harsh, but this code is nonsensical. You compute your nCm on the line that declares "answer", but then compute a bunch of other stuff on the line below your program logic comment, and then on the line further down reference three variables that, simply, don't exist. You should probably scrap all of main() and rewrite it. It shouldn't take too long.

Also, this code is incomplete, either because you didn't finish it, or you didn't post everything (like the fact function). In the future, posting the full code is helpful.

And yes, jonnin is right. Factorials get huge very quickly. You're probably going to want to write a function that computes n!/m! for n >= m.

-Albatross
Last edited on
Jonnin it is supposed to be working off of doubles.

Albatross no need to apologize I did not take your comments as harsh, I am just struggling to build this code and still learning on how to do this so I welcome all critiques and comments as they come need to learn how to do this.
if it works off doubles, then we need to know more.
lets start simple:
what do you get if you write

cout << answer << endl;

after you assign answer a value?

Do you know the answer to your problem? Pop open a calculator and get the right answer so you know when it is right.

if answer is wrong, write out what you got for each factorial, see if that is correct for each one.
Last edited on
djfr33 wrote:
Jonnin it is supposed to be working off of doubles.

There is absolutely NO need to use doubles. 18C3 is only 816 - well within integer arithmetic provided that you DON'T use a factorial function.

Nor is there any need whatsoever to use a factorial function at all.

Think what 18!/3!15! actually cancels down to:
(18*17*16)/(3*2*1)
No real need to compute a factorial there (not even in the denominator).

Rearrange it as (and compute in the following order to prevent truncation and keep intermediate results small):
16 / 1 * 17 / 2 * 18 / 3


In general, for nCr, replace r by the smaller of r and n-r (to save computations). Then compute:
(n-r+1) / 1 * (n - r + 2 ) / 2 * ..... n / r

No factorials. All integers.


To push it further, if you work in, and return, unsigned long long, then you should be able to get up to about n=62 before any of the binomial coefficients start to misbehave. At this point, doubles will be very wrong.


Another option - with no multiplies - is to use Pascal's triangle.




BTW:
If there are 18 people in your class and you want to divide the class into programming teams of 3

Sorry, but nCr (or n!/(n-r)!r!) gives you the number of ways of selecting ONE team of 3 from 18 people ... it's not really what the original question asked.
Last edited on
Topic archived. No new replies allowed.