tags:

views:

180

answers:

3

/edit: thanks for the help so far, however I haven't got any of the solutions to take the sample input and give the sample output. My description isn't the clearest, sorry.

I have an array composed of binary data. What I want to do is determine how long each unbroken segment of 1s or 0s is.

Say I have this data:

0111010001110

In an array binaryArray which I need to translate to:

0100110

stored in nwArray where 0 represents a narrow (less than 3 digits long) and 1 represents wide (>3 digits long). I am not concerned with the binary value but with the length of each component. I'm not sure if that explanation makes sense.

This is what I have; it doesn't work, I can see why, but I can't think of a good solution.

for(x=0;x<1000;x++){
 if(binaryArray[x]==binaryArray[x+1]){
  count++;
  if(count>=3){
   nwArray[y]=1;
   y++;
   count=0;
  }
 }else{
  if(barcodeArray[x]){
   nwArray[y]=0;
  }
 }
}
+3  A: 

One problem you have is that you compare count with 3 too early. Wait until you see a change in the bitstream. Try a while loop until the bit flips then compare the count.

Trent
+5  A: 

Does this do it?

int count = 0;
for (x=0; x<1000;x++)
{
    if (binaryArray[x] != binaryArray[x+1]) 
    {
        if (count < 3)
           nwArray[y]=0;
        else 
           nwArray[y]=1;

        y++;
        count = 0;
    }
    else
        count++;
}
MikeW
You've forgotten to write the last element (eg. in the case that binaryArray[998] == binaryArray[999]) , but other than that it looks fine.
Nils Pipenbrinck
Possibly better to rewrite it as [x-1] ... [x] anyway. Thanks
MikeW
no problem. I just saw it because it's one of the typical errors I write (forgetting the last corner-case that is) :-)
Nils Pipenbrinck
A: 

Modified @MikeW's answer:

int count = 0;
int nwSize = 0;    
const int ilast = SIZEOF(binaryArray) - 1;
for (int i = 0; i <= ilast; ++i)
  if (i == ilast || binaryArray[i] != binaryArray[i+1]) {
    nwArray[nwSize++] = (count > 1); /* true for '1110'; false for '110' */
    count = 0;
  }
  else
    ++count;
assert(count == 0);
J.F. Sebastian