Thread: denary to binary problems

  1. #1
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    Unhappy denary to binary problems

    I've attempted to create a program which converts denary to binary (normal numbers to binary) however i think I'm having trouble towards the end when i print out the binary numbers as they are all print out 0000000.0000000. when i want it to print out the number which is held in each array, therefore printing out the binary equivalent of the denary number. Any help is greatly appreciated. Sorry for the unnecessary use of pointers to structures, they're just for practice really.

    If you are unfamiliar with how the conversion works check out the bottom of this page for a concise explenation.

    http://www.helpwithpcs.com/courses/binary-numbers.htm



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define DIVIDE 2
    #define THRESH 5 /*decision between a 1 or 0.*/
    
    struct numbers
    {
        double int_part; /*where the decimal is held to determine binary digit*/
        double first;
        double usernum;
        double dd; /*outcome of division*/ 
        unsigned int size; /*the size of the array*/
        unsigned int start;
        long collum[];/*put size in here*/
    };
    
    int main (int argc, char *argv[])
    {
    
    if (argc != 2)    {
            printf("Usage: ./denbin <number>.\n");
            return (EXIT_FAILURE);
            }
        
    struct numbers *n;
    n = (struct numbers *) malloc (sizeof(struct numbers));         
    n->size = 0; 
    n->dd = 0;
    n->usernum = atof (argv[1]);
        
        while (n->usernum != 0)
        {        
        n->usernum = n->usernum / DIVIDE; /*start dividing it*/
        n->first = (int)(modf(n->usernum, &n->int_part) * 10 + 0.5);
        
            if (n->first < THRESH)
            {
                ++n->size;
                n->collum[n->size] = 1;
            }
            
            else
            {
                ++n->size;
                n->collum[n->size] = 0;
            }    
            
            /*this is used so that the values each part of the array holds can 
             be reversed and printed out, therefore showing the entire binary number correctly*/
            for (n->start = 0; n->start < n->size; ++n->start)  
            printf("%f", n->collum[n->start]);
            //free(n);
        }
    return (EXIT_SUCCESS);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, where do you malloc memory for this collum[] array?

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    i think you're overengineering the whole thing. the logic is very simple.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void print_binary(int x);
    
    int main()
    {
        int x;
        printf("enter a number> ");
        scanf("%d", &x);
        print_binary(x, 8);
        printf("\n");
        return EXIT_SUCCESS;
    }
    
    void print_binary(int x, int n)
    {
        if (n == 0) return;
        print_binary(x/2, n-1);
        printf("%c", (x%2 == 0) ? '0' : '1');
    }

  4. #4
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    I thought it was allocated when i allocated the entire structure.

    Code:
    n = (struct numbers *) malloc (sizeof(struct numbers));

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by redruby147 View Post
    I thought it was allocated when i allocated the entire structure.

    Code:
    n = (struct numbers *) malloc (sizeof(struct numbers));
    That is an incorrect belief; it would have been a correct belief if collum was a proper array and not a struct hack. Since it is a struct hack, you will have to add the space for it yourself.

  6. #6
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    Thanks for the feedback and code . But the program only allows up to 8 digits.

    Code:
    print_binary(x, 8);
    And if increased there are a lot of preceding zeros. I know it may be a bit over the top but i'd like to know whats specifically wrong with my code.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by redruby147 View Post
    But the program only allows up to 8 digits.

    Code:
    print_binary(x, 8);
    And if increased there are a lot of preceding zeros. I know it may be a bit over the top but i'd like to know whats specifically wrong with my code.
    To start from the back:
    You are printing the number before it's finished. Your indentation isn't very good (to say the least). You are comparing a floating point (double) value with zero - it takes an awful long time to get there - for the integer part of the double, maybe comparing >= 1 would make more sense. To get the decimals out after that will of course be a challenge, but that's a different matter, I guess.

    print_binary, as far as I can tell should work for any number up to 31 bits (assuming modern OS and compiler).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading a binary file.
    By earth_angel in forum C++ Programming
    Replies: 2
    Last Post: 07-08-2005, 10:32 AM
  2. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:54 AM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 06:45 PM