Recursive Power function

This is a discussion on Recursive Power function within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #define BAD -1 int Power(int x, int n); main() { Power(3,20); } int Power(int x, int n) ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    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.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    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.
    Last edited by Cshot; 12-03-2002 at 05:52 PM.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    It's working on my compiler.

  4. #4
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    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.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  5. #5
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    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!
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  6. #6
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    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

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    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.

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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

  10. #10
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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
    hello, internet!

  11. #11
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    My copy of the ANSI C standard says (page 315):

    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. #12
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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.
    hello, internet!

  13. #13
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Recursive Ackermann's function - need help
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2002, 03:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21