449,173 Members | 1,079 Online Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

# variation with repetitions in C++

 P: n/a 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 Apr 4 '06 #1
4 Replies

 P: n/a > 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 Apr 4 '06 #2

 P: n/a Can you be a bit more specific please. B Apr 4 '06 #3

 P: n/a "Branka" schrieb im Newsbeitrag news:11**********************@v46g2000cwv.googlegr oups.com... 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[i] = Counter(limits[i]); } } bool Next() { for (size_t i = 0; i < elements.size(); ++i) { if (elements[i].Next()) return true; } return false; } private: std::vector 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 Apr 4 '06 #4

 P: n/a 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 Apr 4 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion. 