On Saturday, November 30, 2013 7:27:13 PM UTC-6, (e-mail address removed) wrote: [snip requote of original]
Yes, it is exactly what I want to do.
Is this a fair restatement:
* You have 10 arrays, each of 12 items, each item is either 0 or 1.
* In each array:
- Let n = count of 1 values in the array.
- If n < 3, then randomly change 0s into 1s until n = 3.
- If n > 3, then randomly change 1s into 0s until n = 3.
?
OK, but first, the convention for this group is to quote some context,
below that add your response or new text, perhaps quote some more, add
response below, and so on. That's called /bottom posting/. In contrast,
the above, with response first and then quote of the context, is called
/top posting/ and, although that's fine in e-mail (mostly because it
would be practically impossible to re-educate the zillions of e-mail
users), it's not-so-fine on Usenet.
See <url:
http://www.parashift.com/c++-faq/netiquette.html> for more info.
The part about top-posting is in the 4th bullet point.
* * *
Now, (I believe that) the problems mainly stem from /doing too much/ in
one place of the code.
So I suggest, define a suitably named function for counting 1-bits, so
you get that code moved away from the main code.
Also define a suitably named function for replacing one random 0 in an
array, with 1.
And I suspect that it will help with a suitably named function that
given an array returns the index of the N'th zero-bit in that array.
Also, a suitably named function to generate a random integer in the
range [0...M] inclusive, given M as an argument.
Note that I'm using uppercase single-letter names here simply for
brevity. In source code both practices are generally Evil(TM). In order
to avoid undesired text substitutions and name clashes it's a good idea
to reserve uppercase names for macros, and both for readability and in
order to avoid visual confusion between e.g. 1 and l and between e.g. O
and 0, it's a good idea to generally avoid single-letter names, except
where they're established conventions such as for loop control.
* * *
So, the main code that you showed will now, in C++03, look something like
for( int i = 0; i < n_arrays; ++i )
{
ensure_3_nonzero_bits_in( bit_arrays
);
}
or in C++11 like
for( auto& a : bit_arrays )
{
ensure_3_nonzero_bits_in( a );
}
anyway with greatly reduced complexity in that top-level code, and
likewise in each called function (just define functions for relevant
chunks of code), and so on down -- the name of the game is /naming/.
Cheers & hth.,
- Alf