Thread: Two things about my program that I don't quite understand (but it works good)

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    Two things about my program that I don't quite understand (but it works good)

    I've been working with a program that inputs an integer and prints it's binary representation. The program works fine, but there are two things I don't get.

    The final loop at the bottom of convert function prints the array in reverse order but I don't understand why it needs to be written that way. The integer j is assigned whatever variable i is minus one to start that loop.

    Also I want to always print out eight bit places. If I enter the integer 58 I want it to output 00111010. But my program just outputs 111010. So how can I get it to always output all eight bit places?

    Code:
    void convertToBinary(int n)
    {
    	int binaryNumber[8];
    	int i = 0;
    
    	while(n > 0)
    	{
    		binaryNumber[i] = n % 2; // store the 1's and 0's in array
    		n /= 2;
    		i++;
    	}
    
    	cout << "The binary equivalent is ";
            
            
            // print the binary digits in reverse order
    	for(int j = i - 1; j >= 0; j--) // why is this written like this
    		cout << binaryNumber[j] << " ";
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    Okay I figured out why we set j to whatever i is minus one. Because after the final division takes place the value of i gets incremented one more time and we don't want to over run the array bounds. Good! But I don't know how to get it to print all eight bit places.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like so
    Code:
    void convertToBinary(int n)
    {
        int binaryNumber[8] = { 0 };  // so you get leading zeros, and not junk
        int i = 0;
     
        while(n > 0)
        {
            binaryNumber[i] = n % 2; // store the 1's and 0's in array
            n /= 2;
            i++;
        }
     
        cout << "The binary equivalent is ";
             
             
            // print the binary digits in reverse order (start at 7 to get all 8)
        for(int j = 7; j >= 0; j--) // why is this written like this
            cout << binaryNumber[j] << " ";
     
    }
    > Join Date Apr 2007
    And welcome back
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And you were doing so well.....

    My program works good but there are two things I don't understand yet

    But then you cross-posted and wasted my time
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 09-14-2016, 07:56 PM
  2. Replies: 2
    Last Post: 05-10-2015, 03:40 PM
  3. Replies: 4
    Last Post: 08-18-2009, 03:32 PM
  4. A few things I don't understand
    By Opel_Corsa in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2006, 01:13 AM
  5. Need help(Don't understand a few things)
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 10-06-2006, 09:52 PM

Tags for this Thread