#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Posts
    44
    Rep Power
    8

    C: Number system conversion


    The following program converts decimal number into any base (base <= 10):
    Code:
    #include<stdio.h>
    
    void conversion(unsigned num, unsigned base)
    {
       if(num % base)
           conversion(num / base, base);
       printf("%u", num % base);
    }
    
    int main()
    {
       unsigned num,base;
       printf("num=");
       scanf("%u",&num);
       do
       {
           printf("base=");
           scanf("%d",&base);
       }
       while(base < 2 || base > 10);
       conversion(num,base);
       return 0;
    }
    How to modify recursive function conversion() into iterative solution?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Posts
    44
    Rep Power
    8
    Some errors in the previous post:
    Code:
    #include <stdio.h>
    void conversion(unsigned num, unsigned base)
    {
      if (num / base)
        conversion(num / base, base);
      printf("%u", num % base);
    }
    int main()
    {
      unsigned num, base;
      printf("num=");
      scanf("%u", &num);
      do{
         printf("base=");
         scanf("%u", &base);
        }
      while (base < 2 || base > 10);
      conversion(num, base);
      return 0;
    }
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,897
    Rep Power
    509
    Thanks, but can you convert to base factorial? Executable Iverson notation.
    Code:
       5 4 3 2 #: 111
    4 2 1 1
       (4 * 4*3*2) + (2 * 3*2) + (1 * 2) + (1 * 1)
    111
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Posts
    44
    Rep Power
    8
    Code:
    #include <stdio.h>
    
    int main()
    {
      int x,y[10];
      int i=2;
      printf("Enter a number: ");
      scanf("%d", &x);
      while(x)
      {
        y[i-2]=x%i;
        x/=i++;
      }
      printf("Faktoradic: ");
      for (; i>2; printf("%d", y[i-3]),i--);
      return 0;
    }

    Comments on this post

    • b49P23TIvg agrees : now generalize
  8. #5
  9. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,897
    Rep Power
    509
    awesome!
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2015
    Posts
    1
    Rep Power
    0
    Here is a non recursive implementation using a stack.

    I put it side by side with your recursive implementation. No need to recurse if you can store the results in a stack.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define STACK_SIZE 50
    
    typedef struct {
      char* values[STACK_SIZE];
      int top;
    } OutputStack;
    
    int   main();
    void  init(OutputStack *s);
    int   isEmpty(OutputStack *s);
    int   full(OutputStack *s);
    void  push(OutputStack *s, char* output);
    char* pop(OutputStack *s);
    void  printOutputStack(OutputStack *s);
    void  testOutputStack();
    
    void conversionRecursive(unsigned num, unsigned base) {
      if(num % base) {
        conversionRecursive(num / base, base);
      }
      printf("num=%d; base=%d; recursive conversion = %u\n", num, base, num % base);
    }
    
    void conversionNoRecursion(unsigned num, unsigned base, OutputStack *s) {
      char* buff_a = (char*) calloc(100,sizeof(char));
      while (num % base) {
        sprintf(buff_a, "num=%d; base=%d; conversion no recursion = %u\n", num, base, num % base);
        num = num / base;
        push(s, buff_a);
        buff_a = (char*) calloc(100,sizeof(char));
      }
    
      sprintf(buff_a, "num=%d; base=%d; conversion no recursion = %u\n", num, base, num % base);
      push(s, buff_a);
    
      while (!isEmpty(s)) {
        printf("%s", pop(s));
      }
    }
    
    int main()
    {
      //testOutputStack();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
      unsigned num,base;
    
      printf("num=");
      scanf("%u",&num);
       do
         {
           printf("(between [2,10] inclusive)\n");
           printf("base=");
           scanf("%d",&base);
         }
       while((base < 2 || base > 10));
       int orig_num = num;
       int orig_base = base;
       conversionRecursive(num,base);
       printf("========================================================================\n");
       OutputStack* s = (OutputStack*) malloc(sizeof(OutputStack));
       init(s);
       conversionNoRecursion(num, base, s);
       return 0;
    }
    
    // Stack utility functions.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
    
    void init(OutputStack *s) {
      s->top = 0;
    }
    
    int isEmpty(OutputStack *s) {
      return s->top == 0;
    }
    
    int full(OutputStack *s) {
      return s->top >= STACK_SIZE;
    }
    
    void push(OutputStack *s, char* output) {
      if (!full(s)) {
        s->values[s->top] = output;
        s->top++;
      }
    }
    
    char* pop(OutputStack *s) {
      if (isEmpty(s)) {
        return "empty output stack.";
      }
      return s->values[--s->top];
    }
    
    void printOutputStack(OutputStack *s) {
      int i=0;
      if (s->top == 0) {
        printf("Output stack is empty.");
      } else {
        printf("Output stack:\n");
        while (i < s->top) {
          printf("%d: %s\n", i++, s->values[i]);
        }
      }
      printf("\n");
    }
    
    void testOutputStack() {
      OutputStack* stack = (OutputStack*) malloc(sizeof(OutputStack));
      init(stack);
    
      push(stack, "The fist will be last");
      push(stack, "The 2nd will be 2nd to last");
      push(stack, "The third one is already done. First!");
      printOutputStack(stack);
    
    
      printf("** pop! ** => %s\n", pop(stack));
      printOutputStack(stack);
    
      printf("** pop! ** => %s\n", pop(stack));
      printOutputStack(stack);
    
      printf("** pop! ** => %s\n", pop(stack));
      printOutputStack(stack);
    
      printf("\n\n");
    
    }

IMN logo majestic logo threadwatch logo seochat tools logo