Search code examples
binarypseudocode

Need help with pseudocode please


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.


Solution

  • 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.

    import java.util.Scanner;
    
    public class Test 
    {
        private static int decimalNumber = 0;
        private static String binaryNumber = "";
        // Get user input and return it
        // Check to make sure the input received is >= 0
        // If not > 0 then ask for input again
        private static void getInput()
        {
            Scanner sc = new Scanner(System.in); // Scanner to get user input
            System.out.println("Please type in a number >= 0"); // Tells user to type a number > 0
            decimalNumber = sc.nextInt(); // Stores the input in decimalNumber
            if(decimalNumber < 0)   // Loop to make sure input received is > 0
            {
                System.out.println("Incorrect input received"); // Tells the user input received was incorrect.
            }
        }
    
        private static void toBinary() 
        {
            while (decimalNumber != 0) 
            {
                if (binaryNumber.length() % 5 == 0) 
                {
                    binaryNumber = "" + binaryNumber;
                }
                binaryNumber = (decimalNumber % 2) + binaryNumber;
                decimalNumber /= 2;
            }
            System.out.println("Binary representation = " + binaryNumber);
        }
    
        public static void countOnes()
        {
            int ones = 0;
            for(int i=0; i < binaryNumber.length(); i++)
            {
                if(binaryNumber.charAt(i) == '1')
                {
                    ones++;
                }
            }
            System.out.println("No. of 1’s in the binary representation = " + ones);
        }
    
        public static void maximumBlock()
        {
            int block = 0;
            int maxBlock = 0;
            for(int i=0; i < binaryNumber.length(); i++)
            {
                if((binaryNumber.charAt(i) == '1') && (i < binaryNumber.length()))
                {
                    block++;
                    if(maxBlock < block)
                    {
                        maxBlock = block;
                    }
                }
                else
                {
                    block = 0;
                }
            }
            System.out.println("Maximum block length = " + maxBlock);
        }
    
        public static void main(String[] args)
        {
            getInput();
            toBinary();
            countOnes();
            maximumBlock();
        }
    }