About data combination
-
There are n arrays, and there are several elements in each. Count of array and count of elements in array are variable. A1{a1,a2,a3,an1} //there are n1 elements in array A1 B1{b1,b2,b4,bn2} //there are n2 elements in array B1 ..... N1{n1..nn} :confused://there are n elements in array N1 I need all the combination which are composed with one element of every array,order is same as the array's like: a1,b1,.....n1 a1,b2,.....n2 ... a2,b1,.....n1 ... an1,bn2,....nn
-
There are n arrays, and there are several elements in each. Count of array and count of elements in array are variable. A1{a1,a2,a3,an1} //there are n1 elements in array A1 B1{b1,b2,b4,bn2} //there are n2 elements in array B1 ..... N1{n1..nn} :confused://there are n elements in array N1 I need all the combination which are composed with one element of every array,order is same as the array's like: a1,b1,.....n1 a1,b2,.....n2 ... a2,b1,.....n1 ... an1,bn2,....nn
what about stl::Vector, you can use like this :-
#include<vector>
using namespace std;
typedef vector<int> vecINT;
typedef vector<vecINT> vecINTINT;vecINTINT vecArrays;
vecINT vecA;//insert your element of array
vecA = A1{a1,a2,a3,an1} //there are n1 elements in array A1// and copy it to main array
a.push_back(vecA);"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
what about stl::Vector, you can use like this :-
#include<vector>
using namespace std;
typedef vector<int> vecINT;
typedef vector<vecINT> vecINTINT;vecINTINT vecArrays;
vecINT vecA;//insert your element of array
vecA = A1{a1,a2,a3,an1} //there are n1 elements in array A1// and copy it to main array
a.push_back(vecA);"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
Thank you very much for your reply, but I am not quite clear about it. Would please explain it in detail? I need a algorithm to get all the combinations.
-
Thank you very much for your reply, but I am not quite clear about it. Would please explain it in detail? I need a algorithm to get all the combinations.
You've just to write a loop for each array (and nest the loops) is it difficult? You may also use recursion. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You've just to write a loop for each array (and nest the loops) is it difficult? You may also use recursion. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]CPallini wrote:
You may also use recursion.
recusion, sometimes it's quite difficult even for seasoned programmer. Though, recursion is good programming practice, come handy when you travelling through tree like data structure.. but it's a pain when you have to debug it :-)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
You've just to write a loop for each array (and nest the loops) is it difficult? You may also use recursion. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Because the count of array is variable, it is difficult to use loop. Recursion is good idea, but would you please write some sample code for me? thank u!!:rose:
-
Because the count of array is variable, it is difficult to use loop. Recursion is good idea, but would you please write some sample code for me? thank u!!:rose:
#include <vector>
#include <iostream>
using namespace std;
void step(unsigned int level, vector < vector <char> > & v, vector <char> & res);void main()
{vector< char > v1;
vector< char > v2;
vector< char > v3;
vector< char > v4;
vector< char > v5;vector < vector < char > > v;
v1.push_back('a');v1.push_back('b');v1.push_back('c');
v2.push_back('d');v2.push_back('e');
v3.push_back('f');v3.push_back('g');v3.push_back('h');
v4.push_back('i');v4.push_back('j');v4.push_back('k');v4.push_back('l');vector < char > res;
v.push_back(v1);v.push_back(v2);v.push_back(v3);v.push_back(v4);
res.resize(v.size());step(0, v, res);
}void step(unsigned int level, vector < vector <char> > & v, vector <char> & res)
{
for (unsigned int i=0; i<v[level].size(); i++)
{
res[level] = v[level][i];
if ( level == v.size()-1)
{
for (unsigned int k=0; k<res.size(); k++)
{
cout << res[k];
}
cout << endl;
}
else
{
step(level + 1, v, res);
}
}
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
#include <vector>
#include <iostream>
using namespace std;
void step(unsigned int level, vector < vector <char> > & v, vector <char> & res);void main()
{vector< char > v1;
vector< char > v2;
vector< char > v3;
vector< char > v4;
vector< char > v5;vector < vector < char > > v;
v1.push_back('a');v1.push_back('b');v1.push_back('c');
v2.push_back('d');v2.push_back('e');
v3.push_back('f');v3.push_back('g');v3.push_back('h');
v4.push_back('i');v4.push_back('j');v4.push_back('k');v4.push_back('l');vector < char > res;
v.push_back(v1);v.push_back(v2);v.push_back(v3);v.push_back(v4);
res.resize(v.size());step(0, v, res);
}void step(unsigned int level, vector < vector <char> > & v, vector <char> & res)
{
for (unsigned int i=0; i<v[level].size(); i++)
{
res[level] = v[level][i];
if ( level == v.size()-1)
{
for (unsigned int k=0; k<res.size(); k++)
{
cout << res[k];
}
cout << endl;
}
else
{
step(level + 1, v, res);
}
}
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]you are so nice~ :) :rose::rose::rose: