Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

c programing, binary to decimal?

This program should output a number made up of 16,8,4,2,1. a 1 adding to the counter and a 0 not. when i run the program i get massive numbers?

#include <stdio.h>

int main(int argc, char** argv)

{

int input;

unsigned int counter;

printf ("Please input a 5 digit binary number\n") ;

scanf( "%d" , &input );

if (input / 10000 == 1) {counter = counter + 16 ;}

else if (input / 10000 == 0) {counter = counter + 0 ;}

input = input - 10000 ;

if (input / 1000 == 1) {counter = counter + 8 ;}

else if (input / 1000 == 0) {counter = counter + 0 ;}

input = input - 1000 ;

if (input / 100 == 1) {counter = counter + 4 ;}

else if (input / 100 == 0) {counter = counter + 0 ;}

input = input - 100 ;

if (input / 10 == 1) {counter = counter + 2 ;}

else if (input / 10 == 0) {counter = counter + 0 ;}

input = input - 10 ;

if (input / 1 == 1) {counter = counter + 1 ;}

else if (input / 1 == 0) {counter = counter + 0 ;}

printf ("%u" , &counter ) ;

}

3 Answers

Relevance
  • 8 years ago
    Favorite Answer

    Your method would appear to be fine, if a little unusual, at first glance. But it is actually wrong for more than one reason. Of course, the comments you already received about making sure your local variables are initialized are correct and you should do that. But that is just one reason.

    You also don't need to add 0 values to your counter (0 is the additive identity and adding it changes nothing.) So the extra code there really isn't necessary.

    Now for another reason the code won't work. While your first division by 10000 may work (assuming the user doesn't supply a really big number), the other divisions won't work in many cases. You need to extract the digit, which means getting rid of the higher order digits... not just dividing by some power of 10.

    So ultimately, the code has at least two serious flaws. One is not initializing your counter variable. The other is not properly extracting digits. You can use the modulo function to do that.

    Here's an approach that is a little shorter, but uses a loop and correctly isolates digits. It destroys the value of 'input' in the process. But if that is okay, then the following should meet your needs

        #include <stdio.h>

        int main( int argc, char *argv[] ) {

            unsigned int input, result, bit;

            printf( "Please input a 5 digit binary number\n" );

            scanf( "%u", &input );

            for ( result= 0, bit= 1; input != 0; bit <<= 1, input /= 10 )

                if ( (input % 10) != 0 ) result += bit;

            printf( "%u\n", result );

            return 0;

        }

  • roger
    Lv 7
    8 years ago

    counter contains garbage when you start since you did not initialize it...

    change

    unsigned int counter;

    to

    unsigned int counter=0;

  • Anonymous
    8 years ago

    You think it might be a good idea to get rid of the garbage in counter, or do you want random results?

    -=-

    LOL

    -=-

    initialize all variables, always. always.

Still have questions? Get your answers by asking now.