Alright, so I'm having some trouble with ye olde factorials. "bool C++ beginner=true", folks. I'm struggling with this here problemo.
Here's the formula I need to compute : C(n,k) = n!/k!(n-k)!
And I have no idea how to go about this.
First, I have to implement a function for the n!. Then, I have to define a function for C(n,k), which is in the code below somewhere.
The code will ask the user to enter the size of a group of people (n) and then the size of the committee. Then it will output all possible ways to form the committee by choosing people from the given group.

Help would be dearly appreciated! In fact, if you're an online tutor and you need some work, don't hesitate! Use teh emails, folks! You guys rock, thanks.

Here's the skeleton of the code I made:

#include <iostream>
using namespace std;

int factorial (int number);
int combinations (int, int);

void main (void)
int n, k;

cout << "Enter the size of a group of people : " ;
cin >> n;

cout << "Enter the size of the committee : " ;
cin >> k;

//cout << "The choices are: ";


int factorial (int number)
if (number==0)
return 0;
return number*factorial(number-1);

int combinations (int n, int k)
int result;

return result;
9 Years
Discussion Span
Last Post by vmanes

In your factorial function, you've got the right idea of stopping at a base case. But, if you check the definition of factorial, 0! should return 1. As you have it now, n! will always end up as 0.

if( number == 0 || number == 1 )
    return 1;

Using type int (even unsigned int), your function will be good up to 12!, then the results overflow the value. Consider type double if you need larger values, or long long int (or __int64) if available on your compiler

For your combination function, you practically have the answer already. You stated the result is

= n!/k!(n-k)!

You can create variable to store the three values returned from the factorial function, then do the arithmetic, or call the function three times, with the appropriate arguments, with the math operators between those function calls. This will give you the number of combinations.

You say" Then it will output all possible ways to form the committee by choosing people from the given group." Do you mean just the count of how many possible groupings, or must you display all possible groupings?


Thanks for the tips!! And yes, I have to display all possible groupings. I didn't know if I should put in an array or not. I'm very lost with this, lol :/


To show all the groupings is a bit more challenging - I've done this sort of problem a while back. Loops within loops within loops....
One approach is to have k nested loops (or calls to a function that implements a loop), where each loop prints out n-(previous loop) values
Output might look like (for C(10,5)
Take a shot that for a bit.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.