again with the combinations programm..

hello again..

i want to make a programm to find all the combinations of n numbers choosing the k of them

for example

[1 2 3 4 5]

the combinations of two of them are

1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

the combinations of 3 numbers of them are

1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5



i have made this programm that it is close to find the write solution but i cannot go farther

please i want help..
if you can improve my programm or suggest me something already done..


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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "stdafx.h"
#include "iostream"
#include <vector>

using namespace std;
vector<vector<int>> v;
int arithmos_comb_sinolikos;
void comb(vector<int> A,int k,vector<vector<int>> &v,int column_v,int row);
int arithmos_comb_epomeno(int n,int k);
int factorial(int number) ;
ostream & print(const vector<int> & v);
ostream & print(const vector<vector<int> > & v);

int _tmain(int argc, _TCHAR* argv[])
{
	int arr[]={1,4,6,7,9};
	//cout<<arr[0];
	vector<int> A(arr,arr+5);
	v.push_back(vector<int>());
	//cout<<v.size();
	//cout<<A[3];
	arithmos_comb_sinolikos=arithmos_comb_epomeno(A.size(),2);
	//cout<<arithmos_comb_epomeno(4,2);
	//cout<<arithmos_comb_sinolikos;
	comb(A,2,v,0,0);
	//print(v);
	cin>>arr[0];
}
void comb(vector<int> A,int k,vector<vector<int>> &v,int column_v,int row)
{
	if(row>=arithmos_comb_sinolikos)
	{
		//cout<<"epistrefei_giati_row>=arithmos_comb"<<endl;
		//return;
	}
	else
	{

	}
	cout<<endl<<endl<<"mpike_sto_comb";
	print(v);
	for(int i=column_v;i<=A.size()-k+column_v;i++)
	{
		cout<<"mpike_sto_proto_for i="<<i<<endl;
		for(int y=0;y<arithmos_comb_epomeno(A.size()-column_v-i-1,k-column_v-1);y++)
		{
			cout<<"mpike_sto_deytero_for y="<<y<<endl;
			v[v.size()-1].push_back(A[i]);
			print(v);
			cout<<"edaxei"<<endl;
			cout<<"column_v="<<column_v<<endl;
			
			if(column_v+1<k)
			{
				cout<<"mpike_sto_mikrotero_k"<<endl;
				comb(A,k,v,column_v+1,row);
				//return;
				
				
			}
			else
			{
				cout<<"mpike_sto_megalitero_k"<<endl;
				//v[v.size()-1].push_back(A[i+column_v]);
				v.push_back(vector<int> ());
				column_v=0;
				return;

				
				//comb(A,k,v,0,stili_A,row+1);
				//return;
				
			}
			

			
			
		}
		
	}
}
int arithmos_comb_epomeno(int n,int k)
{	
	
	 return (factorial(n)/(factorial(k)*factorial(n-k)));
}
int factorial(int number) {
	int temp;

	if(number <= 1) return 1;

	temp = number * factorial(number - 1);
	return temp;
}

ostream & print(const vector<int> & v)
{
    for (int i=0; i<v.size(); i++)
        cout << v[i] << ' ';

    return cout;
}

ostream & print(const vector<vector<int> > & v)
{
    for (int i=0; i<v.size(); i++)
    {
        for (int j=0; j<v[i].size(); j++)
            cout << v[i][j] << ' ';

        cout << endl;
    }

    return cout;
}
vagelis wrote:
it is close to find the write solution

Really? I compiled it, ran it and got this:



mpike_sto_comb
mpike_sto_proto_for i=0
mpike_sto_deytero_for y=0
1 
edaxei
column_v=0
mpike_sto_mikrotero_k


mpike_sto_comb1 
mpike_sto_proto_for i=1
mpike_sto_deytero_for y=0
1 4 
edaxei
column_v=1
mpike_sto_megalitero_k
mpike_sto_deytero_for y=1
1 4 
1 
edaxei
column_v=0
mpike_sto_mikrotero_k


mpike_sto_comb1 4 
1 
mpike_sto_proto_for i=1
mpike_sto_deytero_for y=0
1 4 
1 4 
edaxei
column_v=1
mpike_sto_megalitero_k
mpike_sto_deytero_for y=2
1 4 
1 4 
1 
edaxei
column_v=0
mpike_sto_mikrotero_k


mpike_sto_comb1 4 
1 4 
1 
mpike_sto_proto_for i=1
mpike_sto_deytero_for y=0
1 4 
1 4 
1 4 
edaxei
column_v=1
mpike_sto_megalitero_k
mpike_sto_deytero_for y=3
1 4 
1 4 
1 4 
1 
edaxei
column_v=0
mpike_sto_mikrotero_k


mpike_sto_comb1 4 
1 4 
1 4 
1 
mpike_sto_proto_for i=1
mpike_sto_deytero_for y=0
1 4 
1 4 
1 4 
1 4 
edaxei
column_v=1
mpike_sto_megalitero_k
mpike_sto_proto_for i=1
mpike_sto_deytero_for y=0
1 4 
1 4 
1 4 
1 4 
4 
edaxei
column_v=0
mpike_sto_mikrotero_k


mpike_sto_comb1 4 
1 4 
1 4 
1 4 
4 
mpike_sto_proto_for i=1
mpike_sto_deytero_for y=0
1 4 
1 4 
1 4 
1 4 
4 4 
edaxei
column_v=1
mpike_sto_megalitero_k
mpike_sto_deytero_for y=1
1 4 
1 4 
1 4 
1 4 
4 4 
4 
edaxei
column_v=0
mpike_sto_mikrotero_k


mpike_sto_comb1 4 
1 4 
1 4 
1 4 
4 4 
4 
mpike_sto_proto_for i=1
mpike_sto_deytero_for y=0
1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
edaxei
column_v=1
mpike_sto_megalitero_k
mpike_sto_deytero_for y=2
1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
4 
edaxei
column_v=0
mpike_sto_mikrotero_k


mpike_sto_comb1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
4 
mpike_sto_proto_for i=1
mpike_sto_deytero_for y=0
1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
4 4 
edaxei
column_v=1
mpike_sto_megalitero_k
mpike_sto_proto_for i=2
mpike_sto_deytero_for y=0
1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
4 4 
6 
edaxei
column_v=0
mpike_sto_mikrotero_k


mpike_sto_comb1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
4 4 
6 
mpike_sto_proto_for i=1
mpike_sto_deytero_for y=0
1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
4 4 
6 4 
edaxei
column_v=1
mpike_sto_megalitero_k
mpike_sto_deytero_for y=1
1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
4 4 
6 4 
6 
edaxei
column_v=0
mpike_sto_mikrotero_k


mpike_sto_comb1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
4 4 
6 4 
6 
mpike_sto_proto_for i=1
mpike_sto_deytero_for y=0
1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
4 4 
6 4 
6 4 
edaxei
column_v=1
mpike_sto_megalitero_k
mpike_sto_proto_for i=3
mpike_sto_deytero_for y=0
1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
4 4 
6 4 
6 4 
7 
edaxei
column_v=0
mpike_sto_mikrotero_k


mpike_sto_comb1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
4 4 
6 4 
6 4 
7 
mpike_sto_proto_for i=1
mpike_sto_deytero_for y=0
1 4 
1 4 
1 4 
1 4 
4 4 
4 4 
4 4 
6 4 
6 4 
7 4 
edaxei
column_v=1
mpike_sto_megalitero_k

In which way is this close to the right solution?

Anyway, if you don't want to follow this -> http://cplusplus.com/forum/general/32591/#msg176142
(which, in my opinion, is simpler) would you mind describing the logic you try to use here, so that we can follow?

EDIT: This here could help too -> http://www.codeproject.com/KB/recipes/CombC.aspx
Last edited on
my friend i try in this case to find the combinations of 2 numbers of the numbers 1,4,6,7,9
the results

must be and i find
1 4--------------1 4
1 6--------------1 4
1 7--------------1 4
1 9--------------1 4
4 6--------------4 4
4 7--------------4 4
4 9--------------4 4
6 7--------------6 4
6 9--------------6 4
7 9--------------7 4

so i find the right number of results,the right first number but the problem is the second number
so i am close
the logic is difficult
if dont know if you can understand this..

if i change the programm like this it finds the results until until it stops
i think the problem is to find a new variable to add in the A[i+v.size()-1] because it works only for the first number
if(column_v+1==k)
{
v[v.size()-1].push_back(A[i+v.size()-1]);
}



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "stdafx.h"
#include "iostream"
#include <vector>

using namespace std;
vector<vector<int>> v;
int arithmos_comb_sinolikos;
void comb(vector<int> A,int k,vector<vector<int>> &v,int column_v,int row);
int arithmos_comb_epomeno(int n,int k);
int factorial(int number) ;
ostream & print(const vector<int> & v);
ostream & print(const vector<vector<int> > & v);

int _tmain(int argc, _TCHAR* argv[])
{
	int arr[]={1,4,6,7,9};
	//cout<<arr[0];
	vector<int> A(arr,arr+5);
	v.push_back(vector<int>());
	//cout<<v.size();
	//cout<<A[3];
	arithmos_comb_sinolikos=arithmos_comb_epomeno(A.size(),2);
	//cout<<arithmos_comb_epomeno(4,2);
	//cout<<arithmos_comb_sinolikos;
	comb(A,2,v,0,0);
	//print(v);
	cin>>arr[0];
}
void comb(vector<int> A,int k,vector<vector<int>> &v,int column_v,int row)
{
	if(row>=arithmos_comb_sinolikos)
	{
		//cout<<"epistrefei_giati_row>=arithmos_comb"<<endl;
		//return;
	}
	else
	{

	}
	cout<<endl<<endl<<"mpike_sto_comb"<<endl;
	print(v);
	for(int i=column_v;i<=A.size()-k+column_v;i++)
	{
		cout<<"mpike_sto_proto_for i="<<i<<endl;
		for(int y=0;y<arithmos_comb_epomeno(A.size()-column_v-i-1,k-column_v-1);y++)
		{
			if(column_v+1==k)
			{
				v[v.size()-1].push_back(A[i+v.size()-1]);
			}
			else
			{
				cout<<"mpike_sto_deytero_for y="<<y<<endl;
				v[v.size()-1].push_back(A[i]);
			}
			
			print(v);
			cout<<"edaxei"<<endl;
			cout<<"column_v="<<column_v<<endl;
			
			if(column_v+1<k)
			{
				cout<<"mpike_sto_mikrotero_k"<<endl;
				comb(A,k,v,column_v+1,row+1);
				//return;
				
				
			}
			else
			{
				cout<<"mpike_sto_megalitero_k"<<endl;
				//v[v.size()-1].push_back(A[i+column_v]);
				v.push_back(vector<int> ());
				column_v=0;
				return;

				
				//comb(A,k,v,0,stili_A,row+1);
				//return;
				
			}
			

			
			
		}
		
	}
}
int arithmos_comb_epomeno(int n,int k)
{	
	
	 return (factorial(n)/(factorial(k)*factorial(n-k)));
}
int factorial(int number) {
	int temp;

	if(number <= 1) return 1;

	temp = number * factorial(number - 1);
	return temp;
}

ostream & print(const vector<int> & v)
{
    for (int i=0; i<v.size(); i++)
        cout << v[i] << ' ';

    return cout;
}

ostream & print(const vector<vector<int> > & v)
{
    for (int i=0; i<v.size(); i++)
    {
        for (int j=0; j<v[i].size(); j++)
            cout << v[i][j] << ' ';

        cout << endl;
    }

    return cout;
}



the logic is for the numbers that can be used in every column of the combination vector()
for(int i=column_v;i<=A.size()-k+column_v;i++)

i write them so many times as the combinations of the numbers without them are
for(int y=0;y<number_of_comb_next(A.size()-column_v-i-1,k-column_v-1);y++)

and then call it again
Last edited on
Topic archived. No new replies allowed.