loop with divition

This is a discussion on loop with divition within the C Programming forums, part of the General Programming Boards category; im tring to code a program that will convert decimals in to binaries... but i need it to keep dividing ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    28

    loop with divition

    im tring to code a program that will convert decimals in to binaries... but i need it to keep dividing the numbers untill 1 or 0 or untill it cant divide anymore.. but i cant get it to work any help?

    Code:
    int binary( int decimal )
    {
        int x, y;
        char  remainder[10];
        char  converted[10];
    
        for( x = 0; x < decimal; x++ )
        {
              x = ( decimal / BASE_2 );
    
              *remainder = decimal % x;
              strcpy( converted, remainder );
              printf( "%d\n", converted[x] );
    
        }
    
       // return x;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Try using something like this instead

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    char *convert(unsigned int);
    
    int main(void)
    {
      printf("%s\n", convert(150));
      return 0;
    }
    
    char *convert(unsigned int num)
    {
      static char dest[33];
      int count;
      unsigned int base= UINT_MAX;
      int ignoreflag=1;
    
    
      for ( count=0; count<32 && num>0; count++, base /= 2)
      {
        if ( num >= base )
        {
          dest[count] = '0' + 1;
          num -= base;
          ignoreflag=0;
        }
        else
          if ( !ignoreflag )
            dest[count] = '0';
          else
            count--;
      }
      return dest;
    }
    Last edited by Thantos; 04-11-2004 at 12:21 AM.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    28
    thank you Thantos, but im still new to c.. do you mind making it a bit more simple for me if you dont mind please

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Sure, give me a couple mins to fix the error I found and add some comments.

    Edit ok here you go:
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    char *convert(unsigned int);
    
    int main(void)
    {
      char *converted;
      converted = convert (150); /* convert 150 to from base 10 to base 2 */
      printf("%s\n", converted); /* Print the string */
      return 0;
    }
    
    /* Returns the address of the static variable dest for use in the printf and whatnot */
    char *convert(unsigned int num)
    {
      static char dest[33]; /* The character array that will hold the converted value.
                               It is static so we can pass the address freely */
      int count;           /*  Counter to so we can go through the array */
    
      unsigned int base = (unsigned)INT_MAX + 1; /* The higheset possible power of 2 that can be stored in an unsigned number */
    
      /* Populate the dest array with characters, go until num is no longer greater than 1 or we run out of room in the array
       * (Should happen in a 32 bit system) */
      for ( count=0; count<32; count++, base /= 2)
      {
        if ( num >= base ) /* If the number is equal to or greater than the base then we can take one out of the number for that power */
        {
          dest[count] = '0' + 1; /* To represent 1 as a character we add 1 to the character value of 0 */
          num -= base; /* Subtract the base from the number */
        }
        else
          dest[count] = '0'; /* if the number wasn't greater than or equal to the base then we put 0 */
      }
      return dest; /* return the address of the array.  WARNING:  This array will be overwritten if you call convert() again */
    }
    Advance note: For more portability you can use the following lines instead:
    Code:
    static char dest[(sizeof(int) * CHAR_BIT) + 1]; 
    /* other code */
    for ( count=0; count<sizeof dest - 1; count++, base /= 2)
    /* rest of function */
    Last edited by Thantos; 04-11-2004 at 12:55 AM. Reason: Edit: add code

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    28
    thank you again

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    a function which converted from any base to any base, would probably be more useful.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    It actually only takes a few changes and that function can do any base conversion. This is due to the fact that to convert from base x to base y is actually a two step process. Step 1 is to convert from base x to internal represtation. Step 2 is to convert from internal represtation to base y. This function would be step 2.

    Edit (Because I can't sleep atm):
    Code:
    char *convert(unsigned num, unsigned base_num)
    {
      static char dest[(sizeof(int) * CHAR_BIT) + 1];
      int count, valcount;
      unsigned base;
      char basechars[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    
      if ( base_num > sizeof basechars - 1 || base_num < 2)
      {
        printf("Requested base is outside of the range.  Valid base ranges are 2 to %d\n", sizeof basechars - 1);
        return NULL;
      }
      base = getMaxValBase(base_num);
      for ( count=0; count<sizeof dest - 1 && num > 0 && base > 0; count++, base /= base_num)
      {
        for ( valcount=0; num >= base; valcount++)
          num -= base;
        dest[count] = basechars[valcount];
      }
      return dest;
    }
    
    unsigned getMaxValBase(unsigned base)
    {
      unsigned max = UINT_MAX / base;
      unsigned val = base;
      while ( val <= max)
        val *= base;
      return val;
    }
    Edit: Ok no idea why its putting a space between n and o but ignore it and realize that its not suppose to be there
    Last edited by Thantos; 04-11-2004 at 05:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21