tags:

views:

27

answers:

3

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*/
    }
A: 

Even while i is the loop counter, it is still just a variable. A valid for statement is for (;;), which is an infinite loop. Notice the for statement increments i, as in i++. The expression i = i + 1 works just as well.

AMissico
A: 

This method should do what you need:

public int LargestSequence(byte[] array) {
  byte? last = null;
  int count = 0;
  int largest = 0;
  foreach (byte b in array) {
    if (last == b)
      ++count;
    else {
      largest = Math.Max(largest, count);
      last = b;
      count = 1;
    }
  }
  return Math.Max(largest, count);
}
Julien Lebosquain
A: 

Im unsure if you need the longest "row" of ones or longest row of either 0 or 1. This will work for the latter

var max = 0;
var start = 0;
var current = -1;
var count = 0;
for(int i = 0;i<table.Length;i++)
{
   if(current = table[i])
   {
      count++;
   }
   else
   {
      current = table[i];
      if(max < count)
      {
        max = count;
        start = i-count;
      }
      count = 1;
   }
}

 if(max < count)
      {
        max = count;
        start = i-count;
      }

//max is the length of the row starting at start;

Rune FS