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!
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!
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:
If you want something else, give me an example, of the typical input, and desired output.Code:while(yourInt > 0) { digit1 = yourInt % 10 print digit1 yourInt /= 10 }
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
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]]); } }
how is this?
but i feel like 64 is a magical number..but that's the only way I got it to work.. what does it represent?
64 is the length of your array, duh. Any number works that is at least 10, since (usually) an unsigned long cannot exceed 4billion.
so i can just replace 64 with base right? cause I also have a hexout function.
Well, no. You are replacing it with 10 because 4billion requires ten decimal digits. That same 4billion requires a mere eight hexadecimal digits.
I have a question here..how would I do this without using printf in the for-loop?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]]); } }
thanks!
is this reasonable?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); }