need help in my program

I'm trying to create a program that converts binary numbers to decimal.
then i ran the program for a test.

i entered an input of 1010,then i get a result of 5. then,
i tried to solve it by using a calculator and get a result of 10.

why is that..?

[code]
#include
#include
#include

main()
{

int ctr, sum;
char bin[50];
sum = 0;

clrscr();
printf("binary: ");

gets(bin);

for(ctr=strlen(bin)-1;ctr>-1;ctr--)
{
if(bin[ctr]=='1')
sum = sum * 2 + 1;
else
sum = sum * 2;
}

printf("decimal: %d",sum);
getch();
}

Comments

  • : I'm trying to create a program that converts binary numbers to
    : decimal.
    : then i ran the program for a test.
    :
    : i entered an input of 1010,then i get a result of 5. then,
    : i tried to solve it by using a calculator and get a result of 10.
    :
    : why is that..?
    :
    : [code]:
    : #include
    : #include
    : #include
    :
    : main()
    : {
    :
    : int ctr, sum;
    : char bin[50];
    : sum = 0;
    :
    : clrscr();
    : printf("binary: ");
    :
    : gets(bin);
    :
    : for(ctr=strlen(bin)-1;ctr>-1;ctr--)
    : {
    : if(bin[ctr]=='1')
    : sum = sum * 2 + 1;
    : else
    : sum = sum * 2;
    : }
    :
    : printf("decimal: %d",sum);
    : getch();
    : }[/code]:

    For the binary number in your example 1010 that is 10 because
    reading right to left in binary the position values are 1, 2, 4, 8
    so you have a 2 and an 8 value and that makes 10.

    You would add only 1 to the sum iff (if and only if) there is a 1 in the 1s position and never after that.

    So basically you have to do this: is the 1s 0 or 1? yes add 1, no add 0
    is the 2s position 0 or 1? yes add 2, no add 0
    and that goes on forever.

    Basically your loop to read each binary position is tallying incorrectly.


    [hr]
    We'll be an army of theives
    Of self-freed slaves
    Of mild-mannered maids
    We'll fight with whispers and blades
    So get ready, a new day is dawning
    - The New Wild West -- Jewel
  • : I'm trying to create a program that converts binary numbers to
    : decimal.
    : then i ran the program for a test.
    :
    : i entered an input of 1010,then i get a result of 5. then,
    : i tried to solve it by using a calculator and get a result of 10.
    :
    : why is that..?
    :
    : [code]:
    : #include
    : #include
    : #include
    :
    : main()
    : {
    :
    : int ctr, sum;
    : char bin[50];
    : sum = 0;
    :
    : clrscr();
    : printf("binary: ");
    :
    : gets(bin);
    :
    : for(ctr=strlen(bin)-1;ctr>-1;ctr--)
    : {
    : if(bin[ctr]=='1')
    : sum = sum * 2 + 1;
    : else
    : sum = sum * 2;
    : }
    :
    : printf("decimal: %d",sum);
    : getch();
    : }[/code]:

    Your logic is sound... your loop-counter is, however, going the wrong way. The logic works for left-to-right reading, whereas the loop counter goes from right to left.
    So: "for(ctr=0; ctr<strlen(bin); ctr++)" would do the trick.
    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories