Thread: decimal to binary conversion

  1. #31
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I saw it.
    I have now this
    Code:
    /*
     *  * =====================================================================================
     *   
     *          Filename:  test2.c
     *    
     *          Description:  conventer from dec to 0101
     *       
     *          Version:  1.0
     *          Created:  14-05-11 10:03:25
     *          Revision:  none
     *          Compiler:  gcc
     *            
     *          Author:  Dr. Fritz Mehner (mn), [email protected]
     *          Company:  FH Südwestfalen, Iserlohn
     *               
     * =====================================================================================
      */
    
    #include        <stdio.h>
    #include        <stdlib.h>
    
    int
    main ( int argc, char *argv[] )
            {
                int nummer, uitkomst, teller, teller2;
                char uitkomst2[10];
                nummer = 4 ;
                uitkomst = 4 ;
                teller = 0 ;
                while (uitkomst > 0)
                    {
                            uitkomst = nummer / 2 ;
                            uitkomst2[teller++]= uitkomst%2;
                            nummer = uitkomst;
                            printf ("teller:%d  deling:%d uitkomst deling:%d  \n",teller, uitkomst%2, uitkomst);
                    }
                teller2 = 1 ;
               while (teller2 < teller)
                      {
                            printf("%d", uitkomst2[teller2]);
                            teller2 = teller2 + 1 ;
                      }
       return EXIT_SUCCESS;
            }
    /*c----------  end of function main  ---------- */
    It working now only not good.
    4 gets now the answer 10 where it must be 100

    Roelof

  2. #32
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's because you divide by 2 before trying to convert the number (as the first line in your while loop). Don't do that.

  3. #33
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    That's because you divide by 2 before trying to convert the number (as the first line in your while loop). Don't do that.
    Old joke....

    A guy walks into the doctor's office and hooks his thumb over the edge of the desk.
    He pulls out a claw hammer and whacks himself right on the thumb.

    "Doc, it hurts like heck every time I do that"

    The doctor replied: "Then don't do that!".

  4. #34
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I changed it to this:
    Code:
    /*
     *  * =====================================================================================
     *   
     *          Filename:  test2.c
     *    
     *          Description:  conventer from dec to 0101
     *       
     *          Version:  1.0
     *          Created:  14-05-11 10:03:25
     *          Revision:  none
     *          Compiler:  gcc
     *            
     *          Author:  Dr. Fritz Mehner (mn), [email protected]
     *          Company:  FH Südwestfalen, Iserlohn
     *               
     * =====================================================================================
      */
    
    #include        <stdio.h>
    #include        <stdlib.h>
    
    int
    main ( int argc, char *argv[] )
            {
                int nummer, uitkomst, teller;
                char uitkomst2[10];
                nummer = 2 ;
                uitkomst = 2 ;
                teller = 0 ;
                while (uitkomst > 0)
                    {
                            uitkomst2[teller++]= uitkomst%2;
                            uitkomst = nummer /2 ;
                            nummer = uitkomst;
                            printf ("teller:%d  deling:%d uitkomst deling:%d  \n",teller, uitkomst%2, uitkomst);
                    }
                printf ("teller is nu %d", teller);
                while (teller >  0)
                      {
                            printf("%d", uitkomst2[teller]);
                            teller = teller - 1;
                      }
       return EXIT_SUCCESS;
            }
    /*c----------  end of function main  ---------- */
    But i see something wierd.
    The output is now this
    Code:
    teller:1  deling:1 uitkomst deling:1  
    teller:2  deling:0 uitkomst deling:0  
    teller is nu 241
    So the loop is executed right but after that teller is a complete other number.

    Roelof

  5. #35
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's because you forgot to print a new line after the number. teller is 2, then you print some uitkomst2 after it.

  6. #36
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    A bit more simplification...
    Code:
                while (uitkomst > 0)
                    {
                            uitkomst2[teller++]= uitkomst%2;
                            uitkomst = nummer /2 ;
                            nummer = uitkomst;
                            printf ("teller:%d  deling:%d uitkomst deling:%d  \n",teller, uitkomst%2, uitkomst);
                    }
    You can eliminate the intermediate variable uitkomst, which you are only using to flip values in nummer by operating directly on nummer, like this...
    Code:
    while (nummer > 0)
       { uitkomst2[teller++] = nummer %2;
         nummer /= 2 ; }
    Again... you should always try to find the simplest solutions to a problem. I appreciate that this is all new to you, but the best time to form good habits is *before* you form the bad ones...
    Last edited by CommonTater; 05-15-2011 at 12:06 PM.

  7. #37
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I found this answer which is working :
    Code:
    /*
     *  * =====================================================================================
     *   
     *          Filename:  test2.c
     *    
     *          Description:  conventer from dec to 0101
     *       
     *          Version:  1.0
     *          Created:  14-05-11 10:03:25
     *          Revision:  none
     *          Compiler:  gcc
     *            
     *          Author:  Dr. Fritz Mehner (mn), [email protected]
     *          Company:  FH Südwestfalen, Iserlohn
     *               
     * =====================================================================================
      */
    
    #include        <stdio.h>
    #include        <stdlib.h>
    
    int
    main ( int argc, char *argv[] )
            {
                int nummer, uitkomst, teller;
                char uitkomst2[10];
                nummer = 100 ;
                uitkomst = 100 ;
                teller = 0 ;
                while (uitkomst > 0)
                    {
                            uitkomst2[teller++]= uitkomst%2;
                            uitkomst = uitkomst /2 ;
                    }
                 printf ("Het getal %d is binair ", nummer);
                 while (teller >  0)
                      {
                            printf("%d", uitkomst2[teller-1]);
                            teller = teller - 1;
                      }
       return EXIT_SUCCESS;
            }
    /*c----------  end of funcrion main  ---------- */
    Roelof

    Edit 1 :

    I saw later your solution.
    I elimated number in the loops.
    I use it only for displaying the answer.
    But thanks for the help
    Last edited by roelof; 05-15-2011 at 12:21 PM.

  8. #38
    Registered User
    Join Date
    May 2010
    Posts
    230
    CommonTater

    Is my programm good or are there more things I have to learn,
    If not, I will try to change this to converting to a octal number.

    Roelof

  9. #39
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    CommonTater

    Is my programm good or are there more things I have to learn,
    If not, I will try to change this to converting to a octal number.

    Roelof
    Well... my thought would be there are always things we ALL have to learn. Programming is a never ending learning opportunity, a gift to those who love a challenge and the nemisis of those who don't.

    Your code is working, which for a chapter 1 practice exercise is a pretty good achievement, especially in such a short time. You can be proud of it.

    Improvement wise, not much left to work on... 100%....

    So, on to the next chapter... don't stray too far from the ordered learning of your texts, they are organized in such a way as to lead you through the process in a well intentioned manner.

    If you're thinking of changing it to print octal... Look at the formatters for printf()... I think you'll find you can reduce your code to about 5 lines. Seriously... they beat you to it...

    Not too long ago there was a guy here who was trying to do decimal to hexidecimal conversion. This went on for several pages and lots of the guys were chipping in... Finally seeing that it was going noplace, and from my penchant for simplicity, I posted something very close to this...

    Code:
    #include <stdio.h>
    
    int main (void)
      { unsigned int number;
    
         scanf("%d", &number);
         printf("\n0x%8X\n",number);
    
         return 0; }
    The final conclusion was: "I'll bet that teacher never gives that assignment again." LOL...

    Keep working my friend, you're doing just fine!
    Last edited by CommonTater; 05-15-2011 at 02:35 PM.

  10. #40
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I know that printf can do the job.
    But because in chapter 1 this is not in the text I'm not allowed to use it.
    But when looking at the prevoius exercises I have a idea how to do it.
    And I think I have to adapt this source a little bit.

    Roelof

  11. #41
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I adapted the script so it looks like this:
    Code:
     *   
     *          Filename:  test2.c
     *    
     *          Description:  conventer from dec to 0101
     *       
     *          Version:  1.0
     *          Created:  14-05-11 10:03:25
     *          Revision:  none
     *          Compiler:  gcc
     *            
     *          Author:  Dr. Fritz Mehner (mn), [email protected]
     *          Company:  FH Südwestfalen, Iserlohn
     *               
     * =====================================================================================
      */
    
    #include        <stdio.h>
    #include        <stdlib.h>
    
    int
    main ( int argc, char *argv[] )
            {
                int nummer, uitkomst, uitkomst3, uitkomst4, teller;
                char uitkomst2[10] ;
                nummer = 21 ;
                uitkomst = 21 ;
                teller = 0 ;
                while (uitkomst > 0)
                    {
                            uitkomst3 = (uitkomst % 16) ;
                            if (uitkomst3 >=10)
                            {
                             uitkomst4 = uitkomst3 + 55 ;
                            }
                            else
                             {
                               uitkomst4 = uitkomst3 + 48 ;
                                    }
    
                            uitkomst2[teller++]= uitkomst4;
                            uitkomst = uitkomst /16 ;
                    }
                 printf ("Het getal %d is octal ", nummer);
                 while (teller >  0)
                      {
                            printf("%c", uitkomst2[teller-1]);
                            teller = teller - 1;
                      }
       return EXIT_SUCCESS;
            }
    and Commentator thanks that you would be a friend.

    Roelof

  12. #42
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You might get a better mark, and you might solve minor bugs more easily, if you learn to indent code properly.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to Binary conversion help
    By tru.cutru in forum C Programming
    Replies: 3
    Last Post: 07-08-2008, 10:17 PM
  2. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 09:51 PM
  3. Decimal to binary conversion
    By blckspder in forum C Programming
    Replies: 5
    Last Post: 04-07-2005, 12:38 PM
  4. decimal to binary conversion
    By noob2c in forum C Programming
    Replies: 4
    Last Post: 05-29-2003, 08:07 PM
  5. Binary to decimal conversion help!
    By matrism in forum C Programming
    Replies: 4
    Last Post: 03-25-2002, 12:22 PM