# Recursive Power function

• 12-03-2002
MethodMan
Recursive Power function
Code:

``` #include <stdio.h> #define BAD -1 int Power(int x, int n);    main() {         Power(3,20); } int Power(int x, int n)    {         int middle;         if(n < 0) exit(BAD);         else if(n == 1) return x;         else if(n == 0) return 1;          else if( (n % 2) == 0) // even         {                 middle = n/2;                 printf("In even middle = %d\n", middle);                 return Power(x, middle) * Power(x, middle);         }         else         if( (n % 2) == 1) //odd         {                 middle = n/2;                 printf("In odd middle = %d\n", middle);                 return Power(x, middle) * Power(x, middle) * x;         } }```
I am trying to get it to do something like this, if it is (x^10) it will do (x^5)*(x^5), and if x^11 it will do (x^5)*(x^5)*x.

It works for Power(3, (0->4)), but once I make the power higher than 4 or 5 it will give me the incorrect answer. I dont know why it works up to a limit of numbers, and then starts to give me wrong answers.

Thanks.
• 12-03-2002
Cshot
It works fine for the most part. The reason why it's not working sometimes is because for example, some values are too large to fit in an integer. That's why your results will appear incorrect.
• 12-03-2002
beege31337
It's working on my compiler.
• 12-03-2002
MethodMan
Quote:

Originally posted by beege31337
It's working on my compiler.
Really, what does Power(3,10) return for you?

I never print out the result, on a unix server I use echo \$? to see what the program returns.
• 12-03-2002
MethodMan
I tried it on my home computer, on my Borland compiler and it gave the correct answer. Dont know what was happening before.

Thanks for the help!:)
• 12-03-2002
beege31337
i added this to main to print the result
printf("%d\n",Power(3,10));

the output was :

In even middle = 5
In odd middle = 2
In even middle = 1
In even middle = 1
In odd middle = 2
In even middle = 1
In even middle = 1
59049
• 12-03-2002
Hammer
Quote:

Originally posted by MethodMan
I tried it on my home computer, on my Borland compiler and it gave the correct answer. Dont know what was happening before.

My Borland compiler (5.5) has an int which is the size of a long int, so the max number you fit in it is quite large. You can compare your 2 compilers by printing something like this to see if that's your problem:
Code:

```#include <limits.h> ... printf ("INT_MAX is %d\n", INT_MAX);```
• 12-07-2002
beege31337
Quote:

Originally posted by MethodMan
I never print out the result, on a unix server I use echo \$? to see what the program returns.

You should use return in main for 0 or an error code, otherwise you are just defeating the point.
• 12-08-2002
Shiro
Also return 0 is not always correct, since some OS'es use 0 as representation of false. Better would be to use the constants as defined in the ANSI C standard:

EXIT_SUCCESS and EXIT_FAILURE
• 12-08-2002
moi
Quote:

Originally posted by Shiro
Also return 0 is not always correct, since some OS'es use 0 as representation of false. Better would be to use the constants as defined in the ANSI C standard:

EXIT_SUCCESS and EXIT_FAILURE

ansi C says 0 is synonomous for EXIT_SUCCESS
• 12-08-2002
Shiro
My copy of the ANSI C standard says (page 315):

Quote:

If the value of status is zero or EXIT_SUCCESS,
Note the _or_. There are OS'es on which EXIT_SUCCESS is implemented as 1, therefore it is recommended to use EXIT_SUCCESS for portability.
• 12-08-2002
moi
Quote:

Originally posted by Shiro
My copy of the ANSI C standard says (page 315):

Note the _or_. There are OS'es on which EXIT_SUCCESS is implemented as 1, therefore it is recommended to use EXIT_SUCCESS for portability.

or means or, no more, no less. 0 is synomous with success, period. if an implementation doesnt do this, it is flawed.
• 12-08-2002
Shiro
It is C99.

Perhaps I have interpreted it wrong. I interpreted it as:

If the value of status is zero or EXIT_SUCCES, where the value of EXIT_SUCCES is defined at the environment and can be equal to 0 but is not necessarily zero, then termination is succesfull.

In my interpretation it was possible that on a certain environment the value of EXIT_SUCCES is defined non-zero. So if you use zero, then the environment interprets it as not succesfull termination and if EXIT_SUCCES, which is then defined as non-zero, it interprets it as succesfull. And therefore on when using this environment and an environment on which EXIT_SUCCES is defined as zero, one would get no problems with portability when using EXIT_SUCCES.