variation with repetitions in C++

Discussion in 'C++' started by Branka, Apr 4, 2006.

  1. Branka

    Branka Guest

    Hi I have 20 'for' loops to create more than 40 million variations with
    repetitions. More precisely: I have total of nine factors with three
    possible levels (3^9) and 11 factors with 2 possible levels (2^11),
    which gives total of 40 310 784 possible variations.

    for (a=0; a<3; a++)
    {for (b=0; b<2; b++)
    {for (c=1; c<3; c++)
    {for (d=0; d<3; d++)
    {for (e=0; e<3; e++)
    {for (f=1; f<3; f++)
    {for (g=0; g<3; g++)
    {for (h=1; h<3; h++)
    {for (i=0; i<3; i+=2)
    {for (j=0; j<3; j++)
    {for (k=1; k<3; k++)
    {for (l=0; l<3; l++)
    {for (m=0; m<3; m+=2)
    {for (n=0; n<3; n++)
    {for (p=0; p<3; p+=2)
    {for (q=0; q<3; q++)
    {for (t=1; t<3; t++)
    {for (u=0; u<3; u++)
    {for (x=1; x<3; x++)
    {for (y=1; y<3; y++)


    Using this approach I have to create a text file which lists all
    possible variation and access it for some additional operations.

    I was wondering if anyone has an idea how to make this more 'object
    oriented' by creating a code which handles the pattern of these
    variation and thus give me the ability to skip creation of text file
    (requires a lots of memory) and simply create one variation after the
    other so that I can do all necessary operation on only one variation at
    the time.

    I would really appreciate any help or hint where I can find this code.

    B
    Branka, Apr 4, 2006
    #1
    1. Advertising

  2. Branka

    benben Guest

    > I was wondering if anyone has an idea how to make this more 'object
    > oriented' by creating a code which handles the pattern of these
    > variation and thus give me the ability to skip creation of text file
    > (requires a lots of memory) and simply create one variation after the
    > other so that I can do all necessary operation on only one variation at
    > the time.
    >
    > I would really appreciate any help or hint where I can find this code.
    >
    > B
    >


    You can use std::set and std::next_permutation, although they are not
    object oriented.

    Regards,
    Ben
    benben, Apr 4, 2006
    #2
    1. Advertising

  3. Branka

    Branka Guest

    Can you be a bit more specific please.

    B
    Branka, Apr 4, 2006
    #3
  4. Branka

    Heinz Ozwirk Guest

    "Branka" <> schrieb im Newsbeitrag news:...
    > Hi I have 20 'for' loops to create more than 40 million variations with
    > repetitions. More precisely: I have total of nine factors with three
    > possible levels (3^9) and 11 factors with 2 possible levels (2^11),
    > which gives total of 40 310 784 possible variations.
    >

    ....
    >
    > I was wondering if anyone has an idea how to make this more 'object
    > oriented' by creating a code which handles the pattern of these
    > variation and thus give me the ability to skip creation of text file
    > (requires a lots of memory) and simply create one variation after the
    > other so that I can do all necessary operation on only one variation at
    > the time.


    If you need objects very badly you could implement one class which simply counts from 0 to n-1 for a given n:

    class Counter
    {
    public:
    Counter(size_t n): limit(n), current(0) {}
    bool Next() { return (current % limit) != 0;
    private:
    size_t limit;
    size_t current;
    };

    Then create another class to represent a "string" of counters:

    class Counters
    {
    Counters(size_t n, size_t* limits): elements(n)
    {
    for (size_t i = 0; i < n; ++i)
    {
    elements = Counter(limits);
    }
    }
    bool Next()
    {
    for (size_t i = 0; i < elements.size(); ++i)
    {
    if (elements.Next()) return true;
    }
    return false;
    }
    private:
    std::vector<Counter> elements;
    };

    Ok, add some method to access the values of all those counters, create an instance of Counters with 20 elements and the limits you need and the start a loop which ends when Counters::Next returns false.

    Of cause you can also forget about objects and simply count from 0 to 2^11 + 3^9 and use / and % to extract your "digits" from that number.

    HTH
    Heinz
    Heinz Ozwirk, Apr 4, 2006
    #4
  5. Branka

    benben Guest

    Branka wrote:
    > Can you be a bit more specific please.
    >
    > B
    >


    Sorry, I meant std::multiset since repetition is allowed. What you want
    to do, I think, is to create the permutations of a set of 1, 2 and 3.
    Consult your book on std::next_permutation it should be very
    straightforward.

    Regards,
    Ben
    benben, Apr 4, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darin dimitrov
    Replies:
    4
    Views:
    1,320
  2. nick048

    How to count some repetitions

    nick048, Nov 1, 2006, in forum: C++
    Replies:
    2
    Views:
    349
    Daniel T.
    Nov 1, 2006
  3. nick048

    How to count some repetitions

    nick048, Nov 1, 2006, in forum: C++
    Replies:
    0
    Views:
    303
    nick048
    Nov 1, 2006
  4. tony
    Replies:
    1
    Views:
    133
    J├╝rgen Exner
    Jul 23, 2003
  5. Wolfram Humann

    Matching repetitions with /g

    Wolfram Humann, Aug 18, 2011, in forum: Perl Misc
    Replies:
    6
    Views:
    205
    Wolfram Humann
    Aug 23, 2011
Loading...

Share This Page