# Counting consecutive 1s?

Page 1 of 1

## 6 Replies - 348 Views - Last Post: 11 September 2012 - 09:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=291663&amp;s=2f4018913d0b2d00883101c2682c866c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 thatguybob

Reputation: 0
• Posts: 13
• Joined: 14-February 12

# Counting consecutive 1s?

Posted 11 September 2012 - 06:13 PM

I've got an assignment for a coding class to
1: Convert a decimal value to binary
2: Count the number of ones in the binary value
3: Count the largest number of 1 blocks (ex: 1001110111101111100's largest block has 5 1s)

I've done parts 1 and 2 but i'm having a little trouble with the loop required for part 3. So far all I have is a loop that checks through the array of 0s and 1s. I add to my blockcount variable if the current number in the array 1...but that's about it. I'm not sure where else to go from there if the number after that is a 0..

```int x;
int y;
int i;
int bin_array[31];
int b;
int blockcount;
int maxcount;
int mincount;
if(bin_array[y] == 1)
{
blockcount++
}
else
{
???
}

```

Is This A Good Question/Topic? 0

## Replies To: Counting consecutive 1s?

### #2 Skydiver

• Code herder

Reputation: 1896
• Posts: 5,687
• Joined: 05-May 12

## Re: Counting consecutive 1s?

Posted 11 September 2012 - 06:21 PM

How did you count the number of ones for part 2 of your assigment? You would do the same thing except each time you encounter a zero, you reset your count to zero. You then keep track of this count and remember the largest value you have see so far.

### #3 thatguybob

Reputation: 0
• Posts: 13
• Joined: 14-February 12

## Re: Counting consecutive 1s?

Posted 11 September 2012 - 06:33 PM

Skydiver, on 11 September 2012 - 06:21 PM, said:

How did you count the number of ones for part 2 of your assigment? You would do the same thing except each time you encounter a zero, you reset your count to zero. You then keep track of this count and remember the largest value you have see so far.

I was thinking it would be similar to the loop needed to count all the ones and I get what you're saying but I still don't know how i'm supposed to translate the bolded part into code.

```if(bin_array[y]==0)
{
onecount++;
bcount++;
}
else if(bin_array[y]==0)
{
bcount=0;
}
}

```

So if i'm resetting bcount to 0 if the loop encounters a 0, how should I be keeping track?

thatguybob, on 11 September 2012 - 06:30 PM, said:

Skydiver, on 11 September 2012 - 06:21 PM, said:

How did you count the number of ones for part 2 of your assigment? You would do the same thing except each time you encounter a zero, you reset your count to zero. You then keep track of this count and remember the largest value you have see so far.

I was thinking it would be similar to the loop needed to count all the ones and I get what you're saying but I still don't know how i'm supposed to translate the bolded part into code.

```if(bin_array[y]==0)
{
onecount++;
bcount++;
}
else if(bin_array[y]==0)
{
bcount=0;
}
}

```

So if i'm resetting bcount to 0 if the loop encounters a 0, how should I be keeping track?

err, first part is supposed to be bin_array[y]==1)

### #4 Skydiver

• Code herder

Reputation: 1896
• Posts: 5,687
• Joined: 05-May 12

## Re: Counting consecutive 1s?

Posted 11 September 2012 - 06:40 PM

This code keeps track of the largest number in an array:
```int highest = 0;
int arr[5] = { 1, 4, 8, 5, 7 };
for(int i = 0; i < 5; i++)
{
if (arr[i] > highest)
highest = arr[i];
}

```

Do you see the concept?

This post has been edited by Skydiver: 11 September 2012 - 06:41 PM

### #5 CTphpnwb

• D.I.C Lover

Reputation: 2485
• Posts: 8,518
• Joined: 08-August 08

## Re: Counting consecutive 1s?

Posted 11 September 2012 - 07:00 PM

Skydiver, what if the array contains all negative numbers? I'd do it this way:
```int arr[5] = { 1, 4, 8, 5, 7 };
int highest = arr[0];
for(int i = 1; i < 5; i++)
{
if (arr[i] > highest)
highest = arr[i];
}

```

This post has been edited by CTphpnwb: 11 September 2012 - 07:01 PM

### #6 Skydiver

• Code herder

Reputation: 1896
• Posts: 5,687
• Joined: 05-May 12

## Re: Counting consecutive 1s?

Posted 11 September 2012 - 08:39 PM

Good catch. That is a good generic.pattern to follow. :cheers:

For his bit counting, he won't have a first element to get the count of so zero or any negative number will have to be his starting state, unless you can recommend a different pattern.

Sent from my T-Mobile G2 using Tapatalk 2

### #7 CTphpnwb

• D.I.C Lover

Reputation: 2485
• Posts: 8,518
• Joined: 08-August 08

## Re: Counting consecutive 1s?

Posted 11 September 2012 - 09:36 PM

Yes, since a count can't be lower than zero it's fine to start it there.