446,232 Members | 1,733 Online
Need help? Post your question and get tips & solutions from a community of 446,232 IT Pros & Developers. It's quick & easy.

# Function to recursively change base

 P: n/a I'm trying to "Write a function itob(n, s, b) that converts the integer n into a base b character represeantation in the string s. In particular, itob(n, s, 16) formats n as a hexadecimal integre in s. itob must be recursive. " It's #3-5 in the C Programming language by KandR. I also made a main to test it: void itob(int n, char s[], int b); main () { /*void itob(int n, char s[], int b);*/ int b[]={2, 6, 8, 10, 16, 32}; int p[]={2, 6, 8, 10, 16, 32, 1239757}; char s[BUFSIZ]; int i, j; for(i=0; i<7; i++){ for (j=0; j<6; j++){ itob(p[i], s, b[j]); printf("s is %d\n",s); printf("*************\n"); } } } void itob(int n, char s[], int b) { int i, sign; i=0; printf("number is %d\n",n); printf("base is %d\n",b); if ((sign = n) <0 ) n = -n; if (n/b>0) itob(n/b,s,b); s[i++]=n%b; s[i]='\0'; printf("grrrr s[i++]is %d\n",s[i]); } But this is the sort of output I get (just a very small sample) ************* number is 1239757 base is 16 number is 77484 base is 16 number is 4842 base is 16 number is 302 base is 16 number is 18 base is 16 number is 1 base is 16 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 s is 134510020 ************* number is 1239757 base is 32 number is 38742 base is 32 number is 1210 base is 32 number is 37 base is 32 number is 1 base is 32 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 s is 134510020 I just don't enjoy recursion :,[ Feb 21 '07 #1
4 Replies

 P: n/a If the problem is to do with the "grrrr .... is 0" appearing in the output, it is because in your recursive function, it is outside of your if statement. When the recursion comes back up, it is going to execute it. On Feb 21, 10:33 am, ComaGr...@gmail.com wrote: I'm trying to "Write a function itob(n, s, b) that converts the integer n into a base b character represeantation in the string s. In particular, itob(n, s, 16) formats n as a hexadecimal integre in s. itob must be recursive. " It's #3-5 in the C Programming language by KandR. I also made a main to test it: void itob(int n, char s[], int b); main () { /*void itob(int n, char s[], int b);*/ int b[]={2, 6, 8, 10, 16, 32}; int p[]={2, 6, 8, 10, 16, 32, 1239757}; char s[BUFSIZ]; int i, j; for(i=0; i<7; i++){ for (j=0; j<6; j++){ itob(p[i], s, b[j]); printf("s is %d\n",s); printf("*************\n"); } } } void itob(int n, char s[], int b) { int i, sign; i=0; printf("number is %d\n",n); printf("base is %d\n",b); if ((sign = n) <0 ) n = -n; if (n/b>0) itob(n/b,s,b); s[i++]=n%b; s[i]='\0'; printf("grrrr s[i++]is %d\n",s[i]); } But this is the sort of output I get (just a very small sample) ************* number is 1239757 base is 16 number is 77484 base is 16 number is 4842 base is 16 number is 302 base is 16 number is 18 base is 16 number is 1 base is 16 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 s is 134510020 ************* number is 1239757 base is 32 number is 38742 base is 32 number is 1210 base is 32 number is 37 base is 32 number is 1 base is 32 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 s is 134510020 I just don't enjoy recursion :,[ Feb 21 '07 #2

 P: n/a On Feb 20, 8:42 pm, "Klarth"

 P: n/a On Feb 20, 5:33 pm, ComaGr...@gmail.com wrote: I'm trying to "Write a function itob(n, s, b) that converts the integer n into a base b character represeantation in the string s. In particular, itob(n, s, 16) formats n as a hexadecimal integre in s. itob must be recursive. " It's #3-5 in the C Programming language by KandR. I also made a main to test it: void itob(int n, char s[], int b); main () { /*void itob(int n, char s[], int b);*/ int b[]={2, 6, 8, 10, 16, 32}; int p[]={2, 6, 8, 10, 16, 32, 1239757}; char s[BUFSIZ]; int i, j; for(i=0; i<7; i++){ for (j=0; j<6; j++){ itob(p[i], s, b[j]); printf("s is %d\n",s); printf("*************\n"); } } } void itob(int n, char s[], int b) { int i, sign; i=0; printf("number is %d\n",n); printf("base is %d\n",b); if ((sign = n) <0 ) n = -n; if (n/b>0) itob(n/b,s,b); s[i++]=n%b; s[i]='\0'; printf("grrrr s[i++]is %d\n",s[i]); } But this is the sort of output I get (just a very small sample) ************* number is 1239757 base is 16 number is 77484 base is 16 number is 4842 base is 16 number is 302 base is 16 number is 18 base is 16 number is 1 base is 16 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 s is 134510020 ************* number is 1239757 base is 32 number is 38742 base is 32 number is 1210 base is 32 number is 37 base is 32 number is 1 base is 32 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 grrrr s[i++]is 0 s is 134510020 I just don't enjoy recursion :,[ I wrote a function that does something similar. But I decided no sense hauling that bulky string all over the place. And I restricted myself to using only putchar() as a library function call within the bit of stuff that does the actual work. Of course, it would not satisfy an assignment that asked you to stuff the answer into a string. P.S. If you use 64 bit integers, then your string must be a minimum of 65 characters in length. But I am not sure I see the efficacy of making it BUFSIZ characters. So I have an assignment for you. Comment this code and tell me what each line of it is doing. After you have done that, it will be easy for you to write code that does exactly what you want. #include #include #include static const char symbol_table[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', }; void emit_digits(unsigned long long whomper, unsigned base) { unsigned long long digit = whomper % base; assert(base <= 36); /* Else, you're going to need a new symbol set */ assert(base >= 2); /* Else you'll have a heck of a long wait */ whomper /= base; if (whomper) emit_digits(whomper, base); putchar(symbol_table[digit]); } void change_base(unsigned long long whomper, unsigned base) { emit_digits(whomper, base); putchar('\n'); } #ifdef UNIT_TEST int main(void) { unsigned long long two_to_the_31 = 2147483648; unsigned long long some_other_thing; unsigned base; size_t i; unsigned j; change_base(two_to_the_31, 2); change_base(two_to_the_31, 16); change_base(two_to_the_31, 32); putchar('\n'); srand((unsigned) time(NULL)); for (i = 1; i < 10; i++) { some_other_thing = rand(); some_other_thing *= rand(); for (j = 2; j <= 36; j++) { change_base(some_other_thing, j); } putchar('\n'); } putchar('\n'); return 0; } #endif Feb 21 '07 #4

 P: n/a On Feb 20, 5:33 pm, ComaGr...@gmail.com wrote: I'm trying to "Write a function itob(n, s, b) that converts the integer n into a base b character represeantation in the string s. In particular, itob(n, s, 16) formats n as a hexadecimal integre in s. itob must be recursive. " It's #3-5 in the C Programming language by KandR. I also made a main to test it: void itob(int n, char s[], int b); main () { /*void itob(int n, char s[], int b);*/ int b[]={2, 6, 8, 10, 16, 32}; int p[]={2, 6, 8, 10, 16, 32, 1239757}; char s[BUFSIZ]; Comment the following out > int i, j; for(i=0; i<7; i++){ for (j=0; j<6; j++){ itob(p[i], s, b[j]); printf("s is %d\n",s); printf("*************\n"); } } Replace with: itob(3, s, 16); printf("s is %d\n", s); } void itob(int n, char s[], int b) { int i, sign; i=0; printf("number is %d\n",n); printf("base is %d\n",b); if ((sign = n) <0 ) n = -n; if (n/b>0) itob(n/b,s,b); s[i++]=n%b; s[i]='\0'; Replace printf("grrrr s[i++]is %d\n",s[i]); with printf("grrrr i is %d and s[i]is %d\n", i, s[i]); > } Then figure out what should be happening, and what actually is. (You may need some more printfs, or you may need a debugger, or whatever.) Fix problems. Then replace itob(3, s, 16); with itob(12, s, 16); Figure out what should be happening, and what actually is. Fix problems. Then with itob(20, s, 16); Figure out what should be happening, and what actually is. Fix problems. At this point, comment back in your original code, and it should work. If not, find the smallest number that doesn't work and put in a single line as I've shown, then track it down. Michael Feb 21 '07 #5

### This discussion thread is closed

Replies have been disabled for this discussion.