permutations and combination program

Hi, i was wondering if i could get some help on a program i am working on for my c++ class. i don't know how to do functions for the factorial function. any help would be really appreciated. i will post the code of what i have so far below.



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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>

using namespace std;

//Add function prototypes here.
double permut ( double temp1, double numCol)
{
    return temp1 * numCol;
}

double precombs ( double temp2, double numSel)
{
    return temp2 * numSel;
}

int main()
{
//Declare variables here.
    double numCol; //n = number of items in a collection.
    double numSel; //k = number of items to be selected from the collection.
    double perms; //P = number of permutations.
    double combos; //C = number of conbinations.
    double precombo;
    double temp1 = 0;
    double temp2 = 0;


//Ask for user inputs.
    cout << "Please enter the number of collection itens (< 35): ";
    cin >> numCol;
    cout << endl;

    cout << "Please enter the number of selected collection items (< 35): ";
    cin >> numSel;
    cout << endl;

    perms = 1;
    precombo = 1;








//Calculates the number of possible permutations.

    {
        for (temp1 = numCol; temp1 != 0 ; --temp1)
            perms *= temp1;
    }





//Calculates the number of different orderings for the selection number./t2 = precombs(temp2 , numSel);
    {
        for (temp2 = numSel; temp2 != 0 ; --temp2)
            precombo *= temp2;
    }





//Calculate the number of combinations.
    combos = perms / precombo;

    cout << "The number of possible permutations is: " << perms << endl;
    cout << "The number of possible combinations is: " << combos << endl;
   system("PAUSE");
    return 0;
    
      

}

Last edited on
can anyone please help me?
just google the equation and slap it into code.
You haven't described a problem yet, but I can decipher from your code that you are dealing with permutations and combinations in some way.

Permutations is calculated by computing the factorial of the number of items
n!

Combinations is computed by:
n!/(k!(n - k)!)

https://www.mathsisfun.com/combinatorics/combinations-permutations.html
Topic archived. No new replies allowed.