Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hey ! I recognize a famous homework I had in first year of my school too (@Epitech students : don't copy/paste the following code, try to come up with your own solution, it's for your own good ^^)</p> <p>The solution to your problem is to perform the problem in a recursive way :</p> <pre><code>void my_putnbr_base(int num, int base) { int start; int remainder; remainder = num % base; start = (num - remainder) / base; if (start != 0) my_putnbr_base(start, base); if (remainder &gt;= 10) printf("%c", remainder + 55); else printf("%d", remainder); } </code></pre> <p>Does your homework specifies that it should only work with positives numbers ? If not, it's easy to include the negative numbers handling :</p> <pre><code>void my_putnbr_base(int num, int base) { int start; int remainder; if (num &lt; 0) { putchar('-'); my_putnbr_base(-num, base); } else { remainder = num % base; start = (num - remainder) / base; if (start != 0) my_putnbr_base(start, base); if (remainder &gt;= 10) printf("%c", remainder + 55); else printf("%d", remainder); } } </code></pre> <p>@arno : that's true, because the exemple code is using ASCII table. If we want something trully flexible we need the base in parameter. For example :</p> <pre><code>&gt;&gt; my_putnbr_base(4242, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") 39U &gt;&gt; my_putnbr_base(42, "0123456789ABCDEF") 2A </code></pre> <p>this implements the example :</p> <pre><code>void my_putnbr_base(int num, char *base) { int start; int remainder; int len; len = strlen(base); if (num &lt; 0) { putchar('-'); my_putnbr_base(-num, base); } else { remainder = num % len; start = (num - remainder) / len; if (start != 0) my_putnbr_base(start, base); printf("%c", base[remainder]); } } </code></pre> <p>I hope it solves your problem !</p> <p>edit: I didn't read correctly ^^ You are not allowed to use functions, so recursion is out of the question... Here is an interative way, you can put this in a main(). You can improve this code by adding the negative numbers handling and flexible bases, as I showed you :)</p> <pre><code>int my_putnbr_base_it(int num, int base) { unsigned int quotient = 1; unsigned int remainder; while ((num / quotient) &gt;= base) quotient *= base; while (quotient) { if ((remainder = (num / quotient) % base) &lt; 10) printf("%d", remainder); else printf("%c", 55 + remainder); quotient /= base; } return (0); } </code></pre> <p>Hope it solves everything now !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload