# denary to binary problems

• 02-09-2009
redruby147
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); }```
• 02-09-2009
tabstop
So, where do you malloc memory for this collum[] array?
• 02-09-2009
Meldreth
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'); }```
• 02-09-2009
redruby147
I thought it was allocated when i allocated the entire structure.

Code:

`n = (struct numbers *) malloc (sizeof(struct numbers));`
• 02-09-2009
tabstop
Quote:

Originally Posted by redruby147
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.
• 02-09-2009
redruby147
Thanks for the feedback and code :D. 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.
• 02-09-2009
matsp
Quote:

Originally Posted by redruby147
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