1-DimArray Counting same elements (0,1)
Posted
by Chris
on Stack Overflow
See other posts from Stack Overflow
or by Chris
Published on 2010-05-23T11:31:18Z
Indexed on
2010/05/23
11:40 UTC
Read the original article
Hit count: 134
Hello,
I have a 1-dim array that fills up a table of 40 random elements (all the values are either 0 or 1).
So i wanne "count" the largest row of the values net to each otherto each other..
Meaning for example : 111100101 => the longest row would be 1111 (= 4 elements of the same kind closest to each other).
So 011100 would result in the longest row being 3 elements (3 x 1).
My problem i have no idea how to check upon the "next element" and check if its a 0 or 1. Like the first would be 1111 (count 4) but the next would be a 0 value => meaning i have to stop counting.
My idea was placing this value (4) in a other array (example: 111100101) , and place the value of the 1's back on zero.
And start the process all over again.
To find the largest value i have made a other method that checks up the biggest value in the array that keeps track of the count of 0's 1's, this is not the problem.
But i cannot find a way to fill the array tabelLdr up. (having all the values of the group of elements of the same kind (being 0 or 1).
In the code below i have 2 if's and offcourse it will never go into the second if (to check if the next value in the array is != then its current state (being 0 or 1)
Best Regards.
public void BerekenDeelrij(byte[] tabel, byte[] tabelLdr)
{
byte LdrNul = 0, Ldréén = 0;
//byte teller = 0;
for (byte i = 0; i < tabel.Length; i++)
{
if (tabel[i] == 0)
{
LdrNul++;
//this 2nd if cleary does not work, but i have no idea how to implend this sort of idea in my program.
if (tabel[i] == 1) //if value != 0 then the total value gets put in the second array tabelLdr,
{
tabelLdr[i] = LdrNul;
LdrNul = 0;
}
}
if (tabel[i] == 1)
{
Ldréén++;
if (tabel[i] == 0)
{
tabelLdr[i] = Ldréén;
Ldréén = 0;
}
}
}/*for*/
}
© Stack Overflow or respective owner