Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. About data combination

About data combination

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chrissie ja
    wrote on last edited by
    #1

    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

    T 1 Reply Last reply
    0
    • C Chrissie ja

      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

      T Offline
      T Offline
      ThatsAlok
      wrote on last edited by
      #2

      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" - Mixture

      cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

      C 1 Reply Last reply
      0
      • T ThatsAlok

        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" - Mixture

        cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

        C Offline
        C Offline
        Chrissie ja
        wrote on last edited by
        #3

        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.

        C 1 Reply Last reply
        0
        • C Chrissie ja

          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.

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          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]

          T C 2 Replies Last reply
          0
          • C CPallini

            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]

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            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" - Mixture

            cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

            1 Reply Last reply
            0
            • C CPallini

              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]

              C Offline
              C Offline
              Chrissie ja
              wrote on last edited by
              #6

              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:

              C 1 Reply Last reply
              0
              • C Chrissie ja

                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:

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                #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]

                C 1 Reply Last reply
                0
                • C CPallini

                  #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]

                  C Offline
                  C Offline
                  Chrissie ja
                  wrote on last edited by
                  #8

                  you are so nice~ :) :rose::rose::rose:

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups