Binary converter Help

Hi I'm having a little trouble with learning properly C, now I'm having trouble with a program that converts decimal numbers to binary. I'll appreciate a hand.

[code]#include
#include
int a[10],x,z,n,i,y;
float b[10];
void main()
{
clrscr();
gotoxy(10,4);
printf("Give a decimal number");
scanf("%f",&x);
n=0;
i=0;
while(x>0)
{
y=x%2;
x=x/2;
n=n+1;
a[i]=y;
i=i+1;
}
for(i=0;i<=n;i++)
{
b[i]=a[n-i];
printf("
%d",b[i]);
}
getch();
}[/code]

Comments

  • : Hi I'm having a little trouble with learning properly C, now I'm
    : having trouble with a program that converts decimal numbers to
    : binary.

    What trouble?
  • : : Hi I'm having a little trouble with learning properly C, now I'm
    : : having trouble with a program that converts decimal numbers to
    : : binary.
    :
    : What trouble?
    :
    It doesnt run, i dont know what I did wrong.
    It gives me cero as a result all the time.
    Thanks.
  • : It doesnt run, i dont know what I did wrong.
    : It gives me cero as a result all the time.
    : Thanks.
    :

    How can you get zero as a result when it doesn't run?
  • : : It doesnt run, i dont know what I did wrong.
    : : It gives me cero as a result all the time.
    : : Thanks.
    : :
    :
    : How can you get zero as a result when it doesn't run?

    I meant it doesnt run properly, it gives me de correct number of figures but they are all zeroes.
  • One basic strategy for debugging:
    Try to minimize the problem. Can you print a number? Can you print an indexed number? Is the first loop working properly? Is the second?

    The last two questions could be answered by using a lot of print statements to debug.

    Search the internet for more advanced debugging techniques.
  • : One basic strategy for debugging:
    : Try to minimize the problem. Can you print a number? Can you print
    : an indexed number? Is the first loop working properly? Is the second?
    :
    : The last two questions could be answered by using a lot of print
    : statements to debug.
    :
    : Search the internet for more advanced debugging techniques.

    Now I'm doubting whether I am 'allowed' to help out...
    "Would IDK be mad if I helped?"

    At any rate, I'm going to ask you a question that's *hint hint*:
    When you have the numbers stored as Integers, why-o-why print them as float (which in particular isn't what you want since float 0.0 == 0.0 might end up being 'false', eg 0.0 might be printed as 0.000001E-99).

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry

  • : Now I'm doubting whether I am 'allowed' to help out...
    : "Would IDK be mad if I helped?"
    :

    Not at all... I just wanted him to know that he could fix it by himself by asking the right questions and then answering them... But I was also wrong, and you're not, so salute to you.
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