Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

2d array element change

44 views
Skip to first unread message

axc...@gmail.com

unread,
Nov 30, 2013, 8:27:13 PM11/30/13
to
I have 2d array (10x12), and they have 0 and 1s in them. I want to count the 1s in each array and I have two cases here: (1) if an array has more than three 1s, say k many, then generate k - 3 random variables and replace that many 1s with 0s if that random variable corresponds to a 1. (2)if an array has less than three 1s, say q many, then generate 3 - q random variables and replace that many 0s with 1s if that random variable corresponds to a 0. I have the following code so far. I need some suggestions here, I could not construct the code well.

int y[10];
int r[10];

for(int i=0; i < 10; i++){
for(int j=0; j < 12; j++)
{
y[i] += Array[i][j];
}
if(y[i] > 3)
{
f = y[i] - 3;
if(Array[i][r[f]] ==1)
Array[i][r[f]] = 0;
}
if(y[i] < 3)
{
f = 3 - y[i];
if(Array[i][r[f]] ==0)
Array[i][r[f]] = 1;
}
}


Thanks

Alf P. Steinbach

unread,
Nov 30, 2013, 9:01:15 PM11/30/13
to
On 01.12.2013 02:27, axc...@gmail.com wrote:
> I have 2d array (10x12), and they have 0 and 1s in them. I want to count
> the 1s in each array and I have two cases here: (1) if an array has more
> than three 1s, say k many, then generate k - 3 random variables and replace
> that many 1s with 0s if that random variable corresponds to a 1. (2)if an
> array has less than three 1s, say q many, then generate 3 - q random
> variables and replace that many 0s with 1s if that random variable
> corresponds to a 0. I have the following code so far. I need some suggestions
> here, I could not construct the code well.

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.

?


> int y[10];
> int r[10];
>
> for(int i=0; i < 10; i++){
> for(int j=0; j < 12; j++)
> {
> y[i] += Array[i][j];
> }

You are not guaranteed that `y` is all zeroes, since `y` is a non-static
variable.

To guarantee all zeros you need to explicitly /zero-initialize/:

int y[10] = {};

But if you use a `std::vector` then its constructor will do it for you:

vector<int> y(10);



> if(y[i] > 3)
> {
> f = y[i] - 3;
> if(Array[i][r[f]] ==1)
> Array[i][r[f]] = 0;

This part doesn't appear meaningful.

Presumably this part is what you're seeking help with?


Cheers,

- Alf

axc...@gmail.com

unread,
Nov 30, 2013, 9:29:37 PM11/30/13
to
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.

?

Yes, I need help in that part but I am also not confident about the usage of "f" and "y", as well.

Thanks

Alf P. Steinbach

unread,
Nov 30, 2013, 10:18:20 PM11/30/13
to
On 01.12.2013 03:29, axc...@gmail.com wrote:
> On Saturday, November 30, 2013 7:27:13 PM UTC-6, axc...@gmail.com 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[i] );
}

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

Öö Tiib

unread,
Dec 1, 2013, 6:13:23 AM12/1/13
to
On Sunday, 1 December 2013 03:27:13 UTC+2, axc...@gmail.com wrote:
> I have 2d array (10x12), and they have 0 and 1s in them.

Value that can be 1 or 0 is called bit. To have array of 12 bits
there is 'std::bitset<12>'.

typedef std::bitset<12> TwelveBits;

Then put it into array of 10

typedef std::array<TwelveBits,10> BitArray10x12;

> I want to count
> the 1s in each array ...

'std::bitset' has member function conveniently
called 'count':

TwelveBits foo (std::string("101110110011"));
std::cout << foo << " has ";
std::cout << foo.count() << " ones and ";
std::cout << (foo.size()-foo.count()) << " zeros.\n";

> ... and I have two cases here: (1) if an array has more
> than three 1s, say k many, then generate k - 3 random variables and
> replace that many 1s with 0s if that random variable corresponds to a 1.
> (2)if an array has less than three 1s, say q many, then generate 3 - q
> random variables and replace that many 0s with 1s if that random variable
> corresponds to a 0. I have the following code so far. I need some suggestions
> here, I could not construct the code well.

Yes, I removed your code since it did look confused.
There were no traces of making anything random. Try something like that:

std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0,11);
auto gen = std::bind( distribution, generator );

int RandomFromTwelve = gen(); // generates int in range 0..11

Barry Schwarz

unread,
Dec 1, 2013, 1:22:57 PM12/1/13
to
On Sat, 30 Nov 2013 17:27:13 -0800 (PST), axc...@gmail.com wrote:

Please set your configuration to limit the lines of text to something
less than 80 characters.

>I have 2d array (10x12), and they have 0 and 1s in them. I want to count
>the 1s in each array and I have two cases here: (1) if an array has more than

From the code you provided, it appears you meant to count the 1s in
each row.

>three 1s, say k many, then generate k - 3 random variables and replace that
>many 1s with 0s if that random variable corresponds to a 1. (2)if an array has
>less than three 1s, say q many, then generate 3 - q random variables and replace
>that many 0s with 1s if that random variable corresponds to a 0. I have the following
>code so far. I need some suggestions here, I could not construct the code well.
>
>int y[10];
>int r[10];

The values in both of these arrays are indeterminate.

>for(int i=0; i < 10; i++){
> for(int j=0; j < 12; j++)
> {
> y[i] += Array[i][j];

Apparently you want the array y to be set to all zeros. You must do
so explicitly, preferably using initialization as part of the array's
definition.

> }
> if(y[i] > 3)
> {
> f = y[i] - 3;

At this point, you know how many random variables you want which is
also the maximum number of 1s that may be replaced with 0s.

> if(Array[i][r[f]] ==1)
> Array[i][r[f]] = 0;

You never generate any of the random values. Your description says
you want to generate f of then. It turns out that once you have a
random value, you can process it completely so there is no need to
save the value, thus eliminating the need for array r.

The maximum possible value of f is 9. You need a loop that runs from
0 to f-1 (or from 1 to f) that in each iteration generates a random
value (call it rv) between 0 and 11. You can then test element
Array[i][rv] and change it if appropriate.

> }
> if(y[i] < 3)
> {
> f = 3 - y[i];
> if(Array[i][r[f]] ==0)
> Array[i][r[f]] = 1;
> }
> }

Since the contents of one row do not affect the processing of a
different row, there is no need for y to be an array either. If you
make it a simple int and set it to zero at the top of the i loop, you
can replace y[i] with y.

--
Remove del for email
0 new messages