473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

variation with repetitions in C++

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 3907
> 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
Can you be a bit more specific please.

B

Apr 4 '06 #3
"Branka" <b_*******@hotmail.com> 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<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
Apr 4 '06 #4
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alison Bowes | last post by:
I know that if $subname contains the name of the subroutine I want to call, I can call that subroutine via &$subname; However, my subroutine names are in an array, so if I want to call $subname...
8
by: MG | last post by:
I'm in need of a form method similar to the typcial "file upload", but rather than submitting the entire file, I need just the filename. I cannot find anything as part of FORM that would allow...
3
by: j0mbolar | last post by:
I would like to see what the majority of people prefer when it comes to elegance, clarity, etc. I'll present the problem first based on having a product, and one product only, at a given time...
4
by: darin dimitrov | last post by:
Hello, I need help with an algoritm that given a set of "n" distinct numbers will generate all the possible permutations of fixed length "m" of these numbers WITH repetitions (a total of n^m...
5
by: Eric | last post by:
I am implementing a variation on the Singleton design pattern, that allows up to 8 objects of a class to be instantiated, and returns a null pointer for anything more than 8. I am running into a...
5
by: coolindienc | last post by:
Plz help me to find out how to count numbers of repetitions of loop and do output of it in C++. Andy
2
by: nick048 | last post by:
Hi, I am a new user of C++ and I am learning this language. I want to resolve this example: 1) I have an indefinite sequence of integer (from 0 to 36) as Input; 2) I want to know the maximum...
13
numberwhun
by: numberwhun | last post by:
Hello everyone! I have a data file that contains miscellaneous information on each line. (Unfortunately, I cannot go into detail of the file layout as it is sensitive information), but I can say...
1
by: spamtrap | last post by:
Hy; I've got a nasty variation of the Guillotine-bug on a three-column layout, sadly the variations of the Holly-hack I tried to apply don't work: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.