combinations of numbers

hello,

i search for a code that finds the combinations of numbers

for example if the numbers are [2 4 3]
the combinations of those numbers with at pairs are
2 4
2 3
4 3

is there a code for that??
Yes, see http://www.cplusplus.com/reference/algorithm/next_permutation/
If you just want two out of three, ignore the last element of each permutation and filter any duplicate results.
If you always need to build pairs, you can just use a nested loop instead.
Are you sure that's what you want?

Looking at your other post ( http://cplusplus.com/forum/general/32572/#msg175643 )
I gather that you want the number of combinations of n things in k places.

There's a recursive formula you can use -> http://en.wikipedia.org/wiki/Binomial_coefficient#Recursive_formula

I'll give you an example of writing a recursive function to calculate the sum of integers from 1 to n
and then you can use that to write a function for what you want.

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
#include <iostream>
using namespace std;

int sum(int n)
{
    if (n==1)
        return 1;
    else
        return n+sum(n-1);
}

int main()
{
    cout << sum(1) << endl; //1
    cout << sum(2) << endl; //1+2
    cout << sum(3) << endl; //1+2+3

    //1+2+3+4+5+6+7+8+9+10
    cout << sum(10) << endl;

    cout << "\nhit enter to quit...";
    cin.get();

    return 0;
}
i hope this code helps you
i try to run
it is ok.
1
2
3
4
5
6
7
8
9
#include"stdafx.h"
#include<iostream>
using namespace std;
void combination(int A[],int n)//Array A has n elements
{
	for(int i=0;i<n;i++)
		for(int j=i+1;j<n;j++)
			cout<<"["<<A[i]<<" "<<A[j]<<"]"<<endl;
}

and result if you have numbers of [2 4 3].
1
2
3
4
[2 4]
[2 3]
[4 3]
Press any key to continue.....


Last edited on
hmm...
i dont want for one case

i want to give a vector with as many number i want and then give the number k of the numbers will be choosed..

then i want to receive a two dimensional vector with all the combinations

Combinations of 3 letters from {A, B, C, D, E} (set of 5 letters)
The total number of possible combinations is: 10

A B C
A B D
A B E
A C D
A C E
A D E
B C D
B C E
B D E
C D E


Combinations of 4 letters from {A, B, C, D, E, F} (set of 6 letters)
The total number of possible combinations is: 15

A B C D
A B C E
A B C F
A B D E
A B D F
A B E F
A C D E
A C D F
A C E F
A D E F
B C D E
B C D F
B C E F
B D E F
C D E F



i dont know if it is clear now..
Last edited on
ok
you need add a for loop only.
sory i think it is not so simple..
Did you see my post? ( http://cplusplus.com/forum/general/32591/#msg175723 )

Well, if you're really sure you want the combinations themselves, you can do that recursively too.
Suppose you want the combinations of 3 letters of the elements of the set {A, B, C, D, E}.

combos({A, B, C, D, E}, 3)=

   [{A} U. combos({B, C, D, E}, 2)]
 U [{B} U. combos({C, D, E}, 2)]
 U [{C} U. combos({D, E}, 2)]

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Then, combos({B, C, D, E}, 2)=

   [{B} U. combos({C,D,E}, 1)]
 U [{C} U. combos({D,E}, 1)]
 U [{D} U. combos({E}, 1)]

etc...

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Then, combos({C,D,E}, 1)=

   [{C} U. combos({D, E}, 0)]
 U [{D} U. combos({E}, 0)]
 U [{E} U. combos({}, 0)]

={{C}} U {{D}} U {{E}}

={ {C}, {D}, {E} }

etc...

{A} U. {X, Y, Z} means {{A}UX, {A}UY, {A}UZ} - see? I even used the matlab notation you're familiar with ;)

EDIT: minor syntactic corrections
EDIT 2: mmm... this might still not be syntactically correct... anyway, I believe the idea isn't hard to grasp.
Last edited on
yes i understand the logic
i know the logic but i cannot make a programm in cpp..

is there not an already done programm to do this?
Well, I don't know... Try googling, you may get lucky. But don't expect anyone here to write it for you :P
Of course, if you have something almost working, we'll be very glad to help you fix it :)
I gave it a try, just out of curiosity, and it really isn't that hard. I'll give you an outline you can use.

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
#include <iostream>
#include <vector>
using namespace std;

typedef vector<char> Set;
typedef vector<vector<char> > SetSet;

ostream & print(const SetSet & ss)
{
	for (unsigned i=0; i<ss.size(); i++)
	{
		for (unsigned j=0; j<ss[i].size(); j++)
			cout << ss[i][j] << ' ';
        	if (i+1!=ss.size()) cout << endl;
	}
    
	return cout;
}

SetSet combos(Set set, unsigned n)
{
	SetSet result;

	if (n==0 || set.size()==0)
	{
		//...
		return result;
	}

	if (n==1)
	{
		//...
		return result;
	}

	if (n==set.size())
	{
		//...
		return result;
	}
	
	//non trivial cases here

	//...

	return result;
}

int main()
{
	Set s;

	s.push_back('E');
	s.push_back('D');
	s.push_back('C');
	s.push_back('B');
	s.push_back('A');

	SetSet ss=combos(s,3);

        cout << "combinations of {A,B,C,D,E} in 3 places\n";
        print(ss)<< endl;

	return 0;
}

Both unions ( U. and U ) can be easily implemented using push_back inside a loop.

Useful links:

http://cplusplus.com/reference/stl/vector/ (vector)
http://cplusplus.com/doc/tutorial/ (if you need to remember basic stuff)
Last edited on
what are these??
i dont want jokes..
What are you talking about? I wrote it and tested it, and it worked fine.
Then, I erased the core parts (the ones you are supposed to write)
and posted the rest. If you can't even appreciate this, why bother posting?

I hope you understand that this is not a forum where you post your homework and other people do it for you...

Maybe this is what you're looking for -> http://www.libeperson.com/
Last edited on
if you cannot answer then dont answer jokes
i dont want to loose my time

i want a solution
if you write if then else its not a solution it is a joke

ps
you could do the same programm with switch case..

vagelis wrote:
i want a solution

I already told you many times that you're not going to find a full solution here.

vagelis wrote:
if you write if then else its not a solution it is a joke

Nope, joke is what you see when you look in your mirror :)

vagelis wrote:
i dont want to loose my time

That's my line... -.-
Last edited on
Topic archived. No new replies allowed.