Jump to content

Need help with pseudocode please

- - - - -

  • Please log in to reply
3 replies to this topic

#1
nomss

nomss

    Newbie

  • Members
  • Pip
  • 2 posts
I have to calculate the maximum block in a binary number.
For ex:
Binary representation = 11111
Maximum block length = 5

Binary representation = 10111011
Maximum block length = 3

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.

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
The way I would do this is to set up a loop that examines one bit at the end at a time, and compares it with the value of the last bit from the previous operation. If it's a match, it increments a counter variable to keep track of how many similar bits it's encountered. If it's not a match, it checks the counter variable to see if you've reached a new maximum (another variable is needed to retain the current maximum) and then resets the counter to 1. After the comparison, you can perform a bitwise shift to push the examined bit off the end so the loop will examine the next bit in line. This entire loop will need to be constrained from the start to only run for as many times as there are bits in your input.

Another option is instead of using a bitwise shift to read the next bit, you could alternatively use a placeholder variable to mark which bit is currently being examined, and increment the placeholder each time. It's your choice. Either would work.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
nomss

nomss

    Newbie

  • Members
  • Pip
  • 2 posts
I'd like more clarification please.

#4
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Please take a look at this tutorial. Though it solves a different problem. But still it has the CODE and certain aspects common from the approach gregwarner has described above. You basically need to modify this code according to the approach he explained.

http://forum.codecal...ts-integer.html




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users