6 Replies - 351 Views - Last Post: 11 September 2012 - 09:36 PM Rate Topic: -----

#1 thatguybob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is online

  • Code herder
  • member icon

Reputation: 1931
  • View blog
  • Posts: 5,757
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 thatguybob  Icon User is offline

  • New D.I.C Head

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

Re: Counting consecutive 1s?

Posted 11 September 2012 - 06:33 PM

View PostSkydiver, 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?

View Postthatguybob, on 11 September 2012 - 06:30 PM, said:

View PostSkydiver, 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)
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1931
  • View blog
  • Posts: 5,757
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1931
  • View blog
  • Posts: 5,757
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1