Given a binary number calculate the maximum block.
For ex:
Binary representation = 11111
Maximum block length = 5
Binary representation = 10111011
Maximum block length = 3
max block means the number of consecutive 1’s or 0’s. So 00010000 would have a max block of 4
Above are the only 2 examples my professor gave.
“Compute the maximum block length of the binary
representation.” This is what he said. I’m assuming this includes 0s as well. I really don’t know how to go about it.
This is what I have come up with:
Let B = the binary number received.
put B into A[], each digit representing an element.
assume A[0] = 1
for A.length – 1
count 1s as long as not hit zero
max = total count of 1s.
count 0s as long as not hit 1
update max if necessary
repeat.
So I figured it out. Here’s the complete code. This gets the user input, converts the decimal number to binary, counts the maximum block of 1’s and counts total 1’s. There is still small bug, input should be accepted if 0 is entered and the program should work properly, but it does not.