Reqd a prog in C++ for conversions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abbey07
    New Member
    • Sep 2006
    • 2

    Reqd a prog in C++ for conversions

    please can anbody help me in solving this problem in C programming!!!
    I have to write a program to convert the Decimal No. Sys in to Binary coded system.
    It has ben a headache these days for me to solve this problem.
    I m completely unable to solve this problem
  • rgb
    New Member
    • Aug 2006
    • 37

    #2
    this is one way to do it (simplified):

    int input=7;
    int r=0, x=0;
    while(input>0)
    {
    int y = i%2;
    input /= 2;
    x += y * pow(10,r++);
    printf("%d\n",x );
    }

    where
    "input" should be in some form of scanf( ) or other input command and statement "x += y * pow(10,r++);" should be change to some type of strcat( ) to prevent overflow.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by rgb
      this is one way to do it (simplified):

      int input=7;
      int r=0, x=0;
      while(input>0)
      {
      int y = i%2;
      input /= 2;
      x += y * pow(10,r++);
      printf("%d\n",x );
      }

      where
      "input" should be in some form of scanf( ) or other input command and statement "x += y * pow(10,r++);" should be change to some type of strcat( ) to prevent overflow.
      This is a very poor solution

      1. It does not compile (i should be input ?)

      2. It goes wrong is input >= 0x400

      3. Because it uses pow it decends into floating point arithmatic unnecessarily which could introduce rounding errors.

      4. It uses an int to represent a decimal coded binary value (which is why 2 happens), in my opinion very poor style.


      abbey, you sound like you have already made an attempt at doing this, ignore rgbs solution and post what you already have we will help you make it work.

      You should be able to do it by using the bitwise operators to determin if any given binary digit should be 1 or 0 and then you can store the result in a string and then print it out when finished.

      Comment

      • abbey07
        New Member
        • Sep 2006
        • 2

        #4
        Pls Help

        plese can any body help me out with this program in C lang.
        question is to convert Decimal Number System to Binary System.
        it is a very tough prog i guess. Plz give the prog wid correct input

        Comment

        • MOHIT AGRAWAL
          New Member
          • Sep 2006
          • 1

          #5
          Originally posted by abbey07
          plese can any body help me out with this program in C lang.
          question is to convert Decimal Number System to Binary System.
          it is a very tough prog i guess. Plz give the prog wid correct input
          this programe is working on every base
          #include<conio. h>
          #include<stdio. h>
          #include<alloc. h>
          void main()
          {
          int digit,num,i,x,b ase;
          int *p;
          clrscr();
          printf("\nenter number");
          scanf("%d",&num );
          printf("\nenter base");
          scanf("%d",&bas e);
          digit=0;
          x=num;
          do
          {
          num=num/base;
          digit++;
          }while(num!=0);
          p=(int *)malloc(sizeof (int));

          for(i=0;i<digit ;i++)
          {

          p[i]=x%base;
          x=x/base;
          }
          for(i=digit-1;i>=0;i--)
          {
          printf("%d",p[i]);
          }
          getch();
          }

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            abbey07 - please don't double post about the same problem

            MOHIT AGRAWAL - please only respond with code where appropriate, in this case I believe that abbey07 was posting a homework task, by giving them the answer instead of helping them write the answer themselves you prevent (or at least hinder) their learning. It is better for people to work out the solutions to their problems themselves even if they do need hints along the way.

            And BTW your solution is wrong because you do not allocate enough memory and because since you do not output a new line or flush the stdout the answer will not necessarily be printed. stdout only has to display output when it receives a new line.

            Comment

            • rgb
              New Member
              • Aug 2006
              • 37

              #7
              Originally posted by Banfa
              This is a very poor solution

              1. It does not compile (i should be input ?)

              2. It goes wrong is input >= 0x400

              3. Because it uses pow it decends into floating point arithmatic unnecessarily which could introduce rounding errors.

              4. It uses an int to represent a decimal coded binary value (which is why 2 happens), in my opinion very poor style.


              abbey, you sound like you have already made an attempt at doing this, ignore rgbs solution and post what you already have we will help you make it work.

              You should be able to do it by using the bitwise operators to determin if any given binary digit should be 1 or 0 and then you can store the result in a string and then print it out when finished.
              I don't want to give out the solution so i just give the idea that you need to do a modulo of 2 to get the remainder and then get the whole number part of the input divided by 2. plus I explicitly mention that the input part wasn't done as I believe as you said that this problem is some type of learning exercise in school setting. I also agree that it won't compile since I don't even have the function declaration.

              Comment

              Working...