Converting decimals to individuals

This is a discussion on Converting decimals to individuals within the C Programming forums, part of the General Programming Boards category; i understand in Java that code would be trivial.. can you give me an example in C, how I would ...

  1. #16
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    i understand in Java that code would be trivial.. can you give me an example in C, how I would implement that not directly (since it just gives it away) but something similar? thank you!

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your description of what you're trying to do is fairly baffling. Have you been reading up on Rube Goldberg, perhaps?

    If you want to pick out each digit from the integer, you can do that, by using the characteristics of the base 10 numbering system:

    Roughly:

    Code:
    while(yourInt > 0)  {
      digit1 = yourInt  % 10
      print digit1
      yourInt /= 10
    }
    If you want something else, give me an example, of the typical input, and desired output.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    For which, the recursion? You're pretty much there:
    Code:
    if (number < 9)
        print number
    else
        digit = number %10;
        same_function(number/10);
        print number

  4. #19
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Code:
    void decout (unsigned long number, FILE * stream)
    {
    	int converted[64];
        long digit_count = 0;
        int base = 10;
        int index = 0;
    
    
        while(digit_count != 0)
        {
        	converted[index] = number % base;
            number = number / base;
            index++;
        }
    
        --index;  /* back up to last entry in the array */
        for( ; index>=0; index--) /* go backward through array */
        {
     	 printf("%c", digits[converted[index]]);
        }
    }

  5. #20
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    how is this?

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That seems reasonable.

  7. #22
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    but i feel like 64 is a magical number..but that's the only way I got it to work.. what does it represent?

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    64 is the length of your array, duh. Any number works that is at least 10, since (usually) an unsigned long cannot exceed 4billion.

  9. #24
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    so i can just replace 64 with base right? cause I also have a hexout function.

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, no. You are replacing it with 10 because 4billion requires ten decimal digits. That same 4billion requires a mere eight hexadecimal digits.

  11. #26
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Code:
    void decout (unsigned long number, FILE * stream)
    {
    	int converted[64];
        long digit_count = 0;
        int base = 10;
        int index = 0;
    
    
        while(digit_count != 0)
        {
        	converted[index] = number % base;
            number = number / base;
            index++;
        }
    
        --index;  /* back up to last entry in the array */
        for( ; index>=0; index--) /* go backward through array */
        {
     	 printf("%c", digits[converted[index]]);
        }
    }
    I have a question here..how would I do this without using printf in the for-loop?

    thanks!

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Using putc? Isn't that where you started?

  13. #28
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Code:
        //base-conversion algorithm
        while(number != 0)
        {
        	converted[index] = number % base;
            number = number / base;
            index++;
        }
        // back up to last entry 
        --index;  
        // go backward through array
        for( ; index>=0; index--) 
        {
     	 fputc(digits[converted[index]],stream);
        }
    is this reasonable?

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-20-2009, 07:39 AM
  2. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 03:13 PM
  3. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  4. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM

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