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); }