-5

Write a function unsigned int pose (unsigned int val, unsigned int base), which returns a value val given in decimal notation for each given value val.

Value of val in the system can be construed with the base base. You can assume that base is always between 2 and 9, and that val is sufficiently small to on the target system to cause any number range violation. Examples:

• pose (3,2) = 11 // 310 = 1 · 2 + 1 · 1 = 12
• pose (5,5) = 10 // 510 = 1 · 5 + 0 · 1 = 105
• pose (19,5) = 34 // 1910 = 3 · 5 + 4 · 1 = 345
• pose (5,6) = 5 // 510 = 5 · 1 = 56
• pose (7,7) = 10 // 710 = 1 · 7 + 0 · 1 = 107
• pose (543,9) = 663 // 54310 = 6 · 9

2 + 6 · 9 + 3 · 1 = 6639 Please note the following rules: • Your output archive should contain exactly one file named "convert.c". This file defines the function pose () and possibly other calls to be made by pose () Functions. Do not specify a main () function, do not include a makefile. • You must not use loops (keywords for, while, goto). • You must not use global variables. • You may not use a library function.

2 Answers2

1

First few hints or approach on how to solve it:-

  • Just solve the problem first.
  • Use loops and local variables and whatever is needed.
  • Once you solved it. Then look for applying the constraints.

The thing pretty easily indicates that you got to use recursion. Now rest is pretty easy - just decide what you will do in one loop. And transfer it to another recursive call with proper parameters.

 unsigned int pose (unsigned int val, unsigned int base){
    unsigned int ret = 0;
    if(val)
        ret=base*pose(val/base,base)+(val%base);
    return ret;
}

Dissecting this code will reveal few things:-

  1. I am just doing the necessary step in each step. In loop solution basically a digit is extracted. Here I do the same.And the reduced subproblem is then solved by another call to pose.

  2. If you consider standard solution you will see the digits are extracted in reverse order. You don't want that. Instead of using another extra variable you should just use recursion to hold the result and do the necessary final operation with it. Reverse is done base*pose(val/base,base)+(val%base); using this specifically.

As discussed with Paul, the question is much more logical if we interpret it to simply "print the number in converted base b".

The solution will be similar like this:-The code next shown is of Paul's idea. Previous code is mine.

void pose (unsigned int val, unsigned int base){
    if(val) {
        pose(val/base,base);
        printf("%c", (val%base)+'0'); 
    }
}
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • The part "_which returns a value val given in __decimal__ notation for each given value val_" is non-sense (contradiction in terms). Either val is converted to `base` or not. Check for your self with 2 and 16 – Paul Ogilvie Nov 11 '17 at 14:11
  • Then the answer will be printed with decimal digits `0..base-1` (but not decimal notation). – Paul Ogilvie Nov 11 '17 at 14:13
  • I just realize the whole question/solution is non-sense as you are converting something and when you assign it to `ret` it gets converted to binary anyway. So the only possible solution is to _print_ the value in some base. Think about binary, octal, decimal, hex: it's all the same bits. – Paul Ogilvie Nov 11 '17 at 14:24
  • @PaulOgilvie.: Yes that's what I am trying to say...I dont know why OP wants this – user2736738 Nov 11 '17 at 14:24
  • @PaulOgilvie.: I felt good writing the solution..thats why said question is nice...what do you think..shoukd I delete it? – user2736738 Nov 11 '17 at 14:27
  • @PaulOgilvie.: Edited.Check once – user2736738 Nov 11 '17 at 14:35
  • @PaulOgilvie.: No need to delete...You can put it back. Thanks for the discussion. – user2736738 Nov 11 '17 at 14:39
1

Coderredoc's answer shows a nice solution using recursion. However, the only sensible interpretation of the question is to print val in base:

 void pose (unsigned int val, unsigned int base){
    if(val) {
        pose(val/base,base);
        printf("%c", (val%base)+'0');
    }
}


int main(void)
{
    pose (8,8);  printf("\n");
    pose (8,2);  printf("\n");
    pose (19,5); printf("\n");
    return 0;
}

Output:

10
1000
34
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41