Function to recursively change base

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ComaGrrrl@gmail.com

    #1

    Function to recursively change base

    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 :,[

  • Klarth

    #2
    Re: Function to recursively change base

    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 :,[

    Comment

    • ComaGrrrl@gmail.com

      #3
      Re: Function to recursively change base

      On Feb 20, 8:42 pm, "Klarth" <kah....@gmail. comwrote:
      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.
      >
      >
      Sorry I should have been more specific. I'm confused because I keep
      getting s is 134510020
      and the grr thing was just to see what was going in (or not going in)
      my array which is 0 for some reason when I'd like it to be number%base.

      Comment

      • user923005

        #4
        Re: Function to recursively change base

        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 <stdio.h>
        #include <assert.h>
        #include <time.h>

        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(uns igned 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(who mper, base);
        putchar(symbol_ table[digit]);
        }

        void change_base(uns igned long long whomper, unsigned base)
        {
        emit_digits(who mper, base);
        putchar('\n');
        }

        #ifdef UNIT_TEST
        int main(void)
        {
        unsigned long long two_to_the_31 = 2147483648;
        unsigned long long some_other_thin g;
        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_thin g = rand();
        some_other_thin g *= rand();
        for (j = 2; j <= 36; j++) {
        change_base(som e_other_thing, j);
        }
        putchar('\n');
        }
        putchar('\n');
        return 0;
        }
        #endif


        Comment

        • Michael

          #5
          Re: Function to recursively change base

          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

          Comment

          Working...