Permutation and combination

I keep getting error on this code, in know it's the cmbo(k-1,r) giving me the error, but i don't know why, can someone help please..
this code is for next_combination
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
#include <iostream>
#include <algorithm>
using namespace std;

int arr[10]={0,1,2,3,4,5,6,7,8,9};
void switc(int a, int b){
    int temp = arr[a];
    arr[a]=arr[b];
    arr[b]=temp;

    return;
}

void print(int size){
      for (int i=0;i<size;i++)
        cout << arr[i] << " ";
    cout << endl;
    return;
}
long Factorial(long val) 
{ 
long Result = 1; 
for(long i = 1; i <= val; i++) 
{ 
Result *= i; 
} 
return Result;
} 
long Combination(long N, long R) 
{ 
return (Factorial(N)) / ((Factorial(N-R)) * Factorial(R)); 
} 

void comb(int n, int r){
	int i;
	int k= Combination(n, r);
    if (k==0)
        print(r);
    else{
        for (i=k-1;i>=0;i--){
            switc(i,k-1);
            comb(k-1,r);
            switc(i,k-1);
        }
   }
return;
}

int main(){
	while (1){
	int n, n1;
	cout<<"Enter a number: ";
	cin>>n>>n1;
	
	comb(n, n1);

	cout<<"The Factoral for the combination is "<<Combination(n,n1)<<endl;
    return 0;
	
	}}
Last edited on
Do you have to re-invent the wheel for the assignment?
http://www.cplusplus.com/reference/algorithm/next_permutation/
Thanks for the fast reply, but i have already created Next_permutation code, and i"m suppose to do the combination, and arrangement, but i'm stuck on combination. the above code is suppose to be for combination, but it shows error for some reason.
Can you explain the difference between "permutation", "combination", and "arrangement"? These terms are synonyms to me.
Form what i heard, they are kinda similar, but they use different formula
permutation = n!
combination = n!/r!(n!-r!)
arrangement = n!/(n-r)!
Last edited on
Form what i heard, they are kinda similar, but they use different formula
permutation = n!
combination = n!/r!(n!-r!)
arrangement = n!/(n-r)!
Last edited on
What error are you getting?
1
2
3
4
5
6
7
8
9
10
11
12
void comb(int n, int r){
	int i;
	int k= Combination(n, r);
    if (k==0)
        print(r);
    else{
        for (i=k-1;i>=0;i--){
            switc(i,k-1);
            comb(k-1,r);
            switc(i,k-1);
        }
   }


This function seems to be a problem. k can be arbitrarily large for any reasonable n and r, and you are trying to access entry arr[k-1]. This in all probability leads to out of bounds array access.

EDIT:
Mathematically, the different terms usually mean
1. Permutations: Number of ways in which n different things can be arranged among themselves (n!)
2. Combinations: Number of ways in which r things can be chosen from n different things (C(n,r))
3. Combination with order: Number of ways in which r things can be chosen from n different things, order of choosing being important (P(n,r))
Last edited on
i found out why i keep getting error. it is because the function comb was dividing numbers by zero. But somehow i am facing some other problems. it works for some combination numbers and for others it doesn't work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int Combination(int N, int R) 
{ 
if (((Factorial(N-R)) * Factorial(R)) != 0)
return (Factorial(N)) / ((Factorial(N-R)) * Factorial(R)); 
else{
cout<<"Error! can't divide by zero. ";cout<<endl;
}
} 
void comb(int n, int r){
	int i;
	int k= Combination(n, r);
    if (k==0)
        print(r);
    else{
        for (i=k-1;i>=0;i--){
            switc(i,k-1);
            comb(k-1,r);
            switc(i,k-1);
        }
   }
return;
}


it works for some numbers, so far it was only able to work for numbers that are less by one digit. for example 4, 3 and 6, 5, combination correctly, for the other numbers, i get wrong answers, or the program stops working because of Zero division.

Can y'all Please Help, it's due tomorrow.
Last edited on
@helnow
Please see my previous post.
Topic archived. No new replies allowed.